From d3dd9ffd9b5f40b60f0cfbb7a36ea25849dcc746 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Moya?= Date: Wed, 14 Feb 2024 16:06:32 +0100 Subject: [PATCH] :bug: Pack swap component in a single transaction and undo group --- .../app/main/data/workspace/libraries.cljs | 22 +++++++++++----- .../src/app/main/data/workspace/shapes.cljs | 26 +++++++++++++------ 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/frontend/src/app/main/data/workspace/libraries.cljs b/frontend/src/app/main/data/workspace/libraries.cljs index 5edcafbe0..4fbf5d1e0 100644 --- a/frontend/src/app/main/data/workspace/libraries.cljs +++ b/frontend/src/app/main/data/workspace/libraries.cljs @@ -865,7 +865,7 @@ 0))))) (defn- add-component-for-swap - [shape file-id id-new-component index target-cell keep-props-values] + [shape file-id id-new-component index target-cell keep-props-values {:keys [undo-group]}] (dm/assert! (uuid? id-new-component)) (dm/assert! (uuid? file-id)) (ptk/reify ::add-component-for-swap @@ -877,6 +877,7 @@ objects (:objects page) position (gpt/point (:x shape) (:y shape)) changes (-> (pcb/empty-changes it (:id page)) + (pcb/set-undo-group undo-group) (pcb/with-objects objects)) position (-> position (with-meta {:cell target-cell})) @@ -925,10 +926,20 @@ index (find-shape-index objects (:parent-id shape) (:id shape)) ;; Store the properties that need to be maintained when the component is swapped - keep-props-values (select-keys shape ctk/swap-keep-attrs)] - (rx/of (dwsh/delete-shapes nil (d/ordered-set (:id shape)) {:component-swap true}) - (add-component-for-swap shape file-id id-new-component index target-cell keep-props-values) - (ptk/data-event :layout/update [(:parent-id shape)])))))) + keep-props-values (select-keys shape ctk/swap-keep-attrs) + + undo-id (js/Symbol) + undo-group (uuid/next)] + + (rx/of + (dwu/start-undo-transaction undo-id) + (dwsh/delete-shapes nil (d/ordered-set (:id shape)) {:component-swap true + :undo-id undo-id + :undo-group undo-group}) + (add-component-for-swap shape file-id id-new-component index target-cell keep-props-values + {:undo-group undo-group}) + (ptk/data-event :layout/update [(:parent-id shape)]) + (dwu/commit-undo-transaction undo-id)))))) (defn component-multi-swap "Swaps several components with another one" @@ -946,7 +957,6 @@ (rx/of (dwu/commit-undo-transaction undo-id)) (rx/of (dwsp/open-specialized-panel :component-swap))))))) - (def valid-asset-types #{:colors :components :typographies}) diff --git a/frontend/src/app/main/data/workspace/shapes.cljs b/frontend/src/app/main/data/workspace/shapes.cljs index 805669e4d..e541c0abb 100644 --- a/frontend/src/app/main/data/workspace/shapes.cljs +++ b/frontend/src/app/main/data/workspace/shapes.cljs @@ -137,22 +137,25 @@ ids-to-hide))))) [ids []]) - undo-id (js/Symbol)] + undo-id (or (:undo-id options) (js/Symbol))] (rx/concat (rx/of (dwu/start-undo-transaction undo-id) (update-shape-flags ids-to-hide {:hidden true})) - (real-delete-shapes file page objects ids-to-delete it components-v2 (:component-swap options)) + (real-delete-shapes file page objects ids-to-delete it {:components-v2 components-v2 + :ignore-touched (:component-swap options) + :undo-group (:undo-group options)}) (rx/of (dwu/commit-undo-transaction undo-id)))))))) (defn- real-delete-shapes-changes - ([file page objects ids it components-v2 ignore-touched] + ([file page objects ids it {:keys [undo-group] :as options}] (let [changes (-> (pcb/empty-changes it (:id page)) + (pcb/set-undo-group undo-group) (pcb/with-page page) (pcb/with-objects objects) (pcb/with-library-data file))] - (real-delete-shapes-changes changes file page objects ids it components-v2 ignore-touched))) - ([changes file page objects ids _it components-v2 ignore-touched] + (real-delete-shapes-changes changes file page objects ids it options))) + ([changes file page objects ids _it {:keys [components-v2 ignore-touched]}] (let [lookup (d/getf objects) groups-to-unmask (reduce (fn [group-ids id] @@ -275,12 +278,19 @@ (defn delete-shapes-changes [changes file page objects ids it components-v2 ignore-touched] - (let [[changes _all-parents] (real-delete-shapes-changes changes file page objects ids it components-v2 ignore-touched)] + (let [[changes _all-parents] (real-delete-shapes-changes changes + file + page + objects + ids + it + {:components-v2 components-v2 + :ignore-touched ignore-touched})] changes)) (defn- real-delete-shapes - [file page objects ids it components-v2 ignore-touched] - (let [[changes all-parents] (real-delete-shapes-changes file page objects ids it components-v2 ignore-touched) + [file page objects ids it options] + (let [[changes all-parents] (real-delete-shapes-changes file page objects ids it options) undo-id (js/Symbol)] (rx/of (dwu/start-undo-transaction undo-id) (dc/detach-comment-thread ids)