@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,114 @@
|
|
|
1
|
+
import React, { Suspense, lazy } from 'react';
|
|
2
|
+
import { LoadBoundary } from '../util/lazyControl';
|
|
3
|
+
import { TemplateSegment, buildTextScope } from '../util/interpolate';
|
|
4
|
+
import type { TemplateResult } from '../util/celTemplate';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Resolves a split template, loading the evaluator only if there is one to
|
|
8
|
+
* resolve.
|
|
9
|
+
*
|
|
10
|
+
* The chunk boundary is the point of this component. A label that asks for
|
|
11
|
+
* interpolation but whose text contains no placeholder never requests the
|
|
12
|
+
* evaluator, because the split has already happened - see `interpolate.ts`,
|
|
13
|
+
* which is deliberately free of any CEL import so that the decision can be
|
|
14
|
+
* made before the chunk is.
|
|
15
|
+
*
|
|
16
|
+
* Children are a render prop rather than an element, so the caller decides
|
|
17
|
+
* what to do with the resolved string. That is what keeps the Markdown chunk
|
|
18
|
+
* independent of this one: a label that interpolates without Markdown never
|
|
19
|
+
* touches the parser, and one that does both loads two chunks rather than a
|
|
20
|
+
* combined one.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
export interface InterpolatedTextProps {
|
|
24
|
+
segments: TemplateSegment[];
|
|
25
|
+
/** Declared parameters, unresolved: their values may be expressions. */
|
|
26
|
+
textParams?: Record<string, unknown>;
|
|
27
|
+
/** What a parameter's value may see - the namespaces, subject to the gate. */
|
|
28
|
+
namespaceScope: Record<string, unknown>;
|
|
29
|
+
locale?: string;
|
|
30
|
+
/** The form's translator, so an expression can look a key up (§9). */
|
|
31
|
+
translate?: (key: string, defaultMessage?: string) => string | undefined;
|
|
32
|
+
escape?: (text: string) => string;
|
|
33
|
+
children: (result: TemplateResult) => React.ReactNode;
|
|
34
|
+
/** The unresolved text, shown while the chunk is in flight or if it fails. */
|
|
35
|
+
fallback: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const LazyEvaluate = lazy(async () => {
|
|
39
|
+
const { renderTemplate, resolveTextParams } =
|
|
40
|
+
await import('../util/celTemplate');
|
|
41
|
+
const Evaluated = ({
|
|
42
|
+
segments,
|
|
43
|
+
textParams,
|
|
44
|
+
namespaceScope,
|
|
45
|
+
locale,
|
|
46
|
+
translate,
|
|
47
|
+
escape,
|
|
48
|
+
children,
|
|
49
|
+
}: Omit<InterpolatedTextProps, 'fallback'>) => {
|
|
50
|
+
/*
|
|
51
|
+
Two levels, in order. The parameters are resolved against the
|
|
52
|
+
namespaces, then the text is resolved against the parameters - which is
|
|
53
|
+
the whole point of the split: the text never sees `data`.
|
|
54
|
+
*/
|
|
55
|
+
const resolved = resolveTextParams(
|
|
56
|
+
textParams,
|
|
57
|
+
namespaceScope,
|
|
58
|
+
locale,
|
|
59
|
+
translate,
|
|
60
|
+
);
|
|
61
|
+
const rendered = renderTemplate(
|
|
62
|
+
segments,
|
|
63
|
+
buildTextScope(resolved.params, locale),
|
|
64
|
+
escape,
|
|
65
|
+
locale,
|
|
66
|
+
translate,
|
|
67
|
+
);
|
|
68
|
+
return (
|
|
69
|
+
<>
|
|
70
|
+
{children({
|
|
71
|
+
text: rendered.text,
|
|
72
|
+
failures: [...resolved.failures, ...rendered.failures],
|
|
73
|
+
})}
|
|
74
|
+
</>
|
|
75
|
+
);
|
|
76
|
+
};
|
|
77
|
+
return { default: Evaluated };
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
export const InterpolatedText = ({
|
|
81
|
+
segments,
|
|
82
|
+
textParams,
|
|
83
|
+
namespaceScope,
|
|
84
|
+
locale,
|
|
85
|
+
translate,
|
|
86
|
+
escape,
|
|
87
|
+
children,
|
|
88
|
+
fallback,
|
|
89
|
+
}: InterpolatedTextProps) => (
|
|
90
|
+
/*
|
|
91
|
+
A pending evaluator shows a compact status indicator, never the authored
|
|
92
|
+
template. The caller supplies safe literal content for a failed load.
|
|
93
|
+
*/
|
|
94
|
+
<LoadBoundary fallback={fallback}>
|
|
95
|
+
<Suspense
|
|
96
|
+
fallback={
|
|
97
|
+
<span role='status' aria-busy='true'>
|
|
98
|
+
…
|
|
99
|
+
</span>
|
|
100
|
+
}
|
|
101
|
+
>
|
|
102
|
+
<LazyEvaluate
|
|
103
|
+
segments={segments}
|
|
104
|
+
textParams={textParams}
|
|
105
|
+
namespaceScope={namespaceScope}
|
|
106
|
+
locale={locale}
|
|
107
|
+
translate={translate}
|
|
108
|
+
escape={escape}
|
|
109
|
+
>
|
|
110
|
+
{children}
|
|
111
|
+
</LazyEvaluate>
|
|
112
|
+
</Suspense>
|
|
113
|
+
</LoadBoundary>
|
|
114
|
+
);
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import React, { useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { createPortal } from 'react-dom';
|
|
3
|
+
import Ractive from 'ractive';
|
|
4
|
+
import { sanitizeUrlAttributes } from '../util/templateUrls';
|
|
5
|
+
import { UrlPolicy, defaultUrlPolicy } from '../util/urlPolicy';
|
|
6
|
+
|
|
7
|
+
Ractive.DEBUG = false;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The Ractive template profile, rendered inside React.
|
|
11
|
+
*
|
|
12
|
+
* Ractive owns a DOM subtree and patches it surgically: `ractive.set('data.x',
|
|
13
|
+
* v)` updates the bound text nodes and nothing else. React cannot render into
|
|
14
|
+
* that subtree directly, so each named child is a **placeholder element** in
|
|
15
|
+
* the template, and React portals the delegated renderer into it.
|
|
16
|
+
*
|
|
17
|
+
* That division is what makes this worth having: a data change repaints the
|
|
18
|
+
* bound text without the template function re-running and without the slotted
|
|
19
|
+
* controls remounting, so focus, caret and local state survive.
|
|
20
|
+
*
|
|
21
|
+
* Two things this has to get right, both learned the hard way:
|
|
22
|
+
*
|
|
23
|
+
* - **Placeholders are re-reported after every update.** A placeholder inside
|
|
24
|
+
* `{{#if}}` or `{{#each}}` is a different DOM node after the condition
|
|
25
|
+
* changes, and a portal aimed at the old node renders into a detached
|
|
26
|
+
* element - visibly nothing. The slot list is therefore refreshed on every
|
|
27
|
+
* `set`, not captured once on mount.
|
|
28
|
+
* - **Portals are keyed by slot name**, so a child keeps its React identity
|
|
29
|
+
* across those refreshes and is not remounted.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
export interface RactiveTemplateProps {
|
|
33
|
+
template: string;
|
|
34
|
+
/** Generated placeholder partials, one per named child. */
|
|
35
|
+
partials?: Record<string, string>;
|
|
36
|
+
/** Bindings the specification names: data, errors, context, elements, translate. */
|
|
37
|
+
data: Record<string, unknown>;
|
|
38
|
+
/** Delegated child renderers, by name. */
|
|
39
|
+
slots: Record<string, React.ReactNode>;
|
|
40
|
+
/** Section 12's URL policy, applied to whatever the template rendered. */
|
|
41
|
+
urlPolicy?: UrlPolicy;
|
|
42
|
+
onError?: (message: string) => void;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface Placeholder {
|
|
46
|
+
name: string;
|
|
47
|
+
el: HTMLElement;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Ractive mounts into this attribute; React portals into what it finds. */
|
|
51
|
+
const SLOT_ATTR = 'data-jsonforms-slot';
|
|
52
|
+
|
|
53
|
+
export const RactiveTemplate = ({
|
|
54
|
+
template,
|
|
55
|
+
partials,
|
|
56
|
+
data,
|
|
57
|
+
slots,
|
|
58
|
+
urlPolicy,
|
|
59
|
+
onError,
|
|
60
|
+
}: RactiveTemplateProps) => {
|
|
61
|
+
const host = useRef<HTMLDivElement>(null);
|
|
62
|
+
const instance = useRef<Ractive<Ractive> | null>(null);
|
|
63
|
+
const [placeholders, setPlaceholders] = useState<Placeholder[]>([]);
|
|
64
|
+
|
|
65
|
+
/*
|
|
66
|
+
Reported after mount and after every data change. The identity of the
|
|
67
|
+
array changes each time, but the elements are compared by name below, so a
|
|
68
|
+
stable placeholder does not cause a re-render loop.
|
|
69
|
+
*/
|
|
70
|
+
/*
|
|
71
|
+
Section 12's URL policy, applied to the subtree Ractive just rendered.
|
|
72
|
+
|
|
73
|
+
Ractive owns this DOM and exposes no hook for a bound attribute value, so
|
|
74
|
+
unlike the JSX profile - where the pragma sees the props before the element
|
|
75
|
+
exists - this necessarily runs *after* the attribute has been written. It
|
|
76
|
+
runs in the same synchronous block as the render that wrote it, so nothing
|
|
77
|
+
has painted and no click can have arrived: `javascript:` in an `href`, the
|
|
78
|
+
case that actually executes attacker code, is gone before it is reachable.
|
|
79
|
+
A `src` may have begun loading, which is the honest limit of doing it here.
|
|
80
|
+
*/
|
|
81
|
+
const policyRef = useRef<UrlPolicy>(urlPolicy ?? defaultUrlPolicy);
|
|
82
|
+
const latest = useRef({ data, onError });
|
|
83
|
+
React.useLayoutEffect(() => {
|
|
84
|
+
policyRef.current = urlPolicy ?? defaultUrlPolicy;
|
|
85
|
+
latest.current = { data, onError };
|
|
86
|
+
}, [urlPolicy, data, onError]);
|
|
87
|
+
|
|
88
|
+
const sanitize = () => {
|
|
89
|
+
if (host.current) {
|
|
90
|
+
sanitizeUrlAttributes(host.current, policyRef.current);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const reportPlaceholders = () => {
|
|
95
|
+
const container = host.current;
|
|
96
|
+
if (!container) return;
|
|
97
|
+
sanitize();
|
|
98
|
+
const found = Array.from(
|
|
99
|
+
container.querySelectorAll<HTMLElement>(`[${SLOT_ATTR}]`),
|
|
100
|
+
).map((el) => ({ name: el.getAttribute(SLOT_ATTR) ?? '', el }));
|
|
101
|
+
setPlaceholders((current) =>
|
|
102
|
+
current.length === found.length &&
|
|
103
|
+
current.every((slot, index) => slot.el === found[index].el)
|
|
104
|
+
? current
|
|
105
|
+
: found,
|
|
106
|
+
);
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
// Mount, and rebuild only when the template string itself changes.
|
|
110
|
+
useEffect(() => {
|
|
111
|
+
const container = host.current;
|
|
112
|
+
if (!container) return undefined;
|
|
113
|
+
try {
|
|
114
|
+
instance.current = new Ractive({
|
|
115
|
+
el: container,
|
|
116
|
+
template,
|
|
117
|
+
partials,
|
|
118
|
+
data: { ...latest.current.data },
|
|
119
|
+
});
|
|
120
|
+
reportPlaceholders();
|
|
121
|
+
} catch (error) {
|
|
122
|
+
latest.current.onError?.((error as Error).message);
|
|
123
|
+
}
|
|
124
|
+
return () => {
|
|
125
|
+
instance.current?.teardown();
|
|
126
|
+
instance.current = null;
|
|
127
|
+
setPlaceholders([]);
|
|
128
|
+
};
|
|
129
|
+
// Partials are derived from the child names, so the template string and
|
|
130
|
+
// the partial set change together.
|
|
131
|
+
}, [template, partials]);
|
|
132
|
+
|
|
133
|
+
/*
|
|
134
|
+
The surgical half. Each binding is `set` by keypath, so Ractive patches
|
|
135
|
+
only the nodes that read it - the template is not re-rendered and the
|
|
136
|
+
portalled children are untouched.
|
|
137
|
+
*/
|
|
138
|
+
useEffect(() => {
|
|
139
|
+
const ractive = instance.current;
|
|
140
|
+
if (!ractive) return;
|
|
141
|
+
try {
|
|
142
|
+
for (const [key, value] of Object.entries(data)) {
|
|
143
|
+
ractive.set(key, value);
|
|
144
|
+
}
|
|
145
|
+
reportPlaceholders();
|
|
146
|
+
} catch (error) {
|
|
147
|
+
latest.current.onError?.((error as Error).message);
|
|
148
|
+
}
|
|
149
|
+
}, [data]);
|
|
150
|
+
|
|
151
|
+
return (
|
|
152
|
+
<>
|
|
153
|
+
<div ref={host} data-ractive-template />
|
|
154
|
+
{placeholders.map(({ name, el }) =>
|
|
155
|
+
slots[name] === undefined
|
|
156
|
+
? null
|
|
157
|
+
: createPortal(slots[name], el, `slot-${name}`),
|
|
158
|
+
)}
|
|
159
|
+
</>
|
|
160
|
+
);
|
|
161
|
+
};
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { ControlProps, JsonSchema, UISchemaElement } from '@jsonforms/core';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import type { RowDetailState } from '@chobantonov/jsonforms-react-renderer-common/collections/rowDetail';
|
|
4
|
+
export type EditorControlFrameProps = React.PropsWithChildren<ControlProps>;
|
|
5
|
+
export type EditorActionProps = React.PropsWithChildren<{
|
|
6
|
+
disabled?: boolean;
|
|
7
|
+
onClick?: React.MouseEventHandler<any>;
|
|
8
|
+
title?: string;
|
|
9
|
+
'aria-label'?: string;
|
|
10
|
+
/** Renderer sets map this onto their own button emphasis. */
|
|
11
|
+
variant?: 'default' | 'primary' | 'danger';
|
|
12
|
+
icon?: React.ReactNode;
|
|
13
|
+
}>;
|
|
14
|
+
/**
|
|
15
|
+
* Chrome drawn around the editor. A renderer set supplies one so the control
|
|
16
|
+
* matches its own text-input styling (border, radius, hover/focus/error states).
|
|
17
|
+
*/
|
|
18
|
+
export type EditorSurfaceProps = React.PropsWithChildren<{
|
|
19
|
+
hovered?: boolean;
|
|
20
|
+
focused?: boolean;
|
|
21
|
+
disabled?: boolean;
|
|
22
|
+
invalid?: boolean;
|
|
23
|
+
maximized?: boolean;
|
|
24
|
+
}>;
|
|
25
|
+
/**
|
|
26
|
+
* Palette read from the host design system. Every field is optional: renderers
|
|
27
|
+
* fall back to neutral light-dark() defaults when a value is missing.
|
|
28
|
+
*/
|
|
29
|
+
export type EditorTheme = {
|
|
30
|
+
isDark?: boolean;
|
|
31
|
+
/** Background of the surface, so the editor canvas can match it exactly. */
|
|
32
|
+
background?: string;
|
|
33
|
+
foreground?: string;
|
|
34
|
+
border?: string;
|
|
35
|
+
accent?: string;
|
|
36
|
+
headerBackground?: string;
|
|
37
|
+
headerForeground?: string;
|
|
38
|
+
rowHover?: string;
|
|
39
|
+
selectedRowBackground?: string;
|
|
40
|
+
fontFamily?: string;
|
|
41
|
+
};
|
|
42
|
+
/** One toolbar action, rendered by the renderer set in its own house style. */
|
|
43
|
+
export type EditorArrayAction = {
|
|
44
|
+
key: string;
|
|
45
|
+
/** Tooltip and accessible name. */
|
|
46
|
+
label: string;
|
|
47
|
+
icon?: React.ReactNode;
|
|
48
|
+
danger?: boolean;
|
|
49
|
+
disabled?: boolean;
|
|
50
|
+
onClick: () => void;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Chrome around an array control: title, validation indicator and toolbar
|
|
54
|
+
* actions. Supplying one lets the grid share the header of the renderer set's
|
|
55
|
+
* other array renderers instead of inventing its own.
|
|
56
|
+
*/
|
|
57
|
+
export type EditorArrayFrameProps = React.PropsWithChildren<{
|
|
58
|
+
options?: Record<string, any>;
|
|
59
|
+
config?: any;
|
|
60
|
+
label?: string;
|
|
61
|
+
description?: string;
|
|
62
|
+
errors?: string;
|
|
63
|
+
actions?: EditorArrayAction[];
|
|
64
|
+
}>;
|
|
65
|
+
export type EditorRendererComponents = {
|
|
66
|
+
Frame: React.ComponentType<EditorControlFrameProps>;
|
|
67
|
+
Button: React.ComponentType<EditorActionProps>;
|
|
68
|
+
RowErrorIndicator?: React.ComponentType<{ message: string }>;
|
|
69
|
+
RowDetailButton?: React.ComponentType<EditorActionProps>;
|
|
70
|
+
/**
|
|
71
|
+
* Optional icons for the editor's maximize/restore toggle. When omitted the
|
|
72
|
+
* toggle falls back to a text label, so existing renderer sets keep working.
|
|
73
|
+
*/
|
|
74
|
+
MaximizeIcon?: React.ComponentType;
|
|
75
|
+
MinimizeIcon?: React.ComponentType;
|
|
76
|
+
/** Icons for the array toolbar; omitted icons fall back to text labels. */
|
|
77
|
+
AddIcon?: React.ComponentType;
|
|
78
|
+
RemoveIcon?: React.ComponentType;
|
|
79
|
+
EditIcon?: React.ComponentType;
|
|
80
|
+
ShowDetailsIcon?: React.ComponentType;
|
|
81
|
+
HideDetailsIcon?: React.ComponentType;
|
|
82
|
+
/** Whole-row detail chrome shared with the renderer set's normal table. */
|
|
83
|
+
RowDetailFrame?: React.ComponentType<
|
|
84
|
+
React.PropsWithChildren<{ state: RowDetailState }>
|
|
85
|
+
>;
|
|
86
|
+
/** Optional chrome around the editor; defaults to an unstyled container. */
|
|
87
|
+
Surface?: React.ComponentType<EditorSurfaceProps>;
|
|
88
|
+
/** Optional chrome around array controls (title + validation + actions). */
|
|
89
|
+
ArrayFrame?: React.ComponentType<EditorArrayFrameProps>;
|
|
90
|
+
/**
|
|
91
|
+
* Wraps each grid cell. Renderer sets use it to put their controls into
|
|
92
|
+
* "cell mode" - same renderers, without the label and inline message.
|
|
93
|
+
*/
|
|
94
|
+
CellFrame?: React.ComponentType<
|
|
95
|
+
React.PropsWithChildren<{
|
|
96
|
+
schema?: JsonSchema;
|
|
97
|
+
uischema?: UISchemaElement;
|
|
98
|
+
path: string;
|
|
99
|
+
}>
|
|
100
|
+
>;
|
|
101
|
+
/**
|
|
102
|
+
* Placeholder while the renderer's heavy dependency (Monaco, AG Grid) is
|
|
103
|
+
* still being fetched. Renderer sets supply their own skeleton/spinner.
|
|
104
|
+
*/
|
|
105
|
+
Loading?: React.ComponentType<{ label?: string }>;
|
|
106
|
+
/** Shown when that dependency fails to load. */
|
|
107
|
+
LoadError?: React.ComponentType<{ label?: string }>;
|
|
108
|
+
/**
|
|
109
|
+
* Routes a destructive array action through the host's confirmation policy.
|
|
110
|
+
*
|
|
111
|
+
* Deliberately **policy-free**: this package is framework-agnostic and must
|
|
112
|
+
* not know the catalog ids, the `always`/`never`/`complex` vocabulary or the
|
|
113
|
+
* dialog. It says what would be discarded and what to do if the host agrees;
|
|
114
|
+
* the renderer set that registers this renderer supplies the rest, because
|
|
115
|
+
* that is where the renderer's catalog identity actually lives.
|
|
116
|
+
*
|
|
117
|
+
* Omitted, the action runs immediately - which is what every renderer set
|
|
118
|
+
* did before, so nothing changes for one that has not opted in.
|
|
119
|
+
*/
|
|
120
|
+
useRemoveConfirmation?: () => {
|
|
121
|
+
request: (input: {
|
|
122
|
+
discarded: unknown[];
|
|
123
|
+
options?: Record<string, unknown>;
|
|
124
|
+
config?: unknown;
|
|
125
|
+
perform: () => void;
|
|
126
|
+
}) => void;
|
|
127
|
+
dialog?: React.ReactNode;
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* Optional hook returning the host design system's active theme (e.g. antd's
|
|
131
|
+
* theme algorithm). Supplying it makes the editors follow the design system
|
|
132
|
+
* instead of guessing from DOM background luminance.
|
|
133
|
+
*/
|
|
134
|
+
useEditorTheme?: () => EditorTheme | undefined;
|
|
135
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The marker a template layout puts on a child so the JSX engine's
|
|
3
|
+
* `createElement` knows to render it.
|
|
4
|
+
*
|
|
5
|
+
* It lives in its own module, apart from the engine that reads it, purely so
|
|
6
|
+
* that importing the marker does not import the engine. `DynamicJSXRenderer`
|
|
7
|
+
* pulls in Sucrase at module scope; a renderer that only needed this symbol
|
|
8
|
+
* would otherwise drag the whole compiler into the initial bundle and defeat
|
|
9
|
+
* the code split around it.
|
|
10
|
+
*
|
|
11
|
+
* `Symbol.for` rather than `Symbol()`: the marker has to match across module
|
|
12
|
+
* instances, which a duplicated dependency or a separately bundled chunk can
|
|
13
|
+
* produce.
|
|
14
|
+
*/
|
|
15
|
+
export const ElementRender = Symbol.for('jsonforms.element.render');
|
package/src/core/ajv.ts
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { createAjv as createDefaultAjv } from '@jsonforms/core';
|
|
2
|
+
import type { JsonFormsI18nState } from '@jsonforms/core';
|
|
3
|
+
import ajvErrors from 'ajv-errors';
|
|
4
|
+
import { AjvLocalizers, ajvTranslations } from './ajvI18n';
|
|
5
|
+
import { registerAjvKeywords } from './keywords';
|
|
6
|
+
import { cronProblem } from '../util/cron';
|
|
7
|
+
|
|
8
|
+
/*
|
|
9
|
+
Types taken from core's own factory rather than from `ajv` directly. `ajv` is
|
|
10
|
+
not a dependency here - upstream resolves it by root hoisting - so importing
|
|
11
|
+
it even for types makes the package build depend on that hoisting holding.
|
|
12
|
+
*/
|
|
13
|
+
type Ajv = ReturnType<typeof createDefaultAjv>;
|
|
14
|
+
type Options = NonNullable<Parameters<typeof createDefaultAjv>[0]>;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The validator this renderer set is written against.
|
|
18
|
+
*
|
|
19
|
+
* JSON Forms' own `createAjv()` is deliberately plain, and three of the things
|
|
20
|
+
* it leaves off are things forms here rely on. The Svelte renderer family
|
|
21
|
+
* makes the same three choices, which is a useful check that this is the
|
|
22
|
+
* shape of validator these renderers expect rather than a local preference.
|
|
23
|
+
*
|
|
24
|
+
* | Option | Why |
|
|
25
|
+
* | --- | --- |
|
|
26
|
+
* | `$data: true` | Without it a `$data` bound does not merely go unenforced - the schema **fails to compile**, and the whole form stops. See below. |
|
|
27
|
+
* | `useDefaults: true` | A schema `default` is written into the data, which several renderers assume when they say a value "is" its default. |
|
|
28
|
+
* | `discriminator: true` | Lets a discriminated `oneOf` report the branch's own errors instead of every branch's. |
|
|
29
|
+
*
|
|
30
|
+
* **`$data` is the one that bites.** Ajv rejects `{"formatMinimum": {"$data": "1/from"}}`
|
|
31
|
+
* at compile time with `formatMinimum value must be ["string"]` unless the
|
|
32
|
+
* option is set, so a date range written the obvious way takes the form down
|
|
33
|
+
* rather than degrading. That is why this factory exists at all, and why the
|
|
34
|
+
* web component uses it by default.
|
|
35
|
+
*/
|
|
36
|
+
export interface FormsAjvOptions extends Options {
|
|
37
|
+
/** Extra formats to register, as `{ name: predicate }`. */
|
|
38
|
+
formats?: Record<string, (value: string) => boolean>;
|
|
39
|
+
/**
|
|
40
|
+
* `ajv-keywords`, the extended `transform`, and the extra dynamic defaults.
|
|
41
|
+
*
|
|
42
|
+
* Not spelled `keywords`: Ajv already has an option by that name, which
|
|
43
|
+
* takes a `Vocabulary`. Two different things called the same thing in one
|
|
44
|
+
* options object is how a `true` ends up where a vocabulary was expected.
|
|
45
|
+
*
|
|
46
|
+
* On by default, and the reason is that **turning them off fails silently**.
|
|
47
|
+
* JSON Forms' factory sets `strictSchema: false`, so Ajv ignores a keyword
|
|
48
|
+
* it does not know: a schema carrying `transform` or `allRequired` compiles
|
|
49
|
+
* without complaint, validates less than it says, and leaves the data
|
|
50
|
+
* untransformed. Nothing throws and nothing is logged. These are what the
|
|
51
|
+
* Vue 2 forms in this organisation are already authored against, so the
|
|
52
|
+
* default has to be on.
|
|
53
|
+
*/
|
|
54
|
+
extendedKeywords?: boolean;
|
|
55
|
+
/**
|
|
56
|
+
* `ajv-errors`, so a schema can carry its own `errorMessage`.
|
|
57
|
+
*
|
|
58
|
+
* On by default. Requires `allErrors`, which this factory therefore turns on
|
|
59
|
+
* with it - see below.
|
|
60
|
+
*/
|
|
61
|
+
errorMessages?: boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Permits the `dynamic` dynamic default, which compiles a function from the
|
|
64
|
+
* schema. Off by default; section 14 requires the permission.
|
|
65
|
+
*
|
|
66
|
+
* **A getter is accepted, and is usually what you want.** Everywhere else in
|
|
67
|
+
* this project the permission comes from a *form's*
|
|
68
|
+
* `config.jsonformsExtended.security.allowScriptEvaluation`, but a validator
|
|
69
|
+
* is created once and shared by every form it validates - Ajv caches
|
|
70
|
+
* compiled schemas, so building one per form would recompile on every
|
|
71
|
+
* keystroke. A getter lets the one validator answer per form, the same way
|
|
72
|
+
* `i18n` lets it answer per locale.
|
|
73
|
+
*
|
|
74
|
+
* It is read when a schema **compiles**, not when it validates, because that
|
|
75
|
+
* is when `dynamicDefaults` generators are built.
|
|
76
|
+
*/
|
|
77
|
+
allowScriptEvaluation?: boolean | (() => boolean);
|
|
78
|
+
/**
|
|
79
|
+
* The form's i18n state, read at validation time.
|
|
80
|
+
*
|
|
81
|
+
* Supplying it turns on `errorMessage` translation. Ajv's **own** messages
|
|
82
|
+
* are localized only if `localizers` is supplied as well.
|
|
83
|
+
*/
|
|
84
|
+
i18n?: () => JsonFormsI18nState | undefined;
|
|
85
|
+
/**
|
|
86
|
+
* Ajv's message localizers - `ajvLocalizers` from
|
|
87
|
+
* `core/ajvI18n/localizers`, or a subset.
|
|
88
|
+
*/
|
|
89
|
+
localizers?: AjvLocalizers;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** `color` is not a JSON Schema format and `ajv-formats` does not define one. */
|
|
93
|
+
const COLOR =
|
|
94
|
+
/^(#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})|rgba?\(.*\)|hsla?\(.*\)|hsb\(.*\)|transparent)$/i;
|
|
95
|
+
|
|
96
|
+
export const createFormsAjv = (options: FormsAjvOptions = {}): Ajv => {
|
|
97
|
+
const {
|
|
98
|
+
formats,
|
|
99
|
+
extendedKeywords = true,
|
|
100
|
+
errorMessages = true,
|
|
101
|
+
allowScriptEvaluation = false,
|
|
102
|
+
i18n,
|
|
103
|
+
localizers,
|
|
104
|
+
...ajvOptions
|
|
105
|
+
} = options;
|
|
106
|
+
|
|
107
|
+
const ajv = createDefaultAjv({
|
|
108
|
+
useDefaults: true,
|
|
109
|
+
$data: true,
|
|
110
|
+
discriminator: true,
|
|
111
|
+
/*
|
|
112
|
+
`ajv-errors` requires `allErrors`, and throws at plugin time without it.
|
|
113
|
+
Turning it on here rather than asking every caller to remember is also
|
|
114
|
+
what a form wants: JSON Forms shows every field's error at once, so
|
|
115
|
+
stopping at the first failure would leave most of the form silent.
|
|
116
|
+
|
|
117
|
+
A caller may still override it; `ajv-errors` is then skipped rather than
|
|
118
|
+
allowed to throw.
|
|
119
|
+
*/
|
|
120
|
+
...(errorMessages ? { allErrors: true } : {}),
|
|
121
|
+
...ajvOptions,
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
ajv.addFormat('color', {
|
|
125
|
+
type: 'string',
|
|
126
|
+
validate: (value: string) => COLOR.test(value),
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
/*
|
|
130
|
+
`cron` is not a JSON Schema format keyword, and without this the schema
|
|
131
|
+
route selects the control and validates nothing - `format: "cron"` would be
|
|
132
|
+
ignored with a warning, leaving a five-field expression to be reported by
|
|
133
|
+
the renderer alone. Six fields, seconds first; see `util/cron.ts` for why
|
|
134
|
+
the dialect has to be stated rather than guessed.
|
|
135
|
+
*/
|
|
136
|
+
ajv.addFormat('cron', {
|
|
137
|
+
type: 'string',
|
|
138
|
+
validate: (value: string) => cronProblem(value) === undefined,
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
for (const [name, validate] of Object.entries(formats ?? {})) {
|
|
142
|
+
ajv.addFormat(name, { type: 'string', validate });
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (extendedKeywords) {
|
|
146
|
+
registerAjvKeywords(ajv as never, {
|
|
147
|
+
allowScriptEvaluation:
|
|
148
|
+
typeof allowScriptEvaluation === 'function'
|
|
149
|
+
? allowScriptEvaluation
|
|
150
|
+
: () => allowScriptEvaluation === true,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (errorMessages) {
|
|
155
|
+
if ((ajv as { opts?: { allErrors?: boolean } }).opts?.allErrors) {
|
|
156
|
+
ajvErrors(ajv as never);
|
|
157
|
+
} else {
|
|
158
|
+
// oxlint-disable-next-line no-console
|
|
159
|
+
console.warn(
|
|
160
|
+
'ajv.errorMessagesNeedAllErrors: `errorMessage` support was requested ' +
|
|
161
|
+
'but `allErrors` is off, which ajv-errors requires. Schema-authored ' +
|
|
162
|
+
'messages will not be applied.',
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/*
|
|
168
|
+
Last, because it wraps `ajv.compile`: anything registering keywords after
|
|
169
|
+
this point would still work, but a second wrap would nest the error
|
|
170
|
+
rewriting and unwrap an already-unwrapped list.
|
|
171
|
+
*/
|
|
172
|
+
if (i18n || localizers) {
|
|
173
|
+
ajvTranslations(ajv as never, { i18n, localizers });
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return ajv;
|
|
177
|
+
};
|