Files
Tom MoorandClaude Opus 5 eaad1a71dc React SSR editor nodes (#13113)
* 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>
2026-08-16 16:36:13 -04:00

133 lines
3.6 KiB
TypeScript

import type { PluginSimple } from "markdown-it";
import type { InputRule } from "prosemirror-inputrules";
import type { NodeType, MarkType, Schema } from "prosemirror-model";
import type { Command, Plugin, Selection } from "prosemirror-state";
import type { Editor } from "../../../app/editor";
import type { SelectionToolbarMenuDescriptor } from "../types";
export type CommandFactory = (attrs?: unknown, options?: unknown) => Command;
export type WidgetProps = {
rtl: boolean;
readOnly: boolean | undefined;
selection?: Selection;
};
export default class Extension<TOptions extends object = object> {
options: TOptions;
editor: Editor;
constructor(options: Partial<TOptions> = {}) {
this.options = {
...this.defaultOptions,
...options,
} as TOptions;
}
bindEditor(editor: Editor) {
this.editor = editor;
}
get type() {
return "extension";
}
get name() {
return "";
}
get plugins(): Plugin[] {
return [];
}
get rulePlugins(): PluginSimple[] {
return [];
}
get defaultOptions(): Partial<TOptions> {
return {};
}
/**
* Whether this extension is needed in read-only mode. When false (default), pure Extension types
* are not instantiated and their commands are blocked. Node and Mark extensions are always
* instantiated for the schema regardless of this setting.
*/
get allowInReadOnly(): boolean {
return false;
}
/**
* Whether a node's React component may be used to render it when generating static HTML on the
* server. When false, the node renders from its `toDOM` spec instead — set this when the
* component depends on browser behavior or app context unavailable outside the editor.
*/
get allowComponentInStaticHTML(): boolean {
return true;
}
get focusAfterExecution(): boolean {
return true;
}
/**
* A widget is a React component to be rendered in the editor's context, independent of any
* specific node or mark. It can be used to render things like toolbars, menus, etc. Note that
* all widgets are observed automatically, so you can use observable values.
*
* @returns A React component
*/
widget(_props: WidgetProps): React.ReactElement | undefined {
return undefined;
}
/**
* A map of ProseMirror keymap bindings. It can be used to bind keyboard shortcuts to commands.
*
* @returns An object mapping key bindings to commands
*/
keys(_options: {
type?: NodeType | MarkType;
schema: Schema;
}): Record<string, Command | CommandFactory> {
return {};
}
/**
* A map of ProseMirror input rules. It can be used to automatically replace certain patterns
* while typing.
*
* @returns An array of input rules
*/
inputRules(_options: {
type?: NodeType | MarkType;
schema: Schema;
}): InputRule[] {
return [];
}
/**
* A map of ProseMirror commands. It can be used to expose commands to the editor. If a single
* command is returned, it will be available under the extension's name.
*
* @returns An object mapping command names to command factories, or a command factory
*/
commands(_options: {
type?: NodeType | MarkType;
schema: Schema;
}): Record<string, CommandFactory> | CommandFactory | undefined {
return {};
}
/**
* Declares selection toolbar menus contributed by this extension. When
* the user selects content or clicks a node, the toolbar evaluates all
* registered menus in priority order and displays the first match.
*
* @returns an array of menu descriptors, or an empty array if this extension does not contribute menus.
*/
selectionToolbarMenus(): SelectionToolbarMenuDescriptor[] {
return [];
}
}