Skip to content

Commit 82d7340

Browse files
committed
remove place multimethod and enhance specs
1 parent d4a2f7b commit 82d7340

File tree

10 files changed

+3093
-914
lines changed

10 files changed

+3093
-914
lines changed

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

+2,891-720
Large diffs are not rendered by default.

src/renderer/core.cljs

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
[renderer.theme.subs]
3838
[renderer.timeline.events]
3939
[renderer.timeline.subs]
40+
[renderer.tool.effects]
4041
[renderer.tool.events]
4142
[renderer.tool.impl.core]
4243
[renderer.tool.subs]

src/renderer/element/handlers.cljs

+42-11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
[renderer.utils.extra :refer [partial-right]]
2020
[renderer.utils.hiccup :as hiccup]
2121
[renderer.utils.map :as map]
22+
[renderer.utils.math :as math :refer [Vec2D]]
2223
[renderer.utils.path :as path]
2324
[renderer.utils.vec :as vec]))
2425

@@ -499,6 +500,7 @@
499500
(set-parent parent-id id)
500501
(update-prop parent-id :children vec/move last-index i))))
501502

503+
(m/=> hovered-svg [:-> App Element])
502504
(defn hovered-svg
503505
[db]
504506
(let [svgs (reverse (root-svgs db))
@@ -507,18 +509,30 @@
507509
(some #(when (bounds/contained-point? (:bounds %) pointer-pos) %) svgs)
508510
(root db))))
509511

512+
(m/=> translate [:function
513+
[:-> App Vec2D App]
514+
[:-> App uuid? Vec2D App]])
510515
(defn translate
516+
"Moves elements by a given offset."
511517
([db offset]
512518
(reduce (partial-right translate offset) db (top-ancestor-ids db)))
513519
([db id offset]
514520
(update-el db id hierarchy/translate offset)))
515521

522+
(m/=> place [:function
523+
[:-> App Vec2D App]
524+
[:-> App uuid? Vec2D App]])
516525
(defn place
517-
([db pos]
518-
(reduce (partial-right place pos) db (top-ancestor-ids db)))
519-
([db id pos]
520-
(update-el db id hierarchy/place pos)))
526+
"Positions elements to a given global position."
527+
([db position]
528+
(reduce (partial-right place position) db (top-ancestor-ids db)))
529+
([db id position]
530+
(let [el (entity db id)
531+
center (bounds/center (hierarchy/bounds el))
532+
offset (mat/sub position center)]
533+
(update-el db id hierarchy/translate offset))))
521534

535+
(m/=> scale [:-> App Vec2D Vec2D boolean? App])
522536
(defn scale
523537
[db ratio pivot-point recursive]
524538
(let [ids-to-scale (cond-> (selected-ids db) recursive (set/union (descendant-ids db)))]
@@ -529,6 +543,12 @@
529543
db
530544
ids-to-scale)))
531545

546+
(def Direction
547+
[:enum :top :center-vertical :bottom :left :center-horizontal :right])
548+
549+
(m/=> parent [:function
550+
[:-> App Direction App]
551+
[:-> App uuid? Direction App]])
532552
(defn align
533553
([db direction]
534554
(reduce (partial-right align direction) db (selected-ids db)))
@@ -547,18 +567,27 @@
547567
:center-horizontal [cx 0]
548568
:right [x2 0])))))
549569

570+
(m/=> ->path [:function
571+
[:-> App App]
572+
[:-> App uuid? App]])
550573
(defn ->path
574+
"Converts elements to paths."
551575
([db]
552576
(reduce ->path db (selected-ids db)))
553577
([db id]
554578
(update-el db id element/->path)))
555579

580+
(m/=> stroke->path [:function
581+
[:-> App App]
582+
[:-> App uuid? App]])
556583
(defn stroke->path
584+
"Converts the stroke of elements to paths."
557585
([db]
558586
(reduce stroke->path db (selected-ids db)))
559587
([db id]
560588
(update-el db id element/stroke->path)))
561589

590+
(m/=> overlapping-svg [:-> App Bounds App])
562591
(defn overlapping-svg
563592
[db el-bounds]
564593
(let [svgs (reverse (root-svgs db))] ; Reverse to select top svgs first.
@@ -567,22 +596,23 @@
567596
(some #(when (bounds/intersect? el-bounds (:bounds %)) %) svgs)
568597
(root db))))
569598

570-
(defn create-parent-id
599+
(m/=> assoc-parent-id [:-> App Element Element])
600+
(defn assoc-parent-id
571601
[db el]
572602
(cond-> el
573-
(not (element/root? el))
574-
(assoc :parent (or (:parent el)
575-
(:id (if (element/svg? el)
576-
(root db)
577-
(overlapping-svg db (hierarchy/bounds el))))))))
603+
(not (or (element/root? el) (:parent el)))
604+
(assoc :parent (:id (if (element/svg? el)
605+
(root db)
606+
(overlapping-svg db (hierarchy/bounds el)))))))
578607

608+
(m/=> create [:-> App map? App])
579609
(defn create
580610
[db el]
581611
(let [id (random-uuid) ; REVIEW: Hard to use a coeffect because of recursion.
582612
new-el (->> (cond-> el (not (string? (:content el))) (dissoc :content))
583613
(map/remove-nils)
584614
(element/normalize-attrs)
585-
(create-parent-id db))
615+
(assoc-parent-id db))
586616
new-el (merge new-el db/default {:id id})
587617
child-els (-> (entities db (set (:children el))) vals (concat (:content el)))
588618
[x1 y1] (hierarchy/bounds (entity db (:parent new-el)))
@@ -610,6 +640,7 @@
610640
child-els
611641
(add-children child-els)))))
612642

643+
(m/=> create-default-canvas [:-> App Vec2D App])
613644
(defn create-default-canvas
614645
[db size]
615646
(cond-> db

src/renderer/element/hierarchy.cljs

-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
(defmulti render-edit "Renders the edit overlay of the element." :tag)
2323
(defmulti bounds "Returns the local bounds of the element." :tag)
2424
(defmulti translate "Translates the element by a given offset." (fn [el _offset] (:tag el)))
25-
(defmulti place "Moves the element to a given global position." (fn [el _position] (:tag el)))
2625
(defmulti scale "Scales the element by a given ratio and pivot-point." (fn [el _ration _pivot-point] (:tag el)))
2726
(defmulti edit "Edits the element by a given offset and handle." (fn [el _offset _handle] (:tag el)))
2827
(defmulti properties "Returns the properties of the element." keyword)
@@ -36,7 +35,6 @@
3635
(defmethod snapping-points :default [])
3736
(defmethod scale :default [element] element)
3837
(defmethod translate :default [element] element)
39-
(defmethod place :default [element] element)
4038
(defmethod edit :default [element] element)
4139
(defmethod properties :default [])
4240

0 commit comments

Comments
 (0)