@atscript/ui 0.1.204 → 0.1.206
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/form/create-form-def.cjs +72 -83
- package/dist/form/create-form-def.d.cts +32 -22
- package/dist/form/create-form-def.d.mts +32 -22
- package/dist/form/create-form-def.mjs +73 -84
- package/dist/form/db-column.cjs +37 -0
- package/dist/form/db-column.mjs +35 -0
- package/dist/form/decimal-format.cjs +9 -11
- package/dist/form/decimal-format.mjs +9 -11
- package/dist/form/diff.cjs +32 -14
- package/dist/form/diff.d.cts +7 -4
- package/dist/form/diff.d.mts +7 -4
- package/dist/form/diff.mjs +32 -14
- package/dist/form/loaded-empty.cjs +56 -0
- package/dist/form/loaded-empty.d.cts +25 -0
- package/dist/form/loaded-empty.d.mts +25 -0
- package/dist/form/loaded-empty.mjs +56 -0
- package/dist/form/omit-derived.cjs +38 -0
- package/dist/form/omit-derived.d.cts +17 -0
- package/dist/form/omit-derived.d.mts +17 -0
- package/dist/form/omit-derived.mjs +38 -0
- package/dist/form/optional-mode.cjs +85 -0
- package/dist/form/optional-mode.d.cts +58 -0
- package/dist/form/optional-mode.d.mts +58 -0
- package/dist/form/optional-mode.mjs +82 -0
- package/dist/form/path-utils.cjs +14 -10
- package/dist/form/path-utils.mjs +14 -10
- package/dist/form/types.d.cts +16 -16
- package/dist/form/types.d.mts +16 -16
- package/dist/form/validate.cjs +2 -2
- package/dist/form/validate.mjs +2 -2
- package/dist/index.cjs +23 -4
- package/dist/index.d.cts +7 -3
- package/dist/index.d.mts +7 -3
- package/dist/index.mjs +10 -6
- package/dist/plugin/annotations.cjs +12 -0
- package/dist/plugin/annotations.mjs +13 -1
- package/dist/plugin/value-help-validation.cjs +25 -1
- package/dist/plugin/value-help-validation.mjs +25 -2
- package/dist/shared/annotation-keys.cjs +3 -0
- package/dist/shared/annotation-keys.d.cts +3 -1
- package/dist/shared/annotation-keys.d.mts +3 -1
- package/dist/shared/annotation-keys.mjs +3 -1
- package/dist/shared/exact-number.cjs +12 -0
- package/dist/shared/exact-number.mjs +12 -0
- package/dist/shared/meta-cache.cjs +1 -1
- package/dist/shared/meta-cache.mjs +1 -1
- package/dist/table/create-table-def.cjs +4 -1
- package/dist/table/create-table-def.mjs +4 -2
- package/dist/table/types.d.cts +23 -16
- package/dist/table/types.d.mts +23 -16
- package/dist/table/value-kind.cjs +37 -2
- package/dist/table/value-kind.d.cts +22 -2
- package/dist/table/value-kind.d.mts +22 -2
- package/dist/table/value-kind.mjs +36 -3
- package/dist/value-help/distinct-client.cjs +5 -4
- package/dist/value-help/distinct-client.d.cts +8 -2
- package/dist/value-help/distinct-client.d.mts +8 -2
- package/dist/value-help/distinct-client.mjs +5 -4
- package/dist/value-help/value-help-client.cjs +45 -9
- package/dist/value-help/value-help-client.mjs +45 -9
- package/package.json +11 -12
|
@@ -2,8 +2,9 @@ import { META_DESCRIPTION, META_LABEL, UI_FORM_COMPONENT, UI_FORM_PUSH_DOWN, UI_
|
|
|
2
2
|
import { isServedType } from "../i18n/model-texts.mjs";
|
|
3
3
|
import { getFieldMeta, hasComputedAnnotations } from "../shared/field-resolver.mjs";
|
|
4
4
|
import { AS_UI_MESSAGES } from "../messages.mjs";
|
|
5
|
-
import {
|
|
5
|
+
import { isPureLiteralUnion } from "../value-help/extract-literals.mjs";
|
|
6
6
|
import { extractValueHelp, isValueHelpPicker } from "../value-help/extract-ref.mjs";
|
|
7
|
+
import { columnHas, isDbDefaulted, soleValueMember } from "./db-column.mjs";
|
|
7
8
|
import { isSkippedNavField } from "./skipped-nav.mjs";
|
|
8
9
|
import { flattenAnnotatedType } from "@atscript/typescript/utils";
|
|
9
10
|
import { writableViewReadOnlyFields } from "@atscript/db/meta";
|
|
@@ -24,7 +25,7 @@ const UI_TAGS = new Set([
|
|
|
24
25
|
* an endless expansion: a tree's `children` items are the same def at every
|
|
25
26
|
* depth, built once. A field def is reused only at the same path relative to
|
|
26
27
|
* its object def, so a reused field always sits where it was built. Both are
|
|
27
|
-
* per
|
|
28
|
+
* per {@link TWrite}, which shapes the defs.
|
|
28
29
|
*/
|
|
29
30
|
const defsInProgress = [];
|
|
30
31
|
const fieldsInProgress = [];
|
|
@@ -51,13 +52,14 @@ function unnamed(field) {
|
|
|
51
52
|
*
|
|
52
53
|
* A server-computed field — `@db.column.derived`, a computed view column
|
|
53
54
|
* (`@db.compute`) or a host-computed one (`@db.compute.host`) — is marked
|
|
54
|
-
* `derived` straight from its metadata; a served field
|
|
55
|
-
* `opts.metaFields`; an update-stamped field (see
|
|
56
|
-
* when `opts.mode` is `"edit"`. Over a compiled
|
|
57
|
-
* a field the view declares itself (a join
|
|
58
|
-
* `derived` as well — its controller drops it
|
|
59
|
-
*
|
|
60
|
-
* `@ui.form.fn.
|
|
55
|
+
* `derived` straight from its metadata; a served field `/meta` marks
|
|
56
|
+
* `readOnly`, from `opts.metaFields`; an update-stamped field (see
|
|
57
|
+
* `CreateFormDefOptions.mode`), when `opts.mode` is `"edit"`. Over a compiled
|
|
58
|
+
* `@db.view.writable` view type, a field the view declares itself (a join
|
|
59
|
+
* column, a computed field) is `derived` as well — its controller drops it
|
|
60
|
+
* from every write. A `readOnly` or own field the form writes stays an
|
|
61
|
+
* ordinary one (a pick's fill target, a `@ui.form.fn.value` or
|
|
62
|
+
* `@ui.form.fn.readonly` field). Since 0.1.204; `readOnly` since 0.1.205.
|
|
61
63
|
*
|
|
62
64
|
* A navigation field (`@db.rel.from` / `.to` / `.via` / `.store`) is left out unless it
|
|
63
65
|
* carries a `@ui.form.*` annotation of its own (see {@link isSkippedNavField}).
|
|
@@ -68,17 +70,19 @@ function unnamed(field) {
|
|
|
68
70
|
* becomes a nested object field, since inlining it would never end.
|
|
69
71
|
*/
|
|
70
72
|
function createFormDef(type, opts) {
|
|
71
|
-
const
|
|
73
|
+
const write = opts?.mode === "edit" ? "edit" : "create";
|
|
74
|
+
const def = buildFormDef(type, write, opts ?? {});
|
|
72
75
|
if (isServedType(type)) def.served = true;
|
|
76
|
+
if (write === "edit") def.edit = true;
|
|
73
77
|
return def;
|
|
74
78
|
}
|
|
75
79
|
/**
|
|
76
80
|
* The def of an object (or a leaf) type. `root`: the caller's options, for the
|
|
77
81
|
* form's own type only — nested objects get none of the root-table ones.
|
|
78
82
|
*/
|
|
79
|
-
function buildFormDef(type,
|
|
83
|
+
function buildFormDef(type, write, root) {
|
|
80
84
|
if (type.type.kind !== "object") {
|
|
81
|
-
const rootField = createFieldDef("", type,
|
|
85
|
+
const rootField = createFieldDef("", type, write);
|
|
82
86
|
const fields = [rootField];
|
|
83
87
|
return {
|
|
84
88
|
type,
|
|
@@ -90,14 +94,14 @@ function buildFormDef(type, edit, root) {
|
|
|
90
94
|
};
|
|
91
95
|
}
|
|
92
96
|
if (!root) {
|
|
93
|
-
const reached = defsInProgress.find((e) => e.type === type && e.
|
|
97
|
+
const reached = defsInProgress.find((e) => e.type === type && e.write === write);
|
|
94
98
|
if (reached) return reached.def;
|
|
95
99
|
}
|
|
96
100
|
const objectType = type;
|
|
97
101
|
const flatMap = flattenAnnotatedType(objectType, { excludePhantomTypes: false });
|
|
98
102
|
const opts = root && {
|
|
99
103
|
...root,
|
|
100
|
-
versionColumn: root.versionColumn ?? (edit ? findVersionColumn(flatMap) : void 0)
|
|
104
|
+
versionColumn: root.versionColumn ?? (write === "edit" ? findVersionColumn(flatMap) : void 0)
|
|
101
105
|
};
|
|
102
106
|
const fields = [];
|
|
103
107
|
const rootField = {
|
|
@@ -121,11 +125,11 @@ function buildFormDef(type, edit, root) {
|
|
|
121
125
|
let collected;
|
|
122
126
|
if (!root) defsInProgress.push({
|
|
123
127
|
type,
|
|
124
|
-
|
|
128
|
+
write,
|
|
125
129
|
def
|
|
126
130
|
});
|
|
127
131
|
try {
|
|
128
|
-
collected = collectFields(objectType, flatMap,
|
|
132
|
+
collected = collectFields(objectType, flatMap, write, opts);
|
|
129
133
|
} finally {
|
|
130
134
|
if (!root) defsInProgress.pop();
|
|
131
135
|
}
|
|
@@ -138,7 +142,7 @@ function buildFormDef(type, edit, root) {
|
|
|
138
142
|
return def;
|
|
139
143
|
}
|
|
140
144
|
/** Builds the top-level fields of an object def from its flattened props, unsorted. */
|
|
141
|
-
function collectFields(objectType, flatMap,
|
|
145
|
+
function collectFields(objectType, flatMap, write, opts) {
|
|
142
146
|
const fields = [];
|
|
143
147
|
const structuredPrefixes = /* @__PURE__ */ new Set();
|
|
144
148
|
for (const [path, prop] of flatMap.entries()) {
|
|
@@ -156,7 +160,7 @@ function collectFields(objectType, flatMap, edit, opts) {
|
|
|
156
160
|
if (originalProp.type.of.type.kind === "array" && !getFieldMeta(originalProp, "ui.form.component")) continue;
|
|
157
161
|
}
|
|
158
162
|
if (kind === "array" || kind === "object" || kind === "union" || kind === "tuple") structuredPrefixes.add(path + ".");
|
|
159
|
-
const field = createFieldDef(path, originalProp,
|
|
163
|
+
const field = createFieldDef(path, originalProp, write);
|
|
160
164
|
if (isValueHelpPicker(originalProp)) field.picker = true;
|
|
161
165
|
fields.push({
|
|
162
166
|
field,
|
|
@@ -170,16 +174,12 @@ function collectFields(objectType, flatMap, edit, opts) {
|
|
|
170
174
|
}
|
|
171
175
|
/**
|
|
172
176
|
* Marks `derived` the fields the server owns although their own metadata does
|
|
173
|
-
* not say so
|
|
174
|
-
*
|
|
175
|
-
* (`writableViewReadOnlyFields`, with
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
*
|
|
179
|
-
* ambiguous served marker. Root def only: only the root form is written
|
|
180
|
-
* through the view's controller, and `metaFields` keys are root paths. The
|
|
181
|
-
* def gets a copy: a recursive type's field defs may be shared with nested
|
|
182
|
-
* defs.
|
|
177
|
+
* not say so — unless the form writes them ({@link writtenByForm}): a field
|
|
178
|
+
* `/meta` marks `readOnly` (`metaFields`; see {@link servedReadOnly}), and a field a compiled
|
|
179
|
+
* `@db.view.writable` view declares itself (`writableViewReadOnlyFields`, with
|
|
180
|
+
* the leaves of one rendered inline). Root def only: only the root form is
|
|
181
|
+
* written through the endpoint, and `metaFields` keys are root paths. The def
|
|
182
|
+
* gets a copy: a recursive type's field defs may be shared with nested defs.
|
|
183
183
|
*/
|
|
184
184
|
function markServerOwned(fields, type, metaFields) {
|
|
185
185
|
const own = writableViewReadOnlyFields(type);
|
|
@@ -190,28 +190,33 @@ function markServerOwned(fields, type, metaFields) {
|
|
|
190
190
|
for (const f of field.valueHelpInfo?.fill ?? []) filled.add(parent + f.local);
|
|
191
191
|
}
|
|
192
192
|
for (const c of fields) {
|
|
193
|
-
const { path,
|
|
194
|
-
if (derived) continue;
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
...c.field,
|
|
198
|
-
derived: true
|
|
199
|
-
};
|
|
200
|
-
continue;
|
|
201
|
-
}
|
|
202
|
-
if (writtenByForm(c.field, filled)) continue;
|
|
203
|
-
if (own.has(path.split(".", 1)[0])) c.field = {
|
|
193
|
+
const { path, derived } = c.field;
|
|
194
|
+
if (derived || writtenByForm(c.field, filled)) continue;
|
|
195
|
+
const head = path.split(".", 1)[0];
|
|
196
|
+
if (own.has(head) || metaFields && (servedReadOnly(metaFields, path) || head !== path && servedReadOnly(metaFields, head))) c.field = {
|
|
204
197
|
...c.field,
|
|
205
198
|
derived: true
|
|
206
199
|
};
|
|
207
|
-
else if (metaFields && !path.includes(".") && getFieldMeta(prop, "meta.readonly") !== void 0) c.field = {
|
|
208
|
-
...c.field,
|
|
209
|
-
derived: true,
|
|
210
|
-
servedReadonly: true
|
|
211
|
-
};
|
|
212
200
|
}
|
|
213
201
|
}
|
|
214
202
|
/**
|
|
203
|
+
* Whether `/meta` marks `path` read-only: its own entry, or — an object field
|
|
204
|
+
* (`/meta` lists the leaves only) — every leaf under it. Every leaf, not one: a
|
|
205
|
+
* permission layer may mark a single leaf read-only.
|
|
206
|
+
*/
|
|
207
|
+
function servedReadOnly(metaFields, path) {
|
|
208
|
+
const entry = metaFields[path];
|
|
209
|
+
if (entry) return entry.readOnly === true;
|
|
210
|
+
const prefix = `${path}.`;
|
|
211
|
+
let leaves = 0;
|
|
212
|
+
for (const key in metaFields) {
|
|
213
|
+
if (!key.startsWith(prefix)) continue;
|
|
214
|
+
if (!metaFields[key]?.readOnly) return false;
|
|
215
|
+
leaves++;
|
|
216
|
+
}
|
|
217
|
+
return leaves > 0;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
215
220
|
* A server-owned field the form writes all the same: a pick fills it, a
|
|
216
221
|
* `@ui.form.fn.value` computes it, or a `@ui.form.fn.readonly` can make it
|
|
217
222
|
* editable.
|
|
@@ -261,36 +266,37 @@ function keepWritableFills(fields) {
|
|
|
261
266
|
}
|
|
262
267
|
}
|
|
263
268
|
/**
|
|
264
|
-
* Creates a FormFieldDef from any ATScript annotated type. `
|
|
265
|
-
*
|
|
269
|
+
* Creates a FormFieldDef from any ATScript annotated type. `write`: how the
|
|
270
|
+
* server writes it ({@link TWrite}, from `CreateFormDefOptions.mode`).
|
|
266
271
|
*
|
|
267
272
|
* An array, union or tuple reached again at the same path inside its own
|
|
268
273
|
* build (an array item, a union variant or a tuple item of a recursive alias)
|
|
269
274
|
* gets the field def under construction. Every other recursion passes an
|
|
270
275
|
* object def, which `defsInProgress` catches, so the build always ends.
|
|
271
276
|
*/
|
|
272
|
-
function createFieldDef(path, prop,
|
|
277
|
+
function createFieldDef(path, prop, write) {
|
|
273
278
|
const kind = prop.type.kind;
|
|
274
|
-
if (kind === "" || kind === "object") return buildFieldDef(path, prop,
|
|
275
|
-
const reached = fieldsInProgress.find((e) => e.prop === prop && e.path === path && e.
|
|
279
|
+
if (kind === "" || kind === "object") return buildFieldDef(path, prop, write);
|
|
280
|
+
const reached = fieldsInProgress.find((e) => e.prop === prop && e.path === path && e.write === write);
|
|
276
281
|
if (reached) return reached.field;
|
|
277
282
|
const building = {
|
|
278
283
|
prop,
|
|
279
284
|
path,
|
|
280
|
-
|
|
285
|
+
write,
|
|
281
286
|
field: {},
|
|
282
287
|
copies: []
|
|
283
288
|
};
|
|
284
289
|
fieldsInProgress.push(building);
|
|
285
290
|
try {
|
|
286
|
-
const field = Object.assign(building.field, buildFieldDef(path, prop,
|
|
291
|
+
const field = Object.assign(building.field, buildFieldDef(path, prop, write));
|
|
287
292
|
for (const copy of building.copies) Object.assign(copy, field, { name: "" });
|
|
288
293
|
return field;
|
|
289
294
|
} finally {
|
|
290
295
|
fieldsInProgress.pop();
|
|
291
296
|
}
|
|
292
297
|
}
|
|
293
|
-
function buildFieldDef(path,
|
|
298
|
+
function buildFieldDef(path, typeProp, write) {
|
|
299
|
+
const prop = write === "create" && path !== "" && !typeProp.optional && isDbDefaulted(typeProp) ? Object.create(typeProp, { optional: { value: true } }) : typeProp;
|
|
294
300
|
const kind = prop.type.kind;
|
|
295
301
|
const name = path.slice(path.lastIndexOf(".") + 1);
|
|
296
302
|
const allStatic = !hasComputedAnnotations(prop);
|
|
@@ -298,7 +304,7 @@ function buildFieldDef(path, prop, edit) {
|
|
|
298
304
|
const uiType = declaredType === "tags" && kind !== "array" ? void 0 : declaredType;
|
|
299
305
|
const pushDown = getFieldMeta(prop, UI_FORM_PUSH_DOWN) !== void 0;
|
|
300
306
|
const valueHelpInfo = extractValueHelp(prop);
|
|
301
|
-
const derived = isServerComputed(prop) || edit && path !== "" && isUpdateStamped(prop);
|
|
307
|
+
const derived = isServerComputed(prop) || write === "edit" && path !== "" && isUpdateStamped(prop);
|
|
302
308
|
const base = {
|
|
303
309
|
path,
|
|
304
310
|
prop,
|
|
@@ -320,21 +326,21 @@ function buildFieldDef(path, prop, edit) {
|
|
|
320
326
|
type: "array",
|
|
321
327
|
customType: isTags ? "tags" : isMultiselect ? "multiselect" : customType,
|
|
322
328
|
itemType: arrayType.of,
|
|
323
|
-
itemField: labelledBy(createFieldDef("", arrayType.of,
|
|
329
|
+
itemField: labelledBy(createFieldDef("", arrayType.of, write), prop)
|
|
324
330
|
};
|
|
325
331
|
}
|
|
326
332
|
if (kind === "object") return {
|
|
327
333
|
...base,
|
|
328
334
|
type: "object",
|
|
329
335
|
customType,
|
|
330
|
-
objectDef: buildFormDef(prop,
|
|
336
|
+
objectDef: buildFormDef(prop, write)
|
|
331
337
|
};
|
|
332
338
|
if (kind === "union") {
|
|
333
339
|
if (isPureLiteralUnion(prop)) return {
|
|
334
340
|
...base,
|
|
335
341
|
type: uiType ?? "select"
|
|
336
342
|
};
|
|
337
|
-
const unionVariants = createUnionVariants(prop,
|
|
343
|
+
const unionVariants = createUnionVariants(prop, soleValueMember(prop) === void 0 ? void 0 : write);
|
|
338
344
|
if (unionVariants.length > 1) return {
|
|
339
345
|
...base,
|
|
340
346
|
type: "union",
|
|
@@ -363,7 +369,7 @@ function buildFieldDef(path, prop, edit) {
|
|
|
363
369
|
...base,
|
|
364
370
|
type: "tuple",
|
|
365
371
|
customType,
|
|
366
|
-
itemFields: tupleType.items.map((item, i) => unnamed(createFieldDef(String(i), item,
|
|
372
|
+
itemFields: tupleType.items.map((item, i) => unnamed(createFieldDef(String(i), item, void 0)))
|
|
367
373
|
};
|
|
368
374
|
}
|
|
369
375
|
if (valueHelpInfo) return {
|
|
@@ -473,7 +479,7 @@ function isChildOfStructured(path, prefixes) {
|
|
|
473
479
|
* Iterates top-level items directly — one variant per item.
|
|
474
480
|
*/
|
|
475
481
|
function buildUnionVariants(typeDef) {
|
|
476
|
-
return createUnionVariants(typeDef,
|
|
482
|
+
return createUnionVariants(typeDef, void 0);
|
|
477
483
|
}
|
|
478
484
|
/**
|
|
479
485
|
* A scalar field inside `outer` (an array item, a union variant) also takes `outer`'s
|
|
@@ -483,11 +489,11 @@ function labelledBy(field, outer) {
|
|
|
483
489
|
if (field.prop?.type.kind === "" && outer.metadata.has("ui.literalLabel")) field.labelsFrom = outer;
|
|
484
490
|
return field;
|
|
485
491
|
}
|
|
486
|
-
function createUnionVariants(typeDef,
|
|
492
|
+
function createUnionVariants(typeDef, write) {
|
|
487
493
|
const items = typeDef.type.items ?? [typeDef];
|
|
488
494
|
const variants = [];
|
|
489
495
|
for (const item of items) {
|
|
490
|
-
const v = createVariant(item,
|
|
496
|
+
const v = createVariant(item, write);
|
|
491
497
|
if (v.itemField) labelledBy(v.itemField, typeDef);
|
|
492
498
|
if (items.length > 1) v.label = `${String(variants.length + 1)}. ${v.label}`;
|
|
493
499
|
variants.push(v);
|
|
@@ -495,7 +501,7 @@ function createUnionVariants(typeDef, edit) {
|
|
|
495
501
|
return variants;
|
|
496
502
|
}
|
|
497
503
|
/** Creates a single union variant from an annotated type item. */
|
|
498
|
-
function createVariant(def,
|
|
504
|
+
function createVariant(def, write) {
|
|
499
505
|
const kind = def.type.kind;
|
|
500
506
|
if (kind === "object") {
|
|
501
507
|
const label = getFieldMeta(def, META_LABEL);
|
|
@@ -506,8 +512,8 @@ function createVariant(def, edit) {
|
|
|
506
512
|
labelText: label
|
|
507
513
|
},
|
|
508
514
|
type: def,
|
|
509
|
-
def: buildFormDef(def,
|
|
510
|
-
itemField: hasComponent ? createFieldDef("", def,
|
|
515
|
+
def: buildFormDef(def, write),
|
|
516
|
+
itemField: hasComponent ? createFieldDef("", def, write) : void 0
|
|
511
517
|
};
|
|
512
518
|
}
|
|
513
519
|
if (kind === "") {
|
|
@@ -515,14 +521,14 @@ function createVariant(def, edit) {
|
|
|
515
521
|
return {
|
|
516
522
|
...typeLabel(dt === "phantom" ? "item" : dt),
|
|
517
523
|
type: def,
|
|
518
|
-
itemField: createFieldDef("", def,
|
|
524
|
+
itemField: createFieldDef("", def, write),
|
|
519
525
|
designType: dt
|
|
520
526
|
};
|
|
521
527
|
}
|
|
522
528
|
return {
|
|
523
529
|
...typeLabel(kind),
|
|
524
530
|
type: def,
|
|
525
|
-
itemField: createFieldDef("", def,
|
|
531
|
+
itemField: createFieldDef("", def, write)
|
|
526
532
|
};
|
|
527
533
|
}
|
|
528
534
|
/**
|
|
@@ -535,26 +541,9 @@ function isServerComputed(prop) {
|
|
|
535
541
|
const meta = prop.metadata;
|
|
536
542
|
return meta.has("db.column.derived") || meta.has("db.compute") || meta.has("db.compute.host");
|
|
537
543
|
}
|
|
538
|
-
/**
|
|
539
|
-
* An update-stamped field (see `CreateFormDefOptions.mode`). Read off the prop,
|
|
540
|
-
* or — as `@atscript/db` does — off the one value member of `T | null`, unless
|
|
541
|
-
* the prop references another field.
|
|
542
|
-
*/
|
|
544
|
+
/** An update-stamped field (see `CreateFormDefOptions.mode`), read as {@link columnHas} reads it. */
|
|
543
545
|
function isUpdateStamped(prop) {
|
|
544
|
-
|
|
545
|
-
const member = prop.ref?.field ? void 0 : soleValueMember(prop);
|
|
546
|
-
return member !== void 0 && isServerStampedOnUpdate(member.metadata);
|
|
547
|
-
}
|
|
548
|
-
/** The one non-`null` / `undefined` member of a (nested) union, if there is exactly one. */
|
|
549
|
-
function soleValueMember(prop) {
|
|
550
|
-
if (prop.type.kind !== "union") return void 0;
|
|
551
|
-
const members = /* @__PURE__ */ new Set();
|
|
552
|
-
const visit = (t) => {
|
|
553
|
-
if (t.type.kind === "union") for (const item of t.type.items) visit(item);
|
|
554
|
-
else if (!isEmptyMember(t)) members.add(t);
|
|
555
|
-
};
|
|
556
|
-
visit(prop);
|
|
557
|
-
return members.size === 1 ? members.values().next().value : void 0;
|
|
546
|
+
return columnHas(prop, isServerStampedOnUpdate);
|
|
558
547
|
}
|
|
559
548
|
function capitalize(s) {
|
|
560
549
|
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const require_extract_literals = require("../value-help/extract-literals.cjs");
|
|
2
|
+
let _atscript_db_validator = require("@atscript/db/validator");
|
|
3
|
+
//#region src/form/db-column.ts
|
|
4
|
+
/** The one non-`null` / `undefined` member of a (nested) union, if there is exactly one. */
|
|
5
|
+
function soleValueMember(prop) {
|
|
6
|
+
if (prop.type.kind !== "union") return void 0;
|
|
7
|
+
const members = /* @__PURE__ */ new Set();
|
|
8
|
+
const visit = (t) => {
|
|
9
|
+
if (t.type.kind === "union") for (const item of t.type.items) visit(item);
|
|
10
|
+
else if (!require_extract_literals.isEmptyMember(t)) members.add(t);
|
|
11
|
+
};
|
|
12
|
+
visit(prop);
|
|
13
|
+
return members.size === 1 ? members.values().next().value : void 0;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Whether `test` holds for the column annotations of a field, read as
|
|
17
|
+
* `@atscript/db` reads them: the prop's own, or those of the one value member
|
|
18
|
+
* of `T | null` — unless the prop references another field, whose union keeps
|
|
19
|
+
* them.
|
|
20
|
+
*/
|
|
21
|
+
function columnHas(prop, test) {
|
|
22
|
+
if (test(prop.metadata)) return true;
|
|
23
|
+
const member = prop.ref?.field ? void 0 : soleValueMember(prop);
|
|
24
|
+
return member !== void 0 && test(member.metadata);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Whether the database fills the field when an insert leaves it out: its column
|
|
28
|
+
* has a `@db.default*` (`@atscript/db`'s own insert rule) — see
|
|
29
|
+
* `CreateFormDefOptions.mode`.
|
|
30
|
+
*/
|
|
31
|
+
function isDbDefaulted(prop) {
|
|
32
|
+
return columnHas(prop, _atscript_db_validator.isDefaultedOnInsert);
|
|
33
|
+
}
|
|
34
|
+
//#endregion
|
|
35
|
+
exports.columnHas = columnHas;
|
|
36
|
+
exports.isDbDefaulted = isDbDefaulted;
|
|
37
|
+
exports.soleValueMember = soleValueMember;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { isEmptyMember } from "../value-help/extract-literals.mjs";
|
|
2
|
+
import { isDefaultedOnInsert } from "@atscript/db/validator";
|
|
3
|
+
//#region src/form/db-column.ts
|
|
4
|
+
/** The one non-`null` / `undefined` member of a (nested) union, if there is exactly one. */
|
|
5
|
+
function soleValueMember(prop) {
|
|
6
|
+
if (prop.type.kind !== "union") return void 0;
|
|
7
|
+
const members = /* @__PURE__ */ new Set();
|
|
8
|
+
const visit = (t) => {
|
|
9
|
+
if (t.type.kind === "union") for (const item of t.type.items) visit(item);
|
|
10
|
+
else if (!isEmptyMember(t)) members.add(t);
|
|
11
|
+
};
|
|
12
|
+
visit(prop);
|
|
13
|
+
return members.size === 1 ? members.values().next().value : void 0;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Whether `test` holds for the column annotations of a field, read as
|
|
17
|
+
* `@atscript/db` reads them: the prop's own, or those of the one value member
|
|
18
|
+
* of `T | null` — unless the prop references another field, whose union keeps
|
|
19
|
+
* them.
|
|
20
|
+
*/
|
|
21
|
+
function columnHas(prop, test) {
|
|
22
|
+
if (test(prop.metadata)) return true;
|
|
23
|
+
const member = prop.ref?.field ? void 0 : soleValueMember(prop);
|
|
24
|
+
return member !== void 0 && test(member.metadata);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Whether the database fills the field when an insert leaves it out: its column
|
|
28
|
+
* has a `@db.default*` (`@atscript/db`'s own insert rule) — see
|
|
29
|
+
* `CreateFormDefOptions.mode`.
|
|
30
|
+
*/
|
|
31
|
+
function isDbDefaulted(prop) {
|
|
32
|
+
return columnHas(prop, isDefaultedOnInsert);
|
|
33
|
+
}
|
|
34
|
+
//#endregion
|
|
35
|
+
export { columnHas, isDbDefaulted, soleValueMember };
|
|
@@ -267,24 +267,22 @@ function formatDecimalForDisplay(opts) {
|
|
|
267
267
|
} else canonical = value;
|
|
268
268
|
if (!/^[+-]?\d*(\.\d*)?$/.test(canonical) || canonical === "." || canonical === "-") return canonical;
|
|
269
269
|
if (scale !== void 0 && scale >= 0) canonical = enforceScale(canonical, scale);
|
|
270
|
-
const parts = splitDecimalString(canonical);
|
|
271
|
-
const asNumber = Number(canonical);
|
|
272
|
-
const intlSafe = Number.isFinite(asNumber);
|
|
273
270
|
if (currency) {
|
|
274
|
-
|
|
275
|
-
|
|
271
|
+
const parts = splitDecimalString(canonical);
|
|
272
|
+
const digits = scale ?? Math.min(Math.max(getCurrencyDecimals(currency, locale) ?? 0, parts.decimal.length), 100);
|
|
273
|
+
if (Number.isFinite(Number(canonical))) try {
|
|
274
|
+
return getNumberFormat(locale, {
|
|
276
275
|
style: "currency",
|
|
277
276
|
currency,
|
|
278
277
|
currencyDisplay: "narrowSymbol",
|
|
279
|
-
minimumFractionDigits:
|
|
280
|
-
maximumFractionDigits:
|
|
278
|
+
minimumFractionDigits: digits,
|
|
279
|
+
maximumFractionDigits: digits,
|
|
281
280
|
useGrouping
|
|
282
|
-
});
|
|
283
|
-
if (intlSafe) return fmt.format(asNumber);
|
|
281
|
+
}).format(canonical);
|
|
284
282
|
} catch {}
|
|
285
|
-
return `${currency} ${formatPlain(parts,
|
|
283
|
+
return `${currency} ${formatPlain(parts, digits, locale, useGrouping)}`;
|
|
286
284
|
}
|
|
287
|
-
const base = formatPlain(
|
|
285
|
+
const base = formatPlain(splitDecimalString(canonical), scale, locale, useGrouping);
|
|
288
286
|
return unit ? `${base} ${unit}` : base;
|
|
289
287
|
}
|
|
290
288
|
function formatPlain(parts, scale, locale, useGrouping) {
|
|
@@ -267,24 +267,22 @@ function formatDecimalForDisplay(opts) {
|
|
|
267
267
|
} else canonical = value;
|
|
268
268
|
if (!/^[+-]?\d*(\.\d*)?$/.test(canonical) || canonical === "." || canonical === "-") return canonical;
|
|
269
269
|
if (scale !== void 0 && scale >= 0) canonical = enforceScale(canonical, scale);
|
|
270
|
-
const parts = splitDecimalString(canonical);
|
|
271
|
-
const asNumber = Number(canonical);
|
|
272
|
-
const intlSafe = Number.isFinite(asNumber);
|
|
273
270
|
if (currency) {
|
|
274
|
-
|
|
275
|
-
|
|
271
|
+
const parts = splitDecimalString(canonical);
|
|
272
|
+
const digits = scale ?? Math.min(Math.max(getCurrencyDecimals(currency, locale) ?? 0, parts.decimal.length), 100);
|
|
273
|
+
if (Number.isFinite(Number(canonical))) try {
|
|
274
|
+
return getNumberFormat(locale, {
|
|
276
275
|
style: "currency",
|
|
277
276
|
currency,
|
|
278
277
|
currencyDisplay: "narrowSymbol",
|
|
279
|
-
minimumFractionDigits:
|
|
280
|
-
maximumFractionDigits:
|
|
278
|
+
minimumFractionDigits: digits,
|
|
279
|
+
maximumFractionDigits: digits,
|
|
281
280
|
useGrouping
|
|
282
|
-
});
|
|
283
|
-
if (intlSafe) return fmt.format(asNumber);
|
|
281
|
+
}).format(canonical);
|
|
284
282
|
} catch {}
|
|
285
|
-
return `${currency} ${formatPlain(parts,
|
|
283
|
+
return `${currency} ${formatPlain(parts, digits, locale, useGrouping)}`;
|
|
286
284
|
}
|
|
287
|
-
const base = formatPlain(
|
|
285
|
+
const base = formatPlain(splitDecimalString(canonical), scale, locale, useGrouping);
|
|
288
286
|
return unit ? `${base} ${unit}` : base;
|
|
289
287
|
}
|
|
290
288
|
function formatPlain(parts, scale, locale, useGrouping) {
|
package/dist/form/diff.cjs
CHANGED
|
@@ -15,7 +15,11 @@ let _atscript_db_validator = require("@atscript/db/validator");
|
|
|
15
15
|
* (`{ value: domainData }`) so this reuses {@link getByPath}.
|
|
16
16
|
*
|
|
17
17
|
* Revert-aware: a value edited back to its baseline produces no change and no
|
|
18
|
-
* patch entry.
|
|
18
|
+
* patch entry. A value stored unset (`null` / absent) that the form now holds
|
|
19
|
+
* as `""` is no change either, at any depth — a field typed into and emptied
|
|
20
|
+
* again, an optional text emptied on an edit form (since 0.1.205). The reverse
|
|
21
|
+
* is a change: a stored `""` that is now unset is sent as `null`. Emptying a
|
|
22
|
+
* stored value is a change to `""` / `null` / `false`.
|
|
19
23
|
*
|
|
20
24
|
* Snapshot contract: the result is NOT a deep copy. `$insert` items, `$replace`
|
|
21
25
|
* arrays, scalar leaf values, and `changes[].before/after` all hold live
|
|
@@ -76,7 +80,7 @@ function isNavKey(def, key) {
|
|
|
76
80
|
*/
|
|
77
81
|
function diffFields(fields, prefix, baseline, current, changes, patch, mergePaths, versionColumn) {
|
|
78
82
|
for (const field of fields) {
|
|
79
|
-
if (field.phantom || field.derived
|
|
83
|
+
if (field.phantom || field.derived || versionColumn !== void 0 && !prefix && field.path === versionColumn) continue;
|
|
80
84
|
const fullPath = prefix ? `${prefix}.${field.path}` : field.path;
|
|
81
85
|
if (require_types.isArrayField(field)) {
|
|
82
86
|
diffArrayField(field, fullPath, baseline, current, changes, patch, mergePaths);
|
|
@@ -111,7 +115,7 @@ function diffLeafRoot(def, baseline, current, changes, patch) {
|
|
|
111
115
|
const field = def.rootField;
|
|
112
116
|
const before = require_path_utils.getByPath(baseline, "");
|
|
113
117
|
const after = require_path_utils.getByPath(current, "");
|
|
114
|
-
if (
|
|
118
|
+
if (sameStored(before, after)) return;
|
|
115
119
|
const mergePaths = leafRootMergePaths(def.type);
|
|
116
120
|
if (require_types.isArrayField(field)) {
|
|
117
121
|
const arrayPatch = diffArray(field, before, after, mergePaths.has(LEAF_ROOT_KEY));
|
|
@@ -137,7 +141,7 @@ function diffLeafRoot(def, baseline, current, changes, patch) {
|
|
|
137
141
|
function diffScalarField(field, fullPath, baseline, current, changes, patch, mergePaths) {
|
|
138
142
|
const before = require_path_utils.getByPath(baseline, fullPath);
|
|
139
143
|
const after = require_path_utils.getByPath(current, fullPath);
|
|
140
|
-
if (
|
|
144
|
+
if (sameStored(before, after)) return;
|
|
141
145
|
changes.push({
|
|
142
146
|
path: fullPath,
|
|
143
147
|
kind: "set",
|
|
@@ -225,7 +229,7 @@ function diffObjectField(field, fullPath, baseline, current, changes, patch, mer
|
|
|
225
229
|
function diffArrayField(field, fullPath, baseline, current, changes, patch, mergePaths) {
|
|
226
230
|
const before = require_path_utils.getByPath(baseline, fullPath);
|
|
227
231
|
const after = require_path_utils.getByPath(current, fullPath);
|
|
228
|
-
if (
|
|
232
|
+
if (sameStored(before, after)) return;
|
|
229
233
|
const value = require_field_resolver.getFieldMeta(field.prop, "db.json") !== void 0 || require_field_resolver.getFieldMeta(field.prop, "db.encrypted") !== void 0 ? after ?? null : diffArray(field, before, after, mergePaths.has(fullPath));
|
|
230
234
|
if (value === void 0) return;
|
|
231
235
|
changes.push({
|
|
@@ -318,7 +322,7 @@ function diffKeyedArray(before, after, keyProps, merge) {
|
|
|
318
322
|
continue;
|
|
319
323
|
}
|
|
320
324
|
if (afterKeys[kept++] !== k) return { $replace: after };
|
|
321
|
-
if (
|
|
325
|
+
if (sameStored(prev, el)) continue;
|
|
322
326
|
const partial = buildKeyedUpdate(prev, el, keyProps);
|
|
323
327
|
if (!partial) continue;
|
|
324
328
|
if (incomplete(el)) return { $replace: after };
|
|
@@ -347,7 +351,7 @@ function buildKeyedUpdate(prev, next, keyProps) {
|
|
|
347
351
|
if (keyProps.includes(k)) continue;
|
|
348
352
|
const a = prev[k];
|
|
349
353
|
const b = next[k];
|
|
350
|
-
if (!
|
|
354
|
+
if (!sameStored(a, b)) {
|
|
351
355
|
partial[k] = b === void 0 ? null : b;
|
|
352
356
|
changed = true;
|
|
353
357
|
}
|
|
@@ -487,13 +491,27 @@ function stableKey(v) {
|
|
|
487
491
|
* unchanged. The patch cannot tell them apart either: it sends a cleared value
|
|
488
492
|
* as `null`.
|
|
489
493
|
*
|
|
490
|
-
* The single comparator shared across the form engine
|
|
491
|
-
* detection ({@link buildFormRebase})
|
|
492
|
-
* reimplement equality elsewhere.
|
|
494
|
+
* The single comparator shared across the form engine — the diff and conflict
|
|
495
|
+
* detection ({@link buildFormRebase}) — never reimplement equality elsewhere.
|
|
493
496
|
*/
|
|
494
497
|
function deepEqual(a, b) {
|
|
498
|
+
return equal(a, b, false);
|
|
499
|
+
}
|
|
500
|
+
/**
|
|
501
|
+
* Whether the form's `after` leaves the stored `before` unchanged: {@link deepEqual},
|
|
502
|
+
* and a value stored unset (`null` / `undefined` / absent) that now holds `""` is
|
|
503
|
+
* unchanged too, at any depth — a field the user typed into and emptied again, an
|
|
504
|
+
* optional text emptied on an edit form (business mode), a field switched on and
|
|
505
|
+
* left empty. Asymmetric: a stored `""` that is now unset is a change. Since 0.1.205.
|
|
506
|
+
*/
|
|
507
|
+
function sameStored(before, after) {
|
|
508
|
+
return equal(before, after, true);
|
|
509
|
+
}
|
|
510
|
+
/** {@link deepEqual}; with `emptyText`, also `b === ""` over an unset `a` ({@link sameStored}). */
|
|
511
|
+
function equal(a, b, emptyText) {
|
|
495
512
|
if (a === b) return true;
|
|
496
|
-
|
|
513
|
+
const aUnset = a === null || a === void 0;
|
|
514
|
+
if (aUnset || b === null || b === void 0) return aUnset && (b === null || b === void 0 || emptyText && b === "");
|
|
497
515
|
if (typeof a === "number" && typeof b === "number") return Number.isNaN(a) && Number.isNaN(b);
|
|
498
516
|
if (typeof a !== "object" || typeof b !== "object") return false;
|
|
499
517
|
const aIsArr = Array.isArray(a);
|
|
@@ -501,13 +519,13 @@ function deepEqual(a, b) {
|
|
|
501
519
|
if (aIsArr !== bIsArr) return false;
|
|
502
520
|
if (aIsArr && bIsArr) {
|
|
503
521
|
if (a.length !== b.length) return false;
|
|
504
|
-
for (let i = 0; i < a.length; i++) if (!
|
|
522
|
+
for (let i = 0; i < a.length; i++) if (!equal(a[i], b[i], emptyText)) return false;
|
|
505
523
|
return true;
|
|
506
524
|
}
|
|
507
525
|
const ao = a;
|
|
508
526
|
const bo = b;
|
|
509
|
-
for (const k of Object.keys(ao)) if (!
|
|
510
|
-
for (const k of Object.keys(bo)) if (!Object.hasOwn(ao, k) &&
|
|
527
|
+
for (const k of Object.keys(ao)) if (!equal(ao[k], Object.hasOwn(bo, k) ? bo[k] : void 0, emptyText)) return false;
|
|
528
|
+
for (const k of Object.keys(bo)) if (!Object.hasOwn(ao, k) && !equal(void 0, bo[k], emptyText)) return false;
|
|
511
529
|
return true;
|
|
512
530
|
}
|
|
513
531
|
//#endregion
|
package/dist/form/diff.d.cts
CHANGED
|
@@ -60,7 +60,11 @@ interface FormDiffResult {
|
|
|
60
60
|
* (`{ value: domainData }`) so this reuses {@link getByPath}.
|
|
61
61
|
*
|
|
62
62
|
* Revert-aware: a value edited back to its baseline produces no change and no
|
|
63
|
-
* patch entry.
|
|
63
|
+
* patch entry. A value stored unset (`null` / absent) that the form now holds
|
|
64
|
+
* as `""` is no change either, at any depth — a field typed into and emptied
|
|
65
|
+
* again, an optional text emptied on an edit form (since 0.1.205). The reverse
|
|
66
|
+
* is a change: a stored `""` that is now unset is sent as `null`. Emptying a
|
|
67
|
+
* stored value is a change to `""` / `null` / `false`.
|
|
64
68
|
*
|
|
65
69
|
* Snapshot contract: the result is NOT a deep copy. `$insert` items, `$replace`
|
|
66
70
|
* arrays, scalar leaf values, and `changes[].before/after` all hold live
|
|
@@ -80,9 +84,8 @@ declare function buildFormDiff(def: FormDef, baseline: Record<string, unknown>,
|
|
|
80
84
|
* unchanged. The patch cannot tell them apart either: it sends a cleared value
|
|
81
85
|
* as `null`.
|
|
82
86
|
*
|
|
83
|
-
* The single comparator shared across the form engine
|
|
84
|
-
* detection ({@link buildFormRebase})
|
|
85
|
-
* reimplement equality elsewhere.
|
|
87
|
+
* The single comparator shared across the form engine — the diff and conflict
|
|
88
|
+
* detection ({@link buildFormRebase}) — never reimplement equality elsewhere.
|
|
86
89
|
*/
|
|
87
90
|
declare function deepEqual(a: unknown, b: unknown): boolean;
|
|
88
91
|
//#endregion
|