From 878b4e7d9dedfef0c080baaa8cd7168a19ed2d8a Mon Sep 17 00:00:00 2001 From: Peng Xiao Date: Wed, 10 Aug 2022 23:46:59 +0800 Subject: [PATCH] fix: clear keybindings after exit whiteboard --- tldraw/packages/core/src/lib/TLApp/TLApp.ts | 15 ++++++++++++++- tldraw/packages/core/src/lib/TLState.ts | 10 ---------- tldraw/packages/core/src/utils/KeyUtils.ts | 2 +- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/tldraw/packages/core/src/lib/TLApp/TLApp.ts b/tldraw/packages/core/src/lib/TLApp/TLApp.ts index 4b41ae7f18..689955a97a 100644 --- a/tldraw/packages/core/src/lib/TLApp/TLApp.ts +++ b/tldraw/packages/core/src/lib/TLApp/TLApp.ts @@ -184,8 +184,21 @@ export class TLApp< // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore const shortcuts = (this.constructor['shortcuts'] || []) as TLShortcut[] + const childrenShortcuts = Array.from(this.children.values()) + // @ts-expect-error ??? + .filter(c => c.constructor['shortcut']) + .map(child => { + return { + // @ts-expect-error ??? + keys: child.constructor['shortcut'] as string | string[], + fn: (_: any, __: any, e: Event) => { + this.transition(child.id) + e.stopPropagation() + }, + } + }) this._disposables.push( - ...[...ownShortcuts, ...shortcuts].map(({ keys, fn }) => { + ...[...ownShortcuts, ...shortcuts, ...childrenShortcuts].map(({ keys, fn }) => { return KeyUtils.registerShortcut(keys, e => { fn(this, this, e) }) diff --git a/tldraw/packages/core/src/lib/TLState.ts b/tldraw/packages/core/src/lib/TLState.ts index de6f5ac5b2..8a72423c43 100644 --- a/tldraw/packages/core/src/lib/TLState.ts +++ b/tldraw/packages/core/src/lib/TLState.ts @@ -417,16 +417,6 @@ export abstract class TLState< } } - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore - const shortcut = this.constructor['shortcut'] as string - if (shortcut) { - KeyUtils.registerShortcut(shortcut, e => { - this.parent.transition(this.id) - e.stopPropagation() - }) - } - // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore const shortcuts = this.constructor['shortcuts'] as TLShortcut[] diff --git a/tldraw/packages/core/src/utils/KeyUtils.ts b/tldraw/packages/core/src/utils/KeyUtils.ts index 4f8db51fae..300c3747ed 100644 --- a/tldraw/packages/core/src/utils/KeyUtils.ts +++ b/tldraw/packages/core/src/utils/KeyUtils.ts @@ -26,7 +26,7 @@ export class KeyUtils { } Mousetrap.bind(keys, fn, 'keydown') return () => { - Mousetrap.unbind(keys) + Mousetrap.unbind(keys, 'keydown') } } }