@tschk/crepus-moonshine 0.3.7 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/src/render.ts +242 -221
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tschk/crepus-moonshine",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Bridge: .crepus (parsed by the Rust parser via WASM) to moonshine React elements",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"type": "module",
|
|
@@ -25,8 +25,8 @@
|
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"@tschk/crepuscularity-wasm": "^0.1.3",
|
|
28
|
-
"@tschk/moonshine": "^0.
|
|
29
|
-
"@tschk/moonshine-framework": "^0.
|
|
28
|
+
"@tschk/moonshine": "^0.4.1",
|
|
29
|
+
"@tschk/moonshine-framework": "^0.4.1"
|
|
30
30
|
},
|
|
31
31
|
"peerDependencies": {
|
|
32
32
|
"react": ">=18",
|
package/src/render.ts
CHANGED
|
@@ -31,246 +31,267 @@ function renderChildren(children: ViewNode[], keyPrefix: string): ReactNode[] {
|
|
|
31
31
|
);
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
href: node.href,
|
|
50
|
-
target: node.target,
|
|
51
|
-
rel: node.rel,
|
|
52
|
-
},
|
|
53
|
-
...renderChildren(node.children, key),
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
case "stack":
|
|
57
|
-
case "scroll":
|
|
58
|
-
return createElement(
|
|
59
|
-
"div",
|
|
60
|
-
{ key, id, className },
|
|
61
|
-
...renderChildren(node.children, key),
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
case "dropzone":
|
|
65
|
-
return createElement(
|
|
66
|
-
"div",
|
|
67
|
-
{ key, id, className, "aria-label": node.label },
|
|
68
|
-
...renderChildren(node.children, key),
|
|
69
|
-
);
|
|
70
|
-
|
|
71
|
-
case "list":
|
|
72
|
-
return createElement(
|
|
73
|
-
node.ordered ? "ol" : "ul",
|
|
74
|
-
{ key, id, className },
|
|
75
|
-
...renderChildren(node.children, key),
|
|
76
|
-
);
|
|
77
|
-
|
|
78
|
-
case "listItem":
|
|
79
|
-
return createElement(
|
|
80
|
-
"li",
|
|
81
|
-
{ key, id, className },
|
|
82
|
-
...renderChildren(node.children, key),
|
|
83
|
-
);
|
|
84
|
-
|
|
85
|
-
case "button":
|
|
86
|
-
return createElement(
|
|
87
|
-
"button",
|
|
88
|
-
{ key, id, className, type: "button" },
|
|
89
|
-
node.label,
|
|
90
|
-
);
|
|
91
|
-
|
|
92
|
-
case "badge":
|
|
93
|
-
return createElement(
|
|
94
|
-
"span",
|
|
95
|
-
{ key, id, className, "data-tone": node.tone },
|
|
96
|
-
node.label,
|
|
97
|
-
);
|
|
98
|
-
|
|
99
|
-
case "divider":
|
|
100
|
-
return createElement("hr", { key, id, className });
|
|
101
|
-
|
|
102
|
-
case "spacer":
|
|
103
|
-
return createElement("div", { key, id, className, "aria-hidden": true });
|
|
104
|
-
|
|
105
|
-
case "image":
|
|
106
|
-
return createElement("img", {
|
|
34
|
+
type NodeRenderer<T extends ViewNode["kind"]> = (
|
|
35
|
+
node: Extract<ViewNode, { kind: T }>,
|
|
36
|
+
key: string,
|
|
37
|
+
id: string | undefined,
|
|
38
|
+
className: string | undefined,
|
|
39
|
+
) => ReactNode;
|
|
40
|
+
|
|
41
|
+
const nodeRenderers: { [K in ViewNode["kind"]]: NodeRenderer<K> } = {
|
|
42
|
+
text: (node, key, id, className) =>
|
|
43
|
+
createElement("span", { key, id, className }, node.content),
|
|
44
|
+
|
|
45
|
+
link: (node, key, id, className) =>
|
|
46
|
+
createElement(
|
|
47
|
+
"a",
|
|
48
|
+
{
|
|
107
49
|
key,
|
|
108
50
|
id,
|
|
109
51
|
className,
|
|
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
|
-
|
|
52
|
+
href: node.href,
|
|
53
|
+
target: node.target,
|
|
54
|
+
rel: node.rel,
|
|
55
|
+
},
|
|
56
|
+
...renderChildren(node.children, key),
|
|
57
|
+
),
|
|
58
|
+
|
|
59
|
+
stack: (node, key, id, className) =>
|
|
60
|
+
createElement(
|
|
61
|
+
"div",
|
|
62
|
+
{ key, id, className },
|
|
63
|
+
...renderChildren(node.children, key),
|
|
64
|
+
),
|
|
65
|
+
|
|
66
|
+
scroll: (node, key, id, className) =>
|
|
67
|
+
createElement(
|
|
68
|
+
"div",
|
|
69
|
+
{ key, id, className },
|
|
70
|
+
...renderChildren(node.children, key),
|
|
71
|
+
),
|
|
72
|
+
|
|
73
|
+
dropzone: (node, key, id, className) =>
|
|
74
|
+
createElement(
|
|
75
|
+
"div",
|
|
76
|
+
{ key, id, className, "aria-label": node.label },
|
|
77
|
+
...renderChildren(node.children, key),
|
|
78
|
+
),
|
|
79
|
+
|
|
80
|
+
list: (node, key, id, className) =>
|
|
81
|
+
createElement(
|
|
82
|
+
node.ordered ? "ol" : "ul",
|
|
83
|
+
{ key, id, className },
|
|
84
|
+
...renderChildren(node.children, key),
|
|
85
|
+
),
|
|
86
|
+
|
|
87
|
+
listItem: (node, key, id, className) =>
|
|
88
|
+
createElement(
|
|
89
|
+
"li",
|
|
90
|
+
{ key, id, className },
|
|
91
|
+
...renderChildren(node.children, key),
|
|
92
|
+
),
|
|
93
|
+
|
|
94
|
+
button: (node, key, id, className) =>
|
|
95
|
+
createElement("button", { key, id, className, type: "button" }, node.label),
|
|
96
|
+
|
|
97
|
+
badge: (node, key, id, className) =>
|
|
98
|
+
createElement(
|
|
99
|
+
"span",
|
|
100
|
+
{ key, id, className, "data-tone": node.tone },
|
|
101
|
+
node.label,
|
|
102
|
+
),
|
|
103
|
+
|
|
104
|
+
divider: (node, key, id, className) =>
|
|
105
|
+
createElement("hr", { key, id, className }),
|
|
106
|
+
|
|
107
|
+
spacer: (node, key, id, className) =>
|
|
108
|
+
createElement("div", { key, id, className, "aria-hidden": true }),
|
|
109
|
+
|
|
110
|
+
image: (node, key, id, className) =>
|
|
111
|
+
createElement("img", {
|
|
112
|
+
key,
|
|
113
|
+
id,
|
|
114
|
+
className,
|
|
115
|
+
src: node.src,
|
|
116
|
+
alt: node.alt ?? "",
|
|
117
|
+
}),
|
|
118
|
+
|
|
119
|
+
webView: (node, key, id, className) =>
|
|
120
|
+
createElement("iframe", { key, id, className, src: node.src }),
|
|
121
|
+
|
|
122
|
+
toggle: (node, key, id, className) =>
|
|
123
|
+
createElement(
|
|
124
|
+
"button",
|
|
125
|
+
{
|
|
146
126
|
key,
|
|
147
127
|
id,
|
|
148
128
|
className,
|
|
149
|
-
type: "
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
129
|
+
type: "button",
|
|
130
|
+
role: "switch",
|
|
131
|
+
"aria-checked": node.checked,
|
|
132
|
+
},
|
|
133
|
+
node.label,
|
|
134
|
+
),
|
|
135
|
+
|
|
136
|
+
checkbox: (node, key, id, className) =>
|
|
137
|
+
createElement(
|
|
138
|
+
"label",
|
|
139
|
+
{ key, id, className },
|
|
140
|
+
createElement("input", {
|
|
141
|
+
key: `${key}.input`,
|
|
142
|
+
type: "checkbox",
|
|
143
|
+
defaultChecked: node.checked,
|
|
154
144
|
name: node.bind,
|
|
155
|
-
})
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
145
|
+
}),
|
|
146
|
+
node.label,
|
|
147
|
+
),
|
|
148
|
+
|
|
149
|
+
slider: (node, key, id, className) =>
|
|
150
|
+
createElement("input", {
|
|
151
|
+
key,
|
|
152
|
+
id,
|
|
153
|
+
className,
|
|
154
|
+
type: "range",
|
|
155
|
+
defaultValue: node.value,
|
|
156
|
+
min: node.min,
|
|
157
|
+
max: node.max,
|
|
158
|
+
step: node.step,
|
|
159
|
+
name: node.bind,
|
|
160
|
+
}),
|
|
161
|
+
|
|
162
|
+
progress: (node, key, id, className) =>
|
|
163
|
+
createElement("progress", {
|
|
164
|
+
key,
|
|
165
|
+
id,
|
|
166
|
+
className,
|
|
167
|
+
value: node.value,
|
|
168
|
+
max: node.max,
|
|
169
|
+
}),
|
|
170
|
+
|
|
171
|
+
meter: (node, key, id, className) =>
|
|
172
|
+
createElement("meter", {
|
|
173
|
+
key,
|
|
174
|
+
id,
|
|
175
|
+
className,
|
|
176
|
+
value: node.value,
|
|
177
|
+
min: node.min,
|
|
178
|
+
max: node.max,
|
|
179
|
+
}),
|
|
180
|
+
|
|
181
|
+
input: (node, key, id, className) =>
|
|
182
|
+
createElement(node.multiline ? "textarea" : "input", {
|
|
183
|
+
key,
|
|
184
|
+
id,
|
|
185
|
+
className,
|
|
186
|
+
type: node.multiline ? undefined : node.secure ? "password" : "text",
|
|
187
|
+
placeholder: node.placeholder,
|
|
188
|
+
name: node.bind,
|
|
189
|
+
}),
|
|
190
|
+
|
|
191
|
+
picker: (node, key, id, className) =>
|
|
192
|
+
createElement(
|
|
193
|
+
"select",
|
|
194
|
+
{ key, id, className, name: node.bind },
|
|
195
|
+
...node.options.map((option, i) =>
|
|
196
|
+
createElement(
|
|
197
|
+
"option",
|
|
198
|
+
{ key: `${key}.${i}`, value: option.value },
|
|
199
|
+
option.label,
|
|
200
|
+
),
|
|
201
|
+
),
|
|
202
|
+
),
|
|
203
|
+
|
|
204
|
+
filePicker: (node, key, id, className) =>
|
|
205
|
+
createElement(
|
|
206
|
+
"label",
|
|
207
|
+
{ key, id, className },
|
|
208
|
+
createElement("input", {
|
|
209
|
+
key: `${key}.input`,
|
|
210
|
+
type: "file",
|
|
211
|
+
accept: node.accept?.join(","),
|
|
212
|
+
multiple: node.multiple,
|
|
213
|
+
}),
|
|
214
|
+
node.label,
|
|
215
|
+
),
|
|
216
|
+
|
|
217
|
+
slotRotate: (node, key, id, className) =>
|
|
218
|
+
createElement(
|
|
219
|
+
"span",
|
|
220
|
+
{
|
|
178
221
|
key,
|
|
179
222
|
id,
|
|
180
223
|
className,
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
224
|
+
"data-crepus-slot-rotate": node.phrases.join("|"),
|
|
225
|
+
"data-interval-ms": node.intervalMs,
|
|
226
|
+
},
|
|
227
|
+
node.phrases[0] ?? "",
|
|
228
|
+
),
|
|
229
|
+
|
|
230
|
+
tabs: (node, key, id, className) =>
|
|
231
|
+
createElement(
|
|
232
|
+
"div",
|
|
233
|
+
{ key, id, className },
|
|
234
|
+
createElement(
|
|
235
|
+
"div",
|
|
236
|
+
{ key: `${key}.tablist`, role: "tablist" },
|
|
237
|
+
...node.tabs.map((tab: TabItem, i: number) =>
|
|
191
238
|
createElement(
|
|
192
|
-
"
|
|
193
|
-
{ key: `${key}.${i}`,
|
|
194
|
-
|
|
239
|
+
"button",
|
|
240
|
+
{ key: `${key}.tab.${i}`, type: "button", role: "tab" },
|
|
241
|
+
tab.label,
|
|
195
242
|
),
|
|
196
243
|
),
|
|
197
|
-
)
|
|
198
|
-
|
|
199
|
-
case "filePicker":
|
|
200
|
-
return createElement(
|
|
201
|
-
"label",
|
|
202
|
-
{ key, id, className },
|
|
203
|
-
createElement("input", {
|
|
204
|
-
key: `${key}.input`,
|
|
205
|
-
type: "file",
|
|
206
|
-
accept: node.accept?.join(","),
|
|
207
|
-
multiple: node.multiple,
|
|
208
|
-
}),
|
|
209
|
-
node.label,
|
|
210
|
-
);
|
|
211
|
-
|
|
212
|
-
case "slotRotate":
|
|
213
|
-
return createElement(
|
|
214
|
-
"span",
|
|
215
|
-
{
|
|
216
|
-
key,
|
|
217
|
-
id,
|
|
218
|
-
className,
|
|
219
|
-
"data-crepus-slot-rotate": node.phrases.join("|"),
|
|
220
|
-
"data-interval-ms": node.intervalMs,
|
|
221
|
-
},
|
|
222
|
-
node.phrases[0] ?? "",
|
|
223
|
-
);
|
|
224
|
-
|
|
225
|
-
case "tabs":
|
|
226
|
-
return createElement(
|
|
227
|
-
"div",
|
|
228
|
-
{ key, id, className },
|
|
244
|
+
),
|
|
245
|
+
...node.tabs.map((tab: TabItem, i: number) =>
|
|
229
246
|
createElement(
|
|
230
247
|
"div",
|
|
231
|
-
{ key: `${key}.
|
|
232
|
-
...
|
|
233
|
-
createElement(
|
|
234
|
-
"button",
|
|
235
|
-
{ key: `${key}.tab.${i}`, type: "button", role: "tab" },
|
|
236
|
-
tab.label,
|
|
237
|
-
),
|
|
238
|
-
),
|
|
239
|
-
),
|
|
240
|
-
...node.tabs.map((tab: TabItem, i: number) =>
|
|
241
|
-
createElement(
|
|
242
|
-
"div",
|
|
243
|
-
{ key: `${key}.panel.${i}`, role: "tabpanel" },
|
|
244
|
-
...renderChildren(tab.children, `${key}.panel.${i}`),
|
|
245
|
-
),
|
|
248
|
+
{ key: `${key}.panel.${i}`, role: "tabpanel" },
|
|
249
|
+
...renderChildren(tab.children, `${key}.panel.${i}`),
|
|
246
250
|
),
|
|
247
|
-
)
|
|
251
|
+
),
|
|
252
|
+
),
|
|
253
|
+
|
|
254
|
+
// Control flow the parser could not resolve statically. The chosen branch is
|
|
255
|
+
// rendered and the source expression kept as a data attribute.
|
|
256
|
+
if: (node, key, id, className) =>
|
|
257
|
+
createElement(
|
|
258
|
+
"div",
|
|
259
|
+
{ key, id, className, "data-crepus-if": node.condition },
|
|
260
|
+
...renderChildren(node.thenChildren, key),
|
|
261
|
+
),
|
|
262
|
+
|
|
263
|
+
forEach: (node, key, id, className) =>
|
|
264
|
+
createElement(
|
|
265
|
+
"div",
|
|
266
|
+
{
|
|
267
|
+
key,
|
|
268
|
+
id,
|
|
269
|
+
className,
|
|
270
|
+
"data-crepus-for-each": node.bind,
|
|
271
|
+
"data-crepus-item": node.itemName,
|
|
272
|
+
},
|
|
273
|
+
...renderChildren(node.itemBody, key),
|
|
274
|
+
),
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
type AnyNodeRenderer = (
|
|
278
|
+
node: ViewNode,
|
|
279
|
+
key: string,
|
|
280
|
+
id: string | undefined,
|
|
281
|
+
className: string | undefined,
|
|
282
|
+
) => ReactNode;
|
|
248
283
|
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
return createElement(
|
|
253
|
-
"div",
|
|
254
|
-
{ key, id, className, "data-crepus-if": node.condition },
|
|
255
|
-
...renderChildren(node.thenChildren, key),
|
|
256
|
-
);
|
|
284
|
+
export function renderCrepusNode(node: ViewNode, key: string): ReactNode {
|
|
285
|
+
const className = classNameOf(node);
|
|
286
|
+
const id = idOf(node);
|
|
257
287
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
id,
|
|
264
|
-
className,
|
|
265
|
-
"data-crepus-for-each": node.bind,
|
|
266
|
-
"data-crepus-item": node.itemName,
|
|
267
|
-
},
|
|
268
|
-
...renderChildren(node.itemBody, key),
|
|
269
|
-
);
|
|
270
|
-
|
|
271
|
-
default:
|
|
272
|
-
return null;
|
|
288
|
+
const renderer = nodeRenderers[node.kind] as AnyNodeRenderer;
|
|
289
|
+
if (renderer) {
|
|
290
|
+
// Cast is necessary because TypeScript cannot correlate the discriminated
|
|
291
|
+
// union `node` with the generic function retrieved from `nodeRenderers`.
|
|
292
|
+
return renderer(node, key, id, className);
|
|
273
293
|
}
|
|
294
|
+
return null;
|
|
274
295
|
}
|
|
275
296
|
|
|
276
297
|
export function renderCrepusIr(
|