Slate
ExamplesGitHubSlack
v0.47
v0.47
  • Introduction
  • Walkthroughs
    • Installing Slate
    • Adding Event Handlers
    • Defining Custom Block Nodes
    • Applying Custom Formatting
    • Using Plugins
    • Saving to a Database
    • Saving and Loading HTML Content
  • Guides
    • Commands & Queries
    • Data Model
    • Plugins
    • Rendering
    • Schemas
  • General
    • Plugins
    • Resources
    • Contributing
    • Changelog
    • FAQ
    • Glossary
  • Slate Core
    • Block
    • Commands
    • Data
    • Decoration
    • Document
    • Editor
    • Inline
    • Mark
    • Node
    • Operation
    • Plugins
    • Point
    • Range
    • Schema
    • Selection
    • Text
    • Utils
    • Value
  • Slate React
    • Editor
    • Plugins
    • Rendering
    • Utils
  • Other Packages
    • slate-html-serializer
    • slate-hyperscript
    • slate-plain-serializer
    • slate-prop-types
Powered by GitBook
On this page
  • Current Selection Commands
  • addMark
  • delete
  • insertBlock
  • deleteBackward
  • deleteForward
  • insertFragment
  • insertInline
  • insertText
  • setBlocks
  • setInlines
  • splitBlock
  • splitInline
  • removeMark
  • replaceMark
  • toggleMark
  • unwrapBlock
  • unwrapInline
  • wrapBlock
  • wrapInline
  • wrapText
  • Selection Commands
  • blur
  • deselect
  • flip
  • focus
  • move{Point}Backward
  • move{Point}Forward
  • move{Point}To
  • moveTo{Point}
  • move{Point}To{Edge}OfBlock
  • move{Point}To{Edge}OfDocument
  • move{Point}To{Edge}OfInline
  • move{Point}To{Edge}OfNode
  • move{Point}To{Edge}OfText
  • move{Point}To{Edge}Of{Direction}Block
  • move{Point}To{Edge}Of{Direction}Inline
  • move{Point}To{Edge}Of{Direction}Text
  • moveToRangeOfNode
  • moveToRangeOfDocument
  • select
  • Document Range Commands
  • addMarkAtRange
  • deleteAtRange
  • deleteBackwardAtRange
  • deleteForwardAtRange
  • insertBlockAtRange
  • insertFragmentAtRange
  • insertInlineAtRange
  • insertTextAtRange
  • setBlocksAtRange
  • setInlinesAtRange
  • splitBlockAtRange
  • splitInlineAtRange
  • removeMarkAtRange
  • toggleMarkAtRange
  • unwrapBlockAtRange
  • unwrapInlineAtRange
  • wrapBlockAtRange
  • wrapInlineAtRange
  • wrapTextAtRange
  • Node Commands
  • addMarkByKey/Path
  • insertNodeByKey/Path
  • insertFragmentByKey/Path
  • insertTextByKey/Path
  • mergeNodeByKey/Path
  • moveNodeByKey/Path
  • removeMarkByKey/Path
  • removeNodeByKey/Path
  • replaceNodeByKey/Path
  • removeTextByKey/Path
  • setMarkByKey/Path
  • setNodeByKey/Path
  • splitNodeByKey/Path
  • unwrapInlineByKey/Path
  • unwrapBlockByKey/Path
  • unwrapNodeByKey/Path
  • wrapBlockByKey/Path
  • wrapInlineByKey/Path
  • wrapNodeByKey/Path
  • History Commands
  • redo
  • undo
  • snapshotSelection
  • Miscellaneous Commands
  • normalize
  • withoutNormalizing
  • withoutSaving
  • withoutMerging
  1. Slate Core

Commands

PreviousBlockNextData

Last updated 5 years ago

The core Editor ships with a bunch of built-in commands that provide common behaviors for rich text editors.

Current Selection Commands

These commands act on the document based on the current selection. They are equivalent to calling the with the current selection as the range argument, but they are there for convenience, since you often want to act with the current selection, as a user would.

addMark

