Skip to content

Commit d040b8b

Browse files
t3dotggclaude
andcommitted
fix(web): Cmd+B bolds only selected text, else toggles the sidebar
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d7a8d6f commit d040b8b

6 files changed

Lines changed: 65 additions & 10 deletions

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,12 @@ function SidebarControl() {
149149
return;
150150
}
151151
if (
152-
isRichTextBoldShortcut(event) &&
152+
isRichTextBoldShortcut(event, window.getSelection()) &&
153153
event.target instanceof HTMLElement &&
154154
event.target.closest('[data-composer-rich-text="true"]')
155155
) {
156-
// The rich-text composer claims Mod+B for bold; the toggle stays
157-
// available everywhere else, including the plain-text composer.
156+
// The rich-text composer claims Mod+B only to bold selected text;
157+
// the toggle stays available everywhere else.
158158
return;
159159
}
160160
if (

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { userEvent } from "vite-plus/test/browser";
1616
import { act, render } from "../../test/render";
1717

1818
import { serializeEditorDoc } from "~/composer-rich-text-doc";
19+
import { isMacPlatform } from "~/lib/utils";
1920
import {
2021
ComposerPromptEditor,
2122
type ComposerPromptEditorHandle,
@@ -148,6 +149,25 @@ it.each([
148149
expect(textWithMark(editor, "code")).toEqual(code);
149150
});
150151

152+
it("bolds with Mod+B only when text is selected", async () => {
153+
await using screen = await mountEditor("ship it");
154+
const { editor } = screen;
155+
const modB = isMacPlatform(navigator.platform) ? "{Meta>}b{/Meta}" : "{Control>}b{/Control}";
156+
await act(async () => {
157+
editor.commands.focus("end");
158+
});
159+
await vi.waitFor(() => expect(document.activeElement).toBe(editor.view.dom));
160+
await userEvent.keyboard(modB);
161+
// Without the guard, Tiptap stores bold for the next typed text.
162+
expect(editor.state.storedMarks).toBeNull();
163+
164+
await act(async () => {
165+
editor.commands.setTextSelection({ from: 1, to: 5 });
166+
});
167+
await userEvent.keyboard(modB);
168+
await vi.waitFor(() => expect(textWithMark(editor, "bold")).toEqual(["ship"]));
169+
});
170+
151171
it.each([false, true])(
152172
"composes an umlaut without wrapping the preedit text (rich text: %s)",
153173
async (richTextEnabled) => {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import {
6868
ComposerListExtensions,
6969
buildTiptapContent,
7070
caretTakesMarksBefore,
71+
ComposerBoldSelectionOnlyExtension,
7172
convertBulletItemToTask,
7273
ComposerCodeExtension,
7374
ComposerItalicExtension,
@@ -874,6 +875,7 @@ function createComposerExtensions(richText: boolean) {
874875
ComposerMarkersExtension,
875876
...(richText
876877
? [
878+
ComposerBoldSelectionOnlyExtension,
877879
ComposerItalicExtension,
878880
ComposerCodeExtension,
879881
ComposerCodeBlockExtension.extend({

‎apps/web/src/composer-rich-text-doc.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { Italic, starInputRegex, starPasteRegex } from "@tiptap/extension-italic
1515
import {
1616
type Command,
1717
type Editor,
18+
Extension,
1819
markInputRule,
1920
markPasteRule,
2021
mergeAttributes,
@@ -153,6 +154,20 @@ export const ComposerItalicExtension = Italic.extend({
153154
},
154155
});
155156

157+
/**
158+
* Mod+B bolds selected text only. Without a selection Tiptap would toggle bold
159+
* for the next typed text, which users hit by accident when they mean the
160+
* sidebar toggle. Runs ahead of the Bold extension and swallows the chord.
161+
*/
162+
export const ComposerBoldSelectionOnlyExtension = Extension.create({
163+
name: "composerBoldSelectionOnly",
164+
priority: 1000,
165+
addKeyboardShortcuts() {
166+
const ignoreWithoutSelection = () => this.editor.state.selection.empty;
167+
return { "Mod-b": ignoreWithoutSelection, "Mod-B": ignoreWithoutSelection };
168+
},
169+
});
170+
156171
/**
157172
* Tiptap's block extensions each bind a chord that turns the current block into
158173
* their node (Mod-Shift-8 for a list, Mod-Alt-c for a fence, and so on). The

‎apps/web/src/keybindings.test.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -713,18 +713,30 @@ describe("resolveShortcutCommand", () => {
713713
});
714714

715715
describe("isRichTextBoldShortcut", () => {
716+
const selected = { isCollapsed: false };
717+
716718
it("matches the B key on non-Latin layouts, like the sidebar toggle does", () => {
717719
const cyrillicB = event({ key: "и", code: "KeyB", ctrlKey: true });
718-
assert.isTrue(isRichTextBoldShortcut(cyrillicB));
720+
assert.isTrue(isRichTextBoldShortcut(cyrillicB, selected));
719721
assert.strictEqual(
720722
resolveShortcutCommand(cyrillicB, DEFAULT_BINDINGS, { platform: "Win32" }),
721723
"sidebar.toggle",
722724
);
723725
});
724726

725727
it("follows the letter a Latin layout types, not the physical key", () => {
726-
assert.isFalse(isRichTextBoldShortcut(event({ key: "x", code: "KeyB", ctrlKey: true })));
727-
assert.isTrue(isRichTextBoldShortcut(event({ key: "b", code: "KeyN", ctrlKey: true })));
728+
assert.isFalse(
729+
isRichTextBoldShortcut(event({ key: "x", code: "KeyB", ctrlKey: true }), selected),
730+
);
731+
assert.isTrue(
732+
isRichTextBoldShortcut(event({ key: "b", code: "KeyN", ctrlKey: true }), selected),
733+
);
734+
});
735+
736+
it("leaves the chord to the sidebar toggle when no text is selected", () => {
737+
const modB = event({ key: "b", metaKey: true });
738+
assert.isFalse(isRichTextBoldShortcut(modB, { isCollapsed: true }));
739+
assert.isFalse(isRichTextBoldShortcut(modB, null));
728740
});
729741
});
730742

‎apps/web/src/keybindings.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,15 +371,21 @@ export function isOpenFavoriteEditorShortcut(
371371
}
372372

373373
/**
374-
* Whether the keypress is the rich-text bold chord (Mod+B without extra
375-
* modifiers). Tiptap binds the same chord, so app shortcuts captured ahead
376-
* of the editor must yield when the rich-text composer is focused.
374+
* Whether the keypress should bold text in the rich-text composer: Mod+B
375+
* without extra modifiers, with text selected. Tiptap binds the same chord, so
376+
* app shortcuts captured ahead of the editor yield only in that case. With no
377+
* selection the chord stays with the app (the sidebar toggle by default).
377378
*/
378-
export function isRichTextBoldShortcut(event: ShortcutEventLike): boolean {
379+
export function isRichTextBoldShortcut(
380+
event: ShortcutEventLike,
381+
selection: Pick<Selection, "isCollapsed"> | null,
382+
): boolean {
379383
if (event.type !== undefined && event.type !== "keydown") {
380384
return false;
381385
}
382386
return (
387+
selection !== null &&
388+
!selection.isCollapsed &&
383389
resolveEventKeys(event).has("b") &&
384390
(event.metaKey || event.ctrlKey) &&
385391
!event.altKey &&

0 commit comments

Comments
 (0)