Skip to content

The Editor

The Editor is the central object you'll interact with at runtime. It wraps a ProseMirror EditorView and exposes typed shortcuts for the schema, commands, nodes, and marks contributed by your extensions.

import { defineBasicExtensionfunction defineBasicExtension(): BasicExtension
Define a basic extension that includes some common functionality. You can copy this function and customize it to your needs. It's a combination of the following extension functions: - {@link defineDoc } - {@link defineText } - {@link defineParagraph } - {@link defineHeading } - {@link defineList } - {@link defineBlockquote } - {@link defineImage } - {@link defineHorizontalRule } - {@link defineHardBreak } - {@link defineTable } - {@link defineCodeBlock } - {@link defineItalic } - {@link defineBold } - {@link defineUnderline } - {@link defineStrike } - {@link defineCode } - {@link defineLink } - {@link defineBaseKeymap } - {@link defineBaseCommands } - {@link defineHistory } - {@link defineGapCursor } - {@link defineVirtualSelection } - {@link defineModClickPrevention }
@public
} from 'prosekit/basic'
import { createEditorfunction createEditor<E extends Extension>(options: EditorOptions<E>): Editor<E>
@public
, unionfunction union<const E extends readonly Extension[]>(...exts: E): Union<E> (+1 overload)
Merges multiple extensions into one. You can pass multiple extensions as arguments or a single array containing multiple extensions.
@throwsIf no extensions are provided.@example```ts function defineFancyNodes() { return union( defineFancyParagraph(), defineFancyHeading(), ) } ```@example```ts function defineFancyNodes() { return union([ defineFancyParagraph(), defineFancyHeading(), ]) } ```@public
} from 'prosekit/core'
const extensionconst extension: BasicExtension = defineBasicExtensionfunction defineBasicExtension(): BasicExtension
Define a basic extension that includes some common functionality. You can copy this function and customize it to your needs. It's a combination of the following extension functions: - {@link defineDoc } - {@link defineText } - {@link defineParagraph } - {@link defineHeading } - {@link defineList } - {@link defineBlockquote } - {@link defineImage } - {@link defineHorizontalRule } - {@link defineHardBreak } - {@link defineTable } - {@link defineCodeBlock } - {@link defineItalic } - {@link defineBold } - {@link defineUnderline } - {@link defineStrike } - {@link defineCode } - {@link defineLink } - {@link defineBaseKeymap } - {@link defineBaseCommands } - {@link defineHistory } - {@link defineGapCursor } - {@link defineVirtualSelection } - {@link defineModClickPrevention }
@public
()
const editorconst editor: Editor<BasicExtension> = createEditorcreateEditor<BasicExtension>(options: EditorOptions<BasicExtension>): Editor<BasicExtension>
@public
({ extensionEditorOptions<BasicExtension>.extension: BasicExtension
The extension to use when creating the editor.
})

createEditor accepts:

OptionTypeNotes
extensionExtensionRequired. Usually a union(...) of define* calls.
defaultContentNodeJSON | string | ElementOptional initial document. It can be a ProseMirror node JSON object, an HTML string, or a DOM element instance.
defaultSelectionSelectionJSONOptional initial selection (only used when defaultContent is set).

The editor doesn't render anything until you mount it on a DOM element.

editorconst editor: Editor<BasicExtension>.mountEditor<BasicExtension>.mount: (place: HTMLElement | null | undefined) => void | VoidFunction
Mount the editor to the given HTML element. Pass `null` or `undefined` to unmount the editor. When an element is passed, this method returns a function to unmount the editor.
(elementconst element: HTMLElement)
// later: editorconst editor: Editor<BasicExtension>.unmountEditor<BasicExtension>.unmount: () => void
Unmount the editor. This is equivalent to `mount(null)`.
()

Framework integrations call mount/unmount for you. See Frameworks.

