fix: fix some react focus issues (#1724), fix #1716, fix #1608, fix #1520

* remove async createNodeViews

* focus asynchronously to fix weird bugs in react
This commit is contained in:
Philipp Kühn
2021-08-12 18:03:45 +02:00
committed by GitHub
parent 812c49bcb1
commit 956566eaad
5 changed files with 46 additions and 27 deletions

View File

@@ -11,10 +11,12 @@ declare module '@tiptap/core' {
}
}
export const blur: RawCommands['blur'] = () => ({ view }) => {
const element = view.dom as HTMLElement
element.blur()
export const blur: RawCommands['blur'] = () => ({ editor, view }) => {
requestAnimationFrame(() => {
if (!editor.isDestroyed) {
(view.dom as HTMLElement).blur()
}
})
return true
}

View File

@@ -47,13 +47,23 @@ export const focus: RawCommands['focus'] = (position = null) => ({
tr,
dispatch,
}) => {
const delayedFocus = () => {
// For React we have to focus asynchronously. Otherwise wild things happen.
// see: https://github.com/ueberdosis/tiptap/issues/1520
requestAnimationFrame(() => {
if (!editor.isDestroyed) {
view.focus()
}
})
}
if ((view.hasFocus() && position === null) || position === false) {
return true
}
// we dont try to resolve a NodeSelection or CellSelection
if (dispatch && position === null && !isTextSelection(editor.state.selection)) {
view.focus()
delayedFocus()
return true
}
@@ -67,7 +77,9 @@ export const focus: RawCommands['focus'] = (position = null) => ({
const isSameSelection = editor.state.selection.eq(selection)
if (dispatch) {
tr.setSelection(selection)
if (!isSameSelection) {
tr.setSelection(selection)
}
// `tr.setSelection` resets the stored marks
// so well restore them if the selection is the same as before
@@ -75,7 +87,7 @@ export const focus: RawCommands['focus'] = (position = null) => ({
tr.setStoredMarks(storedMarks)
}
view.focus()
delayedFocus()
}
return true