@chobantonov/jsonforms-react-extended-renderers 0.0.0-stage → 0.1.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/LICENSE +22 -0
- package/README.md +17 -2
- package/lib/AgGridControlRenderer.impl-BO-Z1x0Y.js +2 -0
- package/lib/AgGridControlRenderer.impl-BO-Z1x0Y.js.map +1 -0
- package/lib/AgGridControlRenderer.impl-BZS8_ocu.mjs +429 -0
- package/lib/AgGridControlRenderer.impl-BZS8_ocu.mjs.map +1 -0
- package/lib/DynamicJSXRenderer-Cyw40Kz0.js +2 -0
- package/lib/DynamicJSXRenderer-Cyw40Kz0.js.map +1 -0
- package/lib/DynamicJSXRenderer-DyB3ZpZV.mjs +109 -0
- package/lib/DynamicJSXRenderer-DyB3ZpZV.mjs.map +1 -0
- package/lib/MarkupLabelRenderer.impl-CWfdcUuH.mjs +12 -0
- package/lib/MarkupLabelRenderer.impl-CWfdcUuH.mjs.map +1 -0
- package/lib/MarkupLabelRenderer.impl-DnuzIPQg.js +2 -0
- package/lib/MarkupLabelRenderer.impl-DnuzIPQg.js.map +1 -0
- package/lib/MonacoControlRenderer.impl-BwgLGNMK.js +3 -0
- package/lib/MonacoControlRenderer.impl-BwgLGNMK.js.map +1 -0
- package/lib/MonacoControlRenderer.impl-CyjMHbOd.mjs +186 -0
- package/lib/MonacoControlRenderer.impl-CyjMHbOd.mjs.map +1 -0
- package/lib/RactiveTemplate-7L44A38e.js +2 -0
- package/lib/RactiveTemplate-7L44A38e.js.map +1 -0
- package/lib/RactiveTemplate-Cat7CxUp.mjs +74 -0
- package/lib/RactiveTemplate-Cat7CxUp.mjs.map +1 -0
- package/lib/ajv-localizers.cjs.js +2 -0
- package/lib/ajv-localizers.cjs.js.map +1 -0
- package/lib/ajv-localizers.esm.js +7 -0
- package/lib/ajv-localizers.esm.js.map +1 -0
- package/lib/ajvI18n-2UTyJv5g.js +2 -0
- package/lib/ajvI18n-2UTyJv5g.js.map +1 -0
- package/lib/ajvI18n-DO8FFvx1.mjs +227 -0
- package/lib/ajvI18n-DO8FFvx1.mjs.map +1 -0
- package/lib/celTemplate-Cl0alaCz.js +3 -0
- package/lib/celTemplate-Cl0alaCz.js.map +1 -0
- package/lib/celTemplate-D_AGhaIL.mjs +137 -0
- package/lib/celTemplate-D_AGhaIL.mjs.map +1 -0
- package/lib/components/DynamicJSXRenderer.d.ts +7 -0
- package/lib/components/ExtendedJsonForms.d.ts +80 -0
- package/lib/components/GenericAdditionalProperties.d.ts +4 -0
- package/lib/components/InterpolatedText.d.ts +34 -0
- package/lib/components/RactiveTemplate.d.ts +37 -0
- package/lib/components/editorRendererTypes.d.ts +139 -0
- package/lib/components/elementRender.d.ts +15 -0
- package/lib/configNamespaces-BFclHtB8.mjs +1 -0
- package/lib/configNamespaces-sboXDudL.js +1 -0
- package/lib/core/ajv.d.ts +82 -0
- package/lib/core/ajvI18n/bg.d.ts +2 -0
- package/lib/core/ajvI18n/index.d.ts +118 -0
- package/lib/core/ajvI18n/localizers.d.ts +20 -0
- package/lib/core/dynamicDefaults.d.ts +34 -0
- package/lib/core/keywords.d.ts +24 -0
- package/lib/core/transform.d.ts +29 -0
- package/lib/core/uiSchema.d.ts +48 -0
- package/lib/elementRender-Dbp2e-Cc.js +2 -0
- package/lib/elementRender-Dbp2e-Cc.js.map +1 -0
- package/lib/elementRender-y8MK9CCE.mjs +5 -0
- package/lib/elementRender-y8MK9CCE.mjs.map +1 -0
- package/lib/index.d.ts +41 -0
- package/lib/jsonforms-react-extended.cjs.js +16 -0
- package/lib/jsonforms-react-extended.cjs.js.map +1 -0
- package/lib/jsonforms-react-extended.esm.js +2335 -0
- package/lib/jsonforms-react-extended.esm.js.map +1 -0
- package/lib/locale/bg.d.ts +14 -0
- package/lib/markdown-C_y73Q0w.mjs +123 -0
- package/lib/markdown-C_y73Q0w.mjs.map +1 -0
- package/lib/markdown-CuqtpGVd.js +2 -0
- package/lib/markdown-CuqtpGVd.js.map +1 -0
- package/lib/objectWithoutProperties-6zedhz76.js +2 -0
- package/lib/objectWithoutProperties-6zedhz76.js.map +1 -0
- package/lib/objectWithoutProperties-yjQMyZ0y.mjs +58 -0
- package/lib/objectWithoutProperties-yjQMyZ0y.mjs.map +1 -0
- package/lib/renderers/AgGridControlRenderer.d.ts +10 -0
- package/lib/renderers/AgGridControlRenderer.impl.d.ts +4 -0
- package/lib/renderers/ButtonRenderer.d.ts +83 -0
- package/lib/renderers/GenericMixedRenderer.d.ts +9 -0
- package/lib/renderers/HorizontalLayoutRenderer.d.ts +5 -0
- package/lib/renderers/ImageViewRenderer.d.ts +48 -0
- package/lib/renderers/LinkRenderer.d.ts +23 -0
- package/lib/renderers/MarkupLabelRenderer.d.ts +35 -0
- package/lib/renderers/MarkupLabelRenderer.impl.d.ts +17 -0
- package/lib/renderers/MonacoControlRenderer.d.ts +8 -0
- package/lib/renderers/MonacoControlRenderer.impl.d.ts +4 -0
- package/lib/renderers/RactiveTemplateLayoutRenderer.d.ts +6 -0
- package/lib/renderers/SeparatorRenderer.d.ts +17 -0
- package/lib/renderers/SlotRenderer.d.ts +5 -0
- package/lib/renderers/SpacerRenderer.d.ts +5 -0
- package/lib/renderers/SplitLayoutRenderer.d.ts +18 -0
- package/lib/renderers/TemplateLayoutRenderer.d.ts +13 -0
- package/lib/renderers/TemplateRenderer.d.ts +17 -0
- package/lib/renderers/TsxTemplateLayoutRenderer.d.ts +111 -0
- package/lib/renderers/index.d.ts +13 -0
- package/lib/renderers/templateEngines.d.ts +43 -0
- package/lib/util/actionContext.d.ts +23 -0
- package/lib/util/additionalErrors.d.ts +77 -0
- package/lib/util/celEnvironment.d.ts +23 -0
- package/lib/util/celTemplate.d.ts +47 -0
- package/lib/util/childNames.d.ts +46 -0
- package/lib/util/configNamespaces.d.ts +1 -0
- package/lib/util/cron.d.ts +122 -0
- package/lib/util/editorControls.d.ts +8 -0
- package/lib/util/editorDiagnostics.d.ts +56 -0
- package/lib/util/extendedControls.d.ts +4 -0
- package/lib/util/extendedLocale.d.ts +32 -0
- package/lib/util/fileFilterValue.d.ts +3 -0
- package/lib/util/formContext.d.ts +54 -0
- package/lib/util/gridColumns.d.ts +6 -0
- package/lib/util/horizontalLayout.d.ts +10 -0
- package/lib/util/i18nDefaults.d.ts +45 -0
- package/lib/util/interpolate.d.ts +105 -0
- package/lib/util/labelFilterValue.d.ts +2 -0
- package/lib/util/lazyControl.d.ts +36 -0
- package/lib/util/lazyTemplate.d.ts +18 -0
- package/lib/util/markdown.d.ts +55 -0
- package/lib/util/markdownEscape.d.ts +27 -0
- package/lib/util/markup.d.ts +74 -0
- package/lib/util/maskControls.d.ts +2 -0
- package/lib/util/templateLang.d.ts +87 -0
- package/lib/util/templateUrls.d.ts +40 -0
- package/lib/util/urlPolicy.d.ts +42 -0
- package/lib/util/useCronPicker.d.ts +24 -0
- package/lib/util/useDurationControl.d.ts +99 -0
- package/lib/util/useEditorAppearance.d.ts +21 -0
- package/lib/util/useExtendedTranslator.d.ts +10 -0
- package/lib/util/useMonacoShadowStyles.d.ts +2 -0
- package/package.json +110 -3
- package/src/components/DynamicJSXRenderer.tsx +274 -0
- package/src/components/ExtendedJsonForms.tsx +329 -0
- package/src/components/GenericAdditionalProperties.tsx +362 -0
- package/src/components/InterpolatedText.tsx +114 -0
- package/src/components/RactiveTemplate.tsx +161 -0
- package/src/components/editorRendererTypes.ts +135 -0
- package/src/components/elementRender.ts +15 -0
- package/src/core/ajv.ts +177 -0
- package/src/core/ajvI18n/bg.ts +203 -0
- package/src/core/ajvI18n/index.ts +267 -0
- package/src/core/ajvI18n/localizers.ts +25 -0
- package/src/core/dynamicDefaults.ts +170 -0
- package/src/core/keywords.ts +71 -0
- package/src/core/transform.ts +137 -0
- package/src/core/uiSchema.ts +53 -0
- package/src/index.tsx +196 -0
- package/src/locale/bg.ts +51 -0
- package/src/renderers/AgGridControlRenderer.impl.tsx +788 -0
- package/src/renderers/AgGridControlRenderer.tsx +24 -0
- package/src/renderers/ButtonRenderer.tsx +328 -0
- package/src/renderers/GenericMixedRenderer.tsx +224 -0
- package/src/renderers/HorizontalLayoutRenderer.tsx +93 -0
- package/src/renderers/ImageViewRenderer.tsx +328 -0
- package/src/renderers/LinkRenderer.tsx +92 -0
- package/src/renderers/MarkupLabelRenderer.impl.tsx +26 -0
- package/src/renderers/MarkupLabelRenderer.tsx +327 -0
- package/src/renderers/MonacoControlRenderer.impl.tsx +351 -0
- package/src/renderers/MonacoControlRenderer.tsx +24 -0
- package/src/renderers/RactiveTemplateLayoutRenderer.tsx +172 -0
- package/src/renderers/SeparatorRenderer.tsx +55 -0
- package/src/renderers/SlotRenderer.tsx +34 -0
- package/src/renderers/SpacerRenderer.tsx +69 -0
- package/src/renderers/SplitLayoutRenderer.tsx +220 -0
- package/src/renderers/TemplateLayoutRenderer.tsx +394 -0
- package/src/renderers/TemplateRenderer.tsx +71 -0
- package/src/renderers/TsxTemplateLayoutRenderer.tsx +258 -0
- package/src/renderers/index.ts +14 -0
- package/src/renderers/templateEngines.tsx +100 -0
- package/src/util/actionContext.ts +29 -0
- package/src/util/additionalErrors.tsx +391 -0
- package/src/util/celEnvironment.ts +300 -0
- package/src/util/celTemplate.ts +267 -0
- package/src/util/childNames.ts +135 -0
- package/src/util/configNamespaces.ts +6 -0
- package/src/util/cron.ts +433 -0
- package/src/util/editorControls.ts +58 -0
- package/src/util/editorDiagnostics.ts +103 -0
- package/src/util/extendedControls.ts +34 -0
- package/src/util/extendedLocale.ts +67 -0
- package/src/util/fileFilterValue.ts +28 -0
- package/src/util/formContext.ts +74 -0
- package/src/util/gridColumns.ts +80 -0
- package/src/util/horizontalLayout.ts +59 -0
- package/src/util/i18nDefaults.ts +80 -0
- package/src/util/interpolate.ts +218 -0
- package/src/util/labelFilterValue.ts +49 -0
- package/src/util/lazyControl.tsx +66 -0
- package/src/util/lazyTemplate.tsx +110 -0
- package/src/util/markdown.tsx +345 -0
- package/src/util/markdownEscape.ts +29 -0
- package/src/util/markup.ts +144 -0
- package/src/util/maskControls.ts +6 -0
- package/src/util/templateLang.ts +145 -0
- package/src/util/templateUrls.ts +274 -0
- package/src/util/urlPolicy.ts +115 -0
- package/src/util/useCronPicker.ts +102 -0
- package/src/util/useDurationControl.ts +272 -0
- package/src/util/useEditorAppearance.ts +86 -0
- package/src/util/useExtendedTranslator.ts +25 -0
- package/src/util/useMonacoShadowStyles.ts +47 -0
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { Translate, evaluateWith } from './celEnvironment';
|
|
2
|
+
import { TemplateSegment, hasExpressions, splitTemplate } from './interpolate';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Evaluating a split template with CEL.
|
|
6
|
+
*
|
|
7
|
+
* The only module that imports the evaluator, so it is the only thing a
|
|
8
|
+
* dynamic import has to pull in. Nothing here decides *whether* to
|
|
9
|
+
* interpolate - that is settled before the chunk is ever requested.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** The evaluator's own first line, which names the cause better than we could. */
|
|
13
|
+
const messageOf = (error: unknown): string =>
|
|
14
|
+
error instanceof Error ? error.message.split('\n')[0] : String(error);
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Whether a failure is the data simply not being there.
|
|
18
|
+
*
|
|
19
|
+
* The distinction this draws is the difference between a bug in the form and
|
|
20
|
+
* a fact about the record being edited:
|
|
21
|
+
*
|
|
22
|
+
* - **An authoring error** is wrong however the form is filled in. A name the
|
|
23
|
+
* text does not declare, a function that does not exist, a type mismatch -
|
|
24
|
+
* these are reported, because someone has to fix the UI schema.
|
|
25
|
+
* - **Absent data is normal.** A record without a middle name, an empty list,
|
|
26
|
+
* an optional object that has not been filled in yet: the expression is
|
|
27
|
+
* correct and the value is not there *at the moment*. Reporting it would
|
|
28
|
+
* put a developer-facing message on the page for every half-filled form,
|
|
29
|
+
* which trains people to ignore diagnostics.
|
|
30
|
+
*
|
|
31
|
+
* The evaluator separates the two cleanly and this is the whole basis for
|
|
32
|
+
* telling them apart: reaching a key that is not present - at any depth, and
|
|
33
|
+
* including an index past the end of a list - is always `No such key`, while
|
|
34
|
+
* an identifier that is not in scope at all is `Unknown variable`.
|
|
35
|
+
*
|
|
36
|
+
* Note this is about keys *beneath* a namespace. A root that is not a
|
|
37
|
+
* namespace at all is `Unknown variable` and stays an error.
|
|
38
|
+
*/
|
|
39
|
+
const isAbsentData = (error: unknown): boolean =>
|
|
40
|
+
messageOf(error).startsWith('No such key:');
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Explains the one failure whose own message explains nothing.
|
|
44
|
+
*
|
|
45
|
+
* A JSON object may have a property called `constructor` - it is a legal key
|
|
46
|
+
* and a schema may well declare one. The evaluator cannot read such an
|
|
47
|
+
* object: its type check is defeated by the shadowed `constructor`, and it
|
|
48
|
+
* rejects the **whole map** with `Unsupported type: object`, so every other
|
|
49
|
+
* field of that object becomes unreadable too. Measured: `__proto__`,
|
|
50
|
+
* `toString`, `valueOf` and `hasOwnProperty` as data keys are all fine; only
|
|
51
|
+
* `constructor` does this.
|
|
52
|
+
*
|
|
53
|
+
* The search runs **only after a failure**, so the common path pays nothing,
|
|
54
|
+
* and it is depth-limited because it exists to name a cause rather than to
|
|
55
|
+
* audit the data.
|
|
56
|
+
*/
|
|
57
|
+
const CONSTRUCTOR_KEY_LIMIT = 4;
|
|
58
|
+
|
|
59
|
+
const findsConstructorKey = (value: unknown, depth = 0): boolean => {
|
|
60
|
+
if (
|
|
61
|
+
depth > CONSTRUCTOR_KEY_LIMIT ||
|
|
62
|
+
value === null ||
|
|
63
|
+
typeof value !== 'object'
|
|
64
|
+
) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
if (Object.prototype.hasOwnProperty.call(value, 'constructor')) {
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
return Object.values(value as Record<string, unknown>).some((child) =>
|
|
71
|
+
findsConstructorKey(child, depth + 1),
|
|
72
|
+
);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const explain = (error: unknown, scope: Record<string, unknown>): string => {
|
|
76
|
+
const message = messageOf(error);
|
|
77
|
+
if (
|
|
78
|
+
message.startsWith('Unsupported type: object') &&
|
|
79
|
+
findsConstructorKey(scope)
|
|
80
|
+
) {
|
|
81
|
+
return `${message} - a value in scope has a property named "constructor", which this evaluator cannot read`;
|
|
82
|
+
}
|
|
83
|
+
return message;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export interface TemplateResult {
|
|
87
|
+
text: string;
|
|
88
|
+
/** One per expression that could not be rendered. Never thrown. */
|
|
89
|
+
failures: string[];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* A CEL value as display text.
|
|
94
|
+
*
|
|
95
|
+
* CEL integers are int64 and arrive as `bigint`, which `String` handles;
|
|
96
|
+
* values taken straight from the form's data keep their JavaScript types.
|
|
97
|
+
*
|
|
98
|
+
* An object or a list has **no** sensible one-line form, so it is refused
|
|
99
|
+
* rather than rendered: `[object Object]` in the middle of a sentence is
|
|
100
|
+
* worse than a diagnostic, because it looks like data and is not.
|
|
101
|
+
*/
|
|
102
|
+
const display = (value: unknown): { text: string; failure?: string } => {
|
|
103
|
+
if (value === null || value === undefined) {
|
|
104
|
+
/*
|
|
105
|
+
Empty, and silent. By the time a value reaches the text its absence is
|
|
106
|
+
a fact about the data rather than a mistake in the form - an optional
|
|
107
|
+
field that has not been filled in renders as nothing, which is what a
|
|
108
|
+
reader expects and what a half-filled form is full of.
|
|
109
|
+
*/
|
|
110
|
+
return { text: '' };
|
|
111
|
+
}
|
|
112
|
+
switch (typeof value) {
|
|
113
|
+
case 'string':
|
|
114
|
+
return { text: value };
|
|
115
|
+
case 'number':
|
|
116
|
+
case 'bigint':
|
|
117
|
+
case 'boolean':
|
|
118
|
+
return { text: String(value) };
|
|
119
|
+
default:
|
|
120
|
+
return {
|
|
121
|
+
text: '',
|
|
122
|
+
failure: `resolved to a ${
|
|
123
|
+
Array.isArray(value) ? 'list' : 'structure'
|
|
124
|
+
}, which has no text form`,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Renders the segments, substituting each expression's value.
|
|
131
|
+
*
|
|
132
|
+
* `escape` is applied to **substituted values only**, never to the literal
|
|
133
|
+
* segments. That distinction is the whole security property when the result
|
|
134
|
+
* is about to be parsed as Markdown: a value containing
|
|
135
|
+
* `[click](javascript:alert(1))` has to render as those characters, while the
|
|
136
|
+
* author's own `**bold**` in the surrounding literal must still work.
|
|
137
|
+
* Escaping the finished string would break the second; escaping nothing would
|
|
138
|
+
* break the first. It is also why the template is split into segments rather
|
|
139
|
+
* than evaluated with a regex replace.
|
|
140
|
+
*
|
|
141
|
+
* An expression that throws does not take the label down: it contributes
|
|
142
|
+
* empty text and a failure, and the caller decides what to show.
|
|
143
|
+
*/
|
|
144
|
+
export const renderTemplate = (
|
|
145
|
+
segments: TemplateSegment[],
|
|
146
|
+
scope: Record<string, unknown>,
|
|
147
|
+
escape: (text: string) => string = (text) => text,
|
|
148
|
+
locale?: string,
|
|
149
|
+
translate?: Translate,
|
|
150
|
+
): TemplateResult => {
|
|
151
|
+
const failures: string[] = [];
|
|
152
|
+
let text = '';
|
|
153
|
+
for (const segment of segments) {
|
|
154
|
+
if (segment.kind === 'literal') {
|
|
155
|
+
text += segment.text;
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
try {
|
|
159
|
+
const { text: rendered, failure } = display(
|
|
160
|
+
evaluateWith(segment.source, scope, locale, translate),
|
|
161
|
+
);
|
|
162
|
+
if (failure) {
|
|
163
|
+
failures.push(`${segment.source.trim()} ${failure}`);
|
|
164
|
+
}
|
|
165
|
+
text += escape(rendered);
|
|
166
|
+
} catch (error) {
|
|
167
|
+
/*
|
|
168
|
+
Absent data contributes empty text and nothing else; anything else is
|
|
169
|
+
an authoring error, and the evaluator's own message names the cause
|
|
170
|
+
better than this layer could. It is developer-facing text, reported
|
|
171
|
+
beside the label rather than thrown.
|
|
172
|
+
*/
|
|
173
|
+
if (!isAbsentData(error)) {
|
|
174
|
+
failures.push(`${segment.source.trim()}: ${explain(error, scope)}`);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return { text, failures };
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Resolves the declared `textParams` against the namespaces.
|
|
183
|
+
*
|
|
184
|
+
* This is the **only** place the form's data is read. A parameter's value is
|
|
185
|
+
* the right-hand side of a declaration in the UI schema, which is where
|
|
186
|
+
* knowledge of the data model belongs; the text it feeds knows only the
|
|
187
|
+
* parameter's name, which is what makes that text translatable.
|
|
188
|
+
*
|
|
189
|
+
* A value is treated as a template, so the same grammar applies one level
|
|
190
|
+
* down: `"Atlas"` is a literal, `"{data.plan}"` resolves, and
|
|
191
|
+
* `"Plan {data.plan}"` does both. A non-string value - a number, a boolean,
|
|
192
|
+
* an object to navigate with `{user.name}` - passes through untouched.
|
|
193
|
+
*
|
|
194
|
+
* Parameters see the namespaces and **not each other**. Allowing one to
|
|
195
|
+
* reference another would need an evaluation order and a cycle check, for a
|
|
196
|
+
* capability that an extra parameter already covers.
|
|
197
|
+
*
|
|
198
|
+
* Values are not escaped here. Escaping belongs to the moment a value is
|
|
199
|
+
* substituted into the text, because that is the string a parser will see.
|
|
200
|
+
*/
|
|
201
|
+
export const resolveTextParams = (
|
|
202
|
+
raw: Record<string, unknown> | undefined,
|
|
203
|
+
namespaceScope: Record<string, unknown>,
|
|
204
|
+
locale?: string,
|
|
205
|
+
translate?: Translate,
|
|
206
|
+
): { params: Record<string, unknown>; failures: string[] } => {
|
|
207
|
+
const params: Record<string, unknown> = {};
|
|
208
|
+
const failures: string[] = [];
|
|
209
|
+
for (const [name, value] of Object.entries(raw ?? {})) {
|
|
210
|
+
if (typeof value !== 'string') {
|
|
211
|
+
params[name] = value;
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
const segments = splitTemplate(value);
|
|
215
|
+
if (!hasExpressions(segments)) {
|
|
216
|
+
/*
|
|
217
|
+
A literal, but still run through the split so the grammar's brace
|
|
218
|
+
escaping applies: `{{x}}` is the literal `{x}` here exactly as it is
|
|
219
|
+
in the text.
|
|
220
|
+
*/
|
|
221
|
+
params[name] = segments
|
|
222
|
+
.map((segment) => (segment.kind === 'literal' ? segment.text : ''))
|
|
223
|
+
.join('');
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
/*
|
|
227
|
+
A value that is exactly one expression keeps the expression's own type,
|
|
228
|
+
so `{data.seats}` stays a number and a comparison against it in the text
|
|
229
|
+
still works. Anything else is string concatenation.
|
|
230
|
+
*/
|
|
231
|
+
if (segments.length === 1 && segments[0].kind === 'expression') {
|
|
232
|
+
try {
|
|
233
|
+
params[name] = evaluateWith(
|
|
234
|
+
segments[0].source,
|
|
235
|
+
namespaceScope,
|
|
236
|
+
locale,
|
|
237
|
+
translate,
|
|
238
|
+
);
|
|
239
|
+
} catch (error) {
|
|
240
|
+
if (!isAbsentData(error)) {
|
|
241
|
+
failures.push(`${name}: ${explain(error, namespaceScope)}`);
|
|
242
|
+
}
|
|
243
|
+
/*
|
|
244
|
+
Empty either way. Where it was an authoring error the cause has just
|
|
245
|
+
been reported at the level it happened, and leaving the name
|
|
246
|
+
unresolved would make the text report it again - one problem, one
|
|
247
|
+
diagnostic. Where the data was simply absent there is nothing to
|
|
248
|
+
report and nothing to show.
|
|
249
|
+
*/
|
|
250
|
+
params[name] = '';
|
|
251
|
+
}
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
const rendered = renderTemplate(
|
|
255
|
+
segments,
|
|
256
|
+
namespaceScope,
|
|
257
|
+
undefined,
|
|
258
|
+
locale,
|
|
259
|
+
translate,
|
|
260
|
+
);
|
|
261
|
+
for (const failure of rendered.failures) {
|
|
262
|
+
failures.push(`${name}: ${failure}`);
|
|
263
|
+
}
|
|
264
|
+
params[name] = rendered.text;
|
|
265
|
+
}
|
|
266
|
+
return { params, failures };
|
|
267
|
+
};
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import { dynamicAuthoredElement } from '@chobantonov/jsonforms-react-renderer-common/dynamic/DynamicUiProvider';
|
|
3
|
+
import type { ExtendedUISchemaElement } from '../core/uiSchema';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Addressing a template's children.
|
|
7
|
+
*
|
|
8
|
+
* Section 13: "each named child is available as a partial that mounts its
|
|
9
|
+
* delegated renderer. Unnamed children receive their decimal index as a
|
|
10
|
+
* fallback name."
|
|
11
|
+
*
|
|
12
|
+
* The fallback is where it gets interesting, and the section does not say what
|
|
13
|
+
* happens when the two collide. They can: a child explicitly named `"0"` wants
|
|
14
|
+
* the same key as the first child's index fallback. The previous
|
|
15
|
+
* implementation let whichever came last win the map, so the **other child was
|
|
16
|
+
* never placed at all** - no partial, no dispatch, no error. A control simply
|
|
17
|
+
* was not on the form, and nothing said why.
|
|
18
|
+
*
|
|
19
|
+
* The rule here: an explicit name always keeps its key, because the author
|
|
20
|
+
* asked for it. An index fallback applies **only if no explicit name has
|
|
21
|
+
* claimed that index**, and when one has, the unnamed child is reported as
|
|
22
|
+
* unaddressable rather than silently dropped or quietly given some other
|
|
23
|
+
* number no template would reference.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
export interface ResolvedChildNames {
|
|
27
|
+
/**
|
|
28
|
+
* One entry per child, in order. `undefined` means the child could not be
|
|
29
|
+
* given an address and no template can place it.
|
|
30
|
+
*/
|
|
31
|
+
names: (string | undefined)[];
|
|
32
|
+
/** Name to child index, for looking a child up from a template. */
|
|
33
|
+
byName: Record<string, number>;
|
|
34
|
+
/** Stable-coded messages for names that could not be assigned. */
|
|
35
|
+
diagnostics: string[];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const resolveChildNames = (
|
|
39
|
+
elements: readonly ExtendedUISchemaElement[] | undefined,
|
|
40
|
+
): ResolvedChildNames => {
|
|
41
|
+
const children = elements ?? [];
|
|
42
|
+
const names: (string | undefined)[] = Array.from(
|
|
43
|
+
{ length: children.length },
|
|
44
|
+
() => undefined,
|
|
45
|
+
);
|
|
46
|
+
const byName: Record<string, number> = {};
|
|
47
|
+
const diagnostics: string[] = [];
|
|
48
|
+
|
|
49
|
+
/*
|
|
50
|
+
Explicit names first, so an index fallback can see every key that is
|
|
51
|
+
already taken - including one belonging to a later child.
|
|
52
|
+
*/
|
|
53
|
+
children.forEach((element, index) => {
|
|
54
|
+
const explicit = element
|
|
55
|
+
? (dynamicAuthoredElement(element) as ExtendedUISchemaElement).name
|
|
56
|
+
: undefined;
|
|
57
|
+
if (typeof explicit !== 'string' || explicit === '') {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
if (byName[explicit] !== undefined) {
|
|
61
|
+
diagnostics.push(
|
|
62
|
+
`template.duplicateChildName: children ${
|
|
63
|
+
byName[explicit]
|
|
64
|
+
} and ${index} are both named ${JSON.stringify(
|
|
65
|
+
explicit,
|
|
66
|
+
)}; the first one keeps the name.`,
|
|
67
|
+
);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
byName[explicit] = index;
|
|
71
|
+
names[index] = explicit;
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
children.forEach((_element, index) => {
|
|
75
|
+
if (names[index] !== undefined) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
const fallback = String(index);
|
|
79
|
+
if (byName[fallback] !== undefined) {
|
|
80
|
+
diagnostics.push(
|
|
81
|
+
`template.childNameCollision: child ${index} has no name and its index ${JSON.stringify(
|
|
82
|
+
fallback,
|
|
83
|
+
)} is already the name of child ${
|
|
84
|
+
byName[fallback]
|
|
85
|
+
}, so it cannot be placed. Give it a name.`,
|
|
86
|
+
);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
byName[fallback] = index;
|
|
90
|
+
names[index] = fallback;
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
return { names, byName, diagnostics };
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Attributes a Ractive template will silently eat.
|
|
98
|
+
*
|
|
99
|
+
* Ractive's parser reads **any** attribute matching `name-in`, `name-out` or
|
|
100
|
+
* `name-in-out` as a transition directive:
|
|
101
|
+
*
|
|
102
|
+
* ```js
|
|
103
|
+
* // ractive.js
|
|
104
|
+
* var transitionPattern = /^([a-zA-Z](?:(?!-in-out)[-a-zA-Z_0-9])*)-(in|out|in-out)$/;
|
|
105
|
+
* ```
|
|
106
|
+
*
|
|
107
|
+
* So `<p data-out>` is parsed as the transition named `data` and never
|
|
108
|
+
* reaches the DOM, while `data-greeting` beside it is untouched. It is
|
|
109
|
+
* hard-coded in the parser, with no option to turn it off.
|
|
110
|
+
*
|
|
111
|
+
* This renderer registers **no transitions**, so every such directive is dead
|
|
112
|
+
* code here - which makes warning about all of them precise rather than
|
|
113
|
+
* guesswork. An author who wanted a transition learns this profile has none;
|
|
114
|
+
* an author who wanted an attribute learns why it vanished.
|
|
115
|
+
*/
|
|
116
|
+
const RACTIVE_TRANSITION_ATTRIBUTE =
|
|
117
|
+
/\s([a-zA-Z](?:(?!-in-out)[-a-zA-Z_0-9])*-(?:in|out|in-out))(?=[\s/>=])/g;
|
|
118
|
+
|
|
119
|
+
export const ractiveTransitionAttributes = (template: string): string[] => {
|
|
120
|
+
const found = new Set<string>();
|
|
121
|
+
let match: RegExpExecArray | null;
|
|
122
|
+
RACTIVE_TRANSITION_ATTRIBUTE.lastIndex = 0;
|
|
123
|
+
while ((match = RACTIVE_TRANSITION_ATTRIBUTE.exec(template)) !== null) {
|
|
124
|
+
found.add(match[1]);
|
|
125
|
+
}
|
|
126
|
+
return [...found];
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
export function useNamedChildren(
|
|
130
|
+
elements: ExtendedUISchemaElement[] | undefined,
|
|
131
|
+
) {
|
|
132
|
+
const children = useMemo(() => elements ?? [], [elements]);
|
|
133
|
+
const resolved = useMemo(() => resolveChildNames(children), [children]);
|
|
134
|
+
return { children, ...resolved };
|
|
135
|
+
}
|