1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-17 12:41:44 +02:00
This commit is contained in:
Sunny Hirai
2021-04-01 18:13:48 -07:00
45 changed files with 733 additions and 911 deletions

15
docs/api/nodes/README.md Normal file
View File

@@ -0,0 +1,15 @@
# Node Types
The `Node` union type represents all of the different types of nodes that occur in a Slate document tree.
```typescript
type Node = Editor | Element | Text
type Descendant = Element | Text
type Ancestor = Editor | Element
```
- [Node](https://github.com/ianstormtaylor/slate/tree/5b6b29d33ddcdb9f7f3601477f1ae93c7d5fe45b/docs/api/api/node.md)
- [Editor](https://github.com/ianstormtaylor/slate/tree/5b6b29d33ddcdb9f7f3601477f1ae93c7d5fe45b/docs/api/api/editor.md)
- [Element](https://github.com/ianstormtaylor/slate/tree/5b6b29d33ddcdb9f7f3601477f1ae93c7d5fe45b/docs/api/api/element.md)
- [Text](https://github.com/ianstormtaylor/slate/tree/5b6b29d33ddcdb9f7f3601477f1ae93c7d5fe45b/docs/api/api/text.md)

401
docs/api/nodes/editor.md Normal file
View File

@@ -0,0 +1,401 @@
# Editor
The `Editor` object stores all the state of a slate editor. It can be extended by plugins to add helpers and implement new behaviors.
```typescript
interface Editor {
children: Node[]
selection: Range | null
operations: Operation[]
marks: Record<string, any> | null
// Schema-specific node behaviors.
isInline: (element: Element) => boolean
isVoid: (element: Element) => boolean
normalizeNode: (entry: NodeEntry) => void
onChange: () => void
// Overrideable core actions.
addMark: (key: string, value: any) => void
apply: (operation: Operation) => void
deleteBackward: (unit: 'character' | 'word' | 'line' | 'block') => void
deleteForward: (unit: 'character' | 'word' | 'line' | 'block') => void
deleteFragment: () => void
insertBreak: () => void
insertFragment: (fragment: Node[]) => void
insertNode: (node: Node) => void
insertText: (text: string) => void
removeMark: (key: string) => void
}
```
- [Instantiation methods](editor.md#instantiation-methods)
- [Static methods](editor.md#static-methods)
- [Retrieval methods](editor.md#retrieval-methods)
- [Manipulation methods](editor.md#manipulation-methods)
- [Check methods](editor.md#check-methods)
- [Normalization methods](editor.md#normalization-methods)
- [Instance methods](editor.md#instance-methods)
- [Schema-specific methods to override](editor.md#schema-specific-methods-to-override)
- [Core actions](editor.md#core-actions)
## Instantiation methods
#### `createEditor(): Editor`
Note: This method is imported directly from Slate and is not part of the Editor object.
Creates a new, empty `Editor` object.
## Static methods
### Retrieval methods
#### `Editor.above<T extends Ancestor>(editor: Editor, options?): NodeEntry | undefined`
Get the ancestor above a location in the document.
Options: `{at?: Location, match?: NodeMatch, mode?: 'highest' | 'lowest', voids?: boolean}`
#### `Editor.after(editor: Editor, at: Location, options?): Point | undefined`
Get the point after a location.
Options: `{distance?: number, unit?: 'offset' | 'character' | 'word' | 'line' | 'block', voids?: boolean}`
#### `Editor.before(editor: Editor, at: Location, options?): Point | undefined`
Get the point before a location.
Options: `{distance?: number, unit?: 'offset' | 'character' | 'word' | 'line' | 'block', voids?: boolean}`
#### `Editor.edges(editor: Editor, at: Location): [Point, Point]`
Get the start and end points of a location.
#### `Editor.end(editor: Editor, at: Location): Point`
Get the end point of a location.
#### `Editor.first(editor: Editor, at: Location): NodeEntry`
Get the first node at a location.
#### `Editor.fragment(editor: Editor, at: Location): Descendant[]`
Get the fragment at a location.
#### `Editor.last(editor: Editor, at: Location): NodeEntry`
Get the last node at a location.
#### `Editor.leaf(editor: Editor, at: Location, options?): NodeEntry`
Get the leaf text node at a location.
Options: `{depth?: number, edge?: 'start' | 'end'}`
#### `Editor.levels<T extends Node>(editor: Editor, options?): Generator<NodeEntry, void, undefined>`
Iterate through all of the levels at a location.
Options: `{at?: Location, match?: NodeMatch, reverse?: boolean, voids?: boolean}`
#### `Editor.next<T extends Descendant>(editor: Editor, options?): NodeEntry<T> | undefined`
Get the matching node in the branch of the document after a location.
Options: `{at?: Location, match?: NodeMatch, mode?: 'all' | 'highest' | 'lowest', voids?: boolean}`
#### `Editor.node(editor: Editor, at: Location, options?): NodeEntry`
Get the node at a location.
Options: `depth?: number, edge?: 'start' | 'end'`
#### `Editor.nodes(editor: Editor, options?): Generator<NodeEntry<T>, void, undefined>`
Iterate through all of the nodes in the Editor.
Options: `{at?: Location | Span, match?: NodeMatch, mode?: 'all' | 'highest' | 'lowest', universal?: boolean, reverse?: boolean, voids?: boolean}`
#### `Editor.parent(editor: Editor, at: Location, options?): NodeEntry<Ancestor>`
Get the parent node of a location.
Options: `{depth?: number, edge?: 'start' | 'end'}`
#### `Editor.path(editor: Editor, at: Location, options?): Path`
Get the path of a location.
Options: `{depth?: number, edge?: 'start' | 'end'}`
#### `Editor.pathRef(editor: Editor, path: Path, options?): PathRef`
Create a mutable ref for a `Path` object, which will stay in sync as new operations are applied to the editor.
Options: `{affinity?: 'backward' | 'forward' | null}`
#### `Editor.pathRefs(editor: Editor): Set<PathRef>`
Get the set of currently tracked path refs of the editor.
#### `Editor.point(editor: Editor, at: Location, options?): Point`
Get the start or end point of a location.
Options: `{edge?: 'start' | 'end'}`
#### `Editor.pointRef(editor: Editor, point: Point, options?): PointRef`
Create a mutable ref for a `Point` object, which will stay in sync as new operations are applied to the editor.
Options: `{affinity?: 'backward' | 'forward' | null}`
#### `Editor.pointRefs(editor: Editor): Set<PointRef>`
Get the set of currently tracked point refs of the editor.
#### `Editor.positions(editor: Editor, options?): Generator<Point, void, undefined>`
Iterate through all of the positions in the document where a `Point` can be placed.
By default it will move forward by individual offsets at a time, but you can pass the `unit: 'character'` option to moved forward one character, word, or line at at time.
Note: By default void nodes are treated as a single point and iteration will not happen inside their content unless you pass in true for the voids option, then iteration will occur.
Options: `{at?: Location, unit?: 'offset' | 'character' | 'word' | 'line' | 'block', reverse?: boolean, voids?: boolean}`
#### `Editor.previous(editor: Editor, options?): NodeEntry<T> | undefined`
Get the matching node in the branch of the document before a location.
Options: `{at?: Location, match?: NodeMatch, mode?: 'all' | 'highest' | 'lowest', voids?: boolean}`
#### `Editor.range(editor: Editor, at: Location, to?: Location): Range`
Get a range of a location.
#### `Editor.rangeRef(editor: Editor, range: Range, options?): RangeRef`
Create a mutable ref for a `Range` object, which will stay in sync as new operations are applied to the editor.
Options: `{affinity?: 'backward' | 'forward' | 'outward' | 'inward' | null}`
#### `Editor.rangeRefs(editor: Editor): Set<RangeRef>`
Get the set of currently tracked range refs of the editor.
#### `Editor.start(editor: Editor, at: Location): Point`
Get the start point of a location.
#### `Editor.string(editor: Editor, at: Location, options?): string`
Get the text string content of a location.
Note: by default the text of void nodes is considered to be an empty string, regardless of content, unless you pass in true for the voids option
Options: : `{voids?: boolean}`
#### `Editor.void(editor: Editor, options?): NodeEntry<Element> | undefined`
Match a void node in the current branch of the editor.
Options: `{at?: Location, mode?: 'highest' | 'lowest', voids?: boolean}`
### Manipulation methods
#### `Editor.addMark(editor: Editor, key: string, value: any): void`
Add a custom property to the leaf text nodes in the current selection.
If the selection is currently collapsed, the marks will be added to the `editor.marks` property instead, and applied when text is inserted next.
#### `Editor.deleteBackward(editor: Editor, options?): void`
Delete content in the editor backward from the current selection.
Options: `{unit?: 'character' | 'word' | 'line' | 'block'}`
#### `Editor.deleteForward(editor: Editor, options?): void`
Delete content in the editor forward from the current selection.
Options: `{unit?: 'character' | 'word' | 'line' | 'block'}`
#### `Editor.deleteFragment(editor: Editor): void`
Delete the content in the current selection.
#### `Editor.insertBreak(editor: Editor): void`
Insert a block break at the current selection.
#### `Editor.insertFragment(editor: Editor, fragment: Node[]): void`
Insert a fragment at the current selection.
If the selection is currently expanded, it will be deleted first.
#### `Editor.insertNode(editor: Editor, node: Node): void`
Insert a node at the current selection.
If the selection is currently expanded, it will be deleted first.
#### `Editor.insertText(editor: Editor, text: string): void`
Insert text at the current selection.
If the selection is currently expanded, it will be deleted first.
#### `Editor.removeMark(editor: Editor, key: string): void`
Remove a custom property from all of the leaf text nodes in the current selection.
If the selection is currently collapsed, the removal will be stored on `editor.marks` and applied to the text inserted next.
#### `Editor.unhangRange(editor: Editor, range: Range, options?): Range`
Convert a range into a non-hanging one.
Options: `{voids?: boolean}`
### Check methods
#### `Editor.hasBlocks(editor: Editor, element: Element): boolean`
Check if a node has block children.
#### `Editor.hasInlines(editor: Editor, element: Element): boolean`
Check if a node has inline and text children.
#### `Editor.hasTexts(editor: Editor, element: Element): boolean`
Check if a node has text children.
#### `Editor.isBlock(editor: Editor, value: any): value is Element`
Check if a value is a block `Element` object.
#### `Editor.isEditor(value: any): value is Editor`
Check if a value is an `Editor` object.
#### `Editor.isEnd(editor: Editor, point: Point, at: Location): boolean`
Check if a point is the end point of a location.
#### `Editor.isEdge(editor: Editor, point: Point, at: Location): boolean`
Check if a point is an edge of a location.
#### `Editor.isEmpty(editor: Editor, element: Element): boolean`
Check if an element is empty, accounting for void nodes.
#### `Editor.isInline(editor: Editor, value: any): value is Element`
Check if a value is an inline `Element` object.
#### `Editor.isNormalizing(editor: Editor): boolean`
Check if the editor is currently normalizing after each operation.
#### `Editor.isStart(editor: Editor, point: Point, at: Location): boolean`
Check if a point is the start point of a location.
#### `Editor.isVoid(editor: Editor, value: any): value is Element`
Check if a value is a void `Element` object.
### Normalization methods
#### `Editor.normalize(editor: Editor, options?): void`
Normalize any dirty objects in the editor.
Options: `{force?: boolean}`
#### `Editor.withoutNormalizing(editor: Editor, fn: () => void): void`
Call a function, deferring normalization until after it completes.
## Instance methods
### Schema-specific methods to override
Replace these methods to modify the original behavior of the editor when building [Plugins](https://github.com/ianstormtaylor/slate/tree/a02787539a460fb70730085e26df13cca959fabd/concepts/07-plugins/README.md). When modifying behavior, call the original method when appropriate. For example, a plugin that marks image nodes as "void":
```javascript
const withImages = editor => {
const { isVoid } = editor
editor.isVoid = element => {
return element.type === 'image' ? true : isVoid(element)
}
return editor
}
```
#### `isInline(element: Element)`
Check if a value is an inline `Element` object.
#### `isVoid(element: Element)`
Check if a value is a void `Element` object.
#### `normalizeNode(entry: NodeEntry)`
Normalize a Node according to the schema.
#### `onChange()`
Called when there is a change in the editor.
### Core actions
#### `addMark(key: string, value: any)`
Add a custom property to the leaf text nodes in the current selection. If the selection is currently collapsed, the marks will be added to the `editor.marks` property instead, and applied when text is inserted next.
#### `removeMark(key: string)`
Remove a custom property from the leaf text nodes in the current selection.
#### `deleteBackward(options?: {unit?: 'character' | 'word' | 'line' | 'block'})`
Delete content in the editor backward from the current selection.
#### `deleteForward(options?: {unit?: 'character' | 'word' | 'line' | 'block'})`
Delete content in the editor forward from the current selection.
#### `insertFragment(fragment: Node[])`
Insert a fragment at the current selection. If the selection is currently expanded, delete it first.
#### `deleteFragment()`
Delete the content of the current selection.
#### `insertBreak()`
Insert a block break at the current selection. If the selection is currently expanded, delete it first.
#### `insertNode(node: Node)`
Insert a node at the current selection. If the selection is currently expanded, delete it first.
#### `insertText(text: string)`
Insert text at the current selection. If the selection is currently expanded, delete it first.
#### `apply(operation: Operation)`
Apply an operation in the editor.

31
docs/api/nodes/element.md Normal file
View File

@@ -0,0 +1,31 @@
# Element
`Element` objects are a type of node in a Slate document that contain other `Element` nodes or `Text` nodes. They can be either "blocks" or "inlines" depending on the Slate editor's configuration.
```typescript
interface Element {
children: Node[]
}
```
- [Static methods](element.md#static-methods)
- [Retrieval methods](element.md#retrieval-methods)
- [Check methods](element.md#check-methods)
## Static methods
### Retrieval methods
#### `Element.matches(element: Element, props: Partial<Element>): boolean`
Check if an element matches a set of `props`. Note: This checks custom properties, but it does not ensure that any children are equivalent.
### Check methods
#### `Element.isElement(value: any): value is Element`
Check if a `value` implements the `Element` interface.
#### `Element.isElementList(value: any): value is Element[]`
Check if a `value` is an array of `Element` objects.

120
docs/api/nodes/node.md Normal file
View File

@@ -0,0 +1,120 @@
# Node
- [Static methods](node.md#static-methods)
- [Retrieval methods](node.md#retrieval-methods)
- [Text methods](node.md#text-methods)
- [Check methods](node.md#check-methods)
## Static methods
### Retrieval methods
#### `Node.ancestor(root: Node, path: Path): Ancestor`
Get the node at a specific `path`, asserting that it is an ancestor node. If the specified node is not an ancestor node, throw an error.
#### `Node.ancestors(root: Node, path: Path, options?): Generator<NodeEntry<Ancestor>>`
Return a generator of all the ancestor nodes above a specific path. By default, the order is bottom-up, from lowest to highest ancestor in the tree, but you can pass the `reverse: true` option to go top-down.
Options: `{reverse?: boolean}`
#### `Node.child(root: Node, index: number): Descendant`
Get the child of a node at the specified `index`.
#### `Node.children(root: Node, path: Path, options?): Generator<NodeEntry<Descendant>>`
Iterate over the children of a node at a specific path.
Options: `{reverse?: boolean}`
#### `Node.common(root: Node, path: Path, another: Path): NodeEntry`
Get an entry for the common ancestor node of two paths.
#### `Node.descendant(root: Node, path: Path): Descendant`
Get the node at a specific path, asserting that it's a descendant node.
#### `Node.descendants(root: Node, options?): Generator<NodeEntry<Descendant>>`
Return a generator of all the descendant node entries inside a root node. Each iteration will return a `NodeEntry` tuple consisting of `[Node, Path]`.
Options: `{from?: Path, to?: Path, reverse?: boolean, pass?: (node: NodeEntry => boolean)}`
#### `Node.elements(root: Node, options?): Generator<ElementEntry>`
Return a generator of all the element nodes inside a root node. Each iteration will return an `ElementEntry` tuple consisting of `[Element, Path]`. If the root node is an element, it will be included in the iteration as well.
Options: `{from?: Path, to?: Path, reverse?: boolean, pass?: (node: NodeEntry => boolean)}`
#### `Node.first(root: Node, path: Path): NodeEntry`
Get the first node entry in a root node from a `path`.
#### `Node.fragment(root: Node, range: Range): Descendant[]`
Get the sliced fragment represented by the `range`.
#### `Node.get(root: Node, path: Path): Node`
Get the descendant node referred to by a specific `path`. If the path is an empty array, get the root node itself.
#### `Node.last(root: Node, path: Path): NodeEntry`
Get the last node entry in a root node at a specific `path`.
#### `Node.leaf(root: Node, path: Path): Text`
Get the node at a specific `path`, ensuring it's a leaf text node. If the node is not a leaf text node, throw an error.
#### `Node.levels(root: Node, path: Path, options?): Generator<NodeEntry>`
Return a generator of the nodes in a branch of the tree, from a specific `path`. By default, the order is top-down, from the lowest to the highest node in the tree, but you can pass the `reverse: true` option to go bottom-up.
Options: `{reverse?: boolean}`
#### `Node.matches(root: Node, props: Partial<Node>): boolean`
Check if a node matches a set of `props`.
#### `Node.nodes(root: Node, options?): Generator<NodeEntry>`
Return a generator of all the node entries of a root node. Each entry is returned as a `[Node, Path]` tuple, with the path referring to the node's position inside the root node.
Options: `{from?: Path, to?: Path, reverse?: boolean, pass?: (node: NodeEntry => boolean)}`
#### `Node.parent(root: Node, path: Path): Ancestor`
Get the parent of a node at a specific `path`.
### Text methods
Methods related to Text.
#### `Node.string(root: Node): string`
Get the concatenated text string of a node's content. Note that this will not include spaces or line breaks between block nodes. This is not intended as a user-facing string, but as a string for performing offset-related computations for a node.
#### `Node.texts(root: Node, options?): Generator<NodeEntry<Text>>`
Return a generator of all leaf text nodes in a root node.
Options: `{from?: Path, to?: Path, reverse?: boolean, pass?: (node: NodeEntry => boolean)}`
### Check methods
Methods used to check some attribute of a Node.
#### `Node.has(root: Node, path: Path): boolean`
Check if a descendant node exists at a specific `path`.
#### `Node.isNode(value: any): value is Node`
Check if a `value` implements the `Node` interface.
#### `Node.isNodeList(value: any): value is Node[]`
Check if a `value` is a list of `Node` objects.

37
docs/api/nodes/text.md Normal file
View File

@@ -0,0 +1,37 @@
# Text
`Text` objects represent the nodes that contain the actual text content of a Slate document along with any formatting properties. They are always leaf nodes in the document tree as they cannot contain any children.
```typescript
interface Text {
text: string
}
```
- [Static methods](text.md#static-methods)
- [Retrieval methods](text.md#retrieval-methods)
- [Check methods](text.md#check-methods)
## Static methods
### Retrieval methods
#### `Text.matches(text: Text, props: Partial<Text>): boolean`
Check if a `text` matches a set of `props`.
#### `Text.decorations(node: Text, decorations: Range[]): Text[]`
Get the leaves for a text node, given `decorations`.
### Check methods
#### `Text.equals(text: Text, another: Text, options?): boolean`
Check if two text nodes are equal.
Options: `{loose?: boolean}`
#### `Text.isText(value: any): value is Text`
Check if a `value` implements the `Text` interface.