X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/db4093d523352a29cabddd79ad40262f6fa1b5c6..refs/pull/5280/head:/resources/js/components/wysiwyg-editor.js diff --git a/resources/js/components/wysiwyg-editor.js b/resources/js/components/wysiwyg-editor.js index 9d6f70e32..56dbe8d7c 100644 --- a/resources/js/components/wysiwyg-editor.js +++ b/resources/js/components/wysiwyg-editor.js @@ -1,762 +1,68 @@ -import Code from "../services/code"; -import DrawIO from "../services/drawio"; -import Clipboard from "../services/clipboard"; +import {Component} from './component'; -/** - * Handle pasting images from clipboard. - * @param {ClipboardEvent} event - * @param {WysiwygEditor} wysiwygComponent - * @param editor - */ -function editorPaste(event, editor, wysiwygComponent) { - const clipboard = new Clipboard(event.clipboardData || event.dataTransfer); +export class WysiwygEditor extends Component { - // Don't handle the event ourselves if no items exist of contains table-looking data - if (!clipboard.hasItems() || clipboard.containsTabularData()) { - return; - } - - const images = clipboard.getImages(); - for (const imageFile of images) { - - const id = "image-" + Math.random().toString(16).slice(2); - const loadingImage = window.baseUrl('/loading.gif'); - event.preventDefault(); - - setTimeout(() => { - editor.insertContent(`
`;
- wrap.querySelector('code').innerText = code;
+ /** @var {SimpleWysiwygEditorInterface|null} */
+ this.editor = null;
- editor.formatter.toggle('pre');
- const node = editor.selection.getNode();
- editor.dom.setHTML(node, wrap.querySelector('pre').innerHTML);
- editor.fire('SetContent');
+ const translations = {
+ ...window.editor_translations,
+ imageUploadErrorText: this.$opts.imageUploadErrorText,
+ serverUploadLimitText: this.$opts.serverUploadLimitText,
+ };
- editor.focus()
+ window.importVersioned('wysiwyg').then(wysiwyg => {
+ const editorContent = this.input.value;
+ this.editor = wysiwyg.createPageEditorInstance(this.editContainer, editorContent, {
+ drawioUrl: this.getDrawIoUrl(),
+ pageId: Number(this.$opts.pageId),
+ darkMode: document.documentElement.classList.contains('dark-mode'),
+ textDirection: this.$opts.textDirection,
+ translations,
});
- return;
- }
-
- const lang = selectedNode.hasAttribute('data-lang') ? selectedNode.getAttribute('data-lang') : '';
- const currentCode = selectedNode.querySelector('textarea').textContent;
-
- window.components.first('code-editor').open(currentCode, lang, (code, lang) => {
- const editorElem = selectedNode.querySelector('.CodeMirror');
- const cmInstance = editorElem.CodeMirror;
- if (cmInstance) {
- Code.setContent(cmInstance, code);
- Code.setMode(cmInstance, lang, code);
- }
- const textArea = selectedNode.querySelector('textarea');
- if (textArea) textArea.textContent = code;
- selectedNode.setAttribute('data-lang', lang);
-
- editor.focus()
});
- }
-
- function codeMirrorContainerToPre(codeMirrorContainer) {
- const textArea = codeMirrorContainer.querySelector('textarea');
- const code = textArea.textContent;
- const lang = codeMirrorContainer.getAttribute('data-lang');
- codeMirrorContainer.removeAttribute('contentEditable');
- const pre = document.createElement('pre');
- const codeElem = document.createElement('code');
- codeElem.classList.add(`language-${lang}`);
- codeElem.textContent = code;
- pre.appendChild(codeElem);
-
- codeMirrorContainer.parentElement.replaceChild(pre, codeMirrorContainer);
- }
-
- window.tinymce.PluginManager.add('codeeditor', function(editor, url) {
-
- const $ = editor.$;
-
- editor.ui.registry.addButton('codeeditor', {
- text: 'Code block',
- icon: false,
- onAction() {
- editor.execCommand('codeeditor');
+ let handlingFormSubmit = false;
+ this.input.form.addEventListener('submit', event => {
+ if (!this.editor) {
+ return;
}
- });
-
- editor.addCommand('codeeditor', () => {
- showPopup(editor);
- });
-
- // Convert
- editor.on('PreProcess', function (e) {
- $('div.CodeMirrorContainer', e.node).each((index, elem) => {
- codeMirrorContainerToPre(elem);
- });
- });
-
- editor.on('dblclick', event => {
- let selectedNode = editor.selection.getNode();
- if (!elemIsCodeBlock(selectedNode)) return;
- showPopup(editor);
- });
- function parseCodeMirrorInstances() {
-
- // Recover broken codemirror instances
- $('.CodeMirrorContainer').filter((index ,elem) => {
- return typeof elem.querySelector('.CodeMirror').CodeMirror === 'undefined';
- }).each((index, elem) => {
- codeMirrorContainerToPre(elem);
- });
-
- const codeSamples = $('body > pre').filter((index, elem) => {
- return elem.contentEditable !== "false";
- });
-
- codeSamples.each((index, elem) => {
- Code.wysiwygView(elem);
- });
- }
-
- editor.on('init', function() {
- // Parse code mirror instances on init, but delay a little so this runs after
- // initial styles are fetched into the editor.
- editor.undoManager.transact(function () {
- parseCodeMirrorInstances();
- });
- // Parsed code mirror blocks when content is set but wait before setting this handler
- // to avoid any init 'SetContent' events.
- setTimeout(() => {
- editor.on('SetContent', () => {
- setTimeout(parseCodeMirrorInstances, 100);
+ if (!handlingFormSubmit) {
+ event.preventDefault();
+ handlingFormSubmit = true;
+ this.editor.getContentAsHtml().then(html => {
+ this.input.value = html;
+ this.input.form.submit();
});
- }, 200);
- });
-
- });
-}
-
-function drawIoPlugin(drawioUrl, isDarkMode, pageId, wysiwygComponent) {
-
- let pageEditor = null;
- let currentNode = null;
-
- function isDrawing(node) {
- return node.hasAttribute('drawio-diagram');
- }
-
- function showDrawingManager(mceEditor, selectedNode = null) {
- pageEditor = mceEditor;
- currentNode = selectedNode;
- // Show image manager
- window.ImageManager.show(function (image) {
- if (selectedNode) {
- let imgElem = selectedNode.querySelector('img');
- pageEditor.dom.setAttrib(imgElem, 'src', image.url);
- pageEditor.dom.setAttrib(selectedNode, 'drawio-diagram', image.id);
- } else {
- let imgHTML = `", cmd: 'codeeditor', format: 'codeeditor'},
- {title: "Inline 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
- const imageUrl = image.thumbs.display || image.url;
- let html = ``;
- html += `
`;
- html += '';
- win.tinyMCE.activeEditor.execCommand('mceInsertContent', false, html);
- }, 'gallery');
- }
-
- },
- paste_preprocess: function (plugin, args) {
- let content = args.content;
- if (content.indexOf('
`;
- html += `
`;
- html += '';
- 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});
- }
+ html: await this.editor.getContentAsHtml(),
};
}
}
-
-export default WysiwygEditor;