Editor laid out in one document, shown in another: supplying frames, viewport, visibility and focus

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: requestMeasure and the input observer’s delayed flushes use win.requestAnimationFrame, which may not run in a hidden layout document;
  • visibility: DOMObserver and tooltips use an IntersectionObserver, which reports the editor as off screen;
  • size changes: ResizeObserver on scrollDOM and on tooltips;
  • the visible rectangle: visiblePixelRange, inWindow, scrollRectIntoView, windowSpace, getCoveredMargins and the visualViewport workaround all clip against innerWidth/innerHeight and walk up to body;
  • focus: hasFocus reads document.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 at viewport().root;
  • skips the visualViewport workaround;
  • 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:

  1. Is this a use case you’d consider supporting?
  2. If so, would you prefer a smaller shape? For example only a frame scheduler plus a “visible rect” callback, with the embedder calling requestMeasure itself on resize and visibility changes.
  3. 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.

This sounds like a very strange thing to do. Does the CodeMirror editor view still get the focus? How does IME work if you’re hiding the actual content the user is focusing? Why not just render a CodeMirror state in custom code directly, without having a disconnected editor view?

(post deleted by author)

I’m withdrawing the request. Your questions sent me back to our side: the layout document reported wrong frames, visibility and focus to every library in it, so the fix belonged in the embedder. It now answers requestAnimationFrame, ResizeObserver, IntersectionObserver, innerWidth/innerHeight and hasFocus for the output the editor is shown on, and @codemirror/view 6.44.2 runs unpatched.

On focus: the view keeps DOM focus in the document it is laid out in. While another output holds the keyboard, that document has no system focus and the browser stops sending it focus and blur events, so the embedder delivers them itself and reports hasFocus() as true.

On IME: each output has its own hidden textarea, placed where coordsAtPos puts the cursor. The input method composes there and the output draws the preedit. When the composition ends, the committed text goes to the inputHandler facet first, and to replaceSelection with userEvent “input.type” if no handler takes it, so the view never sees a composition event. Paste goes through clipboardInputFilter.

On rendering the state directly: every output would then need its own renderer for decorations, gutters, widgets, panels and tooltips, whereas replicating the view’s DOM leaves all extensions working as they are.

Thanks for taking a look!