2020-09-09 21:59:34 +08:00
|
|
|
|
# Events
|
|
|
|
|
|
2020-10-28 22:25:06 +08:00
|
|
|
|
## toc
|
2020-09-27 16:29:01 +08:00
|
|
|
|
|
2020-10-01 18:26:20 +08:00
|
|
|
|
## Introduction
|
2020-11-30 21:50:26 +08:00
|
|
|
|
The editor fires a few different events that you can hook into. There are three ways to register event listeners:
|
2020-10-01 18:26:20 +08:00
|
|
|
|
|
2020-11-30 21:50:26 +08:00
|
|
|
|
## Option 1: Configuration
|
2020-09-26 04:22:44 +08:00
|
|
|
|
You can define your event listeners on a new editor instance right-away:
|
2020-09-09 21:59:34 +08:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
const editor = new Editor({
|
2020-11-30 20:56:42 +08:00
|
|
|
|
onCreate() {
|
2020-09-26 04:22:44 +08:00
|
|
|
|
// The editor is ready.
|
2020-09-09 21:59:34 +08:00
|
|
|
|
},
|
2020-11-17 22:38:46 +08:00
|
|
|
|
onUpdate() {
|
2020-09-26 04:22:44 +08:00
|
|
|
|
// The content has changed.
|
|
|
|
|
},
|
2020-11-27 21:52:19 +08:00
|
|
|
|
onSelection() {
|
|
|
|
|
// The selection has changed.
|
|
|
|
|
},
|
2020-11-17 22:38:46 +08:00
|
|
|
|
onTransaction({ transaction }) {
|
|
|
|
|
// The editor state has changed.
|
|
|
|
|
},
|
|
|
|
|
onFocus({ event }) {
|
2020-09-26 04:22:44 +08:00
|
|
|
|
// The editor is focused.
|
|
|
|
|
},
|
2020-11-17 22:38:46 +08:00
|
|
|
|
onBlur({ event }) {
|
2020-09-26 04:22:44 +08:00
|
|
|
|
// The editor isn’t 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
|
|
|
|
},
|
2020-09-09 21:59:34 +08:00
|
|
|
|
})
|
|
|
|
|
```
|
|
|
|
|
|
2020-11-30 21:50:26 +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:
|
2020-09-09 21:59:34 +08:00
|
|
|
|
|
2020-11-06 00:01:25 +08:00
|
|
|
|
### Bind event listeners
|
2020-09-09 21:59:34 +08:00
|
|
|
|
```js
|
2020-11-30 20:56:42 +08:00
|
|
|
|
editor.on('create', () => {
|
2020-09-26 04:22:44 +08:00
|
|
|
|
// The editor is ready.
|
|
|
|
|
}
|
2020-09-09 21:59:34 +08:00
|
|
|
|
|
2020-09-26 00:02:22 +08:00
|
|
|
|
editor.on('update', () => {
|
2020-09-26 04:22:44 +08:00
|
|
|
|
// The content has changed.
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-27 21:52:19 +08:00
|
|
|
|
editor.on('selection', () => {
|
|
|
|
|
// The selection has changed.
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-17 22:38:46 +08:00
|
|
|
|
editor.on('transaction', ({ transaction }) => {
|
|
|
|
|
// The editor state has changed.
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-27 21:52:19 +08:00
|
|
|
|
editor.on('focus', ({ event }) => {
|
2020-09-26 04:22:44 +08:00
|
|
|
|
// The editor is focused.
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-27 21:52:19 +08:00
|
|
|
|
editor.on('blur', ({ event }) => {
|
2020-09-26 04:22:44 +08:00
|
|
|
|
// The editor isn’t 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-11-30 20:56:42 +08:00
|
|
|
|
}
|
2020-09-09 21:59:34 +08:00
|
|
|
|
```
|
2020-09-23 16:17:54 +08:00
|
|
|
|
|
2020-09-26 00:02:22 +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
|
|
|
|
|
|
|
|
|
## Option 3: Extensions
|
|
|
|
|
Moving your event listeners to custom extensions (or nodes, or marks) is also possible. Here’s how that would look like:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
import { Extension } from '@tiptap/core'
|
|
|
|
|
|
|
|
|
|
const CustomExtension = Extension.create({
|
|
|
|
|
onCreate() {
|
|
|
|
|
// The editor is ready.
|
|
|
|
|
},
|
|
|
|
|
onUpdate() {
|
|
|
|
|
// The content has changed.
|
|
|
|
|
},
|
|
|
|
|
onSelection() {
|
|
|
|
|
// The selection has changed.
|
|
|
|
|
},
|
|
|
|
|
onTransaction({ transaction }) {
|
|
|
|
|
// The editor state has changed.
|
|
|
|
|
},
|
|
|
|
|
onFocus({ event }) {
|
|
|
|
|
// The editor is focused.
|
|
|
|
|
},
|
|
|
|
|
onBlur({ event }) {
|
|
|
|
|
// The editor isn’t focused anymore.
|
|
|
|
|
},
|
|
|
|
|
onDestroy() {
|
|
|
|
|
// The editor is being destroyed.
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
```
|