feat(hotkey): Add mount method to Hotkey class

This commit is contained in:
liujuping 2024-02-29 18:02:29 +08:00 committed by JackLian
parent 202159cfd0
commit 33902e0109
6 changed files with 36 additions and 14 deletions

View File

@ -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;
```
## 使用示例
### 基础示例

View File

@ -68,7 +68,7 @@ export interface IProject extends Omit<IBaseApiProject<
onCurrentDocumentChange(fn: (doc: IDocumentModel) => void): () => void;
onSimulatorReady(fn: (args: any) => void): () => void;
onSimulatorReady(fn: (simulator: ISimulatorHost) => void): () => void;
onRendererReady(fn: () => void): () => void;

View File

@ -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<IPublicApiCommand, 'registerCommand' | 'batchExecuteCommand'> {
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<IPublicApiCommand, 'registerCommand' | 'batchExecuteCommand'> {
private commands: Map<string, IPublicTypeCommand> = new Map();
private commandErrors: Function[] = [];

View File

@ -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<IPublicApiHotkey, 'bind' | 'callbacks'> {
activate(activate: boolean): void;
export interface IHotKey extends Hotkey {
}
export class Hotkey implements IHotKey {
export class Hotkey implements Omit<IPublicApiHotkey, 'bind' | 'callbacks'> {
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;
}

View File

@ -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);
}
}

View File

@ -22,4 +22,10 @@ export interface IPublicApiHotkey {
callback: IPublicTypeHotkeyCallback,
action?: string,
): IPublicTypeDisposable;
/**
*
* @param window window
*/
mount(window: Window): IPublicTypeDisposable;
}