@mapomodule/form 2.0.0-beta.10 → 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.
Files changed (56) hide show
  1. package/README.md +37 -119
  2. package/dist/__tests__/repeaterTemplates.test.d.ts +2 -0
  3. package/dist/__tests__/repeaterTemplates.test.d.ts.map +1 -0
  4. package/dist/__tests__/tabVisibility.test.d.ts +2 -0
  5. package/dist/__tests__/tabVisibility.test.d.ts.map +1 -0
  6. package/dist/index.d.mts +1 -1
  7. package/dist/index.d.ts +1 -1
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/module.json +1 -1
  10. package/dist/runtime/components/MapoForm.vue +12 -3
  11. package/dist/runtime/components/MapoForm.vue.d.ts.map +1 -1
  12. package/dist/runtime/components/MapoFormGroup.vue +2 -2
  13. package/dist/runtime/components/MapoFormGroup.vue.d.ts.map +1 -1
  14. package/dist/runtime/components/MapoFormTabs.vue +19 -7
  15. package/dist/runtime/components/MapoFormTabs.vue.d.ts.map +1 -1
  16. package/dist/runtime/components/fields/MapoMediaField.vue +5 -3
  17. package/dist/runtime/components/fields/MapoMediaField.vue.d.ts.map +1 -1
  18. package/dist/runtime/components/fields/MapoMediaM2mField.vue +3 -2
  19. package/dist/runtime/components/fields/MapoMediaM2mField.vue.d.ts.map +1 -1
  20. package/dist/runtime/components/fields/MapoRepeater.vue +66 -13
  21. package/dist/runtime/components/fields/MapoRepeater.vue.d.ts.map +1 -1
  22. package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue +156 -0
  23. package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue.d.ts +20 -0
  24. package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue.d.ts.map +1 -0
  25. package/dist/runtime/components/fields/MapoRepeaterItem.vue +13 -1
  26. package/dist/runtime/components/fields/MapoRepeaterItem.vue.d.ts +6 -2
  27. package/dist/runtime/components/fields/MapoRepeaterItem.vue.d.ts.map +1 -1
  28. package/dist/runtime/components/fields/MapoWygEditor.vue +18 -12
  29. package/dist/runtime/components/fields/MapoWygEditor.vue.d.ts.map +1 -1
  30. package/dist/runtime/types/descriptor.d.ts +37 -2
  31. package/dist/runtime/types/descriptor.d.ts.map +1 -1
  32. package/dist/runtime/types/index.d.ts +1 -1
  33. package/dist/runtime/types/index.d.ts.map +1 -1
  34. package/dist/runtime/utils/repeaterTemplates.d.ts +18 -0
  35. package/dist/runtime/utils/repeaterTemplates.d.ts.map +1 -0
  36. package/dist/runtime/utils/repeaterTemplates.js +43 -0
  37. package/dist/runtime/utils/tabVisibility.d.ts +18 -0
  38. package/dist/runtime/utils/tabVisibility.d.ts.map +1 -0
  39. package/dist/runtime/utils/tabVisibility.js +18 -0
  40. package/package.json +4 -4
  41. package/src/__tests__/repeaterTemplates.test.ts +134 -0
  42. package/src/__tests__/tabVisibility.test.ts +102 -0
  43. package/src/index.ts +1 -0
  44. package/src/runtime/components/MapoForm.vue +17 -4
  45. package/src/runtime/components/MapoFormGroup.vue +2 -2
  46. package/src/runtime/components/MapoFormTabs.vue +27 -7
  47. package/src/runtime/components/fields/MapoMediaField.vue +5 -3
  48. package/src/runtime/components/fields/MapoMediaM2mField.vue +3 -2
  49. package/src/runtime/components/fields/MapoRepeater.vue +75 -13
  50. package/src/runtime/components/fields/MapoRepeaterBlockPicker.vue +171 -0
  51. package/src/runtime/components/fields/MapoRepeaterItem.vue +14 -0
  52. package/src/runtime/components/fields/MapoWygEditor.vue +18 -12
  53. package/src/runtime/types/descriptor.ts +42 -2
  54. package/src/runtime/types/index.ts +1 -0
  55. package/src/runtime/utils/repeaterTemplates.ts +80 -0
  56. package/src/runtime/utils/tabVisibility.ts +44 -0
