From 6eff73719395c2d4dcaa7350d29affaae55243a9 Mon Sep 17 00:00:00 2001 From: liujuping Date: Thu, 29 Feb 2024 18:02:29 +0800 Subject: [PATCH] feat(hotkey): Add mount method to Hotkey class --- docs/docs/api/hotkey.md | 13 +++++++++++++ packages/designer/src/project/project.ts | 2 +- packages/editor-core/src/command.ts | 10 ++-------- packages/editor-core/src/hotkey.ts | 11 ++++++----- packages/shell/src/api/hotkey.ts | 8 ++++++++ packages/types/src/shell/api/hotkey.ts | 6 ++++++ 6 files changed, 36 insertions(+), 14 deletions(-) diff --git a/docs/docs/api/hotkey.md b/docs/docs/api/hotkey.md index be6a3033d..b391e367b 100644 --- a/docs/docs/api/hotkey.md +++ b/docs/docs/api/hotkey.md @@ -32,6 +32,19 @@ bind( - [IPublicTypeHotkeyCallback](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/hotkey-callback.ts) - [IPublicTypeDisposable](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/disposable.ts) +### mount + +给指定窗口绑定快捷键 + +```typescript +/** + * 给指定窗口绑定快捷键 + * @param window 窗口的 window 对象 + */ +mount(window: Window): IPublicTypeDisposable; + +``` + ## 使用示例 ### 基础示例 diff --git a/packages/designer/src/project/project.ts b/packages/designer/src/project/project.ts index 94913d9fa..88978e467 100644 --- a/packages/designer/src/project/project.ts +++ b/packages/designer/src/project/project.ts @@ -68,7 +68,7 @@ export interface IProject extends Omit void): () => void; - onSimulatorReady(fn: (args: any) => void): () => void; + onSimulatorReady(fn: (simulator: ISimulatorHost) => void): () => void; onRendererReady(fn: () => void): () => void; diff --git a/packages/editor-core/src/command.ts b/packages/editor-core/src/command.ts index 7facc33d9..04bd5ba5a 100644 --- a/packages/editor-core/src/command.ts +++ b/packages/editor-core/src/command.ts @@ -1,18 +1,12 @@ import { IPublicApiCommand, IPublicEnumTransitionType, IPublicModelPluginContext, IPublicTypeCommand, IPublicTypeCommandHandlerArgs, IPublicTypeListCommand } from '@alilc/lowcode-types'; import { checkPropTypes } from '@alilc/lowcode-utils'; -export interface ICommand extends Omit { - registerCommand(command: IPublicTypeCommand, options?: { - commandScope?: string; - }): void; - - batchExecuteCommand(commands: { name: string; args: IPublicTypeCommandHandlerArgs }[], pluginContext?: IPublicModelPluginContext): void; -} +export interface ICommand extends Command {} export interface ICommandOptions { commandScope?: string; } -export class Command implements ICommand { +export class Command implements Omit { private commands: Map = new Map(); private commandErrors: Function[] = []; diff --git a/packages/editor-core/src/hotkey.ts b/packages/editor-core/src/hotkey.ts index d0dd40cc2..496cec251 100644 --- a/packages/editor-core/src/hotkey.ts +++ b/packages/editor-core/src/hotkey.ts @@ -1,6 +1,6 @@ import { isEqual } from 'lodash'; import { globalContext } from './di'; -import { IPublicTypeHotkeyCallback, IPublicTypeHotkeyCallbackConfig, IPublicTypeHotkeyCallbacks, IPublicApiHotkey } from '@alilc/lowcode-types'; +import { IPublicTypeHotkeyCallback, IPublicTypeHotkeyCallbackConfig, IPublicTypeHotkeyCallbacks, IPublicApiHotkey, IPublicTypeDisposable } from '@alilc/lowcode-types'; interface KeyMap { [key: number]: string; @@ -339,11 +339,10 @@ function fireCallback(callback: IPublicTypeHotkeyCallback, e: KeyboardEvent, com } } -export interface IHotKey extends Omit { - activate(activate: boolean): void; +export interface IHotKey extends Hotkey { } -export class Hotkey implements IHotKey { +export class Hotkey implements Omit { callBacks: IPublicTypeHotkeyCallbacks = {}; private directMap: HotkeyDirectMap = {}; @@ -368,7 +367,7 @@ export class Hotkey implements IHotKey { this.isActivate = activate; } - mount(window: Window) { + mount(window: Window): IPublicTypeDisposable { const { document } = window; const handleKeyEvent = this.handleKeyEvent.bind(this); document.addEventListener('keypress', handleKeyEvent, false); @@ -542,6 +541,8 @@ export class Hotkey implements IHotKey { } private handleKeyEvent(e: KeyboardEvent): void { + console.log(e); + // debugger; if (!this.isActivate) { return; } diff --git a/packages/shell/src/api/hotkey.ts b/packages/shell/src/api/hotkey.ts index 4e65844ce..78a782994 100644 --- a/packages/shell/src/api/hotkey.ts +++ b/packages/shell/src/api/hotkey.ts @@ -50,4 +50,12 @@ export class Hotkey implements IPublicApiHotkey { this[hotkeySymbol].unbind(combos, callback, action); }; } + + /** + * 给指定窗口绑定快捷键 + * @param window 窗口的 window 对象 + */ + mount(window: Window) { + return this[hotkeySymbol].mount(window); + } } \ No newline at end of file diff --git a/packages/types/src/shell/api/hotkey.ts b/packages/types/src/shell/api/hotkey.ts index 894eb0e2f..55bde5d01 100644 --- a/packages/types/src/shell/api/hotkey.ts +++ b/packages/types/src/shell/api/hotkey.ts @@ -22,4 +22,10 @@ export interface IPublicApiHotkey { callback: IPublicTypeHotkeyCallback, action?: string, ): IPublicTypeDisposable; + + /** + * 给指定窗口绑定快捷键 + * @param window 窗口的 window 对象 + */ + mount(window: Window): IPublicTypeDisposable; }