Skip to content

Commit 2b72b66

Browse files
committed
support initial attribute value
1 parent de1bde0 commit 2b72b66

File tree

6 files changed

+55
-15
lines changed

6 files changed

+55
-15
lines changed

src/renderer/attribute/hierarchy.cljs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
(ns renderer.attribute.hierarchy)
22

3+
(defmulti initial (fn [tag k] [tag k]))
34
(defmulti update-attr (fn [_ k & _more] k))
45
(defmulti description (fn [tag k] [tag k]))
56
(defmulti form-element (fn [tag k _v _attrs] [tag k]))
67

8+
(defmethod initial :default [_tag _k] nil)
79
(defmethod update-attr :default
810
([el k f]
911
(update-in el [:attrs k] f))

src/renderer/attribute/impl/length.cljs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
:on-click #(rf/dispatch [::element.events/update-attr k inc])}
4848
[views/icon "plus"]]]])
4949

50+
(defmethod attribute.hierarchy/initial ::length [_tag _attr] 0)
51+
5052
(defmethod attribute.hierarchy/update-attr ::length
5153
([el k f]
5254
(update-in el [:attrs k] #(utils.length/transform % f)))

src/renderer/attribute/views.cljs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,7 @@
204204

205205
(defn row
206206
[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)
209208
dispatch-tag (if (contains? (methods attribute.hierarchy/form-element) [tag k])
210209
tag
211210
:default)]

src/renderer/element/impl/custom/blob.cljs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
[renderer.element.subs :as-alias element.subs]
1414
[renderer.event.impl.pointer :as event.impl.pointer]
1515
[renderer.tool.views :as tool.views]
16+
[renderer.utils.attribute :as utils.attribute]
1617
[renderer.utils.element :as utils.element]
1718
[renderer.utils.length :as utils.length]
1819
[renderer.utils.svg :as utils.svg]
@@ -71,6 +72,12 @@
7172
[]
7273
"The size of the bounding box.")
7374

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+
7481
(defmethod element.hierarchy/properties :blob
7582
[]
7683
{:icon "blob"
@@ -135,8 +142,8 @@
135142
[el]
136143
(let [{{:keys [x y]} :attrs} el
137144
[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))
140147
(reduce (fn [options [k v]] (assoc options k (int v))) {})
141148
(clj->js))]
142149
(-> blobs

src/renderer/utils/attribute.cljs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
[camel-snake-kebab.core :as camel-snake-kebab]
66
[clojure.string :as string]
77
[malli.core :as m]
8+
[renderer.attribute.hierarchy :as attribute.hierarchy]
89
[renderer.element.db :as element.db :refer [Attrs Tag]]
910
[renderer.element.hierarchy :as element.hierarchy]))
1011

@@ -235,7 +236,8 @@
235236
(defn enhance-data-readability
236237
[property k]
237238
(cond-> property
238-
(and (get property k) (string? (get property k)))
239+
(and (get property k)
240+
(string? (get property k)))
239241
(update k #(-> (camel-snake-kebab/->kebab-case-string %)
240242
(string/replace "-" " ")))))
241243

@@ -302,3 +304,21 @@
302304
(zipmap (:attrs (element.hierarchy/properties tag)) (repeat ""))))
303305

304306
(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)))

src/renderer/utils/length.cljs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
(:require
33
[clojure.string :as string]
44
[malli.core :as m]
5+
[renderer.element.db :as db :refer [Tag]]
6+
[renderer.utils.attribute :as utils.attribute]
57
[renderer.utils.unit :as utils.unit]))
68

79
(defonce ppi 96)
810

9-
(defonce units #{"px" "ch" "ex" "em" "rem" "in" "cm" "mm" "pt" "pc" "%"})
10-
1111
;; TODO: Find an agnostic way to handle percentages (we need to pass a base).
12-
(defonce unit-to-pixel-map
12+
(defonce units
1313
{"px" 1
1414
"ch" 8
1515
"ex" 7.15625
@@ -32,7 +32,7 @@
3232
"Returns the multiplier by unit.
3333
If the unit is invalid, it fallbacks to px (1)."
3434
[s]
35-
(or (get unit-to-pixel-map (string/lower-case s))
35+
(or (get units (string/lower-case s))
3636
1))
3737

3838
(m/=> ->px [:-> number? string? number?])
@@ -45,13 +45,23 @@
4545
[n unit]
4646
(/ n (multiplier unit)))
4747

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?]])
4952
(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)))))
5565

5666
(m/=> transform [:-> [:or string? number? nil?] ifn? [:* any?] string?])
5767
(defn transform

0 commit comments

Comments
 (0)