Plate
PlateEditorsCreateTemplates
GitHub16kGitHub
DiscordDiscord
  • Plate
  • Editor API
  • Editor Transforms
  • Node
  • Element
  • Text
  • Path
  • Point
  • Range
  • Location
  • Anchor
  • Selection
  • Document Change
  • DOM API
  • React Hooks
  • Plate Core
    • Plate Components
    • Plate Editor
    • Plate Plugin
    • Editor Context
    • Plate Controller
  • Plate Utils
  • Resizable

DOM API

PreviousNext

Read and update the mounted editor view, selection, focus, and clipboard.

createEditor creates an editor with React, DOM, and clipboard host APIs installed. Use useCreateEditor to retain an editor in a React component.

Minimal usage

import { createEditor } from "platejs/react";
 
const editor = createEditor();
 
// Once <EditorRoot editor={editor}> shows an EditorContent, this focuses the
// view that holds the caret, or the only view.
editor.api.dom.focus();
SelectionReact Hooks

On This Page

Minimal usageViewsCheckseditor.api.react.isComposing(): booleaneditor.api.react.isFocused(): booleaneditor.api.react.isReadOnly(): booleaneditor.api.dom.isComposing(): booleaneditor.api.dom.isFocused(): booleaneditor.api.dom.isReadOnly(): booleanFocus and selectioneditor.api.dom.blur(): voideditor.api.dom.focus(options?: { retries?: number }): voideditor.api.dom.deselect(): voidTyped texteditor.api.react.subscribeTypedText(listener: (typed: TypedText) => void): () => voidDOM scopeeditor.api.dom.root(): HTMLElement | nulleditor.api.dom.editable(root?: RootKey): HTMLElement | nulleditor.api.dom.scroll(): HTMLElement | nulleditor.api.dom.visibleRect(element: Element): { top: number; right: number; bottom: number; left: number; width: number; height: number } | nullDOM translationeditor.api.dom.findKey(node: Descendant): Keyeditor.api.dom.resolvePath(node: Node): Path | nulleditor.api.dom.assertPath(node: Node): Patheditor.api.dom.hasDOMNode(target: DOMNode, options?: { editable?: boolean }): booleaneditor.api.dom.hasEditableTarget(target: EventTarget | null): target is DOMNodeeditor.api.dom.hasSelectableTarget(target: EventTarget | null): booleaneditor.api.dom.hasTarget(target: EventTarget | null): target is DOMNodeeditor.api.dom.assertDOMNode(node: Node): HTMLElementeditor.api.dom.resolveDOMNode(nodeOrKey: Node | NodeKey): HTMLElement | nulleditor.api.dom.assertDOMPoint(point: Point): DOMPointeditor.api.dom.resolveDOMPoint(point: Point): DOMPoint | nulleditor.api.dom.assertDOMRange(range: Range): DOMRangeeditor.api.dom.resolveDOMRange(range: Range): DOMRange | nulleditor.api.dom.resolveRangeRect(range: Range): DOMRect | nulleditor.api.dom.textToCaret(from: Point): string | nulleditor.api.dom.resolveVisualPoint(point: Point, options: { affinity?: SelectionAssociation; direction: 'left' | 'right'; unit: 'character' | 'word' }): DOMVisualPoint | nulleditor.api.dom.scrollIntoView(target: Path | Point | Range | DOMRange, options?: ScrollIntoViewOptions): () => voideditor.api.dom.assertEventRange(event: unknown): Rangeeditor.api.dom.resolveEventRange(event: unknown): Range | nulleditor.api.dom.assertNode(domNode: DOMNode): Nodeeditor.api.dom.resolveNode(domNode: DOMNode): Node | nulleditor.api.dom.assertPoint(domPoint: DOMPoint, options: { exactMatch: boolean; searchDirection?: 'backward' | 'forward' }): Pointeditor.api.dom.resolvePoint(domPoint: DOMPoint, options: { exactMatch: boolean; searchDirection?: 'backward' | 'forward' }): Point | nulleditor.api.dom.assertRange(domRange: DOMRange | DOMStaticRange | DOMSelection, options: { exactMatch: boolean }): Rangeeditor.api.dom.resolveRange(domRange: DOMRange | DOMStaticRange | DOMSelection, options: { exactMatch: boolean }): Range | nullDOM environmenteditor.api.dom.assertDocumentOrShadowRoot(): Document | ShadowRooteditor.api.dom.resolveDocumentOrShadowRoot(): Document | ShadowRoot | nulleditor.api.dom.getWindow(): Windoweditor.api.dom.hasRange(range: Range): booleaneditor.api.dom.isTargetInsideNonReadonlyVoid(target: EventTarget | null): booleanDrag and dropeditor.api.dom.drageditor.api.dom.drag.start(event: DragEvent, options: { node: Element }): { origin: { x: number; y: number }; previews: HTMLElement[] } | nulleditor.api.dom.drag.indicate(target: DropTarget | null): voideditor.api.dom.drag.autoScroll(point: { x: number; y: number }): numbereditor.api.dom.resolveDropTarget(input: { clientX: number; clientY: number; target?: EventTarget | null; dataTransfer?: DataTransfer | null }, options?: { copy?: boolean; files?: Element; from?: Editor; nodes?: NodeKey[]; range?: Range }): { key: NodeKey; edge: 'after' | 'before'; axis: 'x' | 'y' } | { key: NodeKey; side: 'end' | 'start' } | { point: Point } | nullDataTransfereditor.api.dom.clipboard.insertData(data: DataTransfer): boolean | undefinededitor.api.dom.clipboard.insertFragmentData(data: DataTransfer): boolean | undefinededitor.api.dom.clipboard.insertTextData(data: DataTransfer): boolean | undefinededitor.api.dom.clipboard.readSlice(data: Pick<DataTransfer, 'getData' | 'types'>): ClipboardSliceReadeditor.api.dom.clipboard.writeSelection(data: Pick<DataTransfer, 'getData' | 'setData'>): voideditor.api.dom.clipboard.writeSlice(data: Pick<DataTransfer, 'getData' | 'setData'>, payload: ClipboardSliceWrite): voidResolution failuresHost utilities
Build your editor
Production-ready AI template and reusable components.
Get all-access
import { createEditor } from "platejs/react";
 
