Skip to content

Commit 51c7be4

Browse files
committed
fix(mdviewer): preserve scroll position on file switch in edit mode
Suppress handleScrollToLine for 500ms after file switch to prevent CM cursor activity from overriding the doc cache's restored scroll position.
1 parent 48dc069 commit 51c7be4

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

src-mdviewer/src/bridge.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ let _lastReceivedSyncId = -1;
1515
let _suppressContentChange = false;
1616
let _scrollFromCM = false;
1717
let _scrollFromViewer = false;
18+
let _suppressScrollToLine = false;
1819
let _baseURL = "";
1920
let _cursorPosBeforeEdit = null; // cursor position before current edit batch
2021
let _cursorPosDirty = false; // true after content changes, reset when emitted
@@ -657,6 +658,11 @@ function handleSwitchFile(data) {
657658

658659
_suppressContentChange = true;
659660

661+
// Suppress scroll-to-line from CM during file switch — the doc cache
662+
// restores the correct scroll position; CM cursor activity would override it.
663+
_suppressScrollToLine = true;
664+
setTimeout(() => { _suppressScrollToLine = false; }, 500);
665+
660666
// Edit mode is global for the md editor frame — preserve it across file switches
661667
const wasEditMode = getState().editMode;
662668

@@ -975,6 +981,9 @@ function handleScrollToLine(data) {
975981
const { line, fromScroll, tableCol } = data;
976982
if (line == null) return;
977983

984+
// Suppress during file switch — doc cache restores the correct scroll
985+
if (_suppressScrollToLine) return;
986+
978987
// In edit mode, ignore scroll-based sync that originated from the viewer
979988
// itself (feedback loop: viewer click → CM scroll → scroll sync back).
980989
if (fromScroll && getState().editMode && _scrollFromViewer) return;

0 commit comments

Comments
 (0)