@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
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=MapoWygEditor.component.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MapoWygEditor.component.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/MapoWygEditor.component.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=formatHtml.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"formatHtml.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/formatHtml.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=sanitizeHtml.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sanitizeHtml.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/sanitizeHtml.test.ts"],"names":[],"mappings":""}
package/dist/index.d.mts CHANGED
@@ -5,5 +5,5 @@ export { FocusTarget, useFocusMode } from '../dist/runtime/composables/useFocusM
5
5
  export { UnsavedChangesGuardOptions, useUnsavedChangesGuard } from '../dist/runtime/composables/useUnsavedChangesGuard.js';
6
6
  export { greaterThan, isEmpty, isNoneOf, isNotEmpty, isOneOf, lessThan, matches, matchesField, when, whenAny, whenNot } from '../dist/runtime/composables/useProgressiveDisclosure.js';
7
7
  export { FieldGroupDescriptor, flattenFieldGroups } from '../dist/runtime/utils/flattenFieldGroups.js';
8
- export { AnyFieldDescriptor, BooleanDescriptor, ColorDescriptor, CustomDescriptor, DateDescriptor, EditorDescriptor, EnhancedMediaDescriptor, EnhancedMediaValue, FieldAccessor, FieldComponentEntry, FieldDescriptor, FieldRegistry, FieldType, FileDescriptor, FksDescriptor, KnownFieldType, MapDescriptor, MediaDescriptor, MediaItem, MediaLink, MediaM2mDescriptor, NumberDescriptor, PartialFieldRegistry, RepeaterDescriptor, RepeaterImageFit, RepeaterTemplate, SelectDescriptor, SeoDescriptor, TextDescriptor } from '../dist/runtime/types/index.js';
8
+ export { AnyFieldDescriptor, BooleanDescriptor, ColorDescriptor, CustomDescriptor, DateDescriptor, EditorDescriptor, EditorToolbarEditor, EditorToolbarItem, EnhancedMediaDescriptor, EnhancedMediaValue, FieldAccessor, FieldComponentEntry, FieldDescriptor, FieldRegistry, FieldType, FileDescriptor, FksDescriptor, KnownFieldType, MapDescriptor, MediaDescriptor, MediaItem, MediaLink, MediaM2mDescriptor, NumberDescriptor, PartialFieldRegistry, RepeaterDescriptor, RepeaterImageFit, RepeaterTemplate, SelectDescriptor, SeoDescriptor, TextDescriptor } from '../dist/runtime/types/index.js';
9
9
  import '@nuxt/schema';