const editor = createEditor();
 
// Once <EditorRoot editor={editor}> shows an EditorContent, this focuses the
// view that holds the caret, or the only view.
editor.api.dom.focus();

Views

A view is one mounted EditorContent, so an EditorRoot that shows a header, a body and a footer holds three views. Every editor.api.dom call runs in one view, picked by the editor you call it on:

  • A view's own editor runs every call in that view, even one given another view's DOM node or event, so pass it only its own view's DOM. A lookup of the DOM of a node, point or range in a root the view does not show returns null. Inside an EditorContent, useEditor() returns that view's editor. Elsewhere inside an EditorRoot, it returns the editor of the view the user last focused or pressed, else of the first one mounted, and the editor passed to EditorRoot until one mounts.
  • An editor from useRootEditor(root) runs in the view of root it follows, as useRootEditor describes, so its DOM calls and its commands reach the same view, even after focus moves to a button. While no EditorContent in its EditorRoot shows root, it finds no EditorContent, even when one in another EditorRoot holds the caret, so editable() returns null and hasDOMNode returns false. resolveNode and resolvePoint still resolve a text node of root that another view renders with the default leaf.
  • The editor createEditor returns runs in a view that shows the root the call is about: its target's root, else the caret's root, else the primary document. When the caret was placed in that root from an EditorRoot, through one of its views or its useRootEditor editor, only that EditorRoot's views of the root count, even after the editor createEditor returns moves the caret within the root. Of the views that count, it takes the only one, else the focused one, also when that view sits in an iframe or a shadow root.
  • The other editors run a call given a DOM node or an event in the view that renders it, when that view belongs to the editor. resolveRange and assertRange run on the editor you call instead, so resolve a DOM range in one view with that view's own editor.
  • On the editor createEditor returns and on an editor from useRootEditor, a block or files drop that resolveDropTarget finds from coordinates alone, and drag.autoScroll, hit-test the pointer in the document of the view picked as above. They run in the editor's view found there, else in the picked view. The hit test enters shadow roots but not iframes, so a pointer over an iframe finds the view that holds the iframe, if any.

