We embed CodeMirror in a web desktop that lays each app’s DOM out once, in one document, and a compositor presents that content on one or more outputs (other pages or windows). Input from an output is mapped back to the original elements. So the document the editor lives in is not the viewport the user is looking at: it can be hidden or throttled while an output is visible, and its window size says nothing about how much of the editor is on screen.
In this setup the view gets the following wrong, because it reads them from view.win or the owner document:
- frames:
requestMeasureand the input observer’s delayed flushes usewin.requestAnimationFrame, which may not run in a hidden layout document; - visibility:
DOMObserverand tooltips use anIntersectionObserver, which reports the editor as off screen; - size changes:
ResizeObserveronscrollDOMand on tooltips; - the visible rectangle:
visiblePixelRange,inWindow,scrollRectIntoView,windowSpace,getCoveredMarginsand thevisualViewportworkaround all clip againstinnerWidth/innerHeightand walk up tobody; - focus:
hasFocusreadsdocument.activeElement.
This differs from the request in EditorView assumes the existance of global “document”: the editor still runs in a real document, and only the sources of frames, visibility, focus and the visible rectangle change.
We run a patch that adds one optional view config field and routes those reads through it when it is set:
interface RenderingHost {
requestAnimationFrame(callback: (time: number) => void): number
cancelAnimationFrame(handle: number): void
/** The rectangle the editor is actually seen in, and the element at which scrolling into view should stop. */
viewport(): Rect & { root: Element | null }
observeVisibility(listener: (visible: boolean) => void): () => void
observeResize(target: Element, listener: () => void): () => void
hasFocus(target?: Element): boolean
observeFocus(listener: () => void): () => void
}
new EditorView({ parent, rendering: host })
Without rendering nothing changes. With it, the view:
- schedules measurement through the host;
- uses the host’s resize and visibility observers instead of creating its own;
- clips against
viewport()instead of the window, and stops scrolling ancestors atviewport().root; - skips the
visualViewportworkaround; - asks the host for focus.
The patch reaches into many parts of the view and has to be ported on each release; 6.44, for example, moved scroll margins into getCoveredMargins. Before going further I’d like to know:
- Is this a use case you’d consider supporting?
- If so, would you prefer a smaller shape? For example only a frame scheduler plus a “visible rect” callback, with the embedder calling
requestMeasureitself on resize and visibility changes. - Is there an existing extension point I’ve missed that covers part of this?
I can post the full diff against 6.44.2. The embedder is Fli OS (https://fleey.me), where the Code and Notes apps run on this patch.