@mapomodule/form 2.0.0-beta.8 → 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 +173 -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__/article.schema.json +193 -0
  95. package/src/__tests__/camomillaRoundTrip.test.ts +73 -0
  96. package/src/__tests__/draft.test.ts +221 -0
  97. package/src/__tests__/fieldComponent.test.ts +31 -0
  98. package/src/__tests__/fieldExtensions.test.ts +63 -0
  99. package/src/__tests__/fieldFocus.test.ts +178 -0
  100. package/src/__tests__/fieldsToJsonSchema.test.ts +630 -0
  101. package/src/__tests__/headlessRenderer.ts +85 -0
  102. package/src/__tests__/repeaterTemplates.test.ts +154 -0
  103. package/src/__tests__/reveal.test.ts +89 -0
  104. package/src/__tests__/schemaSources.test.ts +132 -0
  105. package/src/__tests__/tabVisibility.test.ts +102 -0
  106. package/src/__tests__/useFormFromSchema.test.ts +387 -0
  107. package/src/__tests__/useMapoForm.test.ts +213 -2
  108. package/src/index.ts +9 -0
  109. package/src/module.ts +9 -0
  110. package/src/runtime/components/MapoFocusPortal.vue +15 -2
  111. package/src/runtime/components/MapoForm.vue +47 -4
  112. package/src/runtime/components/MapoFormAnnotation.vue +113 -0
  113. package/src/runtime/components/MapoFormAnnotationBadges.vue +41 -0
  114. package/src/runtime/components/MapoFormField.vue +72 -11
  115. package/src/runtime/components/MapoFormGroup.vue +20 -2
  116. package/src/runtime/components/MapoFormTabs.vue +64 -8
  117. package/src/runtime/components/fields/MapoMediaField.vue +5 -3
  118. package/src/runtime/components/fields/MapoMediaM2mField.vue +3 -2
  119. package/src/runtime/components/fields/MapoRepeater.vue +82 -14
  120. package/src/runtime/components/fields/MapoRepeaterBlockPicker.vue +188 -0
  121. package/src/runtime/components/fields/MapoRepeaterItem.vue +56 -8
  122. package/src/runtime/components/fields/MapoWygEditor.vue +22 -14
  123. package/src/runtime/composables/useFieldAnnotation.ts +38 -0
  124. package/src/runtime/composables/useFocusMode.ts +7 -1
  125. package/src/runtime/composables/useFormFromSchema.ts +306 -42
  126. package/src/runtime/composables/useMapoForm.ts +103 -23
  127. package/src/runtime/types/annotation.ts +53 -0
  128. package/src/runtime/types/descriptor.ts +94 -4
  129. package/src/runtime/types/index.ts +10 -0
  130. package/src/runtime/utils/annotations.ts +169 -0
  131. package/src/runtime/utils/draft.ts +158 -0
  132. package/src/runtime/utils/fieldComponent.ts +34 -0
  133. package/src/runtime/utils/fieldFocus.ts +73 -0
  134. package/src/runtime/utils/fieldsToJsonSchema.ts +405 -0
  135. package/src/runtime/utils/repeaterTemplates.ts +103 -0
  136. package/src/runtime/utils/reveal.ts +68 -0
  137. package/src/runtime/utils/tabVisibility.ts +44 -0
  138. package/dist/__tests__/__mocks__/nuxt-imports.d.ts +0 -4
  139. package/dist/__tests__/__mocks__/nuxt-imports.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,221 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { reactive } from "vue";
