From afe53c5194e13fc4356b240d9ff0652e74f7ed7c Mon Sep 17 00:00:00 2001 From: Aaron Dodson Date: Mon, 16 Jun 2025 15:45:01 -0700 Subject: [PATCH] fix: Dispatch keyboard events with the workspace they occurred on. (#9137) * fix: Dispatch keyboard events with the workspace they occurred on. * chore: Add comment warding off would-be refactorers. --- core/common.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/core/common.ts b/core/common.ts index a4b198ae4..7f23779ec 100644 --- a/core/common.ts +++ b/core/common.ts @@ -320,21 +320,28 @@ export function defineBlocks(blocks: {[key: string]: BlockDefinition}) { * @param e Key down event. */ export function globalShortcutHandler(e: KeyboardEvent) { - const mainWorkspace = getMainWorkspace() as WorkspaceSvg; - if (!mainWorkspace) { - return; + // This would ideally just be a `focusedTree instanceof WorkspaceSvg`, but + // importing `WorkspaceSvg` (as opposed to just its type) causes cycles. + let workspace: WorkspaceSvg = getMainWorkspace() as WorkspaceSvg; + const focusedTree = getFocusManager().getFocusedTree(); + for (const ws of getAllWorkspaces()) { + if (focusedTree === (ws as WorkspaceSvg)) { + workspace = ws as WorkspaceSvg; + break; + } } if ( browserEvents.isTargetInput(e) || - (mainWorkspace.rendered && !mainWorkspace.isVisible()) + !workspace || + (workspace.rendered && !workspace.isFlyout && !workspace.isVisible()) ) { // When focused on an HTML text input widget, don't trap any keys. // Ignore keypresses on rendered workspaces that have been explicitly // hidden. return; } - ShortcutRegistry.registry.onKeyDown(mainWorkspace, e); + ShortcutRegistry.registry.onKeyDown(workspace, e); } export const TEST_ONLY = {defineBlocksWithJsonArrayInternal};