Skip to content

Commit 005e8a2

Browse files
committed
perf(mdviewer): instant viewer→CM cursor highlight via fast path
Add _sendCursorLineToParent() that sends just the source line on selectionchange without debounce. MarkdownSync handles the new mdviewrCursorLine message by immediately updating the CM line highlight without scrolling. Eliminates the visible delay when moving cursor in the viewer.
1 parent 401235e commit 005e8a2

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

src-mdviewer/src/bridge.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,9 @@ export function initBridge() {
472472
if (!_cursorPosDirty) {
473473
_cursorPosBeforeEdit = _getCursorPosition();
474474
}
475+
// Fast path: send just the source line for instant CM highlight
476+
_sendCursorLineToParent();
477+
// Full selection sync (debounced)
475478
_sendSelectionToParent();
476479
});
477480

@@ -1065,6 +1068,19 @@ function handleHighlightSelection(data) {
10651068
}
10661069
}
10671070

1071+
// Fast path: send just the cursor's source line for instant CM highlight (no debounce)
1072+
function _sendCursorLineToParent() {
1073+
const selection = window.getSelection();
1074+
if (!selection || !selection.rangeCount) return;
1075+
const anchorNode = selection.anchorNode;
1076+
const el = anchorNode && (anchorNode.nodeType === Node.ELEMENT_NODE
1077+
? anchorNode : anchorNode.parentElement);
1078+
const sourceLine = _getSourceLineFromElement(el);
1079+
if (sourceLine != null) {
1080+
sendToParent("mdviewrCursorLine", { sourceLine });
1081+
}
1082+
}
1083+
10681084
let _selectionSendTimer = null;
10691085
function _sendSelectionToParent() {
10701086
clearTimeout(_selectionSendTimer);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,15 @@ define(function (require, exports, module) {
153153
}
154154
}
155155
break;
156+
case "mdviewrCursorLine":
157+
// Fast path: just update CM line highlight, no scroll
158+
if (_cursorSyncEnabled && data.sourceLine != null) {
159+
const cm = _getCM();
160+
if (cm) {
161+
_flashCMLine(cm, Math.max(0, data.sourceLine - 1));
162+
}
163+
}
164+
break;
156165
case "mdviewrSelectionSync":
157166
if (_cursorSyncEnabled) {
158167
_handleSelectionFromIframe(data);

0 commit comments

Comments
 (0)