2021-08-24 17:24:10 +08:00
|
|
|
|
---
|
|
|
|
|
tableOfContents: true
|
|
|
|
|
---
|
|
|
|
|
|
2020-09-09 21:59:34 +08:00
|
|
|
|
# Events
|
|
|
|
|
|
2020-10-01 18:26:20 +08:00
|
|
|
|
## Introduction
|
2021-06-23 03:33:05 +08:00
|
|
|
|
The editor fires a few different events that you can hook into. Let’s have a look at all the available events first:
|
2020-10-01 18:26:20 +08:00
|
|
|
|
|
2021-06-23 03:33:05 +08:00
|
|
|
|
## List of available events
|
|
|
|
|
|
2021-10-08 03:02:30 +08:00
|
|
|
|
### 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 isn’t 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:
|
2020-09-09 21:59:34 +08:00
|
|
|
|
|
|
|
|
|
```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.
|
2020-09-09 21:59:34 +08:00
|
|
|
|
},
|
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 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
|
|
|
|
})
|
|
|
|
|
```
|
|
|
|
|
|
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:
|
2020-09-09 21:59:34 +08:00
|
|
|
|
|
2021-06-23 03:33:05 +08:00
|
|
|
|
#### Bind event listeners
|
2020-09-09 21:59:34 +08:00
|
|
|
|
```js
|
2021-04-21 16:27:51 +08:00
|
|
|
|
editor.on('beforeCreate', ({ editor }) => {
|
|
|
|
|
// Before the view is created.
|
2021-11-10 06:15:17 +08:00
|
|
|
|
})
|
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-11-10 06:15:17 +08:00
|
|
|
|
})
|
2020-09-09 21:59:34 +08:00
|
|
|
|
|
2021-03-10 04:25:30 +08:00
|
|
|
|
editor.on('update', ({ editor }) => {
|
2020-09-26 04:22:44 +08:00
|
|
|
|
// The content has changed.
|
2021-11-10 06:15:17 +08:00
|
|
|
|
})
|
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.
|
2021-11-10 06:15:17 +08:00
|
|
|
|
})
|
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.
|
2021-11-10 06:15:17 +08:00
|
|
|
|
})
|
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.
|
2021-11-10 06:15:17 +08:00
|
|
|
|
})
|
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 isn’t focused anymore.
|
2021-11-10 06:15:17 +08:00
|
|
|
|
})
|
2020-11-30 20:56:42 +08:00
|
|
|
|
|
|
|
|
|
editor.on('destroy', () => {
|
2020-11-30 21:50:26 +08:00
|
|
|
|
// The editor is being destroyed.
|
2021-11-10 06:15:17 +08:00
|
|
|
|
})
|
2020-09-09 21:59:34 +08:00
|
|
|
|
```
|
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. Here’s 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 isn’t focused anymore.
|
|
|
|
|
},
|
|
|
|
|
onDestroy() {
|
|
|
|
|
// The editor is being destroyed.
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
```
|