Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import DOMPurify from 'dompurify';
const init = () => {
let hasEdited = false;
let scrollBarSync = false;
let isScrolling = false;

const localStorageNamespace = 'com.markdownlivepreview';
const localStorageKey = 'last_state';
Expand Down Expand Up @@ -119,9 +120,9 @@ This web site is using ${"`"}markedjs/marked${"`"}.
});

editor.onDidScrollChange((e) => {
if (!scrollBarSync) {
return;
}
if (!scrollBarSync || isScrolling) return;

isScrolling = true;

const scrollTop = e.scrollTop;
const scrollHeight = e.scrollHeight;
Expand All @@ -133,8 +134,40 @@ This web site is using ${"`"}markedjs/marked${"`"}.
let previewElement = document.querySelector('#preview');
let targetY = (previewElement.scrollHeight - previewElement.clientHeight) * scrollRatio;
previewElement.scrollTo(0, targetY);

requestAnimationFrame(() => { isScrolling = false; });
});

let previewElement = document.querySelector('#preview');

previewElement.addEventListener('scroll', () => {
if (!scrollBarSync || isScrolling) return;

isScrolling = true;

const scrollTop = previewElement.scrollTop;
const scrollHeight = previewElement.scrollHeight;
const clientHeight = previewElement.clientHeight;

const maxScrollTop = scrollHeight - clientHeight;
const scrollRatio = scrollTop / maxScrollTop;

const editorScrollHeight = editor.getScrollHeight();
const editorHeight = editor.getLayoutInfo().height;
const editorMaxScroll = editorScrollHeight - editorHeight;

editor.setScrollTop(editorMaxScroll * scrollRatio);

requestAnimationFrame(() => { isScrolling = false; });
});

previewElement.addEventListener('wheel', (e) => {
if (!scrollBarSync) return;

e.preventDefault();
previewElement.scrollTop += e.deltaY;
}, { passive: false });

return editor;
};

Expand Down