@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
package/src/util/cron.ts
ADDED
|
@@ -0,0 +1,433 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Six-field cron, read and written.
|
|
3
|
+
*
|
|
4
|
+
* **Six fields, not five and not seven**: `second minute hour day-of-month month day-of-week`.
|
|
5
|
+
* This is Spring's dialect — `org.springframework.scheduling.support.CronExpression`, which is
|
|
6
|
+
* also what Quartz-adjacent Java schedulers accept minus the year field. It is deliberately not
|
|
7
|
+
* Unix cron, which has no seconds, and not Quartz, which adds a seventh `year` field and ORs the
|
|
8
|
+
* two day fields. An expression written for one and read as the other schedules the wrong thing
|
|
9
|
+
* while looking correct, so the dialect is stated rather than guessed.
|
|
10
|
+
*
|
|
11
|
+
* Spring **ANDs** every field — `CronExpression.nextOrSame` applies them in sequence, with none of
|
|
12
|
+
* Quartz's OR special case — and `?` is its alias for `*`, allowed only in the two day fields.
|
|
13
|
+
*
|
|
14
|
+
* The model here is **one value set per field**, which is what lets the picker offer a list to
|
|
15
|
+
* choose from rather than a syntax to learn. A field that cannot be stated as a set — `L`, `W`,
|
|
16
|
+
* `#` — is left as the text it is, and the control edits that field as text. Nothing is lost and
|
|
17
|
+
* nothing is rewritten.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** A placeholder for an empty field: every fifteen minutes, on the minute. */
|
|
21
|
+
export const DEFAULT_CRON = '0 0/15 * * * *';
|
|
22
|
+
|
|
23
|
+
/** The six fields, in the order an expression writes them. */
|
|
24
|
+
export const CRON_FIELDS = [
|
|
25
|
+
'second',
|
|
26
|
+
'minute',
|
|
27
|
+
'hour',
|
|
28
|
+
'dayOfMonth',
|
|
29
|
+
'month',
|
|
30
|
+
'dayOfWeek',
|
|
31
|
+
] as const;
|
|
32
|
+
|
|
33
|
+
export type CronField = (typeof CRON_FIELDS)[number];
|
|
34
|
+
|
|
35
|
+
/** Day-of-week names Spring accepts, in the order it numbers them. */
|
|
36
|
+
export const DAY_NAMES = [
|
|
37
|
+
'SUN',
|
|
38
|
+
'MON',
|
|
39
|
+
'TUE',
|
|
40
|
+
'WED',
|
|
41
|
+
'THU',
|
|
42
|
+
'FRI',
|
|
43
|
+
'SAT',
|
|
44
|
+
] as const;
|
|
45
|
+
|
|
46
|
+
/** Month names Spring accepts. `JAN` is 1, so this list is offset by one from its values. */
|
|
47
|
+
export const MONTH_NAMES = [
|
|
48
|
+
'JAN',
|
|
49
|
+
'FEB',
|
|
50
|
+
'MAR',
|
|
51
|
+
'APR',
|
|
52
|
+
'MAY',
|
|
53
|
+
'JUN',
|
|
54
|
+
'JUL',
|
|
55
|
+
'AUG',
|
|
56
|
+
'SEP',
|
|
57
|
+
'OCT',
|
|
58
|
+
'NOV',
|
|
59
|
+
'DEC',
|
|
60
|
+
] as const;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The range a field's **values** live in.
|
|
64
|
+
*
|
|
65
|
+
* Day-of-week stops at 6 although Spring also accepts `7` for Sunday: a set with two Sundays in it
|
|
66
|
+
* would offer the same day twice in the picker. `7` is folded to `0` on the way in and never
|
|
67
|
+
* written back out.
|
|
68
|
+
*/
|
|
69
|
+
export const FIELD_RANGE: Record<CronField, [number, number]> = {
|
|
70
|
+
second: [0, 59],
|
|
71
|
+
minute: [0, 59],
|
|
72
|
+
hour: [0, 23],
|
|
73
|
+
dayOfMonth: [1, 31],
|
|
74
|
+
month: [1, 12],
|
|
75
|
+
dayOfWeek: [0, 6],
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/** The names a field's values may be written with, where it has any. */
|
|
79
|
+
function namesFor(field: CronField): readonly string[] | undefined {
|
|
80
|
+
if (field === 'dayOfWeek') return DAY_NAMES;
|
|
81
|
+
if (field === 'month') return MONTH_NAMES;
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Macros Spring understands in place of all six fields. `@reboot` is not one of them. */
|
|
86
|
+
const MACROS: Record<string, string> = {
|
|
87
|
+
'@yearly': '0 0 0 1 1 *',
|
|
88
|
+
'@annually': '0 0 0 1 1 *',
|
|
89
|
+
'@monthly': '0 0 0 1 * *',
|
|
90
|
+
'@weekly': '0 0 0 * * 0',
|
|
91
|
+
'@daily': '0 0 0 * * *',
|
|
92
|
+
'@midnight': '0 0 0 * * *',
|
|
93
|
+
'@hourly': '0 0 * * * *',
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/** The six fields of an expression, or `undefined` when it does not have six. */
|
|
97
|
+
export function splitCron(expression: string): string[] | undefined {
|
|
98
|
+
const trimmed = expression.trim();
|
|
99
|
+
if (trimmed === '') return undefined;
|
|
100
|
+
const expanded = MACROS[trimmed.toLowerCase()] ?? trimmed;
|
|
101
|
+
const fields = expanded.split(/\s+/);
|
|
102
|
+
return fields.length === 6 ? fields : undefined;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** A field's six parts joined back into an expression. */
|
|
106
|
+
export function joinCron(fields: readonly string[]): string {
|
|
107
|
+
return fields.join(' ');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** A named or numeric value as the number Spring reads it as. */
|
|
111
|
+
function valueOf(text: string, field: CronField): number | undefined {
|
|
112
|
+
const names = namesFor(field);
|
|
113
|
+
if (names) {
|
|
114
|
+
const index = names.indexOf(text.toUpperCase());
|
|
115
|
+
if (index >= 0) return field === 'month' ? index + 1 : index;
|
|
116
|
+
}
|
|
117
|
+
if (!/^\d+$/.test(text)) return undefined;
|
|
118
|
+
const number = Number(text);
|
|
119
|
+
// Both `0` and `7` are Sunday; the set keeps one of them.
|
|
120
|
+
if (field === 'dayOfWeek' && number === 7) return 0;
|
|
121
|
+
const [low, high] = FIELD_RANGE[field];
|
|
122
|
+
return number >= low && number <= high ? number : undefined;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The values one field selects, or `undefined` when it does not select a set.
|
|
127
|
+
*
|
|
128
|
+
* `undefined` is the honest answer for `L`, `W` and `#` — "the last weekday of the month" is not a
|
|
129
|
+
* list of days, and turning it into one would change what the schedule does. It is also the answer
|
|
130
|
+
* for anything malformed, which is what lets the caller tell a field it can offer choices for from
|
|
131
|
+
* one it has to leave as text.
|
|
132
|
+
*/
|
|
133
|
+
export function parseField(
|
|
134
|
+
text: string,
|
|
135
|
+
field: CronField,
|
|
136
|
+
): number[] | undefined {
|
|
137
|
+
const [low, high] = FIELD_RANGE[field];
|
|
138
|
+
const all = (): number[] =>
|
|
139
|
+
Array.from({ length: high - low + 1 }, (_, index) => low + index);
|
|
140
|
+
const value = text.trim();
|
|
141
|
+
if (value === '') return undefined;
|
|
142
|
+
// `?` is Spring's alias for `*`, and only in the two day fields.
|
|
143
|
+
if (value === '*') return all();
|
|
144
|
+
if (value === '?')
|
|
145
|
+
return field === 'dayOfMonth' || field === 'dayOfWeek' ? all() : undefined;
|
|
146
|
+
|
|
147
|
+
const selected = new Set<number>();
|
|
148
|
+
for (const part of value.split(',')) {
|
|
149
|
+
const [range, step, ...rest] = part.split('/');
|
|
150
|
+
if (rest.length > 0) return undefined;
|
|
151
|
+
let by = 1;
|
|
152
|
+
if (step !== undefined) {
|
|
153
|
+
if (!/^\d+$/.test(step) || Number(step) < 1) return undefined;
|
|
154
|
+
by = Number(step);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
let from: number;
|
|
158
|
+
let to: number;
|
|
159
|
+
if (range === '*' || range === '?') {
|
|
160
|
+
[from, to] = [low, high];
|
|
161
|
+
} else {
|
|
162
|
+
const [start, end, ...extra] = range.split('-');
|
|
163
|
+
if (extra.length > 0) return undefined;
|
|
164
|
+
const first = valueOf(start, field);
|
|
165
|
+
if (first === undefined) return undefined;
|
|
166
|
+
if (end === undefined) {
|
|
167
|
+
// `5/10` means "from 5, every 10" — a bare value only when there is no step beside it.
|
|
168
|
+
[from, to] = step === undefined ? [first, first] : [first, high];
|
|
169
|
+
} else {
|
|
170
|
+
const last = valueOf(end, field);
|
|
171
|
+
if (last === undefined) return undefined;
|
|
172
|
+
// A range that wraps the end of the week (`FRI-MON`) is legal and is not a set this can
|
|
173
|
+
// state without inventing an order, so it is left as text.
|
|
174
|
+
if (last < first) return undefined;
|
|
175
|
+
[from, to] = [first, last];
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
for (let each = from; each <= to; each += by) selected.add(each);
|
|
179
|
+
}
|
|
180
|
+
return [...selected].sort((left, right) => left - right);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** One value as the picker shows it and the expression writes it: a name where the field has one. */
|
|
184
|
+
export function labelFor(value: number, field: CronField): string {
|
|
185
|
+
const names = namesFor(field);
|
|
186
|
+
if (!names) return String(value);
|
|
187
|
+
return field === 'month' ? names[value - 1] : names[value];
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* One field written as compactly as it can be said.
|
|
192
|
+
*
|
|
193
|
+
* Runs become ranges and an even spacing becomes a step, because that is how people write these
|
|
194
|
+
* and a list of every value is not readable at a glance. Named fields are written with their names
|
|
195
|
+
* and never with a step: a step on a name is not something every reader will be sure of, and the
|
|
196
|
+
* list it would save is three characters long.
|
|
197
|
+
*/
|
|
198
|
+
export function formatField(
|
|
199
|
+
values: readonly number[],
|
|
200
|
+
field: CronField,
|
|
201
|
+
): string {
|
|
202
|
+
const [low, high] = FIELD_RANGE[field];
|
|
203
|
+
const sorted = [...new Set(values)]
|
|
204
|
+
.filter((value) => value >= low && value <= high)
|
|
205
|
+
.sort((a, b) => a - b);
|
|
206
|
+
// An empty selection is "every", which is what clearing a field means. It is never written as
|
|
207
|
+
// nothing, which would not be an expression.
|
|
208
|
+
if (sorted.length === 0 || sorted.length === high - low + 1) return '*';
|
|
209
|
+
|
|
210
|
+
const named = namesFor(field) !== undefined;
|
|
211
|
+
if (!named && sorted.length > 2) {
|
|
212
|
+
const step = sorted[1] - sorted[0];
|
|
213
|
+
const even = sorted.every(
|
|
214
|
+
(value, index) => index === 0 || value - sorted[index - 1] === step,
|
|
215
|
+
);
|
|
216
|
+
// Only when the run really goes to the end of the field: `0,15,30` is evenly spaced too, and
|
|
217
|
+
// writing it as a step would add 45 to the schedule.
|
|
218
|
+
if (even && step > 1 && sorted[sorted.length - 1] + step > high) {
|
|
219
|
+
return sorted[0] === low ? `*/${step}` : `${sorted[0]}/${step}`;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const parts: string[] = [];
|
|
224
|
+
let index = 0;
|
|
225
|
+
while (index < sorted.length) {
|
|
226
|
+
let end = index;
|
|
227
|
+
while (end + 1 < sorted.length && sorted[end + 1] === sorted[end] + 1)
|
|
228
|
+
end += 1;
|
|
229
|
+
const first = labelFor(sorted[index], field);
|
|
230
|
+
// Two in a row is written as two: `1-2` is no shorter than `1,2` and reads as less.
|
|
231
|
+
if (end - index >= 2)
|
|
232
|
+
parts.push(`${first}-${labelFor(sorted[end], field)}`);
|
|
233
|
+
else
|
|
234
|
+
for (let each = index; each <= end; each += 1)
|
|
235
|
+
parts.push(labelFor(sorted[each], field));
|
|
236
|
+
index = end + 1;
|
|
237
|
+
}
|
|
238
|
+
return parts.join(',');
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** Whether a field is something Spring would accept, set or not. */
|
|
242
|
+
function validField(value: string, field: CronField): boolean {
|
|
243
|
+
if (parseField(value, field) !== undefined) return true;
|
|
244
|
+
if (
|
|
245
|
+
field === 'dayOfMonth' &&
|
|
246
|
+
(value === 'L' ||
|
|
247
|
+
value === 'LW' ||
|
|
248
|
+
/^L-\d+$/.test(value) ||
|
|
249
|
+
/^\d+W$/.test(value))
|
|
250
|
+
) {
|
|
251
|
+
return true;
|
|
252
|
+
}
|
|
253
|
+
// `5#2` is the second Friday and `FRI#2` is the same day written with a name; Spring accepts both
|
|
254
|
+
// spellings, so a validator that only knew the numeric one would call a working schedule broken.
|
|
255
|
+
if (
|
|
256
|
+
field === 'dayOfWeek' &&
|
|
257
|
+
(/^\d?L$/.test(value) ||
|
|
258
|
+
/^[A-Za-z]{3}L$/.test(value) ||
|
|
259
|
+
/^(?:\d|[A-Za-z]{3})#[1-5]$/.test(value))
|
|
260
|
+
) {
|
|
261
|
+
return true;
|
|
262
|
+
}
|
|
263
|
+
// A wrapping range is legal and simply not a set. `FRI-MON`, `NOV-FEB`.
|
|
264
|
+
const [start, end] = value.split('-');
|
|
265
|
+
return (
|
|
266
|
+
end !== undefined &&
|
|
267
|
+
valueOf(start, field) !== undefined &&
|
|
268
|
+
valueOf(end, field) !== undefined
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** Why this expression cannot be scheduled, as a message key, or `undefined` when it can. */
|
|
273
|
+
export function cronProblem(expression: string): string | undefined {
|
|
274
|
+
const fields = splitCron(expression);
|
|
275
|
+
if (!fields) return 'sixFields';
|
|
276
|
+
const bad = CRON_FIELDS.findIndex(
|
|
277
|
+
(field, index) => !validField(fields[index], field),
|
|
278
|
+
);
|
|
279
|
+
return bad < 0 ? undefined : CRON_FIELDS[bad];
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* How often the schedule repeats — the coarsest thing it puts a condition on.
|
|
284
|
+
*
|
|
285
|
+
* This is what the picker asks first, because it decides which of the six fields are worth showing:
|
|
286
|
+
* a schedule that runs every hour has nothing to say about which day it is, and five dropdowns of
|
|
287
|
+
* "every" is a form that hides its own answer. Derived rather than stored, so an expression typed
|
|
288
|
+
* into the field opens on the period it actually describes.
|
|
289
|
+
*/
|
|
290
|
+
export type CronPeriod =
|
|
291
|
+
| 'year'
|
|
292
|
+
| 'month'
|
|
293
|
+
| 'week'
|
|
294
|
+
| 'day'
|
|
295
|
+
| 'hour'
|
|
296
|
+
| 'minute'
|
|
297
|
+
| 'second';
|
|
298
|
+
|
|
299
|
+
/** Coarsest first: the order a period is decided in, and the order the picker lists fields in. */
|
|
300
|
+
const BY_PERIOD: ReadonlyArray<readonly [CronPeriod, CronField]> = [
|
|
301
|
+
['year', 'month'],
|
|
302
|
+
['month', 'dayOfMonth'],
|
|
303
|
+
['week', 'dayOfWeek'],
|
|
304
|
+
['day', 'hour'],
|
|
305
|
+
['hour', 'minute'],
|
|
306
|
+
['minute', 'second'],
|
|
307
|
+
];
|
|
308
|
+
|
|
309
|
+
/** Whether a field says anything at all, rather than standing for every value it could take. */
|
|
310
|
+
export function isEvery(text: string, field: CronField): boolean {
|
|
311
|
+
const values = parseField(text, field);
|
|
312
|
+
const [low, high] = FIELD_RANGE[field];
|
|
313
|
+
return values !== undefined && values.length === high - low + 1;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
export function periodOf(fields: readonly string[]): CronPeriod {
|
|
317
|
+
for (const [period, field] of BY_PERIOD) {
|
|
318
|
+
if (!isEvery(fields[CRON_FIELDS.indexOf(field)], field)) return period;
|
|
319
|
+
}
|
|
320
|
+
return 'second';
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* The fields the picker shows for a period, coarsest first.
|
|
325
|
+
*
|
|
326
|
+
* Everything finer than the period, which is what "repeats every X" leaves to be decided. A period
|
|
327
|
+
* of `week` hides the day of the month, since naming both days would mean "the 5th, and only when
|
|
328
|
+
* it is a Monday" — legal under Spring's AND, and never what picking a weekday meant.
|
|
329
|
+
*/
|
|
330
|
+
export function fieldsForPeriod(period: CronPeriod): CronField[] {
|
|
331
|
+
const from = BY_PERIOD.findIndex(([each]) => each === period);
|
|
332
|
+
if (from < 0) return [];
|
|
333
|
+
return (
|
|
334
|
+
BY_PERIOD.slice(from)
|
|
335
|
+
.map(([, field]) => field)
|
|
336
|
+
// A weekly schedule picks weekdays, not days of the month.
|
|
337
|
+
.filter((field) => !(period === 'week' && field === 'dayOfMonth'))
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* Whether two expressions schedule the same thing.
|
|
343
|
+
*
|
|
344
|
+
* The point is to **not write**. `0 0/15 * * * *` and its asterisk spelling select the same
|
|
345
|
+
* minutes, and `@daily` is `0 0 0 * * *`; opening the picker on one and pressing OK without
|
|
346
|
+
* touching anything must leave the field exactly as it was. Rewriting it would mark the field
|
|
347
|
+
* dirty on its own and quietly replace what a server sent — the same rule the duration control
|
|
348
|
+
* keeps for `P1DT0H` against `P1D`, and the one section 18 states as "existing data must not be
|
|
349
|
+
* silently normalized solely because the renderer is mounted".
|
|
350
|
+
*
|
|
351
|
+
* Compared as **values**, field by field, which is the only comparison that answers this: the two
|
|
352
|
+
* spellings are different strings and the same schedule. A field neither side can state as a set is
|
|
353
|
+
* compared as text, because two pieces of syntax this does not model are only known to agree when
|
|
354
|
+
* they are identical.
|
|
355
|
+
*/
|
|
356
|
+
export function sameExpression(left: string, right: string): boolean {
|
|
357
|
+
if (left.trim() === right.trim()) return true;
|
|
358
|
+
const a = splitCron(left);
|
|
359
|
+
const b = splitCron(right);
|
|
360
|
+
if (!a || !b) return false;
|
|
361
|
+
return CRON_FIELDS.every((field, index) => {
|
|
362
|
+
const one = parseField(a[index], field);
|
|
363
|
+
const other = parseField(b[index], field);
|
|
364
|
+
if (one === undefined || other === undefined) return a[index] === b[index];
|
|
365
|
+
return (
|
|
366
|
+
one.length === other.length &&
|
|
367
|
+
one.every((value, at) => value === other[at])
|
|
368
|
+
);
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/** Every field standing for every value: what a picker opened on nothing has chosen so far. */
|
|
373
|
+
export const NOTHING_CHOSEN: readonly string[] = ['*', '*', '*', '*', '*', '*'];
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* How coarse each field is, for deciding what is "finer than" what.
|
|
377
|
+
*
|
|
378
|
+
* The two day fields share a rank because they answer the same question — which day — by two
|
|
379
|
+
* different routes, and neither is finer than the other.
|
|
380
|
+
*/
|
|
381
|
+
const COARSENESS: Record<CronField, number> = {
|
|
382
|
+
month: 0,
|
|
383
|
+
dayOfMonth: 1,
|
|
384
|
+
dayOfWeek: 1,
|
|
385
|
+
hour: 2,
|
|
386
|
+
minute: 3,
|
|
387
|
+
second: 4,
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* A period's fields with everything coarser than it standing down to every.
|
|
392
|
+
*
|
|
393
|
+
* The period is a **lens**, not data: it decides which rows are worth showing and nothing else, so
|
|
394
|
+
* this seeds nothing and selects nothing. The one thing it must do is drop the conditions the lens
|
|
395
|
+
* is about to hide — a monthly schedule switched to daily that kept its day of the month would
|
|
396
|
+
* still run once a month, with nothing on screen saying so, because the row that said it is gone.
|
|
397
|
+
*/
|
|
398
|
+
export function standDown(
|
|
399
|
+
fields: readonly string[],
|
|
400
|
+
period: CronPeriod,
|
|
401
|
+
): string[] {
|
|
402
|
+
const shown = new Set(fieldsForPeriod(period));
|
|
403
|
+
return CRON_FIELDS.map((field, index) =>
|
|
404
|
+
shown.has(field) ? (fields[index] ?? '*') : '*',
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* The time of day a choice implies, where it has not been said.
|
|
410
|
+
*
|
|
411
|
+
* Picking nine o'clock means **nine o'clock**, not every second of the hour after it — but in cron
|
|
412
|
+
* a field left alone means every value, so `* * 9 * * *` is 3600 executions and not one. So
|
|
413
|
+
* choosing a value pins the time fields below it that are still saying "every".
|
|
414
|
+
*
|
|
415
|
+
* Only downwards, only the time fields, and only when something was actually chosen. Clearing a
|
|
416
|
+
* field pins nothing, and the day and month fields are never filled in on someone's behalf: that
|
|
417
|
+
* would be the picker deciding which day the schedule runs, which is the question being asked.
|
|
418
|
+
* Every pin it does make is one selection away from being undone, so "every minute during the 9am
|
|
419
|
+
* hour" is still reachable — it is just no longer what picking an hour silently means.
|
|
420
|
+
*/
|
|
421
|
+
export function withTimeDefaults(
|
|
422
|
+
fields: readonly string[],
|
|
423
|
+
changed: CronField,
|
|
424
|
+
): string[] {
|
|
425
|
+
const from = COARSENESS[changed];
|
|
426
|
+
return CRON_FIELDS.map((field, index) => {
|
|
427
|
+
const text = fields[index] ?? '*';
|
|
428
|
+
const isTime = field === 'hour' || field === 'minute' || field === 'second';
|
|
429
|
+
if (!isTime || COARSENESS[field] <= from || !isEvery(text, field))
|
|
430
|
+
return text;
|
|
431
|
+
return formatField([FIELD_RANGE[field][0]], field);
|
|
432
|
+
});
|
|
433
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {
|
|
2
|
+
and,
|
|
3
|
+
isStringControl,
|
|
4
|
+
optionIs,
|
|
5
|
+
or,
|
|
6
|
+
rankWith,
|
|
7
|
+
schemaTypeIs,
|
|
8
|
+
uiTypeIs,
|
|
9
|
+
Tester,
|
|
10
|
+
Resolve,
|
|
11
|
+
} from '@jsonforms/core';
|
|
12
|
+
const hasLanguage: Tester = (ui) =>
|
|
13
|
+
typeof ui.options?.language === 'string' ||
|
|
14
|
+
typeof ui.options?.[':language'] === 'string';
|
|
15
|
+
export const monacoControlTester = rankWith(
|
|
16
|
+
25,
|
|
17
|
+
and(
|
|
18
|
+
uiTypeIs('Control'),
|
|
19
|
+
optionIs('format', 'code'),
|
|
20
|
+
or(
|
|
21
|
+
and(isStringControl, hasLanguage),
|
|
22
|
+
and(optionIs('language', 'json'), optionIs('convertJson', true)),
|
|
23
|
+
),
|
|
24
|
+
),
|
|
25
|
+
);
|
|
26
|
+
export const extendedAgGridTester = rankWith(
|
|
27
|
+
25,
|
|
28
|
+
and(
|
|
29
|
+
uiTypeIs('Control'),
|
|
30
|
+
schemaTypeIs('array'),
|
|
31
|
+
optionIs('variant', 'ag-grid'),
|
|
32
|
+
),
|
|
33
|
+
);
|
|
34
|
+
export const resolveEditorLanguage = (
|
|
35
|
+
options: Record<string, any>,
|
|
36
|
+
data: unknown,
|
|
37
|
+
): string => {
|
|
38
|
+
const language =
|
|
39
|
+
typeof options[':language'] === 'string'
|
|
40
|
+
? Resolve.data(data, options[':language'])
|
|
41
|
+
: undefined;
|
|
42
|
+
return typeof language === 'string' && language
|
|
43
|
+
? language
|
|
44
|
+
: options.language || 'plaintext';
|
|
45
|
+
};
|
|
46
|
+
export const encodeEditorValue = (data: unknown, convert: boolean) =>
|
|
47
|
+
data == null ? '' : convert ? JSON.stringify(data, null, 2) : String(data);
|
|
48
|
+
export const decodeEditorValue = (
|
|
49
|
+
value: string,
|
|
50
|
+
convert: boolean,
|
|
51
|
+
): { valid: boolean; value: unknown } => {
|
|
52
|
+
if (!convert) return { valid: true, value: value || undefined };
|
|
53
|
+
try {
|
|
54
|
+
return { valid: true, value: JSON.parse(value) };
|
|
55
|
+
} catch {
|
|
56
|
+
return { valid: false, value };
|
|
57
|
+
}
|
|
58
|
+
};
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { ErrorObject } from 'ajv';
|
|
2
|
+
import { extendedConfig } from './configNamespaces';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The one summary error an editor publishes, and when it publishes it.
|
|
6
|
+
*
|
|
7
|
+
* Section 21: "publish **at most one summary additionalError per editor
|
|
8
|
+
* instance**, regardless of the number of blocking diagnostics. Publish none
|
|
9
|
+
* when that editor has no error-level diagnostics."
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** The keyword the specification names for this error. */
|
|
13
|
+
export const EDITOR_DIAGNOSTIC_KEYWORD = 'editor.language';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Monaco's `MarkerSeverity.Error`.
|
|
17
|
+
*
|
|
18
|
+
* A plain number rather than an import: `monaco-editor` is loaded as a chunk
|
|
19
|
+
* by `@monaco-editor/react`, and importing the enum for one constant would
|
|
20
|
+
* pull the whole editor into any bundle that touches this module. The values
|
|
21
|
+
* are part of Monaco's public API - Hint 1, Info 2, Warning 4, Error 8.
|
|
22
|
+
*/
|
|
23
|
+
export const MONACO_SEVERITY_ERROR = 8;
|
|
24
|
+
|
|
25
|
+
export interface EditorMarker {
|
|
26
|
+
severity?: number;
|
|
27
|
+
message?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Only error-level markers count. "Warnings … stay inside Monaco." */
|
|
31
|
+
export const countBlockingMarkers = (markers: EditorMarker[] | undefined) =>
|
|
32
|
+
(markers ?? []).filter(
|
|
33
|
+
(marker) => (marker?.severity ?? 0) >= MONACO_SEVERITY_ERROR,
|
|
34
|
+
).length;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Whether this editor publishes into the form's additional errors.
|
|
38
|
+
*
|
|
39
|
+
* Resolution is element option, then namespaced form config.
|
|
40
|
+
*
|
|
41
|
+
* **The default is `false`, and the portable contract says `true`.** That is a
|
|
42
|
+
* deliberate divergence, recorded in adjustment 31: publishing turns a
|
|
43
|
+
* language-service diagnostic into something that can block a form, and this
|
|
44
|
+
* project's position is that a host opts into that rather than inheriting it.
|
|
45
|
+
* A form that wants the portable default sets the option once, form-wide.
|
|
46
|
+
*/
|
|
47
|
+
export const resolvePropagateErrors = (
|
|
48
|
+
elementOptions: Record<string, unknown> | undefined,
|
|
49
|
+
config: unknown,
|
|
50
|
+
): boolean => {
|
|
51
|
+
const own = elementOptions?.['propagateErrors'];
|
|
52
|
+
if (own !== undefined) {
|
|
53
|
+
return own === true;
|
|
54
|
+
}
|
|
55
|
+
const namespaced = extendedConfig(config) as
|
|
56
|
+
| { propagateErrors?: unknown }
|
|
57
|
+
| undefined;
|
|
58
|
+
if (namespaced?.propagateErrors !== undefined) {
|
|
59
|
+
return namespaced.propagateErrors === true;
|
|
60
|
+
}
|
|
61
|
+
return false;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/** `a.b.0.c` - JSON Forms' path spelling - as the JSON Pointer `/a/b/0/c`. */
|
|
65
|
+
export const pointerFor = (path: string): string =>
|
|
66
|
+
path ? `/${path.split('.').join('/')}` : '';
|
|
67
|
+
|
|
68
|
+
export interface EditorSummaryInput {
|
|
69
|
+
/** Unique per editor instance, so two editors never overwrite each other. */
|
|
70
|
+
owner: string;
|
|
71
|
+
path: string;
|
|
72
|
+
language: string | undefined;
|
|
73
|
+
errorCount: number;
|
|
74
|
+
message: string;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The summary, in the AJV-compatible shape the specification gives.
|
|
79
|
+
*
|
|
80
|
+
* `params.owner` is the addition: several renderers may own errors at one
|
|
81
|
+
* path, so the host's registry needs to know whose is whose. Section 18 wants
|
|
82
|
+
* that metadata "in runtime integration state, outside business data and the
|
|
83
|
+
* authored UI schema", which an error's `params` is.
|
|
84
|
+
*/
|
|
85
|
+
export const editorSummaryError = ({
|
|
86
|
+
owner,
|
|
87
|
+
path,
|
|
88
|
+
language,
|
|
89
|
+
errorCount,
|
|
90
|
+
message,
|
|
91
|
+
}: EditorSummaryInput): ErrorObject =>
|
|
92
|
+
({
|
|
93
|
+
instancePath: pointerFor(path),
|
|
94
|
+
schemaPath: '',
|
|
95
|
+
keyword: EDITOR_DIAGNOSTIC_KEYWORD,
|
|
96
|
+
message,
|
|
97
|
+
params: {
|
|
98
|
+
source: 'monaco',
|
|
99
|
+
language,
|
|
100
|
+
errorCount,
|
|
101
|
+
owner,
|
|
102
|
+
},
|
|
103
|
+
}) as ErrorObject;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {
|
|
2
|
+
and,
|
|
3
|
+
formatIs,
|
|
4
|
+
isStringControl,
|
|
5
|
+
optionIs,
|
|
6
|
+
or,
|
|
7
|
+
rankWith,
|
|
8
|
+
schemaTypeIs,
|
|
9
|
+
uiTypeIs,
|
|
10
|
+
} from '@jsonforms/core';
|
|
11
|
+
|
|
12
|
+
export const extendedColorTester = rankWith(
|
|
13
|
+
3,
|
|
14
|
+
and(isStringControl, or(formatIs('color'), optionIs('format', 'color'))),
|
|
15
|
+
);
|
|
16
|
+
export const extendedDurationTester = rankWith(
|
|
17
|
+
3,
|
|
18
|
+
and(
|
|
19
|
+
isStringControl,
|
|
20
|
+
or(formatIs('duration'), optionIs('format', 'duration')),
|
|
21
|
+
),
|
|
22
|
+
);
|
|
23
|
+
export const extendedNullTester = rankWith(
|
|
24
|
+
3,
|
|
25
|
+
and(uiTypeIs('Control'), schemaTypeIs('null')),
|
|
26
|
+
);
|
|
27
|
+
export const colorPickerValue = (value: unknown) => {
|
|
28
|
+
if (typeof value !== 'string') return '#000000';
|
|
29
|
+
if (/^#[a-f\d]{6}$/i.test(value)) return value;
|
|
30
|
+
if (/^#[a-f\d]{3}$/i.test(value))
|
|
31
|
+
return '#' + Array.from(value.slice(1), (c) => c + c).join('');
|
|
32
|
+
if (/^#[a-f\d]{8}$/i.test(value)) return value.slice(0, 7);
|
|
33
|
+
return '#000000';
|
|
34
|
+
};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { ExtendedI18nKey, extendedI18nDefaults } from './i18nDefaults';
|
|
2
|
+
import { bgExtendedLocale } from '../locale/bg';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Locale bundles for this package's **own** strings - the grid's "Add row",
|
|
6
|
+
* the editor's loading and maximize labels, the colour and duration prompts.
|
|
7
|
+
*
|
|
8
|
+
* `extendedI18nDefaults` is one table of English, so those stayed English
|
|
9
|
+
* however `i18n.locale` moved unless the form's catalog carried every key.
|
|
10
|
+
* The antd package has the same mechanism (`rendererLocale.ts`) over its own
|
|
11
|
+
* key space; the two are separate because neither package depends on the
|
|
12
|
+
* other, and each owns the keys it draws.
|
|
13
|
+
*
|
|
14
|
+
* The lookup is synchronous rather than a set of `import()` loaders: these
|
|
15
|
+
* strings are on screen as soon as a renderer mounts, so loading them
|
|
16
|
+
* asynchronously would show English and then swap it out.
|
|
17
|
+
*/
|
|
18
|
+
export type ExtendedLocaleCatalog = Partial<Record<ExtendedI18nKey, string>>;
|
|
19
|
+
|
|
20
|
+
/** `en` is present and empty on purpose: English *is* the defaults table. */
|
|
21
|
+
export const defaultExtendedLocales: Record<string, ExtendedLocaleCatalog> = {
|
|
22
|
+
en: {},
|
|
23
|
+
bg: bgExtendedLocale,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
let catalogs: Record<string, ExtendedLocaleCatalog> = defaultExtendedLocales;
|
|
27
|
+
|
|
28
|
+
/** Replaces the supported set wholesale. */
|
|
29
|
+
export const setExtendedLocales = (
|
|
30
|
+
next: Record<string, ExtendedLocaleCatalog>,
|
|
31
|
+
): void => {
|
|
32
|
+
catalogs = next;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export const getExtendedLocales = (): Record<string, ExtendedLocaleCatalog> =>
|
|
36
|
+
catalogs;
|
|
37
|
+
|
|
38
|
+
/** `bg-BG`, `bg_BG` and `bg` are one locale. */
|
|
39
|
+
const normalize = (locale: string) => locale.toLowerCase().replace(/_/g, '-');
|
|
40
|
+
|
|
41
|
+
/** Adds or replaces one language, leaving the others alone. */
|
|
42
|
+
export const registerExtendedLocale = (
|
|
43
|
+
locale: string,
|
|
44
|
+
catalog: ExtendedLocaleCatalog,
|
|
45
|
+
): void => {
|
|
46
|
+
catalogs = { ...catalogs, [normalize(locale)]: catalog };
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/** The catalog for a locale, falling back to its language (`de-AT` -> `de`). */
|
|
50
|
+
export const resolveExtendedLocale = (
|
|
51
|
+
locale: string | undefined,
|
|
52
|
+
): ExtendedLocaleCatalog | undefined => {
|
|
53
|
+
if (!locale) return undefined;
|
|
54
|
+
const code = normalize(locale);
|
|
55
|
+
return catalogs[code] ?? catalogs[code.split('-')[0]];
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* What a key reads as before the form's own catalog is consulted.
|
|
60
|
+
*
|
|
61
|
+
* Handed to the translator as its *default message*, so the form's catalog
|
|
62
|
+
* still wins where it has the key.
|
|
63
|
+
*/
|
|
64
|
+
export const extendedDefault = (
|
|
65
|
+
locale: string | undefined,
|
|
66
|
+
key: ExtendedI18nKey,
|
|
67
|
+
): string => resolveExtendedLocale(locale)?.[key] ?? extendedI18nDefaults[key];
|