diff --git a/src/uxbox/data/workspace.cljs b/src/uxbox/data/workspace.cljs index 432b3b375..8ccba1ebd 100644 --- a/src/uxbox/data/workspace.cljs +++ b/src/uxbox/data/workspace.cljs @@ -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." diff --git a/src/uxbox/ui/workspace/canvas.cljs b/src/uxbox/ui/workspace/canvas.cljs index 6653576e0..1927f2f1f 100644 --- a/src/uxbox/ui/workspace/canvas.cljs +++ b/src/uxbox/ui/workspace/canvas.cljs @@ -122,7 +122,6 @@ :name "selected-shapes" :mixins [mx/static rum/reactive (mx/local {})]})) - ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Shape ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;