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- 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 (defn group-selected
[] []
(reify rs/UpdateEvent (reify rs/UpdateEvent
(-apply-update [_ state] (-apply-update [_ state]
(let [selected (get-in state [:workspace :selected]) (let [shapes-by-id (get state :shapes-by-id)
shapes-by-id (get state :shapes-by-id)
sid (random-uuid) sid (random-uuid)
pid (get-in state [:workspace :page])
selected (get-in state [:workspace :selected])
shapes (->> selected shapes (->> selected
(map #(get shapes-by-id %)) (map #(get shapes-by-id %))
(map #(assoc % :group sid :id (random-uuid)))) (map #(assoc % :group sid)))
x (apply min (map :x shapes)) [width height x y] (calc-dimensions shapes)
y (apply min (map :y shapes)) group {:type :builtin/group
x' (apply max (map (fn [{:keys [x width]}] (+ x width)) shapes)) :id sid
y' (apply max (map (fn [{:keys [y height]}] (+ y height)) shapes)) :name (str "Group " (rand-int 1000))
width (- x' x) :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
: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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;