@mapomodule/form 2.0.0-beta.9 → 2.0.0-experimental.2
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/README.md +38 -119
- package/dist/index.d.mts +2 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +1 -0
- package/dist/module.d.mts +1 -0
- package/dist/module.d.ts.map +1 -1
- package/dist/module.json +1 -1
- package/dist/module.mjs +9 -0
- package/dist/runtime/components/MapoFocusPortal.vue +9 -2
- package/dist/runtime/components/MapoFocusPortal.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoForm.vue +28 -6
- package/dist/runtime/components/MapoForm.vue.d.ts +13 -1
- package/dist/runtime/components/MapoForm.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoFormAnnotation.vue +79 -0
- package/dist/runtime/components/MapoFormAnnotation.vue.d.ts +27 -0
- package/dist/runtime/components/MapoFormAnnotation.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoFormAnnotationBadges.vue +28 -0
- package/dist/runtime/components/MapoFormAnnotationBadges.vue.d.ts +13 -0
- package/dist/runtime/components/MapoFormAnnotationBadges.vue.d.ts.map +1 -0
- package/dist/runtime/components/MapoFormField.vue +47 -11
- package/dist/runtime/components/MapoFormField.vue.d.ts +15 -9
- package/dist/runtime/components/MapoFormField.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoFormGroup.vue +18 -2
- package/dist/runtime/components/MapoFormGroup.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoFormTabs.vue +50 -8
- package/dist/runtime/components/MapoFormTabs.vue.d.ts.map +1 -1
- package/dist/runtime/components/fields/MapoMediaField.vue +5 -3
- package/dist/runtime/components/fields/MapoMediaField.vue.d.ts.map +1 -1
- package/dist/runtime/components/fields/MapoMediaM2mField.vue +3 -2
- package/dist/runtime/components/fields/MapoMediaM2mField.vue.d.ts.map +1 -1
- package/dist/runtime/components/fields/MapoRepeater.vue +73 -14
- package/dist/runtime/components/fields/MapoRepeater.vue.d.ts.map +1 -1
- package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue +167 -0
- package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue.d.ts +20 -0
- package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue.d.ts.map +1 -0
- package/dist/runtime/components/fields/MapoRepeaterItem.vue +46 -10
- package/dist/runtime/components/fields/MapoRepeaterItem.vue.d.ts +6 -2
- package/dist/runtime/components/fields/MapoRepeaterItem.vue.d.ts.map +1 -1
- package/dist/runtime/components/fields/MapoWygEditor.vue +19 -13
- package/dist/runtime/components/fields/MapoWygEditor.vue.d.ts.map +1 -1
- package/dist/runtime/composables/useFieldAnnotation.d.ts +18 -0
- package/dist/runtime/composables/useFieldAnnotation.d.ts.map +1 -0
- package/dist/runtime/composables/useFieldAnnotation.js +16 -0
- package/dist/runtime/composables/useFocusMode.d.ts +3 -1
- package/dist/runtime/composables/useFocusMode.d.ts.map +1 -1
- package/dist/runtime/composables/useFormFromSchema.d.ts +23 -1
- package/dist/runtime/composables/useFormFromSchema.d.ts.map +1 -1
- package/dist/runtime/composables/useFormFromSchema.js +171 -32
- package/dist/runtime/composables/useMapoForm.d.ts +21 -3
- package/dist/runtime/composables/useMapoForm.d.ts.map +1 -1
- package/dist/runtime/composables/useMapoForm.js +49 -15
- package/dist/runtime/types/annotation.d.ts +43 -0
- package/dist/runtime/types/annotation.d.ts.map +1 -0
- package/dist/runtime/types/annotation.js +0 -0
- package/dist/runtime/types/descriptor.d.ts +81 -4
- package/dist/runtime/types/descriptor.d.ts.map +1 -1
- package/dist/runtime/types/index.d.ts +2 -1
- package/dist/runtime/types/index.d.ts.map +1 -1
- package/dist/runtime/utils/annotations.d.ts +40 -0
- package/dist/runtime/utils/annotations.d.ts.map +1 -0
- package/dist/runtime/utils/annotations.js +78 -0
- package/dist/runtime/utils/draft.d.ts +21 -0
- package/dist/runtime/utils/draft.d.ts.map +1 -0
- package/dist/runtime/utils/draft.js +108 -0
- package/dist/runtime/utils/fieldComponent.d.ts +9 -0
- package/dist/runtime/utils/fieldComponent.d.ts.map +1 -0
- package/dist/runtime/utils/fieldComponent.js +12 -0
- package/dist/runtime/utils/fieldFocus.d.ts +24 -0
- package/dist/runtime/utils/fieldFocus.d.ts.map +1 -0
- package/dist/runtime/utils/fieldFocus.js +41 -0
- package/dist/runtime/utils/fieldsToJsonSchema.d.ts +38 -0
- package/dist/runtime/utils/fieldsToJsonSchema.d.ts.map +1 -0
- package/dist/runtime/utils/fieldsToJsonSchema.js +248 -0
- package/dist/runtime/utils/repeaterTemplates.d.ts +26 -0
- package/dist/runtime/utils/repeaterTemplates.d.ts.map +1 -0
- package/dist/runtime/utils/repeaterTemplates.js +51 -0
- package/dist/runtime/utils/reveal.d.ts +34 -0
- package/dist/runtime/utils/reveal.d.ts.map +1 -0
- package/dist/runtime/utils/reveal.js +29 -0
- package/dist/runtime/utils/tabVisibility.d.ts +18 -0
- package/dist/runtime/utils/tabVisibility.d.ts.map +1 -0
- package/dist/runtime/utils/tabVisibility.js +18 -0
- package/package.json +5 -5
- package/src/__tests__/MapoFocusPortal.component.test.ts +97 -0
- package/src/__tests__/MapoForm.component.test.ts +79 -0
- package/src/__tests__/MapoFormAnnotation.component.test.ts +244 -0
- package/src/__tests__/MapoFormField.component.test.ts +508 -0
- package/src/__tests__/MapoFormGroup.component.test.ts +141 -0
- package/src/__tests__/MapoFormTabs.component.test.ts +254 -0
- package/src/__tests__/MapoRepeaterItem.component.test.ts +286 -0
- package/src/__tests__/MapoWygEditor.component.test.ts +95 -0
- package/src/__tests__/annotations.test.ts +227 -0
- package/src/__tests__/draft.test.ts +221 -0
- package/src/__tests__/fieldComponent.test.ts +31 -0
- package/src/__tests__/fieldExtensions.test.ts +63 -0
- package/src/__tests__/fieldFocus.test.ts +178 -0
- package/src/__tests__/fieldsToJsonSchema.test.ts +630 -0
- package/src/__tests__/headlessRenderer.ts +85 -0
- package/src/__tests__/repeaterTemplates.test.ts +154 -0
- package/src/__tests__/reveal.test.ts +89 -0
- package/src/__tests__/schemaSources.test.ts +132 -0
- package/src/__tests__/tabVisibility.test.ts +102 -0
- package/src/__tests__/useFormFromSchema.test.ts +387 -0
- package/src/__tests__/useMapoForm.test.ts +213 -2
- package/src/index.ts +9 -0
- package/src/module.ts +9 -0
- package/src/runtime/components/MapoFocusPortal.vue +15 -2
- package/src/runtime/components/MapoForm.vue +47 -4
- package/src/runtime/components/MapoFormAnnotation.vue +113 -0
- package/src/runtime/components/MapoFormAnnotationBadges.vue +41 -0
- package/src/runtime/components/MapoFormField.vue +72 -11
- package/src/runtime/components/MapoFormGroup.vue +20 -2
- package/src/runtime/components/MapoFormTabs.vue +64 -8
- package/src/runtime/components/fields/MapoMediaField.vue +5 -3
- package/src/runtime/components/fields/MapoMediaM2mField.vue +3 -2
- package/src/runtime/components/fields/MapoRepeater.vue +82 -14
- package/src/runtime/components/fields/MapoRepeaterBlockPicker.vue +188 -0
- package/src/runtime/components/fields/MapoRepeaterItem.vue +56 -8
- package/src/runtime/components/fields/MapoWygEditor.vue +22 -14
- package/src/runtime/composables/useFieldAnnotation.ts +38 -0
- package/src/runtime/composables/useFocusMode.ts +7 -1
- package/src/runtime/composables/useFormFromSchema.ts +303 -41
- package/src/runtime/composables/useMapoForm.ts +103 -23
- package/src/runtime/types/annotation.ts +53 -0
- package/src/runtime/types/descriptor.ts +94 -4
- package/src/runtime/types/index.ts +10 -0
- package/src/runtime/utils/annotations.ts +169 -0
- package/src/runtime/utils/draft.ts +158 -0
- package/src/runtime/utils/fieldComponent.ts +34 -0
- package/src/runtime/utils/fieldFocus.ts +73 -0
- package/src/runtime/utils/fieldsToJsonSchema.ts +405 -0
- package/src/runtime/utils/repeaterTemplates.ts +103 -0
- package/src/runtime/utils/reveal.ts +68 -0
- package/src/runtime/utils/tabVisibility.ts +44 -0
- package/dist/__tests__/__mocks__/nuxt-imports.d.ts +0 -4
- package/dist/__tests__/__mocks__/nuxt-imports.d.ts.map +0 -1
- package/dist/__tests__/camomillaRoundTrip.test.d.ts +0 -2
- package/dist/__tests__/camomillaRoundTrip.test.d.ts.map +0 -1
- package/dist/__tests__/registry.test.d.ts +0 -2
- package/dist/__tests__/registry.test.d.ts.map +0 -1
- package/dist/__tests__/useFormFromSchema.test.d.ts +0 -2
- package/dist/__tests__/useFormFromSchema.test.d.ts.map +0 -1
- package/dist/__tests__/useMapoForm.test.d.ts +0 -2
- package/dist/__tests__/useMapoForm.test.d.ts.map +0 -1
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { ATTRS_KEYWORD, FIELD_KEYWORD } from "@mapomodule/utils";
|
|
2
|
+
import {
|
|
3
|
+
KEYWORD_ATTRS,
|
|
4
|
+
TEMPLATE_PROPERTY,
|
|
5
|
+
isUnsafeAttr,
|
|
6
|
+
schemaFieldType
|
|
7
|
+
} from "../composables/useFormFromSchema.js";
|
|
8
|
+
const VALUE_SCHEMAS = {
|
|
9
|
+
text: { type: "string" },
|
|
10
|
+
textarea: { type: "string", format: "textarea" },
|
|
11
|
+
email: { type: "string", format: "email" },
|
|
12
|
+
url: { type: "string", format: "uri" },
|
|
13
|
+
number: { type: "number" },
|
|
14
|
+
slider: { type: "number" },
|
|
15
|
+
boolean: { type: "boolean" },
|
|
16
|
+
switch: { type: "boolean" },
|
|
17
|
+
color: { type: "string", format: "color" },
|
|
18
|
+
file: { type: "string", format: "binary" },
|
|
19
|
+
date: { type: "string", format: "date" },
|
|
20
|
+
time: { type: "string", format: "time" },
|
|
21
|
+
datetime: { type: "string", format: "date-time" },
|
|
22
|
+
editor: { type: "string", contentMediaType: "text/html" },
|
|
23
|
+
// What the seo accessor stores.
|
|
24
|
+
seo: {
|
|
25
|
+
type: "object",
|
|
26
|
+
properties: {
|
|
27
|
+
title: { type: "string" },
|
|
28
|
+
description: { type: "string" },
|
|
29
|
+
permalink: { type: "string" }
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
map: {
|
|
33
|
+
type: "object",
|
|
34
|
+
properties: { lat: { type: "number" }, lng: { type: "number" } }
|
|
35
|
+
},
|
|
36
|
+
media: { type: "object" },
|
|
37
|
+
"media-m2m": { type: "array", items: { type: "object" } },
|
|
38
|
+
"enhanced-media": {
|
|
39
|
+
type: "object",
|
|
40
|
+
properties: {
|
|
41
|
+
media: { type: "object" },
|
|
42
|
+
alt: { type: "string" },
|
|
43
|
+
caption: { type: "string" }
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
const SKIP = Symbol("skip");
|
|
48
|
+
function toPlain(value, ancestors = /* @__PURE__ */ new Set()) {
|
|
49
|
+
if (value === null) return null;
|
|
50
|
+
switch (typeof value) {
|
|
51
|
+
case "string":
|
|
52
|
+
case "boolean":
|
|
53
|
+
return value;
|
|
54
|
+
case "number":
|
|
55
|
+
return Number.isFinite(value) ? value : SKIP;
|
|
56
|
+
case "object": {
|
|
57
|
+
if (ancestors.has(value)) return SKIP;
|
|
58
|
+
ancestors.add(value);
|
|
59
|
+
const plain = toPlainObject(value, ancestors);
|
|
60
|
+
ancestors.delete(value);
|
|
61
|
+
return plain;
|
|
62
|
+
}
|
|
63
|
+
default:
|
|
64
|
+
return SKIP;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function toPlainObject(value, ancestors) {
|
|
68
|
+
if (Array.isArray(value)) {
|
|
69
|
+
const items = value.map((item) => toPlain(item, ancestors));
|
|
70
|
+
return items.includes(SKIP) ? SKIP : items;
|
|
71
|
+
}
|
|
72
|
+
const proto = Object.getPrototypeOf(value);
|
|
73
|
+
if (proto !== Object.prototype && proto !== null) return SKIP;
|
|
74
|
+
const entries = [];
|
|
75
|
+
for (const [key, entry] of Object.entries(value)) {
|
|
76
|
+
if (entry === void 0) continue;
|
|
77
|
+
const plain = toPlain(entry, ancestors);
|
|
78
|
+
if (plain === SKIP) return SKIP;
|
|
79
|
+
entries.push([key, plain]);
|
|
80
|
+
}
|
|
81
|
+
return Object.fromEntries(entries);
|
|
82
|
+
}
|
|
83
|
+
function plainEntries(record) {
|
|
84
|
+
const entries = Object.entries(record).filter(([key]) => !isUnsafeAttr(key)).map(([key, value]) => [key, toPlain(value)]).filter(([, value]) => value !== SKIP);
|
|
85
|
+
return entries.length ? Object.fromEntries(entries) : void 0;
|
|
86
|
+
}
|
|
87
|
+
function take(attrs, name) {
|
|
88
|
+
const value = attrs[name];
|
|
89
|
+
delete attrs[name];
|
|
90
|
+
return value;
|
|
91
|
+
}
|
|
92
|
+
function sharedType(values) {
|
|
93
|
+
const types = new Set(
|
|
94
|
+
values.map((value) => value === null ? "null" : typeof value)
|
|
95
|
+
);
|
|
96
|
+
const [only] = [...types];
|
|
97
|
+
const isShared = types.size === 1;
|
|
98
|
+
if (!isShared) return void 0;
|
|
99
|
+
return only === "string" || only === "number" || only === "boolean" ? only : void 0;
|
|
100
|
+
}
|
|
101
|
+
function toChoice(item) {
|
|
102
|
+
if (typeof item !== "object" || item === null)
|
|
103
|
+
return { value: item, label: String(item) };
|
|
104
|
+
const { text, label, value, ...rest } = item;
|
|
105
|
+
return {
|
|
106
|
+
value: toPlain(value),
|
|
107
|
+
label: String(text ?? label ?? value),
|
|
108
|
+
extras: plainEntries(rest)
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
function choicesSchema(list) {
|
|
112
|
+
if (!Array.isArray(list)) return {};
|
|
113
|
+
const choices = list.map(toChoice).filter((choice) => choice.value !== SKIP);
|
|
114
|
+
if (choices.length === 0) return {};
|
|
115
|
+
const values = choices.map((choice) => choice.value);
|
|
116
|
+
const type = sharedType(values);
|
|
117
|
+
const isLabeled = choices.some(
|
|
118
|
+
(choice) => choice.label !== String(choice.value) || choice.extras
|
|
119
|
+
);
|
|
120
|
+
return {
|
|
121
|
+
...type ? { type } : {},
|
|
122
|
+
...isLabeled ? {
|
|
123
|
+
oneOf: choices.map((choice) => ({
|
|
124
|
+
const: choice.value,
|
|
125
|
+
title: choice.label,
|
|
126
|
+
...choice.extras ? { [ATTRS_KEYWORD]: choice.extras } : {}
|
|
127
|
+
}))
|
|
128
|
+
} : { enum: values }
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
function selectSchema(attrs) {
|
|
132
|
+
const items = take(attrs, "items");
|
|
133
|
+
const options = take(attrs, "options");
|
|
134
|
+
const choices = choicesSchema(items ?? options);
|
|
135
|
+
const isMultiple = take(attrs, "multiple") === true;
|
|
136
|
+
return isMultiple ? { type: "array", items: choices, uniqueItems: true } : choices;
|
|
137
|
+
}
|
|
138
|
+
function relationSchema(type, attrs) {
|
|
139
|
+
const endpoint = take(attrs, "endpoint");
|
|
140
|
+
const isMultiple = take(attrs, "multiple") === true || type === "m2m";
|
|
141
|
+
return {
|
|
142
|
+
type: "relation",
|
|
143
|
+
...typeof endpoint === "string" ? { endpoint } : {},
|
|
144
|
+
...isMultiple ? { multiple: true } : {}
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
function templateSchema(key, template, ctx) {
|
|
148
|
+
const normalized = Array.isArray(template) ? { fields: template } : template;
|
|
149
|
+
const { fields, label, description, ...presentation } = normalized;
|
|
150
|
+
const body = objectSchema(fields, ctx);
|
|
151
|
+
const own = plainEntries(presentation);
|
|
152
|
+
return {
|
|
153
|
+
type: "object",
|
|
154
|
+
...label ? { title: label } : {},
|
|
155
|
+
...description ? { description } : {},
|
|
156
|
+
properties: {
|
|
157
|
+
[TEMPLATE_PROPERTY]: { const: key },
|
|
158
|
+
...body.properties
|
|
159
|
+
},
|
|
160
|
+
required: [TEMPLATE_PROPERTY, ...body.required ?? []],
|
|
161
|
+
...own ? { [ATTRS_KEYWORD]: own } : {}
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
function repeaterSchema(field, attrs, ctx) {
|
|
165
|
+
const templates = take(attrs, "templates");
|
|
166
|
+
if (ctx.path.has(field)) {
|
|
167
|
+
return { type: "array", items: { type: "object", properties: {} } };
|
|
168
|
+
}
|
|
169
|
+
const inner = { ...ctx, path: /* @__PURE__ */ new Set([...ctx.path, field]) };
|
|
170
|
+
const hasTemplates = !!templates && Object.keys(templates).length > 0;
|
|
171
|
+
if (!hasTemplates) {
|
|
172
|
+
const fields = field.fields ?? [];
|
|
173
|
+
return { type: "array", items: objectSchema(fields, inner) };
|
|
174
|
+
}
|
|
175
|
+
return {
|
|
176
|
+
type: "array",
|
|
177
|
+
items: {
|
|
178
|
+
oneOf: Object.entries(templates).map(
|
|
179
|
+
([key, template]) => templateSchema(key, template, inner)
|
|
180
|
+
),
|
|
181
|
+
discriminator: { propertyName: TEMPLATE_PROPERTY }
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
function valueSchema(field, attrs, ctx) {
|
|
186
|
+
const type = String(field.type);
|
|
187
|
+
const keywords = {};
|
|
188
|
+
for (const [attr, keyword] of Object.entries(KEYWORD_ATTRS[type] ?? {})) {
|
|
189
|
+
const value = toPlain(take(attrs, attr));
|
|
190
|
+
if (value !== SKIP) keywords[keyword] = value;
|
|
191
|
+
}
|
|
192
|
+
let schema;
|
|
193
|
+
if (type === "select") schema = selectSchema(attrs);
|
|
194
|
+
else if (type === "fks" || type === "m2m")
|
|
195
|
+
schema = relationSchema(type, attrs);
|
|
196
|
+
else if (type === "repeater") schema = repeaterSchema(field, attrs, ctx);
|
|
197
|
+
else if (VALUE_SCHEMAS[type]) schema = structuredClone(VALUE_SCHEMAS[type]);
|
|
198
|
+
else if (type.startsWith("object:")) schema = { type: "object" };
|
|
199
|
+
else schema = {};
|
|
200
|
+
return { ...schema, ...keywords };
|
|
201
|
+
}
|
|
202
|
+
function fieldSchema(field, ctx) {
|
|
203
|
+
const attrs = {
|
|
204
|
+
...field.attrs ?? {}
|
|
205
|
+
};
|
|
206
|
+
const schema = {
|
|
207
|
+
...field.label ? { title: field.label } : {},
|
|
208
|
+
...valueSchema(field, attrs, ctx),
|
|
209
|
+
...field.readonly ? { readOnly: true } : {},
|
|
210
|
+
// A field kept out of drafts holds a secret, such as a password: JSON
|
|
211
|
+
// Schema marks a value the backend never sends back as writeOnly.
|
|
212
|
+
...field.noDraft ? { writeOnly: true } : {},
|
|
213
|
+
...field.translatable ? { translatable: true } : {}
|
|
214
|
+
};
|
|
215
|
+
const rest = plainEntries(attrs);
|
|
216
|
+
if (rest) schema[ATTRS_KEYWORD] = rest;
|
|
217
|
+
return schema;
|
|
218
|
+
}
|
|
219
|
+
function markType(schema, field) {
|
|
220
|
+
const type = String(field.type);
|
|
221
|
+
const isMarked = schema[FIELD_KEYWORD] !== void 0;
|
|
222
|
+
if (isMarked) return schema;
|
|
223
|
+
const readsBackAs = schemaFieldType(schema, String(field.key));
|
|
224
|
+
return readsBackAs === type ? schema : { ...schema, [FIELD_KEYWORD]: type };
|
|
225
|
+
}
|
|
226
|
+
function objectSchema(fields, ctx) {
|
|
227
|
+
const properties = [];
|
|
228
|
+
const required = [];
|
|
229
|
+
const written = /* @__PURE__ */ new Set();
|
|
230
|
+
for (const field of fields) {
|
|
231
|
+
const key = String(field.key);
|
|
232
|
+
if (written.has(key)) continue;
|
|
233
|
+
const schema = fieldSchema(field, ctx);
|
|
234
|
+
const final = ctx.options.property ? ctx.options.property(schema, field) : schema;
|
|
235
|
+
if (!final) continue;
|
|
236
|
+
written.add(key);
|
|
237
|
+
properties.push([key, markType(final, field)]);
|
|
238
|
+
if (field.required) required.push(key);
|
|
239
|
+
}
|
|
240
|
+
return {
|
|
241
|
+
type: "object",
|
|
242
|
+
properties: Object.fromEntries(properties),
|
|
243
|
+
...required.length ? { required } : {}
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
export function fieldsToJsonSchema(fields, options = {}) {
|
|
247
|
+
return objectSchema(fields, { options, path: /* @__PURE__ */ new Set() });
|
|
248
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { AnyFieldDescriptor, NormalizedRepeaterTemplate, RepeaterDescriptor, RepeaterImageFit } from "../types/descriptor.js";
|
|
2
|
+
/** Above this many templates, `picker: "auto"` switches from the dropdown to the modal. */
|
|
3
|
+
export declare const MODAL_PICKER_THRESHOLD = 3;
|
|
4
|
+
/**
|
|
5
|
+
* Turns `attrs.templates` into a list with defaults applied. A bare field array
|
|
6
|
+
* becomes a template whose label is its key. `imageFit` falls back to the
|
|
7
|
+
* repeater's `templateImageFit`, then to `contain`, so a preview is never cropped
|
|
8
|
+
* unless someone asks for it.
|
|
9
|
+
*/
|
|
10
|
+
export declare function normalizeTemplates(templates: NonNullable<RepeaterDescriptor["attrs"]>["templates"], defaultImageFit?: RepeaterImageFit): NormalizedRepeaterTemplate[];
|
|
11
|
+
/** Whether the block picker modal replaces the dropdown for this many templates. */
|
|
12
|
+
export declare function shouldUseModalPicker(picker: "auto" | "select" | "modal" | undefined, templateCount: number): boolean;
|
|
13
|
+
/** Filters templates by category and by a case-insensitive text query. */
|
|
14
|
+
export declare function filterTemplates(templates: NormalizedRepeaterTemplate[], query: string, category: string): NormalizedRepeaterTemplate[];
|
|
15
|
+
export type WireframeShape = "line" | "lines" | "box" | "check";
|
|
16
|
+
/** One schematic shape per field type, capped at 5 so it fits a picker card. */
|
|
17
|
+
export declare function wireframeShapes(fields: AnyFieldDescriptor[]): WireframeShape[];
|
|
18
|
+
/** True when adding one more item reaches `maxItems`. */
|
|
19
|
+
export declare function reachesMax(current: number, maxItems?: number): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* The fields an item renders: those of its template, picked by `item._template`
|
|
22
|
+
* or, for an item without one, by `fallbackKey`; the repeater's own `fields`
|
|
23
|
+
* when it has no templates or the key matches none.
|
|
24
|
+
*/
|
|
25
|
+
export declare function repeaterItemFields(descriptor: RepeaterDescriptor, templates: NormalizedRepeaterTemplate[], item: Record<string, unknown>, fallbackKey: string | undefined): AnyFieldDescriptor[];
|
|
26
|
+
//# sourceMappingURL=repeaterTemplates.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"repeaterTemplates.d.ts","sourceRoot":"","sources":["../../../src/runtime/utils/repeaterTemplates.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,kBAAkB,EAClB,0BAA0B,EAC1B,kBAAkB,EAClB,gBAAgB,EAEjB,MAAM,wBAAwB,CAAC;AAEhC,2FAA2F;AAC3F,eAAO,MAAM,sBAAsB,IAAI,CAAC;AAExC;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAChC,SAAS,EAAE,WAAW,CAAC,kBAAkB,CAAC,OAAO,CAAC,CAAC,CAAC,WAAW,CAAC,EAChE,eAAe,GAAE,gBAA4B,GAC5C,0BAA0B,EAAE,CAe9B;AAED,oFAAoF;AACpF,wBAAgB,oBAAoB,CAClC,MAAM,EAAE,MAAM,GAAG,QAAQ,GAAG,OAAO,GAAG,SAAS,EAC/C,aAAa,EAAE,MAAM,GACpB,OAAO,CAOT;AAED,0EAA0E;AAC1E,wBAAgB,eAAe,CAC7B,SAAS,EAAE,0BAA0B,EAAE,EACvC,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,MAAM,GACf,0BAA0B,EAAE,CAS9B;AAED,MAAM,MAAM,cAAc,GAAG,MAAM,GAAG,OAAO,GAAG,KAAK,GAAG,OAAO,CAAC;AAEhE,gFAAgF;AAChF,wBAAgB,eAAe,CAC7B,MAAM,EAAE,kBAAkB,EAAE,GAC3B,cAAc,EAAE,CAQlB;AAED,yDAAyD;AACzD,wBAAgB,UAAU,CAAC,OAAO,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAEtE;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAChC,UAAU,EAAE,kBAAkB,EAC9B,SAAS,EAAE,0BAA0B,EAAE,EACvC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,WAAW,EAAE,MAAM,GAAG,SAAS,GAC9B,kBAAkB,EAAE,CAMtB"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export const MODAL_PICKER_THRESHOLD = 3;
|
|
2
|
+
export function normalizeTemplates(templates, defaultImageFit = "contain") {
|
|
3
|
+
return Object.entries(templates ?? {}).map(([key, tpl]) => {
|
|
4
|
+
const meta = Array.isArray(tpl) ? {} : tpl;
|
|
5
|
+
return {
|
|
6
|
+
key,
|
|
7
|
+
label: meta.label ?? key,
|
|
8
|
+
description: meta.description,
|
|
9
|
+
icon: meta.icon,
|
|
10
|
+
image: meta.image,
|
|
11
|
+
imageFit: meta.imageFit ?? defaultImageFit,
|
|
12
|
+
wireframe: meta.wireframe,
|
|
13
|
+
category: meta.category,
|
|
14
|
+
fields: Array.isArray(tpl) ? tpl : tpl.fields ?? []
|
|
15
|
+
};
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
export function shouldUseModalPicker(picker, templateCount) {
|
|
19
|
+
if (templateCount === 0) return false;
|
|
20
|
+
const mode = picker ?? "auto";
|
|
21
|
+
return mode === "modal" || mode === "auto" && templateCount > MODAL_PICKER_THRESHOLD;
|
|
22
|
+
}
|
|
23
|
+
export function filterTemplates(templates, query, category) {
|
|
24
|
+
const q = query.trim().toLowerCase();
|
|
25
|
+
return templates.filter((tpl) => {
|
|
26
|
+
if (category && tpl.category !== category) return false;
|
|
27
|
+
if (!q) return true;
|
|
28
|
+
return [tpl.label, tpl.description, tpl.category].some(
|
|
29
|
+
(v) => v?.toLowerCase().includes(q)
|
|
30
|
+
);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
export function wireframeShapes(fields) {
|
|
34
|
+
return fields.slice(0, 5).map((f) => {
|
|
35
|
+
const type = String(f.type);
|
|
36
|
+
if (type === "textarea" || type === "editor") return "lines";
|
|
37
|
+
if (type.includes("media") || type === "file") return "box";
|
|
38
|
+
if (type === "boolean") return "check";
|
|
39
|
+
return "line";
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
export function reachesMax(current, maxItems) {
|
|
43
|
+
return !!maxItems && current + 1 >= maxItems;
|
|
44
|
+
}
|
|
45
|
+
export function repeaterItemFields(descriptor, templates, item, fallbackKey) {
|
|
46
|
+
const ownFields = descriptor.fields ?? [];
|
|
47
|
+
const hasTemplates = templates.length > 0;
|
|
48
|
+
if (!hasTemplates) return ownFields;
|
|
49
|
+
const key = item._template ?? fallbackKey;
|
|
50
|
+
return templates.find((tpl) => tpl.key === key)?.fields ?? ownFields;
|
|
51
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { AnyFieldDescriptor } from "../types/index.js";
|
|
2
|
+
import type { TabEntry } from "../types/layout.js";
|
|
3
|
+
/**
|
|
4
|
+
* A request of `revealField()`, which the parts of a form act on: the tab,
|
|
5
|
+
* the group and the repeater item that hold the field open, and the field
|
|
6
|
+
* puts the focus on its control.
|
|
7
|
+
*/
|
|
8
|
+
export interface FieldRevealRequest {
|
|
9
|
+
/** The path asked, in the form that holds the field: `title`, `translations.it.title`, `blocks.0.title`. */
|
|
10
|
+
readonly path: string;
|
|
11
|
+
/** The key of the form's field the path names, or holds a value of. */
|
|
12
|
+
readonly key: string;
|
|
13
|
+
/** The rest of the path inside that field's value: empty for the field itself, `0.title` in `blocks`. */
|
|
14
|
+
readonly rest: string;
|
|
15
|
+
/** Ends the request: `true` once the control has the focus. A second call does nothing. */
|
|
16
|
+
readonly settle: (focused: boolean) => void;
|
|
17
|
+
}
|
|
18
|
+
/** The field a path names, and what of the path is inside its value. */
|
|
19
|
+
export interface FieldAtPath {
|
|
20
|
+
field: AnyFieldDescriptor;
|
|
21
|
+
/** Empty for the field itself; `2.author` for `blocks.2.author` in the field `blocks`. */
|
|
22
|
+
rest: string;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The field of `fields` whose value `path` is, or is inside: the longest key
|
|
26
|
+
* that matches, so `seo.title` wins over `seo`. A translatable field answers
|
|
27
|
+
* to the path of its value in `lang` and to its bare key, as its mark does.
|
|
28
|
+
*/
|
|
29
|
+
export declare function fieldAtPath(fields: readonly AnyFieldDescriptor[], path: string, lang: string): FieldAtPath | undefined;
|
|
30
|
+
/** The fields of tabs: their groups, the groups' sub-tabs, and their child tabs. */
|
|
31
|
+
export declare function fieldsOfTabs(tabs: readonly TabEntry[]): AnyFieldDescriptor[];
|
|
32
|
+
/** Whether a tab, or one of its child tabs, holds the field of `key`. */
|
|
33
|
+
export declare function tabHoldsField(tab: TabEntry, key: string): boolean;
|
|
34
|
+
//# sourceMappingURL=reveal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reveal.d.ts","sourceRoot":"","sources":["../../../src/runtime/utils/reveal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAGnD;;;;GAIG;AACH,MAAM,WAAW,kBAAkB;IACjC,4GAA4G;IAC5G,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,uEAAuE;IACvE,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,yGAAyG;IACzG,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,2FAA2F;IAC3F,QAAQ,CAAC,MAAM,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,CAAC;CAC7C;AAED,wEAAwE;AACxE,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,kBAAkB,CAAC;IAC1B,0FAA0F;IAC1F,IAAI,EAAE,MAAM,CAAC;CACd;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CACzB,MAAM,EAAE,SAAS,kBAAkB,EAAE,EACrC,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,GACX,WAAW,GAAG,SAAS,CAezB;AAED,oFAAoF;AACpF,wBAAgB,YAAY,CAAC,IAAI,EAAE,SAAS,QAAQ,EAAE,GAAG,kBAAkB,EAAE,CAQ5E;AAED,yEAAyE;AACzE,wBAAgB,aAAa,CAAC,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAEjE"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { valuePath } from "./annotations.js";
|
|
2
|
+
export function fieldAtPath(fields, path, lang) {
|
|
3
|
+
let found;
|
|
4
|
+
let longest = -1;
|
|
5
|
+
for (const field of fields) {
|
|
6
|
+
const key = field.key;
|
|
7
|
+
for (const candidate of /* @__PURE__ */ new Set([valuePath(field, lang), key])) {
|
|
8
|
+
const isField = path === candidate;
|
|
9
|
+
const isInside = path.startsWith(`${candidate}.`);
|
|
10
|
+
if (!isField && !isInside) continue;
|
|
11
|
+
if (candidate.length <= longest) continue;
|
|
12
|
+
longest = candidate.length;
|
|
13
|
+
found = { field, rest: isField ? "" : path.slice(candidate.length + 1) };
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return found;
|
|
17
|
+
}
|
|
18
|
+
export function fieldsOfTabs(tabs) {
|
|
19
|
+
return tabs.flatMap((tab) => [
|
|
20
|
+
...[...tab.groups.values()].flatMap((group) => [
|
|
21
|
+
...group.fields,
|
|
22
|
+
...[...group.subtabs.values()].flat()
|
|
23
|
+
]),
|
|
24
|
+
...fieldsOfTabs([...tab.children.values()])
|
|
25
|
+
]);
|
|
26
|
+
}
|
|
27
|
+
export function tabHoldsField(tab, key) {
|
|
28
|
+
return fieldsOfTabs([tab]).some((field) => field.key === key);
|
|
29
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { AnyFieldDescriptor } from "../types/index.js";
|
|
2
|
+
import type { TabEntry } from "../types/layout.js";
|
|
3
|
+
/**
|
|
4
|
+
* Whether a field shows for this model: neither its `visible` predicate, which
|
|
5
|
+
* removes it, nor its `show` predicate, which hides it, keeps it out.
|
|
6
|
+
*/
|
|
7
|
+
export declare function isFieldShown(field: AnyFieldDescriptor, model: Record<string, unknown>): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Whether a tab has a field to show for this model, in one of its groups (sub-tabs
|
|
10
|
+
* included) or in one of its child tabs.
|
|
11
|
+
*/
|
|
12
|
+
export declare function tabHasShownFields(tab: TabEntry, model: Record<string, unknown>): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* The tab to open among the ones shown: the tab the user picked, while it is
|
|
15
|
+
* shown; otherwise the first one. Empty when no tab is shown.
|
|
16
|
+
*/
|
|
17
|
+
export declare function pickActiveTab(shownTabs: TabEntry[], picked: string | null): string;
|
|
18
|
+
//# sourceMappingURL=tabVisibility.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tabVisibility.d.ts","sourceRoot":"","sources":["../../../src/runtime/utils/tabVisibility.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAEnD;;;GAGG;AACH,wBAAgB,YAAY,CAC1B,KAAK,EAAE,kBAAkB,EACzB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC7B,OAAO,CAIT;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAC/B,GAAG,EAAE,QAAQ,EACb,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC7B,OAAO,CAQT;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAC3B,SAAS,EAAE,QAAQ,EAAE,EACrB,MAAM,EAAE,MAAM,GAAG,IAAI,GACpB,MAAM,CAGR"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export function isFieldShown(field, model) {
|
|
2
|
+
const removed = field.visible ? !field.visible({ model }) : false;
|
|
3
|
+
const hidden = field.show ? !field.show({ model }) : false;
|
|
4
|
+
return !removed && !hidden;
|
|
5
|
+
}
|
|
6
|
+
export function tabHasShownFields(tab, model) {
|
|
7
|
+
for (const group of tab.groups.values()) {
|
|
8
|
+
const fields = [...group.fields, ...[...group.subtabs.values()].flat()];
|
|
9
|
+
if (fields.some((field) => isFieldShown(field, model))) return true;
|
|
10
|
+
}
|
|
11
|
+
return [...tab.children.values()].some(
|
|
12
|
+
(child) => tabHasShownFields(child, model)
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
export function pickActiveTab(shownTabs, picked) {
|
|
16
|
+
const pickedTab = shownTabs.find((tab) => tab.name === picked);
|
|
17
|
+
return (pickedTab ?? shownTabs[0])?.name ?? "";
|
|
18
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mapomodule/form",
|
|
3
|
-
"version": "2.0.0-
|
|
3
|
+
"version": "2.0.0-experimental.2",
|
|
4
4
|
"description": "Mapo declarative typed form engine and field components",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -47,9 +47,9 @@
|
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
49
|
"@internationalized/date": "^3.7.0",
|
|
50
|
-
"@mapomodule/i18n": "2.0.0-
|
|
51
|
-
"@mapomodule/store": "2.0.0-
|
|
52
|
-
"@mapomodule/utils": "2.0.0-
|
|
50
|
+
"@mapomodule/i18n": "2.0.0-experimental.2",
|
|
51
|
+
"@mapomodule/store": "2.0.0-experimental.1",
|
|
52
|
+
"@mapomodule/utils": "2.0.0-experimental.1",
|
|
53
53
|
"@tiptap/extension-image": "^2.11.5",
|
|
54
54
|
"@tiptap/extension-link": "^2.11.5",
|
|
55
55
|
"@tiptap/extension-underline": "^2.11.5",
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
"@tiptap/vue-3": "^2.11.5",
|
|
58
58
|
"leaflet": "^1.9.4",
|
|
59
59
|
"vue-draggable-plus": "^0.6.0",
|
|
60
|
-
"vue-i18n": "^11.4.
|
|
60
|
+
"vue-i18n": "^11.4.8"
|
|
61
61
|
},
|
|
62
62
|
"optionalDependencies": {
|
|
63
63
|
"@types/leaflet": "^1.9.18"
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { computed, h, ref } from "vue";
|
|
3
|
+
import type { App, Component, SetupContext } from "vue";
|
|
4
|
+
|
|
5
|
+
import MapoFocusPortal from "../runtime/components/MapoFocusPortal.vue";
|
|
6
|
+
import { useFocusMode } from "../runtime/composables/useFocusMode.js";
|
|
7
|
+
import type { FieldAnnotations } from "../runtime/types/index.js";
|
|
8
|
+
import { createApp, headlessNode } from "./headlessRenderer.js";
|
|
9
|
+
import type { HeadlessNode } from "./headlessRenderer.js";
|
|
10
|
+
|
|
11
|
+
// The portal renders the item with `MapoForm`: the stub keeps the props it
|
|
12
|
+
// receives, which is what the portal is responsible for.
|
|
13
|
+
const form = vi.hoisted(() => ({
|
|
14
|
+
props: null as { errors?: unknown; annotations?: unknown } | null,
|
|
15
|
+
}));
|
|
16
|
+
vi.mock("../runtime/components/MapoForm.vue", () => ({
|
|
17
|
+
default: {
|
|
18
|
+
name: "MapoForm",
|
|
19
|
+
props: [
|
|
20
|
+
"modelValue",
|
|
21
|
+
"fields",
|
|
22
|
+
"errors",
|
|
23
|
+
"languages",
|
|
24
|
+
"currentLang",
|
|
25
|
+
"registry",
|
|
26
|
+
"annotations",
|
|
27
|
+
],
|
|
28
|
+
setup(props: { errors?: unknown; annotations?: unknown }) {
|
|
29
|
+
form.props = props;
|
|
30
|
+
return () => null;
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
}));
|
|
34
|
+
vi.mock("vue-i18n", () => ({ useI18n: () => ({ t: (key: string) => key }) }));
|
|
35
|
+
|
|
36
|
+
const passthrough = (name: string): Component => ({
|
|
37
|
+
name,
|
|
38
|
+
inheritAttrs: false,
|
|
39
|
+
setup(_props: unknown, { slots }: SetupContext) {
|
|
40
|
+
return () => h("span", null, slots.default?.());
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
let app: App<HeadlessNode> | undefined;
|
|
45
|
+
|
|
46
|
+
beforeEach(() => {
|
|
47
|
+
// The portal listens for Escape on `window`; `<Transition>` measures the
|
|
48
|
+
// element through `window` and waits for animation frames.
|
|
49
|
+
vi.stubGlobal("window", {
|
|
50
|
+
addEventListener: () => {},
|
|
51
|
+
removeEventListener: () => {},
|
|
52
|
+
getComputedStyle: () => ({}),
|
|
53
|
+
});
|
|
54
|
+
vi.stubGlobal("requestAnimationFrame", (callback: () => void) => {
|
|
55
|
+
callback();
|
|
56
|
+
return 0;
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
afterEach(() => {
|
|
61
|
+
app?.unmount();
|
|
62
|
+
app = undefined;
|
|
63
|
+
useFocusMode().exit();
|
|
64
|
+
form.props = null;
|
|
65
|
+
vi.unstubAllGlobals();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe("MapoFocusPortal", () => {
|
|
69
|
+
it("passes the item's errors and annotations to its form", () => {
|
|
70
|
+
const annotations = ref<FieldAnnotations>({
|
|
71
|
+
title: { label: "Suggested by AI" },
|
|
72
|
+
});
|
|
73
|
+
useFocusMode().enter({
|
|
74
|
+
descriptor: { key: "blocks", type: "repeater" },
|
|
75
|
+
fields: [{ key: "title", type: "text" }],
|
|
76
|
+
model: { title: "La storia" },
|
|
77
|
+
errors: ref({ title: ["Too long."] }),
|
|
78
|
+
annotations: computed(() => annotations.value),
|
|
79
|
+
onUpdate: vi.fn(),
|
|
80
|
+
registry: { mapping: {}, attrs: {}, accessor: {} },
|
|
81
|
+
languages: [],
|
|
82
|
+
currentLang: "",
|
|
83
|
+
breadcrumb: ["Page", "Blocks", "Block 1"],
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
const host: Component = { render: () => h(MapoFocusPortal) };
|
|
87
|
+
app = createApp(host);
|
|
88
|
+
app.component("UIcon", passthrough("UIcon"));
|
|
89
|
+
app.component("UButton", passthrough("UButton"));
|
|
90
|
+
app.mount(headlessNode());
|
|
91
|
+
|
|
92
|
+
expect(form.props?.errors).toEqual({ title: ["Too long."] });
|
|
93
|
+
expect(form.props?.annotations).toEqual({
|
|
94
|
+
title: { label: "Suggested by AI" },
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
});
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { h, provide } from "vue";
|
|
3
|
+
import type { App, Component } from "vue";
|
|
4
|
+
|
|
5
|
+
import MapoForm from "../runtime/components/MapoForm.vue";
|
|
6
|
+
import type { FieldRegistry } from "../runtime/types/index.js";
|
|
7
|
+
import { createApp, headlessNode } from "./headlessRenderer.js";
|
|
8
|
+
import type { HeadlessNode } from "./headlessRenderer.js";
|
|
9
|
+
|
|
10
|
+
// The form runs `useMapoForm`, which reads these at setup.
|
|
11
|
+
vi.mock("#app", () => ({
|
|
12
|
+
useNuxtApp: () => ({}),
|
|
13
|
+
useRuntimeConfig: () => ({ public: {} }),
|
|
14
|
+
}));
|
|
15
|
+
vi.mock("@mapomodule/i18n/runtime/composables/useMapoT", () => ({
|
|
16
|
+
useMapoT: () => (key: string) => key,
|
|
17
|
+
}));
|
|
18
|
+
vi.mock("@mapomodule/store/runtime/stores/snack", () => ({
|
|
19
|
+
useSnackStore: () => ({ show: vi.fn() }),
|
|
20
|
+
}));
|
|
21
|
+
vi.mock("@mapomodule/store/runtime/stores/auth", () => ({
|
|
22
|
+
useAuthStore: () => ({ info: null }),
|
|
23
|
+
}));
|
|
24
|
+
vi.mock("vue-i18n", () => ({ useI18n: () => ({ t: (key: string) => key }) }));
|
|
25
|
+
// The layout is out of scope: the tests look at what the form registers.
|
|
26
|
+
vi.mock("../runtime/components/MapoFormGroup.vue", () => ({
|
|
27
|
+
default: { name: "MapoFormGroup", render: () => null },
|
|
28
|
+
}));
|
|
29
|
+
vi.mock("../runtime/components/MapoFormTabs.vue", () => ({
|
|
30
|
+
default: { name: "MapoFormTabs", render: () => null },
|
|
31
|
+
}));
|
|
32
|
+
vi.mock("../runtime/components/MapoFormFlatSection.vue", () => ({
|
|
33
|
+
default: { name: "MapoFormFlatSection", render: () => null },
|
|
34
|
+
}));
|
|
35
|
+
|
|
36
|
+
const registry: FieldRegistry = { mapping: {}, attrs: {}, accessor: {} };
|
|
37
|
+
|
|
38
|
+
let app: App<HeadlessNode> | undefined;
|
|
39
|
+
|
|
40
|
+
/** Mounts a form inside a detail page that collects its `revealField`. */
|
|
41
|
+
function mountInDetail(register: (fn: unknown) => () => void) {
|
|
42
|
+
const host: Component = {
|
|
43
|
+
setup() {
|
|
44
|
+
provide("mapoDetailRegisterReveal", register);
|
|
45
|
+
// A generic component: `h()` cannot infer its props.
|
|
46
|
+
return () =>
|
|
47
|
+
h(MapoForm as Component, {
|
|
48
|
+
modelValue: { title: "" },
|
|
49
|
+
fields: [{ key: "title", type: "text" }],
|
|
50
|
+
registry,
|
|
51
|
+
});
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
app = createApp(host);
|
|
55
|
+
app.mount(headlessNode());
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
afterEach(() => {
|
|
59
|
+
app?.unmount();
|
|
60
|
+
app = undefined;
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe("MapoForm in a detail page", () => {
|
|
64
|
+
it("lets the detail bring its fields into view, while it is there", async () => {
|
|
65
|
+
const unregister = vi.fn();
|
|
66
|
+
const register = vi.fn((_fn: unknown) => unregister);
|
|
67
|
+
mountInDetail(register);
|
|
68
|
+
|
|
69
|
+
expect(register).toHaveBeenCalledTimes(1);
|
|
70
|
+
const revealField = register.mock.calls[0]?.[0] as (
|
|
71
|
+
path: string,
|
|
72
|
+
) => Promise<boolean>;
|
|
73
|
+
await expect(revealField("summary")).resolves.toBe(false);
|
|
74
|
+
|
|
75
|
+
app?.unmount();
|
|
76
|
+
app = undefined;
|
|
77
|
+
expect(unregister).toHaveBeenCalledTimes(1);
|
|
78
|
+
});
|
|
79
|
+
});
|