Skip to content

Commit 6916839

Browse files
committed
fix(mdviewer): center cursor in CM when syncing position from md viewer
1 parent 15432de commit 6916839

1 file changed

Lines changed: 3 additions & 4 deletions

File tree

src/extensionsIntegrated/Phoenix-live-preview/MarkdownSync.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -451,20 +451,19 @@ define(function (require, exports, module) {
451451
return;
452452
}
453453

454-
// Set cursor to the clicked line (suppress scroll-sync echo back)
454+
// Set cursor without CM's default scroll, then center manually
455455
_syncingFromIframe = true;
456-
cm.setCursor({ line: cmLine, ch: 0 });
456+
cm.setCursor({ line: cmLine, ch: 0 }, null, { scroll: false });
457457
_syncingFromIframe = false;
458458

459-
// Scroll only if line is not already visible
459+
// Always center the cursor line in the editor
460460
const scrollInfo = cm.getScrollInfo();
461461
const lineTop = cm.charCoords({ line: cmLine, ch: 0 }, "local").top;
462462
const lineBottom = cm.charCoords({ line: cmLine, ch: 0 }, "local").bottom;
463463
const viewTop = scrollInfo.top;
464464
const viewBottom = scrollInfo.top + scrollInfo.clientHeight;
465465

466466
if (lineTop < viewTop || lineBottom > viewBottom) {
467-
// Centre the line vertically in the editor
468467
const targetScrollTop = lineTop - (scrollInfo.clientHeight / 2);
469468
cm.scrollTo(null, targetScrollTop);
470469
}

0 commit comments

Comments
 (0)