🐛 Undo a sync with the change its group was stacked into

Ungrouping a board in a grid main whose copies are ungrouped alike took
two Ctrl+Z to undo: the first only moved the copies' children back. The
layout update after the ungroup stacks onto its history entry with an
undo group of its own, which the entry records among the groups stacked
into it. The component sync that update causes commits later under that
group without stacking, and `append-undo` gave it a new entry when the
matching entry was the latest. Undo walks entries by their own undo
group, so the new entry was undone alone.

Such a commit now joins the entry that holds its group among the stacked
ones. Commits of one group in a row still keep their entries, which undo
walks together.

Tests: `workspace-undo-test/a-delayed-commit-joins-the-entry-its-group-
was-stacked-into` (fails before the fix), `adjacent-commits-of-one-group-
keep-their-entries`, and the frontend generated `generated-an-ungroup-in-
a-grid-main-undoes-in-one-step`.

ClojureScript only; the benchmark covers JVM code and is not run.

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-15 18:01:19 +01:00
parent fb271c00e5
commit 57c2cff1e1
3 changed files with 39 additions and 2 deletions

View File

@ -199,9 +199,13 @@
(take (inc index) (get-in state [:workspace-undo :items])))))]
(cond
;; A delayed derived change still belongs to its primary action,
;; even after another edit or transaction has started.
;; even after another edit or transaction has started. An entry
;; that holds the group only among the groups stacked into it
;; takes the change too: undo walks entries by their own group.
(and (some? group-index)
(or stack? (< group-index index) transaction))
(or stack? (< group-index index) transaction
(not= (:undo-group entry)
(get-in state [:workspace-undo :items group-index :undo-group]))))
(stack-undo-entry state entry group-index)
(and (get-in state [:workspace-undo :transaction])

View File

@ -1118,6 +1118,12 @@
:ops [[:rotate 16 0 7 0] [:grid-direction 0 0 0 1] [:grid-cell 0 10 0 1] [:add-child 0]
[:structure 3 6 15] [:grid-direction 0 0 0 1] [:grid-cell 0 4 2 1]]})))
(t/deftest ^:async generated-an-ungroup-in-a-grid-main-undoes-in-one-step
;; the copies' repositioning, synced from the layout update after the
;; ungroup, undoes with the ungroup
(await (replay {:world [2 2 {:variants true :text true :layout :grid :layout-copies true}]
:ops [[:detach 6 0 0 0] [:structure 0 2 0]]})))
(t/deftest ^:async generated-a-detach-under-an-unaccepted-child-removal-leaves-it-plain
;; the loaded library no longer has the nested copy's child main, so the
;; detach leaves that child plain until the consumer accepts the library

View File

@ -159,6 +159,33 @@
(t/is (= 1 (count (get-in state [:workspace-undo :items]))))
(t/is (= #{g1 g2} (get-in state [:workspace-undo :items 0 :undo-groups])))))
;; Scenario: an ungroup commits (g1), the layout update after it stacks onto
;; its entry with a group of its own (g2), and the component sync that update
;; causes commits later under g2 without stacking. Proves: the sync joins the
;; same entry, so one Ctrl+Z undoes all three; a separate entry under g2
;; would be undone alone, as undo walks entries by their own group.
(t/deftest a-delayed-commit-joins-the-entry-its-group-was-stacked-into
(let [page-id (uuid/next)
[g1 g2] (repeatedly 2 uuid/next)
state (reduce #(ptk/update %2 %1)
{}
[(dwu/append-undo (entry page-id "A" g1) false)
(dwu/append-undo (entry page-id "B" g2) true)
(dwu/append-undo (entry page-id "C" g2) false)])]
(t/is (= 1 (count (get-in state [:workspace-undo :items]))))
(t/is (= ["A" "B" "C"] (mapv :name (get-in state [:workspace-undo :items 0 :redo-changes]))))))
;; Scenario: two commits of one group in a row, without stacking. Proves:
;; they keep separate entries, which undo walks together by their group.
(t/deftest adjacent-commits-of-one-group-keep-their-entries
(let [page-id (uuid/next)
g1 (uuid/next)
state (reduce #(ptk/update %2 %1)
{}
[(dwu/append-undo (entry page-id "A" g1) false)
(dwu/append-undo (entry page-id "B" g1) false)])]
(t/is (= 2 (count (get-in state [:workspace-undo :items]))))))
;; Scenario: the user presses Ctrl+Z on a page rename. Proves: the commit it
;; makes can be undone (its undo changes redo the rename) and names the index
;; the history moved from, so a rollback can put both back.