PropertyTypeWhat it gives you
mountedbooleanWhether the editor is currently attached to the DOM.
viewEditorViewUnderlying ProseMirror view (only valid after mount).
stateEditorStateThe current ProseMirror state.
schemaSchemaThe merged schema, narrowed to the union of nodes/marks you defined.
focusedbooleanWhether the editor currently has focus.
commandsCommandActionsTyped command actions, keyed by every defined command.
nodesNodeActionsTyped node creators (also exposes .isActive(attrs?)).
marksMarkActionsTyped mark creators (also exposes .isActive(attrs?)).
if (editorconst editor: Editor<BasicExtension>.commands
Editor<BasicExtension>.commands: ToCommandAction<{
    setParagraph: [];
    setHeading: [attrs?: HeadingAttrs | undefined];
    insertHeading: [attrs?: HeadingAttrs | undefined];
    toggleHeading: [attrs?: HeadingAttrs | undefined];
    dedentList: [options?: DedentListOptions];
    indentList: [options?: IndentListOptions];
    moveList: [direction: "up" | "down"];
    splitList: [];
    toggleCollapsed: [options?: ToggleCollapsedOptions];
    ... 58 more ...;
    redo: [];
}>
All {@link CommandAction } s defined by the editor.
.toggleBoldtoggleBold: CommandAction<[]>.canExecCommandAction<[]>.canExec(): boolean
Check if the current command can be executed. Return `true` if the command can be executed, otherwise `false`.
()) {
// Toggle the "bold" mark on the current selected text. editorconst editor: Editor<BasicExtension>.commands
Editor<BasicExtension>.commands: ToCommandAction<{
    setParagraph: [];
    setHeading: [attrs?: HeadingAttrs | undefined];
    insertHeading: [attrs?: HeadingAttrs | undefined];
    toggleHeading: [attrs?: HeadingAttrs | undefined];
    dedentList: [options?: DedentListOptions];
    indentList: [options?: IndentListOptions];
    moveList: [direction: "up" | "down"];
    splitList: [];
    toggleCollapsed: [options?: ToggleCollapsedOptions];
    ... 58 more ...;
    redo: [];
}>
All {@link CommandAction } s defined by the editor.
.toggleBold
toggleBold: CommandAction
() => boolean
Execute the current command. Return `true` if the command was successfully executed, otherwise `false`.
()
} const isH1const isH1: boolean = editorconst editor: Editor<BasicExtension>.nodes
Editor<BasicExtension>.nodes: ToNodeAction<SimplifyDeeper<{
    doc: {
        readonly [x: string]: any;
    };
    text: {
        readonly [x: string]: any;
    };
    paragraph: {
        readonly [x: string]: any;
    };
    heading: {
        level: number;
    };
    list: {
        kind?: "bullet" | "ordered" | "task" | "toggle" | undefined;
        order?: number | null | undefined;
        checked?: boolean | undefined;
        collapsed?: boolean | undefined;
    };
    blockquote: {
        readonly [x: string]: any;
    };
    image: {
        src?: string | null | undefined;
        width?: number | null | undefined;
        height?: number | null | undefined;
    };
    horizontalRule: {
        readonly [x: string]: any;
    };
    hardBreak: {
        readonly [x: string]: any;
    };
    table: {
        readonly [x: string]: any;
    };
    tableRow: {
        readonly [x: string]: any;
    };
    tableCell: {
        colspan?: number | undefined;
        rowspan?: number | undefined;
        colwidth?: number[] | null | undefined;
    };
    tableHeaderCell: {
        colspan?: number | undefined;
        rowspan?: number | undefined;
        colwidth?: number[] | null | undefined;
    };
    codeBlock: {
        ...;
    };
}>>
All {@link NodeAction } s defined by the editor.
.heading
heading: NodeAction<{
    level: number;
}>
.isActive
NodeAction<{ level: number; }>.isActive: (attrs?: {
    level: number;
} | undefined) => boolean
Checks if the node is active in the current editor selection. If the optional `attrs` parameter is provided, it will check if the node is active with the given attributes.
({ levellevel: number: 1 })
const isBoldconst isBold: boolean = editorconst editor: Editor<BasicExtension>.marks
Editor<BasicExtension>.marks: ToMarkAction<SimplifyDeeper<{
    italic: {
        readonly [x: string]: any;
    };
    bold: {
        readonly [x: string]: any;
    };
    underline: {
        readonly [x: string]: any;
    };
    strike: {
        readonly [x: string]: any;
    };
    code: {
        readonly [x: string]: any;
    };
    link: {
        href: string;
        target?: string | null | undefined;
        rel?: string | null | undefined;
    };
}>>
All {@link MarkAction } s defined by the editor.
.bold
bold: MarkAction<{
    readonly [x: string]: any;
}>
.isActive
MarkAction<{ readonly [x: string]: any; }>.isActive: (attrs?: {
    readonly [x: string]: any;
} | undefined) => boolean
Checks if the mark is active in the current editor selection. If the optional `attrs` parameter is provided, it will check if the mark is active with the given attributes.
()
MethodPurpose
setContent(content, sel?)Replace the document. content is NodeJSON | string | Element.
getDocJSON()Return the current document as JSON.
getDocHTML(options?)Render the document as an HTML string.
exec(command)Run a raw ProseMirror Command. Returns true on success.
canExec(command)Test whether a raw command would succeed without running it.
use(extension)Hot-add an extension at runtime. Returns a function that removes it.
focus() / blur()Imperatively change focus.
mount(el) / unmount()Attach / detach from the DOM.

commands.<name>.canExec() is typically what you want for toolbar buttons. It returns true when the command would apply, which is the right signal for "is this button enabled?".

editor.use registers an extension after the editor has been created. Use it for features that depend on runtime state (e.g., a search panel that's only active while open).

const saveExtensionconst saveExtension: Extension<ExtensionTyping<any, any, any>>: Extensioninterface Extension<T extends ExtensionTyping<any, any, any> = ExtensionTyping<any, any, any>>
@public
= defineKeymapfunction defineKeymap(keymap: Keymap): PlainExtension
Adds a set of keybindings to the editor. Please read the [documentation](https://prosemirror.net/docs/ref/#keymap) for more details.
@public
({
'Mod-s': () => { saveDocumentfunction saveDocument(json: unknown): void(editorconst editor: Editor<BasicExtension>.getDocJSONEditor<BasicExtension>.getDocJSON: () => NodeJSON
Return a JSON object representing the editor's current document.
())
return true }, }) const disposeconst dispose: VoidFunction = editorconst editor: Editor<BasicExtension>.useEditor<BasicExtension>.use: (extension: Extension) => VoidFunction
Register an extension to the editor. Return a function to unregister the extension.
(saveExtensionconst saveExtension: Extension<ExtensionTyping<any, any, any>>)
// later, when you no longer want the binding: dispose
const dispose: VoidFunction
() => void
()