Skip to content

Commit 4bbfb41

Browse files
committed
enhance performance
1 parent 5320699 commit 4bbfb41

File tree

9 files changed

+68
-53
lines changed

9 files changed

+68
-53
lines changed

src/renderer/attribute/hierarchy.cljs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,9 @@
55
(defmulti form-element (fn [tag k _v _attrs] [tag k]))
66

77
(defmethod update-attr :default
8-
[el k f & more]
9-
(apply update-in el [:attrs k] f more))
8+
([el k f]
9+
(update-in el [:attrs k] f))
10+
([el k f arg]
11+
(update-in el [:attrs k] f arg))
12+
([el k f arg & more]
13+
(apply update-in el [:attrs k] f arg more)))

src/renderer/attribute/impl/length.cljs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,17 @@
4848
[views/icon "plus"]]]])
4949

5050
(defmethod attribute.hierarchy/update-attr ::length
51-
[el k f & more]
52-
(update-in el [:attrs k] #(apply utils.length/transform % f more)))
51+
([el k f]
52+
(update-in el [:attrs k] #(utils.length/transform % f)))
53+
([el k f arg]
54+
(update-in el [:attrs k] #(utils.length/transform % f arg)))
55+
([el k f arg & more]
56+
(update-in el [:attrs k] #(apply utils.length/transform % f arg more))))
5357

5458
(defmethod attribute.hierarchy/update-attr ::positive-length
55-
[el k f & more]
56-
(update-in el [:attrs k] utils.length/transform (fn [v] (max 0 (apply f v more)))))
59+
([el k f]
60+
(update-in el [:attrs k] utils.length/transform (fn [v] (max 0 (f v)))))
61+
([el k f arg]
62+
(update-in el [:attrs k] utils.length/transform (fn [v] (max 0 (f v arg)))))
63+
([el k f arg & more]
64+
(update-in el [:attrs k] utils.length/transform (fn [v] (max 0 (apply f v arg more))))))

src/renderer/attribute/views.cljs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
(utils.attribute/compatibility tag attr)
6161
(utils.attribute/compatibility tag))
6262
support-data (:support data)
63-
property (when attr (utils.attribute/property-data attr))
63+
property (when attr (utils.attribute/property-data-memo attr))
6464
spec-url (or (:spec_url data) (:href property))
6565
spec-url (if (vector? spec-url) (first spec-url) spec-url)
6666
mdn-url (or (when data (or (:mdn_url data) (construct-mdn-url (name attr))))
@@ -175,7 +175,7 @@
175175
(defn label
176176
[tag k]
177177
(let [clicked-element @(rf/subscribe [::app.subs/clicked-element])
178-
property (utils.attribute/property-data k)
178+
property (utils.attribute/property-data-memo k)
179179
dispatch-tag (if (contains? (methods attribute.hierarchy/description) [tag k])
180180
tag
181181
:default)
@@ -204,7 +204,7 @@
204204

205205
(defn row
206206
[k v locked? tag]
207-
(let [property (utils.attribute/property-data k)
207+
(let [property (utils.attribute/property-data-memo k)
208208
initial (:initial property)
209209
dispatch-tag (if (contains? (methods attribute.hierarchy/form-element) [tag k])
210210
tag

src/renderer/utils/attribute.cljs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,14 @@
247247
css-property
248248
[:appliesto :computed :percentages :animationType])))
249249

250+
(def property-data-memo (memoize property-data))
251+
252+
(defonce whitespace-regex #"\s*[\s,]\s*")
253+
250254
(m/=> str->seq [:-> string? vector?])
251255
(defn str->seq
252256
[s]
253-
(-> s string/trim (string/split #"\s*[\s,]\s*")))
257+
(-> s string/trim (string/split whitespace-regex)))
254258

255259
(m/=> points->vec [:function
256260
[:-> string? vector?]
@@ -283,16 +287,18 @@
283287
(keys)
284288
(zipmap (repeat "")))))
285289

290+
(def ->attrs-memo (memoize ->attrs))
291+
286292
(m/=> defaults [:-> Tag Attrs])
287293
(defn defaults
288294
[tag]
289295
(merge (when (element.db/tag? tag)
290-
(merge (->attrs (or (tag (:elements svg-data)) {}))
296+
(merge (->attrs-memo (or (tag (:elements svg-data)) {}))
291297
(when (or (isa? tag ::element.hierarchy/shape)
292298
(isa? tag ::element.hierarchy/container))
293299
(zipmap core (repeat "")))))
294300
(when (contains? #{:animateMotion :animateTransform} tag)
295-
(->attrs (:animate (:elements svg-data))))
301+
(->attrs-memo (:animate (:elements svg-data))))
296302
(zipmap (:attrs (element.hierarchy/properties tag)) (repeat ""))))
297303

298304
(def defaults-memo (memoize defaults))

src/renderer/utils/bounds.cljs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
(ns renderer.utils.bounds
22
(:require
3-
[clojure.core.matrix :as matrix]
43
[malli.core :as m]
54
[renderer.snap.db :refer [SnapOptions]]
65
[renderer.utils.dom :refer [DomElement]]
@@ -31,27 +30,30 @@
3130
(defn union
3231
"Returns the bounding box that contains the provided collection of bounds."
3332
[& bbox]
34-
(vec (concat (apply map min (map #(take 2 %) bbox))
35-
(apply map max (map #(drop 2 %) bbox)))))
33+
(reduce (fn [[a-min-x a-min-y a-max-x a-max-y] [b-min-x b-min-y b-max-x b-max-y]]
34+
[(min a-min-x b-min-x)
35+
(min a-min-y b-min-y)
36+
(max a-max-x b-max-x)
37+
(max a-max-y b-max-y)])
38+
bbox))
3639

3740
(m/=> ->dimensions [:-> BBox Vec2])
3841
(defn ->dimensions
3942
"Converts a bounding box to [width height]."
4043
[[min-x min-y max-x max-y]]
41-
(matrix/sub [max-x max-y] [min-x min-y]))
44+
[(- max-x min-x) (- max-y min-y)])
4245

4346
(m/=> center [:-> BBox Vec2])
4447
(defn center
4548
"Calculates the center of a bounding box."
46-
[bbox]
47-
(matrix/add (take 2 bbox)
48-
(matrix/div (->dimensions bbox) 2)))
49+
[[min-x min-y max-x max-y]]
50+
[(+ min-x (/ (- max-x min-x) 2))
51+
(+ min-y (/ (- max-y min-y) 2))])
4952

5053
(m/=> intersect? [:-> BBox BBox boolean?])
5154
(defn intersect?
5255
"Tests whether the provided set of bounds intersect."
53-
[[a-min-x a-min-y a-max-x a-max-y]
54-
[b-min-x b-min-y b-max-x b-max-y]]
56+
[[a-min-x a-min-y a-max-x a-max-y] [b-min-x b-min-y b-max-x b-max-y]]
5557
(not (or (> b-min-x a-max-x)
5658
(< b-max-x a-min-x)
5759
(> b-min-y a-max-y)

src/renderer/utils/element.cljs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@
3131
[el]
3232
(or (svg? el) (root? el)))
3333

34+
(def properties-memo (memoize element.hierarchy/properties))
35+
3436
(m/=> properties [:-> Element [:maybe map?]])
3537
(defn properties
3638
[el]
37-
(-> el :tag element.hierarchy/properties))
39+
(-> el :tag properties-memo))
3840

3941
(m/=> ratio-locked? [:-> Element boolean?])
4042
(defn ratio-locked?
@@ -172,8 +174,11 @@
172174
(defn ->dom-element
173175
[el]
174176
(let [{:keys [tag attrs]} el
175-
dom-el (js/document.createElementNS "http://www.w3.org/2000/svg" (name tag))]
176-
(doseq [[k v] attrs]
177-
(when (supported-attr? (dissoc el :attrs) k)
178-
(.setAttributeNS dom-el nil (name k) v)))
177+
dom-el (js/document.createElementNS "http://www.w3.org/2000/svg" (name tag))
178+
el (dissoc el :attrs)
179+
supported-attrs (keep (fn [[k v]]
180+
(when (supported-attr? el k)
181+
[k v])) attrs)]
182+
(doseq [[k v] supported-attrs]
183+
(.setAttributeNS dom-el nil (name k) v))
179184
dom-el))

src/renderer/utils/length.cljs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(ns renderer.utils.length
22
(:require
3+
[clojure.string :as string]
34
[malli.core :as m]
45
[renderer.utils.unit :as utils.unit]))
56

@@ -9,17 +10,17 @@
910

1011
;; TODO: Find an agnostic way to handle percentages (we need to pass a base).
1112
(defonce unit-to-pixel-map
12-
{:px 1
13-
:ch 8
14-
:ex 7.15625
15-
:em 16
16-
:rem 16
17-
:in ppi
18-
:cm (/ ppi 2.54)
19-
:mm (/ ppi 25.4)
20-
:pt (/ ppi 72)
21-
:pc (/ ppi 6)
22-
:% 1})
13+
{"px" 1
14+
"ch" 8
15+
"ex" 7.15625
16+
"em" 16
17+
"rem" 16
18+
"in" ppi
19+
"cm" (/ ppi 2.54)
20+
"mm" (/ ppi 25.4)
21+
"pt" (/ ppi 72)
22+
"pc" (/ ppi 6)
23+
"%" 1})
2324

2425
(m/=> valid-unit? [:-> string? boolean?])
2526
(defn valid-unit?
@@ -29,11 +30,10 @@
2930
(m/=> multiplier [:-> string? number?])
3031
(defn multiplier
3132
"Returns the multiplier by unit.
32-
If the unit is invalid, it fallbacks to :px (1)."
33+
If the unit is invalid, it fallbacks to px (1)."
3334
[s]
34-
(get unit-to-pixel-map (if (valid-unit? s)
35-
(utils.unit/->key s)
36-
:px)))
35+
(or (get unit-to-pixel-map (string/lower-case s))
36+
1))
3737

3838
(m/=> ->px [:-> number? string? number?])
3939
(defn ->px

src/renderer/utils/unit.cljs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@
33
[clojure.string :as string]
44
[malli.core :as m]))
55

6-
(m/=> ->key [:-> string? keyword?])
7-
(defn ->key
8-
"Converts the string unit to a lower-cased keyword."
9-
[s]
10-
(keyword (string/lower-case s)))
6+
(defonce unit-regex #"[\d.\-\+]*\s*(.*)")
117

128
(m/=> match [:-> string? string?])
139
(defn match
1410
[s]
15-
(second (re-matches #"[\d.\-\+]*\s*(.*)" s)))
11+
(second (re-matches unit-regex s)))
1612

1713
(m/=> parse [:-> [:or string? number? nil?] [:tuple number? string?]])
1814
(defn parse

test/utils/unit_test.cljs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
33
[cljs.test :refer-macros [deftest testing are]]
44
[renderer.utils.unit :as utils.unit]))
55

6-
(deftest test-->key
7-
(testing "convert unit string to keyword"
8-
(are [x y] (= x y)
9-
:px (utils.unit/->key "px")
10-
:px (utils.unit/->key "Px"))))
11-
126
(deftest test-unit
137
(testing "match unit"
148
(are [x y] (= x y)

0 commit comments

Comments
 (0)