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 getTasklistPlugin} from './plugins-tasklist';
16 import {handleEmbedAlignmentChanges} from './fixes';
18 const styleFormats = [
19 {title: 'Large Header', format: 'h2', preview: 'color: blue;'},
20 {title: 'Medium Header', format: 'h3'},
21 {title: 'Small Header', format: 'h4'},
22 {title: 'Tiny Header', format: 'h5'},
24 title: 'Paragraph', format: 'p', exact: true, classes: '',
26 {title: 'Blockquote', format: 'blockquote'},
30 {title: 'Information', format: 'calloutinfo'},
31 {title: 'Success', format: 'calloutsuccess'},
32 {title: 'Warning', format: 'calloutwarning'},
33 {title: 'Danger', format: 'calloutdanger'},
39 alignleft: {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img,iframe,video,span', classes: 'align-left'},
40 aligncenter: {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img,iframe,video,span', classes: 'align-center'},
41 alignright: {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img,iframe,video,span', classes: 'align-right'},
42 calloutsuccess: {block: 'p', exact: true, attributes: {class: 'callout success'}},
43 calloutinfo: {block: 'p', exact: true, attributes: {class: 'callout info'}},
44 calloutwarning: {block: 'p', exact: true, attributes: {class: 'callout warning'}},
45 calloutdanger: {block: 'p', exact: true, attributes: {class: 'callout danger'}},
77 function filePickerCallback(callback, value, meta) {
78 // field_name, url, type, win
79 if (meta.filetype === 'file') {
80 /** @type {EntitySelectorPopup} * */
81 const selector = window.$components.first('entity-selector-popup');
82 const selectionText = this.selection.getContent({format: 'text'}).trim();
83 selector.show(entity => {
84 callback(entity.link, {
89 initialValue: selectionText,
90 searchEndpoint: '/search/entity-selector',
91 entityTypes: 'page,book,chapter,bookshelf',
92 entityPermission: 'view',
96 if (meta.filetype === 'image') {
98 /** @type {ImageManager} * */
99 const imageManager = window.$components.first('image-manager');
100 imageManager.show(image => {
101 callback(image.url, {alt: image.name});
107 * @param {WysiwygConfigOptions} options
110 function gatherPlugins(options) {
127 options.textDirection === 'rtl' ? 'directionality' : '',
130 window.tinymce.PluginManager.add('codeeditor', getCodeeditorPlugin());
131 window.tinymce.PluginManager.add('customhr', getCustomhrPlugin());
132 window.tinymce.PluginManager.add('imagemanager', getImagemanagerPlugin());
133 window.tinymce.PluginManager.add('about', getAboutPlugin());
134 window.tinymce.PluginManager.add('details', getDetailsPlugin());
135 window.tinymce.PluginManager.add('tasklist', getTasklistPlugin());
137 if (options.drawioUrl) {
138 window.tinymce.PluginManager.add('drawio', getDrawioPlugin(options));
139 plugins.push('drawio');
142 return plugins.filter(plugin => Boolean(plugin));
146 * Fetch custom HTML head content nodes from the outer page head
147 * and add them to the given editor document.
148 * @param {Document} editorDoc
150 function addCustomHeadContent(editorDoc) {
151 const headContentLines = document.head.innerHTML.split('\n');
152 const startLineIndex = headContentLines.findIndex(line => line.trim() === '<!-- Start: custom user content -->');
153 const endLineIndex = headContentLines.findIndex(line => line.trim() === '<!-- End: custom user content -->');
154 if (startLineIndex === -1 || endLineIndex === -1) {
158 const customHeadHtml = headContentLines.slice(startLineIndex + 1, endLineIndex).join('\n');
159 const el = editorDoc.createElement('div');
160 el.innerHTML = customHeadHtml;
162 editorDoc.head.append(...el.children);
166 * @param {WysiwygConfigOptions} options
167 * @return {function(Editor)}
169 function getSetupCallback(options) {
170 return function setupCallback(editor) {
171 function editorChange() {
172 if (options.darkMode) {
173 editor.contentDocument.documentElement.classList.add('dark-mode');
175 window.$events.emit('editor-html-change', '');
178 editor.on('ExecCommand change input NodeChange ObjectResized', editorChange);
179 listenForCommonEvents(editor);
180 listenForDragAndPaste(editor, options);
182 editor.on('init', () => {
184 scrollToQueryString(editor);
185 window.editor = editor;
186 registerShortcuts(editor);
189 editor.on('PreInit', () => {
190 setupFilters(editor);
193 handleEmbedAlignmentChanges(editor);
195 // Custom handler hook
196 window.$events.emitPublic(options.containerElement, 'editor-tinymce::setup', {editor});
198 // Inline code format button
199 editor.ui.registry.addButton('inlinecode', {
200 tooltip: 'Inline code',
203 editor.execCommand('mceToggleFormat', false, 'code');
210 * @param {WysiwygConfigOptions} options
212 function getContentStyle(options) {
214 html, body, html.dark-mode {
215 background: ${options.darkMode ? '#222' : '#fff'};
218 padding-left: 15px !important;
219 padding-right: 15px !important;
220 height: initial !important;
222 margin-left: auto! important;
223 margin-right: auto !important;
224 overflow-y: hidden !important;
225 }`.trim().replace('\n', '');
229 * @param {WysiwygConfigOptions} options
232 export function buildForEditor(options) {
234 window.tinymce.addI18n(options.language, options.translationMap);
237 const version = document.querySelector('script[src*="/dist/app.js"]').getAttribute('src').split('?version=')[1];
239 // Return config object
243 selector: '#html-editor',
244 cache_suffix: `?version=${version}`,
246 window.baseUrl('/dist/styles.css'),
249 skin: options.darkMode ? 'tinymce-5-dark' : 'tinymce-5',
250 body_class: 'page-content',
251 browser_spellcheck: true,
252 relative_urls: false,
253 language: options.language,
254 directionality: options.textDirection,
255 remove_script_host: false,
256 document_base_url: window.baseUrl('/'),
257 end_container_on_empty_block: true,
258 remove_trailing_brs: false,
261 paste_data_images: false,
262 extended_valid_elements: 'pre[*],svg[*],div[drawio-diagram],details[*],summary[*],div[*],li[class|checked|style]',
263 automatic_uploads: false,
264 custom_elements: 'doc-root,code-block',
266 '-div[p|h1|h2|h3|h4|h5|h6|blockquote|code-block]',
268 '-doc-root[doc-root|#text]',
271 '+doc-root[p|h1|h2|h3|h4|h5|h6|blockquote|code-block|div|hr]',
273 plugins: gatherPlugins(options),
275 toolbar: getPrimaryToolbar(options),
276 content_style: getContentStyle(options),
277 style_formats: styleFormats,
278 style_formats_merge: false,
279 media_alt_source: false,
282 table_style_by_css: true,
283 table_use_colgroups: true,
284 file_picker_types: 'file image',
286 file_picker_callback: filePickerCallback,
287 paste_preprocess(plugin, args) {
288 const {content} = args;
289 if (content.indexOf('<img src="file://') !== -1) {
293 init_instance_callback(editor) {
294 addCustomHeadContent(editor.getDoc());
297 registerCustomIcons(editor);
298 registerAdditionalToolbars(editor);
299 getSetupCallback(options)(editor);
305 * @param {WysiwygConfigOptions} options
306 * @return {RawEditorOptions}
308 export function buildForInput(options) {
310 window.tinymce.addI18n(options.language, options.translationMap);
313 const version = document.querySelector('script[src*="/dist/app.js"]').getAttribute('src').split('?version=')[1];
315 // Return config object
319 target: options.containerElement,
320 cache_suffix: `?version=${version}`,
322 window.baseUrl('/dist/styles.css'),
325 skin: options.darkMode ? 'tinymce-5-dark' : 'tinymce-5',
326 body_class: 'wysiwyg-input',
327 browser_spellcheck: true,
328 relative_urls: false,
329 language: options.language,
330 directionality: options.textDirection,
331 remove_script_host: false,
332 document_base_url: window.baseUrl('/'),
333 end_container_on_empty_block: true,
334 remove_trailing_brs: false,
337 plugins: 'link autolink lists',
339 toolbar: 'bold italic link bullist numlist',
340 content_style: getContentStyle(options),
341 file_picker_types: 'file',
342 valid_elements: 'p,a[href|title],ol,ul,li,strong,em,br',
343 file_picker_callback: filePickerCallback,
344 init_instance_callback(editor) {
345 addCustomHeadContent(editor.getDoc());
347 editor.contentDocument.documentElement.classList.toggle('dark-mode', options.darkMode);
353 * @typedef {Object} WysiwygConfigOptions
354 * @property {Element} containerElement
355 * @property {string} language
356 * @property {boolean} darkMode
357 * @property {string} textDirection
358 * @property {string} drawioUrl
359 * @property {int} pageId
360 * @property {Object} translations
361 * @property {Object} translationMap