-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Remove intermediate render cycle from useEditor #4579
Changes from all commits
a592852
1892b70
431c0cd
cedeee9
a385bb3
07b6d59
994d154
d0badc4
48e2e28
8010dfb
f30bf2a
ab99fac
f55f7f1
6c50f07
6da0423
fbf13bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Editor API Utility Overview | ||
|
||
Welcome to the Editor API Utility section. Here, you'll discover essential tools to enhance your Tiptap experience: | ||
|
||
- **Render JSON as HTML**: Learn to convert JSON content to HTML, even without an editor instance, simplifying content management. | ||
|
||
- **Tiptap for PHP**: Explore PHP integration for Tiptap, enabling seamless content transformation and modification. | ||
|
||
- **Suggestions**: Enhance your editor with suggestions like mentions and emojis, tailored to your needs. | ||
|
||
Explore subpages for in-depth guidance and examples. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import { EditorOptions } from '@tiptap/core' | |
import { | ||
DependencyList, | ||
useEffect, | ||
useMemo, | ||
useRef, | ||
useState, | ||
} from 'react' | ||
|
@@ -113,14 +114,130 @@ export const useEditor = (options: Partial<EditorOptions> = {}, deps: Dependency | |
|
||
return () => { | ||
isMounted = false | ||
editorRef.current?.destroy() | ||
C-Hess marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}, deps) | ||
|
||
return editorRef.current | ||
} | ||
|
||
/** | ||
* This hook will create and immediately return a defined editor instance. In future major versions, this | ||
* hook will be removed and useEditor will be changed to behave like this hook. | ||
*/ | ||
export const useEditorForImmediateRender = (options: Partial<EditorOptions> = {}, deps: DependencyList = []) => { | ||
const { | ||
onBeforeCreate, | ||
onBlur, | ||
onCreate, | ||
onDestroy, | ||
onFocus, | ||
onSelectionUpdate, | ||
onTransaction, | ||
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) | ||
|
||
const isMounted = useRef(false) | ||
|
||
useEffect(() => { | ||
isMounted.current = true | ||
|
||
return () => { | ||
return editorRef.current?.destroy() | ||
isMounted.current = false | ||
} | ||
}, []) | ||
|
||
return editorRef.current | ||
const [, forceUpdate] = useState({}) | ||
const editor = useMemo(() => { | ||
const instance = new Editor(options) | ||
|
||
instance.on('transaction', () => { | ||
requestAnimationFrame(() => { | ||
requestAnimationFrame(() => { | ||
if (isMounted.current) { | ||
forceUpdate({}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not required in this PR, but it would be nice to add comment here why is this section necessary (to call the forceUpdate wrapped in 2 requestAnimationFrame calls on every editor transaction). This is a bit black magic to me. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. I just copied this over from the original hook, but it looks to me like it's logic to get around timing issues between Prosemirror transactions and React render cycles There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if this will help, but this seems to fix the flickering issue mentioned here: #2040 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately there is a habit in this repo of closing non-fixed issues. @szymonchudy is that issue separate from #5166? It's hard to tell if they're related or truly the same. |
||
} | ||
}) | ||
}) | ||
}) | ||
|
||
return instance | ||
}, deps) | ||
|
||
useEffect(() => { | ||
return () => { | ||
editor.destroy() | ||
} | ||
}, [editor]) | ||
|
||
// This effect will handle updating the editor instance | ||
// when the event handlers change. | ||
useEffect(() => { | ||
if (onBeforeCreate) { | ||
editor.off('beforeCreate', onBeforeCreateRef.current) | ||
editor.on('beforeCreate', onBeforeCreate) | ||
|
||
onBeforeCreateRef.current = onBeforeCreate | ||
} | ||
|
||
if (onBlur) { | ||
editor.off('blur', onBlurRef.current) | ||
editor.on('blur', onBlur) | ||
|
||
onBlurRef.current = onBlur | ||
} | ||
|
||
if (onCreate) { | ||
editor.off('create', onCreateRef.current) | ||
editor.on('create', onCreate) | ||
|
||
onCreateRef.current = onCreate | ||
} | ||
|
||
if (onDestroy) { | ||
editor.off('destroy', onDestroyRef.current) | ||
editor.on('destroy', onDestroy) | ||
|
||
onDestroyRef.current = onDestroy | ||
} | ||
|
||
if (onFocus) { | ||
editor.off('focus', onFocusRef.current) | ||
editor.on('focus', onFocus) | ||
|
||
onFocusRef.current = onFocus | ||
} | ||
|
||
if (onSelectionUpdate) { | ||
editor.off('selectionUpdate', onSelectionUpdateRef.current) | ||
editor.on('selectionUpdate', onSelectionUpdate) | ||
|
||
onSelectionUpdateRef.current = onSelectionUpdate | ||
} | ||
|
||
if (onTransaction) { | ||
editor.off('transaction', onTransactionRef.current) | ||
editor.on('transaction', onTransaction) | ||
|
||
onTransactionRef.current = onTransaction | ||
} | ||
|
||
if (onUpdate) { | ||
editor.off('update', onUpdateRef.current) | ||
editor.on('update', onUpdate) | ||
|
||
onUpdateRef.current = onUpdate | ||
} | ||
}, [onBeforeCreate, onBlur, onCreate, onDestroy, onFocus, onSelectionUpdate, onTransaction, onUpdate, editor]) | ||
|
||
return editor | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo defualt to default