mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-27 08:03:37 +00:00
fix(diff): text typed into a blank diff survives switching to another tab (#3784)
This commit is contained in:
@@ -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<DiffData, { left?: Text; right?: Text }>();
|
||||
|
||||
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<Attached | null>(null);
|
||||
const [binary, setBinary] = useState<boolean | null>(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") =>
|
||||
|
||||
@@ -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<HTMLElement>(".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(<DiffEditor input={input} />);
|
||||
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(<DiffEditor input={input} />);
|
||||
await waitFor(() => expect(pane(0).state.doc.toString()).toBe("left text"));
|
||||
expect(pane(1).state.doc.toString()).toBe("right text");
|
||||
});
|
||||
Reference in New Issue
Block a user