tiptap/docs/api/events.md

158 lines
3.1 KiB
Markdown
Raw Normal View History

---
tableOfContents: true
---
# Events
## Introduction
2021-06-23 03:33:05 +08:00
The editor fires a few different events that you can hook into. Lets have a look at all the available events first:
2021-06-23 03:33:05 +08:00
## List of available events
### beforeCreate
Before the view is created.
### create
The editor is ready.
### update
The content has changed.
### selectionUpdate
The selection has changed.
### transaction
The editor state has changed.
### focus
The editor is focused.
### blur
The editor isnt focused anymore.
### destroy
The editor is being destroyed.
2021-06-23 03:33:05 +08:00
## Register event listeners
There are three ways to register event listeners.
### Option 1: Configuration
2020-09-26 04:22:44 +08:00
You can define your event listeners on a new editor instance right-away:
```js
const editor = new Editor({
2021-04-21 16:27:51 +08:00
onBeforeCreate({ editor }) {
// Before the view is created.
},
2021-03-10 04:25:30 +08:00
onCreate({ editor }) {
2020-09-26 04:22:44 +08:00
// The editor is ready.
},
2021-03-10 04:25:30 +08:00
onUpdate({ editor }) {
2020-09-26 04:22:44 +08:00
// The content has changed.
},
2021-03-10 04:25:30 +08:00
onSelectionUpdate({ editor }) {
2020-11-27 21:52:19 +08:00
// The selection has changed.
},
2021-03-10 04:25:30 +08:00
onTransaction({ editor, transaction }) {
2020-11-17 22:38:46 +08:00
// The editor state has changed.
},
2021-03-10 04:25:30 +08:00
onFocus({ editor, event }) {
2020-09-26 04:22:44 +08:00
// The editor is focused.
},
2021-03-10 04:25:30 +08:00
onBlur({ editor, event }) {
2020-09-26 04:22:44 +08:00
// The editor isnt focused anymore.
},
2020-11-30 20:56:42 +08:00
onDestroy() {
2020-11-30 21:50:26 +08:00
// The editor is being destroyed.
2020-11-30 20:56:42 +08:00
},
})
```
2021-06-23 03:33:05 +08:00
### Option 2: Binding
2020-09-26 04:22:44 +08:00
Or you can register your event listeners on a running editor instance:
2021-06-23 03:33:05 +08:00
#### Bind event listeners
```js
2021-04-21 16:27:51 +08:00
editor.on('beforeCreate', ({ editor }) => {
// Before the view is created.
})
2021-04-21 16:27:51 +08:00
2021-03-10 04:25:30 +08:00
editor.on('create', ({ editor }) => {
2020-09-26 04:22:44 +08:00
// The editor is ready.
})
2021-03-10 04:25:30 +08:00
editor.on('update', ({ editor }) => {
2020-09-26 04:22:44 +08:00
// The content has changed.
})
2020-09-26 04:22:44 +08:00
2021-03-10 04:25:30 +08:00
editor.on('selectionUpdate', ({ editor }) => {
2020-11-27 21:52:19 +08:00
// The selection has changed.
})
2020-11-27 21:52:19 +08:00
2021-03-10 04:25:30 +08:00
editor.on('transaction', ({ editor, transaction }) => {
2020-11-17 22:38:46 +08:00
// The editor state has changed.
})
2020-11-17 22:38:46 +08:00
2021-03-10 04:25:30 +08:00
editor.on('focus', ({ editor, event }) => {
2020-09-26 04:22:44 +08:00
// The editor is focused.
})
2020-09-26 04:22:44 +08:00
2021-03-10 04:25:30 +08:00
editor.on('blur', ({ editor, event }) => {
2020-09-26 04:22:44 +08:00
// The editor isnt focused anymore.
})
2020-11-30 20:56:42 +08:00
editor.on('destroy', () => {
2020-11-30 21:50:26 +08:00
// The editor is being destroyed.
})
```
2020-09-23 16:17:54 +08:00
2021-06-23 03:33:05 +08:00
#### Unbind event listeners
2020-09-26 04:22:44 +08:00
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.
2020-09-26 00:02:22 +08:00
```js
2020-09-26 04:22:44 +08:00
const onUpdate = () => {
// The content has changed.
2020-09-26 00:02:22 +08:00
}
2020-09-26 04:22:44 +08:00
// Bind …
editor.on('update', onUpdate)
2020-09-26 00:02:22 +08:00
2020-09-26 04:22:44 +08:00
// … and unbind.
editor.off('update', onUpdate)
```
2020-11-30 21:50:26 +08:00
2021-06-23 03:33:05 +08:00
### Option 3: Extensions
2020-11-30 21:50:26 +08:00
Moving your event listeners to custom extensions (or nodes, or marks) is also possible. Heres how that would look like:
```js
import { Extension } from '@tiptap/core'
const CustomExtension = Extension.create({
2021-04-21 16:27:51 +08:00
onBeforeCreate({ editor }) {
// Before the view is created.
},
2021-03-10 04:25:30 +08:00
onCreate({ editor }) {
2020-11-30 21:50:26 +08:00
// The editor is ready.
},
2021-03-10 04:25:30 +08:00
onUpdate({ editor }) {
2020-11-30 21:50:26 +08:00
// The content has changed.
},
2021-03-10 04:25:30 +08:00
onSelectionUpdate({ editor }) {
2020-11-30 21:50:26 +08:00
// The selection has changed.
},
2021-03-10 04:25:30 +08:00
onTransaction({ editor, transaction }) {
2020-11-30 21:50:26 +08:00
// The editor state has changed.
},
2021-03-10 04:25:30 +08:00
onFocus({ editor, event }) {
2020-11-30 21:50:26 +08:00
// The editor is focused.
},
2021-03-10 04:25:30 +08:00
onBlur({ editor, event }) {
2020-11-30 21:50:26 +08:00
// The editor isnt focused anymore.
},
onDestroy() {
// The editor is being destroyed.
},
})
```