Skip to content

Open source CapCut alternative.

TypeScript

fix(site): undo keeps the selection on what survives the restore

David Leef00108a9b46
diff --git a/site/src/cut/lib/store.test.ts b/site/src/cut/lib/store.test.ts
index 675738dc..e2bed4ad 100644
--- a/site/src/cut/lib/store.test.ts
+++ b/site/src/cut/lib/store.test.ts
@@ -100,6 +100,52 @@ beforeEach(() => {
});
});
+describe("brush stroke history", () => {
+ // The removal brush writes each finished stroke as pushHistory + a
+ // transient seeds update; one undo steps back exactly one stroke.
+ test("each stroke is one undo step", () => {
+ const clip = vclip({ removal: { mode: "custom" } });
+ useEditor.setState({ clips: [clip] });
+ const stroke = (t: number) => {
+ const st = useEditor.getState();
+ const cur = st.clips[0].removal!;
+ st.pushHistory();
+ st.updateClipTransient(clip.id, {
+ removal: {
+ ...cur,
+ seeds: {
+ prompts: [
+ ...(cur.seeds?.prompts ?? []),
+ { t, points: [{ x: 0.5, y: 0.5, label: 1 as const }] },
+ ],
+ },
+ },
+ });
+ };
+ useEditor.setState({ selection: { kind: "clip", id: clip.id } });
+ stroke(0);
+ stroke(1);
+ expect(useEditor.getState().clips[0].removal?.seeds?.prompts.length).toBe(2);
+ useEditor.getState().undo();
+ expect(useEditor.getState().clips[0].removal?.seeds?.prompts.length).toBe(1);
+ // The clip survived the restore, so the brush session's selection holds
+ // and the stage keeps its overlay.
+ expect(useEditor.getState().selection).toEqual({ kind: "clip", id: clip.id });
+ useEditor.getState().undo();
+ expect(useEditor.getState().clips[0].removal?.seeds?.prompts ?? []).toHaveLength(0);
+ });
+
+ test("undoing an add drops the selection with the clip", () => {
+ useEditor.setState({ clips: [], selection: null, multiSelection: [] });
+ useEditor.getState().pushHistory();
+ const clip = vclip({});
+ useEditor.setState({ clips: [clip], selection: { kind: "clip", id: clip.id } });
+ useEditor.getState().undo();
+ expect(useEditor.getState().clips).toHaveLength(0);
+ expect(useEditor.getState().selection).toBeNull();
+ });
+});
+
describe("the document projection", () => {
// Autosave tells an edit from a re-read by comparing this projection's
// identity. A projection that rebuilt itself on every read would mark the
diff --git a/site/src/cut/lib/store.ts b/site/src/cut/lib/store.ts
index 67341e93..8542bf7b 100644
--- a/site/src/cut/lib/store.ts
+++ b/site/src/cut/lib/store.ts
@@ -1384,10 +1384,13 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {
/** Apply a history snapshot, re-attaching the render-owned clips it omitted so
* an undo/redo never disturbs a background run's placements. The live gen
- * clips are read at restore time, so the set is always current. */
+ * clips are read at restore time, so the set is always current. The
+ * selection survives when what it points at still exists in the restored
+ * doc — an undo mid-session (a brush stroke, a panel edit) keeps the panel
+ * and its stage gizmos on the clip being worked. */
const restoreDoc = (snap: DocSnapshot) => {
const { beats, ...doc } = snap;
- const { clips, audioClips, assets } = get();
+ const { clips, audioClips, assets, selection, multiSelection } = get();
const genClips = clips.filter((c) => genClipIds.has(c.id));
const genAudio = audioClips.filter((c) => genAudioIds.has(c.id));
// Beat grids go back onto the assets they came off. An asset imported
@@ -1397,13 +1400,31 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {
grids.has(a.id) && grids.get(a.id) !== a.beats ? { ...a, beats: grids.get(a.id) } : a
);
const beatsMoved = withBeats.some((a, i) => a !== assets[i]);
+ const nextClips = [...doc.clips, ...genClips].sort((a, b) => a.start - b.start);
+ const nextAudio = [...doc.audioClips, ...genAudio];
+ const survives = (sel: Selection): boolean => {
+ if (!sel) return false;
+ switch (sel.kind) {
+ case "clip":
+ return nextClips.some((c) => c.id === sel.id);
+ case "audio":
+ return nextAudio.some((c) => c.id === sel.id);
+ case "overlay":
+ return doc.overlays.some((o) => o.id === sel.id);
+ case "cue":
+ return doc.subtitles.cues.some((c) => c.id === sel.id);
+ case "transition":
+ return doc.transitions.some((t) => t.id === sel.id);
+ }
+ };
+ const nextMulti = multiSelection.filter(survives);
set({
...doc,
- clips: [...doc.clips, ...genClips].sort((a, b) => a.start - b.start),
- audioClips: [...doc.audioClips, ...genAudio],
+ clips: nextClips,
+ audioClips: nextAudio,
...(beatsMoved ? { assets: withBeats } : {}),
- selection: null,
- multiSelection: [],
+ selection: survives(selection) ? selection : nextMulti[nextMulti.length - 1] ?? null,
+ multiSelection: nextMulti,
});
};