Back to Plate

Slate

content/docs/api/slate.mdx

53.0.85.5 KB
Original Source

@platejs/slate is Plate's framework-free Slate layer. It exports the editor factory, editor APIs, transforms, node/location types, DOM helpers from slate-dom, history helpers, and small query utilities.

Installation

bash
npm install @platejs/slate

Use @platejs/slate in package code that should not import React or Plate core. Use platejs in app code when the umbrella package is already available.

Quick Use

ts
import { createEditor, withHistory } from '@platejs/slate';

const editor = withHistory(
  createEditor({
    children: [{ children: [{ text: 'Hello' }], type: 'p' }],
  })
);

editor.tf.insertText(' world');

const text = editor.api.string([]);

createEditor returns an editor with api, tf, and legacy direct methods. withHistory replaces undo, redo, apply, and writeHistory with real history behavior.

Package Surface

SurfaceExportUse
Editor factorycreateEditorCreates a Slate editor and binds Plate's api and tf namespaces.
Editor typesEditor, Value, ValueOfType editor instances and document values.
Editor APIEditorApiRead/query editor state through editor.api.
Editor transformsEditorTransformsChange editor state through editor.tf.
InterfacesElementApi, NodeApi, TextApi, PathApi, PointApi, RangeApi, OperationApiTyped helpers for Slate data structures.
RefsPathRef, PointRef, RangeRefMutable location refs that track editor operations.
HistoryHistoryApi, withHistoryUndo/redo stacks and history batching flags.
DOM helpersslate-dom re-exportsDOM selection, browser, and diff helpers used by editable surfaces.
UtilitiesgetAt, match, queryEditor, queryNode, deleteMergeShared query and transform predicates.

Editor Shape

An Editor contains the Slate document state plus Plate's namespaced helpers.

FieldTypeUse
childrenValueDocument nodes.
selectionTRange | nullCurrent selection.
operationsOperation[]Operations applied since the last change flush.
marksRecord<string, any> | nullMarks applied to the next inserted text.
historyHistoryUndo and redo stacks.
metaUnknownObject & { isNormalizing?: boolean }Custom metadata and normalization state.
apiEditorApiQuery methods.
tfEditorTransformsTransform methods.
transformsEditorTransformsAlias of tf for compatibility.

createEditor also syncs legacy direct methods, so existing Slate-style calls such as editor.insertText() continue to route to the current transform implementation.

API Map

PageCovers
Editor APIQuery methods on editor.api, including locations, DOM lookup, marks, blocks, and selection predicates.
Editor TransformsMutation methods on editor.tf, including text, node, mark, selection, history, and keyboard transforms.
NodeNode, descendant, ancestor, and node-entry helpers.
ElementElement types, element props, and element guards.
TextText node types, mark objects, and text guards.
PathPath comparison, movement, ancestry, and transform helpers.
PointPoint comparison, edge checks, and point transforms.
RangeRange checks, edge helpers, intersection, inclusion, and transforms.
LocationPath, Point, Range, and Span location unions.
Location RefPathRef, PointRef, and RangeRef affinity behavior.
OperationSlate operation types, operation lists, and operation inversion.

History

ts
editor.tf.withNewBatch(() => {
  editor.tf.insertText('Title');
  editor.tf.insertBreak();
});

editor.tf.withoutSaving(() => {
  editor.tf.select([]);
});

withHistory stores undo batches in history.undos and redo batches in history.redos. Selection-only operations are not saved. Adjacent text inserts or removals on the same path merge into one batch unless a history transform sets a different batching mode.

DOM Helpers

@platejs/slate re-exports selected slate-dom helpers, including browser flags, DOM point/range types, DOM selection helpers, string diff helpers, and withDOM.

Use these exports when a package already depends on @platejs/slate; import from slate-dom directly only for code that is intentionally independent of Plate's Slate layer.

Utilities

UtilityUse
getAtNormalizes editor locations and node inputs before query/transform calls.
matchBuilds node predicates from match options.
queryEditorChecks editor state before running a transform.
queryNodeChecks a node against type, path, and predicate options.
deleteMergeShared delete/merge behavior for text deletion transforms.
assignLegacyTransforms / syncLegacyMethodsKeeps legacy direct editor methods wired to editor.tf.
  • Plate shows where @platejs/slate is re-exported from the platejs umbrella package.
  • Plate Editor covers the React/Core editor layer built on top of this package.