@player-tools/dsl 0.5.0--canary.62.1176 → 0.5.0-next.0

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.
Files changed (52) hide show
  1. package/dist/cjs/index.cjs +973 -0
  2. package/dist/cjs/index.cjs.map +1 -0
  3. package/dist/index.legacy-esm.js +910 -0
  4. package/dist/index.mjs +910 -0
  5. package/dist/index.mjs.map +1 -0
  6. package/package.json +27 -33
  7. package/src/__tests__/asset-api.test.tsx +354 -0
  8. package/src/__tests__/edge-cases.test.tsx +81 -0
  9. package/src/__tests__/helpers/asset-library.tsx +371 -0
  10. package/src/__tests__/helpers/mock-data-refs.ts +21 -0
  11. package/src/__tests__/json.test.ts +12 -0
  12. package/src/__tests__/jsx.test.tsx +138 -0
  13. package/src/__tests__/schema.test.tsx +378 -0
  14. package/src/__tests__/switch.test.tsx +251 -0
  15. package/src/__tests__/template.test.tsx +294 -0
  16. package/src/__tests__/view-api.test.tsx +46 -0
  17. package/src/auto-id.tsx +10 -10
  18. package/src/compiler/__tests__/compiler.test.tsx +264 -0
  19. package/src/compiler/__tests__/schema.test.ts +127 -0
  20. package/src/compiler/compiler.ts +50 -46
  21. package/src/compiler/index.ts +3 -3
  22. package/src/compiler/schema.ts +29 -25
  23. package/src/compiler/types.ts +15 -12
  24. package/src/compiler/utils.ts +7 -7
  25. package/src/components.tsx +17 -12
  26. package/src/index.ts +11 -11
  27. package/src/string-templates/__tests__/binding.test.ts +87 -0
  28. package/src/string-templates/__tests__/edge-cases.test.ts +6 -0
  29. package/src/string-templates/__tests__/expression.test.ts +9 -0
  30. package/src/string-templates/__tests__/react.test.tsx +75 -0
  31. package/src/string-templates/index.ts +40 -22
  32. package/src/switch.tsx +12 -12
  33. package/src/template.tsx +38 -34
  34. package/src/types.ts +5 -5
  35. package/src/utils.tsx +8 -8
  36. package/types/auto-id.d.ts +33 -0
  37. package/types/compiler/compiler.d.ts +27 -0
  38. package/types/compiler/index.d.ts +4 -0
  39. package/types/compiler/schema.d.ts +61 -0
  40. package/types/compiler/types.d.ts +55 -0
  41. package/types/compiler/utils.d.ts +4 -0
  42. package/types/components.d.ts +90 -0
  43. package/types/index.d.ts +12 -0
  44. package/types/string-templates/index.d.ts +50 -0
  45. package/types/switch.d.ts +19 -0
  46. package/types/template.d.ts +20 -0
  47. package/types/types.d.ts +42 -0
  48. package/types/utils.d.ts +33 -0
  49. package/README.md +0 -9
  50. package/dist/index.cjs.js +0 -990
  51. package/dist/index.d.ts +0 -404
  52. package/dist/index.esm.js +0 -923
