mirror of
https://github.com/penpot/penpot.git
synced 2025-02-14 19:19:09 -05:00
♻️ Components refactor: generator for restore-component
This commit is contained in:
parent
f91d60eeb6
commit
7d44eef4ab
3 changed files with 27 additions and 20 deletions
|
@ -286,15 +286,15 @@
|
||||||
(reduce skip-near changes children)))
|
(reduce skip-near changes children)))
|
||||||
|
|
||||||
(defn prepare-restore-component
|
(defn prepare-restore-component
|
||||||
([library-data component-id current-page it]
|
([changes library-data component-id current-page]
|
||||||
(let [component (ctkl/get-deleted-component library-data component-id)
|
(let [component (ctkl/get-deleted-component library-data component-id)
|
||||||
page (or (ctf/get-component-page library-data component)
|
page (or (ctf/get-component-page library-data component)
|
||||||
(when (some #(= (:id current-page) %) (:pages library-data)) ;; If the page doesn't belong to the library, it's not valid
|
(when (some #(= (:id current-page) %) (:pages library-data)) ;; If the page doesn't belong to the library, it's not valid
|
||||||
current-page)
|
current-page)
|
||||||
(ctpl/get-last-page library-data))]
|
(ctpl/get-last-page library-data))]
|
||||||
(prepare-restore-component nil library-data component-id it page (gpt/point 0 0) nil nil nil)))
|
(prepare-restore-component changes library-data component-id page (gpt/point 0 0) nil nil nil)))
|
||||||
|
|
||||||
([changes library-data component-id it page delta old-id parent-id frame-id]
|
([changes library-data component-id page delta old-id parent-id frame-id]
|
||||||
(let [component (ctkl/get-deleted-component library-data component-id)
|
(let [component (ctkl/get-deleted-component library-data component-id)
|
||||||
parent (get-in page [:objects parent-id])
|
parent (get-in page [:objects parent-id])
|
||||||
main-inst (get-in component [:objects (:main-instance-id component)])
|
main-inst (get-in component [:objects (:main-instance-id component)])
|
||||||
|
@ -316,7 +316,7 @@
|
||||||
(not inside-component?)
|
(not inside-component?)
|
||||||
(assoc :component-root true))
|
(assoc :component-root true))
|
||||||
|
|
||||||
changes (-> (or changes (pcb/empty-changes it))
|
changes (-> changes
|
||||||
(pcb/with-page page)
|
(pcb/with-page page)
|
||||||
(pcb/with-objects (:objects page))
|
(pcb/with-objects (:objects page))
|
||||||
(pcb/with-library-data library-data))
|
(pcb/with-library-data library-data))
|
||||||
|
@ -1790,4 +1790,22 @@
|
||||||
|
|
||||||
[root (:id root-shape) changes]))
|
[root (:id root-shape) changes]))
|
||||||
|
|
||||||
|
(defn generate-restore-component
|
||||||
|
"Restore a deleted component, with the given id, in the given file library."
|
||||||
|
[changes library-data component-id library-id current-page objects]
|
||||||
|
(let [{:keys [changes shape]} (prepare-restore-component changes library-data component-id current-page)
|
||||||
|
parent-id (:parent-id shape)
|
||||||
|
objects (cond-> (assoc objects (:id shape) shape)
|
||||||
|
(not (nil? parent-id))
|
||||||
|
(update-in [parent-id :shapes]
|
||||||
|
#(conj % (:id shape))))
|
||||||
|
|
||||||
|
;; Adds a resize-parents operation so the groups are updated. We add all the new objects
|
||||||
|
new-objects-ids (->> changes :redo-changes (filter #(= (:type %) :add-obj)) (mapv :id))
|
||||||
|
changes (-> changes
|
||||||
|
(pcb/with-objects objects)
|
||||||
|
(pcb/resize-parents new-objects-ids))]
|
||||||
|
|
||||||
|
(assoc changes :file-id library-id)))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -488,22 +488,11 @@
|
||||||
(watch [it state _]
|
(watch [it state _]
|
||||||
(let [page-id (:current-page-id state)
|
(let [page-id (:current-page-id state)
|
||||||
current-page (dm/get-in state [:workspace-data :pages-index page-id])
|
current-page (dm/get-in state [:workspace-data :pages-index page-id])
|
||||||
objects (wsh/lookup-page-objects state page-id)
|
|
||||||
library-data (wsh/get-file state library-id)
|
library-data (wsh/get-file state library-id)
|
||||||
{:keys [changes shape]} (cflh/prepare-restore-component library-data component-id current-page it)
|
objects (wsh/lookup-page-objects state page-id)
|
||||||
parent-id (:parent-id shape)
|
changes (-> (pcb/empty-changes it)
|
||||||
objects (cond-> (assoc objects (:id shape) shape)
|
(cflh/generate-restore-component library-data component-id library-id current-page objects))]
|
||||||
(not (nil? parent-id))
|
(rx/of (dch/commit-changes changes))))))
|
||||||
(update-in [parent-id :shapes]
|
|
||||||
#(conj % (:id shape))))
|
|
||||||
|
|
||||||
;; Adds a resize-parents operation so the groups are updated. We add all the new objects
|
|
||||||
new-objects-ids (->> changes :redo-changes (filter #(= (:type %) :add-obj)) (mapv :id))
|
|
||||||
changes (-> changes
|
|
||||||
(pcb/with-objects objects)
|
|
||||||
(pcb/resize-parents new-objects-ids))]
|
|
||||||
|
|
||||||
(rx/of (dch/commit-changes (assoc changes :file-id library-id)))))))
|
|
||||||
|
|
||||||
|
|
||||||
(defn restore-components
|
(defn restore-components
|
||||||
|
|
|
@ -447,7 +447,7 @@
|
||||||
{})
|
{})
|
||||||
|
|
||||||
restore-component
|
restore-component
|
||||||
#(let [restore (cflh/prepare-restore-component changes library-data (:component-id component-root) it page delta (:id component-root) parent-id frame-id)]
|
#(let [restore (cflh/prepare-restore-component changes library-data (:component-id component-root) page delta (:id component-root) parent-id frame-id)]
|
||||||
[(:shape restore) (:changes restore)])
|
[(:shape restore) (:changes restore)])
|
||||||
|
|
||||||
[_shape changes]
|
[_shape changes]
|
||||||
|
|
Loading…
Add table
Reference in a new issue