X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/aaa2205df1055a02243c1fe3c191dbe411e8290a..refs/pull/3333/head:/resources/js/components/markdown-editor.js diff --git a/resources/js/components/markdown-editor.js b/resources/js/components/markdown-editor.js index a14047d2f..297d9c8ec 100644 --- a/resources/js/components/markdown-editor.js +++ b/resources/js/components/markdown-editor.js @@ -1,6 +1,5 @@ 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"; @@ -23,13 +22,20 @@ class MarkdownEditor { this.displayStylesLoaded = false; this.input = this.elem.querySelector('textarea'); - this.cm = code.markdownEditor(this.input); + + this.cm = null; + this.Code = null; + const cmLoadPromise = window.importVersioned('code').then(Code => { + this.cm = Code.markdownEditor(this.input); + this.Code = Code; + return this.cm; + }); this.onMarkdownScroll = this.onMarkdownScroll.bind(this); const displayLoad = () => { this.displayDoc = this.display.contentDocument; - this.init(); + this.init(cmLoadPromise); }; if (this.display.contentDocument.readyState === 'complete') { @@ -45,7 +51,7 @@ class MarkdownEditor { }); } - init() { + init(cmLoadPromise) { let lastClick = 0; @@ -98,12 +104,15 @@ class MarkdownEditor { toolbarLabel.closest('.markdown-editor-wrap').classList.add('active'); }); - window.$events.listen('editor-markdown-update', value => { - this.cm.setValue(value); - this.updateAndRender(); + cmLoadPromise.then(cm => { + this.codeMirrorSetup(cm); + + // Refresh CodeMirror on container resize + const resizeDebounced = debounce(() => this.Code.updateLayout(cm), 100, false); + const observer = new ResizeObserver(resizeDebounced); + observer.observe(this.elem); }); - this.codeMirrorSetup(); this.listenForBookStackEditorEvents(); // Scroll to text if needed. @@ -112,11 +121,6 @@ class MarkdownEditor { if (scrollText) { this.scrollToText(scrollText); } - - // Refresh CodeMirror on container resize - const resizeDebounced = debounce(() => code.updateLayout(this.cm), 100, false); - const observer = new ResizeObserver(resizeDebounced); - observer.observe(this.elem); } // Update the input content and render the display. @@ -163,15 +167,14 @@ class MarkdownEditor { topElem.scrollIntoView({ block: 'start', inline: 'nearest', behavior: 'smooth'}); } - codeMirrorSetup() { - const cm = this.cm; + codeMirrorSetup(cm) { const context = this; // Text direction // cm.setOption('direction', this.textDirection); cm.setOption('direction', 'ltr'); // Will force to remain as ltr for now due to issues when HTML is in editor. // Custom key commands - let metaKey = code.getMetaKey(); + let metaKey = this.Code.getMetaKey(); const extraKeys = {}; // Insert Image shortcut extraKeys[`${metaKey}-Alt-I`] = function(cm) {