magic-editor-x 2.3.1 → 2.5.0
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/CHANGELOG.md +125 -0
- package/README.md +428 -148
- package/bin/magic-editor-x.mjs +55 -8
- package/dist/admin/AuditPage-Be78C7EC.mjs +382 -0
- package/dist/admin/AuditPage-iQzpjH8H.js +382 -0
- package/dist/admin/{ConfigurationPage-CYGDLjM7.mjs → ConfigurationPage-C9CPBfWy.mjs} +405 -10
- package/dist/admin/{ConfigurationPage-CFP3lp9p.js → ConfigurationPage-DefuBeYO.js} +415 -20
- package/dist/admin/ConfirmAction-CUhpngYU.js +71 -0
- package/dist/admin/ConfirmAction-CZjpduc3.mjs +72 -0
- package/dist/admin/Content-BttG50Mc.mjs +360 -0
- package/dist/admin/Content-V5MRBsI3.js +362 -0
- package/dist/admin/{CustomBlocksPage-B5Q33N-s.mjs → CustomBlocksPage-DB5nnCsn.mjs} +8 -5
- package/dist/admin/{CustomBlocksPage-DzPOSpfn.js → CustomBlocksPage-Dg2rM3F4.js} +26 -23
- package/dist/admin/EntryWidgets-B8YA0Zxy.mjs +199 -0
- package/dist/admin/EntryWidgets-BcXAAx3F.js +201 -0
- package/dist/admin/MentionsWidget-CJPZDk9Z.mjs +225 -0
- package/dist/admin/MentionsWidget-DUz6YVIG.js +227 -0
- package/dist/admin/{useT-Cmg1jwwG.mjs → SectionCard-8ZNHkvn9.mjs} +2 -12
- package/dist/admin/{useT-DNphiBQR.js → SectionCard-Bs-xiiH1.js} +0 -10
- package/dist/admin/{TemplatesPage-4A8JNO_Z.mjs → TemplatesPage-VsZ_Mc0R.mjs} +307 -10
- package/dist/admin/{TemplatesPage-BVkgafCK.js → TemplatesPage-dx4_yd_t.js} +321 -24
- package/dist/admin/{ConfirmAction-zLuIFj8t.js → api-4AYzwS4-.js} +28 -71
- package/dist/admin/{ConfirmAction-DuQwvRYi.mjs → api-CcTYyckq.mjs} +28 -71
- package/dist/admin/auditText-DIIYVR6k.mjs +110 -0
- package/dist/admin/auditText-DUhWu2Nx.js +109 -0
- package/dist/admin/comments-C7P41lan.js +86 -0
- package/dist/admin/comments-hfIbLQNO.mjs +87 -0
- package/dist/admin/{de-B1X46JJZ.js → de-B6rdcOqQ.js} +293 -5
- package/dist/admin/{de-1cFMs-4c.mjs → de-BWfR2jTg.mjs} +8 -1
- package/dist/admin/{de-BUS1hUw0.mjs → de-Bip0sBRT.mjs} +158 -1
- package/dist/admin/{de-Dn8athM8.js → de-BrPR4sUB.js} +8 -1
- package/dist/admin/{de-DOiGaTNX.mjs → de-CQA8hOpl.mjs} +293 -5
- package/dist/admin/{de-C3AsGauQ.js → de-CnnxKXj7.js} +158 -1
- package/dist/admin/{de-2Ie5jy7j.js → de-Dss5POqk.js} +53 -3
- package/dist/admin/{de-BMY2LBaa.mjs → de-zgHCYTLH.mjs} +53 -3
- package/dist/admin/{en-CIZ5Ggod.js → en--JjAHbpP.js} +8 -1
- package/dist/admin/{en-CU6UzV4g.mjs → en-BCKaVgBM.mjs} +293 -5
- package/dist/admin/{en-Ah-Cw0Wd.js → en-CJ17Sp_e.js} +53 -3
- package/dist/admin/{en-CDg_18Bt.js → en-CbGkcq0Q.js} +293 -5
- package/dist/admin/{en-GCzgS9Aw.mjs → en-Cd6Jk-J0.mjs} +158 -1
- package/dist/admin/{en-BkEjpC3i.js → en-D69Fe9ZO.js} +158 -1
- package/dist/admin/{en-Dc9y-6n9.mjs → en-Dr58wBAo.mjs} +8 -1
- package/dist/admin/{en-CvFiqti2.mjs → en-Uhm7OouM.mjs} +53 -3
- package/dist/admin/entry-panel-events-0msyPrAD.js +18 -0
- package/dist/admin/entry-panel-events-XRuUoLZ9.mjs +19 -0
- package/dist/admin/{es-D2Kw6E4N.mjs → es-B2Hkkbw9.mjs} +8 -1
- package/dist/admin/{es-DbKHZz5l.mjs → es-BMkmfH88.mjs} +53 -3
- package/dist/admin/{es-DHBBZKl2.mjs → es-Bz8d72Fa.mjs} +158 -1
- package/dist/admin/{es-BCpJmUEM.js → es-C8UotT8O.js} +293 -5
- package/dist/admin/{es-C4pEnDRv.js → es-CcEDZpVQ.js} +158 -1
- package/dist/admin/{es-Cr8JH85N.js → es-DJm3HFJt.js} +53 -3
- package/dist/admin/{es-C4IRHV5d.js → es-YvaTAtqY.js} +8 -1
- package/dist/admin/{es-BmfffNM2.mjs → es-pvPfNAld.mjs} +293 -5
- package/dist/admin/{fr-BOvvfEH7.mjs → fr-BvPjRiwr.mjs} +8 -1
- package/dist/admin/{fr-Dvq4xk2e.mjs → fr-CKHZ0xxU.mjs} +158 -1
- package/dist/admin/{fr-OJ4VW1kh.js → fr-CRM9VuAq.js} +53 -3
- package/dist/admin/{fr-DQJ_7aq5.mjs → fr-CrIHmKYX.mjs} +53 -3
- package/dist/admin/{fr-C2aLSwEu.js → fr-Cx29jQ_g.js} +158 -1
- package/dist/admin/{fr-DZd2XJP6.mjs → fr-D4tL6Ag2.mjs} +293 -5
- package/dist/admin/{fr-Zkb8vfjN.js → fr-Di-U-rAH.js} +8 -1
- package/dist/admin/{fr-ZcN0yM_2.js → fr-DrUYv2jh.js} +293 -5
- package/dist/admin/{index-k8_a5o1A.mjs → index-C4ksUkEm.mjs} +20059 -10130
- package/dist/admin/{index-DQVE0ymw.js → index-C5D3VqLq.js} +21272 -11344
- package/dist/admin/index-CMYCZWOh.mjs +3752 -0
- package/dist/admin/index-DRyctF9s.js +3753 -0
- package/dist/admin/index.js +2 -1
- package/dist/admin/index.mjs +3 -2
- package/dist/admin/math-D6jmskGS.js +49 -0
- package/dist/admin/math-DntdSWKX.mjs +47 -0
- package/dist/admin/{pt-CilDggGF.mjs → pt-B8x7v-Xw.mjs} +158 -1
- package/dist/admin/{pt-DX0OAIoL.js → pt-BYn2xx78.js} +158 -1
- package/dist/admin/{pt-CImqDnLB.js → pt-CojQcz-p.js} +293 -5
- package/dist/admin/{pt-C3UdeIpd.mjs → pt-CuoIihKx.mjs} +293 -5
- package/dist/admin/{pt-1IUyx6ae.js → pt-CzrN_3ui.js} +8 -1
- package/dist/admin/{pt-BSLzA17U.mjs → pt-DDlPJYtB.mjs} +53 -3
- package/dist/admin/{pt-D0I0_JYs.js → pt-DPVAycXJ.js} +53 -3
- package/dist/admin/{pt-etwsg3G4.mjs → pt-DdGagO99.mjs} +8 -1
- package/dist/admin/useT-Bpw__IXT.js +12 -0
- package/dist/admin/useT-Cb-R3kyA.mjs +13 -0
- package/dist/blocks/index.d.mts +126 -0
- package/dist/blocks/index.d.ts +126 -0
- package/dist/blocks/index.js +184 -4
- package/dist/blocks/index.mjs +184 -4
- package/dist/math/index.d.mts +27 -0
- package/dist/math/index.d.ts +27 -0
- package/dist/math/index.js +168 -0
- package/dist/math/index.mjs +138 -0
- package/dist/react/index.js +1650 -759
- package/dist/react/index.mjs +1650 -759
- package/dist/render/index.d.mts +48 -0
- package/dist/render/index.d.ts +48 -0
- package/dist/render/index.js +1755 -773
- package/dist/render/index.mjs +1755 -773
- package/dist/server/index.js +29475 -18462
- package/dist/server/index.mjs +29475 -18462
- package/dist/server/mcp-worker.js +11319 -0
- package/dist/vue/index.js +1650 -759
- package/dist/vue/index.mjs +1650 -759
- package/package.json +14 -8
- package/schema/magic-editor-x.schema.json +135 -2
- package/types/blocks.d.ts +126 -0
- package/types/index.d.ts +52 -1
- package/types/math.d.ts +27 -0
- package/types/render.d.ts +48 -0
- package/dist/admin/index-DR8sHkTk.js +0 -1940
- package/dist/admin/index-My6FjjHB.mjs +0 -1939
|
@@ -0,0 +1,3752 @@
|
|
|
1
|
+
import { Feather, Bell, Discuss, Link } from "@strapi/icons";
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
import { lazy, Suspense, createElement } from "react";
|
|
4
|
+
import { useIntl } from "react-intl";
|
|
5
|
+
import { Typography } from "@strapi/design-system";
|
|
6
|
+
import { unstable_useContentManagerContext, useQueryParams, useRBAC } from "@strapi/strapi/admin";
|
|
7
|
+
import createDOMPurify from "dompurify";
|
|
8
|
+
import { createRoot } from "react-dom/client";
|
|
9
|
+
const action = (uid) => [{ action: `plugin::magic-editor-x.${uid}`, subject: null }];
|
|
10
|
+
const PERMISSIONS = {
|
|
11
|
+
access: action("access"),
|
|
12
|
+
customBlocksManage: action("custom-blocks.manage"),
|
|
13
|
+
settingsRead: action("settings.read"),
|
|
14
|
+
settingsUpdate: action("settings.update"),
|
|
15
|
+
templatesManage: action("templates.manage"),
|
|
16
|
+
auditRead: action("audit.read")
|
|
17
|
+
};
|
|
18
|
+
const PLUGIN_ID = "magic-editor-x";
|
|
19
|
+
const getTranslation = (id) => `${PLUGIN_ID}.${id}`;
|
|
20
|
+
const CUSTOM_FIELD_UID = "plugin::magic-editor-x.richtext";
|
|
21
|
+
const HEARTBEAT_MS = 15e3;
|
|
22
|
+
const MAX_BACKOFF_MS = 12e4;
|
|
23
|
+
const IDLE_AFTER_MS = 6e4;
|
|
24
|
+
const MAX_DEPTH$1 = 8;
|
|
25
|
+
const hasEditorFields = (schema, components = {}, depth = 0) => {
|
|
26
|
+
if (!schema || !schema.attributes || depth > MAX_DEPTH$1) return false;
|
|
27
|
+
return Object.values(schema.attributes).some((attribute) => {
|
|
28
|
+
if (!attribute || typeof attribute !== "object") return false;
|
|
29
|
+
if (attribute.customField === CUSTOM_FIELD_UID) return true;
|
|
30
|
+
if (attribute.type === "component")
|
|
31
|
+
return hasEditorFields(components[attribute.component], components, depth + 1);
|
|
32
|
+
if (attribute.type === "dynamiczone" && Array.isArray(attribute.components))
|
|
33
|
+
return attribute.components.some((uid) => hasEditorFields(components[uid], components, depth + 1));
|
|
34
|
+
return false;
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
const nextDelay = (failures, status = null) => {
|
|
38
|
+
if (!failures) return HEARTBEAT_MS;
|
|
39
|
+
const delay = Math.min(MAX_BACKOFF_MS, HEARTBEAT_MS * 2 ** Math.min(failures, 4));
|
|
40
|
+
return status === 429 ? Math.max(6e4, delay) : delay;
|
|
41
|
+
};
|
|
42
|
+
const createTabId = (source = globalThis.crypto) => {
|
|
43
|
+
const bytes = source.getRandomValues(new Uint8Array(16));
|
|
44
|
+
let binary = "";
|
|
45
|
+
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
46
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
47
|
+
};
|
|
48
|
+
const entryPath = (target, extra = {}) => {
|
|
49
|
+
const value = target || {};
|
|
50
|
+
const uid = encodeURIComponent(String(value.uid || ""));
|
|
51
|
+
const path = value.kind === "singleType" ? `/content-manager/single-types/${uid}` : `/content-manager/collection-types/${uid}/${encodeURIComponent(String(value.documentId || ""))}`;
|
|
52
|
+
const params = new URLSearchParams();
|
|
53
|
+
if (value.locale) params.set("plugins[i18n][locale]", value.locale);
|
|
54
|
+
for (const [key, item] of Object.entries(extra)) params.set(key, item);
|
|
55
|
+
const query = params.toString();
|
|
56
|
+
return query ? `${path}?${query}` : path;
|
|
57
|
+
};
|
|
58
|
+
const fieldsWith = (fields, count) => Array.isArray(fields) ? fields.filter((field2) => count(field2) > 0) : [];
|
|
59
|
+
const fieldProblems = (field2) => (field2 && field2.issues ? field2.issues.errors + field2.issues.warnings : 0) + (field2 && field2.brokenLinks && field2.brokenLinks.draft ? field2.brokenLinks.draft : 0);
|
|
60
|
+
const Content = lazy(() => import("./Content-BttG50Mc.mjs"));
|
|
61
|
+
const EntryPanel = ({ model, documentId, document: document2 }) => {
|
|
62
|
+
const { formatMessage } = useIntl();
|
|
63
|
+
const context = unstable_useContentManagerContext();
|
|
64
|
+
const [{ query }] = useQueryParams();
|
|
65
|
+
const { isLoading, allowedActions } = useRBAC(PERMISSIONS.access);
|
|
66
|
+
const locale = query && query.plugins && query.plugins.i18n && typeof query.plugins.i18n.locale === "string" ? query.plugins.i18n.locale : null;
|
|
67
|
+
const editorType = hasEditorFields(context && context.contentType, context && context.components || {});
|
|
68
|
+
if (!documentId || !model || !editorType || isLoading || !allowedActions.canAccess) return null;
|
|
69
|
+
const loading = formatMessage({ id: getTranslation("entryPanel.loading"), defaultMessage: "Loading" });
|
|
70
|
+
return {
|
|
71
|
+
title: formatMessage({ id: getTranslation("entryPanel.title"), defaultMessage: "Magic Editor X" }),
|
|
72
|
+
content: /* @__PURE__ */ jsx(
|
|
73
|
+
Suspense,
|
|
74
|
+
{
|
|
75
|
+
fallback: /* @__PURE__ */ jsx(Typography, { variant: "pi", textColor: "neutral600", children: loading }),
|
|
76
|
+
children: /* @__PURE__ */ jsx(
|
|
77
|
+
Content,
|
|
78
|
+
{
|
|
79
|
+
uid: model,
|
|
80
|
+
documentId,
|
|
81
|
+
locale,
|
|
82
|
+
version: String(document2 && document2.updatedAt || "")
|
|
83
|
+
}
|
|
84
|
+
)
|
|
85
|
+
}
|
|
86
|
+
)
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
const PluginIcon = () => /* @__PURE__ */ jsx(Feather, { "aria-hidden": true });
|
|
90
|
+
const registry$1 = /* @__PURE__ */ new Map();
|
|
91
|
+
const activeBlock = (type) => typeof type === "string" && registry$1.has(type) ? registry$1.get(type) : null;
|
|
92
|
+
const activeBlocks = () => [...registry$1.values()];
|
|
93
|
+
const activeBlockIgnoringCase = (name) => {
|
|
94
|
+
const lower = String(name).toLowerCase();
|
|
95
|
+
for (const definition of registry$1.values()) if (definition.name.toLowerCase() === lower) return definition;
|
|
96
|
+
return null;
|
|
97
|
+
};
|
|
98
|
+
const setActiveBlock = (compiled2) => {
|
|
99
|
+
registry$1.set(compiled2.name, compiled2);
|
|
100
|
+
};
|
|
101
|
+
const isPlainObject$1 = (value) => value !== null && typeof value === "object" && !Array.isArray(value) && (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null);
|
|
102
|
+
const BUILT_IN_MARKER = "_mex";
|
|
103
|
+
const BUILT_IN_MARKER_VERSION = 1;
|
|
104
|
+
const MARKED_BLOCKS = Object.freeze({
|
|
105
|
+
toc: Object.freeze({ keys: Object.freeze(["title", "minLevel", "maxLevel", "style"]), anchor: null }),
|
|
106
|
+
gallery: Object.freeze({
|
|
107
|
+
keys: Object.freeze(["images", "layout", "columns", "caption"]),
|
|
108
|
+
anchor: Object.freeze(["images", "array"])
|
|
109
|
+
}),
|
|
110
|
+
video: Object.freeze({
|
|
111
|
+
keys: Object.freeze(["file", "poster", "title", "caption", "loop", "muted", "tracks"]),
|
|
112
|
+
anchor: Object.freeze(["file", "object"])
|
|
113
|
+
}),
|
|
114
|
+
audio: Object.freeze({
|
|
115
|
+
keys: Object.freeze(["file", "title", "caption", "loop", "tracks"]),
|
|
116
|
+
anchor: Object.freeze(["file", "object"])
|
|
117
|
+
}),
|
|
118
|
+
synced: Object.freeze({ keys: Object.freeze(["ref"]), anchor: Object.freeze(["ref", "string"]) }),
|
|
119
|
+
// 2.4: math block. A Settings custom block named `math` could exist before 2.4.
|
|
120
|
+
math: Object.freeze({ keys: Object.freeze(["tex", "display"]), anchor: Object.freeze(["tex", "string"]) })
|
|
121
|
+
});
|
|
122
|
+
const isBuiltInMarker = (value) => Number.isInteger(value) && /** @type {number} */
|
|
123
|
+
value >= 1;
|
|
124
|
+
const hasBuiltInShape = (shape, data) => {
|
|
125
|
+
if (!Object.keys(data).every((key2) => shape.keys.includes(key2))) return false;
|
|
126
|
+
if (!shape.anchor) return true;
|
|
127
|
+
const [key, kind] = shape.anchor;
|
|
128
|
+
const value = data[key];
|
|
129
|
+
if (kind === "array") return Array.isArray(value);
|
|
130
|
+
if (kind === "object") return isPlainObject$1(value);
|
|
131
|
+
return typeof value === kind;
|
|
132
|
+
};
|
|
133
|
+
const isMarkedBuiltIn = (type, data) => {
|
|
134
|
+
if (!Object.prototype.hasOwnProperty.call(MARKED_BLOCKS, type) || !isPlainObject$1(data)) return false;
|
|
135
|
+
if (isBuiltInMarker(data[BUILT_IN_MARKER])) return true;
|
|
136
|
+
return hasBuiltInShape(MARKED_BLOCKS[type], data);
|
|
137
|
+
};
|
|
138
|
+
const FIELD_KEY_PATTERN = /^[A-Za-z0-9_-]{16,32}$/;
|
|
139
|
+
const FIELD_KEY_LENGTH = 22;
|
|
140
|
+
const BASE64URL = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
|
141
|
+
const isValidFieldKey = (value) => typeof value === "string" && FIELD_KEY_PATTERN.test(value);
|
|
142
|
+
const createFieldKey = (fill) => {
|
|
143
|
+
const bytes = new Uint8Array(22);
|
|
144
|
+
const source = globalThis.crypto && typeof globalThis.crypto.getRandomValues === "function" ? (array) => globalThis.crypto.getRandomValues(array) : null;
|
|
145
|
+
if (!source) throw new Error("No cryptographic random source");
|
|
146
|
+
source(bytes);
|
|
147
|
+
let key = "";
|
|
148
|
+
for (let i = 0; i < FIELD_KEY_LENGTH; i += 1) key += BASE64URL[bytes[i] & 63];
|
|
149
|
+
return key;
|
|
150
|
+
};
|
|
151
|
+
const isPositionalFieldPath = (path) => typeof path === "string" && path.split(".").some((part) => /^\d+$/.test(part));
|
|
152
|
+
const basePathOf = (path) => String(path).split(".").filter((part) => !/^\d+$/.test(part)).join(".");
|
|
153
|
+
const fieldIdentity = (path, fieldKey, positional = isPositionalFieldPath(path)) => {
|
|
154
|
+
if (!positional) return String(path);
|
|
155
|
+
return isValidFieldKey(fieldKey) ? `${basePathOf(path)}#${fieldKey}` : null;
|
|
156
|
+
};
|
|
157
|
+
const EDITOR_DATA_LIMITS = Object.freeze({
|
|
158
|
+
maxBlocks: 5e3,
|
|
159
|
+
maxBytes: 5 * 1024 * 1024,
|
|
160
|
+
maxDepth: 24,
|
|
161
|
+
maxBlockIdLength: 64,
|
|
162
|
+
maxTypeLength: 64
|
|
163
|
+
});
|
|
164
|
+
const FORBIDDEN_KEYS$1 = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
165
|
+
const BLOCK_ID_PATTERN = /^[A-Za-z0-9_-]{1,64}$/;
|
|
166
|
+
const BLOCK_TYPE_PATTERN = /^[A-Za-z][A-Za-z0-9_-]{0,63}$/;
|
|
167
|
+
const isPlainObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value) && (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null);
|
|
168
|
+
class EditorDataError extends Error {
|
|
169
|
+
/**
|
|
170
|
+
* @param {string} message - Fixed, non-sensitive message.
|
|
171
|
+
* @param {string} code - Machine-readable reason.
|
|
172
|
+
*/
|
|
173
|
+
constructor(message, code) {
|
|
174
|
+
super(message);
|
|
175
|
+
this.name = "EditorDataError";
|
|
176
|
+
this.code = code;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
const cloneJsonSafe = (value, depth = 0) => {
|
|
180
|
+
if (depth > EDITOR_DATA_LIMITS.maxDepth) {
|
|
181
|
+
throw new EditorDataError("Editor content is nested too deeply", "TOO_DEEP");
|
|
182
|
+
}
|
|
183
|
+
if (value === null) return null;
|
|
184
|
+
if (Array.isArray(value)) {
|
|
185
|
+
const out = [];
|
|
186
|
+
for (const item of value) {
|
|
187
|
+
const cloned = cloneJsonSafe(item, depth + 1);
|
|
188
|
+
out.push(cloned === void 0 ? null : cloned);
|
|
189
|
+
}
|
|
190
|
+
return out;
|
|
191
|
+
}
|
|
192
|
+
switch (typeof value) {
|
|
193
|
+
case "string":
|
|
194
|
+
case "boolean":
|
|
195
|
+
return value;
|
|
196
|
+
case "number":
|
|
197
|
+
return Number.isFinite(value) ? value : null;
|
|
198
|
+
case "object": {
|
|
199
|
+
const out = {};
|
|
200
|
+
for (const key of Object.keys(value)) {
|
|
201
|
+
if (FORBIDDEN_KEYS$1.has(key)) continue;
|
|
202
|
+
const cloned = cloneJsonSafe(value[key], depth + 1);
|
|
203
|
+
if (cloned !== void 0) out[key] = cloned;
|
|
204
|
+
}
|
|
205
|
+
return out;
|
|
206
|
+
}
|
|
207
|
+
default:
|
|
208
|
+
return void 0;
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
const tryParseJson = (text) => {
|
|
212
|
+
try {
|
|
213
|
+
return { ok: true, value: JSON.parse(text) };
|
|
214
|
+
} catch {
|
|
215
|
+
return { ok: false };
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
const escapeHtml$1 = (text) => String(text).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
219
|
+
const hashString = (text) => {
|
|
220
|
+
let hash = 2166136261;
|
|
221
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
222
|
+
hash ^= text.charCodeAt(i);
|
|
223
|
+
hash = Math.imul(hash, 16777619) >>> 0;
|
|
224
|
+
}
|
|
225
|
+
return hash.toString(16).padStart(8, "0");
|
|
226
|
+
};
|
|
227
|
+
const legacyBlockId = (index2, type, data) => `mx${hashString(`${index2}:${type}:${JSON.stringify(data)}`)}${index2}`;
|
|
228
|
+
const textToBlocks = (text) => {
|
|
229
|
+
const trimmed = text.trim();
|
|
230
|
+
if (!trimmed) return [];
|
|
231
|
+
const looksLikeHtml = /<\/?[a-z][^>]{0,200}>/i.test(trimmed.slice(0, 2e3));
|
|
232
|
+
if (looksLikeHtml) {
|
|
233
|
+
return [{ type: "paragraph", data: { text: trimmed } }];
|
|
234
|
+
}
|
|
235
|
+
return trimmed.split(/\r?\n\s*\r?\n/).map((part) => part.trim()).filter(Boolean).map((part) => ({ type: "paragraph", data: { text: escapeHtml$1(part).replace(/\r?\n/g, "<br>") } }));
|
|
236
|
+
};
|
|
237
|
+
const unwrapValue = (value) => {
|
|
238
|
+
let current = value;
|
|
239
|
+
let legacy = false;
|
|
240
|
+
for (let i = 0; i < 3 && typeof current === "string"; i += 1) {
|
|
241
|
+
const text = current.trim();
|
|
242
|
+
if (!text) return { candidate: null, legacy: true };
|
|
243
|
+
if (text.startsWith("{") || text.startsWith("[") || text.startsWith('"')) {
|
|
244
|
+
const parsed = tryParseJson(text);
|
|
245
|
+
if (parsed.ok) {
|
|
246
|
+
current = parsed.value;
|
|
247
|
+
legacy = true;
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return { candidate: { blocks: textToBlocks(text) }, legacy: true };
|
|
252
|
+
}
|
|
253
|
+
if (Array.isArray(current)) {
|
|
254
|
+
return { candidate: { blocks: current }, legacy: true };
|
|
255
|
+
}
|
|
256
|
+
return { candidate: current, legacy };
|
|
257
|
+
};
|
|
258
|
+
const normalizeEditorData = (value) => {
|
|
259
|
+
if (value === null || value === void 0) return null;
|
|
260
|
+
if (typeof value === "string" && value.length > EDITOR_DATA_LIMITS.maxBytes) {
|
|
261
|
+
throw new EditorDataError("Editor content exceeds the size limit", "TOO_LARGE");
|
|
262
|
+
}
|
|
263
|
+
const { candidate } = unwrapValue(value);
|
|
264
|
+
if (candidate === null || candidate === void 0) return null;
|
|
265
|
+
if (!isPlainObject(candidate)) {
|
|
266
|
+
throw new EditorDataError("Editor content must be an Editor.js object", "INVALID_SHAPE");
|
|
267
|
+
}
|
|
268
|
+
const clean = cloneJsonSafe(candidate);
|
|
269
|
+
const rawBlocks = Array.isArray(clean.blocks) ? clean.blocks : [];
|
|
270
|
+
if (rawBlocks.length > EDITOR_DATA_LIMITS.maxBlocks) {
|
|
271
|
+
throw new EditorDataError("Editor content has too many blocks", "TOO_MANY_BLOCKS");
|
|
272
|
+
}
|
|
273
|
+
const seen = /* @__PURE__ */ new Set();
|
|
274
|
+
const blocks = [];
|
|
275
|
+
rawBlocks.forEach((block, index2) => {
|
|
276
|
+
if (!isPlainObject(block)) return;
|
|
277
|
+
const type = typeof block.type === "string" ? block.type.trim() : "";
|
|
278
|
+
if (!BLOCK_TYPE_PATTERN.test(type)) return;
|
|
279
|
+
const data = isPlainObject(block.data) ? block.data : {};
|
|
280
|
+
let id = typeof block.id === "string" && BLOCK_ID_PATTERN.test(block.id) ? block.id : "";
|
|
281
|
+
if (!id || seen.has(id)) id = legacyBlockId(index2, type, data);
|
|
282
|
+
while (seen.has(id)) id = `${id}x`;
|
|
283
|
+
seen.add(id);
|
|
284
|
+
const normalized = { id, type, data };
|
|
285
|
+
if (isPlainObject(block.tunes) && Object.keys(block.tunes).length > 0) normalized.tunes = block.tunes;
|
|
286
|
+
blocks.push(normalized);
|
|
287
|
+
});
|
|
288
|
+
const time = Number.isFinite(clean.time) && clean.time > 0 ? Math.floor(clean.time) : 0;
|
|
289
|
+
const version = typeof clean.version === "string" && clean.version.length <= 32 ? clean.version : "2.31.7";
|
|
290
|
+
const result = { time, blocks, version };
|
|
291
|
+
if (isValidFieldKey(clean.fieldKey)) result.fieldKey = clean.fieldKey;
|
|
292
|
+
const size = byteLength(JSON.stringify(result));
|
|
293
|
+
if (size > EDITOR_DATA_LIMITS.maxBytes) {
|
|
294
|
+
throw new EditorDataError("Editor content exceeds the size limit", "TOO_LARGE");
|
|
295
|
+
}
|
|
296
|
+
return result;
|
|
297
|
+
};
|
|
298
|
+
const normalizeEditorDataSafe = (value) => {
|
|
299
|
+
try {
|
|
300
|
+
return normalizeEditorData(value);
|
|
301
|
+
} catch {
|
|
302
|
+
return null;
|
|
303
|
+
}
|
|
304
|
+
};
|
|
305
|
+
const byteLength = (text) => {
|
|
306
|
+
let bytes = 0;
|
|
307
|
+
for (let i = 0; i < text.length; i += 1) {
|
|
308
|
+
const code = text.charCodeAt(i);
|
|
309
|
+
if (code < 128) bytes += 1;
|
|
310
|
+
else if (code < 2048) bytes += 2;
|
|
311
|
+
else if (code >= 55296 && code <= 56319) {
|
|
312
|
+
bytes += 4;
|
|
313
|
+
i += 1;
|
|
314
|
+
} else bytes += 3;
|
|
315
|
+
}
|
|
316
|
+
return bytes;
|
|
317
|
+
};
|
|
318
|
+
const computeStats = (data) => {
|
|
319
|
+
if (!data || !Array.isArray(data.blocks)) return { blocks: 0, words: 0, characters: 0 };
|
|
320
|
+
let text = "";
|
|
321
|
+
const collect = (value, depth = 0) => {
|
|
322
|
+
if (depth > 8 || value === null || value === void 0) return;
|
|
323
|
+
if (typeof value === "string") {
|
|
324
|
+
text += ` ${value.replace(/<[^>]{0,500}>/g, " ").replace(/ /g, " ")}`;
|
|
325
|
+
} else if (Array.isArray(value)) value.forEach((item) => collect(item, depth + 1));
|
|
326
|
+
else if (typeof value === "object") {
|
|
327
|
+
for (const [key, item] of Object.entries(value)) {
|
|
328
|
+
if (["url", "link", "href", "src", "id", "documentId", "style", "type", "service"].includes(key))
|
|
329
|
+
continue;
|
|
330
|
+
collect(item, depth + 1);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
};
|
|
334
|
+
data.blocks.forEach((block) => {
|
|
335
|
+
if (block && block.type === "synced" && isMarkedBuiltIn("synced", block.data)) return;
|
|
336
|
+
const definition = block && activeBlock(block.type);
|
|
337
|
+
if (definition) definition.fields(block.data).forEach((field2) => collect(field2.value));
|
|
338
|
+
else collect(block.data);
|
|
339
|
+
});
|
|
340
|
+
const normalized = text.replace(/\s+/g, " ").trim();
|
|
341
|
+
return {
|
|
342
|
+
blocks: data.blocks.length,
|
|
343
|
+
words: normalized ? normalized.split(" ").length : 0,
|
|
344
|
+
characters: normalized.length
|
|
345
|
+
};
|
|
346
|
+
};
|
|
347
|
+
const TABLE_LIMITS = Object.freeze({ rows: 500, cols: 100, merges: 1e3, caption: 4e3 });
|
|
348
|
+
const TABLE_ALIGNS = Object.freeze(["left", "center", "right"]);
|
|
349
|
+
const isObject$3 = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
350
|
+
const tableWidth = (content) => Array.isArray(content) ? content.reduce((width, row) => Math.max(width, Array.isArray(row) ? row.length : 0), 0) : 0;
|
|
351
|
+
const cleanTableAlign = (value, cols) => {
|
|
352
|
+
if (!Array.isArray(value) || cols <= 0) return null;
|
|
353
|
+
const out = value.slice(0, Math.min(cols, TABLE_LIMITS.cols)).map((item) => typeof item === "string" && TABLE_ALIGNS.includes(item) ? item : null);
|
|
354
|
+
while (out.length && out[out.length - 1] === null) out.pop();
|
|
355
|
+
return out.length ? out : null;
|
|
356
|
+
};
|
|
357
|
+
const intersects = (a, b) => a.row < b.row + b.rowspan && b.row < a.row + a.rowspan && a.col < b.col + b.colspan && b.col < a.col + a.colspan;
|
|
358
|
+
const normalizeMerges = (merges, rows, cols, options = {}) => {
|
|
359
|
+
if (!Array.isArray(merges) || rows <= 0 || cols <= 0) return [];
|
|
360
|
+
const out = [];
|
|
361
|
+
for (const merge of merges.slice(0, TABLE_LIMITS.merges)) {
|
|
362
|
+
if (!isObject$3(merge)) continue;
|
|
363
|
+
const { row, col, rowspan, colspan } = merge;
|
|
364
|
+
if (![row, col, rowspan, colspan].every(Number.isInteger)) continue;
|
|
365
|
+
if (row < 0 || col < 0 || rowspan < 1 || colspan < 1 || rowspan * colspan < 2) continue;
|
|
366
|
+
if (row + rowspan > rows || col + colspan > cols) continue;
|
|
367
|
+
if (options.headerRow && row === 0 && rowspan > 1) continue;
|
|
368
|
+
const next = { row, col, rowspan, colspan };
|
|
369
|
+
if (out.some((other) => intersects(other, next))) continue;
|
|
370
|
+
out.push(next);
|
|
371
|
+
}
|
|
372
|
+
return out.sort((a, b) => a.row - b.row || a.col - b.col);
|
|
373
|
+
};
|
|
374
|
+
const mergeMap = (merges) => {
|
|
375
|
+
const anchors = /* @__PURE__ */ new Map();
|
|
376
|
+
const covered = /* @__PURE__ */ new Map();
|
|
377
|
+
for (const merge of merges || []) {
|
|
378
|
+
anchors.set(`${merge.row}:${merge.col}`, merge);
|
|
379
|
+
for (let r = merge.row; r < merge.row + merge.rowspan; r += 1)
|
|
380
|
+
for (let c = merge.col; c < merge.col + merge.colspan; c += 1)
|
|
381
|
+
if (r !== merge.row || c !== merge.col) covered.set(`${r}:${c}`, merge);
|
|
382
|
+
}
|
|
383
|
+
return { anchors, covered };
|
|
384
|
+
};
|
|
385
|
+
const headerKind = (data, row, col) => {
|
|
386
|
+
if (data.withHeadings === true && row === 0) return "col";
|
|
387
|
+
if (data.headerColumn === true && col === 0) return "row";
|
|
388
|
+
return null;
|
|
389
|
+
};
|
|
390
|
+
const tableLayout = (data) => {
|
|
391
|
+
const merges = Array.isArray(data.merges) ? data.merges : [];
|
|
392
|
+
const { anchors, covered } = mergeMap(merges);
|
|
393
|
+
const align = Array.isArray(data.align) ? data.align : [];
|
|
394
|
+
return (data.content || []).map(
|
|
395
|
+
(row, r) => row.map((html, c) => {
|
|
396
|
+
if (covered.has(`${r}:${c}`)) return null;
|
|
397
|
+
const merge = anchors.get(`${r}:${c}`);
|
|
398
|
+
return {
|
|
399
|
+
row: r,
|
|
400
|
+
col: c,
|
|
401
|
+
html,
|
|
402
|
+
rowspan: merge ? merge.rowspan : 1,
|
|
403
|
+
colspan: merge ? merge.colspan : 1,
|
|
404
|
+
header: headerKind(data, r, c),
|
|
405
|
+
align: typeof align[c] === "string" ? align[c] : null
|
|
406
|
+
};
|
|
407
|
+
}).filter(Boolean)
|
|
408
|
+
);
|
|
409
|
+
};
|
|
410
|
+
const isBlankInlineHtml = (html) => typeof html !== "string" || /^(?:\s| | |\u00a0|<br\s*\/?>)*$/i.test(html);
|
|
411
|
+
const cleanTableExtras = (out, d, inline) => {
|
|
412
|
+
if (d.headerColumn === true) out.headerColumn = true;
|
|
413
|
+
const rows = out.content.length;
|
|
414
|
+
const cols = tableWidth(out.content);
|
|
415
|
+
const align = cleanTableAlign(d.align, cols);
|
|
416
|
+
if (align) out.align = align;
|
|
417
|
+
const merges = normalizeMerges(d.merges, rows, cols, { headerRow: out.withHeadings === true });
|
|
418
|
+
if (merges.length) {
|
|
419
|
+
const { covered } = mergeMap(merges);
|
|
420
|
+
out.content = out.content.map(
|
|
421
|
+
(row, r) => Array.from(
|
|
422
|
+
{ length: cols },
|
|
423
|
+
(_, c) => covered.has(`${r}:${c}`) ? "" : row[c] === void 0 ? "" : row[c]
|
|
424
|
+
)
|
|
425
|
+
);
|
|
426
|
+
out.merges = merges;
|
|
427
|
+
}
|
|
428
|
+
if (typeof d.caption === "string") {
|
|
429
|
+
const caption = inline(d.caption.slice(0, TABLE_LIMITS.caption));
|
|
430
|
+
if (!isBlankInlineHtml(caption)) out.caption = caption;
|
|
431
|
+
}
|
|
432
|
+
return out;
|
|
433
|
+
};
|
|
434
|
+
const toTableModel = (data, defaults = {}) => {
|
|
435
|
+
const d = isObject$3(data) ? data : {};
|
|
436
|
+
let content = Array.isArray(d.content) ? d.content.slice(0, TABLE_LIMITS.rows).map(
|
|
437
|
+
(row) => Array.isArray(row) ? row.slice(0, TABLE_LIMITS.cols).map((cell) => typeof cell === "string" ? cell : "") : []
|
|
438
|
+
) : [];
|
|
439
|
+
let cols = tableWidth(content);
|
|
440
|
+
if (!content.length || !cols) {
|
|
441
|
+
const rows = Math.min(Math.max(Number(defaults.rows) || 2, 1), TABLE_LIMITS.rows);
|
|
442
|
+
cols = Math.min(Math.max(Number(defaults.cols) || 2, 1), TABLE_LIMITS.cols);
|
|
443
|
+
content = Array.from({ length: rows }, () => Array(cols).fill(""));
|
|
444
|
+
}
|
|
445
|
+
content = content.map((row) => row.length < cols ? [...row, ...Array(cols - row.length).fill("")] : row);
|
|
446
|
+
const withHeadings = typeof d.withHeadings === "boolean" ? d.withHeadings : Array.isArray(d.content) && d.content.length ? false : defaults.withHeadings === true;
|
|
447
|
+
const align = Array.from({ length: cols }, (_, c) => {
|
|
448
|
+
const value = Array.isArray(d.align) ? d.align[c] : null;
|
|
449
|
+
return TABLE_ALIGNS.includes(value) ? value : null;
|
|
450
|
+
});
|
|
451
|
+
return {
|
|
452
|
+
withHeadings,
|
|
453
|
+
stretched: d.stretched === true,
|
|
454
|
+
headerColumn: d.headerColumn === true,
|
|
455
|
+
content,
|
|
456
|
+
align,
|
|
457
|
+
merges: normalizeMerges(d.merges, content.length, cols, { headerRow: withHeadings }),
|
|
458
|
+
caption: typeof d.caption === "string" ? d.caption.slice(0, TABLE_LIMITS.caption) : ""
|
|
459
|
+
};
|
|
460
|
+
};
|
|
461
|
+
const fromTableModel = (model) => {
|
|
462
|
+
const { covered } = mergeMap(model.merges);
|
|
463
|
+
const out = {
|
|
464
|
+
withHeadings: model.withHeadings === true,
|
|
465
|
+
stretched: model.stretched === true,
|
|
466
|
+
content: model.content.map((row, r) => row.map((cell, c) => covered.has(`${r}:${c}`) ? "" : cell))
|
|
467
|
+
};
|
|
468
|
+
if (model.headerColumn) out.headerColumn = true;
|
|
469
|
+
const align = cleanTableAlign(model.align, tableWidth(model.content));
|
|
470
|
+
if (align) out.align = align;
|
|
471
|
+
if (model.merges.length) out.merges = model.merges.map((merge) => ({ ...merge }));
|
|
472
|
+
if (!isBlankInlineHtml(model.caption)) out.caption = model.caption;
|
|
473
|
+
return out;
|
|
474
|
+
};
|
|
475
|
+
const copy = (model) => ({
|
|
476
|
+
...model,
|
|
477
|
+
content: model.content.map((row) => [...row]),
|
|
478
|
+
align: [...model.align],
|
|
479
|
+
merges: model.merges.map((merge) => ({ ...merge }))
|
|
480
|
+
});
|
|
481
|
+
const rowsOf = (model) => model.content.length;
|
|
482
|
+
const colsOf = (model) => model.content[0] ? model.content[0].length : 0;
|
|
483
|
+
const dropSingles = (merges) => merges.filter((merge) => merge.rowspan * merge.colspan >= 2);
|
|
484
|
+
const mergeAt = (model, row, col) => model.merges.find(
|
|
485
|
+
(merge) => row >= merge.row && row < merge.row + merge.rowspan && col >= merge.col && col < merge.col + merge.colspan
|
|
486
|
+
) || null;
|
|
487
|
+
const spanAt = (model, row, col) => mergeAt(model, row, col) || { row, col, rowspan: 1, colspan: 1 };
|
|
488
|
+
const expandRect = (model, rect) => {
|
|
489
|
+
const out = {
|
|
490
|
+
top: Math.min(rect.top, rect.bottom),
|
|
491
|
+
bottom: Math.max(rect.top, rect.bottom),
|
|
492
|
+
left: Math.min(rect.left, rect.right),
|
|
493
|
+
right: Math.max(rect.left, rect.right)
|
|
494
|
+
};
|
|
495
|
+
let changed = true;
|
|
496
|
+
while (changed) {
|
|
497
|
+
changed = false;
|
|
498
|
+
for (const merge of model.merges) {
|
|
499
|
+
const box = {
|
|
500
|
+
row: out.top,
|
|
501
|
+
col: out.left,
|
|
502
|
+
rowspan: out.bottom - out.top + 1,
|
|
503
|
+
colspan: out.right - out.left + 1
|
|
504
|
+
};
|
|
505
|
+
if (!intersects(box, merge)) continue;
|
|
506
|
+
const bottom = merge.row + merge.rowspan - 1;
|
|
507
|
+
const right = merge.col + merge.colspan - 1;
|
|
508
|
+
if (merge.row < out.top || merge.col < out.left || bottom > out.bottom || right > out.right) {
|
|
509
|
+
out.top = Math.min(out.top, merge.row);
|
|
510
|
+
out.left = Math.min(out.left, merge.col);
|
|
511
|
+
out.bottom = Math.max(out.bottom, bottom);
|
|
512
|
+
out.right = Math.max(out.right, right);
|
|
513
|
+
changed = true;
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
return out;
|
|
518
|
+
};
|
|
519
|
+
const canMergeRect = (model, rect) => {
|
|
520
|
+
const box = expandRect(model, rect);
|
|
521
|
+
if (box.top < 0 || box.left < 0 || box.bottom >= rowsOf(model) || box.right >= colsOf(model)) return false;
|
|
522
|
+
if ((box.bottom - box.top + 1) * (box.right - box.left + 1) < 2) return false;
|
|
523
|
+
if (model.merges.some(
|
|
524
|
+
(m) => m.row === box.top && m.col === box.left && m.row + m.rowspan - 1 === box.bottom && m.col + m.colspan - 1 === box.right
|
|
525
|
+
))
|
|
526
|
+
return false;
|
|
527
|
+
return !(model.withHeadings && box.top === 0 && box.bottom > 0);
|
|
528
|
+
};
|
|
529
|
+
const mergeRect = (model, rect) => {
|
|
530
|
+
if (!canMergeRect(model, rect)) return null;
|
|
531
|
+
const box = expandRect(model, rect);
|
|
532
|
+
const next = copy(model);
|
|
533
|
+
const { covered } = mergeMap(next.merges);
|
|
534
|
+
const parts = [];
|
|
535
|
+
for (let r = box.top; r <= box.bottom; r += 1)
|
|
536
|
+
for (let c = box.left; c <= box.right; c += 1) {
|
|
537
|
+
const html = covered.has(`${r}:${c}`) ? "" : next.content[r][c];
|
|
538
|
+
if (!isBlankInlineHtml(html)) parts.push(html);
|
|
539
|
+
next.content[r][c] = "";
|
|
540
|
+
}
|
|
541
|
+
next.content[box.top][box.left] = parts.join("<br>");
|
|
542
|
+
next.merges = next.merges.filter(
|
|
543
|
+
(m) => !(m.row >= box.top && m.col >= box.left && m.row + m.rowspan - 1 <= box.bottom && m.col + m.colspan - 1 <= box.right)
|
|
544
|
+
);
|
|
545
|
+
if (next.merges.length >= TABLE_LIMITS.merges) return null;
|
|
546
|
+
next.merges.push({
|
|
547
|
+
row: box.top,
|
|
548
|
+
col: box.left,
|
|
549
|
+
rowspan: box.bottom - box.top + 1,
|
|
550
|
+
colspan: box.right - box.left + 1
|
|
551
|
+
});
|
|
552
|
+
next.merges.sort((a, b) => a.row - b.row || a.col - b.col);
|
|
553
|
+
return next;
|
|
554
|
+
};
|
|
555
|
+
const neighbourRect = (model, row, col, direction) => {
|
|
556
|
+
const span = spanAt(model, row, col);
|
|
557
|
+
const rect = {
|
|
558
|
+
top: span.row,
|
|
559
|
+
left: span.col,
|
|
560
|
+
bottom: span.row + span.rowspan - 1,
|
|
561
|
+
right: span.col + span.colspan - 1
|
|
562
|
+
};
|
|
563
|
+
if (direction === "right") {
|
|
564
|
+
if (rect.right + 1 >= colsOf(model)) return null;
|
|
565
|
+
rect.right += 1;
|
|
566
|
+
} else {
|
|
567
|
+
if (rect.bottom + 1 >= rowsOf(model)) return null;
|
|
568
|
+
rect.bottom += 1;
|
|
569
|
+
}
|
|
570
|
+
return expandRect(model, rect);
|
|
571
|
+
};
|
|
572
|
+
const splitCell = (model, row, col) => {
|
|
573
|
+
const merge = mergeAt(model, row, col);
|
|
574
|
+
if (!merge) return null;
|
|
575
|
+
const next = copy(model);
|
|
576
|
+
next.merges = next.merges.filter((m) => !(m.row === merge.row && m.col === merge.col));
|
|
577
|
+
return next;
|
|
578
|
+
};
|
|
579
|
+
const insertRow = (model, at) => {
|
|
580
|
+
if (rowsOf(model) >= TABLE_LIMITS.rows || at < 0 || at > rowsOf(model)) return null;
|
|
581
|
+
const next = copy(model);
|
|
582
|
+
next.content.splice(at, 0, Array(colsOf(model)).fill(""));
|
|
583
|
+
next.merges = next.merges.map((m) => {
|
|
584
|
+
if (m.row >= at) return { ...m, row: m.row + 1 };
|
|
585
|
+
if (at < m.row + m.rowspan) return { ...m, rowspan: m.rowspan + 1 };
|
|
586
|
+
return m;
|
|
587
|
+
});
|
|
588
|
+
return next;
|
|
589
|
+
};
|
|
590
|
+
const deleteRow = (model, at) => {
|
|
591
|
+
if (rowsOf(model) <= 1 || at < 0 || at >= rowsOf(model)) return null;
|
|
592
|
+
const next = copy(model);
|
|
593
|
+
const merges = [];
|
|
594
|
+
for (const m of next.merges) {
|
|
595
|
+
if (m.row + m.rowspan - 1 < at) merges.push(m);
|
|
596
|
+
else if (m.row > at) merges.push({ ...m, row: m.row - 1 });
|
|
597
|
+
else if (m.rowspan > 1) {
|
|
598
|
+
if (m.row === at) next.content[at + 1][m.col] = next.content[at][m.col];
|
|
599
|
+
merges.push({ ...m, rowspan: m.rowspan - 1 });
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
next.content.splice(at, 1);
|
|
603
|
+
next.merges = normalizeMerges(dropSingles(merges), rowsOf(next), colsOf(next), {
|
|
604
|
+
headerRow: next.withHeadings
|
|
605
|
+
});
|
|
606
|
+
return next;
|
|
607
|
+
};
|
|
608
|
+
const insertColumn = (model, at) => {
|
|
609
|
+
if (colsOf(model) >= TABLE_LIMITS.cols || at < 0 || at > colsOf(model)) return null;
|
|
610
|
+
const next = copy(model);
|
|
611
|
+
next.content.forEach((row) => row.splice(at, 0, ""));
|
|
612
|
+
next.align.splice(at, 0, null);
|
|
613
|
+
next.merges = next.merges.map((m) => {
|
|
614
|
+
if (m.col >= at) return { ...m, col: m.col + 1 };
|
|
615
|
+
if (at < m.col + m.colspan) return { ...m, colspan: m.colspan + 1 };
|
|
616
|
+
return m;
|
|
617
|
+
});
|
|
618
|
+
return next;
|
|
619
|
+
};
|
|
620
|
+
const deleteColumn = (model, at) => {
|
|
621
|
+
if (colsOf(model) <= 1 || at < 0 || at >= colsOf(model)) return null;
|
|
622
|
+
const next = copy(model);
|
|
623
|
+
const merges = [];
|
|
624
|
+
for (const m of next.merges) {
|
|
625
|
+
if (m.col + m.colspan - 1 < at) merges.push(m);
|
|
626
|
+
else if (m.col > at) merges.push({ ...m, col: m.col - 1 });
|
|
627
|
+
else if (m.colspan > 1) {
|
|
628
|
+
if (m.col === at) next.content[m.row][at + 1] = next.content[m.row][at];
|
|
629
|
+
merges.push({ ...m, colspan: m.colspan - 1 });
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
next.content.forEach((row) => row.splice(at, 1));
|
|
633
|
+
next.align.splice(at, 1);
|
|
634
|
+
next.merges = dropSingles(merges);
|
|
635
|
+
return next;
|
|
636
|
+
};
|
|
637
|
+
const setHeaderRow = (model, on) => {
|
|
638
|
+
const next = copy(model);
|
|
639
|
+
next.withHeadings = Boolean(on);
|
|
640
|
+
next.merges = normalizeMerges(next.merges, rowsOf(next), colsOf(next), { headerRow: next.withHeadings });
|
|
641
|
+
return next;
|
|
642
|
+
};
|
|
643
|
+
const setColumnAlign = (model, col, value) => {
|
|
644
|
+
const next = copy(model);
|
|
645
|
+
if (col >= 0 && col < next.align.length) next.align[col] = TABLE_ALIGNS.includes(value) ? value : null;
|
|
646
|
+
return next;
|
|
647
|
+
};
|
|
648
|
+
const pasteGrid = (model, row, col, grid) => {
|
|
649
|
+
let next = copy(model);
|
|
650
|
+
const height = Math.min(grid.length, TABLE_LIMITS.rows - row);
|
|
651
|
+
const width = Math.min(tableWidth(grid), TABLE_LIMITS.cols - col);
|
|
652
|
+
while (rowsOf(next) < row + height) next = insertRow(next, rowsOf(next));
|
|
653
|
+
while (colsOf(next) < col + width) next = insertColumn(next, colsOf(next));
|
|
654
|
+
const { covered } = mergeMap(next.merges);
|
|
655
|
+
for (let r = 0; r < height; r += 1)
|
|
656
|
+
for (let c = 0; c < width; c += 1) {
|
|
657
|
+
if (covered.has(`${row + r}:${col + c}`)) continue;
|
|
658
|
+
const value = grid[r][c];
|
|
659
|
+
if (typeof value === "string") next.content[row + r][col + c] = value;
|
|
660
|
+
}
|
|
661
|
+
return next;
|
|
662
|
+
};
|
|
663
|
+
const parseTabularText = (text) => {
|
|
664
|
+
if (typeof text !== "string" || !text.includes(" ") || text.length > 1024 * 1024) return null;
|
|
665
|
+
const lines = text.replace(/\r\n?/g, "\n").replace(/\n$/, "").split("\n");
|
|
666
|
+
if (lines.length > TABLE_LIMITS.rows) lines.length = TABLE_LIMITS.rows;
|
|
667
|
+
return lines.map((line) => line.split(" ").slice(0, TABLE_LIMITS.cols));
|
|
668
|
+
};
|
|
669
|
+
const INLINE_TAGS = Object.freeze([
|
|
670
|
+
"b",
|
|
671
|
+
"strong",
|
|
672
|
+
"i",
|
|
673
|
+
"em",
|
|
674
|
+
"u",
|
|
675
|
+
"s",
|
|
676
|
+
"mark",
|
|
677
|
+
"code",
|
|
678
|
+
"a",
|
|
679
|
+
"br",
|
|
680
|
+
"span",
|
|
681
|
+
"sub",
|
|
682
|
+
"sup"
|
|
683
|
+
]);
|
|
684
|
+
const INTERNAL_LINK_ATTRIBUTES = Object.freeze([
|
|
685
|
+
"data-mex-uid",
|
|
686
|
+
"data-mex-document-id",
|
|
687
|
+
"data-mex-locale",
|
|
688
|
+
"data-mex-mention"
|
|
689
|
+
]);
|
|
690
|
+
const INTERNAL_LINK_RULES = Object.freeze({
|
|
691
|
+
"data-mex-uid": /^api::[a-z0-9-]{1,60}\.[a-z0-9-]{1,60}$/,
|
|
692
|
+
"data-mex-document-id": /^[a-z0-9]{1,64}$/,
|
|
693
|
+
"data-mex-locale": /^[A-Za-z]{2,8}(?:-[A-Za-z0-9]{1,8}){0,3}$/,
|
|
694
|
+
// Boolean flag: written as "true"; an empty value (`<a data-mex-mention>`) is accepted and normalised.
|
|
695
|
+
"data-mex-mention": /^(?:|true)$/
|
|
696
|
+
});
|
|
697
|
+
const cleanInternalLinkAttributes = (attribs) => {
|
|
698
|
+
const out = {};
|
|
699
|
+
if (!attribs || typeof attribs !== "object") return out;
|
|
700
|
+
for (const name of INTERNAL_LINK_ATTRIBUTES) {
|
|
701
|
+
const value = attribs[name];
|
|
702
|
+
if (typeof value === "string" && INTERNAL_LINK_RULES[name].test(value)) out[name] = value;
|
|
703
|
+
}
|
|
704
|
+
if (!out["data-mex-uid"] || !out["data-mex-document-id"]) return {};
|
|
705
|
+
if (out["data-mex-mention"] !== void 0) out["data-mex-mention"] = "true";
|
|
706
|
+
return out;
|
|
707
|
+
};
|
|
708
|
+
const isMentionLink = (attrs) => Boolean(
|
|
709
|
+
attrs && attrs["data-mex-mention"] === "true" && attrs["data-mex-uid"] && attrs["data-mex-document-id"]
|
|
710
|
+
);
|
|
711
|
+
const MATH_ATTRIBUTE = "data-mex-math";
|
|
712
|
+
const FOOTNOTE_ATTRIBUTES = Object.freeze(["data-mex-footnote", "data-mex-note"]);
|
|
713
|
+
const FOOTNOTE_ID = /^[A-Za-z0-9_-]{1,64}$/;
|
|
714
|
+
const MAX_MATH_TEX = 1e4;
|
|
715
|
+
const MAX_INLINE_TEX = 2e3;
|
|
716
|
+
const MAX_FOOTNOTE_NOTE = 4e3;
|
|
717
|
+
const INLINE_FORMAT_ATTRIBUTES = Object.freeze({
|
|
718
|
+
span: ["data-mex-color", "data-mex-annotation", "data-mex-a-*"],
|
|
719
|
+
mark: ["data-mex-highlight"]
|
|
720
|
+
});
|
|
721
|
+
const INLINE_ATTRIBUTES = Object.freeze({
|
|
722
|
+
a: ["href", "target", "rel", "title", ...INTERNAL_LINK_ATTRIBUTES],
|
|
723
|
+
span: ["class", "data-tooltip", MATH_ATTRIBUTE, ...INLINE_FORMAT_ATTRIBUTES.span],
|
|
724
|
+
sup: [...FOOTNOTE_ATTRIBUTES],
|
|
725
|
+
mark: ["class", ...INLINE_FORMAT_ATTRIBUTES.mark],
|
|
726
|
+
u: ["class"],
|
|
727
|
+
s: ["class"],
|
|
728
|
+
code: ["class"]
|
|
729
|
+
});
|
|
730
|
+
const INLINE_CLASSES = Object.freeze({
|
|
731
|
+
span: ["cdx-tooltip"],
|
|
732
|
+
mark: ["cdx-marker"],
|
|
733
|
+
u: ["cdx-underline"],
|
|
734
|
+
s: ["cdx-strikethrough", "cdx-strikethroughs"],
|
|
735
|
+
code: ["inline-code"]
|
|
736
|
+
});
|
|
737
|
+
const LINK_SCHEMES = Object.freeze(["http", "https", "mailto", "tel"]);
|
|
738
|
+
const MAX_URL_LENGTH = 2048;
|
|
739
|
+
const TARGETS = /* @__PURE__ */ new Set(["_blank", "_self", "_parent", "_top"]);
|
|
740
|
+
const sanitizeUrl = (value, options = {}) => {
|
|
741
|
+
const schemes = options.schemes || LINK_SCHEMES;
|
|
742
|
+
const allowRelative = options.allowRelative !== false;
|
|
743
|
+
if (typeof value !== "string") return "";
|
|
744
|
+
const url = value.trim();
|
|
745
|
+
if (!url || url.length > MAX_URL_LENGTH) return "";
|
|
746
|
+
if (/[\u0000-\u001f\u007f\s]/.test(url) && !/^(mailto|tel):/i.test(url)) return "";
|
|
747
|
+
if (url.startsWith("//") || url.startsWith("\\")) return "";
|
|
748
|
+
const scheme = /^([a-zA-Z][a-zA-Z0-9+.-]{0,20}):/.exec(url);
|
|
749
|
+
if (!scheme) {
|
|
750
|
+
return allowRelative && /^[/#?.]/.test(url) ? url : "";
|
|
751
|
+
}
|
|
752
|
+
if (!schemes.includes(scheme[1].toLowerCase())) return "";
|
|
753
|
+
if (scheme[1].toLowerCase().startsWith("http")) {
|
|
754
|
+
try {
|
|
755
|
+
const parsed = new URL(url);
|
|
756
|
+
if (parsed.username || parsed.password) return "";
|
|
757
|
+
} catch {
|
|
758
|
+
return "";
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
return url;
|
|
762
|
+
};
|
|
763
|
+
const plain = (value, max = 1e5) => typeof value === "string" ? value.slice(0, max) : "";
|
|
764
|
+
const oneOf = (value, allowed, fallback) => typeof value === "string" && allowed.includes(value) ? value : fallback;
|
|
765
|
+
const int = (value, min, max, fallback) => {
|
|
766
|
+
const number = typeof value === "string" ? Number(value) : value;
|
|
767
|
+
return Number.isInteger(number) && number >= min && number <= max ? number : fallback;
|
|
768
|
+
};
|
|
769
|
+
const bool = (value) => value === true;
|
|
770
|
+
const MAX_GALLERY_IMAGES = 50;
|
|
771
|
+
const MAX_MEDIA_TRACKS = 8;
|
|
772
|
+
const TRACK_KINDS = Object.freeze(["captions", "subtitles", "descriptions", "chapters"]);
|
|
773
|
+
const SRCLANG = /^[A-Za-z]{2,3}(?:-[A-Za-z0-9]{1,8}){0,3}$/;
|
|
774
|
+
const cleanFocalPoint = (value) => {
|
|
775
|
+
if (!isPlainObject(value)) return void 0;
|
|
776
|
+
const { x, y } = value;
|
|
777
|
+
if (![x, y].every((n) => typeof n === "number" && Number.isFinite(n) && n >= 0 && n <= 1)) return void 0;
|
|
778
|
+
return { x: Math.round(x * 100) / 100, y: Math.round(y * 100) / 100 };
|
|
779
|
+
};
|
|
780
|
+
const cleanMediaFile = (value, kind = {}) => {
|
|
781
|
+
const file = isPlainObject(value) ? value : {};
|
|
782
|
+
const out = { url: sanitizeUrl(file.url, { schemes: ["http", "https"] }) };
|
|
783
|
+
for (const key of ["mime", ...kind.image ? ["alt"] : [], "name", "hash", "ext"])
|
|
784
|
+
if (typeof file[key] === "string") out[key] = plain(file[key], 500);
|
|
785
|
+
for (const key of ["width", "height", "size"]) if (Number.isFinite(file[key])) out[key] = file[key];
|
|
786
|
+
if (kind.image && isPlainObject(file.formats))
|
|
787
|
+
out.formats = generic(file.formats, (s) => plain(s, 2048), 0, "formats");
|
|
788
|
+
if (file.id !== void 0 && (Number.isInteger(file.id) || typeof file.id === "string")) out.id = file.id;
|
|
789
|
+
if (typeof file.documentId === "string") out.documentId = plain(file.documentId, 64);
|
|
790
|
+
const mime = typeof out.mime === "string" ? out.mime.toLowerCase() : "";
|
|
791
|
+
if (kind.mimePrefix && mime && !mime.startsWith(kind.mimePrefix)) out.url = "";
|
|
792
|
+
if (kind.vtt && out.url) {
|
|
793
|
+
const path = out.url.split(/[?#]/)[0].toLowerCase();
|
|
794
|
+
const ext = typeof out.ext === "string" ? out.ext.toLowerCase() : "";
|
|
795
|
+
if (mime !== "text/vtt" && ext !== ".vtt" && !path.endsWith(".vtt")) out.url = "";
|
|
796
|
+
}
|
|
797
|
+
return out;
|
|
798
|
+
};
|
|
799
|
+
const mediaTracks = (tracks) => {
|
|
800
|
+
if (!Array.isArray(tracks)) return [];
|
|
801
|
+
const out = [];
|
|
802
|
+
let hasDefault = false;
|
|
803
|
+
for (const track of tracks.slice(0, MAX_MEDIA_TRACKS)) {
|
|
804
|
+
if (!isPlainObject(track)) continue;
|
|
805
|
+
const file = cleanMediaFile(track.file, { vtt: true });
|
|
806
|
+
if (!file.url) continue;
|
|
807
|
+
const clean = {
|
|
808
|
+
file,
|
|
809
|
+
kind: oneOf(track.kind, TRACK_KINDS, "captions"),
|
|
810
|
+
srclang: typeof track.srclang === "string" && SRCLANG.test(track.srclang) ? track.srclang : "",
|
|
811
|
+
label: plain(track.label, 100)
|
|
812
|
+
};
|
|
813
|
+
if (track.default === true && !hasDefault) {
|
|
814
|
+
clean.default = true;
|
|
815
|
+
hasDefault = true;
|
|
816
|
+
}
|
|
817
|
+
out.push(clean);
|
|
818
|
+
}
|
|
819
|
+
return out;
|
|
820
|
+
};
|
|
821
|
+
const generic = (value, inline, depth = 0, key = "") => {
|
|
822
|
+
if (depth > 20) return null;
|
|
823
|
+
if (typeof value === "string") {
|
|
824
|
+
if (/^(url|href|link|src|photo|source|embed)$/i.test(key)) return sanitizeUrl(value);
|
|
825
|
+
return inline(value);
|
|
826
|
+
}
|
|
827
|
+
if (Array.isArray(value)) return value.map((item) => generic(item, inline, depth + 1, key));
|
|
828
|
+
if (isPlainObject(value)) {
|
|
829
|
+
const out = {};
|
|
830
|
+
for (const [childKey, child] of Object.entries(value))
|
|
831
|
+
out[childKey] = generic(child, inline, depth + 1, childKey);
|
|
832
|
+
return out;
|
|
833
|
+
}
|
|
834
|
+
return value;
|
|
835
|
+
};
|
|
836
|
+
const listItems = (items, inline, depth = 0) => {
|
|
837
|
+
if (!Array.isArray(items) || depth > 12) return [];
|
|
838
|
+
return items.map((item) => {
|
|
839
|
+
if (typeof item === "string") return inline(item);
|
|
840
|
+
if (!isPlainObject(item)) return { content: "", items: [] };
|
|
841
|
+
const out = { content: inline(typeof item.content === "string" ? item.content : plain(item.text)) };
|
|
842
|
+
if (isPlainObject(item.meta)) {
|
|
843
|
+
const meta = {};
|
|
844
|
+
if (typeof item.meta.checked === "boolean") meta.checked = item.meta.checked;
|
|
845
|
+
const start = int(item.meta.start, 0, 1e5, void 0);
|
|
846
|
+
if (start !== void 0) meta.start = start;
|
|
847
|
+
if (typeof item.meta.counterType === "string") {
|
|
848
|
+
meta.counterType = oneOf(
|
|
849
|
+
item.meta.counterType,
|
|
850
|
+
["numeric", "lower-roman", "upper-roman", "lower-alpha", "upper-alpha"],
|
|
851
|
+
"numeric"
|
|
852
|
+
);
|
|
853
|
+
}
|
|
854
|
+
out.meta = meta;
|
|
855
|
+
}
|
|
856
|
+
out.items = listItems(item.items, inline, depth + 1);
|
|
857
|
+
return out;
|
|
858
|
+
});
|
|
859
|
+
};
|
|
860
|
+
const MAX_CODE_FILENAME = 200;
|
|
861
|
+
const FILENAME_STRIP = (
|
|
862
|
+
// eslint-disable-next-line no-control-regex -- the point is to remove control characters
|
|
863
|
+
/[\u0000-\u001f\u007f-\u009f\u061c\u200b-\u200f\u2028-\u202e\u2060\u2066-\u2069\ufeff]/g
|
|
864
|
+
);
|
|
865
|
+
const cleanCodeFilename = (value) => {
|
|
866
|
+
if (typeof value !== "string") return "";
|
|
867
|
+
return Array.from(
|
|
868
|
+
value.slice(0, 4 * MAX_CODE_FILENAME).replace(/[\t\n\r\f\v]/g, " ").replace(FILENAME_STRIP, "").replace(/\s+/g, " ").trim()
|
|
869
|
+
).slice(0, MAX_CODE_FILENAME).join("").trim();
|
|
870
|
+
};
|
|
871
|
+
const BLOCK_POLICIES = {
|
|
872
|
+
paragraph: (d, inline) => ({ text: inline(d.text) }),
|
|
873
|
+
header: (d, inline) => ({ text: inline(d.text), level: int(d.level, 1, 6, 2) }),
|
|
874
|
+
quote: (d, inline) => ({
|
|
875
|
+
text: inline(d.text),
|
|
876
|
+
caption: inline(d.caption),
|
|
877
|
+
alignment: oneOf(d.alignment, ["left", "center"], "left")
|
|
878
|
+
}),
|
|
879
|
+
list: (d, inline) => {
|
|
880
|
+
const out = {
|
|
881
|
+
style: oneOf(d.style, ["ordered", "unordered", "checklist"], "unordered"),
|
|
882
|
+
items: listItems(d.items, inline)
|
|
883
|
+
};
|
|
884
|
+
if (isPlainObject(d.meta)) {
|
|
885
|
+
const meta = {};
|
|
886
|
+
const start = int(d.meta.start, 0, 1e5, void 0);
|
|
887
|
+
if (start !== void 0) meta.start = start;
|
|
888
|
+
if (typeof d.meta.counterType === "string") {
|
|
889
|
+
meta.counterType = oneOf(
|
|
890
|
+
d.meta.counterType,
|
|
891
|
+
["numeric", "lower-roman", "upper-roman", "lower-alpha", "upper-alpha"],
|
|
892
|
+
"numeric"
|
|
893
|
+
);
|
|
894
|
+
}
|
|
895
|
+
out.meta = meta;
|
|
896
|
+
}
|
|
897
|
+
return out;
|
|
898
|
+
},
|
|
899
|
+
checklist: (d, inline) => ({
|
|
900
|
+
items: Array.isArray(d.items) ? d.items.filter(isPlainObject).map((item) => ({ text: inline(item.text), checked: bool(item.checked) })) : []
|
|
901
|
+
}),
|
|
902
|
+
warning: (d, inline) => ({ title: inline(d.title), message: inline(d.message) }),
|
|
903
|
+
alert: (d, inline) => ({
|
|
904
|
+
type: oneOf(
|
|
905
|
+
d.type,
|
|
906
|
+
["primary", "secondary", "info", "success", "warning", "danger", "light", "dark"],
|
|
907
|
+
"info"
|
|
908
|
+
),
|
|
909
|
+
align: oneOf(d.align, ["left", "center", "right"], "left"),
|
|
910
|
+
message: inline(d.message)
|
|
911
|
+
}),
|
|
912
|
+
toggle: (d, inline) => ({
|
|
913
|
+
text: inline(d.text),
|
|
914
|
+
status: oneOf(d.status, ["open", "closed"], "open"),
|
|
915
|
+
fk: typeof d.fk === "string" ? d.fk.slice(0, 64) : void 0,
|
|
916
|
+
items: int(d.items, 0, 5e3, 0)
|
|
917
|
+
}),
|
|
918
|
+
code: (d) => ({ code: plain(d.code, 5e5) }),
|
|
919
|
+
codeFlask: (d) => {
|
|
920
|
+
const out = {
|
|
921
|
+
code: plain(d.code, 5e5),
|
|
922
|
+
language: typeof d.language === "string" && /^[a-z0-9+#-]{1,32}$/i.test(d.language) ? d.language : "plaintext"
|
|
923
|
+
};
|
|
924
|
+
if (typeof d.showlinenumbers === "boolean") out.showlinenumbers = d.showlinenumbers;
|
|
925
|
+
const filename = cleanCodeFilename(d.filename);
|
|
926
|
+
if (filename) out.filename = filename;
|
|
927
|
+
return out;
|
|
928
|
+
},
|
|
929
|
+
raw: (d, _inline, ctx) => {
|
|
930
|
+
const html = plain(d.html, 5e5);
|
|
931
|
+
if (ctx.rawHtml === "allow") return { html };
|
|
932
|
+
if (ctx.rawHtml === "disable") return { html: "" };
|
|
933
|
+
return { html: ctx.rawSanitizer(html) };
|
|
934
|
+
},
|
|
935
|
+
// 2.4: optional header column, column alignment, merged cells and caption (shared/table.mjs).
|
|
936
|
+
table: (d, inline) => cleanTableExtras(
|
|
937
|
+
{
|
|
938
|
+
withHeadings: bool(d.withHeadings),
|
|
939
|
+
stretched: bool(d.stretched),
|
|
940
|
+
content: Array.isArray(d.content) ? d.content.slice(0, TABLE_LIMITS.rows).map(
|
|
941
|
+
(row) => Array.isArray(row) ? row.slice(0, TABLE_LIMITS.cols).map((cell) => inline(cell)) : []
|
|
942
|
+
) : []
|
|
943
|
+
},
|
|
944
|
+
d,
|
|
945
|
+
inline
|
|
946
|
+
),
|
|
947
|
+
image: (d, inline) => {
|
|
948
|
+
const file = isPlainObject(d.file) ? d.file : {};
|
|
949
|
+
const cleanFile = { url: sanitizeUrl(file.url, { schemes: ["http", "https"] }) };
|
|
950
|
+
for (const key of ["mime", "alt", "name", "hash", "ext"])
|
|
951
|
+
if (typeof file[key] === "string") cleanFile[key] = plain(file[key], 500);
|
|
952
|
+
for (const key of ["width", "height", "size"]) if (Number.isFinite(file[key])) cleanFile[key] = file[key];
|
|
953
|
+
if (isPlainObject(file.formats))
|
|
954
|
+
cleanFile.formats = generic(file.formats, (s) => plain(s, 2048), 0, "formats");
|
|
955
|
+
if (file.id !== void 0 && (Number.isInteger(file.id) || typeof file.id === "string"))
|
|
956
|
+
cleanFile.id = file.id;
|
|
957
|
+
if (typeof file.documentId === "string") cleanFile.documentId = plain(file.documentId, 64);
|
|
958
|
+
const out = {
|
|
959
|
+
file: cleanFile,
|
|
960
|
+
caption: inline(d.caption),
|
|
961
|
+
withBorder: bool(d.withBorder),
|
|
962
|
+
withBackground: bool(d.withBackground),
|
|
963
|
+
stretched: bool(d.stretched)
|
|
964
|
+
};
|
|
965
|
+
if (d.decorative === true) out.decorative = true;
|
|
966
|
+
const focalPoint = cleanFocalPoint(d.focalPoint);
|
|
967
|
+
if (focalPoint) out.focalPoint = focalPoint;
|
|
968
|
+
return out;
|
|
969
|
+
},
|
|
970
|
+
simpleImage: (d, inline) => {
|
|
971
|
+
const out = {
|
|
972
|
+
url: sanitizeUrl(d.url, { schemes: ["http", "https"] }),
|
|
973
|
+
caption: inline(d.caption),
|
|
974
|
+
withBorder: bool(d.withBorder),
|
|
975
|
+
withBackground: bool(d.withBackground),
|
|
976
|
+
stretched: bool(d.stretched)
|
|
977
|
+
};
|
|
978
|
+
if (typeof d.alt === "string") out.alt = plain(d.alt, 500);
|
|
979
|
+
if (d.decorative === true) out.decorative = true;
|
|
980
|
+
return out;
|
|
981
|
+
},
|
|
982
|
+
gallery: (d, inline) => {
|
|
983
|
+
const images = [];
|
|
984
|
+
for (const item of Array.isArray(d.images) ? d.images : []) {
|
|
985
|
+
if (images.length >= MAX_GALLERY_IMAGES) break;
|
|
986
|
+
if (!isPlainObject(item)) continue;
|
|
987
|
+
const file = cleanMediaFile(item.file, { image: true, mimePrefix: "image/" });
|
|
988
|
+
if (!file.url) continue;
|
|
989
|
+
const image = { file, caption: inline(item.caption) };
|
|
990
|
+
if (item.decorative === true) image.decorative = true;
|
|
991
|
+
const focalPoint = cleanFocalPoint(item.focalPoint);
|
|
992
|
+
if (focalPoint) image.focalPoint = focalPoint;
|
|
993
|
+
images.push(image);
|
|
994
|
+
}
|
|
995
|
+
return {
|
|
996
|
+
images,
|
|
997
|
+
layout: oneOf(d.layout, ["grid", "carousel"], "grid"),
|
|
998
|
+
columns: int(d.columns, 1, 6, 3),
|
|
999
|
+
caption: inline(d.caption),
|
|
1000
|
+
_mex: BUILT_IN_MARKER_VERSION
|
|
1001
|
+
};
|
|
1002
|
+
},
|
|
1003
|
+
video: (d, inline) => {
|
|
1004
|
+
const out = {
|
|
1005
|
+
file: cleanMediaFile(d.file, { mimePrefix: "video/" }),
|
|
1006
|
+
title: plain(d.title, 200),
|
|
1007
|
+
caption: inline(d.caption),
|
|
1008
|
+
loop: bool(d.loop),
|
|
1009
|
+
muted: bool(d.muted),
|
|
1010
|
+
tracks: mediaTracks(d.tracks)
|
|
1011
|
+
};
|
|
1012
|
+
if (isPlainObject(d.poster)) {
|
|
1013
|
+
const poster = cleanMediaFile(d.poster, { image: true, mimePrefix: "image/" });
|
|
1014
|
+
if (poster.url) out.poster = poster;
|
|
1015
|
+
}
|
|
1016
|
+
out._mex = BUILT_IN_MARKER_VERSION;
|
|
1017
|
+
return out;
|
|
1018
|
+
},
|
|
1019
|
+
audio: (d, inline) => ({
|
|
1020
|
+
file: cleanMediaFile(d.file, { mimePrefix: "audio/" }),
|
|
1021
|
+
title: plain(d.title, 200),
|
|
1022
|
+
caption: inline(d.caption),
|
|
1023
|
+
loop: bool(d.loop),
|
|
1024
|
+
tracks: mediaTracks(d.tracks),
|
|
1025
|
+
_mex: BUILT_IN_MARKER_VERSION
|
|
1026
|
+
}),
|
|
1027
|
+
linkTool: (d) => {
|
|
1028
|
+
const meta = isPlainObject(d.meta) ? d.meta : {};
|
|
1029
|
+
const image = isPlainObject(meta.image) ? { url: sanitizeUrl(meta.image.url, { schemes: ["http", "https"] }) } : void 0;
|
|
1030
|
+
const cleanMeta = { title: plain(meta.title, 500), description: plain(meta.description, 2e3) };
|
|
1031
|
+
if (typeof meta.site_name === "string") cleanMeta.site_name = plain(meta.site_name, 200);
|
|
1032
|
+
if (image && image.url) cleanMeta.image = image;
|
|
1033
|
+
return { link: sanitizeUrl(d.link, { schemes: ["http", "https"] }), meta: cleanMeta };
|
|
1034
|
+
},
|
|
1035
|
+
attaches: (d) => {
|
|
1036
|
+
const file = isPlainObject(d.file) ? d.file : {};
|
|
1037
|
+
const cleanFile = { url: sanitizeUrl(file.url, { schemes: ["http", "https"] }) };
|
|
1038
|
+
if (typeof file.name === "string") cleanFile.name = plain(file.name, 255);
|
|
1039
|
+
if (typeof file.extension === "string") cleanFile.extension = plain(file.extension, 16);
|
|
1040
|
+
if (Number.isFinite(file.size)) cleanFile.size = file.size;
|
|
1041
|
+
if (typeof file.title === "string") cleanFile.title = plain(file.title, 255);
|
|
1042
|
+
if (file.id !== void 0 && (Number.isInteger(file.id) || typeof file.id === "string"))
|
|
1043
|
+
cleanFile.id = file.id;
|
|
1044
|
+
if (typeof file.documentId === "string") cleanFile.documentId = plain(file.documentId, 64);
|
|
1045
|
+
return { file: cleanFile, title: plain(d.title, 255) };
|
|
1046
|
+
},
|
|
1047
|
+
embed: (d, inline) => ({
|
|
1048
|
+
service: typeof d.service === "string" && /^[a-z0-9_-]{1,40}$/i.test(d.service) ? d.service : "",
|
|
1049
|
+
source: sanitizeUrl(d.source, { schemes: ["https", "http"], allowRelative: false }),
|
|
1050
|
+
embed: sanitizeUrl(d.embed, { schemes: ["https"], allowRelative: false }),
|
|
1051
|
+
width: int(d.width, 1, 1e4, void 0),
|
|
1052
|
+
height: int(d.height, 1, 1e4, void 0),
|
|
1053
|
+
caption: inline(d.caption)
|
|
1054
|
+
}),
|
|
1055
|
+
personality: (d) => ({
|
|
1056
|
+
name: plain(d.name, 500),
|
|
1057
|
+
description: plain(d.description, 2e3),
|
|
1058
|
+
link: sanitizeUrl(d.link, { schemes: ["http", "https", "mailto"] }),
|
|
1059
|
+
photo: sanitizeUrl(d.photo, { schemes: ["http", "https"] })
|
|
1060
|
+
}),
|
|
1061
|
+
button: (d) => ({
|
|
1062
|
+
text: plain(d.text, 200),
|
|
1063
|
+
link: sanitizeUrl(d.link),
|
|
1064
|
+
style: oneOf(d.style, ["primary", "secondary", "outline", "ghost", "danger", "success"], "primary"),
|
|
1065
|
+
openInNewTab: bool(d.openInNewTab)
|
|
1066
|
+
}),
|
|
1067
|
+
delimiter: () => ({}),
|
|
1068
|
+
// Synced block (2.3): only the reference to its source is stored; an invalid reference becomes ''.
|
|
1069
|
+
synced: (d) => ({
|
|
1070
|
+
ref: typeof d.ref === "string" && /^[a-z0-9]{1,64}$/.test(d.ref) ? d.ref : "",
|
|
1071
|
+
_mex: BUILT_IN_MARKER_VERSION
|
|
1072
|
+
}),
|
|
1073
|
+
// Math block (2.4): only the TeX source is stored; `display: false` renders it in text style.
|
|
1074
|
+
math: (d) => ({
|
|
1075
|
+
tex: plain(d.tex, MAX_MATH_TEX),
|
|
1076
|
+
display: d.display !== false,
|
|
1077
|
+
_mex: BUILT_IN_MARKER_VERSION
|
|
1078
|
+
}),
|
|
1079
|
+
toc: (d) => {
|
|
1080
|
+
const minLevel = int(d.minLevel, 1, 6, 2);
|
|
1081
|
+
const maxLevel = int(d.maxLevel, 1, 6, 4);
|
|
1082
|
+
return {
|
|
1083
|
+
title: plain(d.title, 200),
|
|
1084
|
+
minLevel: Math.min(minLevel, maxLevel),
|
|
1085
|
+
maxLevel: Math.max(minLevel, maxLevel),
|
|
1086
|
+
style: oneOf(d.style, ["ordered", "unordered"], "unordered"),
|
|
1087
|
+
_mex: BUILT_IN_MARKER_VERSION
|
|
1088
|
+
};
|
|
1089
|
+
}
|
|
1090
|
+
};
|
|
1091
|
+
const isBuiltInBlock = (block) => {
|
|
1092
|
+
if (!block || typeof block.type !== "string") return false;
|
|
1093
|
+
if (!Object.prototype.hasOwnProperty.call(BLOCK_POLICIES, block.type)) return false;
|
|
1094
|
+
if (!Object.prototype.hasOwnProperty.call(MARKED_BLOCKS, block.type)) return true;
|
|
1095
|
+
return isMarkedBuiltIn(block.type, block.data);
|
|
1096
|
+
};
|
|
1097
|
+
const sanitizeTunes = (tunes) => {
|
|
1098
|
+
if (!isPlainObject(tunes)) return void 0;
|
|
1099
|
+
const out = {};
|
|
1100
|
+
if (isPlainObject(tunes.alignmentTune)) {
|
|
1101
|
+
out.alignmentTune = {
|
|
1102
|
+
alignment: oneOf(tunes.alignmentTune.alignment, ["left", "center", "right", "justify"], "left")
|
|
1103
|
+
};
|
|
1104
|
+
}
|
|
1105
|
+
if (typeof tunes.textVariant === "string") {
|
|
1106
|
+
out.textVariant = oneOf(tunes.textVariant, ["", "call-out", "citation", "details"], "");
|
|
1107
|
+
}
|
|
1108
|
+
if (isPlainObject(tunes.indentTune)) {
|
|
1109
|
+
out.indentTune = { indentLevel: int(tunes.indentTune.indentLevel, 0, 10, 0) };
|
|
1110
|
+
}
|
|
1111
|
+
return Object.keys(out).length ? out : void 0;
|
|
1112
|
+
};
|
|
1113
|
+
const registeredBlockOf = (block, explicit) => {
|
|
1114
|
+
if (!block || typeof block.type !== "string" || isBuiltInBlock(block)) return null;
|
|
1115
|
+
if (explicit instanceof Map) return explicit.get(block.type) || null;
|
|
1116
|
+
return activeBlock(block.type);
|
|
1117
|
+
};
|
|
1118
|
+
const sanitizeEditorData = (data, options) => {
|
|
1119
|
+
if (!data) return data;
|
|
1120
|
+
const inline = (value) => typeof value === "string" ? options.sanitizeInlineHtml(value) : "";
|
|
1121
|
+
const ctx = { rawHtml: options.rawHtml || "sanitize", rawSanitizer: options.sanitizeRawHtml };
|
|
1122
|
+
const blocks = data.blocks.map((block) => {
|
|
1123
|
+
const policy = isBuiltInBlock(block) ? BLOCK_POLICIES[block.type] : null;
|
|
1124
|
+
const registered = policy ? null : registeredBlockOf(block, options.blocks);
|
|
1125
|
+
const cleanData = policy ? policy(block.data || {}, inline, ctx) : registered ? registered.clean(block.data || {}, inline) : generic(block.data || {}, inline, 0);
|
|
1126
|
+
for (const key of Object.keys(cleanData)) if (cleanData[key] === void 0) delete cleanData[key];
|
|
1127
|
+
const out2 = { id: block.id, type: block.type, data: cleanData };
|
|
1128
|
+
const tunes = sanitizeTunes(block.tunes);
|
|
1129
|
+
if (tunes) out2.tunes = tunes;
|
|
1130
|
+
return out2;
|
|
1131
|
+
});
|
|
1132
|
+
const out = { time: data.time, blocks, version: data.version };
|
|
1133
|
+
if (typeof data.fieldKey === "string") out.fieldKey = data.fieldKey;
|
|
1134
|
+
return out;
|
|
1135
|
+
};
|
|
1136
|
+
const sanitizeTarget = (value) => typeof value === "string" && TARGETS.has(value) ? value : null;
|
|
1137
|
+
const BLOCK_FIELD_TYPES = Object.freeze([
|
|
1138
|
+
"text",
|
|
1139
|
+
"inline",
|
|
1140
|
+
"url",
|
|
1141
|
+
"enum",
|
|
1142
|
+
"number",
|
|
1143
|
+
"boolean",
|
|
1144
|
+
"array",
|
|
1145
|
+
"object"
|
|
1146
|
+
]);
|
|
1147
|
+
const BLOCK_LIMITS = Object.freeze({
|
|
1148
|
+
/** Fields per object (also the schema root). */
|
|
1149
|
+
maxFields: 50,
|
|
1150
|
+
/** Nesting of arrays and objects below the root. */
|
|
1151
|
+
maxDepth: 4,
|
|
1152
|
+
/** Largest `max` of a text or inline field. */
|
|
1153
|
+
maxText: 1e5,
|
|
1154
|
+
/** Default `max` of a text field. */
|
|
1155
|
+
defaultText: 1e3,
|
|
1156
|
+
/** Default `max` of an inline HTML field. */
|
|
1157
|
+
defaultInline: 1e4,
|
|
1158
|
+
/** Largest `max` of an array field. */
|
|
1159
|
+
maxItems: 500,
|
|
1160
|
+
/** Default `max` of an array field. */
|
|
1161
|
+
defaultItems: 50,
|
|
1162
|
+
/** Values of an enum field. */
|
|
1163
|
+
maxEnumValues: 100,
|
|
1164
|
+
/** Values cleaned per block; later array items are dropped. */
|
|
1165
|
+
maxValues: 2e4,
|
|
1166
|
+
/** Length of a URL value. */
|
|
1167
|
+
maxUrl: 2048
|
|
1168
|
+
});
|
|
1169
|
+
const BLOCK_PRESETS = Object.freeze(["article", "basic", "minimal"]);
|
|
1170
|
+
const RESERVED_BLOCK_NAMES = Object.freeze([
|
|
1171
|
+
"paragraph",
|
|
1172
|
+
"header",
|
|
1173
|
+
"list",
|
|
1174
|
+
"checklist",
|
|
1175
|
+
"quote",
|
|
1176
|
+
"warning",
|
|
1177
|
+
"alert",
|
|
1178
|
+
"toggle",
|
|
1179
|
+
"code",
|
|
1180
|
+
"codeFlask",
|
|
1181
|
+
"delimiter",
|
|
1182
|
+
"toc",
|
|
1183
|
+
"synced",
|
|
1184
|
+
"table",
|
|
1185
|
+
"embed",
|
|
1186
|
+
"raw",
|
|
1187
|
+
"image",
|
|
1188
|
+
"simpleImage",
|
|
1189
|
+
"gallery",
|
|
1190
|
+
"video",
|
|
1191
|
+
"audio",
|
|
1192
|
+
"linkTool",
|
|
1193
|
+
"attaches",
|
|
1194
|
+
"personality",
|
|
1195
|
+
"button",
|
|
1196
|
+
"mediaLib",
|
|
1197
|
+
"embeddedEntry",
|
|
1198
|
+
"bold",
|
|
1199
|
+
"italic",
|
|
1200
|
+
"hyperlink",
|
|
1201
|
+
"mention",
|
|
1202
|
+
"marker",
|
|
1203
|
+
"underline",
|
|
1204
|
+
"strikethrough",
|
|
1205
|
+
"inlineCode",
|
|
1206
|
+
"tooltip",
|
|
1207
|
+
"aiAssistant",
|
|
1208
|
+
"alignmentTune",
|
|
1209
|
+
"textVariant",
|
|
1210
|
+
"indentTune"
|
|
1211
|
+
]);
|
|
1212
|
+
const RESERVED = new Set(RESERVED_BLOCK_NAMES.map((name) => name.toLowerCase()));
|
|
1213
|
+
const BLOCK_NAME_PATTERN = /^[A-Za-z][A-Za-z0-9_-]{1,49}$/;
|
|
1214
|
+
const KEY_PATTERN = /^[A-Za-z][A-Za-z0-9_]{0,63}$/;
|
|
1215
|
+
const FORBIDDEN_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
1216
|
+
const DEFINITION_KEYS = /* @__PURE__ */ new Set([
|
|
1217
|
+
"name",
|
|
1218
|
+
"version",
|
|
1219
|
+
"title",
|
|
1220
|
+
"description",
|
|
1221
|
+
"schema",
|
|
1222
|
+
"sanitize",
|
|
1223
|
+
"text",
|
|
1224
|
+
"markdown",
|
|
1225
|
+
"render",
|
|
1226
|
+
"presets"
|
|
1227
|
+
]);
|
|
1228
|
+
const FIELD_KEYS = {
|
|
1229
|
+
text: ["max", "multiline", "default"],
|
|
1230
|
+
inline: ["max", "default"],
|
|
1231
|
+
url: ["schemes", "relative"],
|
|
1232
|
+
enum: ["values", "default"],
|
|
1233
|
+
number: ["min", "max", "integer", "default"],
|
|
1234
|
+
boolean: ["default"],
|
|
1235
|
+
array: ["items", "max"],
|
|
1236
|
+
object: ["fields"]
|
|
1237
|
+
};
|
|
1238
|
+
class BlockDefinitionError extends Error {
|
|
1239
|
+
/**
|
|
1240
|
+
* @param {string} message - Message naming the definition and the problem.
|
|
1241
|
+
* @param {string} [code] - `INVALID_DEFINITION` | `RESERVED_NAME` | `NAME_TAKEN` | `REGISTRY_SEALED`.
|
|
1242
|
+
*/
|
|
1243
|
+
constructor(message, code = "INVALID_DEFINITION") {
|
|
1244
|
+
super(message);
|
|
1245
|
+
this.name = "BlockDefinitionError";
|
|
1246
|
+
this.code = code;
|
|
1247
|
+
}
|
|
1248
|
+
}
|
|
1249
|
+
const isObject$2 = (value) => value !== null && typeof value === "object" && !Array.isArray(value) && (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null);
|
|
1250
|
+
const fail$1 = (where, message) => {
|
|
1251
|
+
throw new BlockDefinitionError(`Magic Editor X block ${where}: ${message}`);
|
|
1252
|
+
};
|
|
1253
|
+
const boundedInt = (value, min, max, fallback, where, key) => {
|
|
1254
|
+
if (value === void 0) return fallback;
|
|
1255
|
+
if (!Number.isInteger(value) || value < min || value > max)
|
|
1256
|
+
fail$1(where, `"${key}" must be an integer from ${min} to ${max}`);
|
|
1257
|
+
return value;
|
|
1258
|
+
};
|
|
1259
|
+
const normalizeField = (input2, where, depth) => {
|
|
1260
|
+
if (!isObject$2(input2)) fail$1(where, 'a field must be an object with a "type"');
|
|
1261
|
+
const type = input2.type;
|
|
1262
|
+
if (!BLOCK_FIELD_TYPES.includes(type)) fail$1(where, `unknown field type ${JSON.stringify(type)}`);
|
|
1263
|
+
for (const key of Object.keys(input2))
|
|
1264
|
+
if (key !== "type" && key !== "label" && !FIELD_KEYS[type].includes(key))
|
|
1265
|
+
fail$1(where, `unknown option "${key}" for a ${type} field`);
|
|
1266
|
+
const out = { type };
|
|
1267
|
+
if (input2.label !== void 0) {
|
|
1268
|
+
if (typeof input2.label !== "string" || input2.label.length > 100)
|
|
1269
|
+
fail$1(where, '"label" must be a string of at most 100 characters');
|
|
1270
|
+
out.label = input2.label;
|
|
1271
|
+
}
|
|
1272
|
+
switch (type) {
|
|
1273
|
+
case "text":
|
|
1274
|
+
case "inline": {
|
|
1275
|
+
out.max = boundedInt(
|
|
1276
|
+
input2.max,
|
|
1277
|
+
1,
|
|
1278
|
+
BLOCK_LIMITS.maxText,
|
|
1279
|
+
type === "text" ? BLOCK_LIMITS.defaultText : BLOCK_LIMITS.defaultInline,
|
|
1280
|
+
where,
|
|
1281
|
+
"max"
|
|
1282
|
+
);
|
|
1283
|
+
if (input2.default !== void 0 && typeof input2.default !== "string")
|
|
1284
|
+
fail$1(where, '"default" must be a string');
|
|
1285
|
+
out.default = (input2.default || "").slice(0, out.max);
|
|
1286
|
+
if (type === "text") {
|
|
1287
|
+
if (input2.multiline !== void 0 && typeof input2.multiline !== "boolean")
|
|
1288
|
+
fail$1(where, '"multiline" must be a boolean');
|
|
1289
|
+
out.multiline = input2.multiline === true;
|
|
1290
|
+
}
|
|
1291
|
+
break;
|
|
1292
|
+
}
|
|
1293
|
+
case "url": {
|
|
1294
|
+
const schemes = input2.schemes === void 0 ? ["http", "https"] : input2.schemes;
|
|
1295
|
+
if (!Array.isArray(schemes) || !schemes.length || schemes.some((scheme) => !LINK_SCHEMES.includes(scheme)))
|
|
1296
|
+
fail$1(where, `"schemes" must be a non-empty list of ${LINK_SCHEMES.join(", ")}`);
|
|
1297
|
+
if (input2.relative !== void 0 && typeof input2.relative !== "boolean")
|
|
1298
|
+
fail$1(where, '"relative" must be a boolean');
|
|
1299
|
+
out.schemes = Object.freeze([...new Set(schemes)]);
|
|
1300
|
+
out.relative = input2.relative !== false;
|
|
1301
|
+
break;
|
|
1302
|
+
}
|
|
1303
|
+
case "enum": {
|
|
1304
|
+
const values = input2.values;
|
|
1305
|
+
if (!Array.isArray(values) || values.length < 1 || values.length > BLOCK_LIMITS.maxEnumValues || values.some((value) => typeof value !== "string" || value.length < 1 || value.length > 100) || new Set(values).size !== values.length)
|
|
1306
|
+
fail$1(
|
|
1307
|
+
where,
|
|
1308
|
+
`"values" must be 1 to ${BLOCK_LIMITS.maxEnumValues} distinct strings (1-100 characters)`
|
|
1309
|
+
);
|
|
1310
|
+
out.values = Object.freeze([...values]);
|
|
1311
|
+
if (input2.default !== void 0 && !values.includes(input2.default))
|
|
1312
|
+
fail$1(where, '"default" must be one of "values"');
|
|
1313
|
+
out.default = input2.default === void 0 ? values[0] : input2.default;
|
|
1314
|
+
break;
|
|
1315
|
+
}
|
|
1316
|
+
case "number": {
|
|
1317
|
+
for (const key of ["min", "max", "default"])
|
|
1318
|
+
if (input2[key] !== void 0 && (typeof input2[key] !== "number" || !Number.isFinite(input2[key])))
|
|
1319
|
+
fail$1(where, `"${key}" must be a finite number`);
|
|
1320
|
+
if (input2.integer !== void 0 && typeof input2.integer !== "boolean")
|
|
1321
|
+
fail$1(where, '"integer" must be a boolean');
|
|
1322
|
+
out.integer = input2.integer === true;
|
|
1323
|
+
out.min = input2.min === void 0 ? -Number.MAX_SAFE_INTEGER : input2.min;
|
|
1324
|
+
out.max = input2.max === void 0 ? Number.MAX_SAFE_INTEGER : input2.max;
|
|
1325
|
+
if (out.min > out.max) fail$1(where, '"min" must not be greater than "max"');
|
|
1326
|
+
if (input2.default !== void 0) {
|
|
1327
|
+
if (input2.default < out.min || input2.default > out.max || out.integer && !Number.isInteger(input2.default))
|
|
1328
|
+
fail$1(where, '"default" must be a valid value of the field');
|
|
1329
|
+
out.default = input2.default;
|
|
1330
|
+
}
|
|
1331
|
+
break;
|
|
1332
|
+
}
|
|
1333
|
+
case "boolean":
|
|
1334
|
+
if (input2.default !== void 0 && typeof input2.default !== "boolean")
|
|
1335
|
+
fail$1(where, '"default" must be a boolean');
|
|
1336
|
+
out.default = input2.default === true;
|
|
1337
|
+
break;
|
|
1338
|
+
case "array":
|
|
1339
|
+
if (depth >= BLOCK_LIMITS.maxDepth) fail$1(where, `fields nest at most ${BLOCK_LIMITS.maxDepth} levels`);
|
|
1340
|
+
out.max = boundedInt(input2.max, 1, BLOCK_LIMITS.maxItems, BLOCK_LIMITS.defaultItems, where, "max");
|
|
1341
|
+
out.items = normalizeField(input2.items, `${where}[]`, depth + 1);
|
|
1342
|
+
break;
|
|
1343
|
+
case "object":
|
|
1344
|
+
if (depth >= BLOCK_LIMITS.maxDepth) fail$1(where, `fields nest at most ${BLOCK_LIMITS.maxDepth} levels`);
|
|
1345
|
+
out.fields = normalizeFields(input2.fields, where, depth + 1);
|
|
1346
|
+
break;
|
|
1347
|
+
}
|
|
1348
|
+
return Object.freeze(out);
|
|
1349
|
+
};
|
|
1350
|
+
const normalizeFields = (input2, where, depth) => {
|
|
1351
|
+
if (!isObject$2(input2)) fail$1(where, '"fields" must be an object of field descriptors');
|
|
1352
|
+
const keys = Object.keys(input2);
|
|
1353
|
+
if (keys.length < 1 || keys.length > BLOCK_LIMITS.maxFields)
|
|
1354
|
+
fail$1(where, `an object needs 1 to ${BLOCK_LIMITS.maxFields} fields`);
|
|
1355
|
+
const out = /* @__PURE__ */ Object.create(null);
|
|
1356
|
+
for (const key of keys) {
|
|
1357
|
+
if (!KEY_PATTERN.test(key) || FORBIDDEN_KEYS.has(key))
|
|
1358
|
+
fail$1(where, `field name ${JSON.stringify(key)} must match ${KEY_PATTERN}`);
|
|
1359
|
+
out[key] = normalizeField(input2[key], `${where}.${key}`, depth);
|
|
1360
|
+
}
|
|
1361
|
+
return Object.freeze(out);
|
|
1362
|
+
};
|
|
1363
|
+
const blockNameProblem = (name) => {
|
|
1364
|
+
if (typeof name !== "string" || !BLOCK_NAME_PATTERN.test(name))
|
|
1365
|
+
return `the name must match ${BLOCK_NAME_PATTERN}`;
|
|
1366
|
+
if (RESERVED.has(name.toLowerCase())) return `"${name}" is a built-in tool name`;
|
|
1367
|
+
return null;
|
|
1368
|
+
};
|
|
1369
|
+
const defineBlock = (definition) => {
|
|
1370
|
+
if (!isObject$2(definition))
|
|
1371
|
+
throw new BlockDefinitionError("Magic Editor X block: the definition must be an object");
|
|
1372
|
+
const label = typeof definition.name === "string" ? `"${definition.name.slice(0, 50)}"` : "(no name)";
|
|
1373
|
+
for (const key of Object.keys(definition))
|
|
1374
|
+
if (!DEFINITION_KEYS.has(key)) fail$1(label, `unknown key "${key}"`);
|
|
1375
|
+
const problem = blockNameProblem(definition.name);
|
|
1376
|
+
if (problem)
|
|
1377
|
+
throw new BlockDefinitionError(
|
|
1378
|
+
`Magic Editor X block ${label}: ${problem}`,
|
|
1379
|
+
problem.includes("built-in") ? "RESERVED_NAME" : "INVALID_DEFINITION"
|
|
1380
|
+
);
|
|
1381
|
+
const out = {
|
|
1382
|
+
name: definition.name,
|
|
1383
|
+
version: boundedInt(definition.version, 1, 1e6, 1, label, "version")
|
|
1384
|
+
};
|
|
1385
|
+
for (const [key, max] of [
|
|
1386
|
+
["title", 100],
|
|
1387
|
+
["description", 500]
|
|
1388
|
+
]) {
|
|
1389
|
+
if (definition[key] === void 0) continue;
|
|
1390
|
+
if (typeof definition[key] !== "string" || definition[key].length > max)
|
|
1391
|
+
fail$1(label, `"${key}" must be a string of at most ${max} characters`);
|
|
1392
|
+
out[key] = definition[key];
|
|
1393
|
+
}
|
|
1394
|
+
out.schema = normalizeFields(definition.schema, `${label} schema`, 0);
|
|
1395
|
+
for (const key of ["sanitize", "text", "markdown", "render"]) {
|
|
1396
|
+
if (definition[key] === void 0) continue;
|
|
1397
|
+
if (typeof definition[key] !== "function") fail$1(label, `"${key}" must be a function`);
|
|
1398
|
+
out[key] = definition[key];
|
|
1399
|
+
}
|
|
1400
|
+
const presets = definition.presets === void 0 ? [] : definition.presets;
|
|
1401
|
+
if (!Array.isArray(presets) || presets.some((preset) => !BLOCK_PRESETS.includes(preset)))
|
|
1402
|
+
fail$1(label, `"presets" must list some of ${BLOCK_PRESETS.join(", ")}`);
|
|
1403
|
+
out.presets = Object.freeze([...new Set(presets)]);
|
|
1404
|
+
return Object.freeze(out);
|
|
1405
|
+
};
|
|
1406
|
+
const cleanValue = (field2, value, inline, budget) => {
|
|
1407
|
+
budget.left -= 1;
|
|
1408
|
+
switch (field2.type) {
|
|
1409
|
+
case "text":
|
|
1410
|
+
return typeof value === "string" ? value.slice(0, field2.max) : field2.default;
|
|
1411
|
+
case "inline":
|
|
1412
|
+
return inline(typeof value === "string" ? value.slice(0, field2.max) : field2.default);
|
|
1413
|
+
case "url":
|
|
1414
|
+
return typeof value === "string" ? sanitizeUrl(value.slice(0, BLOCK_LIMITS.maxUrl), {
|
|
1415
|
+
schemes: field2.schemes,
|
|
1416
|
+
allowRelative: field2.relative
|
|
1417
|
+
}) : "";
|
|
1418
|
+
case "enum":
|
|
1419
|
+
return typeof value === "string" && field2.values.includes(value) ? value : field2.default;
|
|
1420
|
+
case "number":
|
|
1421
|
+
return typeof value === "number" && Number.isFinite(value) && value >= field2.min && value <= field2.max && (!field2.integer || Number.isInteger(value)) ? value : field2.default;
|
|
1422
|
+
case "boolean":
|
|
1423
|
+
return typeof value === "boolean" ? value : field2.default;
|
|
1424
|
+
case "array": {
|
|
1425
|
+
const out = [];
|
|
1426
|
+
if (!Array.isArray(value)) return out;
|
|
1427
|
+
for (const item of value) {
|
|
1428
|
+
if (out.length >= field2.max || budget.left <= 0) break;
|
|
1429
|
+
if (!matchesType(field2.items, item)) continue;
|
|
1430
|
+
const clean = cleanValue(field2.items, item, inline, budget);
|
|
1431
|
+
if (clean !== void 0) out.push(clean);
|
|
1432
|
+
}
|
|
1433
|
+
return out;
|
|
1434
|
+
}
|
|
1435
|
+
case "object":
|
|
1436
|
+
return cleanObject(field2.fields, isObject$2(value) ? value : {}, inline, budget);
|
|
1437
|
+
default:
|
|
1438
|
+
return void 0;
|
|
1439
|
+
}
|
|
1440
|
+
};
|
|
1441
|
+
const matchesType = (field2, item) => {
|
|
1442
|
+
switch (field2.type) {
|
|
1443
|
+
case "text":
|
|
1444
|
+
case "inline":
|
|
1445
|
+
case "url":
|
|
1446
|
+
case "enum":
|
|
1447
|
+
return typeof item === "string";
|
|
1448
|
+
case "number":
|
|
1449
|
+
return typeof item === "number";
|
|
1450
|
+
case "boolean":
|
|
1451
|
+
return typeof item === "boolean";
|
|
1452
|
+
case "array":
|
|
1453
|
+
return Array.isArray(item);
|
|
1454
|
+
default:
|
|
1455
|
+
return isObject$2(item);
|
|
1456
|
+
}
|
|
1457
|
+
};
|
|
1458
|
+
const cleanObject = (fields, value, inline, budget) => {
|
|
1459
|
+
const out = {};
|
|
1460
|
+
for (const key of Object.keys(fields)) {
|
|
1461
|
+
const own = Object.prototype.hasOwnProperty.call(value, key) ? value[key] : void 0;
|
|
1462
|
+
const clean = cleanValue(fields[key], own, inline, budget);
|
|
1463
|
+
if (clean !== void 0) out[key] = clean;
|
|
1464
|
+
}
|
|
1465
|
+
return out;
|
|
1466
|
+
};
|
|
1467
|
+
const eachLeaf = (fields, data, visit) => {
|
|
1468
|
+
const walk = (field2, value, path, depth) => {
|
|
1469
|
+
if (depth > BLOCK_LIMITS.maxDepth + 1) return;
|
|
1470
|
+
if (field2.type === "array") {
|
|
1471
|
+
if (!Array.isArray(value)) return;
|
|
1472
|
+
value.slice(0, field2.max).forEach((item, index2) => walk(field2.items, item, [...path, index2], depth + 1));
|
|
1473
|
+
} else if (field2.type === "object") {
|
|
1474
|
+
if (!isObject$2(value)) return;
|
|
1475
|
+
for (const key of Object.keys(field2.fields))
|
|
1476
|
+
if (Object.prototype.hasOwnProperty.call(value, key))
|
|
1477
|
+
walk(field2.fields[key], value[key], [...path, key], depth + 1);
|
|
1478
|
+
} else visit(field2, value, path);
|
|
1479
|
+
};
|
|
1480
|
+
if (!isObject$2(data)) return;
|
|
1481
|
+
for (const key of Object.keys(fields))
|
|
1482
|
+
if (Object.prototype.hasOwnProperty.call(data, key)) walk(fields[key], data[key], [key], 0);
|
|
1483
|
+
};
|
|
1484
|
+
const fieldAt = (fields, path) => {
|
|
1485
|
+
let current = { type: "object", fields };
|
|
1486
|
+
for (const part of String(path).split(".")) {
|
|
1487
|
+
if (!part) continue;
|
|
1488
|
+
if (current.type === "array") {
|
|
1489
|
+
if (/^\d+$/.test(part)) {
|
|
1490
|
+
current = current.items;
|
|
1491
|
+
continue;
|
|
1492
|
+
}
|
|
1493
|
+
return null;
|
|
1494
|
+
}
|
|
1495
|
+
if (current.type !== "object" || !Object.prototype.hasOwnProperty.call(current.fields, part)) return null;
|
|
1496
|
+
current = current.fields[part];
|
|
1497
|
+
}
|
|
1498
|
+
return current;
|
|
1499
|
+
};
|
|
1500
|
+
const compiled = /* @__PURE__ */ new WeakMap();
|
|
1501
|
+
const compileBlock = (definition) => {
|
|
1502
|
+
if (definition && typeof definition === "object" && compiled.has(definition))
|
|
1503
|
+
return compiled.get(definition);
|
|
1504
|
+
const def = defineBlock(definition && typeof definition === "object" ? { ...definition } : definition);
|
|
1505
|
+
const schema = def.schema;
|
|
1506
|
+
const run = (data, inline) => cleanObject(schema, isObject$2(data) ? data : {}, inline, { left: BLOCK_LIMITS.maxValues });
|
|
1507
|
+
const result = Object.freeze({
|
|
1508
|
+
...def,
|
|
1509
|
+
/**
|
|
1510
|
+
* Cleans untrusted block data (unknown keys dropped, values bounded).
|
|
1511
|
+
* The optional `sanitize` hook runs on the clean data; its result is
|
|
1512
|
+
* cleaned again, so it cannot add keys or unsafe values. A hook that
|
|
1513
|
+
* throws is ignored (the clean data is used).
|
|
1514
|
+
* @param {unknown} data - Block data.
|
|
1515
|
+
* @param {(html: string) => string} inline - Inline HTML sanitiser of the caller.
|
|
1516
|
+
* @returns {object} Clean data.
|
|
1517
|
+
*/
|
|
1518
|
+
clean: (data, inline) => {
|
|
1519
|
+
const first = run(data, inline);
|
|
1520
|
+
if (!def.sanitize) return first;
|
|
1521
|
+
let next;
|
|
1522
|
+
try {
|
|
1523
|
+
next = def.sanitize(copyData(first), { sanitizeUrl });
|
|
1524
|
+
} catch {
|
|
1525
|
+
return first;
|
|
1526
|
+
}
|
|
1527
|
+
return isObject$2(next) ? run(next, inline) : first;
|
|
1528
|
+
},
|
|
1529
|
+
/**
|
|
1530
|
+
* Text fields of the data in schema order: `{ path, kind, value }`
|
|
1531
|
+
* (`html` for inline fields, `plain` for text fields).
|
|
1532
|
+
* @param {unknown} data - Block data.
|
|
1533
|
+
* @returns {Array<{ path: Array<string|number>, kind: 'html'|'plain', value: string }>} Fields.
|
|
1534
|
+
*/
|
|
1535
|
+
fields: (data) => {
|
|
1536
|
+
const out = [];
|
|
1537
|
+
eachLeaf(schema, data, (field2, value, path) => {
|
|
1538
|
+
if (typeof value !== "string") return;
|
|
1539
|
+
if (field2.type === "inline") out.push({ path, kind: "html", value });
|
|
1540
|
+
else if (field2.type === "text") out.push({ path, kind: "plain", value });
|
|
1541
|
+
});
|
|
1542
|
+
return out;
|
|
1543
|
+
},
|
|
1544
|
+
/**
|
|
1545
|
+
* Non-empty URL values: `{ path, value }`.
|
|
1546
|
+
* @param {unknown} data - Block data.
|
|
1547
|
+
* @returns {Array<{ path: Array<string|number>, value: string }>} URLs.
|
|
1548
|
+
*/
|
|
1549
|
+
urls: (data) => {
|
|
1550
|
+
const out = [];
|
|
1551
|
+
eachLeaf(schema, data, (field2, value, path) => {
|
|
1552
|
+
if (field2.type === "url" && typeof value === "string" && value) out.push({ path, value });
|
|
1553
|
+
});
|
|
1554
|
+
return out;
|
|
1555
|
+
},
|
|
1556
|
+
/**
|
|
1557
|
+
* Kind of the value at a dotted path, for the version diff.
|
|
1558
|
+
* @param {string} path - Dotted path.
|
|
1559
|
+
* @returns {'inline'|'plain'|'value'} Kind.
|
|
1560
|
+
*/
|
|
1561
|
+
kindOf: (path) => {
|
|
1562
|
+
const field2 = fieldAt(schema, path);
|
|
1563
|
+
if (!field2) return "value";
|
|
1564
|
+
if (field2.type === "inline") return "inline";
|
|
1565
|
+
if (field2.type === "text") return "plain";
|
|
1566
|
+
return "value";
|
|
1567
|
+
}
|
|
1568
|
+
});
|
|
1569
|
+
compiled.set(result, result);
|
|
1570
|
+
if (definition && typeof definition === "object") compiled.set(definition, result);
|
|
1571
|
+
return result;
|
|
1572
|
+
};
|
|
1573
|
+
const copyData = (value) => {
|
|
1574
|
+
if (Array.isArray(value)) return value.map(copyData);
|
|
1575
|
+
if (isObject$2(value)) {
|
|
1576
|
+
const out = {};
|
|
1577
|
+
for (const [key, child] of Object.entries(value)) out[key] = copyData(child);
|
|
1578
|
+
return out;
|
|
1579
|
+
}
|
|
1580
|
+
return value;
|
|
1581
|
+
};
|
|
1582
|
+
const listCache = /* @__PURE__ */ new WeakMap();
|
|
1583
|
+
const compileBlockList = (list) => {
|
|
1584
|
+
if (!Array.isArray(list)) return /* @__PURE__ */ new Map();
|
|
1585
|
+
if (listCache.has(list)) return listCache.get(list);
|
|
1586
|
+
const map = /* @__PURE__ */ new Map();
|
|
1587
|
+
for (const definition of list) {
|
|
1588
|
+
const def = compileBlock(definition);
|
|
1589
|
+
map.set(def.name, def);
|
|
1590
|
+
}
|
|
1591
|
+
listCache.set(list, map);
|
|
1592
|
+
return map;
|
|
1593
|
+
};
|
|
1594
|
+
const registerBlockDefinition = (definition) => {
|
|
1595
|
+
const compiled2 = compileBlock(definition);
|
|
1596
|
+
const existing = activeBlockIgnoringCase(compiled2.name);
|
|
1597
|
+
if (existing) {
|
|
1598
|
+
if (existing === compiled2) return compiled2;
|
|
1599
|
+
throw new BlockDefinitionError(
|
|
1600
|
+
`Magic Editor X block "${compiled2.name}": a block named "${existing.name}" is already registered`,
|
|
1601
|
+
"NAME_TAKEN"
|
|
1602
|
+
);
|
|
1603
|
+
}
|
|
1604
|
+
setActiveBlock(compiled2);
|
|
1605
|
+
return activeBlock(compiled2.name);
|
|
1606
|
+
};
|
|
1607
|
+
const LINK_SANITIZE = Object.freeze({
|
|
1608
|
+
href: true,
|
|
1609
|
+
target: true,
|
|
1610
|
+
rel: true,
|
|
1611
|
+
...Object.fromEntries(INTERNAL_LINK_ATTRIBUTES.map((name) => [name, true]))
|
|
1612
|
+
});
|
|
1613
|
+
const isObject$1 = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
|
|
1614
|
+
const localized = (value, locale) => {
|
|
1615
|
+
if (typeof value === "string") return value;
|
|
1616
|
+
if (!isObject$1(value)) return "";
|
|
1617
|
+
const lang = String(locale || "en");
|
|
1618
|
+
const pick = value[lang] ?? value[lang.split("-")[0]] ?? value.en ?? Object.values(value)[0];
|
|
1619
|
+
return typeof pick === "string" ? pick : "";
|
|
1620
|
+
};
|
|
1621
|
+
const sanitizeRulesFor = (fields) => {
|
|
1622
|
+
const rule = (field2) => {
|
|
1623
|
+
if (field2.type === "inline")
|
|
1624
|
+
return {
|
|
1625
|
+
b: true,
|
|
1626
|
+
strong: true,
|
|
1627
|
+
i: true,
|
|
1628
|
+
em: true,
|
|
1629
|
+
u: { class: true },
|
|
1630
|
+
s: { class: true },
|
|
1631
|
+
mark: { class: true },
|
|
1632
|
+
code: { class: true },
|
|
1633
|
+
span: { class: true, "data-tooltip": true, "data-mex-math": true },
|
|
1634
|
+
sub: true,
|
|
1635
|
+
sup: true,
|
|
1636
|
+
br: true,
|
|
1637
|
+
a: LINK_SANITIZE
|
|
1638
|
+
};
|
|
1639
|
+
if (field2.type === "object") return sanitizeRulesFor(field2.fields);
|
|
1640
|
+
if (field2.type === "array") return rule(field2.items);
|
|
1641
|
+
return true;
|
|
1642
|
+
};
|
|
1643
|
+
const out = {};
|
|
1644
|
+
for (const [key, field2] of Object.entries(fields)) out[key] = rule(field2);
|
|
1645
|
+
return out;
|
|
1646
|
+
};
|
|
1647
|
+
const mergeCodeBlocks = (server, admin, locale) => {
|
|
1648
|
+
const rows = /* @__PURE__ */ new Map();
|
|
1649
|
+
for (const block of Array.isArray(server) ? server : []) {
|
|
1650
|
+
rows.set(block.name, {
|
|
1651
|
+
name: block.name,
|
|
1652
|
+
title: block.title || block.name,
|
|
1653
|
+
version: block.version,
|
|
1654
|
+
fields: Array.isArray(block.fields) ? block.fields : [],
|
|
1655
|
+
presets: Array.isArray(block.presets) ? block.presets : [],
|
|
1656
|
+
onServer: true,
|
|
1657
|
+
inAdmin: false
|
|
1658
|
+
});
|
|
1659
|
+
}
|
|
1660
|
+
for (const entry of admin) {
|
|
1661
|
+
const { definition } = entry;
|
|
1662
|
+
const current = rows.get(definition.name);
|
|
1663
|
+
const title = localized(entry.title, locale) || definition.title || definition.name;
|
|
1664
|
+
if (current) rows.set(definition.name, { ...current, title, inAdmin: true });
|
|
1665
|
+
else
|
|
1666
|
+
rows.set(definition.name, {
|
|
1667
|
+
name: definition.name,
|
|
1668
|
+
title,
|
|
1669
|
+
version: definition.version,
|
|
1670
|
+
fields: Object.entries(definition.schema).map(([key, field2]) => ({ key, type: field2.type })),
|
|
1671
|
+
presets: [...definition.presets],
|
|
1672
|
+
onServer: false,
|
|
1673
|
+
inAdmin: true
|
|
1674
|
+
});
|
|
1675
|
+
}
|
|
1676
|
+
return [...rows.values()].sort((a, b) => a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
|
|
1677
|
+
};
|
|
1678
|
+
const VOID_TAGS = /* @__PURE__ */ new Set([
|
|
1679
|
+
"area",
|
|
1680
|
+
"base",
|
|
1681
|
+
"br",
|
|
1682
|
+
"col",
|
|
1683
|
+
"embed",
|
|
1684
|
+
"hr",
|
|
1685
|
+
"img",
|
|
1686
|
+
"input",
|
|
1687
|
+
"link",
|
|
1688
|
+
"meta",
|
|
1689
|
+
"source",
|
|
1690
|
+
"track",
|
|
1691
|
+
"wbr"
|
|
1692
|
+
]);
|
|
1693
|
+
const escapeHtml = (text) => String(text ?? "").replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
1694
|
+
const TAG_NAME = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
|
|
1695
|
+
const ATTRIBUTE_NAME = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/;
|
|
1696
|
+
const isSafeAttributeName = (name) => ATTRIBUTE_NAME.test(name) && !name.startsWith("on") && name !== "style" && name !== "srcdoc";
|
|
1697
|
+
const textNode = (value) => ({ type: "text", value: String(value ?? "") });
|
|
1698
|
+
const el$1 = (tag, attrs, children) => {
|
|
1699
|
+
const clean = {};
|
|
1700
|
+
for (const [name, value] of Object.entries(attrs || {})) {
|
|
1701
|
+
if (value === void 0 || value === null || value === false) continue;
|
|
1702
|
+
if (value === "" && name !== "alt") continue;
|
|
1703
|
+
clean[name] = value === true ? true : String(value);
|
|
1704
|
+
}
|
|
1705
|
+
return { type: "element", tag, attrs: clean, children: toNodes(children) };
|
|
1706
|
+
};
|
|
1707
|
+
const toNodes = (children) => {
|
|
1708
|
+
const out = [];
|
|
1709
|
+
const visit = (child) => {
|
|
1710
|
+
if (child === void 0 || child === null || child === false || child === "") return;
|
|
1711
|
+
if (Array.isArray(child)) {
|
|
1712
|
+
for (const item of child) visit(item);
|
|
1713
|
+
} else if (typeof child === "string" || typeof child === "number") {
|
|
1714
|
+
out.push(textNode(child));
|
|
1715
|
+
} else if (typeof child === "object") {
|
|
1716
|
+
out.push(
|
|
1717
|
+
/** @type {RenderNode} */
|
|
1718
|
+
child
|
|
1719
|
+
);
|
|
1720
|
+
}
|
|
1721
|
+
};
|
|
1722
|
+
visit(children);
|
|
1723
|
+
return out;
|
|
1724
|
+
};
|
|
1725
|
+
const STYLE_PROPERTIES = Object.freeze(["object-position"]);
|
|
1726
|
+
const STYLE_VALUE = /^[0-9]{1,3}(?:\.[0-9]{1,2})?% [0-9]{1,3}(?:\.[0-9]{1,2})?%$/;
|
|
1727
|
+
const safeStyle = (style) => style && typeof style === "object" ? Object.entries(style).filter(
|
|
1728
|
+
([name, value]) => STYLE_PROPERTIES.includes(name) && typeof value === "string" && STYLE_VALUE.test(value)
|
|
1729
|
+
) : [];
|
|
1730
|
+
const serializeNodes = (nodes) => {
|
|
1731
|
+
let out = "";
|
|
1732
|
+
for (const node of nodes) {
|
|
1733
|
+
if (!node || typeof node !== "object") continue;
|
|
1734
|
+
if (node.type === "text") out += escapeHtml(node.value);
|
|
1735
|
+
else if (node.type === "raw") out += node.html;
|
|
1736
|
+
else if (node.type === "block") out += serializeNodes(node.children);
|
|
1737
|
+
else if (node.type === "element") {
|
|
1738
|
+
let attributes = "";
|
|
1739
|
+
for (const [name, value] of Object.entries(node.attrs)) {
|
|
1740
|
+
attributes += value === true ? ` ${name}` : ` ${name}="${escapeHtml(value)}"`;
|
|
1741
|
+
}
|
|
1742
|
+
const style = safeStyle(node.style);
|
|
1743
|
+
if (style.length)
|
|
1744
|
+
attributes += ` style="${escapeHtml(style.map(([name, value]) => `${name}:${value}`).join(";"))}"`;
|
|
1745
|
+
out += VOID_TAGS.has(node.tag) ? `<${node.tag}${attributes}>` : `<${node.tag}${attributes}>${serializeNodes(node.children)}</${node.tag}>`;
|
|
1746
|
+
}
|
|
1747
|
+
}
|
|
1748
|
+
return out;
|
|
1749
|
+
};
|
|
1750
|
+
const nodesTextContent = (nodes) => {
|
|
1751
|
+
let out = "";
|
|
1752
|
+
for (const node of nodes) {
|
|
1753
|
+
if (node.type === "text") out += node.value;
|
|
1754
|
+
else if (node.type === "element" || node.type === "block") out += nodesTextContent(node.children);
|
|
1755
|
+
}
|
|
1756
|
+
return out;
|
|
1757
|
+
};
|
|
1758
|
+
const TOKEN_PATTERN = /^[a-z][a-z0-9-]{0,31}$/;
|
|
1759
|
+
const COLOR_ATTRIBUTE = "data-mex-color";
|
|
1760
|
+
const HIGHLIGHT_ATTRIBUTE = "data-mex-highlight";
|
|
1761
|
+
const PALETTE_LIMITS = Object.freeze({ tokens: 24, label: 60, locales: 20 });
|
|
1762
|
+
const HEX = /^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
|
|
1763
|
+
const LOCALE = /^[A-Za-z]{2,8}(?:-[A-Za-z0-9]{1,8}){0,3}$/;
|
|
1764
|
+
const DEFAULT_SURFACE = Object.freeze({
|
|
1765
|
+
background: Object.freeze({ light: "#ffffff", dark: "#121212" }),
|
|
1766
|
+
text: Object.freeze({ light: "#1f1f1f", dark: "#f2f2f2" })
|
|
1767
|
+
});
|
|
1768
|
+
const isHexColor = (value) => typeof value === "string" && HEX.test(value);
|
|
1769
|
+
const longHex = (value) => {
|
|
1770
|
+
const hex = value.slice(1).toLowerCase();
|
|
1771
|
+
return `#${hex.length === 3 ? [...hex].map((c) => c + c).join("") : hex}`;
|
|
1772
|
+
};
|
|
1773
|
+
const isToken = (value) => typeof value === "string" && TOKEN_PATTERN.test(value);
|
|
1774
|
+
const cleanLabel = (value) => {
|
|
1775
|
+
if (typeof value === "string") return value.trim().slice(0, PALETTE_LIMITS.label) || null;
|
|
1776
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
|
|
1777
|
+
const out = {};
|
|
1778
|
+
for (const [locale, text] of Object.entries(value).slice(0, PALETTE_LIMITS.locales))
|
|
1779
|
+
if (LOCALE.test(locale) && typeof text === "string" && text.trim())
|
|
1780
|
+
out[locale] = text.trim().slice(0, PALETTE_LIMITS.label);
|
|
1781
|
+
return Object.keys(out).length ? out : null;
|
|
1782
|
+
};
|
|
1783
|
+
const cleanTokens = (list) => {
|
|
1784
|
+
const out = [];
|
|
1785
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1786
|
+
for (const item of Array.isArray(list) ? list : []) {
|
|
1787
|
+
if (out.length >= PALETTE_LIMITS.tokens) break;
|
|
1788
|
+
if (!item || typeof item !== "object" || !isToken(item.name) || seen.has(item.name)) continue;
|
|
1789
|
+
if (!isHexColor(item.light)) continue;
|
|
1790
|
+
seen.add(item.name);
|
|
1791
|
+
out.push({
|
|
1792
|
+
name: item.name,
|
|
1793
|
+
label: cleanLabel(item.label),
|
|
1794
|
+
light: longHex(item.light),
|
|
1795
|
+
dark: longHex(isHexColor(item.dark) ? item.dark : item.light)
|
|
1796
|
+
});
|
|
1797
|
+
}
|
|
1798
|
+
return out;
|
|
1799
|
+
};
|
|
1800
|
+
const cleanPair = (value, fallback) => {
|
|
1801
|
+
const source = value && typeof value === "object" ? value : {};
|
|
1802
|
+
return {
|
|
1803
|
+
light: isHexColor(source.light) ? longHex(source.light) : fallback.light,
|
|
1804
|
+
dark: isHexColor(source.dark) ? longHex(source.dark) : fallback.dark
|
|
1805
|
+
};
|
|
1806
|
+
};
|
|
1807
|
+
const normalizePalette = (input2) => {
|
|
1808
|
+
const source = input2 && typeof input2 === "object" ? input2 : {};
|
|
1809
|
+
return {
|
|
1810
|
+
background: cleanPair(source.background, DEFAULT_SURFACE.background),
|
|
1811
|
+
text: cleanPair(source.text, DEFAULT_SURFACE.text),
|
|
1812
|
+
colors: cleanTokens(source.colors),
|
|
1813
|
+
highlights: cleanTokens(source.highlights)
|
|
1814
|
+
};
|
|
1815
|
+
};
|
|
1816
|
+
const hasPalette = (palette) => Boolean(
|
|
1817
|
+
palette && (palette.colors && palette.colors.length || palette.highlights && palette.highlights.length)
|
|
1818
|
+
);
|
|
1819
|
+
const tokenLabel = (token, locale) => {
|
|
1820
|
+
const { label } = token;
|
|
1821
|
+
if (typeof label === "string") return label;
|
|
1822
|
+
if (label && typeof label === "object") {
|
|
1823
|
+
const language = typeof locale === "string" ? locale.split("-")[0] : "";
|
|
1824
|
+
return label[locale] || label[language] || label.en || Object.values(label)[0] || token.name;
|
|
1825
|
+
}
|
|
1826
|
+
return token.name;
|
|
1827
|
+
};
|
|
1828
|
+
const colorClass = (token) => `mex-color-${token}`;
|
|
1829
|
+
const highlightClass = (token) => `mex-highlight-${token}`;
|
|
1830
|
+
const paletteCss = (palette, options = {}) => {
|
|
1831
|
+
const selector = typeof options.selector === "function" ? options.selector : (kind, token) => `.${kind === "color" ? colorClass(token) : highlightClass(token)}`;
|
|
1832
|
+
const mode = options.dark || "media";
|
|
1833
|
+
const rules = (variant) => [
|
|
1834
|
+
...palette.colors.map((token) => `${selector("color", token.name)} { color: ${token[variant]}; }`),
|
|
1835
|
+
...palette.highlights.map(
|
|
1836
|
+
(token) => `${selector("highlight", token.name)} { background-color: ${token[variant]}; color: inherit; }`
|
|
1837
|
+
)
|
|
1838
|
+
];
|
|
1839
|
+
if (mode === "only") return rules("dark").join("\n");
|
|
1840
|
+
const light = rules("light").join("\n");
|
|
1841
|
+
if (mode === "none" || !light) return light;
|
|
1842
|
+
return `${light}
|
|
1843
|
+
@media (prefers-color-scheme: dark) {
|
|
1844
|
+
${rules("dark").map((rule) => ` ${rule}`).join("\n")}
|
|
1845
|
+
}`;
|
|
1846
|
+
};
|
|
1847
|
+
const ANNOTATION_ATTRIBUTE = "data-mex-annotation";
|
|
1848
|
+
const ANNOTATION_VALUE_PREFIX = "data-mex-a-";
|
|
1849
|
+
const ANNOTATION_NAME = /^[a-z][a-z0-9-]{1,39}$/;
|
|
1850
|
+
const ANNOTATION_KEY = /^[a-z][a-z0-9]{0,23}$/;
|
|
1851
|
+
const ANNOTATION_FIELD_TYPES = Object.freeze(["text", "enum", "url"]);
|
|
1852
|
+
const ANNOTATION_LIMITS = Object.freeze({
|
|
1853
|
+
attributes: 8,
|
|
1854
|
+
text: 1e3,
|
|
1855
|
+
textDefault: 200,
|
|
1856
|
+
values: 50,
|
|
1857
|
+
registered: 50,
|
|
1858
|
+
label: 100,
|
|
1859
|
+
resolveItems: 200
|
|
1860
|
+
});
|
|
1861
|
+
const ANNOTATION_PRESETS = Object.freeze(["full", "article", "basic", "minimal"]);
|
|
1862
|
+
const ANNOTATION_RENDER_TAGS = Object.freeze([
|
|
1863
|
+
"abbr",
|
|
1864
|
+
"dfn",
|
|
1865
|
+
"span",
|
|
1866
|
+
"a",
|
|
1867
|
+
"mark",
|
|
1868
|
+
"cite",
|
|
1869
|
+
"q",
|
|
1870
|
+
"small",
|
|
1871
|
+
"em",
|
|
1872
|
+
"strong",
|
|
1873
|
+
"b",
|
|
1874
|
+
"i",
|
|
1875
|
+
"u",
|
|
1876
|
+
"s",
|
|
1877
|
+
"time",
|
|
1878
|
+
"data",
|
|
1879
|
+
"bdi",
|
|
1880
|
+
"kbd",
|
|
1881
|
+
"var",
|
|
1882
|
+
"ins",
|
|
1883
|
+
"del"
|
|
1884
|
+
]);
|
|
1885
|
+
const ANNOTATION_SHORTCUTS = Object.freeze(["CMD+SHIFT+0", "CMD+SHIFT+1", "CMD+SHIFT+8"]);
|
|
1886
|
+
const ABBREVIATION = "abbreviation";
|
|
1887
|
+
const isObject = (value) => value !== null && typeof value === "object" && !Array.isArray(value) && (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null);
|
|
1888
|
+
const fail = (name, message, code = "INVALID_DEFINITION") => {
|
|
1889
|
+
throw new BlockDefinitionError(`Magic Editor X inline annotation ${name}: ${message}`, code);
|
|
1890
|
+
};
|
|
1891
|
+
const defined = /* @__PURE__ */ new WeakSet();
|
|
1892
|
+
const cleanText = (value, max) => typeof value === "string" ? value.trim().slice(0, max).trim() : "";
|
|
1893
|
+
const normalizeAttribute = (where, input2) => {
|
|
1894
|
+
if (!isObject(input2)) fail(where, 'an attribute must be an object with a "type"');
|
|
1895
|
+
const { type } = input2;
|
|
1896
|
+
if (!ANNOTATION_FIELD_TYPES.includes(type)) fail(where, `unknown attribute type ${JSON.stringify(type)}`);
|
|
1897
|
+
const allowed = { text: ["max"], enum: ["values"], url: ["schemes", "relative"] }[type];
|
|
1898
|
+
for (const key of Object.keys(input2))
|
|
1899
|
+
if (!["type", "label", "required", ...allowed].includes(key))
|
|
1900
|
+
fail(where, `unknown option "${key}" for a ${type} attribute`);
|
|
1901
|
+
const out = { type, required: input2.required === true };
|
|
1902
|
+
if (input2.label !== void 0) {
|
|
1903
|
+
if (typeof input2.label !== "string" || input2.label.length > ANNOTATION_LIMITS.label)
|
|
1904
|
+
fail(where, `"label" must be a string of at most ${ANNOTATION_LIMITS.label} characters`);
|
|
1905
|
+
out.label = input2.label;
|
|
1906
|
+
}
|
|
1907
|
+
if (input2.required !== void 0 && typeof input2.required !== "boolean")
|
|
1908
|
+
fail(where, '"required" must be a boolean');
|
|
1909
|
+
if (type === "text") {
|
|
1910
|
+
const max = input2.max === void 0 ? ANNOTATION_LIMITS.textDefault : input2.max;
|
|
1911
|
+
if (!Number.isInteger(max) || max < 1 || max > ANNOTATION_LIMITS.text)
|
|
1912
|
+
fail(where, `"max" must be an integer from 1 to ${ANNOTATION_LIMITS.text}`);
|
|
1913
|
+
out.max = max;
|
|
1914
|
+
} else if (type === "enum") {
|
|
1915
|
+
const { values } = input2;
|
|
1916
|
+
if (!Array.isArray(values) || !values.length || values.length > ANNOTATION_LIMITS.values || !values.every((value) => typeof value === "string" && value.length >= 1 && value.length <= 100) || new Set(values).size !== values.length)
|
|
1917
|
+
fail(where, `"values" must be 1 to ${ANNOTATION_LIMITS.values} distinct strings`);
|
|
1918
|
+
out.values = Object.freeze([...values]);
|
|
1919
|
+
} else {
|
|
1920
|
+
const schemes = input2.schemes === void 0 ? ["http", "https"] : input2.schemes;
|
|
1921
|
+
if (!Array.isArray(schemes) || !schemes.length || !schemes.every((scheme) => LINK_SCHEMES.includes(scheme)))
|
|
1922
|
+
fail(where, `"schemes" must be a non-empty subset of ${LINK_SCHEMES.join(", ")}`);
|
|
1923
|
+
if (input2.relative !== void 0 && typeof input2.relative !== "boolean")
|
|
1924
|
+
fail(where, '"relative" must be a boolean');
|
|
1925
|
+
out.schemes = Object.freeze([...new Set(schemes)]);
|
|
1926
|
+
out.relative = input2.relative !== false;
|
|
1927
|
+
}
|
|
1928
|
+
return Object.freeze(out);
|
|
1929
|
+
};
|
|
1930
|
+
const defineInlineAnnotation = (definition) => {
|
|
1931
|
+
if (!isObject(definition)) fail("(unnamed)", "the definition must be an object");
|
|
1932
|
+
const { name } = definition;
|
|
1933
|
+
if (typeof name !== "string" || !ANNOTATION_NAME.test(name))
|
|
1934
|
+
fail(JSON.stringify(name), "the name must match /^[a-z][a-z0-9-]{1,39}$/");
|
|
1935
|
+
for (const key of Object.keys(definition))
|
|
1936
|
+
if (!["name", "label", "description", "attributes", "presets", "resolve", "render", "shortcut"].includes(
|
|
1937
|
+
key
|
|
1938
|
+
))
|
|
1939
|
+
fail(name, `unknown key "${key}"`);
|
|
1940
|
+
for (const key of ["label", "description"])
|
|
1941
|
+
if (definition[key] !== void 0 && (typeof definition[key] !== "string" || definition[key].length > (key === "label" ? 100 : 500)))
|
|
1942
|
+
fail(name, `"${key}" must be a short string`);
|
|
1943
|
+
if (!isObject(definition.attributes)) fail(name, '"attributes" must be an object');
|
|
1944
|
+
const keys = Object.keys(definition.attributes);
|
|
1945
|
+
if (keys.length > ANNOTATION_LIMITS.attributes)
|
|
1946
|
+
fail(name, `at most ${ANNOTATION_LIMITS.attributes} attributes`);
|
|
1947
|
+
const attributes = {};
|
|
1948
|
+
for (const key of keys) {
|
|
1949
|
+
if (!ANNOTATION_KEY.test(key)) fail(name, `attribute key "${key}" must match /^[a-z][a-z0-9]{0,23}$/`);
|
|
1950
|
+
attributes[key] = normalizeAttribute(`${name}.${key}`, definition.attributes[key]);
|
|
1951
|
+
}
|
|
1952
|
+
const presets = definition.presets === void 0 ? [] : definition.presets;
|
|
1953
|
+
if (!Array.isArray(presets) || !presets.every((preset) => ANNOTATION_PRESETS.includes(preset)))
|
|
1954
|
+
fail(name, `"presets" must list only ${ANNOTATION_PRESETS.join(", ")}`);
|
|
1955
|
+
if (definition.shortcut !== void 0 && !ANNOTATION_SHORTCUTS.includes(definition.shortcut))
|
|
1956
|
+
fail(name, `"shortcut" must be one of ${ANNOTATION_SHORTCUTS.join(", ")}`);
|
|
1957
|
+
for (const key of ["resolve", "render"])
|
|
1958
|
+
if (definition[key] !== void 0 && typeof definition[key] !== "function")
|
|
1959
|
+
fail(name, `"${key}" must be a function`);
|
|
1960
|
+
const out = Object.freeze({
|
|
1961
|
+
name,
|
|
1962
|
+
label: definition.label || name,
|
|
1963
|
+
...definition.description ? { description: definition.description } : {},
|
|
1964
|
+
attributes: Object.freeze(attributes),
|
|
1965
|
+
presets: Object.freeze([...new Set(presets)]),
|
|
1966
|
+
...definition.shortcut ? { shortcut: definition.shortcut } : {},
|
|
1967
|
+
...definition.resolve ? { resolve: definition.resolve } : {},
|
|
1968
|
+
...definition.render ? { render: definition.render } : {}
|
|
1969
|
+
});
|
|
1970
|
+
defined.add(out);
|
|
1971
|
+
return out;
|
|
1972
|
+
};
|
|
1973
|
+
const cleanAnnotationAttributes = (definition, raw) => {
|
|
1974
|
+
const source = raw && typeof raw === "object" ? raw : {};
|
|
1975
|
+
const out = {};
|
|
1976
|
+
for (const [key, field2] of Object.entries(definition.attributes)) {
|
|
1977
|
+
const value = Object.prototype.hasOwnProperty.call(source, key) ? source[key] : void 0;
|
|
1978
|
+
let clean;
|
|
1979
|
+
if (field2.type === "text") clean = cleanText(value, field2.max);
|
|
1980
|
+
else if (field2.type === "enum")
|
|
1981
|
+
clean = typeof value === "string" && field2.values.includes(value) ? value : "";
|
|
1982
|
+
else clean = sanitizeUrl(value, { schemes: field2.schemes, allowRelative: field2.relative });
|
|
1983
|
+
if (clean) out[key] = clean;
|
|
1984
|
+
else if (field2.required) return null;
|
|
1985
|
+
}
|
|
1986
|
+
return out;
|
|
1987
|
+
};
|
|
1988
|
+
const abbreviationAnnotation = defineInlineAnnotation({
|
|
1989
|
+
name: ABBREVIATION,
|
|
1990
|
+
label: "Abbreviation",
|
|
1991
|
+
description: "Short form with its full form (shown as a tooltip and read by screen readers).",
|
|
1992
|
+
attributes: { title: { type: "text", max: 200, required: true, label: "Full form" } },
|
|
1993
|
+
render: (annotation) => ({ tag: "abbr", attrs: { title: annotation.attrs.title } })
|
|
1994
|
+
});
|
|
1995
|
+
const registry = /* @__PURE__ */ new Map();
|
|
1996
|
+
const activeAnnotation = (name) => {
|
|
1997
|
+
if (name === ABBREVIATION) return abbreviationAnnotation;
|
|
1998
|
+
return typeof name === "string" && registry.has(name) ? registry.get(name) : null;
|
|
1999
|
+
};
|
|
2000
|
+
const activeAnnotations = () => [abbreviationAnnotation, ...registry.values()];
|
|
2001
|
+
const annotationsFromSummaries = (summaries) => {
|
|
2002
|
+
const out = [];
|
|
2003
|
+
for (const summary of Array.isArray(summaries) ? summaries.slice(0, ANNOTATION_LIMITS.registered) : []) {
|
|
2004
|
+
if (!isObject(summary) || summary.name === ABBREVIATION) continue;
|
|
2005
|
+
try {
|
|
2006
|
+
out.push(
|
|
2007
|
+
defineInlineAnnotation({
|
|
2008
|
+
name: summary.name,
|
|
2009
|
+
label: typeof summary.label === "string" ? summary.label.slice(0, 100) : void 0,
|
|
2010
|
+
attributes: summary.attributes,
|
|
2011
|
+
presets: Array.isArray(summary.presets) ? summary.presets : [],
|
|
2012
|
+
...typeof summary.shortcut === "string" ? { shortcut: summary.shortcut } : {}
|
|
2013
|
+
})
|
|
2014
|
+
);
|
|
2015
|
+
} catch {
|
|
2016
|
+
}
|
|
2017
|
+
}
|
|
2018
|
+
return out;
|
|
2019
|
+
};
|
|
2020
|
+
const CLASS_TOKEN = /^[A-Za-z_][A-Za-z0-9_-]{0,63}$/;
|
|
2021
|
+
const RENDER_ATTRIBUTES = /* @__PURE__ */ new Set(["title", "class", "lang", "dir", "href", "datetime", "value", "cite"]);
|
|
2022
|
+
const cleanAnnotationWrapper = (out) => {
|
|
2023
|
+
if (!isObject(out) || !ANNOTATION_RENDER_TAGS.includes(out.tag)) return null;
|
|
2024
|
+
const attrs = {};
|
|
2025
|
+
for (const [name, raw] of Object.entries(isObject(out.attrs) ? out.attrs : {})) {
|
|
2026
|
+
if (raw === void 0 || raw === null || raw === false) continue;
|
|
2027
|
+
if (!["string", "number"].includes(typeof raw)) continue;
|
|
2028
|
+
const allowed = RENDER_ATTRIBUTES.has(name) || name === "aria-label" || name.startsWith("data-") && !name.startsWith("data-mex-");
|
|
2029
|
+
if (!allowed || !isSafeAttributeName(name)) continue;
|
|
2030
|
+
const text = String(raw).slice(0, 2e3);
|
|
2031
|
+
if (name === "href" || name === "cite") {
|
|
2032
|
+
if (out.tag !== "a" && name === "href") continue;
|
|
2033
|
+
const url = sanitizeUrl(text);
|
|
2034
|
+
if (url) attrs[name] = url;
|
|
2035
|
+
} else if (name === "class") {
|
|
2036
|
+
const classes = text.split(/\s+/).filter((token) => CLASS_TOKEN.test(token));
|
|
2037
|
+
if (classes.length) attrs.class = classes.join(" ");
|
|
2038
|
+
} else if (text) attrs[name] = text;
|
|
2039
|
+
}
|
|
2040
|
+
return { tag: out.tag, attrs };
|
|
2041
|
+
};
|
|
2042
|
+
const DROP_WITH_CONTENT = /* @__PURE__ */ new Set([
|
|
2043
|
+
"script",
|
|
2044
|
+
"style",
|
|
2045
|
+
"textarea",
|
|
2046
|
+
"title",
|
|
2047
|
+
"xmp",
|
|
2048
|
+
"noscript",
|
|
2049
|
+
"noembed",
|
|
2050
|
+
"noframes",
|
|
2051
|
+
"template",
|
|
2052
|
+
"iframe",
|
|
2053
|
+
"object",
|
|
2054
|
+
"embed",
|
|
2055
|
+
"svg",
|
|
2056
|
+
"math",
|
|
2057
|
+
"select",
|
|
2058
|
+
"option",
|
|
2059
|
+
"plaintext"
|
|
2060
|
+
]);
|
|
2061
|
+
const ALLOWED = new Set(INLINE_TAGS);
|
|
2062
|
+
const VOID = /* @__PURE__ */ new Set(["br"]);
|
|
2063
|
+
const MAX_INPUT = 5e5;
|
|
2064
|
+
const MAX_NODES = 5e4;
|
|
2065
|
+
const MAX_DEPTH = 64;
|
|
2066
|
+
const NAMED_ENTITIES = Object.freeze({
|
|
2067
|
+
amp: "&",
|
|
2068
|
+
lt: "<",
|
|
2069
|
+
gt: ">",
|
|
2070
|
+
quot: '"',
|
|
2071
|
+
apos: "'",
|
|
2072
|
+
nbsp: " ",
|
|
2073
|
+
shy: "",
|
|
2074
|
+
copy: "©",
|
|
2075
|
+
reg: "®",
|
|
2076
|
+
trade: "™",
|
|
2077
|
+
hellip: "…",
|
|
2078
|
+
mdash: "—",
|
|
2079
|
+
ndash: "–",
|
|
2080
|
+
lsquo: "‘",
|
|
2081
|
+
rsquo: "’",
|
|
2082
|
+
ldquo: "“",
|
|
2083
|
+
rdquo: "”",
|
|
2084
|
+
laquo: "«",
|
|
2085
|
+
raquo: "»",
|
|
2086
|
+
euro: "€",
|
|
2087
|
+
middot: "·",
|
|
2088
|
+
bull: "•",
|
|
2089
|
+
times: "×",
|
|
2090
|
+
deg: "°",
|
|
2091
|
+
tab: " ",
|
|
2092
|
+
newline: "\n",
|
|
2093
|
+
colon: ":",
|
|
2094
|
+
sol: "/",
|
|
2095
|
+
lpar: "(",
|
|
2096
|
+
rpar: ")"
|
|
2097
|
+
});
|
|
2098
|
+
const decodeEntities = (text) => String(text).replace(
|
|
2099
|
+
/&(#[xX][0-9a-fA-F]{1,8}|#[0-9]{1,10}|[A-Za-z][A-Za-z0-9]{1,31});?/g,
|
|
2100
|
+
(match, body) => {
|
|
2101
|
+
if (body[0] === "#") {
|
|
2102
|
+
const code = body[1] === "x" || body[1] === "X" ? parseInt(body.slice(2), 16) : parseInt(body.slice(1), 10);
|
|
2103
|
+
if (!Number.isFinite(code) || code <= 0 || code > 1114111 || code >= 55296 && code <= 57343)
|
|
2104
|
+
return "�";
|
|
2105
|
+
return String.fromCodePoint(code);
|
|
2106
|
+
}
|
|
2107
|
+
const named = NAMED_ENTITIES[body] ?? NAMED_ENTITIES[body.toLowerCase()];
|
|
2108
|
+
return named === void 0 ? match : named;
|
|
2109
|
+
}
|
|
2110
|
+
);
|
|
2111
|
+
const parseAttributes = (source) => {
|
|
2112
|
+
const out = {};
|
|
2113
|
+
const pattern = /([^\s"'<>/=]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/g;
|
|
2114
|
+
let match;
|
|
2115
|
+
while (match = pattern.exec(source)) {
|
|
2116
|
+
const name = match[1].toLowerCase();
|
|
2117
|
+
if (Object.prototype.hasOwnProperty.call(out, name)) continue;
|
|
2118
|
+
const raw = match[2] ?? match[3] ?? match[4] ?? "";
|
|
2119
|
+
out[name] = decodeEntities(raw);
|
|
2120
|
+
}
|
|
2121
|
+
return out;
|
|
2122
|
+
};
|
|
2123
|
+
const findTagEnd = (html, from) => {
|
|
2124
|
+
let quote = "";
|
|
2125
|
+
for (let i = from; i < html.length; i += 1) {
|
|
2126
|
+
const char = html[i];
|
|
2127
|
+
if (quote) {
|
|
2128
|
+
if (char === quote) quote = "";
|
|
2129
|
+
} else if (char === '"' || char === "'") quote = char;
|
|
2130
|
+
else if (char === ">") return i;
|
|
2131
|
+
}
|
|
2132
|
+
return -1;
|
|
2133
|
+
};
|
|
2134
|
+
const cleanAttributes = (tag, attribs, options = {}) => {
|
|
2135
|
+
const allowed = INLINE_ATTRIBUTES[tag] || [];
|
|
2136
|
+
const out = {};
|
|
2137
|
+
for (const name of allowed) {
|
|
2138
|
+
if (!Object.prototype.hasOwnProperty.call(attribs, name) || name.startsWith("data-mex-")) continue;
|
|
2139
|
+
const value = attribs[name];
|
|
2140
|
+
if (name === "href") {
|
|
2141
|
+
const url = sanitizeUrl(value);
|
|
2142
|
+
if (url) out.href = url;
|
|
2143
|
+
} else if (name === "target") {
|
|
2144
|
+
const target = sanitizeTarget(value);
|
|
2145
|
+
if (target) out.target = target;
|
|
2146
|
+
} else if (name === "rel") {
|
|
2147
|
+
const rel = String(value).split(/\s+/).filter((token) => /^[a-z-]{1,20}$/.test(token));
|
|
2148
|
+
if (rel.length) out.rel = rel.join(" ");
|
|
2149
|
+
} else if (name === "class") {
|
|
2150
|
+
const classes = String(value).split(/\s+/).filter((token) => (INLINE_CLASSES[tag] || []).includes(token));
|
|
2151
|
+
if (classes.length) out.class = classes.join(" ");
|
|
2152
|
+
} else {
|
|
2153
|
+
out[name] = String(value).slice(0, 2e3);
|
|
2154
|
+
}
|
|
2155
|
+
}
|
|
2156
|
+
if (tag === "a" && !options.inNote) Object.assign(out, cleanInternalLinkAttributes(attribs));
|
|
2157
|
+
return out;
|
|
2158
|
+
};
|
|
2159
|
+
const MAX_MARKER_TEXT = 16;
|
|
2160
|
+
const isMathNode = (node) => node.type === "element" && node.tag === "span" && node.attrs[MATH_ATTRIBUTE] === "true";
|
|
2161
|
+
const isFootnoteNode = (node) => node.type === "element" && node.tag === "sup" && typeof node.attrs["data-mex-footnote"] === "string";
|
|
2162
|
+
const isColorNode = (node) => node.type === "element" && node.tag === "span" && typeof node.attrs[COLOR_ATTRIBUTE] === "string";
|
|
2163
|
+
const isHighlightNode = (node) => node.type === "element" && node.tag === "mark" && typeof node.attrs[HIGHLIGHT_ATTRIBUTE] === "string";
|
|
2164
|
+
const isAnnotationNode = (node) => node.type === "element" && node.tag === "span" && typeof node.attrs[ANNOTATION_ATTRIBUTE] === "string";
|
|
2165
|
+
const annotationOf = (node) => {
|
|
2166
|
+
const attrs = {};
|
|
2167
|
+
for (const [name, value] of Object.entries(node.attrs))
|
|
2168
|
+
if (name.startsWith(ANNOTATION_VALUE_PREFIX)) attrs[name.slice(ANNOTATION_VALUE_PREFIX.length)] = value;
|
|
2169
|
+
return { name: node.attrs[ANNOTATION_ATTRIBUTE], attrs };
|
|
2170
|
+
};
|
|
2171
|
+
const annotationAttributes = (name, values) => {
|
|
2172
|
+
const out = { [ANNOTATION_ATTRIBUTE]: name };
|
|
2173
|
+
for (const key of Object.keys(values).sort()) out[`${ANNOTATION_VALUE_PREFIX}${key}`] = values[key];
|
|
2174
|
+
return out;
|
|
2175
|
+
};
|
|
2176
|
+
const finalizeSpecial = (nodes, depth = 0) => {
|
|
2177
|
+
for (const node of nodes) {
|
|
2178
|
+
if (node.type !== "element") continue;
|
|
2179
|
+
if (isMathNode(node)) {
|
|
2180
|
+
const tex = nodesToText(node.children, { raw: true }).replace(/\u200b/g, "").slice(0, MAX_INLINE_TEX);
|
|
2181
|
+
node.children = tex ? [{ type: "text", value: tex }] : [];
|
|
2182
|
+
} else if (isFootnoteNode(node)) {
|
|
2183
|
+
const text = nodesToText(node.children, { raw: true }).slice(0, MAX_MARKER_TEXT);
|
|
2184
|
+
node.children = text ? [{ type: "text", value: text }] : [];
|
|
2185
|
+
} else if (depth < MAX_DEPTH) finalizeSpecial(node.children, depth + 1);
|
|
2186
|
+
}
|
|
2187
|
+
return nodes;
|
|
2188
|
+
};
|
|
2189
|
+
const specialElement = (tag, attribs, options) => {
|
|
2190
|
+
if (tag === "span" && Object.prototype.hasOwnProperty.call(attribs, MATH_ATTRIBUTE)) {
|
|
2191
|
+
if (!/^(?:|true)$/.test(attribs[MATH_ATTRIBUTE])) return null;
|
|
2192
|
+
return { attrs: { [MATH_ATTRIBUTE]: "true" } };
|
|
2193
|
+
}
|
|
2194
|
+
if (tag === "span" && Object.prototype.hasOwnProperty.call(attribs, ANNOTATION_ATTRIBUTE)) {
|
|
2195
|
+
const name = attribs[ANNOTATION_ATTRIBUTE];
|
|
2196
|
+
if (!ANNOTATION_NAME.test(name)) return null;
|
|
2197
|
+
const values = {};
|
|
2198
|
+
for (const [attribute, value] of Object.entries(attribs)) {
|
|
2199
|
+
if (!attribute.startsWith(ANNOTATION_VALUE_PREFIX)) continue;
|
|
2200
|
+
const key = attribute.slice(ANNOTATION_VALUE_PREFIX.length);
|
|
2201
|
+
if (!ANNOTATION_KEY.test(key) || Object.keys(values).length >= ANNOTATION_LIMITS.attributes) continue;
|
|
2202
|
+
const text = String(value).slice(0, ANNOTATION_LIMITS.text);
|
|
2203
|
+
if (text) values[key] = text;
|
|
2204
|
+
}
|
|
2205
|
+
return { attrs: annotationAttributes(name, values) };
|
|
2206
|
+
}
|
|
2207
|
+
if (tag === "span" && Object.prototype.hasOwnProperty.call(attribs, COLOR_ATTRIBUTE)) {
|
|
2208
|
+
return TOKEN_PATTERN.test(attribs[COLOR_ATTRIBUTE]) ? { attrs: { [COLOR_ATTRIBUTE]: attribs[COLOR_ATTRIBUTE] } } : null;
|
|
2209
|
+
}
|
|
2210
|
+
if (tag === "mark" && Object.prototype.hasOwnProperty.call(attribs, HIGHLIGHT_ATTRIBUTE)) {
|
|
2211
|
+
return TOKEN_PATTERN.test(attribs[HIGHLIGHT_ATTRIBUTE]) ? { attrs: { [HIGHLIGHT_ATTRIBUTE]: attribs[HIGHLIGHT_ATTRIBUTE] } } : null;
|
|
2212
|
+
}
|
|
2213
|
+
if (tag === "sup" && !options.inNote) {
|
|
2214
|
+
const id = attribs["data-mex-footnote"];
|
|
2215
|
+
if (typeof id !== "string" || !FOOTNOTE_ID.test(id)) return null;
|
|
2216
|
+
const source = typeof attribs["data-mex-note"] === "string" ? attribs["data-mex-note"] : "";
|
|
2217
|
+
const note = parseInline(source.slice(0, MAX_FOOTNOTE_NOTE), { inNote: true });
|
|
2218
|
+
return { attrs: { "data-mex-footnote": id }, note };
|
|
2219
|
+
}
|
|
2220
|
+
return null;
|
|
2221
|
+
};
|
|
2222
|
+
const parseInline = (input2, options = {}) => {
|
|
2223
|
+
const html = typeof input2 === "string" ? input2.slice(0, MAX_INPUT) : "";
|
|
2224
|
+
const root = { type: "element", tag: "#root", attrs: {}, children: [] };
|
|
2225
|
+
const stack = [root];
|
|
2226
|
+
let nodes = 0;
|
|
2227
|
+
let text = "";
|
|
2228
|
+
const flush = () => {
|
|
2229
|
+
if (!text) return;
|
|
2230
|
+
const parent = stack[stack.length - 1];
|
|
2231
|
+
const last = parent.children[parent.children.length - 1];
|
|
2232
|
+
const value = decodeEntities(text);
|
|
2233
|
+
if (last && last.type === "text") last.value += value;
|
|
2234
|
+
else if (nodes < MAX_NODES) {
|
|
2235
|
+
parent.children.push({ type: "text", value });
|
|
2236
|
+
nodes += 1;
|
|
2237
|
+
}
|
|
2238
|
+
text = "";
|
|
2239
|
+
};
|
|
2240
|
+
let i = 0;
|
|
2241
|
+
while (i < html.length) {
|
|
2242
|
+
const lt = html.indexOf("<", i);
|
|
2243
|
+
if (lt === -1) {
|
|
2244
|
+
text += html.slice(i);
|
|
2245
|
+
break;
|
|
2246
|
+
}
|
|
2247
|
+
text += html.slice(i, lt);
|
|
2248
|
+
i = lt;
|
|
2249
|
+
if (html.startsWith("<!--", i)) {
|
|
2250
|
+
const end2 = html.indexOf("-->", i + 4);
|
|
2251
|
+
flush();
|
|
2252
|
+
i = end2 === -1 ? html.length : end2 + 3;
|
|
2253
|
+
continue;
|
|
2254
|
+
}
|
|
2255
|
+
if (html.startsWith("<![CDATA[", i)) {
|
|
2256
|
+
const end2 = html.indexOf("]]>", i + 9);
|
|
2257
|
+
flush();
|
|
2258
|
+
i = end2 === -1 ? html.length : end2 + 3;
|
|
2259
|
+
continue;
|
|
2260
|
+
}
|
|
2261
|
+
if (html[i + 1] === "!" || html[i + 1] === "?") {
|
|
2262
|
+
const end2 = html.indexOf(">", i + 2);
|
|
2263
|
+
flush();
|
|
2264
|
+
i = end2 === -1 ? html.length : end2 + 1;
|
|
2265
|
+
continue;
|
|
2266
|
+
}
|
|
2267
|
+
const closing = html[i + 1] === "/";
|
|
2268
|
+
const nameMatch = /^[A-Za-z][A-Za-z0-9-]{0,40}/.exec(html.slice(i + (closing ? 2 : 1), i + 45));
|
|
2269
|
+
if (!nameMatch) {
|
|
2270
|
+
text += "<";
|
|
2271
|
+
i += 1;
|
|
2272
|
+
continue;
|
|
2273
|
+
}
|
|
2274
|
+
const end = findTagEnd(html, i + 1);
|
|
2275
|
+
if (end === -1) {
|
|
2276
|
+
text += html.slice(i);
|
|
2277
|
+
break;
|
|
2278
|
+
}
|
|
2279
|
+
flush();
|
|
2280
|
+
const tag = nameMatch[0].toLowerCase();
|
|
2281
|
+
const body = html.slice(i + (closing ? 2 : 1) + nameMatch[0].length, end);
|
|
2282
|
+
i = end + 1;
|
|
2283
|
+
if (closing) {
|
|
2284
|
+
if (!ALLOWED.has(tag)) continue;
|
|
2285
|
+
for (let s = stack.length - 1; s > 0; s -= 1) {
|
|
2286
|
+
if (stack[s].tag === tag) {
|
|
2287
|
+
stack.length = s;
|
|
2288
|
+
break;
|
|
2289
|
+
}
|
|
2290
|
+
}
|
|
2291
|
+
continue;
|
|
2292
|
+
}
|
|
2293
|
+
if (DROP_WITH_CONTENT.has(tag)) {
|
|
2294
|
+
const selfClosing = /\/\s*$/.test(body);
|
|
2295
|
+
if (selfClosing) continue;
|
|
2296
|
+
const closeIndex = html.slice(i).search(new RegExp(`</${tag}[\\s/>]`, "i"));
|
|
2297
|
+
if (closeIndex === -1) break;
|
|
2298
|
+
const closeEnd = html.indexOf(">", i + closeIndex);
|
|
2299
|
+
i = closeEnd === -1 ? html.length : closeEnd + 1;
|
|
2300
|
+
continue;
|
|
2301
|
+
}
|
|
2302
|
+
if (!ALLOWED.has(tag) || nodes >= MAX_NODES) continue;
|
|
2303
|
+
const attribs = parseAttributes(body);
|
|
2304
|
+
const special = specialElement(tag, attribs, options);
|
|
2305
|
+
const element = {
|
|
2306
|
+
type: "element",
|
|
2307
|
+
tag,
|
|
2308
|
+
attrs: special ? special.attrs : cleanAttributes(tag, attribs, options),
|
|
2309
|
+
children: []
|
|
2310
|
+
};
|
|
2311
|
+
if (special && special.note) element.note = special.note;
|
|
2312
|
+
stack[stack.length - 1].children.push(element);
|
|
2313
|
+
nodes += 1;
|
|
2314
|
+
if (!VOID.has(tag) && stack.length < MAX_DEPTH) stack.push(element);
|
|
2315
|
+
}
|
|
2316
|
+
flush();
|
|
2317
|
+
return finalizeSpecial(root.children);
|
|
2318
|
+
};
|
|
2319
|
+
const resolveLinkAttributes = (attrs, resolver) => {
|
|
2320
|
+
if (typeof resolver !== "function" || !attrs["data-mex-uid"] || !attrs["data-mex-document-id"])
|
|
2321
|
+
return attrs;
|
|
2322
|
+
let result;
|
|
2323
|
+
try {
|
|
2324
|
+
result = resolver({
|
|
2325
|
+
uid: attrs["data-mex-uid"],
|
|
2326
|
+
documentId: attrs["data-mex-document-id"],
|
|
2327
|
+
locale: attrs["data-mex-locale"] || null,
|
|
2328
|
+
href: attrs.href || null,
|
|
2329
|
+
...isMentionLink(attrs) ? { mention: true } : {}
|
|
2330
|
+
});
|
|
2331
|
+
} catch {
|
|
2332
|
+
result = void 0;
|
|
2333
|
+
}
|
|
2334
|
+
if (result === false) return null;
|
|
2335
|
+
if (typeof result === "string") {
|
|
2336
|
+
const url = sanitizeUrl(result);
|
|
2337
|
+
if (url) return { ...attrs, href: url };
|
|
2338
|
+
}
|
|
2339
|
+
return attrs;
|
|
2340
|
+
};
|
|
2341
|
+
const ATTRIBUTE_ORDER = [
|
|
2342
|
+
"href",
|
|
2343
|
+
"target",
|
|
2344
|
+
"rel",
|
|
2345
|
+
"title",
|
|
2346
|
+
"class",
|
|
2347
|
+
"data-tooltip",
|
|
2348
|
+
"data-mex-uid",
|
|
2349
|
+
"data-mex-document-id",
|
|
2350
|
+
"data-mex-locale",
|
|
2351
|
+
"data-mex-mention",
|
|
2352
|
+
MATH_ATTRIBUTE,
|
|
2353
|
+
"data-mex-footnote",
|
|
2354
|
+
"data-mex-note"
|
|
2355
|
+
];
|
|
2356
|
+
const RENDER_STATE = Symbol.for("magic-editor-x.render-state");
|
|
2357
|
+
const storedNote = (note) => serializeNodes(inlineNodesToRender(note || [], { storage: true }));
|
|
2358
|
+
const MENTION_CLASS = "mex-mention";
|
|
2359
|
+
const inlineNodesToRender = (nodes, options = {}) => {
|
|
2360
|
+
const out = [];
|
|
2361
|
+
const state = options[RENDER_STATE];
|
|
2362
|
+
for (const node of nodes) {
|
|
2363
|
+
if (node.type === "text") {
|
|
2364
|
+
out.push({ type: "text", value: node.value });
|
|
2365
|
+
continue;
|
|
2366
|
+
}
|
|
2367
|
+
if (node.tag === "br") {
|
|
2368
|
+
out.push({ type: "element", tag: "br", attrs: {}, children: [] });
|
|
2369
|
+
continue;
|
|
2370
|
+
}
|
|
2371
|
+
if (isMathNode(node)) {
|
|
2372
|
+
const tex = nodesToText(node.children);
|
|
2373
|
+
if (state && !options.storage) {
|
|
2374
|
+
if (tex.trim()) out.push(state.math(tex, false));
|
|
2375
|
+
continue;
|
|
2376
|
+
}
|
|
2377
|
+
out.push({
|
|
2378
|
+
type: "element",
|
|
2379
|
+
tag: "span",
|
|
2380
|
+
attrs: { [MATH_ATTRIBUTE]: "true" },
|
|
2381
|
+
children: tex ? [{ type: "text", value: tex }] : []
|
|
2382
|
+
});
|
|
2383
|
+
continue;
|
|
2384
|
+
}
|
|
2385
|
+
if (isFootnoteNode(node)) {
|
|
2386
|
+
if (state && !options.storage) {
|
|
2387
|
+
out.push(state.footnote(node));
|
|
2388
|
+
continue;
|
|
2389
|
+
}
|
|
2390
|
+
const attrs2 = { "data-mex-footnote": node.attrs["data-mex-footnote"] };
|
|
2391
|
+
const note = storedNote(node.note);
|
|
2392
|
+
if (note) attrs2["data-mex-note"] = note;
|
|
2393
|
+
out.push({ type: "element", tag: "sup", attrs: attrs2, children: inlineNodesToRender(node.children, options) });
|
|
2394
|
+
continue;
|
|
2395
|
+
}
|
|
2396
|
+
if (isColorNode(node) || isHighlightNode(node)) {
|
|
2397
|
+
const color = node.tag === "span";
|
|
2398
|
+
const name = color ? COLOR_ATTRIBUTE : HIGHLIGHT_ATTRIBUTE;
|
|
2399
|
+
const token = node.attrs[name];
|
|
2400
|
+
out.push({
|
|
2401
|
+
type: "element",
|
|
2402
|
+
tag: node.tag,
|
|
2403
|
+
attrs: options.storage ? { [name]: token } : { class: color ? colorClass(token) : highlightClass(token), [name]: token },
|
|
2404
|
+
children: inlineNodesToRender(node.children, options)
|
|
2405
|
+
});
|
|
2406
|
+
continue;
|
|
2407
|
+
}
|
|
2408
|
+
if (isAnnotationNode(node)) {
|
|
2409
|
+
const children = inlineNodesToRender(node.children, options);
|
|
2410
|
+
if (options.storage) out.push({ type: "element", tag: "span", attrs: { ...node.attrs }, children });
|
|
2411
|
+
else out.push(...renderAnnotation(node, children, options));
|
|
2412
|
+
continue;
|
|
2413
|
+
}
|
|
2414
|
+
let attrs = node.attrs;
|
|
2415
|
+
if (node.tag === "a") {
|
|
2416
|
+
attrs = resolveLinkAttributes(attrs, options.links);
|
|
2417
|
+
if (!attrs || !attrs.href) {
|
|
2418
|
+
out.push(...inlineNodesToRender(node.children, options));
|
|
2419
|
+
continue;
|
|
2420
|
+
}
|
|
2421
|
+
if (attrs.target === "_blank") {
|
|
2422
|
+
const rel = new Set(
|
|
2423
|
+
String(attrs.rel || "").split(/\s+/).filter(Boolean)
|
|
2424
|
+
);
|
|
2425
|
+
rel.add("noopener");
|
|
2426
|
+
rel.add("noreferrer");
|
|
2427
|
+
attrs = { ...attrs, rel: Array.from(rel).sort().join(" ") };
|
|
2428
|
+
}
|
|
2429
|
+
if (!options.storage && isMentionLink(attrs)) attrs = { ...attrs, class: MENTION_CLASS };
|
|
2430
|
+
}
|
|
2431
|
+
const ordered = {};
|
|
2432
|
+
for (const name of ATTRIBUTE_ORDER) {
|
|
2433
|
+
if (attrs[name] !== void 0 && attrs[name] !== "") ordered[name] = String(attrs[name]);
|
|
2434
|
+
}
|
|
2435
|
+
out.push({
|
|
2436
|
+
type: "element",
|
|
2437
|
+
tag: node.tag,
|
|
2438
|
+
attrs: ordered,
|
|
2439
|
+
children: inlineNodesToRender(node.children, options)
|
|
2440
|
+
});
|
|
2441
|
+
}
|
|
2442
|
+
return out;
|
|
2443
|
+
};
|
|
2444
|
+
const annotationLists = /* @__PURE__ */ new WeakMap();
|
|
2445
|
+
const annotationDefinition = (name, options) => {
|
|
2446
|
+
const list = options.annotations;
|
|
2447
|
+
if (Array.isArray(list)) {
|
|
2448
|
+
if (!annotationLists.has(list)) {
|
|
2449
|
+
const map = /* @__PURE__ */ new Map();
|
|
2450
|
+
for (const item of list.slice(0, ANNOTATION_LIMITS.registered))
|
|
2451
|
+
if (item && typeof item === "object" && typeof item.name === "string" && item.attributes)
|
|
2452
|
+
map.set(item.name, item);
|
|
2453
|
+
annotationLists.set(list, map);
|
|
2454
|
+
}
|
|
2455
|
+
const found = annotationLists.get(list).get(name);
|
|
2456
|
+
if (found) return found;
|
|
2457
|
+
}
|
|
2458
|
+
return activeAnnotation(name);
|
|
2459
|
+
};
|
|
2460
|
+
const resolvedValue = (options, annotation) => {
|
|
2461
|
+
if (typeof options.resolveAnnotation !== "function") return void 0;
|
|
2462
|
+
let value;
|
|
2463
|
+
try {
|
|
2464
|
+
value = options.resolveAnnotation(annotation);
|
|
2465
|
+
} catch {
|
|
2466
|
+
return void 0;
|
|
2467
|
+
}
|
|
2468
|
+
if (typeof value === "string") return value.slice(0, 2e3);
|
|
2469
|
+
if (value && typeof value === "object" && !Array.isArray(value)) return value;
|
|
2470
|
+
return void 0;
|
|
2471
|
+
};
|
|
2472
|
+
const renderAnnotation = (node, children, options) => {
|
|
2473
|
+
const { name, attrs } = annotationOf(node);
|
|
2474
|
+
const definition = annotationDefinition(name, options);
|
|
2475
|
+
const values = definition ? cleanAnnotationAttributes(definition, attrs) : attrs;
|
|
2476
|
+
if (!values) return children;
|
|
2477
|
+
if (definition && typeof definition.render === "function") {
|
|
2478
|
+
const text = nodesToText(node.children);
|
|
2479
|
+
let out;
|
|
2480
|
+
try {
|
|
2481
|
+
out = definition.render({
|
|
2482
|
+
name,
|
|
2483
|
+
attrs: { ...values },
|
|
2484
|
+
text,
|
|
2485
|
+
resolved: resolvedValue(options, { name, attrs: { ...values }, text })
|
|
2486
|
+
});
|
|
2487
|
+
} catch {
|
|
2488
|
+
out = void 0;
|
|
2489
|
+
}
|
|
2490
|
+
if (out === false) return children;
|
|
2491
|
+
const wrapper = cleanAnnotationWrapper(out);
|
|
2492
|
+
if (wrapper) return [{ type: "element", tag: wrapper.tag, attrs: wrapper.attrs, children }];
|
|
2493
|
+
}
|
|
2494
|
+
return [
|
|
2495
|
+
{
|
|
2496
|
+
type: "element",
|
|
2497
|
+
tag: "span",
|
|
2498
|
+
attrs: {
|
|
2499
|
+
class: `mex-annotation mex-annotation-${name}`,
|
|
2500
|
+
...annotationAttributes(name, definition ? values : {})
|
|
2501
|
+
},
|
|
2502
|
+
children
|
|
2503
|
+
}
|
|
2504
|
+
];
|
|
2505
|
+
};
|
|
2506
|
+
const inlineToNodes = (html, options) => inlineNodesToRender(parseInline(html), options);
|
|
2507
|
+
const nodesToHtml = (nodes, options = {}) => serializeNodes(inlineNodesToRender(nodes, options));
|
|
2508
|
+
const nodesToText = (nodes, options = {}) => nodes.map((node) => {
|
|
2509
|
+
if (node.type === "text") return node.value;
|
|
2510
|
+
if (node.tag === "br") return "\n";
|
|
2511
|
+
if (!options.raw && isFootnoteNode(node))
|
|
2512
|
+
return typeof options.footnote === "function" ? options.footnote(node) : "";
|
|
2513
|
+
return nodesToText(node.children, options);
|
|
2514
|
+
}).join("");
|
|
2515
|
+
const inlineToHtml = (html, options) => nodesToHtml(parseInline(html), options);
|
|
2516
|
+
const inlineToText = (html, options) => nodesToText(parseInline(html), options).replace(/\u00a0/g, " ");
|
|
2517
|
+
const mapFootnoteNotes = (html, fn) => {
|
|
2518
|
+
if (typeof html !== "string" || !html.includes("data-mex-footnote")) return html;
|
|
2519
|
+
let changed = false;
|
|
2520
|
+
const visit = (nodes2, depth = 0) => {
|
|
2521
|
+
if (depth > MAX_DEPTH) return;
|
|
2522
|
+
for (const node of nodes2) {
|
|
2523
|
+
if (node.type !== "element") continue;
|
|
2524
|
+
if (isFootnoteNode(node)) {
|
|
2525
|
+
const before = storedNote(node.note);
|
|
2526
|
+
const after = fn(before);
|
|
2527
|
+
if (after !== before) {
|
|
2528
|
+
node.note = parseInline(String(after || ""), { inNote: true });
|
|
2529
|
+
changed = true;
|
|
2530
|
+
}
|
|
2531
|
+
} else visit(node.children, depth + 1);
|
|
2532
|
+
}
|
|
2533
|
+
};
|
|
2534
|
+
const nodes = parseInline(html);
|
|
2535
|
+
visit(nodes);
|
|
2536
|
+
return changed ? nodesToHtml(nodes, { storage: true }) : html;
|
|
2537
|
+
};
|
|
2538
|
+
const SPECIAL_MARKERS = [
|
|
2539
|
+
MATH_ATTRIBUTE,
|
|
2540
|
+
"data-mex-footnote",
|
|
2541
|
+
COLOR_ATTRIBUTE,
|
|
2542
|
+
HIGHLIGHT_ATTRIBUTE,
|
|
2543
|
+
ANNOTATION_ATTRIBUTE
|
|
2544
|
+
];
|
|
2545
|
+
const isSpecialNode = (node) => isMathNode(node) || isFootnoteNode(node) || isColorNode(node) || isHighlightNode(node) || isAnnotationNode(node);
|
|
2546
|
+
const canonicalInlineExtras = (html, options = {}) => {
|
|
2547
|
+
if (typeof html !== "string") return "";
|
|
2548
|
+
const plain2 = Array.isArray(options.plainTags) ? options.plainTags : [];
|
|
2549
|
+
const special = SPECIAL_MARKERS.some((marker) => html.includes(marker));
|
|
2550
|
+
const restricted = plain2.some((tag) => html.includes(`<${tag}`));
|
|
2551
|
+
if (!special && !restricted) return html;
|
|
2552
|
+
const colors = Array.isArray(options.colors) ? new Set(options.colors) : null;
|
|
2553
|
+
const highlights = Array.isArray(options.highlights) ? new Set(options.highlights) : null;
|
|
2554
|
+
let annotations = null;
|
|
2555
|
+
if (options.annotations instanceof Map) annotations = options.annotations;
|
|
2556
|
+
else if (Array.isArray(options.annotations))
|
|
2557
|
+
annotations = new Map(options.annotations.filter(Boolean).map((item) => [item.name, item]));
|
|
2558
|
+
const nodes = parseInline(html);
|
|
2559
|
+
const filter = (list, depth = 0) => {
|
|
2560
|
+
const out = [];
|
|
2561
|
+
for (const node of list) {
|
|
2562
|
+
if (node.type !== "element") {
|
|
2563
|
+
out.push(node);
|
|
2564
|
+
continue;
|
|
2565
|
+
}
|
|
2566
|
+
if (options.footnotes === false && isFootnoteNode(node)) continue;
|
|
2567
|
+
if (depth < MAX_DEPTH) node.children = filter(node.children, depth + 1);
|
|
2568
|
+
if (node.note) node.note = filter(node.note, depth + 1);
|
|
2569
|
+
let keep = true;
|
|
2570
|
+
if (isColorNode(node)) keep = !colors || colors.has(node.attrs[COLOR_ATTRIBUTE]);
|
|
2571
|
+
else if (isHighlightNode(node)) keep = !highlights || highlights.has(node.attrs[HIGHLIGHT_ATTRIBUTE]);
|
|
2572
|
+
else if (isAnnotationNode(node) && annotations) {
|
|
2573
|
+
const { name, attrs } = annotationOf(node);
|
|
2574
|
+
const definition = annotations.get(name);
|
|
2575
|
+
const values = definition ? cleanAnnotationAttributes(definition, attrs) : null;
|
|
2576
|
+
if (values) node.attrs = annotationAttributes(name, values);
|
|
2577
|
+
else keep = false;
|
|
2578
|
+
} else if (!isSpecialNode(node) && plain2.includes(node.tag)) keep = false;
|
|
2579
|
+
if (keep) out.push(node);
|
|
2580
|
+
else out.push(...node.children);
|
|
2581
|
+
}
|
|
2582
|
+
return out;
|
|
2583
|
+
};
|
|
2584
|
+
return nodesToHtml(filter(nodes), { storage: true });
|
|
2585
|
+
};
|
|
2586
|
+
const walkInlineElements = (nodes, fn, inNote = false, depth = 0) => {
|
|
2587
|
+
if (depth > MAX_DEPTH) return;
|
|
2588
|
+
for (const node of nodes) {
|
|
2589
|
+
if (node.type !== "element") continue;
|
|
2590
|
+
fn(node, inNote);
|
|
2591
|
+
walkInlineElements(node.children, fn, inNote, depth + 1);
|
|
2592
|
+
if (node.note) walkInlineElements(node.note, fn, true, depth + 1);
|
|
2593
|
+
}
|
|
2594
|
+
};
|
|
2595
|
+
let purify = null;
|
|
2596
|
+
const instance = () => {
|
|
2597
|
+
if (purify) return purify;
|
|
2598
|
+
purify = createDOMPurify(window);
|
|
2599
|
+
purify.addHook("uponSanitizeAttribute", (node, data) => {
|
|
2600
|
+
const tag = node.nodeName.toLowerCase();
|
|
2601
|
+
if (tag === "span" && ANNOTATION_VALUE_ATTRIBUTE.test(data.attrName) && typeof node.hasAttribute === "function" && node.hasAttribute(ANNOTATION_ATTRIBUTE)) {
|
|
2602
|
+
data.forceKeepAttr = true;
|
|
2603
|
+
return;
|
|
2604
|
+
}
|
|
2605
|
+
if (data.attrName === "class") {
|
|
2606
|
+
const allowed = INLINE_CLASSES[tag] || [];
|
|
2607
|
+
const kept = String(data.attrValue).split(/\s+/).filter((name) => allowed.includes(name));
|
|
2608
|
+
if (kept.length === 0) data.keepAttr = false;
|
|
2609
|
+
else data.attrValue = kept.join(" ");
|
|
2610
|
+
}
|
|
2611
|
+
if (data.attrName === "href") {
|
|
2612
|
+
const safe = sanitizeUrl(data.attrValue);
|
|
2613
|
+
if (!safe) data.keepAttr = false;
|
|
2614
|
+
else data.attrValue = safe;
|
|
2615
|
+
}
|
|
2616
|
+
if (data.attrName === "target" && !["_blank", "_self", "_parent", "_top"].includes(data.attrValue))
|
|
2617
|
+
data.keepAttr = false;
|
|
2618
|
+
});
|
|
2619
|
+
purify.addHook("afterSanitizeAttributes", (node) => {
|
|
2620
|
+
if (node.nodeName === "A" && node.getAttribute("target") === "_blank")
|
|
2621
|
+
node.setAttribute("rel", "noopener noreferrer");
|
|
2622
|
+
if (typeof node.hasAttribute !== "function") return;
|
|
2623
|
+
if (!INTERNAL_LINK_ATTRIBUTES.some((name) => node.hasAttribute(name))) return;
|
|
2624
|
+
const kept = node.nodeName === "A" ? cleanInternalLinkAttributes(
|
|
2625
|
+
Object.fromEntries(INTERNAL_LINK_ATTRIBUTES.map((name) => [name, node.getAttribute(name)]))
|
|
2626
|
+
) : {};
|
|
2627
|
+
for (const name of INTERNAL_LINK_ATTRIBUTES) {
|
|
2628
|
+
if (kept[name]) node.setAttribute(name, kept[name]);
|
|
2629
|
+
else node.removeAttribute(name);
|
|
2630
|
+
}
|
|
2631
|
+
});
|
|
2632
|
+
return purify;
|
|
2633
|
+
};
|
|
2634
|
+
const ALLOWED_ATTR = Array.from(new Set(Object.values(INLINE_ATTRIBUTES).flat())).filter(
|
|
2635
|
+
(name) => !name.endsWith("*")
|
|
2636
|
+
);
|
|
2637
|
+
const ANNOTATION_VALUE_ATTRIBUTE = /^data-mex-a-[a-z][a-z0-9]{0,23}$/;
|
|
2638
|
+
const sanitizeInline = (html) => canonicalInlineExtras(
|
|
2639
|
+
instance().sanitize(String(html || ""), {
|
|
2640
|
+
ALLOWED_TAGS: [...INLINE_TAGS],
|
|
2641
|
+
ALLOWED_ATTR,
|
|
2642
|
+
ALLOW_DATA_ATTR: false,
|
|
2643
|
+
// DOMPurify checks attribute values like URIs; `api::article.article` looks
|
|
2644
|
+
// like an unknown scheme and was dropped, which removed the entry reference
|
|
2645
|
+
// of internal links whenever content was shown again. The values are
|
|
2646
|
+
// validated by cleanInternalLinkAttributes in the hook above, and math and
|
|
2647
|
+
// footnote attributes (2.4: a note is inline HTML, "See: …" looks like a
|
|
2648
|
+
// scheme too) by `canonicalInlineExtras` below.
|
|
2649
|
+
ADD_URI_SAFE_ATTR: [
|
|
2650
|
+
...INTERNAL_LINK_ATTRIBUTES,
|
|
2651
|
+
MATH_ATTRIBUTE,
|
|
2652
|
+
...FOOTNOTE_ATTRIBUTES,
|
|
2653
|
+
// 2.5: tokens and the annotation name (validated by `canonicalInlineExtras`).
|
|
2654
|
+
COLOR_ATTRIBUTE,
|
|
2655
|
+
HIGHLIGHT_ATTRIBUTE,
|
|
2656
|
+
ANNOTATION_ATTRIBUTE
|
|
2657
|
+
]
|
|
2658
|
+
})
|
|
2659
|
+
);
|
|
2660
|
+
const sanitizeTemplate = (html) => instance().sanitize(String(html || ""), {
|
|
2661
|
+
ALLOWED_TAGS: [
|
|
2662
|
+
"div",
|
|
2663
|
+
"span",
|
|
2664
|
+
"p",
|
|
2665
|
+
"h2",
|
|
2666
|
+
"h3",
|
|
2667
|
+
"h4",
|
|
2668
|
+
"strong",
|
|
2669
|
+
"b",
|
|
2670
|
+
"em",
|
|
2671
|
+
"i",
|
|
2672
|
+
"u",
|
|
2673
|
+
"s",
|
|
2674
|
+
"small",
|
|
2675
|
+
"br",
|
|
2676
|
+
"hr",
|
|
2677
|
+
"ul",
|
|
2678
|
+
"ol",
|
|
2679
|
+
"li",
|
|
2680
|
+
"blockquote",
|
|
2681
|
+
"figure",
|
|
2682
|
+
"figcaption",
|
|
2683
|
+
"img",
|
|
2684
|
+
"a",
|
|
2685
|
+
"section",
|
|
2686
|
+
"header",
|
|
2687
|
+
"footer",
|
|
2688
|
+
"code",
|
|
2689
|
+
"pre",
|
|
2690
|
+
"mark"
|
|
2691
|
+
],
|
|
2692
|
+
ALLOWED_ATTR: [
|
|
2693
|
+
"class",
|
|
2694
|
+
"data-field",
|
|
2695
|
+
"title",
|
|
2696
|
+
"href",
|
|
2697
|
+
"target",
|
|
2698
|
+
"rel",
|
|
2699
|
+
"src",
|
|
2700
|
+
"alt",
|
|
2701
|
+
"width",
|
|
2702
|
+
"height",
|
|
2703
|
+
"loading"
|
|
2704
|
+
],
|
|
2705
|
+
ALLOW_DATA_ATTR: false
|
|
2706
|
+
});
|
|
2707
|
+
const sanitizeSvg = (svg) => instance().sanitize(String(svg || ""), {
|
|
2708
|
+
USE_PROFILES: { svg: true, svgFilters: false },
|
|
2709
|
+
FORBID_TAGS: ["style", "use", "image", "foreignObject", "a", "script"],
|
|
2710
|
+
FORBID_ATTR: ["style", "href", "xlink:href"]
|
|
2711
|
+
});
|
|
2712
|
+
const prepareEditorValue = (value, rawHtml = "allow") => {
|
|
2713
|
+
try {
|
|
2714
|
+
const data = normalizeEditorData(value);
|
|
2715
|
+
if (!data) return { data: null, error: null };
|
|
2716
|
+
return {
|
|
2717
|
+
data: sanitizeEditorData(data, {
|
|
2718
|
+
sanitizeInlineHtml: sanitizeInline,
|
|
2719
|
+
sanitizeRawHtml: (html) => html,
|
|
2720
|
+
rawHtml
|
|
2721
|
+
}),
|
|
2722
|
+
error: null
|
|
2723
|
+
};
|
|
2724
|
+
} catch (error) {
|
|
2725
|
+
return { data: null, error: error && error.code || "INVALID" };
|
|
2726
|
+
}
|
|
2727
|
+
};
|
|
2728
|
+
const prepareRemoteData = (data, cache) => {
|
|
2729
|
+
const blocks = [];
|
|
2730
|
+
for (const block of data && data.blocks || []) {
|
|
2731
|
+
let clean = cache.get(block);
|
|
2732
|
+
if (clean === void 0) {
|
|
2733
|
+
const prepared = prepareEditorValue({ time: data.time, version: data.version, blocks: [block] }).data;
|
|
2734
|
+
clean = prepared && prepared.blocks.length === 1 ? prepared.blocks[0] : null;
|
|
2735
|
+
cache.set(block, clean);
|
|
2736
|
+
}
|
|
2737
|
+
if (clean) blocks.push(clean);
|
|
2738
|
+
}
|
|
2739
|
+
return { time: data && data.time || Date.now(), version: data && data.version || "2.31.7", blocks };
|
|
2740
|
+
};
|
|
2741
|
+
const el = (tag, options = {}) => {
|
|
2742
|
+
const node = document.createElement(tag);
|
|
2743
|
+
const classes = Array.isArray(options.className) ? options.className : [options.className];
|
|
2744
|
+
classes.filter(Boolean).forEach((name) => node.classList.add(name));
|
|
2745
|
+
if (options.text !== void 0) node.textContent = options.text;
|
|
2746
|
+
for (const [name, value] of Object.entries(options.attrs || {})) {
|
|
2747
|
+
if (value === false || value === null || value === void 0) continue;
|
|
2748
|
+
node.setAttribute(name, value === true ? "" : String(value));
|
|
2749
|
+
}
|
|
2750
|
+
(options.children || []).forEach((child) => child && node.appendChild(child));
|
|
2751
|
+
return node;
|
|
2752
|
+
};
|
|
2753
|
+
const field = (label, control) => {
|
|
2754
|
+
const id = `mex-${Math.random().toString(36).slice(2, 10)}`;
|
|
2755
|
+
control.id = id;
|
|
2756
|
+
const labelEl = el("label", { className: "mex-tool__label", text: label, attrs: { for: id } });
|
|
2757
|
+
return el("div", { className: "mex-tool__field", children: [labelEl, control] });
|
|
2758
|
+
};
|
|
2759
|
+
const input = ({ value = "", placeholder = "", type = "text", readOnly = false } = {}) => {
|
|
2760
|
+
const node = el("input", {
|
|
2761
|
+
className: "mex-tool__input",
|
|
2762
|
+
attrs: { type, placeholder, readonly: readOnly }
|
|
2763
|
+
});
|
|
2764
|
+
node.value = value;
|
|
2765
|
+
return node;
|
|
2766
|
+
};
|
|
2767
|
+
const select = (options, value) => {
|
|
2768
|
+
const node = el("select", { className: "mex-tool__select" });
|
|
2769
|
+
node.addEventListener("keydown", (event) => {
|
|
2770
|
+
if (event.key === "Enter") event.preventDefault();
|
|
2771
|
+
});
|
|
2772
|
+
for (const option of options) {
|
|
2773
|
+
const opt = el("option", { text: option.label, attrs: { value: option.value } });
|
|
2774
|
+
if (option.value === value) opt.selected = true;
|
|
2775
|
+
node.appendChild(opt);
|
|
2776
|
+
}
|
|
2777
|
+
return node;
|
|
2778
|
+
};
|
|
2779
|
+
const button = (label, options = {}) => {
|
|
2780
|
+
const node = el("button", {
|
|
2781
|
+
className: ["mex-tool__button", `mex-tool__button--${options.variant || "secondary"}`],
|
|
2782
|
+
attrs: { type: "button", "aria-label": options.ariaLabel }
|
|
2783
|
+
});
|
|
2784
|
+
if (options.icon) {
|
|
2785
|
+
const icon = el("span", { className: "mex-tool__button-icon", attrs: { "aria-hidden": "true" } });
|
|
2786
|
+
icon.innerHTML = options.icon;
|
|
2787
|
+
node.appendChild(icon);
|
|
2788
|
+
}
|
|
2789
|
+
if (label) node.appendChild(el("span", { text: label }));
|
|
2790
|
+
return node;
|
|
2791
|
+
};
|
|
2792
|
+
const editable = ({
|
|
2793
|
+
html = "",
|
|
2794
|
+
placeholder = "",
|
|
2795
|
+
readOnly = false,
|
|
2796
|
+
className = "",
|
|
2797
|
+
label = ""
|
|
2798
|
+
} = {}) => {
|
|
2799
|
+
const node = el("div", {
|
|
2800
|
+
className: ["mex-tool__editable", className],
|
|
2801
|
+
attrs: {
|
|
2802
|
+
contenteditable: readOnly ? "false" : "true",
|
|
2803
|
+
"data-placeholder": placeholder,
|
|
2804
|
+
role: readOnly ? null : "textbox",
|
|
2805
|
+
"aria-label": label || placeholder || null
|
|
2806
|
+
}
|
|
2807
|
+
});
|
|
2808
|
+
node.innerHTML = html;
|
|
2809
|
+
return node;
|
|
2810
|
+
};
|
|
2811
|
+
const ICONS = {
|
|
2812
|
+
alert: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M12 8v5m0 3h.01M10.3 3.9 2.8 17a2 2 0 0 0 1.7 3h15a2 2 0 0 0 1.7-3L13.7 3.9a2 2 0 0 0-3.4 0Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>',
|
|
2813
|
+
toggle: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="m9 6 6 6-6 6" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>',
|
|
2814
|
+
code: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="m8 7-5 5 5 5m8-10 5 5-5 5m-3-12-2 14" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>',
|
|
2815
|
+
person: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><circle cx="12" cy="8" r="4" stroke="currentColor" stroke-width="2"/><path d="M4 21a8 8 0 0 1 16 0" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>',
|
|
2816
|
+
image: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><rect x="3" y="4" width="18" height="16" rx="2" stroke="currentColor" stroke-width="2"/><circle cx="9" cy="10" r="2" stroke="currentColor" stroke-width="2"/><path d="m4 18 5-5 4 4 3-3 4 4" stroke="currentColor" stroke-width="2" stroke-linejoin="round"/></svg>',
|
|
2817
|
+
gallery: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><rect x="3" y="3" width="8" height="8" rx="1.5" stroke="currentColor" stroke-width="2"/><rect x="13" y="3" width="8" height="8" rx="1.5" stroke="currentColor" stroke-width="2"/><rect x="3" y="13" width="8" height="8" rx="1.5" stroke="currentColor" stroke-width="2"/><rect x="13" y="13" width="8" height="8" rx="1.5" stroke="currentColor" stroke-width="2"/></svg>',
|
|
2818
|
+
video: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><rect x="2" y="5" width="14" height="14" rx="2" stroke="currentColor" stroke-width="2"/><path d="m16 10 6-3v10l-6-3" stroke="currentColor" stroke-width="2" stroke-linejoin="round"/></svg>',
|
|
2819
|
+
audio: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M9 18V6l11-2v12" stroke="currentColor" stroke-width="2" stroke-linejoin="round"/><circle cx="6" cy="18" r="3" stroke="currentColor" stroke-width="2"/><circle cx="17" cy="16" r="3" stroke="currentColor" stroke-width="2"/></svg>',
|
|
2820
|
+
arrowLeft: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M19 12H5m6-6-6 6 6 6" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>',
|
|
2821
|
+
arrowRight: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M5 12h14m-6-6 6 6-6 6" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>',
|
|
2822
|
+
delimiter: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M4 12h16" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-dasharray="2 4"/></svg>',
|
|
2823
|
+
link: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M10 14a4 4 0 0 0 6 0l3-3a4 4 0 0 0-6-6l-1 1m2 4a4 4 0 0 0-6 0l-3 3a4 4 0 0 0 6 6l1-1" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>',
|
|
2824
|
+
button: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><rect x="3" y="7" width="18" height="10" rx="3" stroke="currentColor" stroke-width="2"/><path d="M8 12h8" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>',
|
|
2825
|
+
entry: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><rect x="3" y="3" width="18" height="18" rx="2" stroke="currentColor" stroke-width="2"/><path d="M7 8h10M7 12h10M7 16h6" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>',
|
|
2826
|
+
block: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><rect x="3" y="3" width="18" height="18" rx="2" stroke="currentColor" stroke-width="2"/><path d="M7 8h10M7 12h6M7 16h8" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>',
|
|
2827
|
+
media: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><rect x="2" y="6" width="16" height="14" rx="2" stroke="currentColor" stroke-width="2"/><path d="M6 6V4h16v13h-4" stroke="currentColor" stroke-width="2" stroke-linejoin="round"/><path d="m3 18 4-4 3 3 3-3 4 4" stroke="currentColor" stroke-width="2" stroke-linejoin="round"/></svg>',
|
|
2828
|
+
checklist: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="m4 7 2 2 4-4M4 17l2 2 4-4M13 7h7M13 17h7" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>',
|
|
2829
|
+
marker: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="m15 5 4 4L9 19H5v-4L15 5Z" stroke="currentColor" stroke-width="2" stroke-linejoin="round"/></svg>',
|
|
2830
|
+
underline: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M7 4v7a5 5 0 0 0 10 0V4M5 20h14" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>',
|
|
2831
|
+
strike: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M4 12h16M16 6.5C15.3 5 13.8 4 12 4 9.8 4 8 5.3 8 7.3c0 1.3.8 2.2 2 2.7m4 4c1.2.5 2 1.4 2 2.7 0 2-1.8 3.3-4 3.3-1.8 0-3.3-1-4-2.5" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>',
|
|
2832
|
+
tooltip: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M4 5h16v11H9l-5 4V5Z" stroke="currentColor" stroke-width="2" stroke-linejoin="round"/></svg>',
|
|
2833
|
+
sparkle: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M12 3v4m0 10v4M3 12h4m10 0h4M6 6l2 2m8 8 2 2M6 18l2-2m8-8 2-2" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>',
|
|
2834
|
+
alignLeft: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M4 6h16M4 10h10M4 14h16M4 18h10" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>',
|
|
2835
|
+
alignCenter: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M4 6h16M7 10h10M4 14h16M7 18h10" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>',
|
|
2836
|
+
alignRight: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M4 6h16M10 10h10M4 14h16M10 18h10" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>',
|
|
2837
|
+
alignJustify: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M4 6h16M4 10h16M4 14h16M4 18h16" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>',
|
|
2838
|
+
variant: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M5 5h14v14H5z" stroke="currentColor" stroke-width="2" stroke-linejoin="round"/><path d="M9 9h6M9 13h4" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>',
|
|
2839
|
+
check: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="m5 12 5 5 9-10" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>',
|
|
2840
|
+
plus: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M12 5v14M5 12h14" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>',
|
|
2841
|
+
minus: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M5 12h14" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>',
|
|
2842
|
+
upload: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M12 16V4m0 0-4 4m4-4 4 4M4 16v3a1 1 0 0 0 1 1h14a1 1 0 0 0 1-1v-3" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>',
|
|
2843
|
+
trash: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M4 7h16M9 7V4h6v3m-8 0 1 13h8l1-13" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>',
|
|
2844
|
+
border: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><rect x="4" y="4" width="16" height="16" rx="1" stroke="currentColor" stroke-width="2"/></svg>',
|
|
2845
|
+
background: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><rect x="4" y="4" width="16" height="16" rx="1" fill="currentColor" opacity=".3"/><rect x="8" y="8" width="8" height="8" stroke="currentColor" stroke-width="2"/></svg>',
|
|
2846
|
+
stretch: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M3 12h18m-15-3-3 3 3 3m12-6 3 3-3 3" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>',
|
|
2847
|
+
mention: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><circle cx="12" cy="12" r="4" stroke="currentColor" stroke-width="2"/><path d="M16 8v5a3 3 0 0 0 6 0v-1a10 10 0 1 0-4 8" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>',
|
|
2848
|
+
toc: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M4 6h2M4 12h2M4 18h2M9 6h11M12 12h8M12 18h8" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>',
|
|
2849
|
+
lineNumbers: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M4 6h2M4 12h2M4 18h2M10 6h10M10 12h10M10 18h10" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>',
|
|
2850
|
+
math: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M18 5H6l6 7-6 7h12" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>',
|
|
2851
|
+
inlineMath: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M4 18c3 0 3-12 6-12M5 11h5m4 1 6 6m0-6-6 6" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>',
|
|
2852
|
+
footnote: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M4 18h10M4 13h10M17 4l2-1v6m-2 0h4" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>',
|
|
2853
|
+
// 2.5: "Copy block" in the block menu.
|
|
2854
|
+
copy: '<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><rect x="8" y="8" width="12" height="12" rx="2" stroke="currentColor" stroke-width="2"/><path d="M16 8V6a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v8a2 2 0 0 0 2 2h2" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>'
|
|
2855
|
+
};
|
|
2856
|
+
const tools = /* @__PURE__ */ new Map();
|
|
2857
|
+
const registerBlock = (options) => {
|
|
2858
|
+
if (!isObject$1(options))
|
|
2859
|
+
throw new BlockDefinitionError("Magic Editor X registerBlock: pass { definition, tool }");
|
|
2860
|
+
const { definition, tool, title, icon, i18n, inlineToolbar, shortcut } = options;
|
|
2861
|
+
if (typeof tool !== "function")
|
|
2862
|
+
throw new BlockDefinitionError(
|
|
2863
|
+
'Magic Editor X registerBlock: "tool" must be an Editor.js block tool class'
|
|
2864
|
+
);
|
|
2865
|
+
if (shortcut !== void 0 && (typeof shortcut !== "string" || !/^[A-Z0-9+]{1,40}$/.test(shortcut)))
|
|
2866
|
+
throw new BlockDefinitionError('Magic Editor X registerBlock: "shortcut" must look like CMD+SHIFT+K');
|
|
2867
|
+
const existing = tools.get(definition && definition.name);
|
|
2868
|
+
if (existing && existing.tool === tool && existing.source === definition) return existing.definition;
|
|
2869
|
+
const compiled2 = registerBlockDefinition(definition);
|
|
2870
|
+
tools.set(compiled2.name, {
|
|
2871
|
+
source: definition,
|
|
2872
|
+
definition: compiled2,
|
|
2873
|
+
tool,
|
|
2874
|
+
title: title === void 0 ? compiled2.title || compiled2.name : title,
|
|
2875
|
+
icon: typeof icon === "string" ? sanitizeSvg(icon) : "",
|
|
2876
|
+
i18n: isObject$1(i18n) ? i18n : {},
|
|
2877
|
+
inlineToolbar: inlineToolbar === true,
|
|
2878
|
+
shortcut
|
|
2879
|
+
});
|
|
2880
|
+
return compiled2;
|
|
2881
|
+
};
|
|
2882
|
+
const codeBlocks = () => [...tools.values()];
|
|
2883
|
+
const buildCodeBlockTools = (ctx) => {
|
|
2884
|
+
const out = {};
|
|
2885
|
+
for (const entry of tools.values()) {
|
|
2886
|
+
const { definition, tool: Tool } = entry;
|
|
2887
|
+
const messages = isObject$1(entry.i18n[ctx.locale]) ? entry.i18n[ctx.locale] : isObject$1(entry.i18n[String(ctx.locale || "").split("-")[0]]) ? entry.i18n[String(ctx.locale).split("-")[0]] : isObject$1(entry.i18n.en) ? entry.i18n.en : {};
|
|
2888
|
+
const rules = sanitizeRulesFor(definition.schema);
|
|
2889
|
+
const toolbox = {
|
|
2890
|
+
title: localized(entry.title, ctx.locale) || definition.name,
|
|
2891
|
+
icon: entry.icon || ICONS.block
|
|
2892
|
+
};
|
|
2893
|
+
const ownSanitize = Tool.sanitize && typeof Tool.sanitize === "object";
|
|
2894
|
+
const ownToolbox = Boolean(Tool.toolbox);
|
|
2895
|
+
const ToolClass = ownSanitize && ownToolbox ? Tool : class CodeBlockTool extends Tool {
|
|
2896
|
+
static get sanitize() {
|
|
2897
|
+
return ownSanitize ? Tool.sanitize : rules;
|
|
2898
|
+
}
|
|
2899
|
+
static get toolbox() {
|
|
2900
|
+
return ownToolbox ? Tool.toolbox : toolbox;
|
|
2901
|
+
}
|
|
2902
|
+
};
|
|
2903
|
+
out[definition.name] = {
|
|
2904
|
+
class: ToolClass,
|
|
2905
|
+
...entry.inlineToolbar ? { inlineToolbar: ctx.inline } : {},
|
|
2906
|
+
...entry.shortcut ? { shortcut: entry.shortcut } : {},
|
|
2907
|
+
toolbox,
|
|
2908
|
+
config: {
|
|
2909
|
+
...ctx.shared,
|
|
2910
|
+
t: ctx.t,
|
|
2911
|
+
locale: ctx.locale || "en",
|
|
2912
|
+
definition,
|
|
2913
|
+
/** Message of the registration's `i18n` for the admin locale. */
|
|
2914
|
+
translate: (key, fallback) => typeof messages[key] === "string" ? messages[key] : fallback,
|
|
2915
|
+
/** Cleans data with the definition (inline HTML through the admin sanitiser). */
|
|
2916
|
+
clean: (data) => definition.clean(data, sanitizeInline)
|
|
2917
|
+
}
|
|
2918
|
+
};
|
|
2919
|
+
}
|
|
2920
|
+
return out;
|
|
2921
|
+
};
|
|
2922
|
+
const createReactTool = (Component, options = {}) => {
|
|
2923
|
+
if (typeof Component !== "function" && !(Component && typeof Component === "object"))
|
|
2924
|
+
throw new TypeError("Magic Editor X createReactTool: pass a React component");
|
|
2925
|
+
const className = typeof options.className === "string" ? options.className : "mex-code-block-tool";
|
|
2926
|
+
return class ReactBlockTool {
|
|
2927
|
+
static get isReadOnlySupported() {
|
|
2928
|
+
return true;
|
|
2929
|
+
}
|
|
2930
|
+
/**
|
|
2931
|
+
* @param {{ data: object, config: object, readOnly: boolean, block: object }} params - Editor.js tool parameters.
|
|
2932
|
+
*/
|
|
2933
|
+
constructor({ data, config, readOnly, block }) {
|
|
2934
|
+
this.config = config || {};
|
|
2935
|
+
this.readOnly = Boolean(readOnly);
|
|
2936
|
+
this.blockApi = block;
|
|
2937
|
+
this.clean = typeof this.config.clean === "function" ? this.config.clean : (value) => value || {};
|
|
2938
|
+
this.data = this.clean(data);
|
|
2939
|
+
this.root = null;
|
|
2940
|
+
this.element = null;
|
|
2941
|
+
}
|
|
2942
|
+
/** @returns {HTMLElement} Block element with the mounted component. */
|
|
2943
|
+
render() {
|
|
2944
|
+
this.element = document.createElement("div");
|
|
2945
|
+
this.element.className = className;
|
|
2946
|
+
this.root = createRoot(this.element);
|
|
2947
|
+
this.paint();
|
|
2948
|
+
return this.element;
|
|
2949
|
+
}
|
|
2950
|
+
/** Renders the component with the current data. */
|
|
2951
|
+
paint() {
|
|
2952
|
+
if (!this.root) return;
|
|
2953
|
+
this.root.render(
|
|
2954
|
+
createElement(Component, {
|
|
2955
|
+
data: this.data,
|
|
2956
|
+
readOnly: this.readOnly,
|
|
2957
|
+
definition: this.config.definition,
|
|
2958
|
+
translate: this.config.translate || ((_key, fallback) => fallback),
|
|
2959
|
+
onChange: (next) => {
|
|
2960
|
+
if (this.readOnly) return;
|
|
2961
|
+
this.data = this.clean(next);
|
|
2962
|
+
this.paint();
|
|
2963
|
+
if (this.blockApi && typeof this.blockApi.dispatchChange === "function")
|
|
2964
|
+
this.blockApi.dispatchChange();
|
|
2965
|
+
}
|
|
2966
|
+
})
|
|
2967
|
+
);
|
|
2968
|
+
}
|
|
2969
|
+
/** @returns {object} Current data (cleaned again by the editor and the server). */
|
|
2970
|
+
save() {
|
|
2971
|
+
return this.data;
|
|
2972
|
+
}
|
|
2973
|
+
/** Unmounts the React root (deferred: Editor.js may call this while React renders). */
|
|
2974
|
+
destroy() {
|
|
2975
|
+
const root = this.root;
|
|
2976
|
+
this.root = null;
|
|
2977
|
+
if (root) setTimeout(() => root.unmount(), 0);
|
|
2978
|
+
}
|
|
2979
|
+
removed() {
|
|
2980
|
+
this.destroy();
|
|
2981
|
+
}
|
|
2982
|
+
};
|
|
2983
|
+
};
|
|
2984
|
+
const DRAFT_RECORD_VERSION = 1;
|
|
2985
|
+
const DEFAULT_DRAFT_TTL_HOURS = 72;
|
|
2986
|
+
const DRAFT_WRITE_DELAY_MS = 800;
|
|
2987
|
+
const MAX_DRAFT_CHARS = 6 * 1024 * 1024;
|
|
2988
|
+
const VOLUNTARY_LOGOUT_WINDOW_MS = 60 * 1e3;
|
|
2989
|
+
const MAX_PART = 512;
|
|
2990
|
+
const draftIdentity = (target) => {
|
|
2991
|
+
if (!target || target.owner === void 0 || target.owner === null || target.owner === "") return null;
|
|
2992
|
+
const { uid, field: field2 } = target;
|
|
2993
|
+
if (typeof uid !== "string" || !uid || typeof field2 !== "string" || !field2) return null;
|
|
2994
|
+
const identity = isPositionalFieldPath(field2) && isValidFieldKey(target.fieldKey) ? `${basePathOf(field2)}#${target.fieldKey}` : field2;
|
|
2995
|
+
const parts = [
|
|
2996
|
+
String(target.owner),
|
|
2997
|
+
uid,
|
|
2998
|
+
typeof target.documentId === "string" && target.documentId ? target.documentId : "new",
|
|
2999
|
+
typeof target.locale === "string" && target.locale ? target.locale : "-",
|
|
3000
|
+
identity
|
|
3001
|
+
];
|
|
3002
|
+
if (parts.some((part) => part.length > MAX_PART)) return null;
|
|
3003
|
+
return parts.join("|");
|
|
3004
|
+
};
|
|
3005
|
+
const hashText = (text) => {
|
|
3006
|
+
let h1 = 2166136261;
|
|
3007
|
+
let h2 = 16777619;
|
|
3008
|
+
const value = String(text);
|
|
3009
|
+
for (let i = 0; i < value.length; i += 1) {
|
|
3010
|
+
const code = value.charCodeAt(i);
|
|
3011
|
+
h1 = Math.imul(h1 ^ code, 16777619) >>> 0;
|
|
3012
|
+
h2 = Math.imul(h2 ^ code, 2246822519) >>> 0;
|
|
3013
|
+
}
|
|
3014
|
+
return `${h2.toString(16).padStart(8, "0")}${h1.toString(16).padStart(8, "0")}`;
|
|
3015
|
+
};
|
|
3016
|
+
const isValidDraftRecord = (record) => Boolean(
|
|
3017
|
+
record && typeof record === "object" && record.v === DRAFT_RECORD_VERSION && typeof record.id === "string" && typeof record.baseHash === "string" && Number.isFinite(record.savedAt) && Number.isFinite(record.expiresAt) && record.data && typeof record.data === "object" && Array.isArray(record.data.blocks)
|
|
3018
|
+
);
|
|
3019
|
+
const isExpired = (record, now) => !record || !(record.expiresAt > now);
|
|
3020
|
+
const ttlMsOf = (hours) => {
|
|
3021
|
+
const number = Number(hours);
|
|
3022
|
+
const safe = Number.isFinite(number) && number >= 1 && number <= 720 ? number : DEFAULT_DRAFT_TTL_HOURS;
|
|
3023
|
+
return Math.round(safe * 3600 * 1e3);
|
|
3024
|
+
};
|
|
3025
|
+
const buildDraftRecord = ({ id, data, baseKey, now, ttlMs }) => {
|
|
3026
|
+
if (!id || !data || !Array.isArray(data.blocks)) return null;
|
|
3027
|
+
const copy2 = { ...data };
|
|
3028
|
+
delete copy2.fieldKey;
|
|
3029
|
+
let json;
|
|
3030
|
+
try {
|
|
3031
|
+
json = JSON.stringify(copy2);
|
|
3032
|
+
} catch {
|
|
3033
|
+
return null;
|
|
3034
|
+
}
|
|
3035
|
+
if (json.length > MAX_DRAFT_CHARS) return null;
|
|
3036
|
+
return {
|
|
3037
|
+
v: DRAFT_RECORD_VERSION,
|
|
3038
|
+
id,
|
|
3039
|
+
baseHash: hashText(baseKey),
|
|
3040
|
+
savedAt: now,
|
|
3041
|
+
expiresAt: now + ttlMs,
|
|
3042
|
+
data: JSON.parse(json)
|
|
3043
|
+
};
|
|
3044
|
+
};
|
|
3045
|
+
const restoreDecision = ({ record, now, readOnly = false, recordKey, shownKey, savedKey }) => {
|
|
3046
|
+
if (!record) return { action: "none" };
|
|
3047
|
+
if (!isValidDraftRecord(record) || isExpired(record, now)) return { action: "delete" };
|
|
3048
|
+
if (readOnly) return { action: "none" };
|
|
3049
|
+
if (recordKey === shownKey || recordKey === savedKey) return { action: "delete" };
|
|
3050
|
+
return { action: "offer", conflict: record.baseHash !== hashText(savedKey) };
|
|
3051
|
+
};
|
|
3052
|
+
const bufferAction = ({ dirty, collaboration }) => {
|
|
3053
|
+
const synced = collaboration && collaboration.active && collaboration.status === "live" && !collaboration.pending;
|
|
3054
|
+
return dirty && !synced ? "write" : "delete";
|
|
3055
|
+
};
|
|
3056
|
+
const relativeTimeOf = (then, now) => {
|
|
3057
|
+
const seconds = Math.max(0, Math.round((now - then) / 1e3));
|
|
3058
|
+
if (seconds < 60) return { value: -seconds, unit: "second" };
|
|
3059
|
+
const minutes = Math.round(seconds / 60);
|
|
3060
|
+
if (minutes < 60) return { value: -minutes, unit: "minute" };
|
|
3061
|
+
const hours = Math.round(minutes / 60);
|
|
3062
|
+
if (hours < 24) return { value: -hours, unit: "hour" };
|
|
3063
|
+
return { value: -Math.round(hours / 24), unit: "day" };
|
|
3064
|
+
};
|
|
3065
|
+
const createLogoutMiddleware = ({ onLogout, now = () => Date.now() }) => {
|
|
3066
|
+
let voluntaryAt = -Infinity;
|
|
3067
|
+
return () => () => (next) => (action2) => {
|
|
3068
|
+
if (action2 && typeof action2 === "object") {
|
|
3069
|
+
if (isLogoutMutationStart(action2)) voluntaryAt = now();
|
|
3070
|
+
else if (action2.type === "admin/logout" && now() - voluntaryAt <= VOLUNTARY_LOGOUT_WINDOW_MS) {
|
|
3071
|
+
voluntaryAt = -Infinity;
|
|
3072
|
+
try {
|
|
3073
|
+
onLogout();
|
|
3074
|
+
} catch {
|
|
3075
|
+
}
|
|
3076
|
+
}
|
|
3077
|
+
}
|
|
3078
|
+
return next(action2);
|
|
3079
|
+
};
|
|
3080
|
+
};
|
|
3081
|
+
const isLogoutMutationStart = (action2) => Boolean(
|
|
3082
|
+
action2 && action2.type === "adminApi/executeMutation/pending" && action2.meta && action2.meta.arg && action2.meta.arg.endpointName === "logout"
|
|
3083
|
+
);
|
|
3084
|
+
const valueAtPath = (values, path) => {
|
|
3085
|
+
let current = values;
|
|
3086
|
+
for (const part of String(path || "").split(".").filter(Boolean).slice(0, 24)) {
|
|
3087
|
+
if (!current || typeof current !== "object") return void 0;
|
|
3088
|
+
current = current[part];
|
|
3089
|
+
}
|
|
3090
|
+
return current;
|
|
3091
|
+
};
|
|
3092
|
+
const savedValueOf = (initialValues, path, fieldKey = null) => {
|
|
3093
|
+
if (isPositionalFieldPath(path) && isValidFieldKey(fieldKey)) {
|
|
3094
|
+
const parts = basePathOf(path).split(".").filter(Boolean);
|
|
3095
|
+
let found = null;
|
|
3096
|
+
const walk = (data, index2, depth) => {
|
|
3097
|
+
if (found || !data || typeof data !== "object" || depth > 16) return;
|
|
3098
|
+
if (Array.isArray(data)) {
|
|
3099
|
+
data.forEach((item) => walk(item, index2, depth + 1));
|
|
3100
|
+
return;
|
|
3101
|
+
}
|
|
3102
|
+
if (index2 === parts.length) {
|
|
3103
|
+
if (data.fieldKey === fieldKey) found = data;
|
|
3104
|
+
return;
|
|
3105
|
+
}
|
|
3106
|
+
walk(data[parts[index2]], index2 + 1, depth + 1);
|
|
3107
|
+
};
|
|
3108
|
+
walk(initialValues, 0, 0);
|
|
3109
|
+
if (found) return found;
|
|
3110
|
+
}
|
|
3111
|
+
const value = valueAtPath(initialValues, path);
|
|
3112
|
+
return value === void 0 ? null : value;
|
|
3113
|
+
};
|
|
3114
|
+
let loggedOut = false;
|
|
3115
|
+
const isLoggedOut = () => loggedOut;
|
|
3116
|
+
const trackSessionAction = (action2) => {
|
|
3117
|
+
if (!action2 || typeof action2 !== "object") return;
|
|
3118
|
+
if (action2.type === "admin/logout") loggedOut = true;
|
|
3119
|
+
else if (action2.type === "admin/login" || action2.type === "admin/setToken") loggedOut = false;
|
|
3120
|
+
};
|
|
3121
|
+
const createSessionMiddleware = () => () => (next) => (action2) => {
|
|
3122
|
+
trackSessionAction(action2);
|
|
3123
|
+
return next(action2);
|
|
3124
|
+
};
|
|
3125
|
+
const DB_NAME = "magic-editor-x";
|
|
3126
|
+
const STORE = "drafts";
|
|
3127
|
+
let dbPromise = null;
|
|
3128
|
+
const openDb = () => {
|
|
3129
|
+
if (dbPromise) return dbPromise;
|
|
3130
|
+
dbPromise = new Promise((resolve) => {
|
|
3131
|
+
let request;
|
|
3132
|
+
try {
|
|
3133
|
+
if (typeof indexedDB === "undefined") {
|
|
3134
|
+
resolve(null);
|
|
3135
|
+
return;
|
|
3136
|
+
}
|
|
3137
|
+
request = indexedDB.open(DB_NAME, 1);
|
|
3138
|
+
} catch {
|
|
3139
|
+
resolve(null);
|
|
3140
|
+
return;
|
|
3141
|
+
}
|
|
3142
|
+
request.onupgradeneeded = () => {
|
|
3143
|
+
const db = request.result;
|
|
3144
|
+
if (!db.objectStoreNames.contains(STORE)) db.createObjectStore(STORE, { keyPath: "id" });
|
|
3145
|
+
};
|
|
3146
|
+
request.onsuccess = () => {
|
|
3147
|
+
const db = request.result;
|
|
3148
|
+
db.onversionchange = () => {
|
|
3149
|
+
db.close();
|
|
3150
|
+
dbPromise = null;
|
|
3151
|
+
};
|
|
3152
|
+
resolve(db);
|
|
3153
|
+
};
|
|
3154
|
+
request.onerror = () => resolve(null);
|
|
3155
|
+
request.onblocked = () => resolve(null);
|
|
3156
|
+
});
|
|
3157
|
+
return dbPromise;
|
|
3158
|
+
};
|
|
3159
|
+
const withStore = async (mode, run) => {
|
|
3160
|
+
const db = await openDb();
|
|
3161
|
+
if (!db) return null;
|
|
3162
|
+
return new Promise((resolve) => {
|
|
3163
|
+
try {
|
|
3164
|
+
const transaction = db.transaction(STORE, mode);
|
|
3165
|
+
const request = run(transaction.objectStore(STORE));
|
|
3166
|
+
transaction.oncomplete = () => resolve(request ? request.result : true);
|
|
3167
|
+
transaction.onerror = () => resolve(null);
|
|
3168
|
+
transaction.onabort = () => resolve(null);
|
|
3169
|
+
} catch {
|
|
3170
|
+
resolve(null);
|
|
3171
|
+
}
|
|
3172
|
+
});
|
|
3173
|
+
};
|
|
3174
|
+
const getDraft = async (id) => id ? await withStore("readonly", (s) => s.get(id)) || null : null;
|
|
3175
|
+
const putDraft = async (record) => Boolean(record && record.id && await withStore("readwrite", (s) => s.put(record)) !== null);
|
|
3176
|
+
const deleteDraft = async (id) => Boolean(id && await withStore("readwrite", (s) => s.delete(id)) !== null);
|
|
3177
|
+
const clearDrafts = async () => await withStore("readwrite", (s) => s.clear()) !== null;
|
|
3178
|
+
let pruned = false;
|
|
3179
|
+
const pruneDrafts = async (now) => {
|
|
3180
|
+
if (pruned) return;
|
|
3181
|
+
pruned = true;
|
|
3182
|
+
await withStore("readwrite", (store) => {
|
|
3183
|
+
const cursorRequest = store.openCursor();
|
|
3184
|
+
cursorRequest.onsuccess = () => {
|
|
3185
|
+
const cursor = cursorRequest.result;
|
|
3186
|
+
if (!cursor) return;
|
|
3187
|
+
if (isExpired(cursor.value, now)) cursor.delete();
|
|
3188
|
+
cursor.continue();
|
|
3189
|
+
};
|
|
3190
|
+
});
|
|
3191
|
+
};
|
|
3192
|
+
const LOADERS = {
|
|
3193
|
+
en: () => [
|
|
3194
|
+
import("./en-Dr58wBAo.mjs"),
|
|
3195
|
+
import("./en-BCKaVgBM.mjs"),
|
|
3196
|
+
import("./en-Uhm7OouM.mjs"),
|
|
3197
|
+
import("./en-Cd6Jk-J0.mjs")
|
|
3198
|
+
],
|
|
3199
|
+
de: () => [
|
|
3200
|
+
import("./de-BWfR2jTg.mjs"),
|
|
3201
|
+
import("./de-CQA8hOpl.mjs"),
|
|
3202
|
+
import("./de-zgHCYTLH.mjs"),
|
|
3203
|
+
import("./de-Bip0sBRT.mjs")
|
|
3204
|
+
],
|
|
3205
|
+
fr: () => [
|
|
3206
|
+
import("./fr-BvPjRiwr.mjs"),
|
|
3207
|
+
import("./fr-D4tL6Ag2.mjs"),
|
|
3208
|
+
import("./fr-CrIHmKYX.mjs"),
|
|
3209
|
+
import("./fr-CKHZ0xxU.mjs")
|
|
3210
|
+
],
|
|
3211
|
+
es: () => [
|
|
3212
|
+
import("./es-B2Hkkbw9.mjs"),
|
|
3213
|
+
import("./es-pvPfNAld.mjs"),
|
|
3214
|
+
import("./es-BMkmfH88.mjs"),
|
|
3215
|
+
import("./es-Bz8d72Fa.mjs")
|
|
3216
|
+
],
|
|
3217
|
+
pt: () => [
|
|
3218
|
+
import("./pt-DdGagO99.mjs"),
|
|
3219
|
+
import("./pt-CuoIihKx.mjs"),
|
|
3220
|
+
import("./pt-DDlPJYtB.mjs"),
|
|
3221
|
+
import("./pt-B8x7v-Xw.mjs")
|
|
3222
|
+
]
|
|
3223
|
+
};
|
|
3224
|
+
const PRESET_OPTIONS = [
|
|
3225
|
+
{ value: "full", label: ["field.options.preset.full", "Full: every block and formatting"] },
|
|
3226
|
+
{ value: "article", label: ["field.options.preset.article", "Article: long-form text without raw HTML"] },
|
|
3227
|
+
{ value: "basic", label: ["field.options.preset.basic", "Basic: headings 2-3, lists, quotes, images"] },
|
|
3228
|
+
{
|
|
3229
|
+
value: "minimal",
|
|
3230
|
+
label: ["field.options.preset.minimal", "Minimal: paragraphs, lists, bold, italic, links"]
|
|
3231
|
+
}
|
|
3232
|
+
];
|
|
3233
|
+
const LINK_OPTIONS = [
|
|
3234
|
+
{ value: "all", label: ["field.options.linkTargets.all", "Entries and web addresses"] },
|
|
3235
|
+
{ value: "internal", label: ["field.options.linkTargets.internal", "Only entries of this site"] },
|
|
3236
|
+
{ value: "external", label: ["field.options.linkTargets.external", "Only web addresses"] },
|
|
3237
|
+
{ value: "none", label: ["field.options.linkTargets.none", "No links"] }
|
|
3238
|
+
];
|
|
3239
|
+
const loadMessages = async (locale) => {
|
|
3240
|
+
const loader = LOADERS[locale] || LOADERS[locale.split("-")[0]] || LOADERS.en;
|
|
3241
|
+
const modules = await Promise.all(loader());
|
|
3242
|
+
const merged = Object.assign({}, ...modules.map((module) => module.default || module));
|
|
3243
|
+
return Object.fromEntries(Object.entries(merged).map(([key, value]) => [getTranslation(key), value]));
|
|
3244
|
+
};
|
|
3245
|
+
const index = {
|
|
3246
|
+
/**
|
|
3247
|
+
* Registers the plugin, the custom field and the settings links.
|
|
3248
|
+
* @param {object} app - Strapi admin app.
|
|
3249
|
+
*/
|
|
3250
|
+
register(app) {
|
|
3251
|
+
app.registerPlugin({ id: PLUGIN_ID, name: PLUGIN_ID, apis: { registerBlock, createReactTool } });
|
|
3252
|
+
if (app.widgets && typeof app.widgets.register === "function") {
|
|
3253
|
+
app.widgets.register({
|
|
3254
|
+
id: "mentions",
|
|
3255
|
+
pluginId: PLUGIN_ID,
|
|
3256
|
+
icon: Bell,
|
|
3257
|
+
title: { id: getTranslation("widget.mentions.title"), defaultMessage: "Mentions and replies" },
|
|
3258
|
+
component: async () => {
|
|
3259
|
+
const { MentionsWidget } = await import("./MentionsWidget-CJPZDk9Z.mjs");
|
|
3260
|
+
return MentionsWidget;
|
|
3261
|
+
},
|
|
3262
|
+
permissions: [
|
|
3263
|
+
{ action: "plugin::magic-editor-x.access" },
|
|
3264
|
+
{ action: "plugin::magic-editor-x.comments.read" }
|
|
3265
|
+
]
|
|
3266
|
+
});
|
|
3267
|
+
}
|
|
3268
|
+
if (app.widgets && typeof app.widgets.register === "function") {
|
|
3269
|
+
app.widgets.register([
|
|
3270
|
+
{
|
|
3271
|
+
id: "open-threads",
|
|
3272
|
+
pluginId: PLUGIN_ID,
|
|
3273
|
+
icon: Discuss,
|
|
3274
|
+
title: { id: getTranslation("widget.openThreads.title"), defaultMessage: "My open threads" },
|
|
3275
|
+
component: async () => {
|
|
3276
|
+
const { OpenThreadsWidget } = await import("./EntryWidgets-B8YA0Zxy.mjs");
|
|
3277
|
+
return OpenThreadsWidget;
|
|
3278
|
+
},
|
|
3279
|
+
permissions: [
|
|
3280
|
+
{ action: "plugin::magic-editor-x.access" },
|
|
3281
|
+
{ action: "plugin::magic-editor-x.comments.read" }
|
|
3282
|
+
]
|
|
3283
|
+
},
|
|
3284
|
+
{
|
|
3285
|
+
id: "broken-links",
|
|
3286
|
+
pluginId: PLUGIN_ID,
|
|
3287
|
+
icon: Link,
|
|
3288
|
+
title: { id: getTranslation("widget.brokenLinks.title"), defaultMessage: "Broken links" },
|
|
3289
|
+
component: async () => {
|
|
3290
|
+
const { BrokenLinksWidget } = await import("./EntryWidgets-B8YA0Zxy.mjs");
|
|
3291
|
+
return BrokenLinksWidget;
|
|
3292
|
+
},
|
|
3293
|
+
permissions: [{ action: "plugin::magic-editor-x.access" }]
|
|
3294
|
+
}
|
|
3295
|
+
]);
|
|
3296
|
+
}
|
|
3297
|
+
app.customFields.register({
|
|
3298
|
+
name: "richtext",
|
|
3299
|
+
pluginId: PLUGIN_ID,
|
|
3300
|
+
type: "json",
|
|
3301
|
+
intlLabel: { id: getTranslation("field.label"), defaultMessage: "Magic Editor X" },
|
|
3302
|
+
intlDescription: {
|
|
3303
|
+
id: getTranslation("field.description"),
|
|
3304
|
+
defaultMessage: "Block editor with media, embeds, custom blocks, history and live collaboration"
|
|
3305
|
+
},
|
|
3306
|
+
icon: PluginIcon,
|
|
3307
|
+
components: {
|
|
3308
|
+
Input: () => import("./index-C4ksUkEm.mjs").then((n) => n.a)
|
|
3309
|
+
},
|
|
3310
|
+
options: {
|
|
3311
|
+
base: [
|
|
3312
|
+
{
|
|
3313
|
+
sectionTitle: { id: getTranslation("field.options.section"), defaultMessage: "Editor" },
|
|
3314
|
+
items: [
|
|
3315
|
+
{
|
|
3316
|
+
intlLabel: {
|
|
3317
|
+
id: getTranslation("field.options.placeholder.label"),
|
|
3318
|
+
defaultMessage: "Placeholder"
|
|
3319
|
+
},
|
|
3320
|
+
description: {
|
|
3321
|
+
id: getTranslation("field.options.placeholder.description"),
|
|
3322
|
+
defaultMessage: "Text shown while the editor is empty"
|
|
3323
|
+
},
|
|
3324
|
+
name: "options.placeholder",
|
|
3325
|
+
type: "text"
|
|
3326
|
+
},
|
|
3327
|
+
{
|
|
3328
|
+
intlLabel: { id: getTranslation("field.options.preset.label"), defaultMessage: "Preset" },
|
|
3329
|
+
description: {
|
|
3330
|
+
id: getTranslation("field.options.preset.description"),
|
|
3331
|
+
defaultMessage: "Which blocks and formatting editors can use in this field. Enforced when saving."
|
|
3332
|
+
},
|
|
3333
|
+
name: "options.preset",
|
|
3334
|
+
type: "select",
|
|
3335
|
+
options: PRESET_OPTIONS.map(({ value, label: [id, defaultMessage] }) => ({
|
|
3336
|
+
key: value,
|
|
3337
|
+
value,
|
|
3338
|
+
metadatas: { intlLabel: { id: getTranslation(id), defaultMessage } }
|
|
3339
|
+
}))
|
|
3340
|
+
},
|
|
3341
|
+
{
|
|
3342
|
+
intlLabel: {
|
|
3343
|
+
id: getTranslation("field.options.linkTargets.label"),
|
|
3344
|
+
defaultMessage: "Allowed links"
|
|
3345
|
+
},
|
|
3346
|
+
description: {
|
|
3347
|
+
id: getTranslation("field.options.linkTargets.description"),
|
|
3348
|
+
defaultMessage: "Links to entries of this site, to web addresses, both or none"
|
|
3349
|
+
},
|
|
3350
|
+
name: "options.linkTargets",
|
|
3351
|
+
type: "select",
|
|
3352
|
+
options: LINK_OPTIONS.map(({ value, label: [id, defaultMessage] }) => ({
|
|
3353
|
+
key: value,
|
|
3354
|
+
value,
|
|
3355
|
+
metadatas: { intlLabel: { id: getTranslation(id), defaultMessage } }
|
|
3356
|
+
}))
|
|
3357
|
+
},
|
|
3358
|
+
{
|
|
3359
|
+
intlLabel: {
|
|
3360
|
+
id: getTranslation("field.options.requireAlt.label"),
|
|
3361
|
+
defaultMessage: "Require alternative text for images"
|
|
3362
|
+
},
|
|
3363
|
+
description: {
|
|
3364
|
+
id: getTranslation("field.options.requireAlt.description"),
|
|
3365
|
+
defaultMessage: "Images need a text description or must be marked as decorative"
|
|
3366
|
+
},
|
|
3367
|
+
name: "options.requireAlt",
|
|
3368
|
+
type: "checkbox"
|
|
3369
|
+
}
|
|
3370
|
+
]
|
|
3371
|
+
}
|
|
3372
|
+
],
|
|
3373
|
+
advanced: [
|
|
3374
|
+
{
|
|
3375
|
+
sectionTitle: {
|
|
3376
|
+
id: getTranslation("field.options.content"),
|
|
3377
|
+
defaultMessage: "Allowed content and limits"
|
|
3378
|
+
},
|
|
3379
|
+
items: [
|
|
3380
|
+
{
|
|
3381
|
+
intlLabel: {
|
|
3382
|
+
id: getTranslation("field.options.allowedBlocks.label"),
|
|
3383
|
+
defaultMessage: "Allowed blocks"
|
|
3384
|
+
},
|
|
3385
|
+
description: {
|
|
3386
|
+
id: getTranslation("field.options.allowedBlocks.description"),
|
|
3387
|
+
defaultMessage: "Comma-separated block names that replace the preset list, for example: paragraph, header, list, image, myCustomBlock"
|
|
3388
|
+
},
|
|
3389
|
+
name: "options.allowedBlocks",
|
|
3390
|
+
type: "text"
|
|
3391
|
+
},
|
|
3392
|
+
{
|
|
3393
|
+
intlLabel: {
|
|
3394
|
+
id: getTranslation("field.options.headingLevels.label"),
|
|
3395
|
+
defaultMessage: "Heading levels"
|
|
3396
|
+
},
|
|
3397
|
+
description: {
|
|
3398
|
+
id: getTranslation("field.options.headingLevels.description"),
|
|
3399
|
+
defaultMessage: "For example 2-4 or 2,3. Empty uses the preset."
|
|
3400
|
+
},
|
|
3401
|
+
name: "options.headingLevels",
|
|
3402
|
+
type: "text"
|
|
3403
|
+
},
|
|
3404
|
+
{
|
|
3405
|
+
intlLabel: {
|
|
3406
|
+
id: getTranslation("field.options.maxBlocks.label"),
|
|
3407
|
+
defaultMessage: "Maximum number of blocks"
|
|
3408
|
+
},
|
|
3409
|
+
description: {
|
|
3410
|
+
id: getTranslation("field.options.maxBlocks.description"),
|
|
3411
|
+
defaultMessage: "Empty or 0 means no limit"
|
|
3412
|
+
},
|
|
3413
|
+
name: "options.maxBlocks",
|
|
3414
|
+
type: "number"
|
|
3415
|
+
},
|
|
3416
|
+
{
|
|
3417
|
+
intlLabel: {
|
|
3418
|
+
id: getTranslation("field.options.maxCharacters.label"),
|
|
3419
|
+
defaultMessage: "Maximum number of characters"
|
|
3420
|
+
},
|
|
3421
|
+
description: {
|
|
3422
|
+
id: getTranslation("field.options.maxCharacters.description"),
|
|
3423
|
+
defaultMessage: "Visible text, counted like the editor toolbar. Empty or 0 means no limit."
|
|
3424
|
+
},
|
|
3425
|
+
name: "options.maxCharacters",
|
|
3426
|
+
type: "number"
|
|
3427
|
+
}
|
|
3428
|
+
]
|
|
3429
|
+
},
|
|
3430
|
+
// 2.5: extra inline formatting, off unless turned on here.
|
|
3431
|
+
{
|
|
3432
|
+
sectionTitle: {
|
|
3433
|
+
id: getTranslation("field.options.formatting"),
|
|
3434
|
+
defaultMessage: "Extra formatting"
|
|
3435
|
+
},
|
|
3436
|
+
items: [
|
|
3437
|
+
{
|
|
3438
|
+
intlLabel: {
|
|
3439
|
+
id: getTranslation("field.options.inlineColors.label"),
|
|
3440
|
+
defaultMessage: "Text colour and highlight"
|
|
3441
|
+
},
|
|
3442
|
+
description: {
|
|
3443
|
+
id: getTranslation("field.options.inlineColors.description"),
|
|
3444
|
+
defaultMessage: "Editors pick from the colours your developer set in the plugin configuration (palette). Without a palette nothing is shown."
|
|
3445
|
+
},
|
|
3446
|
+
name: "options.inlineColors",
|
|
3447
|
+
type: "checkbox"
|
|
3448
|
+
},
|
|
3449
|
+
{
|
|
3450
|
+
intlLabel: {
|
|
3451
|
+
id: getTranslation("field.options.inlineScript.label"),
|
|
3452
|
+
defaultMessage: "Superscript and subscript"
|
|
3453
|
+
},
|
|
3454
|
+
description: {
|
|
3455
|
+
id: getTranslation("field.options.inlineScript.description"),
|
|
3456
|
+
defaultMessage: "For formulas and units such as m² or H₂O"
|
|
3457
|
+
},
|
|
3458
|
+
name: "options.inlineScript",
|
|
3459
|
+
type: "checkbox"
|
|
3460
|
+
},
|
|
3461
|
+
{
|
|
3462
|
+
intlLabel: {
|
|
3463
|
+
id: getTranslation("field.options.inlineAnnotations.label"),
|
|
3464
|
+
defaultMessage: "Annotations"
|
|
3465
|
+
},
|
|
3466
|
+
description: {
|
|
3467
|
+
id: getTranslation("field.options.inlineAnnotations.description"),
|
|
3468
|
+
defaultMessage: "Comma-separated annotation names, for example: abbreviation. Your developer can add more, such as glossary."
|
|
3469
|
+
},
|
|
3470
|
+
name: "options.inlineAnnotations",
|
|
3471
|
+
type: "text"
|
|
3472
|
+
}
|
|
3473
|
+
]
|
|
3474
|
+
},
|
|
3475
|
+
{
|
|
3476
|
+
sectionTitle: { id: getTranslation("field.options.features"), defaultMessage: "Features" },
|
|
3477
|
+
items: [
|
|
3478
|
+
{
|
|
3479
|
+
intlLabel: {
|
|
3480
|
+
id: getTranslation("field.options.disableAi.label"),
|
|
3481
|
+
defaultMessage: "Turn off the AI assistant for this field"
|
|
3482
|
+
},
|
|
3483
|
+
name: "options.disableAi",
|
|
3484
|
+
type: "checkbox"
|
|
3485
|
+
},
|
|
3486
|
+
{
|
|
3487
|
+
intlLabel: {
|
|
3488
|
+
id: getTranslation("field.options.disableGrammar.label"),
|
|
3489
|
+
defaultMessage: "Turn off the grammar check for this field"
|
|
3490
|
+
},
|
|
3491
|
+
description: {
|
|
3492
|
+
id: getTranslation("field.options.disableGrammar.description"),
|
|
3493
|
+
defaultMessage: "No text from this field is sent to LanguageTool"
|
|
3494
|
+
},
|
|
3495
|
+
name: "options.disableGrammar",
|
|
3496
|
+
type: "checkbox"
|
|
3497
|
+
},
|
|
3498
|
+
{
|
|
3499
|
+
intlLabel: {
|
|
3500
|
+
id: getTranslation("field.options.disableCollaboration.label"),
|
|
3501
|
+
defaultMessage: "Turn off live collaboration for this field"
|
|
3502
|
+
},
|
|
3503
|
+
name: "options.disableCollaboration",
|
|
3504
|
+
type: "checkbox"
|
|
3505
|
+
},
|
|
3506
|
+
{
|
|
3507
|
+
intlLabel: {
|
|
3508
|
+
id: getTranslation("field.options.publishGuard.label"),
|
|
3509
|
+
defaultMessage: "Block publishing while quality checks report errors"
|
|
3510
|
+
},
|
|
3511
|
+
description: {
|
|
3512
|
+
id: getTranslation("field.options.publishGuard.description"),
|
|
3513
|
+
defaultMessage: "Errors are missing alternative texts (when required) and empty links"
|
|
3514
|
+
},
|
|
3515
|
+
name: "options.publishGuard",
|
|
3516
|
+
type: "checkbox"
|
|
3517
|
+
}
|
|
3518
|
+
]
|
|
3519
|
+
},
|
|
3520
|
+
{
|
|
3521
|
+
sectionTitle: { id: getTranslation("field.options.advanced"), defaultMessage: "Layout" },
|
|
3522
|
+
items: [
|
|
3523
|
+
{
|
|
3524
|
+
intlLabel: {
|
|
3525
|
+
id: getTranslation("field.options.minHeight.label"),
|
|
3526
|
+
defaultMessage: "Minimum height (px)"
|
|
3527
|
+
},
|
|
3528
|
+
description: {
|
|
3529
|
+
id: getTranslation("field.options.minHeight.description"),
|
|
3530
|
+
defaultMessage: "Height of the empty editor, between 120 and 2000 pixels"
|
|
3531
|
+
},
|
|
3532
|
+
name: "options.minHeight",
|
|
3533
|
+
type: "number"
|
|
3534
|
+
}
|
|
3535
|
+
]
|
|
3536
|
+
}
|
|
3537
|
+
]
|
|
3538
|
+
}
|
|
3539
|
+
});
|
|
3540
|
+
app.addSettingsLink(
|
|
3541
|
+
{
|
|
3542
|
+
id: PLUGIN_ID,
|
|
3543
|
+
intlLabel: { id: getTranslation("settings.section"), defaultMessage: "Magic Editor X" }
|
|
3544
|
+
},
|
|
3545
|
+
[
|
|
3546
|
+
{
|
|
3547
|
+
id: `${PLUGIN_ID}-configuration`,
|
|
3548
|
+
intlLabel: { id: getTranslation("settings.link.configuration"), defaultMessage: "Configuration" },
|
|
3549
|
+
to: `${PLUGIN_ID}/configuration`,
|
|
3550
|
+
permissions: PERMISSIONS.settingsRead,
|
|
3551
|
+
Component: () => import("./ConfigurationPage-C9CPBfWy.mjs")
|
|
3552
|
+
},
|
|
3553
|
+
{
|
|
3554
|
+
id: `${PLUGIN_ID}-custom-blocks`,
|
|
3555
|
+
intlLabel: { id: getTranslation("settings.link.customBlocks"), defaultMessage: "Custom blocks" },
|
|
3556
|
+
to: `${PLUGIN_ID}/custom-blocks`,
|
|
3557
|
+
permissions: PERMISSIONS.customBlocksManage,
|
|
3558
|
+
Component: () => import("./CustomBlocksPage-DB5nnCsn.mjs")
|
|
3559
|
+
},
|
|
3560
|
+
{
|
|
3561
|
+
id: `${PLUGIN_ID}-templates`,
|
|
3562
|
+
intlLabel: { id: getTranslation("settings.link.templates"), defaultMessage: "Templates" },
|
|
3563
|
+
to: `${PLUGIN_ID}/templates`,
|
|
3564
|
+
permissions: PERMISSIONS.templatesManage,
|
|
3565
|
+
Component: () => import("./TemplatesPage-VsZ_Mc0R.mjs")
|
|
3566
|
+
},
|
|
3567
|
+
{
|
|
3568
|
+
id: `${PLUGIN_ID}-audit`,
|
|
3569
|
+
intlLabel: { id: getTranslation("settings.link.audit"), defaultMessage: "Audit trail" },
|
|
3570
|
+
to: `${PLUGIN_ID}/audit`,
|
|
3571
|
+
permissions: PERMISSIONS.auditRead,
|
|
3572
|
+
Component: () => import("./AuditPage-Be78C7EC.mjs")
|
|
3573
|
+
}
|
|
3574
|
+
]
|
|
3575
|
+
);
|
|
3576
|
+
if (typeof app.addMiddlewares === "function")
|
|
3577
|
+
app.addMiddlewares([
|
|
3578
|
+
createLogoutMiddleware({ onLogout: () => clearDrafts() }),
|
|
3579
|
+
createSessionMiddleware
|
|
3580
|
+
]);
|
|
3581
|
+
},
|
|
3582
|
+
/**
|
|
3583
|
+
* 2.5: the one Magic Editor X side panel of the Content Manager edit view (presence, comments,
|
|
3584
|
+
* quality). Other features add a section to `components/EntryPanel/sections.jsx`, never a panel.
|
|
3585
|
+
* @param {object} app - Strapi admin app.
|
|
3586
|
+
*/
|
|
3587
|
+
bootstrap(app) {
|
|
3588
|
+
const contentManager = app.getPlugin && app.getPlugin("content-manager");
|
|
3589
|
+
const apis = contentManager && contentManager.apis;
|
|
3590
|
+
if (apis && typeof apis.addEditViewSidePanel === "function") apis.addEditViewSidePanel([EntryPanel]);
|
|
3591
|
+
},
|
|
3592
|
+
/**
|
|
3593
|
+
* @param {{ locales: string[] }} options - Admin locales.
|
|
3594
|
+
* @returns {Promise<Array<{ data: object, locale: string }>>} Translations.
|
|
3595
|
+
*/
|
|
3596
|
+
async registerTrads({ locales }) {
|
|
3597
|
+
return Promise.all(
|
|
3598
|
+
locales.map(async (locale) => {
|
|
3599
|
+
try {
|
|
3600
|
+
return { data: await loadMessages(locale), locale };
|
|
3601
|
+
} catch {
|
|
3602
|
+
return { data: {}, locale };
|
|
3603
|
+
}
|
|
3604
|
+
})
|
|
3605
|
+
);
|
|
3606
|
+
}
|
|
3607
|
+
};
|
|
3608
|
+
export {
|
|
3609
|
+
mergeRect as $,
|
|
3610
|
+
ANNOTATION_KEY as A,
|
|
3611
|
+
BLOCK_POLICIES as B,
|
|
3612
|
+
COLOR_ATTRIBUTE as C,
|
|
3613
|
+
FOOTNOTE_ID as D,
|
|
3614
|
+
canonicalInlineExtras as E,
|
|
3615
|
+
FOOTNOTE_ATTRIBUTES as F,
|
|
3616
|
+
TABLE_LIMITS as G,
|
|
3617
|
+
HIGHLIGHT_ATTRIBUTE as H,
|
|
3618
|
+
INTERNAL_LINK_ATTRIBUTES as I,
|
|
3619
|
+
cleanTableAlign as J,
|
|
3620
|
+
normalizeMerges as K,
|
|
3621
|
+
TABLE_ALIGNS as L,
|
|
3622
|
+
MATH_ATTRIBUTE as M,
|
|
3623
|
+
toTableModel as N,
|
|
3624
|
+
isBlankInlineHtml as O,
|
|
3625
|
+
PERMISSIONS as P,
|
|
3626
|
+
mergeMap as Q,
|
|
3627
|
+
headerKind as R,
|
|
3628
|
+
spanAt as S,
|
|
3629
|
+
TRACK_KINDS as T,
|
|
3630
|
+
neighbourRect as U,
|
|
3631
|
+
canMergeRect as V,
|
|
3632
|
+
deleteColumn as W,
|
|
3633
|
+
insertColumn as X,
|
|
3634
|
+
deleteRow as Y,
|
|
3635
|
+
insertRow as Z,
|
|
3636
|
+
splitCell as _,
|
|
3637
|
+
sanitizeTemplate as a,
|
|
3638
|
+
VOID_TAGS as a$,
|
|
3639
|
+
expandRect as a0,
|
|
3640
|
+
setColumnAlign as a1,
|
|
3641
|
+
setHeaderRow as a2,
|
|
3642
|
+
parseTabularText as a3,
|
|
3643
|
+
pasteGrid as a4,
|
|
3644
|
+
prepareEditorValue as a5,
|
|
3645
|
+
fromTableModel as a6,
|
|
3646
|
+
decodeEntities as a7,
|
|
3647
|
+
MAX_MATH_TEX as a8,
|
|
3648
|
+
MAX_INLINE_TEX as a9,
|
|
3649
|
+
inlineToHtml as aA,
|
|
3650
|
+
cleanMediaFile as aB,
|
|
3651
|
+
textNode as aC,
|
|
3652
|
+
escapeHtml as aD,
|
|
3653
|
+
inlineNodesToRender as aE,
|
|
3654
|
+
inlineToNodes as aF,
|
|
3655
|
+
serializeNodes as aG,
|
|
3656
|
+
toNodes as aH,
|
|
3657
|
+
tableLayout as aI,
|
|
3658
|
+
TAG_NAME as aJ,
|
|
3659
|
+
isSafeAttributeName as aK,
|
|
3660
|
+
LINK_SCHEMES as aL,
|
|
3661
|
+
RENDER_STATE as aM,
|
|
3662
|
+
isMathNode as aN,
|
|
3663
|
+
isFootnoteNode as aO,
|
|
3664
|
+
resolveLinkAttributes as aP,
|
|
3665
|
+
normalizePalette as aQ,
|
|
3666
|
+
isColorNode as aR,
|
|
3667
|
+
isHighlightNode as aS,
|
|
3668
|
+
paletteCss as aT,
|
|
3669
|
+
colorClass as aU,
|
|
3670
|
+
highlightClass as aV,
|
|
3671
|
+
cloneJsonSafe as aW,
|
|
3672
|
+
isPositionalFieldPath as aX,
|
|
3673
|
+
isValidFieldKey as aY,
|
|
3674
|
+
basePathOf as aZ,
|
|
3675
|
+
nodesTextContent as a_,
|
|
3676
|
+
MAX_FOOTNOTE_NOTE as aa,
|
|
3677
|
+
parseInline as ab,
|
|
3678
|
+
nodesToText as ac,
|
|
3679
|
+
nodesToHtml as ad,
|
|
3680
|
+
cleanAnnotationAttributes as ae,
|
|
3681
|
+
BUILT_IN_MARKER_VERSION as af,
|
|
3682
|
+
isBuiltInBlock as ag,
|
|
3683
|
+
BUILT_IN_MARKER as ah,
|
|
3684
|
+
activeBlocks as ai,
|
|
3685
|
+
activeAnnotations as aj,
|
|
3686
|
+
computeStats as ak,
|
|
3687
|
+
isPlainObject as al,
|
|
3688
|
+
ANNOTATION_NAME as am,
|
|
3689
|
+
mapFootnoteNotes as an,
|
|
3690
|
+
walkInlineElements as ao,
|
|
3691
|
+
registeredBlockOf as ap,
|
|
3692
|
+
hasPalette as aq,
|
|
3693
|
+
buildCodeBlockTools as ar,
|
|
3694
|
+
MARKED_BLOCKS as as,
|
|
3695
|
+
el$1 as at,
|
|
3696
|
+
normalizeEditorDataSafe as au,
|
|
3697
|
+
EDITOR_DATA_LIMITS as av,
|
|
3698
|
+
byteLength as aw,
|
|
3699
|
+
inlineToText as ax,
|
|
3700
|
+
compileBlockList as ay,
|
|
3701
|
+
sanitizeTunes as az,
|
|
3702
|
+
el as b,
|
|
3703
|
+
safeStyle as b0,
|
|
3704
|
+
draftIdentity as b1,
|
|
3705
|
+
pruneDrafts as b2,
|
|
3706
|
+
getDraft as b3,
|
|
3707
|
+
restoreDecision as b4,
|
|
3708
|
+
deleteDraft as b5,
|
|
3709
|
+
DRAFT_WRITE_DELAY_MS as b6,
|
|
3710
|
+
ttlMsOf as b7,
|
|
3711
|
+
bufferAction as b8,
|
|
3712
|
+
buildDraftRecord as b9,
|
|
3713
|
+
putDraft as ba,
|
|
3714
|
+
relativeTimeOf as bb,
|
|
3715
|
+
abbreviationAnnotation as bc,
|
|
3716
|
+
annotationsFromSummaries as bd,
|
|
3717
|
+
createFieldKey as be,
|
|
3718
|
+
savedValueOf as bf,
|
|
3719
|
+
prepareRemoteData as bg,
|
|
3720
|
+
createTabId as bh,
|
|
3721
|
+
isLoggedOut as bi,
|
|
3722
|
+
nextDelay as bj,
|
|
3723
|
+
IDLE_AFTER_MS as bk,
|
|
3724
|
+
fieldsWith as bl,
|
|
3725
|
+
fieldProblems as bm,
|
|
3726
|
+
fieldIdentity as bn,
|
|
3727
|
+
index as bo,
|
|
3728
|
+
codeBlocks as c,
|
|
3729
|
+
select as d,
|
|
3730
|
+
entryPath as e,
|
|
3731
|
+
input as f,
|
|
3732
|
+
getTranslation as g,
|
|
3733
|
+
field as h,
|
|
3734
|
+
isHexColor as i,
|
|
3735
|
+
ANNOTATION_VALUE_PREFIX as j,
|
|
3736
|
+
ANNOTATION_ATTRIBUTE as k,
|
|
3737
|
+
ICONS as l,
|
|
3738
|
+
mergeCodeBlocks as m,
|
|
3739
|
+
button as n,
|
|
3740
|
+
sanitizeUrl as o,
|
|
3741
|
+
MAX_GALLERY_IMAGES as p,
|
|
3742
|
+
cleanFocalPoint as q,
|
|
3743
|
+
MAX_MEDIA_TRACKS as r,
|
|
3744
|
+
sanitizeSvg as s,
|
|
3745
|
+
tokenLabel as t,
|
|
3746
|
+
editable as u,
|
|
3747
|
+
sanitizeInline as v,
|
|
3748
|
+
MAX_CODE_FILENAME as w,
|
|
3749
|
+
cleanCodeFilename as x,
|
|
3750
|
+
hashString as y,
|
|
3751
|
+
escapeHtml$1 as z
|
|
3752
|
+
};
|