@atscript/ui-fns 0.1.164 → 0.1.166
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/index.cjs +102 -24
- package/dist/index.d.cts +34 -6
- package/dist/index.d.mts +34 -6
- package/dist/index.mjs +103 -26
- package/dist/plugin.cjs +30 -22
- package/dist/plugin.mjs +30 -22
- package/package.json +6 -6
package/dist/index.cjs
CHANGED
|
@@ -2,25 +2,46 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
2
2
|
let _atscript_ui = require("@atscript/ui");
|
|
3
3
|
//#region src/runtime/fn-compiler.ts
|
|
4
4
|
const pool = new (require("@prostojs/deserialize-fn")).FNPool();
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
/** The scope's `t` (see {@link TFnScope.t}), else the active UI texts'. */
|
|
6
|
+
function scopeT(scope) {
|
|
7
|
+
return scope.t ?? (0, _atscript_ui.getUiTexts)().t;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* A compiler calling the function string with `args`, `t` filled by
|
|
11
|
+
* {@link scopeT}. Cached per annotation string, so a hot evaluation skips
|
|
12
|
+
* building the wrapper code string and hashing it in the pool; one cache per
|
|
13
|
+
* kind, since the same string compiles differently per kind.
|
|
14
|
+
*/
|
|
15
|
+
function fnCompiler(args) {
|
|
16
|
+
const cache = /* @__PURE__ */ new Map();
|
|
17
|
+
return (fnStr) => {
|
|
18
|
+
let fn = cache.get(fnStr);
|
|
19
|
+
if (!fn) {
|
|
20
|
+
const run = pool.getFn(`return (${fnStr})(${args})`);
|
|
21
|
+
fn = (scope) => run(scope.t ? scope : {
|
|
22
|
+
...scope,
|
|
23
|
+
t: scopeT(scope)
|
|
24
|
+
});
|
|
25
|
+
cache.set(fnStr, fn);
|
|
26
|
+
}
|
|
27
|
+
return fn;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
const fieldFn = fnCompiler("v, data, context, entry, t");
|
|
31
|
+
const topFn = fnCompiler("data, context, t");
|
|
32
|
+
const tableFn = fnCompiler("row, ctx, t");
|
|
7
33
|
/**
|
|
8
|
-
* Compiles a field-level function string from a `@ui.form.fn.*`
|
|
34
|
+
* Compiles a field-level function string from a `@ui.form.fn.*` annotation
|
|
9
35
|
* into a callable function. Compiled once per string and cached (via FNPool).
|
|
10
36
|
*
|
|
11
37
|
* The function string should be an arrow or regular function expression:
|
|
12
38
|
* `"(v, data, ctx, entry) => !data.firstName"`
|
|
13
39
|
*
|
|
14
40
|
* The compiled function receives a single TFnScope object:
|
|
15
|
-
* `{ v, data, context, entry }`
|
|
41
|
+
* `{ v, data, context, entry, t }` — `t` is the trailing argument (see {@link TFnScope.t}).
|
|
16
42
|
*/
|
|
17
43
|
function compileFieldFn(fnStr) {
|
|
18
|
-
|
|
19
|
-
if (!fn) {
|
|
20
|
-
fn = pool.getFn(`return (${fnStr})(v, data, context, entry)`);
|
|
21
|
-
fieldFns.set(fnStr, fn);
|
|
22
|
-
}
|
|
23
|
-
return fn;
|
|
44
|
+
return fieldFn(fnStr);
|
|
24
45
|
}
|
|
25
46
|
/**
|
|
26
47
|
* Compiles a form-level function string from a `@ui.form.fn.title` or similar annotation.
|
|
@@ -29,15 +50,20 @@ function compileFieldFn(fnStr) {
|
|
|
29
50
|
* `"(data, ctx) => someExpression"`
|
|
30
51
|
*
|
|
31
52
|
* The compiled function receives a single TFnScope object:
|
|
32
|
-
* `{ data, context }`
|
|
53
|
+
* `{ data, context, t }` — `t` is the trailing argument.
|
|
33
54
|
*/
|
|
34
55
|
function compileTopFn(fnStr) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
56
|
+
return topFn(fnStr);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Compiles a per-cell function string from a `@ui.table.fn.*` annotation:
|
|
60
|
+
* `"(row, ctx) => row.status === 'paid' ? 'text-success' : ''"`
|
|
61
|
+
*
|
|
62
|
+
* The compiled function receives a {@link TTableFnScope} `{ row, ctx, t }` and
|
|
63
|
+
* calls the function as `(row, ctx, t)`.
|
|
64
|
+
*/
|
|
65
|
+
function compileTableFn(fnStr) {
|
|
66
|
+
return tableFn(fnStr);
|
|
41
67
|
}
|
|
42
68
|
/**
|
|
43
69
|
* Compiles a validator function string.
|
|
@@ -53,6 +79,48 @@ function compileValidatorFn(fnStr) {
|
|
|
53
79
|
/** Fn keys that store `[{ name, fn }]` arrays rather than a single fn string. */
|
|
54
80
|
const ATTR_FN_KEYS = new Set([_atscript_ui.UI_FORM_FN_ATTR, _atscript_ui.UI_TABLE_FN_ATTR]);
|
|
55
81
|
/**
|
|
82
|
+
* Fn keys whose result is a text shown to users: a string result that is a
|
|
83
|
+
* text the server translated (an exact match) is shown translated.
|
|
84
|
+
*/
|
|
85
|
+
const TEXT_FN_KEYS = new Set([
|
|
86
|
+
_atscript_ui.UI_FORM_FN_TITLE,
|
|
87
|
+
_atscript_ui.UI_FORM_FN_SUBMIT_TEXT,
|
|
88
|
+
_atscript_ui.UI_FORM_FN_LABEL,
|
|
89
|
+
_atscript_ui.UI_FORM_FN_DESCRIPTION,
|
|
90
|
+
_atscript_ui.UI_FORM_FN_HINT,
|
|
91
|
+
_atscript_ui.UI_FORM_FN_PLACEHOLDER
|
|
92
|
+
]);
|
|
93
|
+
/**
|
|
94
|
+
* Option labels through `t` — new option objects only for labels that change
|
|
95
|
+
* (the fn may return objects it shares, e.g. from `context`); the list itself
|
|
96
|
+
* when none does.
|
|
97
|
+
*/
|
|
98
|
+
function translateOptionLabels(options, t) {
|
|
99
|
+
let out;
|
|
100
|
+
for (let i = 0; i < options.length; i++) {
|
|
101
|
+
const option = options[i];
|
|
102
|
+
if (typeof option !== "object" || option === null || typeof option.label !== "string") continue;
|
|
103
|
+
const label = t(option.label);
|
|
104
|
+
if (label === option.label) continue;
|
|
105
|
+
out ??= options.slice();
|
|
106
|
+
out[i] = {
|
|
107
|
+
...option,
|
|
108
|
+
label
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
return out ?? options;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* A fn result in the app's language: a text result translated by exact
|
|
115
|
+
* match, the labels of computed options too (never their keys: a string
|
|
116
|
+
* option is its own key).
|
|
117
|
+
*/
|
|
118
|
+
function translateResult(fnKey, result, scope) {
|
|
119
|
+
if (TEXT_FN_KEYS.has(fnKey)) return typeof result === "string" ? scopeT(scope)(result) : result;
|
|
120
|
+
if (fnKey === _atscript_ui.UI_FORM_FN_OPTIONS && Array.isArray(result)) return translateOptionLabels(result, scopeT(scope));
|
|
121
|
+
return result;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
56
124
|
* Dynamic field resolver — extends static resolution with `new Function` compilation
|
|
57
125
|
* for `ui.fn.*` annotation keys.
|
|
58
126
|
*
|
|
@@ -61,7 +129,7 @@ const ATTR_FN_KEYS = new Set([_atscript_ui.UI_FORM_FN_ATTR, _atscript_ui.UI_TABL
|
|
|
61
129
|
var DynamicFieldResolver = class {
|
|
62
130
|
resolveFieldProp(prop, fnKey, staticKey, scope, opts) {
|
|
63
131
|
if (ATTR_FN_KEYS.has(fnKey)) return this.resolveAttrFns(prop, fnKey, scope);
|
|
64
|
-
return resolveAnnotatedProp(prop.metadata, fnKey, staticKey, scope,
|
|
132
|
+
return resolveAnnotatedProp(prop.metadata, fnKey, staticKey, scope, compilerOf(fnKey), opts);
|
|
65
133
|
}
|
|
66
134
|
resolveFormProp(type, fnKey, staticKey, scope, opts) {
|
|
67
135
|
return resolveAnnotatedProp(type.metadata, fnKey, staticKey, scope, compileTopFn, opts);
|
|
@@ -78,17 +146,22 @@ var DynamicFieldResolver = class {
|
|
|
78
146
|
if (!fnAttrs) return void 0;
|
|
79
147
|
const result = {};
|
|
80
148
|
let hasAttrs = false;
|
|
149
|
+
const compile = compilerOf(fnKey);
|
|
81
150
|
for (const item of (0, _atscript_ui.asArray)(fnAttrs)) if (typeof item === "object" && item !== null && "name" in item && "fn" in item) {
|
|
82
151
|
const { name, fn } = item;
|
|
83
|
-
result[name] =
|
|
152
|
+
result[name] = compile(fn)(scope);
|
|
84
153
|
hasAttrs = true;
|
|
85
154
|
}
|
|
86
155
|
return hasAttrs ? result : void 0;
|
|
87
156
|
}
|
|
88
157
|
};
|
|
158
|
+
/** The compiler of a field-level fn key: per-cell `(row, ctx, t)` for `ui.table.fn.*`. */
|
|
159
|
+
function compilerOf(fnKey) {
|
|
160
|
+
return fnKey.startsWith(_atscript_ui.UI_TABLE_FN_PREFIX) ? compileTableFn : compileFieldFn;
|
|
161
|
+
}
|
|
89
162
|
function resolveAnnotatedProp(metadata, fnKey, staticKey, scope, compileFn, opts) {
|
|
90
163
|
const fnStr = metadata.get(fnKey);
|
|
91
|
-
if (typeof fnStr === "string") return compileFn(fnStr)(scope);
|
|
164
|
+
if (typeof fnStr === "string") return translateResult(fnKey, compileFn(fnStr)(scope), scope);
|
|
92
165
|
return (0, _atscript_ui.resolveStatic)(metadata, staticKey, opts);
|
|
93
166
|
}
|
|
94
167
|
/**
|
|
@@ -158,18 +231,22 @@ function uiFnsValidatorPlugin() {
|
|
|
158
231
|
const container = (0, _atscript_ui.isContainerKind)(def.type.kind);
|
|
159
232
|
if (container && containerShapeMismatch(def.type.kind, value)) return void 0;
|
|
160
233
|
const fnsCtx = ctx.context;
|
|
234
|
+
const data = fnsCtx?.data ?? {};
|
|
235
|
+
const context = fnsCtx?.context ?? {};
|
|
236
|
+
const t = fnsCtx?.t ?? (0, _atscript_ui.getUiTexts)().t;
|
|
161
237
|
const scope = buildFieldEntry(def, {
|
|
162
238
|
v: value,
|
|
163
|
-
data
|
|
164
|
-
context
|
|
165
|
-
entry: void 0
|
|
239
|
+
data,
|
|
240
|
+
context,
|
|
241
|
+
entry: void 0,
|
|
242
|
+
t
|
|
166
243
|
}, ctx.path);
|
|
167
244
|
const fns = (0, _atscript_ui.asArray)(hasValidators);
|
|
168
245
|
for (const fnStr of fns) {
|
|
169
246
|
if (typeof fnStr !== "string") continue;
|
|
170
247
|
const result = compileValidatorFn(fnStr)(scope);
|
|
171
248
|
if (result !== true) {
|
|
172
|
-
ctx.error(typeof result === "string" ? result : "
|
|
249
|
+
ctx.error(t(typeof result === "string" ? result : "UI_VALIDATION_FAILED"));
|
|
173
250
|
if (container) ctx.validateAnnotatedType(def, value);
|
|
174
251
|
return false;
|
|
175
252
|
}
|
|
@@ -196,6 +273,7 @@ function installDynamicResolver() {
|
|
|
196
273
|
exports.DynamicFieldResolver = DynamicFieldResolver;
|
|
197
274
|
exports.buildFieldEntry = buildFieldEntry;
|
|
198
275
|
exports.compileFieldFn = compileFieldFn;
|
|
276
|
+
exports.compileTableFn = compileTableFn;
|
|
199
277
|
exports.compileTopFn = compileTopFn;
|
|
200
278
|
exports.compileValidatorFn = compileValidatorFn;
|
|
201
279
|
exports.installDynamicResolver = installDynamicResolver;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { FieldResolver, TFormEntryOptions, TResolveOptions } from "@atscript/ui";
|
|
1
|
+
import { FieldResolver, TFormEntryOptions, TResolveOptions, UiT } from "@atscript/ui";
|
|
2
2
|
import { TAtscriptAnnotatedType, TValidatorPlugin } from "@atscript/typescript/utils";
|
|
3
3
|
|
|
4
4
|
//#region src/runtime/types.d.ts
|
|
5
5
|
/**
|
|
6
6
|
* Scope object passed to compiled functions.
|
|
7
7
|
* Properties become variables inside compiled function strings:
|
|
8
|
-
* v, data, context, entry
|
|
8
|
+
* v, data, context, entry, t
|
|
9
9
|
*/
|
|
10
10
|
interface TFnScope<V = unknown, D = Record<string, unknown>, C = Record<string, unknown>> {
|
|
11
11
|
v?: V;
|
|
@@ -13,6 +13,24 @@ interface TFnScope<V = unknown, D = Record<string, unknown>, C = Record<string,
|
|
|
13
13
|
context: C;
|
|
14
14
|
entry?: TFieldEvaluated;
|
|
15
15
|
action?: string;
|
|
16
|
+
/**
|
|
17
|
+
* `t(text, params?)` — a text in the app's language: the translation the
|
|
18
|
+
* server served for it (exact match), else `text` itself, `{name}`
|
|
19
|
+
* placeholders filled from `params`. The trailing argument of every fn.
|
|
20
|
+
* Default: the active UI texts (`provideAsLocale` / `setUiLocale`).
|
|
21
|
+
*/
|
|
22
|
+
t?: UiT;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Scope of a `@ui.table.fn.*` function, called as `(row, ctx, t)`.
|
|
26
|
+
*/
|
|
27
|
+
interface TTableFnScope<R = Record<string, unknown>> {
|
|
28
|
+
/** The row's data. */
|
|
29
|
+
row: R;
|
|
30
|
+
/** Table state: `searchTerm`, `filters`, `sorters`, `rowIndex`. */
|
|
31
|
+
ctx: Record<string, unknown>;
|
|
32
|
+
/** As {@link TFnScope.t}. */
|
|
33
|
+
t?: UiT;
|
|
16
34
|
}
|
|
17
35
|
/**
|
|
18
36
|
* A value that is either static or a function of the fn scope.
|
|
@@ -36,14 +54,14 @@ interface TFieldEvaluated {
|
|
|
36
54
|
//#endregion
|
|
37
55
|
//#region src/runtime/fn-compiler.d.ts
|
|
38
56
|
/**
|
|
39
|
-
* Compiles a field-level function string from a `@ui.form.fn.*`
|
|
57
|
+
* Compiles a field-level function string from a `@ui.form.fn.*` annotation
|
|
40
58
|
* into a callable function. Compiled once per string and cached (via FNPool).
|
|
41
59
|
*
|
|
42
60
|
* The function string should be an arrow or regular function expression:
|
|
43
61
|
* `"(v, data, ctx, entry) => !data.firstName"`
|
|
44
62
|
*
|
|
45
63
|
* The compiled function receives a single TFnScope object:
|
|
46
|
-
* `{ v, data, context, entry }`
|
|
64
|
+
* `{ v, data, context, entry, t }` — `t` is the trailing argument (see {@link TFnScope.t}).
|
|
47
65
|
*/
|
|
48
66
|
declare function compileFieldFn<R = unknown>(fnStr: string): (scope: TFnScope) => R;
|
|
49
67
|
/**
|
|
@@ -53,9 +71,17 @@ declare function compileFieldFn<R = unknown>(fnStr: string): (scope: TFnScope) =
|
|
|
53
71
|
* `"(data, ctx) => someExpression"`
|
|
54
72
|
*
|
|
55
73
|
* The compiled function receives a single TFnScope object:
|
|
56
|
-
* `{ data, context }`
|
|
74
|
+
* `{ data, context, t }` — `t` is the trailing argument.
|
|
57
75
|
*/
|
|
58
76
|
declare function compileTopFn<R = unknown>(fnStr: string): (scope: TFnScope) => R;
|
|
77
|
+
/**
|
|
78
|
+
* Compiles a per-cell function string from a `@ui.table.fn.*` annotation:
|
|
79
|
+
* `"(row, ctx) => row.status === 'paid' ? 'text-success' : ''"`
|
|
80
|
+
*
|
|
81
|
+
* The compiled function receives a {@link TTableFnScope} `{ row, ctx, t }` and
|
|
82
|
+
* calls the function as `(row, ctx, t)`.
|
|
83
|
+
*/
|
|
84
|
+
declare function compileTableFn<R = unknown>(fnStr: string): (scope: TTableFnScope) => R;
|
|
59
85
|
/**
|
|
60
86
|
* Compiles a validator function string.
|
|
61
87
|
* Delegates to `compileFieldFn` with a narrowed return type.
|
|
@@ -95,6 +121,8 @@ declare function buildFieldEntry(prop: TAtscriptAnnotatedType, baseScope: TFnSco
|
|
|
95
121
|
interface TValidatorContext {
|
|
96
122
|
data: Record<string, unknown>;
|
|
97
123
|
context: Record<string, unknown>;
|
|
124
|
+
/** The rules' `t` and the language of their messages; the active UI texts when omitted. */
|
|
125
|
+
t?: UiT;
|
|
98
126
|
}
|
|
99
127
|
/**
|
|
100
128
|
* Creates an ATScript validator plugin that processes `@ui.form.validate` annotations
|
|
@@ -129,4 +157,4 @@ declare function uiFnsValidatorPlugin(): TValidatorPlugin;
|
|
|
129
157
|
*/
|
|
130
158
|
declare function installDynamicResolver(): void;
|
|
131
159
|
//#endregion
|
|
132
|
-
export { DynamicFieldResolver, type TBuildFieldEntryOpts, type TComputed, type TFieldEvaluated, type TFnScope, type TValidatorContext, buildFieldEntry, compileFieldFn, compileTopFn, compileValidatorFn, installDynamicResolver, uiFnsValidatorPlugin };
|
|
160
|
+
export { DynamicFieldResolver, type TBuildFieldEntryOpts, type TComputed, type TFieldEvaluated, type TFnScope, type TTableFnScope, type TValidatorContext, buildFieldEntry, compileFieldFn, compileTableFn, compileTopFn, compileValidatorFn, installDynamicResolver, uiFnsValidatorPlugin };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { FieldResolver, TFormEntryOptions, TResolveOptions } from "@atscript/ui";
|
|
1
|
+
import { FieldResolver, TFormEntryOptions, TResolveOptions, UiT } from "@atscript/ui";
|
|
2
2
|
import { TAtscriptAnnotatedType, TValidatorPlugin } from "@atscript/typescript/utils";
|
|
3
3
|
|
|
4
4
|
//#region src/runtime/types.d.ts
|
|
5
5
|
/**
|
|
6
6
|
* Scope object passed to compiled functions.
|
|
7
7
|
* Properties become variables inside compiled function strings:
|
|
8
|
-
* v, data, context, entry
|
|
8
|
+
* v, data, context, entry, t
|
|
9
9
|
*/
|
|
10
10
|
interface TFnScope<V = unknown, D = Record<string, unknown>, C = Record<string, unknown>> {
|
|
11
11
|
v?: V;
|
|
@@ -13,6 +13,24 @@ interface TFnScope<V = unknown, D = Record<string, unknown>, C = Record<string,
|
|
|
13
13
|
context: C;
|
|
14
14
|
entry?: TFieldEvaluated;
|
|
15
15
|
action?: string;
|
|
16
|
+
/**
|
|
17
|
+
* `t(text, params?)` — a text in the app's language: the translation the
|
|
18
|
+
* server served for it (exact match), else `text` itself, `{name}`
|
|
19
|
+
* placeholders filled from `params`. The trailing argument of every fn.
|
|
20
|
+
* Default: the active UI texts (`provideAsLocale` / `setUiLocale`).
|
|
21
|
+
*/
|
|
22
|
+
t?: UiT;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Scope of a `@ui.table.fn.*` function, called as `(row, ctx, t)`.
|
|
26
|
+
*/
|
|
27
|
+
interface TTableFnScope<R = Record<string, unknown>> {
|
|
28
|
+
/** The row's data. */
|
|
29
|
+
row: R;
|
|
30
|
+
/** Table state: `searchTerm`, `filters`, `sorters`, `rowIndex`. */
|
|
31
|
+
ctx: Record<string, unknown>;
|
|
32
|
+
/** As {@link TFnScope.t}. */
|
|
33
|
+
t?: UiT;
|
|
16
34
|
}
|
|
17
35
|
/**
|
|
18
36
|
* A value that is either static or a function of the fn scope.
|
|
@@ -36,14 +54,14 @@ interface TFieldEvaluated {
|
|
|
36
54
|
//#endregion
|
|
37
55
|
//#region src/runtime/fn-compiler.d.ts
|
|
38
56
|
/**
|
|
39
|
-
* Compiles a field-level function string from a `@ui.form.fn.*`
|
|
57
|
+
* Compiles a field-level function string from a `@ui.form.fn.*` annotation
|
|
40
58
|
* into a callable function. Compiled once per string and cached (via FNPool).
|
|
41
59
|
*
|
|
42
60
|
* The function string should be an arrow or regular function expression:
|
|
43
61
|
* `"(v, data, ctx, entry) => !data.firstName"`
|
|
44
62
|
*
|
|
45
63
|
* The compiled function receives a single TFnScope object:
|
|
46
|
-
* `{ v, data, context, entry }`
|
|
64
|
+
* `{ v, data, context, entry, t }` — `t` is the trailing argument (see {@link TFnScope.t}).
|
|
47
65
|
*/
|
|
48
66
|
declare function compileFieldFn<R = unknown>(fnStr: string): (scope: TFnScope) => R;
|
|
49
67
|
/**
|
|
@@ -53,9 +71,17 @@ declare function compileFieldFn<R = unknown>(fnStr: string): (scope: TFnScope) =
|
|
|
53
71
|
* `"(data, ctx) => someExpression"`
|
|
54
72
|
*
|
|
55
73
|
* The compiled function receives a single TFnScope object:
|
|
56
|
-
* `{ data, context }`
|
|
74
|
+
* `{ data, context, t }` — `t` is the trailing argument.
|
|
57
75
|
*/
|
|
58
76
|
declare function compileTopFn<R = unknown>(fnStr: string): (scope: TFnScope) => R;
|
|
77
|
+
/**
|
|
78
|
+
* Compiles a per-cell function string from a `@ui.table.fn.*` annotation:
|
|
79
|
+
* `"(row, ctx) => row.status === 'paid' ? 'text-success' : ''"`
|
|
80
|
+
*
|
|
81
|
+
* The compiled function receives a {@link TTableFnScope} `{ row, ctx, t }` and
|
|
82
|
+
* calls the function as `(row, ctx, t)`.
|
|
83
|
+
*/
|
|
84
|
+
declare function compileTableFn<R = unknown>(fnStr: string): (scope: TTableFnScope) => R;
|
|
59
85
|
/**
|
|
60
86
|
* Compiles a validator function string.
|
|
61
87
|
* Delegates to `compileFieldFn` with a narrowed return type.
|
|
@@ -95,6 +121,8 @@ declare function buildFieldEntry(prop: TAtscriptAnnotatedType, baseScope: TFnSco
|
|
|
95
121
|
interface TValidatorContext {
|
|
96
122
|
data: Record<string, unknown>;
|
|
97
123
|
context: Record<string, unknown>;
|
|
124
|
+
/** The rules' `t` and the language of their messages; the active UI texts when omitted. */
|
|
125
|
+
t?: UiT;
|
|
98
126
|
}
|
|
99
127
|
/**
|
|
100
128
|
* Creates an ATScript validator plugin that processes `@ui.form.validate` annotations
|
|
@@ -129,4 +157,4 @@ declare function uiFnsValidatorPlugin(): TValidatorPlugin;
|
|
|
129
157
|
*/
|
|
130
158
|
declare function installDynamicResolver(): void;
|
|
131
159
|
//#endregion
|
|
132
|
-
export { DynamicFieldResolver, type TBuildFieldEntryOpts, type TComputed, type TFieldEvaluated, type TFnScope, type TValidatorContext, buildFieldEntry, compileFieldFn, compileTopFn, compileValidatorFn, installDynamicResolver, uiFnsValidatorPlugin };
|
|
160
|
+
export { DynamicFieldResolver, type TBuildFieldEntryOpts, type TComputed, type TFieldEvaluated, type TFnScope, type TTableFnScope, type TValidatorContext, buildFieldEntry, compileFieldFn, compileTableFn, compileTopFn, compileValidatorFn, installDynamicResolver, uiFnsValidatorPlugin };
|
package/dist/index.mjs
CHANGED
|
@@ -1,26 +1,47 @@
|
|
|
1
|
-
import { META_READONLY, UI_FORM_COMPONENT, UI_FORM_DISABLED, UI_FORM_FN_ATTR, UI_FORM_FN_DISABLED, UI_FORM_FN_HIDDEN, UI_FORM_FN_PREFIX, UI_FORM_FN_READONLY, UI_FORM_HIDDEN, UI_FORM_TYPE, UI_FORM_VALIDATE, UI_TABLE_FN_ATTR, UI_TABLE_FN_PREFIX, UI_TYPE, asArray, getFieldMeta, isContainerKind, resolveFieldProp, resolveOptions, resolveStatic, setDefaultValidatorPlugins, setResolver } from "@atscript/ui";
|
|
1
|
+
import { META_READONLY, UI_FORM_COMPONENT, UI_FORM_DISABLED, UI_FORM_FN_ATTR, UI_FORM_FN_DESCRIPTION, UI_FORM_FN_DISABLED, UI_FORM_FN_HIDDEN, UI_FORM_FN_HINT, UI_FORM_FN_LABEL, UI_FORM_FN_OPTIONS, UI_FORM_FN_PLACEHOLDER, UI_FORM_FN_PREFIX, UI_FORM_FN_READONLY, UI_FORM_FN_SUBMIT_TEXT, UI_FORM_FN_TITLE, UI_FORM_HIDDEN, UI_FORM_TYPE, UI_FORM_VALIDATE, UI_TABLE_FN_ATTR, UI_TABLE_FN_PREFIX, UI_TYPE, asArray, getFieldMeta, getUiTexts, isContainerKind, resolveFieldProp, resolveOptions, resolveStatic, setDefaultValidatorPlugins, setResolver } from "@atscript/ui";
|
|
2
2
|
import { FNPool } from "@prostojs/deserialize-fn";
|
|
3
3
|
//#region src/runtime/fn-compiler.ts
|
|
4
4
|
const pool = new FNPool();
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
/** The scope's `t` (see {@link TFnScope.t}), else the active UI texts'. */
|
|
6
|
+
function scopeT(scope) {
|
|
7
|
+
return scope.t ?? getUiTexts().t;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* A compiler calling the function string with `args`, `t` filled by
|
|
11
|
+
* {@link scopeT}. Cached per annotation string, so a hot evaluation skips
|
|
12
|
+
* building the wrapper code string and hashing it in the pool; one cache per
|
|
13
|
+
* kind, since the same string compiles differently per kind.
|
|
14
|
+
*/
|
|
15
|
+
function fnCompiler(args) {
|
|
16
|
+
const cache = /* @__PURE__ */ new Map();
|
|
17
|
+
return (fnStr) => {
|
|
18
|
+
let fn = cache.get(fnStr);
|
|
19
|
+
if (!fn) {
|
|
20
|
+
const run = pool.getFn(`return (${fnStr})(${args})`);
|
|
21
|
+
fn = (scope) => run(scope.t ? scope : {
|
|
22
|
+
...scope,
|
|
23
|
+
t: scopeT(scope)
|
|
24
|
+
});
|
|
25
|
+
cache.set(fnStr, fn);
|
|
26
|
+
}
|
|
27
|
+
return fn;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
const fieldFn = fnCompiler("v, data, context, entry, t");
|
|
31
|
+
const topFn = fnCompiler("data, context, t");
|
|
32
|
+
const tableFn = fnCompiler("row, ctx, t");
|
|
7
33
|
/**
|
|
8
|
-
* Compiles a field-level function string from a `@ui.form.fn.*`
|
|
34
|
+
* Compiles a field-level function string from a `@ui.form.fn.*` annotation
|
|
9
35
|
* into a callable function. Compiled once per string and cached (via FNPool).
|
|
10
36
|
*
|
|
11
37
|
* The function string should be an arrow or regular function expression:
|
|
12
38
|
* `"(v, data, ctx, entry) => !data.firstName"`
|
|
13
39
|
*
|
|
14
40
|
* The compiled function receives a single TFnScope object:
|
|
15
|
-
* `{ v, data, context, entry }`
|
|
41
|
+
* `{ v, data, context, entry, t }` — `t` is the trailing argument (see {@link TFnScope.t}).
|
|
16
42
|
*/
|
|
17
43
|
function compileFieldFn(fnStr) {
|
|
18
|
-
|
|
19
|
-
if (!fn) {
|
|
20
|
-
fn = pool.getFn(`return (${fnStr})(v, data, context, entry)`);
|
|
21
|
-
fieldFns.set(fnStr, fn);
|
|
22
|
-
}
|
|
23
|
-
return fn;
|
|
44
|
+
return fieldFn(fnStr);
|
|
24
45
|
}
|
|
25
46
|
/**
|
|
26
47
|
* Compiles a form-level function string from a `@ui.form.fn.title` or similar annotation.
|
|
@@ -29,15 +50,20 @@ function compileFieldFn(fnStr) {
|
|
|
29
50
|
* `"(data, ctx) => someExpression"`
|
|
30
51
|
*
|
|
31
52
|
* The compiled function receives a single TFnScope object:
|
|
32
|
-
* `{ data, context }`
|
|
53
|
+
* `{ data, context, t }` — `t` is the trailing argument.
|
|
33
54
|
*/
|
|
34
55
|
function compileTopFn(fnStr) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
56
|
+
return topFn(fnStr);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Compiles a per-cell function string from a `@ui.table.fn.*` annotation:
|
|
60
|
+
* `"(row, ctx) => row.status === 'paid' ? 'text-success' : ''"`
|
|
61
|
+
*
|
|
62
|
+
* The compiled function receives a {@link TTableFnScope} `{ row, ctx, t }` and
|
|
63
|
+
* calls the function as `(row, ctx, t)`.
|
|
64
|
+
*/
|
|
65
|
+
function compileTableFn(fnStr) {
|
|
66
|
+
return tableFn(fnStr);
|
|
41
67
|
}
|
|
42
68
|
/**
|
|
43
69
|
* Compiles a validator function string.
|
|
@@ -53,6 +79,48 @@ function compileValidatorFn(fnStr) {
|
|
|
53
79
|
/** Fn keys that store `[{ name, fn }]` arrays rather than a single fn string. */
|
|
54
80
|
const ATTR_FN_KEYS = new Set([UI_FORM_FN_ATTR, UI_TABLE_FN_ATTR]);
|
|
55
81
|
/**
|
|
82
|
+
* Fn keys whose result is a text shown to users: a string result that is a
|
|
83
|
+
* text the server translated (an exact match) is shown translated.
|
|
84
|
+
*/
|
|
85
|
+
const TEXT_FN_KEYS = new Set([
|
|
86
|
+
UI_FORM_FN_TITLE,
|
|
87
|
+
UI_FORM_FN_SUBMIT_TEXT,
|
|
88
|
+
UI_FORM_FN_LABEL,
|
|
89
|
+
UI_FORM_FN_DESCRIPTION,
|
|
90
|
+
UI_FORM_FN_HINT,
|
|
91
|
+
UI_FORM_FN_PLACEHOLDER
|
|
92
|
+
]);
|
|
93
|
+
/**
|
|
94
|
+
* Option labels through `t` — new option objects only for labels that change
|
|
95
|
+
* (the fn may return objects it shares, e.g. from `context`); the list itself
|
|
96
|
+
* when none does.
|
|
97
|
+
*/
|
|
98
|
+
function translateOptionLabels(options, t) {
|
|
99
|
+
let out;
|
|
100
|
+
for (let i = 0; i < options.length; i++) {
|
|
101
|
+
const option = options[i];
|
|
102
|
+
if (typeof option !== "object" || option === null || typeof option.label !== "string") continue;
|
|
103
|
+
const label = t(option.label);
|
|
104
|
+
if (label === option.label) continue;
|
|
105
|
+
out ??= options.slice();
|
|
106
|
+
out[i] = {
|
|
107
|
+
...option,
|
|
108
|
+
label
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
return out ?? options;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* A fn result in the app's language: a text result translated by exact
|
|
115
|
+
* match, the labels of computed options too (never their keys: a string
|
|
116
|
+
* option is its own key).
|
|
117
|
+
*/
|
|
118
|
+
function translateResult(fnKey, result, scope) {
|
|
119
|
+
if (TEXT_FN_KEYS.has(fnKey)) return typeof result === "string" ? scopeT(scope)(result) : result;
|
|
120
|
+
if (fnKey === UI_FORM_FN_OPTIONS && Array.isArray(result)) return translateOptionLabels(result, scopeT(scope));
|
|
121
|
+
return result;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
56
124
|
* Dynamic field resolver — extends static resolution with `new Function` compilation
|
|
57
125
|
* for `ui.fn.*` annotation keys.
|
|
58
126
|
*
|
|
@@ -61,7 +129,7 @@ const ATTR_FN_KEYS = new Set([UI_FORM_FN_ATTR, UI_TABLE_FN_ATTR]);
|
|
|
61
129
|
var DynamicFieldResolver = class {
|
|
62
130
|
resolveFieldProp(prop, fnKey, staticKey, scope, opts) {
|
|
63
131
|
if (ATTR_FN_KEYS.has(fnKey)) return this.resolveAttrFns(prop, fnKey, scope);
|
|
64
|
-
return resolveAnnotatedProp(prop.metadata, fnKey, staticKey, scope,
|
|
132
|
+
return resolveAnnotatedProp(prop.metadata, fnKey, staticKey, scope, compilerOf(fnKey), opts);
|
|
65
133
|
}
|
|
66
134
|
resolveFormProp(type, fnKey, staticKey, scope, opts) {
|
|
67
135
|
return resolveAnnotatedProp(type.metadata, fnKey, staticKey, scope, compileTopFn, opts);
|
|
@@ -78,17 +146,22 @@ var DynamicFieldResolver = class {
|
|
|
78
146
|
if (!fnAttrs) return void 0;
|
|
79
147
|
const result = {};
|
|
80
148
|
let hasAttrs = false;
|
|
149
|
+
const compile = compilerOf(fnKey);
|
|
81
150
|
for (const item of asArray(fnAttrs)) if (typeof item === "object" && item !== null && "name" in item && "fn" in item) {
|
|
82
151
|
const { name, fn } = item;
|
|
83
|
-
result[name] =
|
|
152
|
+
result[name] = compile(fn)(scope);
|
|
84
153
|
hasAttrs = true;
|
|
85
154
|
}
|
|
86
155
|
return hasAttrs ? result : void 0;
|
|
87
156
|
}
|
|
88
157
|
};
|
|
158
|
+
/** The compiler of a field-level fn key: per-cell `(row, ctx, t)` for `ui.table.fn.*`. */
|
|
159
|
+
function compilerOf(fnKey) {
|
|
160
|
+
return fnKey.startsWith(UI_TABLE_FN_PREFIX) ? compileTableFn : compileFieldFn;
|
|
161
|
+
}
|
|
89
162
|
function resolveAnnotatedProp(metadata, fnKey, staticKey, scope, compileFn, opts) {
|
|
90
163
|
const fnStr = metadata.get(fnKey);
|
|
91
|
-
if (typeof fnStr === "string") return compileFn(fnStr)(scope);
|
|
164
|
+
if (typeof fnStr === "string") return translateResult(fnKey, compileFn(fnStr)(scope), scope);
|
|
92
165
|
return resolveStatic(metadata, staticKey, opts);
|
|
93
166
|
}
|
|
94
167
|
/**
|
|
@@ -158,18 +231,22 @@ function uiFnsValidatorPlugin() {
|
|
|
158
231
|
const container = isContainerKind(def.type.kind);
|
|
159
232
|
if (container && containerShapeMismatch(def.type.kind, value)) return void 0;
|
|
160
233
|
const fnsCtx = ctx.context;
|
|
234
|
+
const data = fnsCtx?.data ?? {};
|
|
235
|
+
const context = fnsCtx?.context ?? {};
|
|
236
|
+
const t = fnsCtx?.t ?? getUiTexts().t;
|
|
161
237
|
const scope = buildFieldEntry(def, {
|
|
162
238
|
v: value,
|
|
163
|
-
data
|
|
164
|
-
context
|
|
165
|
-
entry: void 0
|
|
239
|
+
data,
|
|
240
|
+
context,
|
|
241
|
+
entry: void 0,
|
|
242
|
+
t
|
|
166
243
|
}, ctx.path);
|
|
167
244
|
const fns = asArray(hasValidators);
|
|
168
245
|
for (const fnStr of fns) {
|
|
169
246
|
if (typeof fnStr !== "string") continue;
|
|
170
247
|
const result = compileValidatorFn(fnStr)(scope);
|
|
171
248
|
if (result !== true) {
|
|
172
|
-
ctx.error(typeof result === "string" ? result : "
|
|
249
|
+
ctx.error(t(typeof result === "string" ? result : "UI_VALIDATION_FAILED"));
|
|
173
250
|
if (container) ctx.validateAnnotatedType(def, value);
|
|
174
251
|
return false;
|
|
175
252
|
}
|
|
@@ -193,4 +270,4 @@ function installDynamicResolver() {
|
|
|
193
270
|
setDefaultValidatorPlugins([uiFnsValidatorPlugin()]);
|
|
194
271
|
}
|
|
195
272
|
//#endregion
|
|
196
|
-
export { DynamicFieldResolver, buildFieldEntry, compileFieldFn, compileTopFn, compileValidatorFn, installDynamicResolver, uiFnsValidatorPlugin };
|
|
273
|
+
export { DynamicFieldResolver, buildFieldEntry, compileFieldFn, compileTableFn, compileTopFn, compileValidatorFn, installDynamicResolver, uiFnsValidatorPlugin };
|
package/dist/plugin.cjs
CHANGED
|
@@ -6,7 +6,7 @@ let _atscript_core = require("@atscript/core");
|
|
|
6
6
|
*/
|
|
7
7
|
function validateFnString(fnStr, range) {
|
|
8
8
|
try {
|
|
9
|
-
new Function("v", "data", "context", "entry", `return (${fnStr})(v, data, context, entry)`);
|
|
9
|
+
new Function("v", "data", "context", "entry", "t", `return (${fnStr})(v, data, context, entry, t)`);
|
|
10
10
|
} catch (error) {
|
|
11
11
|
return [{
|
|
12
12
|
severity: 1,
|
|
@@ -18,11 +18,11 @@ function validateFnString(fnStr, range) {
|
|
|
18
18
|
const FN_MODES = {
|
|
19
19
|
field: {
|
|
20
20
|
nodeType: ["prop", "type"],
|
|
21
|
-
argDoc: "(value, data, context, entry) => result"
|
|
21
|
+
argDoc: "(value, data, context, entry, t) => result"
|
|
22
22
|
},
|
|
23
23
|
top: {
|
|
24
24
|
nodeType: ["interface", "type"],
|
|
25
|
-
argDoc: "(data, context) => result"
|
|
25
|
+
argDoc: "(data, context, t) => result"
|
|
26
26
|
},
|
|
27
27
|
both: {
|
|
28
28
|
nodeType: [
|
|
@@ -30,17 +30,22 @@ const FN_MODES = {
|
|
|
30
30
|
"interface",
|
|
31
31
|
"type"
|
|
32
32
|
],
|
|
33
|
-
argDoc: "(value, data, context, entry) => result on fields; (data, context) => result on the root interface"
|
|
33
|
+
argDoc: "(value, data, context, entry, t) => result on fields; (data, context, t) => result on the root interface"
|
|
34
34
|
}
|
|
35
35
|
};
|
|
36
|
-
|
|
36
|
+
/**
|
|
37
|
+
* `text`: the fn's result is shown to users (a label, a message) — its string
|
|
38
|
+
* literals are user-facing text a server translates and serves next to the type.
|
|
39
|
+
*/
|
|
40
|
+
function makeFnAnnotation(description, mode, text) {
|
|
37
41
|
return new _atscript_core.AnnotationSpec({
|
|
38
42
|
description,
|
|
39
43
|
nodeType: [...FN_MODES[mode].nodeType],
|
|
40
44
|
argument: {
|
|
41
45
|
name: "fn",
|
|
42
46
|
type: "string",
|
|
43
|
-
description: `JS function string: ${FN_MODES[mode].argDoc}
|
|
47
|
+
description: `JS function string: ${FN_MODES[mode].argDoc}`,
|
|
48
|
+
...text ? { userText: "expression" } : {}
|
|
44
49
|
},
|
|
45
50
|
validate: validateFirstArg
|
|
46
51
|
});
|
|
@@ -49,10 +54,10 @@ function makeFnAnnotation(description, mode) {
|
|
|
49
54
|
function validateFirstArg(_token, args) {
|
|
50
55
|
if (args[0]) return validateFnString(args[0].text, args[0].range);
|
|
51
56
|
}
|
|
52
|
-
const fnAnnotation = (description) => makeFnAnnotation(description, "field");
|
|
53
|
-
const fnTopAnnotation = (description) => makeFnAnnotation(description, "top");
|
|
54
|
-
const fnBothAnnotation = (description) => makeFnAnnotation(description, "both");
|
|
55
|
-
const TABLE_ROW_SCOPE_DOC = "
|
|
57
|
+
const fnAnnotation = (description, text) => makeFnAnnotation(description, "field", text);
|
|
58
|
+
const fnTopAnnotation = (description, text) => makeFnAnnotation(description, "top", text);
|
|
59
|
+
const fnBothAnnotation = (description, text) => makeFnAnnotation(description, "both", text);
|
|
60
|
+
const TABLE_ROW_SCOPE_DOC = "Called as `(row, ctx, t)`: `row` is the current row's data object, `ctx` carries table-level context (minimum keys: `searchTerm`, `filters`, `sorters`, `rowIndex`), `t(text, params?)` translates a text. Per-row+cell scope only — every expression must be meaningful when applied to a single cell.";
|
|
56
61
|
function tableFnAnnotation(description) {
|
|
57
62
|
return new _atscript_core.AnnotationSpec({
|
|
58
63
|
description: `${description}\n\n${TABLE_ROW_SCOPE_DOC}`,
|
|
@@ -60,7 +65,7 @@ function tableFnAnnotation(description) {
|
|
|
60
65
|
argument: {
|
|
61
66
|
name: "fn",
|
|
62
67
|
type: "string",
|
|
63
|
-
description: "JS function string
|
|
68
|
+
description: "JS function string: (row, ctx, t) => result"
|
|
64
69
|
},
|
|
65
70
|
validate: validateFirstArg
|
|
66
71
|
});
|
|
@@ -77,7 +82,8 @@ const fnAttrSpec = new _atscript_core.AnnotationSpec({
|
|
|
77
82
|
}, {
|
|
78
83
|
name: "fn",
|
|
79
84
|
type: "string",
|
|
80
|
-
description: "JS function string: (value, data, context, entry) => any"
|
|
85
|
+
description: "JS function string: (value, data, context, entry, t) => any",
|
|
86
|
+
userText: "expression"
|
|
81
87
|
}],
|
|
82
88
|
validate(_token, args) {
|
|
83
89
|
if (args[1]) return validateFnString(args[1].text, args[1].range);
|
|
@@ -95,7 +101,8 @@ const tableFnAttrSpec = new _atscript_core.AnnotationSpec({
|
|
|
95
101
|
}, {
|
|
96
102
|
name: "fn",
|
|
97
103
|
type: "string",
|
|
98
|
-
description: "JS function string
|
|
104
|
+
description: "JS function string: (row, ctx, t) => result",
|
|
105
|
+
userText: "expression"
|
|
99
106
|
}],
|
|
100
107
|
validate(_token, args) {
|
|
101
108
|
if (args[1]) return validateFnString(args[1].text, args[1].range);
|
|
@@ -121,27 +128,28 @@ const uiFnsAnnotations = { ui: {
|
|
|
121
128
|
argument: {
|
|
122
129
|
name: "fn",
|
|
123
130
|
type: "string",
|
|
124
|
-
description: "JS function string: (value, data, context, entry) => boolean | string"
|
|
131
|
+
description: "JS function string: (value, data, context, entry, t) => boolean | string",
|
|
132
|
+
userText: "expression"
|
|
125
133
|
},
|
|
126
134
|
validate: validateFirstArg
|
|
127
135
|
}),
|
|
128
136
|
fn: {
|
|
129
|
-
title: fnTopAnnotation("Computed form title: (data, context) => string"),
|
|
137
|
+
title: fnTopAnnotation("Computed form title: (data, context) => string", true),
|
|
130
138
|
submit: {
|
|
131
|
-
text: fnTopAnnotation("Computed submit button text: (data, context) => string"),
|
|
139
|
+
text: fnTopAnnotation("Computed submit button text: (data, context) => string", true),
|
|
132
140
|
disabled: fnTopAnnotation("Computed submit disabled state: (data, context) => boolean")
|
|
133
141
|
},
|
|
134
|
-
label: fnAnnotation("Computed label: (value, data, context, entry) => string"),
|
|
135
|
-
description: fnBothAnnotation("Computed description: (value, data, context, entry) => string on fields; (data, context) => string on the root interface"),
|
|
136
|
-
hint: fnAnnotation("Computed hint: (value, data, context, entry) => string"),
|
|
137
|
-
placeholder: fnAnnotation("Computed placeholder: (value, data, context, entry) => string"),
|
|
142
|
+
label: fnAnnotation("Computed label: (value, data, context, entry) => string", true),
|
|
143
|
+
description: fnBothAnnotation("Computed description: (value, data, context, entry) => string on fields; (data, context) => string on the root interface", true),
|
|
144
|
+
hint: fnAnnotation("Computed hint: (value, data, context, entry) => string", true),
|
|
145
|
+
placeholder: fnAnnotation("Computed placeholder: (value, data, context, entry) => string", true),
|
|
138
146
|
disabled: fnAnnotation("Computed disabled state: (value, data, context, entry) => boolean"),
|
|
139
147
|
hidden: fnAnnotation("Computed hidden state: (value, data, context, entry) => boolean"),
|
|
140
148
|
readonly: fnAnnotation("Computed readonly state: (value, data, context, entry) => boolean"),
|
|
141
|
-
value: fnAnnotation("Computed
|
|
149
|
+
value: fnAnnotation("Computed value: (value, data, context, entry) => any — the field's default (kept in sync while readonly); the text a display-only field (`ui.paragraph`) shows", true),
|
|
142
150
|
classes: fnAnnotation("Computed CSS classes: (value, data, context, entry) => string | Record<string, boolean>"),
|
|
143
151
|
styles: fnAnnotation("Computed inline styles: (value, data, context, entry) => string | Record<string, string>"),
|
|
144
|
-
options: fnAnnotation("Computed select/radio options: (value, data, context, entry) => Array"),
|
|
152
|
+
options: fnAnnotation("Computed select/radio options: (value, data, context, entry) => Array", true),
|
|
145
153
|
attr: fnAttrSpec
|
|
146
154
|
}
|
|
147
155
|
},
|
package/dist/plugin.mjs
CHANGED
|
@@ -6,7 +6,7 @@ import { AnnotationSpec } from "@atscript/core";
|
|
|
6
6
|
*/
|
|
7
7
|
function validateFnString(fnStr, range) {
|
|
8
8
|
try {
|
|
9
|
-
new Function("v", "data", "context", "entry", `return (${fnStr})(v, data, context, entry)`);
|
|
9
|
+
new Function("v", "data", "context", "entry", "t", `return (${fnStr})(v, data, context, entry, t)`);
|
|
10
10
|
} catch (error) {
|
|
11
11
|
return [{
|
|
12
12
|
severity: 1,
|
|
@@ -18,11 +18,11 @@ function validateFnString(fnStr, range) {
|
|
|
18
18
|
const FN_MODES = {
|
|
19
19
|
field: {
|
|
20
20
|
nodeType: ["prop", "type"],
|
|
21
|
-
argDoc: "(value, data, context, entry) => result"
|
|
21
|
+
argDoc: "(value, data, context, entry, t) => result"
|
|
22
22
|
},
|
|
23
23
|
top: {
|
|
24
24
|
nodeType: ["interface", "type"],
|
|
25
|
-
argDoc: "(data, context) => result"
|
|
25
|
+
argDoc: "(data, context, t) => result"
|
|
26
26
|
},
|
|
27
27
|
both: {
|
|
28
28
|
nodeType: [
|
|
@@ -30,17 +30,22 @@ const FN_MODES = {
|
|
|
30
30
|
"interface",
|
|
31
31
|
"type"
|
|
32
32
|
],
|
|
33
|
-
argDoc: "(value, data, context, entry) => result on fields; (data, context) => result on the root interface"
|
|
33
|
+
argDoc: "(value, data, context, entry, t) => result on fields; (data, context, t) => result on the root interface"
|
|
34
34
|
}
|
|
35
35
|
};
|
|
36
|
-
|
|
36
|
+
/**
|
|
37
|
+
* `text`: the fn's result is shown to users (a label, a message) — its string
|
|
38
|
+
* literals are user-facing text a server translates and serves next to the type.
|
|
39
|
+
*/
|
|
40
|
+
function makeFnAnnotation(description, mode, text) {
|
|
37
41
|
return new AnnotationSpec({
|
|
38
42
|
description,
|
|
39
43
|
nodeType: [...FN_MODES[mode].nodeType],
|
|
40
44
|
argument: {
|
|
41
45
|
name: "fn",
|
|
42
46
|
type: "string",
|
|
43
|
-
description: `JS function string: ${FN_MODES[mode].argDoc}
|
|
47
|
+
description: `JS function string: ${FN_MODES[mode].argDoc}`,
|
|
48
|
+
...text ? { userText: "expression" } : {}
|
|
44
49
|
},
|
|
45
50
|
validate: validateFirstArg
|
|
46
51
|
});
|
|
@@ -49,10 +54,10 @@ function makeFnAnnotation(description, mode) {
|
|
|
49
54
|
function validateFirstArg(_token, args) {
|
|
50
55
|
if (args[0]) return validateFnString(args[0].text, args[0].range);
|
|
51
56
|
}
|
|
52
|
-
const fnAnnotation = (description) => makeFnAnnotation(description, "field");
|
|
53
|
-
const fnTopAnnotation = (description) => makeFnAnnotation(description, "top");
|
|
54
|
-
const fnBothAnnotation = (description) => makeFnAnnotation(description, "both");
|
|
55
|
-
const TABLE_ROW_SCOPE_DOC = "
|
|
57
|
+
const fnAnnotation = (description, text) => makeFnAnnotation(description, "field", text);
|
|
58
|
+
const fnTopAnnotation = (description, text) => makeFnAnnotation(description, "top", text);
|
|
59
|
+
const fnBothAnnotation = (description, text) => makeFnAnnotation(description, "both", text);
|
|
60
|
+
const TABLE_ROW_SCOPE_DOC = "Called as `(row, ctx, t)`: `row` is the current row's data object, `ctx` carries table-level context (minimum keys: `searchTerm`, `filters`, `sorters`, `rowIndex`), `t(text, params?)` translates a text. Per-row+cell scope only — every expression must be meaningful when applied to a single cell.";
|
|
56
61
|
function tableFnAnnotation(description) {
|
|
57
62
|
return new AnnotationSpec({
|
|
58
63
|
description: `${description}\n\n${TABLE_ROW_SCOPE_DOC}`,
|
|
@@ -60,7 +65,7 @@ function tableFnAnnotation(description) {
|
|
|
60
65
|
argument: {
|
|
61
66
|
name: "fn",
|
|
62
67
|
type: "string",
|
|
63
|
-
description: "JS function string
|
|
68
|
+
description: "JS function string: (row, ctx, t) => result"
|
|
64
69
|
},
|
|
65
70
|
validate: validateFirstArg
|
|
66
71
|
});
|
|
@@ -77,7 +82,8 @@ const fnAttrSpec = new AnnotationSpec({
|
|
|
77
82
|
}, {
|
|
78
83
|
name: "fn",
|
|
79
84
|
type: "string",
|
|
80
|
-
description: "JS function string: (value, data, context, entry) => any"
|
|
85
|
+
description: "JS function string: (value, data, context, entry, t) => any",
|
|
86
|
+
userText: "expression"
|
|
81
87
|
}],
|
|
82
88
|
validate(_token, args) {
|
|
83
89
|
if (args[1]) return validateFnString(args[1].text, args[1].range);
|
|
@@ -95,7 +101,8 @@ const tableFnAttrSpec = new AnnotationSpec({
|
|
|
95
101
|
}, {
|
|
96
102
|
name: "fn",
|
|
97
103
|
type: "string",
|
|
98
|
-
description: "JS function string
|
|
104
|
+
description: "JS function string: (row, ctx, t) => result",
|
|
105
|
+
userText: "expression"
|
|
99
106
|
}],
|
|
100
107
|
validate(_token, args) {
|
|
101
108
|
if (args[1]) return validateFnString(args[1].text, args[1].range);
|
|
@@ -121,27 +128,28 @@ const uiFnsAnnotations = { ui: {
|
|
|
121
128
|
argument: {
|
|
122
129
|
name: "fn",
|
|
123
130
|
type: "string",
|
|
124
|
-
description: "JS function string: (value, data, context, entry) => boolean | string"
|
|
131
|
+
description: "JS function string: (value, data, context, entry, t) => boolean | string",
|
|
132
|
+
userText: "expression"
|
|
125
133
|
},
|
|
126
134
|
validate: validateFirstArg
|
|
127
135
|
}),
|
|
128
136
|
fn: {
|
|
129
|
-
title: fnTopAnnotation("Computed form title: (data, context) => string"),
|
|
137
|
+
title: fnTopAnnotation("Computed form title: (data, context) => string", true),
|
|
130
138
|
submit: {
|
|
131
|
-
text: fnTopAnnotation("Computed submit button text: (data, context) => string"),
|
|
139
|
+
text: fnTopAnnotation("Computed submit button text: (data, context) => string", true),
|
|
132
140
|
disabled: fnTopAnnotation("Computed submit disabled state: (data, context) => boolean")
|
|
133
141
|
},
|
|
134
|
-
label: fnAnnotation("Computed label: (value, data, context, entry) => string"),
|
|
135
|
-
description: fnBothAnnotation("Computed description: (value, data, context, entry) => string on fields; (data, context) => string on the root interface"),
|
|
136
|
-
hint: fnAnnotation("Computed hint: (value, data, context, entry) => string"),
|
|
137
|
-
placeholder: fnAnnotation("Computed placeholder: (value, data, context, entry) => string"),
|
|
142
|
+
label: fnAnnotation("Computed label: (value, data, context, entry) => string", true),
|
|
143
|
+
description: fnBothAnnotation("Computed description: (value, data, context, entry) => string on fields; (data, context) => string on the root interface", true),
|
|
144
|
+
hint: fnAnnotation("Computed hint: (value, data, context, entry) => string", true),
|
|
145
|
+
placeholder: fnAnnotation("Computed placeholder: (value, data, context, entry) => string", true),
|
|
138
146
|
disabled: fnAnnotation("Computed disabled state: (value, data, context, entry) => boolean"),
|
|
139
147
|
hidden: fnAnnotation("Computed hidden state: (value, data, context, entry) => boolean"),
|
|
140
148
|
readonly: fnAnnotation("Computed readonly state: (value, data, context, entry) => boolean"),
|
|
141
|
-
value: fnAnnotation("Computed
|
|
149
|
+
value: fnAnnotation("Computed value: (value, data, context, entry) => any — the field's default (kept in sync while readonly); the text a display-only field (`ui.paragraph`) shows", true),
|
|
142
150
|
classes: fnAnnotation("Computed CSS classes: (value, data, context, entry) => string | Record<string, boolean>"),
|
|
143
151
|
styles: fnAnnotation("Computed inline styles: (value, data, context, entry) => string | Record<string, string>"),
|
|
144
|
-
options: fnAnnotation("Computed select/radio options: (value, data, context, entry) => Array"),
|
|
152
|
+
options: fnAnnotation("Computed select/radio options: (value, data, context, entry) => Array", true),
|
|
145
153
|
attr: fnAttrSpec
|
|
146
154
|
}
|
|
147
155
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atscript/ui-fns",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.166",
|
|
4
4
|
"description": "Dynamic fn-compiled field properties for @atscript/ui (opt-in, uses new Function)",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"annotations",
|
|
@@ -48,16 +48,16 @@
|
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
50
|
"@prostojs/deserialize-fn": "^0.0.6",
|
|
51
|
-
"@atscript/ui": "^0.1.
|
|
51
|
+
"@atscript/ui": "^0.1.166"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
|
-
"@atscript/core": "^0.2.
|
|
55
|
-
"@atscript/typescript": "^0.2.
|
|
54
|
+
"@atscript/core": "^0.2.7",
|
|
55
|
+
"@atscript/typescript": "^0.2.7",
|
|
56
56
|
"vitest": "npm:@voidzero-dev/vite-plus-test@0.1.14"
|
|
57
57
|
},
|
|
58
58
|
"peerDependencies": {
|
|
59
|
-
"@atscript/core": "^0.2.
|
|
60
|
-
"@atscript/typescript": "^0.2.
|
|
59
|
+
"@atscript/core": "^0.2.7",
|
|
60
|
+
"@atscript/typescript": "^0.2.7"
|
|
61
61
|
},
|
|
62
62
|
"scripts": {
|
|
63
63
|
"build": "vp pack",
|