- selector: '#html-editor',
- content_css: [
- window.baseUrl('/dist/styles.css'),
- ],
- branding: false,
- skin: this.isDarkMode ? 'dark' : 'lightgray',
- body_class: 'page-content',
- browser_spellcheck: true,
- relative_urls: false,
- directionality : this.textDirection,
- remove_script_host: false,
- document_base_url: window.baseUrl('/'),
- end_container_on_empty_block: true,
- statusbar: false,
- menubar: false,
- paste_data_images: false,
- extended_valid_elements: 'pre[*],svg[*],div[drawio-diagram]',
- automatic_uploads: false,
- valid_children: "-div[p|h1|h2|h3|h4|h5|h6|blockquote],+div[pre],+div[img]",
- plugins: this.plugins,
- imagetools_toolbar: 'imageoptions',
- toolbar: this.getToolBar(),
- content_style: `html, body, html.dark-mode {background: ${this.isDarkMode ? '#222' : '#fff'};} body {padding-left: 15px !important; padding-right: 15px !important; margin:0!important; margin-left:auto!important;margin-right:auto!important;}`,
- style_formats: [
- {title: "Header Large", format: "h2"},
- {title: "Header Medium", format: "h3"},
- {title: "Header Small", format: "h4"},
- {title: "Header Tiny", format: "h5"},
- {title: "Paragraph", format: "p", exact: true, classes: ''},
- {title: "Blockquote", format: "blockquote"},
- {title: "Code Block", icon: "code", cmd: 'codeeditor', format: 'codeeditor'},
- {title: "Inline Code", icon: "code", inline: "code"},
- {title: "Callouts", items: [
- {title: "Info", format: 'calloutinfo'},
- {title: "Success", format: 'calloutsuccess'},
- {title: "Warning", format: 'calloutwarning'},
- {title: "Danger", format: 'calloutdanger'}
- ]},
- ],
- style_formats_merge: false,
- media_alt_source: false,
- media_poster: false,
- formats: {
- codeeditor: {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div'},
- alignleft: {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'align-left'},
- aligncenter: {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'align-center'},
- alignright: {selector: 'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li,table,img', classes: 'align-right'},
- calloutsuccess: {block: 'p', exact: true, attributes: {class: 'callout success'}},
- calloutinfo: {block: 'p', exact: true, attributes: {class: 'callout info'}},
- calloutwarning: {block: 'p', exact: true, attributes: {class: 'callout warning'}},
- calloutdanger: {block: 'p', exact: true, attributes: {class: 'callout danger'}}
- },
- file_browser_callback: function (field_name, url, type, win) {
-
- if (type === 'file') {
- window.EntitySelectorPopup.show(function(entity) {
- const originalField = win.document.getElementById(field_name);
- originalField.value = entity.link;
- const mceForm = originalField.closest('.mce-form');
- const inputs = mceForm.querySelectorAll('input');
-
- // Set text to display if not empty
- if (!inputs[1].value) {
- inputs[1].value = entity.name;
- }
-
- // Set title field
- inputs[2].value = entity.name;
- });
- }
-
- if (type === 'image') {
- // Show image manager
- window.ImageManager.show(function (image) {
-
- // Set popover link input to image url then fire change event
- // to ensure the new value sticks
- win.document.getElementById(field_name).value = image.url;
- if ("createEvent" in document) {
- let evt = document.createEvent("HTMLEvents");
- evt.initEvent("change", false, true);
- win.document.getElementById(field_name).dispatchEvent(evt);
- } else {
- win.document.getElementById(field_name).fireEvent("onchange");
- }
-
- // Replace the actively selected content with the linked image
- let html = `<a href="${image.url}" target="_blank">`;
- html += `<img src="${image.thumbs.display}" alt="${image.name}">`;
- html += '</a>';
- win.tinyMCE.activeEditor.execCommand('mceInsertContent', false, html);
- }, 'gallery');
- }
-
- },
- paste_preprocess: function (plugin, args) {
- let content = args.content;
- if (content.indexOf('<img src="file://') !== -1) {
- args.content = '';
- }
- },
- init_instance_callback: function(editor) {
- loadCustomHeadContent(editor);
- },
- setup: function (editor) {
-
- editor.on('ExecCommand change input NodeChange ObjectResized', editorChange);
-
- editor.on('init', () => {
- editorChange();
- // Scroll to the content if needed.
- const queryParams = (new URL(window.location)).searchParams;
- const scrollId = queryParams.get('content-id');
- if (scrollId) {
- scrollToText(scrollId);
- }
-
- // Override for touch events to allow scroll on mobile
- const container = editor.getContainer();
- const toolbarButtons = container.querySelectorAll('.mce-btn');
- for (let button of toolbarButtons) {
- button.addEventListener('touchstart', event => {
- event.stopPropagation();
- });
- }
- window.editor = editor;
- });
-
- function editorChange() {
- const content = editor.getContent();
- if (context.isDarkMode) {
- editor.contentDocument.documentElement.classList.add('dark-mode');
- }
- window.$events.emit('editor-html-change', content);
- }
-
- function scrollToText(scrollId) {
- const element = editor.dom.get(encodeURIComponent(scrollId).replace(/!/g, '%21'));
- if (!element) {
- return;
- }
-
- // scroll the element into the view and put the cursor at the end.
- element.scrollIntoView();
- editor.selection.select(element, true);
- editor.selection.collapse(false);
- editor.focus();
- }
-
- listenForBookStackEditorEvents(editor);
-
- // TODO - Update to standardise across both editors
- // Use events within listenForBookStackEditorEvents instead (Different event signature)
- window.$events.listen('editor-html-update', html => {
- editor.setContent(html);
- editor.selection.select(editor.getBody(), true);
- editor.selection.collapse(false);
- editorChange(html);
- });
-
- registerEditorShortcuts(editor);
-
- let wrap;
- let draggedContentEditable;
-
- function hasTextContent(node) {
- return node && !!( node.textContent || node.innerText );
- }
-
- editor.on('dragstart', function () {
- let node = editor.selection.getNode();
-
- if (node.nodeName === 'IMG') {
- wrap = editor.dom.getParent(node, '.mceTemp');
-
- if (!wrap && node.parentNode.nodeName === 'A' && !hasTextContent(node.parentNode)) {
- wrap = node.parentNode;
- }
- }
-
- // Track dragged contenteditable blocks
- if (node.hasAttribute('contenteditable') && node.getAttribute('contenteditable') === 'false') {
- draggedContentEditable = node;
- }
-
- });
-
- // Custom drop event handling
- editor.on('drop', function (event) {
- let dom = editor.dom,
- rng = tinymce.dom.RangeUtils.getCaretRangeFromPoint(event.clientX, event.clientY, editor.getDoc());
-
- // Template insertion
- const templateId = event.dataTransfer && event.dataTransfer.getData('bookstack/template');
- if (templateId) {
- event.preventDefault();
- window.$http.get(`/templates/${templateId}`).then(resp => {
- editor.selection.setRng(rng);
- editor.undoManager.transact(function () {
- editor.execCommand('mceInsertContent', false, resp.data.html);
- });
- });
- }
-
- // Don't allow anything to be dropped in a captioned image.
- if (dom.getParent(rng.startContainer, '.mceTemp')) {
- event.preventDefault();
- } else if (wrap) {
- event.preventDefault();
-
- editor.undoManager.transact(function () {
- editor.selection.setRng(rng);
- editor.selection.setNode(wrap);
- dom.remove(wrap);
- });
- }
-
- // Handle contenteditable section drop
- if (!event.isDefaultPrevented() && draggedContentEditable) {
- event.preventDefault();
- editor.undoManager.transact(function () {
- const selectedNode = editor.selection.getNode();
- const range = editor.selection.getRng();
- const selectedNodeRoot = selectedNode.closest('body > *');
- if (range.startOffset > (range.startContainer.length / 2)) {
- editor.$(selectedNodeRoot).after(draggedContentEditable);
- } else {
- editor.$(selectedNodeRoot).before(draggedContentEditable);
- }
- });
- }
-
- // Handle image insert
- if (!event.isDefaultPrevented()) {
- editorPaste(event, editor, context);
- }
-
- wrap = null;
- });
-
- // Custom Image picker button
- editor.addButton('image-insert', {
- title: 'My title',
- icon: 'image',
- tooltip: 'Insert an image',
- onclick: function () {
- window.ImageManager.show(function (image) {
- let html = `<a href="${image.url}" target="_blank">`;
- html += `<img src="${image.thumbs.display}" alt="${image.name}">`;
- html += '</a>';
- editor.execCommand('mceInsertContent', false, html);
- }, 'gallery');
- }
- });
-
- // Paste image-uploads
- editor.on('paste', event => editorPaste(event, editor, context));
-
- // Custom handler hook
- window.$events.emitPublic(context.elem, 'editor-tinymce::setup', {editor});
- }