@stonecrop/stonecrop 0.39.0 → 0.41.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/stonecrop.d.ts +122 -4
- package/dist/stonecrop.js +381 -36
- package/dist/stonecrop.js.map +1 -1
- package/package.json +4 -4
package/dist/stonecrop.js
CHANGED
|
@@ -2,7 +2,7 @@ import { useRouter } from "vue-router";
|
|
|
2
2
|
import { defineStore, storeToRefs } from "pinia";
|
|
3
3
|
import { computed, getCurrentInstance, inject, nextTick, onMounted, provide, reactive, ref, watch } from "vue";
|
|
4
4
|
import { useLocalStorage, useMagicKeys, whenever } from "@vueuse/core";
|
|
5
|
-
import { componentCategory, componentLinkExpansion, getRecordIdField, getRecordIdentity, isActionAllowedInState, normalizeFieldKind, resolveLinkRenderMode } from "@stonecrop/schema";
|
|
5
|
+
import { componentCategory, componentLinkExpansion, flattenFields, getRecordIdField, getRecordIdentity, isActionAllowedInState, normalizeFieldKind, resolveLinkRenderMode, toSlug } from "@stonecrop/schema";
|
|
6
6
|
import { List } from "immutable";
|
|
7
7
|
import { resolvedFieldsToColumns } from "@stonecrop/aform";
|
|
8
8
|
//#region src/client-handler.ts
|
|
@@ -1675,11 +1675,10 @@ var Stonecrop = class Stonecrop {
|
|
|
1675
1675
|
* @param doctype - The doctype to initialize
|
|
1676
1676
|
* @public
|
|
1677
1677
|
*/
|
|
1678
|
-
initializeNestedData(path, doctype) {
|
|
1678
|
+
async initializeNestedData(path, doctype) {
|
|
1679
1679
|
const slug = doctype.slug;
|
|
1680
1680
|
this.ensureDoctypeExists(slug);
|
|
1681
|
-
const
|
|
1682
|
-
const record = this.registry.initializeRecord(resolvedSchema);
|
|
1681
|
+
const { record } = await this.registry.composeNewRecord(doctype);
|
|
1683
1682
|
if (!this.hstStore.get(path)) this.hstStore.set(path, {}, "system");
|
|
1684
1683
|
for (const [key, value] of Object.entries(record)) this.hstStore.set(`${path}.${key}`, value, "system");
|
|
1685
1684
|
}
|
|
@@ -1828,7 +1827,14 @@ function useStonecrop(options) {
|
|
|
1828
1827
|
if (options.doctype && typeof options.doctype !== "string" && registry && stonecrop.value) {
|
|
1829
1828
|
hstStore.value = stonecrop.value.getStore();
|
|
1830
1829
|
resolvedSchema.value = registry.resolveSchema(options.doctype);
|
|
1831
|
-
if (!options.recordId || isDraftRecordId(options.recordId))
|
|
1830
|
+
if (!options.recordId || isDraftRecordId(options.recordId)) {
|
|
1831
|
+
isLoading.value = true;
|
|
1832
|
+
registry.composeNewRecord(options.doctype).then(({ record }) => {
|
|
1833
|
+
formData.value = record;
|
|
1834
|
+
}).finally(() => {
|
|
1835
|
+
isLoading.value = false;
|
|
1836
|
+
});
|
|
1837
|
+
}
|
|
1832
1838
|
if (hstStore.value) setupDeepReactivity(options.doctype, options.recordId || "new", formData, hstStore.value);
|
|
1833
1839
|
}
|
|
1834
1840
|
onMounted(async () => {
|
|
@@ -1870,7 +1876,14 @@ function useStonecrop(options) {
|
|
|
1870
1876
|
} catch {
|
|
1871
1877
|
formData.value = registry.initializeRecord(resolvedSchema.value);
|
|
1872
1878
|
}
|
|
1873
|
-
} else
|
|
1879
|
+
} else {
|
|
1880
|
+
isLoading.value = true;
|
|
1881
|
+
try {
|
|
1882
|
+
formData.value = (await registry.composeNewRecord(doctype)).record;
|
|
1883
|
+
} finally {
|
|
1884
|
+
isLoading.value = false;
|
|
1885
|
+
}
|
|
1886
|
+
}
|
|
1874
1887
|
if (hstStore.value) setupDeepReactivity(doctype, recordId || "new", formData, hstStore.value);
|
|
1875
1888
|
} else if (recordId && !isDraftRecordId(recordId)) {
|
|
1876
1889
|
const doctype = options.doctype;
|
|
@@ -1933,7 +1946,7 @@ function useStonecrop(options) {
|
|
|
1933
1946
|
* @param path - The HST path where initialized data should be stored
|
|
1934
1947
|
* @param doctype - The doctype to initialize
|
|
1935
1948
|
*/
|
|
1936
|
-
const initializeNestedData = (path, doctype) => {
|
|
1949
|
+
const initializeNestedData = async (path, doctype) => {
|
|
1937
1950
|
if (!stonecrop.value) throw new Error("Stonecrop instance not available");
|
|
1938
1951
|
return stonecrop.value.initializeNestedData(path, doctype);
|
|
1939
1952
|
};
|
|
@@ -2541,6 +2554,12 @@ var Doctype = class Doctype {
|
|
|
2541
2554
|
*/
|
|
2542
2555
|
displayField;
|
|
2543
2556
|
/**
|
|
2557
|
+
* Starting values for new records: fixed data in the shape of the record.
|
|
2558
|
+
* @public
|
|
2559
|
+
* @readonly
|
|
2560
|
+
*/
|
|
2561
|
+
defaults;
|
|
2562
|
+
/**
|
|
2544
2563
|
* Creates a new Doctype instance
|
|
2545
2564
|
* @param doctype - The doctype name
|
|
2546
2565
|
* @param schema - The doctype schema definition
|
|
@@ -2549,13 +2568,14 @@ var Doctype = class Doctype {
|
|
|
2549
2568
|
* @param links - Optional relationship links to other doctypes
|
|
2550
2569
|
* @param displayField - Optional field used when displaying references to this doctype
|
|
2551
2570
|
*/
|
|
2552
|
-
constructor(doctype, schema, workflow, component, links, displayField) {
|
|
2571
|
+
constructor(doctype, schema, workflow, component, links, displayField, defaults) {
|
|
2553
2572
|
this.doctype = doctype;
|
|
2554
2573
|
this.schema = schema;
|
|
2555
2574
|
this.workflow = workflow;
|
|
2556
2575
|
this.component = component;
|
|
2557
2576
|
this.links = links;
|
|
2558
2577
|
this.displayField = displayField;
|
|
2578
|
+
this.defaults = defaults;
|
|
2559
2579
|
}
|
|
2560
2580
|
/**
|
|
2561
2581
|
* Creates a Doctype instance from a plain configuration object.
|
|
@@ -2598,7 +2618,7 @@ var Doctype = class Doctype {
|
|
|
2598
2618
|
static fromObject(config) {
|
|
2599
2619
|
const fields = config.fields?.map(normalizeFieldKind);
|
|
2600
2620
|
const schema = fields ? List(fields) : List();
|
|
2601
|
-
return new Doctype(config.name, schema, config.workflow, void 0, config.links, config.displayField);
|
|
2621
|
+
return new Doctype(config.name, schema, config.workflow, void 0, config.links, config.displayField, config.defaults);
|
|
2602
2622
|
}
|
|
2603
2623
|
/**
|
|
2604
2624
|
* Returns the raw authoring schema as a plain array.
|
|
@@ -2774,10 +2794,312 @@ var Doctype = class Doctype {
|
|
|
2774
2794
|
* @public
|
|
2775
2795
|
*/
|
|
2776
2796
|
get slug() {
|
|
2777
|
-
return this.doctype
|
|
2797
|
+
return toSlug(this.doctype);
|
|
2778
2798
|
}
|
|
2779
2799
|
};
|
|
2780
2800
|
//#endregion
|
|
2801
|
+
//#region src/record-fields.ts
|
|
2802
|
+
/**
|
|
2803
|
+
* A doctype's link declarations by the field each one binds to: `link.fieldname`, or the declaration's key when it
|
|
2804
|
+
* names none.
|
|
2805
|
+
* @internal
|
|
2806
|
+
*/
|
|
2807
|
+
function linksByFieldname(doctype) {
|
|
2808
|
+
const links = /* @__PURE__ */ new Map();
|
|
2809
|
+
for (const [key, link] of Object.entries(doctype.links ?? {})) links.set(link.fieldname ?? key, link);
|
|
2810
|
+
return links;
|
|
2811
|
+
}
|
|
2812
|
+
/**
|
|
2813
|
+
* The doctype a field's value is made of, when it holds one: an embedded record (`'record'`) or rows (`'table'`)
|
|
2814
|
+
* of a registered, linked doctype. Undefined when the field holds a value of its own: a plain field, an inline
|
|
2815
|
+
* link's id, or a link whose target is not registered.
|
|
2816
|
+
*
|
|
2817
|
+
* The single definition of what a link field holds. `resolveSchema` renders from it and a new record is composed
|
|
2818
|
+
* from it, so the form and the record cannot disagree about a field's shape.
|
|
2819
|
+
* @internal
|
|
2820
|
+
*/
|
|
2821
|
+
function expandedLink(registry, field, links) {
|
|
2822
|
+
const link = links.get(field.fieldname);
|
|
2823
|
+
if (!link) return void 0;
|
|
2824
|
+
const mode = resolveLinkRenderMode(link, field.component);
|
|
2825
|
+
if (mode === "inline") return void 0;
|
|
2826
|
+
const target = registry.registry[link.target];
|
|
2827
|
+
return target ? {
|
|
2828
|
+
mode,
|
|
2829
|
+
link,
|
|
2830
|
+
target
|
|
2831
|
+
} : void 0;
|
|
2832
|
+
}
|
|
2833
|
+
/**
|
|
2834
|
+
* The keys of a doctype's record, by name. A grouped section adds none of its own: it only groups fields, and
|
|
2835
|
+
* each of them is a key of the record.
|
|
2836
|
+
* @internal
|
|
2837
|
+
*/
|
|
2838
|
+
function recordFields(registry, doctype) {
|
|
2839
|
+
const links = linksByFieldname(doctype);
|
|
2840
|
+
const fields = /* @__PURE__ */ new Map();
|
|
2841
|
+
for (const field of flattenFields(doctype.getSchemaArray())) {
|
|
2842
|
+
const { fieldname } = field;
|
|
2843
|
+
if (field.kind === "table") {
|
|
2844
|
+
fields.set(fieldname, {
|
|
2845
|
+
holds: "columns",
|
|
2846
|
+
fieldname,
|
|
2847
|
+
columns: field.columns
|
|
2848
|
+
});
|
|
2849
|
+
continue;
|
|
2850
|
+
}
|
|
2851
|
+
const expanded = expandedLink(registry, field, links);
|
|
2852
|
+
if (!expanded) fields.set(fieldname, {
|
|
2853
|
+
holds: "value",
|
|
2854
|
+
fieldname,
|
|
2855
|
+
component: field.component
|
|
2856
|
+
});
|
|
2857
|
+
else fields.set(fieldname, {
|
|
2858
|
+
holds: expanded.mode === "table" ? "rows" : "record",
|
|
2859
|
+
fieldname,
|
|
2860
|
+
target: expanded.target
|
|
2861
|
+
});
|
|
2862
|
+
}
|
|
2863
|
+
return fields;
|
|
2864
|
+
}
|
|
2865
|
+
/**
|
|
2866
|
+
* The keys of an inline table's row: one per column.
|
|
2867
|
+
* @internal
|
|
2868
|
+
*/
|
|
2869
|
+
function columnFields(columns) {
|
|
2870
|
+
return new Map(columns.map((column) => [column.fieldname, {
|
|
2871
|
+
holds: "value",
|
|
2872
|
+
fieldname: column.fieldname,
|
|
2873
|
+
component: column.component
|
|
2874
|
+
}]));
|
|
2875
|
+
}
|
|
2876
|
+
//#endregion
|
|
2877
|
+
//#region src/uuidv7.ts
|
|
2878
|
+
/**
|
|
2879
|
+
* Generate a UUID version 7 (time-ordered) string without an external dependency.
|
|
2880
|
+
* @public
|
|
2881
|
+
*/
|
|
2882
|
+
function createUuidv7(now = /* @__PURE__ */ new Date()) {
|
|
2883
|
+
const ms = BigInt(now.getTime());
|
|
2884
|
+
const rand = crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(10));
|
|
2885
|
+
const timeHex = ms.toString(16).padStart(12, "0").slice(-12);
|
|
2886
|
+
const randHex = Array.from(rand, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
2887
|
+
return `${`${timeHex.slice(0, 8)}-${timeHex.slice(8, 12)}`}-${`7${randHex.slice(0, 3)}`}-${`${(parseInt(randHex.slice(3, 5), 16) & 63 | 128).toString(16).padStart(2, "0")}${randHex.slice(5, 7)}`}-${randHex.slice(7, 19)}`;
|
|
2888
|
+
}
|
|
2889
|
+
function isPlainObject$1(value) {
|
|
2890
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2891
|
+
}
|
|
2892
|
+
/**
|
|
2893
|
+
* The day `now` falls on in the user's own time zone, as `YYYY-MM-DD`: the same day the date picker shows as
|
|
2894
|
+
* today. Not the UTC day, which is tomorrow in the evening in the Americas and yesterday in the morning in Asia.
|
|
2895
|
+
*/
|
|
2896
|
+
function localDay(now) {
|
|
2897
|
+
return `${String(now.getFullYear()).padStart(4, "0")}-${String(now.getMonth() + 1).padStart(2, "0")}-${String(now.getDate()).padStart(2, "0")}`;
|
|
2898
|
+
}
|
|
2899
|
+
/**
|
|
2900
|
+
* Resolve compose-time tokens on a scalar value for a field component.
|
|
2901
|
+
* @internal
|
|
2902
|
+
*/
|
|
2903
|
+
function resolveDefaultToken(value, component, now) {
|
|
2904
|
+
if (value === "uuidv7") return createUuidv7(now);
|
|
2905
|
+
if (value !== "now") return value;
|
|
2906
|
+
const category = componentCategory(component);
|
|
2907
|
+
if (category === "datetime") return now.toISOString();
|
|
2908
|
+
if (category === "date") return localDay(now);
|
|
2909
|
+
return value;
|
|
2910
|
+
}
|
|
2911
|
+
/**
|
|
2912
|
+
* Walk a composed record and resolve `"now"` / `"uuidv7"` on its values. Each key is read through its field in the
|
|
2913
|
+
* doctype's declarations, so embedded records and rows use their own doctype, and an inline table's rows use its
|
|
2914
|
+
* columns.
|
|
2915
|
+
* @internal
|
|
2916
|
+
*/
|
|
2917
|
+
function resolveTokensInRecord(record, fields, registry, now) {
|
|
2918
|
+
const out = { ...record };
|
|
2919
|
+
for (const field of fields.values()) {
|
|
2920
|
+
const value = out[field.fieldname];
|
|
2921
|
+
if (field.holds === "value") {
|
|
2922
|
+
if (field.fieldname in out) out[field.fieldname] = resolveDefaultToken(value, field.component, now);
|
|
2923
|
+
} else if (field.holds === "record") {
|
|
2924
|
+
if (isPlainObject$1(value)) out[field.fieldname] = resolveTokensInRecord(value, recordFields(registry, field.target), registry, now);
|
|
2925
|
+
} else if (Array.isArray(value)) {
|
|
2926
|
+
const rowFields = field.holds === "rows" ? recordFields(registry, field.target) : columnFields(field.columns);
|
|
2927
|
+
out[field.fieldname] = value.map((row) => isPlainObject$1(row) ? resolveTokensInRecord(row, rowFields, registry, now) : row);
|
|
2928
|
+
}
|
|
2929
|
+
}
|
|
2930
|
+
return out;
|
|
2931
|
+
}
|
|
2932
|
+
//#endregion
|
|
2933
|
+
//#region src/compose-new-record.ts
|
|
2934
|
+
/**
|
|
2935
|
+
* How long a new record waits for a starting value that has not arrived. A value still missing by then is
|
|
2936
|
+
* skipped and reported, and the record opens without it.
|
|
2937
|
+
* @public
|
|
2938
|
+
*/
|
|
2939
|
+
var DEFAULTS_TIMEOUT_MS = 5e3;
|
|
2940
|
+
var TIMED_OUT = Symbol("timed out");
|
|
2941
|
+
function isPlainObject(value) {
|
|
2942
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2943
|
+
}
|
|
2944
|
+
function isDefaultsDocument(value) {
|
|
2945
|
+
return isPlainObject(value);
|
|
2946
|
+
}
|
|
2947
|
+
/**
|
|
2948
|
+
* Call a defaults function (or take a literal) and wait for its value until the run's deadline. A throw, a
|
|
2949
|
+
* rejection or the deadline each skip this one value and record why; nothing else in the run is affected.
|
|
2950
|
+
*/
|
|
2951
|
+
async function evaluate(raw, context, run, path) {
|
|
2952
|
+
try {
|
|
2953
|
+
const produced = typeof raw === "function" ? raw(context) : raw;
|
|
2954
|
+
const value = await Promise.race([Promise.resolve(produced), run.deadline]);
|
|
2955
|
+
if (value === TIMED_OUT) {
|
|
2956
|
+
run.problems.push(`${path}: no answer within ${run.timeoutMs} ms`);
|
|
2957
|
+
return { ok: false };
|
|
2958
|
+
}
|
|
2959
|
+
return {
|
|
2960
|
+
ok: true,
|
|
2961
|
+
value
|
|
2962
|
+
};
|
|
2963
|
+
} catch (error) {
|
|
2964
|
+
run.problems.push(`${path}: ${error instanceof Error ? error.message : String(error)}`);
|
|
2965
|
+
run.errors.push(error);
|
|
2966
|
+
return { ok: false };
|
|
2967
|
+
}
|
|
2968
|
+
}
|
|
2969
|
+
/**
|
|
2970
|
+
* Apply one defaults document onto `target`. Its fields are awaited side by side; each lands when it is ready,
|
|
2971
|
+
* and a field that fails leaves `target`'s value for it untouched.
|
|
2972
|
+
*
|
|
2973
|
+
* A key that names no field of the record is skipped and reported, since it would otherwise reach the save as
|
|
2974
|
+
* a field nobody declared. A grouped section's name is one such key: the section only groups fields, and each
|
|
2975
|
+
* of them takes its own entry.
|
|
2976
|
+
*/
|
|
2977
|
+
async function applyDocument(target, document, fields, run, path, within) {
|
|
2978
|
+
await Promise.all(Object.entries(document).map(async ([key, rawValue]) => {
|
|
2979
|
+
const fieldPath = `${path} › ${key}`;
|
|
2980
|
+
const field = fields.get(key);
|
|
2981
|
+
if (!field) {
|
|
2982
|
+
run.problems.push(`${fieldPath}: no field by that name`);
|
|
2983
|
+
return;
|
|
2984
|
+
}
|
|
2985
|
+
const evaluated = await evaluate(rawValue, {
|
|
2986
|
+
doctype: run.doctype,
|
|
2987
|
+
record: { ...target },
|
|
2988
|
+
fieldname: key
|
|
2989
|
+
}, run, fieldPath);
|
|
2990
|
+
if (!evaluated.ok) return;
|
|
2991
|
+
await assignField(target, field, evaluated.value, run, fieldPath, within);
|
|
2992
|
+
}));
|
|
2993
|
+
}
|
|
2994
|
+
async function assignField(target, field, value, run, path, within) {
|
|
2995
|
+
const key = field.fieldname;
|
|
2996
|
+
if ((field.holds === "rows" || field.holds === "columns") && Array.isArray(value)) {
|
|
2997
|
+
target[key] = await Promise.all(value.map((row, index) => composeRow(row, field, run, `${path}[${index}]`, within)));
|
|
2998
|
+
return;
|
|
2999
|
+
}
|
|
3000
|
+
if (field.holds === "record" && isDefaultsDocument(value)) {
|
|
3001
|
+
const existing = isPlainObject(target[key]) ? target[key] : {};
|
|
3002
|
+
await applyDocument(existing, value, recordFields(run.registry, field.target), run, path, within);
|
|
3003
|
+
target[key] = existing;
|
|
3004
|
+
return;
|
|
3005
|
+
}
|
|
3006
|
+
target[key] = resolveDefaultToken(value, field.holds === "value" ? field.component : void 0, run.now);
|
|
3007
|
+
}
|
|
3008
|
+
/**
|
|
3009
|
+
* Apply a doctype's own `defaults` onto a record of it, and each embedded record's doctype's `defaults` onto that
|
|
3010
|
+
* record first, so the containing doctype's entries win. Only where the record holds an embedded record, which
|
|
3011
|
+
* `resolveSchema` decided, so a doctype that embeds itself stops where its form does.
|
|
3012
|
+
*
|
|
3013
|
+
* `within` is the doctypes whose `defaults` are being applied above this record. Defaults that start a row of a
|
|
3014
|
+
* doctype already among them would never end, so they stop there and are reported.
|
|
3015
|
+
*/
|
|
3016
|
+
async function applyDoctypeDefaults(target, doctype, run, path, within) {
|
|
3017
|
+
if (within.includes(doctype)) {
|
|
3018
|
+
run.problems.push(`${path}: a new ${doctype.doctype} inside a new ${doctype.doctype} would never end, so it starts empty`);
|
|
3019
|
+
return;
|
|
3020
|
+
}
|
|
3021
|
+
const fields = recordFields(run.registry, doctype);
|
|
3022
|
+
for (const field of fields.values()) {
|
|
3023
|
+
const embedded = target[field.fieldname];
|
|
3024
|
+
if (field.holds === "record" && isPlainObject(embedded)) await applyDoctypeDefaults(embedded, field.target, run, `${path} › ${field.fieldname}`, within);
|
|
3025
|
+
}
|
|
3026
|
+
await applyLayer(target, doctype.defaults, fields, run, path, [...within, doctype]);
|
|
3027
|
+
}
|
|
3028
|
+
/**
|
|
3029
|
+
* One row of a table's starting value: the row's own empty values and its doctype's `defaults`, then the row's
|
|
3030
|
+
* entries. A linked table's rows are records of its target doctype; an inline table's rows have one value per
|
|
3031
|
+
* column.
|
|
3032
|
+
*/
|
|
3033
|
+
async function composeRow(row, table, run, path, within) {
|
|
3034
|
+
const { registry, now } = run;
|
|
3035
|
+
const fields = table.holds === "rows" ? recordFields(registry, table.target) : columnFields(table.columns);
|
|
3036
|
+
const floor = table.holds === "rows" ? registry.initializeRecord(registry.resolveSchema(table.target)) : registry.initializeRecord(table.columns.map((column) => ({
|
|
3037
|
+
kind: "field",
|
|
3038
|
+
fieldname: column.fieldname,
|
|
3039
|
+
component: column.component ?? "ATextInput",
|
|
3040
|
+
label: column.label
|
|
3041
|
+
})));
|
|
3042
|
+
if (table.holds === "rows") await applyDoctypeDefaults(floor, table.target, run, `${path} defaults`, within);
|
|
3043
|
+
const rowRecord = resolveTokensInRecord(floor, fields, registry, now);
|
|
3044
|
+
if (!isDefaultsDocument(row)) {
|
|
3045
|
+
run.problems.push(`${path}: gave back ${JSON.stringify(row)}, not a document of field values`);
|
|
3046
|
+
return rowRecord;
|
|
3047
|
+
}
|
|
3048
|
+
await applyDocument(rowRecord, row, fields, run, path, within);
|
|
3049
|
+
return resolveTokensInRecord(rowRecord, fields, registry, now);
|
|
3050
|
+
}
|
|
3051
|
+
/** A layer is a document, or a function that returns one; either way it is applied only once it is whole. */
|
|
3052
|
+
async function applyLayer(target, layer, fields, run, path, within) {
|
|
3053
|
+
if (!layer) return;
|
|
3054
|
+
const evaluated = await evaluate(layer, {
|
|
3055
|
+
doctype: run.doctype,
|
|
3056
|
+
record: { ...target }
|
|
3057
|
+
}, run, path);
|
|
3058
|
+
if (!evaluated.ok) return;
|
|
3059
|
+
if (!isDefaultsDocument(evaluated.value)) {
|
|
3060
|
+
run.problems.push(`${path}: gave back ${JSON.stringify(evaluated.value)}, not a document of field values`);
|
|
3061
|
+
return;
|
|
3062
|
+
}
|
|
3063
|
+
await applyDocument(target, evaluated.value, fields, run, path, within);
|
|
3064
|
+
}
|
|
3065
|
+
/**
|
|
3066
|
+
* Build a new record, once, from every starting value: the schema's empty values, the doctype's `defaults`, then
|
|
3067
|
+
* the source the app registered for it. Layers apply in that order, so the registered source wins however long
|
|
3068
|
+
* the doctype's took.
|
|
3069
|
+
*
|
|
3070
|
+
* Nothing is returned until every value is in, so nothing is written to the record after a user can see it.
|
|
3071
|
+
* A value that throws, rejects, or has not arrived within `timeoutMs` (default {@link DEFAULTS_TIMEOUT_MS}) is
|
|
3072
|
+
* skipped and reported with `console.warn`; the record still opens with everything else.
|
|
3073
|
+
* @public
|
|
3074
|
+
*/
|
|
3075
|
+
async function composeNewRecord(registry, doctype, options) {
|
|
3076
|
+
const now = options?.now ?? /* @__PURE__ */ new Date();
|
|
3077
|
+
const timeoutMs = options?.timeoutMs ?? 5e3;
|
|
3078
|
+
let timer;
|
|
3079
|
+
const run = {
|
|
3080
|
+
registry,
|
|
3081
|
+
doctype,
|
|
3082
|
+
now,
|
|
3083
|
+
timeoutMs,
|
|
3084
|
+
problems: [],
|
|
3085
|
+
errors: [],
|
|
3086
|
+
deadline: new Promise((resolve) => {
|
|
3087
|
+
timer = setTimeout(() => resolve(TIMED_OUT), timeoutMs);
|
|
3088
|
+
})
|
|
3089
|
+
};
|
|
3090
|
+
try {
|
|
3091
|
+
const fields = recordFields(registry, doctype);
|
|
3092
|
+
const working = resolveTokensInRecord(registry.initializeRecord(registry.resolveSchema(doctype)), fields, registry, now);
|
|
3093
|
+
await applyDoctypeDefaults(working, doctype, run, "doctype defaults", []);
|
|
3094
|
+
await applyLayer(working, registry.getRegisteredDefaults(doctype.slug), fields, run, "registered defaults", [doctype]);
|
|
3095
|
+
Object.assign(working, resolveTokensInRecord(working, fields, registry, now));
|
|
3096
|
+
if (run.problems.length > 0) console.warn(`[stonecrop] A new ${doctype.doctype} opened without some starting values:\n- ${run.problems.join("\n- ")}`, ...run.errors);
|
|
3097
|
+
return { record: working };
|
|
3098
|
+
} finally {
|
|
3099
|
+
clearTimeout(timer);
|
|
3100
|
+
}
|
|
3101
|
+
}
|
|
3102
|
+
//#endregion
|
|
2781
3103
|
//#region src/registry.ts
|
|
2782
3104
|
/**
|
|
2783
3105
|
* Stonecrop Registry class
|
|
@@ -2818,6 +3140,11 @@ var Registry = class Registry {
|
|
|
2818
3140
|
*/
|
|
2819
3141
|
_ancestorIndexDirty = true;
|
|
2820
3142
|
/**
|
|
3143
|
+
* The app's starting values for each doctype, by slug: one registration per doctype (functions allowed).
|
|
3144
|
+
* @internal
|
|
3145
|
+
*/
|
|
3146
|
+
registeredDefaults = /* @__PURE__ */ new Map();
|
|
3147
|
+
/**
|
|
2821
3148
|
* The Vue router instance
|
|
2822
3149
|
* @see {@link https://router.vuejs.org/}
|
|
2823
3150
|
*/
|
|
@@ -2881,9 +3208,7 @@ var Registry = class Registry {
|
|
|
2881
3208
|
if (seen.has(slug)) return (doctype.schema ? doctype.schema.toArray() : []).filter((f) => f.kind === "field").map(({ cardinality: _c, ...rest }) => rest);
|
|
2882
3209
|
seen.add(slug);
|
|
2883
3210
|
const schemaArray = doctype.schema ? doctype.schema.toArray() : [];
|
|
2884
|
-
const
|
|
2885
|
-
if (doctype.links) for (const [key, link] of Object.entries(doctype.links)) linksByFieldname.set(link.fieldname ?? key, link);
|
|
2886
|
-
const result = this.resolveFields(schemaArray, linksByFieldname, seen);
|
|
3211
|
+
const result = this.resolveFields(schemaArray, linksByFieldname(doctype), seen);
|
|
2887
3212
|
seen.delete(slug);
|
|
2888
3213
|
return result;
|
|
2889
3214
|
}
|
|
@@ -2899,30 +3224,26 @@ var Registry = class Registry {
|
|
|
2899
3224
|
if (field.kind === "field" && (linkDecl || field.doctype)) {
|
|
2900
3225
|
const link = linkDecl;
|
|
2901
3226
|
const linkTarget = link?.target ?? field.doctype;
|
|
2902
|
-
|
|
3227
|
+
const expanded = expandedLink(this, field, links);
|
|
3228
|
+
if (!expanded) {
|
|
2903
3229
|
const { cardinality: _c, ...rest } = field;
|
|
2904
|
-
resolved.push({
|
|
3230
|
+
if (!link || resolveLinkRenderMode(link, field.component) === "inline") resolved.push({
|
|
2905
3231
|
...rest,
|
|
2906
3232
|
component: rest.component || "AFormLink",
|
|
2907
3233
|
...linkTarget !== void 0 ? { doctype: linkTarget } : {}
|
|
2908
3234
|
});
|
|
3235
|
+
else resolved.push({ ...rest });
|
|
2909
3236
|
continue;
|
|
2910
3237
|
}
|
|
2911
|
-
const
|
|
2912
|
-
if (!targetDoctype) {
|
|
2913
|
-
const { cardinality: _c, ...rest } = field;
|
|
2914
|
-
resolved.push({ ...rest });
|
|
2915
|
-
continue;
|
|
2916
|
-
}
|
|
2917
|
-
const childSchema = this.resolveSchema(targetDoctype, new Set(visited));
|
|
3238
|
+
const childSchema = this.resolveSchema(expanded.target, new Set(visited));
|
|
2918
3239
|
const { options: _opt, cardinality: _card, kind: _kind, ...fieldRest } = field;
|
|
2919
|
-
if (
|
|
3240
|
+
if (expanded.mode === "table") resolved.push(this.buildTableConfig(field, childSchema, expanded.link.component));
|
|
2920
3241
|
else {
|
|
2921
3242
|
const linkEntry = {
|
|
2922
3243
|
...fieldRest,
|
|
2923
3244
|
kind: "link",
|
|
2924
3245
|
label: fieldRest.label || field.fieldname,
|
|
2925
|
-
component: link.component || fieldRest.component || "AForm",
|
|
3246
|
+
component: expanded.link.component || fieldRest.component || "AForm",
|
|
2926
3247
|
schema: childSchema
|
|
2927
3248
|
};
|
|
2928
3249
|
resolved.push(linkEntry);
|
|
@@ -2984,7 +3305,7 @@ var Registry = class Registry {
|
|
|
2984
3305
|
*
|
|
2985
3306
|
* - `kind: 'table'` or `kind: 'link'` → `[]` or `{}`
|
|
2986
3307
|
* - `kind: 'fieldset'` → its children, at the top level of the record: a fieldset is layout
|
|
2987
|
-
* - `kind: 'field'` →
|
|
3308
|
+
* - `kind: 'field'` → an empty value for the component's category; falls back to `null`
|
|
2988
3309
|
*
|
|
2989
3310
|
* @param schema - The resolved schema array to derive defaults from
|
|
2990
3311
|
* @returns A plain object with default values for each field
|
|
@@ -2996,16 +3317,12 @@ var Registry = class Registry {
|
|
|
2996
3317
|
else if (field.kind === "link") record[field.fieldname] = this.initializeRecord(field.schema);
|
|
2997
3318
|
else if (field.kind === "fieldset") Object.assign(record, this.initializeRecord(field.schema));
|
|
2998
3319
|
else {
|
|
2999
|
-
const
|
|
3000
|
-
if (
|
|
3001
|
-
else
|
|
3002
|
-
|
|
3003
|
-
|
|
3004
|
-
|
|
3005
|
-
else if (category === "boolean") record[field.fieldname] = false;
|
|
3006
|
-
else if (category === "code" && field.language) record[field.fieldname] = field.language === "json" ? {} : "";
|
|
3007
|
-
else record[field.fieldname] = null;
|
|
3008
|
-
}
|
|
3320
|
+
const category = componentCategory(field.component);
|
|
3321
|
+
if (category === "text") record[field.fieldname] = "";
|
|
3322
|
+
else if (category === "number") record[field.fieldname] = 0;
|
|
3323
|
+
else if (category === "boolean") record[field.fieldname] = false;
|
|
3324
|
+
else if (category === "code" && field.language) record[field.fieldname] = field.language === "json" ? {} : "";
|
|
3325
|
+
else record[field.fieldname] = null;
|
|
3009
3326
|
}
|
|
3010
3327
|
return record;
|
|
3011
3328
|
}
|
|
@@ -3089,6 +3406,34 @@ var Registry = class Registry {
|
|
|
3089
3406
|
}
|
|
3090
3407
|
}
|
|
3091
3408
|
}
|
|
3409
|
+
/**
|
|
3410
|
+
* Register the app's starting values for a doctype, by its name or its slug (typically from app bootstrap).
|
|
3411
|
+
* Either one is keyed the way {@link Doctype.slug} is, so `'OrderItem'` and `'order-item'` register for the
|
|
3412
|
+
* same doctype, whether or not it has been added yet.
|
|
3413
|
+
*
|
|
3414
|
+
* They apply over the doctype's own `defaults` for every new record, and a function here runs for each one, so
|
|
3415
|
+
* it can work out what a doctype file cannot (today's date, a value looked up for the user's company). A doctype
|
|
3416
|
+
* takes one registration: a second replaces the first, with a warning.
|
|
3417
|
+
* @public
|
|
3418
|
+
*/
|
|
3419
|
+
registerDefaults(doctype, source) {
|
|
3420
|
+
const slug = toSlug(doctype);
|
|
3421
|
+
if (this.registeredDefaults.has(slug)) console.warn(`[stonecrop] Defaults for "${slug}" were registered again; the new registration replaces the old one.`);
|
|
3422
|
+
this.registeredDefaults.set(slug, source);
|
|
3423
|
+
}
|
|
3424
|
+
/**
|
|
3425
|
+
* @internal
|
|
3426
|
+
*/
|
|
3427
|
+
getRegisteredDefaults(slug) {
|
|
3428
|
+
return this.registeredDefaults.get(slug);
|
|
3429
|
+
}
|
|
3430
|
+
/**
|
|
3431
|
+
* Compose a new record, once, from every starting value. See {@link composeNewRecord}.
|
|
3432
|
+
* @public
|
|
3433
|
+
*/
|
|
3434
|
+
composeNewRecord(doctype, options) {
|
|
3435
|
+
return composeNewRecord(this, doctype, options);
|
|
3436
|
+
}
|
|
3092
3437
|
};
|
|
3093
3438
|
//#endregion
|
|
3094
3439
|
//#region src/plugins/index.ts
|
|
@@ -3575,6 +3920,6 @@ function validateSchema(doctype, schema, registry, workflow, actions) {
|
|
|
3575
3920
|
//#region src/index.ts
|
|
3576
3921
|
var src_default = plugin;
|
|
3577
3922
|
//#endregion
|
|
3578
|
-
export { DRAFT_RECORD_ID, Doctype, FieldTriggerEngine, HST, Registry, SchemaValidator, Stonecrop, ValidationSeverity, createHST, createValidator, src_default as default, executeClientHandler, getGlobalTriggerEngine, isDraftRecordId, markOperationIrreversible, registerGlobalAction, registerTransitionAction, setFieldRollback, triggerTransition, useClientAction, useLazyLink, useOperationLog, useOperationLogStore, useStonecrop, useUndoRedoShortcuts, useValidationStore, validateSchema, withBatch };
|
|
3923
|
+
export { DEFAULTS_TIMEOUT_MS, DRAFT_RECORD_ID, Doctype, FieldTriggerEngine, HST, Registry, SchemaValidator, Stonecrop, ValidationSeverity, composeNewRecord, createHST, createValidator, src_default as default, executeClientHandler, getGlobalTriggerEngine, isDraftRecordId, markOperationIrreversible, registerGlobalAction, registerTransitionAction, setFieldRollback, triggerTransition, useClientAction, useLazyLink, useOperationLog, useOperationLogStore, useStonecrop, useUndoRedoShortcuts, useValidationStore, validateSchema, withBatch };
|
|
3579
3924
|
|
|
3580
3925
|
//# sourceMappingURL=stonecrop.js.map
|