]> BookStack Code Mirror - bookstack/blobdiff - resources/js/wysiwyg/plugin-codeeditor.js
Added 'Sort Book' action to chapters
[bookstack] / resources / js / wysiwyg / plugin-codeeditor.js
index 12b2c25fb2d70922b1a98c9a128adf68bfce145f..82052a40d82bf167dda0f8577234c2145395c47d 100644 (file)
@@ -86,20 +86,53 @@ function defineCodeBlockCustomElement(editor) {
         getContent() {
             const code = this.querySelector('code') || this.querySelector('pre');
             const tempEl = document.createElement('pre');
-            tempEl.innerHTML = code.innerHTML.replace().replace(/<br\s*[\/]?>/gi ,'\n').replace(/\ufeff/g, '');
+            tempEl.innerHTML = code.innerHTML.replace(/\ufeff/g, '');
+
+            const brs = tempEl.querySelectorAll('br');
+            for (const br of brs) {
+                br.replaceWith('\n');
+            }
+
             return tempEl.textContent;
         }
 
         connectedCallback() {
+            const connectedTime = Date.now();
             if (this.cm) {
                 return;
             }
 
+            this.cleanChildContent();
+            const content = this.getContent();
+            const lines = content.split('\n').length;
+            const height = (lines * 19.2) + 18 + 24;
+            this.style.height = `${height}px`;
+
             const container = this.shadowRoot.querySelector('.CodeMirrorContainer');
-            importVersioned('code').then(Code => {
-                this.cm = Code.wysiwygView(container, this.getContent(), this.getLanguage());
+            const renderCodeMirror = (Code) => {
+                this.cm = Code.wysiwygView(container, content, this.getLanguage());
+                Code.updateLayout(this.cm);
+                setTimeout(() => {
+                    this.style.height = null;
+                }, 1);
+            };
+
+            window.importVersioned('code').then((Code) => {
+                const timeout = (Date.now() - connectedTime < 20) ? 20 : 0;
+                setTimeout(() => renderCodeMirror(Code), timeout);
             });
         }
+
+        cleanChildContent() {
+            const pre = this.querySelector('pre');
+            if (!pre) return;
+
+            for (const preChild of pre.childNodes) {
+                if (preChild.nodeName === '#text' && preChild.textContent === '') {
+                    preChild.remove();
+                }
+            }
+        }
     }
 
     win.customElements.define('code-block', CodeBlockElement);
@@ -130,15 +163,13 @@ function register(editor, url) {
         } else {
             const textContent = editor.selection.getContent({format: 'text'});
             showPopup(editor, textContent, '', (newCode, newLang) => {
-                const wrap = doc.createElement('code-block');
                 const pre = doc.createElement('pre');
                 const code = doc.createElement('code');
                 code.classList.add(`language-${newLang}`);
                 code.innerText = newCode;
                 pre.append(code);
-                wrap.append(pre);
 
-                editor.insertContent(wrap.outerHTML);
+                editor.insertContent(pre.outerHTML);
             });
         }
     });
@@ -153,7 +184,7 @@ function register(editor, url) {
     editor.on('PreInit', () => {
         editor.parser.addNodeFilter('pre', function(elms) {
             for (const el of elms) {
-                const wrapper = new tinymce.html.Node.create('code-block', {
+                const wrapper = tinymce.html.Node.create('code-block', {
                     contenteditable: 'false',
                 });
 
@@ -168,7 +199,7 @@ function register(editor, url) {
 
         editor.parser.addNodeFilter('code-block', function(elms) {
             for (const el of elms) {
-                el.attr('content-editable', 'false');
+                el.attr('contenteditable', 'false');
             }
         });