forked from clj-commons/cljss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
typed.clj
139 lines (112 loc) · 3.27 KB
/
typed.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
(ns cljss.css.typed
"CSS Typed OM Level 1
Spec https://drafts.css-houdini.org/css-typed-om/
Article https://developers.google.com/web/updates/2018/03/cssom"
(:require [clojure.spec.alpha :as s]))
(s/def ::CSSNumericBaseType
#{"length" "angle" "time" "frequency"
"resolution" "flex" "percent"})
(s/def ::CSSMathOperator
#{"sum" "product" "negate" "invert" "min" "max"})
(s/def ::CSSUnit
#{"number" "percent" "%" "em" "ex"
"ch" "ic" "rem" "lh" "rlh" "vw" "vh"
"vi" "vb" "vmin" "vmax" "cm" "mm" "Q"
"in" "pt" "pc" "px" "deg" "grad" "rad"
"turn" "s" "ms" "Hz" "kHz" "dpi" "dpcm"
"dppx" "fr"})
(s/def ::CSSUnitValue
(s/cat
:value number?
:unit ::CSSUnit))
(s/def ::CSSNumericValue
(s/or
:unit-value ::CSSUnitValue
:math-value ::CSSMathValue))
(s/def ::CSSNumberish
(s/or
:number-value number?
:numeric-value ::CSSNumericValue))
(s/def ::CSSMathSum
(s/cat
:operator #{:math/+}
:operands (s/+ ::CSSNumberish)))
(s/def ::CSSMathProduct
(s/cat
:operator #{:math/*}
:operands (s/+ ::CSSNumberish)))
(s/def ::CSSMathValue
(s/alt
:math-sum ::CSSMathSum
:math-product ::CSSMathProduct))
(s/def ::CSSPositionValue
(s/cat
:x ::CSSNumericValue
:y ::CSSNumericValue))
;; CSS Transform
(s/def ::CSSTranslate
(s/cat
:op #{:translate}
:x ::CSSNumericValue
:y ::CSSNumericValue
:z (s/? ::CSSNumericValue)))
(s/def ::CSSRotate
(s/alt
:short (s/cat
:op #{:rotate}
:angle ::CSSNumericValue)
:full (s/cat
:op #{:rotate}
:x ::CSSNumberish
:y ::CSSNumberish
:z ::CSSNumberish
:angle ::CSSNumericValue)))
(s/def ::CSSScale
(s/cat
:op #{:scale}
:x ::CSSNumberish
:y ::CSSNumberish
:z (s/? ::CSSNumberish)))
(s/def ::CSSTransformComponent
(s/alt
:translate ::CSSTranslate
:rotate ::CSSRotate
:scale ::CSSScale))
(s/def ::CSSTransformValue
(s/coll-of ::CSSTransformComponent :min-count 1))
;; ========================================
(defn -dispatch-compile-typed-css [v]
(first v))
(defmulti compile-typed-css #'-dispatch-compile-typed-css)
(defmethod compile-typed-css :unit-value
[[_ {:keys [value unit]}]]
`(js/CSSUnitValue. ~value ~unit))
(defmethod compile-typed-css :math-value
[[_ math-value]]
(compile-typed-css math-value))
(defmethod compile-typed-css :numeric-value
[[_ value]]
(compile-typed-css value))
(defmethod compile-typed-css :number-value
[[_ value]]
value)
(defmethod compile-typed-css :math-sum
[[_ {:keys [operands]}]]
`(js/CSSMathSum. ~@(map compile-typed-css operands)))
(defmethod compile-typed-css :math-product
[[_ {:keys [operands]}]]
`(js/CSSMathProduct. ~@(map compile-typed-css operands)))
(defmethod compile-typed-css :translate
[[_ {:keys [x y z]}]]
`(js/CSSTranslate. ~@(->> (filter identity [x y z])
(map compile-typed-css))))
(defmethod compile-typed-css :rotate
[[_ [_ {:keys [angle]}]]]
`(js/CSSRotate. ~(compile-typed-css angle)))
(defmethod compile-typed-css :scale
[[_ {:keys [x y z]}]]
`(js/CSSScale. ~@(->> (filter identity [x y z])
(map compile-typed-css))))
(comment
(let [ast (s/conform ::CSSUnitValue [16 "px"])]
(compile-typed-css [:unit-value ast])))