From 4bdaab637ccb5666ae0ce05c05f461110554a673 Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Tue, 9 Jun 2020 15:16:32 +0200 Subject: [PATCH] :bug: Remove leaks of selrect attrs in the root shape object. --- common/uxbox/common/geom/shapes.cljc | 41 +++----------- .../uxbox/main/data/workspace/drawing.cljs | 56 +++++++++---------- frontend/src/uxbox/main/exports.cljs | 2 +- frontend/src/uxbox/worker/selection.cljs | 2 +- 4 files changed, 39 insertions(+), 62 deletions(-) diff --git a/common/uxbox/common/geom/shapes.cljc b/common/uxbox/common/geom/shapes.cljc index a2b205bef..4b96e7dff 100644 --- a/common/uxbox/common/geom/shapes.cljc +++ b/common/uxbox/common/geom/shapes.cljc @@ -48,11 +48,7 @@ (update :y inc-y))))] (-> shape (update :x inc-x) - (update :x1 inc-x) - (update :x2 inc-x) (update :y inc-y) - (update :y1 inc-y) - (update :y2 inc-y) (update-in [:selrect :x] inc-x) (update-in [:selrect :x1] inc-x) (update-in [:selrect :x2] inc-x) @@ -197,11 +193,10 @@ (us/assert number? width) (us/assert number? height) (-> shape - (assoc :width width - :height height - :x2 (+ (:x1 shape) width) - :y2 (+ (:y1 shape) height)) - (update :selrect (nilf #(resize % width height))))) + (assoc :width width :height height) + (update :selrect (fn [shape] + (assoc :x2 (+ (:x1 shape) width) + :y2 (+ (:y1 shape) height)))))) ;; --- Setup (Initialize) @@ -247,30 +242,16 @@ (declare group->rect-shape) (declare rect->rect-shape) +;; TODO: completly remove + (defn shape->rect-shape "Coerce shape to rect like shape." + [{:keys [type] :as shape}] (case type (:curve :path) (path->rect-shape shape) (rect->rect-shape shape))) -(defn shapes->rect-shape - [shapes] - (let [shapes (mapv shape->rect-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)] - {:x1 minx - :y1 miny - :x2 maxx - :y2 maxy - :x minx - :y miny - :width (- maxx minx) - :height (- maxy miny) - :type :rect})) - ;; -- Points (declare transform-shape-point) @@ -603,10 +584,10 @@ (defn pad-selrec ([selrect] (pad-selrec selrect 1)) - ([selrec size] + ([selrect size] (let [inc #(+ % size) dec #(- % size)] - (-> selrec + (-> selrect (update :x dec) (update :y dec) (update :x1 dec) @@ -728,10 +709,6 @@ (-> rect-shape (update :x check) (update :y check) - (update :x1 check) - (update :y1 check) - (update :x2 check) - (update :y2 check) (update :width (comp to-positive check)) (update :height (comp to-positive check))))) diff --git a/frontend/src/uxbox/main/data/workspace/drawing.cljs b/frontend/src/uxbox/main/data/workspace/drawing.cljs index 7e1cf7dee..803d12180 100644 --- a/frontend/src/uxbox/main/data/workspace/drawing.cljs +++ b/frontend/src/uxbox/main/data/workspace/drawing.cljs @@ -67,14 +67,13 @@ (letfn [(resize-shape [{:keys [x y width height] :as shape} point lock? point-snap] (let [;; The new shape behaves like a resize on the bottom-right corner initial (gpt/point (+ x width) (+ y height)) - shape' (geom/shape->rect-shape shape) - shapev (gpt/point (:width shape') (:height shape')) - deltav (gpt/to-vec initial point-snap) - scalev (gpt/divide (gpt/add shapev deltav) shapev) - scalev (if lock? - (let [v (max (:x scalev) (:y scalev))] - (gpt/point v v)) - scalev)] + shapev (gpt/point width height) + deltav (gpt/to-vec initial point-snap) + scalev (gpt/divide (gpt/add shapev deltav) shapev) + scalev (if lock? + (let [v (max (:x scalev) (:y scalev))] + (gpt/point v v)) + scalev)] (-> shape (assoc-in [:modifiers :resize-vector] scalev) (assoc-in [:modifiers :resize-origin] (gpt/point x y)) @@ -87,45 +86,46 @@ ptk/WatchEvent (watch [_ state stream] (let [{:keys [flags]} (:workspace-local state) + stoper? #(or (ms/mouse-up? %) (= % :interrupt)) - stoper (rx/filter stoper? stream) + stoper (rx/filter stoper? stream) initial @ms/mouse-position page-id (get state :current-page-id) objects (get-in state [:workspace-data page-id :objects]) - layout (get state :workspace-layout) + layout (get state :workspace-layout) - frames (->> objects - vals - (filter (comp #{:frame} :type)) - (remove #(= (:id %) uuid/zero) )) - - frame-id (or (->> frames - (filter #(geom/has-point? % initial)) - first - :id) - uuid/zero) + frames (cp/select-frames objects) + fid (or (->> frames + (filter #(geom/has-point? % initial)) + first + :id) + uuid/zero) shape (-> state (get-in [:workspace-local :drawing]) (geom/setup {:x (:x initial) :y (:y initial) :width 1 :height 1}) - (assoc :frame-id frame-id) + (assoc :frame-id fid) (assoc ::initialized? true))] (rx/concat + ;; Add shape to drawing state (rx/of #(assoc-in state [:workspace-local :drawing] shape)) + ;; Initial SNAP (->> (snap/closest-snap-point page-id [shape] layout initial) (rx/map (fn [{:keys [x y]}] - #(-> % - (assoc-in [:workspace-local :drawing :x] x) - (assoc-in [:workspace-local :drawing :y] y))))) + #(update-in % [:workspace-local :drawing] assoc :x x :y y)))) (->> ms/mouse-position (rx/with-latest vector ms/mouse-position-ctrl) - (rx/switch-map (fn [[point :as current]] - (->> (snap/closest-snap-point page-id [shape] layout point) - (rx/map #(conj current %))))) - (rx/map (fn [[pt ctrl? point-snap]] #(update-drawing % pt ctrl? point-snap))) + (rx/switch-map + (fn [[point :as current]] + (->> (snap/closest-snap-point page-id [shape] layout point) + (rx/map #(conj current %))))) + (rx/map + (fn [[pt ctrl? point-snap]] + #(update-drawing % pt ctrl? point-snap))) + (rx/take-until stoper)) (rx/of handle-finish-drawing))))))) diff --git a/frontend/src/uxbox/main/exports.cljs b/frontend/src/uxbox/main/exports.cljs index b5eb81780..6802e84ad 100644 --- a/frontend/src/uxbox/main/exports.cljs +++ b/frontend/src/uxbox/main/exports.cljs @@ -40,7 +40,7 @@ (defn- calculate-dimensions [{:keys [objects] :as data} vport] (let [shapes (cp/select-toplevel-shapes objects {:include-frames? true})] - (->> (geom/shapes->rect-shape shapes) + (->> (geom/selection-rect shapes) (geom/adjust-to-viewport vport) (geom/fix-invalid-rect-values)))) diff --git a/frontend/src/uxbox/worker/selection.cljs b/frontend/src/uxbox/worker/selection.cljs index e4ae865e8..b75dc7f22 100644 --- a/frontend/src/uxbox/worker/selection.cljs +++ b/frontend/src/uxbox/worker/selection.cljs @@ -65,7 +65,7 @@ [objects] (let [shapes (->> (cp/select-toplevel-shapes objects {:include-frames? true}) (map #(merge % (select-keys % [:x :y :width :height])))) - bounds (geom/shapes->rect-shape shapes) + bounds (geom/selection-rect shapes) bounds #js {:x (:x bounds) :y (:y bounds) :width (:width bounds)