X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/ad48cd3e48ab5b16932d8c38032b80af619f525f..refs/pull/2957/head:/resources/js/components/markdown-editor.js diff --git a/resources/js/components/markdown-editor.js b/resources/js/components/markdown-editor.js index 5ffe4ef4d..a90f74e27 100644 --- a/resources/js/components/markdown-editor.js +++ b/resources/js/components/markdown-editor.js @@ -13,6 +13,8 @@ class MarkdownEditor { this.pageId = this.$opts.pageId; this.textDirection = this.$opts.textDirection; + this.imageUploadErrorText = this.$opts.imageUploadErrorText; + this.serverUploadLimitText = this.$opts.serverUploadLimitText; this.markdown = new MarkdownIt({html: true}); this.markdown.use(mdTasksLists, {label: true}); @@ -21,7 +23,6 @@ class MarkdownEditor { this.displayStylesLoaded = false; this.input = this.elem.querySelector('textarea'); - this.htmlInput = this.elem.querySelector('input[name=html]'); this.cm = code.markdownEditor(this.input); this.onMarkdownScroll = this.onMarkdownScroll.bind(this); @@ -124,7 +125,6 @@ class MarkdownEditor { // Set body content this.displayDoc.body.className = 'page-content'; this.displayDoc.body.innerHTML = html; - this.htmlInput.value = html; // Copy styles from page head and set custom styles for editor this.loadStylesIntoDisplay(); @@ -373,7 +373,7 @@ class MarkdownEditor { const newContent = `[![${selectedText}](${resp.data.thumbs.display})](${resp.data.url})`; replaceContent(placeHolderText, newContent); }).catch(err => { - window.$events.emit('error', trans('errors.image_upload_error')); + window.$events.emit('error', context.imageUploadErrorText); replaceContent(placeHolderText, selectedText); console.log(err); }); @@ -440,15 +440,14 @@ class MarkdownEditor { const data = { image: pngData, - uploaded_to: Number(document.getElementById('page-editor').getAttribute('page-id')) + uploaded_to: Number(this.pageId), }; - window.$http.post(window.baseUrl('/images/drawio'), data).then(resp => { + window.$http.post("/images/drawio", data).then(resp => { this.insertDrawing(resp.data, cursorPos); DrawIO.close(); }).catch(err => { - window.$events.emit('error', trans('errors.image_upload_error')); - console.log(err); + this.handleDrawingUploadError(err); }); }); } @@ -476,10 +475,10 @@ class MarkdownEditor { let data = { image: pngData, - uploaded_to: Number(document.getElementById('page-editor').getAttribute('page-id')) + uploaded_to: Number(this.pageId), }; - window.$http.post(window.baseUrl(`/images/drawio`), data).then(resp => { + window.$http.post("/images/drawio", data).then(resp => { let newText = `
`; let newContent = this.cm.getValue().split('\n').map(line => { if (line.indexOf(`drawio-diagram="${drawingId}"`) !== -1) { @@ -492,12 +491,20 @@ class MarkdownEditor { this.cm.focus(); DrawIO.close(); }).catch(err => { - window.$events.emit('error', trans('errors.image_upload_error')); - console.log(err); + this.handleDrawingUploadError(err); }); }); } + handleDrawingUploadError(error) { + if (error.status === 413) { + window.$events.emit('error', this.serverUploadLimitText); + } else { + window.$events.emit('error', this.imageUploadErrorText); + } + console.log(error); + } + // Make the editor full screen actionFullScreen() { const alreadyFullscreen = this.elem.classList.contains('fullscreen'); @@ -563,6 +570,12 @@ class MarkdownEditor { this.cm.setCursor(cursorPos.line + prependLineCount, cursorPos.ch); }); + // Insert editor content at the current location + window.$events.listen('editor::insert', (eventContent) => { + const markdown = getContentToInsert(eventContent); + this.cm.replaceSelection(markdown); + }); + // Focus on editor window.$events.listen('editor::focus', () => { this.cm.focus();