package/dist/index.d.ts CHANGED
@@ -11,5 +11,5 @@ export { when, whenAny, whenNot, matchesField, isOneOf, isNoneOf, isNotEmpty, is
11
11
  export { flattenFieldGroups } from "./runtime/utils/flattenFieldGroups.js";
12
12
  export type { FieldGroupDescriptor } from "./runtime/utils/flattenFieldGroups.js";
13
13
  export { KnownFieldType } from "./runtime/types/index.js";
14
- export type { FieldType, FieldAccessor, FieldDescriptor, AnyFieldDescriptor, TextDescriptor, NumberDescriptor, BooleanDescriptor, SelectDescriptor, FksDescriptor, DateDescriptor, ColorDescriptor, FileDescriptor, EditorDescriptor, SeoDescriptor, MapDescriptor, RepeaterDescriptor, RepeaterTemplate, RepeaterImageFit, MediaDescriptor, MediaM2mDescriptor, EnhancedMediaDescriptor, CustomDescriptor, MediaLink, MediaItem, EnhancedMediaValue, FieldComponentEntry, FieldRegistry, PartialFieldRegistry, } from "./runtime/types/index.js";
14
+ export type { FieldType, FieldAccessor, FieldDescriptor, AnyFieldDescriptor, TextDescriptor, NumberDescriptor, BooleanDescriptor, SelectDescriptor, FksDescriptor, DateDescriptor, ColorDescriptor, FileDescriptor, EditorDescriptor, EditorToolbarItem, EditorToolbarEditor, SeoDescriptor, MapDescriptor, RepeaterDescriptor, RepeaterTemplate, RepeaterImageFit, MediaDescriptor, MediaM2mDescriptor, EnhancedMediaDescriptor, CustomDescriptor, MediaLink, MediaItem, EnhancedMediaValue, FieldComponentEntry, FieldRegistry, PartialFieldRegistry, } from "./runtime/types/index.js";
15
15
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,4CAA4C,CAAC;AAC/E,YAAY,EAAE,wBAAwB,EAAE,MAAM,4CAA4C,CAAC;AAC3F,OAAO,EAAE,YAAY,EAAE,MAAM,uCAAuC,CAAC;AACrE,YAAY,EAAE,mBAAmB,EAAE,MAAM,uCAAuC,CAAC;AACjF,OAAO,EAAE,YAAY,EAAE,MAAM,uCAAuC,CAAC;AACrE,YAAY,EAAE,WAAW,EAAE,MAAM,uCAAuC,CAAC;AACzE,OAAO,EAAE,sBAAsB,EAAE,MAAM,iDAAiD,CAAC;AACzF,YAAY,EAAE,0BAA0B,EAAE,MAAM,iDAAiD,CAAC;AAElG,OAAO,EACL,IAAI,EACJ,OAAO,EACP,OAAO,EACP,YAAY,EACZ,OAAO,EACP,QAAQ,EACR,UAAU,EACV,OAAO,EACP,WAAW,EACX,QAAQ,EACR,OAAO,GACR,MAAM,mDAAmD,CAAC;AAC3D,OAAO,EAAE,kBAAkB,EAAE,MAAM,uCAAuC,CAAC;AAC3E,YAAY,EAAE,oBAAoB,EAAE,MAAM,uCAAuC,CAAC;AAClF,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC1D,YAAY,EACV,SAAS,EACT,aAAa,EACb,eAAe,EACf,kBAAkB,EAClB,cAAc,EACd,gBAAgB,EAChB,iBAAiB,EACjB,gBAAgB,EAChB,aAAa,EACb,cAAc,EACd,eAAe,EACf,cAAc,EACd,gBAAgB,EAChB,aAAa,EACb,aAAa,EACb,kBAAkB,EAClB,gBAAgB,EAChB,gBAAgB,EAChB,eAAe,EACf,kBAAkB,EAClB,uBAAuB,EACvB,gBAAgB,EAChB,SAAS,EACT,SAAS,EACT,kBAAkB,EAClB,mBAAmB,EACnB,aAAa,EACb,oBAAoB,GACrB,MAAM,0BAA0B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,4CAA4C,CAAC;AAC/E,YAAY,EAAE,wBAAwB,EAAE,MAAM,4CAA4C,CAAC;AAC3F,OAAO,EAAE,YAAY,EAAE,MAAM,uCAAuC,CAAC;AACrE,YAAY,EAAE,mBAAmB,EAAE,MAAM,uCAAuC,CAAC;AACjF,OAAO,EAAE,YAAY,EAAE,MAAM,uCAAuC,CAAC;AACrE,YAAY,EAAE,WAAW,EAAE,MAAM,uCAAuC,CAAC;AACzE,OAAO,EAAE,sBAAsB,EAAE,MAAM,iDAAiD,CAAC;AACzF,YAAY,EAAE,0BAA0B,EAAE,MAAM,iDAAiD,CAAC;AAElG,OAAO,EACL,IAAI,EACJ,OAAO,EACP,OAAO,EACP,YAAY,EACZ,OAAO,EACP,QAAQ,EACR,UAAU,EACV,OAAO,EACP,WAAW,EACX,QAAQ,EACR,OAAO,GACR,MAAM,mDAAmD,CAAC;AAC3D,OAAO,EAAE,kBAAkB,EAAE,MAAM,uCAAuC,CAAC;AAC3E,YAAY,EAAE,oBAAoB,EAAE,MAAM,uCAAuC,CAAC;AAClF,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAC1D,YAAY,EACV,SAAS,EACT,aAAa,EACb,eAAe,EACf,kBAAkB,EAClB,cAAc,EACd,gBAAgB,EAChB,iBAAiB,EACjB,gBAAgB,EAChB,aAAa,EACb,cAAc,EACd,eAAe,EACf,cAAc,EACd,gBAAgB,EAChB,iBAAiB,EACjB,mBAAmB,EACnB,aAAa,EACb,aAAa,EACb,kBAAkB,EAClB,gBAAgB,EAChB,gBAAgB,EAChB,eAAe,EACf,kBAAkB,EAClB,uBAAuB,EACvB,gBAAgB,EAChB,SAAS,EACT,SAAS,EACT,kBAAkB,EAClB,mBAAmB,EACnB,aAAa,EACb,oBAAoB,GACrB,MAAM,0BAA0B,CAAC"}
package/dist/module.d.mts CHANGED
@@ -13,6 +13,19 @@ interface MapoFormOptions {
13
13
  expanded?: boolean;
14
14
  };
15
15
  debounce?: number;
16
+ /** Defaults for every `editor` field; a field's own `attrs` win. */
17
+ editor?: {
18
+ /**
19
+ * Shows the HTML source toggle in the toolbar of every editor field. A
20
+ * field sets `attrs.source` to differ from it. Default: `true`.
21
+ */
22
+ source?: boolean;
23
+ /**
24
+ * Shows the alignment buttons (left, centre, right) in every editor
25
+ * field. A field sets `attrs.textAlign` to differ from it. Default: `false`.
26
+ */
27
+ textAlign?: boolean;
28
+ };
16
29
  }
