Skip to content

Commit ed7ffe0

Browse files
fix(web): composer commands keep Mod+B/I/E ahead of app keybindings
The editor hook that yields formatting chords to app commands skipped the composer's own command handler too, so a composer-only binding such as composer.sendBackground lost to a global one on the same chord. AI-assisted; verified with the tests and tools listed above. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6da5cf0 commit ed7ffe0

2 files changed

Lines changed: 33 additions & 9 deletions

File tree

‎apps/web/src/components/ComposerPromptEditorTiptap.input.browser.test.tsx‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ function Composer({
3737
richTextEnabled,
3838
skills,
3939
onChange,
40+
onCommandKeyDown = () => false,
4041
}: {
4142
value: string;
4243
richTextEnabled: boolean;
4344
skills: ReadonlyArray<ServerProviderSkill>;
4445
onChange?: ComposerPromptEditorProps["onChange"] | undefined;
46+
onCommandKeyDown?: ComposerPromptEditorProps["onCommandKeyDown"] | undefined;
4547
}) {
4648
const [prompt, setPrompt] = useState({ value, cursor: value.length });
4749
return (
@@ -59,6 +61,7 @@ function Composer({
5961
onChange?.(...args);
6062
}}
6163
onPaste={() => {}}
64+
onCommandKeyDown={onCommandKeyDown}
6265
/>
6366
);
6467
}
@@ -68,9 +71,16 @@ function composer(
6871
richTextEnabled = true,
6972
skills: ReadonlyArray<ServerProviderSkill> = [],
7073
onChange?: ComposerPromptEditorProps["onChange"],
74+
onCommandKeyDown?: ComposerPromptEditorProps["onCommandKeyDown"],
7175
) {
7276
return (
73-
<Composer value={value} richTextEnabled={richTextEnabled} skills={skills} onChange={onChange} />
77+
<Composer
78+
value={value}
79+
richTextEnabled={richTextEnabled}
80+
skills={skills}
81+
onChange={onChange}
82+
onCommandKeyDown={onCommandKeyDown}
83+
/>
7484
);
7585
}
7686

@@ -214,9 +224,13 @@ it.each([
214224
},
215225
);
216226

217-
it.each(["e", "i"])(
218-
"leaves Mod+%s to an app command bound to it instead of formatting",
219-
async (key) => {
227+
it.each([
228+
["e", false],
229+
["i", false],
230+
["e", true],
231+
])(
232+
"leaves Mod+%s to an app command bound to it instead of formatting (composer command: %s)",
233+
async (key, composerClaimsKey) => {
220234
const openUsage = vi.fn((event: KeyboardEvent) => {
221235
event.preventDefault();
222236
});
@@ -240,7 +254,8 @@ it.each(["e", "i"])(
240254
},
241255
),
242256
};
243-
await using screen = await mountEditor("word");
257+
const composerCommand = vi.fn(() => composerClaimsKey);
258+
await using screen = await mountEditor("word", true, [], undefined, composerCommand);
244259
const { editor } = screen;
245260
await act(async () => {
246261
editor.commands.focus();
@@ -249,7 +264,8 @@ it.each(["e", "i"])(
249264
await vi.waitFor(() => expect(document.activeElement).toBe(editor.view.dom));
250265
const modifier = isMacPlatform(navigator.platform) ? "Meta" : "Control";
251266
await userEvent.keyboard(`{${modifier}>}${key}{/${modifier}}`);
252-
expect(openUsage).toHaveBeenCalledOnce();
267+
expect(composerCommand).toHaveBeenCalled();
268+
expect(openUsage).toHaveBeenCalledTimes(composerClaimsKey ? 0 : 1);
253269
expect(serializeEditorDoc(editor.state.doc).value).toBe("word");
254270
},
255271
);

‎apps/web/src/components/ComposerPromptEditorTiptap.tsx‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,9 +1112,17 @@ function ComposerPromptEditorTiptapInner(
11121112
editorProps: {
11131113
attributes: editorAttributes,
11141114
handleDOMEvents: {
1115-
// True skips the editor's keymaps without preventDefault, so the app command still runs.
1116-
keydown: (_view, event) =>
1117-
isRichTextFormattingShortcut(event) && hasCommandForKeyDown(event),
1115+
// Composer commands keep precedence; otherwise true skips the editor's keymaps without
1116+
// preventDefault, so a bound app command still runs.
1117+
keydown: (_view, event) => {
1118+
if (!isRichTextFormattingShortcut(event)) return false;
1119+
if (readCallbacks().onCommandKeyDown?.(event.key, event)) {
1120+
event.preventDefault();
1121+
event.stopPropagation();
1122+
return true;
1123+
}
1124+
return hasCommandForKeyDown(event);
1125+
},
11181126
},
11191127
handleKeyDown: (view, event) => {
11201128
// In a right-to-left line the start is on the right.

0 commit comments

Comments
 (0)