|
50 | 50 | (m/=> entity [:-> App [:maybe uuid?] [:maybe Element]])
|
51 | 51 | (defn entity
|
52 | 52 | [db id]
|
53 |
| - (get (entities db) id)) |
| 53 | + (get-in db (path db id))) |
54 | 54 |
|
55 | 55 | (m/=> root [:-> App Element])
|
56 | 56 | (defn root
|
|
62 | 62 | [db id]
|
63 | 63 | (-> db (entity id) :locked boolean))
|
64 | 64 |
|
65 |
| -(m/=> selected [:-> App [:sequential Element]]) |
| 65 | +(m/=> selected [:function |
| 66 | + [:-> fn?] |
| 67 | + [:-> App [:sequential Element]]]) |
66 | 68 | (defn selected
|
67 |
| - [db] |
68 |
| - (->> db entities vals (filter :selected))) |
| 69 | + ([] |
| 70 | + (comp (map val) (filter :selected))) |
| 71 | + ([db] |
| 72 | + (into [] (selected) (entities db)))) |
69 | 73 |
|
70 | 74 | (m/=> ratio-locked? [:-> App boolean?])
|
71 | 75 | (defn ratio-locked?
|
72 | 76 | [db]
|
73 | 77 | (every? utils.element/ratio-locked? (selected db)))
|
74 | 78 |
|
75 |
| -(m/=> selected-ids [:-> App [:set uuid?]]) |
| 79 | +(m/=> selected-ids [:function |
| 80 | + [:-> fn?] |
| 81 | + [:-> App [:set uuid?]]]) |
76 | 82 | (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)))) |
79 | 87 |
|
80 | 88 | (m/=> children-ids [:-> App uuid? [:vector uuid?]])
|
81 | 89 | (defn children-ids
|
82 | 90 | [db id]
|
83 | 91 | (:children (entity db id)))
|
84 | 92 |
|
85 |
| -(m/=> parent-ids [:-> App [:set uuid?]]) |
| 93 | +(m/=> parent-ids [:function |
| 94 | + [:-> fn?] |
| 95 | + [:-> App [:set uuid?]]]) |
86 | 96 | (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)))) |
89 | 101 |
|
90 | 102 | (m/=> parent [:function
|
91 | 103 | [:-> App [:maybe Element]]
|
|
129 | 141 | (defn refresh-bbox
|
130 | 142 | [db id]
|
131 | 143 | (let [el (entity db id)
|
| 144 | + children (children-ids db id) |
132 | 145 | bbox (if (= (:tag el) :g)
|
133 |
| - (let [b (map #(adjusted-bbox db %) (children-ids db id))] |
| 146 | + (let [b (map #(adjusted-bbox db %) children)] |
134 | 147 | (when (seq b) (apply utils.bounds/union b)))
|
135 | 148 | (adjusted-bbox db id))]
|
136 | 149 | (if (or (not bbox) (utils.element/root? el))
|
137 | 150 | db
|
138 |
| - (-> (reduce refresh-bbox db (children-ids db id)) |
| 151 | + (-> (reduce refresh-bbox db children) |
139 | 152 | (update-in (path db id) assoc :bbox bbox)))))
|
140 | 153 |
|
141 | 154 | (m/=> update-el [:-> App uuid? ifn? [:* any?] App])
|
142 | 155 | (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))))) |
148 | 171 |
|
149 | 172 | (m/=> siblings-selected? [:-> App [:maybe boolean?]])
|
150 | 173 | (defn siblings-selected?
|
|
167 | 190 | (m/=> root-children [:-> App [:sequential Element]])
|
168 | 191 | (defn root-children
|
169 | 192 | [db]
|
170 |
| - (->> (:id (root db)) |
171 |
| - (children-ids db) |
172 |
| - (mapv (entities db)))) |
| 193 | + (into [] (map (entities db)) (children-ids db (:id (root db))))) |
173 | 194 |
|
174 | 195 | (m/=> root-svgs [:-> App [:sequential Element]])
|
175 | 196 | (defn root-svgs
|
|
215 | 236 | [:-> App uuid? [:set uuid?]]])
|
216 | 237 | (defn descendant-ids
|
217 | 238 | ([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))) |
219 | 241 | ([db id]
|
220 | 242 | (loop [children-set (set (children-ids db id))
|
221 | 243 | child-keys #{}]
|
|
240 | 262 | [db]
|
241 | 263 | (set/difference (-> db entities keys set) (selected-with-descendant-ids db)))
|
242 | 264 |
|
| 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 | + |
243 | 273 | (m/=> top-selected-ancestors [:-> App [:sequential Element]])
|
244 | 274 | (defn top-selected-ancestors
|
245 | 275 | [db]
|
|
249 | 279 |
|
250 | 280 | (m/=> update-prop [:-> App uuid? ifn? [:* any?] App])
|
251 | 281 | (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))) |
254 | 288 |
|
255 | 289 | (m/=> assoc-prop [:function
|
256 | 290 | [:-> App keyword? any? App]
|
|
303 | 337 |
|
304 | 338 | (m/=> update-attr [:-> App uuid? keyword? ifn? [:* any?] App])
|
305 | 339 | (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)))) |
310 | 352 |
|
311 |
| -(m/=> deselect [:-> App uuid? App]) |
| 353 | +(m/=> deselect [:function |
| 354 | + [:-> App App] |
| 355 | + [:-> App uuid? App]]) |
312 | 356 | (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))) |
321 | 361 |
|
322 | 362 | (m/=> collapse [:-> App uuid? App])
|
323 | 363 | (defn collapse
|
|
327 | 367 | (m/=> collapse-all [:-> App App])
|
328 | 368 | (defn collapse-all
|
329 | 369 | [db]
|
330 |
| - (->> (entities db) |
331 |
| - (keys) |
332 |
| - (reduce collapse db))) |
| 370 | + (transduce (map key) (completing collapse) db (entities db))) |
333 | 371 |
|
334 | 372 | (m/=> expand [:-> App uuid? App])
|
335 | 373 | (defn expand
|
|
339 | 377 | (m/=> expand-ancestors [:-> App uuid? App])
|
340 | 378 | (defn expand-ancestors
|
341 | 379 | [db id]
|
342 |
| - (->> (ancestor-ids db id) |
343 |
| - (reduce expand db))) |
| 380 | + (reduce expand db (ancestor-ids db id))) |
344 | 381 |
|
345 | 382 | (m/=> select [:-> App uuid? App])
|
346 | 383 | (defn select
|
|
355 | 392 | (if (entity db id)
|
356 | 393 | (if multiple
|
357 | 394 | (update-prop db id :selected not)
|
358 |
| - (-> db deselect-all (select id))) |
359 |
| - (deselect-all db))) |
| 395 | + (-> db deselect (select id))) |
| 396 | + (deselect db))) |
360 | 397 |
|
361 | 398 | (m/=> select-all [:-> App App])
|
362 | 399 | (defn select-all
|
|
368 | 405 | (m/=> selected-tags [:-> App [:set Tag]])
|
369 | 406 | (defn selected-tags
|
370 | 407 | [db]
|
371 |
| - (into #{} |
372 |
| - (map :tag) |
373 |
| - (selected db))) |
| 408 | + (into #{} (comp (selected) |
| 409 | + (map :tag)) (entities db))) |
374 | 410 |
|
375 | 411 | (m/=> filter-by-tag [:-> App Tag [:sequential Element]])
|
376 | 412 | (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)))) |
379 | 418 |
|
380 | 419 | (m/=> select-same-tags [:-> App App])
|
381 | 420 | (defn select-same-tags
|
382 | 421 | [db]
|
383 | 422 | (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)))) |
390 | 429 |
|
391 | 430 | (m/=> sort-by-index-path [:-> App [:sequential Element] [:sequential Element]])
|
392 | 431 | (defn sort-by-index-path
|
|
680 | 719 | (m/=> add [:-> App map? App])
|
681 | 720 | (defn add
|
682 | 721 | [db el]
|
683 |
| - (-> (deselect-all db) |
| 722 | + (-> (deselect db) |
684 | 723 | (create (assoc el :selected true))))
|
685 | 724 |
|
686 | 725 | (m/=> swap [:-> App Element App])
|
|
712 | 751 | [:-> App Element App]])
|
713 | 752 | (defn paste-in-place
|
714 | 753 | ([db]
|
715 |
| - (reduce paste-in-place (deselect-all db) (:copied-elements db))) |
| 754 | + (reduce paste-in-place (deselect db) (:copied-elements db))) |
716 | 755 | ([db el]
|
717 | 756 | (->> (selected-ids db)
|
718 | 757 | (reduce select (add db el)))))
|
|
723 | 762 | (defn paste
|
724 | 763 | ([db]
|
725 | 764 | (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)))) |
727 | 766 | ([db el parent-el]
|
728 | 767 | (let [center (utils.bounds/center (:copied-bbox db))
|
729 | 768 | el-center (utils.bounds/center (:bbox el))
|
|
735 | 774 | select
|
736 | 775 | (cond-> db
|
737 | 776 | :always
|
738 |
| - (-> (deselect-all) |
| 777 | + (-> (deselect) |
739 | 778 | (add (assoc el :parent (:id parent-el)))
|
740 | 779 | (place (matrix/add pointer-pos offset)))
|
741 | 780 |
|
|
745 | 784 | (m/=> duplicate [:-> App App])
|
746 | 785 | (defn duplicate
|
747 | 786 | [db]
|
748 |
| - (reduce create (deselect-all db) (top-selected-sorted db))) |
| 787 | + (reduce create (deselect db) (top-selected-sorted db))) |
749 | 788 |
|
750 | 789 | (m/=> animate [:function
|
751 | 790 | [:-> App AnimationTag App]
|
|
755 | 794 | ([db tag]
|
756 | 795 | (animate db tag {}))
|
757 | 796 | ([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))) |
759 | 798 | ([db id tag attrs]
|
760 | 799 | (reduce select (add db {:tag tag
|
761 | 800 | :attrs attrs
|
|
858 | 897 | (defn snapping-points
|
859 | 898 | [db els]
|
860 | 899 | (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))) |
0 commit comments