@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.
@@ -8,17 +8,20 @@ import {
8
8
  useId,
9
9
  } from "vue";
10
10
  import { useI18n } from "vue-i18n";
11
+ import { useRuntimeConfig } from "#app";
11
12
  import { useEditor, EditorContent } from "@tiptap/vue-3";
12
13
  import type { AnyExtension } from "@tiptap/vue-3";
13
14
  import StarterKit from "@tiptap/starter-kit";
14
15
  import Link from "@tiptap/extension-link";
15
16
  import Underline from "@tiptap/extension-underline";
16
17
  import Image from "@tiptap/extension-image";
18
+ import TextAlign from "@tiptap/extension-text-align";
17
19
  import { debounce } from "@mapomodule/utils";
18
20
  import type {
19
21
  EditorDescriptor,
20
22
  EditorToolbarEditor,
21
23
  EditorToolbarItem,
24
+ MapoFormRuntimeConfig,
22
25
  } from "../../types/index.js";
23
26
  import { formatHtml } from "../../utils/formatHtml.js";
24
27
  import {
@@ -39,9 +42,34 @@ const emit = defineEmits<{ "update:modelValue": [value: string] }>();
39
42
 
40
43
  const { t } = useI18n();
41
44
 
45
+ // The module options under `editor`: a field's own `attrs` win over them.
46
+ const appEditorDefaults = (
47
+ useRuntimeConfig().public.mapoForm as MapoFormRuntimeConfig | undefined
48
+ )?.editor;
49
+
42
50
  const extraExtensions =
43
51
  (props.descriptor.attrs?.extensions as AnyExtension[] | undefined) ?? [];
44
52
 
53
+ // ─── Text alignment (`attrs.textAlign`) ───────────────────────────────────
54
+ // Loaded only where the field offers it: elsewhere the schema drops the
55
+ // alignment of pasted text, as it drops any markup no extension supports.
56
+ // Left is the default and writes no style; a field that loads its own
57
+ // TextAlign through `attrs.extensions` keeps it.
58
+ const hasTextAlign =
59
+ props.descriptor.attrs?.textAlign ?? appEditorDefaults?.textAlign ?? false;
60
+ const loadsOwnTextAlign = extraExtensions.some(
61
+ (extension) => extension.name === "textAlign",
62
+ );
63
+ const alignmentExtensions: AnyExtension[] =
64
+ hasTextAlign && !loadsOwnTextAlign
65
+ ? [
66
+ TextAlign.configure({
67
+ types: ["heading", "paragraph"],
68
+ alignments: ["left", "center", "right"],
69
+ }),
70
+ ]
71
+ : [];
72
+
45
73
  const editor = useEditor({
46
74
  content: sanitizeHtml((props.modelValue as string) ?? ""),
47
75
  editable: !props.readonly && !props.disabled,
@@ -54,6 +82,7 @@ const editor = useEditor({
54
82
  }),
55
83
  Underline,
56
84
  Image.configure({ inline: false, allowBase64: false }),
85
+ ...alignmentExtensions,
57
86
  ...extraExtensions,
58
87
  ] as AnyExtension[],
59
88
  onUpdate: ({ editor: e }) => {
@@ -75,7 +104,10 @@ watch(
75
104
  // the textarea (`v-show`), so its history survives the switch.
76
105
  const SOURCE_SYNC_DELAY_MS = 250;
77
106
 
78
- const hasSourceView = props.descriptor.attrs?.source === true;
107
+ // The field's own `attrs.source` wins; otherwise the module option
108
+ // `editor.source`, which the module defaults to `true`.
109
+ const hasSourceView =
110
+ props.descriptor.attrs?.source ?? appEditorDefaults?.source ?? true;
79
111
  const sourceMode = ref(false);
80
112
  const sourceHtml = ref("");
81
113
  const sourceHintId = useId();
@@ -185,6 +217,120 @@ function runItem(item: EditorToolbarItem) {
185
217
  item.action(current);
186
218
  }
187
219
 
220
+ // ─── Links ────────────────────────────────────────────────────────────────
221
+ // A bar under the toolbar edits the link at the selection. Not a <form>: the
222
+ // field sits inside the page's form, which Enter would submit. The address is
223
+ // checked against the same allowlist as the Link extension (relative paths,
224
+ // http, https, mailto, tel); one starting with `www.` gets `https://`.
225
+ const linkOpen = ref(false);
226
+ const linkUrl = ref("");
227
+ const linkInvalid = ref(false);
228
+ const linkErrorId = useId();
229
+
230
+ function closeLink() {
231
+ linkOpen.value = false;
232
+ linkInvalid.value = false;
233
+ }
234
+
235
+ function toggleLink() {
236
+ const current = editor.value;
237
+ if (!current) return;
238
+ if (linkOpen.value) {
239
+ closeLink();
240
+ return;
241
+ }
242
+ linkUrl.value = (current.getAttributes("link").href as string) ?? "";
243
+ linkInvalid.value = false;
244
+ linkOpen.value = true;
245
+ }
246
+
247
+ function removeLink() {
248
+ editor.value?.chain().focus().extendMarkRange("link").unsetLink().run();
249
+ closeLink();
250
+ }
251
+
252
+ function applyLink() {
253
+ const current = editor.value;
254
+ if (!current) return;
255
+ const typed = linkUrl.value.trim();
256
+ if (!typed) {
257
+ removeLink();
258
+ return;
259
+ }
260
+ const href = /^www\./i.test(typed) ? `https://${typed}` : typed;
261
+ if (!isSafeUrl(href)) {
262
+ linkInvalid.value = true;
263
+ return;
264
+ }
265
+ const chain = current.chain().focus();
266
+ if (current.state.selection.empty && !current.isActive("link")) {
267
+ // Nothing selected: the address becomes the link's text.
268
+ chain
269
+ .insertContent({
270
+ type: "text",
271
+ text: href,
272
+ marks: [{ type: "link", attrs: { href } }],
273
+ })
274
+ .run();
275
+ } else {
276
+ chain.extendMarkRange("link").setLink({ href }).run();
277
+ }
278
+ closeLink();
279
+ }
280
+
281
+ /** Enter applies, Escape closes; neither reaches the page's form. */
282
+ function onLinkKeydown(event: KeyboardEvent) {
283
+ if (event.key === "Enter") {
284
+ event.preventDefault();
285
+ applyLink();
286
+ } else if (event.key === "Escape") {
287
+ event.preventDefault();
288
+ closeLink();
289
+ }
290
+ }
291
+
292
+ // The bar goes with the toolbar: in the source view and on a locked field.
293
+ watch(isToolbarDisabled, (disabled) => {
294
+ if (disabled) closeLink();
295
+ });
296
+
297
+ // ─── Alignment buttons ────────────────────────────────────────────────────
298
+ type Alignment = "left" | "center" | "right";
299
+ const alignments: { value: Alignment; icon: string; label: string }[] = [
300
+ {
301
+ value: "left",
302
+ icon: "i-lucide-align-left",
303
+ label: "mapo.wygEditor.alignLeft",
304
+ },
305
+ {
306
+ value: "center",
307
+ icon: "i-lucide-align-center",
308
+ label: "mapo.wygEditor.alignCenter",
309
+ },
310
+ {
311
+ value: "right",
312
+ icon: "i-lucide-align-right",
313
+ label: "mapo.wygEditor.alignRight",
314
+ },
315
+ ];
316
+
317
+ function isAligned(value: Alignment): boolean {
318
+ const current = editor.value;
319
+ if (!current) return false;
320
+ if (value !== "left") return current.isActive({ textAlign: value });
321
+ return (
322
+ !current.isActive({ textAlign: "center" }) &&
323
+ !current.isActive({ textAlign: "right" })
324
+ );
325
+ }
326
+
327
+ function align(value: Alignment) {
328
+ const chain = editor.value?.chain().focus();
329
+ if (!chain) return;
330
+ // Left is the default: removing the alignment writes no style.
331
+ (value === "left" ? chain.unsetTextAlign() : chain.setTextAlign(value)).run();
332
+ }
333
+
188
334
  // ─── Image insertion ──────────────────────────────────────────────────────
189
335
  // MapoMediaManagerDialog is globally registered by @mapomodule/uikit.
190
336
  // resolveComponent() works at runtime because it is registered before this
@@ -299,6 +445,29 @@ function onImageConfirm(
299
445
  @click="editor.chain().focus().toggleOrderedList().run()"
300
446
  />
301
447
 
448
+ <!-- Alignment (`attrs.textAlign`) -->
449
+ <template v-if="hasTextAlign">
450
+ <USeparator orientation="vertical" class="mx-1 h-5" />
451
+ <UTooltip
452
+ v-for="alignment in alignments"
453
+ :key="alignment.value"
454
+ :text="t(alignment.label)"
455
+ :delay-duration="300"
456
+ >
457
+ <UButton
458
+ size="xs"
459
+ variant="ghost"
460
+ color="neutral"
461
+ :icon="alignment.icon"
462
+ :aria-label="t(alignment.label)"
463
+ :aria-pressed="isAligned(alignment.value)"
464
+ :disabled="isToolbarDisabled"
465
+ :class="{ 'bg-gray-200': isAligned(alignment.value) }"
466
+ @click="align(alignment.value)"
467
+ />
468
+ </UTooltip>
469
+ </template>
470
+
302
471
  <USeparator orientation="vertical" class="mx-1 h-5" />
303
472
 
304
473
  <UButton
@@ -320,6 +489,21 @@ function onImageConfirm(
320
489
 
321
490
  <USeparator orientation="vertical" class="mx-1 h-5" />
322
491
 
492
+ <!-- Link: opens the link bar under the toolbar -->
493
+ <UTooltip :text="t('mapo.wygEditor.link')" :delay-duration="300">
494
+ <UButton
495
+ size="xs"
496
+ variant="ghost"
497
+ color="neutral"
498
+ icon="i-lucide-link"
499
+ :aria-label="t('mapo.wygEditor.link')"
500
+ :aria-expanded="linkOpen"
501
+ :disabled="isToolbarDisabled"
502
+ :class="{ 'bg-gray-200': linkOpen || editor.isActive('link') }"
503
+ @click="toggleLink"
504
+ />
505
+ </UTooltip>
506
+
323
507
  <!-- Insert Image via Media Manager (available when @mapomodule/uikit is installed) -->
324
508
  <UTooltip
325
509
  :text="
@@ -381,11 +565,60 @@ function onImageConfirm(
381
565
  </template>
382
566
  </div>
383
567
 
384
- <!-- Editor content: hidden, not unmounted, in the source view -->
568
+ <!-- Link bar: the address of the link at the selection -->
569
+ <div
570
+ v-if="linkOpen && editor"
571
+ class="flex flex-wrap items-center gap-2 border-b border-gray-200 px-2 py-1.5"
572
+ >
573
+ <UInput
574
+ v-model="linkUrl"
575
+ size="xs"
576
+ autofocus
577
+ class="min-w-56 flex-1"
578
+ :placeholder="t('mapo.wygEditor.linkPlaceholder')"
579
+ :aria-label="t('mapo.wygEditor.linkUrl')"
580
+ :aria-invalid="linkInvalid"
581
+ :aria-describedby="linkInvalid ? linkErrorId : undefined"
582
+ @keydown="onLinkKeydown"
583
+ />
584
+ <UButton
585
+ size="xs"
586
+ :label="t('mapo.wygEditor.linkApply')"
587
+ @click="applyLink"
588
+ />
589
+ <UButton
590
+ v-if="editor.isActive('link')"
591
+ size="xs"
592
+ variant="ghost"
593
+ color="neutral"
594
+ :label="t('mapo.wygEditor.linkRemove')"
595
+ @click="removeLink"
596
+ />
597
+ <UButton
598
+ size="xs"
599
+ variant="ghost"
600
+ color="neutral"
601
+ icon="i-lucide-x"
602
+ :aria-label="t('mapo.wygEditor.linkCancel')"
603
+ @click="closeLink"
604
+ />
605
+ <p
606
+ v-if="linkInvalid"
607
+ :id="linkErrorId"
608
+ class="w-full text-xs text-red-500"
609
+ role="alert"
610
+ >
611
+ {{ t("mapo.wygEditor.linkInvalid") }}
612
+ </p>
613
+ </div>
614
+
615
+ <!-- Editor content: hidden, not unmounted, in the source view. Its
616
+ elements are styled below: Tailwind's preflight leaves headings,
617
+ lists and links looking like plain text. -->
385
618
  <EditorContent
386
619
  v-show="!sourceMode"
387
620
  :editor="editor"
388
- class="prose prose-sm max-w-none px-3 py-2 focus-within:outline-none"
621
+ class="mapo-wyg-content px-3 py-2 focus-within:outline-none"
389
622
  />
390
623
 
391
624
  <div v-if="sourceMode" class="px-3 py-2">
@@ -432,3 +665,103 @@ function onImageConfirm(
432
665
  />
433
666
  </div>
434
667
  </template>
668
+
669
+ <style scoped>
670
+ /* The editor's content. Tailwind's preflight resets headings, lists and links
671
+ to plain text, and the app may not load a typography plugin: these rules
672
+ give each element its look, with Nuxt UI's tokens, so they follow the theme
673
+ and the colour mode. */
674
+ .mapo-wyg-content {
675
+ font-size: 0.875rem;
676
+ line-height: 1.6;
677
+ color: var(--ui-text);
678
+ }
679
+ .mapo-wyg-content :deep(.ProseMirror > * + *) {
680
+ margin-top: 0.75em;
681
+ }
682
+ .mapo-wyg-content :deep(h2),
683
+ .mapo-wyg-content :deep(h3) {
684
+ font-weight: 600;
685
+ color: var(--ui-text-highlighted);
686
+ }
687
+ .mapo-wyg-content :deep(h2) {
688
+ font-size: 1.375em;
689
+ line-height: 1.3;
690
+ }
691
+ .mapo-wyg-content :deep(h3) {
692
+ font-size: 1.125em;
693
+ line-height: 1.4;
694
+ }
695
+ .mapo-wyg-content :deep(.ProseMirror > :is(h2, h3):not(:first-child)) {
696
+ margin-top: 1.25em;
697
+ }
698
+ .mapo-wyg-content :deep(ul) {
699
+ list-style: disc;
700
+ padding-left: 1.5em;
701
+ }
702
+ .mapo-wyg-content :deep(ol) {
703
+ list-style: decimal;
704
+ padding-left: 1.5em;
705
+ }
706
+ .mapo-wyg-content :deep(li + li),
707
+ .mapo-wyg-content :deep(li > :is(ul, ol)) {
708
+ margin-top: 0.25em;
709
+ }
710
+ .mapo-wyg-content :deep(a) {
711
+ color: var(--ui-primary);
712
+ text-decoration: underline;
713
+ text-underline-offset: 2px;
714
+ }
715
+ .mapo-wyg-content :deep(blockquote) {
716
+ border-left: 3px solid var(--ui-border-accented);
717
+ padding-left: 1em;
718
+ color: var(--ui-text-muted);
719
+ }
720
+ .mapo-wyg-content :deep(code) {
721
+ font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
722
+ font-size: 0.875em;
723
+ padding: 0.1em 0.3em;
724
+ border-radius: 0.25rem;
725
+ background: var(--ui-bg-elevated);
726
+ }
727
+ .mapo-wyg-content :deep(pre) {
728
+ padding: 0.75em 1em;
729
+ border-radius: 0.375rem;
730
+ background: var(--ui-bg-elevated);
731
+ overflow-x: auto;
732
+ }
733
+ .mapo-wyg-content :deep(pre code) {
734
+ padding: 0;
735
+ background: none;
736
+ }
737
+ .mapo-wyg-content :deep(hr) {
738
+ border: 0;
739
+ border-top: 1px solid var(--ui-border);
740
+ }
741
+ .mapo-wyg-content :deep(img) {
742
+ max-width: 100%;
743
+ height: auto;
744
+ }
745
+ /* Tables, for the fields that load the table extensions. */
746
+ .mapo-wyg-content :deep(table) {
747
+ width: 100%;
748
+ border-collapse: collapse;
749
+ table-layout: fixed;
750
+ }
751
+ .mapo-wyg-content :deep(:is(th, td)) {
752
+ border: 1px solid var(--ui-border-accented);
753
+ padding: 0.375em 0.5em;
754
+ vertical-align: top;
755
+ text-align: left;
756
+ }
757
+ .mapo-wyg-content :deep(th) {
758
+ font-weight: 600;
759
+ background: var(--ui-bg-elevated);
760
+ }
761
+ .mapo-wyg-content :deep(:is(th, td) > p) {
762
+ margin: 0;
763
+ }
764
+ .mapo-wyg-content :deep(.selectedCell) {
765
+ background: color-mix(in oklab, var(--ui-primary) 12%, transparent);
766
+ }
767
+ </style>
@@ -12,6 +12,7 @@ const { $mapoFormRegistry } = useNuxtApp() as unknown as {
12
12
  const config = useRuntimeConfig().public.mapoForm as {
13
13
  groups?: { expanded?: boolean };
14
14
  debounce?: number;
15
+ editor?: { source?: boolean; textAlign?: boolean };
15
16
  };
16
17
 
17
18
  /** Sorted list of all registered field types currently available in the runtime registry. */
@@ -48,6 +49,10 @@ const userMappings = computed(() =>
48
49
 
49
50
  const globalDebounce = computed(() => config?.debounce || 300);
50
51
  const globalExpanded = computed(() => config?.groups?.expanded !== false);
52
+ const globalEditorSource = computed(() => config?.editor?.source !== false);
53
+ const globalEditorTextAlign = computed(
54
+ () => config?.editor?.textAlign === true,
55
+ );
51
56
 
52
57
  function componentLabel(type: string): string {
53
58
  const v = $mapoFormRegistry?.mapping?.[type];
@@ -111,6 +116,24 @@ function isUserType(type: string): boolean {
111
116
  {{ globalExpanded }}
112
117
  </span>
113
118
  </div>
119
+ <div>
120
+ <span class="text-gray-500">editor.source</span>
121
+ <span
122
+ class="ml-2"
123
+ :class="globalEditorSource ? 'text-green-400' : 'text-red-400'"
124
+ >
125
+ {{ globalEditorSource }}
126
+ </span>
127
+ </div>
128
+ <div>
129
+ <span class="text-gray-500">editor.textAlign</span>
130
+ <span
131
+ class="ml-2"
132
+ :class="globalEditorTextAlign ? 'text-green-400' : 'text-red-400'"
133
+ >
134
+ {{ globalEditorTextAlign }}
135
+ </span>
136
+ </div>
114
137
  <div>
115
138
  <span class="text-gray-500">custom types</span>
116
139
  <span class="ml-2 text-blue-400">{{ userMappings.length }}</span>
@@ -341,9 +341,18 @@ export interface EditorDescriptor<
341
341
  * edits the HTML in a textarea; what it saves goes through the editor, so it
342
342
  * is sanitized, and the tags and attributes no extension supports are
343
343
  * dropped. A read-only field shows the source without letting it change.
344
- * Default: `false`.
344
+ * Default: the module option `editor.source` (`mapoForm.editor.source` in
345
+ * `nuxt.config`), which is `true`; `false` hides the toggle on this field.
345
346
  */
346
347
  source?: boolean;
348
+ /**
349
+ * Adds left, centre and right alignment buttons for paragraphs and
350
+ * headings, and loads Tiptap's TextAlign extension, which writes
351
+ * `style="text-align: center"` (left writes no style). A field without it
352
+ * drops the alignment of pasted text. Default: the module option
353
+ * `editor.textAlign` (`mapoForm.editor.textAlign`), which is `false`.
354
+ */
355
+ textAlign?: boolean;
347
356
  /**
348
357
  * Buttons added after the built-in ones, past a separator: typically the
349
358
  * commands of the `extensions` the field loads. Each item is
@@ -89,10 +89,12 @@ export function resolveFieldAccessor(
89
89
 
90
90
  /**
91
91
  * Shape of `runtimeConfig.public.mapoForm` as written by the module setup.
92
- * `groups` and `debounce` are guaranteed by the module `defaults`.
92
+ * `groups`, `debounce` and `editor` are guaranteed by the module `defaults`.
93
93
  */
94
94
  export interface MapoFormRuntimeConfig {
95
95
  groups: { expanded: boolean };
96
96
  debounce: number;
97
+ /** Defaults for every `editor` field (module option `editor`). */
98
+ editor: { source: boolean; textAlign: boolean };
97
99
  fields: { attrs: Record<string, Record<string, unknown>> };
98
100
  }