Skip to content
TypeScript

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

Johannes Billefcf77fc726b
diff --git a/apps/web/src/components/ComposerPromptEditorTiptap.input.test.tsx b/apps/web/src/components/ComposerPromptEditorTiptap.input.test.tsx
new file mode 100644
index 00000000000..d4f82a829fb
--- /dev/null
+++ b/apps/web/src/components/ComposerPromptEditorTiptap.input.test.tsx
@@ -0,0 +1,140 @@
+// @vitest-environment jsdom
+
+import type { Editor } from "@tiptap/core";
+import { act, createRef } from "react";
+import { createRoot, type Root } from "react-dom/client";
+import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
+
+import { serializeEditorDoc } from "~/composer-rich-text-doc";
+import { ComposerPromptEditor, type ComposerPromptEditorHandle } from "./ComposerPromptEditor";
+
+let root: Root;
+let container: HTMLDivElement;
+
+beforeEach(() => {
+ vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
+ container = document.createElement("div");
+ document.body.append(container);
+ root = createRoot(container);
+});
+
+afterEach(async () => {
+ await act(async () => root.unmount());
+ container.remove();
+ vi.unstubAllGlobals();
+});
+
+async function mountEditor(value: string, richTextEnabled = true) {
+ await act(async () =>
+ root.render(
+ <ComposerPromptEditor
+ editorRef={createRef<ComposerPromptEditorHandle>()}
+ value={value}
+ cursor={value.length}
+ richTextEnabled={richTextEnabled}
+ contextRecords={new Map()}
+ skills={[]}
+ disabled={false}
+ placeholder=""
+ onChange={() => {}}
+ onPaste={() => {}}
+ />,
+ ),
+ );
+ const element = container.querySelector<HTMLElement & { editor: Editor }>(".tiptap");
+ if (!element) throw new Error("Composer did not mount");
+ element.editor.view.setProps({ handleScrollToSelection: () => true });
+ return element.editor;
+}
+
+async function replaceDomText(editor: Editor, text: string, isComposing = false) {
+ await act(async () => {
+ const node = editor.view.dom.querySelector("p")?.firstChild;
+ if (!(node instanceof Text)) throw new Error("Composition text is missing");
+ node.data = text;
+ window.getSelection()?.collapse(node, text.length);
+ if (isComposing) {
+ editor.view.dom.dispatchEvent(new CompositionEvent("compositionupdate", { data: text }));
+ }
+ editor.view.dom.dispatchEvent(
+ new InputEvent("input", {
+ inputType: isComposing ? "insertCompositionText" : "insertText",
+ data: text,
+ isComposing,
+ }),
+ );
+ });
+}
+
+function typeText(editor: Editor, from: number, to: number, text: string) {
+ const insert = () => editor.state.tr.insertText(text, from, to);
+ if (
+ !editor.view.someProp("handleTextInput", (handler) =>
+ handler(editor.view, from, to, text, insert),
+ )
+ ) {
+ editor.view.dispatch(insert());
+ }
+}
+
+it.each([false, true])(
+ "composes an umlaut without wrapping the preedit text (rich text: %s)",
+ async (richTextEnabled) => {
+ // The middle dot stands in for the provisional text shown by the Compose key.
+ const editor = await mountEditor("·", richTextEnabled);
+ await act(async () => {
+ editor.view.focus();
+ editor.view.dom.dispatchEvent(new CompositionEvent("compositionstart"));
+ });
+ await replaceDomText(editor, '"', true);
+ expect(serializeEditorDoc(editor.state.doc).value).toBe('"');
+ await replaceDomText(editor, "ö", true);
+ await act(async () => {
+ editor.view.dom.dispatchEvent(new CompositionEvent("compositionend", { data: "ö" }));
+ });
+ expect(serializeEditorDoc(editor.state.doc).value).toBe("ö");
+ },
+);
+
+it("commits a composed quote without surrounding the preedit text", async () => {
+ const editor = await mountEditor("·");
+ await act(async () => {
+ editor.view.focus();
+ editor.view.dom.dispatchEvent(new CompositionEvent("compositionstart"));
+ editor.view.dom.dispatchEvent(new CompositionEvent("compositionend", { data: '"' }));
+ });
+ await replaceDomText(editor, '"');
+ expect(serializeEditorDoc(editor.state.doc).value).toBe('"');
+});
+
+it("lets composition replace selected text without surrounding it", async () => {
+ const editor = await mountEditor("word");
+ await act(async () => {
+ editor.commands.setTextSelection({ from: 1, to: 5 });
+ editor.view.dom.dispatchEvent(new CompositionEvent("compositionstart"));
+ typeText(editor, 1, 5, '"');
+ });
+ expect(serializeEditorDoc(editor.state.doc).value).toBe('"');
+});
+
+it("replaces text outside the selected range without surrounding it", async () => {
+ const editor = await mountEditor("word");
+ await act(async () => {
+ editor.commands.setTextSelection({ from: 1, to: 5 });
+ typeText(editor, 2, 3, '"');
+ });
+ expect(serializeEditorDoc(editor.state.doc).value).toBe('w"rd');
+});
+
+it.each([false, true])(
+ "still surrounds selected text with quotes (rich text: %s)",
+ async (richTextEnabled) => {
+ const editor = await mountEditor("word", richTextEnabled);
+ await act(async () => {
+ editor.commands.setTextSelection({ from: 1, to: 5 });
+ editor.view.focus();
+ });
+ await replaceDomText(editor, '"');
+ expect(serializeEditorDoc(editor.state.doc).value).toBe('"word"');
+ },
+);
diff --git a/apps/web/src/components/ComposerPromptEditorTiptap.tsx b/apps/web/src/components/ComposerPromptEditorTiptap.tsx
index f3ba1ee9542..14562f73cd4 100644
--- a/apps/web/src/components/ComposerPromptEditorTiptap.tsx
+++ b/apps/web/src/components/ComposerPromptEditorTiptap.tsx
@@ -1296,9 +1296,19 @@ function ComposerPromptEditorTiptapInner(props: ComposerPromptEditorProps) {
return handled;
},
handleTextInput: (view, from, to, text) => {
- if (literalText || text.length !== 1) return false;
+ if (literalText || view.composing || text.length !== 1) return false;
const closer = SURROUND_CLOSE[text];
- if (!closer || from === to) return false;
+ const { selection } = view.state;
+ // Compose preedit updates replace a range even without a user selection.
+ if (
+ !closer ||
+ !(selection instanceof TextSelection) ||
+ selection.empty ||
+ selection.from !== from ||
+ selection.to !== to
+ ) {
+ return false;
+ }
// Never wrap chips or other atoms, and never wrap styled text: the
// default replace keeps marks intact, wrapping would drop them.
let touchesSpecial = false;