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 ;

View File

@ -222,7 +222,7 @@ class PageDisplay {
}
setupEditOnHeader() {
const headingEditIcon = document.querySelector('.heading-edit-icon');
if (headingEditIcon.length === 0) {
if (headingEditIcon === null) {
// user does not have permission to edit.
return;
}
@ -239,7 +239,10 @@ class PageDisplay {
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);
}

View File

@ -483,22 +483,25 @@ class WysiwygEditor {
},
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);
}
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;
}
@ -508,6 +511,13 @@ class WysiwygEditor {
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);

View File

@ -43,13 +43,6 @@ function mounted() {
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 = {