✨ feat(vscode): 代码阅读预览按住 ⌘ / Ctrl 点名字跳到定义

This commit is contained in:
cruldra committed 2026-09-25 12:54:45 +08:00
1 parent e5db1a2e65
commit 4e146eb48f
20 files changed
+870 -60

No files matched your search

@@ -1,14 +1,14 @@
import type { BlockBox } from './scrollPosition'
import type { DocumentMessage } from './types'
import { useEffect, useLayoutEffect, useRef } from 'react'
import { anchorAt, blockForLine, scrollTopFor } from './scrollPosition'
import { anchorAt, scrollTopFor } from './scrollPosition'
import { postMessage, viewState } from './vscode'
/** 滚动时最多这么久记一次位置 */
const RECORD_INTERVAL_MS = 100
/** 页面上每块(BlockCard 带 data-line)的位置,按页面上的先后。 */
function measureBlocks(): BlockBox[] {
export function measureBlocks(): BlockBox[] {
const y = window.scrollY
return Array.from(document.querySelectorAll<HTMLElement>('.cr-block[data-line]'), (el) => {
const rect = el.getBoundingClientRect()
@@ -31,8 +31,8 @@ function record(reportedLine: { current: number | undefined }): void {
/**
* 记住并恢复预览看到的位置:
* ① 滚动时把视口顶部那块记进 webview 状态,页面因标签页切走被重新加载后照着滚回去;
* ② 同时把那块的起始行报给扩展端,切回文本编辑器时把这行放到顶部;
* ③ 第一份文档带 revealLine(刚从文本编辑器切过来)时优先滚到包含这行的块。
* ② 同时把那块的起始行报给扩展端,切回文本编辑器时把这行放到顶部。
* 第一份文档带 reveal(刚从文本编辑器切过来、跳到定义)时不恢复,交给 useReveal 滚。
*/
export function useScrollPosition(doc: DocumentMessage | null): void {
const restored = useRef(false)
@@ -43,11 +43,8 @@ export function useScrollPosition(doc: DocumentMessage | null): void {
if (!doc || restored.current)
return
restored.current = true
const boxes = measureBlocks()
const saved = viewState.get().scroll
const top = doc.revealLine !== undefined
? blockForLine(boxes, doc.revealLine)?.top
: saved && scrollTopFor(boxes, saved)
const top = doc.reveal === undefined && saved ? scrollTopFor(measureBlocks(), saved) : undefined
if (top !== undefined)
window.scrollTo(0, top)
record(reportedLine)