Skip to content

Commit 707e3d6

Browse files
committed
fix(studio): a press refuses, as main did, once its edit is undone and a newer edit exists
1 parent 5f4055d commit 707e3d6

2 files changed

Lines changed: 63 additions & 3 deletions

File tree

‎packages/studio/src/hooks/usePersistentEditHistory.test.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,57 @@ it("does not undo again an edit a later press undid first", async () => {
265265
expect(file()).toBe("A");
266266
});
267267

268+
it("refuses a second press whose edit is undone once an edit was made after the press", async () => {
269+
const { dir, hook, file, save, readFile } = await studio();
270+
save("B");
271+
await act(() =>
272+
hook().recordEdit({
273+
label: "Moved Title",
274+
files: { "index.html": { before: "A", after: "B" } },
275+
}),
276+
);
277+
await vi.waitFor(() => expect(hook().undoLabel).toBe("Moved Title"));
278+
const atKey = hook().claims();
279+
save("C");
280+
await act(() =>
281+
hook().recordEdit({
282+
label: "Moved Card",
283+
files: { "index.html": { before: "B", after: "C" } },
284+
}),
285+
);
286+
let open!: () => void;
287+
const opened = new Promise<void>((resolve) => {
288+
open = resolve;
289+
});
290+
let queue: Promise<unknown> = Promise.resolve();
291+
let queued = 0;
292+
const serialize = <T>(_paths: readonly string[], task: () => Promise<T>) => {
293+
queued += 1;
294+
const wait = queued === 2 ? opened : Promise.resolve();
295+
const run = queue.then(() => wait).then(task);
296+
queue = run.catch(() => undefined);
297+
return run;
298+
};
299+
const press = () => hook().undo({ readFile, claimedAfter: atKey, serialize });
300+
const presses = Promise.all([press(), press()]);
301+
await vi.waitFor(() => expect(file()).toBe("B"));
302+
writeFileSync(join(dir, "card.html"), "Y");
303+
await act(() =>
304+
hook().recordEdit({
305+
label: "Added Card",
306+
files: { "card.html": { before: "", after: "Y" } },
307+
}),
308+
);
309+
310+
open();
311+
const [first, second] = await act(() => presses);
312+
313+
expect(first).toMatchObject({ ok: true, label: "Undid: Moved Card" });
314+
expect(second).toMatchObject({ ok: false, reason: "content-mismatch" });
315+
expect(readFileSync(join(dir, "card.html"), "utf8")).toBe("Y");
316+
expect(file()).toBe("B");
317+
});
318+
268319
it("a drag's edits under one key undo as one step, even before the drag goes idle", async () => {
269320
const { hook, file, save, readFile } = await studio();
270321
const writes: Array<[before: string, after: string]> = [

‎packages/studio/src/hooks/usePersistentEditHistory.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ function createOwnHistory() {
7979
changes: () => changes,
8080
claimCount: () => claimed.count,
8181
claimedAfter: (count: number) => (claimed.count > count && claimed.id ? claimed.id : null),
82-
unlessUndone: (id: string | null) => (id && !undoneIds.has(id) ? id : null),
82+
targetFor: (id: string | null, pressedAt: number) =>
83+
!id || !undoneIds.has(id) ? id : claimed.count > pressedAt ? false : null,
8384
noteClaim: (id: string) => {
8485
claimed = { count: claimed.count + 1, id };
8586
},
@@ -126,6 +127,10 @@ function historyUrl(projectId: string, path = ""): string {
126127
return `/api/projects/${encodeURIComponent(projectId)}/history${path}`;
127128
}
128129

130+
async function cantUndo(paths: string[] | undefined): Promise<ApplyResult> {
131+
return { ok: false, reason: "content-mismatch", paths: paths ?? [] };
132+
}
133+
129134
function unionPaths(...lists: Array<readonly string[] | undefined>): string[] {
130135
return [...new Set(lists.flatMap((list) => list ?? []))];
131136
}
@@ -261,9 +266,9 @@ export function usePersistentEditHistory({ projectId }: UsePersistentEditHistory
261266
const next = direction === "undo" ? view.back : view.forward;
262267
const stepPaths = candidate ? Object.keys(own.afterOf(candidate)) : next?.paths;
263268
const paths = unionPaths(stepPaths, heldClaimRef.current?.paths);
269+
const pressedAt = own.claimCount();
264270
own.overtake();
265-
const run = async (): Promise<ApplyResult> => {
266-
const target = own.unlessUndone(candidate);
271+
const attempt = async (target: string | null): Promise<ApplyResult> => {
267272
const previous = await readAll(paths, callbacks.readFile);
268273
const posted = await post(
269274
historyUrl(projectId, target ? "/undo" : "/step"),
@@ -295,6 +300,10 @@ export function usePersistentEditHistory({ projectId }: UsePersistentEditHistory
295300
),
296301
};
297302
};
303+
const run = () => {
304+
const target = own.targetFor(candidate, pressedAt);
305+
return target === false ? cantUndo(stepPaths) : attempt(target);
306+
};
298307
return callbacks.serialize ? callbacks.serialize(paths, run) : run();
299308
},
300309
[projectId, view, refresh, own],

0 commit comments

Comments
 (0)