Skip to content

Commit

Permalink
feat(hotkey): Add mount method to Hotkey class
Browse files Browse the repository at this point in the history
  • Loading branch information
liujuping committed Feb 29, 2024
1 parent dbd9382 commit 6eff737
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 14 deletions.
13 changes: 13 additions & 0 deletions docs/docs/api/hotkey.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;

```


## 使用示例
### 基础示例
Expand Down
2 changes: 1 addition & 1 deletion packages/designer/src/project/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
10 changes: 2 additions & 8 deletions packages/editor-core/src/command.ts
Original file line number Diff line number Diff line change
@@ -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[] = [];

Expand Down
11 changes: 6 additions & 5 deletions packages/editor-core/src/hotkey.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 = {};
Expand All @@ -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);
Expand Down Expand Up @@ -542,6 +541,8 @@ export class Hotkey implements IHotKey {
}

private handleKeyEvent(e: KeyboardEvent): void {
console.log(e);
// debugger;
if (!this.isActivate) {
return;
}
Expand Down
8 changes: 8 additions & 0 deletions packages/shell/src/api/hotkey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
6 changes: 6 additions & 0 deletions packages/types/src/shell/api/hotkey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ export interface IPublicApiHotkey {
callback: IPublicTypeHotkeyCallback,
action?: string,
): IPublicTypeDisposable;

/**
* 给指定窗口绑定快捷键
* @param window 窗口的 window 对象
*/
mount(window: Window): IPublicTypeDisposable;
}

0 comments on commit 6eff737

Please sign in to comment.