Skip to content

Commit

Permalink
Revert "[#66628] fix scrolling on click in large documents"
Browse files Browse the repository at this point in the history
This reverts commit 9b8fdd3abe7d09b48c006ac5c7117fad9ddbde83.
  • Loading branch information
Trzcin authored and MaciejWas committed Oct 7, 2024
1 parent 65664ff commit cef96ee
Showing 1 changed file with 9 additions and 29 deletions.
38 changes: 9 additions & 29 deletions src/extensions/syncDualPane.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,30 +67,6 @@ export function handlePreviewClickToScroll(/** @type {{ target: HTMLElement }} *
if (!id) return;

const lineNumber = getLineById(lineMap.current, id);
const line = window.myst_editor.main_editor.state.doc.line(lineNumber);
const visible = window.myst_editor.main_editor.visibleRanges[0];
if (line.to < visible.to && line.from > visible.from) {
scrollEditor(lineNumber, elem, preview, () => {
window.myst_editor.main_editor.dispatch({
selection: EditorSelection.create([EditorSelection.range(line.to, line.to)]),
});
window.myst_editor.main_editor.focus();
});
} else {
scrollEditor(lineNumber, elem, preview, () => {
scrollEditor(lineNumber, elem, preview, () => {
scrollEditor(lineNumber, elem, preview, () => {
window.myst_editor.main_editor.dispatch({
selection: EditorSelection.create([EditorSelection.range(line.to, line.to)]),
});
window.myst_editor.main_editor.focus();
});
});
});
}
}

function scrollEditor(lineNumber, elem, preview, scrollEnd = () => {}) {
const line = window.myst_editor.main_editor.state.doc.line(lineNumber);
const lineBlock = window.myst_editor.main_editor.lineBlockAt(line.from);
const targetRect = elem.getBoundingClientRect();
Expand All @@ -101,18 +77,22 @@ function scrollEditor(lineNumber, elem, preview, scrollEnd = () => {}) {
const top = lineBlock.top - editorScrollOffset + previewRect.top + previewTopPadding;
const direction = Math.sign(editor.scrollTop - top);
const canScroll =
!(direction === 1 && editor.scrollTop === 0) &&
!(direction === -1 && editor.scrollTop + editor.clientHeight >= editor.scrollHeight) &&
editor.scrollTop != top;
!(direction === 1 && editor.scrollTop === 0) && !(direction === -1 && editor.scrollTop + editor.clientHeight >= editor.scrollHeight);
editor.scrollTo({
top,
behavior: "smooth",
});

function setCursor() {
window.myst_editor.main_editor.dispatch({
selection: EditorSelection.create([EditorSelection.range(line.to, line.to)]),
});
window.myst_editor.main_editor.focus();
}
if (canScroll) {
editor.addEventListener("scrollend", scrollEnd, { once: true });
editor.addEventListener("scrollend", setCursor, { once: true });
} else {
scrollEnd();
setCursor();
}
}

Expand Down

0 comments on commit cef96ee

Please sign in to comment.