@devries-cameron20 / t3code-1 / commits / efcf77fc72
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(web): Compose input no longer adds stray quotes (#20350)

Johannes Bill committed Oct 18, 2026 at 22:05 UTC efcf77fc726bf7945806b1940500a96a97bde9d7
2 files changed +152 -2
apps/web/src/components/ComposerPromptEditorTiptap.input.test.tsx new
+140
@@ -0,0 +1,140 @@
1 +// @vitest-environment jsdom
2 +
3 +import type { Editor } from "@tiptap/core";
4 +import { act, createRef } from "react";
5 +import { createRoot, type Root } from "react-dom/client";
6 +import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
7 +
8 +import { serializeEditorDoc } from "~/composer-rich-text-doc";
9 +import { ComposerPromptEditor, type ComposerPromptEditorHandle } from "./ComposerPromptEditor";
10 +
11 +let root: Root;
12 +let container: HTMLDivElement;
13 +
14 +beforeEach(() => {
15 + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
16 + container = document.createElement("div");
17 + document.body.append(container);
18 + root = createRoot(container);
19 +});
20 +
21 +afterEach(async () => {
22 + await act(async () => root.unmount());
23 + container.remove();
24 + vi.unstubAllGlobals();
25 +});
26 +
27 +async function mountEditor(value: string, richTextEnabled = true) {
28 + await act(async () =>
29 + root.render(
30 + <ComposerPromptEditor
31 + editorRef={createRef<ComposerPromptEditorHandle>()}
32 + value={value}
33 + cursor={value.length}
34 + richTextEnabled={richTextEnabled}
35 + contextRecords={new Map()}
36 + skills={[]}
37 + disabled={false}
38 + placeholder=""
39 + onChange={() => {}}
40 + onPaste={() => {}}
41 + />,
42 + ),
43 + );
44 + const element = container.querySelector<HTMLElement & { editor: Editor }>(".tiptap");
45 + if (!element) throw new Error("Composer did not mount");
46 + element.editor.view.setProps({ handleScrollToSelection: () => true });
47 + return element.editor;
48 +}
49 +
50 +async function replaceDomText(editor: Editor, text: string, isComposing = false) {
51 + await act(async () => {
52 + const node = editor.view.dom.querySelector("p")?.firstChild;
53 + if (!(node instanceof Text)) throw new Error("Composition text is missing");
54 + node.data = text;
55 + window.getSelection()?.collapse(node, text.length);
56 + if (isComposing) {
57 + editor.view.dom.dispatchEvent(new CompositionEvent("compositionupdate", { data: text }));
58 + }
59 + editor.view.dom.dispatchEvent(
60 + new InputEvent("input", {
61 + inputType: isComposing ? "insertCompositionText" : "insertText",
62 + data: text,
63 + isComposing,
64 + }),
65 + );
66 + });
67 +}
68 +
69 +function typeText(editor: Editor, from: number, to: number, text: string) {
70 + const insert = () => editor.state.tr.insertText(text, from, to);
71 + if (
72 + !editor.view.someProp("handleTextInput", (handler) =>
73 + handler(editor.view, from, to, text, insert),
74 + )
75 + ) {
76 + editor.view.dispatch(insert());
77 + }
78 +}
79 +
80 +it.each([false, true])(
81 + "composes an umlaut without wrapping the preedit text (rich text: %s)",
82 + async (richTextEnabled) => {
83 + // The middle dot stands in for the provisional text shown by the Compose key.
84 + const editor = await mountEditor("·", richTextEnabled);
85 + await act(async () => {
86 + editor.view.focus();
87 + editor.view.dom.dispatchEvent(new CompositionEvent("compositionstart"));
88 + });
89 + await replaceDomText(editor, '"', true);
90 + expect(serializeEditorDoc(editor.state.doc).value).toBe('"');
91 + await replaceDomText(editor, "ö", true);
92 + await act(async () => {
93 + editor.view.dom.dispatchEvent(new CompositionEvent("compositionend", { data: "ö" }));
94 + });
95 + expect(serializeEditorDoc(editor.state.doc).value).toBe("ö");
96 + },
97 +);
98 +
99 +it("commits a composed quote without surrounding the preedit text", async () => {
100 + const editor = await mountEditor("·");
101 + await act(async () => {
102 + editor.view.focus();
103 + editor.view.dom.dispatchEvent(new CompositionEvent("compositionstart"));
104 + editor.view.dom.dispatchEvent(new CompositionEvent("compositionend", { data: '"' }));
105 + });
106 + await replaceDomText(editor, '"');
107 + expect(serializeEditorDoc(editor.state.doc).value).toBe('"');
108 +});
109 +
110 +it("lets composition replace selected text without surrounding it", async () => {
111 + const editor = await mountEditor("word");
112 + await act(async () => {
113 + editor.commands.setTextSelection({ from: 1, to: 5 });
114 + editor.view.dom.dispatchEvent(new CompositionEvent("compositionstart"));
115 + typeText(editor, 1, 5, '"');
116 + });
117 + expect(serializeEditorDoc(editor.state.doc).value).toBe('"');
118 +});
119 +
120 +it("replaces text outside the selected range without surrounding it", async () => {
121 + const editor = await mountEditor("word");
122 + await act(async () => {
123 + editor.commands.setTextSelection({ from: 1, to: 5 });
124 + typeText(editor, 2, 3, '"');
125 + });
126 + expect(serializeEditorDoc(editor.state.doc).value).toBe('w"rd');
127 +});
128 +
129 +it.each([false, true])(
130 + "still surrounds selected text with quotes (rich text: %s)",
131 + async (richTextEnabled) => {
132 + const editor = await mountEditor("word", richTextEnabled);
133 + await act(async () => {
134 + editor.commands.setTextSelection({ from: 1, to: 5 });
135 + editor.view.focus();
136 + });
137 + await replaceDomText(editor, '"');
138 + expect(serializeEditorDoc(editor.state.doc).value).toBe('"word"');
139 + },
140 +);
apps/web/src/components/ComposerPromptEditorTiptap.tsx
+12 -2
@@ -1296,9 +1296,19 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
1296 return handled;
1297 },
1298 handleTextInput: (view, from, to, text) => {
1299 - if (literalText || text.length !== 1) return false;
1299 + if (literalText || view.composing || text.length !== 1) return false;
1300 const closer = SURROUND_CLOSE[text];
1301 - if (!closer || from === to) return false;
1301 + const { selection } = view.state;
1302 + // Compose preedit updates replace a range even without a user selection.
1303 + if (
1304 + !closer ||
1305 + !(selection instanceof TextSelection) ||
1306 + selection.empty ||
1307 + selection.from !== from ||
1308 + selection.to !== to
1309 + ) {
1310 + return false;
1311 + }
1312 // Never wrap chips or other atoms, and never wrap styled text: the
1313 // default replace keeps marks intact, wrapping would drop them.
1314 let touchesSpecial = false;