mirror of
https://github.com/penpot/penpot.git
synced 2025-02-24 15:56:11 -05:00
🔧 Refactor shape ordering to use changes-builder
This commit is contained in:
parent
8682c07148
commit
56b74c6ff2
2 changed files with 87 additions and 244 deletions
|
@ -103,7 +103,7 @@
|
||||||
:option option-key
|
:option option-key
|
||||||
:value old-val}))))
|
:value old-val}))))
|
||||||
|
|
||||||
;; Shape changes
|
;; Shape tree changes
|
||||||
|
|
||||||
(defn add-obj
|
(defn add-obj
|
||||||
([changes obj]
|
([changes obj]
|
||||||
|
@ -239,6 +239,7 @@
|
||||||
:parent-id (:frame-id shape)
|
:parent-id (:frame-id shape)
|
||||||
:frame-id (:frame-id shape)
|
:frame-id (:frame-id shape)
|
||||||
:id id
|
:id id
|
||||||
|
:index (cph/get-position-on-parent objects (:id shape))
|
||||||
:obj (cond-> shape
|
:obj (cond-> shape
|
||||||
(contains? shape :shapes)
|
(contains? shape :shapes)
|
||||||
(assoc :shapes []))})))
|
(assoc :shapes []))})))
|
||||||
|
|
|
@ -761,247 +761,91 @@
|
||||||
(watch [it state _]
|
(watch [it state _]
|
||||||
(let [page-id (:current-page-id state)
|
(let [page-id (:current-page-id state)
|
||||||
objects (wsh/lookup-page-objects state page-id)
|
objects (wsh/lookup-page-objects state page-id)
|
||||||
selected (wsh/lookup-selected state)
|
selected-shapes (->> (wsh/lookup-selected state)
|
||||||
rchanges (mapv (fn [id]
|
(map (d/getf objects)))
|
||||||
(let [obj (get objects id)
|
|
||||||
parent (get objects (:parent-id obj))
|
|
||||||
shapes (:shapes parent)
|
|
||||||
cindex (d/index-of shapes id)
|
|
||||||
nindex (case loc
|
|
||||||
:top (count shapes)
|
|
||||||
:down (max 0 (- cindex 1))
|
|
||||||
:up (min (count shapes) (+ (inc cindex) 1))
|
|
||||||
:bottom 0)]
|
|
||||||
{:type :mov-objects
|
|
||||||
:parent-id (:parent-id obj)
|
|
||||||
:frame-id (:frame-id obj)
|
|
||||||
:page-id page-id
|
|
||||||
:index nindex
|
|
||||||
:shapes [id]}))
|
|
||||||
selected)
|
|
||||||
|
|
||||||
uchanges (mapv (fn [id]
|
move-shape
|
||||||
(let [obj (get objects id)]
|
(fn [changes shape]
|
||||||
{:type :mov-objects
|
(let [parent (get objects (:parent-id shape))
|
||||||
:parent-id (:parent-id obj)
|
sibling-ids (:shapes parent)
|
||||||
:frame-id (:frame-id obj)
|
current-index (d/index-of sibling-ids (:id shape))
|
||||||
:page-id page-id
|
new-index (case loc
|
||||||
:shapes [id]
|
:top (count sibling-ids)
|
||||||
:index (cph/get-position-on-parent objects id)}))
|
:down (max 0 (- current-index 1))
|
||||||
selected)]
|
:up (min (count sibling-ids) (+ (inc current-index) 1))
|
||||||
;; TODO: maybe missing the :reg-objects event?
|
:bottom 0)]
|
||||||
(rx/of (dch/commit-changes {:redo-changes rchanges
|
(pcb/change-parent changes
|
||||||
:undo-changes uchanges
|
(:id parent)
|
||||||
:origin it}))))))
|
[shape]
|
||||||
|
new-index)))
|
||||||
|
|
||||||
|
changes (reduce move-shape
|
||||||
|
(-> (pcb/empty-changes it page-id)
|
||||||
|
(pcb/with-objects objects))
|
||||||
|
selected-shapes)]
|
||||||
|
|
||||||
|
(rx/of (dch/commit-changes changes))))))
|
||||||
|
|
||||||
|
|
||||||
;; --- Change Shape Order (D&D Ordering)
|
;; --- Change Shape Order (D&D Ordering)
|
||||||
|
|
||||||
(defn relocate-shapes-changes [objects parents parent-id page-id to-index ids
|
(defn relocate-shapes-changes [it objects parents parent-id page-id to-index ids
|
||||||
groups-to-delete groups-to-unmask shapes-to-detach
|
groups-to-delete groups-to-unmask shapes-to-detach
|
||||||
shapes-to-reroot shapes-to-deroot shapes-to-unconstraint]
|
shapes-to-reroot shapes-to-deroot shapes-to-unconstraint]
|
||||||
(let [;; Changes to the shapes that are being move
|
(let [shapes (map (d/getf objects) ids)]
|
||||||
r-mov-change
|
|
||||||
[{:type :mov-objects
|
|
||||||
:parent-id parent-id
|
|
||||||
:page-id page-id
|
|
||||||
:index to-index
|
|
||||||
:shapes (vec (reverse ids))}]
|
|
||||||
|
|
||||||
u-mov-change
|
(-> (pcb/empty-changes it page-id)
|
||||||
(map (fn [id]
|
(pcb/with-objects objects)
|
||||||
(let [obj (get objects id)]
|
|
||||||
{:type :mov-objects
|
|
||||||
:parent-id (:parent-id obj)
|
|
||||||
:page-id page-id
|
|
||||||
:index (cph/get-position-on-parent objects id)
|
|
||||||
:shapes [id]}))
|
|
||||||
(reverse ids))
|
|
||||||
|
|
||||||
;; Changes deleting empty groups
|
; Move the shapes
|
||||||
r-del-change
|
(pcb/change-parent parent-id
|
||||||
(map (fn [group-id]
|
(reverse shapes)
|
||||||
{:type :del-obj
|
to-index)
|
||||||
:page-id page-id
|
|
||||||
:id group-id})
|
|
||||||
groups-to-delete)
|
|
||||||
|
|
||||||
u-del-change
|
; Remove empty groups
|
||||||
(concat
|
(pcb/remove-objects groups-to-delete)
|
||||||
;; Create the groups
|
|
||||||
(map (fn [group-id]
|
|
||||||
(let [group (get objects group-id)]
|
|
||||||
{:type :add-obj
|
|
||||||
:page-id page-id
|
|
||||||
:parent-id parent-id
|
|
||||||
:frame-id (:frame-id group)
|
|
||||||
:id group-id
|
|
||||||
:obj (-> group
|
|
||||||
(assoc :shapes []))}))
|
|
||||||
groups-to-delete)
|
|
||||||
;; Creates the hierarchy
|
|
||||||
(map (fn [group-id]
|
|
||||||
(let [group (get objects group-id)]
|
|
||||||
{:type :mov-objects
|
|
||||||
:page-id page-id
|
|
||||||
:parent-id (:id group)
|
|
||||||
:shapes (:shapes group)}))
|
|
||||||
groups-to-delete))
|
|
||||||
|
|
||||||
;; Changes removing the masks from the groups without mask shape
|
; Unmask groups whose mask have moved outside
|
||||||
r-mask-change
|
(pcb/update-shapes groups-to-unmask
|
||||||
(map (fn [group-id]
|
(fn [shape]
|
||||||
{:type :mod-obj
|
(assoc shape :masked-group? false)))
|
||||||
:page-id page-id
|
|
||||||
:id group-id
|
|
||||||
:operations [{:type :set
|
|
||||||
:attr :masked-group?
|
|
||||||
:val false}]})
|
|
||||||
groups-to-unmask)
|
|
||||||
|
|
||||||
u-mask-change
|
; Detach shapes moved out of their component
|
||||||
(map (fn [group-id]
|
(pcb/update-shapes shapes-to-detach
|
||||||
(let [group (get objects group-id)]
|
(fn [shape]
|
||||||
{:type :mod-obj
|
(assoc shape :component-id nil
|
||||||
:page-id page-id
|
:component-file nil
|
||||||
:id group-id
|
:component-root? nil
|
||||||
:operations [{:type :set
|
:remote-synced? nil
|
||||||
:attr :masked-group?
|
:shape-ref nil
|
||||||
:val (:masked-group? group)}]}))
|
:touched nil)))
|
||||||
groups-to-unmask)
|
|
||||||
|
|
||||||
;; Changes to the components metadata
|
; Make non root a component moved inside another one
|
||||||
|
(pcb/update-shapes shapes-to-deroot
|
||||||
|
(fn [shape]
|
||||||
|
(assoc shape :component-root? nil)))
|
||||||
|
|
||||||
detach-keys [:component-id :component-file :component-root? :remote-synced? :shape-ref :touched]
|
; Make root a subcomponent moved outside its parent component
|
||||||
|
(pcb/update-shapes shapes-to-reroot
|
||||||
|
(fn [shape]
|
||||||
|
(assoc shape :component-root? true)))
|
||||||
|
|
||||||
r-detach-change
|
; Reset constraints depending on the new parent
|
||||||
(map (fn [id]
|
(pcb/update-shapes shapes-to-unconstraint
|
||||||
{:type :mod-obj
|
(fn [shape]
|
||||||
:page-id page-id
|
(let [parent (get objects parent-id)
|
||||||
:id id
|
|
||||||
:operations (mapv #(hash-map :type :set :attr % :val nil) detach-keys)})
|
|
||||||
shapes-to-detach)
|
|
||||||
|
|
||||||
u-detach-change
|
|
||||||
(map (fn [id]
|
|
||||||
(let [obj (get objects id)]
|
|
||||||
{:type :mod-obj
|
|
||||||
:page-id page-id
|
|
||||||
:id id
|
|
||||||
:operations (mapv #(hash-map :type :set :attr % :val (get obj %)) detach-keys)}))
|
|
||||||
shapes-to-detach)
|
|
||||||
|
|
||||||
r-deroot-change
|
|
||||||
(map (fn [id]
|
|
||||||
{:type :mod-obj
|
|
||||||
:page-id page-id
|
|
||||||
:id id
|
|
||||||
:operations [{:type :set
|
|
||||||
:attr :component-root?
|
|
||||||
:val nil}]})
|
|
||||||
shapes-to-deroot)
|
|
||||||
|
|
||||||
u-deroot-change
|
|
||||||
(map (fn [id]
|
|
||||||
{:type :mod-obj
|
|
||||||
:page-id page-id
|
|
||||||
:id id
|
|
||||||
:operations [{:type :set
|
|
||||||
:attr :component-root?
|
|
||||||
:val true}]})
|
|
||||||
shapes-to-deroot)
|
|
||||||
|
|
||||||
r-reroot-change
|
|
||||||
(map (fn [id]
|
|
||||||
{:type :mod-obj
|
|
||||||
:page-id page-id
|
|
||||||
:id id
|
|
||||||
:operations [{:type :set
|
|
||||||
:attr :component-root?
|
|
||||||
:val true}]})
|
|
||||||
shapes-to-reroot)
|
|
||||||
|
|
||||||
u-reroot-change
|
|
||||||
(map (fn [id]
|
|
||||||
{:type :mod-obj
|
|
||||||
:page-id page-id
|
|
||||||
:id id
|
|
||||||
:operations [{:type :set
|
|
||||||
:attr :component-root?
|
|
||||||
:val nil}]})
|
|
||||||
shapes-to-reroot)
|
|
||||||
|
|
||||||
|
|
||||||
;; Changes resetting constraints
|
|
||||||
|
|
||||||
r-unconstraint-change
|
|
||||||
(map (fn [id]
|
|
||||||
(let [obj (get objects id)
|
|
||||||
parent (get objects parent-id)
|
|
||||||
frame-id (if (= (:type parent) :frame)
|
frame-id (if (= (:type parent) :frame)
|
||||||
(:id parent)
|
(:id parent)
|
||||||
(:frame-id parent))]
|
(:frame-id parent))
|
||||||
{:type :mod-obj
|
moved-shape (assoc shape
|
||||||
:page-id page-id
|
:parent-id parent-id
|
||||||
:id id
|
:frame-id frame-id)]
|
||||||
:operations [{:type :set
|
(assoc shape :constraints-h (gsh/default-constraints-h moved-shape)
|
||||||
:attr :constraints-h
|
:constraints-v (gsh/default-constraints-v moved-shape))))
|
||||||
:val (gsh/default-constraints-h
|
{:ignore-touched true})
|
||||||
(assoc obj :parent-id parent-id :frame-id frame-id))
|
|
||||||
:ignore-touched true}
|
|
||||||
{:type :set
|
|
||||||
:attr :constraints-v
|
|
||||||
:val (gsh/default-constraints-v
|
|
||||||
(assoc obj :parent-id parent-id :frame-id frame-id))
|
|
||||||
:ignore-touched true}]}))
|
|
||||||
shapes-to-unconstraint)
|
|
||||||
|
|
||||||
u-unconstraint-change
|
; Resize parent containers that need to
|
||||||
(map (fn [id]
|
(pcb/resize-parents parents))))
|
||||||
(let [obj (get objects id)]
|
|
||||||
{:type :mod-obj
|
|
||||||
:page-id page-id
|
|
||||||
:id id
|
|
||||||
:operations [{:type :set
|
|
||||||
:attr :constraints-h
|
|
||||||
:val (:constraints-h obj)
|
|
||||||
:ignore-touched true}
|
|
||||||
{:type :set
|
|
||||||
:attr :constraints-v
|
|
||||||
:val (:constraints-v obj)
|
|
||||||
:ignore-touched true}]}))
|
|
||||||
shapes-to-unconstraint)
|
|
||||||
|
|
||||||
r-reg-change
|
|
||||||
[{:type :reg-objects
|
|
||||||
:page-id page-id
|
|
||||||
:shapes (vec parents)}]
|
|
||||||
|
|
||||||
u-reg-change
|
|
||||||
[{:type :reg-objects
|
|
||||||
:page-id page-id
|
|
||||||
:shapes (vec parents)}]
|
|
||||||
|
|
||||||
rchanges (d/concat-vec
|
|
||||||
r-mov-change
|
|
||||||
r-del-change
|
|
||||||
r-mask-change
|
|
||||||
r-detach-change
|
|
||||||
r-deroot-change
|
|
||||||
r-reroot-change
|
|
||||||
r-unconstraint-change
|
|
||||||
r-reg-change)
|
|
||||||
|
|
||||||
uchanges (d/concat-vec
|
|
||||||
u-del-change
|
|
||||||
u-reroot-change
|
|
||||||
u-deroot-change
|
|
||||||
u-detach-change
|
|
||||||
u-mask-change
|
|
||||||
u-mov-change
|
|
||||||
u-unconstraint-change
|
|
||||||
u-reg-change)]
|
|
||||||
[rchanges uchanges]))
|
|
||||||
|
|
||||||
(defn relocate-shapes
|
(defn relocate-shapes
|
||||||
[ids parent-id to-index]
|
[ids parent-id to-index]
|
||||||
|
@ -1100,8 +944,8 @@
|
||||||
[[] [] []]
|
[[] [] []]
|
||||||
ids)
|
ids)
|
||||||
|
|
||||||
[rchanges uchanges]
|
changes (relocate-shapes-changes it
|
||||||
(relocate-shapes-changes objects
|
objects
|
||||||
parents
|
parents
|
||||||
parent-id
|
parent-id
|
||||||
page-id
|
page-id
|
||||||
|
@ -1114,9 +958,7 @@
|
||||||
shapes-to-deroot
|
shapes-to-deroot
|
||||||
ids)]
|
ids)]
|
||||||
|
|
||||||
(rx/of (dch/commit-changes {:redo-changes rchanges
|
(rx/of (dch/commit-changes changes)
|
||||||
:undo-changes uchanges
|
|
||||||
:origin it})
|
|
||||||
(dwc/expand-collapse parent-id))))))
|
(dwc/expand-collapse parent-id))))))
|
||||||
|
|
||||||
(defn relocate-selected-shapes
|
(defn relocate-selected-shapes
|
||||||
|
|
Loading…
Add table
Reference in a new issue