-
-
Notifications
You must be signed in to change notification settings - Fork 97
/
tinymce--implementation.js
351 lines (325 loc) · 12.6 KB
/
tinymce--implementation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import $ from "jquery";
import I18n from "../../core/i18n";
import events from "@patternslib/patternslib/src/core/events";
import logger from "@patternslib/patternslib/src/core/logging";
import _t from "../../core/i18n-wrapper";
import utils from "../../core/utils";
const log = logger.getLogger("tinymce--implementation");
let LinkModal = null;
export default class TinyMCE {
constructor(el, options) {
this.el = el;
this.$el = $(el);
this.options = options;
}
addLinkClicked() {
var self = this;
if (self.linkModal === null) {
var $el = $("<div/>").insertAfter(self.$el);
var linkTypes = ["internal", "upload", "external", "email", "anchor"];
if (!self.options.upload) {
linkTypes.splice(1, 1);
}
self.linkModal = new LinkModal(
$el,
$.extend(true, {}, self.options, {
tinypattern: self,
linkTypes: linkTypes,
})
);
self.linkModal.show();
} else {
self.linkModal.reinitialize();
self.linkModal.show();
}
}
addImageClicked() {
var self = this;
if (self.imageModal === null) {
var linkTypes = ["image", "uploadImage", "externalImage"];
if (!self.options.upload) {
linkTypes.splice(1, 1);
}
var options = $.extend(true, {}, self.options, {
tinypattern: self,
linkTypes: linkTypes,
initialLinkType: "image",
text: {
insertHeading: _t("Insert Image"),
},
relatedItems: {
selectableTypes: self.options.imageTypes,
},
});
var $el = $("<div/>").insertAfter(self.$el);
self.imageModal = new LinkModal($el, options);
self.imageModal.show();
} else {
self.imageModal.reinitialize();
self.imageModal.show();
}
}
generateUrl(data) {
var self = this;
let part = "undefined";
if(typeof data === "object" && Object.keys(data).indexOf(self.options.linkAttribute) != -1) {
part = data[self.options.linkAttribute];
} else if (typeof data === "string") {
part = data;
}
return self.options.prependToUrl + part + self.options.appendToUrl;
}
generateImageUrl(data, scale_name) {
var self = this;
var url = self.generateUrl(data);
if (scale_name) {
url =
url +
self.options.prependToScalePart +
scale_name +
self.options.appendToScalePart;
} else {
url = url + self.options.appendToOriginalScalePart;
}
return url;
}
stripGeneratedUrl(url) {
// to get original attribute back
var self = this;
url = url.split(self.options.prependToScalePart, 2)[0];
if (self.options.prependToUrl) {
var parts = url.split(self.options.prependToUrl, 2);
if (parts.length === 2) {
url = parts[1];
}
}
if (self.options.appendToUrl) {
url = url.split(self.options.appendToUrl)[0];
}
return url;
}
getScaleFromUrl(url) {
var self = this;
var split = url.split(self.options.prependToScalePart);
if (split.length !== 2) {
// not valid scale, screw it
return null;
}
if (self.options.appendToScalePart) {
url = split[1].split(self.options.appendToScalePart)[0];
} else {
url = split[1];
}
if (url.indexOf("/image_") !== -1) {
url = url.split("/image_")[1];
}
return url;
}
async initLanguage() {
var self = this;
var i18n = new I18n();
var lang = i18n.currentLanguage;
if (lang !== "en" && self.options.tiny.language !== "en") {
try {
await import(`tinymce-i18n/langs6/${lang}`);
} catch (e) {
log.debug("Could not load TinyMCE language: ", lang);
try {
// expected lang not available, let's fallback to closest one
if (lang.split("_").length > 1) {
lang = lang.split("_")[0];
} else {
lang = lang + "_" + lang.toUpperCase();
}
log.debug("Trying with: ", lang);
await import(`tinymce-i18n/langs6/${lang}`);
self.options.tiny.language = lang;
} catch (e) {
log.debug("Could not load TinyMCE language. Fallback to English");
self.options.tiny.language = "en";
}
}
}
}
async init() {
import("./tinymce.scss");
const theme = $('html').attr('data-bs-theme');
let css = 'oxide';
if (theme && theme == 'dark') {
css = 'oxide-dark';
}
import(`tinymce/skins/ui/${css}/content.css`);
import(`tinymce/skins/ui/${css}/skin.css`);
const tinymce = (await import("tinymce/tinymce")).default;
await import("tinymce/models/dom");
let valid_plugins = [];
// tinyMCE Plugins
for (const plugin of this.options.tiny.plugins) {
if (plugin == "plonelink" || plugin == "ploneimage") {
valid_plugins.push(plugin);
continue;
} else if (plugin == "template") {
// load backported template plugin
const TemplatePlugin = (await import("./js/template")).default;
TemplatePlugin();
valid_plugins.push(plugin);
continue;
}
try {
await import("tinymce/plugins/" + plugin);
valid_plugins.push(plugin);
} catch (e) {
log.debug("Could not load TinyMCE plugin: ", plugin);
}
}
this.options.tiny.plugins = valid_plugins;
await import("tinymce/themes/silver");
await import("tinymce/icons/default");
LinkModal = (await import("./js/links")).default;
var self = this;
self.linkModal = self.imageModal = self.uploadModal = self.pasteModal = null;
// tiny needs an id in order to initialize. Creat it if not set.
var id = utils.setId(self.$el);
if (
self.options.pictureVariants &&
typeof self.options.pictureVariants === "string"
) {
self.options.pictureVariants = JSON.parse(self.options.pictureVariants);
}
var tinyOptions = self.options.tiny;
if (self.options.inline === true) {
self.options.tiny.inline = true;
self.options.tiny.toolbar_mode = "scrolling";
}
self.tinyId = self.options.inline ? id + "-editable" : id; // when displaying TinyMCE inline, a separate div is created.
tinyOptions.selector = "#" + self.tinyId;
// XXX: disabled skin means it wont load css files which we already
// include in widgets.min.css
tinyOptions.skin = false;
// do not show the "upgrade" button for plugins
tinyOptions.promotion = false;
// TinyMCE 7 needs "license_key": "gpl" explicitly
tinyOptions.license_key = "gpl";
tinyOptions.init_instance_callback = function (editor) {
if (self.tiny === undefined || self.tiny === null) {
self.tiny = editor;
}
};
tinyOptions["setup"] = (editor) => {
editor.ui.registry.addMenuButton("inserttable", {
icon: "table",
tooltip: _t("Insert table with header row"),
fetch: function (callback) {
callback([
{
type: "fancymenuitem",
fancytype: "inserttable",
onAction: function (data) {
// https://www.tiny.cloud/docs/plugins/table/#commands
editor.execCommand("mceInsertTable", false, {
rows: data.numRows,
columns: data.numColumns,
options: { headerRows: 1 },
});
},
},
]);
},
});
// handle 'change' event to ensure correct validation (eg. required textfield)
// eslint-disable-next-line no-unused-vars
editor.on("change", (e) => {
// setting tiny content manually
this.el.value = editor.getContent();
// dispatch "change" event for pat-validation
this.el.dispatchEvent(events.change_event());
});
};
await self.initLanguage();
if (typeof self.options.folderTypes === "string") {
self.options.folderTypes = self.options.folderTypes.split(",");
}
if (typeof self.options.imageTypes === "string") {
self.options.imageTypes = self.options.imageTypes.split(",");
}
if (self.options.inline === true) {
// create a div, which will be made content-editable by TinyMCE and
// copy contents from textarea to it. Then hide textarea.
self.$el.after('<div id="' + self.tinyId + '">' + self.$el.val() + "</div>");
self.$el.hide();
}
if (
tinyOptions.importcss_file_filter &&
typeof tinyOptions.importcss_file_filter.indexOf === "function" &&
tinyOptions.importcss_file_filter.indexOf(",") !== -1
) {
// need a custom function to check now
var files = tinyOptions.importcss_file_filter.split(",");
tinyOptions.importcss_file_filter = function (value) {
for (var i = 0; i < files.length; i++) {
if (value.indexOf(files[i]) !== -1) {
return true;
}
}
return false;
};
}
if (
tinyOptions.importcss_selector_filter &&
tinyOptions.importcss_selector_filter.length
) {
tinyOptions.importcss_selector_filter = new RegExp(
tinyOptions.importcss_selector_filter
);
}
if (tinyOptions.importcss_groups && tinyOptions.importcss_groups.length) {
for (var i = 0; i < tinyOptions.importcss_groups.length; i++) {
if (
tinyOptions.importcss_groups[i].filter &&
tinyOptions.importcss_groups[i].filter.length
) {
tinyOptions.importcss_groups[i].filter = new RegExp(
tinyOptions.importcss_groups[i].filter
);
}
}
}
// add "urlconverter_callback" to leave external URLs/Images as is
tinyOptions["urlconverter_callback"] = (url) => {
if (url.indexOf("http") === 0) {
// if url starts with "http" return it as is
return url;
}
// otherwise default tiny behavior
if (self.tiny.options.get('relative_urls')) {
return self.tiny.documentBaseURI.toRelative(url);
}
url = self.tiny.documentBaseURI.toAbsolute(url, self.tiny.options.get('remove_script_host'));
return url;
}
// BBB: TinyMCE 6+ has renamed toolbar and menuitem plugins.
// map them here until they are updated in Plone's configuration:
// menu: "formats" -> "styles"
if (tinyOptions?.menu?.format) {
tinyOptions.menu.format.items = tinyOptions.menu.format.items.replace('formats', 'styles');
}
// toolbar: "styleselect" -> "styles"
if (tinyOptions?.toolbar) {
tinyOptions.toolbar = tinyOptions.toolbar.replace('styleselect', 'styles');
}
tinymce.init(tinyOptions);
self.tiny = tinymce.get(self.tinyId);
}
destroy() {
if (this.tiny) {
if (this.options.inline === true) {
// destroy also inline editable
this.$el.val(this.tiny.getContent());
$("#" + this.tinyId).remove();
this.$el.show();
}
this.tiny.destroy();
this.tiny = undefined;
}
}
}