Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hotkey): Add mount method to Hotkey class #2952

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Loading