mirror of
https://github.com/outline/outline.git
synced 2026-11-21 21:44:30 +03:00
* Render React node views in server-side HTML export Passes nodeViews to the headless EditorView used for export so that nodes with a React component render with it, matching the editor, and falling back to toDOM when a component cannot render server-side. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * Let nodes opt out of component rendering in static HTML Replaces the hardcoded set of node names in the server nodeViews factory with an allowComponentInStaticHTML getter on Extension, so a node declares the behavior alongside its own definition rather than in a server file that does not know about it. Mention opts out: its component requires stores and a router, so it always threw and fell back to toDOM, at the cost of a React render, an exception and a log line per node. A document with 100 mentions now exports in ~12ms rather than ~25ms and emits no warnings. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * Yield one less macrotask turn when flushing React effects Only two turns are needed: the first flushes passive effects, the second fires Frame's own setTimeout(0) whose setState commits the iframe. The third turn absorbed nothing that the components produce. Each turn is roughly a millisecond of idle time held inside the render lock, on every export — including plain documents, share page SSR and notification emails — so dropping it takes a small export from ~6ms to ~4ms. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
136 lines
4.3 KiB
TypeScript
136 lines
4.3 KiB
TypeScript
import type { Node as ProsemirrorNode } from "prosemirror-model";
|
||
import type { Decoration, EditorView, NodeView } from "prosemirror-view";
|
||
import type { FunctionComponent } from "react";
|
||
import ReactDOM from "react-dom";
|
||
import {
|
||
type ServerStyleSheet,
|
||
StyleSheetManager,
|
||
ThemeProvider,
|
||
} from "styled-components";
|
||
import type { ComponentProps } from "@shared/editor/types";
|
||
import light from "@shared/styles/theme";
|
||
|
||
type ComponentViewOptions = {
|
||
/** The node the view is responsible for. */
|
||
node: ProsemirrorNode;
|
||
/** The editor view instance. */
|
||
view: EditorView;
|
||
/** A function that returns the current position of the node. */
|
||
getPos: () => number | undefined;
|
||
/** The decorations applied to the node. */
|
||
decorations: readonly Decoration[];
|
||
/** The per-export styled-components sheet collecting the component's styles. */
|
||
sheet: ServerStyleSheet;
|
||
};
|
||
|
||
/**
|
||
* Server-side ProseMirror NodeView that renders a node's React `component` –
|
||
* the same one used in the browser – into a headless jsdom document, so HTML
|
||
* export matches the in-app rendering. Rendering is synchronous under React 17;
|
||
* content-bearing nodes expose a `contentDOM` hole that ProseMirror fills,
|
||
* while leaf nodes render entirely from React.
|
||
*/
|
||
export class ComponentView implements NodeView {
|
||
/** The DOM element the node is rendered into. */
|
||
public dom: HTMLElement;
|
||
/** The element ProseMirror-managed child content is mounted within, if any. */
|
||
public contentDOM?: HTMLElement;
|
||
|
||
private node: ProsemirrorNode;
|
||
private decorations: readonly Decoration[];
|
||
private className: string;
|
||
|
||
constructor(
|
||
component: FunctionComponent<Omit<ComponentProps, "theme">>,
|
||
{ node, view, getPos, decorations, sheet }: ComponentViewOptions
|
||
) {
|
||
this.node = node;
|
||
this.decorations = decorations;
|
||
|
||
const tag = node.type.spec.inline ? "span" : "div";
|
||
this.dom = document.createElement(tag);
|
||
if (!node.isLeaf) {
|
||
this.contentDOM = document.createElement(tag);
|
||
}
|
||
|
||
this.className = `component-${node.type.name}`;
|
||
this.dom.classList.add(this.className);
|
||
this.applyDecorationClasses();
|
||
|
||
// Theme is delivered via ThemeProvider context per ReactNode's
|
||
// `Omit<ComponentProps, "theme">` contract.
|
||
const props: Omit<ComponentProps, "theme"> = {
|
||
node,
|
||
view,
|
||
isSelected: false,
|
||
isEditable: false,
|
||
getPos: () => getPos() ?? 0,
|
||
decorations: [...decorations],
|
||
contentRef: this.handleContentRef,
|
||
};
|
||
|
||
const Component = component;
|
||
ReactDOM.render(
|
||
<StyleSheetManager sheet={sheet.instance}>
|
||
<ThemeProvider theme={light}>
|
||
<Component {...props} />
|
||
</ThemeProvider>
|
||
</StyleSheetManager>,
|
||
this.dom
|
||
);
|
||
}
|
||
|
||
/**
|
||
* The document is static, so never reuse a view for a changed node; forcing
|
||
* re-creation keeps the render pure. Returning false also stops ProseMirror's
|
||
* DOMObserver from reacting to late passive-effect renders.
|
||
*/
|
||
public update(): boolean {
|
||
return false;
|
||
}
|
||
|
||
/** Ignore all mutations so late React effect renders don't disturb ProseMirror. */
|
||
public ignoreMutation(): boolean {
|
||
return true;
|
||
}
|
||
|
||
/** Unmount the React tree, clearing any timers the component scheduled. */
|
||
public destroy(): void {
|
||
ReactDOM.unmountComponentAtNode(this.dom);
|
||
}
|
||
|
||
/**
|
||
* Apply decoration classes (e.g. revision diff markers) to the DOM element,
|
||
* extracting them from inline decorations that overlap this node's position.
|
||
*/
|
||
private applyDecorationClasses(): void {
|
||
this.decorations.forEach((decoration) => {
|
||
const attrs = (
|
||
decoration as Decoration & { type?: { attrs?: { class?: string } } }
|
||
).type?.attrs;
|
||
if (attrs?.class) {
|
||
attrs.class.split(" ").forEach((className) => {
|
||
if (className) {
|
||
this.dom.classList.add(className);
|
||
}
|
||
});
|
||
}
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Ref callback marking the element ProseMirror-managed content is mounted
|
||
* within. Refs fire synchronously under React 17, so `contentDOM` is attached
|
||
* before the constructor returns.
|
||
*/
|
||
private handleContentRef = (element: HTMLElement | null) => {
|
||
if (
|
||
element &&
|
||
this.contentDOM &&
|
||
element !== this.contentDOM.parentElement
|
||
) {
|
||
element.appendChild(this.contentDOM);
|
||
}
|
||
};
|
||
}
|