Skip to content

Commit

Permalink
[#63062] use top line to sync scroll when cursor is further below
Browse files Browse the repository at this point in the history
  • Loading branch information
Trzcin authored and MaciejWas committed Sep 18, 2024
1 parent cee9e4a commit 7b19c4e
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/extensions/syncDualPane.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ export const syncPreviewWithEditorScroll = (lineMap, preview) => {
const editorRect = view.dom.parentElement.getBoundingClientRect();
const topBlock = view.lineBlockAtHeight(editorRect.top - view.documentTop);
const topLine = view.state.doc.lineAt(topBlock.from).number + 1;
const line = Math.max(cursorLine, topLine);
const bottomBlock = view.lineBlockAtHeight(editorRect.bottom - view.documentTop);
const bottomLine = view.state.doc.lineAt(bottomBlock.from).number;
const line = cursorLine < topLine || cursorLine > bottomLine ? topLine : cursorLine;
const [matchingElem, matchingLine] = findNearestElementForLine(line, lineMap, preview.current);
if (matchingElem) {
scrollPreviewElemIntoView({
Expand Down Expand Up @@ -112,7 +114,9 @@ export function syncEditorWithPreviewScroll(/** @type {HTMLElement} */ preview,
const editorRect = view.dom.parentElement.getBoundingClientRect();
const topBlock = view.lineBlockAtHeight(editorRect.top - view.documentTop);
const topLine = view.state.doc.lineAt(topBlock.from).number + 1;
if (cursorLine >= topLine) {
const bottomBlock = view.lineBlockAtHeight(editorRect.bottom - editorRect.top + view.dom.parentElement.scrollTop);
const bottomLine = view.state.doc.lineAt(bottomBlock.from).number - 2;
if (cursorLine >= topLine && cursorLine <= bottomLine) {
// scroll to match the cursor position
const [matchingElem, matchingLine] = findNearestElementForLine(cursorLine, lineMap, preview);
if (matchingElem) {
Expand Down

0 comments on commit 7b19c4e

Please sign in to comment.