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, {
91 if (meta.filetype === 'image') {
93 /** @type {ImageManager} * */
94 const imageManager = window.$components.first('image-manager');
95 imageManager.show(image => {
96 callback(image.url, {alt: image.name});
102 * @param {WysiwygConfigOptions} options
105 function gatherPlugins(options) {
122 options.textDirection === 'rtl' ? 'directionality' : '',
125 window.tinymce.PluginManager.add('codeeditor', getCodeeditorPlugin());
126 window.tinymce.PluginManager.add('customhr', getCustomhrPlugin());
127 window.tinymce.PluginManager.add('imagemanager', getImagemanagerPlugin());
128 window.tinymce.PluginManager.add('about', getAboutPlugin());
129 window.tinymce.PluginManager.add('details', getDetailsPlugin());
130 window.tinymce.PluginManager.add('tasklist', getTasklistPlugin());
132 if (options.drawioUrl) {
133 window.tinymce.PluginManager.add('drawio', getDrawioPlugin(options));
134 plugins.push('drawio');
137 return plugins.filter(plugin => Boolean(plugin));
141 * Fetch custom HTML head content from the parent page head into the editor.
143 function fetchCustomHeadContent() {
144 const headContentLines = document.head.innerHTML.split('\n');
145 const startLineIndex = headContentLines.findIndex(line => line.trim() === '<!-- Start: custom user content -->');
146 const endLineIndex = headContentLines.findIndex(line => line.trim() === '<!-- End: custom user content -->');
147 if (startLineIndex === -1 || endLineIndex === -1) {
150 return headContentLines.slice(startLineIndex + 1, endLineIndex).join('\n');
154 * @param {WysiwygConfigOptions} options
155 * @return {function(Editor)}
157 function getSetupCallback(options) {
158 return function setupCallback(editor) {
159 function editorChange() {
160 if (options.darkMode) {
161 editor.contentDocument.documentElement.classList.add('dark-mode');
163 window.$events.emit('editor-html-change', '');
166 editor.on('ExecCommand change input NodeChange ObjectResized', editorChange);
167 listenForCommonEvents(editor);
168 listenForDragAndPaste(editor, options);
170 editor.on('init', () => {
172 scrollToQueryString(editor);
173 window.editor = editor;
174 registerShortcuts(editor);
177 editor.on('PreInit', () => {
178 setupFilters(editor);
181 handleEmbedAlignmentChanges(editor);
183 // Custom handler hook
184 window.$events.emitPublic(options.containerElement, 'editor-tinymce::setup', {editor});
186 // Inline code format button
187 editor.ui.registry.addButton('inlinecode', {
188 tooltip: 'Inline code',
191 editor.execCommand('mceToggleFormat', false, 'code');
198 * @param {WysiwygConfigOptions} options
200 function getContentStyle(options) {
202 html, body, html.dark-mode {
203 background: ${options.darkMode ? '#222' : '#fff'};
206 padding-left: 15px !important;
207 padding-right: 15px !important;
208 height: initial !important;
210 margin-left: auto! important;
211 margin-right: auto !important;
212 overflow-y: hidden !important;
213 }`.trim().replace('\n', '');
217 * @param {WysiwygConfigOptions} options
220 export function buildForEditor(options) {
222 window.tinymce.addI18n(options.language, options.translationMap);
225 const version = document.querySelector('script[src*="/dist/app.js"]').getAttribute('src').split('?version=')[1];
227 // Return config object
231 selector: '#html-editor',
232 cache_suffix: `?version=${version}`,
234 window.baseUrl('/dist/styles.css'),
237 skin: options.darkMode ? 'tinymce-5-dark' : 'tinymce-5',
238 body_class: 'page-content',
239 browser_spellcheck: true,
240 relative_urls: false,
241 language: options.language,
242 directionality: options.textDirection,
243 remove_script_host: false,
244 document_base_url: window.baseUrl('/'),
245 end_container_on_empty_block: true,
246 remove_trailing_brs: false,
249 paste_data_images: false,
250 extended_valid_elements: 'pre[*],svg[*],div[drawio-diagram],details[*],summary[*],div[*],li[class|checked|style]',
251 automatic_uploads: false,
252 custom_elements: 'doc-root,code-block',
254 '-div[p|h1|h2|h3|h4|h5|h6|blockquote|code-block]',
256 '-doc-root[doc-root|#text]',
259 '+doc-root[p|h1|h2|h3|h4|h5|h6|blockquote|code-block|div|hr]',
261 plugins: gatherPlugins(options),
263 toolbar: getPrimaryToolbar(options),
264 content_style: getContentStyle(options),
265 style_formats: styleFormats,
266 style_formats_merge: false,
267 media_alt_source: false,
270 table_style_by_css: true,
271 table_use_colgroups: true,
272 file_picker_types: 'file image',
274 file_picker_callback: filePickerCallback,
275 paste_preprocess(plugin, args) {
276 const {content} = args;
277 if (content.indexOf('<img src="file://') !== -1) {
281 init_instance_callback(editor) {
282 const head = editor.getDoc().querySelector('head');
283 head.innerHTML += fetchCustomHeadContent();
286 registerCustomIcons(editor);
287 registerAdditionalToolbars(editor);
288 getSetupCallback(options)(editor);
294 * @param {WysiwygConfigOptions} options
295 * @return {RawEditorOptions}
297 export function buildForInput(options) {
299 window.tinymce.addI18n(options.language, options.translationMap);
302 const version = document.querySelector('script[src*="/dist/app.js"]').getAttribute('src').split('?version=')[1];
304 // Return config object
308 target: options.containerElement,
309 cache_suffix: `?version=${version}`,
311 window.baseUrl('/dist/styles.css'),
314 skin: options.darkMode ? 'tinymce-5-dark' : 'tinymce-5',
315 body_class: 'page-content',
316 browser_spellcheck: true,
317 relative_urls: false,
318 language: options.language,
319 directionality: options.textDirection,
320 remove_script_host: false,
321 document_base_url: window.baseUrl('/'),
322 end_container_on_empty_block: true,
323 remove_trailing_brs: false,
326 plugins: 'link autolink',
328 toolbar: 'bold italic underline link',
329 content_style: getContentStyle(options),
331 init_instance_callback(editor) {
332 const head = editor.getDoc().querySelector('head');
333 head.innerHTML += fetchCustomHeadContent();
342 * @typedef {Object} WysiwygConfigOptions
343 * @property {Element} containerElement
344 * @property {string} language
345 * @property {boolean} darkMode
346 * @property {string} textDirection
347 * @property {string} drawioUrl
348 * @property {int} pageId
349 * @property {Object} translations
350 * @property {Object} translationMap