1 import {register as registerShortcuts} from "./shortcuts";
2 import {listen as listenForCommonEvents} from "./common-events";
3 import {scrollToQueryString} from "./scrolling";
4 import {listenForDragAndPaste} from "./drop-paste-handling";
5 import {getPrimaryToolbar, registerAdditionalToolbars} from "./toolbars";
6 import {registerCustomIcons} from "./icons";
8 import {getPlugin as getCodeeditorPlugin} from "./plugin-codeeditor";
9 import {getPlugin as getDrawioPlugin} from "./plugin-drawio";
10 import {getPlugin as getCustomhrPlugin} from "./plugins-customhr";
11 import {getPlugin as getImagemanagerPlugin} from "./plugins-imagemanager";
12 import {getPlugin as getAboutPlugin} from "./plugins-about";
13 import {getPlugin as getDetailsPlugin} from "./plugins-details";
14 import {getPlugin as getTasklistPlugin} from "./plugins-tasklist";
16 const style_formats = [
17 {title: "Large Header", format: "h2", preview: 'color: blue;'},
18 {title: "Medium Header", format: "h3"},
19 {title: "Small Header", format: "h4"},
20 {title: "Tiny Header", format: "h5"},
21 {title: "Paragraph", format: "p", exact: true, classes: ''},
22 {title: "Blockquote", format: "blockquote"},
24 title: "Callouts", items: [
25 {title: "Information", format: 'calloutinfo'},
26 {title: "Success", format: 'calloutsuccess'},
27 {title: "Warning", format: 'calloutwarning'},
28 {title: "Danger", format: 'calloutdanger'}
34 alignleft: {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'align-left'},
35 aligncenter: {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'align-center'},
36 alignright: {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'align-right'},
37 calloutsuccess: {block: 'p', exact: true, attributes: {class: 'callout success'}},
38 calloutinfo: {block: 'p', exact: true, attributes: {class: 'callout info'}},
39 calloutwarning: {block: 'p', exact: true, attributes: {class: 'callout warning'}},
40 calloutdanger: {block: 'p', exact: true, attributes: {class: 'callout danger'}}
72 function file_picker_callback(callback, value, meta) {
74 // field_name, url, type, win
75 if (meta.filetype === 'file') {
76 window.EntitySelectorPopup.show(entity => {
77 callback(entity.link, {
84 if (meta.filetype === 'image') {
86 window.ImageManager.show(function (image) {
87 callback(image.url, {alt: image.name});
94 * @param {WysiwygConfigOptions} options
97 function gatherPlugins(options) {
114 options.textDirection === 'rtl' ? 'directionality' : '',
117 window.tinymce.PluginManager.add('codeeditor', getCodeeditorPlugin(options));
118 window.tinymce.PluginManager.add('customhr', getCustomhrPlugin(options));
119 window.tinymce.PluginManager.add('imagemanager', getImagemanagerPlugin(options));
120 window.tinymce.PluginManager.add('about', getAboutPlugin(options));
121 window.tinymce.PluginManager.add('details', getDetailsPlugin(options));
122 window.tinymce.PluginManager.add('tasklist', getTasklistPlugin(options));
124 if (options.drawioUrl) {
125 window.tinymce.PluginManager.add('drawio', getDrawioPlugin(options));
126 plugins.push('drawio');
129 return plugins.filter(plugin => Boolean(plugin));
133 * Fetch custom HTML head content from the parent page head into the editor.
135 function fetchCustomHeadContent() {
136 const headContentLines = document.head.innerHTML.split("\n");
137 const startLineIndex = headContentLines.findIndex(line => line.trim() === '<!-- Start: custom user content -->');
138 const endLineIndex = headContentLines.findIndex(line => line.trim() === '<!-- End: custom user content -->');
139 if (startLineIndex === -1 || endLineIndex === -1) {
142 return headContentLines.slice(startLineIndex + 1, endLineIndex).join('\n');
146 * Setup a serializer filter for <br> tags to ensure they're not rendered
147 * within code blocks and that we use newlines there instead.
148 * @param {Editor} editor
150 function setupBrFilter(editor) {
151 editor.serializer.addNodeFilter('br', function(nodes) {
152 for (const node of nodes) {
153 if (node.parent && node.parent.name === 'code') {
154 const newline = tinymce.html.Node.create('#text');
155 newline.value = '\n';
156 node.replace(newline);
163 * @param {WysiwygConfigOptions} options
164 * @return {function(Editor)}
166 function getSetupCallback(options) {
167 return function(editor) {
168 editor.on('ExecCommand change input NodeChange ObjectResized', editorChange);
169 listenForCommonEvents(editor);
170 listenForDragAndPaste(editor, options);
172 editor.on('init', () => {
174 scrollToQueryString(editor);
175 window.editor = editor;
176 registerShortcuts(editor);
179 editor.on('PreInit', () => {
180 setupBrFilter(editor);
183 function editorChange() {
184 const content = editor.getContent();
185 if (options.darkMode) {
186 editor.contentDocument.documentElement.classList.add('dark-mode');
188 window.$events.emit('editor-html-change', content);
191 // Custom handler hook
192 window.$events.emitPublic(options.containerElement, 'editor-tinymce::setup', {editor});
194 // Inline code format button
195 editor.ui.registry.addButton('inlinecode', {
196 tooltip: 'Inline code',
199 editor.execCommand('mceToggleFormat', false, 'code');
206 * @param {WysiwygConfigOptions} options
208 function getContentStyle(options) {
210 html, body, html.dark-mode {
211 background: ${options.darkMode ? '#222' : '#fff'};
214 padding-left: 15px !important;
215 padding-right: 15px !important;
216 height: initial !important;
218 margin-left: auto! important;
219 margin-right: auto !important;
220 overflow-y: hidden !important;
221 }`.trim().replace('\n', '');
225 * @param {WysiwygConfigOptions} options
228 export function build(options) {
231 window.tinymce.addI18n(options.language, options.translationMap);
234 const version = document.querySelector('script[src*="/dist/app.js"]').getAttribute('src').split('?version=')[1];
236 // Return config object
240 selector: '#html-editor',
241 cache_suffix: '?version=' + version,
243 window.baseUrl('/dist/styles.css'),
246 skin: options.darkMode ? 'tinymce-5-dark' : 'tinymce-5',
247 body_class: 'page-content',
248 browser_spellcheck: true,
249 relative_urls: false,
250 language: options.language,
251 directionality: options.textDirection,
252 remove_script_host: false,
253 document_base_url: window.baseUrl('/'),
254 end_container_on_empty_block: true,
255 remove_trailing_brs: false,
258 paste_data_images: false,
259 extended_valid_elements: 'pre[*],svg[*],div[drawio-diagram],details[*],summary[*],div[*],li[class|checked|style]',
260 automatic_uploads: false,
261 custom_elements: 'doc-root,code-block',
263 "-div[p|h1|h2|h3|h4|h5|h6|blockquote|code-block]",
265 "-doc-root[doc-root|#text]",
268 "+doc-root[p|h1|h2|h3|h4|h5|h6|blockquote|code-block|div]"
270 plugins: gatherPlugins(options),
272 toolbar: getPrimaryToolbar(options),
273 content_style: getContentStyle(options),
275 style_formats_merge: false,
276 media_alt_source: false,
279 table_style_by_css: true,
280 table_use_colgroups: true,
281 file_picker_types: 'file image',
283 file_picker_callback,
284 paste_preprocess(plugin, args) {
285 const content = args.content;
286 if (content.indexOf('<img src="file://') !== -1) {
290 init_instance_callback(editor) {
291 const head = editor.getDoc().querySelector('head');
292 head.innerHTML += fetchCustomHeadContent();
295 registerCustomIcons(editor);
296 registerAdditionalToolbars(editor, options);
297 getSetupCallback(options)(editor);
303 * @typedef {Object} WysiwygConfigOptions
304 * @property {Element} containerElement
305 * @property {string} language
306 * @property {boolean} darkMode
307 * @property {string} textDirection
308 * @property {string} drawioUrl
309 * @property {int} pageId
310 * @property {Object} translations
311 * @property {Object} translationMap