mirror of
https://github.com/penpot/penpot.git
synced 2025-06-02 22:01:39 +02:00
⬆️ Upgrade to beicon2 (part2)
This commit is contained in:
parent
96f5a33f5f
commit
ccf063b8ef
5 changed files with 67 additions and 49 deletions
|
@ -24,6 +24,7 @@
|
||||||
[app.main.data.workspace.state-helpers :as wsh]
|
[app.main.data.workspace.state-helpers :as wsh]
|
||||||
[app.main.snap :as snap]
|
[app.main.snap :as snap]
|
||||||
[app.main.streams :as ms]
|
[app.main.streams :as ms]
|
||||||
|
[app.util.array :as array]
|
||||||
[app.util.mouse :as mse]
|
[app.util.mouse :as mse]
|
||||||
[beicon.v2.core :as rx]
|
[beicon.v2.core :as rx]
|
||||||
[potok.v2.core :as ptk]))
|
[potok.v2.core :as ptk]))
|
||||||
|
@ -128,12 +129,11 @@
|
||||||
|
|
||||||
(->> ms/mouse-position
|
(->> ms/mouse-position
|
||||||
(rx/filter #(> (gpt/distance % initial) (/ 2 zoom)))
|
(rx/filter #(> (gpt/distance % initial) (/ 2 zoom)))
|
||||||
(rx/with-latest vector ms/mouse-position-shift)
|
(rx/with-latest-from ms/mouse-position-shift ms/mouse-position-mod)
|
||||||
(rx/with-latest conj ms/mouse-position-mod)
|
|
||||||
(rx/switch-map
|
(rx/switch-map
|
||||||
(fn [[point :as current]]
|
(fn [[point :as current]]
|
||||||
(->> (snap/closest-snap-point page-id [shape] objects layout zoom focus point)
|
(->> (snap/closest-snap-point page-id [shape] objects layout zoom focus point)
|
||||||
(rx/map #(conj current %)))))
|
(rx/map (partial array/conj current)))))
|
||||||
(rx/map
|
(rx/map
|
||||||
(fn [[_ shift? mod? point]]
|
(fn [[_ shift? mod? point]]
|
||||||
#(update-drawing % initial (cond-> point snap-pixel? (gpt/round-step snap-prec)) shift? mod?)))))
|
#(update-drawing % initial (cond-> point snap-pixel? (gpt/round-step snap-prec)) shift? mod?)))))
|
||||||
|
|
|
@ -46,7 +46,8 @@
|
||||||
command (helpers/next-node shape position last-point prev-handler)]
|
command (helpers/next-node shape position last-point prev-handler)]
|
||||||
(assoc-in state [:workspace-local :edit-path id :preview] command)))))
|
(assoc-in state [:workspace-local :edit-path id :preview] command)))))
|
||||||
|
|
||||||
(defn add-node [{:keys [x y shift?]}]
|
(defn add-node
|
||||||
|
[{:keys [x y shift?]}]
|
||||||
(ptk/reify ::add-node
|
(ptk/reify ::add-node
|
||||||
ptk/UpdateEvent
|
ptk/UpdateEvent
|
||||||
(update [_ state]
|
(update [_ state]
|
||||||
|
@ -135,7 +136,7 @@
|
||||||
(first handlers))
|
(first handlers))
|
||||||
|
|
||||||
drag-events-stream
|
drag-events-stream
|
||||||
(->> (streams/position-stream)
|
(->> (streams/position-stream state)
|
||||||
(rx/map #(drag-handler position idx prefix %))
|
(rx/map #(drag-handler position idx prefix %))
|
||||||
(rx/take-until
|
(rx/take-until
|
||||||
(rx/merge
|
(rx/merge
|
||||||
|
@ -164,7 +165,7 @@
|
||||||
(defn start-path-from-point [position]
|
(defn start-path-from-point [position]
|
||||||
(ptk/reify ::start-path-from-point
|
(ptk/reify ::start-path-from-point
|
||||||
ptk/WatchEvent
|
ptk/WatchEvent
|
||||||
(watch [_ _ stream]
|
(watch [_ state stream]
|
||||||
(let [stoper (rx/merge
|
(let [stoper (rx/merge
|
||||||
(->> stream
|
(->> stream
|
||||||
(rx/filter mse/mouse-event?)
|
(rx/filter mse/mouse-event?)
|
||||||
|
@ -172,7 +173,7 @@
|
||||||
(->> stream
|
(->> stream
|
||||||
(rx/filter helpers/end-path-event?)))
|
(rx/filter helpers/end-path-event?)))
|
||||||
|
|
||||||
drag-events (->> (streams/position-stream)
|
drag-events (->> (streams/position-stream state)
|
||||||
(rx/map #(drag-handler %))
|
(rx/map #(drag-handler %))
|
||||||
(rx/take-until stoper))]
|
(rx/take-until stoper))]
|
||||||
(rx/concat
|
(rx/concat
|
||||||
|
@ -190,7 +191,12 @@
|
||||||
(rx/merge-map #(rx/empty))))
|
(rx/merge-map #(rx/empty))))
|
||||||
|
|
||||||
(defn make-drag-stream
|
(defn make-drag-stream
|
||||||
[stream down-event]
|
[state stream down-event]
|
||||||
|
|
||||||
|
(dm/assert!
|
||||||
|
"should be a pointer"
|
||||||
|
(gpt/point? down-event))
|
||||||
|
|
||||||
(let [stoper (rx/merge
|
(let [stoper (rx/merge
|
||||||
(->> stream
|
(->> stream
|
||||||
(rx/filter mse/mouse-event?)
|
(rx/filter mse/mouse-event?)
|
||||||
|
@ -198,7 +204,7 @@
|
||||||
(->> stream
|
(->> stream
|
||||||
(rx/filter helpers/end-path-event?)))
|
(rx/filter helpers/end-path-event?)))
|
||||||
|
|
||||||
drag-events (->> (streams/position-stream)
|
drag-events (->> (streams/position-stream state)
|
||||||
(rx/map #(drag-handler %))
|
(rx/map #(drag-handler %))
|
||||||
(rx/take-until stoper))]
|
(rx/take-until stoper))]
|
||||||
(rx/concat
|
(rx/concat
|
||||||
|
@ -217,7 +223,7 @@
|
||||||
(assoc-in state [:workspace-local :edit-path id :edit-mode] :draw)))
|
(assoc-in state [:workspace-local :edit-path id :edit-mode] :draw)))
|
||||||
|
|
||||||
ptk/WatchEvent
|
ptk/WatchEvent
|
||||||
(watch [_ _ stream]
|
(watch [_ state stream]
|
||||||
(let [mouse-down (->> stream
|
(let [mouse-down (->> stream
|
||||||
(rx/filter mse/mouse-event?)
|
(rx/filter mse/mouse-event?)
|
||||||
(rx/filter mse/mouse-down-event?))
|
(rx/filter mse/mouse-down-event?))
|
||||||
|
@ -226,7 +232,7 @@
|
||||||
|
|
||||||
;; Mouse move preview
|
;; Mouse move preview
|
||||||
mousemove-events
|
mousemove-events
|
||||||
(->> (streams/position-stream)
|
(->> (streams/position-stream state)
|
||||||
(rx/take-until end-path-events)
|
(rx/take-until end-path-events)
|
||||||
(rx/map #(preview-next-point %)))
|
(rx/map #(preview-next-point %)))
|
||||||
|
|
||||||
|
@ -234,12 +240,13 @@
|
||||||
mousedown-events
|
mousedown-events
|
||||||
(->> mouse-down
|
(->> mouse-down
|
||||||
(rx/take-until end-path-events)
|
(rx/take-until end-path-events)
|
||||||
(rx/with-latest merge (streams/position-stream))
|
;; We just ignore the mouse event and stream down the
|
||||||
|
;; last position event
|
||||||
|
(rx/with-latest-from #(-> %2) (streams/position-stream state))
|
||||||
;; We change to the stream that emits the first event
|
;; We change to the stream that emits the first event
|
||||||
(rx/switch-map
|
(rx/switch-map
|
||||||
#(rx/race (make-node-events-stream stream)
|
#(rx/race (make-node-events-stream stream)
|
||||||
(make-drag-stream stream %))))]
|
(make-drag-stream state stream %))))]
|
||||||
|
|
||||||
(rx/concat
|
(rx/concat
|
||||||
(rx/of (undo/start-path-undo))
|
(rx/of (undo/start-path-undo))
|
||||||
|
|
|
@ -115,7 +115,6 @@
|
||||||
|
|
||||||
(defn move-handler-stream
|
(defn move-handler-stream
|
||||||
[start-point node handler opposite points]
|
[start-point node handler opposite points]
|
||||||
|
|
||||||
(let [zoom (get-in @st/state [:workspace-local :zoom] 1)
|
(let [zoom (get-in @st/state [:workspace-local :zoom] 1)
|
||||||
ranges (snap/create-ranges points)
|
ranges (snap/create-ranges points)
|
||||||
d-pos (/ snap/snap-path-accuracy zoom)
|
d-pos (/ snap/snap-path-accuracy zoom)
|
||||||
|
@ -145,16 +144,20 @@
|
||||||
(let [snap (snap/get-snap-delta [handler] ranges d-pos)]
|
(let [snap (snap/get-snap-delta [handler] ranges d-pos)]
|
||||||
(merge position (gpt/add position snap)))))
|
(merge position (gpt/add position snap)))))
|
||||||
position))]
|
position))]
|
||||||
|
|
||||||
(->> ms/mouse-position
|
(->> ms/mouse-position
|
||||||
(rx/map to-pixel-snap)
|
(rx/map to-pixel-snap)
|
||||||
(rx/with-latest merge (->> ms/mouse-position-shift (rx/map #(hash-map :shift? %))))
|
(rx/with-latest-from
|
||||||
(rx/with-latest merge (->> ms/mouse-position-alt (rx/map #(hash-map :alt? %))))
|
(fn [position shift? alt?]
|
||||||
|
(assoc position :shift? shift? :alt? alt?))
|
||||||
|
ms/mouse-position-shift
|
||||||
|
ms/mouse-position-alt)
|
||||||
(rx/with-latest-from (snap-toggled-stream))
|
(rx/with-latest-from (snap-toggled-stream))
|
||||||
(rx/map check-path-snap))))
|
(rx/map check-path-snap))))
|
||||||
|
|
||||||
(defn position-stream
|
(defn position-stream
|
||||||
[]
|
[state]
|
||||||
(let [zoom (get-in @st/state [:workspace-local :zoom] 1)
|
(let [zoom (get-in state [:workspace-local :zoom] 1)
|
||||||
d-pos (/ snap/snap-path-accuracy zoom)
|
d-pos (/ snap/snap-path-accuracy zoom)
|
||||||
get-content #(pst/get-path % :content)
|
get-content #(pst/get-path % :content)
|
||||||
|
|
||||||
|
@ -169,12 +172,14 @@
|
||||||
|
|
||||||
(->> ms/mouse-position
|
(->> ms/mouse-position
|
||||||
(rx/map to-pixel-snap)
|
(rx/map to-pixel-snap)
|
||||||
(rx/with-latest vector ranges-stream)
|
(rx/with-latest-from ranges-stream (snap-toggled-stream))
|
||||||
(rx/with-latest-from (snap-toggled-stream))
|
(rx/map (fn [[position ranges snap-toggled]]
|
||||||
(rx/map (fn [[[position ranges] snap-toggled]]
|
|
||||||
(if snap-toggled
|
(if snap-toggled
|
||||||
(let [snap (snap/get-snap-delta [position] ranges d-pos)]
|
(let [snap (snap/get-snap-delta [position] ranges d-pos)]
|
||||||
(gpt/add position snap))
|
(gpt/add position snap))
|
||||||
position)))
|
position)))
|
||||||
(rx/with-latest merge (->> ms/mouse-position-shift (rx/map #(hash-map :shift? %))))
|
(rx/with-latest-from
|
||||||
(rx/with-latest merge (->> ms/mouse-position-alt (rx/map #(hash-map :alt? %)))))))
|
(fn [position shift? alt?]
|
||||||
|
(assoc position :shift? shift? :alt? alt?))
|
||||||
|
ms/mouse-position-shift
|
||||||
|
ms/mouse-position-alt))))
|
||||||
|
|
|
@ -32,6 +32,7 @@
|
||||||
[app.main.data.workspace.undo :as dwu]
|
[app.main.data.workspace.undo :as dwu]
|
||||||
[app.main.snap :as snap]
|
[app.main.snap :as snap]
|
||||||
[app.main.streams :as ms]
|
[app.main.streams :as ms]
|
||||||
|
[app.util.array :as array]
|
||||||
[app.util.dom :as dom]
|
[app.util.dom :as dom]
|
||||||
[app.util.keyboard :as kbd]
|
[app.util.keyboard :as kbd]
|
||||||
[app.util.mouse :as mse]
|
[app.util.mouse :as mse]
|
||||||
|
@ -109,7 +110,7 @@
|
||||||
"Enter mouse resize mode, until mouse button is released."
|
"Enter mouse resize mode, until mouse button is released."
|
||||||
[handler ids shape]
|
[handler ids shape]
|
||||||
(letfn [(resize
|
(letfn [(resize
|
||||||
[shape initial layout [point lock? center? point-snap]]
|
[shape initial layout [point lock? center? point-snap]]
|
||||||
(let [{:keys [width height]} (:selrect shape)
|
(let [{:keys [width height]} (:selrect shape)
|
||||||
{:keys [rotation]} shape
|
{:keys [rotation]} shape
|
||||||
|
|
||||||
|
@ -333,10 +334,9 @@
|
||||||
angle))]
|
angle))]
|
||||||
(rx/concat
|
(rx/concat
|
||||||
(->> ms/mouse-position
|
(->> ms/mouse-position
|
||||||
(rx/with-latest vector ms/mouse-position-mod)
|
(rx/with-latest-from ms/mouse-position-mod ms/mouse-position-shift)
|
||||||
(rx/with-latest vector ms/mouse-position-shift)
|
|
||||||
(rx/map
|
(rx/map
|
||||||
(fn [[[pos mod?] shift?]]
|
(fn [[pos mod? shift?]]
|
||||||
(let [delta-angle (calculate-angle pos mod? shift?)]
|
(let [delta-angle (calculate-angle pos mod? shift?)]
|
||||||
(dwm/set-rotation-modifiers delta-angle shapes group-center))))
|
(dwm/set-rotation-modifiers delta-angle shapes group-center))))
|
||||||
(rx/take-until stoper))
|
(rx/take-until stoper))
|
||||||
|
@ -354,8 +354,8 @@
|
||||||
objects (wsh/lookup-page-objects state page-id)
|
objects (wsh/lookup-page-objects state page-id)
|
||||||
shapes (->> ids (map #(get objects %)))]
|
shapes (->> ids (map #(get objects %)))]
|
||||||
(rx/concat
|
(rx/concat
|
||||||
(rx/of (dwm/set-delta-rotation-modifiers rotation shapes))
|
(rx/of (dwm/set-delta-rotation-modifiers rotation shapes))
|
||||||
(rx/of (dwm/apply-modifiers)))))))
|
(rx/of (dwm/apply-modifiers)))))))
|
||||||
|
|
||||||
|
|
||||||
;; -- Move ----------------------------------------------------------
|
;; -- Move ----------------------------------------------------------
|
||||||
|
@ -395,7 +395,7 @@
|
||||||
(rx/map #(gpt/length %))
|
(rx/map #(gpt/length %))
|
||||||
(rx/filter #(> % (/ 10 zoom)))
|
(rx/filter #(> % (/ 10 zoom)))
|
||||||
(rx/take 1)
|
(rx/take 1)
|
||||||
(rx/with-latest vector ms/mouse-position-alt)
|
(rx/with-latest-from ms/mouse-position-alt)
|
||||||
(rx/mapcat
|
(rx/mapcat
|
||||||
(fn [[_ alt?]]
|
(fn [[_ alt?]]
|
||||||
(rx/concat
|
(rx/concat
|
||||||
|
@ -480,22 +480,23 @@
|
||||||
;; We send the nil first so the stream is not waiting for the first value
|
;; We send the nil first so the stream is not waiting for the first value
|
||||||
(rx/of nil)
|
(rx/of nil)
|
||||||
(->> position
|
(->> position
|
||||||
|
;; FIXME: performance throttle
|
||||||
(rx/throttle 20)
|
(rx/throttle 20)
|
||||||
(rx/switch-map
|
(rx/switch-map
|
||||||
(fn [pos]
|
(fn [pos]
|
||||||
(->> (snap/closest-snap-move page-id shapes objects layout zoom focus pos)
|
(->> (snap/closest-snap-move page-id shapes objects layout zoom focus pos)
|
||||||
(rx/map #(vector pos %)))))))]
|
(rx/map #(array pos %)))))))]
|
||||||
(if (empty? shapes)
|
(if (empty? shapes)
|
||||||
(rx/of (finish-transform))
|
(rx/of (finish-transform))
|
||||||
(let [move-stream
|
(let [move-stream
|
||||||
(->> position
|
(->> position
|
||||||
;; We ask for the snap position but we continue even if the result is not available
|
;; We ask for the snap position but we continue even if the result is not available
|
||||||
(rx/with-latest vector snap-delta)
|
(rx/with-latest-from snap-delta)
|
||||||
|
|
||||||
;; We try to use the previous snap so we don't have to wait for the result of the new
|
;; We try to use the previous snap so we don't have to wait for the result of the new
|
||||||
(rx/map snap/correct-snap-point)
|
(rx/map snap/correct-snap-point)
|
||||||
|
|
||||||
(rx/with-latest vector ms/mouse-position-mod)
|
(rx/with-latest-from ms/mouse-position-mod)
|
||||||
|
|
||||||
(rx/map
|
(rx/map
|
||||||
(fn [[move-vector mod?]]
|
(fn [[move-vector mod?]]
|
||||||
|
@ -506,16 +507,16 @@
|
||||||
grid-layout? (ctl/grid-layout? objects target-frame)
|
grid-layout? (ctl/grid-layout? objects target-frame)
|
||||||
drop-index (when flex-layout? (gslf/get-drop-index target-frame objects position))
|
drop-index (when flex-layout? (gslf/get-drop-index target-frame objects position))
|
||||||
cell-data (when (and grid-layout? (not mod?)) (gslg/get-drop-cell target-frame objects position))]
|
cell-data (when (and grid-layout? (not mod?)) (gslg/get-drop-cell target-frame objects position))]
|
||||||
[move-vector target-frame drop-index cell-data])))
|
(array move-vector target-frame drop-index cell-data))))
|
||||||
|
|
||||||
(rx/take-until stopper))]
|
(rx/take-until stopper))]
|
||||||
|
|
||||||
(rx/merge
|
(rx/merge
|
||||||
;; Temporary modifiers stream
|
;; Temporary modifiers stream
|
||||||
(->> move-stream
|
(->> move-stream
|
||||||
(rx/with-latest-from ms/mouse-position-shift)
|
(rx/with-latest-from array/conj ms/mouse-position-shift)
|
||||||
(rx/map
|
(rx/map
|
||||||
(fn [[[move-vector target-frame drop-index cell-data] shift?]]
|
(fn [[move-vector target-frame drop-index cell-data shift?]]
|
||||||
(let [x-disp? (> (mth/abs (:x move-vector)) (mth/abs (:y move-vector)))
|
(let [x-disp? (> (mth/abs (:x move-vector)) (mth/abs (:y move-vector)))
|
||||||
[move-vector snap-ignore-axis]
|
[move-vector snap-ignore-axis]
|
||||||
(cond
|
(cond
|
||||||
|
@ -538,15 +539,15 @@
|
||||||
(dwm/set-modifiers false false {:snap-ignore-axis snap-ignore-axis}))))))
|
(dwm/set-modifiers false false {:snap-ignore-axis snap-ignore-axis}))))))
|
||||||
|
|
||||||
(->> move-stream
|
(->> move-stream
|
||||||
(rx/with-latest-from ms/mouse-position-alt)
|
(rx/with-latest-from array/conj ms/mouse-position-alt)
|
||||||
(rx/filter (fn [[_ alt?]] alt?))
|
(rx/filter (fn [[_ alt?]] alt?))
|
||||||
(rx/take 1)
|
(rx/take 1)
|
||||||
(rx/mapcat
|
(rx/mapcat
|
||||||
(fn [[_ alt?]]
|
(fn [[_ alt?]]
|
||||||
(if (and (not duplicate-move-started?) alt?)
|
(if (and (not duplicate-move-started?) alt?)
|
||||||
(rx/of (start-move-duplicate from-position)
|
(rx/of (start-move-duplicate from-position)
|
||||||
(dws/duplicate-selected false true))
|
(dws/duplicate-selected false true))
|
||||||
(rx/empty)))))
|
(rx/empty)))))
|
||||||
|
|
||||||
(->> move-stream
|
(->> move-stream
|
||||||
(rx/map (comp set-ghost-displacement first)))
|
(rx/map (comp set-ghost-displacement first)))
|
||||||
|
@ -743,7 +744,7 @@
|
||||||
|
|
||||||
cpos (gpt/point (:x bbox) (:y bbox))
|
cpos (gpt/point (:x bbox) (:y bbox))
|
||||||
pos (gpt/point (or (:x position) (:x bbox))
|
pos (gpt/point (or (:x position) (:x bbox))
|
||||||
(or (:y position) (:y bbox)))
|
(or (:y position) (:y bbox)))
|
||||||
|
|
||||||
delta (gpt/subtract pos cpos)
|
delta (gpt/subtract pos cpos)
|
||||||
|
|
||||||
|
|
|
@ -6,10 +6,15 @@
|
||||||
|
|
||||||
(ns app.util.array
|
(ns app.util.array
|
||||||
"A collection of helpers for work with javascript arrays."
|
"A collection of helpers for work with javascript arrays."
|
||||||
(:refer-clojure :exclude [conj!]))
|
(:refer-clojure :exclude [conj! conj]))
|
||||||
|
|
||||||
|
(defn conj
|
||||||
|
"A conj like function for js arrays."
|
||||||
|
[a v]
|
||||||
|
(js* "[...~{}, ~{}]" a v))
|
||||||
|
|
||||||
(defn conj!
|
(defn conj!
|
||||||
"A conj like function for js arrays."
|
"A conj! like function for js arrays."
|
||||||
[a v]
|
[a v]
|
||||||
(.push ^js a v)
|
(.push ^js a v)
|
||||||
a)
|
a)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue