Note: Slate recently released version 0.50. This document is not applicable to that or any later version
A quick reference for common actions in a SlateJS Editor.
You may want to use this when someone presses "Enter" after a block like a heading:
editor.splitBlock().setBlocks("paragraph")
To make a series of commands without being interrupted by normalization:
// normalization is run after the delegate returns
editor.withoutNormalizing(() => {
editor.splitBlock().setBlocks({ type: "paragraph", data: {}})
}
editor.moveToRangeOfDocument()
// Save selection before focusing on something else
const selection = editor.value.selection
// Restore editor selection later on
editor.select(selection)
// also has start, end, anchor
const { key, offset, path } = editor.value.selection.focus
const type = "paragraph"
const isInType = editor.value.blocks.some(block => block.type === type)
const previous = editor.value.document.getPreviousSibling(editor.value.focusText.key)
const isAfter = previous.type === type && editor.value.focus.offset === 0
const data = { alignment: "left" }
editor.setBlocks({data})
const data = { alignment: "left" }
editor.setNodeByKey(node.key, { data: node.data.merge(data)})
// `key` == the node that you want to find the parent for
const document = editor.value.document
document.getParent(key)
const parent = editor.value.document.getParent(node.key)
// See Immutable JS List for other search functions
const index = parent.nodes.indexOf(node);
onChange = (change) => {
const opTypes = change.operations.map(operation => operation.type)
}
node.text === ""
editor.value.startBlock.text === ""
import { Value } from "slate"
import initialValue from "./json-value.json"
// This value can only be used in one `Editor` component.
const value1 = Value.fromJSON(initialValue)
// If we want to use the same initial value, we create a new one:
const value2 = Value.fromJSON(initialValue)
<Editor value={value1}>
<Editor value={value2}>