X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/d17eb0f54cd1e12fae9a1d015f4ad4b6b6f764c6..addfb9600277f0ce7d9a4159f3b77d9daf4ea36f:/resources/assets/js/components/markdown-editor.js diff --git a/resources/assets/js/components/markdown-editor.js b/resources/assets/js/components/markdown-editor.js index 7b051dd12..3393829cc 100644 --- a/resources/assets/js/components/markdown-editor.js +++ b/resources/assets/js/components/markdown-editor.js @@ -1,6 +1,8 @@ const MarkdownIt = require("markdown-it"); const mdTasksLists = require('markdown-it-task-lists'); -const code = require('../code'); +const code = require('../libs/code'); + +const DrawIO = require('../libs/drawio'); class MarkdownEditor { @@ -20,13 +22,26 @@ class MarkdownEditor { init() { + let lastClick = 0; + // Prevent markdown display link click redirect this.display.addEventListener('click', event => { + let isDblClick = Date.now() - lastClick < 300; + let link = event.target.closest('a'); - if (link === null) return; + if (link !== null) { + event.preventDefault(); + window.open(link.getAttribute('href')); + return; + } - event.preventDefault(); - window.open(link.getAttribute('href')); + let drawing = event.target.closest('[drawio-diagram]'); + if (drawing !== null && isDblClick) { + this.actionEditDrawing(drawing); + return; + } + + lastClick = Date.now(); }); // Button actions @@ -37,6 +52,7 @@ class MarkdownEditor { let action = button.getAttribute('data-action'); if (action === 'insertImage') this.actionInsertImage(); if (action === 'insertLink') this.actionShowLinkSelector(); + if (action === 'insertDrawing') this.actionStartDrawing(); }); window.$events.listen('editor-markdown-update', value => { @@ -290,6 +306,70 @@ class MarkdownEditor { }); } + // Show draw.io if enabled and handle save. + actionStartDrawing() { + if (document.querySelector('[drawio-enabled]').getAttribute('drawio-enabled') !== 'true') return; + let cursorPos = this.cm.getCursor('from'); + + DrawIO.show(() => { + return Promise.resolve(''); + }, (pngData) => { + // let id = "image-" + Math.random().toString(16).slice(2); + // let loadingImage = window.baseUrl('/loading.gif'); + let data = { + image: pngData, + uploaded_to: Number(document.getElementById('page-editor').getAttribute('page-id')) + }; + + window.$http.post(window.baseUrl('/images/drawing/upload'), data).then(resp => { + let newText = `