merge fix/editor-reveal-remeasure into dev(#837 后续:宿主用 MOVED 的方式 park/还原 tab body,移动节点不改变 border box,所以只看 box 变化的 ResizeObserver 永远不触发——一个在隐藏宿主里被打开的编辑器会把视口量成 0 并一直留着空白的虚拟视口(行号槽还在画,正文空)。改为每次「显现」都重新测量:IntersectionObserver 报显现本身、rAF 兜住与挂载同帧的那次显现,另接 visibilitychange / window resize;全部可缺省且逐个 dispose)

This commit is contained in:
Menghuan1918 committed 2026-10-08 21:58:34 +08:00
commit d241faf8ea
2 files changed
+99 -7

No files matched your search

+35 -7
View File
@@ -344,9 +344,14 @@ export function TextEditor(props: FileViewerProps) {
// Native sidebar tabs stay mounted while another tab is active. If a file
// is opened while that host is hidden, CodeMirror can measure its viewport at
// zero and retain an empty virtualized viewport after the tab is revealed.
// Re-measure both on mount and whenever the host's box changes so returning
// to a parked editor always repaints its document without remounting it.
// zero and retain an empty virtualized viewport after the tab is revealed —
// the gutter still draws its line numbers while the document body stays
// blank. Re-measure on every reveal, not just on a box-size change: the
// host's `retainTab` parks a body by MOVING it into a hidden seat and back,
// and moving a node never changes its border box, so a ResizeObserver alone
// never fires for that path. An IntersectionObserver reports the reveal
// itself; the rAF follow-up covers a reveal landing in the mount's own frame,
// where CodeMirror's first measure may still have read a zero box.
useEffect(() => {
const host = hostRef.current
const view = viewRef.current
@@ -355,10 +360,33 @@ export function TextEditor(props: FileViewerProps) {
if (host.isConnected && host.offsetWidth > 0 && host.offsetHeight > 0) view.requestMeasure()
}
measure()
if (typeof ResizeObserver === 'undefined') return
const observer = new ResizeObserver(measure)
observer.observe(host)
return () => { observer.disconnect() }
const frame = typeof requestAnimationFrame === 'function'
? requestAnimationFrame(() => { measure() })
: undefined
const disposers: (() => void)[] = []
if (typeof ResizeObserver !== 'undefined') {
const resize = new ResizeObserver(measure)
resize.observe(host)
disposers.push(() => { resize.disconnect() })
}
if (typeof IntersectionObserver !== 'undefined') {
const intersection = new IntersectionObserver((entries) => {
if (entries.some(entry => entry.isIntersecting)) measure()
})
intersection.observe(host)
disposers.push(() => { intersection.disconnect() })
}
const onVisibility = (): void => {
if (document.visibilityState !== 'hidden') measure()
}
document.addEventListener('visibilitychange', onVisibility)
window.addEventListener('resize', measure)
return () => {
if (frame !== undefined) cancelAnimationFrame(frame)
for (const dispose of disposers) dispose()
document.removeEventListener('visibilitychange', onVisibility)
window.removeEventListener('resize', measure)
}
}, [content, path])
// Scheme flip: re-theme in place (the compartment holds only the
+64
View File
@@ -20,7 +20,9 @@ setupReactAct()
const CTX = {} as Context
const instances: FakeResizeObserver[] = []
const intersectionInstances: FakeIntersectionObserver[] = []
const OriginalResizeObserver = globalThis.ResizeObserver
const OriginalIntersectionObserver = globalThis.IntersectionObserver
class FakeResizeObserver {
readonly targets: Element[] = []
@@ -44,6 +46,33 @@ class FakeResizeObserver {
}
}
/** The reveal signal the retainTab path produces: a MOVED body, same box. */
class FakeIntersectionObserver {
readonly targets: Element[] = []
disconnected = false
constructor(private readonly callback: IntersectionObserverCallback) {
intersectionInstances.push(this)
}
observe(target: Element): void {
this.targets.push(target)
}
unobserve(target: Element): void {
const index = this.targets.indexOf(target)
if (index >= 0) this.targets.splice(index, 1)
}
disconnect(): void {
this.disconnected = true
this.targets.length = 0
}
takeRecords(): IntersectionObserverEntry[] {
return []
}
trigger(isIntersecting: boolean): void {
const entry = { isIntersecting, target: this.targets[0] } as unknown as IntersectionObserverEntry
this.callback([entry], this as unknown as IntersectionObserver)
}
}
function props(store: ReturnType<typeof createSidebarStore>): FileViewerProps {
return {
ctx: CTX,
@@ -63,7 +92,9 @@ describe('TextEditor visibility recovery', () => {
beforeEach(() => {
instances.length = 0
intersectionInstances.length = 0
globalThis.ResizeObserver = FakeResizeObserver as unknown as typeof ResizeObserver
globalThis.IntersectionObserver = FakeIntersectionObserver as unknown as typeof IntersectionObserver
})
afterEach(() => {
@@ -72,11 +103,17 @@ describe('TextEditor visibility recovery', () => {
root = undefined
container = undefined
instances.length = 0
intersectionInstances.length = 0
if (OriginalResizeObserver === undefined) {
Reflect.deleteProperty(globalThis, 'ResizeObserver')
} else {
globalThis.ResizeObserver = OriginalResizeObserver
}
if (OriginalIntersectionObserver === undefined) {
Reflect.deleteProperty(globalThis, 'IntersectionObserver')
} else {
globalThis.IntersectionObserver = OriginalIntersectionObserver
}
vi.restoreAllMocks()
})
@@ -102,6 +139,33 @@ describe('TextEditor visibility recovery', () => {
expect(requestMeasure).toHaveBeenCalled()
})
it('requests a measure when the host is moved back into view (no box change)', () => {
// The host's retainTab path parks a tab body by MOVING it into a hidden
// seat: the border box never changes, so a ResizeObserver cannot see the
// reveal — the IntersectionObserver must.
const store = createSidebarStore()
container = document.createElement('div')
document.body.append(container)
root = createRoot(container)
act(() => { root!.render(createElement(TextEditor, props(store))) })
const editorHost = container.querySelector('[class*="editorCm"]') as HTMLDivElement
Object.defineProperties(editorHost, {
offsetWidth: { configurable: true, value: 480 },
offsetHeight: { configurable: true, value: 640 },
})
const intersection = intersectionInstances.find(candidate => candidate.targets.includes(editorHost))
expect(intersection).toBeDefined()
const requestMeasure = vi.spyOn(EditorView.prototype, 'requestMeasure')
intersection!.trigger(true)
expect(requestMeasure).toHaveBeenCalled()
// A non-intersecting report must NOT spend a measure (every scroll event in
// the pane would otherwise queue one).
requestMeasure.mockClear()
intersection!.trigger(false)
expect(requestMeasure).not.toHaveBeenCalled()
})
it('disconnects the observer when the editor unmounts', () => {
const store = createSidebarStore()
container = document.createElement('div')