mirror of
https://github.com/penpot/penpot.git
synced 2025-01-25 07:58:49 -05:00
♻️ Reimplement recursive shape update event.
This commit is contained in:
parent
0f99b1ab90
commit
bc672932ad
4 changed files with 69 additions and 65 deletions
|
@ -157,7 +157,7 @@
|
||||||
(-> state
|
(-> state
|
||||||
(assoc :current-page-id page-id ; mainly used by events
|
(assoc :current-page-id page-id ; mainly used by events
|
||||||
:workspace-local local
|
:workspace-local local
|
||||||
:workspace-page (dissoc page :data))
|
:workspace-page (dissoc page :data))
|
||||||
(assoc-in [:workspace-data page-id] (:data page)))))
|
(assoc-in [:workspace-data page-id] (:data page)))))
|
||||||
|
|
||||||
ptk/WatchEvent
|
ptk/WatchEvent
|
||||||
|
@ -568,51 +568,71 @@
|
||||||
(us/verify ::us/uuid id)
|
(us/verify ::us/uuid id)
|
||||||
(us/verify ::shape-attrs attrs)
|
(us/verify ::shape-attrs attrs)
|
||||||
(ptk/reify ::update-shape
|
(ptk/reify ::update-shape
|
||||||
dwc/IBatchedChange
|
ptk/WatchEvent
|
||||||
dwc/IUpdateGroup
|
(watch [_ state stream]
|
||||||
(get-ids [_] [id])
|
(let [page-id (:current-page-id state)
|
||||||
|
object (get-in state [:workspace-data page-id :objects id])
|
||||||
|
nobject (merge object attrs)
|
||||||
|
rops (dwc/generate-operations object nobject)
|
||||||
|
uops (dwc/generate-operations nobject object)
|
||||||
|
rchg {:type :mod-obj
|
||||||
|
:operations rops
|
||||||
|
:id id}
|
||||||
|
uchg {:type :mod-obj
|
||||||
|
:operations uops
|
||||||
|
:id id}]
|
||||||
|
(rx/of (dwc/commit-changes [rchg] [uchg] {:commit-local? true}))))))
|
||||||
|
|
||||||
ptk/UpdateEvent
|
(defn update-shapes-recursive
|
||||||
(update [_ state]
|
[ids attrs]
|
||||||
(let [pid (:current-page-id state)]
|
|
||||||
(update-in state [:workspace-data pid :objects id] merge attrs)))))
|
|
||||||
|
|
||||||
(defn update-shape-recursive
|
|
||||||
[id attrs]
|
|
||||||
(us/verify ::us/uuid id)
|
|
||||||
(us/verify ::shape-attrs attrs)
|
(us/verify ::shape-attrs attrs)
|
||||||
(letfn [(update-shape [shape]
|
(letfn [(impl-update [shape]
|
||||||
(cond-> (merge shape attrs)
|
(cond-> (merge shape attrs)
|
||||||
(and (= :text (:type shape))
|
(and (= :text (:type shape))
|
||||||
(string? (:fill-color attrs)))
|
(string? (:fill-color attrs)))
|
||||||
(dwtxt/impl-update-shape-attrs {:fill (:fill-color attrs)})))]
|
(dwtxt/impl-update-shape-attrs {:fill (:fill-color attrs)})))
|
||||||
(ptk/reify ::update-shape
|
|
||||||
dwc/IBatchedChange
|
|
||||||
dwc/IUpdateGroup
|
|
||||||
(get-ids [_] [id])
|
|
||||||
|
|
||||||
ptk/UpdateEvent
|
(impl-get-children [objects id]
|
||||||
(update [_ state]
|
(cons id (cph/get-children id objects)))
|
||||||
(let [page-id (:current-page-id state)
|
|
||||||
grouped #{:frame :group}]
|
|
||||||
(update-in state [:workspace-data page-id :objects]
|
|
||||||
(fn [objects]
|
|
||||||
(->> (d/concat [id] (cph/get-children id objects))
|
|
||||||
(map #(get objects %))
|
|
||||||
(remove #(grouped (:type %)))
|
|
||||||
(reduce #(update %1 (:id %2) update-shape) objects)))))))))
|
|
||||||
|
|
||||||
;; --- Update Page Options
|
(impl-gen-changes [objects ids]
|
||||||
|
(loop [sids (seq ids)
|
||||||
|
cids (seq (impl-get-children objects (first sids)))
|
||||||
|
rchanges []
|
||||||
|
uchanges []]
|
||||||
|
(cond
|
||||||
|
(nil? sids)
|
||||||
|
[rchanges uchanges]
|
||||||
|
|
||||||
(defn update-options
|
(nil? cids)
|
||||||
[opts]
|
(recur (next sids)
|
||||||
(us/verify ::cp/options opts)
|
(seq (impl-get-children objects (first (next sids))))
|
||||||
(ptk/reify ::update-options
|
rchanges
|
||||||
dwc/IBatchedChange
|
uchanges)
|
||||||
ptk/UpdateEvent
|
|
||||||
(update [_ state]
|
:else
|
||||||
(let [pid (:current-page-id state)]
|
(let [id (first cids)
|
||||||
(update-in state [:workspace-data pid :options] merge opts)))))
|
obj1 (get objects id)
|
||||||
|
obj2 (impl-update obj1)
|
||||||
|
rops (dwc/generate-operations obj1 obj2)
|
||||||
|
uops (dwc/generate-operations obj2 obj1)
|
||||||
|
rchg {:type :mod-obj
|
||||||
|
:operations rops
|
||||||
|
:id id}
|
||||||
|
uchg {:type :mod-obj
|
||||||
|
:operations uops
|
||||||
|
:id id}]
|
||||||
|
(recur sids
|
||||||
|
(next cids)
|
||||||
|
(conj rchanges rchg)
|
||||||
|
(conj uchanges uchg))))))]
|
||||||
|
(ptk/reify ::update-shapes-recursive
|
||||||
|
ptk/WatchEvent
|
||||||
|
(watch [_ state stream]
|
||||||
|
(let [page-id (get-in state [:workspace-page :id])
|
||||||
|
objects (get-in state [:workspace-data page-id :objects])
|
||||||
|
[rchanges uchanges] (impl-gen-changes objects (seq ids))]
|
||||||
|
(rx/of (dwc/commit-changes rchanges uchanges {:commit-local? true})))))))
|
||||||
|
|
||||||
;; --- Update Selected Shapes attrs
|
;; --- Update Selected Shapes attrs
|
||||||
|
|
||||||
|
@ -633,9 +653,8 @@
|
||||||
(watch [_ state stream]
|
(watch [_ state stream]
|
||||||
(let [selected (get-in state [:workspace-local :selected])
|
(let [selected (get-in state [:workspace-local :selected])
|
||||||
page-id (get-in state [:workspace-page :id])]
|
page-id (get-in state [:workspace-page :id])]
|
||||||
(->> (rx/from (seq selected))
|
(rx/of (update-shapes-recursive selected attrs))))))
|
||||||
(rx/map (fn [id]
|
|
||||||
(update-shape-recursive id attrs))))))))
|
|
||||||
|
|
||||||
;; --- Shape Movement (using keyboard shorcuts)
|
;; --- Shape Movement (using keyboard shorcuts)
|
||||||
|
|
||||||
|
@ -1051,21 +1070,6 @@
|
||||||
(update [_ state]
|
(update [_ state]
|
||||||
(update state :workspace-local dissoc :expanded))))
|
(update state :workspace-local dissoc :expanded))))
|
||||||
|
|
||||||
(defn recursive-assign
|
|
||||||
"A helper for assign recursively a shape attr."
|
|
||||||
[id attr value]
|
|
||||||
(ptk/reify ::recursive-assign
|
|
||||||
ptk/UpdateEvent
|
|
||||||
(update [_ state]
|
|
||||||
(let [page-id (get-in state [:workspace-page :id])
|
|
||||||
objects (get-in state [:workspace-data page-id :objects])
|
|
||||||
childs (cph/get-children id objects)]
|
|
||||||
(update-in state [:workspace-data page-id :objects]
|
|
||||||
(fn [objects]
|
|
||||||
(reduce (fn [objects id]
|
|
||||||
(assoc-in objects [id attr] value))
|
|
||||||
objects
|
|
||||||
(conj childs id))))))))
|
|
||||||
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; Navigation
|
;; Navigation
|
||||||
|
|
|
@ -72,7 +72,7 @@
|
||||||
:redo-changes changes}]
|
:redo-changes changes}]
|
||||||
(rx/of (append-undo entry))))))))))
|
(rx/of (append-undo entry))))))))))
|
||||||
|
|
||||||
(defn- generate-operations
|
(defn generate-operations
|
||||||
[ma mb]
|
[ma mb]
|
||||||
(let [ma-keys (set (keys ma))
|
(let [ma-keys (set (keys ma))
|
||||||
mb-keys (set (keys mb))
|
mb-keys (set (keys mb))
|
||||||
|
|
|
@ -54,10 +54,10 @@
|
||||||
do-bring-to-front #(st/emit! (dw/vertical-order-selected :top))
|
do-bring-to-front #(st/emit! (dw/vertical-order-selected :top))
|
||||||
do-send-backward #(st/emit! (dw/vertical-order-selected :down))
|
do-send-backward #(st/emit! (dw/vertical-order-selected :down))
|
||||||
do-send-to-back #(st/emit! (dw/vertical-order-selected :bottom))
|
do-send-to-back #(st/emit! (dw/vertical-order-selected :bottom))
|
||||||
do-show-shape #(st/emit! (dw/recursive-assign id :hidden false))
|
do-show-shape #(st/emit! (dw/update-shapes-recursive [id] {:hidden false}))
|
||||||
do-hide-shape #(st/emit! (dw/recursive-assign id :hidden true))
|
do-hide-shape #(st/emit! (dw/update-shapes-recursive [id] {:hidden true}))
|
||||||
do-lock-shape #(st/emit! (dw/recursive-assign id :blocked true))
|
do-lock-shape #(st/emit! (dw/update-shapes-recursive [id] {:blocked true}))
|
||||||
do-unlock-shape #(st/emit! (dw/recursive-assign id :blocked false))
|
do-unlock-shape #(st/emit! (dw/update-shapes-recursive [id] {:blocked false}))
|
||||||
do-create-group #(st/emit! dw/group-selected)
|
do-create-group #(st/emit! dw/group-selected)
|
||||||
do-remove-group #(st/emit! dw/ungroup-selected)]
|
do-remove-group #(st/emit! dw/ungroup-selected)]
|
||||||
[:*
|
[:*
|
||||||
|
|
|
@ -118,15 +118,15 @@
|
||||||
(fn [event]
|
(fn [event]
|
||||||
(dom/stop-propagation event)
|
(dom/stop-propagation event)
|
||||||
(if (:blocked item)
|
(if (:blocked item)
|
||||||
(st/emit! (dw/recursive-assign id :blocked false))
|
(st/emit! (dw/update-shapes-recursive [id] {:blocked false}))
|
||||||
(st/emit! (dw/recursive-assign id :blocked true))))
|
(st/emit! (dw/update-shapes-recursive [id] {:blocked true}))))
|
||||||
|
|
||||||
toggle-visibility
|
toggle-visibility
|
||||||
(fn [event]
|
(fn [event]
|
||||||
(dom/stop-propagation event)
|
(dom/stop-propagation event)
|
||||||
(if (:hidden item)
|
(if (:hidden item)
|
||||||
(st/emit! (dw/recursive-assign id :hidden false))
|
(st/emit! (dw/update-shapes-recursive [id] {:hidden false}))
|
||||||
(st/emit! (dw/recursive-assign id :hidden true))))
|
(st/emit! (dw/update-shapes-recursive [id] {:hidden true}))))
|
||||||
|
|
||||||
select-shape
|
select-shape
|
||||||
(fn [event]
|
(fn [event]
|
||||||
|
|
Loading…
Add table
Reference in a new issue