addMark(mark: Mark) => Editor addMark(properties: Object) => Editor addMark(type: String) => Editor

Add a to the characters in the current selection. For convenience, you can pass a type string or properties object to implicitly create a of that type.

delete

delete() => Editor

Delete everything in the current selection.

insertBlock

insertBlock(block: Block) => Editor insertBlock(properties: Object) => Editor insertBlock(type: String) => Editor

Insert a new block at the same level as the current block, splitting the current block to make room if it is non-empty. If the selection is expanded, it will be deleted first.

deleteBackward

deleteBackward(n: Number) => Editor

deleteForward

deleteForward(n: Number) => Editor

insertFragment

insertFragment(fragment: Document) => Editor

insertInline

insertInline(inline: Inline) => Editor insertInline(properties: Object) => Editor

Insert a new inline at the current cursor position, splitting the text to make room if it is non-empty. If the selection is expanded, it will be deleted first.

insertText

insertText(text: String) => Editor

Insert a string of text at the current selection. If the selection is expanded, it will be deleted first.

setBlocks

setBlocks(properties: Object) => Editor setBlocks(type: String) => Editor

setInlines

setInlines(properties: Object) => Editor setInlines(type: String) => Editor

splitBlock

splitBlock(depth: Number) => Editor

splitInline

splitInline(depth: Number) => Editor

removeMark

removeMark(mark: Mark) => Editor removeMark(properties: Object) => Editor removeMark(type: String) => Editor

replaceMark

replaceMark(oldMark: Mark, newMark: Mark) => Editor replaceMark(oldProperties: Object, newProperties: Object) => Editor replaceMark(oldType: String, newType: String) => Editor

toggleMark

toggleMark(mark: Mark) => Editor toggleMark(properties: Object) => Editor toggleMark(type: String) => Editor

unwrapBlock

unwrapBlock(type: String) => Editor unwrapBlock(properties: Object) => Editor

unwrapInline

unwrapInline(type: String) => Editor unwrapInline(properties: Object) => Editor

wrapBlock

wrapBlock(type: String) => Editor wrapBlock(properties: Object) => Editor

wrapInline

wrapInline(type: String) => Editor wrapInline(properties: Object) => Editor

wrapText

wrapText(prefix: String, [suffix: String]) => Editor

Surround the text in the current selection with prefix and suffix strings. If the suffix is ommitted, the prefix will be used instead.

Selection Commands

These commands change the current selection, without touching the document.

blur

blur() => Editor

Blur the current selection.

deselect

deselect() => Editor

Unset the selection.

flip

flip() => Editor

Flip the selection.

focus

focus() => Editor

Focus the current selection.

move{Point}Backward

move{Point}Backward(n: Number) => Editor

Move the {Point} of the selection backward n characters. Where {Point} is either Anchor, Focus, Start or End. You can also omit {Point} to move both the anchor and focus points at the same time.

move{Point}Forward

move{Point}Forward(n: Number) => Editor

Move the {Point} of the selection forward n characters. Where {Point} is either Anchor, Focus, Start or End. You can also omit {Point} to move both the anchor and focus points at the same time.

move{Point}To

moveTo(path: List, offset: Number) => Editor moveTo(key: String, offset: Number) => Editor moveTo(offset: Number) => Editor

Move the {Point} of the selection to a new path or key and offset. Where {Point} is either Anchor, Focus, Start or End. You can also omit {Point} to move both the anchor and focus points at the same time.

moveTo{Point}

moveTo{Point}() => Editor

Collapse the current selection to one of its points. Where {Point} is either Anchor, Focus, Start or End.

move{Point}To{Edge}OfBlock

move{Point}To{Edge}OfBlock() => Editor

Move the current selection to the {Edge} of the closest block parent. Where {Edge} is either Start or End. And where {Point} is either Anchor, Focus, Start or End. You can also omit {Point} to move both the anchor and focus points at the same time.

move{Point}To{Edge}OfDocument

move{Point}To{Edge}OfDocument() => Editor

