🐛 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,34 @@
import type { ScrollAnchor } from './scrollPosition'
/**
* 标签页切走时 VS Code 会销毁 webview、切回来重新加载,靠它恢复:哪些块停在「编辑」、
* 哪些块折叠了(块 id 是它在树里的位置,文档刷新后照旧对得上),以及滚到了哪。
*/
export interface ViewState {
editing: string[]
collapsed: string[]
scroll?: ScrollAnchor
}
export interface ViewStateStorage {
getState: () => ViewState | undefined
setState: (state: ViewState) => void
}
export interface ViewStateStore {
get: () => ViewState
/** 只改给出的那几项,其余照旧,整份写回 */
update: (patch: Partial<ViewState>) => void
}
/** 一个 webview 只有一份状态:块的状态和滚动位置由不同地方各自更新,存的时候合在一起。 */
export function createViewStateStore(storage: ViewStateStorage): ViewStateStore {
let current: ViewState = storage.getState() ?? { editing: [], collapsed: [] }
return {
get: () => current,
update: (patch) => {
current = { ...current, ...patch }
storage.setState(current)
},
}
}