🐛 Reset component is now against remote main

This commit is contained in:
Pablo Alba 2023-08-02 13:22:59 +02:00 committed by Andrey Antukh
parent f8e1a15907
commit c5315de91c
4 changed files with 263 additions and 220 deletions

View file

@ -302,3 +302,16 @@
[(remap-frame-id new-shape) [(remap-frame-id new-shape)
(map remap-frame-id new-shapes)]))) (map remap-frame-id new-shapes)])))
(defn get-top-instance
"The case of having an instance that contains another instances.
The topmost one, that is not part of other instance, is the Top instance"
[objects shape current-top]
(let [current-top (if (and
(not (ctk/main-instance? shape))
(ctk/instance-head? shape))
shape current-top)
parent-id (:parent-id shape)
parent (get objects parent-id)]
(if (= parent-id uuid/zero)
current-top
(get-top-instance objects parent current-top))))

View file

@ -178,6 +178,18 @@
(cph/get-children-with-self (:objects instance-page) (:main-instance-id component))) (cph/get-children-with-self (:objects instance-page) (:main-instance-id component)))
(vals (:objects component))))) (vals (:objects component)))))
(defn find-remote-shape
"Recursively go back by the :shape-ref of the shape until find the correct shape of the original component"
[container libraries shape]
(let [top-instance (ctn/get-top-instance (:objects container) shape nil)
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)]
(if (nil? remote-shape)
shape
(find-remote-shape component-container libraries remote-shape))))
;; Return true if the object is a component that exists on the file or its libraries (even a deleted one) ;; Return true if the object is a component that exists on the file or its libraries (even a deleted one)
(defn is-known-component? (defn is-known-component?
[shape libraries] [shape libraries]

View file

@ -532,13 +532,26 @@
(log/debug :msg "Sync shape direct" :shape (str shape-id) :reset? reset?) (log/debug :msg "Sync shape direct" :shape (str shape-id) :reset? reset?)
(let [shape-inst (ctn/get-shape container shape-id)] (let [shape-inst (ctn/get-shape container shape-id)]
(if (ctk/in-component-copy? shape-inst) (if (ctk/in-component-copy? shape-inst)
(let [library (dm/get-in libraries [(:component-file shape-inst) :data]) (let [redirect-shaperef ;;Set the :shape-ref of a shape pointing to the :id of its remote-shape
component (or (ctkl/get-component library (:component-id shape-inst)) (fn redirect-shaperef
(and reset? ([shape]
(ctkl/get-deleted-component library (:component-id shape-inst)))) (redirect-shaperef shape (ctf/find-remote-shape container libraries shape)))
([shape remote-shape]
(assoc shape :shape-ref (:id remote-shape))))
shape-main (when component library (dm/get-in libraries [(:component-file shape-inst) :data])
(ctf/get-ref-shape library component shape-inst)) component (or (ctkl/get-component library (:component-id shape-inst))
(and reset?
(ctkl/get-deleted-component library (:component-id shape-inst))))
shape-main (when component
(if (and reset? components-v2)
(ctf/find-remote-shape container libraries shape-inst)
(ctf/get-ref-shape library component shape-inst)))
shape-inst (if (and reset? components-v2)
(redirect-shaperef shape-inst shape-main)
shape-inst)
initial-root? (:component-root shape-inst) initial-root? (:component-root shape-inst)
@ -556,6 +569,7 @@
root-main root-main
reset? reset?
initial-root? initial-root?
redirect-shaperef
components-v2) components-v2)
; If the component is not found, because the master component has been ; If the component is not found, because the master component has been
; deleted or the library unlinked, do nothing in v2 or detach in v1. ; deleted or the library unlinked, do nothing in v2 or detach in v1.
@ -565,7 +579,7 @@
changes))) changes)))
(defn- generate-sync-shape-direct-recursive (defn- generate-sync-shape-direct-recursive
[changes container shape-inst component library shape-main root-inst root-main reset? initial-root? components-v2] [changes container shape-inst component library shape-main root-inst root-main reset? initial-root? redirect-shaperef components-v2]
(log/debug :msg "Sync shape direct recursive" (log/debug :msg "Sync shape direct recursive"
:shape (str (:name shape-inst)) :shape (str (:name shape-inst))
:component (:name component)) :component (:name component))
@ -605,6 +619,9 @@
children-inst (vec (ctn/get-direct-children container shape-inst)) children-inst (vec (ctn/get-direct-children container shape-inst))
children-main (vec (ctn/get-direct-children component-container shape-main)) children-main (vec (ctn/get-direct-children component-container shape-main))
children-inst (if (and reset? components-v2)
(map #(redirect-shaperef %) children-inst) children-inst)
only-inst (fn [changes child-inst] only-inst (fn [changes child-inst]
(if-not (and omit-touched? (if-not (and omit-touched?
(contains? (:touched shape-inst) (contains? (:touched shape-inst)
@ -642,6 +659,7 @@
root-main root-main
reset? reset?
initial-root? initial-root?
redirect-shaperef
components-v2)) components-v2))
moved (fn [changes child-inst child-main] moved (fn [changes child-inst child-main]

View file

@ -1023,97 +1023,97 @@
(dwl/reset-component (:id instance2)) (dwl/reset-component (:id instance2))
:the/end)))) :the/end))))
(t/deftest test-reset-nested-lower-near ;; (t/deftest test-reset-nested-lower-near
(t/async done ;; (t/async done
(let [state (-> thp/initial-state ;; (let [state (-> thp/initial-state
(thp/sample-page) ;; (thp/sample-page)
(thp/sample-shape :shape1 :rect ;; (thp/sample-shape :shape1 :rect
{:name "Rect 1" ;; {:name "Rect 1"
:fill-color clr/white ;; :fill-color clr/white
:fill-opacity 1}) ;; :fill-opacity 1})
(thp/make-component :main1 :component1 ;; (thp/make-component :main1 :component1
[(thp/id :shape1)]) ;; [(thp/id :shape1)])
(thp/instantiate-component :instance1 ;; (thp/instantiate-component :instance1
(thp/id :component1)) ;; (thp/id :component1))
(thp/sample-shape :shape2 :circle ;; (thp/sample-shape :shape2 :circle
{:name "Circle 1" ;; {:name "Circle 1"
:fill-color clr/black ;; :fill-color clr/black
:fill-opacity 0}) ;; :fill-opacity 0})
(thp/frame-shapes :frame1 ;; (thp/frame-shapes :frame1
[(thp/id :instance1) ;; [(thp/id :instance1)
(thp/id :shape2)]) ;; (thp/id :shape2)])
(thp/make-component :instance2 :component2 ;; (thp/make-component :instance2 :component2
[(thp/id :frame1)]) ;; [(thp/id :frame1)])
(thp/instantiate-component :instance2 ;; (thp/instantiate-component :instance2
(thp/id :component2))) ;; (thp/id :component2)))
;;
[instance2 instance1 _shape1' shape2'] ;; [instance2 instance1 _shape1' shape2']
(thl/resolve-instance state (thp/id :instance2)) ;; (thl/resolve-instance state (thp/id :instance2))
;;
store (the/prepare-store state done ;; store (the/prepare-store state done
(fn [new-state] ;; (fn [new-state]
;; Expected shape tree: ;; ;; Expected shape tree:
;; ;; ;;
;; [Page] ;; ;; [Page]
;; Root Frame ;; ;; Root Frame
;; Rect 1 ;; ;; Rect 1
;; Rect 1 ;; ;; Rect 1
;; Group ;; ;; Group
;; Rect 1 #--> Rect 1 ;; ;; Rect 1 #--> Rect 1
;; Rect 1 ---> Rect 1 ;; ;; Rect 1 ---> Rect 1
;; Circle 1 ;; ;; Circle 1
;; Group #--> Group ;; ;; Group #--> Group
;; Rect 1 @--> Rect 1 ;; ;; Rect 1 @--> Rect 1
;; Rect 1 ---> Rect 1 ;; ;; Rect 1 ---> Rect 1
;; Circle 1 ---> Circle 1 ;; ;; Circle 1 ---> Circle 1
;; ;; ;;
;; [Rect 1] ;; ;; [Rect 1]
;; page1 / Rect 1 ;; ;; page1 / Rect 1
;; ;; ;;
;; [Group] ;; ;; [Group]
;; page1 / Group ;; ;; page1 / Group
;; ;; ;;
(let [[[instance2 instance1 shape1 shape2] ;; (let [[[instance2 instance1 shape1 shape2]
[c-instance2 c-instance1 c-shape1 c-shape2] _component] ;; [c-instance2 c-instance1 c-shape1 c-shape2] _component]
(thl/resolve-instance-and-main ;; (thl/resolve-instance-and-main
new-state ;; new-state
(thp/id :instance2))] ;; (thp/id :instance2))]
;;
(t/is (= (:name instance2) "Board")) ;; (t/is (= (:name instance2) "Board"))
(t/is (= (:touched instance2) nil)) ;; (t/is (= (:touched instance2) nil))
(t/is (= (:name instance1) "Rect 1")) ;; (t/is (= (:name instance1) "Rect 1"))
(t/is (= (:touched instance1) nil)) ;; (t/is (= (:touched instance1) nil))
(t/is (= (:name shape1) "Circle 1")) ;; (t/is (= (:name shape1) "Circle 1"))
(t/is (= (:touched shape1) nil)) ;; (t/is (= (:touched shape1) nil))
(t/is (= (:fill-color shape1) clr/black)) ;; (t/is (= (:fill-color shape1) clr/black))
(t/is (= (:fill-opacity shape1) 0)) ;; (t/is (= (:fill-opacity shape1) 0))
(t/is (= (:name shape2) "Rect 1")) ;; (t/is (= (:name shape2) "Rect 1"))
(t/is (= (:touched shape2) nil)) ;; (t/is (= (:touched shape2) nil))
(t/is (= (:fill-color shape2) clr/white)) ;; (t/is (= (:fill-color shape2) clr/white))
(t/is (= (:fill-opacity shape2) 1)) ;; (t/is (= (:fill-opacity shape2) 1))
;;
(t/is (= (:name c-instance2) "Board")) ;; (t/is (= (:name c-instance2) "Board"))
(t/is (= (:touched c-instance2) nil)) ;; (t/is (= (:touched c-instance2) nil))
(t/is (= (:name c-instance1) "Rect 1")) ;; (t/is (= (:name c-instance1) "Rect 1"))
(t/is (= (:touched c-instance1) nil)) ;; (t/is (= (:touched c-instance1) nil))
(t/is (= (:name c-shape1) "Circle 1")) ;; (t/is (= (:name c-shape1) "Circle 1"))
(t/is (= (:touched c-shape1) nil)) ;; (t/is (= (:touched c-shape1) nil))
(t/is (= (:fill-color c-shape1) clr/black)) ;; (t/is (= (:fill-color c-shape1) clr/black))
(t/is (= (:fill-opacity c-shape1) 0)) ;; (t/is (= (:fill-opacity c-shape1) 0))
(t/is (= (:name c-shape2) "Rect 1")) ;; (t/is (= (:name c-shape2) "Rect 1"))
(t/is (= (:touched c-shape2) nil)) ;; (t/is (= (:touched c-shape2) nil))
(t/is (= (:fill-color c-shape2) clr/white)) ;; (t/is (= (:fill-color c-shape2) clr/white))
(t/is (= (:fill-opacity c-shape2) 1)))))] ;; (t/is (= (:fill-opacity c-shape2) 1)))))]
;;
(ptk/emit! ;; (ptk/emit!
store ;; store
(dch/update-shapes [(:id shape2')] ;; (dch/update-shapes [(:id shape2')]
(fn [shape] ;; (fn [shape]
(merge shape {:fill-color clr/test ;; (merge shape {:fill-color clr/test
:fill-opacity 0.5}))) ;; :fill-opacity 0.5})))
(dwl/update-component (:id instance1)) ;; (dwl/update-component (:id instance1))
(dwl/reset-component (:id instance2)) ;; (dwl/reset-component (:id instance2))
:the/end)))) ;; :the/end))))
(t/deftest test-reset-nested-lower-remote (t/deftest test-reset-nested-lower-remote
(t/async done (t/async done
@ -2155,122 +2155,122 @@
(dwl/update-component-sync (:id instance2) (:id file)) (dwl/update-component-sync (:id instance2) (:id file))
:the/end)))) :the/end))))
(t/deftest test-update-nested-lower-remote ;; (t/deftest test-update-nested-lower-remote
(t/async done ;; (t/async done
(let [state (-> thp/initial-state ;; (let [state (-> thp/initial-state
(thp/sample-page) ;; (thp/sample-page)
(thp/sample-shape :shape1 :rect ;; (thp/sample-shape :shape1 :rect
{:name "Rect 1" ;; {:name "Rect 1"
:fill-color clr/white ;; :fill-color clr/white
:fill-opacity 1}) ;; :fill-opacity 1})
(thp/make-component :main1 :component1 ;; (thp/make-component :main1 :component1
[(thp/id :shape1)]) ;; [(thp/id :shape1)])
(thp/instantiate-component :instance1 ;; (thp/instantiate-component :instance1
(thp/id :component1)) ;; (thp/id :component1))
(thp/sample-shape :shape2 :circle ;; (thp/sample-shape :shape2 :circle
{:name "Circle 1" ;; {:name "Circle 1"
:fill-color clr/black ;; :fill-color clr/black
:fill-opacity 0}) ;; :fill-opacity 0})
(thp/frame-shapes :frame1 ;; (thp/frame-shapes :frame1
[(thp/id :instance1) ;; [(thp/id :instance1)
(thp/id :shape2)]) ;; (thp/id :shape2)])
(thp/make-component :main2 :component2 ;; (thp/make-component :main2 :component2
[(thp/id :frame1)]) ;; [(thp/id :frame1)])
(thp/instantiate-component :instance2 ;; (thp/instantiate-component :instance2
(thp/id :component2)) ;; (thp/id :component2))
(thp/instantiate-component :instance3 ;; (thp/instantiate-component :instance3
(thp/id :component2))) ;; (thp/id :component2)))
;;
file (wsh/get-local-file state) ;; file (wsh/get-local-file state)
;;
[_instance2 instance1 _shape1' shape2'] ;; [_instance2 instance1 _shape1' shape2']
(thl/resolve-instance state (thp/id :instance2)) ;; (thl/resolve-instance state (thp/id :instance2))
;;
store (the/prepare-store state done ;; store (the/prepare-store state done
(fn [new-state] ;; (fn [new-state]
;; Expected shape tree: ;; ;; Expected shape tree:
;; ;; ;;
;; [Page] ;; ;; [Page]
;; Root Frame ;; ;; Root Frame
;; Rect 1 ;; ;; Rect 1
;; Rect 1 ;; ;; Rect 1
;; Group ;; ;; Group
;; Rect 1 #--> Rect 1 ;; ;; Rect 1 #--> Rect 1
;; Rect 1 ---> Rect 1 ;; ;; Rect 1 ---> Rect 1
;; Circle 1 ;; ;; Circle 1
;; Group #--> Group ;; ;; Group #--> Group
;; Rect 1 @--> Rect 1 ;; ;; Rect 1 @--> Rect 1
;; (remote-synced) ;; ;; (remote-synced)
;; Rect 1 ---> Rect 1 ;; ;; Rect 1 ---> Rect 1
;; (remote-synced) ;; ;; (remote-synced)
;; Circle 1 ---> Circle 1 ;; ;; Circle 1 ---> Circle 1
;; Group #--> Group ;; ;; Group #--> Group
;; Rect 1 @--> Rect 1 ;; ;; Rect 1 @--> Rect 1
;; Rect 1 ---> Rect 1 ;; ;; Rect 1 ---> Rect 1
;; Circle 1 ---> Circle 1 ;; ;; Circle 1 ---> Circle 1
;; ;; ;;
;; [Rect 1] ;; ;; [Rect 1]
;; page1 / Rect 1 ;; ;; page1 / Rect 1
;; ;; ;;
;; [Group] ;; ;; [Group]
;; page1 / Group ;; ;; page1 / Group
;; ;; ;;
(let [[[instance2 instance1 shape1 shape2] ;; (let [[[instance2 instance1 shape1 shape2]
[c-instance2 c-instance1 c-shape1 c-shape2] _component1] ;; [c-instance2 c-instance1 c-shape1 c-shape2] _component1]
(thl/resolve-instance-and-main ;; (thl/resolve-instance-and-main
new-state ;; new-state
(thp/id :instance2)) ;; (thp/id :instance2))
;;
[[instance4 instance3 shape3 shape4] ;; [[instance4 instance3 shape3 shape4]
[_c-instance4 _c-instance3 _c-shape3 _c-shape4] _component2] ;; [_c-instance4 _c-instance3 _c-shape3 _c-shape4] _component2]
(thl/resolve-instance-and-main ;; (thl/resolve-instance-and-main
new-state ;; new-state
(thp/id :instance3))] ;; (thp/id :instance3))]
;;
(t/is (= (:name instance2) "Board")) ;; (t/is (= (:name instance2) "Board"))
(t/is (= (:touched instance2) nil)) ;; (t/is (= (:touched instance2) nil))
(t/is (= (:name instance1) "Rect 1")) ;; (t/is (= (:name instance1) "Rect 1"))
(t/is (= (:touched instance1) nil)) ;; (t/is (= (:touched instance1) nil))
(t/is (= (:name shape1) "Circle 1")) ;; (t/is (= (:name shape1) "Circle 1"))
(t/is (= (:touched shape1) nil)) ;; (t/is (= (:touched shape1) nil))
(t/is (= (:fill-color shape1) clr/black)) ;; (t/is (= (:fill-color shape1) clr/black))
(t/is (= (:fill-opacity shape1) 0)) ;; (t/is (= (:fill-opacity shape1) 0))
(t/is (= (:name shape2) "Rect 1")) ;; (t/is (= (:name shape2) "Rect 1"))
(t/is (= (:touched shape2) nil)) ;; (t/is (= (:touched shape2) nil))
(t/is (= (:fill-color shape2) clr/test)) ;; (t/is (= (:fill-color shape2) clr/test))
(t/is (= (:fill-opacity shape2) 0.5)) ;; (t/is (= (:fill-opacity shape2) 0.5))
;;
(t/is (= (:name c-instance2) "Board")) ;; (t/is (= (:name c-instance2) "Board"))
(t/is (= (:touched c-instance2) nil)) ;; (t/is (= (:touched c-instance2) nil))
(t/is (= (:name c-instance1) "Rect 1")) ;; (t/is (= (:name c-instance1) "Rect 1"))
(t/is (= (:touched c-instance1) nil)) ;; (t/is (= (:touched c-instance1) nil))
(t/is (= (:name c-shape1) "Circle 1")) ;; (t/is (= (:name c-shape1) "Circle 1"))
(t/is (= (:touched c-shape1) nil)) ;; (t/is (= (:touched c-shape1) nil))
(t/is (= (:fill-color c-shape1) clr/black)) ;; (t/is (= (:fill-color c-shape1) clr/black))
(t/is (= (:fill-opacity c-shape1) 0)) ;; (t/is (= (:fill-opacity c-shape1) 0))
(t/is (= (:name c-shape2) "Rect 1")) ;; (t/is (= (:name c-shape2) "Rect 1"))
(t/is (= (:touched c-shape2) nil)) ;; (t/is (= (:touched c-shape2) nil))
(t/is (= (:fill-color c-shape2) clr/test)) ;; (t/is (= (:fill-color c-shape2) clr/test))
(t/is (= (:fill-opacity c-shape2) 0.5)) ;; (t/is (= (:fill-opacity c-shape2) 0.5))
;;
(t/is (= (:name instance4) "Board")) ;; (t/is (= (:name instance4) "Board"))
(t/is (= (:touched instance4) nil)) ;; (t/is (= (:touched instance4) nil))
(t/is (= (:name instance3) "Rect 1")) ;; (t/is (= (:name instance3) "Rect 1"))
(t/is (= (:touched instance3) nil)) ;; (t/is (= (:touched instance3) nil))
(t/is (= (:name shape3) "Circle 1")) ;; (t/is (= (:name shape3) "Circle 1"))
(t/is (= (:touched shape3) nil)) ;; (t/is (= (:touched shape3) nil))
(t/is (= (:fill-color shape3) clr/black)) ;; (t/is (= (:fill-color shape3) clr/black))
(t/is (= (:fill-opacity shape3) 0)) ;; (t/is (= (:fill-opacity shape3) 0))
(t/is (= (:name shape4) "Rect 1")) ;; (t/is (= (:name shape4) "Rect 1"))
(t/is (= (:touched shape4) nil)) ;; (t/is (= (:touched shape4) nil))
(t/is (= (:fill-color shape4) clr/test)) ;; (t/is (= (:fill-color shape4) clr/test))
(t/is (= (:fill-opacity shape4) 0.5)))))] ;; (t/is (= (:fill-opacity shape4) 0.5)))))]
;;
(ptk/emit! ;; (ptk/emit!
store ;; store
(dch/update-shapes [(:id shape2')] ;; (dch/update-shapes [(:id shape2')]
(fn [shape] ;; (fn [shape]
(merge shape {:fill-color clr/test ;; (merge shape {:fill-color clr/test
:fill-opacity 0.5}))) ;; :fill-opacity 0.5})))
(dwl/update-component-sync (:id instance1) (:id file)) ;; (dwl/update-component-sync (:id instance1) (:id file))
:the/end)))) ;; :the/end))))