feat(hotkey): Add mount method to Hotkey class
This commit is contained in:
parent
202159cfd0
commit
33902e0109
|
@ -32,6 +32,19 @@ bind(
|
||||||
- [IPublicTypeHotkeyCallback](https://github.com/alibaba/lowcode-engine/blob/main/packages/types/src/shell/type/hotkey-callback.ts)
|
- [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)
|
- [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;
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
## 使用示例
|
## 使用示例
|
||||||
### 基础示例
|
### 基础示例
|
||||||
|
|
|
@ -68,7 +68,7 @@ export interface IProject extends Omit<IBaseApiProject<
|
||||||
|
|
||||||
onCurrentDocumentChange(fn: (doc: IDocumentModel) => void): () => void;
|
onCurrentDocumentChange(fn: (doc: IDocumentModel) => void): () => void;
|
||||||
|
|
||||||
onSimulatorReady(fn: (args: any) => void): () => void;
|
onSimulatorReady(fn: (simulator: ISimulatorHost) => void): () => void;
|
||||||
|
|
||||||
onRendererReady(fn: () => void): () => void;
|
onRendererReady(fn: () => void): () => void;
|
||||||
|
|
||||||
|
|
|
@ -1,18 +1,12 @@
|
||||||
import { IPublicApiCommand, IPublicEnumTransitionType, IPublicModelPluginContext, IPublicTypeCommand, IPublicTypeCommandHandlerArgs, IPublicTypeListCommand } from '@alilc/lowcode-types';
|
import { IPublicApiCommand, IPublicEnumTransitionType, IPublicModelPluginContext, IPublicTypeCommand, IPublicTypeCommandHandlerArgs, IPublicTypeListCommand } from '@alilc/lowcode-types';
|
||||||
import { checkPropTypes } from '@alilc/lowcode-utils';
|
import { checkPropTypes } from '@alilc/lowcode-utils';
|
||||||
export interface ICommand extends Omit<IPublicApiCommand, 'registerCommand' | 'batchExecuteCommand'> {
|
export interface ICommand extends Command {}
|
||||||
registerCommand(command: IPublicTypeCommand, options?: {
|
|
||||||
commandScope?: string;
|
|
||||||
}): void;
|
|
||||||
|
|
||||||
batchExecuteCommand(commands: { name: string; args: IPublicTypeCommandHandlerArgs }[], pluginContext?: IPublicModelPluginContext): void;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ICommandOptions {
|
export interface ICommandOptions {
|
||||||
commandScope?: string;
|
commandScope?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Command implements ICommand {
|
export class Command implements Omit<IPublicApiCommand, 'registerCommand' | 'batchExecuteCommand'> {
|
||||||
private commands: Map<string, IPublicTypeCommand> = new Map();
|
private commands: Map<string, IPublicTypeCommand> = new Map();
|
||||||
private commandErrors: Function[] = [];
|
private commandErrors: Function[] = [];
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { isEqual } from 'lodash';
|
import { isEqual } from 'lodash';
|
||||||
import { globalContext } from './di';
|
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 {
|
interface KeyMap {
|
||||||
[key: number]: string;
|
[key: number]: string;
|
||||||
|
@ -339,11 +339,10 @@ function fireCallback(callback: IPublicTypeHotkeyCallback, e: KeyboardEvent, com
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IHotKey extends Omit<IPublicApiHotkey, 'bind' | 'callbacks'> {
|
export interface IHotKey extends Hotkey {
|
||||||
activate(activate: boolean): void;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Hotkey implements IHotKey {
|
export class Hotkey implements Omit<IPublicApiHotkey, 'bind' | 'callbacks'> {
|
||||||
callBacks: IPublicTypeHotkeyCallbacks = {};
|
callBacks: IPublicTypeHotkeyCallbacks = {};
|
||||||
|
|
||||||
private directMap: HotkeyDirectMap = {};
|
private directMap: HotkeyDirectMap = {};
|
||||||
|
@ -368,7 +367,7 @@ export class Hotkey implements IHotKey {
|
||||||
this.isActivate = activate;
|
this.isActivate = activate;
|
||||||
}
|
}
|
||||||
|
|
||||||
mount(window: Window) {
|
mount(window: Window): IPublicTypeDisposable {
|
||||||
const { document } = window;
|
const { document } = window;
|
||||||
const handleKeyEvent = this.handleKeyEvent.bind(this);
|
const handleKeyEvent = this.handleKeyEvent.bind(this);
|
||||||
document.addEventListener('keypress', handleKeyEvent, false);
|
document.addEventListener('keypress', handleKeyEvent, false);
|
||||||
|
@ -542,6 +541,8 @@ export class Hotkey implements IHotKey {
|
||||||
}
|
}
|
||||||
|
|
||||||
private handleKeyEvent(e: KeyboardEvent): void {
|
private handleKeyEvent(e: KeyboardEvent): void {
|
||||||
|
console.log(e);
|
||||||
|
// debugger;
|
||||||
if (!this.isActivate) {
|
if (!this.isActivate) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,4 +50,12 @@ export class Hotkey implements IPublicApiHotkey {
|
||||||
this[hotkeySymbol].unbind(combos, callback, action);
|
this[hotkeySymbol].unbind(combos, callback, action);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 给指定窗口绑定快捷键
|
||||||
|
* @param window 窗口的 window 对象
|
||||||
|
*/
|
||||||
|
mount(window: Window) {
|
||||||
|
return this[hotkeySymbol].mount(window);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -22,4 +22,10 @@ export interface IPublicApiHotkey {
|
||||||
callback: IPublicTypeHotkeyCallback,
|
callback: IPublicTypeHotkeyCallback,
|
||||||
action?: string,
|
action?: string,
|
||||||
): IPublicTypeDisposable;
|
): IPublicTypeDisposable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 给指定窗口绑定快捷键
|
||||||
|
* @param window 窗口的 window 对象
|
||||||
|
*/
|
||||||
|
mount(window: Window): IPublicTypeDisposable;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue