mirror of
https://github.com/penpot/penpot.git
synced 2025-08-07 14:38:33 +02:00
Add alignment to movement.
This commit is contained in:
parent
8c99683eaf
commit
7d17926060
3 changed files with 44 additions and 102 deletions
|
@ -14,23 +14,19 @@
|
||||||
[uxbox.util.geom.point :as gpt]
|
[uxbox.util.geom.point :as gpt]
|
||||||
[uxbox.util.workers :as uw]))
|
[uxbox.util.workers :as uw]))
|
||||||
|
|
||||||
(defn- move
|
;; (defn- move
|
||||||
[shape p1]
|
;; [shape p1]
|
||||||
(let [dx (- (:x2 shape) (:x1 shape))
|
;; (let [dx (- (:x2 shape) (:x1 shape))
|
||||||
dy (- (:y2 shape) (:y1 shape))
|
;; dy (- (:y2 shape) (:y1 shape))
|
||||||
p2 (gpt/add p1 [dx dy])]
|
;; p2 (gpt/add p1 [dx dy])]
|
||||||
(assoc shape
|
;; (assoc shape
|
||||||
:x1 (:x p1)
|
;; :x1 (:x p1)
|
||||||
:y1 (:y p1)
|
;; :y1 (:y p1)
|
||||||
:x2 (:x p2)
|
;; :x2 (:x p2)
|
||||||
:y2 (:y p2))))
|
;; :y2 (:y p2))))
|
||||||
|
|
||||||
(defn translate
|
(defn translate
|
||||||
[{:keys [x1 y1] :as shape}]
|
[point]
|
||||||
(let [message {:cmd :grid/align
|
(let [message {:cmd :grid/align :point point}]
|
||||||
:point (gpt/point x1 y1)}]
|
|
||||||
(->> (uw/ask! worker message)
|
(->> (uw/ask! worker message)
|
||||||
(rx/map (fn [{:keys [point]}]
|
(rx/map :point))))
|
||||||
(if point
|
|
||||||
(move shape point)
|
|
||||||
shape))))))
|
|
||||||
|
|
|
@ -58,6 +58,14 @@
|
||||||
(-> (l/in [:workspace :zoom])
|
(-> (l/in [:workspace :zoom])
|
||||||
(l/focus-atom st/state)))
|
(l/focus-atom st/state)))
|
||||||
|
|
||||||
|
(def ^:const alignment-l
|
||||||
|
(letfn [(getter [flags]
|
||||||
|
(and (contains? flags :grid/indexed)
|
||||||
|
(contains? flags :grid/alignment)
|
||||||
|
(contains? flags :grid)))]
|
||||||
|
(-> (l/getter getter)
|
||||||
|
(l/focus-atom flags-l))))
|
||||||
|
|
||||||
;; --- Scroll Stream
|
;; --- Scroll Stream
|
||||||
|
|
||||||
(defonce scroll-b (rx/bus))
|
(defonce scroll-b (rx/bus))
|
||||||
|
@ -113,3 +121,4 @@
|
||||||
(rx/buffer 2 1)
|
(rx/buffer 2 1)
|
||||||
(rx/map coords-delta)
|
(rx/map coords-delta)
|
||||||
(rx/share)))
|
(rx/share)))
|
||||||
|
|
||||||
|
|
|
@ -22,16 +22,6 @@
|
||||||
|
|
||||||
;; --- Lenses
|
;; --- Lenses
|
||||||
|
|
||||||
;; (declare translate-to-viewport)
|
|
||||||
|
|
||||||
;; (defn- resolve-selected
|
|
||||||
;; [state]
|
|
||||||
;; (let [selected (get-in state [:workspace :selected])
|
|
||||||
;; xf (comp
|
|
||||||
;; (map #(get-in state [:shapes-by-id %]))
|
|
||||||
;; (map translate-to-viewport))]
|
|
||||||
;; (into #{} xf selected)))
|
|
||||||
|
|
||||||
(defn- resolve-selected
|
(defn- resolve-selected
|
||||||
[state]
|
[state]
|
||||||
(let [selected (get-in state [:workspace :selected])
|
(let [selected (get-in state [:workspace :selected])
|
||||||
|
@ -42,14 +32,6 @@
|
||||||
(-> (l/getter resolve-selected)
|
(-> (l/getter resolve-selected)
|
||||||
(l/focus-atom st/state)))
|
(l/focus-atom st/state)))
|
||||||
|
|
||||||
(def ^:const ^:private alignment-l
|
|
||||||
(letfn [(getter [flags]
|
|
||||||
(and (contains? flags :grid/indexed)
|
|
||||||
(contains? flags :grid/alignment)
|
|
||||||
(contains? flags :grid)))]
|
|
||||||
(-> (l/getter getter)
|
|
||||||
(l/focus-atom wb/flags-l))))
|
|
||||||
|
|
||||||
;; --- Public Api
|
;; --- Public Api
|
||||||
|
|
||||||
(declare initialize)
|
(declare initialize)
|
||||||
|
@ -62,77 +44,32 @@
|
||||||
|
|
||||||
;; --- Implementation
|
;; --- Implementation
|
||||||
|
|
||||||
;; (def coords
|
(declare watch-movement)
|
||||||
;; (gpt/point c/canvas-start-x
|
|
||||||
;; c/canvas-start-y))
|
|
||||||
|
|
||||||
;; (defn- translate-to-viewport
|
(defn- initialize
|
||||||
;; [shape]
|
[]
|
||||||
;; (let [dx (- (:x2 shape) (:x1 shape))
|
(let [align? @wb/alignment-l
|
||||||
;; dy (- (:y2 shape) (:y1 shape))
|
stoper (->> uuc/actions-s
|
||||||
;; p1 (gpt/point (:x1 shape) (:y1 shape))
|
(rx/map :type)
|
||||||
;; p2 (gpt/add p1 coords)
|
(rx/filter empty?)
|
||||||
;; p3 (gpt/add p2 [dx dy])]
|
(rx/take 1))]
|
||||||
;; (assoc shape
|
(run! (partial watch-movement stoper align?)
|
||||||
;; :x1 (:x p2)
|
(deref selected-shapes-l))))
|
||||||
;; :y1 (:y p2)
|
|
||||||
;; :x2 (:x p3)
|
|
||||||
;; :y2 (:y p3))))
|
|
||||||
|
|
||||||
;; (defn- translate-to-canvas
|
|
||||||
;; [shape]
|
|
||||||
;; (let [dx (- (:x2 shape) (:x1 shape))
|
|
||||||
;; dy (- (:y2 shape) (:y1 shape))
|
|
||||||
;; p1 (gpt/point (:x1 shape) (:y1 shape))
|
|
||||||
;; p2 (gpt/subtract p1 coords)
|
|
||||||
;; p3 (gpt/add p2 [dx dy])]
|
|
||||||
;; (assoc shape
|
|
||||||
;; :x1 (:x p2)
|
|
||||||
;; :y1 (:y p2)
|
|
||||||
;; :x2 (:x p3)
|
|
||||||
;; :y2 (:y p3))))
|
|
||||||
|
|
||||||
;; (defn- initialize
|
|
||||||
;; []
|
|
||||||
;; (let [shapes @selected-shapes-l
|
|
||||||
;; align? @alignment-l
|
|
||||||
;; stoper (->> uuc/actions-s
|
|
||||||
;; (rx/map :type)
|
|
||||||
;; (rx/filter empty?)
|
|
||||||
;; (rx/take 1))]
|
|
||||||
;; (as-> wb/mouse-delta-s $
|
|
||||||
;; (rx/take-until stoper $)
|
|
||||||
;; (rx/map #(gpt/divide % @wb/zoom-l) $)
|
|
||||||
;; (rx/scan (fn [acc delta]
|
|
||||||
;; (let [xf (map #(geom/move % delta))]
|
|
||||||
;; (into [] xf acc))) shapes $)
|
|
||||||
;; (rx/mapcat (fn [items]
|
|
||||||
;; (if align?
|
|
||||||
;; (->> (apply rx/of items)
|
|
||||||
;; (rx/mapcat align/translate)
|
|
||||||
;; (rx/reduce conj []))
|
|
||||||
;; (rx/of items))) $)
|
|
||||||
;; (rx/map (fn [items]
|
|
||||||
;; (mapv translate-to-canvas items)) $)
|
|
||||||
;; (rx/subscribe $ handle-movement))))
|
|
||||||
|
|
||||||
(defn- handle-movement
|
(defn- handle-movement
|
||||||
[{:keys [id] :as shape} delta]
|
[{:keys [id] :as shape} delta]
|
||||||
(rs/emit! (uds/move-shape id delta)))
|
(rs/emit! (uds/move-shape id delta)))
|
||||||
|
|
||||||
(defn- watch-movement
|
(defn- watch-movement
|
||||||
[stoper shape]
|
[stoper align? shape]
|
||||||
(as-> wb/mouse-delta-s $
|
(let [stream (->> wb/mouse-viewport-s
|
||||||
(rx/take-until stoper $)
|
(rx/sample 10)
|
||||||
(rx/map #(gpt/divide % @wb/zoom-l) $)
|
(rx/mapcat (fn [point]
|
||||||
(rx/subscribe $ (partial handle-movement shape))))
|
(if align?
|
||||||
|
(align/translate point)
|
||||||
(defn- initialize
|
(rx/of point))))
|
||||||
[]
|
(rx/buffer 2 1)
|
||||||
(let [;; align? @alignment-l
|
(rx/map wb/coords-delta)
|
||||||
stoper (->> uuc/actions-s
|
(rx/take-until stoper)
|
||||||
(rx/map :type)
|
(rx/map #(gpt/divide % @wb/zoom-l)))]
|
||||||
(rx/filter empty?)
|
(rx/subscribe stream (partial handle-movement shape))))
|
||||||
(rx/take 1))]
|
|
||||||
(run! (partial watch-movement stoper)
|
|
||||||
(deref selected-shapes-l))))
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue