File tree Expand file tree Collapse file tree 6 files changed +55
-15
lines changed Expand file tree Collapse file tree 6 files changed +55
-15
lines changed Original file line number Diff line number Diff line change 1
1
(ns renderer.attribute.hierarchy )
2
2
3
+ (defmulti initial (fn [tag k] [tag k]))
3
4
(defmulti update-attr (fn [_ k & _more] k))
4
5
(defmulti description (fn [tag k] [tag k]))
5
6
(defmulti form-element (fn [tag k _v _attrs] [tag k]))
6
7
8
+ (defmethod initial :default [_tag _k] nil )
7
9
(defmethod update-attr :default
8
10
([el k f]
9
11
(update-in el [:attrs k] f))
Original file line number Diff line number Diff line change 47
47
:on-click #(rf/dispatch [::element.events/update-attr k inc])}
48
48
[views/icon " plus" ]]]])
49
49
50
+ (defmethod attribute.hierarchy /initial ::length [_tag _attr] 0 )
51
+
50
52
(defmethod attribute.hierarchy /update-attr ::length
51
53
([el k f]
52
54
(update-in el [:attrs k] #(utils.length/transform % f)))
Original file line number Diff line number Diff line change 204
204
205
205
(defn row
206
206
[k v locked? tag]
207
- (let [property (utils.attribute/property-data-memo k)
208
- initial (:initial property)
207
+ (let [initial (utils.attribute/initial-memo tag k)
209
208
dispatch-tag (if (contains? (methods attribute.hierarchy/form-element) [tag k])
210
209
tag
211
210
:default )]
Original file line number Diff line number Diff line change 13
13
[renderer.element.subs :as-alias element.subs]
14
14
[renderer.event.impl.pointer :as event.impl.pointer]
15
15
[renderer.tool.views :as tool.views]
16
+ [renderer.utils.attribute :as utils.attribute]
16
17
[renderer.utils.element :as utils.element]
17
18
[renderer.utils.length :as utils.length]
18
19
[renderer.utils.svg :as utils.svg]
71
72
[]
72
73
" The size of the bounding box." )
73
74
75
+ (defmethod attr.hierarchy /initial [:blob :extraPoints ] [] " 0" )
76
+
77
+ (defmethod attr.hierarchy /initial [:blob :randomness ] [] " 0" )
78
+
79
+ (defmethod attr.hierarchy /initial [:blob :size ] [] " 0" )
80
+
74
81
(defmethod element.hierarchy /properties :blob
75
82
[]
76
83
{:icon " blob"
135
142
[el]
136
143
(let [{{:keys [x y]} :attrs } el
137
144
[x y] (mapv utils.length/unit->px [x y])
138
- options (->> [:seed :extraPoints :randomness :size ]
139
- (select-keys ( :attrs el ))
145
+ options (->> ( select-keys ( :attrs el) [:seed :extraPoints :randomness :size ])
146
+ (merge ( utils.attribute/defaults-with-vals :blob ))
140
147
(reduce (fn [options [k v]] (assoc options k (int v))) {})
141
148
(clj->js ))]
142
149
(-> blobs
Original file line number Diff line number Diff line change 5
5
[camel-snake-kebab.core :as camel-snake-kebab]
6
6
[clojure.string :as string]
7
7
[malli.core :as m]
8
+ [renderer.attribute.hierarchy :as attribute.hierarchy]
8
9
[renderer.element.db :as element.db :refer [Attrs Tag]]
9
10
[renderer.element.hierarchy :as element.hierarchy]))
10
11
235
236
(defn enhance-data-readability
236
237
[property k]
237
238
(cond-> property
238
- (and (get property k) (string? (get property k)))
239
+ (and (get property k)
240
+ (string? (get property k)))
239
241
(update k #(-> (camel-snake-kebab/->kebab-case-string %)
240
242
(string/replace " -" " " )))))
241
243
302
304
(zipmap (:attrs (element.hierarchy/properties tag)) (repeat " " ))))
303
305
304
306
(def defaults-memo (memoize defaults ))
307
+
308
+ (m/=> initial [:-> Tag keyword? string?])
309
+ (defn initial
310
+ [tag k]
311
+ (let [dispatch-tag (if (contains? (methods attribute.hierarchy/initial) [tag k])
312
+ tag
313
+ :default )]
314
+ (or (attribute.hierarchy/initial dispatch-tag k)
315
+ (:initial (property-data-memo k)))))
316
+
317
+ (def initial-memo (memoize initial))
318
+
319
+ (m/=> defaults-with-vals [:-> Tag Attrs])
320
+ (defn defaults-with-vals
321
+ [tag]
322
+ (into {}
323
+ (map (fn [[k v]]
324
+ [k (or (initial-memo tag k) v)])) (defaults-memo tag )))
Original file line number Diff line number Diff line change 2
2
(:require
3
3
[clojure.string :as string]
4
4
[malli.core :as m]
5
+ [renderer.element.db :as db :refer [Tag]]
6
+ [renderer.utils.attribute :as utils.attribute]
5
7
[renderer.utils.unit :as utils.unit]))
6
8
7
9
(defonce ppi 96 )
8
10
9
- (defonce units #{" px" " ch" " ex" " em" " rem" " in" " cm" " mm" " pt" " pc" " %" })
10
-
11
11
; ; TODO: Find an agnostic way to handle percentages (we need to pass a base).
12
- (defonce unit-to-pixel-map
12
+ (defonce units
13
13
{" px" 1
14
14
" ch" 8
15
15
" ex" 7.15625
32
32
" Returns the multiplier by unit.
33
33
If the unit is invalid, it fallbacks to px (1)."
34
34
[s]
35
- (or (get unit-to-pixel-map (string/lower-case s))
35
+ (or (get units (string/lower-case s))
36
36
1 ))
37
37
38
38
(m/=> ->px [:-> number? string? number?])
45
45
[n unit]
46
46
(/ n (multiplier unit)))
47
47
48
- (m/=> unit->px [:-> [:or string? number? nil?] number?])
48
+ (m/=> unit->px [:function
49
+ [:-> [:or string? number? nil?] number?]
50
+ [:-> [:or string? number? nil?] number? number?]
51
+ [:-> [:or string? number? nil?] Tag keyword? number?]])
49
52
(defn unit->px
50
- [v]
51
- (let [[n unit] (utils.unit/parse v)]
52
- (if (empty? unit)
53
- n
54
- (if (valid-unit? unit) (->px n unit) 0 ))))
53
+ ([v]
54
+ (unit->px v 0 ))
55
+ ([v initial]
56
+ (let [[n unit] (utils.unit/parse v)]
57
+ (if (empty? unit)
58
+ n
59
+ (if (valid-unit? unit)
60
+ (->px n unit)
61
+ initial))))
62
+ ([v tag attr]
63
+ (let [initial (utils.attribute/initial-memo tag attr)]
64
+ (unit->px v (unit->px initial 0 )))))
55
65
56
66
(m/=> transform [:-> [:or string? number? nil?] ifn? [:* any?] string?])
57
67
(defn transform
You can’t perform that action at this time.
0 commit comments