0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-08 07:50:43 -05:00

🐛 Fix is-main-of? calculation for v2 components

This commit is contained in:
Alejandro Alonso 2024-03-21 10:48:47 +01:00 committed by Andrés Moya
parent 8f156a7fd0
commit b9743891bb
3 changed files with 24 additions and 20 deletions

View file

@ -179,7 +179,7 @@
component-child (first component-children)] component-child (first component-children)]
(if (or (nil? child) (nil? component-child)) (if (or (nil? child) (nil? component-child))
container container
(let [container (if (and (not (ctk/is-main-of? component-child child)) (let [container (if (and (not (ctk/is-main-of? component-child child true))
(nil? (ctk/get-swap-slot child)) (nil? (ctk/get-swap-slot child))
(ctk/instance-head? child)) (ctk/instance-head? child))
(let [slot (guess-swap-slot component-child component-container)] (let [slot (guess-swap-slot component-child component-container)]

View file

@ -138,10 +138,10 @@
(= (:component-file shape) file-id))) (= (:component-file shape) file-id)))
(defn is-main-of? (defn is-main-of?
[shape-main shape-inst] [shape-main shape-inst components-v2]
(and (:shape-ref shape-inst) (or (= (:shape-ref shape-inst) (:id shape-main))
(or (= (:shape-ref shape-inst) (:id shape-main)) (and (= (:shape-ref shape-inst) (:shape-ref shape-main))
(= (:shape-ref shape-inst) (:shape-ref shape-main))))) (not components-v2))))
(defn main-instance? (defn main-instance?
"Check if this shape is the root of the main instance of some "Check if this shape is the root of the main instance of some

View file

@ -759,7 +759,8 @@
root-inst root-inst
root-main root-main
omit-touched? omit-touched?
set-remote-synced?) set-remote-synced?
components-v2)
changes)) changes))
both (fn [changes child-inst child-main] both (fn [changes child-inst child-main]
@ -813,7 +814,8 @@
swapped swapped
moved moved
false false
reset?)))) reset?
components-v2))))
(defn- generate-rename-component (defn- generate-rename-component
@ -948,7 +950,8 @@
component-container component-container
container container
root-inst root-inst
root-main)) root-main
components-v2))
only-main (fn [changes child-main] only-main (fn [changes child-main]
(remove-shape changes (remove-shape changes
@ -1001,7 +1004,8 @@
swapped swapped
moved moved
true true
true) true
components-v2)
;; The inverse sync may be made on a component that is inside a ;; The inverse sync may be made on a component that is inside a
;; remote library. We need to separate changes that are from ;; remote library. We need to separate changes that are from
@ -1019,7 +1023,7 @@
;; ---- Operation generation helpers ---- ;; ---- Operation generation helpers ----
(defn- compare-children (defn- compare-children
[changes children-inst children-main container-inst container-main file libraries only-inst-cb only-main-cb both-cb swapped-cb moved-cb inverse? reset?] [changes children-inst children-main container-inst container-main file libraries only-inst-cb only-main-cb both-cb swapped-cb moved-cb inverse? reset? components-v2]
(log/trace :msg "Compare children") (log/trace :msg "Compare children")
(loop [children-inst (seq (or children-inst [])) (loop [children-inst (seq (or children-inst []))
children-main (seq (or children-main [])) children-main (seq (or children-main []))
@ -1039,18 +1043,18 @@
(reduce only-inst-cb changes children-inst) (reduce only-inst-cb changes children-inst)
:else :else
(if (or (ctk/is-main-of? child-main child-inst) (if (or (ctk/is-main-of? child-main child-inst components-v2)
(and (ctf/match-swap-slot? child-main child-inst container-inst container-main file libraries) (not reset?))) (and (ctf/match-swap-slot? child-main child-inst container-inst container-main file libraries) (not reset?)))
(recur (next children-inst) (recur (next children-inst)
(next children-main) (next children-main)
(if (ctk/is-main-of? child-main child-inst) (if (ctk/is-main-of? child-main child-inst components-v2)
(both-cb changes child-inst child-main) (both-cb changes child-inst child-main)
(swapped-cb changes child-inst child-main))) (swapped-cb changes child-inst child-main)))
(let [child-inst' (d/seek #(or (ctk/is-main-of? child-main %) (let [child-inst' (d/seek #(or (ctk/is-main-of? child-main % components-v2)
(and (ctf/match-swap-slot? child-main % container-inst container-main file libraries) (not reset?))) (and (ctf/match-swap-slot? child-main % container-inst container-main file libraries) (not reset?)))
children-inst) children-inst)
child-main' (d/seek #(or (ctk/is-main-of? % child-inst) child-main' (d/seek #(or (ctk/is-main-of? % child-inst components-v2)
(and (ctf/match-swap-slot? % child-inst container-inst container-main file libraries) (not reset?))) (and (ctf/match-swap-slot? % child-inst container-inst container-main file libraries) (not reset?)))
children-main)] children-main)]
(cond (cond
@ -1066,7 +1070,7 @@
:else :else
(if inverse? (if inverse?
(let [is-main? (ctk/is-main-of? child-inst child-main')] (let [is-main? (ctk/is-main-of? child-inst child-main' components-v2)]
(recur (next children-inst) (recur (next children-inst)
(remove #(= (:id %) (:id child-main')) children-main) (remove #(= (:id %) (:id child-main')) children-main)
(cond-> changes (cond-> changes
@ -1076,7 +1080,7 @@
(swapped-cb child-inst child-main') (swapped-cb child-inst child-main')
:always :always
(moved-cb child-inst child-main')))) (moved-cb child-inst child-main'))))
(let [is-main? (ctk/is-main-of? child-inst' child-main)] (let [is-main? (ctk/is-main-of? child-inst' child-main components-v2)]
(recur (remove #(= (:id %) (:id child-inst')) children-inst) (recur (remove #(= (:id %) (:id child-inst')) children-inst)
(next children-main) (next children-main)
(cond-> changes (cond-> changes
@ -1088,13 +1092,13 @@
(moved-cb child-inst' child-main)))))))))))) (moved-cb child-inst' child-main))))))))))))
(defn- add-shape-to-instance (defn- add-shape-to-instance
[changes component-shape index component-page container root-instance root-main omit-touched? set-remote-synced?] [changes component-shape index component-page container root-instance root-main omit-touched? set-remote-synced? components-v2]
(log/info :msg (str "ADD [P " (pretty-uuid (:id container)) "] " (log/info :msg (str "ADD [P " (pretty-uuid (:id container)) "] "
(:name component-shape) (:name component-shape)
" " " "
(pretty-uuid (:id component-shape)))) (pretty-uuid (:id component-shape))))
(let [component-parent-shape (ctn/get-shape component-page (:parent-id component-shape)) (let [component-parent-shape (ctn/get-shape component-page (:parent-id component-shape))
parent-shape (d/seek #(ctk/is-main-of? component-parent-shape %) parent-shape (d/seek #(ctk/is-main-of? component-parent-shape % components-v2)
(cfh/get-children-with-self (:objects container) (cfh/get-children-with-self (:objects container)
(:id root-instance))) (:id root-instance)))
all-parents (into [(:id parent-shape)] all-parents (into [(:id parent-shape)]
@ -1163,13 +1167,13 @@
changes'))) changes')))
(defn- add-shape-to-main (defn- add-shape-to-main
[changes shape index component component-container page root-instance root-main] [changes shape index component component-container page root-instance root-main components-v2]
(log/info :msg (str "ADD [C " (pretty-uuid (:id component-container)) "] " (log/info :msg (str "ADD [C " (pretty-uuid (:id component-container)) "] "
(:name shape) (:name shape)
" " " "
(pretty-uuid (:id shape)))) (pretty-uuid (:id shape))))
(let [parent-shape (ctn/get-shape page (:parent-id shape)) (let [parent-shape (ctn/get-shape page (:parent-id shape))
component-parent-shape (d/seek #(ctk/is-main-of? % parent-shape) component-parent-shape (d/seek #(ctk/is-main-of? % parent-shape components-v2)
(cfh/get-children-with-self (:objects component-container) (cfh/get-children-with-self (:objects component-container)
(:id root-main))) (:id root-main)))
all-parents (into [(:id component-parent-shape)] all-parents (into [(:id component-parent-shape)]