Move the current selection to the {Edge} of the document. Where {Edge} is either Start or End. And where {Point} is either Anchor, Focus, Start or End. You can also omit {Point} to move both the anchor and focus points at the same time.

move{Point}To{Edge}OfInline

move{Point}To{Edge}OfInline() => Editor

Move the current selection to the {Edge} of the closest inline parent. Where {Edge} is either Start or End. And where {Point} is either Anchor, Focus, Start or End. You can also omit {Point} to move both the anchor and focus points at the same time.

move{Point}To{Edge}OfNode

move{Point}To{Edge}OfNode(node: Node) => Editor

Move the current selection to the {Edge} of a node. Where {Edge} is either Start or End. And where {Point} is either Anchor, Focus, Start or End. You can also omit {Point} to move both the anchor and focus points at the same time.

move{Point}To{Edge}OfText

move{Point}To{Edge}OfText() => Editor

Move the current selection to the {Edge} of the current text node. Where {Edge} is either Start or End. And where {Point} is either Anchor, Focus, Start or End. You can also omit {Point} to move both the anchor and focus points at the same time.

move{Point}To{Edge}Of{Direction}Block

move{Point}To{Edge}Of{Direction}Block() => Editor

Move the current selection to the {Edge} of the closest block parent. Where {Edge} is either Start or End. And where {Point} is either Anchor, Focus, Start or End. You can also omit {Point} to move both the anchor and focus points at the same time.

move{Point}To{Edge}Of{Direction}Inline

move{Point}To{Edge}Of{Direction}Inline() => Editor

Move the current selection to the {Edge} of the closest inline parent. Where {Edge} is either Start or End. And where {Point} is either Anchor, Focus, Start or End. You can also omit {Point} to move both the anchor and focus points at the same time.

move{Point}To{Edge}Of{Direction}Text

move{Point}To{Edge}Of{Direction}Text() => Editor

Move the current selection to the {Edge} of the current text node. Where {Edge} is either Start or End. And where {Point} is either Anchor, Focus, Start or End. You can also omit {Point} to move both the anchor and focus points at the same time.

moveToRangeOfNode

moveToRangeOfNode(node: Node) => Editor

Move the current selection's anchor point to the start of a node and its focus point to the end of the node.

moveToRangeOfDocument

moveToRangeOfDocument() => Editor

Move the current selection's anchor point to the start of the document and its focus point to the end of the document, selecting everything.

select

select(properties: Range || Object) => Editor

Document Range Commands

addMarkAtRange

addMarkAtRange(range: Range, mark: Mark) => Editor addMarkAtRange(range: Range, properties: Object) => Editor addMarkAtRange(range: Range, type: String) => Editor

deleteAtRange

deleteAtRange(range: Range, ) => Editor

Delete everything in a range.

deleteBackwardAtRange

deleteBackwardAtRange(range: Range, n: Number) => Editor

deleteForwardAtRange

deleteForwardAtRange(range: Range, n: Number) => Editor

insertBlockAtRange

insertBlockAtRange(range: Range, block: Block) => Editor insertBlockAtRange(range: Range, properties: Object) => Editor insertBlockAtRange(range: Range, type: String) => Editor

Insert a new block at the same level as the leaf block at a range, splitting the current block to make room if it is non-empty. If the selection is expanded, it will be deleted first.

insertFragmentAtRange

insertFragmentAtRange(range: Range, fragment: Document) => Editor

insertInlineAtRange

insertInlineAtRange(range: Range, inline: Inline) => Editor insertInlineAtRange(range: Range, properties: Object) => Editor

Insert a new inline at a range, splitting the text to make room if it is non-empty. If the selection is expanded, it will be deleted first.

insertTextAtRange

insertTextAtRange(range: Range, text: String) => Editor

Insert a string of text at a range. If the selection is expanded, it will be deleted first.

setBlocksAtRange

setBlocksAtRange(range: Range, properties: Object) => Editor setBlocks(range: Range, type: String) => Editor

setInlinesAtRange

setInlinesAtRange(range: Range, properties: Object) => Editor setInlines(range: Range, type: String) => Editor

