@mapomodule/form 2.0.0-beta.15 → 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.
Files changed (43) hide show
  1. package/dist/__tests__/MapoWygEditor.component.test.d.ts +2 -0
  2. package/dist/__tests__/MapoWygEditor.component.test.d.ts.map +1 -0
  3. package/dist/__tests__/formatHtml.test.d.ts +2 -0
  4. package/dist/__tests__/formatHtml.test.d.ts.map +1 -0
  5. package/dist/__tests__/sanitizeHtml.test.d.ts +2 -0
  6. package/dist/__tests__/sanitizeHtml.test.d.ts.map +1 -0
  7. package/dist/index.d.mts +1 -1
  8. package/dist/index.d.ts +1 -1
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/module.d.mts +13 -0
  11. package/dist/module.d.ts +13 -0
  12. package/dist/module.d.ts.map +1 -1
  13. package/dist/module.json +1 -1
  14. package/dist/module.mjs +6 -1
  15. package/dist/runtime/components/fields/MapoWygEditor.vue +351 -51
  16. package/dist/runtime/components/fields/MapoWygEditor.vue.d.ts.map +1 -1
  17. package/dist/runtime/devtools/DevtoolsFormsPage.vue +22 -0
  18. package/dist/runtime/devtools/DevtoolsFormsPage.vue.d.ts.map +1 -1
  19. package/dist/runtime/types/descriptor.d.ts +64 -0
  20. package/dist/runtime/types/descriptor.d.ts.map +1 -1
  21. package/dist/runtime/types/index.d.ts +1 -1
  22. package/dist/runtime/types/index.d.ts.map +1 -1
  23. package/dist/runtime/types/registry.d.ts +6 -1
  24. package/dist/runtime/types/registry.d.ts.map +1 -1
  25. package/dist/runtime/utils/formatHtml.d.ts +16 -0
  26. package/dist/runtime/utils/formatHtml.d.ts.map +1 -0
  27. package/dist/runtime/utils/formatHtml.js +137 -0
  28. package/dist/runtime/utils/sanitizeHtml.d.ts +16 -0
  29. package/dist/runtime/utils/sanitizeHtml.d.ts.map +1 -0
  30. package/dist/runtime/utils/sanitizeHtml.js +37 -0
  31. package/package.json +3 -2
  32. package/src/__tests__/MapoWygEditor.component.test.ts +886 -0
  33. package/src/__tests__/formatHtml.test.ts +145 -0
  34. package/src/__tests__/sanitizeHtml.test.ts +40 -0
  35. package/src/index.ts +2 -0
  36. package/src/module.ts +20 -2
  37. package/src/runtime/components/fields/MapoWygEditor.vue +532 -69
  38. package/src/runtime/devtools/DevtoolsFormsPage.vue +23 -0
  39. package/src/runtime/types/descriptor.ts +72 -1
  40. package/src/runtime/types/index.ts +2 -0
  41. package/src/runtime/types/registry.ts +3 -1
  42. package/src/runtime/utils/formatHtml.ts +198 -0
  43. package/src/runtime/utils/sanitizeHtml.ts +60 -0
@@ -1,13 +1,34 @@
1
1
  <script setup lang="ts">
2
- import { ref, watch, onBeforeUnmount, resolveComponent } from "vue";
2
+ import {
3
+ computed,
4
+ ref,
5
+ watch,
6
+ onBeforeUnmount,
7
+ resolveComponent,
8
+ useId,
9
+ } from "vue";
3
10
  import { useI18n } from "vue-i18n";
11
+ import { useRuntimeConfig } from "#app";
4
12
  import { useEditor, EditorContent } from "@tiptap/vue-3";
5
13
  import type { AnyExtension } from "@tiptap/vue-3";
6
14
  import StarterKit from "@tiptap/starter-kit";
7
15
  import Link from "@tiptap/extension-link";
8
16
  import Underline from "@tiptap/extension-underline";
9
17
  import Image from "@tiptap/extension-image";