17
30
  declare const _default: NuxtModule<MapoFormOptions, MapoFormOptions, false>;
18
31
 
package/dist/module.d.ts CHANGED
@@ -6,6 +6,19 @@ export interface MapoFormOptions {
6
6
  expanded?: boolean;
7
7
  };
8
8
  debounce?: number;
9
+ /** Defaults for every `editor` field; a field's own `attrs` win. */
10
+ editor?: {
11
+ /**
12
+ * Shows the HTML source toggle in the toolbar of every editor field. A
13
+ * field sets `attrs.source` to differ from it. Default: `true`.
14
+ */
15
+ source?: boolean;
16
+ /**
17
+ * Shows the alignment buttons (left, centre, right) in every editor
18
+ * field. A field sets `attrs.textAlign` to differ from it. Default: `false`.
19
+ */
20
+ textAlign?: boolean;
21
+ };
9
22
  }
10
23
  declare const _default: NuxtModule<MapoFormOptions, MapoFormOptions, false>;
11
24
  export default _default;
@@ -1 +1 @@
1
- {"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../src/module.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAErE,MAAM,WAAW,eAAe;IAC9B,MAAM,CAAC,EAAE,oBAAoB,CAAC;IAC9B,MAAM,CAAC,EAAE;QAAE,QAAQ,CAAC,EAAE,OAAO,CAAA;KAAE,CAAC;IAChC,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;;AAED,wBAkOyC"}
1
+ {"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../src/module.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AAErE,MAAM,WAAW,eAAe;IAC9B,MAAM,CAAC,EAAE,oBAAoB,CAAC;IAC9B,MAAM,CAAC,EAAE;QAAE,QAAQ,CAAC,EAAE,OAAO,CAAA;KAAE,CAAC;IAChC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,oEAAoE;IACpE,MAAM,CAAC,EAAE;QACP;;;WAGG;QACH,MAAM,CAAC,EAAE,OAAO,CAAC;QACjB;;;WAGG;QACH,SAAS,CAAC,EAAE,OAAO,CAAC;KACrB,CAAC;CACH;;AAED,wBAuOyC"}
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mapomodule/form",
3
3
  "configKey": "mapoForm",
4
- "version": "2.0.0-beta.15",
4
+ "version": "2.0.0-beta.17",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.2",
7
7
  "unbuild": "unknown"
