export class KeyEventManager { stateManager: any constructor(options) { this.stateManager = options.stateManager; this.registerEvents() } /** 处理键盘事件 */ handleKeyDown(event: any) { const ctrl = event.ctrlKey ? 'ctrl-' : ""; const shift = event.shiftKey ? 'shift-' : ""; const key = event.key; const reg = new RegExp(`^${ctrl}${shift}${key}$`, 'i') const list = [ // { key: "ctrl-c", handler: () => this.handleCopy(event) }, // { key: "delete", handler: () => this.handleDelete(event) }, { key: "ctrl-z", handler: () => this.stateManager.undoState() }, { key: "ctrl-shift-z", handler: () => this.stateManager.redoState() }, ] list.forEach((v: any) => { if (reg.test(v.key)) v.handler(event) }) } /** 注册事件 */ registerEvents() { document.addEventListener('keydown', this.handleKeyDown.bind(this)) } /** 删除事件 */ removeEvents() { document.removeEventListener('keydown', this.handleKeyDown.bind(this)) } dispose() { this.removeEvents() } }