deckjsx 0.8.0 → 0.8.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.
@@ -1,403 +0,0 @@
1
- //#region src/authoring/tags.ts
2
- const INTRINSIC_VIEW_TAGS = new Set([
3
- "article",
4
- "aside",
5
- "div",
6
- "figure",
7
- "footer",
8
- "header",
9
- "main",
10
- "nav",
11
- "section"
12
- ]);
13
- const INTRINSIC_TEXT_TAGS = new Set([
14
- "h1",
15
- "h2",
16
- "h3",
17
- "h4",
18
- "h5",
19
- "h6",
20
- "p"
21
- ]);
22
- function isIntrinsicViewTag(value) {
23
- return INTRINSIC_VIEW_TAGS.has(value);
24
- }
25
- function isIntrinsicTextTag(value) {
26
- return INTRINSIC_TEXT_TAGS.has(value);
27
- }
28
- function isAuthoredTag(value) {
29
- return isIntrinsicViewTag(value) || isIntrinsicTextTag(value) || value === "img" || value === "shape" || value === "span";
30
- }
31
- //#endregion
32
- //#region src/authoring/tree.ts
33
- function isRecord$1(value) {
34
- return typeof value === "object" && value !== null;
35
- }
36
- function isAuthorElementPropValue(value) {
37
- if (value === null || value === void 0 || typeof value === "string" || typeof value === "number" || typeof value === "boolean") return true;
38
- if (Array.isArray(value)) return value.every(isAuthorElementPropValue);
39
- if (typeof value !== "object" || isAuthorTreeNode(value)) return false;
40
- return Object.values(value).every(isAuthorElementPropValue);
41
- }
42
- function authorElementPropsFromEntries(entries) {
43
- const props = {};
44
- for (const [key, value] of entries) {
45
- if (!isAuthorElementPropValue(value)) throw new Error(`JSX prop "${key}" must be serializable authoring data.`);
46
- props[key] = value;
47
- }
48
- return props;
49
- }
50
- function createAuthorText(value, sourceSpan) {
51
- return {
52
- $$typeof: "deckjsx.author-tree",
53
- kind: "text",
54
- value,
55
- ...sourceSpan ? { sourceSpan } : {}
56
- };
57
- }
58
- function buildAuthorElement(input) {
59
- return {
60
- $$typeof: "deckjsx.author-tree",
61
- kind: "element",
62
- source: input.source,
63
- ...input.key !== void 0 ? { key: input.key } : {},
64
- props: input.props,
65
- children: normalizeAuthorChildren(input.children ?? []),
66
- ...input.sourceSpan ? { sourceSpan: input.sourceSpan } : {}
67
- };
68
- }
69
- function isSlideElementInput(input) {
70
- return input.source.kind === "slide";
71
- }
72
- function isViewElementInput(input) {
73
- return input.source.kind === "tag" && isIntrinsicViewTag(input.source.tag);
74
- }
75
- function isTextElementInput(input) {
76
- return input.source.kind === "tag" && isIntrinsicTextTag(input.source.tag);
77
- }
78
- function isSpanElementInput(input) {
79
- return input.source.kind === "tag" && input.source.tag === "span";
80
- }
81
- function isImageElementInput(input) {
82
- return input.source.kind === "tag" && input.source.tag === "img";
83
- }
84
- function isShapeElementInput(input) {
85
- return input.source.kind === "tag" && input.source.tag === "shape";
86
- }
87
- function createAuthorElement(input) {
88
- if (isSlideElementInput(input)) return buildAuthorElement({
89
- ...input,
90
- source: input.source,
91
- props: input.props ?? {}
92
- });
93
- if (isViewElementInput(input)) return buildAuthorElement({
94
- ...input,
95
- source: input.source,
96
- props: input.props ?? {}
97
- });
98
- if (isTextElementInput(input)) return buildAuthorElement({
99
- ...input,
100
- source: input.source,
101
- props: input.props ?? {}
102
- });
103
- if (isSpanElementInput(input)) return buildAuthorElement({
104
- ...input,
105
- source: input.source,
106
- props: input.props ?? {}
107
- });
108
- if (isImageElementInput(input)) return buildAuthorElement({
109
- ...input,
110
- source: input.source,
111
- props: input.props
112
- });
113
- if (isShapeElementInput(input)) return buildAuthorElement({
114
- ...input,
115
- source: input.source,
116
- props: input.props
117
- });
118
- throw new Error("Unsupported author element source.");
119
- }
120
- function createAuthorFragment(input) {
121
- return {
122
- $$typeof: "deckjsx.author-tree",
123
- kind: "fragment",
124
- ...input.key !== void 0 ? { key: input.key } : {},
125
- children: normalizeAuthorChildren(input.children ?? []),
126
- ...input.sourceSpan ? { sourceSpan: input.sourceSpan } : {}
127
- };
128
- }
129
- function isAuthorTreeNode(value) {
130
- return isRecord$1(value) && value.$$typeof === "deckjsx.author-tree";
131
- }
132
- function isAuthorTreeChild(value) {
133
- return value === null || value === void 0 || typeof value === "boolean" || typeof value === "string" || typeof value === "number" || isAuthorTreeNode(value) || Array.isArray(value) && value.every(isAuthorTreeChild);
134
- }
135
- function authorTreeChildrenFromUnknown(children) {
136
- return children.map((child) => {
137
- if (!isAuthorTreeChild(child)) throw new Error("JSX children must be deckjsx author tree nodes or primitive text values.");
138
- return child;
139
- });
140
- }
141
- function normalizeAuthorChildren(children) {
142
- return children.flatMap((child) => {
143
- if (child === null || child === void 0 || typeof child === "boolean") return [];
144
- if (typeof child === "string" || typeof child === "number") return [createAuthorText(child)];
145
- if (Array.isArray(child)) return normalizeAuthorChildren(child);
146
- if (isAuthorTreeNode(child)) return [child];
147
- throw new Error("JSX children must be deckjsx author tree nodes or primitive text values.");
148
- });
149
- }
150
- function collectChildren(propsObject, children) {
151
- if (children.length === 0) return propsObject.children;
152
- if (children.length === 1) return children[0];
153
- return children;
154
- }
155
- //#endregion
156
- //#region src/authoring/author-node.ts
157
- function textAuthorNode(props, children) {
158
- return {
159
- $$typeof: "deckjsx.author-node",
160
- kind: "text",
161
- props,
162
- children
163
- };
164
- }
165
- function textFromPrimitive(value) {
166
- if (typeof value === "string" && value.trim().length === 0) return null;
167
- const text = typeof value === "string" && /[\n\r\t]/.test(value) ? value.trim() : String(value);
168
- if (text.length === 0) return null;
169
- return textAuthorNode({}, [text]);
170
- }
171
- function convertContentChildren(children) {
172
- const converted = [];
173
- for (const child of children) {
174
- if (child.kind === "fragment") {
175
- converted.push(...convertContentChildren(child.children));
176
- continue;
177
- }
178
- if (child.kind === "text") {
179
- const textNode = textFromPrimitive(child.value);
180
- if (textNode) converted.push(textNode);
181
- continue;
182
- }
183
- converted.push(toAuthorNode(child));
184
- }
185
- return converted;
186
- }
187
- function convertTextChildren(children) {
188
- const converted = [];
189
- for (const child of children) {
190
- if (child.kind === "fragment") {
191
- converted.push(...convertTextChildren(child.children));
192
- continue;
193
- }
194
- if (child.kind === "text") {
195
- converted.push(child.value);
196
- continue;
197
- }
198
- const node = toAuthorNode(child);
199
- if (node.kind === "text") converted.push(node);
200
- }
201
- return converted;
202
- }
203
- function slideAuthorNode(props, children) {
204
- return {
205
- $$typeof: "deckjsx.author-node",
206
- kind: "slide",
207
- props,
208
- children
209
- };
210
- }
211
- function viewAuthorNode(props, children) {
212
- return {
213
- $$typeof: "deckjsx.author-node",
214
- kind: "view",
215
- props,
216
- children
217
- };
218
- }
219
- function imageAuthorNode(props) {
220
- return {
221
- $$typeof: "deckjsx.author-node",
222
- kind: "image",
223
- props,
224
- children: []
225
- };
226
- }
227
- function shapeAuthorNode(props) {
228
- return {
229
- $$typeof: "deckjsx.author-node",
230
- kind: "shape",
231
- props,
232
- children: []
233
- };
234
- }
235
- function isSlideElement(node) {
236
- return node.source.kind === "slide";
237
- }
238
- function isViewElement(node) {
239
- return node.source.kind === "tag" && isIntrinsicViewTag(node.source.tag);
240
- }
241
- function isTextElement(node) {
242
- return node.source.kind === "tag" && isIntrinsicTextTag(node.source.tag);
243
- }
244
- function isSpanElement(node) {
245
- return node.source.kind === "tag" && node.source.tag === "span";
246
- }
247
- function isImageElement(node) {
248
- return node.source.kind === "tag" && node.source.tag === "img";
249
- }
250
- function isShapeElement(node) {
251
- return node.source.kind === "tag" && node.source.tag === "shape";
252
- }
253
- function toAuthorNode(node) {
254
- if (isSlideElement(node)) return slideAuthorNode(node.props, convertContentChildren(node.children));
255
- if (isViewElement(node)) return viewAuthorNode(node.props, convertContentChildren(node.children));
256
- if (isTextElement(node)) return textAuthorNode(node.props, convertTextChildren(node.children));
257
- if (isSpanElement(node)) return textAuthorNode(node.props, convertTextChildren(node.children));
258
- if (isImageElement(node)) return imageAuthorNode(node.props);
259
- if (isShapeElement(node)) return shapeAuthorNode(node.props);
260
- throw new Error("Unsupported author element node.");
261
- }
262
- function toAuthorJsxNode(value) {
263
- if (isAuthorNodeValue(value)) return value;
264
- if (Array.isArray(value)) return value.map((item) => toAuthorJsxNode(item));
265
- if (!isAuthorTreeNode(value)) {
266
- if (typeof value === "string" || typeof value === "number") return textFromPrimitive(value) ?? void 0;
267
- if (value === null || value === void 0 || typeof value === "boolean") return value;
268
- throw new Error("JSX content must be deckjsx author tree nodes or primitive text values.");
269
- }
270
- if (value.kind === "fragment") return value.children.map((child) => toAuthorJsxNode(child));
271
- if (value.kind === "text") return textFromPrimitive(value.value) ?? void 0;
272
- return toAuthorNode(value);
273
- }
274
- function isAuthorNodeValue(value) {
275
- return typeof value === "object" && value !== null && "$$typeof" in value && value.$$typeof === "deckjsx.author-node";
276
- }
277
- //#endregion
278
- //#region src/jsx.ts
279
- function isRecord(value) {
280
- return typeof value === "object" && value !== null;
281
- }
282
- function propsRecordWithoutChildren(props) {
283
- return authorElementPropsFromEntries(Object.entries(props).filter(([key]) => key !== "children"));
284
- }
285
- function splitProps(props, children) {
286
- const rawChildren = collectChildren(props, children);
287
- return {
288
- props: propsRecordWithoutChildren(props),
289
- children: rawChildren === void 0 ? [] : [rawChildren]
290
- };
291
- }
292
- function intrinsicElement(type, propsObject, children, key, sourceSpan) {
293
- if (isIntrinsicViewTag(type)) {
294
- const authored = splitProps(propsObject, children);
295
- return createAuthorElement({
296
- source: {
297
- kind: "tag",
298
- tag: type
299
- },
300
- props: authored.props,
301
- children: authored.children,
302
- ...key !== void 0 ? { key } : {},
303
- ...sourceSpan ? { sourceSpan } : {}
304
- });
305
- }
306
- if (isIntrinsicTextTag(type)) {
307
- const authored = splitProps(propsObject, children);
308
- return createAuthorElement({
309
- source: {
310
- kind: "tag",
311
- tag: type
312
- },
313
- props: authored.props,
314
- children: authored.children,
315
- ...key !== void 0 ? { key } : {},
316
- ...sourceSpan ? { sourceSpan } : {}
317
- });
318
- }
319
- if (type === "span") {
320
- const authored = splitProps(propsObject, children);
321
- return createAuthorElement({
322
- source: {
323
- kind: "tag",
324
- tag: type
325
- },
326
- props: authored.props,
327
- children: authored.children,
328
- ...key !== void 0 ? { key } : {},
329
- ...sourceSpan ? { sourceSpan } : {}
330
- });
331
- }
332
- if (type === "img") {
333
- const authored = splitProps(propsObject, children);
334
- return createAuthorElement({
335
- source: {
336
- kind: "tag",
337
- tag: type
338
- },
339
- props: authored.props,
340
- children: authored.children,
341
- ...key !== void 0 ? { key } : {},
342
- ...sourceSpan ? { sourceSpan } : {}
343
- });
344
- }
345
- const authored = splitProps(propsObject, children);
346
- return createAuthorElement({
347
- source: {
348
- kind: "tag",
349
- tag: type
350
- },
351
- props: authored.props,
352
- children: authored.children,
353
- ...key !== void 0 ? { key } : {},
354
- ...sourceSpan ? { sourceSpan } : {}
355
- });
356
- }
357
- function createElementWithMetadata(type, props, key, sourceSpan, children = []) {
358
- if (typeof type === "string") {
359
- if (isIntrinsicViewTag(type) || isIntrinsicTextTag(type) || type === "img" || type === "shape" || type === "span") return intrinsicElement(type, isRecord(props) ? props : {}, children, key, sourceSpan);
360
- if (!isAuthoredTag(type)) throw new Error(`Intrinsic element is not supported: <${type}>.`);
361
- }
362
- if (typeof type !== "function") throw new Error("JSX element type must be a function component.");
363
- if (type === Fragment) {
364
- const rawChildren = collectChildren(isRecord(props) ? props : {}, children);
365
- return createAuthorFragment({
366
- children: rawChildren === void 0 ? [] : [rawChildren],
367
- ...key !== void 0 ? { key } : {},
368
- ...sourceSpan ? { sourceSpan } : {}
369
- });
370
- }
371
- const propsObject = isRecord(props) ? props : {};
372
- const rawChildren = collectChildren(propsObject, children);
373
- const result = type({
374
- ...propsObject,
375
- children: rawChildren
376
- });
377
- if (!isAuthorTreeNode(result)) throw new Error("Function components must return a deckjsx author tree node.");
378
- if (key === void 0 && sourceSpan === void 0) return result;
379
- if (result.kind === "element") return {
380
- ...result,
381
- ...key !== void 0 ? { key } : {},
382
- ...sourceSpan ? { sourceSpan } : {}
383
- };
384
- return result;
385
- }
386
- function Fragment(props) {
387
- return createAuthorFragment({ children: props.children === void 0 ? [] : authorTreeChildrenFromUnknown([props.children]) });
388
- }
389
- function isAuthorNodeKind(value) {
390
- return value === "slide" || value === "view" || value === "text" || value === "image" || value === "shape";
391
- }
392
- function isAuthorNode(value) {
393
- if (!isAuthorNodeValue(value)) return false;
394
- return isAuthorNodeKind(value.kind);
395
- }
396
- function isSlideNode(value) {
397
- return isAuthorNode(value) && value.kind === "slide";
398
- }
399
- function isContentNode(value) {
400
- return isAuthorNode(value) && value.kind !== "slide";
401
- }
402
- //#endregion
403
- export { isSlideNode as a, authorElementPropsFromEntries as c, isAuthorTreeNode as d, isAuthoredTag as f, isContentNode as i, createAuthorElement as l, createElementWithMetadata as n, isAuthorNodeValue as o, isAuthorNode as r, toAuthorJsxNode as s, Fragment as t, isAuthorTreeChild as u };