Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(react): fix rebinding events not overwriting editor.on #3935

Merged
merged 2 commits into from
Apr 3, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions packages/react/src/useEditor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { EditorOptions } from '@tiptap/core'
import { DependencyList, useEffect, useState } from 'react'
import {
DependencyList,
useEffect,
useRef,
useState,
} from 'react'

import { Editor } from './Editor'

Expand All @@ -25,6 +30,15 @@ export const useEditor = (options: Partial<EditorOptions> = {}, deps: Dependency
onUpdate,
} = options

const onBeforeCreateRef = useRef(onBeforeCreate)
const onBlurRef = useRef(onBlur)
const onCreateRef = useRef(onCreate)
const onDestroyRef = useRef(onDestroy)
const onFocusRef = useRef(onFocus)
const onSelectionUpdateRef = useRef(onSelectionUpdate)
const onTransactionRef = useRef(onTransaction)
const onUpdateRef = useRef(onUpdate)

// This effect will handle updating the editor instance
// when the event handlers change.
useEffect(() => {
Expand All @@ -33,45 +47,53 @@ export const useEditor = (options: Partial<EditorOptions> = {}, deps: Dependency
}

if (onBeforeCreate) {
editor.off('beforeCreate')
editor.off('beforeCreate', onBeforeCreateRef.current)
editor.on('beforeCreate', onBeforeCreate)
onBeforeCreateRef.current = onBeforeCreate
}

if (onBlur) {
editor.off('blur')
editor.off('blur', onBlurRef.current)
editor.on('blur', onBlur)
onBlurRef.current = onBlur
}

if (onCreate) {
editor.off('create')
editor.off('create', onCreateRef.current)
editor.on('create', onCreate)
onCreateRef.current = onCreate
}

if (onDestroy) {
editor.off('destroy')
editor.off('destroy', onDestroyRef.current)
editor.on('destroy', onDestroy)
onDestroyRef.current = onDestroy
}

if (onFocus) {
editor.off('focus')
editor.off('focus', onFocusRef.current)
editor.on('focus', onFocus)
onFocusRef.current = onFocus
}

if (onSelectionUpdate) {
editor.off('selectionUpdate')
editor.off('selectionUpdate', onSelectionUpdateRef.current)
editor.on('selectionUpdate', onSelectionUpdate)
onSelectionUpdateRef.current = onSelectionUpdate
}

if (onTransaction) {
editor.off('transaction')
editor.off('transaction', onTransactionRef.current)
editor.on('transaction', onTransaction)
onTransactionRef.current = onTransaction
}

if (onUpdate) {
editor.off('update')
editor.off('update', onUpdateRef.current)
editor.on('update', onUpdate)
onUpdateRef.current = onUpdate
}
}, [onBeforeCreate, onBlur, onCreate, onDestroy, onFocus, onSelectionUpdate, onTransaction, onUpdate])
}, [onBeforeCreate, onBlur, onCreate, onDestroy, onFocus, onSelectionUpdate, onTransaction, onUpdate, editor])

useEffect(() => {
let isMounted = true
Expand Down