fix(core): run Option+letter shortcuts outside text input on macOS

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
This commit is contained in:
DarkSky committed 2026-11-23 15:39:36 +09:00
1 parent 8e549b05d2
commit 897eaaf099
2 files changed
+89 -12

No files matched your search

@@ -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);
}
);
});
@@ -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<string, AffineCommand> = 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();