0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-10 00:40:30 -05:00

Rewritten the group-selected event.

This commit is contained in:
Andrey Antukh 2016-01-24 00:37:22 +02:00
parent 746a8196ba
commit 708305c66d
2 changed files with 44 additions and 40 deletions

View file

@ -226,7 +226,7 @@
(update-in state [:shapes-by-id id] assoc :x x :y y))
(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
:width width
:height height
@ -343,40 +343,44 @@
(map #(add-shape % %) $)
(rx/from-coll $))))))
(defn group-selected
[]
(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])
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]))]
(letfn [(update-shapes-on-page [state pid selected group]
(as-> (get-in state [:pages-by-id pid :shapes]) $
(remove selected $)
(into [group] $)
(assoc-in state [:pages-by-id pid :shapes] $)))
(as-> state $
(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))))))
(update-shapes-on-index [state shapes dimensions group]
(let [{:keys [x y]} dimensions]
(reduce (fn [state {:keys [id] :as shape}]
(as-> shape $
(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
"Deselect all and remove all selected shapes."

View file

@ -161,21 +161,21 @@
:width final-width
:height final-height})))
(defn group-size-and-position
(defn group-dimensions
"Given a collection of shapes, calculates the
dimensions of possible envolving rect.
Mainly used for calculate the selection
rect or shapes grop size."
dimensions of the resultant group."
[shapes]
{:pre [(seq shapes)]}
(let [shapes (map container-rect shapes)
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)
y' (apply max (map (fn [{:keys [y height]}] (+ y height)) shapes))
width (- x' x)
height (- y' y)]
{:width width
:height height
:view-box [0 0 width height]
:x x
:y y}))