mirror of
https://github.com/penpot/penpot.git
synced 2025-02-12 18:18:24 -05:00
✨ Toggle snap on button
This commit is contained in:
parent
f396ef4fa0
commit
74f99f0d48
4 changed files with 42 additions and 25 deletions
|
@ -131,10 +131,11 @@
|
|||
(ms/mouse-up? %))))
|
||||
|
||||
content (get-in state (st/get-path state :content))
|
||||
snap-toggled (get-in state [:workspace-local :edit-path id :snap-toggled])
|
||||
points (ugp/content->points content)
|
||||
|
||||
drag-events-stream
|
||||
(->> (streams/position-stream points)
|
||||
(->> (streams/position-stream snap-toggled points)
|
||||
(rx/take-until stop-stream)
|
||||
(rx/map #(drag-handler %)))]
|
||||
|
||||
|
@ -166,7 +167,10 @@
|
|||
content (get-in state (st/get-path state :content))
|
||||
points (ugp/content->points content)
|
||||
|
||||
drag-events (->> (streams/position-stream points)
|
||||
id (st/get-path-id state)
|
||||
snap-toggled (get-in state [:workspace-local :edit-path id :snap-toggled])
|
||||
|
||||
drag-events (->> (streams/position-stream snap-toggled points)
|
||||
(rx/take-until mouse-up)
|
||||
(rx/map #(drag-handler %)))]
|
||||
|
||||
|
@ -186,10 +190,11 @@
|
|||
(rx/merge-map #(rx/empty))))
|
||||
|
||||
(defn make-drag-stream
|
||||
[stream down-event zoom points]
|
||||
[stream snap-toggled zoom points down-event]
|
||||
(let [mouse-up (->> stream (rx/filter #(or (helpers/end-path-event? %)
|
||||
(ms/mouse-up? %))))
|
||||
drag-events (->> (streams/position-stream points)
|
||||
|
||||
drag-events (->> (streams/position-stream snap-toggled points)
|
||||
(rx/take-until mouse-up)
|
||||
(rx/map #(drag-handler %)))]
|
||||
|
||||
|
@ -219,9 +224,12 @@
|
|||
content (get-in state (st/get-path state :content))
|
||||
points (ugp/content->points content)
|
||||
|
||||
id (st/get-path-id state)
|
||||
snap-toggled (get-in state [:workspace-local :edit-path id :snap-toggled])
|
||||
|
||||
;; Mouse move preview
|
||||
mousemove-events
|
||||
(->> (streams/position-stream points)
|
||||
(->> (streams/position-stream snap-toggled points)
|
||||
(rx/take-until end-path-events)
|
||||
(rx/map #(preview-next-point %)))
|
||||
|
||||
|
@ -229,12 +237,12 @@
|
|||
mousedown-events
|
||||
(->> mouse-down
|
||||
(rx/take-until end-path-events)
|
||||
(rx/with-latest merge (streams/position-stream points))
|
||||
(rx/with-latest merge (streams/position-stream snap-toggled points))
|
||||
|
||||
;; We change to the stream that emits the first event
|
||||
(rx/switch-map
|
||||
#(rx/race (make-node-events-stream stream)
|
||||
(make-drag-stream stream % zoom points))))]
|
||||
(make-drag-stream stream snap-toggled zoom points %))))]
|
||||
|
||||
(rx/concat
|
||||
(rx/of (common/init-path))
|
||||
|
|
|
@ -118,6 +118,7 @@
|
|||
zoom (get-in state [:workspace-local :zoom])
|
||||
id (get-in state [:workspace-local :edition])
|
||||
selected-points (get-in state [:workspace-local :edit-path id :selected-points] #{})
|
||||
snap-toggled (get-in state [:workspace-local :edit-path id :snap-toggled])
|
||||
selected? (contains? selected-points position)
|
||||
|
||||
content (get-in state (st/get-path state :content))
|
||||
|
@ -127,7 +128,7 @@
|
|||
(rx/concat
|
||||
;; This stream checks the consecutive mouse positions to do the draging
|
||||
(->> points
|
||||
(streams/move-points-stream start-position selected-points)
|
||||
(streams/move-points-stream snap-toggled start-position selected-points)
|
||||
(rx/take-until stopper)
|
||||
(rx/map #(move-selected-path-point start-position %)))
|
||||
(rx/of (apply-content-modifiers)))
|
||||
|
@ -162,11 +163,12 @@
|
|||
handler (-> content (get index) (ugp/get-handler prefix))
|
||||
|
||||
current-distance (when opposite-handler (gpt/distance (ugp/opposite-handler point handler) opposite-handler))
|
||||
match-opposite? (and opposite-handler (mth/almost-zero? current-distance))]
|
||||
match-opposite? (and opposite-handler (mth/almost-zero? current-distance))
|
||||
snap-toggled (get-in state [:workspace-local :edit-path id :snap-toggled])]
|
||||
|
||||
(streams/drag-stream
|
||||
(rx/concat
|
||||
(->> (streams/move-handler-stream start-point handler points)
|
||||
(->> (streams/move-handler-stream snap-toggled start-point handler points)
|
||||
(rx/take-until (->> stream (rx/filter ms/mouse-up?)))
|
||||
(rx/map
|
||||
(fn [{:keys [x y alt? shift?]}]
|
||||
|
|
|
@ -55,7 +55,7 @@
|
|||
(* (mth/floor (/ num k)) k)))
|
||||
|
||||
(defn move-points-stream
|
||||
[start-point selected-points points]
|
||||
[snap-toggled start-point selected-points points]
|
||||
|
||||
(let [zoom (get-in @st/state [:workspace-local :zoom] 1)
|
||||
ranges (snap/create-ranges points selected-points)
|
||||
|
@ -63,15 +63,17 @@
|
|||
|
||||
check-path-snap
|
||||
(fn [position]
|
||||
(let [delta (gpt/subtract position start-point)
|
||||
moved-points (->> selected-points (mapv #(gpt/add % delta)))
|
||||
snap (snap/get-snap-delta moved-points ranges d-pos)]
|
||||
(gpt/add position snap)))]
|
||||
(if snap-toggled
|
||||
(let [delta (gpt/subtract position start-point)
|
||||
moved-points (->> selected-points (mapv #(gpt/add % delta)))
|
||||
snap (snap/get-snap-delta moved-points ranges d-pos)]
|
||||
(gpt/add position snap))
|
||||
position))]
|
||||
(->> ms/mouse-position
|
||||
(rx/map check-path-snap))))
|
||||
|
||||
(defn move-handler-stream
|
||||
[start-point handler points]
|
||||
[snap-toggled start-point handler points]
|
||||
|
||||
(let [zoom (get-in @st/state [:workspace-local :zoom] 1)
|
||||
ranges (snap/create-ranges points)
|
||||
|
@ -79,15 +81,17 @@
|
|||
|
||||
check-path-snap
|
||||
(fn [position]
|
||||
(let [delta (gpt/subtract position start-point)
|
||||
handler-position (gpt/add handler delta)
|
||||
snap (snap/get-snap-delta [handler-position] ranges d-pos)]
|
||||
(gpt/add position snap)))]
|
||||
(if snap-toggled
|
||||
(let [delta (gpt/subtract position start-point)
|
||||
handler-position (gpt/add handler delta)
|
||||
snap (snap/get-snap-delta [handler-position] ranges d-pos)]
|
||||
(gpt/add position snap))
|
||||
position))]
|
||||
(->> ms/mouse-position
|
||||
(rx/map check-path-snap))))
|
||||
|
||||
(defn position-stream
|
||||
[points]
|
||||
[snap-toggled points]
|
||||
(let [zoom (get-in @st/state [:workspace-local :zoom] 1)
|
||||
;; ranges (snap/create-ranges points)
|
||||
d-pos (/ snap/snap-path-accuracy zoom)
|
||||
|
@ -105,8 +109,10 @@
|
|||
(->> ms/mouse-position
|
||||
(rx/with-latest vector ranges-stream)
|
||||
(rx/map (fn [[position ranges]]
|
||||
(let [snap (snap/get-snap-delta [position] ranges d-pos)]
|
||||
(gpt/add position snap))))
|
||||
(if snap-toggled
|
||||
(let [snap (snap/get-snap-delta [position] ranges d-pos)]
|
||||
(gpt/add position snap))
|
||||
position)))
|
||||
|
||||
(rx/with-latest merge (->> ms/mouse-position-shift (rx/map #(hash-map :shift? %))))
|
||||
(rx/with-latest merge (->> ms/mouse-position-alt (rx/map #(hash-map :alt? %)))))))
|
||||
|
|
|
@ -171,7 +171,8 @@
|
|||
moving-nodes
|
||||
moving-handler
|
||||
hover-handlers
|
||||
hover-points]
|
||||
hover-points
|
||||
snap-toggled]
|
||||
:as edit-path} (mf/deref edit-path-ref)
|
||||
|
||||
selected-points (or selected-points #{})
|
||||
|
@ -200,7 +201,7 @@
|
|||
[(->> selected-points (map base->point) (into #{}))
|
||||
(->> points (remove selected-points) (into #{}))])
|
||||
|
||||
show-snap? (or (some? drag-handler) (some? preview) (some? moving-handler) moving-nodes)
|
||||
show-snap? (and snap-toggled (or (some? drag-handler) (some? preview) (some? moving-handler) moving-nodes))
|
||||
|
||||
handle-double-click-outside
|
||||
(fn [event]
|
||||
|
|
Loading…
Add table
Reference in a new issue