@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,272 @@
|
|
|
1
|
+
import { formatDurationComponents } from '@chobantonov/jsonforms-react-renderer-common/formats/duration';
|
|
2
|
+
import { ControlProps } from '@jsonforms/core';
|
|
3
|
+
import { useState } from 'react';
|
|
4
|
+
import { useExtendedTranslator } from './useExtendedTranslator';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The picker's shape, and why it is not one field per component.
|
|
8
|
+
*
|
|
9
|
+
* ISO 8601 gives seven components, and showing all seven means five rows of
|
|
10
|
+
* `0` for a value like "2 days, 3 hours" - the two that carry information are
|
|
11
|
+
* found by scanning past the five that do not. So the picker shows only the
|
|
12
|
+
* units **in play**, with the rest reachable through an add control.
|
|
13
|
+
*
|
|
14
|
+
* Weeks is the other half of it. ISO forbids combining `W` with anything else,
|
|
15
|
+
* which the previous picker enforced by greying the Weeks row whenever another
|
|
16
|
+
* field was non-zero. That is correct and unexplained: a user who wants weeks
|
|
17
|
+
* has to work out that five other fields must be zeroed first. It is a
|
|
18
|
+
* **mode**, not a component, so it is modelled as one.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
export const emptyDurationParts = {
|
|
22
|
+
weeks: 0,
|
|
23
|
+
years: 0,
|
|
24
|
+
months: 0,
|
|
25
|
+
days: 0,
|
|
26
|
+
hours: 0,
|
|
27
|
+
minutes: 0,
|
|
28
|
+
seconds: 0,
|
|
29
|
+
};
|
|
30
|
+
export type ExtendedDurationParts = typeof emptyDurationParts;
|
|
31
|
+
export const durationFields = Object.keys(emptyDurationParts) as Array<
|
|
32
|
+
keyof ExtendedDurationParts
|
|
33
|
+
>;
|
|
34
|
+
|
|
35
|
+
/** The components, in magnitude order. Weeks is a mode and is not among them. */
|
|
36
|
+
export const durationComponentFields = durationFields.filter(
|
|
37
|
+
(field) => field !== 'weeks',
|
|
38
|
+
) as Array<Exclude<keyof ExtendedDurationParts, 'weeks'>>;
|
|
39
|
+
|
|
40
|
+
export type DurationMode = 'weeks' | 'components';
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Whether a value is written in the weeks form.
|
|
44
|
+
*
|
|
45
|
+
* Not `parts.weeks > 0`: `P0W` is a valid duration - RFC 3339's
|
|
46
|
+
* `dur-week = 1*DIGIT "W"` admits `0` - and it parses to a draft of all
|
|
47
|
+
* zeros, which is indistinguishable from `P0D`. The **text** is what says
|
|
48
|
+
* which form was written.
|
|
49
|
+
*/
|
|
50
|
+
export const isWeeksDuration = (value: unknown): boolean =>
|
|
51
|
+
typeof value === 'string' && /^P\d+W$/i.test(value.trim());
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* What an empty picker opens on.
|
|
55
|
+
*
|
|
56
|
+
* Something has to be on screen or the panel is a bare add control. Hours is
|
|
57
|
+
* the unit forms ask for most often, and any other choice is one click away.
|
|
58
|
+
*/
|
|
59
|
+
const defaultComponentField: keyof ExtendedDurationParts = 'hours';
|
|
60
|
+
/**
|
|
61
|
+
* The largest value a component can hold - and it is not 59.
|
|
62
|
+
*
|
|
63
|
+
* A duration's components are **quantities, not clock fields**. ISO 8601
|
|
64
|
+
* bounds none of them: `PT90M`, `PT3600S`, `P18M`, `P400D` and `PT1H591212M`
|
|
65
|
+
* are all valid, and Ajv's `duration` format accepts every one of them. The
|
|
66
|
+
* picker used to cap months at 11 and hours, minutes and seconds at 23 or 59,
|
|
67
|
+
* which is the shape of a *time of day* and has nothing to do with a length
|
|
68
|
+
* of time.
|
|
69
|
+
*
|
|
70
|
+
* The cap was not only wrong, it was **destructive**: `changePart` clamped to
|
|
71
|
+
* it, so typing 90 into Minutes silently became 59. And `PT90M` is not
|
|
72
|
+
* interchangeable with `PT1H30M` as stored data even though the two are the
|
|
73
|
+
* same length - rewriting one into the other is the normalisation §32.1a
|
|
74
|
+
* already forbids.
|
|
75
|
+
*
|
|
76
|
+
* What remains is the only real bound: the range in which an integer is
|
|
77
|
+
* exact. Past it the formatted string would no longer say what was typed.
|
|
78
|
+
*/
|
|
79
|
+
export const durationFieldMax = Number.MAX_SAFE_INTEGER;
|
|
80
|
+
export const parseExtendedDuration = (
|
|
81
|
+
value: unknown,
|
|
82
|
+
): ExtendedDurationParts | null => {
|
|
83
|
+
if (typeof value !== 'string') return null;
|
|
84
|
+
const text = value.trim();
|
|
85
|
+
const weeks = /^P(\d+)W$/i.exec(text);
|
|
86
|
+
if (weeks) return { ...emptyDurationParts, weeks: Number(weeks[1]) };
|
|
87
|
+
const match =
|
|
88
|
+
/^P(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)D)?(?:T(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?)?$/i.exec(
|
|
89
|
+
text,
|
|
90
|
+
);
|
|
91
|
+
if (!match || !match.slice(1).some(Boolean) || /T$/i.test(text)) return null;
|
|
92
|
+
const [, years, months, days, hours, minutes, seconds] = match;
|
|
93
|
+
const parts = {
|
|
94
|
+
weeks: 0,
|
|
95
|
+
years: Number(years || 0),
|
|
96
|
+
months: Number(months || 0),
|
|
97
|
+
days: Number(days || 0),
|
|
98
|
+
hours: Number(hours || 0),
|
|
99
|
+
minutes: Number(minutes || 0),
|
|
100
|
+
seconds: Number(seconds || 0),
|
|
101
|
+
};
|
|
102
|
+
return Object.values(parts).every(Number.isFinite) ? parts : null;
|
|
103
|
+
};
|
|
104
|
+
export const formatExtendedDuration = (parts: ExtendedDurationParts) => {
|
|
105
|
+
const safe = Object.fromEntries(
|
|
106
|
+
Object.entries(parts).map(([key, value]) => [
|
|
107
|
+
key,
|
|
108
|
+
Number.isFinite(value) ? Math.max(0, Math.floor(value)) : 0,
|
|
109
|
+
]),
|
|
110
|
+
) as ExtendedDurationParts;
|
|
111
|
+
if (safe.weeks) return `P${safe.weeks}W`;
|
|
112
|
+
return formatDurationComponents(safe);
|
|
113
|
+
};
|
|
114
|
+
export const useDurationControl = (props: ControlProps) => {
|
|
115
|
+
const t = useExtendedTranslator();
|
|
116
|
+
const [open, setOpen] = useState(false);
|
|
117
|
+
const [draft, setDraft] = useState({ ...emptyDurationParts });
|
|
118
|
+
/**
|
|
119
|
+
* The components on screen.
|
|
120
|
+
*
|
|
121
|
+
* Not derived from "value > 0", because a unit the user has just added is
|
|
122
|
+
* still zero and must stay visible until they type in it or remove it.
|
|
123
|
+
*/
|
|
124
|
+
const [shown, setShown] = useState<Array<keyof ExtendedDurationParts>>([]);
|
|
125
|
+
/**
|
|
126
|
+
* Held, not derived from the draft.
|
|
127
|
+
*
|
|
128
|
+
* It used to be `draft.weeks > 0`, which meant clearing the Weeks box
|
|
129
|
+
* flipped the whole panel into components mode mid-edit: the row the user
|
|
130
|
+
* was typing in vanished and the segmented control jumped. A mode is a
|
|
131
|
+
* thing the user chose; it must not depend on the value they are in the
|
|
132
|
+
* middle of entering.
|
|
133
|
+
*/
|
|
134
|
+
const [mode, setModeState] = useState<DurationMode>('components');
|
|
135
|
+
const options = { ...props.config, ...props.uischema.options };
|
|
136
|
+
const disabled = !props.enabled || Boolean(props.readonly);
|
|
137
|
+
const value = typeof props.data === 'string' ? props.data : '';
|
|
138
|
+
const commit = (next: string | undefined) => {
|
|
139
|
+
if (!disabled) props.handleChange(props.path, next);
|
|
140
|
+
};
|
|
141
|
+
/**
|
|
142
|
+
* Whether these parts are what the current value already says.
|
|
143
|
+
*
|
|
144
|
+
* Several spellings parse to the same duration - `P0W`, `P0D` and `PT0S`
|
|
145
|
+
* are all "nothing", and `P1DT0H` is `P1D`. Writing the canonical form back
|
|
146
|
+
* over an equal value is a rewrite of data the user did not touch: it marks
|
|
147
|
+
* the field dirty on its own, and it silently replaces whatever a server
|
|
148
|
+
* sent.
|
|
149
|
+
*
|
|
150
|
+
* The specification already forbids this in general - "existing data must
|
|
151
|
+
* not be silently normalized solely because the renderer is mounted" - and
|
|
152
|
+
* the zero rule ("serialize zero as `P0D`") says what to write when the
|
|
153
|
+
* user *means* zero, not what to do with an equivalent value that arrived
|
|
154
|
+
* from somewhere else.
|
|
155
|
+
*/
|
|
156
|
+
const unchangedFrom = (next: ExtendedDurationParts) => {
|
|
157
|
+
const current = parseExtendedDuration(value);
|
|
158
|
+
return (
|
|
159
|
+
current !== null &&
|
|
160
|
+
durationFields.every((field) => current[field] === next[field])
|
|
161
|
+
);
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
/** Commits, unless it would only rewrite the value into another spelling. */
|
|
165
|
+
const commitParts = (next: ExtendedDurationParts) => {
|
|
166
|
+
if (unchangedFrom(next)) return;
|
|
167
|
+
commit(formatExtendedDuration(next));
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const commitIfLive = (next: ExtendedDurationParts) => {
|
|
171
|
+
if (options.showActions === false) commitParts(next);
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
/** In magnitude order however they were added. */
|
|
175
|
+
const activeFields =
|
|
176
|
+
mode === 'weeks'
|
|
177
|
+
? (['weeks'] as Array<keyof ExtendedDurationParts>)
|
|
178
|
+
: durationComponentFields.filter((field) => shown.includes(field));
|
|
179
|
+
|
|
180
|
+
const openFields = (parts: ExtendedDurationParts) => {
|
|
181
|
+
const withValues = durationComponentFields.filter(
|
|
182
|
+
(field) => parts[field] > 0,
|
|
183
|
+
);
|
|
184
|
+
return withValues.length ? withValues : [defaultComponentField];
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
return {
|
|
188
|
+
open,
|
|
189
|
+
draft,
|
|
190
|
+
options,
|
|
191
|
+
disabled,
|
|
192
|
+
value,
|
|
193
|
+
mode,
|
|
194
|
+
/** The rows to draw, and the order to draw them in. */
|
|
195
|
+
activeFields,
|
|
196
|
+
/** What the add control may offer. Empty in weeks mode. */
|
|
197
|
+
addableFields:
|
|
198
|
+
mode === 'weeks'
|
|
199
|
+
? []
|
|
200
|
+
: durationComponentFields.filter((field) => !shown.includes(field)),
|
|
201
|
+
error:
|
|
202
|
+
props.errors ||
|
|
203
|
+
(value && !parseExtendedDuration(value) ? t('duration.invalid') : ''),
|
|
204
|
+
showActions: options.showActions !== false,
|
|
205
|
+
openPicker: () => {
|
|
206
|
+
if (!disabled) {
|
|
207
|
+
const parts = parseExtendedDuration(value) ?? {
|
|
208
|
+
...emptyDurationParts,
|
|
209
|
+
};
|
|
210
|
+
setDraft(parts);
|
|
211
|
+
setModeState(isWeeksDuration(value) ? 'weeks' : 'components');
|
|
212
|
+
setShown(openFields(parts));
|
|
213
|
+
setOpen(true);
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
close: () => setOpen(false),
|
|
217
|
+
apply: () => {
|
|
218
|
+
commitParts(draft);
|
|
219
|
+
setOpen(false);
|
|
220
|
+
},
|
|
221
|
+
changeText: (next: string) => commit(next || undefined),
|
|
222
|
+
/**
|
|
223
|
+
* Switches between the two representations.
|
|
224
|
+
*
|
|
225
|
+
* They cannot coexist - `P2W3D` is not a duration - so switching clears
|
|
226
|
+
* the other side rather than pretending to carry it across. The draft is
|
|
227
|
+
* only committed on Apply, so Cancel still restores what was there.
|
|
228
|
+
*/
|
|
229
|
+
setMode: (next: DurationMode) => {
|
|
230
|
+
if (disabled || next === mode) return;
|
|
231
|
+
/*
|
|
232
|
+
Starting at zero rather than inventing a week: switching mode is not
|
|
233
|
+
the user saying how long something is. Zero serializes as `P0D`,
|
|
234
|
+
which section 21 requires regardless of which form was on screen -
|
|
235
|
+
there is no distinct `P0W` to preserve, since `P0W` and `P0D` are the
|
|
236
|
+
same duration.
|
|
237
|
+
*/
|
|
238
|
+
const parts = { ...emptyDurationParts };
|
|
239
|
+
setModeState(next);
|
|
240
|
+
setDraft(parts);
|
|
241
|
+
setShown(next === 'weeks' ? [] : openFields(parts));
|
|
242
|
+
commitIfLive(parts);
|
|
243
|
+
},
|
|
244
|
+
/** Reveals a unit at zero, in magnitude order. */
|
|
245
|
+
addField: (key: keyof ExtendedDurationParts) => {
|
|
246
|
+
if (disabled || key === 'weeks') return;
|
|
247
|
+
setShown((current) =>
|
|
248
|
+
current.includes(key) ? current : [...current, key],
|
|
249
|
+
);
|
|
250
|
+
},
|
|
251
|
+
/** Hides a unit *and clears it*, so the value matches what is on screen. */
|
|
252
|
+
removeField: (key: keyof ExtendedDurationParts) => {
|
|
253
|
+
if (disabled) return;
|
|
254
|
+
setShown((current) => {
|
|
255
|
+
const next = current.filter((field) => field !== key);
|
|
256
|
+
return next.length ? next : [defaultComponentField];
|
|
257
|
+
});
|
|
258
|
+
const next = { ...draft, [key]: 0 };
|
|
259
|
+
setDraft(next);
|
|
260
|
+
commitIfLive(next);
|
|
261
|
+
},
|
|
262
|
+
changePart: (key: keyof ExtendedDurationParts, value: number) => {
|
|
263
|
+
if (disabled) return;
|
|
264
|
+
const next = {
|
|
265
|
+
...draft,
|
|
266
|
+
[key]: Math.max(0, Math.min(durationFieldMax, Math.floor(value || 0))),
|
|
267
|
+
};
|
|
268
|
+
setDraft(next);
|
|
269
|
+
commitIfLive(next);
|
|
270
|
+
},
|
|
271
|
+
};
|
|
272
|
+
};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { RefObject, useEffect, useState } from 'react';
|
|
2
|
+
|
|
3
|
+
export type EditorAppearance = {
|
|
4
|
+
/** Resolved light/dark for the control, whatever the source. */
|
|
5
|
+
isDark: boolean;
|
|
6
|
+
/**
|
|
7
|
+
* A theme name the uischema supplied verbatim (i.e. not light/dark/system).
|
|
8
|
+
* Only meaningful to renderers that understand named themes, such as Monaco.
|
|
9
|
+
*/
|
|
10
|
+
customTheme?: string;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const useEditorAppearance = (
|
|
14
|
+
ref: RefObject<HTMLElement>,
|
|
15
|
+
explicitTheme?: string,
|
|
16
|
+
/**
|
|
17
|
+
* Authoritative light/dark from the host design system (e.g. antd's active
|
|
18
|
+
* theme algorithm). When supplied it wins over the DOM luminance heuristic
|
|
19
|
+
* below, which only guesses and silently falls back to prefers-color-scheme
|
|
20
|
+
* when no ancestor paints an opaque background.
|
|
21
|
+
*/
|
|
22
|
+
isDark?: boolean,
|
|
23
|
+
) => {
|
|
24
|
+
const [dark, setDark] = useState(false);
|
|
25
|
+
useEffect(() => {
|
|
26
|
+
const node = ref.current;
|
|
27
|
+
if (!node) return;
|
|
28
|
+
const ancestors: Element[] = [];
|
|
29
|
+
let current: Element | null = node;
|
|
30
|
+
while (current) {
|
|
31
|
+
ancestors.push(current);
|
|
32
|
+
const root = current.getRootNode();
|
|
33
|
+
current =
|
|
34
|
+
current.parentElement ??
|
|
35
|
+
(root instanceof ShadowRoot ? root.host : null);
|
|
36
|
+
}
|
|
37
|
+
const media = window.matchMedia?.('(prefers-color-scheme: dark)');
|
|
38
|
+
const update = () => {
|
|
39
|
+
for (const element of ancestors) {
|
|
40
|
+
const color = getComputedStyle(element).backgroundColor;
|
|
41
|
+
const values = color.match(/[\d.]+/g)?.map(Number);
|
|
42
|
+
if (
|
|
43
|
+
values &&
|
|
44
|
+
values.length >= 3 &&
|
|
45
|
+
(values.length < 4 || values[3] > 0.5)
|
|
46
|
+
) {
|
|
47
|
+
setDark(
|
|
48
|
+
values[0] * 0.299 + values[1] * 0.587 + values[2] * 0.114 < 128,
|
|
49
|
+
);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
setDark(Boolean(media?.matches));
|
|
54
|
+
};
|
|
55
|
+
const observer = new MutationObserver(update);
|
|
56
|
+
ancestors.forEach((element) =>
|
|
57
|
+
observer.observe(element, {
|
|
58
|
+
attributes: true,
|
|
59
|
+
attributeFilter: ['class', 'style', 'mode', 'dark'],
|
|
60
|
+
}),
|
|
61
|
+
);
|
|
62
|
+
media?.addEventListener('change', update);
|
|
63
|
+
update();
|
|
64
|
+
return () => {
|
|
65
|
+
observer.disconnect();
|
|
66
|
+
media?.removeEventListener('change', update);
|
|
67
|
+
};
|
|
68
|
+
}, [ref]);
|
|
69
|
+
// Precedence: explicit uischema theme > host design system > DOM heuristic.
|
|
70
|
+
// 'system' deliberately skips the design system and follows the environment.
|
|
71
|
+
const named = ['light', 'dark', 'system'];
|
|
72
|
+
return {
|
|
73
|
+
customTheme:
|
|
74
|
+
explicitTheme && !named.includes(explicitTheme)
|
|
75
|
+
? explicitTheme
|
|
76
|
+
: undefined,
|
|
77
|
+
isDark:
|
|
78
|
+
explicitTheme === 'dark'
|
|
79
|
+
? true
|
|
80
|
+
: explicitTheme === 'light'
|
|
81
|
+
? false
|
|
82
|
+
: explicitTheme !== 'system' && isDark !== undefined
|
|
83
|
+
? isDark
|
|
84
|
+
: dark,
|
|
85
|
+
};
|
|
86
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useJsonForms } from '@jsonforms/react';
|
|
2
|
+
import { ExtendedI18nKey } from './i18nDefaults';
|
|
3
|
+
import { extendedDefault } from './extendedLocale';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Translates this package's own strings, and substitutes {name} placeholders.
|
|
7
|
+
*
|
|
8
|
+
* Three sources, in order: the form's own catalog, the locale bundle
|
|
9
|
+
* registered for the current locale, then English. The bundle's string is
|
|
10
|
+
* passed as the translator's *default message*, which is what puts the form's
|
|
11
|
+
* catalog first - see `extendedLocale.ts`.
|
|
12
|
+
*/
|
|
13
|
+
export const useExtendedTranslator = () => {
|
|
14
|
+
const ctx = useJsonForms();
|
|
15
|
+
const translate = ctx.i18n?.translate;
|
|
16
|
+
const locale = ctx.i18n?.locale;
|
|
17
|
+
return (key: ExtendedI18nKey, values?: Record<string, unknown>): string => {
|
|
18
|
+
const fallback = extendedDefault(locale, key);
|
|
19
|
+
const message = translate ? translate(key, fallback, values) : fallback;
|
|
20
|
+
return Object.entries(values ?? {}).reduce(
|
|
21
|
+
(text, [name, value]) => text.replace(`{${name}}`, String(value)),
|
|
22
|
+
message ?? fallback,
|
|
23
|
+
);
|
|
24
|
+
};
|
|
25
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { RefObject, useEffect } from 'react';
|
|
2
|
+
|
|
3
|
+
// Monaco's loader inserts global styles; mirror only Monaco styles for shadow hosts.
|
|
4
|
+
export const useMonacoShadowStyles = (ref: RefObject<HTMLElement>) => {
|
|
5
|
+
useEffect(() => {
|
|
6
|
+
const root = ref.current?.getRootNode();
|
|
7
|
+
if (!(root instanceof ShadowRoot)) return;
|
|
8
|
+
const copies = new Map<Element, Element>();
|
|
9
|
+
const sync = () => {
|
|
10
|
+
for (const source of Array.from(
|
|
11
|
+
document.head.querySelectorAll('style, link[rel="stylesheet"]'),
|
|
12
|
+
)) {
|
|
13
|
+
const content = source.getAttribute('href') ?? source.textContent ?? '';
|
|
14
|
+
if (!/monaco|\/vs\//i.test(content)) continue;
|
|
15
|
+
const previous = copies.get(source);
|
|
16
|
+
if (
|
|
17
|
+
previous &&
|
|
18
|
+
previous.textContent === source.textContent &&
|
|
19
|
+
previous.getAttribute('href') === source.getAttribute('href')
|
|
20
|
+
)
|
|
21
|
+
continue;
|
|
22
|
+
const copy = source.cloneNode(true) as Element;
|
|
23
|
+
if (previous) previous.replaceWith(copy);
|
|
24
|
+
else root.appendChild(copy);
|
|
25
|
+
copies.set(source, copy);
|
|
26
|
+
}
|
|
27
|
+
for (const [source, copy] of copies) {
|
|
28
|
+
if (!source.isConnected) {
|
|
29
|
+
copy.remove();
|
|
30
|
+
copies.delete(source);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
const observer = new MutationObserver(sync);
|
|
35
|
+
observer.observe(document.head, {
|
|
36
|
+
childList: true,
|
|
37
|
+
subtree: true,
|
|
38
|
+
characterData: true,
|
|
39
|
+
attributes: true,
|
|
40
|
+
});
|
|
41
|
+
sync();
|
|
42
|
+
return () => {
|
|
43
|
+
observer.disconnect();
|
|
44
|
+
copies.forEach((copy) => copy.remove());
|
|
45
|
+
};
|
|
46
|
+
}, [ref]);
|
|
47
|
+
};
|