@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.
- 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,244 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { h, nextTick, onUnmounted, shallowRef } from "vue";
|
|
3
|
+
import type { App, Component, SetupContext, VNode } from "vue";
|
|
4
|
+
|
|
5
|
+
import MapoFormAnnotation from "../runtime/components/MapoFormAnnotation.vue";
|
|
6
|
+
import type { FieldAnnotation } from "../runtime/types/index.js";
|
|
7
|
+
import { refocusField } from "../runtime/utils/fieldFocus.js";
|
|
8
|
+
|
|
9
|
+
// The focus is the browser's: the test sees when the mark gives it back.
|
|
10
|
+
vi.mock("../runtime/utils/fieldFocus.js", () => ({
|
|
11
|
+
refocusField: vi.fn(() => true),
|
|
12
|
+
}));
|
|
13
|
+
import {
|
|
14
|
+
createApp,
|
|
15
|
+
findAll,
|
|
16
|
+
headlessNode,
|
|
17
|
+
textOf,
|
|
18
|
+
} from "./headlessRenderer.js";
|
|
19
|
+
import type { HeadlessNode } from "./headlessRenderer.js";
|
|
20
|
+
|
|
21
|
+
// `UPopover` renders its trigger and, as if open, its content; it keeps the
|
|
22
|
+
// `content` and `ui` props the annotation passes.
|
|
23
|
+
const popover = { content: undefined as unknown, ui: undefined as unknown };
|
|
24
|
+
const UPopover: Component = {
|
|
25
|
+
name: "UPopover",
|
|
26
|
+
props: { content: Object, ui: Object },
|
|
27
|
+
setup(props: { content?: object; ui?: object }, { slots }: SetupContext) {
|
|
28
|
+
return () => {
|
|
29
|
+
popover.content = props.content;
|
|
30
|
+
popover.ui = props.ui;
|
|
31
|
+
return h("div", null, [
|
|
32
|
+
slots.default?.({ open: true }),
|
|
33
|
+
slots.content?.(),
|
|
34
|
+
]);
|
|
35
|
+
};
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// `UButton` declares what the annotation binds: `icon`, `color` and `label` as
|
|
40
|
+
// props, the accessible name as an attribute, `click` as the event.
|
|
41
|
+
interface ButtonStub {
|
|
42
|
+
readonly icon: unknown;
|
|
43
|
+
readonly color: unknown;
|
|
44
|
+
readonly label: unknown;
|
|
45
|
+
readonly ariaLabel: unknown;
|
|
46
|
+
click(): void;
|
|
47
|
+
}
|
|
48
|
+
const buttons: ButtonStub[] = [];
|
|
49
|
+
|
|
50
|
+
const UButton: Component = {
|
|
51
|
+
name: "UButton",
|
|
52
|
+
inheritAttrs: false,
|
|
53
|
+
props: { icon: String, color: String, label: String },
|
|
54
|
+
emits: ["click"],
|
|
55
|
+
setup(
|
|
56
|
+
props: { icon?: string; color?: string; label?: string },
|
|
57
|
+
{ attrs, emit, slots }: SetupContext<["click"]>,
|
|
58
|
+
) {
|
|
59
|
+
const stub: ButtonStub = {
|
|
60
|
+
get icon() {
|
|
61
|
+
return props.icon;
|
|
62
|
+
},
|
|
63
|
+
get color() {
|
|
64
|
+
return props.color;
|
|
65
|
+
},
|
|
66
|
+
get label() {
|
|
67
|
+
return props.label;
|
|
68
|
+
},
|
|
69
|
+
get ariaLabel() {
|
|
70
|
+
return attrs["aria-label"];
|
|
71
|
+
},
|
|
72
|
+
click: () => emit("click"),
|
|
73
|
+
};
|
|
74
|
+
buttons.push(stub);
|
|
75
|
+
onUnmounted(() => {
|
|
76
|
+
buttons.splice(buttons.indexOf(stub), 1);
|
|
77
|
+
});
|
|
78
|
+
return () => h("button", null, slots.default?.());
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
let app: App<HeadlessNode> | undefined;
|
|
83
|
+
|
|
84
|
+
/** Mounts the annotation and returns the root node of what it renders. */
|
|
85
|
+
function mountAnnotation(
|
|
86
|
+
annotation: FieldAnnotation,
|
|
87
|
+
slots: Record<string, (bindings: never) => VNode[]> = {},
|
|
88
|
+
extra: { field?: string } = {},
|
|
89
|
+
) {
|
|
90
|
+
const root = headlessNode();
|
|
91
|
+
const host: Component = {
|
|
92
|
+
render: () => h(MapoFormAnnotation, { annotation, ...extra }, slots),
|
|
93
|
+
};
|
|
94
|
+
app = createApp(host);
|
|
95
|
+
app.component("UPopover", UPopover);
|
|
96
|
+
app.component("UButton", UButton);
|
|
97
|
+
app.mount(root);
|
|
98
|
+
return root;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** The dot on the mark: the only element hidden from assistive technology. */
|
|
102
|
+
const dots = (root: HeadlessNode) =>
|
|
103
|
+
findAll(root, (node) => node.props["aria-hidden"] === "true");
|
|
104
|
+
|
|
105
|
+
afterEach(() => {
|
|
106
|
+
app?.unmount();
|
|
107
|
+
app = undefined;
|
|
108
|
+
vi.mocked(refocusField).mockClear();
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
describe("MapoFormAnnotation", () => {
|
|
112
|
+
it("names the mark with the label and falls back to the default icon and color", () => {
|
|
113
|
+
const root = mountAnnotation({ label: "Note" });
|
|
114
|
+
|
|
115
|
+
expect(buttons).toHaveLength(1);
|
|
116
|
+
expect(buttons[0]).toMatchObject({
|
|
117
|
+
icon: "i-lucide-info",
|
|
118
|
+
color: "neutral",
|
|
119
|
+
ariaLabel: "Note",
|
|
120
|
+
});
|
|
121
|
+
expect(dots(root)).toHaveLength(0);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("shows the title, the description and a dot when asked", () => {
|
|
125
|
+
const root = mountAnnotation({
|
|
126
|
+
label: "To check",
|
|
127
|
+
icon: "i-lucide-sparkles",
|
|
128
|
+
color: "warning",
|
|
129
|
+
dot: true,
|
|
130
|
+
description: "No citation found in the source.",
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
expect(buttons[0]).toMatchObject({
|
|
134
|
+
icon: "i-lucide-sparkles",
|
|
135
|
+
color: "warning",
|
|
136
|
+
});
|
|
137
|
+
expect(textOf(root)).toBe("To check No citation found in the source.");
|
|
138
|
+
expect(dots(root)).toHaveLength(1);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("renders one button per action and runs it on click", () => {
|
|
142
|
+
const confirm = vi.fn();
|
|
143
|
+
mountAnnotation({
|
|
144
|
+
label: "Suggested by AI",
|
|
145
|
+
actions: [
|
|
146
|
+
{ label: "Confirm", icon: "i-lucide-check", onClick: confirm },
|
|
147
|
+
{ label: "Restore", color: "error", onClick: vi.fn() },
|
|
148
|
+
],
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
const actions = buttons.filter((button) => button.label);
|
|
152
|
+
expect(actions).toMatchObject([
|
|
153
|
+
{ label: "Confirm", icon: "i-lucide-check", color: "neutral" },
|
|
154
|
+
{ label: "Restore", color: "error" },
|
|
155
|
+
]);
|
|
156
|
+
actions[0]?.click();
|
|
157
|
+
expect(confirm).toHaveBeenCalledTimes(1);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("gives the focus back to its field once an action ran and Vue updated", async () => {
|
|
161
|
+
// Keep takes the mark away with the button pressed: the browser would
|
|
162
|
+
// drop the focus on the page.
|
|
163
|
+
const steps: string[] = [];
|
|
164
|
+
vi.mocked(refocusField).mockImplementation((id) => {
|
|
165
|
+
steps.push(`refocus ${id}`);
|
|
166
|
+
return true;
|
|
167
|
+
});
|
|
168
|
+
mountAnnotation(
|
|
169
|
+
{
|
|
170
|
+
label: "Filled by the AI",
|
|
171
|
+
actions: [{ label: "Keep", onClick: () => steps.push("keep") }],
|
|
172
|
+
},
|
|
173
|
+
{},
|
|
174
|
+
{ field: "field-title" },
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
buttons.find((button) => button.label === "Keep")?.click();
|
|
178
|
+
expect(steps).toEqual(["keep"]);
|
|
179
|
+
await nextTick();
|
|
180
|
+
|
|
181
|
+
expect(steps).toEqual(["keep", "refocus field-title"]);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it("leaves the focus alone when it knows no field", async () => {
|
|
185
|
+
mountAnnotation({
|
|
186
|
+
label: "Note",
|
|
187
|
+
actions: [{ label: "Done", onClick: vi.fn() }],
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
buttons.find((button) => button.label === "Done")?.click();
|
|
191
|
+
await nextTick();
|
|
192
|
+
|
|
193
|
+
expect(refocusField).not.toHaveBeenCalled();
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it("keeps two actions with one label apart when the actions change", async () => {
|
|
197
|
+
// An app's own mark may repeat a label: Vue warns about duplicate keys
|
|
198
|
+
// when it patches the buttons, and may reuse one for another action.
|
|
199
|
+
const action = (label: string) => ({ label, onClick: vi.fn() });
|
|
200
|
+
const annotation = shallowRef<FieldAnnotation>({
|
|
201
|
+
label: "Note",
|
|
202
|
+
actions: ["Apply", "Open", "Open", "Ignore"].map(action),
|
|
203
|
+
});
|
|
204
|
+
const warnings: string[] = [];
|
|
205
|
+
const host: Component = {
|
|
206
|
+
render: () => h(MapoFormAnnotation, { annotation: annotation.value }),
|
|
207
|
+
};
|
|
208
|
+
app = createApp(host);
|
|
209
|
+
app.config.warnHandler = (message) => {
|
|
210
|
+
warnings.push(message);
|
|
211
|
+
};
|
|
212
|
+
app.component("UPopover", UPopover);
|
|
213
|
+
app.component("UButton", UButton);
|
|
214
|
+
app.mount(headlessNode());
|
|
215
|
+
|
|
216
|
+
const actions = ["Restore", "Open", "Open", "Done"].map(action);
|
|
217
|
+
annotation.value = { label: "Note", actions };
|
|
218
|
+
await nextTick();
|
|
219
|
+
|
|
220
|
+
expect(warnings.filter((one) => one.includes("Duplicate keys"))).toEqual(
|
|
221
|
+
[],
|
|
222
|
+
);
|
|
223
|
+
const labels = buttons.flatMap((button) =>
|
|
224
|
+
typeof button.label === "string" ? [button.label] : [],
|
|
225
|
+
);
|
|
226
|
+
expect(labels.sort()).toEqual(["Done", "Open", "Open", "Restore"]);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it("names the popover and lifts it above the expand panel and the focus portal", () => {
|
|
230
|
+
mountAnnotation({ label: "Suggested by AI" });
|
|
231
|
+
|
|
232
|
+
expect(popover.content).toMatchObject({ "aria-label": "Suggested by AI" });
|
|
233
|
+
expect(popover.ui).toEqual({ content: "z-[10000]" });
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it("lets the body slot replace the description", () => {
|
|
237
|
+
const root = mountAnnotation(
|
|
238
|
+
{ label: "Suggested by AI", description: "Replaced" },
|
|
239
|
+
{ body: () => [h("q", null, "From page 1")] },
|
|
240
|
+
);
|
|
241
|
+
|
|
242
|
+
expect(textOf(root)).toBe("Suggested by AI From page 1");
|
|
243
|
+
});
|
|
244
|
+
});
|