diff --git a/common/uxbox/common/geom/shapes.cljc b/common/uxbox/common/geom/shapes.cljc index b69700935..143526e0a 100644 --- a/common/uxbox/common/geom/shapes.cljc +++ b/common/uxbox/common/geom/shapes.cljc @@ -398,12 +398,11 @@ "Returns a rect that contains all the shapes and is aware of the rotation of each shape. Mainly used for multiple selection." [shapes] - (let [xf-resolve-shape (map :selrect) - shapes (into [] xf-resolve-shape shapes) - minx (transduce (map :x1) min ##Inf shapes) - miny (transduce (map :y1) min ##Inf shapes) - maxx (transduce (map :x2) max ##-Inf shapes) - maxy (transduce (map :y2) max ##-Inf shapes)] + (let [shapes (map :selrect shapes) + minx (transduce (map :x1) min ##Inf shapes) + miny (transduce (map :y1) min ##Inf shapes) + maxx (transduce (map :x2) max ##-Inf shapes) + maxy (transduce (map :y2) max ##-Inf shapes)] {:x1 minx :y1 miny :x2 maxx diff --git a/frontend/src/uxbox/main/data/workspace.cljs b/frontend/src/uxbox/main/data/workspace.cljs index a72937ebe..4bc6885a3 100644 --- a/frontend/src/uxbox/main/data/workspace.cljs +++ b/frontend/src/uxbox/main/data/workspace.cljs @@ -9,7 +9,6 @@ (ns uxbox.main.data.workspace (:require - [uxbox.util.debug :refer [logjs]] [beicon.core :as rx] [cljs.spec.alpha :as s] [clojure.set :as set] @@ -197,21 +196,53 @@ (declare zoom-to-fit-all) -;; TODO: add-spec - (defn initialize-viewport [{:keys [width height] :as size}] - (ptk/reify ::initialize-viewport - ptk/UpdateEvent - (update [_ state] - (update state :workspace-local - (fn [local] - (-> local - (assoc :vport size) - (update :vbox (fn [vbox] - (if (nil? vbox) - (assoc size :x 0 :y 0) - vbox))))))))) + (letfn [(update* [{:keys [vbox vport] :as local}] + (let [wprop (/ (:width vport) width) + hprop (/ (:height vport) height)] + (-> local + (assoc :vport size) + (update :vbox (fn [vbox] + (-> vbox + (update :width #(/ % wprop)) + (update :height #(/ % hprop)))))))) + + (initialize [state local] + (let [page-id (get-in state [:workspace-page :id]) + objects (get-in state [:workspace-data page-id :objects]) + shapes (cp/select-toplevel-shapes objects {:include-frames? true}) + srect (geom/selection-rect shapes) + local (assoc local :vport size)] + (cond + (or (not (mth/finite? (:width srect))) + (not (mth/finite? (:height srect)))) + (assoc local :vbox (assoc size :x 0 :y 0)) + + (or (> (:width srect) width) + (> (:height srect) height)) + (let [srect (geom/adjust-to-viewport size srect {:padding 40}) + zoom (/ (:width size) (:width srect))] + (-> local + (assoc :zoom zoom) + (update :vbox merge srect))) + + :else + (assoc local :vbox (assoc size + :x (- (:x srect) 40) + :y (- (:y srect) 40)))))) + + (setup [state local] + (if (:vbox local) + (update* local) + (initialize state local)))] + + (ptk/reify ::initialize-viewport + ptk/UpdateEvent + (update [_ state] + (update state :workspace-local + (fn [local] + (setup state local))))))) (defn update-viewport-position [{:keys [x y] :or {x identity y identity}}] @@ -226,8 +257,6 @@ (update :x x) (update :y y))))))) -;; TODO: add spec - (defn update-viewport-size [{:keys [width height] :as size}] (ptk/reify ::update-viewport-size @@ -368,7 +397,7 @@ (let [page-id (get-in state [:workspace-page :id]) objects (get-in state [:workspace-data page-id :objects]) shapes (cp/select-toplevel-shapes objects {:include-frames? true}) - srect (geom/shapes->rect-shape shapes)] + srect (geom/selection-rect shapes)] (if (or (mth/nan? (:width srect)) (mth/nan? (:height srect)))