From d8121364adef30f0e4fc90f8dbd669bd089dd91e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Moya?= Date: Wed, 21 Jun 2023 14:38:09 +0200 Subject: [PATCH] :bug: Fix touched on adding shapes to a component copy and undo --- .../src/app/common/pages/changes_builder.cljc | 32 +++++++++++++++++-- common/src/app/common/types/shape_tree.cljc | 4 +-- .../app/main/data/workspace/libraries.cljs | 3 +- .../data/workspace/libraries_helpers.cljs | 1 + .../app/main/data/workspace/selection.cljs | 2 +- .../src/app/main/data/workspace/shapes.cljs | 6 ++-- .../test/frontend_tests/helpers/pages.cljs | 3 +- 7 files changed, 41 insertions(+), 10 deletions(-) diff --git a/common/src/app/common/pages/changes_builder.cljc b/common/src/app/common/pages/changes_builder.cljc index 56ba9e95b..288ec1cfc 100644 --- a/common/src/app/common/pages/changes_builder.cljc +++ b/common/src/app/common/pages/changes_builder.cljc @@ -15,6 +15,7 @@ [app.common.math :as mth] [app.common.pages :as cp] [app.common.pages.helpers :as cph] + [app.common.types.component :as ctk] [app.common.types.file :as ctf] [app.common.uuid :as uuid])) @@ -216,9 +217,14 @@ ([changes obj {:keys [index ignore-touched] :or {index ::undefined ignore-touched false}}] (assert-page-id changes) + (assert-objects changes) (let [obj (cond-> obj (not= index ::undefined) (assoc ::index index)) + + objects (lookup-objects changes) + parent (get objects (:parent-id obj)) + add-change {:type :add-obj :id (:id obj) @@ -232,10 +238,20 @@ del-change {:type :del-obj :id (:id obj) - :page-id (::page-id (meta changes))}] + :page-id (::page-id (meta changes))} + + restore-touched-change + {:type :mod-obj + :page-id (::page-id (meta changes)) + :id (:id parent) + :operations [{:type :set-touched + :touched (:touched parent)}]}] (-> changes (update :redo-changes conj add-change) + (cond-> + (and (ctk/in-component-copy? parent) (not ignore-touched)) + (update :undo-changes d/preconj restore-touched-change)) (update :undo-changes d/preconj del-change) (apply-changes-local))))) @@ -256,6 +272,8 @@ (assert-page-id changes) (assert-objects changes) (let [objects (lookup-objects changes) + parent (get objects parent-id) + set-parent-change (cond-> {:type :mov-objects :parent-id parent-id @@ -275,10 +293,20 @@ :parent-id (:parent-id shape) :shapes [(:id shape)] :after-shape prev-sibling - :index 0})))] ; index is used in case there is no after-shape (moving bottom shapes) + :index 0}))) ; index is used in case there is no after-shape (moving bottom shapes) + + restore-touched-change + {:type :mod-obj + :page-id (::page-id (meta changes)) + :id (:id parent) + :operations [{:type :set-touched + :touched (:touched parent)}]}] (-> changes (update :redo-changes conj set-parent-change) + (cond-> + (ctk/in-component-copy? parent) + (update :undo-changes d/preconj restore-touched-change)) (update :undo-changes #(reduce mk-undo-change % shapes)) (apply-changes-local))))) diff --git a/common/src/app/common/types/shape_tree.cljc b/common/src/app/common/types/shape_tree.cljc index 6c96daa18..731f31de2 100644 --- a/common/src/app/common/types/shape_tree.cljc +++ b/common/src/app/common/types/shape_tree.cljc @@ -39,9 +39,7 @@ (-> parent (update :shapes update-parent-shapes) (update :shapes d/vec-without-nils) - (cond-> (and (:shape-ref parent) - (not= (:id parent) frame-id) - (not ignore-touched)) + (cond-> (and (ctk/in-component-copy? parent) (not ignore-touched)) (-> (update :touched cph/set-touched-group :shapes-group) (dissoc :remote-synced?))))) diff --git a/frontend/src/app/main/data/workspace/libraries.cljs b/frontend/src/app/main/data/workspace/libraries.cljs index 2a2d17e8a..edff00bc3 100644 --- a/frontend/src/app/main/data/workspace/libraries.cljs +++ b/frontend/src/app/main/data/workspace/libraries.cljs @@ -485,7 +485,8 @@ (let [page (wsh/lookup-page state) libraries (wsh/get-libraries state) - changes (pcb/empty-changes it (:id page)) + changes (-> (pcb/empty-changes it (:id page)) + (pcb/with-objects (:objects page))) [new-shape changes] (dwlh/generate-instantiate-component changes diff --git a/frontend/src/app/main/data/workspace/libraries_helpers.cljs b/frontend/src/app/main/data/workspace/libraries_helpers.cljs index 67ff5c991..ebe7e1cdd 100644 --- a/frontend/src/app/main/data/workspace/libraries_helpers.cljs +++ b/frontend/src/app/main/data/workspace/libraries_helpers.cljs @@ -228,6 +228,7 @@ (assoc :parent-id parent-id)) changes (-> (or changes (pcb/empty-changes it)) (pcb/with-page page) + (pcb/with-objects (:objects page)) (pcb/with-library-data library-data)) changes (cond-> (pcb/add-object changes first-shape {:ignore-touched true}) (some? old-id) (pcb/amend-last-change #(assoc % :old-id old-id))) ; on copy/paste old id is used later to reorder the paster layers diff --git a/frontend/src/app/main/data/workspace/selection.cljs b/frontend/src/app/main/data/workspace/selection.cljs index 2937de92f..db4dfee10 100644 --- a/frontend/src/app/main/data/workspace/selection.cljs +++ b/frontend/src/app/main/data/workspace/selection.cljs @@ -433,7 +433,7 @@ (gsh/move delta) (d/update-when :interactions #(ctsi/remap-interactions % ids-map objects))) - changes (-> (pcb/add-object changes new-obj {:ignore-touched true}) + changes (-> (pcb/add-object changes new-obj) (pcb/amend-last-change #(assoc % :old-id (:id obj)))) changes (cond-> changes diff --git a/frontend/src/app/main/data/workspace/shapes.cljs b/frontend/src/app/main/data/workspace/shapes.cljs index 4414fc650..295e880a9 100644 --- a/frontend/src/app/main/data/workspace/shapes.cljs +++ b/frontend/src/app/main/data/workspace/shapes.cljs @@ -108,7 +108,8 @@ objects (wsh/lookup-page-objects state page-id) selected (wsh/lookup-selected state) - changes (pcb/empty-changes it page-id) + changes (-> (pcb/empty-changes it page-id) + (pcb/with-objects objects)) [shape changes] (prepare-add-shape changes attrs objects selected) @@ -433,7 +434,8 @@ selected (wsh/lookup-selected state) selected (cph/clean-loops objects selected) - changes (pcb/empty-changes it page-id) + changes (-> (pcb/empty-changes it page-id) + (pcb/with-objects objects)) [frame-shape changes] (prepare-create-artboard-from-selection changes diff --git a/frontend/test/frontend_tests/helpers/pages.cljs b/frontend/test/frontend_tests/helpers/pages.cljs index bd8ce6598..b4c76dbe5 100644 --- a/frontend/test/frontend_tests/helpers/pages.cljs +++ b/frontend/test/frontend_tests/helpers/pages.cljs @@ -154,7 +154,8 @@ (let [page (current-page state) libraries (wsh/get-libraries state) - changes (pcb/empty-changes nil (:id page)) + changes (-> (pcb/empty-changes nil (:id page)) + (pcb/with-objects (:objects page))) [new-shape changes] (dwlh/generate-instantiate-component changes