From b5feedce961edd73404db28d6b646bd5f29edd6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E4=B8=89?= <940119273@qq.com> Date: Tue, 14 Dec 2021 09:17:13 +0000 Subject: [PATCH] =?UTF-8?q?!1735=20=E5=A2=9E=E5=BC=BA:=E5=A2=9E=E5=8A=A0ho?= =?UTF-8?q?me,end,insert,pageup,pgdown=E5=BF=AB=E6=8D=B7=E9=94=AE=E8=BF=90?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/CommandPanel/InputHotKeys.tsx | 107 ++++++++++-------- 1 file changed, 62 insertions(+), 45 deletions(-) diff --git a/src/UI/Components/CommandPanel/InputHotKeys.tsx b/src/UI/Components/CommandPanel/InputHotKeys.tsx index c2bb63fc5..2f37c908d 100644 --- a/src/UI/Components/CommandPanel/InputHotKeys.tsx +++ b/src/UI/Components/CommandPanel/InputHotKeys.tsx @@ -14,73 +14,89 @@ enum TipType IsExist = 4, //重复 } +const SYSTEM_BEGIN_HOTKEYS = ["Shift", "Alt", "Control"]; + +const BEGIN_HOTKEYS = ["End", "Insert", "PageUp", "PageDown", "Home", "Delete"]; + @observer export class InputHotKeys extends React.Component<{ store: CommandItemProps; }, {}> { - keys: string[] = []; + _Keys: string[] = []; _InputElKey = React.createRef(); - @observable str: string = this.props.store.commandData.hotkeysCustomize + @observable _Str: string = this.props.store.commandData.hotkeysCustomize || (CommandServer.GetInstance().GetCommandCanUseDefaultHotkey(this.props.store.commandData) && CommandServer.GetInstance().HotKeyCommandMap.get(this.props.store.commandData.defaultHotkeys) ? this.props.store.commandData.defaultHotkeys : "") || this.props.store.commandData.systemHotkeys; - @observable flag: TipType = TipType.OK; - @observable hotkey: string = ""; - @observable isReset: boolean = false; + @observable _Flag: TipType = TipType.OK; + @observable _Hotkey: string = ""; + @observable _IsReset: boolean = false; checkHotKey = () => { - if (!this.isReset) + if (!this._IsReset) { - let keys = []; - for (let key of this.keys) + let keys: string[] = []; + for (let key of this._Keys) { keys.push(key[0].toUpperCase() + key.substring(1)); } keys.sort((a, b) => { - return (a.length > b.length) ? -1 : (a.length < b.length) ? 1 : 0; + return SYSTEM_BEGIN_HOTKEYS.indexOf(b) - SYSTEM_BEGIN_HOTKEYS.indexOf(a); }); - this.str = keys.join("+"); - this.flag = TipType.OK; - if (this.keys[0] !== "Control" && this.keys[0] !== "Alt" && this.keys[0] !== "Shift") + this._Str = keys.join("+"); + this._Flag = TipType.OK; + if (!SYSTEM_BEGIN_HOTKEYS.includes(keys[0]) && !BEGIN_HOTKEYS.includes(keys[0])) { - this.flag = TipType.StrIllegal; + this._Flag = TipType.StrIllegal; return; } - if (this.keys.length == 1) + if (keys.length == 1 && !BEGIN_HOTKEYS.includes(keys[0])) { - this.flag = TipType.InputNull; + this._Flag = TipType.InputNull; return; } if (keys.length > 1 && (keys[keys.length - 1] === "Alt" || keys[keys.length - 1] === "Shift")) { - this.flag = TipType.InputNull; + this._Flag = TipType.InputNull; + return; + } + if (keys.length > 1 && this.includeBeginHotkeys(keys)) + { + this._Flag = TipType.InputNull; return; } } - let value = this.str; + let value = this._Str; let curCmd = this.props.store.commandData; - let oldCmd = CommandServer.GetInstance().HotKeyCommandMap.get(this.str); + let oldCmd = CommandServer.GetInstance().HotKeyCommandMap.get(this._Str); if (oldCmd && oldCmd !== curCmd.command) { - this.flag = TipType.IsExist; - this.hotkey = value; + this._Flag = TipType.IsExist; + this._Hotkey = value; } - }; + + private includeBeginHotkeys(keys: string[]) + { + if (BEGIN_HOTKEYS.includes(keys[0])) + return true; + return false; + } + handleOnBlur = () => { const commandData = this.props.store.commandData; let cserver = CommandServer.GetInstance(); - if (this.flag === TipType.OK || this.str === "") + if (this._Flag === TipType.OK || this._Str === "") { - commandData.hotkeysCustomize = this.str; + commandData.hotkeysCustomize = this._Str; cserver.ChangeCustonCommand(commandData); } else - this.str = commandData.hotkeysCustomize ?? (cserver.GetCommandCanUseDefaultHotkey(commandData) ? (commandData.defaultHotkeys) : "") ?? ""; - this.flag = TipType.OK; + this._Str = commandData.hotkeysCustomize ?? (cserver.GetCommandCanUseDefaultHotkey(commandData) ? (commandData.defaultHotkeys) : "") ?? ""; + this._Flag = TipType.OK; }; public render() { @@ -90,12 +106,13 @@ export class InputHotKeys extends React.Component<{ store: CommandItemProps; }, outline: "none", }; let errorMsg = ""; - if (this.flag & TipType.InputNull) + if (this._Flag & TipType.InputNull) errorMsg = "不允许的快捷键组合!"; - if (this.flag & TipType.StrIllegal) + if (this._Flag & TipType.StrIllegal) errorMsg = "快捷键须以Control或Alt或Shift开头"; - if (this.flag & TipType.IsExist) - errorMsg = "快捷键 " + this.hotkey + " 重复! 请重新输入!"; + if (this._Flag & TipType.IsExist) + errorMsg = "快捷键 " + this._Hotkey + " 重复! 请重新输入!"; + return (
@@ -103,7 +120,7 @@ export class InputHotKeys extends React.Component<{ store: CommandItemProps; }, content={errorMsg} position={Position.TOP} intent={Intent.WARNING} - isOpen={this.flag !== TipType.OK} + isOpen={this._Flag !== TipType.OK} > { - this.keys = this.keys.filter((key) => key !== e.key); + this._Keys = this._Keys.filter((key) => key !== e.key); e.preventDefault(); }} - value={this.str} + value={this._Str} onClick={(e) => e.stopPropagation()} onChange={this.checkHotKey} onBlur={(e) => { - this.keys.length = 0; + this._Keys.length = 0; this.handleOnBlur(); }} /> @@ -151,8 +168,8 @@ export class InputHotKeys extends React.Component<{ store: CommandItemProps; }, { this.isReset = false; }} + isOpen={this._IsReset} + onClose={() => { this._IsReset = false; }} content={
) => { - this.isReset = false; + this._IsReset = false; event.stopPropagation(); }} /> @@ -184,10 +201,10 @@ export class InputHotKeys extends React.Component<{ store: CommandItemProps; }, intent={Intent.PRIMARY} onClick={(event: React.MouseEvent) => { - this.str = this.props.store.commandData.defaultHotkeys || ""; + this._Str = this.props.store.commandData.defaultHotkeys || ""; this.checkHotKey(); this.handleOnBlur(); - this.isReset = false; + this._IsReset = false; this._InputElKey.current.focus(); event.stopPropagation(); }} @@ -202,7 +219,7 @@ export class InputHotKeys extends React.Component<{ store: CommandItemProps; }, onClick={(event: React.MouseEvent) => { if (!this.props.store.commandData.systemHotkeys) - this.isReset = true; + this._IsReset = true; event.stopPropagation(); }} />