mirror of
https://github.com/penpot/penpot.git
synced 2025-02-10 17:18:21 -05:00
Minor refactor on group-selected (shapes) event implementation.
This commit is contained in:
parent
7933955a69
commit
0d0a94e594
2 changed files with 40 additions and 26 deletions
|
@ -274,43 +274,58 @@
|
|||
(map #(add-shape % %) $)
|
||||
(rx/from-coll $))))))
|
||||
|
||||
(defn- calc-dimensions
|
||||
[shapes]
|
||||
(let [x (apply min (map :x shapes))
|
||||
y (apply min (map :y shapes))
|
||||
x' (apply max (map (fn [{:keys [x width]}] (+ x width)) shapes))
|
||||
y' (apply max (map (fn [{:keys [y height]}] (+ y height)) shapes))
|
||||
width (- x' x)
|
||||
height (- y' y)]
|
||||
[width height x y]))
|
||||
|
||||
(defn- map-coords
|
||||
"Given a shape and initial coords, transform
|
||||
it mapping its coords to new provided initial coords."
|
||||
[shape x y]
|
||||
(let [x' (:x shape)
|
||||
y' (:y shape)]
|
||||
(assoc shape :x (- x' x) :y (- y' y))))
|
||||
|
||||
(defn group-selected
|
||||
[]
|
||||
(reify rs/UpdateEvent
|
||||
(-apply-update [_ state]
|
||||
(let [selected (get-in state [:workspace :selected])
|
||||
shapes-by-id (get state :shapes-by-id)
|
||||
(let [shapes-by-id (get state :shapes-by-id)
|
||||
sid (random-uuid)
|
||||
pid (get-in state [:workspace :page])
|
||||
selected (get-in state [:workspace :selected])
|
||||
shapes (->> selected
|
||||
(map #(get shapes-by-id %))
|
||||
(map #(assoc % :group sid :id (random-uuid))))
|
||||
x (apply min (map :x shapes))
|
||||
y (apply min (map :y shapes))
|
||||
x' (apply max (map (fn [{:keys [x width]}] (+ x width)) shapes))
|
||||
y' (apply max (map (fn [{:keys [y height]}] (+ y height)) shapes))
|
||||
width (- x' x)
|
||||
height (- y' y)
|
||||
pid (get-in state [:workspace :page])
|
||||
shapes (map (fn [item]
|
||||
(assoc item
|
||||
:x (- (:x item) x)
|
||||
:y (- (:y item) y))) shapes)
|
||||
shape {:type :builtin/group
|
||||
:items (mapv :id shapes)
|
||||
:view-box [0 0 width height]
|
||||
:page (get-in state [:workspace :page])
|
||||
:id sid}
|
||||
props {:x x
|
||||
(map #(assoc % :group sid)))
|
||||
[width height x y] (calc-dimensions shapes)
|
||||
group {:type :builtin/group
|
||||
:id sid
|
||||
:name (str "Group " (rand-int 1000))
|
||||
:x x
|
||||
:y y
|
||||
:width width
|
||||
:height height
|
||||
:items (mapv :id shapes)
|
||||
:page (get-in state [:workspace :page])
|
||||
:view-box [0 0 width height]}
|
||||
shape (merge shape props)
|
||||
shapes-map (reduce #(assoc %1 (:id %2) %2) {} shapes)]
|
||||
shapes-map (->> shapes
|
||||
(map #(map-coords % x y))
|
||||
(reduce #(assoc %1 (:id %2) %2) {}))
|
||||
shapes-list (->> (get-in state [:pages-by-id pid :shapes])
|
||||
(filter (comp not selected))
|
||||
(into [sid]))]
|
||||
|
||||
(as-> state $
|
||||
(update-in $ [:pages-by-id pid :shapes] conj sid)
|
||||
(assoc-in $ [:shapes-by-id sid] shape)
|
||||
(update $ :shapes-by-id merge shapes-map))))))
|
||||
(update $ :shapes-by-id merge shapes-map)
|
||||
(update $ :shapes-by-id assoc sid group)
|
||||
(update $ :workspace assoc :selected #{})
|
||||
(update-in $ [:pages-by-id pid] assoc :shapes shapes-list))))))
|
||||
|
||||
(defn delete-selected
|
||||
"Deselect all and remove all selected shapes."
|
||||
|
|
|
@ -122,7 +122,6 @@
|
|||
:name "selected-shapes"
|
||||
:mixins [mx/static rum/reactive (mx/local {})]}))
|
||||
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;; Shape
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
|
Loading…
Add table
Reference in a new issue