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();