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";
7 import {getPlugin as getCodeeditorPlugin} from "./plugin-codeeditor";
8 import {getPlugin as getDrawioPlugin} from "./plugin-drawio";
9 import {getPlugin as getCustomhrPlugin} from "./plugins-customhr";
10 import {getPlugin as getImagemanagerPlugin} from "./plugins-imagemanager";
11 import {getPlugin as getAboutPlugin} from "./plugins-about";
12 import {getPlugin as getDetailsPlugin} from "./plugins-details";
13 import {getPlugin as getTasklistPlugin} from "./plugins-tasklist";
15 const style_formats = [
16 {title: "Large Header", format: "h2", preview: 'color: blue;'},
17 {title: "Medium Header", format: "h3"},
18 {title: "Small Header", format: "h4"},
19 {title: "Tiny Header", format: "h5"},
20 {title: "Paragraph", format: "p", exact: true, classes: ''},
21 {title: "Blockquote", format: "blockquote"},
23 title: "Callouts", items: [
24 {title: "Information", format: 'calloutinfo'},
25 {title: "Success", format: 'calloutsuccess'},
26 {title: "Warning", format: 'calloutwarning'},
27 {title: "Danger", format: 'calloutdanger'}
33 alignleft: {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'align-left'},
34 aligncenter: {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'align-center'},
35 alignright: {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'align-right'},
36 calloutsuccess: {block: 'p', exact: true, attributes: {class: 'callout success'}},
37 calloutinfo: {block: 'p', exact: true, attributes: {class: 'callout info'}},
38 calloutwarning: {block: 'p', exact: true, attributes: {class: 'callout warning'}},
39 calloutdanger: {block: 'p', exact: true, attributes: {class: 'callout danger'}}
71 function file_picker_callback(callback, value, meta) {
73 // field_name, url, type, win
74 if (meta.filetype === 'file') {
75 window.EntitySelectorPopup.show(entity => {
76 callback(entity.link, {
83 if (meta.filetype === 'image') {
85 window.ImageManager.show(function (image) {
86 callback(image.url, {alt: image.name});
93 * @param {WysiwygConfigOptions} options
96 function gatherPlugins(options) {
113 options.textDirection === 'rtl' ? 'directionality' : '',
116 window.tinymce.PluginManager.add('codeeditor', getCodeeditorPlugin(options));
117 window.tinymce.PluginManager.add('customhr', getCustomhrPlugin(options));
118 window.tinymce.PluginManager.add('imagemanager', getImagemanagerPlugin(options));
119 window.tinymce.PluginManager.add('about', getAboutPlugin(options));
120 window.tinymce.PluginManager.add('details', getDetailsPlugin(options));
121 window.tinymce.PluginManager.add('tasklist', getTasklistPlugin(options));
123 if (options.drawioUrl) {
124 window.tinymce.PluginManager.add('drawio', getDrawioPlugin(options));
125 plugins.push('drawio');
128 return plugins.filter(plugin => Boolean(plugin));
132 * Fetch custom HTML head content from the parent page head into the editor.
134 function fetchCustomHeadContent() {
135 const headContentLines = document.head.innerHTML.split("\n");
136 const startLineIndex = headContentLines.findIndex(line => line.trim() === '<!-- Start: custom user content -->');
137 const endLineIndex = headContentLines.findIndex(line => line.trim() === '<!-- End: custom user content -->');
138 if (startLineIndex === -1 || endLineIndex === -1) {
141 return headContentLines.slice(startLineIndex + 1, endLineIndex).join('\n');
145 * Setup a serializer filter for <br> tags to ensure they're not rendered
146 * within code blocks and that we use newlines there instead.
147 * @param {Editor} editor
149 function setupBrFilter(editor) {
150 editor.serializer.addNodeFilter('br', function(nodes) {
151 for (const node of nodes) {
152 if (node.parent && node.parent.name === 'code') {
153 const newline = tinymce.html.Node.create('#text');
154 newline.value = '\n';
155 node.replace(newline);
162 * @param {WysiwygConfigOptions} options
163 * @return {function(Editor)}
165 function getSetupCallback(options) {
166 return function(editor) {
167 editor.on('ExecCommand change input NodeChange ObjectResized', editorChange);
168 listenForCommonEvents(editor);
169 listenForDragAndPaste(editor, options);
171 editor.on('init', () => {
173 scrollToQueryString(editor);
174 window.editor = editor;
175 registerShortcuts(editor);
178 editor.on('PreInit', () => {
179 setupBrFilter(editor);
182 function editorChange() {
183 const content = editor.getContent();
184 if (options.darkMode) {
185 editor.contentDocument.documentElement.classList.add('dark-mode');
187 window.$events.emit('editor-html-change', content);
190 // Custom handler hook
191 window.$events.emitPublic(options.containerElement, 'editor-tinymce::setup', {editor});
193 // Inline code format button
194 editor.ui.registry.addButton('inlinecode', {
195 tooltip: 'Inline code',
198 editor.execCommand('mceToggleFormat', false, 'code');
205 * @param {WysiwygConfigOptions} options
207 function getContentStyle(options) {
209 html, body, html.dark-mode {
210 background: ${options.darkMode ? '#222' : '#fff'};
213 padding-left: 15px !important;
214 padding-right: 15px !important;
215 height: initial !important;
217 margin-left: auto! important;
218 margin-right: auto !important;
219 overflow-y: hidden !important;
220 }`.trim().replace('\n', '');
224 * @param {WysiwygConfigOptions} options
227 export function build(options) {
230 window.tinymce.addI18n(options.language, options.translationMap);
233 const version = document.querySelector('script[src*="/dist/app.js"]').getAttribute('src').split('?version=')[1];
235 // Return config object
239 selector: '#html-editor',
240 cache_suffix: '?version=' + version,
242 window.baseUrl('/dist/styles.css'),
245 skin: options.darkMode ? 'tinymce-5-dark' : 'tinymce-5',
246 body_class: 'page-content',
247 browser_spellcheck: true,
248 relative_urls: false,
249 language: options.language,
250 directionality: options.textDirection,
251 remove_script_host: false,
252 document_base_url: window.baseUrl('/'),
253 end_container_on_empty_block: true,
254 remove_trailing_brs: false,
257 paste_data_images: false,
258 extended_valid_elements: 'pre[*],svg[*],div[drawio-diagram],details[*],summary[*],div[*],li[class|checked]',
259 automatic_uploads: false,
260 custom_elements: 'doc-root,code-block',
262 "-div[p|h1|h2|h3|h4|h5|h6|blockquote|code-block]",
264 "-doc-root[doc-root|#text]",
267 "+doc-root[p|h1|h2|h3|h4|h5|h6|blockquote|code-block|div]"
269 plugins: gatherPlugins(options),
271 toolbar: getPrimaryToolbar(options),
272 content_style: getContentStyle(options),
274 style_formats_merge: false,
275 media_alt_source: false,
278 table_style_by_css: true,
279 table_use_colgroups: true,
280 file_picker_types: 'file image',
282 file_picker_callback,
283 paste_preprocess(plugin, args) {
284 const content = args.content;
285 if (content.indexOf('<img src="file://') !== -1) {
289 init_instance_callback(editor) {
290 const head = editor.getDoc().querySelector('head');
291 head.innerHTML += fetchCustomHeadContent();
294 registerAdditionalToolbars(editor, options);
295 getSetupCallback(options)(editor);
301 * @typedef {Object} WysiwygConfigOptions
302 * @property {Element} containerElement
303 * @property {string} language
304 * @property {boolean} darkMode
305 * @property {string} textDirection
306 * @property {string} drawioUrl
307 * @property {int} pageId
308 * @property {Object} translations
309 * @property {Object} translationMap