Skip to content
Closed
Show file tree
Hide file tree
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
20 changes: 18 additions & 2 deletions src/scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,34 @@ export function startObserving(sourcePane: HTMLElement, targetPane: HTMLElement)
}

if ('onscrollend' in window) {
sourcePane.addEventListener('scrollend', () => syncScrollProgress(sourcePane, targetPane));
sourcePane.addEventListener('scrollend', () => {
if (!states.syncSuppressed) {
syncScrollProgress(sourcePane, targetPane);
}
});
} else {
sourcePane.addEventListener('scroll', () => {
if (states.scrollUpdater !== undefined) {
clearTimeout(states.scrollUpdater);
}

states.scrollUpdater = setTimeout(() => {
syncScrollProgress(sourcePane, targetPane);
if (!states.syncSuppressed) {
syncScrollProgress(sourcePane, targetPane);
}
}, 100);
});
}
}

export function suppressScrollSync() {
states.syncSuppressed = true;
}

export function resumeScrollSync() {
states.syncSuppressed = false;
}

export function syncScrollProgress(sourcePane: HTMLElement, targetPane: HTMLElement, animated = true) {
const { line, progress } = getScrollProgress(sourcePane);
scrollToProgress(targetPane, line, progress, animated);
Expand Down Expand Up @@ -136,6 +150,8 @@ function clampProgressValue(value: number) {

const states: {
scrollUpdater: ReturnType<typeof setTimeout> | undefined;
syncSuppressed: boolean;
} = {
scrollUpdater: undefined,
syncSuppressed: false,
};
7 changes: 6 additions & 1 deletion src/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { renderMarkdown, renderMermaid, renderKatex, handlePostRender, applyStyl
import { replaceImageURLs } from './features/image';
import { hidePreviewButtons, previewModes } from './support/settings';
import { localized } from './shared/strings';
import { syncScrollProgress } from './scroll';
import { syncScrollProgress, suppressScrollSync, resumeScrollSync } from './scroll';
import { resolveTaskToggle } from './features/task';
import { ClassNames, CacheKeys } from './shared/const';

Expand Down Expand Up @@ -377,10 +377,15 @@ function handleTaskItemToggle(event: MouseEvent) {

// Let the native toggle stand for instant feedback; just sync the source
const from = lineRange.from + toggle.offset;

// Suppress scroll-sync for this dispatch: the editor's scrollDOM can emit a
// scrollend event that would jump the preview to the editor's cursor line.
suppressScrollSync();
MarkEdit.editorView.dispatch({
changes: { from, to: from + 1, insert: toggle.replacement },
annotations: silentChange.of(true),
});
requestAnimationFrame(resumeScrollSync);
}

const states: {
Expand Down