♻️ Components refactor: generator for reset component

This commit is contained in:
Alejandro Alonso 2024-04-25 09:32:09 +02:00 committed by Andrés Moya
parent f3220fa985
commit c73eb77125
2 changed files with 31 additions and 48 deletions

View file

@ -2048,10 +2048,29 @@
[new-shape all-parents changes])) [new-shape all-parents changes]))
(defn generate-sync-head (defn generate-sync-head
[changes file-full libraries container head components-v2] [changes file-full libraries container id components-v2 reset?]
(let [changes (let [shape-inst (ctn/get-shape container id)
objects (:objects container)
parent (get objects (:parent-id shape-inst))
head (ctn/get-component-shape container parent)
changes
(-> changes (-> changes
(pcb/with-container container) (pcb/with-container container)
(pcb/with-objects (:objects container)) (pcb/with-objects (:objects container))
(generate-sync-shape-direct file-full libraries container (:id head) false components-v2))] (generate-sync-shape-direct file-full libraries container (:id head) reset? components-v2))]
changes)) changes))
(defn generate-reset-component
[changes file-full libraries container id components-v2]
(let [objects (:objects container)
swap-slot (-> (ctn/get-shape container id)
(ctk/get-swap-slot))
changes
(-> changes
(pcb/with-container container)
(pcb/with-objects objects)
(generate-sync-shape-direct file-full libraries container id true components-v2))]
(cond-> changes
(some? swap-slot)
(generate-sync-head file-full libraries container id components-v2 true))))

View file

@ -672,36 +672,6 @@
(rx/take-until stopper-s)))))) (rx/take-until stopper-s))))))
(defn sync-head
[id]
(ptk/reify ::sync-head
ptk/WatchEvent
(watch [it state _]
(log/info :msg "SYNC-head of shape" :id (str id))
(let [file (wsh/get-local-file state)
file-full (wsh/get-local-file-full state)
libraries (wsh/get-libraries state)
page-id (:current-page-id state)
container (cfh/get-container file :page page-id)
objects (:objects container)
shape-inst (ctn/get-shape container id)
parent (get objects (:parent-id shape-inst))
head (ctn/get-component-shape container parent)
components-v2
(features/active-feature? state "components/v2")
changes
(-> (pcb/empty-changes it)
(cflh/generate-sync-head file-full libraries container head components-v2))]
(log/debug :msg "SYNC-head finished" :js/rchanges (log-changes
(:redo-changes changes)
file))
(rx/of (dch/commit-changes changes))))))
(defn reset-component (defn reset-component
"Cancels all modifications in the shape with the given id, and all its children, in "Cancels all modifications in the shape with the given id, and all its children, in
the current page. Set all attributes equal to the ones in the linked component, the current page. Set all attributes equal to the ones in the linked component,
@ -722,25 +692,19 @@
components-v2 components-v2
(features/active-feature? state "components/v2") (features/active-feature? state "components/v2")
swap-slot (-> (ctn/get-shape container id)
(ctk/get-swap-slot))
undo-id (js/Symbol) undo-id (js/Symbol)
changes changes
(-> (pcb/empty-changes it) (-> (pcb/empty-changes it)
(pcb/with-container container) (cflh/generate-reset-component file-full libraries container id components-v2))]
(pcb/with-objects (:objects container))
(cflh/generate-sync-shape-direct file-full libraries container id true components-v2))]
(log/debug :msg "RESET-COMPONENT finished" :js/rchanges (log-changes (log/debug :msg "RESET-COMPONENT finished" :js/rchanges (log-changes
(:redo-changes changes) (:redo-changes changes)
file)) file))
(rx/of (rx/of
(dwu/start-undo-transaction undo-id) (dwu/start-undo-transaction undo-id)
(dch/commit-changes changes) (dch/commit-changes changes)
(when (some? swap-slot)
(sync-head id))
(dwu/commit-undo-transaction undo-id)))))) (dwu/commit-undo-transaction undo-id))))))
(defn reset-components (defn reset-components