When no view is picked, a lookup of DOM, such as root(), editable() or resolveDOMNode, returns null, its assert* twin throws, focus() does nothing and drag.autoScroll returns 0. Calls that need no view still answer: resolvePath and assertPath read the model, findKey and visibleRect read what you pass, and scroll() returns a scroll container registered for the editor you call. While the EditorRoot that placed the caret shows no view of the caret's root, for example while React hides it with Activity, the editor createEditor returns picks no view of that root.

To act on one of several views, call the DOM API on that view's editor:

import { EditorContent, EditorRoot, useEditor } from "platejs/react";
 
function FocusThisView() {
  const view = useEditor();
 
  return <button onClick={() => view.api.dom.focus()}>Focus</button>;
}
 
<>
  <EditorRoot editor={editor}>
    <EditorContent />
    <FocusThisView />
  </EditorRoot>
  <EditorRoot editor={editor}>
    <EditorContent />
    <FocusThisView />
  </EditorRoot>
</>
import { EditorContent, EditorRoot, useEditor } from "platejs/react";
 
function FocusThisView() {
  const view = useEditor();
 
  return <button onClick={() => view.api.dom.focus()}>Focus</button>;
}
 
<>
  <EditorRoot editor={editor}>
    <EditorContent />
    <FocusThisView />
  </EditorRoot>
  <EditorRoot editor={editor}>
    <EditorContent />
    <FocusThisView />
  </

Checks

editor.api.react.isComposing(): boolean

Check if the user is currently composing inside the editor.

editor.api.react.isFocused(): boolean

Check if the editor is focused.

editor.api.react.isReadOnly(): boolean

Check if the editor is in read-only mode.

editor.api.dom.isComposing(): boolean

Check if the user is currently composing inside the editor from the DOM bridge.

editor.api.dom.isFocused(): boolean

Check if the DOM editor is focused.

editor.api.dom.isReadOnly(): boolean

Check if the DOM editor is in read-only mode.

Focus and selection

editor.api.dom.blur(): void

Blur the editor, and take focus from the root's nodeSelectionFocus element while it holds it. An unmounted editor only clears its focused state.

editor.api.dom.focus(options?: { retries?: number }): void

Focus the editor's view. Does nothing when no view is picked. The root's nodeSelectionFocus element takes focus while no range is shown into the root and the root's own selection is none or a node selection of its top-level blocks, including one the root kept while the caret was in another root. A range shown into the root stays shown. Inside an open update or read, such a root's editable takes focus instead.

editor.api.dom.deselect(): void

Clear the native DOM selection and the editor selection. An unmounted editor clears only the editor selection.

Typed text

editor.api.react.subscribeTypedText(listener: (typed: TypedText) => void): () => void

Call listener with the text each mounted editable types: an insertText input or IME composition commit whose one change is an insertion ending at a collapsed caret. typed.editable is the editable root element that received the input, typed.range the inserted text in the model, ending at the caret, and typed.text that text. Returns the unsubscribe function.

Paste, drop, yank, replacement text, history replay, remote edits and typing over a selection report nothing, and so does a commit whose document an earlier commit listener changes before the report. Android keyboards that paste through insertText report the pasted text. When an Android keyboard sends typed and pasted text for the same text node before one input event, that input reports nothing.

DOM scope

editor.api.dom.root(): HTMLElement | null

Return the root element of the editor's view.

editor.api.dom.editable(root?: RootKey): HTMLElement | null

Return the editable element of the editor's view, or of its view of root when given.

editor.api.dom.scroll(): HTMLElement | null

Return the editor's scroll container. That is the element registered with useEditorScrollElementRef(editor), else a fallback registration such as EditorContainer, else the editor root.

editor.api.dom.visibleRect(element: Element): { top: number; right: number; bottom: number; left: number; width: number; height: number } | null

