@mapomodule/form 2.0.0-beta.14 → 2.0.0-beta.16
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/__tests__/MapoFormGroup.component.test.d.ts +2 -0
- package/dist/__tests__/MapoFormGroup.component.test.d.ts.map +1 -0
- package/dist/__tests__/MapoWygEditor.component.test.d.ts +2 -0
- package/dist/__tests__/MapoWygEditor.component.test.d.ts.map +1 -0
- package/dist/__tests__/fieldVisibility.component.test.d.ts +2 -0
- package/dist/__tests__/fieldVisibility.component.test.d.ts.map +1 -0
- package/dist/__tests__/formatHtml.test.d.ts +2 -0
- package/dist/__tests__/formatHtml.test.d.ts.map +1 -0
- package/dist/__tests__/sanitizeHtml.test.d.ts +2 -0
- package/dist/__tests__/sanitizeHtml.test.d.ts.map +1 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/module.json +1 -1
- package/dist/runtime/components/MapoFormField.vue +5 -10
- package/dist/runtime/components/MapoFormField.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoFormFlatSection.vue +21 -3
- package/dist/runtime/components/MapoFormFlatSection.vue.d.ts +1 -0
- package/dist/runtime/components/MapoFormFlatSection.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoFormGroup.vue +69 -34
- package/dist/runtime/components/MapoFormGroup.vue.d.ts +19 -4
- package/dist/runtime/components/MapoFormGroup.vue.d.ts.map +1 -1
- package/dist/runtime/components/fields/MapoRepeaterItem.vue +18 -2
- package/dist/runtime/components/fields/MapoRepeaterItem.vue.d.ts.map +1 -1
- package/dist/runtime/components/fields/MapoWygEditor.vue +157 -50
- package/dist/runtime/components/fields/MapoWygEditor.vue.d.ts.map +1 -1
- package/dist/runtime/types/descriptor.d.ts +65 -2
- package/dist/runtime/types/descriptor.d.ts.map +1 -1
- package/dist/runtime/types/index.d.ts +1 -1
- package/dist/runtime/types/index.d.ts.map +1 -1
- package/dist/runtime/utils/formatHtml.d.ts +16 -0
- package/dist/runtime/utils/formatHtml.d.ts.map +1 -0
- package/dist/runtime/utils/formatHtml.js +137 -0
- package/dist/runtime/utils/sanitizeHtml.d.ts +16 -0
- package/dist/runtime/utils/sanitizeHtml.d.ts.map +1 -0
- package/dist/runtime/utils/sanitizeHtml.js +37 -0
- package/dist/runtime/utils/tabVisibility.d.ts +8 -2
- package/dist/runtime/utils/tabVisibility.d.ts.map +1 -1
- package/dist/runtime/utils/tabVisibility.js +20 -2
- package/package.json +2 -2
- package/src/__tests__/MapoFormGroup.component.test.ts +130 -0
- package/src/__tests__/MapoWygEditor.component.test.ts +525 -0
- package/src/__tests__/fieldVisibility.component.test.ts +546 -0
- package/src/__tests__/formatHtml.test.ts +145 -0
- package/src/__tests__/sanitizeHtml.test.ts +40 -0
- package/src/__tests__/tabVisibility.test.ts +27 -0
- package/src/index.ts +2 -0
- package/src/runtime/components/MapoFormField.vue +7 -13
- package/src/runtime/components/MapoFormFlatSection.vue +38 -5
- package/src/runtime/components/MapoFormGroup.vue +116 -43
- package/src/runtime/components/fields/MapoRepeaterItem.vue +32 -2
- package/src/runtime/components/fields/MapoWygEditor.vue +199 -69
- package/src/runtime/types/descriptor.ts +73 -3
- package/src/runtime/types/index.ts +2 -0
- package/src/runtime/utils/formatHtml.ts +198 -0
- package/src/runtime/utils/sanitizeHtml.ts +60 -0
- package/src/runtime/utils/tabVisibility.ts +42 -4
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
<script setup lang="ts">
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
computed,
|
|
4
|
+
ref,
|
|
5
|
+
watch,
|
|
6
|
+
onBeforeUnmount,
|
|
7
|
+
resolveComponent,
|
|
8
|
+
useId,
|
|
9
|
+
} from "vue";
|
|
3
10
|
import { useI18n } from "vue-i18n";
|
|
4
11
|
import { useEditor, EditorContent } from "@tiptap/vue-3";
|
|
5
12
|
import type { AnyExtension } from "@tiptap/vue-3";
|
|
@@ -7,7 +14,18 @@ import StarterKit from "@tiptap/starter-kit";
|
|
|
7
14
|
import Link from "@tiptap/extension-link";
|
|
8
15
|
import Underline from "@tiptap/extension-underline";
|
|
9
16
|
import Image from "@tiptap/extension-image";
|
|
10
|
-
import
|
|
17
|
+
import { debounce } from "@mapomodule/utils";
|
|
18
|
+
import type {
|
|
19
|
+
EditorDescriptor,
|
|
20
|
+
EditorToolbarEditor,
|
|
21
|
+
EditorToolbarItem,
|
|
22
|
+
} from "../../types/index.js";
|
|
23
|
+
import { formatHtml } from "../../utils/formatHtml.js";
|
|
24
|
+
import {
|
|
25
|
+
ALLOWED_LINK_PROTOCOLS,
|
|
26
|
+
isSafeUrl,
|
|
27
|
+
sanitizeHtml,
|
|
28
|
+
} from "../../utils/sanitizeHtml.js";
|
|
11
29
|
|
|
12
30
|
const props = defineProps<{
|
|
13
31
|
modelValue: unknown;
|
|
@@ -24,60 +42,6 @@ const { t } = useI18n();
|
|
|
24
42
|
const extraExtensions =
|
|
25
43
|
(props.descriptor.attrs?.extensions as AnyExtension[] | undefined) ?? [];
|
|
26
44
|
|
|
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
|
-
}
|
|
80
|
-
|
|
81
45
|
const editor = useEditor({
|
|
82
46
|
content: sanitizeHtml((props.modelValue as string) ?? ""),
|
|
83
47
|
editable: !props.readonly && !props.disabled,
|
|
@@ -103,7 +67,65 @@ watch(
|
|
|
103
67
|
([ro, dis]) => editor.value?.setEditable(!ro && !dis),
|
|
104
68
|
);
|
|
105
69
|
|
|
106
|
-
//
|
|
70
|
+
// ─── HTML source view (`attrs.source`) ────────────────────────────────────
|
|
71
|
+
// The textarea edits a copy of the HTML. After each pause in the typing the
|
|
72
|
+
// copy goes through the editor (sanitizer, then schema), and the editor's HTML
|
|
73
|
+
// is what the field emits: the saved value is sanitized and normalized even
|
|
74
|
+
// when the form is saved from the source view. The editor stays mounted behind
|
|
75
|
+
// the textarea (`v-show`), so its history survives the switch.
|
|
76
|
+
const SOURCE_SYNC_DELAY_MS = 250;
|
|
77
|
+
|
|
78
|
+
const hasSourceView = props.descriptor.attrs?.source === true;
|
|
79
|
+
const sourceMode = ref(false);
|
|
80
|
+
const sourceHtml = ref("");
|
|
81
|
+
const sourceHintId = useId();
|
|
82
|
+
// Whether the textarea holds typing the editor has not received yet.
|
|
83
|
+
let sourcePending = false;
|
|
84
|
+
|
|
85
|
+
const isLocked = computed(() => props.readonly || props.disabled);
|
|
86
|
+
const isToolbarDisabled = computed(() => isLocked.value || sourceMode.value);
|
|
87
|
+
|
|
88
|
+
/** Writes the textarea's typing into the editor and emits the result. */
|
|
89
|
+
function flushSource() {
|
|
90
|
+
syncSource.cancel();
|
|
91
|
+
const current = editor.value;
|
|
92
|
+
if (!current) return;
|
|
93
|
+
if (!sourcePending) return;
|
|
94
|
+
sourcePending = false;
|
|
95
|
+
const before = current.getHTML();
|
|
96
|
+
current.commands.setContent(sanitizeHtml(sourceHtml.value), false);
|
|
97
|
+
const after = current.getHTML();
|
|
98
|
+
// Like the visual editor, emit only when the document changed: typing that
|
|
99
|
+
// the schema drops, or whitespace, leaves the value as it is.
|
|
100
|
+
const documentChanged = after !== before;
|
|
101
|
+
if (documentChanged) emit("update:modelValue", after);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const syncSource = debounce(flushSource, SOURCE_SYNC_DELAY_MS);
|
|
105
|
+
|
|
106
|
+
function onSourceInput(value: unknown) {
|
|
107
|
+
if (isLocked.value) return;
|
|
108
|
+
sourceHtml.value = String(value ?? "");
|
|
109
|
+
sourcePending = true;
|
|
110
|
+
syncSource();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function toggleSource() {
|
|
114
|
+
const current = editor.value;
|
|
115
|
+
if (!current) return;
|
|
116
|
+
if (sourceMode.value) {
|
|
117
|
+
// The editor is up to date once the last typing is in.
|
|
118
|
+
flushSource();
|
|
119
|
+
sourceMode.value = false;
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
sourceHtml.value = formatHtml(current.getHTML());
|
|
123
|
+
sourceMode.value = true;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Sync modelValue esterno → editor (es. reset dopo save). The field's own
|
|
127
|
+
// emits come back here as the editor's HTML and are skipped, so the textarea
|
|
128
|
+
// is never rewritten under the cursor.
|
|
107
129
|
watch(
|
|
108
130
|
() => props.modelValue,
|
|
109
131
|
(val) => {
|
|
@@ -112,11 +134,56 @@ watch(
|
|
|
112
134
|
sanitizeHtml((val as string) ?? ""),
|
|
113
135
|
false,
|
|
114
136
|
);
|
|
137
|
+
if (!sourceMode.value) return;
|
|
138
|
+
// A value from outside (a reset, a draft) replaces the typing.
|
|
139
|
+
syncSource.cancel();
|
|
140
|
+
sourcePending = false;
|
|
141
|
+
sourceHtml.value = formatHtml(editor.value.getHTML());
|
|
115
142
|
}
|
|
116
143
|
},
|
|
117
144
|
);
|
|
118
145
|
|
|
119
|
-
onBeforeUnmount(() =>
|
|
146
|
+
onBeforeUnmount(() => {
|
|
147
|
+
// Typing still waiting for the pause is not lost when the field goes away.
|
|
148
|
+
flushSource();
|
|
149
|
+
editor.value?.destroy();
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
// ─── Custom toolbar buttons (`attrs.toolbar`) ─────────────────────────────
|
|
153
|
+
const toolbarItems: EditorToolbarItem[] = props.descriptor.attrs?.toolbar ?? [];
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Asks an item's predicate about the editor; no predicate means `false`. One
|
|
157
|
+
* that throws (a command whose extension the field does not load) gives
|
|
158
|
+
* `fallback` instead of breaking the toolbar.
|
|
159
|
+
*/
|
|
160
|
+
function askItem(
|
|
161
|
+
question: (editor: EditorToolbarEditor) => boolean | undefined,
|
|
162
|
+
fallback: boolean,
|
|
163
|
+
): boolean {
|
|
164
|
+
const current = editor.value;
|
|
165
|
+
if (!current) return fallback;
|
|
166
|
+
try {
|
|
167
|
+
return question(current) ?? false;
|
|
168
|
+
} catch {
|
|
169
|
+
return fallback;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function isItemDisabled(item: EditorToolbarItem): boolean {
|
|
174
|
+
if (isToolbarDisabled.value) return true;
|
|
175
|
+
return askItem((current) => item.isDisabled?.(current), true);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function isItemActive(item: EditorToolbarItem): boolean {
|
|
179
|
+
return askItem((current) => item.isActive?.(current), false);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function runItem(item: EditorToolbarItem) {
|
|
183
|
+
const current = editor.value;
|
|
184
|
+
if (!current) return;
|
|
185
|
+
item.action(current);
|
|
186
|
+
}
|
|
120
187
|
|
|
121
188
|
// ─── Image insertion ──────────────────────────────────────────────────────
|
|
122
189
|
// MapoMediaManagerDialog is globally registered by @mapomodule/uikit.
|
|
@@ -161,7 +228,7 @@ function onImageConfirm(
|
|
|
161
228
|
size="xs"
|
|
162
229
|
variant="ghost"
|
|
163
230
|
color="neutral"
|
|
164
|
-
:disabled="
|
|
231
|
+
:disabled="isToolbarDisabled"
|
|
165
232
|
:class="{ 'bg-gray-200': editor.isActive('bold') }"
|
|
166
233
|
@click="editor.chain().focus().toggleBold().run()"
|
|
167
234
|
>
|
|
@@ -171,7 +238,7 @@ function onImageConfirm(
|
|
|
171
238
|
size="xs"
|
|
172
239
|
variant="ghost"
|
|
173
240
|
color="neutral"
|
|
174
|
-
:disabled="
|
|
241
|
+
:disabled="isToolbarDisabled"
|
|
175
242
|
:class="{ 'bg-gray-200': editor.isActive('italic') }"
|
|
176
243
|
@click="editor.chain().focus().toggleItalic().run()"
|
|
177
244
|
>
|
|
@@ -181,7 +248,7 @@ function onImageConfirm(
|
|
|
181
248
|
size="xs"
|
|
182
249
|
variant="ghost"
|
|
183
250
|
color="neutral"
|
|
184
|
-
:disabled="
|
|
251
|
+
:disabled="isToolbarDisabled"
|
|
185
252
|
:class="{ 'bg-gray-200': editor.isActive('underline') }"
|
|
186
253
|
@click="editor.chain().focus().toggleUnderline().run()"
|
|
187
254
|
>
|
|
@@ -194,7 +261,7 @@ function onImageConfirm(
|
|
|
194
261
|
size="xs"
|
|
195
262
|
variant="ghost"
|
|
196
263
|
color="neutral"
|
|
197
|
-
:disabled="
|
|
264
|
+
:disabled="isToolbarDisabled"
|
|
198
265
|
:class="{ 'bg-gray-200': editor.isActive('heading', { level: 2 }) }"
|
|
199
266
|
@click="editor.chain().focus().toggleHeading({ level: 2 }).run()"
|
|
200
267
|
>
|
|
@@ -204,7 +271,7 @@ function onImageConfirm(
|
|
|
204
271
|
size="xs"
|
|
205
272
|
variant="ghost"
|
|
206
273
|
color="neutral"
|
|
207
|
-
:disabled="
|
|
274
|
+
:disabled="isToolbarDisabled"
|
|
208
275
|
:class="{ 'bg-gray-200': editor.isActive('heading', { level: 3 }) }"
|
|
209
276
|
@click="editor.chain().focus().toggleHeading({ level: 3 }).run()"
|
|
210
277
|
>
|
|
@@ -217,7 +284,7 @@ function onImageConfirm(
|
|
|
217
284
|
size="xs"
|
|
218
285
|
variant="ghost"
|
|
219
286
|
color="neutral"
|
|
220
|
-
:disabled="
|
|
287
|
+
:disabled="isToolbarDisabled"
|
|
221
288
|
:class="{ 'bg-gray-200': editor.isActive('bulletList') }"
|
|
222
289
|
icon="i-lucide-list"
|
|
223
290
|
@click="editor.chain().focus().toggleBulletList().run()"
|
|
@@ -226,7 +293,7 @@ function onImageConfirm(
|
|
|
226
293
|
size="xs"
|
|
227
294
|
variant="ghost"
|
|
228
295
|
color="neutral"
|
|
229
|
-
:disabled="
|
|
296
|
+
:disabled="isToolbarDisabled"
|
|
230
297
|
:class="{ 'bg-gray-200': editor.isActive('orderedList') }"
|
|
231
298
|
icon="i-lucide-list-ordered"
|
|
232
299
|
@click="editor.chain().focus().toggleOrderedList().run()"
|
|
@@ -238,7 +305,7 @@ function onImageConfirm(
|
|
|
238
305
|
size="xs"
|
|
239
306
|
variant="ghost"
|
|
240
307
|
color="neutral"
|
|
241
|
-
:disabled="
|
|
308
|
+
:disabled="isToolbarDisabled"
|
|
242
309
|
icon="i-lucide-undo"
|
|
243
310
|
@click="editor.chain().focus().undo().run()"
|
|
244
311
|
/>
|
|
@@ -246,7 +313,7 @@ function onImageConfirm(
|
|
|
246
313
|
size="xs"
|
|
247
314
|
variant="ghost"
|
|
248
315
|
color="neutral"
|
|
249
|
-
:disabled="
|
|
316
|
+
:disabled="isToolbarDisabled"
|
|
250
317
|
icon="i-lucide-redo"
|
|
251
318
|
@click="editor.chain().focus().redo().run()"
|
|
252
319
|
/>
|
|
@@ -267,18 +334,81 @@ function onImageConfirm(
|
|
|
267
334
|
variant="ghost"
|
|
268
335
|
color="neutral"
|
|
269
336
|
icon="i-lucide-image"
|
|
270
|
-
:disabled="
|
|
337
|
+
:disabled="isToolbarDisabled || !hasMediaManager"
|
|
271
338
|
@click="imagePickerOpen = true"
|
|
272
339
|
/>
|
|
273
340
|
</UTooltip>
|
|
341
|
+
|
|
342
|
+
<!-- Custom buttons (`attrs.toolbar`), after the built-in ones -->
|
|
343
|
+
<template v-if="toolbarItems.length">
|
|
344
|
+
<USeparator orientation="vertical" class="mx-1 h-5" />
|
|
345
|
+
<UTooltip
|
|
346
|
+
v-for="item in toolbarItems"
|
|
347
|
+
:key="item.key"
|
|
348
|
+
:text="item.label"
|
|
349
|
+
:delay-duration="300"
|
|
350
|
+
>
|
|
351
|
+
<UButton
|
|
352
|
+
size="xs"
|
|
353
|
+
variant="ghost"
|
|
354
|
+
color="neutral"
|
|
355
|
+
:icon="item.icon"
|
|
356
|
+
:label="item.icon ? undefined : item.label"
|
|
357
|
+
:aria-label="item.label"
|
|
358
|
+
:disabled="isItemDisabled(item)"
|
|
359
|
+
:class="{ 'bg-gray-200': isItemActive(item) }"
|
|
360
|
+
@click="runItem(item)"
|
|
361
|
+
/>
|
|
362
|
+
</UTooltip>
|
|
363
|
+
</template>
|
|
364
|
+
|
|
365
|
+
<!-- HTML source toggle (`attrs.source`): stays active in the source
|
|
366
|
+
view, and on a read-only field, which can show its source. -->
|
|
367
|
+
<template v-if="hasSourceView">
|
|
368
|
+
<USeparator orientation="vertical" class="mx-1 h-5" />
|
|
369
|
+
<UTooltip :text="t('mapo.wygEditor.source')" :delay-duration="300">
|
|
370
|
+
<UButton
|
|
371
|
+
size="xs"
|
|
372
|
+
variant="ghost"
|
|
373
|
+
color="neutral"
|
|
374
|
+
icon="i-lucide-code-xml"
|
|
375
|
+
:aria-label="t('mapo.wygEditor.source')"
|
|
376
|
+
:aria-pressed="sourceMode"
|
|
377
|
+
:class="{ 'bg-gray-200': sourceMode }"
|
|
378
|
+
@click="toggleSource"
|
|
379
|
+
/>
|
|
380
|
+
</UTooltip>
|
|
381
|
+
</template>
|
|
274
382
|
</div>
|
|
275
383
|
|
|
276
|
-
<!-- Editor content -->
|
|
384
|
+
<!-- Editor content: hidden, not unmounted, in the source view -->
|
|
277
385
|
<EditorContent
|
|
386
|
+
v-show="!sourceMode"
|
|
278
387
|
:editor="editor"
|
|
279
388
|
class="prose prose-sm max-w-none px-3 py-2 focus-within:outline-none"
|
|
280
389
|
/>
|
|
281
390
|
|
|
391
|
+
<div v-if="sourceMode" class="px-3 py-2">
|
|
392
|
+
<UTextarea
|
|
393
|
+
:model-value="sourceHtml"
|
|
394
|
+
autoresize
|
|
395
|
+
:rows="6"
|
|
396
|
+
:readonly="isLocked"
|
|
397
|
+
spellcheck="false"
|
|
398
|
+
autocapitalize="off"
|
|
399
|
+
autocomplete="off"
|
|
400
|
+
:aria-label="t('mapo.wygEditor.source')"
|
|
401
|
+
:aria-describedby="sourceHintId"
|
|
402
|
+
:ui="{ base: 'font-mono text-xs' }"
|
|
403
|
+
class="w-full"
|
|
404
|
+
@update:model-value="onSourceInput"
|
|
405
|
+
@blur="flushSource"
|
|
406
|
+
/>
|
|
407
|
+
<p :id="sourceHintId" class="mt-1 text-xs text-muted">
|
|
408
|
+
{{ t("mapo.wygEditor.sourceHint") }}
|
|
409
|
+
</p>
|
|
410
|
+
</div>
|
|
411
|
+
|
|
282
412
|
<p
|
|
283
413
|
v-for="err in errors"
|
|
284
414
|
:key="err"
|
|
@@ -82,9 +82,17 @@ interface FieldBase<T> {
|
|
|
82
82
|
required?: boolean;
|
|
83
83
|
/** Renders the field in read-only mode (no user input allowed). */
|
|
84
84
|
readonly?: boolean;
|
|
85
|
-
/**
|
|
85
|
+
/**
|
|
86
|
+
* `v-if` predicate — when `false`, removes the field from the DOM. Its grid
|
|
87
|
+
* cell in a form section, group, sub-tab or repeater item goes with it, so it
|
|
88
|
+
* leaves no gap. Inside a repeater item, `model` is the item.
|
|
89
|
+
*/
|
|
86
90
|
visible?: (ctx: { model: T }) => boolean;
|
|
87
|
-
/**
|
|
91
|
+
/**
|
|
92
|
+
* `v-show` predicate — when `false`, hides the field and its grid cell in a
|
|
93
|
+
* form section, group, sub-tab or repeater item; the field stays mounted and
|
|
94
|
+
* keeps its state. Inside a repeater item, `model` is the item.
|
|
95
|
+
*/
|
|
88
96
|
show?: (ctx: { model: T }) => boolean;
|
|
89
97
|
/** Callback fired after the field value changes. */
|
|
90
98
|
onChange?: (ctx: { model: T; val: unknown }) => void;
|
|
@@ -276,12 +284,74 @@ export interface FileDescriptor<
|
|
|
276
284
|
attrs?: { accept?: string; maxSize?: number };
|
|
277
285
|
}
|
|
278
286
|
|
|
287
|
+
/**
|
|
288
|
+
* The Tiptap editor a toolbar button acts on, described by the members a button
|
|
289
|
+
* needs so that the descriptor types do not depend on Tiptap. At runtime it is
|
|
290
|
+
* the field's Tiptap `Editor`: cast it for the rest of its API. The command
|
|
291
|
+
* builders are untyped because the commands available depend on the extensions
|
|
292
|
+
* the field loads (`attrs.extensions`).
|
|
293
|
+
*/
|
|
294
|
+
export interface EditorToolbarEditor {
|
|
295
|
+
/** Starts a command chain, as in `editor.chain().focus().toggleBold().run()`. */
|
|
296
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
297
|
+
chain(): any;
|
|
298
|
+
/** Dry-runs commands: `editor.can().addRowAfter()` is `true` when the command would apply. */
|
|
299
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
300
|
+
can(): any;
|
|
301
|
+
/** Runs one command, as in `editor.commands.setHorizontalRule()`. */
|
|
302
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
303
|
+
readonly commands: any;
|
|
304
|
+
/** Whether a mark or a node is active at the selection, as in `editor.isActive('table')`. */
|
|
305
|
+
isActive(name: string, attributes?: Record<string, unknown>): boolean;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* A button that `attrs.toolbar` adds to an editor field, after the built-in
|
|
310
|
+
* buttons. Methods, not function properties: a function typed with Tiptap's own
|
|
311
|
+
* `Editor` fits them too.
|
|
312
|
+
*/
|
|
313
|
+
export interface EditorToolbarItem {
|
|
314
|
+
/** Identifies the button among the custom ones; unique within the toolbar. */
|
|
315
|
+
key: string;
|
|
316
|
+
/** Tooltip and accessible name of the button; its text when it has no `icon`. */
|
|
317
|
+
label: string;
|
|
318
|
+
/** Icon name, as `UButton` takes it (e.g. `i-lucide-table`). */
|
|
319
|
+
icon?: string;
|
|
320
|
+
/** Runs on click, as in `(editor) => editor.chain().focus().insertTable().run()`. */
|
|
321
|
+
action(editor: EditorToolbarEditor): void;
|
|
322
|
+
/** Shows the button as active, like the built-in ones on bold text or a heading. */
|
|
323
|
+
isActive?(editor: EditorToolbarEditor): boolean;
|
|
324
|
+
/**
|
|
325
|
+
* Disables the button, as in `(editor) => !editor.can().addRowAfter()`. The
|
|
326
|
+
* button is also disabled when the field is read-only and in the source view.
|
|
327
|
+
*/
|
|
328
|
+
isDisabled?(editor: EditorToolbarEditor): boolean;
|
|
329
|
+
}
|
|
330
|
+
|
|
279
331
|
/** Descriptor for a Tiptap rich-text editor. */
|
|
280
332
|
export interface EditorDescriptor<
|
|
281
333
|
T = Record<string, unknown>,
|
|
282
334
|
> extends FieldBase<T> {
|
|
283
335
|
type: `${KnownFieldType.Editor}`;
|
|
284
|
-
attrs?: {
|
|
336
|
+
attrs?: {
|
|
337
|
+
/** Tiptap v2 extensions loaded after the built-in ones (StarterKit, Link, Underline, Image). */
|
|
338
|
+
extensions?: unknown[];
|
|
339
|
+
/**
|
|
340
|
+
* Adds an HTML source toggle at the end of the toolbar. The source view
|
|
341
|
+
* edits the HTML in a textarea; what it saves goes through the editor, so it
|
|
342
|
+
* is sanitized, and the tags and attributes no extension supports are
|
|
343
|
+
* dropped. A read-only field shows the source without letting it change.
|
|
344
|
+
* Default: `false`.
|
|
345
|
+
*/
|
|
346
|
+
source?: boolean;
|
|
347
|
+
/**
|
|
348
|
+
* Buttons added after the built-in ones, past a separator: typically the
|
|
349
|
+
* commands of the `extensions` the field loads. Each item is
|
|
350
|
+
* `{ key, label, icon?, action(editor), isActive?(editor), isDisabled?(editor) }`.
|
|
351
|
+
* Disabled when the field is read-only and in the source view.
|
|
352
|
+
*/
|
|
353
|
+
toolbar?: EditorToolbarItem[];
|
|
354
|
+
};
|
|
285
355
|
}
|
|
286
356
|
|
|
287
357
|
/** Descriptor for the SEO preview field (title + description + live SERP preview). */
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Block-level tags: each one starts a line of its own, and a block that holds
|
|
3
|
+
* other blocks closes on a line of its own, one indent step out.
|
|
4
|
+
*/
|
|
5
|
+
const BLOCK_TAGS = new Set([
|
|
6
|
+
"p",
|
|
7
|
+
"h1",
|
|
8
|
+
"h2",
|
|
9
|
+
"h3",
|
|
10
|
+
"h4",
|
|
11
|
+
"h5",
|
|
12
|
+
"h6",
|
|
13
|
+
"ul",
|
|
14
|
+
"ol",
|
|
15
|
+
"li",
|
|
16
|
+
"table",
|
|
17
|
+
"colgroup",
|
|
18
|
+
"col",
|
|
19
|
+
"thead",
|
|
20
|
+
"tbody",
|
|
21
|
+
"tfoot",
|
|
22
|
+
"tr",
|
|
23
|
+
"th",
|
|
24
|
+
"td",
|
|
25
|
+
"blockquote",
|
|
26
|
+
"pre",
|
|
27
|
+
"hr",
|
|
28
|
+
"img",
|
|
29
|
+
"div",
|
|
30
|
+
"figure",
|
|
31
|
+
"figcaption",
|
|
32
|
+
]);
|
|
33
|
+
|
|
34
|
+
/** Blocks that hold a line of text: an `<img>` inside one is part of the text. */
|
|
35
|
+
const TEXTBLOCK_TAGS = new Set([
|
|
36
|
+
"p",
|
|
37
|
+
"h1",
|
|
38
|
+
"h2",
|
|
39
|
+
"h3",
|
|
40
|
+
"h4",
|
|
41
|
+
"h5",
|
|
42
|
+
"h6",
|
|
43
|
+
"figcaption",
|
|
44
|
+
]);
|
|
45
|
+
|
|
46
|
+
/** Tags with no closing tag: they never open a level. */
|
|
47
|
+
const VOID_TAGS = new Set([
|
|
48
|
+
"area",
|
|
49
|
+
"base",
|
|
50
|
+
"br",
|
|
51
|
+
"col",
|
|
52
|
+
"embed",
|
|
53
|
+
"hr",
|
|
54
|
+
"img",
|
|
55
|
+
"input",
|
|
56
|
+
"link",
|
|
57
|
+
"meta",
|
|
58
|
+
"source",
|
|
59
|
+
"track",
|
|
60
|
+
"wbr",
|
|
61
|
+
]);
|
|
62
|
+
|
|
63
|
+
/** Tags whose content is copied as it is: whitespace is part of the content there. */
|
|
64
|
+
const VERBATIM_TAGS = new Set(["pre", "code"]);
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* A comment, a tag (quoted attribute values may hold `>`), a run of text, or a
|
|
68
|
+
* `<` that starts none of them.
|
|
69
|
+
*/
|
|
70
|
+
const TOKEN =
|
|
71
|
+
/<!--[\s\S]*?-->|<\/?[a-zA-Z][^\s/>]*(?:"[^"]*"|'[^']*'|[^'">])*>|[^<]+|</g;
|
|
72
|
+
const TAG_NAME = /^<\/?([a-zA-Z][a-zA-Z0-9-]*)/;
|
|
73
|
+
/** HTML whitespace only: a no-break space is content. */
|
|
74
|
+
const BLANK = /^[ \t\n\f\r]*$/;
|
|
75
|
+
const LEADING_BLANK = /^[ \t\n\f\r]+/;
|
|
76
|
+
const TRAILING_BLANK = /[ \t\n\f\r]+$/;
|
|
77
|
+
|
|
78
|
+
interface OpenBlock {
|
|
79
|
+
name: string;
|
|
80
|
+
/** Whether a block opened inside it, so its closing tag needs a line of its own. */
|
|
81
|
+
holdsBlocks: boolean;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** What came last: a block's opening tag, the end of a block, or inline content. */
|
|
85
|
+
type LastToken = "block-open" | "block-end" | "inline";
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Lays out an HTML fragment for reading: a line per block-level tag (paragraphs,
|
|
89
|
+
* headings, lists, tables, quotes, `pre`, `hr`, `img`), indented by nesting, with
|
|
90
|
+
* inline content kept on its block's line.
|
|
91
|
+
*
|
|
92
|
+
* The result parses back to the same document: only whitespace between blocks
|
|
93
|
+
* and at the edges of a block is added or dropped, which an HTML parser that
|
|
94
|
+
* collapses whitespace (Tiptap's) ignores. The content of `<pre>` and `<code>`
|
|
95
|
+
* is copied as it is, and an `<img>` inside a line of text stays on that line.
|
|
96
|
+
*
|
|
97
|
+
* @param html The fragment to lay out, typically Tiptap's `editor.getHTML()`.
|
|
98
|
+
* @param indent The string one nesting level adds. Default: two spaces.
|
|
99
|
+
* @returns The fragment over several lines, with no trailing newline.
|
|
100
|
+
*/
|
|
101
|
+
export function formatHtml(html: string, indent = " "): string {
|
|
102
|
+
const lines: string[] = [];
|
|
103
|
+
const open: OpenBlock[] = [];
|
|
104
|
+
let line = "";
|
|
105
|
+
let last: LastToken = "block-open";
|
|
106
|
+
let verbatim: { name: string; depth: number } | null = null;
|
|
107
|
+
|
|
108
|
+
const startLine = () => {
|
|
109
|
+
const isBlankLine = BLANK.test(line);
|
|
110
|
+
if (!isBlankLine) lines.push(line.replace(TRAILING_BLANK, ""));
|
|
111
|
+
line = indent.repeat(open.length);
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Closes the innermost open block named `name`; a block that held other
|
|
116
|
+
* blocks closes on a line of its own. A closing tag with no opening one
|
|
117
|
+
* stays where it is.
|
|
118
|
+
*/
|
|
119
|
+
const closeBlock = (name: string, token: string) => {
|
|
120
|
+
let at = open.length - 1;
|
|
121
|
+
while (at >= 0 && open[at]!.name !== name) at -= 1;
|
|
122
|
+
const isStray = at < 0;
|
|
123
|
+
if (isStray) {
|
|
124
|
+
line += token;
|
|
125
|
+
last = "block-end";
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
const holdsBlocks = open[at]!.holdsBlocks;
|
|
129
|
+
open.length = at;
|
|
130
|
+
if (holdsBlocks) startLine();
|
|
131
|
+
line += token;
|
|
132
|
+
last = "block-end";
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
for (const [token] of html.matchAll(TOKEN)) {
|
|
136
|
+
const name = TAG_NAME.exec(token)?.[1]?.toLowerCase();
|
|
137
|
+
const isClosing = token.startsWith("</");
|
|
138
|
+
|
|
139
|
+
if (verbatim) {
|
|
140
|
+
const isVerbatimTag = name === verbatim.name;
|
|
141
|
+
if (isVerbatimTag && isClosing) verbatim.depth -= 1;
|
|
142
|
+
else if (isVerbatimTag) verbatim.depth += 1;
|
|
143
|
+
const verbatimEnds = verbatim.depth === 0;
|
|
144
|
+
if (!verbatimEnds) {
|
|
145
|
+
line += token;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
verbatim = null;
|
|
149
|
+
// `</pre>` closes a block, `</code>` an inline element.
|
|
150
|
+
if (name === "pre") closeBlock(name, token);
|
|
151
|
+
else line += token;
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const isBlock = name !== undefined && BLOCK_TAGS.has(name);
|
|
156
|
+
const parent = open.at(-1);
|
|
157
|
+
const isInsideText =
|
|
158
|
+
last === "inline" ||
|
|
159
|
+
(parent !== undefined && TEXTBLOCK_TAGS.has(parent.name));
|
|
160
|
+
const isInlineImage = name === "img" && isInsideText;
|
|
161
|
+
|
|
162
|
+
if (!isBlock || isInlineImage) {
|
|
163
|
+
const isText = name === undefined;
|
|
164
|
+
const isBlankBetweenBlocks =
|
|
165
|
+
isText && last !== "inline" && BLANK.test(token);
|
|
166
|
+
if (isBlankBetweenBlocks) continue;
|
|
167
|
+
if (last === "block-end") startLine();
|
|
168
|
+
// Whitespace at the start of a block's text is not content.
|
|
169
|
+
const startsText = isText && last !== "inline";
|
|
170
|
+
line += startsText ? token.replace(LEADING_BLANK, "") : token;
|
|
171
|
+
last = "inline";
|
|
172
|
+
const opensVerbatim =
|
|
173
|
+
name !== undefined && VERBATIM_TAGS.has(name) && !isClosing;
|
|
174
|
+
if (opensVerbatim) verbatim = { name, depth: 1 };
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (isClosing) {
|
|
179
|
+
closeBlock(name, token);
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (parent) parent.holdsBlocks = true;
|
|
184
|
+
startLine();
|
|
185
|
+
line += token;
|
|
186
|
+
const isVoid = VOID_TAGS.has(name) || token.endsWith("/>");
|
|
187
|
+
if (isVoid) {
|
|
188
|
+
last = "block-end";
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
open.push({ name, holdsBlocks: false });
|
|
192
|
+
last = "block-open";
|
|
193
|
+
if (name === "pre") verbatim = { name, depth: 1 };
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
startLine();
|
|
197
|
+
return lines.join("\n");
|
|
198
|
+
}
|