]> BookStack Code Mirror - bookstack/blobdiff - resources/js/markdown/display.js
respective book and chapter structure added.
[bookstack] / resources / js / markdown / display.js
index 742198b46065f43c4dd7169ccf9f7c6eb1cc001d..60be26b5fb705491ab5a5712977ac90291382421 100644 (file)
@@ -1,4 +1,4 @@
-import {patchDomFromHtmlString} from "../services/vdom";
+import {patchDomFromHtmlString} from '../services/vdom.ts';
 
 export class Display {
 
@@ -17,6 +17,14 @@ export class Display {
         } else {
             this.container.addEventListener('load', this.onLoad.bind(this));
         }
+
+        this.updateVisibility(editor.settings.get('showPreview'));
+        editor.settings.onChange('showPreview', show => this.updateVisibility(show));
+    }
+
+    updateVisibility(show) {
+        const wrap = this.container.closest('.markdown-editor-wrap');
+        wrap.style.display = show ? null : 'none';
     }
 
     onLoad() {
@@ -26,7 +34,7 @@ export class Display {
         this.doc.body.className = 'page-content';
 
         // Prevent markdown display link click redirect
-        this.doc.addEventListener('click', this.onDisplayClick)
+        this.doc.addEventListener('click', this.onDisplayClick.bind(this));
     }
 
     /**
@@ -73,7 +81,7 @@ export class Display {
      * @param {String} html
      */
     patchWithHtml(html) {
-        const body = this.doc.body;
+        const {body} = this.doc;
 
         if (body.children.length === 0) {
             const wrap = document.createElement('div');
@@ -91,11 +99,11 @@ export class Display {
      * @param {Number} index
      */
     scrollToIndex(index) {
-        const elems = this.doc.body.children;
-        if (elems.length <= index) return;
+        const elems = this.doc.body?.children[0]?.children;
+        if (elems && elems.length <= index) return;
 
-        const topElem = (index === -1) ? elems[elems.length-1] : elems[index];
-        topElem.scrollIntoView({ block: 'start', inline: 'nearest', behavior: 'smooth'});
+        const topElem = (index === -1) ? elems[elems.length - 1] : elems[index];
+        topElem.scrollIntoView({block: 'start', inline: 'nearest', behavior: 'smooth'});
     }
 
-}
\ No newline at end of file
+}