Skip to content

Commit b78b8e6

Browse files
committed
add snapping points of selection bounds and refactor
1 parent 685223c commit b78b8e6

File tree

6 files changed

+47
-176
lines changed

6 files changed

+47
-176
lines changed

.clj-kondo/metosin/malli-types-cljs/config.edn

+3-149
Large diffs are not rendered by default.

src/renderer/snap/handlers.cljs

+11-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
[renderer.element.handlers :as element.h]
1010
[renderer.snap.db :refer [SnapOption NearestNeighbor]]
1111
[renderer.snap.subs :as-alias snap.s]
12-
[renderer.utils.element :as utils.el]
12+
[renderer.utils.bounds :as bounds]
13+
[renderer.utils.element :as element]
1314
[renderer.utils.math :refer [Vec2D]]))
1415

1516
(m/=> toggle-option [:-> App SnapOption App])
@@ -23,13 +24,17 @@
2324
(defn base-points
2425
[db]
2526
(let [elements (vals (element.h/entities db))
26-
selected-visible (filter #(and (:visible %) (:selected %)) elements)]
27+
selected (filter :selected elements)
28+
selected-visible (filter :visible selected)
29+
options (-> db :snap :options)]
2730
(when (-> db :snap :active)
2831
(cond
29-
(and (contains? #{:translate :clone} (:state db)) (seq selected-visible))
30-
(reduce (fn [points element]
31-
(apply conj points (utils.el/snapping-points element (-> db :snap :options))))
32-
[] selected-visible)
32+
(and (contains? #{:translate :clone} (:state db)) (seq selected))
33+
(reduce (fn [points el] (into points (element/snapping-points el options)))
34+
(if (seq (rest selected))
35+
(bounds/->snapping-points (element.h/bounds db) options)
36+
[])
37+
selected-visible)
3338

3439
(contains? #{:edit :scale} (:state db))
3540
[(mat/add [(-> db :clicked-element :x) (-> db :clicked-element :y)]

src/renderer/snap/subs.cljs

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@
3232
:<- [::options]
3333
(fn [[non-selected-visible-elements active options] _]
3434
(when active
35-
(reduce (fn [points element]
36-
(apply conj points (utils.el/snapping-points element options)))
35+
(reduce (fn [points el] (into points (utils.el/snapping-points el options)))
3736
[] non-selected-visible-elements))))
3837

3938
(rf/reg-sub

src/renderer/utils/bounds.cljs

+22
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
(:require
33
[clojure.core.matrix :as mat]
44
[malli.core :as m]
5+
[renderer.snap.db :refer [SnapOptions]]
56
[renderer.utils.dom :refer [DomElement]]
67
[renderer.utils.math :refer [Vec2D]]))
78

@@ -73,3 +74,24 @@
7374
(<= top y)
7475
(>= right x)
7576
(>= bottom y)))
77+
78+
(m/=> ->snapping-points [:-> Bounds SnapOptions [:* Vec2D]])
79+
(defn ->snapping-points
80+
[bounds options]
81+
(let [[x1 y1 x2 y2] bounds
82+
[cx cy] (center bounds)]
83+
(cond-> []
84+
(:corners options)
85+
(into [[x1 y1]
86+
[x1 y2]
87+
[x2 y1]
88+
[x2 y2]])
89+
90+
(:centers options)
91+
(into [[cx cy]])
92+
93+
(:midpoints options)
94+
(into [[x1 cy]
95+
[x2 cy]
96+
[cx y1]
97+
[cx y2]]))))

src/renderer/utils/element.cljs

+3-19
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,9 @@
5757
(defn snapping-points
5858
[el options]
5959
(let [points (or (element.hierarchy/snapping-points el) [])]
60-
(if-let [bounds (:bounds el)]
61-
(let [[x1 y1 x2 y2] bounds
62-
[cx cy] (utils.bounds/center bounds)]
63-
(cond-> points
64-
(:corners options)
65-
(into [[x1 y1]
66-
[x1 y2]
67-
[x2 y1]
68-
[x2 y2]])
69-
70-
(:centers options)
71-
(into [[cx cy]])
72-
73-
(:midpoints options)
74-
(into [[x1 cy]
75-
[x2 cy]
76-
[cx y1]
77-
[cx y2]])))
78-
points)))
60+
(cond-> points
61+
(:bounds el)
62+
(into (utils.bounds/->snapping-points (:bounds el) options)))))
7963

8064
(m/=> attributes [:-> Element Attrs])
8165
(defn attributes

test/utils/bounds_test.cljs

+7
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,10 @@
3636
(deftest test-center
3737
(testing "center of bounds"
3838
(is (= (bounds/center [0 0 10 10]) [5 5]))))
39+
40+
(deftest test-->snapping-points
41+
(testing "snapping points of bounds"
42+
(is (= (bounds/->snapping-points [0 0 10 10] #{:corners :centers :midpoints})
43+
[[0 0] [0 10] [10 0] [10 10] [5 5] [0 5] [10 5] [5 0] [5 10]]))
44+
45+
(is (= (bounds/->snapping-points [0 0 10 10] #{}) []))))

0 commit comments

Comments
 (0)