Return the part of an element the user can see, in client coordinates: its box cut to the viewport and to every ancestor that clips it. A fixed box is cut only by the viewport, an absolute box skips static ancestors up to its positioned container, and html and body count as the viewport, measured without its scrollbars. Returns null when none of the element is visible.

DOM translation

editor.api.dom.findKey(node: Descendant): Key

Find a key for an editor node.

editor.api.dom.resolvePath(node: Node): Path | null

Resolve the current path of an editor node. Returns null when the node is not mounted in the current editor value.

editor.api.dom.assertPath(node: Node): Path

Assert the current path of an editor node.

editor.api.dom.hasDOMNode(target: DOMNode, options?: { editable?: boolean }): boolean

Check if a DOM node is within the editor.

editor.api.dom.hasEditableTarget(target: EventTarget | null): target is DOMNode

Check if the target is editable and in the editor.

editor.api.dom.hasSelectableTarget(target: EventTarget | null): boolean

Check if the target can be selected by the editor.

editor.api.dom.hasTarget(target: EventTarget | null): target is DOMNode

Check if the target is in the editor.

editor.api.dom.assertDOMNode(node: Node): HTMLElement

Assert the native DOM element for an editor node.

editor.api.dom.resolveDOMNode(nodeOrKey: Node | NodeKey): HTMLElement | null

Resolve the native DOM element for an editor node or its live NodeKey. Returns null when the node is not mounted or the key is removed or belongs to another editor.

editor.api.dom.assertDOMPoint(point: Point): DOMPoint

Assert a native DOM selection point from an editor point. Throws where resolveDOMPoint returns null.

editor.api.dom.resolveDOMPoint(point: Point): DOMPoint | null

Resolve a native DOM selection point from an editor point. Returns null when the editor point is not currently mappable. It also returns null while the page's text differs from the editor's, such as right after an update or during IME composition, so resolve again after the page paints.

editor.api.dom.assertDOMRange(range: Range): DOMRange

Assert a native DOM range from an editor range. Throws where resolveDOMRange returns null.

editor.api.dom.resolveDOMRange(range: Range): DOMRange | null

Resolve a native DOM range from an editor range. Returns null when the editor range is not currently mappable. It also returns null while the page's text differs from the editor's, such as right after an update or during IME composition, so resolve again after the page paints.

editor.api.dom.resolveRangeRect(range: Range): DOMRect | null

Resolve the bounding rect for an editor range. Returns null when the range is not currently mappable to mounted DOM.

editor.api.dom.textToCaret(from: Point): string | null

Read the DOM text from from to the native caret, including IME preedit text the editor has not received yet. Non-editable, retained and nested-editor DOM is left out. Returns null when the editor root or from is not mounted, when from is in another root, or when the caret is outside this editor or before from.

editor.api.dom.resolveVisualPoint(point: Point, options: { affinity?: SelectionAssociation; direction: 'left' | 'right'; unit: 'character' | 'word' }): DOMVisualPoint | null

Resolve one physical horizontal caret step through the mounted browser layout. The result carries both the next model point and its affinity. Returns null when the point is not mounted or the browser cannot resolve the step.

editor.api.dom.scrollIntoView(target: Path | Point | Range | DOMRange, options?: ScrollIntoViewOptions): () => void

Schedule scrolling to an editor path, point, range, or native DOM range in the mounted view. Range targets use their bounding rectangle, so caret scrolling follows the actual position inside a text leaf. The returned function cancels pending work for this request; return it from an effect to cancel scrolling when its target changes or the component unmounts.

editor.api.dom.assertEventRange(event: unknown): Range

Assert the target range from a DOM event.

editor.api.dom.resolveEventRange(event: unknown): Range | null

Resolve the target range from a DOM event. Returns null when the event target cannot be mapped into the editor.

editor.api.dom.assertNode(domNode: DOMNode): Node

Assert an editor node from a native DOM node.

editor.api.dom.resolveNode(domNode: DOMNode): Node | null

Resolve an editor node from a native DOM node. Returns null when the DOM node is not owned by the editor.

