mirror of
https://github.com/penpot/penpot.git
synced 2025-01-10 08:50:57 -05:00
Rewritten the group-selected event.
This commit is contained in:
parent
746a8196ba
commit
708305c66d
2 changed files with 44 additions and 40 deletions
|
@ -226,7 +226,7 @@
|
||||||
(update-in state [:shapes-by-id id] assoc :x x :y y))
|
(update-in state [:shapes-by-id id] assoc :x x :y y))
|
||||||
|
|
||||||
(update-group-size [shape shapes]
|
(update-group-size [shape shapes]
|
||||||
(let [{:keys [width height]} (sh/group-size-and-position shapes)]
|
(let [{:keys [width height]} (sh/group-dimensions shapes)]
|
||||||
(assoc shape
|
(assoc shape
|
||||||
:width width
|
:width width
|
||||||
:height height
|
:height height
|
||||||
|
@ -343,40 +343,44 @@
|
||||||
(map #(add-shape % %) $)
|
(map #(add-shape % %) $)
|
||||||
(rx/from-coll $))))))
|
(rx/from-coll $))))))
|
||||||
|
|
||||||
|
|
||||||
(defn group-selected
|
(defn group-selected
|
||||||
[]
|
[]
|
||||||
(reify rs/UpdateEvent
|
(letfn [(update-shapes-on-page [state pid selected group]
|
||||||
(-apply-update [_ state]
|
(as-> (get-in state [:pages-by-id pid :shapes]) $
|
||||||
(let [shapes-by-id (get state :shapes-by-id)
|
(remove selected $)
|
||||||
sid (random-uuid)
|
(into [group] $)
|
||||||
pid (get-in state [:workspace :page])
|
(assoc-in state [:pages-by-id pid :shapes] $)))
|
||||||
selected (get-in state [:workspace :selected])
|
|
||||||
shapes (->> selected
|
|
||||||
(map #(get shapes-by-id %))
|
|
||||||
(map #(assoc % :group sid)))
|
|
||||||
{:keys [width height x y]} (sh/group-size-and-position 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]}
|
|
||||||
shapes-map (->> shapes
|
|
||||||
(map #(sh/translate-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-shapes-on-index [state shapes dimensions group]
|
||||||
(update $ :shapes-by-id merge shapes-map)
|
(let [{:keys [x y]} dimensions]
|
||||||
(update $ :shapes-by-id assoc sid group)
|
(reduce (fn [state {:keys [id] :as shape}]
|
||||||
(update $ :workspace assoc :selected #{})
|
(as-> shape $
|
||||||
(update-in $ [:pages-by-id pid] assoc :shapes shapes-list))))))
|
(sh/translate-coords $ x y)
|
||||||
|
(assoc $ :group group)
|
||||||
|
(assoc-in state [:shapes-by-id id] $)))
|
||||||
|
state
|
||||||
|
shapes)))]
|
||||||
|
(reify rs/UpdateEvent
|
||||||
|
(-apply-update [_ state]
|
||||||
|
(let [shapes-by-id (get state :shapes-by-id)
|
||||||
|
sid (random-uuid)
|
||||||
|
pid (get-in state [:workspace :page])
|
||||||
|
selected (get-in state [:workspace :selected])
|
||||||
|
selected' (map #(get shapes-by-id %) selected)
|
||||||
|
dimensions (sh/group-dimensions selected')
|
||||||
|
|
||||||
|
data {:type :builtin/group
|
||||||
|
:name (str "Group " (rand-int 1000))
|
||||||
|
:items (into [] selected)
|
||||||
|
:id sid
|
||||||
|
:page pid}
|
||||||
|
group (merge data dimensions)]
|
||||||
|
(as-> state $
|
||||||
|
(update-shapes-on-index $ selected' dimensions sid)
|
||||||
|
(update-shapes-on-page $ pid selected sid)
|
||||||
|
(update $ :shapes-by-id assoc sid group)
|
||||||
|
(update $ :workspace assoc :selected #{})))))))
|
||||||
|
|
||||||
(defn delete-selected
|
(defn delete-selected
|
||||||
"Deselect all and remove all selected shapes."
|
"Deselect all and remove all selected shapes."
|
||||||
|
|
|
@ -161,21 +161,21 @@
|
||||||
:width final-width
|
:width final-width
|
||||||
:height final-height})))
|
:height final-height})))
|
||||||
|
|
||||||
(defn group-size-and-position
|
(defn group-dimensions
|
||||||
"Given a collection of shapes, calculates the
|
"Given a collection of shapes, calculates the
|
||||||
dimensions of possible envolving rect.
|
dimensions of the resultant group."
|
||||||
|
|
||||||
Mainly used for calculate the selection
|
|
||||||
rect or shapes grop size."
|
|
||||||
[shapes]
|
[shapes]
|
||||||
{:pre [(seq shapes)]}
|
{:pre [(seq shapes)]}
|
||||||
(let [shapes (map container-rect shapes)
|
(let [shapes (map container-rect shapes)
|
||||||
x (apply min (map :x shapes))
|
x (apply min (map :x shapes))
|
||||||
y (apply min (map :y shapes))
|
y (apply min (map :y shapes))
|
||||||
x' (apply max (map (fn [{:keys [x width]}] (+ x width)) shapes))
|
x' (apply max (map (fn [{:keys [x width]}] (+ x width)) shapes))
|
||||||
y' (apply max (map (fn [{:keys [y height]}] (+ y height)) shapes))]
|
y' (apply max (map (fn [{:keys [y height]}] (+ y height)) shapes))
|
||||||
{:width (- x' x)
|
width (- x' x)
|
||||||
:height (- y' y)
|
height (- y' y)]
|
||||||
|
{:width width
|
||||||
|
:height height
|
||||||
|
:view-box [0 0 width height]
|
||||||
:x x
|
:x x
|
||||||
:y y}))
|
:y y}))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue