Files
Tom Moor d7711ee96f perf: Render editor UI from observable state instead of forceUpdate (#14073)
* perf: Coalesce editor React renders into an animation frame

dispatchTransaction ran calculateDir and forceUpdate synchronously on every
transaction, adding a style read and a React render to each keystroke. These
now run in an animation frame, at most once every 50ms.

Refs #14069

* fix: Reset the pending render frame on unmount

StrictMode remounts the same instance, and a stale handle would stop all later renders.

* perf: Render editor UI from observable state instead of forceUpdate

Replace the coalesced animation frame render with an observable editorState.
The editor no longer re-renders its widget tree on every transaction; the
selection toolbar observes the state only while it is active, so updates stay
synchronous and typing with a collapsed caret renders nothing. The floating
toolbar no longer measures the selection while hidden.

Refs #14069
2026-10-17 11:11:46 -04:00

137 lines
3.8 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 } from "prosemirror-state";
import type { Editor } from "../../../app/editor";
import type { SelectionToolbarMenuDescriptor } from "../types";
export type CommandFactory = (attrs?: unknown, options?: unknown) => Command;
/** Props passed to the widget rendered by an extension. */
export type WidgetProps = {
/** Whether the editor is displayed right-to-left */
rtl: boolean;
/** Whether the editor is read-only */
readOnly: boolean | undefined;
/** Whether the editor currently holds focus */
isEditorFocused: boolean;
};
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 [];
}
}