docs: add more content about dispatching commands

This commit is contained in:
Hans Pagel
2021-01-13 11:08:37 +01:00
parent ecd0a6669a
commit 775adc7af7
2 changed files with 48 additions and 2 deletions

View File

@@ -69,6 +69,38 @@ editor
Both calls would return `true` if its possible to apply the commands, and `false` in case its not.
In order to make that work with your custom commands, dont forget to return `true` or `false`.
For some of your own commands, you probably want to work with the raw [transaction](/api/overview). To make them work with `.can()` you should check if the transaction should be dispatched. Here is how we do that within `.insertText()`:
```js
export default (value: string): Command => ({ tr, dispatch }) => {
if (dispatch) {
tr.insertText(value)
}
return true
}
```
If youre just wrapping another tiptap command, you dont need to check that, well do it for you.
```js
bold: (): Command => ({ commands }) => {
return commands.toggleMark('bold')
},
```
If youre just wrapping a ProseMirror command, youll need to pass `dispatch` anyway. Then theres also no need to check it:
```js
export default (typeOrName: string | NodeType): Command => ({ state, dispatch }) => {
const type = getNodeType(typeOrName, state.schema)
return liftListItem(type)(state, dispatch)
}
```
### 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 `.first()` method. This method runs one command after the other and stops at the first which returns `true`.