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

71 lines
1.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Events
## toc
## Introduction
The editor fires a few different events that you can hook into. There are two ways to register event listeners:
## Option 1: Right-away
You can define your event listeners on a new editor instance right-away:
```js
const editor = new Editor({
onInit: () => {
// The editor is ready.
},
onUpdate: () => {
// The content has changed.
},
onFocus: () => {
// The editor is focused.
},
onBlur: () => {
// The editor isnt focused anymore.
},
onTransaction: ({ transaction }) => {
// The editor state has changed.
},
})
```
## Option 2: Later
Or you can register your event listeners on a running editor instance:
```js
editor.on('init', () => {
// The editor is ready.
}
editor.on('update', () => {
// The content has changed.
}
editor.on('focus', () => {
// The editor is focused.
}
editor.on('blur', () => {
// The editor isnt focused anymore.
}
editor.on('transaction', ({ transaction }) => {
// The editor state has changed.
}
```
### Unbind event listeners
If you need to unbind those event listeners at some point, you should register your event listeners with `.on()` and unbind them with `.off()` then.
```js
const onUpdate = () => {
// The content has changed.
}
// Bind …
editor.on('update', onUpdate)
// … and unbind.
editor.off('update', onUpdate)
```