@mapomodule/form 2.0.0-beta.11 → 2.0.0-beta.12
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/__tests__/repeaterTemplates.test.d.ts +2 -0
- package/dist/__tests__/repeaterTemplates.test.d.ts.map +1 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/module.json +1 -1
- package/dist/runtime/components/MapoFormGroup.vue +2 -2
- package/dist/runtime/components/MapoFormGroup.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoFormTabs.vue +2 -2
- package/dist/runtime/components/MapoFormTabs.vue.d.ts.map +1 -1
- package/dist/runtime/components/fields/MapoRepeater.vue +66 -13
- package/dist/runtime/components/fields/MapoRepeater.vue.d.ts.map +1 -1
- package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue +156 -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 +13 -1
- 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/types/descriptor.d.ts +37 -2
- package/dist/runtime/types/descriptor.d.ts.map +1 -1
- package/dist/runtime/types/index.d.ts +1 -1
- package/dist/runtime/types/index.d.ts.map +1 -1
- package/dist/runtime/utils/repeaterTemplates.d.ts +18 -0
- package/dist/runtime/utils/repeaterTemplates.d.ts.map +1 -0
- package/dist/runtime/utils/repeaterTemplates.js +43 -0
- package/package.json +3 -3
- package/src/__tests__/repeaterTemplates.test.ts +134 -0
- package/src/index.ts +1 -0
- package/src/runtime/components/MapoFormGroup.vue +2 -2
- package/src/runtime/components/MapoFormTabs.vue +2 -2
- package/src/runtime/components/fields/MapoRepeater.vue +75 -13
- package/src/runtime/components/fields/MapoRepeaterBlockPicker.vue +171 -0
- package/src/runtime/components/fields/MapoRepeaterItem.vue +14 -0
- package/src/runtime/types/descriptor.ts +42 -2
- package/src/runtime/types/index.ts +1 -0
- package/src/runtime/utils/repeaterTemplates.ts +80 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export const MODAL_PICKER_THRESHOLD = 3;
|
|
2
|
+
export function normalizeTemplates(templates) {
|
|
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
|
+
wireframe: meta.wireframe,
|
|
12
|
+
category: meta.category,
|
|
13
|
+
fields: Array.isArray(tpl) ? tpl : tpl.fields ?? []
|
|
14
|
+
};
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
export function shouldUseModalPicker(picker, templateCount) {
|
|
18
|
+
if (templateCount === 0) return false;
|
|
19
|
+
const mode = picker ?? "auto";
|
|
20
|
+
return mode === "modal" || mode === "auto" && templateCount > MODAL_PICKER_THRESHOLD;
|
|
21
|
+
}
|
|
22
|
+
export function filterTemplates(templates, query, category) {
|
|
23
|
+
const q = query.trim().toLowerCase();
|
|
24
|
+
return templates.filter((tpl) => {
|
|
25
|
+
if (category && tpl.category !== category) return false;
|
|
26
|
+
if (!q) return true;
|
|
27
|
+
return [tpl.label, tpl.description, tpl.category].some(
|
|
28
|
+
(v) => v?.toLowerCase().includes(q)
|
|
29
|
+
);
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
export function wireframeShapes(fields) {
|
|
33
|
+
return fields.slice(0, 5).map((f) => {
|
|
34
|
+
const type = String(f.type);
|
|
35
|
+
if (type === "textarea" || type === "editor") return "lines";
|
|
36
|
+
if (type.includes("media") || type === "file") return "box";
|
|
37
|
+
if (type === "boolean") return "check";
|
|
38
|
+
return "line";
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
export function reachesMax(current, maxItems) {
|
|
42
|
+
return !!maxItems && current + 1 >= maxItems;
|
|
43
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mapomodule/form",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.12",
|
|
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-beta.
|
|
50
|
+
"@mapomodule/i18n": "2.0.0-beta.4",
|
|
51
51
|
"@mapomodule/store": "2.0.0-beta.6",
|
|
52
|
-
"@mapomodule/utils": "2.0.0-beta.
|
|
52
|
+
"@mapomodule/utils": "2.0.0-beta.7",
|
|
53
53
|
"@tiptap/extension-image": "^2.11.5",
|
|
54
54
|
"@tiptap/extension-link": "^2.11.5",
|
|
55
55
|
"@tiptap/extension-underline": "^2.11.5",
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
filterTemplates,
|
|
4
|
+
normalizeTemplates,
|
|
5
|
+
reachesMax,
|
|
6
|
+
shouldUseModalPicker,
|
|
7
|
+
wireframeShapes,
|
|
8
|
+
} from "../runtime/utils/repeaterTemplates.js";
|
|
9
|
+
import type { AnyFieldDescriptor } from "../runtime/types/descriptor.js";
|
|
10
|
+
|
|
11
|
+
const title: AnyFieldDescriptor = { key: "title", type: "text" };
|
|
12
|
+
|
|
13
|
+
describe("normalizeTemplates", () => {
|
|
14
|
+
it("returns an empty list without templates", () => {
|
|
15
|
+
expect(normalizeTemplates(undefined)).toEqual([]);
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("uses the key as label for a bare field array", () => {
|
|
19
|
+
const [tpl] = normalizeTemplates({ cta: [title] });
|
|
20
|
+
expect(tpl).toMatchObject({ key: "cta", label: "cta", fields: [title] });
|
|
21
|
+
expect(tpl?.description).toBeUndefined();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("keeps the metadata of a RepeaterTemplate", () => {
|
|
25
|
+
const [tpl] = normalizeTemplates({
|
|
26
|
+
hero: {
|
|
27
|
+
label: "Hero",
|
|
28
|
+
description: "Big headline",
|
|
29
|
+
icon: "i-lucide-image",
|
|
30
|
+
category: "Header",
|
|
31
|
+
wireframe: true,
|
|
32
|
+
fields: [title],
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
expect(tpl).toMatchObject({
|
|
36
|
+
key: "hero",
|
|
37
|
+
label: "Hero",
|
|
38
|
+
description: "Big headline",
|
|
39
|
+
icon: "i-lucide-image",
|
|
40
|
+
category: "Header",
|
|
41
|
+
wireframe: true,
|
|
42
|
+
fields: [title],
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("tolerates a template object without fields", () => {
|
|
47
|
+
expect(
|
|
48
|
+
normalizeTemplates({ empty: { label: "Empty" } as never })[0]?.fields,
|
|
49
|
+
).toEqual([]);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe("shouldUseModalPicker", () => {
|
|
54
|
+
it("never opens a modal without templates", () => {
|
|
55
|
+
expect(shouldUseModalPicker("modal", 0)).toBe(false);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("switches to the modal above 3 templates in auto mode", () => {
|
|
59
|
+
expect(shouldUseModalPicker(undefined, 3)).toBe(false);
|
|
60
|
+
expect(shouldUseModalPicker("auto", 4)).toBe(true);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("honours an explicit mode", () => {
|
|
64
|
+
expect(shouldUseModalPicker("select", 10)).toBe(false);
|
|
65
|
+
expect(shouldUseModalPicker("modal", 1)).toBe(true);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe("filterTemplates", () => {
|
|
70
|
+
const list = normalizeTemplates({
|
|
71
|
+
hero: { label: "Hero", category: "Header", fields: [] },
|
|
72
|
+
text: {
|
|
73
|
+
label: "Rich text",
|
|
74
|
+
description: "Paragraphs",
|
|
75
|
+
category: "Content",
|
|
76
|
+
fields: [],
|
|
77
|
+
},
|
|
78
|
+
cta: [title],
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("returns everything without filters", () => {
|
|
82
|
+
expect(filterTemplates(list, "", "")).toHaveLength(3);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("matches label and description, case-insensitively", () => {
|
|
86
|
+
expect(filterTemplates(list, "HERO", "").map((t) => t.key)).toEqual([
|
|
87
|
+
"hero",
|
|
88
|
+
]);
|
|
89
|
+
expect(filterTemplates(list, "paragr", "").map((t) => t.key)).toEqual([
|
|
90
|
+
"text",
|
|
91
|
+
]);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("filters by category", () => {
|
|
95
|
+
expect(filterTemplates(list, "", "Content").map((t) => t.key)).toEqual([
|
|
96
|
+
"text",
|
|
97
|
+
]);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("combines query and category", () => {
|
|
101
|
+
expect(filterTemplates(list, "hero", "Content")).toEqual([]);
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
describe("wireframeShapes", () => {
|
|
106
|
+
it("maps field types to shapes and caps at 5", () => {
|
|
107
|
+
const fields = [
|
|
108
|
+
{ key: "a", type: "text" },
|
|
109
|
+
{ key: "b", type: "textarea" },
|
|
110
|
+
{ key: "c", type: "media" },
|
|
111
|
+
{ key: "d", type: "boolean" },
|
|
112
|
+
{ key: "e", type: "editor" },
|
|
113
|
+
{ key: "f", type: "text" },
|
|
114
|
+
] as AnyFieldDescriptor[];
|
|
115
|
+
expect(wireframeShapes(fields)).toEqual([
|
|
116
|
+
"line",
|
|
117
|
+
"lines",
|
|
118
|
+
"box",
|
|
119
|
+
"check",
|
|
120
|
+
"lines",
|
|
121
|
+
]);
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
describe("reachesMax", () => {
|
|
126
|
+
it("is false without a limit", () => {
|
|
127
|
+
expect(reachesMax(100, undefined)).toBe(false);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("is true when the next item hits the limit", () => {
|
|
131
|
+
expect(reachesMax(2, 3)).toBe(true);
|
|
132
|
+
expect(reachesMax(1, 3)).toBe(false);
|
|
133
|
+
});
|
|
134
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -217,12 +217,12 @@ function colClass(cols: AnyFieldDescriptor["cols"]): string {
|
|
|
217
217
|
<!-- Sub-tab bar + content (when subtabs are present) -->
|
|
218
218
|
<div v-if="subtabs && subtabs.size > 0" class="px-5 pb-5 pt-4">
|
|
219
219
|
<!-- Sub-tab bar -->
|
|
220
|
-
<div class="flex gap-1 border-b border-gray-200
|
|
220
|
+
<div class="mb-4 flex flex-wrap gap-1 border-b border-gray-200">
|
|
221
221
|
<button
|
|
222
222
|
v-for="[subtabName] of subtabs"
|
|
223
223
|
:key="subtabName"
|
|
224
224
|
type="button"
|
|
225
|
-
class="relative px-3 py-1.5 text-sm font-medium transition-colors focus:outline-none"
|
|
225
|
+
class="relative max-w-full px-3 py-1.5 text-sm font-medium transition-colors focus:outline-none"
|
|
226
226
|
:class="[
|
|
227
227
|
activeSubtab === subtabName
|
|
228
228
|
? 'text-primary-600 border-b-2 border-primary-600 -mb-px'
|
|
@@ -58,12 +58,12 @@ const tabsWithErrors = computed(() => {
|
|
|
58
58
|
<template>
|
|
59
59
|
<div class="mapo-form-tabs">
|
|
60
60
|
<!-- Tab bar -->
|
|
61
|
-
<div class="flex gap-1 border-b border-gray-200 pb-px">
|
|
61
|
+
<div class="flex flex-wrap gap-1 border-b border-gray-200 pb-px">
|
|
62
62
|
<button
|
|
63
63
|
v-for="tab in shownTabs"
|
|
64
64
|
:key="tab.name"
|
|
65
65
|
type="button"
|
|
66
|
-
class="relative flex items-center gap-1.5 rounded-t px-4 py-2 text-sm font-medium transition-colors"
|
|
66
|
+
class="relative flex max-w-full items-center gap-1.5 rounded-t px-4 py-2 text-sm font-medium transition-colors"
|
|
67
67
|
:class="[
|
|
68
68
|
activeTab === tab.name
|
|
69
69
|
? 'border-b-2 border-primary-500 text-primary-600'
|
|
@@ -4,8 +4,14 @@ import { useI18n } from "vue-i18n";
|
|
|
4
4
|
import { VueDraggable } from "vue-draggable-plus";
|
|
5
5
|
import { useConfirmStore } from "@mapomodule/store/runtime/stores/confirm";
|
|
6
6
|
import type { RepeaterDescriptor } from "../../types/index.js";
|
|
7
|
+
import {
|
|
8
|
+
normalizeTemplates,
|
|
9
|
+
reachesMax,
|
|
10
|
+
shouldUseModalPicker,
|
|
11
|
+
} from "../../utils/repeaterTemplates.js";
|
|
7
12
|
import { injectMapoForm } from "../../composables/useMapoForm.js";
|
|
8
13
|
import MapoRepeaterItem from "./MapoRepeaterItem.vue";
|
|
14
|
+
import MapoRepeaterBlockPicker from "./MapoRepeaterBlockPicker.vue";
|
|
9
15
|
|
|
10
16
|
const props = defineProps<{
|
|
11
17
|
modelValue: unknown;
|
|
@@ -67,12 +73,40 @@ function undo() {
|
|
|
67
73
|
// ─── Template selection ──────────────────────────────────────────────────────
|
|
68
74
|
|
|
69
75
|
const templates = computed(() => attrs.value.templates);
|
|
70
|
-
|
|
71
|
-
|
|
76
|
+
|
|
77
|
+
// Bare field arrays and RepeaterTemplate objects are both accepted.
|
|
78
|
+
const templateList = computed(() => normalizeTemplates(templates.value));
|
|
79
|
+
const templateByKey = computed(
|
|
80
|
+
() => new Map(templateList.value.map((tpl) => [tpl.key, tpl])),
|
|
72
81
|
);
|
|
82
|
+
const templateKeys = computed(() => templateList.value.map((tpl) => tpl.key));
|
|
73
83
|
const hasTemplates = computed(() => templateKeys.value.length > 0);
|
|
74
84
|
const selectedTemplate = ref<string>("");
|
|
75
85
|
|
|
86
|
+
const useModalPicker = computed(() =>
|
|
87
|
+
shouldUseModalPicker(attrs.value.picker, templateKeys.value.length),
|
|
88
|
+
);
|
|
89
|
+
const pickerOpen = ref(false);
|
|
90
|
+
|
|
91
|
+
const selectItems = computed(() =>
|
|
92
|
+
templateList.value.map((tpl) => ({
|
|
93
|
+
label: tpl.label,
|
|
94
|
+
value: tpl.key,
|
|
95
|
+
description: tpl.description,
|
|
96
|
+
icon: tpl.icon,
|
|
97
|
+
})),
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
function templateLabelFor(item: Record<string, unknown>): string | undefined {
|
|
101
|
+
const key = item._template as string | undefined;
|
|
102
|
+
if (!key) return undefined;
|
|
103
|
+
return templateByKey.value.get(key)?.label ?? key;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function templateIconFor(item: Record<string, unknown>): string | undefined {
|
|
107
|
+
return templateByKey.value.get(item._template as string)?.icon;
|
|
108
|
+
}
|
|
109
|
+
|
|
76
110
|
// Async-safe: when templates load after mount, auto-pick the first one.
|
|
77
111
|
watchEffect(() => {
|
|
78
112
|
if (!selectedTemplate.value && templateKeys.value[0]) {
|
|
@@ -81,9 +115,9 @@ watchEffect(() => {
|
|
|
81
115
|
});
|
|
82
116
|
|
|
83
117
|
function fieldsForItem(item: Record<string, unknown>) {
|
|
84
|
-
if (!
|
|
118
|
+
if (!hasTemplates.value) return props.descriptor.fields ?? [];
|
|
85
119
|
const key = (item._template as string) ?? selectedTemplate.value;
|
|
86
|
-
return
|
|
120
|
+
return templateByKey.value.get(key)?.fields ?? props.descriptor.fields ?? [];
|
|
87
121
|
}
|
|
88
122
|
|
|
89
123
|
// ─── Item CRUD ───────────────────────────────────────────────────────────────
|
|
@@ -122,15 +156,28 @@ async function askConfirm(message: string, title?: string): Promise<boolean> {
|
|
|
122
156
|
}
|
|
123
157
|
}
|
|
124
158
|
|
|
125
|
-
function addItem() {
|
|
159
|
+
function addItem(templateKey?: string) {
|
|
126
160
|
if (!canAdd.value) return;
|
|
127
161
|
pushUndo();
|
|
128
162
|
const newItem: Record<string, unknown> = hasTemplates.value
|
|
129
|
-
? { _template: selectedTemplate.value }
|
|
163
|
+
? { _template: templateKey ?? selectedTemplate.value }
|
|
130
164
|
: {};
|
|
131
165
|
emitItems([...items.value, newItem]);
|
|
132
166
|
}
|
|
133
167
|
|
|
168
|
+
function onAddClick() {
|
|
169
|
+
if (useModalPicker.value) pickerOpen.value = true;
|
|
170
|
+
else addItem();
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function onPickTemplate(key: string, keepOpen: boolean) {
|
|
174
|
+
// `items` still holds the pre-add array here, so count the new item explicitly.
|
|
175
|
+
const full = reachesMax(items.value.length, attrs.value.maxItems);
|
|
176
|
+
selectedTemplate.value = key;
|
|
177
|
+
addItem(key);
|
|
178
|
+
if (!keepOpen || full) pickerOpen.value = false;
|
|
179
|
+
}
|
|
180
|
+
|
|
134
181
|
async function deleteItem(index: number) {
|
|
135
182
|
if (!canDelete.value) return;
|
|
136
183
|
if (attrs.value.confirmDelete !== false) {
|
|
@@ -372,27 +419,38 @@ const errorPrefix = (index: number) =>
|
|
|
372
419
|
@click="toggleSelectionMode"
|
|
373
420
|
/>
|
|
374
421
|
|
|
375
|
-
<!-- Template selector -->
|
|
422
|
+
<!-- Template selector (dropdown mode; the modal picker opens from "Add") -->
|
|
376
423
|
<USelectMenu
|
|
377
|
-
v-if="hasTemplates"
|
|
424
|
+
v-if="hasTemplates && !useModalPicker"
|
|
378
425
|
v-model="selectedTemplate"
|
|
379
|
-
:items="
|
|
426
|
+
:items="selectItems"
|
|
427
|
+
value-key="value"
|
|
428
|
+
:search-input="templateKeys.length > 6"
|
|
429
|
+
:aria-label="t('mapo.repeater.blockType')"
|
|
380
430
|
size="xs"
|
|
381
|
-
class="w-
|
|
431
|
+
class="w-48"
|
|
382
432
|
/>
|
|
383
433
|
|
|
384
434
|
<UButton
|
|
385
435
|
size="sm"
|
|
386
436
|
color="primary"
|
|
387
|
-
icon="i-lucide-plus"
|
|
437
|
+
:icon="useModalPicker ? 'i-lucide-layout-grid' : 'i-lucide-plus'"
|
|
388
438
|
:disabled="!canAdd"
|
|
389
|
-
@click="
|
|
439
|
+
@click="onAddClick"
|
|
390
440
|
>
|
|
391
|
-
{{ t("mapo.add") }}
|
|
441
|
+
{{ useModalPicker ? t("mapo.repeater.addBlock") : t("mapo.add") }}
|
|
392
442
|
</UButton>
|
|
393
443
|
</div>
|
|
394
444
|
</div>
|
|
395
445
|
|
|
446
|
+
<MapoRepeaterBlockPicker
|
|
447
|
+
v-if="useModalPicker"
|
|
448
|
+
v-model:open="pickerOpen"
|
|
449
|
+
:templates="templateList"
|
|
450
|
+
:disabled="!canAdd"
|
|
451
|
+
@select="onPickTemplate"
|
|
452
|
+
/>
|
|
453
|
+
|
|
396
454
|
<!-- ── Bulk actions bar (visible when there is a selection) ───────────── -->
|
|
397
455
|
<Transition name="slide-down">
|
|
398
456
|
<div
|
|
@@ -469,6 +527,8 @@ const errorPrefix = (index: number) =>
|
|
|
469
527
|
<MapoRepeaterItem
|
|
470
528
|
v-for="(item, index) in items"
|
|
471
529
|
:key="`${uidFor(item)}-${collapseVersion}`"
|
|
530
|
+
:template-label="templateLabelFor(item)"
|
|
531
|
+
:template-icon="templateIconFor(item)"
|
|
472
532
|
:uid="uidFor(item)"
|
|
473
533
|
:item="item"
|
|
474
534
|
:fields="fieldsForItem(item)"
|
|
@@ -501,6 +561,8 @@ const errorPrefix = (index: number) =>
|
|
|
501
561
|
<MapoRepeaterItem
|
|
502
562
|
v-for="(item, index) in items"
|
|
503
563
|
:key="`${uidFor(item)}-${collapseVersion}`"
|
|
564
|
+
:template-label="templateLabelFor(item)"
|
|
565
|
+
:template-icon="templateIconFor(item)"
|
|
504
566
|
:uid="uidFor(item)"
|
|
505
567
|
:item="item"
|
|
506
568
|
:fields="fieldsForItem(item)"
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
<script setup lang="ts">
|
|
2
|
+
import { ref, computed, watch } from "vue";
|
|
3
|
+
import { useI18n } from "vue-i18n";
|
|
4
|
+
import type { NormalizedRepeaterTemplate } from "../../types/descriptor.js";
|
|
5
|
+
import {
|
|
6
|
+
filterTemplates,
|
|
7
|
+
wireframeShapes,
|
|
8
|
+
} from "../../utils/repeaterTemplates.js";
|
|
9
|
+
|
|
10
|
+
const props = defineProps<{
|
|
11
|
+
templates: NormalizedRepeaterTemplate[];
|
|
12
|
+
/** Disables the cards (e.g. `maxItems` reached). */
|
|
13
|
+
disabled?: boolean;
|
|
14
|
+
}>();
|
|
15
|
+
|
|
16
|
+
const open = defineModel<boolean>("open", { default: false });
|
|
17
|
+
|
|
18
|
+
const emit = defineEmits<{
|
|
19
|
+
/** `keepOpen` is true when Shift was held, so the caller can add several blocks. */
|
|
20
|
+
select: [key: string, keepOpen: boolean];
|
|
21
|
+
}>();
|
|
22
|
+
|
|
23
|
+
const { t } = useI18n();
|
|
24
|
+
|
|
25
|
+
const query = ref("");
|
|
26
|
+
const category = ref("");
|
|
27
|
+
|
|
28
|
+
watch(open, (isOpen) => {
|
|
29
|
+
if (isOpen) {
|
|
30
|
+
query.value = "";
|
|
31
|
+
category.value = "";
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const categories = computed(
|
|
36
|
+
() =>
|
|
37
|
+
[
|
|
38
|
+
...new Set(props.templates.map((tpl) => tpl.category).filter(Boolean)),
|
|
39
|
+
] as string[],
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
const filtered = computed(() =>
|
|
43
|
+
filterTemplates(props.templates, query.value, category.value),
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
const shapesFor = wireframeShapes;
|
|
47
|
+
function pick(key: string, event: MouseEvent) {
|
|
48
|
+
if (props.disabled) return;
|
|
49
|
+
emit("select", key, event.shiftKey);
|
|
50
|
+
}
|
|
51
|
+
</script>
|
|
52
|
+
|
|
53
|
+
<template>
|
|
54
|
+
<UModal v-model:open="open" :title="t('mapo.repeater.chooseBlock')">
|
|
55
|
+
<template #body>
|
|
56
|
+
<div class="space-y-4">
|
|
57
|
+
<UInput
|
|
58
|
+
v-model="query"
|
|
59
|
+
icon="i-lucide-search"
|
|
60
|
+
:placeholder="t('mapo.repeater.searchBlocks')"
|
|
61
|
+
class="w-full"
|
|
62
|
+
autofocus
|
|
63
|
+
/>
|
|
64
|
+
|
|
65
|
+
<div v-if="categories.length" class="flex flex-wrap gap-1.5">
|
|
66
|
+
<UButton
|
|
67
|
+
size="xs"
|
|
68
|
+
:variant="category === '' ? 'solid' : 'outline'"
|
|
69
|
+
color="neutral"
|
|
70
|
+
:label="t('mapo.repeater.allCategories')"
|
|
71
|
+
@click="category = ''"
|
|
72
|
+
/>
|
|
73
|
+
<UButton
|
|
74
|
+
v-for="cat in categories"
|
|
75
|
+
:key="cat"
|
|
76
|
+
size="xs"
|
|
77
|
+
:variant="category === cat ? 'solid' : 'outline'"
|
|
78
|
+
color="neutral"
|
|
79
|
+
:label="cat"
|
|
80
|
+
@click="category = cat"
|
|
81
|
+
/>
|
|
82
|
+
</div>
|
|
83
|
+
|
|
84
|
+
<div
|
|
85
|
+
v-if="filtered.length"
|
|
86
|
+
class="grid grid-cols-1 gap-3 sm:grid-cols-2"
|
|
87
|
+
>
|
|
88
|
+
<button
|
|
89
|
+
v-for="tpl in filtered"
|
|
90
|
+
:key="tpl.key"
|
|
91
|
+
type="button"
|
|
92
|
+
class="group flex flex-col overflow-hidden rounded-lg border border-default text-left transition hover:border-primary hover:shadow focus-visible:outline-2 focus-visible:outline-primary disabled:cursor-not-allowed disabled:opacity-50"
|
|
93
|
+
:disabled="disabled"
|
|
94
|
+
:data-template="tpl.key"
|
|
95
|
+
@click="pick(tpl.key, $event)"
|
|
96
|
+
>
|
|
97
|
+
<!-- Preview: image > wireframe > icon -->
|
|
98
|
+
<div
|
|
99
|
+
class="flex h-28 w-full items-center justify-center bg-elevated"
|
|
100
|
+
>
|
|
101
|
+
<img
|
|
102
|
+
v-if="tpl.image"
|
|
103
|
+
:src="tpl.image"
|
|
104
|
+
alt=""
|
|
105
|
+
class="h-full w-full object-cover"
|
|
106
|
+
/>
|
|
107
|
+
<div
|
|
108
|
+
v-else-if="tpl.wireframe"
|
|
109
|
+
class="flex w-3/4 flex-col gap-1.5"
|
|
110
|
+
aria-hidden="true"
|
|
111
|
+
>
|
|
112
|
+
<template v-for="(shape, i) in shapesFor(tpl.fields)" :key="i">
|
|
113
|
+
<div
|
|
114
|
+
v-if="shape === 'line'"
|
|
115
|
+
class="h-2 w-full rounded bg-accented"
|
|
116
|
+
/>
|
|
117
|
+
<div v-else-if="shape === 'lines'" class="space-y-1">
|
|
118
|
+
<div class="h-1.5 w-full rounded bg-accented" />
|
|
119
|
+
<div class="h-1.5 w-5/6 rounded bg-accented" />
|
|
120
|
+
</div>
|
|
121
|
+
<div
|
|
122
|
+
v-else-if="shape === 'box'"
|
|
123
|
+
class="h-8 w-full rounded bg-accented"
|
|
124
|
+
/>
|
|
125
|
+
<div v-else class="h-2.5 w-2.5 rounded-sm bg-accented" />
|
|
126
|
+
</template>
|
|
127
|
+
</div>
|
|
128
|
+
<UIcon
|
|
129
|
+
v-else
|
|
130
|
+
:name="tpl.icon ?? 'i-lucide-layout-template'"
|
|
131
|
+
class="h-10 w-10 text-dimmed group-hover:text-primary"
|
|
132
|
+
/>
|
|
133
|
+
</div>
|
|
134
|
+
|
|
135
|
+
<div class="space-y-0.5 p-3">
|
|
136
|
+
<div class="flex items-center gap-1.5">
|
|
137
|
+
<UIcon
|
|
138
|
+
v-if="tpl.icon && (tpl.image || tpl.wireframe)"
|
|
139
|
+
:name="tpl.icon"
|
|
140
|
+
class="h-4 w-4 shrink-0 text-muted"
|
|
141
|
+
/>
|
|
142
|
+
<span class="truncate text-sm font-medium text-highlighted">
|
|
143
|
+
{{ tpl.label }}
|
|
144
|
+
</span>
|
|
145
|
+
<UBadge
|
|
146
|
+
v-if="tpl.category"
|
|
147
|
+
size="xs"
|
|
148
|
+
variant="subtle"
|
|
149
|
+
color="neutral"
|
|
150
|
+
class="ml-auto"
|
|
151
|
+
>
|
|
152
|
+
{{ tpl.category }}
|
|
153
|
+
</UBadge>
|
|
154
|
+
</div>
|
|
155
|
+
<p v-if="tpl.description" class="line-clamp-2 text-xs text-muted">
|
|
156
|
+
{{ tpl.description }}
|
|
157
|
+
</p>
|
|
158
|
+
</div>
|
|
159
|
+
</button>
|
|
160
|
+
</div>
|
|
161
|
+
<p v-else class="py-6 text-center text-sm text-dimmed">
|
|
162
|
+
{{ t("mapo.repeater.noBlocksFound") }}
|
|
163
|
+
</p>
|
|
164
|
+
|
|
165
|
+
<p class="text-xs text-dimmed">
|
|
166
|
+
{{ t("mapo.repeater.keepOpenHint") }}
|
|
167
|
+
</p>
|
|
168
|
+
</div>
|
|
169
|
+
</template>
|
|
170
|
+
</UModal>
|
|
171
|
+
</template>
|
|
@@ -35,6 +35,10 @@ const props = defineProps<{
|
|
|
35
35
|
selected?: boolean;
|
|
36
36
|
/** true = bulk selection mode is active in the parent repeater. */
|
|
37
37
|
selectionMode?: boolean;
|
|
38
|
+
/** Display name of the item's template (repeaters with `attrs.templates`). */
|
|
39
|
+
templateLabel?: string;
|
|
40
|
+
/** Icon of the item's template. */
|
|
41
|
+
templateIcon?: string;
|
|
38
42
|
}>();
|
|
39
43
|
|
|
40
44
|
const emit = defineEmits<{
|
|
@@ -291,6 +295,16 @@ function enterFocusMode() {
|
|
|
291
295
|
selectionMode ? toggleSelectFromEvent($event) : (expanded = !expanded)
|
|
292
296
|
"
|
|
293
297
|
>
|
|
298
|
+
<UBadge
|
|
299
|
+
v-if="templateLabel"
|
|
300
|
+
color="neutral"
|
|
301
|
+
variant="subtle"
|
|
302
|
+
size="xs"
|
|
303
|
+
:icon="templateIcon"
|
|
304
|
+
class="shrink-0"
|
|
305
|
+
>
|
|
306
|
+
{{ templateLabel }}
|
|
307
|
+
</UBadge>
|
|
294
308
|
<span class="truncate">{{ label }}</span>
|
|
295
309
|
<UBadge v-if="hasErrors" color="error" variant="soft" size="xs">
|
|
296
310
|
errors
|
|
@@ -303,6 +303,36 @@ export interface MapDescriptor<
|
|
|
303
303
|
};
|
|
304
304
|
}
|
|
305
305
|
|
|
306
|
+
/**
|
|
307
|
+
* A repeater block template with presentation metadata.
|
|
308
|
+
* Use it instead of a bare field array to describe the block in the picker.
|
|
309
|
+
*/
|
|
310
|
+
export interface RepeaterTemplate {
|
|
311
|
+
/** Field descriptors of the block. */
|
|
312
|
+
fields: AnyFieldDescriptor[];
|
|
313
|
+
/** Human-readable name. Defaults to the template key. */
|
|
314
|
+
label?: string;
|
|
315
|
+
/** Short explanation of what the block is for. */
|
|
316
|
+
description?: string;
|
|
317
|
+
/** Iconify name (e.g. `i-lucide-image`). Used as the preview fallback and in the item header. */
|
|
318
|
+
icon?: string;
|
|
319
|
+
/** Preview image URL, shown in the picker card. Takes precedence over `wireframe` and `icon`. */
|
|
320
|
+
image?: string;
|
|
321
|
+
/** Draw a schematic preview generated from the template fields (used when there is no `image`). */
|
|
322
|
+
wireframe?: boolean;
|
|
323
|
+
/** Group name: the picker shows one filter chip per category. */
|
|
324
|
+
category?: string;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/** A {@link RepeaterTemplate} normalized for display, with its key and defaults applied. @internal */
|
|
328
|
+
export interface NormalizedRepeaterTemplate extends Omit<
|
|
329
|
+
RepeaterTemplate,
|
|
330
|
+
"label"
|
|
331
|
+
> {
|
|
332
|
+
key: string;
|
|
333
|
+
label: string;
|
|
334
|
+
}
|
|
335
|
+
|
|
306
336
|
/** Descriptor for a drag-and-drop repeater with nested field descriptors. */
|
|
307
337
|
export interface RepeaterDescriptor<
|
|
308
338
|
T = Record<string, unknown>,
|
|
@@ -314,8 +344,18 @@ export interface RepeaterDescriptor<
|
|
|
314
344
|
*/
|
|
315
345
|
fields?: AnyFieldDescriptor[];
|
|
316
346
|
attrs?: {
|
|
317
|
-
/**
|
|
318
|
-
|
|
347
|
+
/**
|
|
348
|
+
* Multiple templates for heterogeneous items (`oneOf` discriminated union).
|
|
349
|
+
* A value is either a bare field array or a {@link RepeaterTemplate} with label,
|
|
350
|
+
* description, icon, preview image and category.
|
|
351
|
+
*/
|
|
352
|
+
templates?: Record<string, AnyFieldDescriptor[] | RepeaterTemplate>;
|
|
353
|
+
/**
|
|
354
|
+
* How the user chooses a template when adding an item: `select` (dropdown),
|
|
355
|
+
* `modal` (card gallery with search) or `auto`. Default: `auto`, which uses the
|
|
356
|
+
* modal when there are more than 3 templates.
|
|
357
|
+
*/
|
|
358
|
+
picker?: "auto" | "select" | "modal";
|
|
319
359
|
/** Label shown when an item is collapsed. Receives the item and its index. */
|
|
320
360
|
previewLabel?: (item: unknown, index: number) => string;
|
|
321
361
|
confirmDelete?: boolean;
|