@novaprose/tinymce-compat 0.0.0-stage → 0.4.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/LICENSE +21 -0
- package/README.md +70 -2
- package/dist/index.cjs +950 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +222 -0
- package/dist/index.d.ts +222 -0
- package/dist/index.js +922 -0
- package/dist/index.js.map +1 -0
- package/dist/novaprose.css +2233 -0
- package/dist/tinymce.global.js +38 -0
- package/dist/tinymce.global.js.map +1 -0
- package/package.json +53 -3
package/dist/index.js
ADDED
|
@@ -0,0 +1,922 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { createEditor } from "@novaprose/core";
|
|
3
|
+
|
|
4
|
+
// src/editor.ts
|
|
5
|
+
import { HTMLSerializer, icons, parserFor } from "@novaprose/core";
|
|
6
|
+
import { Slice } from "prosemirror-model";
|
|
7
|
+
import { AllSelection, TextSelection } from "prosemirror-state";
|
|
8
|
+
var serializer = new HTMLSerializer();
|
|
9
|
+
var BLOCK_TAGS = /* @__PURE__ */ new Set([
|
|
10
|
+
"P",
|
|
11
|
+
"H1",
|
|
12
|
+
"H2",
|
|
13
|
+
"H3",
|
|
14
|
+
"H4",
|
|
15
|
+
"H5",
|
|
16
|
+
"H6",
|
|
17
|
+
"UL",
|
|
18
|
+
"OL",
|
|
19
|
+
"TABLE",
|
|
20
|
+
"BLOCKQUOTE",
|
|
21
|
+
"PRE",
|
|
22
|
+
"HR",
|
|
23
|
+
"DIV",
|
|
24
|
+
"FIGURE"
|
|
25
|
+
]);
|
|
26
|
+
var COMMANDS = {
|
|
27
|
+
bold: "bold",
|
|
28
|
+
italic: "italic",
|
|
29
|
+
underline: "underline",
|
|
30
|
+
strikethrough: "strike",
|
|
31
|
+
superscript: "superscript",
|
|
32
|
+
subscript: "subscript",
|
|
33
|
+
justifyleft: "alignLeft",
|
|
34
|
+
justifycenter: "alignCenter",
|
|
35
|
+
justifyright: "alignRight",
|
|
36
|
+
justifyfull: "alignJustify",
|
|
37
|
+
insertunorderedlist: "bulletList",
|
|
38
|
+
insertorderedlist: "orderedList",
|
|
39
|
+
indent: "indent",
|
|
40
|
+
outdent: "outdent",
|
|
41
|
+
undo: "undo",
|
|
42
|
+
redo: "redo",
|
|
43
|
+
removeformat: "clearFormatting",
|
|
44
|
+
unlink: "unlink",
|
|
45
|
+
inserthorizontalrule: "horizontalRule",
|
|
46
|
+
mceblockquote: "blockquote"
|
|
47
|
+
};
|
|
48
|
+
var FORMAT_BLOCKS = {
|
|
49
|
+
p: "paragraph",
|
|
50
|
+
h1: "h1",
|
|
51
|
+
h2: "h2",
|
|
52
|
+
h3: "h3",
|
|
53
|
+
h4: "h4",
|
|
54
|
+
h5: "h5",
|
|
55
|
+
h6: "h6",
|
|
56
|
+
blockquote: "blockquote",
|
|
57
|
+
pre: "codeBlock"
|
|
58
|
+
};
|
|
59
|
+
var ICON_NAMES = {
|
|
60
|
+
bold: "bold",
|
|
61
|
+
italic: "italic",
|
|
62
|
+
underline: "underline",
|
|
63
|
+
"strike-through": "strike",
|
|
64
|
+
link: "link",
|
|
65
|
+
unlink: "unlink",
|
|
66
|
+
image: "image",
|
|
67
|
+
table: "table",
|
|
68
|
+
undo: "undo",
|
|
69
|
+
redo: "redo",
|
|
70
|
+
"align-left": "alignLeft",
|
|
71
|
+
"align-center": "alignCenter",
|
|
72
|
+
"align-right": "alignRight",
|
|
73
|
+
"align-justify": "alignJustify",
|
|
74
|
+
"unordered-list": "bulletList",
|
|
75
|
+
"ordered-list": "orderedList",
|
|
76
|
+
indent: "indent",
|
|
77
|
+
outdent: "outdent",
|
|
78
|
+
quote: "blockquote",
|
|
79
|
+
"code-sample": "codeBlock",
|
|
80
|
+
"horizontal-rule": "horizontalRule",
|
|
81
|
+
"remove-formatting": "clearFormatting",
|
|
82
|
+
fullscreen: "fullscreen",
|
|
83
|
+
preview: "preview",
|
|
84
|
+
sourcecode: "sourceCode",
|
|
85
|
+
help: "help",
|
|
86
|
+
print: "print",
|
|
87
|
+
search: "search",
|
|
88
|
+
copy: "copy",
|
|
89
|
+
"edit-block": "edit",
|
|
90
|
+
checkmark: "check",
|
|
91
|
+
close: "close",
|
|
92
|
+
upload: "upload",
|
|
93
|
+
subscript: "subscript",
|
|
94
|
+
superscript: "superscript",
|
|
95
|
+
"paste-text": "pasteText",
|
|
96
|
+
"text-color": "textColor",
|
|
97
|
+
"highlight-bg-color": "highlight",
|
|
98
|
+
"line-height": "lineHeight"
|
|
99
|
+
};
|
|
100
|
+
var customIcons = /* @__PURE__ */ new Map();
|
|
101
|
+
function resolveIcon(name) {
|
|
102
|
+
if (typeof name !== "string" || !name) return void 0;
|
|
103
|
+
if (customIcons.has(name)) return customIcons.get(name);
|
|
104
|
+
const own = ICON_NAMES[name] ?? (name in icons ? name : void 0);
|
|
105
|
+
return own ? icons[own] : void 0;
|
|
106
|
+
}
|
|
107
|
+
function buttonApi(compat) {
|
|
108
|
+
let active = false;
|
|
109
|
+
let enabled = true;
|
|
110
|
+
return {
|
|
111
|
+
state: {
|
|
112
|
+
get active() {
|
|
113
|
+
return active;
|
|
114
|
+
},
|
|
115
|
+
get enabled() {
|
|
116
|
+
return enabled;
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
api: {
|
|
120
|
+
isActive: () => active,
|
|
121
|
+
setActive: (value) => {
|
|
122
|
+
active = value;
|
|
123
|
+
compat.core.updateUI();
|
|
124
|
+
},
|
|
125
|
+
isEnabled: () => enabled,
|
|
126
|
+
setEnabled: (value) => {
|
|
127
|
+
enabled = value;
|
|
128
|
+
compat.core.updateUI();
|
|
129
|
+
},
|
|
130
|
+
// Совместимость со старыми версиями API.
|
|
131
|
+
isDisabled: () => !enabled,
|
|
132
|
+
setDisabled: (value) => {
|
|
133
|
+
enabled = !value;
|
|
134
|
+
compat.core.updateUI();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
var CompatEditor = class {
|
|
140
|
+
id;
|
|
141
|
+
core;
|
|
142
|
+
targetElm;
|
|
143
|
+
settings;
|
|
144
|
+
initialized = false;
|
|
145
|
+
removed = false;
|
|
146
|
+
handlers = /* @__PURE__ */ new Map();
|
|
147
|
+
dirty = false;
|
|
148
|
+
teardowns = [];
|
|
149
|
+
constructor(core, target, id, settings) {
|
|
150
|
+
this.core = core;
|
|
151
|
+
this.targetElm = target;
|
|
152
|
+
this.id = id;
|
|
153
|
+
this.settings = settings;
|
|
154
|
+
}
|
|
155
|
+
// ---------- содержимое ----------
|
|
156
|
+
getContent(args = {}) {
|
|
157
|
+
const event = { format: args.format ?? "html", content: "" };
|
|
158
|
+
event.content = event.format === "text" ? this.core.getText() : this.core.getHTML();
|
|
159
|
+
this.fire("GetContent", event);
|
|
160
|
+
return event.content;
|
|
161
|
+
}
|
|
162
|
+
setContent(content, args = {}) {
|
|
163
|
+
const event = { content, ...args };
|
|
164
|
+
this.fire("BeforeSetContent", event);
|
|
165
|
+
this.core.setContent(event.content);
|
|
166
|
+
this.fire("SetContent", event);
|
|
167
|
+
return event.content;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Вставить HTML на место выделения. Блоки (<p>, списки, таблицы) — отдельными блоками (абзац
|
|
171
|
+
* разрезается), строчное содержимое — в текущую строку. Разбор — схемой редактора: те же проверки
|
|
172
|
+
* ссылок и разметки, что при загрузке содержимого.
|
|
173
|
+
*/
|
|
174
|
+
insertContent(content) {
|
|
175
|
+
const template = document.createElement("template");
|
|
176
|
+
template.innerHTML = content;
|
|
177
|
+
const container = document.createElement("div");
|
|
178
|
+
container.append(template.content);
|
|
179
|
+
const parser = parserFor(this.core.schema);
|
|
180
|
+
const hasBlocks = [...container.children].some((el) => BLOCK_TAGS.has(el.tagName));
|
|
181
|
+
const slice = hasBlocks ? new Slice(parser.parse(container).content, 0, 0) : parser.parseSlice(container);
|
|
182
|
+
const { state, dispatch } = this.core.view;
|
|
183
|
+
dispatch(state.tr.replaceSelection(slice).scrollIntoView());
|
|
184
|
+
}
|
|
185
|
+
resetContent(content) {
|
|
186
|
+
this.core.setContent(content ?? this.settings.__initialContent ?? "", {
|
|
187
|
+
addToHistory: false
|
|
188
|
+
});
|
|
189
|
+
this.setDirty(false);
|
|
190
|
+
}
|
|
191
|
+
getBody() {
|
|
192
|
+
return this.core.view.dom;
|
|
193
|
+
}
|
|
194
|
+
getElement() {
|
|
195
|
+
return this.targetElm;
|
|
196
|
+
}
|
|
197
|
+
getContainer() {
|
|
198
|
+
return this.core.root;
|
|
199
|
+
}
|
|
200
|
+
getContentAreaContainer() {
|
|
201
|
+
return this.core.view.dom.parentElement ?? this.core.root;
|
|
202
|
+
}
|
|
203
|
+
getDoc() {
|
|
204
|
+
return this.core.root.ownerDocument;
|
|
205
|
+
}
|
|
206
|
+
getWin() {
|
|
207
|
+
return this.core.root.ownerDocument.defaultView;
|
|
208
|
+
}
|
|
209
|
+
// ---------- команды ----------
|
|
210
|
+
execCommand(command, _ui, value) {
|
|
211
|
+
const name = command.toLowerCase();
|
|
212
|
+
let done = false;
|
|
213
|
+
if (COMMANDS[name]) done = this.core.exec(COMMANDS[name]);
|
|
214
|
+
else if (name === "formatblock" || name === "mcetoggleformat") {
|
|
215
|
+
const block = FORMAT_BLOCKS[String(value ?? "").toLowerCase()];
|
|
216
|
+
done = block ? this.core.exec(block) : false;
|
|
217
|
+
} else if (name === "mceinsertcontent" || name === "inserthtml") {
|
|
218
|
+
this.insertContent(String(value ?? ""));
|
|
219
|
+
done = true;
|
|
220
|
+
} else if (name === "mcesetcontent") {
|
|
221
|
+
this.setContent(String(value ?? ""));
|
|
222
|
+
done = true;
|
|
223
|
+
} else if (name === "selectall") {
|
|
224
|
+
const { state, dispatch } = this.core.view;
|
|
225
|
+
dispatch(state.tr.setSelection(new AllSelection(state.doc)));
|
|
226
|
+
done = true;
|
|
227
|
+
} else if (name === "mcecleanup") {
|
|
228
|
+
done = true;
|
|
229
|
+
} else {
|
|
230
|
+
const custom = this.commands.get(name);
|
|
231
|
+
if (custom) {
|
|
232
|
+
custom(_ui, value);
|
|
233
|
+
done = true;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
if (done) this.fire("ExecCommand", { command, ui: _ui, value });
|
|
237
|
+
return done;
|
|
238
|
+
}
|
|
239
|
+
queryCommandState(command) {
|
|
240
|
+
const name = COMMANDS[command.toLowerCase()];
|
|
241
|
+
return name ? this.core.isActive(name) : false;
|
|
242
|
+
}
|
|
243
|
+
queryCommandSupported(command) {
|
|
244
|
+
const name = command.toLowerCase();
|
|
245
|
+
return name in COMMANDS || ["formatblock", "mceinsertcontent", "mcesetcontent"].includes(name) || this.commands.has(name);
|
|
246
|
+
}
|
|
247
|
+
commands = /* @__PURE__ */ new Map();
|
|
248
|
+
addCommand(name, callback) {
|
|
249
|
+
this.commands.set(name.toLowerCase(), callback);
|
|
250
|
+
}
|
|
251
|
+
// ---------- события ----------
|
|
252
|
+
on(names, handler, prepend = false) {
|
|
253
|
+
for (const name of names.toLowerCase().split(/\s+/).filter(Boolean)) {
|
|
254
|
+
const set = this.handlers.get(name) ?? /* @__PURE__ */ new Set();
|
|
255
|
+
if (prepend) this.handlers.set(name, /* @__PURE__ */ new Set([handler, ...set]));
|
|
256
|
+
else this.handlers.set(name, set.add(handler));
|
|
257
|
+
}
|
|
258
|
+
return this;
|
|
259
|
+
}
|
|
260
|
+
off(names, handler) {
|
|
261
|
+
if (!names) {
|
|
262
|
+
this.handlers.clear();
|
|
263
|
+
return this;
|
|
264
|
+
}
|
|
265
|
+
for (const name of names.toLowerCase().split(/\s+/).filter(Boolean)) {
|
|
266
|
+
if (handler) this.handlers.get(name)?.delete(handler);
|
|
267
|
+
else this.handlers.delete(name);
|
|
268
|
+
}
|
|
269
|
+
return this;
|
|
270
|
+
}
|
|
271
|
+
once(names, handler) {
|
|
272
|
+
const wrapped = (event) => {
|
|
273
|
+
this.off(names, wrapped);
|
|
274
|
+
return handler(event);
|
|
275
|
+
};
|
|
276
|
+
return this.on(names, wrapped);
|
|
277
|
+
}
|
|
278
|
+
fire(name, event = {}) {
|
|
279
|
+
const payload = Object.assign(event, { type: name.toLowerCase(), target: this });
|
|
280
|
+
for (const handler of [...this.handlers.get(name.toLowerCase()) ?? []]) {
|
|
281
|
+
try {
|
|
282
|
+
handler(payload);
|
|
283
|
+
} catch (error) {
|
|
284
|
+
console.error(`[nova] tinymce-compat: "${name}" handler failed`, error);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return payload;
|
|
288
|
+
}
|
|
289
|
+
/** DOM-событие текста (keydown, click…) — обработчикам передаётся само событие браузера. */
|
|
290
|
+
fireNative(name, event) {
|
|
291
|
+
for (const handler of [...this.handlers.get(name) ?? []]) {
|
|
292
|
+
try {
|
|
293
|
+
handler(event);
|
|
294
|
+
} catch (error) {
|
|
295
|
+
console.error(`[nova] tinymce-compat: "${name}" handler failed`, error);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
/** Иконка для кнопок по имени (tinymce.IconManager / editor.ui.registry.addIcon). */
|
|
300
|
+
static registerIcon(name, svg) {
|
|
301
|
+
customIcons.set(name, svg);
|
|
302
|
+
}
|
|
303
|
+
/** Новое имя fire в последних версиях API. */
|
|
304
|
+
dispatch(name, event) {
|
|
305
|
+
return this.fire(name, event);
|
|
306
|
+
}
|
|
307
|
+
hasEventListeners(name) {
|
|
308
|
+
return (this.handlers.get(name.toLowerCase())?.size ?? 0) > 0;
|
|
309
|
+
}
|
|
310
|
+
// ---------- состояние ----------
|
|
311
|
+
isDirty() {
|
|
312
|
+
return this.dirty;
|
|
313
|
+
}
|
|
314
|
+
setDirty(state) {
|
|
315
|
+
const changed = this.dirty !== state;
|
|
316
|
+
this.dirty = state;
|
|
317
|
+
if (changed && state) this.fire("Dirty");
|
|
318
|
+
}
|
|
319
|
+
/** Записать содержимое в исходный <textarea> (как при отправке формы). */
|
|
320
|
+
save() {
|
|
321
|
+
const content = this.getContent();
|
|
322
|
+
const event = this.fire("SaveContent", { content });
|
|
323
|
+
if (this.targetElm instanceof HTMLTextAreaElement) {
|
|
324
|
+
this.targetElm.value = String(event.content);
|
|
325
|
+
}
|
|
326
|
+
this.setDirty(false);
|
|
327
|
+
return String(event.content);
|
|
328
|
+
}
|
|
329
|
+
focus() {
|
|
330
|
+
this.core.focus();
|
|
331
|
+
}
|
|
332
|
+
hasFocus() {
|
|
333
|
+
return this.core.view.hasFocus();
|
|
334
|
+
}
|
|
335
|
+
hide() {
|
|
336
|
+
this.core.root.hidden = true;
|
|
337
|
+
if (this.targetElm instanceof HTMLTextAreaElement) this.targetElm.hidden = false;
|
|
338
|
+
}
|
|
339
|
+
show() {
|
|
340
|
+
this.core.root.hidden = false;
|
|
341
|
+
if (this.targetElm instanceof HTMLTextAreaElement) this.targetElm.hidden = true;
|
|
342
|
+
}
|
|
343
|
+
isHidden() {
|
|
344
|
+
return this.core.root.hidden === true;
|
|
345
|
+
}
|
|
346
|
+
getParam(name, defaultValue) {
|
|
347
|
+
return name in this.settings ? this.settings[name] : defaultValue;
|
|
348
|
+
}
|
|
349
|
+
options = {
|
|
350
|
+
get: (name) => this.settings[name],
|
|
351
|
+
set: (name, value) => {
|
|
352
|
+
this.settings[name] = value;
|
|
353
|
+
return true;
|
|
354
|
+
},
|
|
355
|
+
isSet: (name) => name in this.settings,
|
|
356
|
+
isRegistered: () => true,
|
|
357
|
+
register: () => {
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
mode = {
|
|
361
|
+
get: () => this.core.isEditable() ? "design" : "readonly",
|
|
362
|
+
set: (mode) => this.core.setEditable(mode !== "readonly"),
|
|
363
|
+
isReadOnly: () => !this.core.isEditable()
|
|
364
|
+
};
|
|
365
|
+
undoManager = {
|
|
366
|
+
undo: () => this.core.exec("undo"),
|
|
367
|
+
redo: () => this.core.exec("redo"),
|
|
368
|
+
hasUndo: () => this.core.can("undo"),
|
|
369
|
+
hasRedo: () => this.core.can("redo"),
|
|
370
|
+
add: () => null,
|
|
371
|
+
clear: () => {
|
|
372
|
+
},
|
|
373
|
+
transact: (callback) => {
|
|
374
|
+
callback();
|
|
375
|
+
return null;
|
|
376
|
+
},
|
|
377
|
+
ignore: (callback) => callback()
|
|
378
|
+
};
|
|
379
|
+
selection = {
|
|
380
|
+
getContent: (args = {}) => {
|
|
381
|
+
const { state } = this.core.view;
|
|
382
|
+
const { from, to } = state.selection;
|
|
383
|
+
if (args.format === "text") return state.doc.textBetween(from, to, "\n");
|
|
384
|
+
return serializer.serializeFragment(state.selection.content().content);
|
|
385
|
+
},
|
|
386
|
+
setContent: (content) => this.insertContent(content),
|
|
387
|
+
getNode: () => {
|
|
388
|
+
const { from } = this.core.view.state.selection;
|
|
389
|
+
const { node } = this.core.view.domAtPos(from);
|
|
390
|
+
return node instanceof Element ? node : node.parentElement;
|
|
391
|
+
},
|
|
392
|
+
getStart: () => this.selection.getNode(),
|
|
393
|
+
getEnd: () => this.selection.getNode(),
|
|
394
|
+
isCollapsed: () => this.core.view.state.selection.empty,
|
|
395
|
+
collapse: (toStart = false) => {
|
|
396
|
+
const { state, dispatch } = this.core.view;
|
|
397
|
+
const pos = toStart ? state.selection.from : state.selection.to;
|
|
398
|
+
dispatch(state.tr.setSelection(TextSelection.near(state.doc.resolve(pos))));
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
notificationManager = {
|
|
402
|
+
open: (args) => this.core.ui.notify({
|
|
403
|
+
text: args.text,
|
|
404
|
+
...args.type ? { type: args.type } : {},
|
|
405
|
+
...args.timeout !== void 0 ? { timeout: args.timeout } : {}
|
|
406
|
+
}),
|
|
407
|
+
close: () => {
|
|
408
|
+
}
|
|
409
|
+
};
|
|
410
|
+
windowManager = {
|
|
411
|
+
alert: (message, callback) => {
|
|
412
|
+
const p = document.createElement("p");
|
|
413
|
+
p.textContent = message;
|
|
414
|
+
this.core.ui.openDialog({
|
|
415
|
+
title: "",
|
|
416
|
+
body: p,
|
|
417
|
+
onSubmit: () => true,
|
|
418
|
+
...callback ? { onClose: callback } : {}
|
|
419
|
+
});
|
|
420
|
+
},
|
|
421
|
+
confirm: (message, callback) => {
|
|
422
|
+
const p = document.createElement("p");
|
|
423
|
+
p.textContent = message;
|
|
424
|
+
let confirmed = false;
|
|
425
|
+
this.core.ui.openDialog({
|
|
426
|
+
title: "",
|
|
427
|
+
body: p,
|
|
428
|
+
submitLabel: "OK",
|
|
429
|
+
onSubmit: () => {
|
|
430
|
+
confirmed = true;
|
|
431
|
+
return true;
|
|
432
|
+
},
|
|
433
|
+
onClose: () => callback?.(confirmed)
|
|
434
|
+
});
|
|
435
|
+
},
|
|
436
|
+
open: () => {
|
|
437
|
+
console.warn(
|
|
438
|
+
"[nova] tinymce-compat: windowManager.open is not supported yet \u2014 use editor.ui.openDialog"
|
|
439
|
+
);
|
|
440
|
+
return { close: () => {
|
|
441
|
+
} };
|
|
442
|
+
}
|
|
443
|
+
};
|
|
444
|
+
/** editor.ui.registry — кнопки и пункты меню в формате TinyMCE поверх реестра NovaProse. */
|
|
445
|
+
ui = {
|
|
446
|
+
registry: {
|
|
447
|
+
addIcon: (name, svg) => {
|
|
448
|
+
customIcons.set(name, svg);
|
|
449
|
+
},
|
|
450
|
+
addButton: (name, spec) => this.addButton(name, spec, false),
|
|
451
|
+
addToggleButton: (name, spec) => this.addButton(name, spec, true),
|
|
452
|
+
addMenuButton: (name, spec) => {
|
|
453
|
+
let items = [];
|
|
454
|
+
spec.fetch((result) => items = result);
|
|
455
|
+
const icon = resolveIcon(spec.icon);
|
|
456
|
+
this.core.ui.registry.addMenuButton(name, {
|
|
457
|
+
tooltip: spec.tooltip ?? spec.text ?? name,
|
|
458
|
+
...icon ? { icon } : { text: spec.text ?? name },
|
|
459
|
+
items: () => {
|
|
460
|
+
spec.fetch((result) => items = result);
|
|
461
|
+
return items.map(
|
|
462
|
+
(item, index) => item.type === "separator" ? { type: "separator" } : {
|
|
463
|
+
id: `${name}-${index}`,
|
|
464
|
+
label: item.text ?? "",
|
|
465
|
+
onSelect: () => item.onAction?.({})
|
|
466
|
+
}
|
|
467
|
+
);
|
|
468
|
+
}
|
|
469
|
+
});
|
|
470
|
+
},
|
|
471
|
+
addMenuItem: (name, spec) => {
|
|
472
|
+
const icon = resolveIcon(spec.icon);
|
|
473
|
+
this.core.ui.registry.addMenuItem(name, {
|
|
474
|
+
text: spec.text ?? name,
|
|
475
|
+
...icon ? { icon } : {},
|
|
476
|
+
onAction: () => spec.onAction?.({})
|
|
477
|
+
});
|
|
478
|
+
},
|
|
479
|
+
addContextToolbar: () => {
|
|
480
|
+
console.warn("[nova] tinymce-compat: addContextToolbar is not supported yet");
|
|
481
|
+
},
|
|
482
|
+
getAll: () => ({})
|
|
483
|
+
}
|
|
484
|
+
};
|
|
485
|
+
addButton(name, spec, toggle) {
|
|
486
|
+
const { api, state } = buttonApi(this);
|
|
487
|
+
if (spec.enabled === false) api.setEnabled(false);
|
|
488
|
+
if (spec.active) api.setActive(true);
|
|
489
|
+
const icon = resolveIcon(spec.icon);
|
|
490
|
+
this.core.ui.registry.addButton(name, {
|
|
491
|
+
tooltip: spec.tooltip ?? spec.text ?? name,
|
|
492
|
+
...icon ? { icon } : { text: spec.text ?? name },
|
|
493
|
+
isEnabled: () => state.enabled,
|
|
494
|
+
...toggle ? { isActive: () => state.active } : {},
|
|
495
|
+
onAction: () => spec.onAction?.(api)
|
|
496
|
+
});
|
|
497
|
+
if (spec.onSetup) {
|
|
498
|
+
const run = () => {
|
|
499
|
+
const teardown = spec.onSetup(api);
|
|
500
|
+
if (typeof teardown === "function") this.teardowns.push(teardown);
|
|
501
|
+
};
|
|
502
|
+
if (this.initialized) run();
|
|
503
|
+
else this.once("init", run);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
/** Убрать редактор и вернуть исходный элемент (как editor.remove()). */
|
|
507
|
+
remove() {
|
|
508
|
+
if (this.removed) return;
|
|
509
|
+
this.fire("remove");
|
|
510
|
+
this.save();
|
|
511
|
+
this.teardowns.splice(0).forEach((teardown) => teardown());
|
|
512
|
+
this.removed = true;
|
|
513
|
+
this.core.destroy();
|
|
514
|
+
this.core.root.remove();
|
|
515
|
+
const wrapper = this.settings.__wrapper;
|
|
516
|
+
wrapper?.remove();
|
|
517
|
+
this.targetElm.hidden = false;
|
|
518
|
+
this.off();
|
|
519
|
+
}
|
|
520
|
+
destroy() {
|
|
521
|
+
this.remove();
|
|
522
|
+
}
|
|
523
|
+
};
|
|
524
|
+
|
|
525
|
+
// src/options.ts
|
|
526
|
+
import { isPluginRegistered } from "@novaprose/core";
|
|
527
|
+
var BUILT_IN = /* @__PURE__ */ new Set([
|
|
528
|
+
"lists",
|
|
529
|
+
"link",
|
|
530
|
+
"image",
|
|
531
|
+
"table",
|
|
532
|
+
"code",
|
|
533
|
+
"codesample",
|
|
534
|
+
"preview",
|
|
535
|
+
"searchreplace",
|
|
536
|
+
"fullscreen",
|
|
537
|
+
"help",
|
|
538
|
+
"wordcount",
|
|
539
|
+
"paste",
|
|
540
|
+
"hr",
|
|
541
|
+
"print",
|
|
542
|
+
"textcolor",
|
|
543
|
+
"colorpicker",
|
|
544
|
+
"contextmenu",
|
|
545
|
+
"tabfocus",
|
|
546
|
+
"textpattern",
|
|
547
|
+
"imagetools",
|
|
548
|
+
"powerpaste",
|
|
549
|
+
"importcss",
|
|
550
|
+
"accordion"
|
|
551
|
+
]);
|
|
552
|
+
var RENAMED = {
|
|
553
|
+
tinycomments: "comments"
|
|
554
|
+
};
|
|
555
|
+
var IGNORED = /* @__PURE__ */ new Set([
|
|
556
|
+
"selector",
|
|
557
|
+
"target",
|
|
558
|
+
"license_key",
|
|
559
|
+
"api_key",
|
|
560
|
+
"branding",
|
|
561
|
+
"promotion",
|
|
562
|
+
"skin",
|
|
563
|
+
"skin_url",
|
|
564
|
+
"content_css",
|
|
565
|
+
"icons",
|
|
566
|
+
"icons_url",
|
|
567
|
+
"theme",
|
|
568
|
+
"theme_url",
|
|
569
|
+
"base_url",
|
|
570
|
+
"suffix",
|
|
571
|
+
"referrer_policy",
|
|
572
|
+
"convert_urls",
|
|
573
|
+
"relative_urls",
|
|
574
|
+
"remove_script_host",
|
|
575
|
+
"document_base_url",
|
|
576
|
+
"entity_encoding",
|
|
577
|
+
"height",
|
|
578
|
+
"min_height",
|
|
579
|
+
"max_height",
|
|
580
|
+
"width",
|
|
581
|
+
"resize",
|
|
582
|
+
"content_style",
|
|
583
|
+
"setup",
|
|
584
|
+
"init_instance_callback",
|
|
585
|
+
"inline",
|
|
586
|
+
"plugins",
|
|
587
|
+
"toolbar",
|
|
588
|
+
"menubar",
|
|
589
|
+
"statusbar",
|
|
590
|
+
"placeholder",
|
|
591
|
+
"readonly",
|
|
592
|
+
"language",
|
|
593
|
+
"images_upload_handler",
|
|
594
|
+
"paste_as_text",
|
|
595
|
+
"paste_preprocess",
|
|
596
|
+
"paste_postprocess",
|
|
597
|
+
"invalid_elements",
|
|
598
|
+
"valid_elements",
|
|
599
|
+
"valid_styles",
|
|
600
|
+
"toolbar_mode",
|
|
601
|
+
"toolbar_sticky",
|
|
602
|
+
"font_family_formats",
|
|
603
|
+
"font_size_formats",
|
|
604
|
+
"line_height_formats",
|
|
605
|
+
"color_map",
|
|
606
|
+
"autosave_interval",
|
|
607
|
+
"autosave_retention",
|
|
608
|
+
"autosave_prefix",
|
|
609
|
+
"quickbars_selection_toolbar",
|
|
610
|
+
"quickbars_insert_toolbar",
|
|
611
|
+
"mentions_fetch",
|
|
612
|
+
"tinycomments_author",
|
|
613
|
+
"tinycomments_author_name"
|
|
614
|
+
]);
|
|
615
|
+
var list = (value) => (Array.isArray(value) ? value.join(" ") : String(value ?? "")).split(/[\s,]+/).filter(Boolean);
|
|
616
|
+
function parseFormats(value) {
|
|
617
|
+
if (typeof value !== "string") return void 0;
|
|
618
|
+
return value.split(";").map((item) => item.trim()).filter(Boolean).map((item) => {
|
|
619
|
+
const [label, css] = item.includes("=") ? item.split("=") : [item, item];
|
|
620
|
+
return { label: label.trim(), value: (css ?? label).trim() };
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
function blobInfo(file) {
|
|
624
|
+
let base64 = "";
|
|
625
|
+
const ready = new Promise((resolve) => {
|
|
626
|
+
const reader = new FileReader();
|
|
627
|
+
reader.onload = () => {
|
|
628
|
+
base64 = String(reader.result).replace(/^data:[^,]*,/, "");
|
|
629
|
+
resolve();
|
|
630
|
+
};
|
|
631
|
+
reader.onerror = () => resolve();
|
|
632
|
+
reader.readAsDataURL(file);
|
|
633
|
+
});
|
|
634
|
+
let uri = "";
|
|
635
|
+
return {
|
|
636
|
+
ready,
|
|
637
|
+
info: {
|
|
638
|
+
id: () => file.name.replace(/\.[^.]+$/, "") || "image",
|
|
639
|
+
name: () => file.name.replace(/\.[^.]+$/, "") || "image",
|
|
640
|
+
filename: () => file.name || "image",
|
|
641
|
+
blob: () => file,
|
|
642
|
+
base64: () => base64,
|
|
643
|
+
blobUri: () => uri ||= URL.createObjectURL(file),
|
|
644
|
+
uri: () => void 0
|
|
645
|
+
}
|
|
646
|
+
};
|
|
647
|
+
}
|
|
648
|
+
function mapOptions(config) {
|
|
649
|
+
const unknown = Object.keys(config).filter((key) => !IGNORED.has(key));
|
|
650
|
+
const plugins = [];
|
|
651
|
+
const missingPlugins = [];
|
|
652
|
+
for (const name of list(config.plugins)) {
|
|
653
|
+
const target = RENAMED[name] ?? name;
|
|
654
|
+
if (BUILT_IN.has(name)) continue;
|
|
655
|
+
if (isPluginRegistered(target)) plugins.push(target);
|
|
656
|
+
else missingPlugins.push(name);
|
|
657
|
+
}
|
|
658
|
+
const options = { plugins };
|
|
659
|
+
if (config.toolbar === false) options.toolbar = false;
|
|
660
|
+
else if (Array.isArray(config.toolbar)) options.toolbar = config.toolbar.join(" | ");
|
|
661
|
+
else if (typeof config.toolbar === "string") options.toolbar = config.toolbar;
|
|
662
|
+
if (config.menubar !== void 0) options.menubar = config.menubar;
|
|
663
|
+
if (typeof config.statusbar === "boolean") options.statusbar = config.statusbar;
|
|
664
|
+
if (typeof config.placeholder === "string") options.placeholder = config.placeholder;
|
|
665
|
+
if (config.readonly === true) options.editable = false;
|
|
666
|
+
if (typeof config.language === "string") options.language = config.language.replace("_", "-");
|
|
667
|
+
if (config.toolbar_mode === "sliding" || config.toolbar_mode === "wrap") {
|
|
668
|
+
options.toolbarMode = config.toolbar_mode;
|
|
669
|
+
} else if (config.toolbar_mode === "floating" || config.toolbar_mode === "scrolling") {
|
|
670
|
+
options.toolbarMode = "sliding";
|
|
671
|
+
}
|
|
672
|
+
if (config.inline === true) options.mode = "inline";
|
|
673
|
+
if (config.paste_as_text === true) options.pasteAsText = true;
|
|
674
|
+
if (typeof config.paste_preprocess === "function") {
|
|
675
|
+
const fn = config.paste_preprocess;
|
|
676
|
+
options.pastePreprocess = (html, editor) => {
|
|
677
|
+
const args = { content: html };
|
|
678
|
+
fn(editor, args);
|
|
679
|
+
return args.content;
|
|
680
|
+
};
|
|
681
|
+
}
|
|
682
|
+
if (typeof config.invalid_elements === "string")
|
|
683
|
+
options.invalidElements = config.invalid_elements;
|
|
684
|
+
if (typeof config.valid_elements === "string") options.validElements = config.valid_elements;
|
|
685
|
+
if (config.valid_styles) options.validStyles = config.valid_styles;
|
|
686
|
+
const fonts = parseFormats(config.font_family_formats);
|
|
687
|
+
if (fonts) options.fontFamilies = fonts;
|
|
688
|
+
const sizes = parseFormats(config.font_size_formats);
|
|
689
|
+
if (sizes) options.fontSizes = sizes.map((s) => s.value);
|
|
690
|
+
const heights = parseFormats(config.line_height_formats);
|
|
691
|
+
if (heights) options.lineHeights = heights.map((s) => s.value);
|
|
692
|
+
if (Array.isArray(config.color_map)) {
|
|
693
|
+
options.colors = config.color_map.filter((_, i) => i % 2 === 0).map((c) => c.startsWith("#") ? c : `#${c}`);
|
|
694
|
+
}
|
|
695
|
+
if (typeof config.images_upload_handler === "function") {
|
|
696
|
+
const handler = config.images_upload_handler;
|
|
697
|
+
const upload = async (file, progress) => {
|
|
698
|
+
const { ready, info } = blobInfo(file);
|
|
699
|
+
await ready;
|
|
700
|
+
return handler(info, progress);
|
|
701
|
+
};
|
|
702
|
+
options.uploadImage = upload;
|
|
703
|
+
}
|
|
704
|
+
const pluginOptions = {};
|
|
705
|
+
if (config.autosave_interval || config.autosave_retention || config.autosave_prefix) {
|
|
706
|
+
const seconds = (value) => parseInt(String(value), 10);
|
|
707
|
+
pluginOptions.autosave = {
|
|
708
|
+
...config.autosave_interval ? { delay: seconds(config.autosave_interval) * 1e3 } : {},
|
|
709
|
+
...config.autosave_retention ? { retention: seconds(config.autosave_retention) } : {},
|
|
710
|
+
...config.autosave_prefix ? { key: String(config.autosave_prefix) } : {}
|
|
711
|
+
};
|
|
712
|
+
}
|
|
713
|
+
if (config.quickbars_selection_toolbar !== void 0 || config.quickbars_insert_toolbar !== void 0) {
|
|
714
|
+
pluginOptions.quickbars = {
|
|
715
|
+
...config.quickbars_selection_toolbar !== void 0 ? { selection: config.quickbars_selection_toolbar } : {},
|
|
716
|
+
...config.quickbars_insert_toolbar !== void 0 ? { insert: config.quickbars_insert_toolbar } : {}
|
|
717
|
+
};
|
|
718
|
+
}
|
|
719
|
+
if (typeof config.mentions_fetch === "function") {
|
|
720
|
+
const fetch = config.mentions_fetch;
|
|
721
|
+
pluginOptions.mentions = {
|
|
722
|
+
fetch: (term) => new Promise((resolve) => fetch({ term }, resolve))
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
if (config.tinycomments_author) {
|
|
726
|
+
pluginOptions.comments = {
|
|
727
|
+
author: {
|
|
728
|
+
id: String(config.tinycomments_author),
|
|
729
|
+
name: String(config.tinycomments_author_name ?? config.tinycomments_author)
|
|
730
|
+
}
|
|
731
|
+
};
|
|
732
|
+
}
|
|
733
|
+
if (Object.keys(pluginOptions).length) options.pluginOptions = pluginOptions;
|
|
734
|
+
return { options, unknown, missingPlugins };
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
// src/index.ts
|
|
738
|
+
var warned = /* @__PURE__ */ new Set();
|
|
739
|
+
function warnOnce(message) {
|
|
740
|
+
if (warned.has(message)) return;
|
|
741
|
+
warned.add(message);
|
|
742
|
+
console.warn(`[nova] tinymce-compat: ${message}`);
|
|
743
|
+
}
|
|
744
|
+
var counter = 0;
|
|
745
|
+
function applyContentStyle(core, css) {
|
|
746
|
+
const scope = `nova-compat-${++counter}`;
|
|
747
|
+
core.root.classList.add(scope);
|
|
748
|
+
const style = document.createElement("style");
|
|
749
|
+
style.setAttribute("data-nova-compat", scope);
|
|
750
|
+
style.textContent = `.${scope} .nova-prosemirror { ${css.replace(/\b(html|body)\b/g, "&")} }`;
|
|
751
|
+
core.root.prepend(style);
|
|
752
|
+
}
|
|
753
|
+
var px = (value) => typeof value === "number" ? `${value}px` : String(value);
|
|
754
|
+
function createTinymce() {
|
|
755
|
+
const all = [];
|
|
756
|
+
const plugins = /* @__PURE__ */ new Map();
|
|
757
|
+
let active = null;
|
|
758
|
+
async function initOne(target, config) {
|
|
759
|
+
const existing = all.find((e) => e.targetElm === target && !e.removed);
|
|
760
|
+
if (existing) return existing;
|
|
761
|
+
const { options, unknown, missingPlugins } = mapOptions(config);
|
|
762
|
+
if (unknown.length) warnOnce(`options not supported yet: ${unknown.join(", ")}`);
|
|
763
|
+
const custom = String(
|
|
764
|
+
Array.isArray(config.plugins) ? config.plugins.join(" ") : config.plugins ?? ""
|
|
765
|
+
).split(/[\s,]+/).filter((name) => plugins.has(name));
|
|
766
|
+
const missing = missingPlugins.filter((name) => !plugins.has(name));
|
|
767
|
+
if (missing.length) warnOnce(`plugins not available: ${missing.join(", ")}`);
|
|
768
|
+
const id = target.id || `mce_${++counter}`;
|
|
769
|
+
if (!target.id) target.id = id;
|
|
770
|
+
const isTextarea = target instanceof HTMLTextAreaElement;
|
|
771
|
+
const initial = isTextarea ? target.value : target.innerHTML;
|
|
772
|
+
const settings = { ...config, __initialContent: initial };
|
|
773
|
+
let host;
|
|
774
|
+
if (config.inline === true && !isTextarea) {
|
|
775
|
+
target.replaceChildren();
|
|
776
|
+
host = target;
|
|
777
|
+
} else {
|
|
778
|
+
host = document.createElement("div");
|
|
779
|
+
host.className = "nova-compat";
|
|
780
|
+
target.after(host);
|
|
781
|
+
target.hidden = true;
|
|
782
|
+
settings.__wrapper = host;
|
|
783
|
+
}
|
|
784
|
+
let compat;
|
|
785
|
+
const core = createEditor({
|
|
786
|
+
...options,
|
|
787
|
+
element: host,
|
|
788
|
+
content: initial,
|
|
789
|
+
setup: (editor) => {
|
|
790
|
+
compat = new CompatEditor(editor, target, id, settings);
|
|
791
|
+
for (const name of custom) {
|
|
792
|
+
try {
|
|
793
|
+
plugins.get(name)(compat, "");
|
|
794
|
+
} catch (error) {
|
|
795
|
+
console.error(`[nova] tinymce-compat: plugin "${name}" failed`, error);
|
|
796
|
+
}
|
|
797
|
+
}
|
|
798
|
+
config.setup?.(compat);
|
|
799
|
+
}
|
|
800
|
+
});
|
|
801
|
+
if (config.height !== void 0) core.root.style.height = px(config.height);
|
|
802
|
+
if (config.min_height !== void 0) core.root.style.minHeight = px(config.min_height);
|
|
803
|
+
if (config.max_height !== void 0) core.root.style.maxHeight = px(config.max_height);
|
|
804
|
+
if (config.width !== void 0) core.root.style.width = px(config.width);
|
|
805
|
+
if (typeof config.content_style === "string") applyContentStyle(core, config.content_style);
|
|
806
|
+
let changedSinceFocus = false;
|
|
807
|
+
core.on("change", () => {
|
|
808
|
+
compat.setDirty(true);
|
|
809
|
+
changedSinceFocus = true;
|
|
810
|
+
compat.fire("input");
|
|
811
|
+
});
|
|
812
|
+
core.on("selectionChange", () => {
|
|
813
|
+
compat.fire("NodeChange", { element: compat.selection.getNode(), selectionChange: true });
|
|
814
|
+
});
|
|
815
|
+
core.on("focus", () => {
|
|
816
|
+
active = compat;
|
|
817
|
+
changedSinceFocus = false;
|
|
818
|
+
compat.fire("focus");
|
|
819
|
+
});
|
|
820
|
+
core.on("blur", () => {
|
|
821
|
+
if (changedSinceFocus) compat.fire("change", { level: { content: compat.getContent() } });
|
|
822
|
+
changedSinceFocus = false;
|
|
823
|
+
compat.fire("blur");
|
|
824
|
+
});
|
|
825
|
+
for (const name of [
|
|
826
|
+
"keydown",
|
|
827
|
+
"keyup",
|
|
828
|
+
"keypress",
|
|
829
|
+
"click",
|
|
830
|
+
"dblclick",
|
|
831
|
+
"mousedown",
|
|
832
|
+
"mouseup",
|
|
833
|
+
"paste",
|
|
834
|
+
"copy",
|
|
835
|
+
"cut",
|
|
836
|
+
"drop"
|
|
837
|
+
]) {
|
|
838
|
+
core.view.dom.addEventListener(name, (event) => compat.fireNative(name, event));
|
|
839
|
+
}
|
|
840
|
+
const form = isTextarea ? target.form : target.closest("form");
|
|
841
|
+
form?.addEventListener("submit", () => compat.save());
|
|
842
|
+
compat.on("remove", () => {
|
|
843
|
+
const index = all.indexOf(compat);
|
|
844
|
+
if (index >= 0) all.splice(index, 1);
|
|
845
|
+
if (active === compat) active = all[0] ?? null;
|
|
846
|
+
});
|
|
847
|
+
all.push(compat);
|
|
848
|
+
active ??= compat;
|
|
849
|
+
await core.ready;
|
|
850
|
+
if (compat.removed) return compat;
|
|
851
|
+
compat.initialized = true;
|
|
852
|
+
compat.fire("init");
|
|
853
|
+
config.init_instance_callback?.(compat);
|
|
854
|
+
return compat;
|
|
855
|
+
}
|
|
856
|
+
const api = {
|
|
857
|
+
majorVersion: "7",
|
|
858
|
+
minorVersion: "0.0",
|
|
859
|
+
novaprose: true,
|
|
860
|
+
get activeEditor() {
|
|
861
|
+
return active;
|
|
862
|
+
},
|
|
863
|
+
get editors() {
|
|
864
|
+
return [...all];
|
|
865
|
+
},
|
|
866
|
+
async init(config) {
|
|
867
|
+
if (document.readyState === "loading") {
|
|
868
|
+
await new Promise(
|
|
869
|
+
(resolve) => document.addEventListener("DOMContentLoaded", resolve, { once: true })
|
|
870
|
+
);
|
|
871
|
+
}
|
|
872
|
+
const targets = config.target ? [config.target] : config.selector ? [...document.querySelectorAll(config.selector)] : [];
|
|
873
|
+
if (!targets.length) warnOnce(`no elements match "${config.selector ?? ""}"`);
|
|
874
|
+
const editors = [];
|
|
875
|
+
for (const target of targets) editors.push(await initOne(target, config));
|
|
876
|
+
return editors;
|
|
877
|
+
},
|
|
878
|
+
get: ((id) => {
|
|
879
|
+
if (id === void 0) return [...all];
|
|
880
|
+
if (typeof id === "number") return all[id] ?? null;
|
|
881
|
+
return all.find((e) => e.id === id) ?? null;
|
|
882
|
+
}),
|
|
883
|
+
remove(selector) {
|
|
884
|
+
const targets = selector === void 0 ? [...all] : typeof selector === "string" ? all.filter(
|
|
885
|
+
(e) => e.id === selector.replace(/^#/, "") || e.targetElm.matches(selector)
|
|
886
|
+
) : [selector];
|
|
887
|
+
targets.forEach((editor) => editor.remove());
|
|
888
|
+
},
|
|
889
|
+
triggerSave() {
|
|
890
|
+
all.forEach((editor) => editor.save());
|
|
891
|
+
},
|
|
892
|
+
execCommand(command, ui, value) {
|
|
893
|
+
return active?.execCommand(command, ui, value) ?? false;
|
|
894
|
+
},
|
|
895
|
+
PluginManager: {
|
|
896
|
+
add: (name, plugin) => {
|
|
897
|
+
plugins.set(name, plugin);
|
|
898
|
+
},
|
|
899
|
+
get: (name) => plugins.get(name),
|
|
900
|
+
requireLangPack: () => {
|
|
901
|
+
},
|
|
902
|
+
urls: {}
|
|
903
|
+
},
|
|
904
|
+
IconManager: {
|
|
905
|
+
add: (_name, pack) => {
|
|
906
|
+
for (const [icon, svg] of Object.entries(pack.icons ?? {})) {
|
|
907
|
+
CompatEditor.registerIcon(icon, svg);
|
|
908
|
+
}
|
|
909
|
+
}
|
|
910
|
+
},
|
|
911
|
+
util: { I18n: { translate: (text) => text } }
|
|
912
|
+
};
|
|
913
|
+
return api;
|
|
914
|
+
}
|
|
915
|
+
var tinymce = createTinymce();
|
|
916
|
+
export {
|
|
917
|
+
CompatEditor,
|
|
918
|
+
createTinymce,
|
|
919
|
+
mapOptions,
|
|
920
|
+
tinymce
|
|
921
|
+
};
|
|
922
|
+
//# sourceMappingURL=index.js.map
|