splitBlockAtRange

splitBlockAtRange(range: Range, depth: Number) => Editor

splitInlineAtRange

splitInlineAtRange(range: Range, depth: Number) => Editor

removeMarkAtRange

removeMarkAtRange(range: Range, mark: Mark) => Editor removeMarkAtRange(range: Range, properties: Object) => Editor removeMarkAtRange(range: Range, type: String) => Editor

toggleMarkAtRange

toggleMarkAtRange(range: Range, mark: Mark) => Editor toggleMarkAtRange(range: Range, properties: Object) => Editor toggleMarkAtRange(range: Range, type: String) => Editor

unwrapBlockAtRange

unwrapBlockAtRange(range: Range, properties: Object) => Editor unwrapBlockAtRange(range: Range, type: String) => Editor

unwrapInlineAtRange

unwrapInlineAtRange(range: Range, properties: Object) => Editor unwrapInlineAtRange(range: Range, type: String) => Editor

wrapBlockAtRange

wrapBlockAtRange(range: Range, properties: Object) => Editor wrapBlockAtRange(range: Range, type: String) => Editor

wrapInlineAtRange

wrapInlineAtRange(range: Range, properties: Object) => Editor wrapInlineAtRange(range: Range, type: String) => Editor

wrapTextAtRange

wrapTextAtRange(range: Range, prefix: String, [suffix: String]) => Editor

Surround the text in a range with prefix and suffix strings. If the suffix is ommitted, the prefix will be used instead.

Node Commands

These commands are lower-level, and act on a specific node by its key or path. They're often used in your custom components because you'll have access to props.node.

addMarkByKey/Path

addMarkByKey(key: String, offset: Number, length: Number, mark: Mark) => Editor addMarkByPath(path: List, offset: Number, length: Number, mark: Mark) => Editor

insertNodeByKey/Path

insertNodeByKey(key: String, index: Number, node: Node) => Editor insertNodeByPath(path: List, index: Number, node: Node) => Editor

insertFragmentByKey/Path

insertFragmentByKey(key: String, index: Number, fragment: Fragment) => Transform insertFragmentByPath(path: list, index: Number, fragment: Fragment) => Transform

insertTextByKey/Path

insertTextByKey(key: String, offset: Number, text: String, [marks: Set]) => Editor insertTextByPath(path: List, offset: Number, text: String, [marks: Set]) => Editor

mergeNodeByKey/Path

mergeNodeByKey(key: String) => Editor mergeNodeByPath(path: List) => Editor

moveNodeByKey/Path

moveNodeByKey(key: String, newKey: String, newIndex: Number) => Editor moveNodeByPath(path: List, newKey: String, newIndex: Number) => Editor

removeMarkByKey/Path

removeMarkByKey(key: String, offset: Number, length: Number, mark: Mark) => Editor removeMarkByPath(path: List, offset: Number, length: Number, mark: Mark) => Editor

removeNodeByKey/Path

removeNodeByKey(key: String) => Editor removeNodeByPath(path: List) => Editor

replaceNodeByKey/Path

replaceNodeByKey(key: String, node: Node) => Editor replaceNodeByPath(path: List, node: Node) => Editor

removeTextByKey/Path

removeTextByKey(key: String, offset: Number, length: Number) => Editor removeTextByPath(path: List, offset: Number, length: Number) => Editor

setMarkByKey/Path

setMarkByKey(key: String, offset: Number, length: Number, properties: Object, newProperties: Object) => Editor setMarkByPath(path: List, offset: Number, length: Number, properties: Object, newProperties: Object) => Editor

setNodeByKey/Path

setNodeByKey(key: String, properties: Object) => Editor setNodeByKey(key: String, type: String) => Editor setNodeByPath(path: List, properties: Object) => Editor setNodeByPath(path: List, type: String) => Editor

splitNodeByKey/Path

splitNodeByKey(key: String, offset: Number) => Editor splitNodeByPath(path: List, offset: Number) => Editor

