TheSunny
doc-ios
doc-ios
  • 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
    • Changes
    • Data Model
    • Plugins
    • Rendering
    • Schemas
  • General
    • Plugins
    • Resources
    • Contributing
    • Changelog
    • FAQ
  • Slate Core
    • Block
    • Change
    • Character
    • Data
    • Document
    • Inline
    • Mark
    • Node
    • Operation
    • Range
    • Schema
    • Text
    • Value
    • setKeyGenerator
    • resetKeyGenerator
  • Slate React
    • Editor
    • Plugins
    • Custom Nodes
    • Core Plugins
    • cloneFragment
    • findDOMNode
    • findDOMRange
    • findNode
    • findRange
    • getEventRange
    • getEventTransfer
    • setEventTransfer
  • Other Packages
    • slate-html-serializer
    • slate-hyperscript
    • slate-plain-serializer
    • slate-prop-types
    • slate-schema-violations
    • slate-simulator
  • Contributing
    • iOS Development Guide
Powered by GitBook
On this page
  • Properties
  • object
  • value
  • Methods
  • call
  • withoutNormalization
  • Current Value Changes
  • deleteBackward
  • deleteForward
  • delete
  • insertBlock
  • insertFragment
  • insertInline
  • insertText
  • addMark
  • setBlocks
  • setInlines
  • splitBlock
  • splitInline
  • removeMark
  • toggleMark
  • unwrapBlock
  • unwrapInline
  • wrapBlock
  • wrapInline
  • wrapText
  • Selection Changes
  • blur
  • collapseTo{Edge}
  • collapseTo{Edge}Of
  • collapseTo{Edge}Of{Direction}Block
  • collapseTo{Edge}Of{Direction}Text
  • extend
  • extendTo{Edge}Of
  • flip
  • focus
  • move
  • move{Edge}
  • moveOffsetsTo
  • moveToRangeOf
  • select
  • selectAll
  • deselect
  • Document Changes
  • deleteBackwardAtRange
  • deleteForwardAtRange
  • deleteAtRange
  • insertBlockAtRange
  • insertFragmentAtRange
  • insertInlineAtRange
  • insertTextAtRange
  • addMarkAtRange
  • setBlocksAtRange
  • setInlinesAtRange
  • splitBlockAtRange
  • splitInlineAtRange
  • removeMarkAtRange
  • toggleMarkAtRange
  • unwrapBlockAtRange
  • unwrapInlineAtRange
  • wrapBlockAtRange
  • wrapInlineAtRange
  • wrapTextAtRange
  • Node Changes
  • addMarkByKey
  • insertNodeByKey
  • insertFragmentByKey
  • insertTextByKey
  • moveNodeByKey
  • removeMarkByKey
  • removeNodeByKey
  • replaceNodeByKey
  • removeTextByKey
  • setMarkByKey
  • setNodeByKey
  • splitNodeByKey
  • unwrapInlineByKey
  • unwrapBlockByKey
  • unwrapNodeByKey
  • wrapBlockByKey
  • wrapInlineByKey
  • History Changes
  • redo
  • undo
  • snapshotSelection
  1. Slate Core

Change

PreviousBlockNextCharacter

Last updated 7 years ago

import { Change } from 'slate'

A change allows you to define a series of changes you'd like to make to the current .

All changes are performed through Change objects, so that a history of changes can be preserved for use in undo/redo operations, and to make collaborative editing possible.

Properties

object

String

A string with a value of 'change'.

value

A with the change's current operations applied. Each time you run a new change function this property will be updated.

Methods

call

call(customChange: Function, ...args) => Change

This method calls the provided customChange function with the current instance of the Change object as the first argument and passes through the remaining args.

The purpose of call is to enable custom change methods to exist and called in a chain. For example:

function addBoldMark(change) {
  change.addMark('bold_mark')
}

function insertParagraph(change) {
  change.insertBlock('paragraph_block')
}

function onSomeEvent(event, change) {
  change
    .call(insertParagraph)
    .insertText('Some text...')
    .extendToStartOfBlock()
    .call(addBoldMark)
    .collapseToEnd()
}

withoutNormalization

withoutNormalization(customChange: Function) => Change

This method calls the provided customChange function with the current instance of the Change object as the first argument. While customChange is executing, normalization is temporarily suppressed, but normalization will be executed once the customChange function completes execution.

The purpose of withoutNormalization is to allow a sequence of change operations that should not be interrupted by normalization. For example:

/**
 * Only allow block nodes in documents.
 *
 * @type {Object}
 */
validateNode(node) {
  if (node.object != 'document') return
  const invalids = node.nodes.filter(n => n.object != 'block')
  if (!invalids.size) return

  return (change) => {
    change.withoutNormalization((c) => {
      invalids.forEach((child) => {
        c.removeNodeByKey(child.key)
      })
    })
  }
}

Current Value Changes

deleteBackward

deleteBackward(n: Number) => Change

deleteForward

deleteForward(n: Number) => Change

delete

delete() => Change

Delete everything in the current selection.

insertBlock

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

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.

insertFragment

insertFragment(fragment: Document) => Change

insertInline

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

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) => Change

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

addMark

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

setBlocks

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

setInlines

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

splitBlock

splitBlock(depth: Number) => Change

splitInline

splitInline(depth: Number) => Change

