Signed-off-by: Abijeet <redacted>
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);
+ }
+ // 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
}
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;
}
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);
}
},
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;
}
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);
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)
- }