mirror of
https://github.com/penpot/penpot.git
synced 2025-07-25 15:27:17 +02:00
⚡ Add minor performance optimization to snap-distances components
This commit is contained in:
parent
4b09172b69
commit
2bd31dcbd2
1 changed files with 51 additions and 35 deletions
|
@ -132,7 +132,8 @@
|
||||||
(and (>= s1c1 s2c1) (<= s1c1 s2c2))
|
(and (>= s1c1 s2c1) (<= s1c1 s2c2))
|
||||||
(and (>= s1c2 s2c1) (<= s1c2 s2c2)))))
|
(and (>= s1c2 s2c1) (<= s1c2 s2c2)))))
|
||||||
|
|
||||||
(defn calculate-segments [coord selrect lt-shapes gt-shapes]
|
(defn calculate-segments
|
||||||
|
[coord selrect lt-shapes gt-shapes]
|
||||||
(let [distance-to-selrect
|
(let [distance-to-selrect
|
||||||
(fn [shape]
|
(fn [shape]
|
||||||
(let [sr (:selrect shape)]
|
(let [sr (:selrect shape)]
|
||||||
|
@ -202,6 +203,26 @@
|
||||||
|
|
||||||
segments-to-display))
|
segments-to-display))
|
||||||
|
|
||||||
|
(defn- query-worker
|
||||||
|
[page-id coord [selrect selected frame]]
|
||||||
|
(let [lt-side (if (= coord :x) :left :top)
|
||||||
|
gt-side (if (= coord :x) :right :bottom)
|
||||||
|
|
||||||
|
vbox (deref refs/vbox)
|
||||||
|
areas (gsh/get-areas
|
||||||
|
(or (grc/clip-rect (dm/get-prop frame :selrect) vbox) vbox)
|
||||||
|
selrect)
|
||||||
|
|
||||||
|
query-side
|
||||||
|
(fn [side]
|
||||||
|
(let [rect (get areas side)]
|
||||||
|
(if (and (> (:width rect) 0) (> (:height rect) 0))
|
||||||
|
(ams/select-shapes-area page-id (:id frame) selected @refs/workspace-page-objects rect)
|
||||||
|
(rx/of nil))))]
|
||||||
|
|
||||||
|
(rx/combine-latest (query-side lt-side)
|
||||||
|
(query-side gt-side))))
|
||||||
|
|
||||||
(mf/defc shape-distance
|
(mf/defc shape-distance
|
||||||
{::mf/wrap-props false}
|
{::mf/wrap-props false}
|
||||||
[props]
|
[props]
|
||||||
|
@ -213,51 +234,46 @@
|
||||||
selected (unchecked-get props "selected")
|
selected (unchecked-get props "selected")
|
||||||
|
|
||||||
subject (mf/use-memo #(rx/subject))
|
subject (mf/use-memo #(rx/subject))
|
||||||
to-measure (mf/use-state [])
|
|
||||||
|
|
||||||
query-worker
|
|
||||||
(fn [[selrect selected frame]]
|
|
||||||
(let [lt-side (if (= coord :x) :left :top)
|
|
||||||
gt-side (if (= coord :x) :right :bottom)
|
|
||||||
|
|
||||||
vbox (deref refs/vbox)
|
lt-shapes* (mf/use-state nil)
|
||||||
areas (gsh/get-areas
|
lt-shapes (deref lt-shapes*)
|
||||||
(or (grc/clip-rect (dm/get-prop frame :selrect) vbox) vbox)
|
|
||||||
selrect)
|
|
||||||
|
|
||||||
query-side (fn [side]
|
gt-shapes* (mf/use-state nil)
|
||||||
(let [rect (get areas side)]
|
gt-shapes (deref gt-shapes*)
|
||||||
(if (and (> (:width rect) 0) (> (:height rect) 0))
|
|
||||||
(ams/select-shapes-area page-id (:id frame) selected @refs/workspace-page-objects rect)
|
|
||||||
(rx/of nil))))]
|
|
||||||
(rx/combine-latest (query-side lt-side)
|
|
||||||
(query-side gt-side))))
|
|
||||||
|
|
||||||
[lt-shapes gt-shapes] @to-measure
|
segments-to-display
|
||||||
|
(mf/with-memo [lt-shapes gt-shapes selrect]
|
||||||
|
(calculate-segments coord selrect lt-shapes gt-shapes))]
|
||||||
|
|
||||||
segments-to-display (mf/use-memo
|
(mf/with-effect [page-id]
|
||||||
(mf/deps @to-measure)
|
(let [sub (->> subject
|
||||||
#(calculate-segments coord selrect lt-shapes gt-shapes))]
|
(rx/throttle 100)
|
||||||
|
;; NOTE: we don't put coord on deps because we
|
||||||
(mf/use-effect
|
;; know it is a static value and will not go to
|
||||||
(fn []
|
;; change
|
||||||
(let [sub (->> subject
|
(rx/switch-map (partial query-worker page-id coord))
|
||||||
(rx/throttle 100)
|
(rx/subs (fn [[lt-shapes gt-shapes]]
|
||||||
(rx/switch-map query-worker)
|
(reset! lt-shapes* lt-shapes)
|
||||||
(rx/subs #(reset! to-measure %)))]
|
(reset! gt-shapes* gt-shapes))))]
|
||||||
;; On unmount dispose
|
;; On unmount dispose
|
||||||
#(rx/dispose! sub))))
|
#(rx/dispose! sub)))
|
||||||
|
|
||||||
(mf/use-effect
|
(mf/use-effect
|
||||||
(mf/deps selrect)
|
(mf/deps selrect)
|
||||||
#(rx/push! subject [selrect selected frame]))
|
#(rx/push! subject [selrect selected frame]))
|
||||||
|
|
||||||
(for [[sr1 sr2] segments-to-display]
|
(for [[sr1 sr2] segments-to-display]
|
||||||
[:& shape-distance-segment {:key (str/join "-" [(:x sr1) (:y sr1) (:x sr2) (:y sr2)])
|
[:& shape-distance-segment
|
||||||
:sr1 sr1
|
{:key (str/ffmt "%-%-%-%"
|
||||||
:sr2 sr2
|
(dm/get-prop sr1 :x)
|
||||||
:coord coord
|
(dm/get-prop sr1 :y)
|
||||||
:zoom zoom}])))
|
(dm/get-prop sr2 :x)
|
||||||
|
(dm/get-prop sr2 :y))
|
||||||
|
:sr1 sr1
|
||||||
|
:sr2 sr2
|
||||||
|
:coord coord
|
||||||
|
:zoom zoom}])))
|
||||||
|
|
||||||
(mf/defc snap-distances
|
(mf/defc snap-distances
|
||||||
{::mf/wrap-props false}
|
{::mf/wrap-props false}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue