Added code to handle scroll for markdown.

Signed-off-by: Abijeet <abijeetpatro@gmail.com>
This commit is contained in:
Abijeet
2018-06-10 13:11:10 +05:30
parent 134a96fa32
commit b936e1f403
4 changed files with 60 additions and 19 deletions

View File

@ -18,6 +18,13 @@ class MarkdownEditor {
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() {
@ -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 ;