editor.api.dom.assertPoint(domPoint: DOMPoint, options: { exactMatch: boolean; searchDirection?: 'backward' | 'forward' }): Point

Assert an editor point from a DOM point.

editor.api.dom.resolvePoint(domPoint: DOMPoint, options: { exactMatch: boolean; searchDirection?: 'backward' | 'forward' }): Point | null

Resolve an editor point from a DOM point. Returns null when the DOM point is not currently mappable.

editor.api.dom.assertRange(domRange: DOMRange | DOMStaticRange | DOMSelection, options: { exactMatch: boolean }): Range

Assert an editor range from a DOM range or selection.

editor.api.dom.resolveRange(domRange: DOMRange | DOMStaticRange | DOMSelection, options: { exactMatch: boolean }): Range | null

Resolve an editor range from a DOM range or selection. Returns null when the DOM range is not currently mappable.

DOM environment

editor.api.dom.assertDocumentOrShadowRoot(): Document | ShadowRoot

Return the document or shadow root that holds the editor's view. Throws where resolveDocumentOrShadowRoot returns null.

editor.api.dom.resolveDocumentOrShadowRoot(): Document | ShadowRoot | null

Resolve the document or shadow root that holds the editor's view. Returns null when no view is picked.

editor.api.dom.getWindow(): Window

Return the window that owns the editor.

editor.api.dom.hasRange(range: Range): boolean

Check whether an editor range can currently be mapped to DOM.

editor.api.dom.isTargetInsideNonReadonlyVoid(target: EventTarget | null): boolean

Check whether a DOM event target is inside a non-read-only void element.

Drag and drop

editor.api.dom.drag

Native block dragging for drag handles and custom drivers. A block drag moves inside one document and copies between editors; every drop runs editor.api.transfer.

editor.api.dom.drag.start(event: DragEvent, options: { node: Element }): { origin: { x: number; y: number }; previews: HTMLElement[] } | null

Start a block drag from a handle's dragstart. It makes a block selection of the blocks editor.read.transfer.nodes({ node }) returns, such as the selected blocks around node or a list item's family, marks their hosts with data-editor-dragging, and returns inert previews plus the pointer's offset for event.dataTransfer.setDragImage. A read-only view starts a copy-only drag. Returns null when nothing can be dragged.

editor.api.dom.drag.indicate(target: DropTarget | null): void

Publish this view's drop indicator for a target from resolveDropTarget, or clear it. useDropIndicator reads it.

editor.api.dom.drag.autoScroll(point: { x: number; y: number }): number

Scroll one step for a pointer drag at point, in client coordinates of the window that holds the editor's view, and return the pixels scrolled, or 0. On the editor createEditor returns, it runs in the view under the point, found in the document of the view the editor picks, else in the picked view. It scrolls the innermost scroller around that view whose horizontal bounds hold the point and that can still move toward the pointer, else the window, at the speed text selection and block dragging use. Scrolling starts within 48px of the visible top or bottom edge, at most half the visible height, and gets faster nearer the edge. Above or below the visible area, it scrolls at the maximum step of 28px per call. Call it once per animation frame while a custom drag lasts, and call again on the next frame while it returns a value other than 0.

editor.api.dom.resolveDropTarget(input: { clientX: number; clientY: number; target?: EventTarget | null; dataTransfer?: DataTransfer | null }, options?: { copy?: boolean; files?: Element; from?: Editor; nodes?: NodeKey[]; range?: Range }): { key: NodeKey; edge: 'after' | 'before'; axis: 'x' | 'y' } | { key: NodeKey; side: 'end' | 'start' } | { point: Point } | null