3
+ import { modelForDraft } from "../runtime/utils/draft.js";
4
+ import type { AnyFieldDescriptor } from "../runtime/types/index.js";
5
+
6
+ describe("modelForDraft", () => {
7
+ it("leaves out a noDraft field at its key, and keeps the others", () => {
8
+ const fields: AnyFieldDescriptor[] = [
9
+ { key: "title", type: "text" },
10
+ { key: "password", type: "text", noDraft: true },
11
+ ];
12
+ const draft = modelForDraft({ title: "Hello", password: "s3cret" }, fields);
13
+ expect(draft).toEqual({ title: "Hello" });
14
+ });
15
+
16
+ it("leaves out a noDraft field under a dotted key, and keeps its siblings", () => {
17
+ const fields: AnyFieldDescriptor[] = [
18
+ { key: "api.name", type: "text" },
19
+ { key: "api.key", type: "text", noDraft: true },
20
+ ];
21
+ const draft = modelForDraft(
22
+ { api: { name: "Maps", key: "k-123" } },
23
+ fields,
24
+ );
25
+ expect(draft).toEqual({ api: { name: "Maps" } });
26
+ });
27
+
28
+ it("leaves out a translatable noDraft field in every language and at its key", () => {
29
+ const fields: AnyFieldDescriptor[] = [
30
+ { key: "title", type: "text", translatable: true },
31
+ { key: "token", type: "text", translatable: true, noDraft: true },
32
+ ];
33
+ const draft = modelForDraft(
34
+ {
35
+ token: "root",
36
+ translations: {
37
+ en: { title: "Hello", token: "t-en" },
38
+ it: { title: "Ciao", token: "t-it" },
39
+ },
40
+ },
41
+ fields,
42
+ );
43
+ expect(draft).toEqual({
44
+ translations: { en: { title: "Hello" }, it: { title: "Ciao" } },
45
+ });
46
+ });
47
+
48
+ it("leaves out a synced noDraft field in every language", () => {
49
+ const fields: AnyFieldDescriptor[] = [
50
+ { key: "pin", type: "text", synci18n: true, noDraft: true },
51
+ ];
52
+ const draft = modelForDraft(
53
+ { translations: { en: { pin: "1234" }, it: { pin: "1234" } } },
54
+ fields,
55
+ );
56
+ expect(draft).toEqual({ translations: { en: {}, it: {} } });
57
+ });
58
+
59
+ it("keeps a value of the same name in the languages for a field that is not translated", () => {
60
+ const fields: AnyFieldDescriptor[] = [
61
+ { key: "code", type: "text", noDraft: true },
62
+ ];
63
+ const draft = modelForDraft(
64
+ { code: "x", translations: { en: { code: "label" } } },
65
+ fields,
66
+ );
67
+ expect(draft).toEqual({ translations: { en: { code: "label" } } });
68
+ });
69
+
70
+ it("leaves out a noDraft field in each item of a repeater", () => {
71
+ const fields: AnyFieldDescriptor[] = [
72
+ {
73
+ key: "speakers",
74
+ type: "repeater",
75
+ fields: [
76
+ { key: "name", type: "text" },
77
+ { key: "phone", type: "text", noDraft: true },
78
+ ],
79
+ },
80
+ ];
81
+ const draft = modelForDraft(
82
+ {
83
+ speakers: [
84
+ { name: "Ada", phone: "555" },
85
+ { name: "Grace", phone: "556" },
86
+ ],
87
+ },
88
+ fields,
89
+ );
90
+ expect(draft).toEqual({ speakers: [{ name: "Ada" }, { name: "Grace" }] });
91
+ });
92
+
93
+ it("reads each item by its template, and an item without one by the first", () => {
94
+ const fields: AnyFieldDescriptor[] = [
95
+ {
96
+ key: "blocks",
97
+ type: "repeater",
98
+ attrs: {
99
+ templates: {
100
+ login: [
101
+ { key: "user", type: "text" },
102
+ { key: "secret", type: "text", noDraft: true },
103
+ ],
104
+ text: [
105
+ { key: "body", type: "textarea" },
106
+ { key: "secret", type: "text" },
107
+ ],
108
+ },
109
+ },
110
+ },
111
+ ];
112
+ const draft = modelForDraft(
113
+ {
114
+ blocks: [
115
+ { _template: "login", user: "ada", secret: "a" },
116
+ { _template: "text", body: "Hi", secret: "kept" },
117
+ { user: "grace", secret: "b" },
118
+ ],
119
+ },
120
+ fields,
121
+ );
122
+ expect(draft).toEqual({
123
+ blocks: [
124
+ { _template: "login", user: "ada" },
125
+ { _template: "text", body: "Hi", secret: "kept" },
126
+ { user: "grace" },
127
+ ],
128
+ });
129
+ });
130
+
131
+ it("reaches repeaters in every language and inside other repeaters", () => {
132
+ const fields: AnyFieldDescriptor[] = [
133
+ {
134
+ key: "sections",
135
+ type: "repeater",
136
+ translatable: true,
137
+ fields: [
138
+ {
139
+ key: "rows",
140
+ type: "repeater",
141
+ fields: [
142
+ { key: "label", type: "text" },
143
+ { key: "otp", type: "text", noDraft: true },
144
+ ],
145
+ },
146
+ ],
147
+ },
148
+ ];
149
+ const draft = modelForDraft(
150
+ {
151
+ translations: {
152
+ en: { sections: [{ rows: [{ label: "A", otp: "1" }] }] },
153
+ it: { sections: [{ rows: [{ label: "B", otp: "2" }] }] },
154
+ },
155
+ },
156
+ fields,
157
+ );
158
+ expect(draft).toEqual({
159
+ translations: {
160
+ en: { sections: [{ rows: [{ label: "A" }] }] },
161
+ it: { sections: [{ rows: [{ label: "B" }] }] },
162
+ },
163
+ });
164
+ });
165
+
166
+ it("returns a copy, and leaves the model as it is", () => {
167
+ const fields: AnyFieldDescriptor[] = [
168
+ { key: "password", type: "text", noDraft: true },
169
+ ];
170
+ const model = reactive({ title: "Hello", password: "s3cret" });
171
+ const draft = modelForDraft(model, fields);
172
+ expect(draft).toEqual({ title: "Hello" });
173
+ expect(model).toEqual({ title: "Hello", password: "s3cret" });
174
+ });
175
+
176
+ it("goes through no key that leads to a prototype", () => {
177
+ const fields: AnyFieldDescriptor[] = [
178
+ { key: "__proto__.polluted", type: "text", noDraft: true },
179
+ { key: "constructor", type: "text", noDraft: true },
180
+ ];
181
+ const draft = modelForDraft({ title: "Hello" }, fields);
182
+ expect(draft).toEqual({ title: "Hello" });
183
+ expect(({} as Record<string, unknown>).polluted).toBeUndefined();
184
+ });
185
+
186
+ it("keeps only what JSON carries back as it was: no file, date, map or number JSON cannot write", () => {
187
+ const fields: AnyFieldDescriptor[] = [{ key: "title", type: "text" }];
188
+ const draft = modelForDraft(
189
+ {
190
+ title: "Hello",
191
+ subtitle: undefined,
192
+ cover: new Blob(["image"]),
193
+ published_on: new Date("2026-11-10T09:00:00.000Z"),
194
+ ratio: Number.NaN,
195
+ tags: ["open data", new Date(0), null],
196
+ seen: new Map([["a", 1]]),
197
+ seo: { title: "Open data", image: new Blob(["og"]) },
198
+ },
199
+ fields,
200
+ );
201
+ expect(draft).toEqual({
202
+ title: "Hello",
203
+ tags: ["open data", null],
204
+ seo: { title: "Open data" },
205
+ });
206
+ });
207
+
208
+ it("keeps a key named __proto__ as data, and ends a value met again inside itself", () => {
209
+ const fields: AnyFieldDescriptor[] = [{ key: "title", type: "text" }];
210
+ const model: Record<string, unknown> = JSON.parse(
211
+ '{"title":"Hello","__proto__":{"polluted":true}}',
212
+ );
213
+ model.self = model;
214
+
215
+ const draft = modelForDraft(model, fields) as Record<string, unknown>;
216
+
217
+ expect(Object.keys(draft)).toEqual(["title", "__proto__"]);
218
+ expect(Object.getPrototypeOf(draft)).toBe(Object.prototype);
219
+ expect(({} as Record<string, unknown>).polluted).toBeUndefined();
220
+ });
221
+ });
@@ -0,0 +1,31 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import type { Component } from "vue";
3
+ import { toFieldComponent } from "../runtime/utils/fieldComponent.js";
4
+
5
+ const loaderOf = (component: Component) => () =>
6
+ Promise.resolve({ default: component });
7
+
8
+ describe("toFieldComponent", () => {
9
+ it("wraps a lazy loader in an async component", () => {
10
+ const loader = loaderOf({ name: "TextField" });
11
+ expect(toFieldComponent(loader)).not.toBe(loader);
12
+ });
13
+
14
+ it("returns the same async component for the same loader", () => {
15
+ const loader = loaderOf({ name: "TextField" });
16
+ expect(toFieldComponent(loader)).toBe(toFieldComponent(loader));
17
+ });
18
+
19
+ it("gives each loader its own async component", () => {
20
+ const component: Component = { name: "TextField" };
21
+ expect(toFieldComponent(loaderOf(component))).not.toBe(
22
+ toFieldComponent(loaderOf(component)),
23
+ );
24
+ });
25
+
26
+ it("returns component names and components as they are", () => {
27
+ const component: Component = { name: "StarRatingField" };
28
+ expect(toFieldComponent("UInput")).toBe("UInput");
29
+ expect(toFieldComponent(component)).toBe(component);
30
+ });
31
+ });
@@ -0,0 +1,63 @@
1
+ import { describe, expect, expectTypeOf, it } from "vitest";
2
+
3
+ import type {
4
+ AnyFieldDescriptor,
5
+ FieldDescriptor,
6
+ } from "../runtime/types/index.js";
7
+ import { fieldsToJsonSchema } from "../runtime/utils/fieldsToJsonSchema.js";
8
+
9
+ // A package declares a descriptor property of its own. `pnpm typecheck`
10
+ // checks this file: the `@ts-expect-error` lines fail it when the extension
11
+ // stops being checked.
12
+ declare module "../runtime/types/descriptor.js" {
13
+ interface FieldExtensions {
14
+ example?: { hint?: string; exclude?: boolean };
15
+ }
16
+ }
17
+
18
+ describe("FieldExtensions", () => {
19
+ it("adds the declared property to every descriptor type", () => {
20
+ expectTypeOf<FieldDescriptor["example"]>().toEqualTypeOf<
21
+ { hint?: string; exclude?: boolean } | undefined
22
+ >();
23
+ expectTypeOf<AnyFieldDescriptor["example"]>().toEqualTypeOf<
24
+ { hint?: string; exclude?: boolean } | undefined
25
+ >();
26
+
27
+ const price: FieldDescriptor = {
28
+ key: "price",
29
+ type: "number",
30
+ example: { hint: "Whole euros" },
31
+ };
32
+ expect(price.example?.hint).toBe("Whole euros");
33
+ });
34
+
35
+ it("type-checks the declared property and rejects undeclared ones", () => {
36
+ const fields: FieldDescriptor[] = [
37
+ // @ts-expect-error `hnt` is not in the declared shape.
38
+ { key: "title", type: "text", example: { hnt: "typo" } },
39
+ // @ts-expect-error `undeclared` was never declared.
40
+ { key: "title", type: "text", undeclared: true },
41
+ ];
42
+ expect(fields).toHaveLength(2);
43
+ });
44
+
45
+ it("reaches the schema through the property option", () => {
46
+ const schema = fieldsToJsonSchema(
47
+ [
48
+ { key: "price", type: "number", example: { hint: "Whole euros" } },
49
+ { key: "slug", type: "text", example: { exclude: true } },
50
+ ],
51
+ {
52
+ property: (written, field) => {
53
+ if (field.example?.exclude) return undefined;
54
+ const hint = field.example?.hint;
55
+ return hint ? { ...written, description: hint } : written;
56
+ },
57
+ },
58
+ );
59
+ expect(schema.properties).toEqual({
60
+ price: { type: "number", description: "Whole euros" },
61
+ });
62
+ });
63
+ });
@@ -0,0 +1,178 @@
1
+ import { afterEach, describe, expect, it, vi } from "vitest";
2
+
3
+ import { focusFieldControl, refocusField } from "../runtime/utils/fieldFocus";
4
+
5
+ /** An element as `refocusField()` reads it: what it matches, what it holds, `focus()`. */
6
+ interface FakeElement {
7
+ matches(selector: string): boolean;
8
+ querySelector(selector: string): FakeElement | null;
9
+ focus: ReturnType<typeof vi.fn>;
10
+ }
11
+
12
+ function element(focusable: boolean, inside: FakeElement | null = null) {
13
+ const made: FakeElement = {
14
+ matches: () => focusable,
15
+ querySelector: () => inside,
16
+ focus: vi.fn(),
17
+ };
18
+ return made;
19
+ }
20
+
21
+ const body = element(false);
22
+
23
+ /** A page whose focus is on `active`, with the elements of `byId`. */
24
+ function page(active: FakeElement | null, byId: Record<string, FakeElement>) {
25
+ vi.stubGlobal("document", {
26
+ body,
27
+ activeElement: active,
28
+ getElementById: (id: string) => byId[id] ?? null,
29
+ });
30
+ }
31
+
32
+ afterEach(() => {
33
+ vi.unstubAllGlobals();
34
+ });
35
+
36
+ describe("refocusField", () => {
37
+ it("focuses the field's control when the focus fell on the page", () => {
38
+ const input = element(true);
39
+ page(body, { "field-title": input });
40
+
41
+ expect(refocusField("field-title")).toBe(true);
42
+ expect(input.focus).toHaveBeenCalledTimes(1);
43
+ });
44
+
45
+ it("focuses what takes the focus inside the field, as an editor's content", () => {
46
+ const content = element(true);
47
+ const editor = element(false, content);
48
+ page(null, { "field-body": editor });
49
+
50
+ expect(refocusField("field-body")).toBe(true);
51
+ expect(content.focus).toHaveBeenCalledTimes(1);
52
+ expect(editor.focus).not.toHaveBeenCalled();
53
+ });
54
+
55
+ it("leaves a focus held elsewhere where it is, as by a dialog", () => {
56
+ const input = element(true);
57
+ const dialog = element(true);
58
+ page(dialog, { "field-title": input });
59
+
60
+ expect(refocusField("field-title")).toBe(false);
61
+ expect(input.focus).not.toHaveBeenCalled();
62
+ });
63
+
64
+ it("does nothing for a field it cannot find, or one with nothing to focus", () => {
65
+ page(body, { "field-plain": element(false) });
66
+
67
+ expect(refocusField("field-missing")).toBe(false);
68
+ expect(refocusField("field-plain")).toBe(false);
69
+ });
70
+
71
+ it("does nothing on the server", () => {
72
+ expect(typeof document).toBe("undefined");
73
+ expect(refocusField("field-title")).toBe(false);
74
+ });
75
+ });
76
+
77
+ /** The page of {@link focusFieldControl}: what has the focus. */
78
+ interface FakeDocument {
79
+ activeElement: unknown;
80
+ }
81
+
82
+ /** A field's control: whether it takes the focus, and the focusable inside it. */
83
+ function control(doc: FakeDocument, focusable: boolean, inside?: unknown) {
84
+ const made = {
85
+ matches: () => focusable,
86
+ querySelector: () => inside ?? null,
87
+ focus: vi.fn(() => {
88
+ doc.activeElement = made;
89
+ }),
90
+ };
91
+ return made;
92
+ }
93
+
94
+ /** A field's element, holding `byId` and scrolled with `scrollIntoView`. */
95
+ function fieldElement(doc: FakeDocument, byId: Record<string, unknown>) {
96
+ return {
97
+ ownerDocument: doc,
98
+ querySelector: vi.fn((selector: string) => {
99
+ const id = /^\[id="(.*)"\]$/.exec(selector)?.[1] ?? "";
100
+ return byId[id] ?? null;
101
+ }),
102
+ scrollIntoView: vi.fn(),
103
+ };
104
+ }
105
+
106
+ describe("focusFieldControl", () => {
107
+ it("focuses the control with the field's id inside the field, and scrolls the field to the middle", () => {
108
+ const doc: FakeDocument = { activeElement: null };
109
+ const input = control(doc, true);
110
+ const field = fieldElement(doc, { "field-title": input });
111
+
112
+ expect(focusFieldControl(field, "field-title")).toBe(true);
113
+ expect(input.focus).toHaveBeenCalledWith({ preventScroll: true });
114
+ expect(field.scrollIntoView).toHaveBeenCalledWith({
115
+ block: "center",
116
+ behavior: "smooth",
117
+ });
118
+ });
119
+
120
+ it("focuses what takes the focus inside the control, as an editor's content", () => {
121
+ const doc: FakeDocument = { activeElement: null };
122
+ const content = control(doc, true);
123
+ const editor = control(doc, false, content);
124
+ const field = fieldElement(doc, { "field-body": editor });
125
+
126
+ expect(focusFieldControl(field, "field-body")).toBe(true);
127
+ expect(content.focus).toHaveBeenCalledTimes(1);
128
+ expect(editor.focus).not.toHaveBeenCalled();
129
+ });
130
+
131
+ it("does not animate the scroll when the operator asks for reduced motion", () => {
132
+ vi.stubGlobal("window", {
133
+ matchMedia: (query: string) => ({
134
+ matches: query === "(prefers-reduced-motion: reduce)",
135
+ }),
136
+ });
137
+ const doc: FakeDocument = { activeElement: null };
138
+ const field = fieldElement(doc, { "field-title": control(doc, true) });
139
+
140
+ focusFieldControl(field, "field-title");
141
+
142
+ expect(field.scrollIntoView).toHaveBeenCalledWith({
143
+ block: "center",
144
+ behavior: "auto",
145
+ });
146
+ });
147
+
148
+ it("answers false when the control does not keep the focus, as a disabled one", () => {
149
+ const doc: FakeDocument = { activeElement: null };
150
+ const disabled = { ...control(doc, true), focus: vi.fn() };
151
+ const field = fieldElement(doc, { "field-title": disabled });
152
+
153
+ expect(focusFieldControl(field, "field-title")).toBe(false);
154
+ });
155
+
156
+ it("answers false before the control renders, or with nothing to focus in it", () => {
157
+ const doc: FakeDocument = { activeElement: null };
158
+ const field = fieldElement(doc, { "field-plain": control(doc, false) });
159
+
160
+ expect(focusFieldControl(field, "field-title")).toBe(false);
161
+ expect(focusFieldControl(field, "field-plain")).toBe(false);
162
+ expect(field.scrollIntoView).not.toHaveBeenCalled();
163
+ });
164
+
165
+ it("answers false for what is not an element, as a headless node", () => {
166
+ expect(focusFieldControl(null, "field-title")).toBe(false);
167
+ expect(focusFieldControl({ tag: "div" }, "field-title")).toBe(false);
168
+ });
169
+
170
+ it("looks the control up by an id written safely into the selector", () => {
171
+ const doc: FakeDocument = { activeElement: null };
172
+ const field = fieldElement(doc, {});
173
+
174
+ focusFieldControl(field, 'field-a"b');
175
+
176
+ expect(field.querySelector).toHaveBeenCalledWith('[id="field-a\\"b"]');
177
+ });
178
+ });