diff --git a/.serena/memories/frontend/workspace-state-persistence-subtleties.md b/.serena/memories/frontend/workspace-state-persistence-subtleties.md index 7e8196080f..ef2eae493c 100644 --- a/.serena/memories/frontend/workspace-state-persistence-subtleties.md +++ b/.serena/memories/frontend/workspace-state-persistence-subtleties.md @@ -43,7 +43,7 @@ - Exports (exporter and client/WASM PDF/SVG/PNG alike), the dashboard `dd/duplicate-files` and the `.penpot` download call `force-persist-files-and-wait file-ids timeout` first. A client export draws local data, so its warning says the export includes unsaved changes (`workspace.export.unsaved-changes-included`); exporter exports and the duplicate copy server data and warn they miss them. `duplicate-files` owns its one toast, shown once every copy exists: the plural `dashboard.duplicate-unsaved-changes` warning when the files had unsaved changes before the copy request, else the success toast; a failed copy errors the stream, so neither shows (the file menu must not emit its own success toast, the single-toast store would let it replace the warning). - Exports and the `.penpot` download call `force-persist-files-and-wait file-ids timeout` (waits only for those files, ends on `:error` or timeout, never raises) and then `unsaved-changes? state file-ids` to warn; it reads `refs/persistence`, so tests mock it with `(l/derived :persistence store)`. - `persistence-failed` emits a `save-failed` audit event (`:cause-type :code :retrying :queued-commits`) whenever the user is notified (`notify-failure?`, same rule as the toast), including delegated auth failures. -- `start-undo-transaction` stamps the transaction with a fresh `:commit-group` (dropped from the history entry); `commit-changes` uses `(or undo-group (:undo-group tx) (:commit-group tx) (uuid/next))`, so all commits of one transaction are one rollback unit. Pre-setting `:undo-group` instead breaks entries whose group is set later (sync transactions). +- `start-undo-transaction` stamps the transaction with a fresh `:commit-group` (dropped from the history entry); `commit-changes` uses `(or undo-group (:undo-group tx) (:commit-group tx) (uuid/next))`, so all commits of one transaction are one rollback unit. `` (`get-stacked-undo-group`) is the `:undo-group` of the entry at the current undo index, taken only by a `:stack-undo? true` commit that records undo (`save-undo?` and non-empty undo changes, the history watcher's test) and only when the open transaction holds no changes (else `append-undo` accumulates it into the transaction): the commits it starts (component sync after a main text resizes) inherit it, so one Ctrl+Z reverts the whole action. Heals (`:stack-undo? true :save-undo? false`) never take it, or a rejected heal would roll back the user's last action. Known limit: a late stacked commit (font-load resize) that lands while an unrelated user transaction is open but still empty (colorpicker mousedown) takes the previous entry's group; its sync reuses that transaction (`start-undo-transaction` keeps an open one) and sets the transaction's `:undo-group`, so the user's next action undoes together with the previous one. Rare and loses no data; left as is. Pre-setting `:undo-group` instead breaks entries whose group is set later (sync transactions). - Undo transactions can stay open only temporarily; timed-out pending transactions are force-committed after about 20s. Undo entries are capped at 50. - Undo/redo are ignored while a normal editor/drawing interaction is active, except grid-layout edition handles undo through this path. `undo`, `redo` and `undo-to-index` also do nothing while the save lock holds: `materialize-undo` would move the index while the commit is refused. - After local commits and when render-wasm is active, text shapes get derived `:position-data` recomputed in a separate commit tagged `#{:position-data}`; that tag is excluded from the position-data watcher to avoid loops. diff --git a/frontend/src/app/main/data/changes.cljs b/frontend/src/app/main/data/changes.cljs index e114ed26b7..6a85466819 100644 --- a/frontend/src/app/main/data/changes.cljs +++ b/frontend/src/app/main/data/changes.cljs @@ -267,6 +267,19 @@ [state file-id] (:vern (dsh/lookup-file state file-id))) +(defn- get-stacked-undo-group + "The undo group of the history entry that a `stack-undo?` commit is + added to: the entry at the current undo index, unless an open undo + transaction already holds changes (then the commit joins the + transaction instead). Mirrors `app.main.data.workspace.undo/append-undo`." + [state] + (let [{:keys [items index transaction]} (:workspace-undo state)] + (when (and (empty? (:undo-changes transaction)) + (empty? (:redo-changes transaction)) + (some? index) + (>= index 0)) + (dm/get-in items [index :undo-group])))) + (defn commit-changes "Schedules a list of changes to execute now, and add the corresponding undo changes to the undo stack. @@ -275,8 +288,10 @@ - save-undo?: if set to false, do not add undo changes. - undo-group: if some consecutive changes (or even transactions) share the same undo-group, they will be undone or redone in a single step. Without - it, a commit made while an undo transaction is open joins the - transaction's group, and any other gets a new one. + it, a `stack-undo?` commit that records undo takes the group of + the history entry it is added to, a commit made while an undo + transaction is open joins the transaction's group, and any + other gets a new one. - ignore-save-lock: commit even while the save-failure lock holds. Only for commits that are not user edits: the undo of rolled-back commits and data derived from commits already made. @@ -310,9 +325,19 @@ (log/trace :hint "commit-changes" :redo-changes redo-changes) (let [selected (dm/get-in state [:workspace-local :selected]) tx (dm/get-in state [:workspace-undo :transaction]) - ;; Inside an undo transaction, the group of its history entry - ;; (taken from its first commit), or the one it was opened with - undo-group (or undo-group (:undo-group tx) (:commit-group tx) (uuid/next))] + ;; A stacked commit that records undo belongs to the action + ;; of the entry it is added to, and so do the commits it + ;; starts, so one undo reverts them all. A commit that + ;; records no undo joins no entry, so it keeps a group of + ;; its own. Inside an undo transaction, the group of its + ;; history entry (taken from its first commit), or the one + ;; it was opened with + undo-group (or undo-group + (when (and stack-undo? save-undo? (seq undo-changes)) + (get-stacked-undo-group state)) + (:undo-group tx) + (:commit-group tx) + (uuid/next))] (rx/of (-> params (assoc :undo-group undo-group) (assoc :features features) diff --git a/frontend/test/frontend_tests/data/workspace_heal_undo_test.cljs b/frontend/test/frontend_tests/data/workspace_heal_undo_test.cljs index 71082eb29a..1dc4934d35 100644 --- a/frontend/test/frontend_tests/data/workspace_heal_undo_test.cljs +++ b/frontend/test/frontend_tests/data/workspace_heal_undo_test.cljs @@ -10,22 +10,32 @@ own last edit." (:require [app.common.math :as mth] + [app.common.test-helpers.components :as cthc] [app.common.test-helpers.compositions :as ctho] [app.common.test-helpers.files :as cthf] [app.common.test-helpers.ids-map :as cthi] [app.common.test-helpers.shapes :as cths] [app.main.data.changes :as dch] [app.main.data.helpers :as dsh] + [app.main.data.workspace.libraries :as dwl] + [app.main.data.workspace.pages :as dwpg] [app.main.data.workspace.shape-layout :as dwsl] + [app.main.data.workspace.shapes :as dwsh] [app.main.data.workspace.texts :as dwt] + [app.main.data.workspace.thumbnails :as dwth] + [app.main.data.workspace.thumbnails-wasm :as dwth.wasm] [app.main.data.workspace.undo :as dwu] [app.main.data.workspace.wasm-text :as dwwt] + [app.main.refs :as refs] + [app.main.store :as st] [beicon.v2.core :as rx] [cljs.test :as t :include-macros true] [frontend-tests.helpers.async :as async] + [frontend-tests.helpers.mock :as mock] [frontend-tests.helpers.state :as ths] [frontend-tests.helpers.wasm :as thw] [frontend-tests.tokens.helpers.state :as tohs] + [okulary.core :as l] [potok.v2.core :as ptk])) (t/use-fixtures :each @@ -356,3 +366,79 @@ :svg #(dwt/resize-text % 60 40) #(dwt/resize-text % 50 30 {:save-undo? false})))) + +;; Main text edit, its own resize and the component sync + +(defn- get-shape-in-store + [store id] + (get (dsh/lookup-page-objects @store) id)) + +(defn- noop-render-thumbnail + [& _] + (ptk/reify ::noop-render-thumbnail + ptk/WatchEvent + (watch [_ _ _] (rx/empty)))) + +;; Scenario: the user edits a main text, the main text then resizes itself +;; (`resize-text`, a stacked commit in its own undo transaction), and the +;; real component watcher syncs both changes to the copy. Proves: one Ctrl+Z +;; reverts the edit, the resize and the syncs, on the main and on the copy. +;; Before, the resize kept the group of its transaction, the sync it started +;; inherited it and became an undo entry of its own, so the first Ctrl+Z +;; undid only that sync. +(t/deftest ^:async one-undo-reverts-a-main-text-edit-its-resize-and-their-sync + (let [file (-> (cthf/sample-file :file1) + (ctho/add-frame-with-text :main-root :main-child "hello world" + :child-params {:grow-type :auto-height}) + (cthc/make-component :component1 :main-root) + (cthc/instantiate-component :component1 :copy-root + {:children-labels [:copy-child]})) + store (ths/setup-store file {:renderer :svg}) + page-id (cthf/current-page-id file) + main-id (cthi/id :main-child) + copy-id (cthi/id :copy-child) + before (fn [id] (select-keys (get-shape-in-store store id) [:opacity :width :height])) + main-0 (before main-id) + copy-0 (before copy-id)] + (await + (mock/with-mocks* + {;; The component watcher reads the file data through this lens + refs/workspace-data (l/derived dsh/lookup-file-data store) + ;; Thumbnail renders read the global store + st/state store + dwth.wasm/render-thumbnail noop-render-thumbnail + ;; The SVG renderer updates the main's thumbnail after the sync; its + ;; render needs a DOM and finishes after the test + dwth/update-thumbnail noop-render-thumbnail + ;; sync-file clears frame thumbnails on a timer that outlives the test + dwth/clear-thumbnail (mock/stub (constantly nil))} + + (ptk/emit! store (tohs/watch-undo-stack)) + (ptk/emit! store (dwl/watch-component-changes)) + + ;; The user edit, synced to the copy + (ptk/emit! store (dwsh/update-shapes [main-id] #(assoc % :opacity 0.5))) + (await (async/wait-for #(= 0.5 (:opacity (get-shape-in-store store copy-id))) + "the edit to reach the copy" {:max-ticks 400})) + + ;; The main text resizes itself, synced to the copy + (ptk/emit! store (dwt/resize-text main-id 60 40)) + (await (async/wait-for #(mth/close? 40 (:height (get-shape-in-store store copy-id))) + "the resize to reach the copy" {:max-ticks 400})) + (await (async/settle)) + + (let [index (get-in @store [:workspace-undo :index])] + (ptk/emit! store dwu/undo) + (await (async/wait-for #(not= index (get-in @store [:workspace-undo :index])) + "the undo" {:max-ticks 400}))) + (await (async/settle)) + + (t/is (= -1 (get-in @store [:workspace-undo :index])) + "one undo reverts the whole action") + (t/is (= main-0 (before main-id)) + "the main is back to its size and opacity") + (t/is (= copy-0 (before copy-id)) + "the copy is back to its size and opacity") + + (ptk/emit! store (dwpg/finalize-page (:id file) page-id)) + (await (async/settle)))))) diff --git a/frontend/test/frontend_tests/data/workspace_undo_test.cljs b/frontend/test/frontend_tests/data/workspace_undo_test.cljs index 55a1ae0cca..97bcd4fb86 100644 --- a/frontend/test/frontend_tests/data/workspace_undo_test.cljs +++ b/frontend/test/frontend_tests/data/workspace_undo_test.cljs @@ -14,6 +14,7 @@ [app.main.data.changes :as dch] [app.main.data.comments :as dcmt] [app.main.data.fonts :as df] + [app.main.data.helpers :as dsh] [app.main.data.workspace :as dw] [app.main.data.workspace.undo :as dwu] [beicon.v2.core :as rx] @@ -21,6 +22,7 @@ [frontend-tests.helpers.async :as async] [frontend-tests.helpers.mock :as mock] [frontend-tests.helpers.state :as ths] + [frontend-tests.tokens.helpers.state :as tohs] [potok.v2.core :as ptk])) (t/use-fixtures :each {:before cthi/reset-idmap!}) @@ -158,6 +160,177 @@ (t/is (= 1 (count (get-in state [:workspace-undo :items])))) (t/is (= #{g1 g2} (get-in state [:workspace-undo :items 0 :undo-groups]))))) +;; Scenario: a commit stacks onto the last history entry. Proves: it takes +;; that entry's undo group, so the commits it starts (e.g. a component sync) +;; inherit it, and one undo reverts the whole action. +(t/deftest a-stacked-commit-takes-the-undo-group-of-its-entry + (t/async + done + (let [group (uuid/next)] + (run-commits done + (fn [page-id] + [#(assoc % :workspace-undo {:items [(entry page-id "A" group)] :index 0}) + (rename-page page-id "B" :stack-undo? true)]) + (fn [groups _] + (t/is (= [group] groups))))))) + +;; Scenario: after an undo, the undo index sits below the end of the history +;; when a commit stacks onto it. Proves: the commit takes the group of the +;; entry at the index, where `append-undo` stacks it, not the group of the +;; undone entry at the end. +(t/deftest a-stacked-commit-after-an-undo-takes-the-group-of-the-entry-at-the-index + (t/async + done + (let [[group-a group-b] (repeatedly 2 uuid/next)] + (run-commits done + (fn [page-id] + [#(assoc % :workspace-undo {:items [(entry page-id "A" group-a) + (entry page-id "B" group-b)] + :index 0}) + (rename-page page-id "C" :stack-undo? true)]) + (fn [groups _] + (t/is (= [group-a] groups))))))) + +;; Scenario: a commit stacks onto an empty undo history. Proves: with no +;; entry to join, it gets a group of its own. +(t/deftest a-stacked-commit-on-an-empty-history-gets-its-own-undo-group + (t/async + done + (run-commits done + (fn [page-id] + [#(assoc % :workspace-undo {}) + (rename-page page-id "A" :stack-undo? true)]) + (fn [[group :as groups] _] + (t/is (= 1 (count groups))) + (t/is (some? group)))))) + +;; Scenario: the user edits the font size of a main text, which then resizes +;; itself in its own undo transaction with a stacked commit (`resize-text`). +;; Proves: the resize takes the group of the user's edit, not the one the +;; transaction was opened with. Before, the component sync that the resize +;; starts got a group of its own, and the first Ctrl+Z undid only that sync. +(t/deftest a-stacked-commit-in-an-empty-transaction-takes-the-group-of-its-entry + (t/async + done + (let [group (uuid/next) + tx (js/Symbol)] + (run-commits done + (fn [page-id] + [#(assoc % :workspace-undo {:items [(entry page-id "A" group)] :index 0}) + (dwu/start-undo-transaction tx) + (rename-page page-id "B" :stack-undo? true) + (dwu/commit-undo-transaction tx)]) + (fn [groups _] + (t/is (= [group] groups))))))) + +;; Scenario: an open transaction already holds changes when a stacked commit +;; arrives. Proves: the stacked commit joins the transaction and its group, +;; not the group of the last history entry. +(t/deftest a-stacked-commit-in-a-transaction-with-changes-takes-its-group + (t/async + done + (let [[entry-group tx-group] (repeatedly 2 uuid/next)] + (run-commits done + (fn [page-id] + (let [held (entry page-id "B" tx-group)] + [#(assoc % :workspace-undo + {:items [(entry page-id "A" entry-group)] + :index 0 + :transaction (select-keys held [:undo-changes :redo-changes :undo-group])}) + (rename-page page-id "C" :stack-undo? true)])) + (fn [groups _] + (t/is (= [tx-group] groups))))))) + +;; Scenario: an automatic heal commits with `:stack-undo? true` and +;; `:save-undo? false` (the auto-grow text heals). Proves: it does not take +;; the group of the user's last action, the rollback unit, since it never +;; joins that history entry. Inside an empty transaction it takes the +;; transaction's group. +(t/deftest a-stacked-commit-without-undo-keeps-its-own-undo-group + (t/async + done + (let [group (uuid/next) + tx (js/Symbol) + tx-group (atom nil) + spy (ptk/reify ::spy-heal-transaction + ptk/EffectEvent + (effect [_ state _] + (reset! tx-group (get-in state [:workspace-undo :transaction :commit-group]))))] + (run-commits done + (fn [page-id] + [#(assoc % :workspace-undo {:items [(entry page-id "A" group)] :index 0}) + (rename-page page-id "B" :stack-undo? true :save-undo? false) + (dwu/start-undo-transaction tx) + spy + (rename-page page-id "C" :stack-undo? true :save-undo? false) + (dwu/commit-undo-transaction tx)]) + (fn [[heal-group tx-heal-group] _] + (t/is (some? heal-group)) + (t/is (not= group heal-group)) + (t/is (= @tx-group tx-heal-group))))))) + +(defn- run-edit-resize-and-sync + "Runs the user's edit of a main text (rename to A), its resize in its own + undo transaction with a stacked commit (B) and the component sync the + resize starts (C), which inherits the resize's group. Then runs + `then-events` and calls `check` with the page id and the final state." + [done then-events check] + (let [file (cthf/sample-file :file1 :page-label :page1) + page-id (cthf/current-page-id file) + store (ths/setup-store file) + last-group (atom nil) + sub (->> (ptk/input-stream store) + (rx/filter dch/commit?) + (rx/subs! #(reset! last-group (:undo-group (deref %))))) + tx (js/Symbol) + ;; The component sync: commits with the group of the commit it + ;; follows, as `component-changed` does + sync (ptk/reify ::sync + ptk/WatchEvent + (watch [_ _ _] + (rx/of (rename-page page-id "C" + :undo-changes [{:type :mod-page :id page-id :name "B"}] + :undo-group @last-group))))] + (ths/run-store store done + (concat [(tohs/watch-undo-stack) + (rename-page page-id "A") + (dwu/start-undo-transaction tx) + (rename-page page-id "B" + :undo-changes [{:type :mod-page :id page-id :name "A"}] + :stack-undo? true) + (dwu/commit-undo-transaction tx) + sync] + then-events) + (fn [state] + (rx/dispose! sub) + (check page-id state))))) + +;; Scenario: the user edits a main text; the text resizes itself in its own +;; undo transaction with a stacked commit, and that resize starts a component +;; sync that inherits the resize's group. Proves: one Ctrl+Z reverts the +;; edit, the resize and the sync. Before, the sync was an undo entry of its +;; own and the first Ctrl+Z undid only the sync. +(t/deftest one-undo-reverts-an-edit-its-stacked-resize-and-their-sync + (t/async + done + (run-edit-resize-and-sync done + [dwu/undo] + (fn [page-id state] + (t/is (= 2 (count (get-in state [:workspace-undo :items])))) + (t/is (= -1 (get-in state [:workspace-undo :index]))) + (t/is (= "Page 1" (:name (dsh/lookup-page state page-id)))))))) + +;; Scenario: the same action as above is undone and then redone. Proves: one +;; redo restores the edit, the resize and the sync together. +(t/deftest one-redo-restores-an-edit-its-stacked-resize-and-their-sync + (t/async + done + (run-edit-resize-and-sync done + [dwu/undo dwu/redo] + (fn [page-id state] + (t/is (= 1 (get-in state [:workspace-undo :index]))) + (t/is (= "C" (:name (dsh/lookup-page state page-id)))))))) + ;; 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.