From 897eaaf0990cfd6c8cb5568d187833774fb0ef87 Mon Sep 17 00:00:00 2001 From: DarkSky Date: Mon, 23 Nov 2026 15:39:36 +0900 Subject: [PATCH] fix(core): run Option+letter shortcuts outside text input on macOS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Option+S never switched between page and edgeless on the US macOS layout. The guard added for Polish input skipped every Alt event whose key is a non-ASCII character, and Option+S produces 'ß' there, so the shortcut could not fire at all. Only skip the shortcut when the key event targets an editable element, where the character is text input. Elsewhere there is nothing to type and the shortcut runs. fix #15232 --- .../src/commands/registry/registry.spec.ts | 65 +++++++++++++++++++ .../core/src/commands/registry/registry.ts | 36 ++++++---- 2 files changed, 89 insertions(+), 12 deletions(-) create mode 100644 packages/frontend/core/src/commands/registry/registry.spec.ts diff --git a/packages/frontend/core/src/commands/registry/registry.spec.ts b/packages/frontend/core/src/commands/registry/registry.spec.ts new file mode 100644 index 0000000000..9ab672bddc --- /dev/null +++ b/packages/frontend/core/src/commands/registry/registry.spec.ts @@ -0,0 +1,65 @@ +/** + * @vitest-environment happy-dom + */ +import { afterEach, describe, expect, test, vi } from 'vitest'; + +import { registerAffineCommand } from './registry'; + +const targets = { + body: () => document.body, + input: () => document.createElement('input'), + textarea: () => document.createElement('textarea'), + contenteditable: () => { + const div = document.createElement('div'); + div.contentEditable = 'true'; + return div; + }, +}; + +describe('Alt+KeyS command', () => { + let unregister: (() => void) | undefined; + + afterEach(() => { + unregister?.(); + document.body.innerHTML = ''; + }); + + test.each([ + { key: 's', target: 'body', runs: true }, + { key: 's', target: 'contenteditable', runs: true }, + { key: 'ß', target: 'body', runs: true }, + { key: 'ś', target: 'body', runs: true }, + { key: 'ß', target: 'input', runs: false }, + { key: 'ś', target: 'textarea', runs: false }, + { key: 'ś', target: 'contenteditable', runs: false }, + ] as const)( + 'key "$key" on $target runs the command: $runs', + ({ key, target, runs }) => { + const run = vi.fn(); + unregister = registerAffineCommand({ + id: 'test:alt-s', + label: 'test', + icon: null, + keyBinding: { binding: 'Alt+KeyS', capture: true }, + run, + }); + const element = targets[target](); + if (element !== document.body) { + document.body.append(element); + } + + const event = new KeyboardEvent('keydown', { + key, + code: 'KeyS', + altKey: true, + bubbles: true, + composed: true, + cancelable: true, + }); + element.dispatchEvent(event); + + expect(run).toHaveBeenCalledTimes(runs ? 1 : 0); + expect(event.defaultPrevented).toBe(runs); + } + ); +}); diff --git a/packages/frontend/core/src/commands/registry/registry.ts b/packages/frontend/core/src/commands/registry/registry.ts index c6472da085..97630f7b04 100644 --- a/packages/frontend/core/src/commands/registry/registry.ts +++ b/packages/frontend/core/src/commands/registry/registry.ts @@ -45,6 +45,29 @@ const bindKeys = ( }; }; +// On macOS, Option+letter types a character (e.g. "ß" on the US layout, +// "ś" on the Polish one) and the event alone cannot tell which the user meant. +// Inside an editable element it is text input; elsewhere there is no text to +// type, so the shortcut bound to the physical key runs. +const isAltTextInput = (event: KeyboardEvent) => { + if ( + !event.altKey || + event.metaKey || + event.ctrlKey || + event.key.length !== 1 || + event.key.charCodeAt(0) <= 0x7e + ) { + return false; + } + const target = event.composedPath()[0]; + return ( + target instanceof HTMLElement && + (target.isContentEditable || + target instanceof HTMLInputElement || + target instanceof HTMLTextAreaElement) + ); +}; + export const AffineCommandRegistry = new (class { readonly commands: Map = new Map(); @@ -68,18 +91,7 @@ export const AffineCommandRegistry = new (class { window, { [keybinding]: (e: Event) => { - // Skip when Alt produces a locale input character (e.g. macOS - // Polish layout: Option+S → "ś"). The user is typing the - // character, not invoking the shortcut bound to the physical - // key. Matches the handling in blocksuite's keymap.ts. - if ( - e instanceof KeyboardEvent && - e.altKey && - !e.metaKey && - !e.ctrlKey && - e.key.length === 1 && - e.key.charCodeAt(0) > 0x7e - ) { + if (e instanceof KeyboardEvent && isAltTextInput(e)) { return; } e.preventDefault();