Split a node by its key or path at an offset.

unwrapInlineByKey/Path

unwrapInlineByKey(key: String, properties: Object) => Editor unwrapInlineByKey(key: String, type: String) => Editor unwrapInlineByPath(path: List, properties: Object) => Editor unwrapInlineByPath(path: List, type: String) => Editor

unwrapBlockByKey/Path

unwrapBlockByKey(key: String, properties: Object) => Editor unwrapBlockByKey(key: String, type: String) => Editor unwrapBlockByPath(path: List, properties: Object) => Editor unwrapBlockByPath(path: List, type: String) => Editor

unwrapNodeByKey/Path

unwrapNodeByKey(key: String) => Editor unwrapNodeByPath(path: List) => Editor

Unwrap a single node from its parent. If the node is surrounded with siblings, its parent will be split. If the node is the only child, the parent is removed, and simply replaced by the node itself. Cannot unwrap a root node.

wrapBlockByKey/Path

wrapBlockByKey(key: String, properties: Object) => Editor wrapBlockByKey(key: String, type: String) => Editor wrapBlockByPath(path: List, properties: Object) => Editor wrapBlockByPath(path: List, type: String) => Editor

wrapInlineByKey/Path

wrapInlineByKey(key: String, properties: Object) => Editor wrapInlineByKey(key: String, type: String) => Editor wrapInlineByPath(path: List, properties: Object) => Editor wrapInlineByPath(path: List, type: String) => Editor

wrapNodeByKey/Path

wrapNodeByKey(key: String, parent: Node) => Editor wrapNodeByPath(path: List, parent: Node) => Editor

History Commands

These commands use the history to undo/redo previously made changes.

redo

redo() => Editor

Move forward one step in the history.

undo

undo() => Editor

Move backward one step in the history.

snapshotSelection

snapshotSelection() => Editor

Snapshot value.selection for undo purposes, useful with delete operations like editor.removeNodeByKey(focusBlock.key).undo().

Miscellaneous Commands

normalize

normalize() => Editor

This method normalizes the document with the value's schema. This should run automatically-you should not need to call this method unless you have manually disabled normalization (and you should rarely, if ever, need to manually disable normalization). The vast majority of changes, whether by the user or invoked programmatically, will run normalize by default to ensure the document is always in adherence to its schema.

🤖 If you must use this method, use it sparingly and strategically. Calling this method can be very expensive as it will run normalization on all of the nodes in your document.

withoutNormalizing

withoutNormalizing(fn: Function) => Editor

This method calls the provided function with the current instance of the Change object as the first argument. Normalization does not occur while the fuction is executing, and is instead deferred to be be run immediately after it completes.

This method can be used to allow a sequence of change operations that should not be interrupted by normalization. For example:

editor.withoutNormalizing(() => {
  node.nodes.filter(n => n.object != 'block').forEach(child => {
    editor.removeNodeByKey(child.key)
  })
})

withoutSaving

withoutSaving(fn: Function) => Editor

By default all new operations are saved to the editor's history. If you have changes that you don't want to show up in the history when the user presses cmd+z, you can use withoutSaving to skip those changes.

editor.withoutSaving(() => {
  editor.setDecorations(decorations)
})

However, be sure you know what you are doing because this will create changes that cannot be undone by the user, and might result in confusing behaviors.

withoutMerging

withoutMerging(fn: Function) => Editor

Usually, all of the operations in a Change are grouped into a single save point in the editor's history. However, sometimes you may want more control over this, to be able to create distinct save points in a single change. To do that, you can use the withoutMerging helper.

Delete backward n characters at the current cursor. If the selection is expanded, this method is equivalent to a regular . n defaults to 1.

Delete forward n characters at the current cursor. If the selection is expanded, this method is equivalent to a regular . n defaults to 1.

Insert a at the current selection. If the selection is expanded, it will be deleted first.

Set the properties of the in the current selection. For convenience, you can pass a type string to set the blocks' type only.

Set the properties of the nodes in the current selection. For convenience, you can pass a type string to set the inline nodes' type only.

