@mapomodule/form 2.0.0-beta.9 → 2.0.0-experimental.1

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 (145) hide show
  1. package/README.md +38 -119
  2. package/dist/index.d.mts +2 -1
  3. package/dist/index.d.ts +3 -1
  4. package/dist/index.d.ts.map +1 -1
  5. package/dist/index.mjs +1 -0
  6. package/dist/module.d.mts +1 -0
  7. package/dist/module.d.ts.map +1 -1
  8. package/dist/module.json +1 -1
  9. package/dist/module.mjs +9 -0
  10. package/dist/runtime/components/MapoFocusPortal.vue +9 -2
  11. package/dist/runtime/components/MapoFocusPortal.vue.d.ts.map +1 -1
  12. package/dist/runtime/components/MapoForm.vue +28 -6
  13. package/dist/runtime/components/MapoForm.vue.d.ts +13 -1
  14. package/dist/runtime/components/MapoForm.vue.d.ts.map +1 -1
  15. package/dist/runtime/components/MapoFormAnnotation.vue +79 -0
  16. package/dist/runtime/components/MapoFormAnnotation.vue.d.ts +27 -0
  17. package/dist/runtime/components/MapoFormAnnotation.vue.d.ts.map +1 -0
  18. package/dist/runtime/components/MapoFormAnnotationBadges.vue +28 -0
  19. package/dist/runtime/components/MapoFormAnnotationBadges.vue.d.ts +13 -0
  20. package/dist/runtime/components/MapoFormAnnotationBadges.vue.d.ts.map +1 -0
  21. package/dist/runtime/components/MapoFormField.vue +47 -11
  22. package/dist/runtime/components/MapoFormField.vue.d.ts +15 -9
  23. package/dist/runtime/components/MapoFormField.vue.d.ts.map +1 -1
  24. package/dist/runtime/components/MapoFormGroup.vue +18 -2
  25. package/dist/runtime/components/MapoFormGroup.vue.d.ts.map +1 -1
  26. package/dist/runtime/components/MapoFormTabs.vue +50 -8
  27. package/dist/runtime/components/MapoFormTabs.vue.d.ts.map +1 -1
  28. package/dist/runtime/components/fields/MapoMediaField.vue +5 -3
  29. package/dist/runtime/components/fields/MapoMediaField.vue.d.ts.map +1 -1
  30. package/dist/runtime/components/fields/MapoMediaM2mField.vue +3 -2
  31. package/dist/runtime/components/fields/MapoMediaM2mField.vue.d.ts.map +1 -1
  32. package/dist/runtime/components/fields/MapoRepeater.vue +73 -14
  33. package/dist/runtime/components/fields/MapoRepeater.vue.d.ts.map +1 -1
  34. package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue +167 -0
  35. package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue.d.ts +20 -0
  36. package/dist/runtime/components/fields/MapoRepeaterBlockPicker.vue.d.ts.map +1 -0
  37. package/dist/runtime/components/fields/MapoRepeaterItem.vue +46 -10
  38. package/dist/runtime/components/fields/MapoRepeaterItem.vue.d.ts +6 -2
  39. package/dist/runtime/components/fields/MapoRepeaterItem.vue.d.ts.map +1 -1
  40. package/dist/runtime/components/fields/MapoWygEditor.vue +19 -13
  41. package/dist/runtime/components/fields/MapoWygEditor.vue.d.ts.map +1 -1
  42. package/dist/runtime/composables/useFieldAnnotation.d.ts +18 -0
  43. package/dist/runtime/composables/useFieldAnnotation.d.ts.map +1 -0
  44. package/dist/runtime/composables/useFieldAnnotation.js +16 -0
  45. package/dist/runtime/composables/useFocusMode.d.ts +3 -1
  46. package/dist/runtime/composables/useFocusMode.d.ts.map +1 -1
  47. package/dist/runtime/composables/useFormFromSchema.d.ts +23 -1
  48. package/dist/runtime/composables/useFormFromSchema.d.ts.map +1 -1
  49. package/dist/runtime/composables/useFormFromSchema.js +171 -32
  50. package/dist/runtime/composables/useMapoForm.d.ts +21 -3
  51. package/dist/runtime/composables/useMapoForm.d.ts.map +1 -1
  52. package/dist/runtime/composables/useMapoForm.js +49 -15
  53. package/dist/runtime/types/annotation.d.ts +43 -0
  54. package/dist/runtime/types/annotation.d.ts.map +1 -0
  55. package/dist/runtime/types/annotation.js +0 -0
  56. package/dist/runtime/types/descriptor.d.ts +81 -4
  57. package/dist/runtime/types/descriptor.d.ts.map +1 -1
  58. package/dist/runtime/types/index.d.ts +2 -1
  59. package/dist/runtime/types/index.d.ts.map +1 -1
  60. package/dist/runtime/utils/annotations.d.ts +40 -0
  61. package/dist/runtime/utils/annotations.d.ts.map +1 -0
  62. package/dist/runtime/utils/annotations.js +78 -0
  63. package/dist/runtime/utils/draft.d.ts +21 -0
  64. package/dist/runtime/utils/draft.d.ts.map +1 -0
  65. package/dist/runtime/utils/draft.js +108 -0
  66. package/dist/runtime/utils/fieldComponent.d.ts +9 -0
  67. package/dist/runtime/utils/fieldComponent.d.ts.map +1 -0
  68. package/dist/runtime/utils/fieldComponent.js +12 -0
  69. package/dist/runtime/utils/fieldFocus.d.ts +24 -0
  70. package/dist/runtime/utils/fieldFocus.d.ts.map +1 -0
  71. package/dist/runtime/utils/fieldFocus.js +41 -0
  72. package/dist/runtime/utils/fieldsToJsonSchema.d.ts +38 -0
  73. package/dist/runtime/utils/fieldsToJsonSchema.d.ts.map +1 -0
  74. package/dist/runtime/utils/fieldsToJsonSchema.js +248 -0
  75. package/dist/runtime/utils/repeaterTemplates.d.ts +26 -0
  76. package/dist/runtime/utils/repeaterTemplates.d.ts.map +1 -0
  77. package/dist/runtime/utils/repeaterTemplates.js +51 -0
  78. package/dist/runtime/utils/reveal.d.ts +34 -0
  79. package/dist/runtime/utils/reveal.d.ts.map +1 -0
  80. package/dist/runtime/utils/reveal.js +29 -0
  81. package/dist/runtime/utils/tabVisibility.d.ts +18 -0
  82. package/dist/runtime/utils/tabVisibility.d.ts.map +1 -0
  83. package/dist/runtime/utils/tabVisibility.js +18 -0
  84. package/package.json +5 -5
  85. package/src/__tests__/MapoFocusPortal.component.test.ts +97 -0
  86. package/src/__tests__/MapoForm.component.test.ts +79 -0
  87. package/src/__tests__/MapoFormAnnotation.component.test.ts +244 -0
  88. package/src/__tests__/MapoFormField.component.test.ts +508 -0
  89. package/src/__tests__/MapoFormGroup.component.test.ts +141 -0
  90. package/src/__tests__/MapoFormTabs.component.test.ts +254 -0
  91. package/src/__tests__/MapoRepeaterItem.component.test.ts +286 -0
  92. package/src/__tests__/MapoWygEditor.component.test.ts +95 -0
  93. package/src/__tests__/annotations.test.ts +227 -0
  94. package/src/__tests__/draft.test.ts +221 -0
  95. package/src/__tests__/fieldComponent.test.ts +31 -0
  96. package/src/__tests__/fieldExtensions.test.ts +63 -0
  97. package/src/__tests__/fieldFocus.test.ts +178 -0
  98. package/src/__tests__/fieldsToJsonSchema.test.ts +630 -0
  99. package/src/__tests__/headlessRenderer.ts +85 -0
  100. package/src/__tests__/repeaterTemplates.test.ts +154 -0
  101. package/src/__tests__/reveal.test.ts +89 -0
  102. package/src/__tests__/schemaSources.test.ts +132 -0
  103. package/src/__tests__/tabVisibility.test.ts +102 -0
  104. package/src/__tests__/useFormFromSchema.test.ts +387 -0
  105. package/src/__tests__/useMapoForm.test.ts +213 -2
  106. package/src/index.ts +9 -0
  107. package/src/module.ts +9 -0
  108. package/src/runtime/components/MapoFocusPortal.vue +15 -2
  109. package/src/runtime/components/MapoForm.vue +47 -4
  110. package/src/runtime/components/MapoFormAnnotation.vue +113 -0
  111. package/src/runtime/components/MapoFormAnnotationBadges.vue +41 -0
  112. package/src/runtime/components/MapoFormField.vue +72 -11
  113. package/src/runtime/components/MapoFormGroup.vue +20 -2
  114. package/src/runtime/components/MapoFormTabs.vue +64 -8
  115. package/src/runtime/components/fields/MapoMediaField.vue +5 -3
  116. package/src/runtime/components/fields/MapoMediaM2mField.vue +3 -2
  117. package/src/runtime/components/fields/MapoRepeater.vue +82 -14
  118. package/src/runtime/components/fields/MapoRepeaterBlockPicker.vue +188 -0
  119. package/src/runtime/components/fields/MapoRepeaterItem.vue +56 -8
  120. package/src/runtime/components/fields/MapoWygEditor.vue +22 -14
  121. package/src/runtime/composables/useFieldAnnotation.ts +38 -0
  122. package/src/runtime/composables/useFocusMode.ts +7 -1
  123. package/src/runtime/composables/useFormFromSchema.ts +303 -41
  124. package/src/runtime/composables/useMapoForm.ts +103 -23
  125. package/src/runtime/types/annotation.ts +53 -0
  126. package/src/runtime/types/descriptor.ts +94 -4
  127. package/src/runtime/types/index.ts +10 -0
  128. package/src/runtime/utils/annotations.ts +169 -0
  129. package/src/runtime/utils/draft.ts +158 -0
  130. package/src/runtime/utils/fieldComponent.ts +34 -0
  131. package/src/runtime/utils/fieldFocus.ts +73 -0
  132. package/src/runtime/utils/fieldsToJsonSchema.ts +405 -0
  133. package/src/runtime/utils/repeaterTemplates.ts +103 -0
  134. package/src/runtime/utils/reveal.ts +68 -0
  135. package/src/runtime/utils/tabVisibility.ts +44 -0
  136. package/dist/__tests__/__mocks__/nuxt-imports.d.ts +0 -4
  137. package/dist/__tests__/__mocks__/nuxt-imports.d.ts.map +0 -1
  138. package/dist/__tests__/camomillaRoundTrip.test.d.ts +0 -2
  139. package/dist/__tests__/camomillaRoundTrip.test.d.ts.map +0 -1
  140. package/dist/__tests__/registry.test.d.ts +0 -2
  141. package/dist/__tests__/registry.test.d.ts.map +0 -1
  142. package/dist/__tests__/useFormFromSchema.test.d.ts +0 -2
  143. package/dist/__tests__/useFormFromSchema.test.d.ts.map +0 -1
  144. package/dist/__tests__/useMapoForm.test.d.ts +0 -2
  145. package/dist/__tests__/useMapoForm.test.d.ts.map +0 -1
