]> BookStack Code Mirror - bookstack/commitdiff
Added pre-render sizes to wysiwyg code blocks
authorDan Brown <redacted>
Fri, 25 Mar 2022 11:13:04 +0000 (11:13 +0000)
committerDan Brown <redacted>
Fri, 25 Mar 2022 11:13:04 +0000 (11:13 +0000)
Sets sizes on WYSIWYG code block sections based on content lines
as an early pre-codemirror height prediction to avoid excessive
jumping in the editor.

For #3326

resources/js/wysiwyg/plugin-codeeditor.js

index b0640a450bb1cd2640d1000b0fd8a9de348b8015..82ab7d3c8eebad8327a842ef208c575e6451ded0 100644 (file)
@@ -97,11 +97,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) => {