🐛 Fix rmap shape refs on components v2 migration

This commit is contained in:
Alejandro Alonso 2024-02-15 10:57:52 +01:00 committed by Andrés Moya
parent 89b43d7127
commit d6b60ce43a

View file

@ -731,43 +731,61 @@
(fn [file-data] (fn [file-data]
;; Remap shape-refs so that they point to the near main. ;; Remap shape-refs so that they point to the near main.
;; At the same time, if there are any dangling ref, detach the shape and its children. ;; At the same time, if there are any dangling ref, detach the shape and its children.
(letfn [(fix-container [container] (let [count (volatile! 0)
(reduce fix-shape container (ctn/shapes-seq container)))
(fix-shape [container shape] fix-shape
(if (ctk/in-component-copy? shape) (fn [container shape]
;; First look for the direct shape. (if (ctk/in-component-copy? shape)
(let [root (ctn/get-component-shape (:objects container) shape) ;; First look for the direct shape.
libraries (assoc-in libraries [(:id file-data) :data] file-data) (let [root (ctn/get-component-shape (:objects container) shape)
library (get libraries (:component-file root)) libraries (assoc-in libraries [(:id file-data) :data] file-data)
component (ctkl/get-component (:data library) (:component-id root) true) library (get libraries (:component-file root))
direct-shape (ctf/get-component-shape (:data library) component (:shape-ref shape))] component (ctkl/get-component (:data library) (:component-id root) true)
(if (some? direct-shape) direct-shape (ctf/get-component-shape (:data library) component (:shape-ref shape))]
;; If it exists, there is nothing else to do. (if (some? direct-shape)
container ;; If it exists, there is nothing else to do.
;; If not found, find the near shape. container
(let [near-shape (d/seek #(= (:shape-ref %) (:shape-ref shape)) ;; If not found, find the near shape.
(ctf/get-component-shapes (:data library) component))] (let [near-shape (d/seek #(= (:shape-ref %) (:shape-ref shape))
(if (some? near-shape) (ctf/get-component-shapes (:data library) component))]
;; If found, update the ref to point to the near shape. (if (some? near-shape)
(ctn/update-shape container (:id shape) #(assoc % :shape-ref (:id near-shape))) ;; If found, update the ref to point to the near shape.
;; If not found, it may be a fostered component. Try to locate a direct shape (do
;; in the head component. (vswap! count inc)
(let [head (ctn/get-head-shape (:objects container) shape) (ctn/update-shape container (:id shape) #(assoc % :shape-ref (:id near-shape))))
library-2 (get libraries (:component-file head)) ;; If not found, it may be a fostered component. Try to locate a direct shape
component-2 (ctkl/get-component (:data library-2) (:component-id head) true) ;; in the head component.
direct-shape-2 (ctf/get-component-shape (:data library-2) component-2 (:shape-ref shape))] (let [head (ctn/get-head-shape (:objects container) shape)
(if (some? direct-shape-2) library-2 (get libraries (:component-file head))
;; If it exists, there is nothing else to do. component-2 (ctkl/get-component (:data library-2) (:component-id head) true)
container direct-shape-2 (ctf/get-component-shape (:data library-2) component-2 (:shape-ref shape))]
;; If not found, detach shape and all children. (if (some? direct-shape-2)
;; container ;; If it exists, there is nothing else to do.
(detach-shape container shape))))))) container
container))] ;; If not found, detach shape and all children.
;; container
(do
(vswap! count inc)
(detach-shape container shape))))))))
container))
(-> file-data fix-container
(update :pages-index update-vals fix-container) (fn [container]
(d/update-when :components update-vals fix-container)))) (reduce fix-shape container (ctn/shapes-seq container)))]
[(-> file-data
(update :pages-index update-vals fix-container)
(d/update-when :components update-vals fix-container))
@count]))
remap-refs-recur
;; remapping refs can generate cascade changes so we call it until no changes are done
(fn [file-data]
(loop [f-data file-data]
(let [[f-data count] (remap-refs f-data)]
(if (= count 0)
f-data
(recur f-data)))))
fix-converted-copies fix-converted-copies
(fn [file-data] (fn [file-data]
@ -993,8 +1011,8 @@
(remove-nested-roots) (remove-nested-roots)
(add-not-nested-roots) (add-not-nested-roots)
(fix-components-without-id) (fix-components-without-id)
(remap-refs)
(fix-converted-copies) (fix-converted-copies)
(remap-refs-recur)
(wrap-non-group-component-roots) (wrap-non-group-component-roots)
(detach-non-group-instance-roots) (detach-non-group-instance-roots)
(transform-to-frames) (transform-to-frames)