@@ -0,0 +1,254 @@
1
+ import { afterEach, describe, expect, it, vi } from "vitest";
2
+ import { h, nextTick, onUnmounted, provide, ref } from "vue";
3
+ import type { App, Component, Ref, SetupContext } from "vue";
4
+
5
+ import MapoFormTabs from "../runtime/components/MapoFormTabs.vue";
6
+ import { FORM_KEY } from "../runtime/composables/useMapoForm.js";
7
+ import type { MapoFormContext } from "../runtime/composables/useMapoForm.js";
8
+ import type {
9
+ AnyFieldDescriptor,
10
+ FieldAnnotation,
11
+ FieldAnnotations,
12
+ } from "../runtime/types/index.js";
13
+ import type { TabEntry } from "../runtime/types/layout.js";
14
+ import type { FieldRevealRequest } from "../runtime/utils/reveal.js";
15
+ import {
16
+ createApp,
17
+ findAll,
18
+ headlessNode,
19
+ textOf,
20
+ } from "./headlessRenderer.js";
21
+ import type { HeadlessNode } from "./headlessRenderer.js";
22
+
23
+ // `useMapoForm.ts` pulls in `nuxt/app` through `useMapoT`, which cannot resolve
24
+ // outside a Nuxt build. The tabs only inject the form context.
25
+ vi.mock("@mapomodule/i18n/runtime/composables/useMapoT", () => ({
26
+ useMapoT: () => (key: string) => key,
27
+ }));
28
+ // The translation shows its key and its parameters, so a test can read them.
29
+ vi.mock("vue-i18n", () => ({
30
+ useI18n: () => ({
31
+ t: (key: string, named: Record<string, unknown> = {}) =>
32
+ `${key} ${JSON.stringify(named)}`,
33
+ }),
34
+ }));
35
+ // The panels are out of scope: the tests look at the tab bar.
36
+ vi.mock("../runtime/components/MapoFormGroup.vue", () => ({
37
+ default: { name: "MapoFormGroup", render: () => null },
38
+ }));
39
+ vi.mock("../runtime/components/MapoFormFlatSection.vue", () => ({
40
+ default: { name: "MapoFormFlatSection", render: () => null },
41
+ }));
42
+
43
+ // `UBadge` declares what the tab binds: `icon`, `color` and `label` as props,
44
+ // the role and the accessible name as attributes.
45
+ interface BadgeStub {
46
+ readonly icon: unknown;
47
+ readonly color: unknown;
48
+ readonly label: unknown;
49
+ readonly role: unknown;
50
+ readonly ariaLabel: unknown;
51
+ }
52
+ const badges: BadgeStub[] = [];
53
+
54
+ const UBadge: Component = {
55
+ name: "UBadge",
56
+ inheritAttrs: false,
57
+ props: { icon: String, color: String, label: [String, Number] },
58
+ setup(
59
+ props: { icon?: string; color?: string; label?: string | number },
60
+ { attrs }: SetupContext,
61
+ ) {
62
+ const stub: BadgeStub = {
63
+ get icon() {
64
+ return props.icon;
65
+ },
66
+ get color() {
67
+ return props.color;
68
+ },
69
+ get label() {
70
+ return props.label;
71
+ },
72
+ get role() {
73
+ return attrs.role;
74
+ },
75
+ get ariaLabel() {
76
+ return attrs["aria-label"];
77
+ },
78
+ };
79
+ badges.push(stub);
80
+ onUnmounted(() => {
81
+ badges.splice(badges.indexOf(stub), 1);
82
+ });
83
+ return () => h("span");
84
+ },
85
+ };
86
+
87
+ const tab = (name: string, fields: AnyFieldDescriptor[]): TabEntry => ({
88
+ name,
89
+ groups: new Map([["__flat__", { fields, subtabs: new Map() }]]),
90
+ children: new Map(),
91
+ });
92
+
93
+ const tabs = [
94
+ tab("content", [
95
+ { key: "title", type: "text" },
96
+ { key: "slug", type: "text" },
97
+ { key: "summary", type: "textarea" },
98
+ ]),
99
+ tab("seo", [{ key: "seo_title", type: "text" }]),
100
+ ];
101
+
102
+ let app: App<HeadlessNode> | undefined;
103
+
104
+ function mountTabs(
105
+ annotations: Ref<FieldAnnotations>,
106
+ reveal?: Ref<FieldRevealRequest | undefined>,
107
+ entries: TabEntry[] = tabs,
108
+ ) {
109
+ const context = {
110
+ model: ref({}),
111
+ errors: ref({}),
112
+ currentLang: ref(""),
113
+ annotations,
114
+ reveal,
115
+ } as unknown as MapoFormContext<Record<string, unknown>>;
116
+ const host: Component = {
117
+ setup() {
118
+ provide(FORM_KEY, context);
119
+ return () => h(MapoFormTabs, { tabs: entries });
120
+ },
121
+ };
122
+ app = createApp(host);
123
+ app.component("UBadge", UBadge);
124
+ const root = headlessNode();
125
+ app.mount(root);
126
+ return root;
127
+ }
128
+
129
+ /** The names of the tabs that show as open, outer tabs first. */
130
+ function openTabs(root: HeadlessNode): string[] {
131
+ return findAll(root, (node) => node.tag === "button")
132
+ .filter((button) =>
133
+ String(button.props.class).includes("border-primary-500"),
134
+ )
135
+ .map(textOf);
136
+ }
137
+
138
+ afterEach(() => {
139
+ app?.unmount();
140
+ app = undefined;
141
+ });
142
+
143
+ describe("MapoFormTabs annotations", () => {
144
+ const ai: FieldAnnotation = {
145
+ label: "Suggested by AI",
146
+ icon: "i-lucide-sparkles",
147
+ color: "info",
148
+ };
149
+ const toCheck: FieldAnnotation = {
150
+ ...ai,
151
+ label: "To check",
152
+ color: "warning",
153
+ };
154
+
155
+ it("shows a badge per icon and color, with its count and an accessible name", async () => {
156
+ const annotations = ref<FieldAnnotations>({
157
+ title: ai,
158
+ summary: ai,
159
+ slug: toCheck,
160
+ seo_title: ai,
161
+ });
162
+ mountTabs(annotations);
163
+
164
+ expect(badges).toEqual([
165
+ {
166
+ icon: "i-lucide-sparkles",
167
+ color: "info",
168
+ label: 2,
169
+ role: "img",
170
+ ariaLabel:
171
+ 'mapo.form.annotationCount {"n":2,"labels":"Suggested by AI"}',
172
+ },
173
+ {
174
+ icon: "i-lucide-sparkles",
175
+ color: "warning",
176
+ label: 1,
177
+ role: "img",
178
+ ariaLabel: 'mapo.form.annotationCount {"n":1,"labels":"To check"}',
179
+ },
180
+ {
181
+ icon: "i-lucide-sparkles",
182
+ color: "info",
183
+ label: 1,
184
+ role: "img",
185
+ ariaLabel:
186
+ 'mapo.form.annotationCount {"n":1,"labels":"Suggested by AI"}',
187
+ },
188
+ ]);
189
+ });
190
+
191
+ it("updates the badges when the map changes", async () => {
192
+ const annotations = ref<FieldAnnotations>({ seo_title: ai });
193
+ mountTabs(annotations);
194
+ expect(badges.map((badge) => badge.label)).toEqual([1]);
195
+
196
+ annotations.value = { title: ai, slug: ai };
197
+ await nextTick();
198
+ expect(badges.map((badge) => badge.label)).toEqual([2]);
199
+
200
+ annotations.value = {};
201
+ await nextTick();
202
+ expect(badges).toEqual([]);
203
+ });
204
+ });
205
+
206
+ describe("MapoFormTabs reveal", () => {
207
+ /** A request of `revealField()` for the field of `key`. */
208
+ const request = (key: string): FieldRevealRequest => ({
209
+ path: key,
210
+ key,
211
+ rest: "",
212
+ settle: vi.fn(),
213
+ });
214
+
215
+ it("opens the tab that holds the field the form reveals", async () => {
216
+ const reveal = ref<FieldRevealRequest | undefined>();
217
+ const root = mountTabs(ref({}), reveal);
218
+ expect(openTabs(root)).toEqual(["content"]);
219
+
220
+ reveal.value = request("seo_title");
221
+ await nextTick();
222
+
223
+ expect(openTabs(root)).toEqual(["seo"]);
224
+ });
225
+
226
+ it("opens a child tab and the tab around it", async () => {
227
+ const more: TabEntry = {
228
+ name: "more",
229
+ groups: new Map(),
230
+ children: new Map([
231
+ ["meta", tab("meta", [{ key: "canonical", type: "text" }])],
232
+ ["social", tab("social", [{ key: "og_title", type: "text" }])],
233
+ ]),
234
+ };
235
+ const reveal = ref<FieldRevealRequest | undefined>();
236
+ const root = mountTabs(ref({}), reveal, [...tabs, more]);
237
+ expect(openTabs(root)).toEqual(["content", "meta"]);
238
+
239
+ reveal.value = request("og_title");
240
+ await nextTick();
241
+
242
+ expect(openTabs(root)).toEqual(["more", "social"]);
243
+ });
244
+
245
+ it("leaves the open tab for a field no tab holds", async () => {
246
+ const reveal = ref<FieldRevealRequest | undefined>();
247
+ const root = mountTabs(ref({}), reveal);
248
+
249
+ reveal.value = request("missing");
250
+ await nextTick();
251
+
252
+ expect(openTabs(root)).toEqual(["content"]);
253
+ });
254
+ });
@@ -0,0 +1,286 @@
1
+ import { afterEach, describe, expect, it, vi } from "vitest";
2
+ import { h, onUnmounted, provide, ref, unref } from "vue";
3
+ import type { App, Component, Ref, SetupContext } from "vue";
4
+
5
+ import MapoRepeaterItem from "../runtime/components/fields/MapoRepeaterItem.vue";
6
+ import { FORM_KEY } from "../runtime/composables/useMapoForm.js";
7
+ import type { MapoFormContext } from "../runtime/composables/useMapoForm.js";
8
+ import { useFocusMode } from "../runtime/composables/useFocusMode.js";
9
+ import { useFieldAnnotation } from "../runtime/composables/useFieldAnnotation.js";
10
+ import type {
11
+ AnyFieldDescriptor,
12
+ FieldAnnotation,
13
+ FieldAnnotations,
14
+ FieldRegistry,
15
+ } from "../runtime/types/index.js";
16
+ import type { FieldRevealRequest } from "../runtime/utils/reveal.js";
17
+ import { createApp, findAll, headlessNode } from "./headlessRenderer.js";
18
+ import type { HeadlessNode } from "./headlessRenderer.js";
19
+
20
+ // The item runs its own `useMapoForm`, which reads these at setup.
21
+ vi.mock("@mapomodule/i18n/runtime/composables/useMapoT", () => ({
22
+ useMapoT: () => (key: string) => key,
23
+ }));
24
+ vi.mock("@mapomodule/store/runtime/stores/snack", () => ({
25
+ useSnackStore: () => ({ show: vi.fn() }),
26
+ }));
27
+ vi.mock("@mapomodule/store/runtime/stores/auth", () => ({
28
+ useAuthStore: () => ({ info: null }),
29
+ }));
30
+ // Headless nodes cannot take the focus: the field's control always does here.
31
+ const focus = vi.hoisted(() => ({
32
+ control: vi.fn((_field: unknown, _id: string) => true),
33
+ }));
34
+ vi.mock(import("../runtime/utils/fieldFocus.js"), async (original) => ({
35
+ ...(await original()),
36
+ focusFieldControl: focus.control,
37
+ }));
38
+ // The translation shows its key and its parameters, so a test can read them.
39
+ vi.mock("vue-i18n", () => ({
40
+ useI18n: () => ({
41
+ t: (key: string, named: Record<string, unknown> = {}) =>
42
+ `${key} ${JSON.stringify(named)}`,
43
+ }),
44
+ }));
45
+
46
+ const suggested: FieldAnnotation = {
47
+ label: "Suggested by AI",
48
+ icon: "i-lucide-sparkles",
49
+ color: "info",
50
+ };
51
+
52
+ // A text field the registry loads lazily, as the real ones are. It reads its
53
+ // annotation as a custom field would.
54
+ const seen: { annotation: FieldAnnotation | undefined } = {
55
+ annotation: undefined,
56
+ };
57
+ const TextField: Component = {
58
+ props: { descriptor: { type: Object, required: true } },
59
+ setup(props: { descriptor?: AnyFieldDescriptor }) {
60
+ const annotation = useFieldAnnotation(
61
+ () => props.descriptor as AnyFieldDescriptor,
62
+ );
63
+ return () => {
64
+ seen.annotation = annotation.value;
65
+ return h("input");
66
+ };
67
+ },
68
+ };
69
+ const textModule = { __esModule: true, default: TextField };
70
+ const registry: FieldRegistry = {
71
+ mapping: { text: () => Promise.resolve(textModule) },
72
+ attrs: {},
73
+ accessor: {},
74
+ };
75
+
76
+ const passthrough = (name: string): Component => ({
77
+ name,
78
+ inheritAttrs: false,
79
+ setup(_props: unknown, { slots }: SetupContext) {
80
+ return () => h("span", null, slots.default?.());
81
+ },
82
+ });
83
+
84
+ // `UPopover` renders its trigger and, as if open, its content.
85
+ const UPopover: Component = {
86
+ name: "UPopover",
87
+ setup(_props: unknown, { slots }: SetupContext) {
88
+ return () =>
89
+ h("div", null, [slots.default?.({ open: true }), slots.content?.()]);
90
+ },
91
+ };
92
+
93
+ // `UButton` and `UBadge` keep what the item binds: icon, color and label as
94
+ // props, the accessible name and the click handler as attributes.
95
+ interface Stub {
96
+ readonly icon: unknown;
97
+ readonly color: unknown;
98
+ readonly label: unknown;
99
+ readonly ariaLabel: unknown;
100
+ click(): void;
101
+ }
102
+ const buttons: Stub[] = [];
103
+ const badges: Stub[] = [];
104
+
105
+ function recording(name: string, list: Stub[]): Component {
106
+ return {
107
+ name,
108
+ inheritAttrs: false,
109
+ props: { icon: String, color: String, label: [String, Number] },
110
+ setup(
111
+ props: { icon?: string; color?: string; label?: string | number },
112
+ { attrs, slots }: SetupContext,
113
+ ) {
114
+ const stub: Stub = {
115
+ get icon() {
116
+ return props.icon;
117
+ },
118
+ get color() {
119
+ return props.color;
120
+ },
121
+ get label() {
122
+ return props.label;
123
+ },
124
+ get ariaLabel() {
125
+ return attrs["aria-label"];
126
+ },
127
+ click: () => (attrs.onClick as (() => void) | undefined)?.(),
128
+ };
129
+ list.push(stub);
130
+ onUnmounted(() => {
131
+ list.splice(list.indexOf(stub), 1);
132
+ });
133
+ return () => h("span", null, slots.default?.());
134
+ },
135
+ };
136
+ }
137
+
138
+ let app: App<HeadlessNode> | undefined;
139
+
140
+ interface ItemOptions {
141
+ index?: number;
142
+ defaultExpanded?: boolean;
143
+ reveal?: Ref<FieldRevealRequest | undefined>;
144
+ }
145
+
146
+ /**
147
+ * Mounts an item of `blocks`, the first unless `index` says another, inside
148
+ * a form that marks `annotations`.
149
+ */
150
+ function mountItem(annotations: FieldAnnotations, options: ItemOptions = {}) {
151
+ const index = options.index ?? 0;
152
+ const context = {
153
+ annotations: ref(annotations),
154
+ submitted: ref(false),
155
+ immediate: true,
156
+ debounce: 0,
157
+ reveal: options.reveal,
158
+ } as unknown as MapoFormContext<Record<string, unknown>>;
159
+ const host: Component = {
160
+ setup() {
161
+ provide(FORM_KEY, context);
162
+ return () =>
163
+ h(MapoRepeaterItem, {
164
+ item: { title: "La storia" },
165
+ fields: [{ key: "title", type: "text", label: "Title" }],
166
+ index,
167
+ errorPrefix: `blocks.${index}`,
168
+ parentErrors: {},
169
+ languages: [],
170
+ currentLang: "",
171
+ readonly: false,
172
+ registry,
173
+ defaultExpanded: options.defaultExpanded ?? true,
174
+ repeaterDescriptor: { key: "blocks", type: "repeater" },
175
+ });
176
+ },
177
+ };
178
+ app = createApp(host);
179
+ for (const name of ["UIcon", "UInput", "UCheckbox"])
180
+ app.component(name, passthrough(name));
181
+ app.component("UPopover", UPopover);
182
+ app.component("UButton", recording("UButton", buttons));
183
+ app.component("UBadge", recording("UBadge", badges));
184
+ const root = headlessNode();
185
+ app.mount(root);
186
+ return root;
187
+ }
188
+
189
+ // The field component resolves its loader over a few promise jobs.
190
+ const settle = () => new Promise((resolve) => setTimeout(resolve, 0));
191
+
192
+ afterEach(() => {
193
+ app?.unmount();
194
+ app = undefined;
195
+ useFocusMode().exit();
196
+ });
197
+
198
+ describe("MapoRepeaterItem annotations", () => {
199
+ const annotations = {
200
+ "blocks.0.title": suggested,
201
+ "blocks.1.title": { ...suggested, label: "Another item" },
202
+ };
203
+
204
+ it("shows the parent's marks for this item on its fields", async () => {
205
+ mountItem(annotations);
206
+ await settle();
207
+
208
+ const marks = buttons.filter((button) => button.ariaLabel);
209
+ expect(marks.map((mark) => mark.ariaLabel)).toEqual(["Suggested by AI"]);
210
+ });
211
+
212
+ it("lets a custom field in the item read its annotation with item keys", async () => {
213
+ mountItem(annotations);
214
+ await settle();
215
+
216
+ expect(seen.annotation).toEqual(suggested);
217
+ });
218
+
219
+ it("counts them on the item header", async () => {
220
+ mountItem(annotations);
221
+ await settle();
222
+
223
+ const counts = badges.filter((badge) => badge.ariaLabel);
224
+ expect(counts).toMatchObject([
225
+ {
226
+ icon: "i-lucide-sparkles",
227
+ color: "info",
228
+ label: 1,
229
+ ariaLabel:
230
+ 'mapo.form.annotationCount {"n":1,"labels":"Suggested by AI"}',
231
+ },
232
+ ]);
233
+ });
234
+
235
+ it("hands them to focus mode", async () => {
236
+ mountItem(annotations);
237
+ await settle();
238
+
239
+ const maximize = buttons.find(
240
+ (button) => button.icon === "i-lucide-maximize-2",
241
+ );
242
+ maximize?.click();
243
+
244
+ const target = useFocusMode().focusTarget.value;
245
+ expect(unref(target?.annotations)).toEqual({ title: suggested });
246
+ });
247
+ });
248
+
249
+ describe("MapoRepeaterItem reveal", () => {
250
+ /** A request of `revealField()` for the title of the item at `index`. */
251
+ const titleOf = (index: number): FieldRevealRequest => ({
252
+ path: `blocks.${index}.title`,
253
+ key: "blocks",
254
+ rest: `${index}.title`,
255
+ settle: vi.fn(),
256
+ });
257
+
258
+ /** The controls the item renders: one per field while it is open. */
259
+ const controls = (root: HeadlessNode) =>
260
+ findAll(root, (node) => node.tag === "input");
261
+
262
+ it("opens and brings in the field the parent form reveals inside it", async () => {
263
+ const reveal = ref<FieldRevealRequest | undefined>();
264
+ const root = mountItem({}, { index: 1, defaultExpanded: false, reveal });
265
+ expect(controls(root)).toEqual([]);
266
+
267
+ const asked = titleOf(1);
268
+ reveal.value = asked;
269
+
270
+ await vi.waitFor(() => expect(asked.settle).toHaveBeenCalledWith(true));
271
+ expect(controls(root)).toHaveLength(1);
272
+ expect(focus.control.mock.calls.at(-1)?.[1]).toBe("field-title");
273
+ });
274
+
275
+ it("stays closed for a field of another item", async () => {
276
+ const reveal = ref<FieldRevealRequest | undefined>();
277
+ const root = mountItem({}, { index: 1, defaultExpanded: false, reveal });
278
+
279
+ const asked = titleOf(0);
280
+ reveal.value = asked;
281
+ await settle();
282
+
283
+ expect(controls(root)).toEqual([]);
284
+ expect(asked.settle).not.toHaveBeenCalled();
285
+ });
286
+ });
@@ -0,0 +1,95 @@
1
+ import { afterEach, describe, expect, it, vi } from "vitest";
2
+ import { h, nextTick, ref } from "vue";
3
+ import type { App, Component, SetupContext } from "vue";
4
+
5
+ import MapoWygEditor from "../runtime/components/fields/MapoWygEditor.vue";
6
+ import type { EditorDescriptor } from "../runtime/types/index.js";
7
+ import { createApp, headlessNode } from "./headlessRenderer.js";
8
+ import type { HeadlessNode } from "./headlessRenderer.js";
9
+
10
+ // Tiptap needs a DOM: the test sees the editor through what the field asks
11
+ // of it. `setEditable` is the call under test.
12
+ const tiptap = vi.hoisted(() => {
13
+ const chain: Record<string, () => unknown> = new Proxy(
14
+ {},
15
+ { get: () => () => chain },
16
+ );
17
+ return {
18
+ editor: {
19
+ setEditable: vi.fn(),
20
+ getHTML: () => "<p>Ten years of open data</p>",
21
+ isActive: () => false,
22
+ chain: () => chain,
23
+ commands: { setContent: vi.fn() },
24
+ destroy: vi.fn(),
25
+ },
26
+ };
27
+ });
28
+
29
+ vi.mock("@tiptap/vue-3", async () => {
30
+ const { shallowRef } = await import("vue");
31
+ return {
32
+ useEditor: () => shallowRef(tiptap.editor),
33
+ EditorContent: { name: "EditorContent", render: () => null },
34
+ };
35
+ });
36
+ vi.mock("@tiptap/starter-kit", () => ({ default: {} }));
37
+ vi.mock("@tiptap/extension-link", () => ({
38
+ default: { configure: () => ({}) },
39
+ }));
40
+ vi.mock("@tiptap/extension-underline", () => ({ default: {} }));
41
+ vi.mock("@tiptap/extension-image", () => ({
42
+ default: { configure: () => ({}) },
43
+ }));
44
+ vi.mock("vue-i18n", () => ({
45
+ useI18n: () => ({ t: (key: string) => key }),
46
+ }));
47
+
48
+ /** Renders its default slot: the toolbar's Nuxt UI parts. */
49
+ const passThrough = (name: string): Component => ({
50
+ name,
51
+ setup:
52
+ (_props: object, { slots }: SetupContext) =>
53
+ () =>
54
+ h("div", null, slots.default?.()),
55
+ });
56
+
57
+ let app: App<HeadlessNode> | undefined;
58
+
59
+ const descriptor: EditorDescriptor = { key: "body", type: "editor" };
60
+
61
+ afterEach(() => {
62
+ app?.unmount();
63
+ app = undefined;
64
+ tiptap.editor.setEditable.mockClear();
65
+ });
66
+
67
+ describe("MapoWygEditor", () => {
68
+ it("makes the editor read-only without an update, so the value stays the one the field holds", async () => {
69
+ const readonly = ref(false);
70
+ const updates: unknown[] = [];
71
+ const host: Component = {
72
+ render: () =>
73
+ h(MapoWygEditor, {
74
+ modelValue: "<p>Ten years of open data</p>",
75
+ descriptor,
76
+ readonly: readonly.value,
77
+ "onUpdate:modelValue": (value: unknown) => updates.push(value),
78
+ }),
79
+ };
80
+ app = createApp(host);
81
+ for (const name of ["UButton", "USeparator", "UTooltip"]) {
82
+ app.component(name, passThrough(name));
83
+ }
84
+ app.mount(headlessNode());
85
+
86
+ readonly.value = true;
87
+ await nextTick();
88
+ expect(tiptap.editor.setEditable).toHaveBeenLastCalledWith(false, false);
89
+
90
+ readonly.value = false;
91
+ await nextTick();
92
+ expect(tiptap.editor.setEditable).toHaveBeenLastCalledWith(true, false);
93
+ expect(updates).toEqual([]);
94
+ });
95
+ });