Minor refactor on group-selected (shapes) event implementation.

This commit is contained in:
Andrey Antukh 2016-01-18 22:56:12 +02:00
parent 7933955a69
commit 0d0a94e594
2 changed files with 40 additions and 26 deletions

View file

@ -274,43 +274,58 @@
(map #(add-shape % %) $) (map #(add-shape % %) $)
(rx/from-coll $)))))) (rx/from-coll $))))))
(defn group-selected (defn- calc-dimensions
[] [shapes]
(reify rs/UpdateEvent (let [x (apply min (map :x shapes))
(-apply-update [_ state]
(let [selected (get-in state [:workspace :selected])
shapes-by-id (get state :shapes-by-id)
sid (random-uuid)
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)) 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 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 [shapes-by-id (get state :shapes-by-id)
sid (random-uuid)
pid (get-in state [:workspace :page]) pid (get-in state [:workspace :page])
shapes (map (fn [item] selected (get-in state [:workspace :selected])
(assoc item shapes (->> selected
:x (- (:x item) x) (map #(get shapes-by-id %))
:y (- (:y item) y))) shapes) (map #(assoc % :group sid)))
shape {:type :builtin/group [width height x y] (calc-dimensions shapes)
:items (mapv :id shapes) group {:type :builtin/group
:view-box [0 0 width height] :id sid
:page (get-in state [:workspace :page]) :name (str "Group " (rand-int 1000))
:id sid} :x x
props {:x x
:y y :y y
:width width :width width
:height height :height height
:items (mapv :id shapes)
:page (get-in state [:workspace :page])
:view-box [0 0 width height]} :view-box [0 0 width height]}
shape (merge shape props) shapes-map (->> shapes
shapes-map (reduce #(assoc %1 (:id %2) %2) {} 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 $ (as-> state $
(update-in $ [:pages-by-id pid :shapes] conj sid) (update $ :shapes-by-id merge shapes-map)
(assoc-in $ [:shapes-by-id sid] shape) (update $ :shapes-by-id assoc sid group)
(update $ :shapes-by-id merge shapes-map)))))) (update $ :workspace assoc :selected #{})
(update-in $ [:pages-by-id pid] assoc :shapes shapes-list))))))
(defn delete-selected (defn delete-selected
"Deselect all and remove all selected shapes." "Deselect all and remove all selected shapes."

View file

@ -122,7 +122,6 @@
:name "selected-shapes" :name "selected-shapes"
:mixins [mx/static rum/reactive (mx/local {})]})) :mixins [mx/static rum/reactive (mx/local {})]}))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Shape ;; Shape
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;