@atscript/ui 0.1.163 → 0.1.165
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client-factory.cjs +27 -0
- package/dist/client-factory.d.cts +58 -0
- package/dist/client-factory.d.mts +58 -0
- package/dist/client-factory.mjs +25 -0
- package/dist/form/apply.cjs +38 -0
- package/dist/form/apply.d.cts +36 -0
- package/dist/form/apply.d.mts +36 -0
- package/dist/form/apply.mjs +38 -0
- package/dist/form/clone.cjs +30 -0
- package/dist/form/clone.d.cts +23 -0
- package/dist/form/clone.d.mts +23 -0
- package/dist/form/clone.mjs +30 -0
- package/dist/form/create-form-def.cjs +462 -0
- package/dist/form/create-form-def.d.cts +67 -0
- package/dist/form/create-form-def.d.mts +67 -0
- package/dist/form/create-form-def.mjs +460 -0
- package/dist/form/decimal-format.cjs +324 -0
- package/dist/form/decimal-format.d.cts +86 -0
- package/dist/form/decimal-format.d.mts +86 -0
- package/dist/form/decimal-format.mjs +315 -0
- package/dist/form/diff.cjs +458 -0
- package/dist/form/diff.d.cts +80 -0
- package/dist/form/diff.d.mts +80 -0
- package/dist/form/diff.mjs +457 -0
- package/dist/form/dirty.cjs +65 -0
- package/dist/form/dirty.d.cts +47 -0
- package/dist/form/dirty.d.mts +47 -0
- package/dist/form/dirty.mjs +64 -0
- package/dist/form/error-utils.cjs +97 -0
- package/dist/form/error-utils.d.cts +58 -0
- package/dist/form/error-utils.d.mts +58 -0
- package/dist/form/error-utils.mjs +92 -0
- package/dist/form/form-actions.cjs +30 -0
- package/dist/form/form-actions.d.cts +17 -0
- package/dist/form/form-actions.d.mts +17 -0
- package/dist/form/form-actions.mjs +30 -0
- package/dist/form/grid.cjs +69 -0
- package/dist/form/grid.d.cts +49 -0
- package/dist/form/grid.d.mts +49 -0
- package/dist/form/grid.mjs +64 -0
- package/dist/form/labels.cjs +13 -0
- package/dist/form/labels.d.cts +11 -0
- package/dist/form/labels.d.mts +11 -0
- package/dist/form/labels.mjs +13 -0
- package/dist/form/measurement.cjs +20 -0
- package/dist/form/measurement.d.cts +29 -0
- package/dist/form/measurement.d.mts +29 -0
- package/dist/form/measurement.mjs +20 -0
- package/dist/form/path-utils.cjs +182 -0
- package/dist/form/path-utils.d.cts +47 -0
- package/dist/form/path-utils.d.mts +47 -0
- package/dist/form/path-utils.mjs +175 -0
- package/dist/form/rebase.cjs +89 -0
- package/dist/form/rebase.d.cts +61 -0
- package/dist/form/rebase.d.mts +61 -0
- package/dist/form/rebase.mjs +89 -0
- package/dist/form/skipped-nav.cjs +100 -0
- package/dist/form/skipped-nav.d.cts +13 -0
- package/dist/form/skipped-nav.d.mts +13 -0
- package/dist/form/skipped-nav.mjs +98 -0
- package/dist/form/types.cjs +40 -0
- package/dist/form/types.d.cts +147 -0
- package/dist/form/types.d.mts +147 -0
- package/dist/form/types.mjs +36 -0
- package/dist/form/union-detect.cjs +46 -0
- package/dist/form/union-detect.d.cts +22 -0
- package/dist/form/union-detect.d.mts +22 -0
- package/dist/form/union-detect.mjs +46 -0
- package/dist/form/validate.cjs +186 -0
- package/dist/form/validate.d.cts +81 -0
- package/dist/form/validate.d.mts +81 -0
- package/dist/form/validate.mjs +182 -0
- package/dist/i18n/ui-texts.cjs +135 -0
- package/dist/i18n/ui-texts.d.cts +81 -0
- package/dist/i18n/ui-texts.d.mts +81 -0
- package/dist/i18n/ui-texts.mjs +132 -0
- package/dist/index.cjs +223 -3360
- package/dist/index.d.cts +39 -1693
- package/dist/index.d.mts +39 -1693
- package/dist/index.mjs +36 -3179
- package/dist/messages.cjs +301 -0
- package/dist/messages.d.cts +302 -0
- package/dist/messages.d.mts +302 -0
- package/dist/messages.mjs +300 -0
- package/dist/nav/model-routes.cjs +59 -0
- package/dist/nav/model-routes.d.cts +33 -0
- package/dist/nav/model-routes.d.mts +33 -0
- package/dist/nav/model-routes.mjs +59 -0
- package/dist/plugin/annotations.cjs +592 -0
- package/dist/plugin/annotations.mjs +592 -0
- package/dist/plugin/primitives.cjs +30 -0
- package/dist/plugin/primitives.mjs +30 -0
- package/dist/plugin/value-help-validation.cjs +429 -0
- package/dist/plugin/value-help-validation.mjs +423 -0
- package/dist/plugin.cjs +4 -1001
- package/dist/plugin.mjs +2 -999
- package/dist/shared/annotation-keys.cjs +189 -0
- package/dist/shared/annotation-keys.d.cts +82 -0
- package/dist/shared/annotation-keys.d.mts +82 -0
- package/dist/shared/annotation-keys.mjs +97 -0
- package/dist/shared/field-resolver.cjs +123 -0
- package/dist/shared/field-resolver.d.cts +77 -0
- package/dist/shared/field-resolver.d.mts +77 -0
- package/dist/shared/field-resolver.mjs +110 -0
- package/dist/shared/meta-cache.cjs +282 -0
- package/dist/shared/meta-cache.d.cts +130 -0
- package/dist/shared/meta-cache.d.mts +130 -0
- package/dist/shared/meta-cache.mjs +275 -0
- package/dist/shared/str.cjs +19 -0
- package/dist/shared/str.d.cts +7 -0
- package/dist/shared/str.d.mts +7 -0
- package/dist/shared/str.mjs +17 -0
- package/dist/table/column-resolver.cjs +17 -0
- package/dist/table/column-resolver.d.cts +11 -0
- package/dist/table/column-resolver.d.mts +11 -0
- package/dist/table/column-resolver.mjs +15 -0
- package/dist/table/create-table-def.cjs +242 -0
- package/dist/table/create-table-def.d.cts +19 -0
- package/dist/table/create-table-def.d.mts +19 -0
- package/dist/table/create-table-def.mjs +242 -0
- package/dist/table/navigate-href.cjs +45 -0
- package/dist/table/navigate-href.d.cts +31 -0
- package/dist/table/navigate-href.d.mts +31 -0
- package/dist/table/navigate-href.mjs +45 -0
- package/dist/table/types.d.cts +290 -0
- package/dist/table/types.d.mts +290 -0
- package/dist/table/value-kind.cjs +20 -0
- package/dist/table/value-kind.d.cts +16 -0
- package/dist/table/value-kind.d.mts +16 -0
- package/dist/table/value-kind.mjs +19 -0
- package/dist/value-help/dict-paths.cjs +17 -0
- package/dist/value-help/dict-paths.d.cts +11 -0
- package/dist/value-help/dict-paths.d.mts +11 -0
- package/dist/value-help/dict-paths.mjs +17 -0
- package/dist/value-help/distinct-client.cjs +51 -0
- package/dist/value-help/distinct-client.d.cts +40 -0
- package/dist/value-help/distinct-client.d.mts +40 -0
- package/dist/value-help/distinct-client.mjs +50 -0
- package/dist/value-help/extract-literals.cjs +115 -0
- package/dist/value-help/extract-literals.d.cts +44 -0
- package/dist/value-help/extract-literals.d.mts +44 -0
- package/dist/value-help/extract-literals.mjs +111 -0
- package/dist/value-help/extract-ref.cjs +100 -0
- package/dist/value-help/extract-ref.d.cts +37 -0
- package/dist/value-help/extract-ref.d.mts +37 -0
- package/dist/value-help/extract-ref.mjs +100 -0
- package/dist/value-help/filter-tree.cjs +45 -0
- package/dist/value-help/filter-tree.mjs +45 -0
- package/dist/value-help/index.d.mts +8 -0
- package/dist/value-help/resolve-options.cjs +68 -0
- package/dist/value-help/resolve-options.d.cts +40 -0
- package/dist/value-help/resolve-options.d.mts +40 -0
- package/dist/value-help/resolve-options.mjs +64 -0
- package/dist/value-help/resolve.cjs +66 -0
- package/dist/value-help/resolve.d.cts +36 -0
- package/dist/value-help/resolve.d.mts +36 -0
- package/dist/value-help/resolve.mjs +65 -0
- package/dist/value-help/types.d.cts +41 -0
- package/dist/value-help/types.d.mts +41 -0
- package/dist/value-help/value-help-client.cjs +136 -0
- package/dist/value-help/value-help-client.d.cts +67 -0
- package/dist/value-help/value-help-client.d.mts +67 -0
- package/dist/value-help/value-help-client.mjs +134 -0
- package/package.json +15 -10
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import "../shared/annotation-keys.mjs";
|
|
2
|
+
import { childFields } from "./types.mjs";
|
|
3
|
+
import { getUiTexts } from "../i18n/ui-texts.mjs";
|
|
4
|
+
import { isSkippedNavField } from "./skipped-nav.mjs";
|
|
5
|
+
import { validatorErrorsToMap } from "./error-utils.mjs";
|
|
6
|
+
import { Validator, localizeErrors, normalizeForType } from "@atscript/typescript/utils";
|
|
7
|
+
import { createDbValidatorPlugin } from "@atscript/db/validator";
|
|
8
|
+
//#region src/form/validate.ts
|
|
9
|
+
/**
|
|
10
|
+
* `error`'s message in the language of `t` (looked up by code or text, see
|
|
11
|
+
* {@link TFormValidatorCallOptions.t}); the English message when `t` has none.
|
|
12
|
+
* Its `details` are dropped first: only the message is read.
|
|
13
|
+
*/
|
|
14
|
+
function messageIn(error, t) {
|
|
15
|
+
return localizeErrors([{
|
|
16
|
+
...error,
|
|
17
|
+
details: void 0
|
|
18
|
+
}], (id, params) => {
|
|
19
|
+
const text = t(id, params);
|
|
20
|
+
return text === id ? void 0 : text;
|
|
21
|
+
})[0].message;
|
|
22
|
+
}
|
|
23
|
+
const dbPlugin = createDbValidatorPlugin();
|
|
24
|
+
const INSERT_CONTEXT = { mode: "insert" };
|
|
25
|
+
/**
|
|
26
|
+
* Always-on plugin for fields the server fills in, so a create form is not
|
|
27
|
+
* blocked on them:
|
|
28
|
+
* - `@db.column.derived` — read-only and server-computed: never validated
|
|
29
|
+
* (not even a loaded value, which the user could not fix).
|
|
30
|
+
* - a navigation field the form leaves out (`isSkippedNavField`): loaded
|
|
31
|
+
* related rows the form neither renders nor sends.
|
|
32
|
+
* - an absent `@db.default*` / `@db.column.version` / `@db.onUpdate.inc` value
|
|
33
|
+
* — delegated to `@atscript/db`'s own insert-mode rule, so the key list has
|
|
34
|
+
* one owner.
|
|
35
|
+
* `db.rel.FK` is kept out: a form fills the FK itself (no nested nav
|
|
36
|
+
* object), so an empty required FK must still fail.
|
|
37
|
+
*
|
|
38
|
+
* The db plugin reads its mode from the validation context, which here
|
|
39
|
+
* carries the form's `{ data, context }` — it gets a view of `ctx` with the
|
|
40
|
+
* insert context instead.
|
|
41
|
+
*/
|
|
42
|
+
const serverManagedPlugin = (ctx, def, value) => {
|
|
43
|
+
if (def.metadata.has("db.column.derived") || isSkippedNavField(def)) return true;
|
|
44
|
+
if (value !== void 0 || def.metadata.has("db.rel.FK")) return void 0;
|
|
45
|
+
return dbPlugin(Object.create(ctx, { context: { value: INSERT_CONTEXT } }), def, value);
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* The props of the fields `createFormDef` marked `derived`, at any depth — a
|
|
49
|
+
* computed view column (`metaFields`) or an edit form's update-stamped field
|
|
50
|
+
* (see `CreateFormDefOptions.mode`) carries no annotation the plugin above
|
|
51
|
+
* could read. The validator matches by prop, so a prop that is also used where
|
|
52
|
+
* it is not derived (an interface's stamp below a union of several types, too)
|
|
53
|
+
* stays validated. A recursive type's def graph is cyclic: each field def is
|
|
54
|
+
* visited once.
|
|
55
|
+
*/
|
|
56
|
+
function collectDerivedProps(def) {
|
|
57
|
+
const derived = /* @__PURE__ */ new Set();
|
|
58
|
+
const kept = /* @__PURE__ */ new Set();
|
|
59
|
+
const seen = /* @__PURE__ */ new Set();
|
|
60
|
+
const walk = (fields) => {
|
|
61
|
+
for (const field of fields) {
|
|
62
|
+
if (seen.has(field)) continue;
|
|
63
|
+
seen.add(field);
|
|
64
|
+
if (field.derived) {
|
|
65
|
+
derived.add(field.prop);
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
kept.add(field.prop);
|
|
69
|
+
walk(childFields(field));
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
walk(def.fields);
|
|
73
|
+
for (const prop of kept) derived.delete(prop);
|
|
74
|
+
return derived;
|
|
75
|
+
}
|
|
76
|
+
let defaultValidatorPlugins = [];
|
|
77
|
+
/** Replace the default validator plugins applied to every form/field validator. */
|
|
78
|
+
function setDefaultValidatorPlugins(plugins) {
|
|
79
|
+
defaultValidatorPlugins = plugins;
|
|
80
|
+
}
|
|
81
|
+
/** Get the currently registered default validator plugins. */
|
|
82
|
+
function getDefaultValidatorPlugins() {
|
|
83
|
+
return defaultValidatorPlugins;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Returns a reusable validator for a whole FormDef.
|
|
87
|
+
*
|
|
88
|
+
* Validator is created once and reused on every call.
|
|
89
|
+
* ATScript's @expect.* validation runs automatically; server-managed db
|
|
90
|
+
* fields (every `derived` field, absent `@db.default*` / version) pass.
|
|
91
|
+
* For custom `ui.fn.*` validators, install ui-fns and pass its plugin via `opts.plugins`.
|
|
92
|
+
*
|
|
93
|
+
* It validates the data as given: apply `@normalize.*` first with its
|
|
94
|
+
* `normalize(data)` and validate (and submit) the result, as the server does.
|
|
95
|
+
*
|
|
96
|
+
* Errors are keyed by dotted path, worded in the language of `callOpts.t`.
|
|
97
|
+
* A root-level error (a failing `@ui.form.validate` on the form's interface)
|
|
98
|
+
* is keyed `__form`.
|
|
99
|
+
*/
|
|
100
|
+
function getFormValidator(def, opts) {
|
|
101
|
+
const derived = collectDerivedProps(def);
|
|
102
|
+
const derivedPlugin = (_ctx, prop) => derived.has(prop) ? true : void 0;
|
|
103
|
+
const options = {
|
|
104
|
+
unknownProps: "ignore",
|
|
105
|
+
...opts,
|
|
106
|
+
plugins: [
|
|
107
|
+
...derived.size > 0 ? [derivedPlugin] : [],
|
|
108
|
+
serverManagedPlugin,
|
|
109
|
+
...defaultValidatorPlugins,
|
|
110
|
+
...opts?.plugins ?? []
|
|
111
|
+
]
|
|
112
|
+
};
|
|
113
|
+
const validator = new Validator(def.type, options);
|
|
114
|
+
const validate = (callOpts) => {
|
|
115
|
+
if (validator.validate(callOpts.data, true, {
|
|
116
|
+
data: callOpts.data,
|
|
117
|
+
context: callOpts.context ?? {},
|
|
118
|
+
t: callOpts.t
|
|
119
|
+
})) return {};
|
|
120
|
+
const t = callOpts.t ?? getUiTexts().t;
|
|
121
|
+
return validatorErrorsToMap(validator.errors.map((e) => ({
|
|
122
|
+
path: e.path,
|
|
123
|
+
message: messageIn(e, t)
|
|
124
|
+
})));
|
|
125
|
+
};
|
|
126
|
+
return Object.assign(validate, { normalize: (data) => normalizeForType(def.type, data, options) });
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Root-only plugin: passes every descendant without visiting it. Only
|
|
130
|
+
* `object` / `array` / `tuple` roots use it — their root-path errors (shape,
|
|
131
|
+
* length, a container-level `@ui.form.validate`) are all decided before the
|
|
132
|
+
* walk descends, so skipping the children cannot change the root error. A
|
|
133
|
+
* union or intersection root evaluates its branches AT the root path, where
|
|
134
|
+
* a branch's verdict depends on its children — those keep the full walk.
|
|
135
|
+
*/
|
|
136
|
+
const skipDescendantsPlugin = (ctx) => ctx.path === "" ? void 0 : true;
|
|
137
|
+
/**
|
|
138
|
+
* Whether a type kind is a container that carries its own children:
|
|
139
|
+
* `object`, `array` or `tuple`. Union / intersection kinds are not
|
|
140
|
+
* containers — they evaluate their branches at their own path.
|
|
141
|
+
*/
|
|
142
|
+
function isContainerKind(kind) {
|
|
143
|
+
return kind === "object" || kind === "array" || kind === "tuple";
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Creates a cached validator function for a single ATScript prop.
|
|
147
|
+
*
|
|
148
|
+
* The `Validator` instance is created lazily on first call and reused.
|
|
149
|
+
* Returns `true` when valid, or the first error message string when invalid,
|
|
150
|
+
* worded in the language of `externalCtx.t` (else `opts.t`, else the active
|
|
151
|
+
* UI texts) — looked up by the error's code, see {@link TFormValidatorCallOptions.t}.
|
|
152
|
+
* The value is judged with the prop's `@normalize.*` applied, as a submit
|
|
153
|
+
* judges it (the value itself is not changed).
|
|
154
|
+
* With `rootOnly`, an object / array / tuple prop is validated at its root
|
|
155
|
+
* only (descendants are not walked — their errors would be discarded anyway).
|
|
156
|
+
*/
|
|
157
|
+
function createFieldValidator(prop, opts) {
|
|
158
|
+
let cached;
|
|
159
|
+
let options;
|
|
160
|
+
const rootOfContainer = opts?.rootOnly === true && isContainerKind(prop.type.kind);
|
|
161
|
+
return (value, externalCtx) => {
|
|
162
|
+
if (!cached) {
|
|
163
|
+
options = { plugins: rootOfContainer ? [
|
|
164
|
+
skipDescendantsPlugin,
|
|
165
|
+
serverManagedPlugin,
|
|
166
|
+
...defaultValidatorPlugins
|
|
167
|
+
] : [serverManagedPlugin, ...defaultValidatorPlugins] };
|
|
168
|
+
cached = new Validator(prop, options);
|
|
169
|
+
}
|
|
170
|
+
const judged = rootOfContainer ? value : normalizeForType(prop, value, options);
|
|
171
|
+
if (cached.validate(judged, true, externalCtx)) return true;
|
|
172
|
+
const t = externalCtx?.t ?? opts?.t ?? getUiTexts().t;
|
|
173
|
+
if (opts?.rootOnly) {
|
|
174
|
+
const rootError = cached.errors?.find((e) => e.path === "");
|
|
175
|
+
return rootError ? messageIn(rootError, t) : true;
|
|
176
|
+
}
|
|
177
|
+
const error = cached.errors?.[0];
|
|
178
|
+
return error && messageIn(error, t) || t("UI_INVALID_VALUE");
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
//#endregion
|
|
182
|
+
export { createFieldValidator, getDefaultValidatorPlugins, getFormValidator, isContainerKind, setDefaultValidatorPlugins };
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
const require_messages = require("../messages.cjs");
|
|
2
|
+
//#region src/i18n/ui-texts.ts
|
|
3
|
+
const PLACEHOLDER = /\{(\w+)\}/g;
|
|
4
|
+
/**
|
|
5
|
+
* Fills `{name}` placeholders in one pass (arrays joined with `, `); a
|
|
6
|
+
* placeholder without a param stays, filled-in values are never filled again.
|
|
7
|
+
*/
|
|
8
|
+
function fill(template, params) {
|
|
9
|
+
if (!params || !template.includes("{")) return template;
|
|
10
|
+
return template.replace(PLACEHOLDER, (match, name) => {
|
|
11
|
+
const value = Object.hasOwn(params, name) ? params[name] : void 0;
|
|
12
|
+
if (value === void 0) return match;
|
|
13
|
+
return Array.isArray(value) ? value.map(String).join(", ") : String(value);
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
let failureLogged = false;
|
|
17
|
+
/** The translator's text; `undefined` when it has none or throws (logged once). */
|
|
18
|
+
function callTranslator(translate, id, params, locale) {
|
|
19
|
+
try {
|
|
20
|
+
const out = translate(id, params, locale);
|
|
21
|
+
return typeof out === "string" ? out : void 0;
|
|
22
|
+
} catch (error) {
|
|
23
|
+
if (!failureLogged) {
|
|
24
|
+
failureLogged = true;
|
|
25
|
+
console.error(`[atscript-ui] The UI translator threw for "${id}" (logged once)`, error);
|
|
26
|
+
}
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/** The string entries of a served `$i18n` dict, `undefined` when it has none. */
|
|
31
|
+
function servedEntries(served) {
|
|
32
|
+
if (typeof served !== "object" || served === null || Array.isArray(served)) return void 0;
|
|
33
|
+
let out;
|
|
34
|
+
for (const [id, text] of Object.entries(served)) if (typeof text === "string") (out ??= {})[id] = text;
|
|
35
|
+
return out;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Creates a source of UI texts. `dicts` holds the served texts — pass a Vue
|
|
39
|
+
* `shallowRef({})` for reactive lookups; it is replaced wholesale on merge.
|
|
40
|
+
*
|
|
41
|
+
* @internal Shared with the `@atscript/vue-*` packages.
|
|
42
|
+
*/
|
|
43
|
+
function createUiTexts(opts) {
|
|
44
|
+
const { locale, translate } = opts;
|
|
45
|
+
const dicts = opts.dicts ?? { value: {} };
|
|
46
|
+
const t = (id, params) => {
|
|
47
|
+
const loc = locale();
|
|
48
|
+
const translator = translate?.();
|
|
49
|
+
if (translator) {
|
|
50
|
+
const own = callTranslator(translator, id, params, loc);
|
|
51
|
+
if (own !== void 0 && own !== id) return own;
|
|
52
|
+
}
|
|
53
|
+
const dict = dicts.value[loc ?? ""];
|
|
54
|
+
return fill(dict && Object.hasOwn(dict, id) ? dict[id] : Object.hasOwn(require_messages.AS_UI_MESSAGES, id) ? require_messages.AS_UI_MESSAGES[id] : id, params);
|
|
55
|
+
};
|
|
56
|
+
const merge = (loc, served) => {
|
|
57
|
+
const entries = servedEntries(served);
|
|
58
|
+
if (!entries) return;
|
|
59
|
+
const key = loc ?? "";
|
|
60
|
+
const prev = dicts.value[key];
|
|
61
|
+
if (prev && Object.keys(entries).every((id) => prev[id] === entries[id])) return;
|
|
62
|
+
dicts.value = {
|
|
63
|
+
...dicts.value,
|
|
64
|
+
[key]: Object.freeze({
|
|
65
|
+
...prev,
|
|
66
|
+
...entries
|
|
67
|
+
})
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
return {
|
|
71
|
+
locale,
|
|
72
|
+
t,
|
|
73
|
+
merge
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
let globalLocale;
|
|
77
|
+
let globalTranslate;
|
|
78
|
+
const globalSource = createUiTexts({
|
|
79
|
+
locale: () => globalLocale,
|
|
80
|
+
translate: () => globalTranslate
|
|
81
|
+
});
|
|
82
|
+
const globalTexts = {
|
|
83
|
+
...globalSource,
|
|
84
|
+
merge: (locale, served) => {
|
|
85
|
+
if (typeof window !== "undefined") globalSource.merge(locale, served);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
/** Activated sources, most recent last; the global one when empty. */
|
|
89
|
+
const activated = [];
|
|
90
|
+
/**
|
|
91
|
+
* Sets the locale of the built-in UI texts, and optionally your own
|
|
92
|
+
* translator for them, for an app that does not use the Vue provider
|
|
93
|
+
* (`provideAsLocale` in `@atscript/vue-form`). Browser-global: never call it
|
|
94
|
+
* on a server that renders for many users.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* setUiLocale("de", (id, params) => i18n.t(id, params));
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
function setUiLocale(locale, translate) {
|
|
102
|
+
globalLocale = locale;
|
|
103
|
+
globalTranslate = translate;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* The texts code outside a component reads (definitions built from
|
|
107
|
+
* metadata, table state): the browser-side app provider's when one is
|
|
108
|
+
* active, else the browser-global ones (`setUiLocale`).
|
|
109
|
+
*
|
|
110
|
+
* @internal Shared with the `@atscript/vue-*` packages.
|
|
111
|
+
*/
|
|
112
|
+
function getUiTexts() {
|
|
113
|
+
return activated.at(-1) ?? globalTexts;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Makes `texts` the ones {@link getUiTexts} returns (browser only — the Vue
|
|
117
|
+
* provider never calls it on a server) until undone; undoing one of several
|
|
118
|
+
* gives the most recent remaining one back. Returns the undo (idempotent).
|
|
119
|
+
*
|
|
120
|
+
* @internal Shared with `@atscript/vue-form`.
|
|
121
|
+
*/
|
|
122
|
+
function activateUiTexts(texts) {
|
|
123
|
+
activated.push(texts);
|
|
124
|
+
let undone = false;
|
|
125
|
+
return () => {
|
|
126
|
+
if (undone) return;
|
|
127
|
+
undone = true;
|
|
128
|
+
activated.splice(activated.lastIndexOf(texts), 1);
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
//#endregion
|
|
132
|
+
exports.activateUiTexts = activateUiTexts;
|
|
133
|
+
exports.createUiTexts = createUiTexts;
|
|
134
|
+
exports.getUiTexts = getUiTexts;
|
|
135
|
+
exports.setUiLocale = setUiLocale;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
//#region src/i18n/ui-texts.d.ts
|
|
2
|
+
/** Message params: the values a text's `{name}` placeholders are filled with. */
|
|
3
|
+
type UiTextParams = Record<string, unknown>;
|
|
4
|
+
/**
|
|
5
|
+
* An app's own translator for the built-in UI texts: return the final text
|
|
6
|
+
* (params already filled in), or `undefined` to fall back to the texts the
|
|
7
|
+
* server served for the locale, then to English. `id` is a code of
|
|
8
|
+
* `AS_UI_MESSAGES` (`UI_CANCEL`, …) or a literal text.
|
|
9
|
+
*/
|
|
10
|
+
type UiTranslate = (id: string, params: UiTextParams | undefined, locale: string | undefined) => string | undefined;
|
|
11
|
+
/** `t(id, params?)` — a UI text in the current locale. */
|
|
12
|
+
type UiT = (id: string, params?: UiTextParams) => string;
|
|
13
|
+
/** Served texts per locale: `{ [locale]: { [code or text]: template } }` (`""` = no locale). */
|
|
14
|
+
type UiDicts = Readonly<Record<string, Readonly<Record<string, string>>>>;
|
|
15
|
+
/**
|
|
16
|
+
* One source of UI texts: the browser-global one (`setUiLocale`) or an app's
|
|
17
|
+
* (`provideAsLocale` in `@atscript/vue-form`).
|
|
18
|
+
*
|
|
19
|
+
* @internal Shared with the `@atscript/vue-*` packages.
|
|
20
|
+
*/
|
|
21
|
+
interface UiTexts {
|
|
22
|
+
/** The current locale. */
|
|
23
|
+
locale: () => string | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* A text in the current locale — reactive when the locale and the dict
|
|
26
|
+
* are: your translator's text, else the text the server served for the
|
|
27
|
+
* locale, else English (`AS_UI_MESSAGES`), else `id` — with `{name}`
|
|
28
|
+
* placeholders filled.
|
|
29
|
+
*/
|
|
30
|
+
t: UiT;
|
|
31
|
+
/**
|
|
32
|
+
* Merges texts the server served for `locale` (an `$i18n` dict: codes and
|
|
33
|
+
* literal texts) into that locale's dict. Pass the locale the request was
|
|
34
|
+
* made for: a late answer for a previous locale lands in that locale's dict,
|
|
35
|
+
* never in the current one.
|
|
36
|
+
*/
|
|
37
|
+
merge: (locale: string | undefined, served: unknown) => void;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Creates a source of UI texts. `dicts` holds the served texts — pass a Vue
|
|
41
|
+
* `shallowRef({})` for reactive lookups; it is replaced wholesale on merge.
|
|
42
|
+
*
|
|
43
|
+
* @internal Shared with the `@atscript/vue-*` packages.
|
|
44
|
+
*/
|
|
45
|
+
declare function createUiTexts(opts: {
|
|
46
|
+
locale: () => string | undefined;
|
|
47
|
+
translate?: () => UiTranslate | undefined;
|
|
48
|
+
dicts?: {
|
|
49
|
+
value: UiDicts;
|
|
50
|
+
};
|
|
51
|
+
}): UiTexts;
|
|
52
|
+
/**
|
|
53
|
+
* Sets the locale of the built-in UI texts, and optionally your own
|
|
54
|
+
* translator for them, for an app that does not use the Vue provider
|
|
55
|
+
* (`provideAsLocale` in `@atscript/vue-form`). Browser-global: never call it
|
|
56
|
+
* on a server that renders for many users.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* setUiLocale("de", (id, params) => i18n.t(id, params));
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
declare function setUiLocale(locale: string | undefined, translate?: UiTranslate): void;
|
|
64
|
+
/**
|
|
65
|
+
* The texts code outside a component reads (definitions built from
|
|
66
|
+
* metadata, table state): the browser-side app provider's when one is
|
|
67
|
+
* active, else the browser-global ones (`setUiLocale`).
|
|
68
|
+
*
|
|
69
|
+
* @internal Shared with the `@atscript/vue-*` packages.
|
|
70
|
+
*/
|
|
71
|
+
declare function getUiTexts(): UiTexts;
|
|
72
|
+
/**
|
|
73
|
+
* Makes `texts` the ones {@link getUiTexts} returns (browser only — the Vue
|
|
74
|
+
* provider never calls it on a server) until undone; undoing one of several
|
|
75
|
+
* gives the most recent remaining one back. Returns the undo (idempotent).
|
|
76
|
+
*
|
|
77
|
+
* @internal Shared with `@atscript/vue-form`.
|
|
78
|
+
*/
|
|
79
|
+
declare function activateUiTexts(texts: UiTexts): () => void;
|
|
80
|
+
//#endregion
|
|
81
|
+
export { UiT, UiTextParams, UiTexts, UiTranslate, activateUiTexts, createUiTexts, getUiTexts, setUiLocale };
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
//#region src/i18n/ui-texts.d.ts
|
|
2
|
+
/** Message params: the values a text's `{name}` placeholders are filled with. */
|
|
3
|
+
type UiTextParams = Record<string, unknown>;
|
|
4
|
+
/**
|
|
5
|
+
* An app's own translator for the built-in UI texts: return the final text
|
|
6
|
+
* (params already filled in), or `undefined` to fall back to the texts the
|
|
7
|
+
* server served for the locale, then to English. `id` is a code of
|
|
8
|
+
* `AS_UI_MESSAGES` (`UI_CANCEL`, …) or a literal text.
|
|
9
|
+
*/
|
|
10
|
+
type UiTranslate = (id: string, params: UiTextParams | undefined, locale: string | undefined) => string | undefined;
|
|
11
|
+
/** `t(id, params?)` — a UI text in the current locale. */
|
|
12
|
+
type UiT = (id: string, params?: UiTextParams) => string;
|
|
13
|
+
/** Served texts per locale: `{ [locale]: { [code or text]: template } }` (`""` = no locale). */
|
|
14
|
+
type UiDicts = Readonly<Record<string, Readonly<Record<string, string>>>>;
|
|
15
|
+
/**
|
|
16
|
+
* One source of UI texts: the browser-global one (`setUiLocale`) or an app's
|
|
17
|
+
* (`provideAsLocale` in `@atscript/vue-form`).
|
|
18
|
+
*
|
|
19
|
+
* @internal Shared with the `@atscript/vue-*` packages.
|
|
20
|
+
*/
|
|
21
|
+
interface UiTexts {
|
|
22
|
+
/** The current locale. */
|
|
23
|
+
locale: () => string | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* A text in the current locale — reactive when the locale and the dict
|
|
26
|
+
* are: your translator's text, else the text the server served for the
|
|
27
|
+
* locale, else English (`AS_UI_MESSAGES`), else `id` — with `{name}`
|
|
28
|
+
* placeholders filled.
|
|
29
|
+
*/
|
|
30
|
+
t: UiT;
|
|
31
|
+
/**
|
|
32
|
+
* Merges texts the server served for `locale` (an `$i18n` dict: codes and
|
|
33
|
+
* literal texts) into that locale's dict. Pass the locale the request was
|
|
34
|
+
* made for: a late answer for a previous locale lands in that locale's dict,
|
|
35
|
+
* never in the current one.
|
|
36
|
+
*/
|
|
37
|
+
merge: (locale: string | undefined, served: unknown) => void;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Creates a source of UI texts. `dicts` holds the served texts — pass a Vue
|
|
41
|
+
* `shallowRef({})` for reactive lookups; it is replaced wholesale on merge.
|
|
42
|
+
*
|
|
43
|
+
* @internal Shared with the `@atscript/vue-*` packages.
|
|
44
|
+
*/
|
|
45
|
+
declare function createUiTexts(opts: {
|
|
46
|
+
locale: () => string | undefined;
|
|
47
|
+
translate?: () => UiTranslate | undefined;
|
|
48
|
+
dicts?: {
|
|
49
|
+
value: UiDicts;
|
|
50
|
+
};
|
|
51
|
+
}): UiTexts;
|
|
52
|
+
/**
|
|
53
|
+
* Sets the locale of the built-in UI texts, and optionally your own
|
|
54
|
+
* translator for them, for an app that does not use the Vue provider
|
|
55
|
+
* (`provideAsLocale` in `@atscript/vue-form`). Browser-global: never call it
|
|
56
|
+
* on a server that renders for many users.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* setUiLocale("de", (id, params) => i18n.t(id, params));
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
declare function setUiLocale(locale: string | undefined, translate?: UiTranslate): void;
|
|
64
|
+
/**
|
|
65
|
+
* The texts code outside a component reads (definitions built from
|
|
66
|
+
* metadata, table state): the browser-side app provider's when one is
|
|
67
|
+
* active, else the browser-global ones (`setUiLocale`).
|
|
68
|
+
*
|
|
69
|
+
* @internal Shared with the `@atscript/vue-*` packages.
|
|
70
|
+
*/
|
|
71
|
+
declare function getUiTexts(): UiTexts;
|
|
72
|
+
/**
|
|
73
|
+
* Makes `texts` the ones {@link getUiTexts} returns (browser only — the Vue
|
|
74
|
+
* provider never calls it on a server) until undone; undoing one of several
|
|
75
|
+
* gives the most recent remaining one back. Returns the undo (idempotent).
|
|
76
|
+
*
|
|
77
|
+
* @internal Shared with `@atscript/vue-form`.
|
|
78
|
+
*/
|
|
79
|
+
declare function activateUiTexts(texts: UiTexts): () => void;
|
|
80
|
+
//#endregion
|
|
81
|
+
export { UiT, UiTextParams, UiTexts, UiTranslate, activateUiTexts, createUiTexts, getUiTexts, setUiLocale };
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { AS_UI_MESSAGES } from "../messages.mjs";
|
|
2
|
+
//#region src/i18n/ui-texts.ts
|
|
3
|
+
const PLACEHOLDER = /\{(\w+)\}/g;
|
|
4
|
+
/**
|
|
5
|
+
* Fills `{name}` placeholders in one pass (arrays joined with `, `); a
|
|
6
|
+
* placeholder without a param stays, filled-in values are never filled again.
|
|
7
|
+
*/
|
|
8
|
+
function fill(template, params) {
|
|
9
|
+
if (!params || !template.includes("{")) return template;
|
|
10
|
+
return template.replace(PLACEHOLDER, (match, name) => {
|
|
11
|
+
const value = Object.hasOwn(params, name) ? params[name] : void 0;
|
|
12
|
+
if (value === void 0) return match;
|
|
13
|
+
return Array.isArray(value) ? value.map(String).join(", ") : String(value);
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
let failureLogged = false;
|
|
17
|
+
/** The translator's text; `undefined` when it has none or throws (logged once). */
|
|
18
|
+
function callTranslator(translate, id, params, locale) {
|
|
19
|
+
try {
|
|
20
|
+
const out = translate(id, params, locale);
|
|
21
|
+
return typeof out === "string" ? out : void 0;
|
|
22
|
+
} catch (error) {
|
|
23
|
+
if (!failureLogged) {
|
|
24
|
+
failureLogged = true;
|
|
25
|
+
console.error(`[atscript-ui] The UI translator threw for "${id}" (logged once)`, error);
|
|
26
|
+
}
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/** The string entries of a served `$i18n` dict, `undefined` when it has none. */
|
|
31
|
+
function servedEntries(served) {
|
|
32
|
+
if (typeof served !== "object" || served === null || Array.isArray(served)) return void 0;
|
|
33
|
+
let out;
|
|
34
|
+
for (const [id, text] of Object.entries(served)) if (typeof text === "string") (out ??= {})[id] = text;
|
|
35
|
+
return out;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Creates a source of UI texts. `dicts` holds the served texts — pass a Vue
|
|
39
|
+
* `shallowRef({})` for reactive lookups; it is replaced wholesale on merge.
|
|
40
|
+
*
|
|
41
|
+
* @internal Shared with the `@atscript/vue-*` packages.
|
|
42
|
+
*/
|
|
43
|
+
function createUiTexts(opts) {
|
|
44
|
+
const { locale, translate } = opts;
|
|
45
|
+
const dicts = opts.dicts ?? { value: {} };
|
|
46
|
+
const t = (id, params) => {
|
|
47
|
+
const loc = locale();
|
|
48
|
+
const translator = translate?.();
|
|
49
|
+
if (translator) {
|
|
50
|
+
const own = callTranslator(translator, id, params, loc);
|
|
51
|
+
if (own !== void 0 && own !== id) return own;
|
|
52
|
+
}
|
|
53
|
+
const dict = dicts.value[loc ?? ""];
|
|
54
|
+
return fill(dict && Object.hasOwn(dict, id) ? dict[id] : Object.hasOwn(AS_UI_MESSAGES, id) ? AS_UI_MESSAGES[id] : id, params);
|
|
55
|
+
};
|
|
56
|
+
const merge = (loc, served) => {
|
|
57
|
+
const entries = servedEntries(served);
|
|
58
|
+
if (!entries) return;
|
|
59
|
+
const key = loc ?? "";
|
|
60
|
+
const prev = dicts.value[key];
|
|
61
|
+
if (prev && Object.keys(entries).every((id) => prev[id] === entries[id])) return;
|
|
62
|
+
dicts.value = {
|
|
63
|
+
...dicts.value,
|
|
64
|
+
[key]: Object.freeze({
|
|
65
|
+
...prev,
|
|
66
|
+
...entries
|
|
67
|
+
})
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
return {
|
|
71
|
+
locale,
|
|
72
|
+
t,
|
|
73
|
+
merge
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
let globalLocale;
|
|
77
|
+
let globalTranslate;
|
|
78
|
+
const globalSource = createUiTexts({
|
|
79
|
+
locale: () => globalLocale,
|
|
80
|
+
translate: () => globalTranslate
|
|
81
|
+
});
|
|
82
|
+
const globalTexts = {
|
|
83
|
+
...globalSource,
|
|
84
|
+
merge: (locale, served) => {
|
|
85
|
+
if (typeof window !== "undefined") globalSource.merge(locale, served);
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
/** Activated sources, most recent last; the global one when empty. */
|
|
89
|
+
const activated = [];
|
|
90
|
+
/**
|
|
91
|
+
* Sets the locale of the built-in UI texts, and optionally your own
|
|
92
|
+
* translator for them, for an app that does not use the Vue provider
|
|
93
|
+
* (`provideAsLocale` in `@atscript/vue-form`). Browser-global: never call it
|
|
94
|
+
* on a server that renders for many users.
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* setUiLocale("de", (id, params) => i18n.t(id, params));
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
function setUiLocale(locale, translate) {
|
|
102
|
+
globalLocale = locale;
|
|
103
|
+
globalTranslate = translate;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* The texts code outside a component reads (definitions built from
|
|
107
|
+
* metadata, table state): the browser-side app provider's when one is
|
|
108
|
+
* active, else the browser-global ones (`setUiLocale`).
|
|
109
|
+
*
|
|
110
|
+
* @internal Shared with the `@atscript/vue-*` packages.
|
|
111
|
+
*/
|
|
112
|
+
function getUiTexts() {
|
|
113
|
+
return activated.at(-1) ?? globalTexts;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Makes `texts` the ones {@link getUiTexts} returns (browser only — the Vue
|
|
117
|
+
* provider never calls it on a server) until undone; undoing one of several
|
|
118
|
+
* gives the most recent remaining one back. Returns the undo (idempotent).
|
|
119
|
+
*
|
|
120
|
+
* @internal Shared with `@atscript/vue-form`.
|
|
121
|
+
*/
|
|
122
|
+
function activateUiTexts(texts) {
|
|
123
|
+
activated.push(texts);
|
|
124
|
+
let undone = false;
|
|
125
|
+
return () => {
|
|
126
|
+
if (undone) return;
|
|
127
|
+
undone = true;
|
|
128
|
+
activated.splice(activated.lastIndexOf(texts), 1);
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
//#endregion
|
|
132
|
+
export { activateUiTexts, createUiTexts, getUiTexts, setUiLocale };
|