From 39b5f10529a0577ac5c5dd85eed983f3c0748bbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Moya?= Date: Mon, 12 Feb 2024 17:40:13 +0100 Subject: [PATCH] :bug: Fix update main when there are swapped copies --- common/src/app/common/types/container.cljc | 7 -- common/src/app/common/types/file.cljc | 21 +++++- .../app/main/data/workspace/libraries.cljs | 6 +- .../data/workspace/libraries_helpers.cljs | 74 ++++++++++++------- 4 files changed, 68 insertions(+), 40 deletions(-) diff --git a/common/src/app/common/types/container.cljc b/common/src/app/common/types/container.cljc index c217969e1..4265329ea 100644 --- a/common/src/app/common/types/container.cljc +++ b/common/src/app/common/types/container.cljc @@ -166,13 +166,6 @@ :else (get-instance-root objects (get objects (:parent-id shape))))) -(defn get-copy-root - "Get the top shape of the copy." - [objects shape] - (when (:shape-ref shape) - (let [parent (cfh/get-parent objects (:id shape))] - (or (get-copy-root objects parent) shape)))) - (defn inside-component-main? "Check if the shape is a component main instance or is inside one." [objects shape] diff --git a/common/src/app/common/types/file.cljc b/common/src/app/common/types/file.cljc index 3f8e7b3bd..1fd8b1f00 100644 --- a/common/src/app/common/types/file.cljc +++ b/common/src/app/common/types/file.cljc @@ -190,7 +190,7 @@ "Locate the near component in the local file or libraries, and retrieve the shape referenced by the instance shape." [file page libraries shape & {:keys [include-deleted?] :or {include-deleted? false}}] - (let [root-shape (ctn/get-copy-root (:objects page) shape) + (let [root-shape (ctn/get-component-shape (:objects page) shape) component-file (when root-shape (if (and (some? file) (= (:component-file root-shape) (:id file))) file @@ -218,10 +218,23 @@ component-file (get-in libraries [(:component-file top-instance) :data]) component (ctkl/get-component component-file (:component-id top-instance) true) remote-shape (get-ref-shape component-file component shape) - component-container (get-component-container component-file component)] + component-container (get-component-container component-file component) + [remote-shape component-container] + (if (some? remote-shape) + [remote-shape component-container] + ;; If not found, try the case of this being a fostered or swapped children + (let [head-instance (ctn/get-head-shape (:objects container) shape) + component-file (get-in libraries [(:component-file head-instance) :data]) + head-component (ctkl/get-component component-file (:component-id head-instance) true) + remote-shape' (get-ref-shape component-file head-component shape) + component-container (get-component-container component-file component)] + [remote-shape' component-container]))] + (if (nil? remote-shape) - shape - (find-remote-shape component-container libraries remote-shape)))) + nil + (if (nil? (:shape-ref remote-shape)) + remote-shape + (find-remote-shape component-container libraries remote-shape))))) (defn get-component-shapes "Retrieve all shapes of the component" diff --git a/frontend/src/app/main/data/workspace/libraries.cljs b/frontend/src/app/main/data/workspace/libraries.cljs index 269701c78..1fab0ddcd 100644 --- a/frontend/src/app/main/data/workspace/libraries.cljs +++ b/frontend/src/app/main/data/workspace/libraries.cljs @@ -77,7 +77,11 @@ extract (cond-> {:type (:type change) :raw-change change} shape - (assoc :shape (str prefix (:name shape))) + (assoc :shape (str prefix (:name shape)) + :shape-id (str (:id shape))) + (:obj change) + (assoc :obj (:name (:obj change)) + :obj-id (:id (:obj change))) (:operations change) (assoc :operations (:operations change)))] extract))] diff --git a/frontend/src/app/main/data/workspace/libraries_helpers.cljs b/frontend/src/app/main/data/workspace/libraries_helpers.cljs index d19e776f3..9f19e14e6 100644 --- a/frontend/src/app/main/data/workspace/libraries_helpers.cljs +++ b/frontend/src/app/main/data/workspace/libraries_helpers.cljs @@ -886,7 +886,6 @@ (map #(redirect-shaperef %) children-inst) children-inst) - only-inst (fn [changes child-inst] (add-shape-to-main changes child-inst @@ -1088,10 +1087,8 @@ root-main)) update-original-shape (fn [original-shape new-shape] - (if-not (:shape-ref original-shape) - (assoc original-shape - :shape-ref (:id new-shape)) - original-shape)) + (assoc original-shape + :shape-ref (:id new-shape))) [_new-shape new-shapes updated-shapes] (ctst/clone-shape shape @@ -1116,25 +1113,46 @@ :obj shape'})))) mod-obj-change (fn [changes shape'] - (update changes :redo-changes conj - {:type :mod-obj - :page-id (:id page) - :id (:id shape') - :operations [{:type :set - :attr :component-id - :val (:component-id shape')} - {:type :set - :attr :component-file - :val (:component-file shape')} - {:type :set - :attr :component-root - :val (:component-root shape')} - {:type :set - :attr :shape-ref - :val (:shape-ref shape')} - {:type :set - :attr :touched - :val (:touched shape')}]})) + (let [shape-original (ctn/get-shape page (:id shape'))] + (-> changes + (update :redo-changes conj + {:type :mod-obj + :page-id (:id page) + :id (:id shape') + :operations [{:type :set + :attr :component-id + :val (:component-id shape')} + {:type :set + :attr :component-file + :val (:component-file shape')} + {:type :set + :attr :component-root + :val (:component-root shape')} + {:type :set + :attr :shape-ref + :val (:shape-ref shape')} + {:type :set + :attr :touched + :val (:touched shape')}]}) + (update :undo-changes conj + {:type :mod-obj + :page-id (:id page) + :id (:id shape-original) + :operations [{:type :set + :attr :component-id + :val (:component-id shape-original)} + {:type :set + :attr :component-file + :val (:component-file shape-original)} + {:type :set + :attr :component-root + :val (:component-root shape-original)} + {:type :set + :attr :shape-ref + :val (:shape-ref shape-original)} + {:type :set + :attr :touched + :val (:touched shape-original)}]})))) del-obj-change (fn [changes shape'] (update changes :undo-changes conj @@ -1161,7 +1179,8 @@ parents (cfh/get-parent-ids objects (:id shape)) parent (first parents) children (cfh/get-children-ids objects (:id shape)) - ids (into [(:id shape)] children) + ids (-> (into [(:id shape)] children) + (reverse)) ;; Remove from bottom to top add-redo-change (fn [changes id] (update changes :redo-changes conj @@ -1190,12 +1209,11 @@ (update :redo-changes conj (make-change container {:type :reg-objects - :shapes (vec parents)})) - (add-undo-change (:id shape))) + :shapes (vec parents)}))) changes' (reduce add-undo-change changes' - children)] + ids)] (if (and (cfh/touched-group? parent :shapes-group) omit-touched?) changes