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';
7 import {setupFilters} from './filters';
9 import {getPlugin as getCodeeditorPlugin} from './plugin-codeeditor';
10 import {getPlugin as getDrawioPlugin} from './plugin-drawio';
11 import {getPlugin as getCustomhrPlugin} from './plugins-customhr';
12 import {getPlugin as getImagemanagerPlugin} from './plugins-imagemanager';
13 import {getPlugin as getAboutPlugin} from './plugins-about';
14 import {getPlugin as getDetailsPlugin} from './plugins-details';
15 import {getPlugin as getTableAdditionsPlugin} from './plugins-table-additions';
16 import {getPlugin as getTasklistPlugin} from './plugins-tasklist';
17 import {handleClearFormattingOnTableCells, handleEmbedAlignmentChanges} from './fixes';
19 const styleFormats = [
20 {title: 'Large Header', format: 'h2', preview: 'color: blue;'},
21 {title: 'Medium Header', format: 'h3'},
22 {title: 'Small Header', format: 'h4'},
23 {title: 'Tiny Header', format: 'h5'},
25 title: 'Paragraph', format: 'p', exact: true, classes: '',
27 {title: 'Blockquote', format: 'blockquote'},
31 {title: 'Information', format: 'calloutinfo'},
32 {title: 'Success', format: 'calloutsuccess'},
33 {title: 'Warning', format: 'calloutwarning'},
34 {title: 'Danger', format: 'calloutdanger'},
40 alignleft: {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img,iframe,video,span', classes: 'align-left'},
41 aligncenter: {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img,iframe,video,span', classes: 'align-center'},
42 alignright: {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img,iframe,video,span', classes: 'align-right'},
43 calloutsuccess: {block: 'p', exact: true, attributes: {class: 'callout success'}},
44 calloutinfo: {block: 'p', exact: true, attributes: {class: 'callout info'}},
45 calloutwarning: {block: 'p', exact: true, attributes: {class: 'callout warning'}},
46 calloutdanger: {block: 'p', exact: true, attributes: {class: 'callout danger'}},
78 function filePickerCallback(callback, value, meta) {
79 // field_name, url, type, win
80 if (meta.filetype === 'file') {
81 /** @type {EntitySelectorPopup} * */
82 const selector = window.$components.first('entity-selector-popup');
83 const selectionText = this.selection.getContent({format: 'text'}).trim();
84 selector.show(entity => {
85 callback(entity.link, {
90 initialValue: selectionText,
91 searchEndpoint: '/search/entity-selector',
92 entityTypes: 'page,book,chapter,bookshelf',
93 entityPermission: 'view',
97 if (meta.filetype === 'image') {
99 /** @type {ImageManager} * */
100 const imageManager = window.$components.first('image-manager');
101 imageManager.show(image => {
102 callback(image.url, {alt: image.name});
108 * @param {WysiwygConfigOptions} options
111 function gatherPlugins(options) {
129 options.textDirection === 'rtl' ? 'directionality' : '',
132 window.tinymce.PluginManager.add('codeeditor', getCodeeditorPlugin());
133 window.tinymce.PluginManager.add('customhr', getCustomhrPlugin());
134 window.tinymce.PluginManager.add('imagemanager', getImagemanagerPlugin());
135 window.tinymce.PluginManager.add('about', getAboutPlugin());
136 window.tinymce.PluginManager.add('details', getDetailsPlugin());
137 window.tinymce.PluginManager.add('tasklist', getTasklistPlugin());
138 window.tinymce.PluginManager.add('tableadditions', getTableAdditionsPlugin());
140 if (options.drawioUrl) {
141 window.tinymce.PluginManager.add('drawio', getDrawioPlugin(options));
142 plugins.push('drawio');
145 return plugins.filter(plugin => Boolean(plugin));
149 * Fetch custom HTML head content nodes from the outer page head
150 * and add them to the given editor document.
151 * @param {Document} editorDoc
153 function addCustomHeadContent(editorDoc) {
154 const headContentLines = document.head.innerHTML.split('\n');
155 const startLineIndex = headContentLines.findIndex(line => line.trim() === '<!-- Start: custom user content -->');
156 const endLineIndex = headContentLines.findIndex(line => line.trim() === '<!-- End: custom user content -->');
157 if (startLineIndex === -1 || endLineIndex === -1) {
161 const customHeadHtml = headContentLines.slice(startLineIndex + 1, endLineIndex).join('\n');
162 const el = editorDoc.createElement('div');
163 el.innerHTML = customHeadHtml;
165 editorDoc.head.append(...el.children);
169 * @param {WysiwygConfigOptions} options
170 * @return {function(Editor)}
172 function getSetupCallback(options) {
173 return function setupCallback(editor) {
174 function editorChange() {
175 if (options.darkMode) {
176 editor.contentDocument.documentElement.classList.add('dark-mode');
178 window.$events.emit('editor-html-change', '');
181 editor.on('ExecCommand change input NodeChange ObjectResized', editorChange);
182 listenForCommonEvents(editor);
183 listenForDragAndPaste(editor, options);
185 editor.on('init', () => {
187 scrollToQueryString(editor);
188 window.editor = editor;
189 registerShortcuts(editor);
192 editor.on('PreInit', () => {
193 setupFilters(editor);
196 handleEmbedAlignmentChanges(editor);
197 handleClearFormattingOnTableCells(editor);
199 // Custom handler hook
200 window.$events.emitPublic(options.containerElement, 'editor-tinymce::setup', {editor});
202 // Inline code format button
203 editor.ui.registry.addButton('inlinecode', {
204 tooltip: 'Inline code',
207 editor.execCommand('mceToggleFormat', false, 'code');
214 * @param {WysiwygConfigOptions} options
216 function getContentStyle(options) {
218 html, body, html.dark-mode {
219 background: ${options.darkMode ? '#222' : '#fff'};
222 padding-left: 15px !important;
223 padding-right: 15px !important;
224 height: initial !important;
226 margin-left: auto! important;
227 margin-right: auto !important;
228 overflow-y: hidden !important;
229 }`.trim().replace('\n', '');
233 * @param {WysiwygConfigOptions} options
236 export function buildForEditor(options) {
238 window.tinymce.addI18n(options.language, options.translationMap);
241 const version = document.querySelector('script[src*="/dist/app.js"]').getAttribute('src').split('?version=')[1];
243 // Return config object
247 selector: '#html-editor',
248 cache_suffix: `?version=${version}`,
250 window.baseUrl('/dist/styles.css'),
253 skin: options.darkMode ? 'tinymce-5-dark' : 'tinymce-5',
254 body_class: 'page-content',
255 browser_spellcheck: true,
256 relative_urls: false,
257 language: options.language,
258 directionality: options.textDirection,
259 remove_script_host: false,
260 document_base_url: window.baseUrl('/'),
261 end_container_on_empty_block: true,
262 remove_trailing_brs: false,
265 paste_data_images: false,
266 extended_valid_elements: 'pre[*],svg[*],div[drawio-diagram],details[*],summary[*],div[*],li[class|checked|style]',
267 automatic_uploads: false,
268 custom_elements: 'doc-root,code-block',
270 '-div[p|h1|h2|h3|h4|h5|h6|blockquote|code-block]',
272 '-doc-root[doc-root|#text]',
275 '+doc-root[p|h1|h2|h3|h4|h5|h6|blockquote|code-block|div|hr]',
277 plugins: gatherPlugins(options),
279 toolbar: getPrimaryToolbar(options),
280 content_style: getContentStyle(options),
281 style_formats: styleFormats,
282 style_formats_merge: false,
283 media_alt_source: false,
286 table_style_by_css: true,
287 table_use_colgroups: true,
288 file_picker_types: 'file image',
290 file_picker_callback: filePickerCallback,
291 paste_preprocess(plugin, args) {
292 const {content} = args;
293 if (content.indexOf('<img src="file://') !== -1) {
297 init_instance_callback(editor) {
298 addCustomHeadContent(editor.getDoc());
301 registerCustomIcons(editor);
302 registerAdditionalToolbars(editor);
303 getSetupCallback(options)(editor);
309 * @param {WysiwygConfigOptions} options
310 * @return {RawEditorOptions}
312 export function buildForInput(options) {
314 window.tinymce.addI18n(options.language, options.translationMap);
317 const version = document.querySelector('script[src*="/dist/app.js"]').getAttribute('src').split('?version=')[1];
319 // Return config object
323 target: options.containerElement,
324 cache_suffix: `?version=${version}`,
326 window.baseUrl('/dist/styles.css'),
329 skin: options.darkMode ? 'tinymce-5-dark' : 'tinymce-5',
330 body_class: 'wysiwyg-input',
331 browser_spellcheck: true,
332 relative_urls: false,
333 language: options.language,
334 directionality: options.textDirection,
335 remove_script_host: false,
336 document_base_url: window.baseUrl('/'),
337 end_container_on_empty_block: true,
338 remove_trailing_brs: false,
341 plugins: 'link autolink lists',
343 toolbar: 'bold italic link bullist numlist',
344 content_style: getContentStyle(options),
345 file_picker_types: 'file',
346 valid_elements: 'p,a[href|title],ol,ul,li,strong,em,br',
347 file_picker_callback: filePickerCallback,
348 init_instance_callback(editor) {
349 addCustomHeadContent(editor.getDoc());
351 editor.contentDocument.documentElement.classList.toggle('dark-mode', options.darkMode);
357 * @typedef {Object} WysiwygConfigOptions
358 * @property {Element} containerElement
359 * @property {string} language
360 * @property {boolean} darkMode
361 * @property {string} textDirection
362 * @property {string} drawioUrl
363 * @property {int} pageId
364 * @property {Object} translations
365 * @property {Object} translationMap