package/dist/index.esm.js DELETED
@@ -1,923 +0,0 @@
1
- import * as React from 'react';
2
- import React__default from 'react';
3
- import mergeRefs from 'react-merge-refs';
4
- import { flattenNodes, ProxyNode, createPortal, PropertyNode, ValueNode, ArrayNode, toJSON, render } from 'react-json-reconciler';
5
- export * from 'react-json-reconciler';
6
- import { parseExpression } from '@player-ui/player';
7
- import { dequal } from 'dequal';
8
- import { SyncWaterfallHook, SyncHook, AsyncSeriesWaterfallHook, AsyncSeriesHook } from 'tapable-ts';
9
- import { SourceMapGenerator, SourceMapConsumer } from 'source-map-js';
10
-
11
- const IDSuffixContext = React__default.createContext("root");
12
- const IndexSuffixStopContext = React__default.createContext(false);
13
- const useGetIdPrefix = () => {
14
- return React__default.useContext(IDSuffixContext);
15
- };
16
- const IDSuffixProvider = (props) => {
17
- const currentPrefix = useGetIdPrefix();
18
- return /* @__PURE__ */ React__default.createElement(IDSuffixContext.Provider, {
19
- value: [
20
- currentPrefix === "root" ? void 0 : currentPrefix,
21
- props.suffix
22
- ].filter(Boolean).join("-")
23
- }, props.children);
24
- };
25
- const IDProvider = (props) => {
26
- if (props.id) {
27
- return /* @__PURE__ */ React__default.createElement(IDSuffixContext.Provider, {
28
- value: props.id
29
- }, props.children);
30
- }
31
- return /* @__PURE__ */ React__default.createElement(React__default.Fragment, null, props.children);
32
- };
33
- const useIndexInSlot = (ref) => {
34
- const [index, setIndex] = React__default.useState(-1);
35
- const slotContext = React__default.useContext(SlotContext);
36
- React__default.useEffect(() => {
37
- var _a, _b;
38
- if (!(slotContext == null ? void 0 : slotContext.isArray)) {
39
- throw new Error("Cannot get index in non-array slot");
40
- }
41
- if (ref.current && ((_b = (_a = slotContext == null ? void 0 : slotContext.ref.current) == null ? void 0 : _a.valueNode) == null ? void 0 : _b.type) === "array") {
42
- const allChildren = flattenNodes(slotContext.ref.current.valueNode.children);
43
- const foundIndex = allChildren.indexOf(ref.current);
44
- if (foundIndex !== index) {
45
- setIndex(foundIndex);
46
- }
47
- }
48
- }, [index, ref, slotContext == null ? void 0 : slotContext.isArray, slotContext == null ? void 0 : slotContext.ref]);
49
- return index;
50
- };
51
- const IDSuffixIndexProvider = (props) => {
52
- var _a;
53
- const slotIndex = useIndexInSlot(props.wrapperRef);
54
- const stopIndex = React__default.useContext(IndexSuffixStopContext);
55
- if (stopIndex) {
56
- return /* @__PURE__ */ React__default.createElement(React__default.Fragment, null, props.children);
57
- }
58
- return /* @__PURE__ */ React__default.createElement(IDSuffixProvider, {
59
- suffix: (_a = props.templateIndex) != null ? _a : String(slotIndex)
60
- }, /* @__PURE__ */ React__default.createElement(IndexSuffixStopContext.Provider, {
61
- value: true
62
- }, props.children));
63
- };
64
- const OptionalIDSuffixProvider = (props) => {
65
- const slotContext = React__default.useContext(SlotContext);
66
- if (slotContext == null ? void 0 : slotContext.isArray) {
67
- return /* @__PURE__ */ React__default.createElement(IDSuffixIndexProvider, {
68
- wrapperRef: props.wrapperRef,
69
- templateIndex: props.templateIndex
70
- }, props.children);
71
- }
72
- return /* @__PURE__ */ React__default.createElement(React__default.Fragment, null, props.children);
73
- };
74
-
75
- var __defProp$5 = Object.defineProperty;
76
- var __defProps$4 = Object.defineProperties;
77
- var __getOwnPropDescs$4 = Object.getOwnPropertyDescriptors;
78
- var __getOwnPropSymbols$5 = Object.getOwnPropertySymbols;
79
- var __hasOwnProp$5 = Object.prototype.hasOwnProperty;
80
- var __propIsEnum$5 = Object.prototype.propertyIsEnumerable;
81
- var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
82
- var __spreadValues$5 = (a, b) => {
83
- for (var prop in b || (b = {}))
84
- if (__hasOwnProp$5.call(b, prop))
85
- __defNormalProp$5(a, prop, b[prop]);
86
- if (__getOwnPropSymbols$5)
87
- for (var prop of __getOwnPropSymbols$5(b)) {
88
- if (__propIsEnum$5.call(b, prop))
89
- __defNormalProp$5(a, prop, b[prop]);
90
- }
91
- return a;
92
- };
93
- var __spreadProps$4 = (a, b) => __defProps$4(a, __getOwnPropDescs$4(b));
94
- const OpaqueIdentifier = Symbol("TemplateStringType");
95
- const TemplateStringComponent = (props) => {
96
- return React.createElement("value", {
97
- value: props.value
98
- }, null);
99
- };
100
- const createTemplateInstance = (options) => {
101
- const value = options.strings.reduce((sum, next, i) => {
102
- var _a;
103
- const element2 = options.other[i];
104
- if (typeof element2 === "string") {
105
- return sum + next + element2;
106
- }
107
- return sum + next + ((_a = element2 == null ? void 0 : element2.toRefString(options)) != null ? _a : "");
108
- }, "");
109
- if (options.nestedContext === "expression") {
110
- const parsedExpression = parseExpression(value, { strict: false });
111
- if (parsedExpression.error) {
112
- throw parsedExpression.error;
113
- }
114
- }
115
- const toString = () => {
116
- return options.toRefString({}, value);
117
- };
118
- const toValue = () => {
119
- return value;
120
- };
121
- const element = React.createElement(TemplateStringComponent, {
122
- value: toString()
123
- }, null);
124
- return __spreadProps$4(__spreadValues$5({}, element), {
125
- [OpaqueIdentifier]: true,
126
- toString,
127
- toValue,
128
- toRefString: (refStringOptions) => {
129
- return options.toRefString(refStringOptions, value);
130
- }
131
- });
132
- };
133
- const createBindingTemplateInstance = (options) => {
134
- const templateInstance = createTemplateInstance(__spreadProps$4(__spreadValues$5({}, options), {
135
- toRefString: (context, value) => {
136
- return `{{${value}}}`;
137
- }
138
- }));
139
- templateInstance.__type = "binding";
140
- return templateInstance;
141
- };
142
- const createExpressionTemplateInstance = (options) => {
143
- const templateInstance = createTemplateInstance(__spreadProps$4(__spreadValues$5({}, options), {
144
- toRefString: (contextOptions, value) => {
145
- if ((contextOptions == null ? void 0 : contextOptions.nestedContext) === "expression") {
146
- return value;
147
- }
148
- const inBinding = (contextOptions == null ? void 0 : contextOptions.nestedContext) === "binding";
149
- return `${inBinding ? "`" : "@["}${value}${inBinding ? "`" : "]@"}`;
150
- }
151
- }));
152
- templateInstance.__type = "expression";
153
- return templateInstance;
154
- };
155
- const binding = (strings, ...nested) => {
156
- return createBindingTemplateInstance({
157
- strings,
158
- other: nested,
159
- nestedContext: "binding"
160
- });
161
- };
162
- const expression = (strings, ...nested) => {
163
- return createExpressionTemplateInstance({
164
- strings,
165
- other: nested,
166
- nestedContext: "expression"
167
- });
168
- };
169
- const isTemplateStringInstance = (val) => {
170
- return val !== null && typeof val === "object" && val[OpaqueIdentifier] === true;
171
- };
172
-
173
- var __defProp$4 = Object.defineProperty;
174
- var __defProps$3 = Object.defineProperties;
175
- var __getOwnPropDescs$3 = Object.getOwnPropertyDescriptors;
176
- var __getOwnPropSymbols$4 = Object.getOwnPropertySymbols;
177
- var __hasOwnProp$4 = Object.prototype.hasOwnProperty;
178
- var __propIsEnum$4 = Object.prototype.propertyIsEnumerable;
179
- var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
180
- var __spreadValues$4 = (a, b) => {
181
- for (var prop in b || (b = {}))
182
- if (__hasOwnProp$4.call(b, prop))
183
- __defNormalProp$4(a, prop, b[prop]);
184
- if (__getOwnPropSymbols$4)
185
- for (var prop of __getOwnPropSymbols$4(b)) {
186
- if (__propIsEnum$4.call(b, prop))
187
- __defNormalProp$4(a, prop, b[prop]);
188
- }
189
- return a;
190
- };
191
- var __spreadProps$3 = (a, b) => __defProps$3(a, __getOwnPropDescs$3(b));
192
- function toArray(val) {
193
- return Array.isArray(val) ? val : [val];
194
- }
195
- function toJsonElement(value, indexOrKey, options) {
196
- var _a;
197
- const indexProp = typeof indexOrKey === "number" ? { key: indexOrKey } : null;
198
- if (Array.isArray(value)) {
199
- return /* @__PURE__ */ React.createElement("array", __spreadValues$4({}, indexProp), value.map((item, idx) => toJsonElement(item, idx, options)));
200
- }
201
- if (isTemplateStringInstance(value)) {
202
- if (typeof indexOrKey === "string" && ((_a = options == null ? void 0 : options.propertiesToSkip) == null ? void 0 : _a.includes(indexOrKey))) {
203
- return /* @__PURE__ */ React.createElement("value", __spreadValues$4({}, indexProp), value.toValue());
204
- }
205
- return /* @__PURE__ */ React.createElement("value", __spreadValues$4({}, indexProp), value.toRefString());
206
- }
207
- if (typeof value === "object" && value !== null) {
208
- return /* @__PURE__ */ React.createElement("obj", __spreadValues$4({}, indexProp), Object.keys(value).map((key) => /* @__PURE__ */ React.createElement("property", {
209
- key,
210
- name: key
211
- }, toJsonElement(value[key], key, options))));
212
- }
213
- return /* @__PURE__ */ React.createElement("value", __spreadProps$3(__spreadValues$4({}, indexProp), {
214
- value
215
- }));
216
- }
217
- function toJsonProperties(value, options = { propertiesToSkip: ["applicability"] }) {
218
- return Object.keys(value).map((key) => {
219
- return /* @__PURE__ */ React.createElement("property", {
220
- key,
221
- name: key
222
- }, toJsonElement(value[key], key, options));
223
- });
224
- }
225
- function normalizeText(options) {
226
- const { node, TextComp } = options;
227
- const nodeArr = React.Children.toArray(node);
228
- if (nodeArr.every((n) => React.isValidElement(n) && n.type !== TemplateStringComponent)) {
229
- return node;
230
- }
231
- if (TextComp) {
232
- return /* @__PURE__ */ React.createElement(TextComp, null, nodeArr);
233
- }
234
- throw new Error(`Tried to convert node to Text Asset, but no Component was supplied.`);
235
- }
236
- function normalizeToCollection(options) {
237
- const { node, CollectionComp } = options;
238
- if (React.Children.count(node) > 1 && React.Children.toArray(node).every((n) => typeof n !== "string")) {
239
- if (!CollectionComp) {
240
- throw new Error(`Tried to convert array to a collection asset, but no Component was given.`);
241
- }
242
- return /* @__PURE__ */ React.createElement(CollectionComp, null, node);
243
- }
244
- return normalizeText(__spreadProps$3(__spreadValues$4({}, options), { node }));
245
- }
246
- function flattenChildren(children) {
247
- const childrenArray = React.Children.toArray(children);
248
- return childrenArray.reduce((flatChildren, child) => {
249
- if (child.type === React.Fragment) {
250
- return flatChildren.concat(flattenChildren(child.props.children));
251
- }
252
- flatChildren.push(child);
253
- return flatChildren;
254
- }, []);
255
- }
256
-
257
- var __defProp$3 = Object.defineProperty;
258
- var __defProps$2 = Object.defineProperties;
259
- var __getOwnPropDescs$2 = Object.getOwnPropertyDescriptors;
260
- var __getOwnPropSymbols$3 = Object.getOwnPropertySymbols;
261
- var __hasOwnProp$3 = Object.prototype.hasOwnProperty;
262
- var __propIsEnum$3 = Object.prototype.propertyIsEnumerable;
263
- var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
264
- var __spreadValues$3 = (a, b) => {
265
- for (var prop in b || (b = {}))
266
- if (__hasOwnProp$3.call(b, prop))
267
- __defNormalProp$3(a, prop, b[prop]);
268
- if (__getOwnPropSymbols$3)
269
- for (var prop of __getOwnPropSymbols$3(b)) {
270
- if (__propIsEnum$3.call(b, prop))
271
- __defNormalProp$3(a, prop, b[prop]);
272
- }
273
- return a;
274
- };
275
- var __spreadProps$2 = (a, b) => __defProps$2(a, __getOwnPropDescs$2(b));
276
- var __objRest = (source, exclude) => {
277
- var target = {};
278
- for (var prop in source)
279
- if (__hasOwnProp$3.call(source, prop) && exclude.indexOf(prop) < 0)
280
- target[prop] = source[prop];
281
- if (source != null && __getOwnPropSymbols$3)
282
- for (var prop of __getOwnPropSymbols$3(source)) {
283
- if (exclude.indexOf(prop) < 0 && __propIsEnum$3.call(source, prop))
284
- target[prop] = source[prop];
285
- }
286
- return target;
287
- };
288
- const SlotContext = React__default.createContext(void 0);
289
- const AssetWrapper = React__default.forwardRef((props, ref) => {
290
- const _a = props, { children } = _a, rest = __objRest(_a, ["children"]);
291
- return /* @__PURE__ */ React__default.createElement("obj", {
292
- ref
293
- }, toJsonProperties(rest), /* @__PURE__ */ React__default.createElement("property", {
294
- name: "asset"
295
- }, children));
296
- });
297
- const GeneratedIDProperty = (props) => {
298
- var _a;
299
- const currentPrefixId = useGetIdPrefix();
300
- return /* @__PURE__ */ React__default.createElement("property", {
301
- name: "id"
302
- }, (_a = props.id) != null ? _a : currentPrefixId);
303
- };
304
- const Asset = React__default.forwardRef((props, ref) => {
305
- const _a = props, { id, type, applicability, children } = _a, rest = __objRest(_a, ["id", "type", "applicability", "children"]);
306
- const slotContext = React__default.useContext(SlotContext);
307
- const localRef = React__default.useRef(null);
308
- const Wrapper = (slotContext == null ? void 0 : slotContext.wrapInAsset) ? AssetWrapper : React__default.Fragment;
309
- return /* @__PURE__ */ React__default.createElement(Wrapper, __spreadValues$3({
310
- ref: (slotContext == null ? void 0 : slotContext.wrapInAsset) ? mergeRefs([ref, localRef]) : void 0
311
- }, (slotContext == null ? void 0 : slotContext.wrapInAsset) && (slotContext == null ? void 0 : slotContext.additionalProperties) ? slotContext == null ? void 0 : slotContext.additionalProperties : {}), /* @__PURE__ */ React__default.createElement(OptionalIDSuffixProvider, {
312
- wrapperRef: localRef
313
- }, /* @__PURE__ */ React__default.createElement(SlotContext.Provider, {
314
- value: void 0
315
- }, /* @__PURE__ */ React__default.createElement(IDProvider, {
316
- id
317
- }, /* @__PURE__ */ React__default.createElement("obj", {
318
- ref: (slotContext == null ? void 0 : slotContext.wrapInAsset) ? void 0 : mergeRefs([ref, localRef])
319
- }, /* @__PURE__ */ React__default.createElement(GeneratedIDProperty, {
320
- id
321
- }), /* @__PURE__ */ React__default.createElement("property", {
322
- name: "type"
323
- }, type), applicability !== void 0 && /* @__PURE__ */ React__default.createElement("property", {
324
- name: "applicability"
325
- }, /* @__PURE__ */ React__default.createElement("value", {
326
- value: typeof applicability === "boolean" ? applicability : applicability.toValue()
327
- })), toJsonProperties(rest), children)))));
328
- });
329
- Asset.defaultProps = {
330
- id: void 0,
331
- children: void 0
332
- };
333
- const View = React__default.forwardRef((props, ref) => {
334
- const _a = props, { validation, children } = _a, rest = __objRest(_a, ["validation", "children"]);
335
- return /* @__PURE__ */ React__default.createElement(Asset, __spreadValues$3({
336
- ref
337
- }, rest), validation && /* @__PURE__ */ React__default.createElement("property", {
338
- key: "validation",
339
- name: "validation"
340
- }, toJsonElement(validation, "validation", {
341
- propertiesToSkip: ["ref"]
342
- })), children);
343
- });
344
- View.defaultProps = {
345
- id: void 0,
346
- children: void 0
347
- };
348
- const Slot = (props) => {
349
- var _a, _b;
350
- const { TextComp, CollectionComp } = props;
351
- const children = flattenChildren(props.children);
352
- const propRef = React__default.useRef(null);
353
- return /* @__PURE__ */ React__default.createElement("property", {
354
- ref: propRef,
355
- name: props.name
356
- }, /* @__PURE__ */ React__default.createElement(IDSuffixProvider, {
357
- suffix: props.name
358
- }, /* @__PURE__ */ React__default.createElement(IndexSuffixStopContext.Provider, {
359
- value: false
360
- }, /* @__PURE__ */ React__default.createElement(SlotContext.Provider, {
361
- value: {
362
- ref: propRef,
363
- propertyName: props.name,
364
- wrapInAsset: (_a = props.wrapInAsset) != null ? _a : false,
365
- isArray: (_b = props.isArray) != null ? _b : false,
366
- additionalProperties: props.additionalProperties,
367
- TextComp,
368
- CollectionComp
369
- }
370
- }, props.isArray && /* @__PURE__ */ React__default.createElement("array", null, React__default.Children.map(children, (child, index) => {
371
- return /* @__PURE__ */ React__default.createElement(React__default.Fragment, {
372
- key: `${props.name}-${index}`
373
- }, normalizeText({ node: child, TextComp }));
374
- })), !props.isArray && normalizeToCollection({
375
- node: children,
376
- TextComp,
377
- CollectionComp
378
- })))));
379
- };
380
- function createSlot(options) {
381
- return (props) => {
382
- const _a = props, { children } = _a, other = __objRest(_a, ["children"]);
383
- return /* @__PURE__ */ React__default.createElement(Slot, __spreadProps$2(__spreadValues$3({}, options), {
384
- additionalProperties: other
385
- }), children);
386
- };
387
- }
388
-
389
- var __defProp$2 = Object.defineProperty;
390
- var __defProps$1 = Object.defineProperties;
391
- var __getOwnPropDescs$1 = Object.getOwnPropertyDescriptors;
392
- var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
393
- var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
394
- var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
395
- var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
396
- var __spreadValues$2 = (a, b) => {
397
- for (var prop in b || (b = {}))
398
- if (__hasOwnProp$2.call(b, prop))
399
- __defNormalProp$2(a, prop, b[prop]);
400
- if (__getOwnPropSymbols$2)
401
- for (var prop of __getOwnPropSymbols$2(b)) {
402
- if (__propIsEnum$2.call(b, prop))
403
- __defNormalProp$2(a, prop, b[prop]);
404
- }
405
- return a;
406
- };
407
- var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b));
408
- const SwitchContext = React__default.createContext({});
409
- const Switch = (props) => {
410
- const slotContext = React__default.useContext(SlotContext);
411
- const propertyNode = React__default.useRef(null);
412
- return /* @__PURE__ */ React__default.createElement("obj", {
413
- ref: propertyNode
414
- }, /* @__PURE__ */ React__default.createElement(SwitchContext.Provider, {
415
- value: __spreadProps$1(__spreadValues$2({}, props), {
416
- TextComp: slotContext == null ? void 0 : slotContext.TextComp,
417
- CollectionComp: slotContext == null ? void 0 : slotContext.CollectionComp
418
- })
419
- }, /* @__PURE__ */ React__default.createElement(OptionalIDSuffixProvider, {
420
- wrapperRef: propertyNode
421
- }, /* @__PURE__ */ React__default.createElement("property", {
422
- name: props.isDynamic ? "dynamicSwitch" : "staticSwitch"
423
- }, /* @__PURE__ */ React__default.createElement(SlotContext.Provider, {
424
- value: void 0
425
- }, /* @__PURE__ */ React__default.createElement("array", null, props.children))))), (slotContext == null ? void 0 : slotContext.additionalProperties) && toJsonProperties(slotContext.additionalProperties));
426
- };
427
- const findParentArray = (node) => {
428
- if (node.type === "array") {
429
- return node;
430
- }
431
- if (node.parent) {
432
- return findParentArray(node.parent);
433
- }
434
- throw new Error("can't find parent array");
435
- };
436
- const findArrayIndex = (node) => {
437
- const parentArray = findParentArray(node);
438
- const allSearch = flattenNodes(parentArray.children);
439
- return allSearch.indexOf(node);
440
- };
441
- const Case = (props) => {
442
- const slotContext = React__default.useContext(SlotContext);
443
- const switchContext = React__default.useContext(SwitchContext);
444
- const [caseIndex, setCaseIndex] = React__default.useState(-1);
445
- const caseNode = React__default.useRef(null);
446
- React__default.useLayoutEffect(() => {
447
- if (caseNode.current) {
448
- const index = findArrayIndex(caseNode.current);
449
- if (index !== caseIndex) {
450
- setCaseIndex(index);
451
- }
452
- }
453
- }, [caseIndex]);
454
- let expValue = true;
455
- if (props.exp !== void 0) {
456
- expValue = isTemplateStringInstance(props.exp) ? props.exp.toValue() : props.exp;
457
- }
458
- return /* @__PURE__ */ React__default.createElement("obj", {
459
- ref: caseNode
460
- }, /* @__PURE__ */ React__default.createElement("property", {
461
- name: "case"
462
- }, /* @__PURE__ */ React__default.createElement("value", {
463
- value: expValue
464
- })), /* @__PURE__ */ React__default.createElement(IDSuffixProvider, {
465
- suffix: `${switchContext.isDynamic ? "dynamicSwitch" : "staticSwitch"}-${caseIndex}`
466
- }, /* @__PURE__ */ React__default.createElement(SlotContext.Provider, {
467
- value: slotContext ? __spreadProps$1(__spreadValues$2({}, slotContext), { wrapInAsset: false }) : void 0
468
- }, /* @__PURE__ */ React__default.createElement("property", {
469
- name: "asset"
470
- }, normalizeToCollection({
471
- node: props.children,
472
- TextComp: switchContext == null ? void 0 : switchContext.TextComp,
473
- CollectionComp: switchContext == null ? void 0 : switchContext.CollectionComp
474
- })))));
475
- };
476
- Switch.Case = Case;
477
-
478
- const TemplateContext = React__default.createContext({
479
- depth: 0
480
- });
481
- function addTemplateToObject(obj, templateObj, templateParentNodeType) {
482
- var _a;
483
- let templateProp = obj.properties.find((p) => {
484
- var _a2;
485
- return p.keyNode.value === "template" && ((_a2 = p.valueNode) == null ? void 0 : _a2.type) === "array";
486
- });
487
- if (!templateProp) {
488
- templateProp = new PropertyNode(new ValueNode("template"), new ArrayNode());
489
- templateProp.parent = obj;
490
- obj.properties.push(templateProp);
491
- }
492
- const templateItems = templateProp.valueNode;
493
- templateItems.items.push(templateObj);
494
- templateObj.parent = templateItems;
495
- const templateParentProp = obj.properties.find((p) => {
496
- var _a2;
497
- return p.keyNode.value === templateParentNodeType && ((_a2 = p.valueNode) == null ? void 0 : _a2.type) === "array";
498
- });
499
- if (templateParentProp) {
500
- const indexOfTemplateParent = obj.properties.indexOf(templateParentProp, 1);
501
- const templateParentValueNode = (_a = obj.properties[indexOfTemplateParent]) == null ? void 0 : _a.valueNode;
502
- if (templateParentValueNode) {
503
- const templateParentArray = toJSON(templateParentValueNode);
504
- if (Array.isArray(templateParentArray) && templateParentArray.length === 0) {
505
- obj.properties.splice(indexOfTemplateParent, 1);
506
- }
507
- }
508
- }
509
- return () => {
510
- templateItems.items = templateItems.items.filter((t) => t !== templateObj);
511
- if (templateItems.children.length === 0 && templateProp) {
512
- obj.properties.splice(obj.properties.indexOf(templateProp, 1));
513
- }
514
- };
515
- }
516
- const TemplateProvider = (props) => {
517
- const baseContext = React__default.useContext(TemplateContext);
518
- return /* @__PURE__ */ React__default.createElement(TemplateContext.Provider, {
519
- value: { depth: baseContext.depth + 1 }
520
- }, props.children);
521
- };
522
- const getParentObject = (node) => {
523
- if (node.type === "object") {
524
- return node;
525
- }
526
- if (node.parent) {
527
- return getParentObject(node.parent);
528
- }
529
- };
530
- const getParentProperty = (node) => {
531
- if (node.type === "property") {
532
- return node;
533
- }
534
- if (node.parent) {
535
- return getParentProperty(node.parent);
536
- }
537
- };
538
- const Template = (props) => {
539
- var _a;
540
- const baseContext = React__default.useContext(TemplateContext);
541
- const dynamicProp = (_a = props.dynamic) != null ? _a : false;
542
- const [outputProp, setOutputProp] = React__default.useState(props.output);
543
- const proxyRef = React__default.useRef(null);
544
- const valueRef = React__default.useRef(null);
545
- const outputElement = React__default.useMemo(() => new ProxyNode(), []);
546
- React__default.useLayoutEffect(() => {
547
- const propNode = proxyRef.current && getParentProperty(proxyRef.current);
548
- if (outputProp === void 0 && propNode) {
549
- setOutputProp(propNode.keyNode.value);
550
- }
551
- }, [proxyRef, outputProp]);
552
- React__default.useEffect(() => {
553
- const templateObj = outputElement.items[0];
554
- if (proxyRef.current) {
555
- const parentObject = getParentObject(proxyRef.current);
556
- if (!parentObject) {
557
- throw new Error("Unable to find parent to add template to");
558
- }
559
- if (!outputProp) {
560
- return;
561
- }
562
- return addTemplateToObject(parentObject, templateObj, outputProp);
563
- }
564
- }, [proxyRef, outputProp, outputElement.items]);
565
- return /* @__PURE__ */ React__default.createElement("proxy", {
566
- ref: proxyRef
567
- }, createPortal(/* @__PURE__ */ React__default.createElement(OptionalIDSuffixProvider, {
568
- wrapperRef: valueRef,
569
- templateIndex: `_index${baseContext.depth === 0 ? "" : baseContext.depth}_`
570
- }, /* @__PURE__ */ React__default.createElement(TemplateProvider, null, /* @__PURE__ */ React__default.createElement("object", null, /* @__PURE__ */ React__default.createElement("property", {
571
- name: "data"
572
- }, props.data.toValue()), /* @__PURE__ */ React__default.createElement("property", {
573
- name: "output"
574
- }, outputProp), /* @__PURE__ */ React__default.createElement("property", {
575
- name: "value"
576
- }, props.children), dynamicProp && /* @__PURE__ */ React__default.createElement("property", {
577
- name: "dynamic"
578
- }, toJsonElement(dynamicProp))))), outputElement), /* @__PURE__ */ React__default.createElement("value", {
579
- ref: valueRef,
580
- value: void 0
581
- }));
582
- };
583
-
584
- var __defProp$1 = Object.defineProperty;
585
- var __defProps = Object.defineProperties;
586
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
587
- var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
588
- var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
589
- var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
590
- var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
591
- var __spreadValues$1 = (a, b2) => {
592
- for (var prop in b2 || (b2 = {}))
593
- if (__hasOwnProp$1.call(b2, prop))
594
- __defNormalProp$1(a, prop, b2[prop]);
595
- if (__getOwnPropSymbols$1)
596
- for (var prop of __getOwnPropSymbols$1(b2)) {
597
- if (__propIsEnum$1.call(b2, prop))
598
- __defNormalProp$1(a, prop, b2[prop]);
599
- }
600
- return a;
601
- };
602
- var __spreadProps = (a, b2) => __defProps(a, __getOwnPropDescs(b2));
603
- const bindingSymbol = Symbol("binding");
604
- const SchemaTypeName = Symbol("Schema Rename");
605
- const isTypeDef = (property) => {
606
- return property.type !== void 0;
607
- };
608
- class SchemaGenerator {
609
- constructor(logger) {
610
- this.hooks = {
611
- createSchemaNode: new SyncWaterfallHook()
612
- };
613
- this.toSchema = (schema) => {
614
- const newSchema = {
615
- ROOT: {}
616
- };
617
- this.children = [];
618
- this.generatedDataTypes.clear();
619
- Object.keys(schema).forEach((property) => {
620
- const subType = schema[property];
621
- newSchema.ROOT[property] = this.hooks.createSchemaNode.call(this.processChild(property, subType), subType);
622
- });
623
- while (this.children.length > 0) {
624
- const c = this.children.pop();
625
- if (c === void 0) {
626
- break;
627
- }
628
- const { name, child } = c;
629
- const typeDef = {};
630
- Object.keys(child).forEach((property) => {
631
- const subType = child[property];
632
- typeDef[property] = this.hooks.createSchemaNode.call(this.processChild(property, subType), subType);
633
- });
634
- newSchema[name] = typeDef;
635
- }
636
- return newSchema;
637
- };
638
- this.makePlaceholderType = (typeName) => {
639
- return {
640
- type: `${typeName}Type`
641
- };
642
- };
643
- this.children = [];
644
- this.generatedDataTypes = new Map();
645
- this.logger = logger != null ? logger : console;
646
- }
647
- processChild(property, subType) {
648
- var _a, _b, _c;
649
- if (isTypeDef(subType)) {
650
- return subType;
651
- }
652
- let intermediateType;
653
- if (Array.isArray(subType)) {
654
- if (subType.length > 1) {
655
- this.logger.warn(`Type ${property} has multiple types in array, should only contain one top level object type. Only taking first defined type`);
656
- }
657
- const subTypeName = (_a = subType[0][SchemaTypeName]) != null ? _a : property;
658
- intermediateType = this.makePlaceholderArrayType(subTypeName);
659
- this.children.push({ name: intermediateType.type, child: subType[0] });
660
- } else {
661
- const subTypeName = (_b = subType[SchemaTypeName]) != null ? _b : property;
662
- intermediateType = this.makePlaceholderType(subTypeName);
663
- this.children.push({ name: intermediateType.type, child: subType });
664
- }
665
- if (this.generatedDataTypes.has(intermediateType.type)) {
666
- const generatedType = this.generatedDataTypes.get(intermediateType.type);
667
- if (!dequal(subType, (_c = this.generatedDataTypes.get(intermediateType.type)) == null ? void 0 : _c.node)) {
668
- generatedType.count += 1;
669
- const newIntermediateType = __spreadProps(__spreadValues$1({}, intermediateType), {
670
- type: `${intermediateType.type}${generatedType.count}`
671
- });
672
- this.logger.warn(`WARNING: Generated two intermediate types with the name: ${intermediateType.type} that are of different shapes, using artificial type ${newIntermediateType.type}`);
673
- intermediateType = newIntermediateType;
674
- this.children.pop();
675
- this.children.push({ name: intermediateType.type, child: subType });
676
- }
677
- }
678
- this.generatedDataTypes.set(intermediateType.type, {
679
- node: subType,
680
- count: 1
681
- });
682
- return intermediateType;
683
- }
684
- makePlaceholderArrayType(typeName) {
685
- return {
686
- type: `${typeName}Type`,
687
- isArray: true
688
- };
689
- }
690
- }
691
- function makeBindingsForObject(obj, arrayAccessorKeys = ["_index_"]) {
692
- const accessor = (paths) => {
693
- const bindingMap = new WeakMap();
694
- return {
695
- ownKeys(target) {
696
- return Reflect.ownKeys(target);
697
- },
698
- get(target, key) {
699
- const bindingKeys = Object.keys(target);
700
- if (Array.isArray(target[key]) && target[key].length > 0 && target[key].every((it) => typeof it !== "object")) {
701
- return [...target[key]];
702
- }
703
- if (!bindingMap.has(target)) {
704
- bindingMap.set(target, binding`${paths.join(".")}`);
705
- }
706
- if (key === bindingSymbol) {
707
- return paths;
708
- }
709
- if (Array.isArray(target) && (arrayAccessorKeys.includes(key) || typeof key === "number")) {
710
- return new Proxy(target[0], accessor(paths.concat([key])));
711
- }
712
- if (bindingKeys.includes(key) && typeof target[key] === "object") {
713
- return new Proxy(target[key], accessor(paths.concat([key])));
714
- }
715
- const createdInstance = bindingMap.get(target);
716
- return createdInstance == null ? void 0 : createdInstance[key];
717
- }
718
- };
719
- };
720
- return new Proxy(obj, accessor([]));
721
- }
722
- const getBindingFromObject = (obj) => {
723
- const baseBindings = obj[bindingSymbol];
724
- if (!Array.isArray(baseBindings) || baseBindings.length === 0) {
725
- throw new Error(`Unable to get binding for ${obj}`);
726
- }
727
- return binding`${baseBindings.join(".")}`;
728
- };
729
- const getBindingStringFromObject = (obj) => {
730
- return getBindingFromObject(obj).toString();
731
- };
732
- const getRefStringFromObject = (obj) => {
733
- return getBindingFromObject(obj).toRefString();
734
- };
735
-
736
- const fingerprintContent = (content, filename) => {
737
- if (content !== null || content !== void 0) {
738
- if (React__default.isValidElement(content)) {
739
- return "view";
740
- }
741
- if (typeof content === "object" && "navigation" in content) {
742
- return "flow";
743
- }
744
- if (!filename || filename.includes("schema")) {
745
- return "schema";
746
- }
747
- }
748
- };
749
-
750
- var __defProp = Object.defineProperty;
751
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
752
- var __hasOwnProp = Object.prototype.hasOwnProperty;
753
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
754
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
755
- var __spreadValues = (a, b) => {
756
- for (var prop in b || (b = {}))
757
- if (__hasOwnProp.call(b, prop))
758
- __defNormalProp(a, prop, b[prop]);
759
- if (__getOwnPropSymbols)
760
- for (var prop of __getOwnPropSymbols(b)) {
761
- if (__propIsEnum.call(b, prop))
762
- __defNormalProp(a, prop, b[prop]);
763
- }
764
- return a;
765
- };
766
- var __async = (__this, __arguments, generator) => {
767
- return new Promise((resolve, reject) => {
768
- var fulfilled = (value) => {
769
- try {
770
- step(generator.next(value));
771
- } catch (e) {
772
- reject(e);
773
- }
774
- };
775
- var rejected = (value) => {
776
- try {
777
- step(generator.throw(value));
778
- } catch (e) {
779
- reject(e);
780
- }
781
- };
782
- var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
783
- step((generator = generator.apply(__this, __arguments)).next());
784
- });
785
- };
786
- const parseNavigationExpressions = (nav) => {
787
- function replaceExpWithStr(obj) {
788
- function convExp(value) {
789
- return value && typeof value === "object" && value.__type === "expression" ? value.toValue() : replaceExpWithStr(value);
790
- }
791
- if (Array.isArray(obj)) {
792
- return obj.map(convExp);
793
- }
794
- if (typeof obj === "object") {
795
- return Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, convExp(value)]));
796
- }
797
- return obj;
798
- }
799
- return replaceExpWithStr(nav);
800
- };
801
- const mergeSourceMaps = (sourceMaps, generated) => {
802
- const generator = new SourceMapGenerator();
803
- sourceMaps.forEach(({ sourceMap, offsetIndexSearch, source }) => {
804
- const generatedLineOffset = generated.split("\n").findIndex((line) => line.includes(offsetIndexSearch));
805
- const sourceLineOffset = source.split("\n").findIndex((line) => line.includes(offsetIndexSearch));
806
- const lineOffset = generatedLineOffset - sourceLineOffset;
807
- const generatedLine = generated.split("\n")[generatedLineOffset];
808
- const sourceLine = source.split("\n")[sourceLineOffset];
809
- const generatedColumn = generatedLine.indexOf(offsetIndexSearch);
810
- const sourceColumn = sourceLine.indexOf(offsetIndexSearch);
811
- const columnOffset = generatedColumn - sourceColumn;
812
- const consumer = new SourceMapConsumer(JSON.parse(sourceMap));
813
- consumer.eachMapping((mapping) => {
814
- generator.addMapping({
815
- generated: {
816
- line: mapping.generatedLine + lineOffset,
817
- column: mapping.generatedColumn + columnOffset
818
- },
819
- original: {
820
- line: mapping.originalLine,
821
- column: mapping.originalColumn
822
- },
823
- source: mapping.source
824
- });
825
- });
826
- });
827
- return generator.toString();
828
- };
829
- class DSLCompiler {
830
- constructor(logger) {
831
- this.hooks = {
832
- schemaGenerator: new SyncHook(),
833
- preProcessFlow: new AsyncSeriesWaterfallHook(),
834
- postProcessFlow: new AsyncSeriesWaterfallHook(),
835
- onEnd: new AsyncSeriesHook()
836
- };
837
- this.logger = logger != null ? logger : console;
838
- }
839
- serialize(value, context) {
840
- return __async(this, null, function* () {
841
- var _a, _b;
842
- if (typeof value !== "object" || value === null) {
843
- throw new Error("Unable to serialize non-object");
844
- }
845
- const type = (context == null ? void 0 : context.type) ? context.type : fingerprintContent(value);
846
- const schemaGenerator = new SchemaGenerator(this.logger);
847
- this.hooks.schemaGenerator.call(schemaGenerator);
848
- if (type === "view") {
849
- const { jsonValue, sourceMap } = yield render(value, {
850
- collectSourceMap: true
851
- });
852
- return {
853
- value: jsonValue,
854
- sourceMap
855
- };
856
- }
857
- if (type === "flow") {
858
- const allSourceMaps = [];
859
- const copiedValue = __spreadValues({}, value);
860
- copiedValue.views = yield Promise.all((_b = (_a = copiedValue == null ? void 0 : copiedValue.views) == null ? void 0 : _a.map((node) => __async(this, null, function* () {
861
- if (React__default.isValidElement(node)) {
862
- const { jsonValue, sourceMap, stringValue } = yield render(node, {
863
- collectSourceMap: true
864
- });
865
- if (sourceMap) {
866
- const searchIdLine = stringValue.split("\n").find((line) => line.includes(`"id": "${jsonValue.id}"`));
867
- if (searchIdLine) {
868
- allSourceMaps.push({
869
- sourceMap,
870
- offsetIndexSearch: searchIdLine,
871
- source: stringValue
872
- });
873
- }
874
- }
875
- return jsonValue;
876
- }
877
- return node;
878
- }))) != null ? _b : []);
879
- if ("navigation" in value) {
880
- Object.entries(value.navigation).forEach(([navKey, node]) => {
881
- if (typeof node === "object") {
882
- Object.entries(node).forEach(([nodeKey, flowNode]) => {
883
- var _a2, _b2, _c, _d;
884
- if (flowNode && typeof flowNode === "object" && "state_type" in flowNode && flowNode.state_type === "VIEW" && React__default.isValidElement(flowNode.ref)) {
885
- const actualViewIndex = (_b2 = (_a2 = value.views) == null ? void 0 : _a2.indexOf) == null ? void 0 : _b2.call(_a2, flowNode.ref);
886
- if (actualViewIndex !== void 0 && actualViewIndex > -1) {
887
- const actualId = (_d = (_c = copiedValue.views) == null ? void 0 : _c[actualViewIndex]) == null ? void 0 : _d.id;
888
- copiedValue.navigation[navKey][nodeKey].ref = actualId;
889
- }
890
- }
891
- });
892
- }
893
- });
894
- if ("schema" in copiedValue) {
895
- copiedValue.schema = schemaGenerator.toSchema(copiedValue.schema);
896
- }
897
- copiedValue.navigation = parseNavigationExpressions(copiedValue.navigation);
898
- }
899
- if (value) {
900
- const postProcessFlow = yield this.hooks.postProcessFlow.call(copiedValue);
901
- return {
902
- value: postProcessFlow,
903
- sourceMap: mergeSourceMaps(allSourceMaps, JSON.stringify(copiedValue, null, 2))
904
- };
905
- }
906
- }
907
- if (type === "schema") {
908
- return {
909
- value: schemaGenerator.toSchema(value)
910
- };
911
- }
912
- throw Error("DSL Compiler Error: Unable to determine type to compile as");
913
- });
914
- }
915
- }
916
-
917
- const DefaultCompilerContentTypes = ["view", "flow", "schema"];
918
- function isDefaultCompilerContentType(t) {
919
- return DefaultCompilerContentTypes.includes(t);
920
- }
921
-
922
- export { Asset, AssetWrapper, DSLCompiler, GeneratedIDProperty, IDProvider, IDSuffixIndexProvider, IDSuffixProvider, IndexSuffixStopContext, OptionalIDSuffixProvider, SchemaGenerator, SchemaTypeName, Slot, SlotContext, Switch, Template, TemplateContext, TemplateStringComponent, View, binding, createSlot, expression, fingerprintContent, flattenChildren, getBindingFromObject, getBindingStringFromObject, getRefStringFromObject, isDefaultCompilerContentType, isTemplateStringInstance, makeBindingsForObject, normalizeText, normalizeToCollection, toArray, toJsonElement, toJsonProperties, useGetIdPrefix, useIndexInSlot };
923
- //# sourceMappingURL=index.esm.js.map