10
- import type { EditorDescriptor } from "../../types/index.js";
18
+ import TextAlign from "@tiptap/extension-text-align";
19
+ import { debounce } from "@mapomodule/utils";
20
+ import type {
21
+ EditorDescriptor,
22
+ EditorToolbarEditor,
23
+ EditorToolbarItem,
24
+ MapoFormRuntimeConfig,
25
+ } from "../../types/index.js";
26
+ import { formatHtml } from "../../utils/formatHtml.js";
27
+ import {
28
+ ALLOWED_LINK_PROTOCOLS,
29
+ isSafeUrl,
30
+ sanitizeHtml,
31
+ } from "../../utils/sanitizeHtml.js";
11
32
 
12
33
  const props = defineProps<{
13
34
  modelValue: unknown;
@@ -21,62 +42,33 @@ const emit = defineEmits<{ "update:modelValue": [value: string] }>();
21
42
 
22
43
  const { t } = useI18n();
23
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
+
24
50
  const extraExtensions =
25
51
  (props.descriptor.attrs?.extensions as AnyExtension[] | undefined) ?? [];
26
52
 
27
- // Protocol allowlist for user-entered links. Without this,
28
- // `<a href="javascript:...">` would be accepted, leading to XSS when content is
29
- // rendered elsewhere (preview, server-side, other editors).
30
- const ALLOWED_LINK_PROTOCOLS = ["http", "https", "mailto", "tel"];
31
-
32
- function isSafeUrl(url: string): boolean {
33
- try {
34
- // URL relativi (es. "/page") non hanno protocol — li accettiamo.
35
- if (!/^[a-z][a-z0-9+.-]*:/i.test(url)) return true;
36
- const proto = new URL(url, "https://placeholder.local").protocol
37
- .replace(":", "")
38
- .toLowerCase();
39
- return ALLOWED_LINK_PROTOCOLS.includes(proto);
40
- } catch {
41
- return false;
42
- }
43
- }
44
-
45
- // Sanitise incoming HTML from external sources (initial set, server sync)
46
- // by removing <script>, inline handlers (on*), and href: javascript:. This is
47
- // intentionally minimal (no DOMPurify) to avoid a heavy dependency: it covers the
48
- // most common vectors; for truly untrusted HTML the consumer should use DOMPurify
49
- // at the app level.
50
- function sanitizeHtml(html: string): string {
51
- if (typeof window === "undefined" || !html) return html;
52
- const tpl = document.createElement("template");
53
- tpl.innerHTML = html;
54
- const walker = document.createTreeWalker(
55
- tpl.content,
56
- NodeFilter.SHOW_ELEMENT,
57
- );
58
- const toRemove: Element[] = [];
59
- let node = walker.nextNode() as Element | null;
60
- while (node) {
61
- if (node.tagName === "SCRIPT" || node.tagName === "STYLE") {
62
- toRemove.push(node);
63
- } else {
64
- for (const attr of Array.from(node.attributes)) {
65
- const name = attr.name.toLowerCase();
66
- if (name.startsWith("on")) node.removeAttribute(attr.name);
67
- if (
68
- (name === "href" || name === "src" || name === "xlink:href") &&
69
- !isSafeUrl(attr.value)
70
- ) {
71
- node.removeAttribute(attr.name);
72
- }
73
- }
74
- }
75
- node = walker.nextNode() as Element | null;
76
- }
77
- toRemove.forEach((el) => el.remove());
78
- return tpl.innerHTML;
79
- }
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
+ : [];
80
72
 
81
73
  const editor = useEditor({
82
74
  content: sanitizeHtml((props.modelValue as string) ?? ""),
@@ -90,6 +82,7 @@ const editor = useEditor({
90
82
  }),
91
83
  Underline,
92
84
  Image.configure({ inline: false, allowBase64: false }),
85
+ ...alignmentExtensions,
93
86
  ...extraExtensions,
94
87
  ] as AnyExtension[],
95
88
  onUpdate: ({ editor: e }) => {
@@ -103,7 +96,68 @@ watch(
103
96
  ([ro, dis]) => editor.value?.setEditable(!ro && !dis),
104
97
  );
105
98
 
106
- // Sync modelValue esterno → editor (es. reset dopo save)
99
+ // ─── HTML source view (`attrs.source`) ────────────────────────────────────
100
+ // The textarea edits a copy of the HTML. After each pause in the typing the
101
+ // copy goes through the editor (sanitizer, then schema), and the editor's HTML
102
+ // is what the field emits: the saved value is sanitized and normalized even
103
+ // when the form is saved from the source view. The editor stays mounted behind
104
+ // the textarea (`v-show`), so its history survives the switch.
105
+ const SOURCE_SYNC_DELAY_MS = 250;
106
+
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;
111
+ const sourceMode = ref(false);
112
+ const sourceHtml = ref("");
113
+ const sourceHintId = useId();
114
+ // Whether the textarea holds typing the editor has not received yet.
115
+ let sourcePending = false;
116
+
117
+ const isLocked = computed(() => props.readonly || props.disabled);
118
+ const isToolbarDisabled = computed(() => isLocked.value || sourceMode.value);
119
+
120
+ /** Writes the textarea's typing into the editor and emits the result. */
121
+ function flushSource() {
122
+ syncSource.cancel();
123
+ const current = editor.value;
124
+ if (!current) return;
125
+ if (!sourcePending) return;
126
+ sourcePending = false;
127
+ const before = current.getHTML();
128
+ current.commands.setContent(sanitizeHtml(sourceHtml.value), false);
129
+ const after = current.getHTML();
130
+ // Like the visual editor, emit only when the document changed: typing that
131
+ // the schema drops, or whitespace, leaves the value as it is.
132
+ const documentChanged = after !== before;
133
+ if (documentChanged) emit("update:modelValue", after);
134
+ }
135
+
136
+ const syncSource = debounce(flushSource, SOURCE_SYNC_DELAY_MS);
137
+
138
+ function onSourceInput(value: unknown) {
139
+ if (isLocked.value) return;
140
+ sourceHtml.value = String(value ?? "");
141
+ sourcePending = true;
142
+ syncSource();
143
+ }
144
+
145
+ function toggleSource() {
146
+ const current = editor.value;
147
+ if (!current) return;
148
+ if (sourceMode.value) {
149
+ // The editor is up to date once the last typing is in.
150
+ flushSource();
151
+ sourceMode.value = false;
152
+ return;
153
+ }
154
+ sourceHtml.value = formatHtml(current.getHTML());
155
+ sourceMode.value = true;
156
+ }
157
+
158
+ // Sync modelValue esterno → editor (es. reset dopo save). The field's own
159
+ // emits come back here as the editor's HTML and are skipped, so the textarea
160
+ // is never rewritten under the cursor.
107
161
  watch(
108
162
  () => props.modelValue,
109
163
  (val) => {
@@ -112,11 +166,170 @@ watch(
112
166
  sanitizeHtml((val as string) ?? ""),
113
167
  false,
114
168
  );
169
+ if (!sourceMode.value) return;
170
+ // A value from outside (a reset, a draft) replaces the typing.
171
+ syncSource.cancel();
172
+ sourcePending = false;
173
+ sourceHtml.value = formatHtml(editor.value.getHTML());
115
174
  }
116
175
  },
117
176
  );
118
177
 
119
- onBeforeUnmount(() => editor.value?.destroy());
178
+ onBeforeUnmount(() => {
179
+ // Typing still waiting for the pause is not lost when the field goes away.
180
+ flushSource();
181
+ editor.value?.destroy();
182
+ });
183
+
184
+ // ─── Custom toolbar buttons (`attrs.toolbar`) ─────────────────────────────
185
+ const toolbarItems: EditorToolbarItem[] = props.descriptor.attrs?.toolbar ?? [];
186
+
187
+ /**
188
+ * Asks an item's predicate about the editor; no predicate means `false`. One
189
+ * that throws (a command whose extension the field does not load) gives
190
+ * `fallback` instead of breaking the toolbar.
191
+ */
192
+ function askItem(
193
+ question: (editor: EditorToolbarEditor) => boolean | undefined,
194
+ fallback: boolean,
195
+ ): boolean {
196
+ const current = editor.value;
197
+ if (!current) return fallback;
198
+ try {
199
+ return question(current) ?? false;
200
+ } catch {
201
+ return fallback;
202
+ }
203
+ }
204
+
205
+ function isItemDisabled(item: EditorToolbarItem): boolean {
206
+ if (isToolbarDisabled.value) return true;
207
+ return askItem((current) => item.isDisabled?.(current), true);
208
+ }
209
+
210
+ function isItemActive(item: EditorToolbarItem): boolean {
211
+ return askItem((current) => item.isActive?.(current), false);
212
+ }
213
+
214
+ function runItem(item: EditorToolbarItem) {
215
+ const current = editor.value;
216
+ if (!current) return;
217
+ item.action(current);
218
+ }
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
+ }
120
333
 
121
334
  // ─── Image insertion ──────────────────────────────────────────────────────
122
335
  // MapoMediaManagerDialog is globally registered by @mapomodule/uikit.
@@ -161,7 +374,7 @@ function onImageConfirm(
161
374
  size="xs"
162
375
  variant="ghost"
163
376
  color="neutral"
164
- :disabled="readonly || disabled"
377
+ :disabled="isToolbarDisabled"
165
378
  :class="{ 'bg-gray-200': editor.isActive('bold') }"
166
379
  @click="editor.chain().focus().toggleBold().run()"
167
380
  >
@@ -171,7 +384,7 @@ function onImageConfirm(
171
384
  size="xs"
172
385
  variant="ghost"
173
386
  color="neutral"
174
- :disabled="readonly || disabled"
387
+ :disabled="isToolbarDisabled"
175
388
  :class="{ 'bg-gray-200': editor.isActive('italic') }"
176
389
  @click="editor.chain().focus().toggleItalic().run()"
177
390
  >
@@ -181,7 +394,7 @@ function onImageConfirm(
181
394
  size="xs"
182
395
  variant="ghost"
183
396
  color="neutral"
184
- :disabled="readonly || disabled"
397
+ :disabled="isToolbarDisabled"
185
398
  :class="{ 'bg-gray-200': editor.isActive('underline') }"
186
399
  @click="editor.chain().focus().toggleUnderline().run()"
187
400
  >
@@ -194,7 +407,7 @@ function onImageConfirm(
194
407
  size="xs"
195
408
  variant="ghost"
196
409
  color="neutral"
197
- :disabled="readonly || disabled"
410
+ :disabled="isToolbarDisabled"
198
411
  :class="{ 'bg-gray-200': editor.isActive('heading', { level: 2 }) }"
199
412
  @click="editor.chain().focus().toggleHeading({ level: 2 }).run()"
200
413
  >
@@ -204,7 +417,7 @@ function onImageConfirm(
204
417
  size="xs"
205
418
  variant="ghost"
206
419
  color="neutral"
207
- :disabled="readonly || disabled"
420
+ :disabled="isToolbarDisabled"
208
421
  :class="{ 'bg-gray-200': editor.isActive('heading', { level: 3 }) }"
209
422
  @click="editor.chain().focus().toggleHeading({ level: 3 }).run()"
210
423
  >
@@ -217,7 +430,7 @@ function onImageConfirm(
217
430
  size="xs"
218
431
  variant="ghost"
219
432
  color="neutral"
220
- :disabled="readonly || disabled"
433
+ :disabled="isToolbarDisabled"
221
434
  :class="{ 'bg-gray-200': editor.isActive('bulletList') }"
222
435
  icon="i-lucide-list"
223
436
  @click="editor.chain().focus().toggleBulletList().run()"
@@ -226,19 +439,42 @@ function onImageConfirm(
226
439
  size="xs"
227
440
  variant="ghost"
228
441
  color="neutral"
229
- :disabled="readonly || disabled"
442
+ :disabled="isToolbarDisabled"
230
443
  :class="{ 'bg-gray-200': editor.isActive('orderedList') }"
231
444
  icon="i-lucide-list-ordered"
232
445
  @click="editor.chain().focus().toggleOrderedList().run()"
233
446
  />
234
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
+
235
471
  <USeparator orientation="vertical" class="mx-1 h-5" />
236
472
 
237
473
  <UButton
238
474
  size="xs"
239
475
  variant="ghost"
240
476
  color="neutral"
241
- :disabled="readonly || disabled"
477
+ :disabled="isToolbarDisabled"
242
478
  icon="i-lucide-undo"
243
479
  @click="editor.chain().focus().undo().run()"
244
480
  />
@@ -246,13 +482,28 @@ function onImageConfirm(
246
482
  size="xs"
247
483
  variant="ghost"
248
484
  color="neutral"
249
- :disabled="readonly || disabled"
485
+ :disabled="isToolbarDisabled"
250
486
  icon="i-lucide-redo"
251
487
  @click="editor.chain().focus().redo().run()"
252
488
  />
253
489
 
254
490
  <USeparator orientation="vertical" class="mx-1 h-5" />
255
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
+
256
507
  <!-- Insert Image via Media Manager (available when @mapomodule/uikit is installed) -->
257
508
  <UTooltip
258
509
  :text="
@@ -267,18 +518,130 @@ function onImageConfirm(
267
518
  variant="ghost"
268
519
  color="neutral"
269
520
  icon="i-lucide-image"
270
- :disabled="readonly || disabled || !hasMediaManager"
521
+ :disabled="isToolbarDisabled || !hasMediaManager"
271
522
  @click="imagePickerOpen = true"
272
523
  />
273
524
  </UTooltip>
525
+
526
+ <!-- Custom buttons (`attrs.toolbar`), after the built-in ones -->
527
+ <template v-if="toolbarItems.length">
528
+ <USeparator orientation="vertical" class="mx-1 h-5" />
529
+ <UTooltip
530
+ v-for="item in toolbarItems"
531
+ :key="item.key"
532
+ :text="item.label"
533
+ :delay-duration="300"
534
+ >
535
+ <UButton
536
+ size="xs"
537
+ variant="ghost"
538
+ color="neutral"
539
+ :icon="item.icon"
540
+ :label="item.icon ? undefined : item.label"
541
+ :aria-label="item.label"
542
+ :disabled="isItemDisabled(item)"
543
+ :class="{ 'bg-gray-200': isItemActive(item) }"
544
+ @click="runItem(item)"
545
+ />
546
+ </UTooltip>
547
+ </template>
548
+
549
+ <!-- HTML source toggle (`attrs.source`): stays active in the source
550
+ view, and on a read-only field, which can show its source. -->
551
+ <template v-if="hasSourceView">
552
+ <USeparator orientation="vertical" class="mx-1 h-5" />
553
+ <UTooltip :text="t('mapo.wygEditor.source')" :delay-duration="300">
554
+ <UButton
555
+ size="xs"
556
+ variant="ghost"
557
+ color="neutral"
558
+ icon="i-lucide-code-xml"
559
+ :aria-label="t('mapo.wygEditor.source')"
560
+ :aria-pressed="sourceMode"
561
+ :class="{ 'bg-gray-200': sourceMode }"
562
+ @click="toggleSource"
563
+ />
564
+ </UTooltip>
565
+ </template>
274
566
  </div>
275
567
 
276
- <!-- Editor content -->
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. -->
277
618
  <EditorContent
619
+ v-show="!sourceMode"
278
620
  :editor="editor"
279
- 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"
280
622
  />
281
623
 
624
+ <div v-if="sourceMode" class="px-3 py-2">
625
+ <UTextarea
626
+ :model-value="sourceHtml"
627
+ autoresize
628
+ :rows="6"
629
+ :readonly="isLocked"
630
+ spellcheck="false"
631
+ autocapitalize="off"
632
+ autocomplete="off"
633
+ :aria-label="t('mapo.wygEditor.source')"
634
+ :aria-describedby="sourceHintId"
635
+ :ui="{ base: 'font-mono text-xs' }"
636
+ class="w-full"
637
+ @update:model-value="onSourceInput"
638
+ @blur="flushSource"
639
+ />
640
+ <p :id="sourceHintId" class="mt-1 text-xs text-muted">
641
+ {{ t("mapo.wygEditor.sourceHint") }}
642
+ </p>
643
+ </div>
644
+
282
645
  <p
283
646
  v-for="err in errors"
284
647
  :key="err"
@@ -302,3 +665,103 @@ function onImageConfirm(
302
665
  />
303
666
  </div>
304
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>