🐛 Fix update main when there are swapped copies

This commit is contained in:
Andrés Moya 2024-02-12 17:40:13 +01:00
parent af7142e97b
commit 39b5f10529
4 changed files with 68 additions and 40 deletions

View file

@ -166,13 +166,6 @@
:else :else
(get-instance-root objects (get objects (:parent-id shape))))) (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? (defn inside-component-main?
"Check if the shape is a component main instance or is inside one." "Check if the shape is a component main instance or is inside one."
[objects shape] [objects shape]

View file

@ -190,7 +190,7 @@
"Locate the near component in the local file or libraries, and retrieve the shape "Locate the near component in the local file or libraries, and retrieve the shape
referenced by the instance shape." referenced by the instance shape."
[file page libraries shape & {:keys [include-deleted?] :or {include-deleted? false}}] [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 component-file (when root-shape
(if (and (some? file) (= (:component-file root-shape) (:id file))) (if (and (some? file) (= (:component-file root-shape) (:id file)))
file file
@ -218,10 +218,23 @@
component-file (get-in libraries [(:component-file top-instance) :data]) component-file (get-in libraries [(:component-file top-instance) :data])
component (ctkl/get-component component-file (:component-id top-instance) true) component (ctkl/get-component component-file (:component-id top-instance) true)
remote-shape (get-ref-shape component-file component shape) remote-shape (get-ref-shape component-file component shape)
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)] component-container (get-component-container component-file component)]
[remote-shape' component-container]))]
(if (nil? remote-shape) (if (nil? remote-shape)
shape nil
(find-remote-shape component-container libraries remote-shape)))) (if (nil? (:shape-ref remote-shape))
remote-shape
(find-remote-shape component-container libraries remote-shape)))))
(defn get-component-shapes (defn get-component-shapes
"Retrieve all shapes of the component" "Retrieve all shapes of the component"

View file

@ -77,7 +77,11 @@
extract (cond-> {:type (:type change) extract (cond-> {:type (:type change)
:raw-change change} :raw-change change}
shape 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) (:operations change)
(assoc :operations (:operations change)))] (assoc :operations (:operations change)))]
extract))] extract))]

View file

@ -886,7 +886,6 @@
(map #(redirect-shaperef %) children-inst) (map #(redirect-shaperef %) children-inst)
children-inst) children-inst)
only-inst (fn [changes child-inst] only-inst (fn [changes child-inst]
(add-shape-to-main changes (add-shape-to-main changes
child-inst child-inst
@ -1088,10 +1087,8 @@
root-main)) root-main))
update-original-shape (fn [original-shape new-shape] update-original-shape (fn [original-shape new-shape]
(if-not (:shape-ref original-shape)
(assoc original-shape (assoc original-shape
:shape-ref (:id new-shape)) :shape-ref (:id new-shape)))
original-shape))
[_new-shape new-shapes updated-shapes] [_new-shape new-shapes updated-shapes]
(ctst/clone-shape shape (ctst/clone-shape shape
@ -1116,7 +1113,9 @@
:obj shape'})))) :obj shape'}))))
mod-obj-change (fn [changes shape'] mod-obj-change (fn [changes shape']
(update changes :redo-changes conj (let [shape-original (ctn/get-shape page (:id shape'))]
(-> changes
(update :redo-changes conj
{:type :mod-obj {:type :mod-obj
:page-id (:id page) :page-id (:id page)
:id (:id shape') :id (:id shape')
@ -1134,7 +1133,26 @@
:val (:shape-ref shape')} :val (:shape-ref shape')}
{:type :set {:type :set
:attr :touched :attr :touched
:val (:touched shape')}]})) :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'] del-obj-change (fn [changes shape']
(update changes :undo-changes conj (update changes :undo-changes conj
@ -1161,7 +1179,8 @@
parents (cfh/get-parent-ids objects (:id shape)) parents (cfh/get-parent-ids objects (:id shape))
parent (first parents) parent (first parents)
children (cfh/get-children-ids objects (:id shape)) 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] add-redo-change (fn [changes id]
(update changes :redo-changes conj (update changes :redo-changes conj
@ -1190,12 +1209,11 @@
(update :redo-changes conj (make-change (update :redo-changes conj (make-change
container container
{:type :reg-objects {:type :reg-objects
:shapes (vec parents)})) :shapes (vec parents)})))
(add-undo-change (:id shape)))
changes' (reduce add-undo-change changes' (reduce add-undo-change
changes' changes'
children)] ids)]
(if (and (cfh/touched-group? parent :shapes-group) omit-touched?) (if (and (cfh/touched-group? parent :shapes-group) omit-touched?)
changes changes