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'}}
42 function file_picker_callback(callback, value, meta) {
44 // field_name, url, type, win
45 if (meta.filetype === 'file') {
46 window.EntitySelectorPopup.show(entity => {
47 callback(entity.link, {
54 if (meta.filetype === 'image') {
56 window.ImageManager.show(function (image) {
57 callback(image.url, {alt: image.name});
64 * @param {WysiwygConfigOptions} options
67 function gatherPlugins(options) {
86 options.textDirection === 'rtl' ? 'directionality' : '',
89 window.tinymce.PluginManager.add('codeeditor', getCodeeditorPlugin(options));
90 window.tinymce.PluginManager.add('customhr', getCustomhrPlugin(options));
91 window.tinymce.PluginManager.add('imagemanager', getImagemanagerPlugin(options));
92 window.tinymce.PluginManager.add('about', getAboutPlugin(options));
93 window.tinymce.PluginManager.add('details', getDetailsPlugin(options));
94 window.tinymce.PluginManager.add('tasklist', getTasklistPlugin(options));
96 if (options.drawioUrl) {
97 window.tinymce.PluginManager.add('drawio', getDrawioPlugin(options));
98 plugins.push('drawio');
101 return plugins.filter(plugin => Boolean(plugin)).join(' ');
105 * Fetch custom HTML head content from the parent page head into the editor.
107 function fetchCustomHeadContent() {
108 const headContentLines = document.head.innerHTML.split("\n");
109 const startLineIndex = headContentLines.findIndex(line => line.trim() === '<!-- Start: custom user content -->');
110 const endLineIndex = headContentLines.findIndex(line => line.trim() === '<!-- End: custom user content -->');
111 if (startLineIndex === -1 || endLineIndex === -1) {
114 return headContentLines.slice(startLineIndex + 1, endLineIndex).join('\n');
118 * Setup a serializer filter for <br> tags to ensure they're not rendered
119 * within code blocks and that we use newlines there instead.
120 * @param {Editor} editor
122 function setupBrFilter(editor) {
123 editor.serializer.addNodeFilter('br', function(nodes) {
124 for (const node of nodes) {
125 if (node.parent && node.parent.name === 'code') {
126 const newline = new tinymce.html.Node.create('#text');
127 newline.value = '\n';
128 node.replace(newline);
135 * @param {WysiwygConfigOptions} options
136 * @return {function(Editor)}
138 function getSetupCallback(options) {
139 return function(editor) {
140 editor.on('ExecCommand change input NodeChange ObjectResized', editorChange);
141 listenForCommonEvents(editor);
142 registerShortcuts(editor);
143 listenForDragAndPaste(editor, options);
145 editor.on('init', () => {
147 scrollToQueryString(editor);
148 window.editor = editor;
151 editor.on('PreInit', () => {
152 setupBrFilter(editor);
155 function editorChange() {
156 const content = editor.getContent();
157 if (options.darkMode) {
158 editor.contentDocument.documentElement.classList.add('dark-mode');
160 window.$events.emit('editor-html-change', content);
163 // Custom handler hook
164 window.$events.emitPublic(options.containerElement, 'editor-tinymce::setup', {editor});
166 // Inline code format button
167 editor.ui.registry.addButton('inlinecode', {
168 tooltip: 'Inline code',
171 editor.execCommand('mceToggleFormat', false, 'code');
178 * @param {WysiwygConfigOptions} options
180 function getContentStyle(options) {
182 html, body, html.dark-mode {
183 background: ${options.darkMode ? '#222' : '#fff'};
186 padding-left: 15px !important;
187 padding-right: 15px !important;
188 height: initial !important;
190 margin-left: auto! important;
191 margin-right: auto !important;
192 overflow-y: hidden !important;
193 }`.trim().replace('\n', '');
197 * @param {WysiwygConfigOptions} options
200 export function build(options) {
203 window.tinymce.addI18n(options.language, options.translationMap);
206 const version = document.querySelector('script[src*="/dist/app.js"]').getAttribute('src').split('?version=')[1];
208 // Return config object
212 selector: '#html-editor',
213 cache_suffix: '?version=' + version,
215 window.baseUrl('/dist/styles.css'),
218 skin: options.darkMode ? 'oxide-dark' : 'oxide',
219 body_class: 'page-content',
220 browser_spellcheck: true,
221 relative_urls: false,
222 language: options.language,
223 directionality: options.textDirection,
224 remove_script_host: false,
225 document_base_url: window.baseUrl('/'),
226 end_container_on_empty_block: true,
227 remove_trailing_brs: false,
230 paste_data_images: false,
231 extended_valid_elements: 'pre[*],svg[*],div[drawio-diagram],details[*],summary[*],div[*],li[class|checked]',
232 automatic_uploads: false,
233 custom_elements: 'doc-root,code-block',
235 "-div[p|h1|h2|h3|h4|h5|h6|blockquote|code-block]",
237 "-doc-root[doc-root|#text]",
240 "+doc-root[code-block]"
242 plugins: gatherPlugins(options),
243 imagetools_toolbar: 'imageoptions',
245 toolbar: getPrimaryToolbar(options),
246 content_style: getContentStyle(options),
248 style_formats_merge: false,
249 media_alt_source: false,
252 file_picker_types: 'file image',
253 file_picker_callback,
254 paste_preprocess(plugin, args) {
255 const content = args.content;
256 if (content.indexOf('<img src="file://') !== -1) {
260 init_instance_callback(editor) {
261 const head = editor.getDoc().querySelector('head');
262 head.innerHTML += fetchCustomHeadContent();
265 registerAdditionalToolbars(editor, options);
266 getSetupCallback(options)(editor);
272 * @typedef {Object} WysiwygConfigOptions
273 * @property {Element} containerElement
274 * @property {string} language
275 * @property {boolean} darkMode
276 * @property {string} textDirection
277 * @property {string} drawioUrl
278 * @property {int} pageId
279 * @property {Object} translations
280 * @property {Object} translationMap