@player-ui/player 0.8.0--canary.307.9621 → 0.8.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.
- package/dist/Player.native.js +11630 -0
- package/dist/Player.native.js.map +1 -0
- package/dist/cjs/index.cjs +5626 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/{index.esm.js → index.legacy-esm.js} +2044 -1667
- package/dist/{index.cjs.js → index.mjs} +2052 -1761
- package/dist/index.mjs.map +1 -0
- package/package.json +29 -63
- package/src/__tests__/data.test.ts +498 -0
- package/src/__tests__/flow.test.ts +312 -0
- package/src/__tests__/helpers/action-exp.plugin.ts +22 -0
- package/src/__tests__/helpers/actions.flow.ts +67 -0
- package/src/__tests__/helpers/binding.plugin.ts +125 -0
- package/src/__tests__/helpers/expression.plugin.ts +88 -0
- package/src/__tests__/helpers/transform-plugin.ts +19 -0
- package/src/__tests__/helpers/validation.flow.ts +56 -0
- package/src/__tests__/player.test.ts +597 -0
- package/src/__tests__/string-resolver.test.ts +186 -0
- package/src/__tests__/validation.test.ts +3555 -0
- package/src/__tests__/view.test.ts +715 -0
- package/src/binding/__tests__/binding.test.ts +113 -0
- package/src/binding/__tests__/index.test.ts +208 -0
- package/src/binding/__tests__/resolver.test.ts +83 -0
- package/src/binding/binding.ts +6 -6
- package/src/binding/index.ts +34 -34
- package/src/binding/resolver.ts +19 -19
- package/src/binding/utils.ts +7 -7
- package/src/binding-grammar/__tests__/parser.test.ts +64 -0
- package/src/binding-grammar/__tests__/test-utils/ast-cases.ts +198 -0
- package/src/binding-grammar/__tests__/test-utils/perf-test.ts +66 -0
- package/src/binding-grammar/ast.ts +11 -11
- package/src/binding-grammar/custom/index.ts +19 -22
- package/src/binding-grammar/ebnf/index.ts +20 -21
- package/src/binding-grammar/ebnf/types.ts +13 -13
- package/src/binding-grammar/index.ts +4 -4
- package/src/binding-grammar/parsimmon/index.ts +14 -14
- package/src/controllers/constants/__tests__/index.test.ts +106 -0
- package/src/controllers/constants/index.ts +3 -3
- package/src/controllers/constants/utils.ts +4 -4
- package/src/controllers/data/controller.ts +22 -22
- package/src/controllers/data/index.ts +1 -1
- package/src/controllers/data/utils.ts +7 -7
- package/src/controllers/flow/__tests__/controller.test.ts +195 -0
- package/src/controllers/flow/__tests__/flow.test.ts +381 -0
- package/src/controllers/flow/controller.ts +13 -13
- package/src/controllers/flow/flow.ts +23 -23
- package/src/controllers/flow/index.ts +2 -2
- package/src/controllers/index.ts +5 -5
- package/src/controllers/validation/binding-tracker.ts +71 -59
- package/src/controllers/validation/controller.ts +104 -104
- package/src/controllers/validation/index.ts +2 -2
- package/src/controllers/view/asset-transform.ts +20 -20
- package/src/controllers/view/controller.ts +27 -27
- package/src/controllers/view/index.ts +4 -4
- package/src/controllers/view/store.ts +3 -3
- package/src/controllers/view/types.ts +7 -7
- package/src/data/__tests__/__snapshots__/dependency-tracker.test.ts.snap +64 -0
- package/src/data/__tests__/dependency-tracker.test.ts +146 -0
- package/src/data/__tests__/local-model.test.ts +46 -0
- package/src/data/__tests__/model.test.ts +78 -0
- package/src/data/dependency-tracker.ts +16 -16
- package/src/data/index.ts +4 -4
- package/src/data/local-model.ts +6 -6
- package/src/data/model.ts +17 -17
- package/src/data/noop-model.ts +1 -1
- package/src/expressions/__tests__/__snapshots__/parser.test.ts.snap +854 -0
- package/src/expressions/__tests__/evaluator-functions.test.ts +47 -0
- package/src/expressions/__tests__/evaluator.test.ts +410 -0
- package/src/expressions/__tests__/parser.test.ts +115 -0
- package/src/expressions/__tests__/utils.test.ts +44 -0
- package/src/expressions/evaluator-functions.ts +6 -6
- package/src/expressions/evaluator.ts +71 -67
- package/src/expressions/index.ts +4 -4
- package/src/expressions/parser.ts +102 -105
- package/src/expressions/types.ts +29 -21
- package/src/expressions/utils.ts +32 -21
- package/src/index.ts +13 -13
- package/src/logger/__tests__/consoleLogger.test.ts +46 -0
- package/src/logger/__tests__/noopLogger.test.ts +13 -0
- package/src/logger/__tests__/proxyLogger.test.ts +31 -0
- package/src/logger/__tests__/tapableLogger.test.ts +41 -0
- package/src/logger/consoleLogger.ts +9 -9
- package/src/logger/index.ts +5 -5
- package/src/logger/noopLogger.ts +1 -1
- package/src/logger/proxyLogger.ts +6 -6
- package/src/logger/tapableLogger.ts +7 -7
- package/src/logger/types.ts +2 -2
- package/src/player.ts +60 -58
- package/src/plugins/default-exp-plugin.ts +10 -10
- package/src/plugins/default-view-plugin.ts +29 -0
- package/src/plugins/flow-exp-plugin.ts +6 -6
- package/src/schema/__tests__/schema.test.ts +243 -0
- package/src/schema/index.ts +2 -2
- package/src/schema/schema.ts +24 -24
- package/src/schema/types.ts +4 -4
- package/src/string-resolver/__tests__/index.test.ts +361 -0
- package/src/string-resolver/index.ts +17 -17
- package/src/types.ts +17 -17
- package/src/utils/__tests__/replaceParams.test.ts +33 -0
- package/src/utils/index.ts +1 -1
- package/src/utils/replaceParams.ts +1 -1
- package/src/validator/__tests__/binding-map-splice.test.ts +53 -0
- package/src/validator/__tests__/validation-middleware.test.ts +127 -0
- package/src/validator/binding-map-splice.ts +5 -5
- package/src/validator/index.ts +4 -4
- package/src/validator/registry.ts +1 -1
- package/src/validator/types.ts +13 -13
- package/src/validator/validation-middleware.ts +15 -15
- package/src/view/__tests__/view.immutable.test.ts +269 -0
- package/src/view/__tests__/view.test.ts +959 -0
- package/src/view/builder/index.test.ts +69 -0
- package/src/view/builder/index.ts +3 -3
- package/src/view/index.ts +5 -5
- package/src/view/parser/__tests__/__snapshots__/parser.test.ts.snap +394 -0
- package/src/view/parser/__tests__/parser.test.ts +264 -0
- package/src/view/parser/index.ts +43 -33
- package/src/view/parser/types.ts +11 -11
- package/src/view/parser/utils.ts +5 -5
- package/src/view/plugins/__tests__/__snapshots__/template.test.ts.snap +278 -0
- package/src/view/plugins/__tests__/applicability.test.ts +265 -0
- package/src/view/plugins/__tests__/string.test.ts +122 -0
- package/src/view/plugins/__tests__/template.test.ts +724 -0
- package/src/view/plugins/applicability.ts +19 -19
- package/src/view/plugins/index.ts +4 -5
- package/src/view/plugins/options.ts +1 -1
- package/src/view/plugins/string-resolver.ts +22 -22
- package/src/view/plugins/switch.ts +22 -23
- package/src/view/plugins/template-plugin.ts +26 -27
- package/src/view/resolver/__tests__/dependencies.test.ts +321 -0
- package/src/view/resolver/__tests__/edgecases.test.ts +626 -0
- package/src/view/resolver/index.ts +42 -42
- package/src/view/resolver/types.ts +21 -20
- package/src/view/resolver/utils.ts +9 -9
- package/src/view/view.ts +32 -22
- package/types/binding/binding.d.ts +50 -0
- package/types/binding/index.d.ts +29 -0
- package/types/binding/resolver.d.ts +26 -0
- package/types/binding/utils.d.ts +12 -0
- package/types/binding-grammar/ast.d.ts +67 -0
- package/types/binding-grammar/custom/index.d.ts +4 -0
- package/types/binding-grammar/ebnf/index.d.ts +4 -0
- package/types/binding-grammar/ebnf/types.d.ts +75 -0
- package/types/binding-grammar/index.d.ts +5 -0
- package/types/binding-grammar/parsimmon/index.d.ts +4 -0
- package/types/controllers/constants/index.d.ts +45 -0
- package/types/controllers/constants/utils.d.ts +6 -0
- package/types/controllers/data/controller.d.ts +45 -0
- package/types/controllers/data/index.d.ts +2 -0
- package/types/controllers/data/utils.d.ts +14 -0
- package/types/controllers/flow/controller.d.ts +25 -0
- package/types/controllers/flow/flow.d.ts +50 -0
- package/types/controllers/flow/index.d.ts +3 -0
- package/types/controllers/index.d.ts +6 -0
- package/types/controllers/validation/binding-tracker.d.ts +32 -0
- package/types/controllers/validation/controller.d.ts +151 -0
- package/types/controllers/validation/index.d.ts +3 -0
- package/types/controllers/view/asset-transform.d.ts +19 -0
- package/types/controllers/view/controller.d.ts +37 -0
- package/types/controllers/view/index.d.ts +5 -0
- package/types/controllers/view/store.d.ts +20 -0
- package/types/controllers/view/types.d.ts +16 -0
- package/types/data/dependency-tracker.d.ts +49 -0
- package/types/data/index.d.ts +5 -0
- package/types/data/local-model.d.ts +16 -0
- package/types/data/model.d.ts +86 -0
- package/types/data/noop-model.d.ts +13 -0
- package/types/expressions/evaluator-functions.d.ts +15 -0
- package/types/expressions/evaluator.d.ts +52 -0
- package/types/expressions/index.d.ts +5 -0
- package/types/expressions/parser.d.ts +10 -0
- package/types/expressions/types.d.ts +144 -0
- package/types/expressions/utils.d.ts +12 -0
- package/types/index.d.ts +14 -0
- package/types/logger/consoleLogger.d.ts +17 -0
- package/types/logger/index.d.ts +6 -0
- package/types/logger/noopLogger.d.ts +10 -0
- package/types/logger/proxyLogger.d.ts +15 -0
- package/types/logger/tapableLogger.d.ts +23 -0
- package/types/logger/types.d.ts +6 -0
- package/types/player.d.ts +101 -0
- package/types/plugins/default-exp-plugin.d.ts +9 -0
- package/types/plugins/default-view-plugin.d.ts +9 -0
- package/types/plugins/flow-exp-plugin.d.ts +11 -0
- package/types/schema/index.d.ts +3 -0
- package/types/schema/schema.d.ts +36 -0
- package/types/schema/types.d.ts +38 -0
- package/types/string-resolver/index.d.ts +30 -0
- package/types/types.d.ts +73 -0
- package/types/utils/index.d.ts +2 -0
- package/types/utils/replaceParams.d.ts +9 -0
- package/types/validator/binding-map-splice.d.ts +10 -0
- package/types/validator/index.d.ts +5 -0
- package/types/validator/registry.d.ts +11 -0
- package/types/validator/types.d.ts +53 -0
- package/types/validator/validation-middleware.d.ts +36 -0
- package/types/view/builder/index.d.ts +35 -0
- package/types/view/index.d.ts +6 -0
- package/types/view/parser/index.d.ts +52 -0
- package/types/view/parser/types.d.ts +109 -0
- package/types/view/parser/utils.d.ts +6 -0
- package/types/view/plugins/applicability.d.ts +10 -0
- package/types/view/plugins/index.d.ts +5 -0
- package/types/view/plugins/options.d.ts +4 -0
- package/types/view/plugins/string-resolver.d.ts +13 -0
- package/types/view/plugins/switch.d.ts +14 -0
- package/types/view/plugins/template-plugin.d.ts +33 -0
- package/types/view/resolver/index.d.ts +73 -0
- package/types/view/resolver/types.d.ts +129 -0
- package/types/view/resolver/utils.d.ts +11 -0
- package/types/view/view.d.ts +37 -0
- package/dist/index.d.ts +0 -1814
- package/dist/player.dev.js +0 -11472
- package/dist/player.prod.js +0 -2
- package/src/view/plugins/plugin.ts +0 -21
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { omit } from
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
4
|
-
import type {
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
1
|
+
import { omit } from "timm";
|
|
2
|
+
import type { Options } from "./options";
|
|
3
|
+
import type { Resolver } from "../resolver";
|
|
4
|
+
import type { Node, ParseObjectOptions, Parser } from "../parser";
|
|
5
|
+
import { NodeType } from "../parser";
|
|
6
|
+
import { ViewInstance, ViewPlugin } from "../view";
|
|
7
7
|
|
|
8
8
|
/** A view plugin to remove inapplicable assets from the tree */
|
|
9
9
|
export default class ApplicabilityPlugin implements ViewPlugin {
|
|
10
10
|
applyResolver(resolver: Resolver) {
|
|
11
11
|
resolver.hooks.beforeResolve.tap(
|
|
12
|
-
|
|
12
|
+
"applicability",
|
|
13
13
|
(node: Node.Node | null, options: Options) => {
|
|
14
14
|
let newNode = node;
|
|
15
15
|
|
|
@@ -24,31 +24,31 @@ export default class ApplicabilityPlugin implements ViewPlugin {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
return newNode;
|
|
27
|
-
}
|
|
27
|
+
},
|
|
28
28
|
);
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
applyParser(parser: Parser) {
|
|
32
32
|
/** Switches resolved during the parsing phase are static */
|
|
33
|
-
parser.hooks.determineNodeType.tap(
|
|
34
|
-
if (Object.prototype.hasOwnProperty.call(obj,
|
|
33
|
+
parser.hooks.determineNodeType.tap("applicability", (obj: any) => {
|
|
34
|
+
if (Object.prototype.hasOwnProperty.call(obj, "applicability")) {
|
|
35
35
|
return NodeType.Applicability;
|
|
36
36
|
}
|
|
37
37
|
});
|
|
38
38
|
|
|
39
39
|
parser.hooks.parseNode.tap(
|
|
40
|
-
|
|
40
|
+
"applicability",
|
|
41
41
|
(
|
|
42
42
|
obj: any,
|
|
43
43
|
nodeType: Node.ChildrenTypes,
|
|
44
44
|
options: ParseObjectOptions,
|
|
45
|
-
determinedNodeType: null | NodeType
|
|
45
|
+
determinedNodeType: null | NodeType,
|
|
46
46
|
) => {
|
|
47
47
|
if (determinedNodeType === NodeType.Applicability) {
|
|
48
48
|
const parsedApplicability = parser.parseObject(
|
|
49
|
-
omit(obj,
|
|
49
|
+
omit(obj, "applicability"),
|
|
50
50
|
nodeType,
|
|
51
|
-
options
|
|
51
|
+
options,
|
|
52
52
|
);
|
|
53
53
|
if (parsedApplicability !== null) {
|
|
54
54
|
const applicabilityNode = parser.createASTNode(
|
|
@@ -57,7 +57,7 @@ export default class ApplicabilityPlugin implements ViewPlugin {
|
|
|
57
57
|
expression: (obj as any).applicability,
|
|
58
58
|
value: parsedApplicability,
|
|
59
59
|
},
|
|
60
|
-
obj
|
|
60
|
+
obj,
|
|
61
61
|
);
|
|
62
62
|
|
|
63
63
|
if (applicabilityNode?.type === NodeType.Applicability) {
|
|
@@ -67,12 +67,12 @@ export default class ApplicabilityPlugin implements ViewPlugin {
|
|
|
67
67
|
return applicabilityNode;
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
|
-
}
|
|
70
|
+
},
|
|
71
71
|
);
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
-
apply(view:
|
|
75
|
-
view.hooks.resolver.tap(
|
|
76
|
-
view.hooks.parser.tap(
|
|
74
|
+
apply(view: ViewInstance) {
|
|
75
|
+
view.hooks.resolver.tap("applicability", this.applyResolver.bind(this));
|
|
76
|
+
view.hooks.parser.tap("applicability", this.applyParser.bind(this));
|
|
77
77
|
}
|
|
78
78
|
}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
export { default as TemplatePlugin } from
|
|
2
|
-
export { default as StringResolverPlugin } from
|
|
3
|
-
export { default as ApplicabilityPlugin } from
|
|
4
|
-
export { default as SwitchPlugin } from
|
|
5
|
-
export type { ViewPlugin } from './plugin';
|
|
1
|
+
export { default as TemplatePlugin } from "./template-plugin";
|
|
2
|
+
export { default as StringResolverPlugin } from "./string-resolver";
|
|
3
|
+
export { default as ApplicabilityPlugin } from "./applicability";
|
|
4
|
+
export { default as SwitchPlugin } from "./switch";
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { set } from
|
|
2
|
-
import { resolveDataRefs } from
|
|
3
|
-
import type { Options } from
|
|
4
|
-
import type {
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
7
|
-
import
|
|
1
|
+
import { set } from "timm";
|
|
2
|
+
import { resolveDataRefs } from "../../string-resolver";
|
|
3
|
+
import type { Options } from "./options";
|
|
4
|
+
import type { Node } from "../parser";
|
|
5
|
+
import { NodeType } from "../parser";
|
|
6
|
+
import type { Resolver } from "../resolver";
|
|
7
|
+
import { ViewInstance, ViewPlugin } from "../view";
|
|
8
8
|
|
|
9
9
|
/** Create a function that checks for a start/end sequence in a string */
|
|
10
10
|
const createPatternMatcher = (start: string, end: string) => {
|
|
@@ -25,8 +25,8 @@ const createPatternMatcher = (start: string, end: string) => {
|
|
|
25
25
|
};
|
|
26
26
|
};
|
|
27
27
|
|
|
28
|
-
const bindingResolveLookup = createPatternMatcher(
|
|
29
|
-
const expressionResolveLookup = createPatternMatcher(
|
|
28
|
+
const bindingResolveLookup = createPatternMatcher("{{", "}}");
|
|
29
|
+
const expressionResolveLookup = createPatternMatcher("@[", "]@");
|
|
30
30
|
|
|
31
31
|
/** Check to see if a string contains a reference to dynamic content */
|
|
32
32
|
function hasSomethingToResolve(str: string) {
|
|
@@ -47,17 +47,17 @@ function resolveString(str: string, resolveOptions: Options) {
|
|
|
47
47
|
export function resolveAllRefs(
|
|
48
48
|
node: any,
|
|
49
49
|
resolveOptions: Options,
|
|
50
|
-
propertiesToSkip: Set<string | number
|
|
50
|
+
propertiesToSkip: Set<string | number>,
|
|
51
51
|
): any {
|
|
52
52
|
if (
|
|
53
53
|
node === null ||
|
|
54
54
|
node === undefined ||
|
|
55
|
-
(typeof node !==
|
|
55
|
+
(typeof node !== "object" && typeof node !== "string")
|
|
56
56
|
) {
|
|
57
57
|
return node;
|
|
58
58
|
}
|
|
59
59
|
|
|
60
|
-
if (typeof node ===
|
|
60
|
+
if (typeof node === "string") {
|
|
61
61
|
return resolveString(node, resolveOptions);
|
|
62
62
|
}
|
|
63
63
|
|
|
@@ -72,9 +72,9 @@ export function resolveAllRefs(
|
|
|
72
72
|
|
|
73
73
|
let newVal = val;
|
|
74
74
|
|
|
75
|
-
if (typeof val ===
|
|
75
|
+
if (typeof val === "object") {
|
|
76
76
|
newVal = resolveAllRefs(val, resolveOptions, propertiesToSkip);
|
|
77
|
-
} else if (typeof val ===
|
|
77
|
+
} else if (typeof val === "string") {
|
|
78
78
|
newVal = resolveString(val, resolveOptions);
|
|
79
79
|
}
|
|
80
80
|
|
|
@@ -89,14 +89,14 @@ export function resolveAllRefs(
|
|
|
89
89
|
/** Traverse up the node tree finding the first available 'path' */
|
|
90
90
|
const findBasePath = (
|
|
91
91
|
node: Node.Node,
|
|
92
|
-
resolver: Resolver
|
|
92
|
+
resolver: Resolver,
|
|
93
93
|
): Node.PathSegment[] => {
|
|
94
94
|
const parentNode = node.parent;
|
|
95
95
|
if (!parentNode) {
|
|
96
96
|
return [];
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
-
if (
|
|
99
|
+
if ("children" in parentNode) {
|
|
100
100
|
const original = resolver.getSourceNode(node);
|
|
101
101
|
return (
|
|
102
102
|
parentNode.children?.find((child) => child.value === original)?.path ?? []
|
|
@@ -119,7 +119,7 @@ export default class StringResolverPlugin implements ViewPlugin {
|
|
|
119
119
|
}
|
|
120
120
|
|
|
121
121
|
applyResolver(resolver: Resolver) {
|
|
122
|
-
resolver.hooks.resolve.tap(
|
|
122
|
+
resolver.hooks.resolve.tap("string-resolver", (value, node, options) => {
|
|
123
123
|
if (node.type === NodeType.Empty || node.type === NodeType.Unknown) {
|
|
124
124
|
return null;
|
|
125
125
|
}
|
|
@@ -133,7 +133,7 @@ export default class StringResolverPlugin implements ViewPlugin {
|
|
|
133
133
|
let propsToSkip: Set<string>;
|
|
134
134
|
if (node.type === NodeType.Asset || node.type === NodeType.View) {
|
|
135
135
|
propsToSkip = new Set(
|
|
136
|
-
node.plugins?.stringResolver?.propertiesToSkip ?? [
|
|
136
|
+
node.plugins?.stringResolver?.propertiesToSkip ?? ["exp"],
|
|
137
137
|
);
|
|
138
138
|
if (node.value?.id) {
|
|
139
139
|
this.propertiesToSkipCache.set(node.value.id, propsToSkip);
|
|
@@ -146,10 +146,10 @@ export default class StringResolverPlugin implements ViewPlugin {
|
|
|
146
146
|
this.propertiesToSkipCache.has(node.parent.parent.value.id)
|
|
147
147
|
) {
|
|
148
148
|
propsToSkip = this.propertiesToSkipCache.get(
|
|
149
|
-
node.parent.parent.value.id
|
|
149
|
+
node.parent.parent.value.id,
|
|
150
150
|
) as Set<string>;
|
|
151
151
|
} else {
|
|
152
|
-
propsToSkip = new Set([
|
|
152
|
+
propsToSkip = new Set(["exp"]);
|
|
153
153
|
}
|
|
154
154
|
|
|
155
155
|
const nodePath = findBasePath(node, resolver);
|
|
@@ -169,7 +169,7 @@ export default class StringResolverPlugin implements ViewPlugin {
|
|
|
169
169
|
});
|
|
170
170
|
}
|
|
171
171
|
|
|
172
|
-
apply(view:
|
|
173
|
-
view.hooks.resolver.tap(
|
|
172
|
+
apply(view: ViewInstance) {
|
|
173
|
+
view.hooks.resolver.tap("string-resolver", this.applyResolver.bind(this));
|
|
174
174
|
}
|
|
175
175
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
1
|
+
import type { Options } from "./options";
|
|
2
|
+
import type { Parser, Node, ParseObjectOptions } from "../parser";
|
|
3
|
+
import { EMPTY_NODE, NodeType } from "../parser";
|
|
4
|
+
import type { Resolver } from "../resolver";
|
|
5
|
+
import { ViewInstance, ViewPlugin } from "../view";
|
|
6
6
|
|
|
7
7
|
/** A view plugin to resolve switches */
|
|
8
8
|
export default class SwitchPlugin implements ViewPlugin {
|
|
@@ -15,7 +15,6 @@ export default class SwitchPlugin implements ViewPlugin {
|
|
|
15
15
|
private resolveSwitch(node: Node.Switch, options: Options): Node.Node {
|
|
16
16
|
for (const switchCase of node.cases) {
|
|
17
17
|
const isApplicable = options.evaluate(switchCase.case);
|
|
18
|
-
|
|
19
18
|
if (isApplicable) {
|
|
20
19
|
return switchCase.value;
|
|
21
20
|
}
|
|
@@ -26,7 +25,7 @@ export default class SwitchPlugin implements ViewPlugin {
|
|
|
26
25
|
|
|
27
26
|
applyParser(parser: Parser) {
|
|
28
27
|
/** Switches resolved during the parsing phase are static */
|
|
29
|
-
parser.hooks.onCreateASTNode.tap(
|
|
28
|
+
parser.hooks.onCreateASTNode.tap("switch", (node) => {
|
|
30
29
|
if (node && node.type === NodeType.Switch && !node.dynamic) {
|
|
31
30
|
return this.resolveSwitch(node, this.options);
|
|
32
31
|
}
|
|
@@ -34,27 +33,27 @@ export default class SwitchPlugin implements ViewPlugin {
|
|
|
34
33
|
return node;
|
|
35
34
|
});
|
|
36
35
|
|
|
37
|
-
parser.hooks.determineNodeType.tap(
|
|
36
|
+
parser.hooks.determineNodeType.tap("switch", (obj) => {
|
|
38
37
|
if (
|
|
39
|
-
Object.prototype.hasOwnProperty.call(obj,
|
|
40
|
-
Object.prototype.hasOwnProperty.call(obj,
|
|
38
|
+
Object.prototype.hasOwnProperty.call(obj, "dynamicSwitch") ||
|
|
39
|
+
Object.prototype.hasOwnProperty.call(obj, "staticSwitch")
|
|
41
40
|
) {
|
|
42
41
|
return NodeType.Switch;
|
|
43
42
|
}
|
|
44
43
|
});
|
|
45
44
|
|
|
46
45
|
parser.hooks.parseNode.tap(
|
|
47
|
-
|
|
46
|
+
"switch",
|
|
48
47
|
(
|
|
49
48
|
obj: any,
|
|
50
|
-
|
|
49
|
+
_nodeType: Node.ChildrenTypes,
|
|
51
50
|
options: ParseObjectOptions,
|
|
52
|
-
determinedNodeType: null | NodeType
|
|
51
|
+
determinedNodeType: null | NodeType,
|
|
53
52
|
) => {
|
|
54
53
|
if (determinedNodeType === NodeType.Switch) {
|
|
55
|
-
const dynamic =
|
|
54
|
+
const dynamic = "dynamicSwitch" in obj;
|
|
56
55
|
const switchContent =
|
|
57
|
-
|
|
56
|
+
"dynamicSwitch" in obj ? obj.dynamicSwitch : obj.staticSwitch;
|
|
58
57
|
|
|
59
58
|
const cases: Node.SwitchCase[] = [];
|
|
60
59
|
|
|
@@ -70,7 +69,7 @@ export default class SwitchPlugin implements ViewPlugin {
|
|
|
70
69
|
const value = parser.parseObject(
|
|
71
70
|
switchBody,
|
|
72
71
|
NodeType.Value,
|
|
73
|
-
options
|
|
72
|
+
options,
|
|
74
73
|
);
|
|
75
74
|
|
|
76
75
|
if (value) {
|
|
@@ -79,7 +78,7 @@ export default class SwitchPlugin implements ViewPlugin {
|
|
|
79
78
|
value: value as Node.Value,
|
|
80
79
|
});
|
|
81
80
|
}
|
|
82
|
-
}
|
|
81
|
+
},
|
|
83
82
|
);
|
|
84
83
|
|
|
85
84
|
const switchAST = parser.hooks.onCreateASTNode.call(
|
|
@@ -88,7 +87,7 @@ export default class SwitchPlugin implements ViewPlugin {
|
|
|
88
87
|
dynamic,
|
|
89
88
|
cases,
|
|
90
89
|
},
|
|
91
|
-
obj
|
|
90
|
+
obj,
|
|
92
91
|
);
|
|
93
92
|
|
|
94
93
|
if (switchAST?.type === NodeType.Switch) {
|
|
@@ -104,13 +103,13 @@ export default class SwitchPlugin implements ViewPlugin {
|
|
|
104
103
|
|
|
105
104
|
return switchAST ?? null;
|
|
106
105
|
}
|
|
107
|
-
}
|
|
106
|
+
},
|
|
108
107
|
);
|
|
109
108
|
}
|
|
110
109
|
|
|
111
110
|
applyResolver(resolver: Resolver) {
|
|
112
111
|
/** Switches resolved during the parsing phase are dynamic */
|
|
113
|
-
resolver.hooks.beforeResolve.tap(
|
|
112
|
+
resolver.hooks.beforeResolve.tap("switch", (node, options) => {
|
|
114
113
|
if (node && node.type === NodeType.Switch && node.dynamic) {
|
|
115
114
|
return this.resolveSwitch(node, options);
|
|
116
115
|
}
|
|
@@ -119,8 +118,8 @@ export default class SwitchPlugin implements ViewPlugin {
|
|
|
119
118
|
});
|
|
120
119
|
}
|
|
121
120
|
|
|
122
|
-
apply(view:
|
|
123
|
-
view.hooks.parser.tap(
|
|
124
|
-
view.hooks.resolver.tap(
|
|
121
|
+
apply(view: ViewInstance) {
|
|
122
|
+
view.hooks.parser.tap("switch", this.applyParser.bind(this));
|
|
123
|
+
view.hooks.resolver.tap("switch", this.applyResolver.bind(this));
|
|
125
124
|
}
|
|
126
125
|
}
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { SyncWaterfallHook } from
|
|
2
|
-
import type { Node, ParseObjectOptions, Parser } from
|
|
3
|
-
import { NodeType } from
|
|
4
|
-
import type {
|
|
5
|
-
import type {
|
|
6
|
-
import
|
|
7
|
-
import type { Resolver } from '../resolver';
|
|
1
|
+
import { SyncWaterfallHook } from "tapable-ts";
|
|
2
|
+
import type { Node, ParseObjectOptions, Parser } from "../parser";
|
|
3
|
+
import { NodeType } from "../parser";
|
|
4
|
+
import type { Options } from "./options";
|
|
5
|
+
import type { Resolver } from "../resolver";
|
|
6
|
+
import { ViewInstance, ViewPlugin } from "../view";
|
|
8
7
|
|
|
9
8
|
export interface TemplateItemInfo {
|
|
10
9
|
/** The index of the data for the current iteration of the template */
|
|
@@ -24,7 +23,7 @@ export interface TemplateSubstitution {
|
|
|
24
23
|
|
|
25
24
|
export type TemplateSubstitutionsFunc = (
|
|
26
25
|
baseSubstitutions: TemplateSubstitution[],
|
|
27
|
-
templateItemInfo: TemplateItemInfo
|
|
26
|
+
templateItemInfo: TemplateItemInfo,
|
|
28
27
|
) => TemplateSubstitution[];
|
|
29
28
|
|
|
30
29
|
/** A view plugin to resolve/manage templates */
|
|
@@ -44,7 +43,7 @@ export default class TemplatePlugin implements ViewPlugin {
|
|
|
44
43
|
private parseTemplate(
|
|
45
44
|
parseObject: any,
|
|
46
45
|
node: Node.Template,
|
|
47
|
-
options: Options
|
|
46
|
+
options: Options,
|
|
48
47
|
): Node.Node | null {
|
|
49
48
|
const { template, depth } = node;
|
|
50
49
|
const data = options.data.model.get(node.data);
|
|
@@ -64,7 +63,7 @@ export default class TemplatePlugin implements ViewPlugin {
|
|
|
64
63
|
this.hooks.resolveTemplateSubstitutions.call(
|
|
65
64
|
[
|
|
66
65
|
{
|
|
67
|
-
expression: new RegExp(`_index${depth ||
|
|
66
|
+
expression: new RegExp(`_index${depth || ""}_`),
|
|
68
67
|
value: String(index),
|
|
69
68
|
},
|
|
70
69
|
],
|
|
@@ -72,14 +71,14 @@ export default class TemplatePlugin implements ViewPlugin {
|
|
|
72
71
|
depth,
|
|
73
72
|
data: dataItem,
|
|
74
73
|
index,
|
|
75
|
-
}
|
|
74
|
+
},
|
|
76
75
|
);
|
|
77
76
|
let templateStr = JSON.stringify(template);
|
|
78
77
|
|
|
79
78
|
for (const { expression, value } of templateSubstitutions) {
|
|
80
|
-
let flags =
|
|
81
|
-
if (typeof expression ===
|
|
82
|
-
flags = `${expression.flags}${expression.global ?
|
|
79
|
+
let flags = "g";
|
|
80
|
+
if (typeof expression === "object") {
|
|
81
|
+
flags = `${expression.flags}${expression.global ? "" : "g"}`;
|
|
83
82
|
}
|
|
84
83
|
|
|
85
84
|
templateStr = templateStr.replace(new RegExp(expression, flags), value);
|
|
@@ -104,31 +103,31 @@ export default class TemplatePlugin implements ViewPlugin {
|
|
|
104
103
|
}
|
|
105
104
|
|
|
106
105
|
applyParser(parser: Parser) {
|
|
107
|
-
parser.hooks.onCreateASTNode.tap(
|
|
106
|
+
parser.hooks.onCreateASTNode.tap("template", (node) => {
|
|
108
107
|
if (node && node.type === NodeType.Template && !node.dynamic) {
|
|
109
108
|
return this.parseTemplate(
|
|
110
109
|
parser.parseObject.bind(parser),
|
|
111
110
|
node,
|
|
112
|
-
this.options
|
|
111
|
+
this.options,
|
|
113
112
|
);
|
|
114
113
|
}
|
|
115
114
|
|
|
116
115
|
return node;
|
|
117
116
|
});
|
|
118
117
|
|
|
119
|
-
parser.hooks.determineNodeType.tap(
|
|
120
|
-
if (obj ===
|
|
118
|
+
parser.hooks.determineNodeType.tap("template", (obj: any) => {
|
|
119
|
+
if (obj === "template") {
|
|
121
120
|
return NodeType.Template;
|
|
122
121
|
}
|
|
123
122
|
});
|
|
124
123
|
|
|
125
124
|
parser.hooks.parseNode.tap(
|
|
126
|
-
|
|
125
|
+
"template",
|
|
127
126
|
(
|
|
128
127
|
obj: any,
|
|
129
|
-
|
|
128
|
+
_nodeType: Node.ChildrenTypes,
|
|
130
129
|
options: ParseObjectOptions,
|
|
131
|
-
determinedNodeType: null | NodeType
|
|
130
|
+
determinedNodeType: null | NodeType,
|
|
132
131
|
) => {
|
|
133
132
|
if (determinedNodeType === NodeType.Template) {
|
|
134
133
|
const templateNode = parser.createASTNode(
|
|
@@ -139,19 +138,19 @@ export default class TemplatePlugin implements ViewPlugin {
|
|
|
139
138
|
template: obj.value,
|
|
140
139
|
dynamic: obj.dynamic ?? false,
|
|
141
140
|
},
|
|
142
|
-
obj
|
|
141
|
+
obj,
|
|
143
142
|
);
|
|
144
143
|
|
|
145
144
|
if (templateNode) {
|
|
146
145
|
return templateNode;
|
|
147
146
|
}
|
|
148
147
|
}
|
|
149
|
-
}
|
|
148
|
+
},
|
|
150
149
|
);
|
|
151
150
|
}
|
|
152
151
|
|
|
153
152
|
applyResolverHooks(resolver: Resolver) {
|
|
154
|
-
resolver.hooks.beforeResolve.tap(
|
|
153
|
+
resolver.hooks.beforeResolve.tap("template", (node, options) => {
|
|
155
154
|
if (node && node.type === NodeType.Template && node.dynamic) {
|
|
156
155
|
return this.parseTemplate(options.parseNode, node, options);
|
|
157
156
|
}
|
|
@@ -160,8 +159,8 @@ export default class TemplatePlugin implements ViewPlugin {
|
|
|
160
159
|
});
|
|
161
160
|
}
|
|
162
161
|
|
|
163
|
-
apply(view:
|
|
164
|
-
view.hooks.parser.tap(
|
|
165
|
-
view.hooks.resolver.tap(
|
|
162
|
+
apply(view: ViewInstance) {
|
|
163
|
+
view.hooks.parser.tap("template", this.applyParser.bind(this));
|
|
164
|
+
view.hooks.resolver.tap("template", this.applyResolverHooks.bind(this));
|
|
166
165
|
}
|
|
167
166
|
}
|