@novaprose/tinymce-compat 0.4.0 → 0.5.1
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/README.md +12 -3
- package/dist/index.cjs +488 -10
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +134 -4
- package/dist/index.d.ts +134 -4
- package/dist/index.js +495 -11
- package/dist/index.js.map +1 -1
- package/dist/novaprose.css +74 -0
- package/dist/tinymce.global.js +23 -23
- package/dist/tinymce.global.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -51,8 +51,16 @@ editor.on('change', () => console.log(editor.getContent()));
|
|
|
51
51
|
`addCommand`, `on`/`off`/`once`/`fire`/`dispatch`, `selection`, `undoManager`, `mode`,
|
|
52
52
|
`isDirty`/`setDirty`/`save`, `focus`, `remove`, `notificationManager.open`,
|
|
53
53
|
`windowManager.alert`/`confirm`, `getParam`/`options.get`.
|
|
54
|
+
- **Dialogs**: `windowManager.open` with `panel` or `tabpanel` bodies — `input`, `textarea`,
|
|
55
|
+
`checkbox`, `selectbox`/`listbox`, `urlinput`, `colorinput`, `button`, `htmlpanel` (sanitized),
|
|
56
|
+
`alertbanner`, `label`, `grid`, `bar`; `initialData`, `onSubmit`, `onChange`, `onAction`, `onCancel`,
|
|
57
|
+
`onClose`, `onTabChange`; `api.getData/setData/close/block/unblock/focus/disable/enable/showTab`.
|
|
58
|
+
- **`editor.dom`** (reading): `select`, `get`, `getParent(s)`, `is`, `getAttrib`, `hasClass`, `encode`,
|
|
59
|
+
`decode`, `create`, `uniqueId`. **`editor.formatter`**: `apply`/`remove`/`toggle`/`match` for built-in
|
|
60
|
+
formats (`bold`…`h1`…`alignleft`…, `forecolor`, `hilitecolor`, `fontname`, `fontsize`).
|
|
54
61
|
- **`editor.ui.registry`**: `addButton`, `addToggleButton` (with `onSetup` and `api.setActive`),
|
|
55
|
-
`addMenuButton` (`fetch`), `addMenuItem`, `addIcon`; TinyMCE icon names map to
|
|
62
|
+
`addMenuButton` (`fetch`), `addMenuItem`, `addContextToolbar`, `addIcon`; TinyMCE icon names map to
|
|
63
|
+
NovaProse icons.
|
|
56
64
|
- **Events**: `init`, `input`, `change` (on blur after edits), `NodeChange`, `focus`, `blur`,
|
|
57
65
|
`keydown`, `keyup`, `click`, `paste`, `SetContent`, `GetContent`, `SaveContent`, `ExecCommand`,
|
|
58
66
|
`Dirty`, `remove`.
|
|
@@ -67,5 +75,6 @@ Plugins built into NovaProse (`lists`, `link`, `image`, `table`, `code`, `search
|
|
|
67
75
|
nothing; optional ones (`charmap`, `emoticons`, `anchor`, `media`, `autosave`…) load by name.
|
|
68
76
|
Unknown options and plugins don't break initialization — they are listed in a console warning.
|
|
69
77
|
|
|
70
|
-
Not yet:
|
|
71
|
-
|
|
78
|
+
Not yet: direct DOM edits through `editor.dom` (NovaProse redraws the text from its document model —
|
|
79
|
+
use `setContent`, `insertContent` or `editor.formatter`), custom formats (`formatter.register`),
|
|
80
|
+
`addContextForm`, iframe content (`content_css`). See [the migration guide](https://novaprose.com/guides/migrating-from-tinymce/).
|
package/dist/index.cjs
CHANGED
|
@@ -30,6 +30,301 @@ var import_core3 = require("@novaprose/core");
|
|
|
30
30
|
|
|
31
31
|
// src/editor.ts
|
|
32
32
|
var import_core = require("@novaprose/core");
|
|
33
|
+
|
|
34
|
+
// src/dialog.ts
|
|
35
|
+
var uid = 0;
|
|
36
|
+
var nextId = () => `nova-compat-field-${++uid}`;
|
|
37
|
+
function el(tag, className, text) {
|
|
38
|
+
const node = document.createElement(tag);
|
|
39
|
+
if (className) node.className = className;
|
|
40
|
+
if (text !== void 0) node.textContent = text;
|
|
41
|
+
return node;
|
|
42
|
+
}
|
|
43
|
+
function safeHtml(html) {
|
|
44
|
+
const template = document.createElement("template");
|
|
45
|
+
template.innerHTML = html;
|
|
46
|
+
template.content.querySelectorAll("script, style, iframe, object, embed, link, meta, base, form").forEach((node) => node.remove());
|
|
47
|
+
template.content.querySelectorAll("*").forEach((node) => {
|
|
48
|
+
for (const attr of [...node.attributes]) {
|
|
49
|
+
const value = attr.value.trim().toLowerCase();
|
|
50
|
+
if (attr.name.startsWith("on") || /^(javascript|vbscript|data):/.test(value)) {
|
|
51
|
+
node.removeAttribute(attr.name);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
return template.content;
|
|
56
|
+
}
|
|
57
|
+
function openSpecDialog(editor, spec, reopen) {
|
|
58
|
+
const controls = /* @__PURE__ */ new Map();
|
|
59
|
+
const tabs = /* @__PURE__ */ new Map();
|
|
60
|
+
let currentTab = "";
|
|
61
|
+
let closedByApi = false;
|
|
62
|
+
let handle = null;
|
|
63
|
+
const notifyChange = (name) => spec.onChange?.(api, { name });
|
|
64
|
+
function field(item, input) {
|
|
65
|
+
const id = nextId();
|
|
66
|
+
input.id = id;
|
|
67
|
+
const wrap = el("div", "nova-field");
|
|
68
|
+
if (item.label) {
|
|
69
|
+
const label = el("label", "nova-label", item.label);
|
|
70
|
+
label.htmlFor = id;
|
|
71
|
+
wrap.append(label);
|
|
72
|
+
}
|
|
73
|
+
wrap.append(input);
|
|
74
|
+
return wrap;
|
|
75
|
+
}
|
|
76
|
+
function render(item) {
|
|
77
|
+
const name = item.name ?? "";
|
|
78
|
+
const enabled = item.enabled !== false && item.disabled !== true;
|
|
79
|
+
switch (item.type) {
|
|
80
|
+
case "input":
|
|
81
|
+
case "urlinput":
|
|
82
|
+
case "colorinput": {
|
|
83
|
+
const input = el("input", "nova-input");
|
|
84
|
+
input.type = item.type === "colorinput" ? "color" : item.type === "urlinput" ? "url" : "text";
|
|
85
|
+
if (item.inputMode) input.inputMode = item.inputMode;
|
|
86
|
+
if (item.placeholder) input.placeholder = item.placeholder;
|
|
87
|
+
input.disabled = !enabled;
|
|
88
|
+
input.addEventListener("input", () => notifyChange(name));
|
|
89
|
+
if (name) {
|
|
90
|
+
controls.set(name, {
|
|
91
|
+
// urlinput хранит { value }, остальные — строку.
|
|
92
|
+
get: () => item.type === "urlinput" ? { value: input.value } : input.value,
|
|
93
|
+
set: (value) => {
|
|
94
|
+
input.value = String(
|
|
95
|
+
item.type === "urlinput" && value && typeof value === "object" ? value.value ?? "" : value ?? ""
|
|
96
|
+
);
|
|
97
|
+
},
|
|
98
|
+
focus: () => input.focus(),
|
|
99
|
+
setEnabled: (on) => input.disabled = !on
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
return field(item, input);
|
|
103
|
+
}
|
|
104
|
+
case "textarea": {
|
|
105
|
+
const input = el("textarea", "nova-input");
|
|
106
|
+
input.rows = item.maximized ? 10 : 4;
|
|
107
|
+
if (item.placeholder) input.placeholder = item.placeholder;
|
|
108
|
+
input.disabled = !enabled;
|
|
109
|
+
input.addEventListener("input", () => notifyChange(name));
|
|
110
|
+
if (name) {
|
|
111
|
+
controls.set(name, {
|
|
112
|
+
get: () => input.value,
|
|
113
|
+
set: (value) => input.value = String(value ?? ""),
|
|
114
|
+
focus: () => input.focus(),
|
|
115
|
+
setEnabled: (on) => input.disabled = !on
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
return field(item, input);
|
|
119
|
+
}
|
|
120
|
+
case "checkbox": {
|
|
121
|
+
const input = el("input", "nova-checkbox");
|
|
122
|
+
input.type = "checkbox";
|
|
123
|
+
input.id = nextId();
|
|
124
|
+
input.disabled = !enabled;
|
|
125
|
+
input.addEventListener("change", () => notifyChange(name));
|
|
126
|
+
const label = el("label", void 0, item.label ?? "");
|
|
127
|
+
label.htmlFor = input.id;
|
|
128
|
+
if (name) {
|
|
129
|
+
controls.set(name, {
|
|
130
|
+
get: () => input.checked,
|
|
131
|
+
set: (value) => input.checked = Boolean(value),
|
|
132
|
+
focus: () => input.focus(),
|
|
133
|
+
setEnabled: (on) => input.disabled = !on
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
const wrap = el("div", "nova-field nova-field-inline");
|
|
137
|
+
wrap.append(input, label);
|
|
138
|
+
return wrap;
|
|
139
|
+
}
|
|
140
|
+
case "selectbox":
|
|
141
|
+
case "listbox": {
|
|
142
|
+
const select = el("select", "nova-input nova-select");
|
|
143
|
+
for (const option of item.items ?? []) {
|
|
144
|
+
const node = el("option", void 0, option.text);
|
|
145
|
+
node.value = option.value;
|
|
146
|
+
select.append(node);
|
|
147
|
+
}
|
|
148
|
+
select.disabled = !enabled;
|
|
149
|
+
select.addEventListener("change", () => notifyChange(name));
|
|
150
|
+
if (name) {
|
|
151
|
+
controls.set(name, {
|
|
152
|
+
get: () => select.value,
|
|
153
|
+
set: (value) => select.value = String(value ?? ""),
|
|
154
|
+
focus: () => select.focus(),
|
|
155
|
+
setEnabled: (on) => select.disabled = !on
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
return field(item, select);
|
|
159
|
+
}
|
|
160
|
+
case "button": {
|
|
161
|
+
const button = el(
|
|
162
|
+
"button",
|
|
163
|
+
item.primary || item.buttonType === "primary" ? "nova-button nova-button-primary" : "nova-button",
|
|
164
|
+
item.text ?? ""
|
|
165
|
+
);
|
|
166
|
+
button.type = "button";
|
|
167
|
+
button.disabled = !enabled;
|
|
168
|
+
button.addEventListener("click", () => spec.onAction?.(api, { name }));
|
|
169
|
+
if (name) {
|
|
170
|
+
controls.set(name, {
|
|
171
|
+
get: () => void 0,
|
|
172
|
+
set: () => {
|
|
173
|
+
},
|
|
174
|
+
focus: () => button.focus(),
|
|
175
|
+
setEnabled: (on) => button.disabled = !on
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
return button;
|
|
179
|
+
}
|
|
180
|
+
case "htmlpanel": {
|
|
181
|
+
const panel = el("div", "nova-compat-html");
|
|
182
|
+
panel.append(safeHtml(item.html ?? ""));
|
|
183
|
+
return panel;
|
|
184
|
+
}
|
|
185
|
+
case "alertbanner": {
|
|
186
|
+
const banner = el("p", `nova-compat-alert is-${item.level ?? "info"}`, item.text ?? "");
|
|
187
|
+
banner.setAttribute("role", item.level === "error" ? "alert" : "status");
|
|
188
|
+
return banner;
|
|
189
|
+
}
|
|
190
|
+
case "label": {
|
|
191
|
+
const group = el("div", "nova-compat-group");
|
|
192
|
+
group.append(el("p", "nova-label", item.label ?? ""));
|
|
193
|
+
for (const child of item.items ?? []) {
|
|
194
|
+
const node = render(child);
|
|
195
|
+
if (node) group.append(node);
|
|
196
|
+
}
|
|
197
|
+
return group;
|
|
198
|
+
}
|
|
199
|
+
case "grid":
|
|
200
|
+
case "bar": {
|
|
201
|
+
const grid = el("div", "nova-compat-grid");
|
|
202
|
+
const columns = item.type === "bar" ? item.items?.length ?? 1 : item.columns === "auto" ? 2 : item.columns ?? 2;
|
|
203
|
+
grid.style.gridTemplateColumns = `repeat(${Number(columns) || 2}, minmax(0, 1fr))`;
|
|
204
|
+
for (const child of item.items ?? []) {
|
|
205
|
+
const node = render(child);
|
|
206
|
+
if (node) grid.append(node);
|
|
207
|
+
}
|
|
208
|
+
return grid;
|
|
209
|
+
}
|
|
210
|
+
default:
|
|
211
|
+
console.warn(`[nova] tinymce-compat: dialog component "${item.type}" is not supported yet`);
|
|
212
|
+
return null;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
const body = el("div", "nova-compat-dialog nova-form");
|
|
216
|
+
if (spec.body.type === "tabpanel") {
|
|
217
|
+
const tabList = el("div", "nova-compat-tabs");
|
|
218
|
+
tabList.setAttribute("role", "tablist");
|
|
219
|
+
body.append(tabList);
|
|
220
|
+
spec.body.tabs.forEach((tab, index) => {
|
|
221
|
+
const name = tab.name ?? tab.title;
|
|
222
|
+
const button = el("button", "nova-compat-tab", tab.title);
|
|
223
|
+
button.type = "button";
|
|
224
|
+
button.setAttribute("role", "tab");
|
|
225
|
+
const panel = el("div", "nova-compat-tabpanel");
|
|
226
|
+
panel.setAttribute("role", "tabpanel");
|
|
227
|
+
for (const item of tab.items) {
|
|
228
|
+
const node = render(item);
|
|
229
|
+
if (node) panel.append(node);
|
|
230
|
+
}
|
|
231
|
+
button.addEventListener("click", () => api.showTab(name));
|
|
232
|
+
tabs.set(name, { button, panel });
|
|
233
|
+
tabList.append(button);
|
|
234
|
+
body.append(panel);
|
|
235
|
+
if (index === 0) currentTab = name;
|
|
236
|
+
});
|
|
237
|
+
} else {
|
|
238
|
+
for (const item of spec.body.items) {
|
|
239
|
+
const node = render(item);
|
|
240
|
+
if (node) body.append(node);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
const buttons = spec.buttons ?? [];
|
|
244
|
+
const submit = buttons.find((b) => b.type === "submit");
|
|
245
|
+
const custom = buttons.filter((b) => b.type === "custom");
|
|
246
|
+
if (custom.length) {
|
|
247
|
+
const row = el("div", "nova-compat-actions");
|
|
248
|
+
for (const b of custom) {
|
|
249
|
+
const button = el(
|
|
250
|
+
"button",
|
|
251
|
+
b.primary ? "nova-button nova-button-primary" : "nova-button",
|
|
252
|
+
b.text
|
|
253
|
+
);
|
|
254
|
+
button.type = "button";
|
|
255
|
+
button.disabled = b.enabled === false;
|
|
256
|
+
button.addEventListener("click", () => spec.onAction?.(api, { name: b.name ?? b.text }));
|
|
257
|
+
if (b.name) {
|
|
258
|
+
controls.set(b.name, {
|
|
259
|
+
get: () => void 0,
|
|
260
|
+
set: () => {
|
|
261
|
+
},
|
|
262
|
+
focus: () => button.focus(),
|
|
263
|
+
setEnabled: (on) => button.disabled = !on
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
row.append(button);
|
|
267
|
+
}
|
|
268
|
+
body.append(row);
|
|
269
|
+
}
|
|
270
|
+
const api = {
|
|
271
|
+
getData: () => {
|
|
272
|
+
const data = {};
|
|
273
|
+
controls.forEach((control, name) => {
|
|
274
|
+
const value = control.get();
|
|
275
|
+
if (value !== void 0) data[name] = value;
|
|
276
|
+
});
|
|
277
|
+
return data;
|
|
278
|
+
},
|
|
279
|
+
setData: (data) => {
|
|
280
|
+
for (const [name, value] of Object.entries(data)) controls.get(name)?.set(value);
|
|
281
|
+
},
|
|
282
|
+
close: () => {
|
|
283
|
+
closedByApi = true;
|
|
284
|
+
handle?.close();
|
|
285
|
+
},
|
|
286
|
+
block: () => handle?.setBusy(true),
|
|
287
|
+
unblock: () => handle?.setBusy(false),
|
|
288
|
+
focus: (name) => controls.get(name)?.focus(),
|
|
289
|
+
disable: (name) => controls.get(name)?.setEnabled(false),
|
|
290
|
+
enable: (name) => controls.get(name)?.setEnabled(true),
|
|
291
|
+
showTab: (name) => {
|
|
292
|
+
if (!tabs.has(name)) return;
|
|
293
|
+
const old = currentTab;
|
|
294
|
+
currentTab = name;
|
|
295
|
+
tabs.forEach(({ button, panel }, key) => {
|
|
296
|
+
const on = key === name;
|
|
297
|
+
button.setAttribute("aria-selected", String(on));
|
|
298
|
+
button.tabIndex = on ? 0 : -1;
|
|
299
|
+
panel.hidden = !on;
|
|
300
|
+
});
|
|
301
|
+
if (old && old !== name) spec.onTabChange?.(api, { newTabName: name, oldTabName: old });
|
|
302
|
+
},
|
|
303
|
+
redial: (next) => {
|
|
304
|
+
api.close();
|
|
305
|
+
Object.assign(api, reopen(next));
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
if (spec.initialData) api.setData(spec.initialData);
|
|
309
|
+
if (currentTab) api.showTab(currentTab);
|
|
310
|
+
handle = editor.ui.openDialog({
|
|
311
|
+
title: spec.title,
|
|
312
|
+
body,
|
|
313
|
+
...submit ? { submitLabel: submit.text } : {},
|
|
314
|
+
...spec.size === "large" ? { size: "large" } : {},
|
|
315
|
+
onSubmit: () => {
|
|
316
|
+
spec.onSubmit?.(api);
|
|
317
|
+
return closedByApi;
|
|
318
|
+
},
|
|
319
|
+
onClose: () => {
|
|
320
|
+
if (!closedByApi) spec.onCancel?.(api);
|
|
321
|
+
spec.onClose?.();
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
return api;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// src/editor.ts
|
|
33
328
|
var import_prosemirror_model = require("prosemirror-model");
|
|
34
329
|
var import_prosemirror_state = require("prosemirror-state");
|
|
35
330
|
var serializer = new import_core.HTMLSerializer();
|
|
@@ -124,6 +419,55 @@ var ICON_NAMES = {
|
|
|
124
419
|
"highlight-bg-color": "highlight",
|
|
125
420
|
"line-height": "lineHeight"
|
|
126
421
|
};
|
|
422
|
+
var FORMAT_COMMANDS = {
|
|
423
|
+
bold: "bold",
|
|
424
|
+
italic: "italic",
|
|
425
|
+
underline: "underline",
|
|
426
|
+
strikethrough: "strike",
|
|
427
|
+
superscript: "superscript",
|
|
428
|
+
subscript: "subscript",
|
|
429
|
+
code: "code",
|
|
430
|
+
p: "paragraph",
|
|
431
|
+
h1: "h1",
|
|
432
|
+
h2: "h2",
|
|
433
|
+
h3: "h3",
|
|
434
|
+
h4: "h4",
|
|
435
|
+
h5: "h5",
|
|
436
|
+
h6: "h6",
|
|
437
|
+
blockquote: "blockquote",
|
|
438
|
+
pre: "codeBlock",
|
|
439
|
+
alignleft: "alignLeft",
|
|
440
|
+
aligncenter: "alignCenter",
|
|
441
|
+
alignright: "alignRight",
|
|
442
|
+
alignjustify: "alignJustify"
|
|
443
|
+
};
|
|
444
|
+
var TAG_FORMATS = /* @__PURE__ */ new Set(["p", "h1", "h2", "h3", "h4", "h5", "h6", "blockquote", "pre"]);
|
|
445
|
+
var BLOCK_FORMATS = /* @__PURE__ */ new Set([
|
|
446
|
+
"p",
|
|
447
|
+
"h1",
|
|
448
|
+
"h2",
|
|
449
|
+
"h3",
|
|
450
|
+
"h4",
|
|
451
|
+
"h5",
|
|
452
|
+
"h6",
|
|
453
|
+
"blockquote",
|
|
454
|
+
"pre",
|
|
455
|
+
"alignleft",
|
|
456
|
+
"aligncenter",
|
|
457
|
+
"alignright",
|
|
458
|
+
"alignjustify"
|
|
459
|
+
]);
|
|
460
|
+
var STYLE_FORMATS = {
|
|
461
|
+
forecolor: "color",
|
|
462
|
+
hilitecolor: "background",
|
|
463
|
+
fontname: "font_family",
|
|
464
|
+
fontsize: "font_size"
|
|
465
|
+
};
|
|
466
|
+
function warnDom(method) {
|
|
467
|
+
console.warn(
|
|
468
|
+
`[nova] tinymce-compat: editor.dom.${method} changes the editor DOM directly, which NovaProse redraws from its document model. Use setContent, insertContent or editor.formatter instead.`
|
|
469
|
+
);
|
|
470
|
+
}
|
|
127
471
|
var customIcons = /* @__PURE__ */ new Map();
|
|
128
472
|
function resolveIcon(name) {
|
|
129
473
|
if (typeof name !== "string" || !name) return void 0;
|
|
@@ -204,7 +548,7 @@ var CompatEditor = class {
|
|
|
204
548
|
const container = document.createElement("div");
|
|
205
549
|
container.append(template.content);
|
|
206
550
|
const parser = (0, import_core.parserFor)(this.core.schema);
|
|
207
|
-
const hasBlocks = [...container.children].some((
|
|
551
|
+
const hasBlocks = [...container.children].some((el2) => BLOCK_TAGS.has(el2.tagName));
|
|
208
552
|
const slice = hasBlocks ? new import_prosemirror_model.Slice(parser.parse(container).content, 0, 0) : parser.parseSlice(container);
|
|
209
553
|
const { state, dispatch } = this.core.view;
|
|
210
554
|
dispatch(state.tr.replaceSelection(slice).scrollIntoView());
|
|
@@ -460,13 +804,7 @@ var CompatEditor = class {
|
|
|
460
804
|
onClose: () => callback?.(confirmed)
|
|
461
805
|
});
|
|
462
806
|
},
|
|
463
|
-
open: () =>
|
|
464
|
-
console.warn(
|
|
465
|
-
"[nova] tinymce-compat: windowManager.open is not supported yet \u2014 use editor.ui.openDialog"
|
|
466
|
-
);
|
|
467
|
-
return { close: () => {
|
|
468
|
-
} };
|
|
469
|
-
}
|
|
807
|
+
open: (spec) => openSpecDialog(this.core, spec, (next) => this.windowManager.open(next))
|
|
470
808
|
};
|
|
471
809
|
/** editor.ui.registry — кнопки и пункты меню в формате TinyMCE поверх реестра NovaProse. */
|
|
472
810
|
ui = {
|
|
@@ -503,8 +841,23 @@ var CompatEditor = class {
|
|
|
503
841
|
onAction: () => spec.onAction?.({})
|
|
504
842
|
});
|
|
505
843
|
},
|
|
506
|
-
|
|
507
|
-
|
|
844
|
+
/**
|
|
845
|
+
* Контекстная панель: predicate(node) получает элемент под курсором, items — кнопки тулбара.
|
|
846
|
+
* position: 'node' (над элементом) или 'selection'/'line' (у выделения).
|
|
847
|
+
*/
|
|
848
|
+
addContextToolbar: (name, spec) => {
|
|
849
|
+
this.core.ui.registry.addContextToolbar(name, {
|
|
850
|
+
label: spec.label ?? name,
|
|
851
|
+
items: spec.items,
|
|
852
|
+
position: spec.position === "node" ? "node" : "selection",
|
|
853
|
+
predicate: () => {
|
|
854
|
+
const node = this.selection.getNode();
|
|
855
|
+
return !!node && this.getBody().contains(node) && spec.predicate(node);
|
|
856
|
+
}
|
|
857
|
+
});
|
|
858
|
+
},
|
|
859
|
+
addContextForm: () => {
|
|
860
|
+
console.warn("[nova] tinymce-compat: addContextForm is not supported yet");
|
|
508
861
|
},
|
|
509
862
|
getAll: () => ({})
|
|
510
863
|
}
|
|
@@ -530,6 +883,131 @@ var CompatEditor = class {
|
|
|
530
883
|
else this.once("init", run);
|
|
531
884
|
}
|
|
532
885
|
}
|
|
886
|
+
/**
|
|
887
|
+
* editor.dom — то, что имеет смысл поверх модели документа: поиск и чтение. Менять DOM текста
|
|
888
|
+
* напрямую нельзя (редактор перерисует его из модели) — для правок есть setContent/insertContent/
|
|
889
|
+
* formatter; такие вызовы пишут предупреждение.
|
|
890
|
+
*/
|
|
891
|
+
dom = {
|
|
892
|
+
select: (selector, scope) => [
|
|
893
|
+
...(scope ?? this.getBody()).querySelectorAll(selector)
|
|
894
|
+
],
|
|
895
|
+
get: (id) => typeof id === "string" ? this.getBody().querySelector(`#${CSS.escape(id)}`) : id,
|
|
896
|
+
getParent: (node, selector) => {
|
|
897
|
+
let current = node instanceof Element ? node : node?.parentElement ?? null;
|
|
898
|
+
while (current && current !== this.getBody()) {
|
|
899
|
+
if (typeof selector === "string" ? current.matches(selector) : selector(current))
|
|
900
|
+
return current;
|
|
901
|
+
current = current.parentElement;
|
|
902
|
+
}
|
|
903
|
+
return null;
|
|
904
|
+
},
|
|
905
|
+
getParents: (node, selector) => {
|
|
906
|
+
const result = [];
|
|
907
|
+
let current = node instanceof Element ? node : node?.parentElement ?? null;
|
|
908
|
+
while (current && current !== this.getBody()) {
|
|
909
|
+
if (!selector || current.matches(selector)) result.push(current);
|
|
910
|
+
current = current.parentElement;
|
|
911
|
+
}
|
|
912
|
+
return result;
|
|
913
|
+
},
|
|
914
|
+
is: (node, selector) => !!node?.matches(selector),
|
|
915
|
+
getAttrib: (node, name, fallback = "") => node?.getAttribute(name) ?? fallback,
|
|
916
|
+
hasClass: (node, name) => !!node?.classList.contains(name),
|
|
917
|
+
getStyle: (node, name) => node instanceof HTMLElement ? node.style.getPropertyValue(name) : "",
|
|
918
|
+
getRoot: () => this.getBody(),
|
|
919
|
+
encode: (text) => text.replace(/[&<>"']/g, (ch) => `&#${ch.charCodeAt(0)};`),
|
|
920
|
+
decode: (html) => {
|
|
921
|
+
const textarea = document.createElement("textarea");
|
|
922
|
+
textarea.innerHTML = html;
|
|
923
|
+
return textarea.value;
|
|
924
|
+
},
|
|
925
|
+
/** Новый (не вставленный) элемент: атрибуты — через setAttribute, содержимое — текстом. */
|
|
926
|
+
create: (tag, attrs = {}, text) => {
|
|
927
|
+
const node = document.createElement(tag);
|
|
928
|
+
for (const [name, value] of Object.entries(attrs)) {
|
|
929
|
+
if (!name.startsWith("on")) node.setAttribute(name, value);
|
|
930
|
+
}
|
|
931
|
+
if (text !== void 0) node.textContent = text;
|
|
932
|
+
return node;
|
|
933
|
+
},
|
|
934
|
+
uniqueId: (prefix = "mce_") => `${prefix}${Math.random().toString(36).slice(2, 10)}`,
|
|
935
|
+
setAttrib: () => warnDom("setAttrib"),
|
|
936
|
+
addClass: () => warnDom("addClass"),
|
|
937
|
+
removeClass: () => warnDom("removeClass"),
|
|
938
|
+
setStyle: () => warnDom("setStyle"),
|
|
939
|
+
remove: () => warnDom("remove")
|
|
940
|
+
};
|
|
941
|
+
/**
|
|
942
|
+
* editor.formatter — встроенные форматы TinyMCE: apply/remove/toggle/match.
|
|
943
|
+
* Свои форматы (register) пока не поддержаны.
|
|
944
|
+
*/
|
|
945
|
+
formatter = {
|
|
946
|
+
apply: (name, vars = {}) => this.applyFormat(name, vars, "apply"),
|
|
947
|
+
remove: (name) => this.applyFormat(name, {}, "remove"),
|
|
948
|
+
toggle: (name, vars = {}) => this.applyFormat(name, vars, this.formatter.match(name) ? "remove" : "apply"),
|
|
949
|
+
match: (name) => {
|
|
950
|
+
const key = name.toLowerCase();
|
|
951
|
+
if (TAG_FORMATS.has(key)) return this.blockMatches(key);
|
|
952
|
+
const command = FORMAT_COMMANDS[key];
|
|
953
|
+
if (command) return this.core.isActive(command);
|
|
954
|
+
const style = STYLE_FORMATS[key];
|
|
955
|
+
if (!style) return false;
|
|
956
|
+
const { state } = this.core.view;
|
|
957
|
+
const { from, to, empty } = state.selection;
|
|
958
|
+
const type = state.schema.marks[style];
|
|
959
|
+
return empty || !type ? (0, import_core.getStyle)(state, style) !== null : state.doc.rangeHasMark(from, to, type);
|
|
960
|
+
},
|
|
961
|
+
canApply: (name) => name.toLowerCase() in FORMAT_COMMANDS || name.toLowerCase() in STYLE_FORMATS,
|
|
962
|
+
register: (name) => {
|
|
963
|
+
console.warn(`[nova] tinymce-compat: formatter.register("${name}") is not supported yet`);
|
|
964
|
+
}
|
|
965
|
+
};
|
|
966
|
+
blockMatches(key) {
|
|
967
|
+
const { $from } = this.core.view.state.selection;
|
|
968
|
+
for (let depth = $from.depth; depth > 0; depth -= 1) {
|
|
969
|
+
const node = $from.node(depth);
|
|
970
|
+
const name = node.type.name;
|
|
971
|
+
if (key === "blockquote" && name === "blockquote") return true;
|
|
972
|
+
if (depth !== $from.depth) continue;
|
|
973
|
+
if (key === "p") return name === "paragraph";
|
|
974
|
+
if (key === "pre") return name === "code_block";
|
|
975
|
+
if (/^h[1-6]$/.test(key)) return name === "heading" && node.attrs.level === Number(key[1]);
|
|
976
|
+
}
|
|
977
|
+
let first = null;
|
|
978
|
+
this.core.view.state.doc.descendants((node) => {
|
|
979
|
+
if (first || !node.isTextblock) return !first;
|
|
980
|
+
first = { name: node.type.name, level: node.attrs.level };
|
|
981
|
+
return false;
|
|
982
|
+
});
|
|
983
|
+
if (!$from.depth && first) {
|
|
984
|
+
const block = first;
|
|
985
|
+
if (key === "p") return block.name === "paragraph";
|
|
986
|
+
if (key === "pre") return block.name === "code_block";
|
|
987
|
+
if (/^h[1-6]$/.test(key)) return block.name === "heading" && block.level === Number(key[1]);
|
|
988
|
+
}
|
|
989
|
+
return false;
|
|
990
|
+
}
|
|
991
|
+
applyFormat(name, vars, action) {
|
|
992
|
+
const key = name.toLowerCase();
|
|
993
|
+
const command = FORMAT_COMMANDS[key];
|
|
994
|
+
if (command) {
|
|
995
|
+
const active = this.formatter.match(key);
|
|
996
|
+
if (BLOCK_FORMATS.has(key)) {
|
|
997
|
+
if (action === "apply") this.core.exec(command);
|
|
998
|
+
else if (active) this.core.exec("paragraph");
|
|
999
|
+
} else if (action === "apply" !== active) {
|
|
1000
|
+
this.core.exec(command);
|
|
1001
|
+
}
|
|
1002
|
+
return;
|
|
1003
|
+
}
|
|
1004
|
+
const style = STYLE_FORMATS[key];
|
|
1005
|
+
if (style) {
|
|
1006
|
+
this.core.exec((0, import_core.setStyle)(style, action === "apply" ? vars.value ?? null : null));
|
|
1007
|
+
return;
|
|
1008
|
+
}
|
|
1009
|
+
console.warn(`[nova] tinymce-compat: format "${name}" is not supported yet`);
|
|
1010
|
+
}
|
|
533
1011
|
/** Убрать редактор и вернуть исходный элемент (как editor.remove()). */
|
|
534
1012
|
remove() {
|
|
535
1013
|
if (this.removed) return;
|