X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/b936e1f403e0565adc9a619d8b9426dfd6b9492c..b5a2d3c1c423d81b80fd7034a33ada85863a29ad:/resources/assets/js/components/markdown-editor.js diff --git a/resources/assets/js/components/markdown-editor.js b/resources/assets/js/components/markdown-editor.js index 4e0ba83ba..9e2bb3915 100644 --- a/resources/assets/js/components/markdown-editor.js +++ b/resources/assets/js/components/markdown-editor.js @@ -395,31 +395,30 @@ class MarkdownEditor { } // Scroll to a specified text - scrollToText(searchText) {; + 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; - } - } + let lineNumber = lines.findIndex(line => { + return line && line.indexOf(searchText) !== -1; + }); - if (lineNumber !== -1) { - this.cm.scrollIntoView({ - line: lineNumber, - char: lines[lineNumber].length - }, 200); - this.cm.focus(); + if (lineNumber === -1) { + return; } + + this.cm.scrollIntoView({ + line: lineNumber, + }, 200); + this.cm.focus(); + // set the cursor location. + this.cm.setCursor({ + line: lineNumber, + char: lines[lineNumber].length + }) } }