tiptap/docs/src/docPages/api/commands.md

119 lines
6.1 KiB
Markdown
Raw Normal View History

2020-04-22 15:23:53 +08:00
# Commands
## toc
## Introduction
2020-10-02 22:56:02 +08:00
The editor provides a ton of commands to programmtically add or change content or alter the selection. If you want to build your own editor you definitely want to learn more about them.
## Execute a command
All available commands are accessible through an editor instance. Lets say you want to make text bold when a user clicks on a button. Thats how that would look like:
```js
2020-11-13 18:52:02 +08:00
editor.commands.bold()
2020-10-02 22:56:02 +08:00
```
While thats perfectly fine and does make the selected bold, youd likely want to change multiple commands in one run. Lets have a look at how that works.
2020-11-05 23:02:12 +08:00
### Chain commands
2020-11-12 22:31:44 +08:00
Most commands can be combined to one call. Thats shorter than separate function calls in most cases. Here is an example to make the selected text bold:
```js
2020-11-06 18:37:30 +08:00
editor.chain().bold().focus().run()
```
2020-11-12 22:31:44 +08:00
The `.chain()` is required to start a new chain and the `.run()` is needed to actually execute all the commands in between.
2020-10-02 22:56:02 +08:00
2020-11-12 22:31:44 +08:00
In the example above two different commands are executed at once. When a user clicks on a button outside of the content, the editor isnt in focus anymore. Thats why you probably want to add a `.focus()` call to most of your commands. It brings back the focus to the editor, so the user can continue to type.
2020-10-02 22:56:02 +08:00
All chained commands are kind of queued up. They are combined to one single transaction. That means, the content is only updated once, also the `update` event is only triggered once.
2020-11-05 23:02:12 +08:00
### Dry run for commands
Sometimes, you dont want to actually run the commands, but only know if it would be possible to run commands, for example to show or hide buttons in a menu. Thats what we added `.can()` for. Everything coming after this method will be executed, without applying the changes to the document:
2020-11-05 21:39:28 +08:00
```js
2020-11-05 23:02:12 +08:00
editor.can().bold()
2020-11-05 21:39:28 +08:00
```
2020-11-05 23:02:12 +08:00
And you can use it together with `.chain()`, too. Here is an example which checks if its possible to apply all the commands:
```js
editor.can().chain().bold().italic().run()
```
Both calls would return `true` if its possible to apply the commands, and `false` in case its not.
### Try commands
If you want to run a list of commands, but want only the first successful command to be applied, you can do this with the `.try()` method. This method runs one command after the other and stops at the first which returns `true`.
For example, the backspace key tries to undo an input rule first. If that was successful, it stops there. If no input rule has been applied and thus cant be reverted, it runs the next command and deletes the selection, if there is one. Here is the simplified example:
2020-11-05 21:39:28 +08:00
```js
editor.try(({ commands }) => [
2020-11-05 23:02:12 +08:00
() => commands.undoInputRule(),
() => commands.deleteSelection(),
// …
2020-11-05 21:39:28 +08:00
])
```
2020-11-05 23:02:12 +08:00
Inside of commands you can do the same thing like that:
```js
commands.try([
() => commands.undoInputRule(),
() => commands.deleteSelection(),
// …
])
```
2020-11-05 21:39:28 +08:00
2020-10-02 23:22:38 +08:00
## List of commands
Have a look at all of the core commands listed below. They should give you a good first impression of whats possible.
### Content
2020-11-06 04:25:03 +08:00
| Command | Description |
| --------------- | ------------------------------------------------ |
| .clearContent() | Clear the whole document. |
| .insertHTML() | Insert a string of HTML at the current position. |
| .insertText() | Insert a string of text at the current position. |
| .setContent() | Replace the whole document with new content. |
2020-10-02 23:22:38 +08:00
### Nodes & Marks
| Command | Description |
| ----------------------- | --------------------------------------------------------- |
| .clearNodes() | Normalize nodes to a simple paragraph. |
2020-11-06 20:44:37 +08:00
| .extendMarkRange() | Extends the text selection to the current mark. |
| .removeMark() | Remove a mark in the current selection. |
| .removeMarks() | Remove all marks in the current selection. |
| .removeMarks() | Remove all marks in the current selection. |
| .resetNodeAttributes() | Resets all node attributes to the default value. |
| .selectParentNode() | Select the parent node. |
| .setBlockType() | Replace a given range with a node. |
| .splitBlock() | Forks a new node from an existing node. |
| .toggleBlockType() | Toggle a node with another node. |
| .toggleMark() | Toggle a mark on and off. |
| .toggleWrap() | Wraps nodes in another node, or removes an existing wrap. |
| .updateMarkAttributes() | Update a mark with new attributes. |
| .updateNodeAttributes() | Update attributes of a node. |
2020-09-26 04:37:44 +08:00
2020-10-02 23:22:38 +08:00
### Lists
2020-11-13 22:08:30 +08:00
| Command | Description |
| ---------------- | ------------------------------------------- |
| .liftListItem() | Lift the list item into a wrapping list. |
| .sinkListItem() | Sink the list item down into an inner list. |
| .splitListItem() | Splits one list item into two list items. |
| .toggleList() | Toggle between different list types. |
| .wrapInList() | Wrap a node in a list. |
2020-10-02 23:22:38 +08:00
### Selection
| Command | Description |
| ------------------ | --------------------------------------- |
2020-11-13 22:08:30 +08:00
| .blur() | Removes focus from the editor. |
| .deleteSelection() | Delete the selection, if there is one. |
| .focus() | Focus the editor at the given position. |
2020-09-26 04:37:44 +08:00
| .scrollIntoView() | Scroll the selection into view. |
| .selectAll() | Select the whole document. |
2020-10-02 22:56:02 +08:00
2020-11-05 23:02:12 +08:00
## Add your own commands
All extensions can add additional commands (and most do), check out the specific [documentation for the provided nodes](/api/nodes), [marks](/api/marks), and [extensions](/api/extensions) to learn more about those.
Of course, you can [add your custom extensions](/guide/build-custom-extensions) with custom commands aswell.