-
Notifications
You must be signed in to change notification settings - Fork 0
/
components.sh
executable file
·220 lines (201 loc) · 5.37 KB
/
components.sh
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
elements='a abbr acronym address applet area article aside audio b base basefont bdi bdo big blockquote br button canvas caption center cite code col colgroup data datalist dd del details dfn dialog dir div dl dt em embed fieldset figcaption figure font footer form frame frameset h1 h2 h3 h4 h5 h6 head header hr i iframe img input ins kbd label legend li link main map mark meter nav noframes noscript object ol optgroup output p param path picture pre progress q rp rt ruby s samp section select small source span strike strong sub summary sup svg table tbody td textarea tfoot th thead time title tr track tt u ul var video wbr'
components_dir='src/lib/components'
nochildren() {
case $1 in
img|hr|br|wbr|area|base|col|embed|input|link|param|source|track)
echo "yes"
;;
*)
echo "no"
;;
esac
}
noprops() {
case $1 in
textarea|br)
echo "yes"
;;
*)
echo "no"
;;
esac
}
generate() {
element=$1
no_children=`nochildren ${element}`
no_props=`noprops ${element}`
myprops=""
elprops=""
a11y=""
if [ "${element}" = "a" ]; then
myprops=" href: '' "
elprops="href={props.href} "
elif [ "${element}" = "iframe" ]; then
myprops=" title: '' "
elprops="title={props.title} "
elif [ "${element}" = "object" ]; then
myprops=" title: '' "
elprops="title={props.title} "
elif [ "${element}" = "area" ]; then
myprops=" alt: '' "
elprops="alt={props.alt} "
elif [ "${element}" = "img" ]; then
myprops=" alt: '' "
elprops="alt={props.alt} "
elif [ "${element}" = "video" ]; then
a11y="<!-- svelte-ignore a11y-media-has-caption -->"
elif [ "${element}" = "figcaption" ]; then
a11y="<!-- svelte-ignore a11y-structure -->"
fi
if [ "${no_children}" = "yes" -a "${no_props}" = "yes" ]; then
cat <<EOF
<script lang="ts">
import { prepareStyle } from '\$lib/utils'
import { selected, parent as storeParent } from '\$lib/store'
import { compile } from '\$lib/utils/props'
import type { Component } from '\$lib/types'
export let style = {}
export let data: Component = {
id: '',
name: '',
component: '',
props: compile({}),
style: compile({}),
children: [],
text: '',
}
function select() {
\$selected = data
\$storeParent = parent
}
</script>
<${element} style={prepareStyle(style)} on:click|stopPropagation={select} on:keypress={select} />
EOF
elif [ "${no_children}" = "yes" ]; then
cat <<EOF
<script lang="ts">
import { prepareStyle } from '\$lib/utils'
import { selected, parent as storeParent } from '\$lib/store'
import { compile } from '\$lib/utils/props'
import type { Component } from '\$lib/types'
export let style = {}
export let props = {$myprops}
export let data: Component = {
id: '',
name: '',
component: '',
props: compile({}),
style: compile({}),
children: [],
text: '',
}
function select() {
\$selected = data
\$storeParent = parent
}
</script>
<${element} {...props} $elprops style={prepareStyle(style)} on:click|stopPropagation={select} on:keypress={select} />
EOF
elif [ "${no_props}" = "yes" ]; then
cat <<EOF
<script lang="ts">
import { prepareStyle } from '\$lib/utils'
import { dragStart, dragEnd, drop } from '\$lib/utils/dnd'
import { compile } from '\$lib/utils/props'
import { selected, parent as storeParent } from '\$lib/store'
import type { Component } from '\$lib/types'
export let style = {}
export let data: Component = {
id: '',
name: '',
component: '',
props: compile({}),
style: compile({}),
children: [],
text: '',
}
export let parent: Component = {
id: '',
component: '',
name: '',
text: '',
children: [],
props: {},
style: {},
}
export let index = -1
function select() {
\$selected = data
\$storeParent = parent
}
</script>
<${element}
style={prepareStyle(style)}
draggable={true}
on:dragstart={dragStart(data, parent, index)}
on:dragend={dragEnd}
on:drop={drop(data)}
on:click|stopPropagation={select}
on:keypress={select}
>
<slot />
</${element}>
EOF
else
cat <<EOF
<script lang="ts">
import { prepareStyle } from '\$lib/utils'
import { dragStart, dragEnd, drop } from '\$lib/utils/dnd'
import { compile } from '\$lib/utils/props'
import { selected, parent as storeParent } from '\$lib/store'
import type { Component } from '\$lib/types'
export let props = {$myprops}
export let style = {}
export let data: Component = {
id: '',
name: '',
component: '',
props: compile({}),
style: compile({}),
children: [],
text: '',
}
export let parent: Component = {
id: '',
component: '',
name: '',
text: '',
children: [],
props: {},
style: {},
}
export let index = -1
function select() {
\$selected = data
\$storeParent = parent
}
</script>
$a11y
<${element}
{...props}
$elprops
style={prepareStyle(style)}
draggable={true}
on:dragstart={dragStart(data, parent, index)}
on:dragend={dragEnd}
on:drop={drop(data)}
on:click|stopPropagation={select}
on:keypress={select}
>
<slot />
</${element}>
EOF
fi
}
rm ${components_dir}/*.svelte
for element in ${elements}; do
component=`echo ${element} | awk '{print toupper(substr($0,0,1))tolower(substr($0,2))}'`
component_file="${components_dir}/${component}.svelte"
generate ${element} >"${component_file}"
done
yarn run format