mirror of
https://github.com/penpot/penpot.git
synced 2026-09-15 00:19:01 +00:00
Enable path drawing termination with enter and right-click.
It also posible do it with double click while shift key is pressed.
This commit is contained in:
parent
92a0ffc1ae
commit
8910400444
@ -80,7 +80,6 @@
|
|||||||
(defonce scroll-a
|
(defonce scroll-a
|
||||||
(rx/to-atom scroll-s))
|
(rx/to-atom scroll-s))
|
||||||
|
|
||||||
|
|
||||||
;; --- Events
|
;; --- Events
|
||||||
|
|
||||||
(defonce events-b (rx/bus))
|
(defonce events-b (rx/bus))
|
||||||
|
|||||||
@ -86,7 +86,7 @@
|
|||||||
(let [opts {:key (.-keyCode event)
|
(let [opts {:key (.-keyCode event)
|
||||||
:shift? (kbd/shift? event)
|
:shift? (kbd/shift? event)
|
||||||
:ctrl? (kbd/ctrl? event)}]
|
:ctrl? (kbd/ctrl? event)}]
|
||||||
(rx/push! wb/events-b [:keyboard/down opts])
|
(rx/push! wb/events-b [:key/down opts])
|
||||||
(when (kbd/space? event)
|
(when (kbd/space? event)
|
||||||
(rlocks/acquire! :workspace/scroll))))
|
(rlocks/acquire! :workspace/scroll))))
|
||||||
|
|
||||||
@ -94,7 +94,7 @@
|
|||||||
(let [opts {:key (.-keyCode event)
|
(let [opts {:key (.-keyCode event)
|
||||||
:shift? (kbd/shift? event)
|
:shift? (kbd/shift? event)
|
||||||
:ctrl? (kbd/ctrl? event)}]
|
:ctrl? (kbd/ctrl? event)}]
|
||||||
(rx/push! wb/events-b [:keyboard/up opts])))
|
(rx/push! wb/events-b [:key/up opts])))
|
||||||
|
|
||||||
(on-mousemove [event]
|
(on-mousemove [event]
|
||||||
(let [wpt (gpt/point (.-clientX event)
|
(let [wpt (gpt/point (.-clientX event)
|
||||||
@ -106,6 +106,9 @@
|
|||||||
:window-coords wpt
|
:window-coords wpt
|
||||||
:viewport-coords vppt
|
:viewport-coords vppt
|
||||||
:canvas-coords cvpt}]
|
:canvas-coords cvpt}]
|
||||||
|
;; FIXME: refactor streams in order to use the wb/events-b
|
||||||
|
;; for all keyboard and mouse events and then derive
|
||||||
|
;; all the other from it.
|
||||||
(rx/push! wb/mouse-b event)))]
|
(rx/push! wb/mouse-b event)))]
|
||||||
|
|
||||||
(let [key1 (events/listen js/document EventType.MOUSEMOVE on-mousemove)
|
(let [key1 (events/listen js/document EventType.MOUSEMOVE on-mousemove)
|
||||||
@ -139,19 +142,32 @@
|
|||||||
(if (:drawing workspace)
|
(if (:drawing workspace)
|
||||||
(rlocks/acquire! :ui/draw)
|
(rlocks/acquire! :ui/draw)
|
||||||
(rlocks/acquire! :ui/selrect)))
|
(rlocks/acquire! :ui/selrect)))
|
||||||
|
(on-context-menu [event]
|
||||||
|
(dom/prevent-default event)
|
||||||
|
(dom/stop-propagation event)
|
||||||
|
(let [opts {:shift? (kbd/shift? event)
|
||||||
|
:ctrl? (kbd/ctrl? event)}]
|
||||||
|
(rx/push! wb/events-b [:mouse/right-click opts])))
|
||||||
(on-mouse-up [event]
|
(on-mouse-up [event]
|
||||||
(dom/stop-propagation event)
|
(dom/stop-propagation event)
|
||||||
(rx/push! wb/events-b [:mouse/up]))
|
(let [opts {:shift? (kbd/shift? event)
|
||||||
|
:ctrl? (kbd/ctrl? event)}]
|
||||||
|
(rx/push! wb/events-b [:mouse/up])))
|
||||||
(on-click [event]
|
(on-click [event]
|
||||||
(dom/stop-propagation event)
|
(dom/stop-propagation event)
|
||||||
(rx/push! wb/events-b [:mouse/click]))
|
(let [opts {:shift? (kbd/shift? event)
|
||||||
|
:ctrl? (kbd/ctrl? event)}]
|
||||||
|
(rx/push! wb/events-b [:mouse/click opts])))
|
||||||
(on-double-click [event]
|
(on-double-click [event]
|
||||||
(dom/stop-propagation event)
|
(dom/stop-propagation event)
|
||||||
(rx/push! wb/events-b [:mouse/double-click]))]
|
(let [opts {:shift? (kbd/shift? event)
|
||||||
|
:ctrl? (kbd/ctrl? event)}]
|
||||||
|
(rx/push! wb/events-b [:mouse/double-click opts])))]
|
||||||
[:svg.viewport {:width (* c/viewport-width zoom)
|
[:svg.viewport {:width (* c/viewport-width zoom)
|
||||||
:height (* c/viewport-height zoom)
|
:height (* c/viewport-height zoom)
|
||||||
:ref "viewport"
|
:ref "viewport"
|
||||||
:class (when drawing? "drawing")
|
:class (when drawing? "drawing")
|
||||||
|
:on-context-menu on-context-menu
|
||||||
:on-click on-click
|
:on-click on-click
|
||||||
:on-double-click on-double-click
|
:on-double-click on-double-click
|
||||||
:on-mouse-down on-mouse-down
|
:on-mouse-down on-mouse-down
|
||||||
|
|||||||
@ -115,97 +115,107 @@
|
|||||||
|
|
||||||
(defn- on-init-draw-path
|
(defn- on-init-draw-path
|
||||||
[shape]
|
[shape]
|
||||||
(let [mouse (->> (rx/sample 10 wb/mouse-viewport-s)
|
(letfn [(conditional-align-point [point]
|
||||||
(rx/mapcat (fn [point]
|
(if @wb/alignment-ref
|
||||||
(if @wb/alignment-ref
|
(uds/align-point point)
|
||||||
(uds/align-point point)
|
(rx/of point)))
|
||||||
(rx/of point))))
|
|
||||||
(rx/map #(gpt/subtract % canvas-coords)))
|
|
||||||
stoper (->> wb/events-s
|
|
||||||
(rx/map first)
|
|
||||||
(rx/filter #(= % :mouse/double-click))
|
|
||||||
(rx/take 1))
|
|
||||||
firstpos (rx/take 1 mouse)
|
|
||||||
stream (->> (rx/take-until stoper mouse)
|
|
||||||
(rx/skip-while #(nil? @drawing-shape))
|
|
||||||
(rx/with-latest-from vector wb/mouse-ctrl-s))
|
|
||||||
ptstream (->> (rx/take-until stoper wb/events-s)
|
|
||||||
(rx/map first)
|
|
||||||
(rx/filter #(= % :mouse/click))
|
|
||||||
(rx/with-latest-from vector mouse)
|
|
||||||
(rx/map second))
|
|
||||||
counter (atom 0)]
|
|
||||||
(letfn [(append-point [{:keys [type] :as shape} point]
|
|
||||||
(let [point (gpt/point point)]
|
|
||||||
(update shape :points conj point)))
|
|
||||||
|
|
||||||
(update-point [{:keys [type] :as shape} point index]
|
(stoper-event? [[type opts]]
|
||||||
(let [point (gpt/point point)
|
(or (and (= type :key/down)
|
||||||
points (:points shape)]
|
(= (:key opts) 13))
|
||||||
(if (= (count points) index)
|
(and (= type :mouse/double-click)
|
||||||
(append-point shape point)
|
(true? (:shift? opts)))
|
||||||
(assoc-in shape [:points index] point))))
|
(= type :mouse/right-click)))
|
||||||
|
|
||||||
(normalize-shape [{:keys [points] :as shape}]
|
(new-point-event? [[type opts]]
|
||||||
(let [minx (apply min (map :x points))
|
(and (= type :mouse/click)
|
||||||
miny (apply min (map :y points))
|
(false? (:shift? opts))))]
|
||||||
maxx (apply max (map :x points))
|
|
||||||
maxy (apply max (map :y points))
|
|
||||||
|
|
||||||
;; dx (- 0 minx)
|
(let [mouse (->> (rx/sample 10 wb/mouse-viewport-s)
|
||||||
;; dy (- 0 miny)
|
(rx/mapcat conditional-align-point)
|
||||||
;; points (mapv #(gpt/add % [dx dy]) points)
|
(rx/map #(gpt/subtract % canvas-coords)))
|
||||||
width (- maxx minx)
|
|
||||||
height (- maxy miny)]
|
|
||||||
|
|
||||||
(assoc shape
|
stoper (rx/take 1 (rx/filter stoper-event? wb/events-s))
|
||||||
;; :x1 minx
|
firstpos (rx/take 1 mouse)
|
||||||
;; :y1 miny
|
stream (->> (rx/take-until stoper mouse)
|
||||||
;; :x2 maxx
|
(rx/skip-while #(nil? @drawing-shape))
|
||||||
;; :y2 maxy
|
(rx/with-latest-from vector wb/mouse-ctrl-s))
|
||||||
;; :view-box [0 0 width height]
|
ptstream (->> (rx/take-until stoper wb/events-s)
|
||||||
:points points)))
|
(rx/filter new-point-event?)
|
||||||
|
(rx/with-latest-from vector mouse)
|
||||||
|
(rx/map second))
|
||||||
|
counter (atom 0)]
|
||||||
|
(letfn [(append-point [{:keys [type] :as shape} point]
|
||||||
|
(let [point (gpt/point point)]
|
||||||
|
(update shape :points conj point)))
|
||||||
|
|
||||||
(on-first-point [point]
|
(update-point [{:keys [type] :as shape} point index]
|
||||||
(let [shape (append-point shape point)]
|
(let [point (gpt/point point)
|
||||||
(swap! counter inc)
|
points (:points shape)]
|
||||||
(reset! drawing-shape shape)))
|
(if (= (count points) index)
|
||||||
|
(append-point shape point)
|
||||||
|
(assoc-in shape [:points index] point))))
|
||||||
|
|
||||||
(on-click [point]
|
(normalize-shape [{:keys [points] :as shape}]
|
||||||
(let [shape (append-point @drawing-shape point)]
|
(let [minx (apply min (map :x points))
|
||||||
(swap! counter inc)
|
miny (apply min (map :y points))
|
||||||
(reset! drawing-shape shape)))
|
maxx (apply max (map :x points))
|
||||||
|
maxy (apply max (map :y points))
|
||||||
|
|
||||||
(on-assisted-draw [point]
|
;; dx (- 0 minx)
|
||||||
(let [center (get-in @drawing-shape [:points (dec @counter)])
|
;; dy (- 0 miny)
|
||||||
point (as-> point $
|
;; points (mapv #(gpt/add % [dx dy]) points)
|
||||||
(gpt/subtract $ center)
|
width (- maxx minx)
|
||||||
(align-position (gpt/angle $) $)
|
height (- maxy miny)]
|
||||||
(gpt/add $ center))]
|
|
||||||
|
(assoc shape
|
||||||
|
;; :x1 minx
|
||||||
|
;; :y1 miny
|
||||||
|
;; :x2 maxx
|
||||||
|
;; :y2 maxy
|
||||||
|
;; :view-box [0 0 width height]
|
||||||
|
:points points)))
|
||||||
|
|
||||||
|
(on-first-point [point]
|
||||||
|
(let [shape (append-point shape point)]
|
||||||
|
(swap! counter inc)
|
||||||
|
(reset! drawing-shape shape)))
|
||||||
|
|
||||||
|
(on-click [point]
|
||||||
|
(let [shape (append-point @drawing-shape point)]
|
||||||
|
(swap! counter inc)
|
||||||
|
(reset! drawing-shape shape)))
|
||||||
|
|
||||||
|
(on-assisted-draw [point]
|
||||||
|
(let [center (get-in @drawing-shape [:points (dec @counter)])
|
||||||
|
point (as-> point $
|
||||||
|
(gpt/subtract $ center)
|
||||||
|
(align-position (gpt/angle $) $)
|
||||||
|
(gpt/add $ center))]
|
||||||
|
(->> (update-point @drawing-shape point @counter)
|
||||||
|
(reset! drawing-shape))))
|
||||||
|
|
||||||
|
(on-free-draw [point]
|
||||||
(->> (update-point @drawing-shape point @counter)
|
(->> (update-point @drawing-shape point @counter)
|
||||||
(reset! drawing-shape))))
|
(reset! drawing-shape)))
|
||||||
|
|
||||||
(on-free-draw [point]
|
(on-draw [[point ctrl?]]
|
||||||
(->> (update-point @drawing-shape point @counter)
|
(if ctrl?
|
||||||
(reset! drawing-shape)))
|
(on-assisted-draw point)
|
||||||
|
(on-free-draw point)))
|
||||||
|
|
||||||
(on-draw [[point ctrl?]]
|
(on-end []
|
||||||
(if ctrl?
|
(let [shape (normalize-shape @drawing-shape)]
|
||||||
(on-assisted-draw point)
|
(rs/emit! (uds/add-shape shape)
|
||||||
(on-free-draw point)))
|
(udw/select-for-drawing nil)
|
||||||
|
(uds/select-first-shape))
|
||||||
|
(reset! drawing-shape nil)
|
||||||
|
(reset! drawing-position nil)
|
||||||
|
(rlocks/release! :ui/draw)))]
|
||||||
|
|
||||||
(on-end []
|
(rx/subscribe firstpos on-first-point)
|
||||||
(let [shape (normalize-shape @drawing-shape)]
|
(rx/subscribe ptstream on-click)
|
||||||
(rs/emit! (uds/add-shape shape)
|
(rx/subscribe stream on-draw nil on-end)))))
|
||||||
(udw/select-for-drawing nil)
|
|
||||||
(uds/select-first-shape))
|
|
||||||
(reset! drawing-shape nil)
|
|
||||||
(reset! drawing-position nil)
|
|
||||||
(rlocks/release! :ui/draw)))]
|
|
||||||
|
|
||||||
(rx/subscribe firstpos on-first-point)
|
|
||||||
(rx/subscribe ptstream on-click)
|
|
||||||
(rx/subscribe stream on-draw nil on-end))))
|
|
||||||
|
|
||||||
(defn- on-init-draw-free-path
|
(defn- on-init-draw-free-path
|
||||||
[shape]
|
[shape]
|
||||||
|
|||||||
@ -16,7 +16,7 @@
|
|||||||
(defn watch-scroll-interactions
|
(defn watch-scroll-interactions
|
||||||
[own]
|
[own]
|
||||||
(letfn [(is-space-up? [[type {:keys [key]}]]
|
(letfn [(is-space-up? [[type {:keys [key]}]]
|
||||||
(and (= 32 key) (= :keyboard/up type)))
|
(and (= 32 key) (= :key/up type)))
|
||||||
|
|
||||||
(on-start []
|
(on-start []
|
||||||
(let [stoper (->> wb/events-s
|
(let [stoper (->> wb/events-s
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user