Skip to content

Commit 0e9f0e9

Browse files
committed
fix(web): escape formatting at composer boundaries
Reset implicit typing marks when placing the caret, pasting, dropping, or restoring history. Keep normal typing inclusive so explicit formatting shortcuts continue working across keystrokes.
1 parent 3a9c1a6 commit 0e9f0e9

3 files changed

Lines changed: 201 additions & 0 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import {
6262
groupUndoByChangeKind,
6363
markAsClipboardEdit,
6464
} from "~/composer-undo-grouping";
65+
import { ComposerTypingMarksExtension } from "~/composer-typing-marks";
6566
import { collectInlineContextIds } from "~/lib/composerContextReferences";
6667
import { cn, isMacPlatform } from "~/lib/utils";
6768
import { basenameOfPath } from "~/pierre-icons";
@@ -832,6 +833,7 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
832833
ComposerMarkersExtension,
833834
...(richText
834835
? [
836+
ComposerTypingMarksExtension,
835837
ComposerCodeExtension,
836838
TaskList,
837839
ComposerTaskItemExtension.extend({
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
import { getSchema } from "@tiptap/core";
2+
import StarterKit from "@tiptap/starter-kit";
3+
import { history, redo, undo } from "@tiptap/pm/history";
4+
import { EditorState, TextSelection } from "@tiptap/pm/state";
5+
import { describe, expect, it } from "vite-plus/test";
6+
7+
import { buildDocJson, ComposerCodeExtension, serializeEditorDoc } from "./composer-rich-text-doc";
8+
import { createComposerTypingMarksPlugin } from "./composer-typing-marks";
9+
10+
const schema = getSchema([StarterKit.configure({ code: false }), ComposerCodeExtension]);
11+
12+
function createState(value: string, cursor: number) {
13+
const doc = schema.nodeFromJSON(buildDocJson(value, () => ({ label: "", description: null })));
14+
return EditorState.create({
15+
doc,
16+
selection: TextSelection.create(doc, cursor),
17+
plugins: [createComposerTypingMarksPlugin(), history()],
18+
});
19+
}
20+
21+
function move(state: EditorState, cursor: number) {
22+
return state.applyTransaction(state.tr.setSelection(TextSelection.create(state.doc, cursor)))
23+
.state;
24+
}
25+
26+
function type(state: EditorState, text: string) {
27+
for (const character of text)
28+
state = state.applyTransaction(state.tr.insertText(character)).state;
29+
return state;
30+
}
31+
32+
function value(state: EditorState) {
33+
return serializeEditorDoc(state.doc).value;
34+
}
35+
36+
describe("composer typing marks after moving the caret", () => {
37+
it.each(["**b**", "*b*", "~~b~~", "`b`", "***b***", "**`b`**"])(
38+
"types plain text after navigating away from and back to %s",
39+
(markdown) => {
40+
let state = createState(markdown, 2);
41+
state = state.applyTransaction(state.tr.setStoredMarks([])).state;
42+
state = move(move(state, 1), 2);
43+
expect(value(type(state, " plain"))).toBe(`${markdown} plain`);
44+
},
45+
);
46+
47+
it("covers caret placement after a controlled rewrite, including an unchanged position", () => {
48+
let state = createState("x", 2);
49+
const content = schema.nodeFromJSON(
50+
buildDocJson("**b**", () => ({ label: "", description: null })),
51+
);
52+
state = state.applyTransaction(
53+
state.tr.replaceWith(0, state.doc.content.size, content.content),
54+
).state;
55+
state = move(state, 2);
56+
expect(value(type(state, " plain"))).toBe("**b** plain");
57+
});
58+
59+
it("resets typing marks after a paste and subsequent navigation", () => {
60+
let state = createState("", 1);
61+
const content = schema.nodeFromJSON(
62+
buildDocJson("**b**", () => ({ label: "", description: null })),
63+
);
64+
state = state.applyTransaction(
65+
state.tr.replaceSelectionWith(content.firstChild!, false).setMeta("uiEvent", "paste"),
66+
).state;
67+
expect(value(type(state, " plain"))).toBe("**b** plain");
68+
state = move(move(state, 1), 2);
69+
expect(value(type(state, " plain"))).toBe("**b** plain");
70+
});
71+
72+
it("continues the following span after pasting in the middle of a line", () => {
73+
let state = createState("tail", 1);
74+
state = state.applyTransaction(
75+
state.tr
76+
.insertText("b")
77+
.addMark(1, 2, schema.marks.bold!.create())
78+
.setMeta("uiEvent", "paste"),
79+
).state;
80+
expect(value(type(state, "xy"))).toBe("**b**xytail");
81+
});
82+
83+
it("resets typing marks after dropping formatted text", () => {
84+
let state = createState("", 1);
85+
state = state.applyTransaction(
86+
state.tr
87+
.insertText("b")
88+
.addMark(1, 2, schema.marks.bold!.create())
89+
.setMeta("uiEvent", "drop"),
90+
).state;
91+
expect(value(type(state, " plain"))).toBe("**b** plain");
92+
});
93+
94+
it("preserves formatting while typing inside a span", () => {
95+
const state = move(createState("**bold**", 5), 3);
96+
expect(value(type(state, "xy"))).toBe("**boxyld**");
97+
});
98+
99+
it("adopts following text marks at a boundary", () => {
100+
const state = move(createState("**b***tail*", 6), 2);
101+
expect(value(type(state, "xy"))).toBe("**b***xytail*");
102+
});
103+
104+
it("matches the opening marker when placing the caret before formatted text", () => {
105+
const state = move(createState("x **bold**", 7), 3);
106+
expect(value(type(state, "xy"))).toBe("x **xybold**");
107+
});
108+
109+
it("preserves explicit formatting on and off over several keystrokes", () => {
110+
let state = move(createState("**b**", 2), 2);
111+
state = state.applyTransaction(state.tr.addStoredMark(schema.marks.bold!.create())).state;
112+
state = type(state, "xy");
113+
expect(value(state)).toBe("**bxy**");
114+
state = state.applyTransaction(state.tr.removeStoredMark(schema.marks.bold!)).state;
115+
expect(value(type(state, " plain"))).toBe("**bxy** plain");
116+
});
117+
118+
it("preserves an explicit mark decision in a selection transaction", () => {
119+
let state = createState("**b**", 1);
120+
state = state.applyTransaction(
121+
state.tr
122+
.setSelection(TextSelection.create(state.doc, 2))
123+
.addStoredMark(schema.marks.italic!.create()),
124+
).state;
125+
expect(value(type(state, "xy"))).toBe("**b*xy***");
126+
});
127+
128+
it("does not alter ranged selection replacement", () => {
129+
let state = createState("**bold**", 5);
130+
state = state.applyTransaction(
131+
state.tr.setSelection(TextSelection.create(state.doc, 2, 4)),
132+
).state;
133+
expect(value(type(state, "x"))).toBe("**bxd**");
134+
});
135+
136+
it("leaves normal typing transactions alone", () => {
137+
const state = createState("**b**", 2);
138+
expect(value(type(state, "xy"))).toBe("**bxy**");
139+
expect(state.applyTransaction(state.tr.scrollIntoView()).transactions).toHaveLength(1);
140+
});
141+
142+
it("adds no undo step and preserves undo/redo content and selection", () => {
143+
let state = move(move(createState("**b**", 2), 1), 2);
144+
state = type(state, " plain");
145+
expect(
146+
undo(state, (transaction) => {
147+
state = state.applyTransaction(transaction).state;
148+
}),
149+
).toBe(true);
150+
expect(value(state)).toBe("**b**");
151+
expect(state.selection.from).toBe(2);
152+
expect(value(type(state, " again"))).toBe("**b** again");
153+
expect(
154+
redo(state, (transaction) => {
155+
state = state.applyTransaction(transaction).state;
156+
}),
157+
).toBe(true);
158+
expect(value(state)).toBe("**b** plain");
159+
expect(state.selection.from).toBe(8);
160+
});
161+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { Extension } from "@tiptap/core";
2+
import { isHistoryTransaction } from "@tiptap/pm/history";
3+
import { Mark } from "@tiptap/pm/model";
4+
import { Plugin } from "@tiptap/pm/state";
5+
6+
/**
7+
* Follow the text on the right when moving the caret, or type plain at a line end.
8+
* Keep inclusive marks during edits so explicit formatting shortcuts continue typing.
9+
*/
10+
export function createComposerTypingMarksPlugin() {
11+
return new Plugin({
12+
appendTransaction(transactions, _oldState, state) {
13+
if (
14+
!state.selection.empty ||
15+
state.storedMarks !== null ||
16+
!transactions.some(
17+
(transaction) =>
18+
(transaction.selectionSet && !transaction.docChanged) ||
19+
isHistoryTransaction(transaction) ||
20+
transaction.getMeta("uiEvent") === "paste" ||
21+
transaction.getMeta("uiEvent") === "drop",
22+
)
23+
) {
24+
return null;
25+
}
26+
const marks = state.selection.$from.nodeAfter?.marks ?? [];
27+
if (Mark.sameSet(marks, state.selection.$from.marks())) return null;
28+
return state.tr.setStoredMarks(marks);
29+
},
30+
});
31+
}
32+
33+
export const ComposerTypingMarksExtension = Extension.create({
34+
name: "composer-typing-marks",
35+
addProseMirrorPlugins() {
36+
return [createComposerTypingMarksPlugin()];
37+
},
38+
});

0 commit comments

Comments
 (0)