0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-14 11:09:04 -05:00

Simplify movement and resize alignment code.

This commit is contained in:
Andrey Antukh 2016-04-22 20:02:03 +03:00
parent c2e63d5603
commit f054233a9e
No known key found for this signature in database
GPG key ID: 4DFEBCB8316A8B95
3 changed files with 25 additions and 58 deletions

View file

@ -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)))

View file

@ -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 %)))))

View file

@ -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))))