-
-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathtinymce--implementation.js
More file actions
391 lines (353 loc) · 14 KB
/
Copy pathtinymce--implementation.js
File metadata and controls
391 lines (353 loc) · 14 KB
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
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 {
linkModal;
imageModal;
constructor(el, options) {
this.el = el;
this.$el = $(el);
this.options = options;
}
addLinkClicked() {
if (!this.linkModal) {
const $el = $("<div/>").insertAfter(this.$el);
const linkTypes = ["internal", "external", "email", "anchor"];
this.linkModal = new LinkModal(
$el,
$.extend(true, {}, this.options, {
tinypattern: this,
linkTypes: linkTypes,
}),
);
this.linkModal.show();
} else {
this.linkModal.reinitialize();
this.linkModal.show();
}
}
addImageClicked() {
if (!this.imageModal) {
const linkTypes = ["image", "externalImage"];
const options = $.extend(true, {}, this.options, {
tinypattern: this,
linkTypes: linkTypes,
initialLinkType: "image",
text: {
insertHeading: _t("Insert Image"),
},
relatedItems: {
selectableTypes: this.options.imageTypes,
},
});
const $el = $("<div/>").insertAfter(this.$el);
this.imageModal = new LinkModal($el, options);
this.imageModal.show();
} else {
this.imageModal.reinitialize();
this.imageModal.show();
}
}
generateUrl(data) {
let part = "undefined";
if (
typeof data === "object" &&
Object.keys(data).indexOf(this.options.linkAttribute) != -1
) {
part = data[this.options.linkAttribute];
} else if (typeof data === "string") {
part = data;
}
return this.options.prependToUrl + part + this.options.appendToUrl;
}
generateImageUrl(data, scale_name) {
let url = this.generateUrl(data);
if (scale_name) {
url =
url +
this.options.prependToScalePart +
scale_name +
this.options.appendToScalePart;
} else {
url = url + this.options.appendToOriginalScalePart;
}
return url;
}
stripGeneratedUrl(url) {
// to get original attribute back
url = url.split(this.options.prependToScalePart, 2)[0];
if (this.options.prependToUrl) {
const parts = url.split(this.options.prependToUrl, 2);
if (parts.length === 2) {
url = parts[1];
}
}
if (this.options.appendToUrl) {
url = url.split(this.options.appendToUrl)[0];
}
return url;
}
getScaleFromUrl(url) {
const split = url.split(this.options.prependToScalePart);
if (split.length !== 2) {
// not valid scale, screw it
return null;
}
if (this.options.appendToScalePart) {
url = split[1].split(this.options.appendToScalePart)[0];
} else {
url = split[1];
}
if (url.indexOf("/image_") !== -1) {
url = url.split("/image_")[1];
}
return url;
}
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");
await import("tinymce/themes/silver");
await import("tinymce/icons/default");
LinkModal = (await import("./js/links")).default;
// tiny needs an id in order to initialize. Creat it if not set.
const id = utils.setId(this.$el);
if (
this.options.pictureVariants &&
typeof this.options.pictureVariants === "string"
) {
this.options.pictureVariants = JSON.parse(this.options.pictureVariants);
}
// TinyMCE editor options
const tinyOptions = structuredClone(this.options.tiny);
this.lang = tinyOptions.language = await this.initLanguage();
tinyOptions.plugins = await this.initPlugins();
if (this.options.inline === true) {
tinyOptions.inline = true;
tinyOptions.toolbar_mode = "scrolling";
}
this.tinyId = this.options.inline ? `${id}-editable` : id; // when displaying TinyMCE inline, a separate div is created.
tinyOptions.selector = `#${this.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;
if (!tinyOptions?.license_key) {
// set default license key to "gpl" if not set
tinyOptions.license_key = "gpl";
}
tinyOptions.init_instance_callback = (editor) => {
if (this.tiny === undefined || this.tiny === null) {
this.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());
});
};
if (typeof this.options.folderTypes === "string") {
this.options.folderTypes = this.options.folderTypes.split(",");
}
if (typeof this.options.imageTypes === "string") {
this.options.imageTypes = this.options.imageTypes.split(",");
}
if (this.options.inline === true) {
// create a div, which will be made content-editable by TinyMCE and
// copy contents from textarea to it. Then hide textarea.
this.$el.after('<div id="' + this.tinyId + '">' + this.$el.val() + "</div>");
this.$el.hide();
}
// The `importcss_file_filter` is used to filter the CSS files
// from `content_css` which should be used to automatically create the
// styles dropdown.
// Also see:
// https://6.docs.plone.org/classic-ui/tinymce-customization.html#inject-formats-with-files-named-tinymce-formats-css
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
const files = tinyOptions.importcss_file_filter.split(",");
tinyOptions.importcss_file_filter = function (value) {
for (const file of files) {
if (value.indexOf(file) !== -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 (const group of tinyOptions.importcss_groups) {
if (group.filter?.length) {
group.filter = new RegExp(group.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 (this.tiny.options.get("relative_urls")) {
return this.tiny.documentBaseURI.toRelative(url);
}
url = this.tiny.documentBaseURI.toAbsolute(
url,
this.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");
}
// XXX: This is a quickfix for the wrong "menubar" type in "plone.base.interfaces.controlpanel.ITinyMCEPluginSchema"
if (tinyOptions.menubar && Array.isArray(tinyOptions.menubar)) {
tinyOptions.menubar = tinyOptions.menubar.join(" ").trim();
}
tinymce.init(tinyOptions);
this.tiny = tinymce.get(this.tinyId);
}
async initLanguage() {
const i18n = new I18n();
const lang_parts = i18n.currentLanguage.split("-");
// Country specific languages in TinyMCE 8 in in the form: `lang-COUNTRY_CODE`
let lang =
lang_parts.length === 1
? lang_parts[0]
: `${lang_parts[0]}-${lang_parts[1].toUpperCase()}`;
debugger;
if (lang !== "en") {
// load translations from tinymce-i18n
try {
await import(`tinymce-i18n/langs8/${lang}.js`);
} catch {
try {
log.debug("Could not load TinyMCE language: ", lang);
lang = lang_parts[0];
log.debug("Fall back to: ", lang);
await import(`tinymce-i18n/langs8/${lang}.js`);
} catch {
try {
log.debug("Could not load TinyMCE language: ", lang);
// Some languages have the form of `lang-LANG`, etc. `fr-FR`.
lang = `${lang_parts[0]}-${lang_parts[0].toUpperCase()}`;
log.debug("Fall back to: ", lang);
await import(`tinymce-i18n/langs8/${lang}.js`);
} catch {
log.debug(
"Could not load TinyMCE language. Fallback to English",
);
lang = "en";
}
log.debug("Could not load TinyMCE language. Fallback to English");
lang = "en";
}
}
}
return lang;
}
async initPlugins() {
const 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;
} else if (plugin == "emoticons") {
// fix emiticons plugin
// see https://community.plone.org/t/tinymce-menubar-settings-not-working-6-1-1/22190/1
await import(`tinymce/plugins/emoticons/js/emojis.min.js`);
} else if (plugin == "help") {
// fix help plugin
// see https://community.plone.org/t/tinymce-menubar-settings-not-working-6-1-1/22190/1
await import(`tinymce/plugins/help/js/i18n/keynav/${this.lang}.js`);
}
try {
await import("tinymce/plugins/" + plugin);
valid_plugins.push(plugin);
} catch {
log.debug("Could not load TinyMCE plugin: ", plugin);
}
}
return valid_plugins;
}
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;
}
}
}