Skip to content

Commit 5f40e1a

Browse files
committed
enhance performance and refactor
1 parent fed6c75 commit 5f40e1a

File tree

5 files changed

+117
-82
lines changed

5 files changed

+117
-82
lines changed

src/renderer/element/events.cljs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
::select-ids
2525
(fn [db [_ ids]]
2626
(-> (partial-right element.handlers/assoc-prop :selected true)
27-
(reduce (element.handlers/deselect-all db) ids)
27+
(reduce (element.handlers/deselect db) ids)
2828
(history.handlers/finalize "Select elements"))))
2929

3030
(rf/reg-event-db
@@ -84,7 +84,7 @@
8484
(rf/reg-event-db
8585
::deselect-all
8686
(fn [db]
87-
(-> (element.handlers/deselect-all db)
87+
(-> (element.handlers/deselect db)
8888
(history.handlers/finalize "Deselect all"))))
8989

9090
(rf/reg-event-db

src/renderer/element/handlers.cljs

Lines changed: 100 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
(m/=> entity [:-> App [:maybe uuid?] [:maybe Element]])
5151
(defn entity
5252
[db id]
53-
(get (entities db) id))
53+
(get-in db (path db id)))
5454

5555
(m/=> root [:-> App Element])
5656
(defn root
@@ -62,30 +62,42 @@
6262
[db id]
6363
(-> db (entity id) :locked boolean))
6464

65-
(m/=> selected [:-> App [:sequential Element]])
65+
(m/=> selected [:function
66+
[:-> fn?]
67+
[:-> App [:sequential Element]]])
6668
(defn selected
67-
[db]
68-
(->> db entities vals (filter :selected)))
69+
([]
70+
(comp (map val) (filter :selected)))
71+
([db]
72+
(into [] (selected) (entities db))))
6973

7074
(m/=> ratio-locked? [:-> App boolean?])
7175
(defn ratio-locked?
7276
[db]
7377
(every? utils.element/ratio-locked? (selected db)))
7478

75-
(m/=> selected-ids [:-> App [:set uuid?]])
79+
(m/=> selected-ids [:function
80+
[:-> fn?]
81+
[:-> App [:set uuid?]]])
7682
(defn selected-ids
77-
[db]
78-
(into #{} (map :id) (selected db)))
83+
([]
84+
(comp (selected) (map :id)))
85+
([db]
86+
(into #{} (selected-ids) (entities db))))
7987

8088
(m/=> children-ids [:-> App uuid? [:vector uuid?]])
8189
(defn children-ids
8290
[db id]
8391
(:children (entity db id)))
8492

85-
(m/=> parent-ids [:-> App [:set uuid?]])
93+
(m/=> parent-ids [:function
94+
[:-> fn?]
95+
[:-> App [:set uuid?]]])
8696
(defn parent-ids
87-
[db]
88-
(into #{} (keep :parent) (selected db)))
97+
([]
98+
(comp (selected) (keep :parent)))
99+
([db]
100+
(into #{} (parent-ids) (entities db))))
89101

90102
(m/=> parent [:function
91103
[:-> App [:maybe Element]]
@@ -129,22 +141,33 @@
129141
(defn refresh-bbox
130142
[db id]
131143
(let [el (entity db id)
144+
children (children-ids db id)
132145
bbox (if (= (:tag el) :g)
133-
(let [b (map #(adjusted-bbox db %) (children-ids db id))]
146+
(let [b (map #(adjusted-bbox db %) children)]
134147
(when (seq b) (apply utils.bounds/union b)))
135148
(adjusted-bbox db id))]
136149
(if (or (not bbox) (utils.element/root? el))
137150
db
138-
(-> (reduce refresh-bbox db (children-ids db id))
151+
(-> (reduce refresh-bbox db children)
139152
(update-in (path db id) assoc :bbox bbox)))))
140153

141154
(m/=> update-el [:-> App uuid? ifn? [:* any?] App])
142155
(defn update-el
143-
[db id f & more]
144-
(if (locked? db id)
145-
db
146-
(-> (apply update-in db (path db id) f more)
147-
(refresh-bbox id))))
156+
([db id f]
157+
(if (locked? db id)
158+
db
159+
(-> (update-in db (path db id) f)
160+
(refresh-bbox id))))
161+
([db id f arg]
162+
(if (locked? db id)
163+
db
164+
(-> (update-in db (path db id) f arg)
165+
(refresh-bbox id))))
166+
([db id f arg & more]
167+
(if (locked? db id)
168+
db
169+
(-> (apply update-in db (path db id) f arg more)
170+
(refresh-bbox id)))))
148171

149172
(m/=> siblings-selected? [:-> App [:maybe boolean?]])
150173
(defn siblings-selected?
@@ -167,9 +190,7 @@
167190
(m/=> root-children [:-> App [:sequential Element]])
168191
(defn root-children
169192
[db]
170-
(->> (:id (root db))
171-
(children-ids db)
172-
(mapv (entities db))))
193+
(into [] (map (entities db)) (children-ids db (:id (root db)))))
173194

174195
(m/=> root-svgs [:-> App [:sequential Element]])
175196
(defn root-svgs
@@ -215,7 +236,8 @@
215236
[:-> App uuid? [:set uuid?]]])
216237
(defn descendant-ids
217238
([db]
218-
(reduce #(set/union %1 (descendant-ids db %2)) #{} (selected-ids db)))
239+
(into #{} (comp (selected-ids)
240+
(mapcat #(descendant-ids db %))) (entities db)))
219241
([db id]
220242
(loop [children-set (set (children-ids db id))
221243
child-keys #{}]
@@ -240,6 +262,14 @@
240262
[db]
241263
(set/difference (-> db entities keys set) (selected-with-descendant-ids db)))
242264

265+
(m/=> non-selected-visible [:-> App [:sequential Element]])
266+
(defn non-selected-visible
267+
[db]
268+
(sequence (comp (filter (comp (non-selected-ids db) key))
269+
(map val)
270+
(filter :visible))
271+
(entities db)))
272+
243273
(m/=> top-selected-ancestors [:-> App [:sequential Element]])
244274
(defn top-selected-ancestors
245275
[db]
@@ -249,8 +279,12 @@
249279

250280
(m/=> update-prop [:-> App uuid? ifn? [:* any?] App])
251281
(defn update-prop
252-
[db id k & more]
253-
(apply update-in db (path db id k) more))
282+
([db id k f]
283+
(update-in db (path db id k) f))
284+
([db id k f arg]
285+
(update-in db (path db id k) f arg))
286+
([db id k f arg & more]
287+
(apply update-in db (path db id k) f arg more)))
254288

255289
(m/=> assoc-prop [:function
256290
[:-> App keyword? any? App]
@@ -303,21 +337,27 @@
303337

304338
(m/=> update-attr [:-> App uuid? keyword? ifn? [:* any?] App])
305339
(defn update-attr
306-
[db id k f & more]
307-
(if (utils.element/supported-attr? (entity db id) k)
308-
(apply update-el db id attr.hierarchy/update-attr k f more)
309-
db))
340+
([db id k f]
341+
(cond-> db
342+
(utils.element/supported-attr? (entity db id) k)
343+
(update-el id attr.hierarchy/update-attr k f)))
344+
([db id k f arg]
345+
(cond-> db
346+
(utils.element/supported-attr? (entity db id) k)
347+
(update-el id attr.hierarchy/update-attr k f arg)))
348+
([db id k f arg & more]
349+
(cond-> db
350+
(utils.element/supported-attr? (entity db id) k)
351+
(apply update-el id attr.hierarchy/update-attr k f arg more))))
310352

311-
(m/=> deselect [:-> App uuid? App])
353+
(m/=> deselect [:function
354+
[:-> App App]
355+
[:-> App uuid? App]])
312356
(defn deselect
313-
[db id]
314-
(assoc-prop db id :selected false))
315-
316-
(m/=> deselect-all [:-> App App])
317-
(defn deselect-all
318-
[db]
319-
(->> (selected-ids db)
320-
(reduce deselect db)))
357+
([db]
358+
(transduce (selected-ids) (completing deselect) db (entities db)))
359+
([db id]
360+
(assoc-prop db id :selected false)))
321361

322362
(m/=> collapse [:-> App uuid? App])
323363
(defn collapse
@@ -327,9 +367,7 @@
327367
(m/=> collapse-all [:-> App App])
328368
(defn collapse-all
329369
[db]
330-
(->> (entities db)
331-
(keys)
332-
(reduce collapse db)))
370+
(transduce (map key) (completing collapse) db (entities db)))
333371

334372
(m/=> expand [:-> App uuid? App])
335373
(defn expand
@@ -339,8 +377,7 @@
339377
(m/=> expand-ancestors [:-> App uuid? App])
340378
(defn expand-ancestors
341379
[db id]
342-
(->> (ancestor-ids db id)
343-
(reduce expand db)))
380+
(reduce expand db (ancestor-ids db id)))
344381

345382
(m/=> select [:-> App uuid? App])
346383
(defn select
@@ -355,8 +392,8 @@
355392
(if (entity db id)
356393
(if multiple
357394
(update-prop db id :selected not)
358-
(-> db deselect-all (select id)))
359-
(deselect-all db)))
395+
(-> db deselect (select id)))
396+
(deselect db)))
360397

361398
(m/=> select-all [:-> App App])
362399
(defn select-all
@@ -368,25 +405,27 @@
368405
(m/=> selected-tags [:-> App [:set Tag]])
369406
(defn selected-tags
370407
[db]
371-
(into #{}
372-
(map :tag)
373-
(selected db)))
408+
(into #{} (comp (selected)
409+
(map :tag)) (entities db)))
374410

375411
(m/=> filter-by-tag [:-> App Tag [:sequential Element]])
376412
(defn filter-by-tag
377-
[db tag]
378-
(filter #(= tag (:tag %)) (selected db)))
413+
([tag]
414+
(comp (selected)
415+
(filter #(= tag (:tag %)))))
416+
([db tag]
417+
(into [] (filter-by-tag tag) (entities db))))
379418

380419
(m/=> select-same-tags [:-> App App])
381420
(defn select-same-tags
382421
[db]
383422
(let [tags (selected-tags db)]
384-
(->> (entities db)
385-
(vals)
386-
(reduce (fn [db el]
387-
(cond-> db
388-
(contains? tags (:tag el))
389-
(select (:id el)))) db))))
423+
(transduce (comp (map val)
424+
(filter #(contains? tags (:tag %)))
425+
(map :id))
426+
(completing select)
427+
db
428+
(entities db))))
390429

391430
(m/=> sort-by-index-path [:-> App [:sequential Element] [:sequential Element]])
392431
(defn sort-by-index-path
@@ -680,7 +719,7 @@
680719
(m/=> add [:-> App map? App])
681720
(defn add
682721
[db el]
683-
(-> (deselect-all db)
722+
(-> (deselect db)
684723
(create (assoc el :selected true))))
685724

686725
(m/=> swap [:-> App Element App])
@@ -712,7 +751,7 @@
712751
[:-> App Element App]])
713752
(defn paste-in-place
714753
([db]
715-
(reduce paste-in-place (deselect-all db) (:copied-elements db)))
754+
(reduce paste-in-place (deselect db) (:copied-elements db)))
716755
([db el]
717756
(->> (selected-ids db)
718757
(reduce select (add db el)))))
@@ -723,7 +762,7 @@
723762
(defn paste
724763
([db]
725764
(let [parent-el (hovered-svg db)]
726-
(reduce (partial-right paste parent-el) (deselect-all db) (:copied-elements db))))
765+
(reduce (partial-right paste parent-el) (deselect db) (:copied-elements db))))
727766
([db el parent-el]
728767
(let [center (utils.bounds/center (:copied-bbox db))
729768
el-center (utils.bounds/center (:bbox el))
@@ -735,7 +774,7 @@
735774
select
736775
(cond-> db
737776
:always
738-
(-> (deselect-all)
777+
(-> (deselect)
739778
(add (assoc el :parent (:id parent-el)))
740779
(place (matrix/add pointer-pos offset)))
741780

@@ -745,7 +784,7 @@
745784
(m/=> duplicate [:-> App App])
746785
(defn duplicate
747786
[db]
748-
(reduce create (deselect-all db) (top-selected-sorted db)))
787+
(reduce create (deselect db) (top-selected-sorted db)))
749788

750789
(m/=> animate [:function
751790
[:-> App AnimationTag App]
@@ -755,7 +794,7 @@
755794
([db tag]
756795
(animate db tag {}))
757796
([db tag attrs]
758-
(reduce (partial-right animate tag attrs) (deselect-all db) (selected-ids db)))
797+
(reduce (partial-right animate tag attrs) (deselect db) (selected-ids db)))
759798
([db id tag attrs]
760799
(reduce select (add db {:tag tag
761800
:attrs attrs
@@ -858,5 +897,4 @@
858897
(defn snapping-points
859898
[db els]
860899
(let [options (-> db :snap :options)]
861-
(reduce (fn [points el]
862-
(into points (utils.element/snapping-points el options))) [] els)))
900+
(into [] (mapcat #(utils.element/snapping-points % options)) els)))

src/renderer/tool/impl/base/edit.cljs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,7 @@
100100

101101
(defmethod tool.hierarchy/snapping-elements :edit
102102
[db]
103-
(let [non-selected-ids (element.handlers/non-selected-ids db)
104-
non-selected (select-keys (element.handlers/entities db)
105-
(vec non-selected-ids))]
106-
(->> (vals non-selected)
107-
(filter :visible))))
103+
(element.handlers/non-selected-visible db))
108104

109105
(defmethod tool.hierarchy/render :edit
110106
[]

0 commit comments

Comments
 (0)