From f054233a9e288cb30510b0b6386f9531808a1d6e Mon Sep 17 00:00:00 2001 From: Andrey Antukh Date: Fri, 22 Apr 2016 20:02:03 +0300 Subject: [PATCH] Simplify movement and resize alignment code. --- src/uxbox/ui/workspace/base.cljs | 5 +++ src/uxbox/ui/workspace/movement.cljs | 58 +++++++--------------------- src/uxbox/ui/workspace/resize.cljs | 20 +++------- 3 files changed, 25 insertions(+), 58 deletions(-) diff --git a/src/uxbox/ui/workspace/base.cljs b/src/uxbox/ui/workspace/base.cljs index a88bbd8cf..90467f6ae 100644 --- a/src/uxbox/ui/workspace/base.cljs +++ b/src/uxbox/ui/workspace/base.cljs @@ -13,6 +13,7 @@ [uxbox.state :as st] [uxbox.state.project :as stpr] [uxbox.data.workspace :as dw] + [uxbox.data.shapes :as uds] [uxbox.util.geom.point :as gpt] [uxbox.util.lens :as ul] [goog.events :as events]) @@ -118,6 +119,10 @@ (defonce mouse-delta-s (->> mouse-viewport-s (rx/sample 10) + (rx/mapcat (fn [point] + (if @alignment-l + (uds/align-point point) + (rx/of point)))) (rx/buffer 2 1) (rx/map coords-delta) (rx/share))) diff --git a/src/uxbox/ui/workspace/movement.cljs b/src/uxbox/ui/workspace/movement.cljs index 5df0ff54a..9f3249a39 100644 --- a/src/uxbox/ui/workspace/movement.cljs +++ b/src/uxbox/ui/workspace/movement.cljs @@ -18,57 +18,27 @@ [uxbox.util.geom :as geom] [uxbox.util.geom.point :as gpt])) -;; --- Lenses - -(defn- resolve-selected - [state] - (let [selected (get-in state [:workspace :selected]) - xf (map #(get-in state [:shapes-by-id %]))] - (into #{} xf selected))) - -(def ^:const ^:private selected-shapes-l - (-> (l/getter resolve-selected) - (l/focus-atom st/state))) - ;; --- Public Api -(declare initialize) - -(defn watch-move-actions - [] - (as-> uuc/actions-s $ - (rx/filter #(= "ui.shape.move" (:type %)) $) - (rx/on-value $ initialize))) - -;; --- Implementation - (declare watch-movement) -(defn- initialize +(defn watch-move-actions [] - (let [align? @wb/alignment-l - stoper (->> uuc/actions-s - (rx/map :type) - (rx/filter empty?) - (rx/take 1))] - (run! (partial watch-movement stoper align?) - (deref selected-shapes-l)))) + (let [initialize #(run! watch-movement @wb/selected-shapes-l) + stream (rx/filter #(= "ui.shape.move" (:type %)) uuc/actions-s)] + (rx/subscribe stream initialize))) -(defn- handle-movement - [{:keys [id] :as shape} delta] - (rs/emit! (uds/move-shape id delta))) +;; --- Implementation (defn- watch-movement - [stoper align? {:keys [id] :as shape}] - (when align? (rs/emit! (uds/initial-align-shape id))) - (let [stream (->> wb/mouse-viewport-s - (rx/sample 10) - (rx/mapcat (fn [point] - (if align? - (uds/align-point point) - (rx/of point)))) - (rx/buffer 2 1) - (rx/map wb/coords-delta) + [shape] + (let [stoper (->> uuc/actions-s + (rx/map :type) + (rx/filter empty?) + (rx/take 1)) + stream (->> wb/mouse-delta-s (rx/take-until stoper) (rx/map #(gpt/divide % @wb/zoom-l)))] - (rx/subscribe stream (partial handle-movement shape)))) + (when @wb/alignment-l + (rs/emit! (uds/initial-align-shape shape))) + (rx/subscribe stream #(rs/emit! (uds/move-shape shape %))))) diff --git a/src/uxbox/ui/workspace/resize.cljs b/src/uxbox/ui/workspace/resize.cljs index ba8acbf7c..2bc353afd 100644 --- a/src/uxbox/ui/workspace/resize.cljs +++ b/src/uxbox/ui/workspace/resize.cljs @@ -14,7 +14,6 @@ [uxbox.util.geom.point :as gpt])) (declare initialize) -(declare handle-resize) ;; --- Public Api @@ -27,6 +26,8 @@ ;; --- Implementation +(declare handle-resize) + (defn- initialize [event] (let [{:keys [vid shape] :as payload} (:payload event) @@ -34,24 +35,15 @@ (rx/map :type) (rx/filter #(empty? %)) (rx/take 1)) - - align? @wb/alignment-l - stream (->> wb/mouse-viewport-s - (rx/sample 10) - (rx/mapcat (fn [point] - (if align? - (uds/align-point point) - (rx/of point)))) - (rx/buffer 2 1) - (rx/map wb/coords-delta) + stream (->> wb/mouse-delta-s (rx/take-until stoper) (rx/map #(gpt/divide % @wb/zoom-l)) (rx/with-latest-from vector wb/mouse-ctrl-s))] - (when align? + (when @wb/alignment-l (rs/emit! (uds/initial-vertext-align shape vid))) - (rx/subscribe stream #(handle-resize payload %)))) + (rx/subscribe stream #(handle-resize shape vid %)))) (defn- handle-resize - [{:keys [vid shape]} [delta ctrl?]] + [shape vid [delta ctrl?]] (let [params {:vid vid :delta (assoc delta :lock ctrl?)}] (rs/emit! (uds/update-vertex-position shape params))))