]> BookStack Code Mirror - bookstack/commitdiff
Added code to handle scroll for markdown.
authorAbijeet <redacted>
Sun, 10 Jun 2018 07:41:10 +0000 (13:11 +0530)
committerAbijeet <redacted>
Sun, 10 Jun 2018 07:41:10 +0000 (13:11 +0530)
Signed-off-by: Abijeet <redacted>
resources/assets/js/components/markdown-editor.js
resources/assets/js/components/page-display.js
resources/assets/js/components/wysiwyg-editor.js
resources/assets/js/vues/page-editor.js

index 06426bf349a0bfba4d9cda94ec06c769aa99e4b6..4e0ba83baadd4fe3eb18607b90b93caa86e82806 100644 (file)
@@ -18,6 +18,13 @@ class MarkdownEditor {
 
         this.onMarkdownScroll = this.onMarkdownScroll.bind(this);
         this.init();
 
         this.onMarkdownScroll = this.onMarkdownScroll.bind(this);
         this.init();
+
+        // Scroll to text if needed.
+        const queryParams = (new URL(window.location)).searchParams;
+        const scrollText = queryParams.get('content-text');
+        if (scrollText) {
+            this.scrollToText(scrollText);
+        }
     }
 
     init() {
     }
 
     init() {
@@ -387,6 +394,34 @@ class MarkdownEditor {
         });
     }
 
         });
     }
 
+    // Scroll to a specified text
+    scrollToText(searchText) {;
+        if (!searchText) {
+            return;
+        }
+        const content = this.cm.getValue();
+        const lines = content.split(/\r?\n/);
+        let lineNumber = -1;
+        for (let i = 0; i !== lines.length; ++i) {
+            const line = lines[i];
+            if (!line) {
+                continue;
+            }
+            if (line.indexOf(searchText) !== -1) {
+                lineNumber = i;
+                break;
+            }
+        }
+
+        if (lineNumber !== -1) {
+            this.cm.scrollIntoView({
+                line: lineNumber,
+                char: lines[lineNumber].length
+            }, 200);
+            this.cm.focus();
+        }
+    }
+
 }
 
 module.exports = MarkdownEditor ;
\ No newline at end of file
 }
 
 module.exports = MarkdownEditor ;
\ No newline at end of file
index 82676b61b88a583149ada57a42f99c4bc5a23f03..ec5bcd67e99494d12e9133cf98b09a9f1abcf820 100644 (file)
@@ -222,7 +222,7 @@ class PageDisplay {
     }
     setupEditOnHeader() {
         const headingEditIcon = document.querySelector('.heading-edit-icon');
     }
     setupEditOnHeader() {
         const headingEditIcon = document.querySelector('.heading-edit-icon');
-        if (headingEditIcon.length === 0) {
+        if (headingEditIcon === null) {
             // user does not have permission to edit.
             return;
         }
             // user does not have permission to edit.
             return;
         }
@@ -239,7 +239,10 @@ class PageDisplay {
             const headingId = currHeading.id;
 
             let editIcon = visibleHeadingEditIcon.cloneNode(true);
             const headingId = currHeading.id;
 
             let editIcon = visibleHeadingEditIcon.cloneNode(true);
-            editIcon.href += `#${headingId}`;
+
+            // get the first 50 characters.
+            let queryContent = currHeading.textContent && currHeading.textContent.substring(0, 50);
+            editIcon.href += `?content-id=${headingId}&content-text=${encodeURIComponent(queryContent)}`;
 
             currHeading.appendChild(editIcon);
         }
 
             currHeading.appendChild(editIcon);
         }
index 69286abffff6227051ebcf722c20fa6d8a367082..a094359eced82de020133117046184ab0d7f4ba7 100644 (file)
@@ -483,22 +483,25 @@ class WysiwygEditor {
             },
             setup: function (editor) {
 
             },
             setup: function (editor) {
 
-                editor.on('init ExecCommand change input NodeChange ObjectResized', editorChange);
+                editor.on('ExecCommand change input NodeChange ObjectResized', editorChange);
+
+                editor.on('init', () => {
+                    editorChange();
+                    // Scroll to the content if needed.
+                    const queryParams = (new URL(window.location)).searchParams;
+                    const scrollId = queryParams.get('content-id');
+                    if (scrollId) {
+                        scrollToText(scrollId);
+                    }
+                });
 
                 function editorChange() {
                     let content = editor.getContent();
                     window.$events.emit('editor-html-change', content);
                 }
 
 
                 function editorChange() {
                     let content = editor.getContent();
                     window.$events.emit('editor-html-change', content);
                 }
 
-                window.$events.listen('editor-html-update', html => {
-                    editor.setContent(html);
-                    editor.selection.select(editor.getBody(), true);
-                    editor.selection.collapse(false);
-                    editorChange(html);
-                });
-
-                window.$events.listen('editor-scroll-to-text', textId => {
-                    const element = editor.dom.get(textId)
+                function scrollToText(scrollId) {
+                    const element = editor.dom.get(scrollId)
                     if (!element) {
                         return;
                     }
                     if (!element) {
                         return;
                     }
@@ -508,6 +511,13 @@ class WysiwygEditor {
                     editor.selection.select(element, true);
                     editor.selection.collapse(false);
                     editor.focus();
                     editor.selection.select(element, true);
                     editor.selection.collapse(false);
                     editor.focus();
+                }
+
+                window.$events.listen('editor-html-update', html => {
+                    editor.setContent(html);
+                    editor.selection.select(editor.getBody(), true);
+                    editor.selection.collapse(false);
+                    editorChange(html);
                 });
 
                 registerEditorShortcuts(editor);
                 });
 
                 registerEditorShortcuts(editor);
index 3796fbf655dc21548105ab1dd30424d66701519c..020e371b03021b85f9fe41fb7966c405032c18cb 100644 (file)
@@ -43,13 +43,6 @@ function mounted() {
     window.$events.listen('editor-markdown-change', markdown => {
         this.editorMarkdown = markdown;
     });
     window.$events.listen('editor-markdown-change', markdown => {
         this.editorMarkdown = markdown;
     });
-
-    const scrollToText = window.location.hash ? window.location.hash.substr(1) : '';
-    if (scrollToText) {
-        setTimeout(() => {
-            window.$events.emit('editor-scroll-to-text', scrollToText);
-        }, 1000)
-    }
 }
 
 let data = {
 }
 
 let data = {