Skip to content

Commit 2904fea

Browse files
fix(web): Mod+B toggles bold at a collapsed cursor when the sidebar toggle is remapped (#29445)
1 parent cc62443 commit 2904fea

5 files changed

Lines changed: 61 additions & 27 deletions

File tree

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

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,51 @@ it.each([
151151
expect(textWithMark(editor, "code")).toEqual(code);
152152
});
153153

154+
it.each([
155+
{ mark: "bold", key: "b", marker: "**" },
156+
{ mark: "italic", key: "i", marker: "*" },
157+
])("toggles $mark with Mod+$key at a collapsed cursor", async ({ mark, key, marker }) => {
158+
await using screen = await mountEditor("ship it");
159+
const { editor } = screen;
160+
const mod = isMacPlatform(navigator.platform) ? "Meta" : "Control";
161+
const chord = `{${mod}>}${key}{/${mod}}`;
162+
await act(async () => {
163+
editor.commands.focus("end");
164+
});
165+
await vi.waitFor(() => expect(document.activeElement).toBe(editor.view.dom));
166+
await userEvent.keyboard(" ");
167+
await userEvent.keyboard(chord);
168+
await userEvent.keyboard("now");
169+
await userEvent.keyboard(chord);
170+
await userEvent.keyboard(" done");
171+
await vi.waitFor(() =>
172+
expect(serializeEditorDoc(editor.state.doc).value).toBe(`ship it ${marker}now${marker} done`),
173+
);
174+
expect(textWithMark(editor, mark)).toEqual(["now"]);
175+
});
176+
177+
it.each([
178+
{ key: "i", caret: 10, expected: "ship it **n*X*ow** done" },
179+
{ key: "i", caret: 9, expected: "ship it *X***now** done" },
180+
{ key: "e", caret: "{ArrowLeft>5/}", expected: "ship it **now**`X` done" },
181+
])(
182+
"Mod+$key at $caret in a bold word styles the next typed text",
183+
async ({ key, caret, expected }) => {
184+
await using screen = await mountEditor("ship it **now** done");
185+
const { editor } = screen;
186+
const mod = isMacPlatform(navigator.platform) ? "Meta" : "Control";
187+
await act(async () => {
188+
if (typeof caret === "number") editor.commands.focus(caret);
189+
else editor.commands.focus("end");
190+
});
191+
await vi.waitFor(() => expect(document.activeElement).toBe(editor.view.dom));
192+
if (typeof caret === "string") await userEvent.keyboard(caret);
193+
await userEvent.keyboard(`{${mod}>}${key}{/${mod}}`);
194+
await userEvent.keyboard("X");
195+
await vi.waitFor(() => expect(serializeEditorDoc(editor.state.doc).value).toBe(expected));
196+
},
197+
);
198+
154199
it.each([
155200
{ word: "ship", prefix: "", source: ["**ship**"] },
156201
{ word: "path\\", prefix: "", source: [] },
@@ -169,7 +214,7 @@ it.each([
169214
code: ["path\\"],
170215
},
171216
])(
172-
"bolds $word after '$prefix' with Mod+B only when text is selected",
217+
"bolds $word after '$prefix' with Mod+B over a selection",
173218
async ({ word, prefix, prefixText = prefix, source, code = [] }) => {
174219
const value = `${prefix}${word} it`;
175220
await using screen = await mountEditor(value);
@@ -179,10 +224,6 @@ it.each([
179224
editor.commands.focus("end");
180225
});
181226
await vi.waitFor(() => expect(document.activeElement).toBe(editor.view.dom));
182-
await userEvent.keyboard(modB);
183-
// Without the guard, Tiptap stores bold for the next typed text.
184-
expect(editor.state.storedMarks).toBeNull();
185-
186227
await act(async () => {
187228
editor.commands.setTextSelection({
188229
from: prefixText.length + 1,

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ import {
7676
ComposerCodeBlockExtension,
7777
ComposerListExtensions,
7878
buildTiptapContent,
79-
ComposerBoldSelectionOnlyExtension,
8079
convertBulletItemToTask,
8180
ComposerCodeExtension,
8281
ComposerBoldExtension,
@@ -834,7 +833,6 @@ function createComposerExtensions(richText: boolean) {
834833
...(richText
835834
? [
836835
ComposerMarkdownSourceExtension,
837-
ComposerBoldSelectionOnlyExtension,
838836
ComposerItalicExtension,
839837
ComposerBoldExtension,
840838
ComposerStrikeExtension,

‎apps/web/src/composer-markdown-source.ts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -550,10 +550,20 @@ export const ComposerMarkdownSourceExtension = TiptapMark.create({
550550
// in another style. Parse the source first and toggle the result.
551551
const toggle = (name: RichTextMark) => () => {
552552
const { state } = this.editor;
553-
if (state.selection.empty) return false;
554-
const touched = sourceRegions(state.doc, this.type).some((region) =>
555-
touches(state.selection, region),
556-
);
553+
const { selection } = state;
554+
const touched = sourceRegions(state.doc, this.type).some((region) => {
555+
if (!touches(selection, region)) return false;
556+
if (!selection.empty) return true;
557+
// Turning a style off just inside its own markers would type into them.
558+
// Everywhere else Tiptap's own toggle styles the typed text correctly.
559+
const { units } = regionUnits(state.doc, region);
560+
const offset = selection.from - region.from;
561+
const [before, after] = [units[offset - 1], units[offset]];
562+
return (
563+
(before === null && after?.includes(name) === true) ||
564+
(after === null && before?.includes(name) === true)
565+
);
566+
});
557567
if (!touched) return false;
558568
return this.editor.chain().command(collapseShownMarkdownSource).toggleMark(name).run();
559569
};

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import {
2222
getChangedRanges,
2323
type Command,
2424
type Editor,
25-
Extension,
2625
InputRule,
2726
markInputRule,
2827
markPasteRule,
@@ -372,20 +371,6 @@ export const ComposerStrikeExtension = Strike.extend({
372371
},
373372
});
374373

375-
/**
376-
* Mod+B bolds selected text only. Without a selection Tiptap would toggle bold
377-
* for the next typed text, which users hit by accident when they mean the
378-
* sidebar toggle. Runs ahead of the Bold extension and swallows the chord.
379-
*/
380-
export const ComposerBoldSelectionOnlyExtension = Extension.create({
381-
name: "composerBoldSelectionOnly",
382-
priority: 1000,
383-
addKeyboardShortcuts() {
384-
const ignoreWithoutSelection = () => this.editor.state.selection.empty;
385-
return { "Mod-b": ignoreWithoutSelection, "Mod-B": ignoreWithoutSelection };
386-
},
387-
});
388-
389374
/**
390375
* Tiptap's block chords can nest blocks inside quotes that the serializer cannot write,
391376
* silently dropping their text from stored drafts.

‎apps/web/src/keybindings.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ export function isOpenFavoriteEditorShortcut(
379379
* Whether the keypress should bold text in the rich-text composer: Mod+B
380380
* without extra modifiers, with text selected. Tiptap binds the same chord, so
381381
* app shortcuts captured ahead of the editor yield only in that case. With no
382-
* selection the chord stays with the app (the sidebar toggle by default).
382+
* selection an app binding on the chord wins (the sidebar toggle by default).
383383
*/
384384
export function isRichTextBoldShortcut(
385385
event: ShortcutEventLike,

0 commit comments

Comments
 (0)