From 8f156a7fd04a4c75b8253ddd93224fda82b63e00 Mon Sep 17 00:00:00 2001 From: Alejandro Alonso Date: Wed, 20 Mar 2024 07:34:39 +0100 Subject: [PATCH] :bug: Limit reset overrides on swapped components to affected subtree --- .../app/main/data/workspace/libraries.cljs | 49 ++++++++++++++++--- .../state_components_sync_test.cljs | 2 +- 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/frontend/src/app/main/data/workspace/libraries.cljs b/frontend/src/app/main/data/workspace/libraries.cljs index e9ea4ee5e..6f08743c9 100644 --- a/frontend/src/app/main/data/workspace/libraries.cljs +++ b/frontend/src/app/main/data/workspace/libraries.cljs @@ -706,6 +706,38 @@ (rx/take-until stopper-s)))))) +(defn sync-head + [id] + (ptk/reify ::sync-head + ptk/WatchEvent + (watch [it state _] + (log/info :msg "SYNC-head of shape" :id (str id)) + (let [file (wsh/get-local-file state) + file-full (wsh/get-local-file-full state) + libraries (wsh/get-libraries state) + + page-id (:current-page-id state) + container (cfh/get-container file :page page-id) + objects (:objects container) + + shape-inst (ctn/get-shape container id) + parent (get objects (:parent-id shape-inst)) + head (ctn/get-component-shape container parent) + + components-v2 + (features/active-feature? state "components/v2") + + changes + (-> (pcb/empty-changes it) + (pcb/with-container container) + (pcb/with-objects (:objects container)) + (dwlh/generate-sync-shape-direct file-full libraries container (:id head) false components-v2))] + + (log/debug :msg "SYNC-head finished" :js/rchanges (log-changes + (:redo-changes changes) + file)) + (rx/of (dch/commit-changes changes)))))) + (defn reset-component "Cancels all modifications in the shape with the given id, and all its children, in the current page. Set all attributes equal to the ones in the linked component, @@ -726,23 +758,26 @@ components-v2 (features/active-feature? state "components/v2") - shape-inst (ctn/get-shape container id) swap-slot (-> (ctn/get-shape container id) (ctk/get-swap-slot)) + + undo-id (js/Symbol) + changes (-> (pcb/empty-changes it) (pcb/with-container container) (pcb/with-objects (:objects container)) - (dwlh/generate-sync-shape-direct file-full libraries container id true components-v2) - (cond-> - (some? swap-slot) - ;; We need to propagate parent changes - (dwlh/generate-sync-shape-direct file-full libraries container (:parent-id shape-inst) true components-v2)))] + (dwlh/generate-sync-shape-direct file-full libraries container id true components-v2))] (log/debug :msg "RESET-COMPONENT finished" :js/rchanges (log-changes (:redo-changes changes) file)) - (rx/of (dch/commit-changes changes)))))) + (rx/of + (dwu/start-undo-transaction undo-id) + (dch/commit-changes changes) + (when (some? swap-slot) + (sync-head id)) + (dwu/commit-undo-transaction undo-id)))))) (defn reset-components "Cancels all modifications in the shapes with the given ids" diff --git a/frontend/test/frontend_tests/state_components_sync_test.cljs b/frontend/test/frontend_tests/state_components_sync_test.cljs index 1751fe9a5..4928ad74c 100644 --- a/frontend/test/frontend_tests/state_components_sync_test.cljs +++ b/frontend/test/frontend_tests/state_components_sync_test.cljs @@ -588,7 +588,7 @@ (t/is (= (:fill-color shape1) clr/black)) (t/is (= (:fill-opacity shape1) 0)) (t/is (= (:name shape2) "Rect 1")) - (t/is (= (:touched shape2) nil)) + (t/is (= (:touched shape2) #{:fill-group})) (t/is (= (:fill-color shape2) clr/test)) (t/is (= (:fill-opacity shape2) 0.5)) (t/is (= (:name c-instance2) "Board"))