removeMark

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

toggleMark

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

unwrapBlock

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

unwrapInline

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

wrapBlock

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

wrapInline

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

wrapText

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

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

Selection Changes

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

blur

blur() => Change

Blur the current selection.

collapseTo{Edge}

collapseTo{Edge}() => Change

Collapse the current selection to its {Edge}. Where {Edge} is either Anchor, Focus, Start or End.

collapseTo{Edge}Of

collapseTo{Edge}Of(node: Node) => Change

Collapse the current selection to the {Edge} of node. Where {Edge} is either Start or End.

collapseTo{Edge}Of{Direction}Block

collapseTo{Edge}Of{Direction}Block() => Change

collapseTo{Edge}Of{Direction}Text

collapseTo{Edge}Of{Direction}Text() => Change

extend

extend(n: Number) => Change

Extend the current selection's points by n characters. n can be positive or negative to indicate direction.

extendTo{Edge}Of

extendTo{Edge}Of(node: Node) => Change

Extend the current selection to the {Edge} of a node. Where {Edge} is either Start or End.

flip

flip() => Change

Flip the selection.

focus

focus() => Change

Focus the current selection.

move

move(n: Number) => Change

Move the current selection's offsets by n.

move{Edge}

move{Edge}(n: Number) => Change

Move the current selection's {Edge} offset by n. {Edge} can be one of Start, End.

moveOffsetsTo

moveOffsetsTo(anchorOffset: Number, focusOffset: Number) => Change

Move the current selection's offsets to a new anchorOffset and focusOffset.

moveToRangeOf

moveToRangeOf(node: Node) => Change

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

select

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

selectAll

selectAll() => Change

Select the entire document and focus the selection.

deselect

deselect() => Change

Unset the selection.

Document Changes

deleteBackwardAtRange

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

deleteForwardAtRange

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

deleteAtRange

deleteAtRange(range: Range, ) => Change

Delete everything in a range.

insertBlockAtRange

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

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) => Change

insertInlineAtRange

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

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) => Change

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

addMarkAtRange

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

setBlocksAtRange

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

setInlinesAtRange

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

splitBlockAtRange

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

splitInlineAtRange

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

removeMarkAtRange

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

toggleMarkAtRange

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

unwrapBlockAtRange

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

unwrapInlineAtRange

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

wrapBlockAtRange

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

wrapInlineAtRange

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

wrapTextAtRange

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

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

Node Changes

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

addMarkByKey

addMarkByKey(key: String, offset: Number, length: Number, mark: Mark) => Change

insertNodeByKey

insertNodeByKey(key: String, index: Number, node: Node) => Change

insertFragmentByKey

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

insertTextByKey

insertTextByKey(key: String, offset: Number, text: String, [marks: Set]) => Change

moveNodeByKey

moveNodeByKey(key: String, newKey: String, newIndex: Number) => Change

removeMarkByKey

removeMarkByKey(key: String, offset: Number, length: Number, mark: Mark) => Change

removeNodeByKey

removeNodeByKey(key: String) => Change

replaceNodeByKey

replaceNodeByKey(key: String, node: Node) => Change

removeTextByKey

removeTextByKey(key: String, offset: Number, length: Number) => Change

setMarkByKey

setMarkByKey(key: String, offset: Number, length: Number, mark: Mark, properties: Object) => Change

setNodeByKey

setNodeByKey(key: String, properties: Object) => Change setNodeByKey(key: String, type: String) => Change

splitNodeByKey

splitNodeByKey(key: String, offset: Number) => Change

Split a node by its key at an offset.

unwrapInlineByKey

unwrapInlineByKey(key: String, properties: Object) => Change unwrapInlineByKey(key: String, type: String) => Change

unwrapBlockByKey

unwrapBlockByKey(key: String, properties: Object) => Change unwrapBlockByKey(key: String, type: String) => Change

unwrapNodeByKey

unwrapNodeByKey(key: String) => Change

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

wrapBlockByKey(key: String, properties: Object) => Change wrapBlockByKey(key: String, type: String) => Change

wrapInlineByKey

wrapInlineByKey(key: String, properties: Object) => Change wrapInlineByKey(key: String, type: String) => Change

History Changes

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

redo

redo() => Change

Move forward one step in the history.

undo

undo() => Change

Move backward one step in the history.

snapshotSelection

snapshotSelection() => Change

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

These changes 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.

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.

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.

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.

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.

Collapse the current selection to the {Edge} of the next node in {Direction}. Where {Edge} is either Start or End and {Direction} is either Next or Previous.

Collapse the current selection to the {Edge} of the next node in {Direction}. Where {Edge} is either Start or End and {Direction} is either Next or Previous.

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 changes act on a specific of the document.

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.

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.

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.

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

Insert a at index inside a parent by its key.

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

Move a by its key 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.

Remove a from the document by its key.

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

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

Set a dictionary of properties on a on a by its key.

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

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

Unwrap all inner content of a node by its key 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.

Value
Value
fragment
mark
Mark
Blocks
Inlines
Block
Inline
mark
Mark
mark
Mark
Block
Inline
Block
Block
Inline
Inline
Block
Text
Range
Range
fragment
mark
Mark
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
mark
Node
Node
Inline
Block
Block
Inline
Document Changes
delete()
delete()
delete()
delete()