Resolve where a drop under a pointer lands: an edge the schema, the landing redirects and the vetoes admit, or a text point for a text drag. Within 8px of a container's top or bottom edge, clamped to a quarter of its height, the drop lands beside that container when that edge is admitted; otherwise it lands beside the block under the pointer, or beside an outer block when the schema or a veto refuses that edge. An edge inside the dragged blocks, or one that would not move them, drops nothing. A move whose pointer is over or beside its own blocks also drops nothing, even within a container's band. Edges inside collapsed content are refused. A block or files drop whose pointer is beside a top-level block, as in the root's side padding, and that finds no drop target resolves again just inside that block. Without options it reads the active drag session. With no target, a text drop resolves nothing, and a block or files drop on the editor createEditor returns resolves in the view under the pointer, found in the document of the view the editor picks, else in the picked view. It reads clientX and clientY in the window that holds the picked view. A read-only editor finds no drop target unless it is a view's own editor. Pass files with the block that dropped files become to place an upload. The drop checks one such block per file the browser exposes, and one block when the browser hides them during the drag.

When a feature builds side landings, such as the Column plugin, a move inside the editor whose pointer rests in the inline-end strip of the innermost block resolves to { key, side: 'end' }. The strip is the outer 24px of the block's box, at most a quarter of its width, in the middle half of its height; a container's band wins where they overlap. A copy, a file drop or a drag from another editor never resolves to a side. Pass the target to editor.api.transfer.move as to, or narrow with 'edge' in target.

DataTransfer

editor.api.dom.clipboard.insertData(data: DataTransfer): boolean | undefined

Insert data from a DataTransfer into the editor. Returns true when Plate or a plugin inserts content, false when no format applies, and undefined when the editor refuses the update, as Suggesting does for a write it cannot record. The DOM adapter dispatches domCommands.insertData; typed plugin command interceptors can claim the payload, transform it, or return next() to continue. The accepted command spec commits through one transaction.

The default command path reads the editor fragment format, then configured DataTransfer formats, then plain text. A format that returns null, or whose slice does not fit, lets the next format try. An empty slice ends the paste with nothing inserted.

editor.api.dom.clipboard.insertFragmentData(data: DataTransfer): boolean | undefined

Insert editor fragment data from a DataTransfer. Returns true when fragment content was inserted and undefined when the editor refuses the update.

editor.api.dom.clipboard.insertTextData(data: DataTransfer): boolean | undefined

Insert plain text data from a DataTransfer. Returns true when text content was inserted and undefined when the editor refuses the update.

editor.api.dom.clipboard.readSlice(data: Pick<DataTransfer, 'getData' | 'types'>): ClipboardSliceRead

Read one exact editor slice. The result distinguishes absent data, invalid MIME or HTML data, and a valid ContentSlice.

editor.api.dom.clipboard.writeSelection(data: Pick<DataTransfer, 'getData' | 'setData'>): void

Write the current selection to a DataTransfer.

editor.api.dom.clipboard.writeSlice(data: Pick<DataTransfer, 'getData' | 'setData'>, payload: ClipboardSliceWrite): void

Write one exact ContentSlice plus optional browser MIME representations.

Plate writes plain text, HTML, and an internal editor fragment payload. The fragment payload uses application/${clipboardFormatKey} and the HTML fallback is tagged with the same key, so differently configured editors do not blindly import each other's internal JSON. Use Clipboard for copy, paste, drop, DataTransfer format ordering, and fitted slice replacement.

Resolution failures

resolve* methods return null when a model position has no current mounted DOM representation. The corresponding assert* methods throw DOMResolutionError. Import the error from platejs/dom when a boundary needs to handle it.

The document can contain hidden, external, or virtualized content. Configure content boundaries, external text, or virtualization on their owning components. A successful model lookup does not prove that its DOM is mounted.

Host utilities

platejs/dom exports isHotkey for platform-aware keyboard matching, ariaKeyShortcuts and usesAppleDOMHotkeys for a shortcut's aria-keyshortcuts value, and domCommands, dataTransferFormats, and writeDataTransferFragment for browser transfer integration. Semantic HTML, Markdown, and plain-text node mappings belong in the owning plugin's formats declaration.

Its public types describe DOM primitives (DOMNode, DOMElement, DOMText, DOMPoint, DOMRange, DOMStaticRange, DOMSelection), mounted editor services (DOMApi, DOMClipboardApi), clipboard slices, DataTransfer formats, keyboard matching, visual caret movement, and scrolling.

See Selection for the interaction between model selection and the browser, and Editor Components for mounting the view.

EditorRoot
>
</>