diff --git a/packages-user/client-base/src/use/hotkey.ts b/packages-user/client-base/src/use/hotkey.ts index 75e7f71..448b2bb 100644 --- a/packages-user/client-base/src/use/hotkey.ts +++ b/packages-user/client-base/src/use/hotkey.ts @@ -13,6 +13,8 @@ class HotkeyUsing implements IHotkeyUsing { private keyScope: symbol | null = null; /** 当前的按键实现对象 */ private impl: Map void> = new Map(); + /** 当前设置的 `when` 判断函数 */ + private whenFunc: (() => boolean) | null = null; constructor( readonly system: IHotkeySystem, @@ -26,6 +28,7 @@ class HotkeyUsing implements IHotkeyUsing { const scope = system.enterScope(clearImpl); this.keyScope = scope; resolve(scope); + if (this.whenFunc) system.when(this.whenFunc); for (const [command, func] of this.impl) { system.implement(command, func); } @@ -45,6 +48,15 @@ class HotkeyUsing implements IHotkeyUsing { return this.scopePromise; } + when(func: () => boolean): this { + if (this.mounted) { + this.system.when(func); + } else { + this.whenFunc = func; + } + return this; + } + implement(command: string, func: (info: IHotkeyEmitInfo) => void): this { if (this.mounted) { this.system.implement(command, func); diff --git a/packages-user/client-base/src/use/types.ts b/packages-user/client-base/src/use/types.ts index b92eb61..4a7f25d 100644 --- a/packages-user/client-base/src/use/types.ts +++ b/packages-user/client-base/src/use/types.ts @@ -6,6 +6,12 @@ export interface IHotkeyUsing { */ scope(): Promise; + /** + * 设置当前的按键判断函数,当函数返回 `true` 时,按键才会真正生效 + * @param func 判断函数,返回当前是否应该生效 + */ + when(func: () => boolean): this; + /** * 实现按键指令 * @param command 按键指令