@player-tools/dsl 0.0.2-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.
- package/README.md +9 -0
- package/dist/index.cjs.js +865 -0
- package/dist/index.d.ts +324 -0
- package/dist/index.esm.js +818 -0
- package/package.json +51 -0
- package/src/auto-id.tsx +136 -0
- package/src/compiler/compiler.ts +219 -0
- package/src/compiler/index.ts +2 -0
- package/src/compiler/schema.ts +250 -0
- package/src/compiler/types.ts +18 -0
- package/src/components.tsx +234 -0
- package/src/index.ts +10 -0
- package/src/string-templates/index.ts +172 -0
- package/src/switch.tsx +136 -0
- package/src/template.tsx +186 -0
- package/src/types.ts +81 -0
- package/src/utils.tsx +109 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
import React$1, { PropsWithChildren } from 'react';
|
|
2
|
+
import { PropertyNode, ObjectNode, JsonNode, JsonType } from 'react-json-reconciler';
|
|
3
|
+
export * from 'react-json-reconciler';
|
|
4
|
+
import { Asset as Asset$1, Navigation as Navigation$1, Expression, Schema, Flow } from '@player-ui/types';
|
|
5
|
+
import { SyncWaterfallHook, SyncHook } from 'tapable-ts';
|
|
6
|
+
|
|
7
|
+
declare type TemplateInstanceRefStringContext = 'binding' | 'expression';
|
|
8
|
+
interface TemplateRefStringOptions {
|
|
9
|
+
/** If this template string is inside of another binding or expression */
|
|
10
|
+
nestedContext?: TemplateInstanceRefStringContext;
|
|
11
|
+
}
|
|
12
|
+
interface TemplateInstanceRefStringOptions {
|
|
13
|
+
/** The array of strings for the template */
|
|
14
|
+
strings: TemplateStringsArray;
|
|
15
|
+
/** the other data that's present in the template */
|
|
16
|
+
other: Array<string | TemplateStringType>;
|
|
17
|
+
/** If this template string is inside of another binding or expression */
|
|
18
|
+
nestedContext?: TemplateInstanceRefStringContext;
|
|
19
|
+
/** Convert the value to a reference nested in the given context */
|
|
20
|
+
toRefString: (options: TemplateRefStringOptions | undefined, value: string) => string;
|
|
21
|
+
}
|
|
22
|
+
declare const OpaqueIdentifier: unique symbol;
|
|
23
|
+
declare type TemplateStringType = React$1.ReactElement & {
|
|
24
|
+
/** An identifier to show that this is a template type */
|
|
25
|
+
[OpaqueIdentifier]: true;
|
|
26
|
+
/** The value of the template string when in another string */
|
|
27
|
+
toString: () => string;
|
|
28
|
+
/** the raw value of the template string */
|
|
29
|
+
toValue: () => string;
|
|
30
|
+
/** the dereferenced value when used in another */
|
|
31
|
+
toRefString: (options?: TemplateRefStringOptions) => string;
|
|
32
|
+
};
|
|
33
|
+
declare type BindingTemplateInstance = TemplateStringType & {
|
|
34
|
+
/** An identifier for a binding instance */
|
|
35
|
+
__type: 'binding';
|
|
36
|
+
};
|
|
37
|
+
declare type ExpressionTemplateInstance = TemplateStringType & {
|
|
38
|
+
/** The identifier for an expression instance */
|
|
39
|
+
__type: 'expression';
|
|
40
|
+
};
|
|
41
|
+
/** A react component for rendering a template string type */
|
|
42
|
+
declare const TemplateStringComponent: (props: {
|
|
43
|
+
/** The string value of the child template string */
|
|
44
|
+
value: string;
|
|
45
|
+
}) => React$1.ReactElement<{
|
|
46
|
+
value: string;
|
|
47
|
+
}, string | React$1.JSXElementConstructor<any>>;
|
|
48
|
+
/** A tagged-template constructor for a binding */
|
|
49
|
+
declare const binding: (strings: TemplateStringsArray, ...nested: Array<TemplateStringType | string>) => BindingTemplateInstance;
|
|
50
|
+
/** A tagged-template constructor for an expression */
|
|
51
|
+
declare const expression: (strings: TemplateStringsArray, ...nested: Array<ExpressionTemplateInstance | BindingTemplateInstance | string>) => ExpressionTemplateInstance;
|
|
52
|
+
/** Check if a value is a template string */
|
|
53
|
+
declare const isTemplateStringInstance: (val: unknown) => val is BindingTemplateInstance | ExpressionTemplateInstance;
|
|
54
|
+
|
|
55
|
+
declare type WithChildren<T = Record<string, unknown>> = T & {
|
|
56
|
+
/** child nodes */
|
|
57
|
+
children?: React.ReactNode;
|
|
58
|
+
};
|
|
59
|
+
declare type RemoveUnknownIndex<T> = {
|
|
60
|
+
[P in keyof T as T[P] extends unknown ? unknown extends T[P] ? never : P : P]: T[P];
|
|
61
|
+
};
|
|
62
|
+
declare type AddUnknownIndex<T> = T & {
|
|
63
|
+
[key: string]: unknown;
|
|
64
|
+
};
|
|
65
|
+
/** Make an ID prop optional an a type */
|
|
66
|
+
declare type OmitProp<T, K extends string> = {
|
|
67
|
+
[P in keyof T as P extends K ? never : P]: T[P];
|
|
68
|
+
};
|
|
69
|
+
interface PlayerApplicability {
|
|
70
|
+
/** An expression to evaluate to determine if this node should appear in a view or not */
|
|
71
|
+
applicability?: BindingTemplateInstance | ExpressionTemplateInstance | boolean;
|
|
72
|
+
}
|
|
73
|
+
declare type AssetPropsWithChildren<T extends Asset$1> = WithChildren<WithTemplateTypes<OmitProp<RemoveUnknownIndex<T>, 'id' | 'type'> & Partial<Pick<Asset$1, 'id'>>> & PlayerApplicability>;
|
|
74
|
+
declare type SwapKeysToType<T, K extends keyof T, NewType> = {
|
|
75
|
+
[P in keyof T]: P extends K ? NewType : T[P];
|
|
76
|
+
};
|
|
77
|
+
declare type WithTemplateTypes<T> = T extends Record<any, any> ? {
|
|
78
|
+
[P in keyof T]: WithTemplateTypes<T[P]>;
|
|
79
|
+
} : T | BindingTemplateInstance | ExpressionTemplateInstance;
|
|
80
|
+
declare type ValidKeys = 'exp' | 'onStart' | 'onEnd';
|
|
81
|
+
declare type DeepReplace<T, Old, New> = {
|
|
82
|
+
[P in keyof T]: T[P] extends Old ? P extends ValidKeys ? New : DeepReplace<T[P], Old, New> : T[P] extends (infer R)[] ? DeepReplace<R, Old, New>[] : T[P] extends object ? DeepReplace<T[P], Old, New> : Extract<T[P], Old> extends Old ? DeepReplace<Extract<T[P], object>, Old, New> | Exclude<T[P], Old | object> | New : T[P];
|
|
83
|
+
};
|
|
84
|
+
declare type Navigation = DeepReplace<Navigation$1, Expression, ExpressionTemplateInstance | ExpressionTemplateInstance[] | Expression>;
|
|
85
|
+
|
|
86
|
+
declare const SlotContext: React$1.Context<{
|
|
87
|
+
/** The property name for the slot */
|
|
88
|
+
propertyName: string;
|
|
89
|
+
/** If the slot represents an array */
|
|
90
|
+
isArray: boolean;
|
|
91
|
+
/** If the items in the slot should be wrapped in an "asset" object */
|
|
92
|
+
wrapInAsset: boolean;
|
|
93
|
+
/** Other props to add to the slot */
|
|
94
|
+
additionalProperties?: any;
|
|
95
|
+
/** The ref to the property node */
|
|
96
|
+
ref: React$1.RefObject<PropertyNode>;
|
|
97
|
+
/** A text component if we hit a string but expect an asset */
|
|
98
|
+
TextComp?: React$1.ComponentType<{}> | undefined;
|
|
99
|
+
/** A component to create a collection asset is we get an array but need a single element */
|
|
100
|
+
CollectionComp?: React$1.ComponentType<{}> | undefined;
|
|
101
|
+
} | undefined>;
|
|
102
|
+
/**
|
|
103
|
+
* Wraps the children in an `asset` object.
|
|
104
|
+
* Additional props are added to the top level object
|
|
105
|
+
*/
|
|
106
|
+
declare const AssetWrapper: React$1.ForwardRefExoticComponent<Pick<WithChildren<{
|
|
107
|
+
[key: string]: any;
|
|
108
|
+
}>, string | number> & React$1.RefAttributes<ObjectNode>>;
|
|
109
|
+
/** Create a ID property for a node */
|
|
110
|
+
declare const GeneratedIDProperty: (props: {
|
|
111
|
+
/** the id to use if supplied by the user */
|
|
112
|
+
id?: string;
|
|
113
|
+
}) => JSX.Element;
|
|
114
|
+
/** An asset */
|
|
115
|
+
declare const Asset: React$1.ForwardRefExoticComponent<Pick<{
|
|
116
|
+
[key: string]: unknown;
|
|
117
|
+
/** id of the asset */
|
|
118
|
+
id?: string | undefined;
|
|
119
|
+
/** the asset type */
|
|
120
|
+
type: string;
|
|
121
|
+
/** Any other properties on the asset */
|
|
122
|
+
children?: React$1.ReactNode;
|
|
123
|
+
} & PlayerApplicability, string | number> & React$1.RefAttributes<ObjectNode>>;
|
|
124
|
+
/** A component to generate a named property slot */
|
|
125
|
+
declare const Slot: (props: {
|
|
126
|
+
/** The name of the slot */
|
|
127
|
+
name: string;
|
|
128
|
+
/** if the slot is an array or single object */
|
|
129
|
+
isArray?: boolean;
|
|
130
|
+
/** if each item should be wrapped in an asset */
|
|
131
|
+
wrapInAsset?: boolean;
|
|
132
|
+
/** Any children to render in the slot */
|
|
133
|
+
children?: React$1.ReactNode;
|
|
134
|
+
/** Other properties to add to the slot */
|
|
135
|
+
additionalProperties?: any;
|
|
136
|
+
/** A text component if we hit a string but expect an asset */
|
|
137
|
+
TextComp?: React$1.ComponentType;
|
|
138
|
+
/** A component to create a collection asset is we get an array but need a single element */
|
|
139
|
+
CollectionComp?: React$1.ComponentType;
|
|
140
|
+
}) => JSX.Element;
|
|
141
|
+
/** Create a slot for a given property */
|
|
142
|
+
declare function createSlot<SlotProps = unknown>(options: {
|
|
143
|
+
/** The name of the slot */
|
|
144
|
+
name: string;
|
|
145
|
+
/** if the slot is an array or single object */
|
|
146
|
+
isArray?: boolean;
|
|
147
|
+
/** if each item should be wrapped in an asset */
|
|
148
|
+
wrapInAsset?: boolean;
|
|
149
|
+
/** Any children to render in the slot */
|
|
150
|
+
children?: React$1.ReactNode;
|
|
151
|
+
/** A text component if we hit a string but expect an asset */
|
|
152
|
+
TextComp?: React$1.ComponentType;
|
|
153
|
+
/** A component to create a collection asset is we get an array but need a single element */
|
|
154
|
+
CollectionComp?: React$1.ComponentType;
|
|
155
|
+
}): (props: {
|
|
156
|
+
/** An object to include in this property */
|
|
157
|
+
children?: React$1.ReactNode;
|
|
158
|
+
} & SlotProps) => JSX.Element;
|
|
159
|
+
|
|
160
|
+
declare const IndexSuffixStopContext: React$1.Context<boolean>;
|
|
161
|
+
/** Get the generated id */
|
|
162
|
+
declare const useGetIdPrefix: () => string;
|
|
163
|
+
/** Add a suffix to a generated id */
|
|
164
|
+
declare const IDSuffixProvider: (props: WithChildren<{
|
|
165
|
+
/** The suffix to append */
|
|
166
|
+
suffix: string;
|
|
167
|
+
}>) => JSX.Element;
|
|
168
|
+
/** Override the generated id with the supplied one */
|
|
169
|
+
declare const IDProvider: (props: WithChildren<{
|
|
170
|
+
/** The new id to use */
|
|
171
|
+
id?: string;
|
|
172
|
+
}>) => JSX.Element;
|
|
173
|
+
/** Get the index of an item in a slot */
|
|
174
|
+
declare const useIndexInSlot: (ref: React$1.RefObject<JsonNode>) => number;
|
|
175
|
+
/** Add the index to the id path when in an array slot */
|
|
176
|
+
declare const IDSuffixIndexProvider: (props: WithChildren<{
|
|
177
|
+
/** The ref to use */
|
|
178
|
+
wrapperRef: React$1.RefObject<JsonNode>;
|
|
179
|
+
/** if the suffix is in a template, the id to use */
|
|
180
|
+
templateIndex?: string;
|
|
181
|
+
}>) => JSX.Element;
|
|
182
|
+
/** Wrap a slot with the index if in an array slot */
|
|
183
|
+
declare const OptionalIDSuffixProvider: (props: WithChildren<{
|
|
184
|
+
/** The ref to walk upwards and use as an index */
|
|
185
|
+
wrapperRef: React$1.RefObject<JsonNode>;
|
|
186
|
+
/** if the suffix is in a template, the id to use */
|
|
187
|
+
templateIndex?: string;
|
|
188
|
+
}>) => JSX.Element;
|
|
189
|
+
|
|
190
|
+
/** Get an array version of the value */
|
|
191
|
+
declare function toArray<T>(val: T | Array<T>): Array<T>;
|
|
192
|
+
/** Create a component version */
|
|
193
|
+
declare function toJsonElement(value: any, index?: number): React$1.ReactElement;
|
|
194
|
+
/** Create a fragment for the properties */
|
|
195
|
+
declare function toJsonProperties(value: Record<string, any>): JSX.Element[];
|
|
196
|
+
/** Create a text asset if needed */
|
|
197
|
+
declare function normalizeText(options: {
|
|
198
|
+
/** The current node */
|
|
199
|
+
node: React$1.ReactNode;
|
|
200
|
+
/** A component to render a text asset */
|
|
201
|
+
TextComp?: React$1.ComponentType;
|
|
202
|
+
}): React$1.ReactNode;
|
|
203
|
+
/** Create a collection if needed */
|
|
204
|
+
declare function normalizeToCollection(options: {
|
|
205
|
+
/** the node to look at */
|
|
206
|
+
node: React$1.ReactNode;
|
|
207
|
+
/** A Text asset */
|
|
208
|
+
TextComp?: React$1.ComponentType;
|
|
209
|
+
/** A collection asset */
|
|
210
|
+
CollectionComp?: React$1.ComponentType;
|
|
211
|
+
}): React$1.ReactNode;
|
|
212
|
+
|
|
213
|
+
interface SwitchProps {
|
|
214
|
+
/** defaults to a staticSwitch */
|
|
215
|
+
isDynamic?: boolean;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Switches allow users to fork content between 1 or more assets
|
|
219
|
+
*/
|
|
220
|
+
declare const Switch: {
|
|
221
|
+
(props: PropsWithChildren<SwitchProps>): JSX.Element;
|
|
222
|
+
Case: (props: PropsWithChildren<CaseProps>) => JSX.Element;
|
|
223
|
+
};
|
|
224
|
+
interface CaseProps {
|
|
225
|
+
/** the test for this case statement */
|
|
226
|
+
exp?: ExpressionTemplateInstance | BindingTemplateInstance | boolean;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
interface TemplateContextType {
|
|
230
|
+
/** The number of nested templates */
|
|
231
|
+
depth: number;
|
|
232
|
+
}
|
|
233
|
+
declare const TemplateContext: React$1.Context<TemplateContextType>;
|
|
234
|
+
interface TemplateProps {
|
|
235
|
+
/** The source binding */
|
|
236
|
+
data: BindingTemplateInstance;
|
|
237
|
+
/** The target property */
|
|
238
|
+
output?: string;
|
|
239
|
+
/** The template value */
|
|
240
|
+
children: React$1.ReactNode;
|
|
241
|
+
}
|
|
242
|
+
/** A template allows users to dynamically map over an array of data */
|
|
243
|
+
declare const Template: (props: TemplateProps) => JSX.Element;
|
|
244
|
+
|
|
245
|
+
declare const SchemaTypeName: unique symbol;
|
|
246
|
+
/**
|
|
247
|
+
* Generator for `Schema.Schema` Objects
|
|
248
|
+
*/
|
|
249
|
+
declare class SchemaGenerator {
|
|
250
|
+
private children;
|
|
251
|
+
private generatedDataTypeNames;
|
|
252
|
+
hooks: {
|
|
253
|
+
createSchemaNode: SyncWaterfallHook<[node: Schema.DataType<unknown>, originalProperty: Record<string | symbol, unknown>], Record<string, any>>;
|
|
254
|
+
};
|
|
255
|
+
constructor();
|
|
256
|
+
/**
|
|
257
|
+
* Converts an object to a `Schema.Schema` representation
|
|
258
|
+
* Note: uses iteration to prevent potentially very deep recursion on large objects
|
|
259
|
+
*/
|
|
260
|
+
toSchema: (schema: any) => Schema.Schema;
|
|
261
|
+
/**
|
|
262
|
+
* Processes the children of an object Node
|
|
263
|
+
* Newly discovered children get added to the provided array
|
|
264
|
+
*/
|
|
265
|
+
private processChildren;
|
|
266
|
+
/**
|
|
267
|
+
* Make an intermediate `Schema.DataType` object given a name
|
|
268
|
+
*/
|
|
269
|
+
private makePlaceholderType;
|
|
270
|
+
/**
|
|
271
|
+
* Make an intermediate `Schema.DataType` object with array support given a name
|
|
272
|
+
*/
|
|
273
|
+
private makePlaceholderArrayType;
|
|
274
|
+
}
|
|
275
|
+
declare type MakeArrayIntoIndexRef<T extends any[]> = {
|
|
276
|
+
[key: number]: MakeBindingRefable<T[0]>;
|
|
277
|
+
/** Used when referencing bindings from within a template */
|
|
278
|
+
_index_: MakeBindingRefable<T[0]>;
|
|
279
|
+
} & BindingTemplateInstance;
|
|
280
|
+
declare type MakeBindingRefable<T> = {
|
|
281
|
+
[P in keyof T]: T[P] extends any[] ? MakeArrayIntoIndexRef<T[P]> : MakeBindingRefable<T[P]>;
|
|
282
|
+
} & BindingTemplateInstance;
|
|
283
|
+
/**
|
|
284
|
+
* Adds bindings to an object so that the object can be directly used in JSX
|
|
285
|
+
*/
|
|
286
|
+
declare function makeBindingsForObject<Type>(obj: Type, arrayAccessorKeys?: string[]): MakeBindingRefable<Type>;
|
|
287
|
+
/**
|
|
288
|
+
* Generates binding for an object property
|
|
289
|
+
*/
|
|
290
|
+
declare const getBindingFromObject: (obj: any) => BindingTemplateInstance;
|
|
291
|
+
/**
|
|
292
|
+
* Returns the binding string from an object path
|
|
293
|
+
*/
|
|
294
|
+
declare const getBindingStringFromObject: (obj: any) => string;
|
|
295
|
+
/**
|
|
296
|
+
* Returns the ref string from an object path
|
|
297
|
+
*/
|
|
298
|
+
declare const getRefStringFromObject: (obj: any) => string;
|
|
299
|
+
|
|
300
|
+
declare type FlowWithoutUnknown = RemoveUnknownIndex<Flow>;
|
|
301
|
+
declare type FlowWithReactViews = AddUnknownIndex<Omit<FlowWithoutUnknown, 'views'> & {
|
|
302
|
+
/** An array of JSX view elements */
|
|
303
|
+
views?: Array<React.ReactElement>;
|
|
304
|
+
}>;
|
|
305
|
+
declare type SerializeType = 'view' | 'flow' | 'schema' | 'navigation';
|
|
306
|
+
declare type SerializablePlayerExportTypes = React.ReactElement | FlowWithReactViews | Schema.Schema | Navigation$1;
|
|
307
|
+
|
|
308
|
+
/** A compiler for transforming DSL content into JSON */
|
|
309
|
+
declare class DSLCompiler {
|
|
310
|
+
hooks: {
|
|
311
|
+
schemaGenerator: SyncHook<[SchemaGenerator], Record<string, any>>;
|
|
312
|
+
};
|
|
313
|
+
/** Convert an object (flow, view, schema, etc) into it's JSON representation */
|
|
314
|
+
serialize(value: unknown): Promise<{
|
|
315
|
+
/** the JSON value of the source */
|
|
316
|
+
value: JsonType | undefined;
|
|
317
|
+
/** the fingerprinted content type of the source */
|
|
318
|
+
contentType: SerializeType;
|
|
319
|
+
/** The sourcemap of the content */
|
|
320
|
+
sourceMap?: string;
|
|
321
|
+
}>;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
export { AddUnknownIndex, Asset, AssetPropsWithChildren, AssetWrapper, BindingTemplateInstance, CaseProps, DSLCompiler, ExpressionTemplateInstance, FlowWithReactViews, FlowWithoutUnknown, GeneratedIDProperty, IDProvider, IDSuffixIndexProvider, IDSuffixProvider, IndexSuffixStopContext, MakeArrayIntoIndexRef, MakeBindingRefable, Navigation, OmitProp, OptionalIDSuffixProvider, PlayerApplicability, RemoveUnknownIndex, SchemaGenerator, SchemaTypeName, SerializablePlayerExportTypes, SerializeType, Slot, SlotContext, SwapKeysToType, Switch, SwitchProps, Template, TemplateContext, TemplateContextType, TemplateInstanceRefStringContext, TemplateInstanceRefStringOptions, TemplateProps, TemplateRefStringOptions, TemplateStringComponent, TemplateStringType, WithChildren, WithTemplateTypes, binding, createSlot, expression, getBindingFromObject, getBindingStringFromObject, getRefStringFromObject, isTemplateStringInstance, makeBindingsForObject, normalizeText, normalizeToCollection, toArray, toJsonElement, toJsonProperties, useGetIdPrefix, useIndexInSlot };
|