From 1dbd2847b94d08cc2ea2634fc385329e72c953af Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Mon, 16 Nov 2026 23:35:40 +0800 Subject: [PATCH] fix(diff): text typed into a blank diff survives switching to another tab (#3784) --- src/features/vcs/DiffEditor.tsx | 22 +++++++++---- .../vcs/__tests__/blankDiffTypedText.test.tsx | 33 +++++++++++++++++++ 2 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 src/features/vcs/__tests__/blankDiffTypedText.test.tsx diff --git a/src/features/vcs/DiffEditor.tsx b/src/features/vcs/DiffEditor.tsx index 686a01016..5cb011e1c 100644 --- a/src/features/vcs/DiffEditor.tsx +++ b/src/features/vcs/DiffEditor.tsx @@ -249,6 +249,18 @@ function isStrokeOf(e: KeyboardEvent, actionId: string): boolean { return !!stroke && bindingsFor(actionId).some((b) => normalizeBinding(b) === stroke); } +/** + * The typed text of editable text sides (by the request's side), per diff request: it outlives + * rebuilt views and the editor unmounting while its tab is hidden. + */ +const typedSides = new WeakMap(); + +function typedSidesOf(data: DiffData): { left?: Text; right?: Text } { + let typed = typedSides.get(data); + if (!typed) typedSides.set(data, (typed = {})); + return typed; +} + export function DiffEditor({ input, toolbarActions, @@ -284,10 +296,8 @@ export function DiffEditor({ [input_, swapped], ); const { path, left, right } = data; - /** The typed text of editable text sides (by the request's side), kept across rebuilt views. */ - const edited = useRef<{ of: DiffData; left?: Text; right?: Text }>({ of: input_ }); - if (edited.current.of !== input_) edited.current = { of: input_ }; - const editedText = (s: Side) => edited.current[originOf(s, swapped)]?.toString(); + const edited = typedSidesOf(input_); + const editedText = (s: Side) => edited[originOf(s, swapped)]?.toString(); const [attached, setAttached] = useState(null); const [binary, setBinary] = useState(null); /** Text of the sides that are not live documents. */ @@ -464,7 +474,7 @@ export function DiffEditor({ useEffect(() => { const host = hostRef.current; if (!host || !attached || !statics || lang === null) return; - const typed = (s: Side) => edited.current[originOf(s, swapped)]?.toString(); + const typed = (s: Side) => edited[originOf(s, swapped)]?.toString(); const textA = typed("a") ?? attached.a?.text() ?? statics.a ?? ""; const textB = typed("b") ?? attached.b?.text() ?? statics.b ?? ""; const prev = viewsRef.current; @@ -483,7 +493,7 @@ export function DiffEditor({ const keepEdits = (s: Side) => isEditableText(s === "a" ? left : right) ? EditorView.updateListener.of((u) => { - if (u.docChanged) edited.current[originOf(s, swapped)] = u.state.doc; + if (u.docChanged) edited[originOf(s, swapped)] = u.state.doc; }) : []; const focusTracker = (side: "a" | "b") => diff --git a/src/features/vcs/__tests__/blankDiffTypedText.test.tsx b/src/features/vcs/__tests__/blankDiffTypedText.test.tsx new file mode 100644 index 000000000..542153985 --- /dev/null +++ b/src/features/vcs/__tests__/blankDiffTypedText.test.tsx @@ -0,0 +1,33 @@ +import { afterEach, expect, it } from "vitest"; +import { act, cleanup, render, waitFor } from "@testing-library/react"; +import { EditorView } from "@codemirror/view"; +import type { EditorInput } from "@/workbench/contributions"; +import { DiffEditor } from "../DiffEditor"; + +afterEach(cleanup); + +const pane = (index: number) => + EditorView.findFromDOM(document.querySelectorAll(".cm-mergeViewEditor .cm-editor")[index]!)!; + +it("keeps text typed into a blank diff while its tab is hidden and unmounted", async () => { + const input = { + kind: "diff", + key: "diff:blank", + data: { + path: "", + left: { type: "text", text: "", label: "", editable: true }, + right: { type: "text", text: "", label: "", editable: true }, + }, + } as unknown as EditorInput; + const first = render(); + await waitFor(() => expect(document.querySelectorAll(".cm-mergeViewEditor .cm-editor")).toHaveLength(2)); + act(() => { + pane(0).dispatch({ changes: { from: 0, insert: "left text" } }); + pane(1).dispatch({ changes: { from: 0, insert: "right text" } }); + }); + first.unmount(); + + render(); + await waitFor(() => expect(pane(0).state.doc.toString()).toBe("left text")); + expect(pane(1).state.doc.toString()).toBe("right text"); +});