diff --git a/src/client/TextEditor.tsx b/src/client/TextEditor.tsx index 6184810f..d8e7cc64 100644 --- a/src/client/TextEditor.tsx +++ b/src/client/TextEditor.tsx @@ -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 diff --git a/tests/text-editor-visibility.spec.tsx b/tests/text-editor-visibility.spec.tsx index 27d6c57b..cac53f0e 100644 --- a/tests/text-editor-visibility.spec.tsx +++ b/tests/text-editor-visibility.spec.tsx @@ -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): 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')