]> BookStack Code Mirror - bookstack/blobdiff - resources/js/wysiwyg/plugin-codeeditor.js
Added language list favourites sorting, updated styles
[bookstack] / resources / js / wysiwyg / plugin-codeeditor.js
index b0640a450bb1cd2640d1000b0fd8a9de348b8015..db449021501fa72282ac63359c29242c2ed6ef53 100644 (file)
@@ -86,7 +86,13 @@ 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;
         }
 
@@ -97,11 +103,18 @@ function defineCodeBlockCustomElement(editor) {
             }
 
             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');
             const renderCodeMirror = (Code) => {
-                this.cm = Code.wysiwygView(container, this.getContent(), this.getLanguage());
+                this.cm = Code.wysiwygView(container, content, this.getLanguage());
                 Code.updateLayout(this.cm);
+                setTimeout(() => {
+                    this.style.height = null;
+                }, 1);
             };
 
             window.importVersioned('code').then((Code) => {
@@ -152,6 +165,7 @@ function register(editor, url) {
             showPopup(editor, textContent, '', (newCode, newLang) => {
                 const pre = doc.createElement('pre');
                 const code = doc.createElement('code');
+                console.log(newCode);
                 code.classList.add(`language-${newLang}`);
                 code.innerText = newCode;
                 pre.append(code);
@@ -171,7 +185,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',
                 });