@microsoft/fast-html 1.0.0-alpha.21 → 1.0.0-alpha.23

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,3 +1,4 @@
1
+ import { type ChildrenMap } from "./utilities.js";
1
2
  type FastContextMetaData = "$fast_context";
2
3
  type FastContextsMetaData = "$fast_parent_contexts";
3
4
  export interface JSONSchemaDefinition extends JSONSchemaCommon {
@@ -8,6 +9,7 @@ interface JSONSchemaCommon {
8
9
  type?: string;
9
10
  properties?: any;
10
11
  items?: any;
12
+ anyOf?: Array<any>;
11
13
  }
12
14
  export interface JSONSchema extends JSONSchemaCommon {
13
15
  $schema: string;
@@ -37,10 +39,11 @@ export interface RepeatCachedPath extends CachedPathCommon {
37
39
  type: RepeatCachedPathType;
38
40
  }
39
41
  export type CachedPath = DefaultCachedPath | RepeatCachedPath | AccessCachedPath | EventCachedPath;
40
- export type CachedPathMap = Map<string, JSONSchema>;
42
+ export type CachedPathMap = Map<string, Map<string, JSONSchema>>;
41
43
  interface RegisterPathConfig {
42
44
  rootPropertyName: string;
43
45
  pathConfig: CachedPath;
46
+ childrenMap: ChildrenMap | null;
44
47
  }
45
48
  export declare const fastContextMetaData: FastContextMetaData;
46
49
  export declare const fastContextsMetaData: FastContextsMetaData;
@@ -57,7 +60,7 @@ export declare class Schema {
57
60
  /**
58
61
  * A JSON schema describing each root schema
59
62
  */
60
- private jsonSchemaMap;
63
+ static jsonSchemaMap: CachedPathMap;
61
64
  constructor(name: string);
62
65
  /**
63
66
  * Add a path to a schema
@@ -87,6 +90,13 @@ export declare class Schema {
87
90
  * @returns A string to use as a $ref
88
91
  */
89
92
  private getDefsPath;
93
+ /**
94
+ * Get the schema $id
95
+ * @param customElementName - The custom element name
96
+ * @param propertyName - The property name
97
+ * @returns The ID that can be used in the JSON schema as $id
98
+ */
99
+ private getSchemaId;
90
100
  /**
91
101
  * Add a new JSON schema to the JSON schema map
92
102
  * @param propertyName The name of the property to assign this JSON schema to
@@ -22,7 +22,6 @@ declare class TemplateElement extends FASTElement {
22
22
  * A dictionary of custom element options
23
23
  */
24
24
  static elementOptions: ElementOptionsDictionary;
25
- private partials;
26
25
  /**
27
26
  * ObserverMap instance for caching binding paths
28
27
  */
@@ -36,6 +36,10 @@ export interface TemplateDirectiveBehaviorConfig extends BehaviorConfig {
36
36
  closingTagStartIndex: number;
37
37
  closingTagEndIndex: number;
38
38
  }
39
+ export interface ChildrenMap {
40
+ customElementName: string;
41
+ attributeName: string;
42
+ }
39
43
  /**
40
44
  * Get the index of the next matching tag
41
45
  * @param openingTagStartSlice - The slice starting from the opening tag
@@ -59,7 +63,7 @@ export declare function getNextBehavior(innerHTML: string): DataBindingBehaviorC
59
63
  * @returns A function to access the value from a given path.
60
64
  */
61
65
  export declare function pathResolver(path: string, contextPath: string | null, level: number, rootSchema: JSONSchema): (accessibleObject: any, context: any) => any;
62
- export declare function bindingResolver(rootPropertyName: string | null, path: string, parentContext: string | null, type: PathType, schema: Schema, currentContext: string | null, level: number): (accessibleObject: any, context: any) => any;
66
+ export declare function bindingResolver(previousString: string | null, rootPropertyName: string | null, path: string, parentContext: string | null, type: PathType, schema: Schema, currentContext: string | null, level: number): (accessibleObject: any, context: any) => any;
63
67
  export declare function expressionResolver(rootPropertyName: string | null, expression: ChainedExpression, parentContext: string | null, level: number, schema: Schema): (accessibleObject: any, context: any) => any;
64
68
  /**
65
69
  * Extracts all paths from a ChainedExpression, including nested expressions
@@ -145,4 +149,10 @@ export declare function assignProxy(schema: JSONSchema | JSONSchemaDefinition, r
145
149
  * @returns
146
150
  */
147
151
  export declare function getRootPropertyName(rootPropertyName: string | null, path: string, context: null | string, type: PathType): string | null;
152
+ /**
153
+ * Get details of bindings to the attributes of child custom elements
154
+ * @param previousString - The previous string before the binding
155
+ * @returns null, or a custom element name and attribute name
156
+ */
157
+ export declare function getChildrenMap(previousString: string | null): ChildrenMap | null;
148
158
  export {};
@@ -9,11 +9,8 @@ export const refPropertyName = "$ref";
9
9
  */
10
10
  export class Schema {
11
11
  constructor(name) {
12
- /**
13
- * A JSON schema describing each root schema
14
- */
15
- this.jsonSchemaMap = new Map();
16
12
  this.customElementName = name;
13
+ Schema.jsonSchemaMap.set(this.customElementName, new Map());
17
14
  }
18
15
  /**
19
16
  * Add a path to a schema
@@ -22,18 +19,27 @@ export class Schema {
22
19
  addPath(config) {
23
20
  var _a, _b, _c;
24
21
  const splitPath = this.getSplitPath(config.pathConfig.path);
25
- let schema = this.jsonSchemaMap.get(config.rootPropertyName);
22
+ let schema = Schema.jsonSchemaMap.get(this.customElementName).get(config.rootPropertyName);
23
+ let childRef = null;
26
24
  // Create a root level property JSON
27
25
  if (!schema) {
28
26
  this.addNewSchema(config.rootPropertyName);
29
- schema = this.jsonSchemaMap.get(config.rootPropertyName);
27
+ schema = Schema.jsonSchemaMap.get(this.customElementName).get(config.rootPropertyName);
28
+ }
29
+ if (config.childrenMap) {
30
+ childRef = this.getSchemaId(config.childrenMap.customElementName, config.childrenMap.attributeName);
31
+ if (splitPath.length === 1) {
32
+ schema.anyOf
33
+ ? schema.anyOf.push({ [refPropertyName]: childRef })
34
+ : (schema.anyOf = [{ [refPropertyName]: childRef }]);
35
+ }
30
36
  }
31
37
  switch (config.pathConfig.type) {
32
38
  case "default":
33
39
  case "access": {
34
40
  if (splitPath.length > 1) {
35
41
  if (config.pathConfig.currentContext === null) {
36
- this.addPropertiesToAnObject(schema, splitPath.slice(1), config.pathConfig.currentContext);
42
+ this.addPropertiesToAnObject(schema, splitPath.slice(1), config.pathConfig.currentContext, childRef);
37
43
  }
38
44
  else {
39
45
  if (!((_a = schema[defsPropertyName]) === null || _a === void 0 ? void 0 : _a[splitPath[0]])) {
@@ -41,7 +47,7 @@ export class Schema {
41
47
  [splitPath[0]]: {},
42
48
  };
43
49
  }
44
- this.addPropertiesToAContext(schema[defsPropertyName][splitPath[0]], splitPath.slice(1), config.pathConfig.currentContext);
50
+ this.addPropertiesToAContext(schema[defsPropertyName][splitPath[0]], splitPath.slice(1), config.pathConfig.currentContext, childRef);
45
51
  }
46
52
  }
47
53
  break;
@@ -54,16 +60,16 @@ export class Schema {
54
60
  let updatedSchema = schema;
55
61
  const hasParentContext = !!config.pathConfig.parentContext;
56
62
  if (hasParentContext) {
57
- updatedSchema = this.addPropertiesToAnObject((_b = schema[defsPropertyName]) === null || _b === void 0 ? void 0 : _b[config.pathConfig.parentContext], splitPath.slice(1, -1), config.pathConfig.parentContext);
63
+ updatedSchema = this.addPropertiesToAnObject((_b = schema[defsPropertyName]) === null || _b === void 0 ? void 0 : _b[config.pathConfig.parentContext], splitPath.slice(1, -1), config.pathConfig.parentContext, childRef);
58
64
  }
59
- this.addPropertiesToAnObject(updatedSchema, hasParentContext ? splitPath.slice(2) : splitPath.slice(1), config.pathConfig.currentContext, "array");
65
+ this.addPropertiesToAnObject(updatedSchema, hasParentContext ? splitPath.slice(2) : splitPath.slice(1), config.pathConfig.currentContext, childRef, "array");
60
66
  }
61
67
  else if (splitPath.length > 1) {
62
68
  let schemaDefinition;
63
69
  if (config.pathConfig.parentContext) {
64
70
  schemaDefinition = (_c = schema === null || schema === void 0 ? void 0 : schema[defsPropertyName]) === null || _c === void 0 ? void 0 : _c[config.pathConfig.parentContext];
65
71
  }
66
- this.addPropertiesToAnObject(schemaDefinition !== null && schemaDefinition !== void 0 ? schemaDefinition : schema, splitPath.slice(1), config.pathConfig.currentContext, "array");
72
+ this.addPropertiesToAnObject(schemaDefinition !== null && schemaDefinition !== void 0 ? schemaDefinition : schema, splitPath.slice(1), config.pathConfig.currentContext, childRef, "array");
67
73
  }
68
74
  else {
69
75
  schema.type = "array";
@@ -80,14 +86,14 @@ export class Schema {
80
86
  */
81
87
  getSchema(rootPropertyName) {
82
88
  var _a;
83
- return (_a = this.jsonSchemaMap.get(rootPropertyName)) !== null && _a !== void 0 ? _a : null;
89
+ return ((_a = Schema.jsonSchemaMap.get(this.customElementName).get(rootPropertyName)) !== null && _a !== void 0 ? _a : null);
84
90
  }
85
91
  /**
86
92
  * Gets root properties
87
93
  * @returns IterableIterator<string>
88
94
  */
89
95
  getRootProperties() {
90
- return this.jsonSchemaMap.keys();
96
+ return Schema.jsonSchemaMap.get(this.customElementName).keys();
91
97
  }
92
98
  /**
93
99
  * Get a path split into property names
@@ -105,14 +111,23 @@ export class Schema {
105
111
  getDefsPath(context) {
106
112
  return `#/${defsPropertyName}/${context}`;
107
113
  }
114
+ /**
115
+ * Get the schema $id
116
+ * @param customElementName - The custom element name
117
+ * @param propertyName - The property name
118
+ * @returns The ID that can be used in the JSON schema as $id
119
+ */
120
+ getSchemaId(customElementName, propertyName) {
121
+ return `https://fast.design/schemas/${customElementName}/${propertyName}.json`;
122
+ }
108
123
  /**
109
124
  * Add a new JSON schema to the JSON schema map
110
125
  * @param propertyName The name of the property to assign this JSON schema to
111
126
  */
112
127
  addNewSchema(propertyName) {
113
- this.jsonSchemaMap.set(propertyName, {
128
+ Schema.jsonSchemaMap.get(this.customElementName).set(propertyName, {
114
129
  $schema: "https://json-schema.org/draft/2019-09/schema",
115
- $id: `https://fast.design/schemas/${this.customElementName}/${propertyName}.json`,
130
+ $id: this.getSchemaId(this.customElementName, propertyName),
116
131
  [defsPropertyName]: {},
117
132
  });
118
133
  }
@@ -122,7 +137,7 @@ export class Schema {
122
137
  * @param splitPath The path split into property/context names
123
138
  * @param context The paths context
124
139
  */
125
- addPropertiesToAContext(schema, splitPath, context) {
140
+ addPropertiesToAContext(schema, splitPath, context, childRef) {
126
141
  schema.type = "object";
127
142
  if (schema.properties && !schema.properties[splitPath[0]]) {
128
143
  schema.properties[splitPath[0]] = {};
@@ -133,7 +148,17 @@ export class Schema {
133
148
  };
134
149
  }
135
150
  if (splitPath.length > 1) {
136
- this.addPropertiesToAnObject(schema.properties[splitPath[0]], splitPath.slice(1), context);
151
+ this.addPropertiesToAnObject(schema.properties[splitPath[0]], splitPath.slice(1), context, childRef);
152
+ }
153
+ else if (childRef) {
154
+ if (schema.properties[splitPath[0]].anyOf) {
155
+ schema.properties[splitPath[0]].anyOf.push({
156
+ [refPropertyName]: childRef,
157
+ });
158
+ }
159
+ else {
160
+ schema.properties[splitPath[0]].anyOf = [{ [refPropertyName]: childRef }];
161
+ }
137
162
  }
138
163
  }
139
164
  /**
@@ -143,7 +168,7 @@ export class Schema {
143
168
  * @param context The paths context
144
169
  * @param type The data type (see JSON schema for details)
145
170
  */
146
- addPropertiesToAnObject(schema, splitPath, context, type = "object") {
171
+ addPropertiesToAnObject(schema, splitPath, context, childRef, type = "object") {
147
172
  schema.type = "object";
148
173
  if (schema.properties && !schema.properties[splitPath[0]]) {
149
174
  schema.properties[splitPath[0]] = {};
@@ -154,16 +179,22 @@ export class Schema {
154
179
  };
155
180
  }
156
181
  if (type === "object" && splitPath.length > 1) {
157
- return this.addPropertiesToAnObject(schema.properties[splitPath[0]], splitPath.slice(1), context, type);
182
+ return this.addPropertiesToAnObject(schema.properties[splitPath[0]], splitPath.slice(1), context, childRef, type);
158
183
  }
159
184
  else if (type === "array") {
160
185
  if (splitPath.length > 1) {
161
- return this.addPropertiesToAnObject(schema.properties[splitPath[0]], splitPath.slice(1), context, type);
186
+ return this.addPropertiesToAnObject(schema.properties[splitPath[0]], splitPath.slice(1), context, childRef, type);
162
187
  }
163
188
  else {
164
189
  return this.addArrayToAnObject(schema.properties[splitPath[0]], context);
165
190
  }
166
191
  }
192
+ if (schema.properties[splitPath[0]].anyOf && childRef) {
193
+ schema.properties[splitPath[0]].anyOf.push({ [refPropertyName]: childRef });
194
+ }
195
+ else if (childRef) {
196
+ schema.properties[splitPath[0]].anyOf = [{ [refPropertyName]: childRef }];
197
+ }
167
198
  return schema.properties[splitPath[0]];
168
199
  }
169
200
  /**
@@ -213,3 +244,7 @@ export class Schema {
213
244
  return this.getParentContexts(schema, parentParentContext.at(-1), [parentContext, ...contexts]);
214
245
  }
215
246
  }
247
+ /**
248
+ * A JSON schema describing each root schema
249
+ */
250
+ Schema.jsonSchemaMap = new Map();