@mapomodule/form 2.0.0-beta.16 → 2.0.0-beta.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/module.d.mts +13 -0
- package/dist/module.d.ts +13 -0
- package/dist/module.d.ts.map +1 -1
- package/dist/module.json +1 -1
- package/dist/module.mjs +6 -1
- package/dist/runtime/components/fields/MapoWygEditor.vue +196 -3
- package/dist/runtime/components/fields/MapoWygEditor.vue.d.ts.map +1 -1
- package/dist/runtime/devtools/DevtoolsFormsPage.vue +22 -0
- package/dist/runtime/devtools/DevtoolsFormsPage.vue.d.ts.map +1 -1
- package/dist/runtime/types/descriptor.d.ts +10 -1
- package/dist/runtime/types/descriptor.d.ts.map +1 -1
- package/dist/runtime/types/registry.d.ts +6 -1
- package/dist/runtime/types/registry.d.ts.map +1 -1
- package/package.json +3 -2
- package/src/__tests__/MapoWygEditor.component.test.ts +377 -16
- package/src/module.ts +20 -2
- package/src/runtime/components/fields/MapoWygEditor.vue +336 -3
- package/src/runtime/devtools/DevtoolsFormsPage.vue +23 -0
- package/src/runtime/types/descriptor.ts +10 -1
- package/src/runtime/types/registry.ts +3 -1
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
2
|
import { createRenderer, h, nextTick, ref } from "vue";
|
|
3
3
|
import type { App, Component, Ref } from "vue";
|
|
4
|
+
import TextAlign from "@tiptap/extension-text-align";
|
|
4
5
|
|
|
5
6
|
import MapoWygEditor from "../runtime/components/fields/MapoWygEditor.vue";
|
|
6
7
|
import type { EditorDescriptor } from "../runtime/types/index.js";
|
|
7
8
|
|
|
8
|
-
// The editor field
|
|
9
|
+
// The editor field: its toolbar (links, alignment, custom buttons) and the
|
|
10
|
+
// HTML source view (`attrs.source`).
|
|
9
11
|
// Tiptap needs a DOM, which these tests do not have: `@tiptap/vue-3` is
|
|
10
12
|
// replaced by an editor that keeps its document as an HTML string and
|
|
11
13
|
// normalizes what it is given, the way a schema would.
|
|
@@ -14,13 +16,20 @@ interface FakeEditor {
|
|
|
14
16
|
getHTML: () => string;
|
|
15
17
|
commands: { setContent: ReturnType<typeof vi.fn> };
|
|
16
18
|
setEditable: ReturnType<typeof vi.fn>;
|
|
17
|
-
isActive:
|
|
19
|
+
isActive: ReturnType<typeof vi.fn>;
|
|
20
|
+
getAttributes: ReturnType<typeof vi.fn>;
|
|
21
|
+
state: { selection: { empty: boolean } };
|
|
18
22
|
chain: () => unknown;
|
|
19
23
|
can: () => unknown;
|
|
20
24
|
destroy: ReturnType<typeof vi.fn>;
|
|
25
|
+
/** The commands run through `chain()`, in order, with their arguments. */
|
|
26
|
+
calls: [string, ...unknown[]][];
|
|
21
27
|
}
|
|
22
28
|
|
|
23
|
-
const tiptap = vi.hoisted(() => ({
|
|
29
|
+
const tiptap = vi.hoisted(() => ({
|
|
30
|
+
editor: undefined as unknown,
|
|
31
|
+
options: undefined as { extensions: { name: string }[] } | undefined,
|
|
32
|
+
}));
|
|
24
33
|
|
|
25
34
|
/**
|
|
26
35
|
* What the fake schema does to HTML: whitespace between tags goes, a `<span>`
|
|
@@ -35,9 +44,19 @@ const normalize = (html: string) =>
|
|
|
35
44
|
|
|
36
45
|
function createFakeEditor(content: string): FakeEditor {
|
|
37
46
|
let html = normalize(content);
|
|
38
|
-
|
|
47
|
+
const calls: [string, ...unknown[]][] = [];
|
|
48
|
+
// A command chain: any command returns the chain and is recorded, `run()`
|
|
49
|
+
// applies nothing. `can()` answers through a chain that records nothing.
|
|
39
50
|
const chain: unknown = new Proxy(() => chain, {
|
|
40
|
-
get:
|
|
51
|
+
get:
|
|
52
|
+
(_target, name) =>
|
|
53
|
+
(...args: unknown[]) => {
|
|
54
|
+
calls.push([String(name), ...args]);
|
|
55
|
+
return chain;
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
const dryRun: unknown = new Proxy(() => dryRun, {
|
|
59
|
+
get: () => () => dryRun,
|
|
41
60
|
});
|
|
42
61
|
return {
|
|
43
62
|
getHTML: () => html,
|
|
@@ -47,17 +66,24 @@ function createFakeEditor(content: string): FakeEditor {
|
|
|
47
66
|
}),
|
|
48
67
|
},
|
|
49
68
|
setEditable: vi.fn(),
|
|
50
|
-
isActive: () => false,
|
|
69
|
+
isActive: vi.fn(() => false),
|
|
70
|
+
getAttributes: vi.fn(() => ({})),
|
|
71
|
+
state: { selection: { empty: false } },
|
|
51
72
|
chain: () => chain,
|
|
52
|
-
can: () =>
|
|
73
|
+
can: () => dryRun,
|
|
53
74
|
destroy: vi.fn(),
|
|
75
|
+
calls,
|
|
54
76
|
};
|
|
55
77
|
}
|
|
56
78
|
|
|
57
79
|
vi.mock("@tiptap/vue-3", async () => {
|
|
58
80
|
const { h, shallowRef } = await import("vue");
|
|
59
81
|
return {
|
|
60
|
-
useEditor: (options: {
|
|
82
|
+
useEditor: (options: {
|
|
83
|
+
content: string;
|
|
84
|
+
extensions: { name: string }[];
|
|
85
|
+
}) => {
|
|
86
|
+
tiptap.options = options;
|
|
61
87
|
tiptap.editor = createFakeEditor(options.content);
|
|
62
88
|
return shallowRef(tiptap.editor);
|
|
63
89
|
},
|
|
@@ -83,6 +109,16 @@ vi.mock("../runtime/utils/sanitizeHtml.js", async (importOriginal) => ({
|
|
|
83
109
|
|
|
84
110
|
vi.mock("vue-i18n", () => ({ useI18n: () => ({ t: (key: string) => key }) }));
|
|
85
111
|
|
|
112
|
+
// The module's runtime config; `beforeEach` restores what the module writes by default.
|
|
113
|
+
const runtimeConfig = vi.hoisted(() => ({
|
|
114
|
+
public: {
|
|
115
|
+
mapoForm: undefined as
|
|
116
|
+
| { editor: { source: boolean; textAlign?: boolean } }
|
|
117
|
+
| undefined,
|
|
118
|
+
},
|
|
119
|
+
}));
|
|
120
|
+
vi.mock("#app", () => ({ useRuntimeConfig: () => runtimeConfig }));
|
|
121
|
+
|
|
86
122
|
// Nodes are plain objects: each keeps its tag, its props (attributes and
|
|
87
123
|
// listeners included) and its text; `style` takes what `v-show` writes.
|
|
88
124
|
interface HeadlessNode {
|
|
@@ -148,6 +184,17 @@ const stubs: Record<string, Component> = {
|
|
|
148
184
|
h("tooltip", { text: props.text }, slots.default?.()),
|
|
149
185
|
},
|
|
150
186
|
USeparator: { render: () => h("separator") },
|
|
187
|
+
UInput: {
|
|
188
|
+
props: { modelValue: { type: String, default: "" } },
|
|
189
|
+
emits: ["update:modelValue"],
|
|
190
|
+
setup:
|
|
191
|
+
(props, { emit }) =>
|
|
192
|
+
() =>
|
|
193
|
+
h("input", {
|
|
194
|
+
value: props.modelValue,
|
|
195
|
+
onInput: (value: string) => emit("update:modelValue", value),
|
|
196
|
+
}),
|
|
197
|
+
},
|
|
151
198
|
UTextarea: {
|
|
152
199
|
props: { modelValue: { type: String, default: "" } },
|
|
153
200
|
emits: ["update:modelValue"],
|
|
@@ -215,6 +262,7 @@ beforeEach(() => {
|
|
|
215
262
|
vi.useFakeTimers();
|
|
216
263
|
modelValue = ref("<p>Hello <strong>world</strong></p><h2>Title</h2>");
|
|
217
264
|
emitted = [];
|
|
265
|
+
runtimeConfig.public.mapoForm = { editor: { source: true } };
|
|
218
266
|
});
|
|
219
267
|
|
|
220
268
|
afterEach(() => {
|
|
@@ -223,21 +271,57 @@ afterEach(() => {
|
|
|
223
271
|
vi.useRealTimers();
|
|
224
272
|
});
|
|
225
273
|
|
|
226
|
-
describe("MapoWygEditor without
|
|
227
|
-
it("renders the built-in toolbar
|
|
274
|
+
describe("MapoWygEditor without attrs", () => {
|
|
275
|
+
it("renders the built-in toolbar and the source toggle, enabled, and the editor", () => {
|
|
228
276
|
mount();
|
|
229
277
|
|
|
230
|
-
expect(buttons()).toHaveLength(
|
|
278
|
+
expect(buttons()).toHaveLength(12);
|
|
231
279
|
expect(buttons().every((node) => !node.props.disabled)).toBe(true);
|
|
232
|
-
expect(sourceToggle()).
|
|
280
|
+
expect(sourceToggle()).toBeDefined();
|
|
233
281
|
expect(textarea()).toBeUndefined();
|
|
234
282
|
expect(isHidden(editorContent())).toBe(false);
|
|
235
283
|
});
|
|
236
284
|
|
|
237
|
-
it("
|
|
285
|
+
it("renders the built-in toolbar only when the field turns the source view off", () => {
|
|
286
|
+
mount({ source: false });
|
|
287
|
+
|
|
288
|
+
expect(buttons()).toHaveLength(11);
|
|
289
|
+
expect(sourceToggle()).toBeUndefined();
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it("disables the built-in toolbar when read-only, not the source toggle", () => {
|
|
238
293
|
mount({}, { readonly: true });
|
|
239
294
|
|
|
240
|
-
|
|
295
|
+
const builtIn = buttons().filter((node) => node !== sourceToggle());
|
|
296
|
+
expect(builtIn.every((node) => node.props.disabled)).toBe(true);
|
|
297
|
+
expect(sourceToggle()!.props.disabled).toBeFalsy();
|
|
298
|
+
});
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
describe("MapoWygEditor source toggle default", () => {
|
|
302
|
+
it("follows the module option when the field does not set attrs.source", () => {
|
|
303
|
+
runtimeConfig.public.mapoForm = { editor: { source: false } };
|
|
304
|
+
mount();
|
|
305
|
+
|
|
306
|
+
expect(sourceToggle()).toBeUndefined();
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
it("lets the field's attrs.source win over the module option, both ways", () => {
|
|
310
|
+
runtimeConfig.public.mapoForm = { editor: { source: false } };
|
|
311
|
+
mount({ source: true });
|
|
312
|
+
expect(sourceToggle()).toBeDefined();
|
|
313
|
+
app!.unmount();
|
|
314
|
+
|
|
315
|
+
runtimeConfig.public.mapoForm = { editor: { source: true } };
|
|
316
|
+
mount({ source: false });
|
|
317
|
+
expect(sourceToggle()).toBeUndefined();
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
it("shows the toggle when the app has no form runtime config", () => {
|
|
321
|
+
runtimeConfig.public.mapoForm = undefined;
|
|
322
|
+
mount();
|
|
323
|
+
|
|
324
|
+
expect(sourceToggle()).toBeDefined();
|
|
241
325
|
});
|
|
242
326
|
});
|
|
243
327
|
|
|
@@ -441,7 +525,8 @@ describe("MapoWygEditor custom toolbar buttons", () => {
|
|
|
441
525
|
expect(row[table - 1]!.type).toBe("separator");
|
|
442
526
|
expect(row[table - 2]).toBe(tooltipOf("mapo.wygEditor.insertImage"));
|
|
443
527
|
expect(row[table + 1]).toBe(tooltipOf("Quote"));
|
|
444
|
-
|
|
528
|
+
// 11 built-in, 2 custom, the source toggle.
|
|
529
|
+
expect(buttons()).toHaveLength(14);
|
|
445
530
|
|
|
446
531
|
expect(buttonOf("Insert table")!.props.icon).toBe("i-lucide-table");
|
|
447
532
|
// A button with no icon shows its label.
|
|
@@ -520,6 +605,282 @@ describe("MapoWygEditor custom toolbar buttons", () => {
|
|
|
520
605
|
|
|
521
606
|
expect(hasActiveClass(buttonOf("Add row"))).toBe(false);
|
|
522
607
|
expect(buttonOf("Add row")!.props.disabled).toBe(true);
|
|
523
|
-
|
|
608
|
+
// 11 built-in, 1 custom, the source toggle.
|
|
609
|
+
expect(buttons()).toHaveLength(13);
|
|
610
|
+
});
|
|
611
|
+
});
|
|
612
|
+
|
|
613
|
+
/** A button by its accessible name, or by its text when it has none. */
|
|
614
|
+
const byLabel = (label: string) =>
|
|
615
|
+
buttons().find(
|
|
616
|
+
(node) => node.props["aria-label"] === label || node.props.label === label,
|
|
617
|
+
);
|
|
618
|
+
|
|
619
|
+
describe("MapoWygEditor links", () => {
|
|
620
|
+
const linkButton = () => byLabel("mapo.wygEditor.link");
|
|
621
|
+
const linkInput = () => all(root).find((node) => node.type === "input");
|
|
622
|
+
const typeLink = (url: string) =>
|
|
623
|
+
(linkInput()!.props.onInput as (value: string) => void)(url);
|
|
624
|
+
const alert = () => all(root).find((node) => node.props.role === "alert");
|
|
625
|
+
/** Fires the input's keydown listeners, as `@keydown.<key>` binds them. */
|
|
626
|
+
function press(key: string) {
|
|
627
|
+
const event = { key, preventDefault: vi.fn() };
|
|
628
|
+
const handlers = [linkInput()!.props.onKeydown].flat() as Array<
|
|
629
|
+
(event: unknown) => void
|
|
630
|
+
>;
|
|
631
|
+
handlers.forEach((handler) => handler(event));
|
|
632
|
+
return event;
|
|
633
|
+
}
|
|
634
|
+
async function openLink(url?: string) {
|
|
635
|
+
click(linkButton());
|
|
636
|
+
await nextTick();
|
|
637
|
+
if (url !== undefined) {
|
|
638
|
+
typeLink(url);
|
|
639
|
+
await nextTick();
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
it("opens a bar with the address of the link at the selection, and closes it", async () => {
|
|
644
|
+
mount();
|
|
645
|
+
fakeEditor().getAttributes.mockReturnValue({ href: "/contatti/" });
|
|
646
|
+
|
|
647
|
+
await openLink();
|
|
648
|
+
|
|
649
|
+
expect(fakeEditor().getAttributes).toHaveBeenCalledWith("link");
|
|
650
|
+
expect(linkInput()!.props.value).toBe("/contatti/");
|
|
651
|
+
expect(linkButton()!.props["aria-expanded"]).toBe(true);
|
|
652
|
+
|
|
653
|
+
click(byLabel("mapo.wygEditor.linkCancel"));
|
|
654
|
+
await nextTick();
|
|
655
|
+
expect(linkInput()).toBeUndefined();
|
|
656
|
+
});
|
|
657
|
+
|
|
658
|
+
it("links the selection, extending the link already there", async () => {
|
|
659
|
+
mount();
|
|
660
|
+
await openLink("https://example.com");
|
|
661
|
+
|
|
662
|
+
click(byLabel("mapo.wygEditor.linkApply"));
|
|
663
|
+
await nextTick();
|
|
664
|
+
|
|
665
|
+
expect(fakeEditor().calls).toEqual([
|
|
666
|
+
["focus"],
|
|
667
|
+
["extendMarkRange", "link"],
|
|
668
|
+
["setLink", { href: "https://example.com" }],
|
|
669
|
+
["run"],
|
|
670
|
+
]);
|
|
671
|
+
expect(linkInput()).toBeUndefined();
|
|
672
|
+
});
|
|
673
|
+
|
|
674
|
+
it("inserts the address as linked text when nothing is selected", async () => {
|
|
675
|
+
mount();
|
|
676
|
+
fakeEditor().state.selection.empty = true;
|
|
677
|
+
await openLink("/contatti/");
|
|
678
|
+
|
|
679
|
+
click(byLabel("mapo.wygEditor.linkApply"));
|
|
680
|
+
|
|
681
|
+
expect(fakeEditor().calls).toEqual([
|
|
682
|
+
["focus"],
|
|
683
|
+
[
|
|
684
|
+
"insertContent",
|
|
685
|
+
{
|
|
686
|
+
type: "text",
|
|
687
|
+
text: "/contatti/",
|
|
688
|
+
marks: [{ type: "link", attrs: { href: "/contatti/" } }],
|
|
689
|
+
},
|
|
690
|
+
],
|
|
691
|
+
["run"],
|
|
692
|
+
]);
|
|
693
|
+
});
|
|
694
|
+
|
|
695
|
+
it("adds https:// to an address that starts with www.", async () => {
|
|
696
|
+
mount();
|
|
697
|
+
await openLink("www.example.com");
|
|
698
|
+
|
|
699
|
+
click(byLabel("mapo.wygEditor.linkApply"));
|
|
700
|
+
|
|
701
|
+
expect(fakeEditor().calls).toContainEqual([
|
|
702
|
+
"setLink",
|
|
703
|
+
{ href: "https://www.example.com" },
|
|
704
|
+
]);
|
|
705
|
+
});
|
|
706
|
+
|
|
707
|
+
it("refuses an address outside the allowlist and keeps the bar open with an error", async () => {
|
|
708
|
+
mount();
|
|
709
|
+
await openLink("javascript:alert(1)");
|
|
710
|
+
|
|
711
|
+
click(byLabel("mapo.wygEditor.linkApply"));
|
|
712
|
+
await nextTick();
|
|
713
|
+
|
|
714
|
+
expect(fakeEditor().calls).toEqual([]);
|
|
715
|
+
expect(alert()!.text).toContain("mapo.wygEditor.linkInvalid");
|
|
716
|
+
expect(linkInput()!.props["aria-invalid"]).toBe(true);
|
|
717
|
+
});
|
|
718
|
+
|
|
719
|
+
it("removes the link with its button, or when the address is emptied", async () => {
|
|
720
|
+
mount();
|
|
721
|
+
fakeEditor().isActive.mockImplementation((name) => name === "link");
|
|
722
|
+
await openLink();
|
|
723
|
+
|
|
724
|
+
click(byLabel("mapo.wygEditor.linkRemove"));
|
|
725
|
+
await nextTick();
|
|
726
|
+
expect(fakeEditor().calls).toEqual([
|
|
727
|
+
["focus"],
|
|
728
|
+
["extendMarkRange", "link"],
|
|
729
|
+
["unsetLink"],
|
|
730
|
+
["run"],
|
|
731
|
+
]);
|
|
732
|
+
expect(linkInput()).toBeUndefined();
|
|
733
|
+
|
|
734
|
+
fakeEditor().calls.length = 0;
|
|
735
|
+
await openLink(" ");
|
|
736
|
+
click(byLabel("mapo.wygEditor.linkApply"));
|
|
737
|
+
expect(fakeEditor().calls).toEqual([
|
|
738
|
+
["focus"],
|
|
739
|
+
["extendMarkRange", "link"],
|
|
740
|
+
["unsetLink"],
|
|
741
|
+
["run"],
|
|
742
|
+
]);
|
|
743
|
+
});
|
|
744
|
+
|
|
745
|
+
it("applies with Enter and closes with Escape, keeping the keys from the page's form", async () => {
|
|
746
|
+
mount();
|
|
747
|
+
await openLink("/pagina/");
|
|
748
|
+
|
|
749
|
+
const enter = press("Enter");
|
|
750
|
+
expect(enter.preventDefault).toHaveBeenCalled();
|
|
751
|
+
expect(fakeEditor().calls).toContainEqual([
|
|
752
|
+
"setLink",
|
|
753
|
+
{ href: "/pagina/" },
|
|
754
|
+
]);
|
|
755
|
+
await nextTick();
|
|
756
|
+
expect(linkInput()).toBeUndefined();
|
|
757
|
+
|
|
758
|
+
await openLink();
|
|
759
|
+
const escape = press("Escape");
|
|
760
|
+
expect(escape.preventDefault).toHaveBeenCalled();
|
|
761
|
+
await nextTick();
|
|
762
|
+
expect(linkInput()).toBeUndefined();
|
|
763
|
+
});
|
|
764
|
+
|
|
765
|
+
it("closes the bar for the source view and disables the button on a read-only field", async () => {
|
|
766
|
+
mount();
|
|
767
|
+
await openLink();
|
|
768
|
+
|
|
769
|
+
click(sourceToggle());
|
|
770
|
+
await nextTick();
|
|
771
|
+
expect(linkInput()).toBeUndefined();
|
|
772
|
+
expect(linkButton()!.props.disabled).toBe(true);
|
|
773
|
+
app!.unmount();
|
|
774
|
+
|
|
775
|
+
mount({}, { readonly: true });
|
|
776
|
+
expect(linkButton()!.props.disabled).toBe(true);
|
|
777
|
+
});
|
|
778
|
+
});
|
|
779
|
+
|
|
780
|
+
describe("MapoWygEditor alignment", () => {
|
|
781
|
+
const alignButtons = () =>
|
|
782
|
+
["alignLeft", "alignCenter", "alignRight"].map((key) =>
|
|
783
|
+
byLabel(`mapo.wygEditor.${key}`),
|
|
784
|
+
);
|
|
785
|
+
const loadedTextAlign = () =>
|
|
786
|
+
tiptap.options!.extensions.filter(
|
|
787
|
+
(extension) => extension.name === "textAlign",
|
|
788
|
+
) as unknown as { options: { types: string[]; alignments: string[] } }[];
|
|
789
|
+
|
|
790
|
+
it("has no buttons and loads no TextAlign by default", () => {
|
|
791
|
+
mount();
|
|
792
|
+
|
|
793
|
+
expect(alignButtons().every((node) => node === undefined)).toBe(true);
|
|
794
|
+
expect(loadedTextAlign()).toHaveLength(0);
|
|
795
|
+
});
|
|
796
|
+
|
|
797
|
+
it("adds left, centre and right with attrs.textAlign, for paragraphs and headings", () => {
|
|
798
|
+
mount({ textAlign: true });
|
|
799
|
+
|
|
800
|
+
expect(alignButtons().map((node) => node?.props.icon)).toEqual([
|
|
801
|
+
"i-lucide-align-left",
|
|
802
|
+
"i-lucide-align-center",
|
|
803
|
+
"i-lucide-align-right",
|
|
804
|
+
]);
|
|
805
|
+
expect(loadedTextAlign()).toHaveLength(1);
|
|
806
|
+
expect(loadedTextAlign()[0]!.options.types).toEqual([
|
|
807
|
+
"heading",
|
|
808
|
+
"paragraph",
|
|
809
|
+
]);
|
|
810
|
+
expect(loadedTextAlign()[0]!.options.alignments).toEqual([
|
|
811
|
+
"left",
|
|
812
|
+
"center",
|
|
813
|
+
"right",
|
|
814
|
+
]);
|
|
815
|
+
});
|
|
816
|
+
|
|
817
|
+
it("follows the module option, and the field's attrs.textAlign wins", () => {
|
|
818
|
+
runtimeConfig.public.mapoForm = {
|
|
819
|
+
editor: { source: true, textAlign: true },
|
|
820
|
+
};
|
|
821
|
+
mount();
|
|
822
|
+
expect(alignButtons().every((node) => node !== undefined)).toBe(true);
|
|
823
|
+
app!.unmount();
|
|
824
|
+
|
|
825
|
+
mount({ textAlign: false });
|
|
826
|
+
expect(alignButtons().every((node) => node === undefined)).toBe(true);
|
|
827
|
+
});
|
|
828
|
+
|
|
829
|
+
it("centres and right-aligns with setTextAlign; left removes the alignment", () => {
|
|
830
|
+
mount({ textAlign: true });
|
|
831
|
+
const [left, center, right] = alignButtons();
|
|
832
|
+
|
|
833
|
+
click(center);
|
|
834
|
+
click(right);
|
|
835
|
+
click(left);
|
|
836
|
+
|
|
837
|
+
expect(fakeEditor().calls).toEqual([
|
|
838
|
+
["focus"],
|
|
839
|
+
["setTextAlign", "center"],
|
|
840
|
+
["run"],
|
|
841
|
+
["focus"],
|
|
842
|
+
["setTextAlign", "right"],
|
|
843
|
+
["run"],
|
|
844
|
+
["focus"],
|
|
845
|
+
["unsetTextAlign"],
|
|
846
|
+
["run"],
|
|
847
|
+
]);
|
|
848
|
+
});
|
|
849
|
+
|
|
850
|
+
it("marks the selection's alignment as pressed, left when none is set", async () => {
|
|
851
|
+
mount({ textAlign: true });
|
|
852
|
+
expect(alignButtons().map((node) => node!.props["aria-pressed"])).toEqual([
|
|
853
|
+
true,
|
|
854
|
+
false,
|
|
855
|
+
false,
|
|
856
|
+
]);
|
|
857
|
+
|
|
858
|
+
fakeEditor().isActive.mockImplementation(
|
|
859
|
+
(attributes) =>
|
|
860
|
+
typeof attributes === "object" &&
|
|
861
|
+
(attributes as { textAlign?: string }).textAlign === "center",
|
|
862
|
+
);
|
|
863
|
+
// Opening the link bar re-renders the toolbar with the new selection.
|
|
864
|
+
click(byLabel("mapo.wygEditor.link"));
|
|
865
|
+
await nextTick();
|
|
866
|
+
|
|
867
|
+
expect(alignButtons().map((node) => node!.props["aria-pressed"])).toEqual([
|
|
868
|
+
false,
|
|
869
|
+
true,
|
|
870
|
+
false,
|
|
871
|
+
]);
|
|
872
|
+
});
|
|
873
|
+
|
|
874
|
+
it("keeps a TextAlign the field loads itself, instead of adding its own", () => {
|
|
875
|
+
const own = TextAlign.configure({ types: ["paragraph"] });
|
|
876
|
+
mount({ textAlign: true, extensions: [own] });
|
|
877
|
+
|
|
878
|
+
expect(loadedTextAlign()).toEqual([own]);
|
|
879
|
+
});
|
|
880
|
+
|
|
881
|
+
it("disables the buttons on a read-only field", () => {
|
|
882
|
+
mount({ textAlign: true }, { readonly: true });
|
|
883
|
+
|
|
884
|
+
expect(alignButtons().every((node) => node!.props.disabled)).toBe(true);
|
|
524
885
|
});
|
|
525
886
|
});
|
package/src/module.ts
CHANGED
|
@@ -16,6 +16,19 @@ export interface MapoFormOptions {
|
|
|
16
16
|
fields?: PartialFieldRegistry;
|
|
17
17
|
groups?: { expanded?: boolean };
|
|
18
18
|
debounce?: number;
|
|
19
|
+
/** Defaults for every `editor` field; a field's own `attrs` win. */
|
|
20
|
+
editor?: {
|
|
21
|
+
/**
|
|
22
|
+
* Shows the HTML source toggle in the toolbar of every editor field. A
|
|
23
|
+
* field sets `attrs.source` to differ from it. Default: `true`.
|
|
24
|
+
*/
|
|
25
|
+
source?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Shows the alignment buttons (left, centre, right) in every editor
|
|
28
|
+
* field. A field sets `attrs.textAlign` to differ from it. Default: `false`.
|
|
29
|
+
*/
|
|
30
|
+
textAlign?: boolean;
|
|
31
|
+
};
|
|
19
32
|
}
|
|
20
33
|
|
|
21
34
|
export default defineNuxtModule<MapoFormOptions>({
|
|
@@ -27,6 +40,7 @@ export default defineNuxtModule<MapoFormOptions>({
|
|
|
27
40
|
defaults: {
|
|
28
41
|
debounce: 300,
|
|
29
42
|
groups: { expanded: true },
|
|
43
|
+
editor: { source: true, textAlign: false },
|
|
30
44
|
},
|
|
31
45
|
|
|
32
46
|
async setup(options, nuxt) {
|
|
@@ -55,11 +69,15 @@ export default defineNuxtModule<MapoFormOptions>({
|
|
|
55
69
|
// export default defineNuxtPlugin(({ $mapoFormRegistry }) => {
|
|
56
70
|
// $mapoFormRegistry.mapping['my-type'] = () => import('~/components/MyField.vue')
|
|
57
71
|
// })
|
|
58
|
-
// `defaults` guarantees groups/debounce at runtime; fall back
|
|
59
|
-
// so the assignment type is fully defined.
|
|
72
|
+
// `defaults` guarantees groups/debounce/editor at runtime; fall back
|
|
73
|
+
// explicitly so the assignment type is fully defined.
|
|
60
74
|
nuxt.options.runtimeConfig.public.mapoForm = {
|
|
61
75
|
groups: { expanded: options.groups?.expanded ?? true },
|
|
62
76
|
debounce: options.debounce ?? 300,
|
|
77
|
+
editor: {
|
|
78
|
+
source: options.editor?.source ?? true,
|
|
79
|
+
textAlign: options.editor?.textAlign ?? false,
|
|
80
|
+
},
|
|
63
81
|
// attrs is JSON-serialisable (plain objects) — merged in the plugin
|
|
64
82
|
fields: { attrs: options.fields?.attrs ?? {} },
|
|
65
83
|
};
|