X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/6917ea088f905ec57e16d8795ad0bcb191c38b52..refs/pull/1998/head:/resources/js/components/markdown-editor.js diff --git a/resources/js/components/markdown-editor.js b/resources/js/components/markdown-editor.js index c89a7ad8b..25d6bde47 100644 --- a/resources/js/components/markdown-editor.js +++ b/resources/js/components/markdown-editor.js @@ -1,6 +1,7 @@ import MarkdownIt from "markdown-it"; import mdTasksLists from 'markdown-it-task-lists'; import code from '../services/code'; +import Clipboard from "../services/clipboard"; import {debounce} from "../services/util"; import DrawIO from "../services/drawio"; @@ -30,6 +31,12 @@ class MarkdownEditor { this.displayDoc = this.display.contentDocument; this.init(); }); + + window.$events.emitPublic(elem, 'editor-markdown::setup', { + markdownIt: this.markdown, + displayEl: this.display, + codeMirrorInstance: this.cm, + }); } init() { @@ -69,6 +76,7 @@ class MarkdownEditor { return; } if (action === 'insertDrawing') this.actionStartDrawing(); + if (action === 'fullscreen') this.actionFullScreen(); }); // Mobile section toggling @@ -209,20 +217,16 @@ class MarkdownEditor { // Handle image paste cm.on('paste', (cm, event) => { - const clipboardItems = event.clipboardData.items; - if (!event.clipboardData || !clipboardItems) return; + const clipboard = new Clipboard(event.clipboardData || event.dataTransfer); - // Don't handle if clipboard includes text content - for (let clipboardItem of clipboardItems) { - if (clipboardItem.type.includes('text/')) { - return; - } + // Don't handle the event ourselves if no items exist of contains table-looking data + if (!clipboard.hasItems() || clipboard.containsTabularData()) { + return; } - for (let clipboardItem of clipboardItems) { - if (clipboardItem.type.includes("image")) { - uploadImage(clipboardItem.getAsFile()); - } + const images = clipboard.getImages(); + for (const image of images) { + uploadImage(image); } }); @@ -240,13 +244,15 @@ class MarkdownEditor { }); } - if (event.dataTransfer && event.dataTransfer.files && event.dataTransfer.files.length > 0) { + const clipboard = new Clipboard(event.dataTransfer); + if (clipboard.hasItems()) { const cursorPos = cm.coordsChar({left: event.pageX, top: event.pageY}); cm.setCursor(cursorPos); event.stopPropagation(); event.preventDefault(); - for (let i = 0; i < event.dataTransfer.files.length; i++) { - uploadImage(event.dataTransfer.files[i]); + const images = clipboard.getImages(); + for (const image of images) { + uploadImage(image); } } @@ -475,6 +481,13 @@ class MarkdownEditor { }); } + // Make the editor full screen + actionFullScreen() { + const alreadyFullscreen = this.elem.classList.contains('fullscreen'); + this.elem.classList.toggle('fullscreen', !alreadyFullscreen); + document.body.classList.toggle('markdown-fullscreen', !alreadyFullscreen); + } + // Scroll to a specified text scrollToText(searchText) { if (!searchText) {