@@ -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
@@ -142,10 +142,13 @@ function onImageConfirm(
142
142
  </script>
143
143
 
144
144
  <template>
145
+ <!-- An input surface of its own, like the Nuxt UI inputs next to it
146
+ (`bg-default`, `accented` border): without it the text area takes the
147
+ page background and disappears on a tinted page. -->
145
148
  <div
146
- class="mapo-wyg-editor rounded border"
149
+ class="mapo-wyg-editor rounded-md border bg-default"
147
150
  :class="[
148
- errors?.length ? 'border-red-300' : 'border-gray-200',
151
+ errors?.length ? 'border-red-300' : 'border-accented',
149
152
  readonly || disabled ? 'opacity-60' : '',
150
153
  ]"
151
154
  >
@@ -284,15 +287,18 @@ function onImageConfirm(
284
287
  >
285
288
  {{ err }}
286
289
  </p>
287
- </div>
288
290
 
289
- <!-- Image picker dialog — only rendered when MapoMediaManagerDialog is globally available -->
290
- <component
291
- :is="MediaManagerDialog"
292
- v-if="hasMediaManager"
293
- v-model="imagePickerOpen"
294
- selection-mode="single"
295
- mime="image/*"
296
- @confirm="onImageConfirm"
297
- />
291
+ <!-- Image picker dialog — only rendered when MapoMediaManagerDialog is
292
+ globally available. Inside the root on purpose: a second root would
293
+ drop what MapoFormField passes down (`flex-1`, `id`, `style`), and the
294
+ editor would shrink to its toolbar. As a modal it renders in <body>. -->
295
+ <component
296
+ :is="MediaManagerDialog"
297
+ v-if="hasMediaManager"
298
+ v-model="imagePickerOpen"
299
+ selection-mode="single"
300
+ mime="image/*"
301
+ @confirm="onImageConfirm"
302
+ />
303
+ </div>
298
304
  </template>
@@ -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
- /** Multiple templates for heterogeneous items (`oneOf` discriminated union). */
318
- templates?: Record<string, AnyFieldDescriptor[]>;
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;
@@ -21,6 +21,7 @@ export type {
21
21
  SeoDescriptor,
22
22
  MapDescriptor,
23
23
  RepeaterDescriptor,
24
+ RepeaterTemplate,
24
25
  MediaDescriptor,
25
26
  MediaM2mDescriptor,
26
27
  EnhancedMediaDescriptor,
@@ -0,0 +1,80 @@
1
+ import type {
2
+ AnyFieldDescriptor,
3
+ NormalizedRepeaterTemplate,
4
+ RepeaterDescriptor,
5
+ RepeaterTemplate,
6
+ } from "../types/descriptor.js";
7
+
8
+ /** Above this many templates, `picker: "auto"` switches from the dropdown to the modal. */
9
+ export const MODAL_PICKER_THRESHOLD = 3;
10
+
11
+ /**
12
+ * Turns `attrs.templates` into a list with defaults applied. A bare field array
13
+ * becomes a template whose label is its key.
14
+ */
15
+ export function normalizeTemplates(
16
+ templates: NonNullable<RepeaterDescriptor["attrs"]>["templates"],
17
+ ): NormalizedRepeaterTemplate[] {
18
+ return Object.entries(templates ?? {}).map(([key, tpl]) => {
19
+ const meta: Partial<RepeaterTemplate> = Array.isArray(tpl) ? {} : tpl;
20
+ return {
21
+ key,
22
+ label: meta.label ?? key,
23
+ description: meta.description,
24
+ icon: meta.icon,
25
+ image: meta.image,
26
+ wireframe: meta.wireframe,
27
+ category: meta.category,
28
+ fields: Array.isArray(tpl) ? tpl : (tpl.fields ?? []),
29
+ };
30
+ });
31
+ }
32
+
33
+ /** Whether the block picker modal replaces the dropdown for this many templates. */
34
+ export function shouldUseModalPicker(
35
+ picker: "auto" | "select" | "modal" | undefined,
36
+ templateCount: number,
37
+ ): boolean {
38
+ if (templateCount === 0) return false;
39
+ const mode = picker ?? "auto";
40
+ return (
41
+ mode === "modal" ||
42
+ (mode === "auto" && templateCount > MODAL_PICKER_THRESHOLD)
43
+ );
44
+ }
45
+
46
+ /** Filters templates by category and by a case-insensitive text query. */
47
+ export function filterTemplates(
48
+ templates: NormalizedRepeaterTemplate[],
49
+ query: string,
50
+ category: string,
51
+ ): NormalizedRepeaterTemplate[] {
52
+ const q = query.trim().toLowerCase();
53
+ return templates.filter((tpl) => {
54
+ if (category && tpl.category !== category) return false;
55
+ if (!q) return true;
56
+ return [tpl.label, tpl.description, tpl.category].some((v) =>
57
+ v?.toLowerCase().includes(q),
58
+ );
59
+ });
60
+ }
61
+
62
+ export type WireframeShape = "line" | "lines" | "box" | "check";
63
+
64
+ /** One schematic shape per field type, capped at 5 so it fits a picker card. */
65
+ export function wireframeShapes(
66
+ fields: AnyFieldDescriptor[],
67
+ ): WireframeShape[] {
68
+ return fields.slice(0, 5).map((f) => {
69
+ const type = String(f.type);
70
+ if (type === "textarea" || type === "editor") return "lines";
71
+ if (type.includes("media") || type === "file") return "box";
72
+ if (type === "boolean") return "check";
73
+ return "line";
74
+ });
75
+ }
76
+
77
+ /** True when adding one more item reaches `maxItems`. */
78
+ export function reachesMax(current: number, maxItems?: number): boolean {
79
+ return !!maxItems && current + 1 >= maxItems;
80
+ }
@@ -0,0 +1,44 @@
1
+ import type { AnyFieldDescriptor } from "../types/index.js";
2
+ import type { TabEntry } from "../types/layout.js";
3
+
4
+ /**
5
+ * Whether a field shows for this model: neither its `visible` predicate, which
6
+ * removes it, nor its `show` predicate, which hides it, keeps it out.
7
+ */
8
+ export function isFieldShown(
9
+ field: AnyFieldDescriptor,
10
+ model: Record<string, unknown>,
11
+ ): boolean {
12
+ const removed = field.visible ? !field.visible({ model }) : false;
13
+ const hidden = field.show ? !field.show({ model }) : false;
14
+ return !removed && !hidden;
15
+ }
16
+
17
+ /**
18
+ * Whether a tab has a field to show for this model, in one of its groups (sub-tabs
19
+ * included) or in one of its child tabs.
20
+ */
21
+ export function tabHasShownFields(
22
+ tab: TabEntry,
23
+ model: Record<string, unknown>,
24
+ ): boolean {
25
+ for (const group of tab.groups.values()) {
26
+ const fields = [...group.fields, ...[...group.subtabs.values()].flat()];
27
+ if (fields.some((field) => isFieldShown(field, model))) return true;
28
+ }
29
+ return [...tab.children.values()].some((child) =>
30
+ tabHasShownFields(child, model),
31
+ );
32
+ }
33
+
34
+ /**
35
+ * The tab to open among the ones shown: the tab the user picked, while it is
36
+ * shown; otherwise the first one. Empty when no tab is shown.
37
+ */
38
+ export function pickActiveTab(
39
+ shownTabs: TabEntry[],
40
+ picked: string | null,
41
+ ): string {
42
+ const pickedTab = shownTabs.find((tab) => tab.name === picked);
43
+ return (pickedTab ?? shownTabs[0])?.name ?? "";
44
+ }