mirror of
https://github.com/penpot/penpot.git
synced 2026-07-31 18:36:18 +00:00
🐛 Fix draw a line/arrow with a single click (#10974)
This commit is contained in:
parent
3fc1e6aadd
commit
e952d9c70a
@ -7,8 +7,11 @@
|
||||
(ns app.main.data.workspace.drawing.common
|
||||
(:require
|
||||
[app.common.files.helpers :as cfh]
|
||||
[app.common.geom.point :as gpt]
|
||||
[app.common.geom.rect :as grc]
|
||||
[app.common.geom.shapes :as gsh]
|
||||
[app.common.types.modifiers :as ctm]
|
||||
[app.common.types.path :as path]
|
||||
[app.common.types.shape :as cts]
|
||||
[app.main.data.helpers :as dsh]
|
||||
[app.main.data.workspace.shapes :as dwsh]
|
||||
@ -17,6 +20,31 @@
|
||||
[beicon.v2.core :as rx]
|
||||
[potok.v2.core :as ptk]))
|
||||
|
||||
(defn- click-draw-box
|
||||
[shape width height]
|
||||
(-> shape
|
||||
(assoc :width width)
|
||||
(assoc :height height)
|
||||
(assoc :selrect nil)
|
||||
(assoc :points nil)
|
||||
(cts/setup-shape)
|
||||
(gsh/transform-shape (ctm/move-modifiers (- (/ width 2)) (- (/ height 2))))))
|
||||
|
||||
(defn- click-draw-path
|
||||
[shape]
|
||||
(let [start-point (path/get-handler-point (:content shape) 0 nil)
|
||||
center-x (:x start-point)
|
||||
center-y (:y start-point)
|
||||
start (gpt/point (- center-x 50) center-y)
|
||||
end (gpt/point (+ center-x 50) center-y)
|
||||
content (path/points->content [start end])
|
||||
selrect (path/calc-selrect content)]
|
||||
(-> shape
|
||||
(assoc :content content)
|
||||
(assoc :selrect selrect)
|
||||
(assoc :points (grc/rect->points selrect))
|
||||
(assoc :grow-type :fixed))))
|
||||
|
||||
(defn clear-drawing
|
||||
([] (clear-drawing nil))
|
||||
([{:keys [preserve-tool?]}]
|
||||
@ -60,15 +88,11 @@
|
||||
(not click-draw?)
|
||||
(assoc :grow-type :fixed)
|
||||
|
||||
(and ^boolean click-draw? (not ^boolean text?))
|
||||
(-> (assoc :width width)
|
||||
(assoc :height height)
|
||||
;; NOTE: we need to recalculate the selrect and
|
||||
;; points, so we assign `nil` to it
|
||||
(assoc :selrect nil)
|
||||
(assoc :points nil)
|
||||
(cts/setup-shape)
|
||||
(gsh/transform-shape (ctm/move-modifiers (- (/ width 2)) (- (/ height 2)))))
|
||||
(and ^boolean click-draw? (not ^boolean text?) (not= :path (:type shape)))
|
||||
(click-draw-box width height)
|
||||
|
||||
(and ^boolean click-draw? (= :path (:type shape)))
|
||||
(click-draw-path)
|
||||
|
||||
(and click-draw? text?)
|
||||
(-> (assoc :height 17 :width 4 :grow-type :auto-width)
|
||||
|
||||
@ -63,7 +63,8 @@
|
||||
(-> object
|
||||
(assoc :content content)
|
||||
(assoc :selrect selrect)
|
||||
(assoc :points points')))))))
|
||||
(assoc :points points')
|
||||
(assoc :click-draw? false)))))))
|
||||
|
||||
(defn- setup-frame
|
||||
[]
|
||||
@ -101,9 +102,10 @@
|
||||
(update-in state [:workspace-drawing :object]
|
||||
(fn [{:keys [content selrect] :as shape}]
|
||||
(cond-> shape
|
||||
(or (empty? content)
|
||||
(nil? selrect)
|
||||
(<= (count content) 1))
|
||||
(and (not (:click-draw? shape))
|
||||
(or (empty? content)
|
||||
(nil? selrect)
|
||||
(<= (count content) 1)))
|
||||
(assoc :initialized? false)))))))
|
||||
|
||||
(defn- arrow-strokes
|
||||
@ -125,11 +127,13 @@
|
||||
(let [arrow? (= tool :arrow)]
|
||||
(ptk/reify ::handle-drawing
|
||||
ptk/WatchEvent
|
||||
(watch [_ _ stream]
|
||||
(watch [_ state stream]
|
||||
(let [stopper (mse/drag-stopper stream)
|
||||
zoom (dm/get-in state [:workspace-local :zoom] 1)
|
||||
start @ms/mouse-position
|
||||
shape (cts/setup-shape {:type :path
|
||||
:initialized? true
|
||||
:click-draw? true
|
||||
:frame-id uuid/zero
|
||||
:parent-id uuid/zero
|
||||
:strokes (arrow-strokes arrow?)
|
||||
@ -139,6 +143,7 @@
|
||||
(rx/of #(update % :workspace-drawing assoc :object shape))
|
||||
|
||||
(->> ms/mouse-position
|
||||
(rx/filter #(> (* (gpt/distance % start) zoom) 10))
|
||||
(rx/with-latest-from ms/mouse-position-shift)
|
||||
(rx/map (fn [[point shift?]]
|
||||
(if shift?
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user