🐛 fix(vscode): 代码阅读预览记住浏览位置,编辑 / 预览互切位置对齐

This commit is contained in:
cruldra committed 2026-09-25 03:21:14 +08:00
1 parent 6897954644
commit 37306bd23e
13 files changed
+409 -37

No files matched your search

@@ -0,0 +1,40 @@
/** 预览的滚动位置按「块」记,而不是按像素:文件在标签页隐藏期间可能被改过,块比 scrollTop 对得准。 */
/** 一块在页面里的位置:它的起始行,和相对页面顶部的上下边(像素)。按页面上的先后排列。 */
export interface BlockBox {
line: number
top: number
bottom: number
}
/** 视口顶部那块的起始行 + 视口顶部在块内往下偏了多少像素;按块对不上时退回 scrollTop。 */
export interface ScrollAnchor {
line: number
offset: number
scrollTop: number
}
/** 视口顶部那块:第一个下边还在视口顶部以下的块;全都滚过去了就取最后一块。没有块时没有位置可记。 */
export function anchorAt(boxes: readonly BlockBox[], scrollTop: number): ScrollAnchor | undefined {
const box = boxes.find(b => b.bottom > scrollTop) ?? boxes.at(-1)
return box && { line: box.line, offset: scrollTop - box.top, scrollTop }
}
/** 恢复时的 scrollTop:起始行还是那一行的块还在就对齐到它,否则用原来的 scrollTop。 */
export function scrollTopFor(boxes: readonly BlockBox[], anchor: ScrollAnchor): number {
const box = boxes.find(b => b.line === anchor.line)
return box ? box.top + anchor.offset : anchor.scrollTop
}
/**
* 包含这一行的块:起始行不超过它的块里起始行最大的那块(行落在块与块之间的空行、
* 或折叠起来的子块里时,就是它之前最近的块);这一行在所有块之前时取第一块。
*/
export function blockForLine(boxes: readonly BlockBox[], line: number): BlockBox | undefined {
let found: BlockBox | undefined
for (const box of boxes) {
if (box.line <= line && (!found || box.line > found.line))
found = box
}
return found ?? boxes[0]
}