package/dist/module.mjs CHANGED
@@ -7,7 +7,8 @@ const module$1 = defineNuxtModule({
7
7
  },
8
8
  defaults: {
9
9
  debounce: 300,
10
- groups: { expanded: true }
10
+ groups: { expanded: true },
11
+ editor: { source: true, textAlign: false }
11
12
  },
12
13
  async setup(options, nuxt) {
13
14
  const resolver = createResolver(import.meta.url);
@@ -22,6 +23,10 @@ const module$1 = defineNuxtModule({
22
23
  nuxt.options.runtimeConfig.public.mapoForm = {
23
24
  groups: { expanded: options.groups?.expanded ?? true },
24
25
  debounce: options.debounce ?? 300,
26
+ editor: {
27
+ source: options.editor?.source ?? true,
28
+ textAlign: options.editor?.textAlign ?? false
29
+ },
25
30
  // attrs is JSON-serialisable (plain objects) — merged in the plugin
26
31
  fields: { attrs: options.fields?.attrs ?? {} }
27
32
  };
@@ -1,11 +1,27 @@
1
1
  <script setup>
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 StarterKit from "@tiptap/starter-kit";
6
14
  import Link from "@tiptap/extension-link";
7
15
  import Underline from "@tiptap/extension-underline";
8
16
  import Image from "@tiptap/extension-image";
17
+ import TextAlign from "@tiptap/extension-text-align";
18
+ import { debounce } from "@mapomodule/utils";
19
+ import { formatHtml } from "../../utils/formatHtml.js";
20
+ import {
21
+ ALLOWED_LINK_PROTOCOLS,
22
+ isSafeUrl,
23
+ sanitizeHtml
24
+ } from "../../utils/sanitizeHtml.js";
9
25
  const props = defineProps({
10
26
  modelValue: { type: null, required: true },
11
27
  descriptor: { type: Object, required: true },
@@ -15,44 +31,18 @@ const props = defineProps({
15
31
  });
16
32
  const emit = defineEmits(["update:modelValue"]);
17
33
  const { t } = useI18n();
34
+ const appEditorDefaults = useRuntimeConfig().public.mapoForm?.editor;
18
35
  const extraExtensions = props.descriptor.attrs?.extensions ?? [];
19
- const ALLOWED_LINK_PROTOCOLS = ["http", "https", "mailto", "tel"];
20
- function isSafeUrl(url) {
21
- try {
22
- if (!/^[a-z][a-z0-9+.-]*:/i.test(url)) return true;
23
- const proto = new URL(url, "https://placeholder.local").protocol.replace(":", "").toLowerCase();
24
- return ALLOWED_LINK_PROTOCOLS.includes(proto);
25
- } catch {
26
- return false;
27
- }
28
- }
29
- function sanitizeHtml(html) {
30
- if (typeof window === "undefined" || !html) return html;
31
- const tpl = document.createElement("template");
32
- tpl.innerHTML = html;
33
- const walker = document.createTreeWalker(
34
- tpl.content,
35
- NodeFilter.SHOW_ELEMENT
36
- );
37
- const toRemove = [];
38
- let node = walker.nextNode();
39
- while (node) {
40
- if (node.tagName === "SCRIPT" || node.tagName === "STYLE") {
41
- toRemove.push(node);
42
- } else {
43
- for (const attr of Array.from(node.attributes)) {
44
- const name = attr.name.toLowerCase();
45
- if (name.startsWith("on")) node.removeAttribute(attr.name);
46
- if ((name === "href" || name === "src" || name === "xlink:href") && !isSafeUrl(attr.value)) {
47
- node.removeAttribute(attr.name);
48
- }
49
- }
50
- }
51
- node = walker.nextNode();
52
- }
53
- toRemove.forEach((el) => el.remove());
54
- return tpl.innerHTML;
55
- }
36
+ const hasTextAlign = props.descriptor.attrs?.textAlign ?? appEditorDefaults?.textAlign ?? false;
37
+ const loadsOwnTextAlign = extraExtensions.some(
38
+ (extension) => extension.name === "textAlign"
39
+ );
40
+ const alignmentExtensions = hasTextAlign && !loadsOwnTextAlign ? [
41
+ TextAlign.configure({
42
+ types: ["heading", "paragraph"],
43
+ alignments: ["left", "center", "right"]
44
+ })
45
+ ] : [];
56
46
  const editor = useEditor({
57
47
  content: sanitizeHtml(props.modelValue ?? ""),
58
48
  editable: !props.readonly && !props.disabled,
@@ -65,6 +55,7 @@ const editor = useEditor({
65
55
  }),
66
56
  Underline,
67
57
  Image.configure({ inline: false, allowBase64: false }),
58
+ ...alignmentExtensions,
68
59
  ...extraExtensions
69
60
  ],
70
61
  onUpdate: ({ editor: e }) => {
@@ -75,6 +66,44 @@ watch(
75
66
  () => [props.readonly, props.disabled],
76
67
  ([ro, dis]) => editor.value?.setEditable(!ro && !dis)
77
68
  );
69
+ const SOURCE_SYNC_DELAY_MS = 250;
70
+ const hasSourceView = props.descriptor.attrs?.source ?? appEditorDefaults?.source ?? true;
71
+ const sourceMode = ref(false);
72
+ const sourceHtml = ref("");
73
+ const sourceHintId = useId();
74
+ let sourcePending = false;
75
+ const isLocked = computed(() => props.readonly || props.disabled);
76
+ const isToolbarDisabled = computed(() => isLocked.value || sourceMode.value);
77
+ function flushSource() {
78
+ syncSource.cancel();
79
+ const current = editor.value;
80
+ if (!current) return;
81
+ if (!sourcePending) return;
82
+ sourcePending = false;
83
+ const before = current.getHTML();
84
+ current.commands.setContent(sanitizeHtml(sourceHtml.value), false);
85
+ const after = current.getHTML();
86
+ const documentChanged = after !== before;
87
+ if (documentChanged) emit("update:modelValue", after);
88
+ }
89
+ const syncSource = debounce(flushSource, SOURCE_SYNC_DELAY_MS);
90
+ function onSourceInput(value) {
91
+ if (isLocked.value) return;
92
+ sourceHtml.value = String(value ?? "");
93
+ sourcePending = true;
94
+ syncSource();
95
+ }
96
+ function toggleSource() {
97
+ const current = editor.value;
98
+ if (!current) return;
99
+ if (sourceMode.value) {
100
+ flushSource();
101
+ sourceMode.value = false;
102
+ return;
103
+ }
104
+ sourceHtml.value = formatHtml(current.getHTML());
105
+ sourceMode.value = true;
106
+ }
78
107
  watch(
79
108
  () => props.modelValue,
80
109
  (val) => {
@@ -83,10 +112,127 @@ watch(
83
112
  sanitizeHtml(val ?? ""),
84
113
  false
85
114
  );
115
+ if (!sourceMode.value) return;
116
+ syncSource.cancel();
117
+ sourcePending = false;
118
+ sourceHtml.value = formatHtml(editor.value.getHTML());
86
119
  }
87
120
  }
88
121
  );
89
- onBeforeUnmount(() => editor.value?.destroy());
122
+ onBeforeUnmount(() => {
123
+ flushSource();
124
+ editor.value?.destroy();
125
+ });
126
+ const toolbarItems = props.descriptor.attrs?.toolbar ?? [];
127
+ function askItem(question, fallback) {
128
+ const current = editor.value;
129
+ if (!current) return fallback;
130
+ try {
131
+ return question(current) ?? false;
132
+ } catch {
133
+ return fallback;
134
+ }
135
+ }
136
+ function isItemDisabled(item) {
137
+ if (isToolbarDisabled.value) return true;
138
+ return askItem((current) => item.isDisabled?.(current), true);
139
+ }
140
+ function isItemActive(item) {
141
+ return askItem((current) => item.isActive?.(current), false);
142
+ }
143
+ function runItem(item) {
144
+ const current = editor.value;
145
+ if (!current) return;
146
+ item.action(current);
147
+ }
148
+ const linkOpen = ref(false);
149
+ const linkUrl = ref("");
150
+ const linkInvalid = ref(false);
151
+ const linkErrorId = useId();
152
+ function closeLink() {
153
+ linkOpen.value = false;
154
+ linkInvalid.value = false;
155
+ }
156
+ function toggleLink() {
157
+ const current = editor.value;
158
+ if (!current) return;
159
+ if (linkOpen.value) {
160
+ closeLink();
161
+ return;
162
+ }
163
+ linkUrl.value = current.getAttributes("link").href ?? "";
164
+ linkInvalid.value = false;
165
+ linkOpen.value = true;
166
+ }
167
+ function removeLink() {
168
+ editor.value?.chain().focus().extendMarkRange("link").unsetLink().run();
169
+ closeLink();
170
+ }
171
+ function applyLink() {
172
+ const current = editor.value;
173
+ if (!current) return;
174
+ const typed = linkUrl.value.trim();
175
+ if (!typed) {
176
+ removeLink();
177
+ return;
178
+ }
179
+ const href = /^www\./i.test(typed) ? `https://${typed}` : typed;
180
+ if (!isSafeUrl(href)) {
181
+ linkInvalid.value = true;
182
+ return;
183
+ }
184
+ const chain = current.chain().focus();
185
+ if (current.state.selection.empty && !current.isActive("link")) {
186
+ chain.insertContent({
187
+ type: "text",
188
+ text: href,
189
+ marks: [{ type: "link", attrs: { href } }]
190
+ }).run();
191
+ } else {
192
+ chain.extendMarkRange("link").setLink({ href }).run();
193
+ }
194
+ closeLink();
195
+ }
196
+ function onLinkKeydown(event) {
197
+ if (event.key === "Enter") {
198
+ event.preventDefault();
199
+ applyLink();
200
+ } else if (event.key === "Escape") {
201
+ event.preventDefault();
202
+ closeLink();
203
+ }
204
+ }
205
+ watch(isToolbarDisabled, (disabled) => {
206
+ if (disabled) closeLink();
207
+ });
208
+ const alignments = [
209
+ {
210
+ value: "left",
211
+ icon: "i-lucide-align-left",
212
+ label: "mapo.wygEditor.alignLeft"
213
+ },
214
+ {
215
+ value: "center",
216
+ icon: "i-lucide-align-center",
217
+ label: "mapo.wygEditor.alignCenter"
218
+ },
219
+ {
220
+ value: "right",
221
+ icon: "i-lucide-align-right",
222
+ label: "mapo.wygEditor.alignRight"
223
+ }
224
+ ];
225
+ function isAligned(value) {
226
+ const current = editor.value;
227
+ if (!current) return false;
228
+ if (value !== "left") return current.isActive({ textAlign: value });
229
+ return !current.isActive({ textAlign: "center" }) && !current.isActive({ textAlign: "right" });
230
+ }
231
+ function align(value) {
232
+ const chain = editor.value?.chain().focus();
233
+ if (!chain) return;
234
+ (value === "left" ? chain.unsetTextAlign() : chain.setTextAlign(value)).run();
235
+ }
90
236
  const imagePickerOpen = ref(false);
91
237
  const MediaManagerDialog = resolveComponent("MapoMediaManagerDialog");
92
238
  const hasMediaManager = typeof MediaManagerDialog !== "string";
@@ -117,7 +263,7 @@ function onImageConfirm(selection) {
117
263
  size="xs"
118
264
  variant="ghost"
119
265
  color="neutral"
120
- :disabled="readonly || disabled"
266
+ :disabled="isToolbarDisabled"
121
267
  :class="{ 'bg-gray-200': editor.isActive('bold') }"
122
268
  @click="editor.chain().focus().toggleBold().run()"
123
269
  >
@@ -127,7 +273,7 @@ function onImageConfirm(selection) {
127
273
  size="xs"
128
274
  variant="ghost"
129
275
  color="neutral"
130
- :disabled="readonly || disabled"
276
+ :disabled="isToolbarDisabled"
131
277
  :class="{ 'bg-gray-200': editor.isActive('italic') }"
132
278
  @click="editor.chain().focus().toggleItalic().run()"
133
279
  >
@@ -137,7 +283,7 @@ function onImageConfirm(selection) {
137
283
  size="xs"
138
284
  variant="ghost"
139
285
  color="neutral"
140
- :disabled="readonly || disabled"
286
+ :disabled="isToolbarDisabled"
141
287
  :class="{ 'bg-gray-200': editor.isActive('underline') }"
142
288
  @click="editor.chain().focus().toggleUnderline().run()"
143
289
  >
@@ -150,7 +296,7 @@ function onImageConfirm(selection) {
150
296
  size="xs"
151
297
  variant="ghost"
152
298
  color="neutral"
153
- :disabled="readonly || disabled"
299
+ :disabled="isToolbarDisabled"
154
300
  :class="{ 'bg-gray-200': editor.isActive('heading', { level: 2 }) }"
155
301
  @click="editor.chain().focus().toggleHeading({ level: 2 }).run()"
156
302
  >
@@ -160,7 +306,7 @@ function onImageConfirm(selection) {
160
306
  size="xs"
161
307
  variant="ghost"
162
308
  color="neutral"
163
- :disabled="readonly || disabled"
309
+ :disabled="isToolbarDisabled"
164
310
  :class="{ 'bg-gray-200': editor.isActive('heading', { level: 3 }) }"
165
311
  @click="editor.chain().focus().toggleHeading({ level: 3 }).run()"
166
312
  >
@@ -173,7 +319,7 @@ function onImageConfirm(selection) {
173
319
  size="xs"
174
320
  variant="ghost"
175
321
  color="neutral"
176
- :disabled="readonly || disabled"
322
+ :disabled="isToolbarDisabled"
177
323
  :class="{ 'bg-gray-200': editor.isActive('bulletList') }"
178
324
  icon="i-lucide-list"
179
325
  @click="editor.chain().focus().toggleBulletList().run()"
@@ -182,19 +328,42 @@ function onImageConfirm(selection) {
182
328
  size="xs"
183
329
  variant="ghost"
184
330
  color="neutral"
185
- :disabled="readonly || disabled"
331
+ :disabled="isToolbarDisabled"
186
332
  :class="{ 'bg-gray-200': editor.isActive('orderedList') }"
187
333
  icon="i-lucide-list-ordered"
188
334
  @click="editor.chain().focus().toggleOrderedList().run()"
189
335
  />
190
336
 
337
+ <!-- Alignment (`attrs.textAlign`) -->
338
+ <template v-if="hasTextAlign">
339
+ <USeparator orientation="vertical" class="mx-1 h-5" />
340
+ <UTooltip
341
+ v-for="alignment in alignments"
342
+ :key="alignment.value"
343
+ :text="t(alignment.label)"
344
+ :delay-duration="300"
345
+ >
346
+ <UButton
347
+ size="xs"
348
+ variant="ghost"
349
+ color="neutral"
350
+ :icon="alignment.icon"
351
+ :aria-label="t(alignment.label)"
352
+ :aria-pressed="isAligned(alignment.value)"
353
+ :disabled="isToolbarDisabled"
354
+ :class="{ 'bg-gray-200': isAligned(alignment.value) }"
355
+ @click="align(alignment.value)"
356
+ />
357
+ </UTooltip>
358
+ </template>
359
+
191
360
  <USeparator orientation="vertical" class="mx-1 h-5" />
192
361
 
193
362
  <UButton
194
363
  size="xs"
195
364
  variant="ghost"
196
365
  color="neutral"
197
- :disabled="readonly || disabled"
366
+ :disabled="isToolbarDisabled"
198
367
  icon="i-lucide-undo"
199
368
  @click="editor.chain().focus().undo().run()"
200
369
  />
@@ -202,13 +371,28 @@ function onImageConfirm(selection) {
202
371
  size="xs"
203
372
  variant="ghost"
204
373
  color="neutral"
205
- :disabled="readonly || disabled"
374
+ :disabled="isToolbarDisabled"
206
375
  icon="i-lucide-redo"
207
376
  @click="editor.chain().focus().redo().run()"
208
377
  />
209
378
 
210
379
  <USeparator orientation="vertical" class="mx-1 h-5" />
211
380
 
381
+ <!-- Link: opens the link bar under the toolbar -->
382
+ <UTooltip :text="t('mapo.wygEditor.link')" :delay-duration="300">
383
+ <UButton
384
+ size="xs"
385
+ variant="ghost"
386
+ color="neutral"
387
+ icon="i-lucide-link"
388
+ :aria-label="t('mapo.wygEditor.link')"
389
+ :aria-expanded="linkOpen"
390
+ :disabled="isToolbarDisabled"
391
+ :class="{ 'bg-gray-200': linkOpen || editor.isActive('link') }"
392
+ @click="toggleLink"
393
+ />
394
+ </UTooltip>
395
+
212
396
  <!-- Insert Image via Media Manager (available when @mapomodule/uikit is installed) -->
213
397
  <UTooltip
214
398
  :text="
@@ -221,18 +405,130 @@ function onImageConfirm(selection) {
221
405
  variant="ghost"
222
406
  color="neutral"
223
407
  icon="i-lucide-image"
224
- :disabled="readonly || disabled || !hasMediaManager"
408
+ :disabled="isToolbarDisabled || !hasMediaManager"
225
409
  @click="imagePickerOpen = true"
226
410
  />
227
411
  </UTooltip>
412
+
413
+ <!-- Custom buttons (`attrs.toolbar`), after the built-in ones -->
414
+ <template v-if="toolbarItems.length">
415
+ <USeparator orientation="vertical" class="mx-1 h-5" />
416
+ <UTooltip
417
+ v-for="item in toolbarItems"
418
+ :key="item.key"
419
+ :text="item.label"
420
+ :delay-duration="300"
421
+ >
422
+ <UButton
423
+ size="xs"
424
+ variant="ghost"
425
+ color="neutral"
426
+ :icon="item.icon"
427
+ :label="item.icon ? void 0 : item.label"
428
+ :aria-label="item.label"
429
+ :disabled="isItemDisabled(item)"
430
+ :class="{ 'bg-gray-200': isItemActive(item) }"
431
+ @click="runItem(item)"
432
+ />
433
+ </UTooltip>
434
+ </template>
435
+
436
+ <!-- HTML source toggle (`attrs.source`): stays active in the source
437
+ view, and on a read-only field, which can show its source. -->
438
+ <template v-if="hasSourceView">
439
+ <USeparator orientation="vertical" class="mx-1 h-5" />
440
+ <UTooltip :text="t('mapo.wygEditor.source')" :delay-duration="300">
441
+ <UButton
442
+ size="xs"
443
+ variant="ghost"
444
+ color="neutral"
445
+ icon="i-lucide-code-xml"
446
+ :aria-label="t('mapo.wygEditor.source')"
447
+ :aria-pressed="sourceMode"
448
+ :class="{ 'bg-gray-200': sourceMode }"
449
+ @click="toggleSource"
450
+ />
451
+ </UTooltip>
452
+ </template>
453
+ </div>
454
+
455
+ <!-- Link bar: the address of the link at the selection -->
456
+ <div
457
+ v-if="linkOpen && editor"
458
+ class="flex flex-wrap items-center gap-2 border-b border-gray-200 px-2 py-1.5"
459
+ >
460
+ <UInput
461
+ v-model="linkUrl"
462
+ size="xs"
463
+ autofocus
464
+ class="min-w-56 flex-1"
465
+ :placeholder="t('mapo.wygEditor.linkPlaceholder')"
466
+ :aria-label="t('mapo.wygEditor.linkUrl')"
467
+ :aria-invalid="linkInvalid"
468
+ :aria-describedby="linkInvalid ? linkErrorId : void 0"
469
+ @keydown="onLinkKeydown"
470
+ />
471
+ <UButton
472
+ size="xs"
473
+ :label="t('mapo.wygEditor.linkApply')"
474
+ @click="applyLink"
475
+ />
476
+ <UButton
477
+ v-if="editor.isActive('link')"
478
+ size="xs"
479
+ variant="ghost"
480
+ color="neutral"
481
+ :label="t('mapo.wygEditor.linkRemove')"
482
+ @click="removeLink"
483
+ />
484
+ <UButton
485
+ size="xs"
486
+ variant="ghost"
487
+ color="neutral"
488
+ icon="i-lucide-x"
489
+ :aria-label="t('mapo.wygEditor.linkCancel')"
490
+ @click="closeLink"
491
+ />
492
+ <p
493
+ v-if="linkInvalid"
494
+ :id="linkErrorId"
495
+ class="w-full text-xs text-red-500"
496
+ role="alert"
497
+ >
498
+ {{ t("mapo.wygEditor.linkInvalid") }}
499
+ </p>
228
500
  </div>
229
501
 
230
- <!-- Editor content -->
502
+ <!-- Editor content: hidden, not unmounted, in the source view. Its
503
+ elements are styled below: Tailwind's preflight leaves headings,
504
+ lists and links looking like plain text. -->
231
505
  <EditorContent
506
+ v-show="!sourceMode"
232
507
  :editor="editor"
233
- class="prose prose-sm max-w-none px-3 py-2 focus-within:outline-none"
508
+ class="mapo-wyg-content px-3 py-2 focus-within:outline-none"
234
509
  />
235
510
 
511
+ <div v-if="sourceMode" class="px-3 py-2">
512
+ <UTextarea
513
+ :model-value="sourceHtml"
514
+ autoresize
515
+ :rows="6"
516
+ :readonly="isLocked"
517
+ spellcheck="false"
518
+ autocapitalize="off"
519
+ autocomplete="off"
520
+ :aria-label="t('mapo.wygEditor.source')"
521
+ :aria-describedby="sourceHintId"
522
+ :ui="{ base: 'font-mono text-xs' }"
523
+ class="w-full"
524
+ @update:model-value="onSourceInput"
525
+ @blur="flushSource"
526
+ />
527
+ <p :id="sourceHintId" class="mt-1 text-xs text-muted">
528
+ {{ t("mapo.wygEditor.sourceHint") }}
529
+ </p>
530
+ </div>
531
+
236
532
  <p
237
533
  v-for="err in errors"
238
534
  :key="err"
@@ -256,3 +552,7 @@ function onImageConfirm(selection) {
256
552
  />
257
553
  </div>
258
554
  </template>
555
+
556
+ <style scoped>
557
+ .mapo-wyg-content{color:var(--ui-text);font-size:.875rem;line-height:1.6}.mapo-wyg-content :deep(.ProseMirror>*+*){margin-top:.75em}.mapo-wyg-content :deep(h2),.mapo-wyg-content :deep(h3){color:var(--ui-text-highlighted);font-weight:600}.mapo-wyg-content :deep(h2){font-size:1.375em;line-height:1.3}.mapo-wyg-content :deep(h3){font-size:1.125em;line-height:1.4}.mapo-wyg-content :deep(.ProseMirror>:is(h2,h3):not(:first-child)){margin-top:1.25em}.mapo-wyg-content :deep(ul){list-style:disc;padding-left:1.5em}.mapo-wyg-content :deep(ol){list-style:decimal;padding-left:1.5em}.mapo-wyg-content :deep(li+li),.mapo-wyg-content :deep(li>:is(ul,ol)){margin-top:.25em}.mapo-wyg-content :deep(a){color:var(--ui-primary);text-decoration:underline;text-underline-offset:2px}.mapo-wyg-content :deep(blockquote){border-left:3px solid var(--ui-border-accented);color:var(--ui-text-muted);padding-left:1em}.mapo-wyg-content :deep(code){background:var(--ui-bg-elevated);border-radius:.25rem;font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:.875em;padding:.1em .3em}.mapo-wyg-content :deep(pre){background:var(--ui-bg-elevated);border-radius:.375rem;overflow-x:auto;padding:.75em 1em}.mapo-wyg-content :deep(pre code){background:none;padding:0}.mapo-wyg-content :deep(hr){border:0;border-top:1px solid var(--ui-border)}.mapo-wyg-content :deep(img){height:auto;max-width:100%}.mapo-wyg-content :deep(table){border-collapse:collapse;table-layout:fixed;width:100%}.mapo-wyg-content :deep(:is(th,td)){border:1px solid var(--ui-border-accented);padding:.375em .5em;text-align:left;vertical-align:top}.mapo-wyg-content :deep(th){background:var(--ui-bg-elevated);font-weight:600}.mapo-wyg-content :deep(:is(th,td)>p){margin:0}.mapo-wyg-content :deep(.selectedCell){background:color-mix(in oklab,var(--ui-primary) 12%,transparent)}
558
+ </style>
@@ -1 +1 @@
1
- {"version":3,"file":"MapoWygEditor.vue.d.ts","sourceRoot":"","sources":["../../../../src/runtime/components/fields/MapoWygEditor.vue"],"names":[],"mappings":"AA2TA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAE7D,KAAK,WAAW,GAAG;IACjB,UAAU,EAAE,OAAO,CAAC;IACpB,UAAU,EAAE,gBAAgB,CAAC;IAC7B,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAClB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB,CAAC;AA6pBF,QAAA,MAAM,YAAY;;;;kFAGhB,CAAC;wBACkB,OAAO,YAAY;AAAxC,wBAAyC"}
1
+ {"version":3,"file":"MapoWygEditor.vue.d.ts","sourceRoot":"","sources":["../../../../src/runtime/components/fields/MapoWygEditor.vue"],"names":[],"mappings":"AAoxBA,OAAO,KAAK,EACV,gBAAgB,EAIjB,MAAM,sBAAsB,CAAC;AAQ9B,KAAK,WAAW,GAAG;IACjB,UAAU,EAAE,OAAO,CAAC;IACpB,UAAU,EAAE,gBAAgB,CAAC;IAC7B,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;IAClB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB,CAAC;AAg2CF,QAAA,MAAM,YAAY;;;;kFAGhB,CAAC;wBACkB,OAAO,YAAY;AAAxC,wBAAyC"}