Split the in the current selection by depth levels. If the selection is expanded, it will be deleted first. depth defaults to 1.

Split the node in the current selection by depth levels. If the selection is expanded, it will be deleted first. depth defaults to Infinity.

Remove a from the characters in the current selection. For convenience, you can pass a type string or properties object to implicitly create a of that type.

Replace a in the characters in the current selection. For convenience, you can pass a type string or properties object to implicitly create a of that type.

Add or remove a from the characters in the current selection, depending on it already exists on any or not. For convenience, you can pass a type string or properties object to implicitly create a of that type.

Unwrap all nodes in the current selection that match a type and/or data.

Unwrap all nodes in the current selection that match a type and/or data.

Wrap the nodes in the current selection with a new node of type, with optional data.

Wrap the nodes in the current selection with a new node of type, with optional data.

Set the current selection to a range with merged properties. The properties can either be a object or a plain JavaScript object of selection properties.

These commands act on a specific of the document.

Add a to the characters in a range. For convenience, you can pass a type string or properties object to implicitly create a of that type.

Delete backward n characters at a range. If the range is expanded, this method is equivalent to a regular . n defaults to 1.

Delete forward n characters at a range. If the range is expanded, this method is equivalent to a regular . n defaults to 1.

Insert a at a range. If the selection is expanded, it will be deleted first.

Set the properties of the in a range. For convenience, you can pass a type string to set the blocks' type only.

Set the properties of the nodes in a range. For convenience, you can pass a type string to set the inline nodes' type only.

Split the in a range by depth levels. If the selection is expanded, it will be deleted first. depth defaults to 1.

Split the node in a range by depth levels. If the selection is expanded, it will be deleted first. depth defaults to Infinity.

Remove a from the characters in a range. For convenience, you can pass a type string or properties object to implicitly create a of that type.

Add or remove a from the characters in a range, depending on whether any of them already have the mark. For convenience, you can pass a type string or properties object to implicitly create a of that type.

Unwrap all nodes in a range that match properties. For convenience, you can pass a type string or properties object.

Unwrap all nodes in a range that match properties. For convenience, you can pass a type string or properties object.

Wrap the nodes in a range with a new node with properties. For convenience, you can pass a type string or properties object.

Wrap the nodes in a range with a new node with properties. For convenience, you can pass a type string or properties object.

Add a mark to length characters starting at an offset in a by its key or path.

Insert a node at index inside a parent by its key or path.

Insert a at index inside a parent by its key or path.

Insert text at an offset in a by its key with optional marks.

Merge a by its key or path with its previous sibling.

Move a by its key or path to a new parent node with its newKey and at a newIndex.

Remove a mark from length characters starting at an offset in a by its key or path.

Remove a from the document by its key or path.

Replace a in the document with a new by its key or path.

Remove length characters of text starting at an offset in a by its key or path.

Set a dictionary of newProperties on a on a by its key or path.

Set a dictionary of properties on a by its key or path. For convenience, you can pass a type string or properties object.

Unwrap all inner content of an node by its key or path that match properties. For convenience, you can pass a type string or properties object.

Unwrap all inner content of a node by its key or path that match properties. For convenience, you can pass a type string or properties object.

Wrap the given node in a node that match properties. For convenience, you can pass a type string or properties object.

Wrap the given node in a node that match properties. For convenience, you can pass a type string or properties object.

Wrap the node with the specified key with the parent . This will clear all children of the parent.

fragment
Blocks
Inlines
Block
Inline
Mark
Mark
Mark
Mark
Mark
Mark
Block
Inline
Block
Block
Inline
Inline
Range
Range
Mark
Mark
fragment
Blocks
Inlines
Block
Inline
Mark
Mark
Mark
Mark
Block
Inline
Block
Block
Inline
Inline
Node
Node
Fragment
Node
Text Node
Node
Node
Node
Node
Node
Node
Node
Mark
Node
Node
Inline
Block
Block
Inline
Node
Mark
Mark
Document Range Commands
delete()
delete()
delete()
delete()