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 some react focus issues #1724

Merged
merged 4 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
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
10 changes: 6 additions & 4 deletions packages/core/src/commands/blur.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
18 changes: 15 additions & 3 deletions packages/core/src/commands/focus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 don’t try to resolve a NodeSelection or CellSelection
if (dispatch && position === null && !isTextSelection(editor.state.selection)) {
view.focus()
delayedFocus()
return true
}

Expand All @@ -67,15 +77,17 @@ 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 we’ll restore them if the selection is the same as before
if (isSameSelection && storedMarks) {
tr.setStoredMarks(storedMarks)
}

view.focus()
delayedFocus()
}

return true
Expand Down
19 changes: 12 additions & 7 deletions packages/extension-bubble-menu/src/bubble-menu-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class BubbleMenuView {

public preventHide = false

public tippy!: Instance
public tippy: Instance | undefined

public shouldShow: Exclude<BubbleMenuPluginProps['shouldShow'], null> = ({ state, from, to }) => {
const { doc, selection } = state
Expand Down Expand Up @@ -74,8 +74,13 @@ export class BubbleMenuView {
this.view.dom.addEventListener('dragstart', this.dragstartHandler)
this.editor.on('focus', this.focusHandler)
this.editor.on('blur', this.blurHandler)
this.createTooltip(tippyOptions)
this.element.style.visibility = 'visible'

// We create tippy asynchronously to make sure that `editor.options.element`
// has already been moved to the right position in the DOM
requestAnimationFrame(() => {
this.createTooltip(tippyOptions)
})
}

mousedownHandler = () => {
Expand Down Expand Up @@ -109,7 +114,7 @@ export class BubbleMenuView {
}

createTooltip(options: Partial<Props> = {}) {
this.tippy = tippy(this.view.dom, {
this.tippy = tippy(this.editor.options.element, {
duration: 0,
getReferenceClientRect: null,
content: this.element,
Expand Down Expand Up @@ -150,7 +155,7 @@ export class BubbleMenuView {
return
}

this.tippy.setProps({
this.tippy?.setProps({
getReferenceClientRect: () => {
if (isNodeSelection(state.selection)) {
const node = view.nodeDOM(from) as HTMLElement
Expand All @@ -168,15 +173,15 @@ export class BubbleMenuView {
}

show() {
this.tippy.show()
this.tippy?.show()
}

hide() {
this.tippy.hide()
this.tippy?.hide()
}

destroy() {
this.tippy.destroy()
this.tippy?.destroy()
this.element.removeEventListener('mousedown', this.mousedownHandler)
this.view.dom.removeEventListener('dragstart', this.dragstartHandler)
this.editor.off('focus', this.focusHandler)
Expand Down
19 changes: 12 additions & 7 deletions packages/extension-floating-menu/src/floating-menu-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class FloatingMenuView {

public preventHide = false

public tippy!: Instance
public tippy: Instance | undefined

public shouldShow: Exclude<FloatingMenuPluginProps['shouldShow'], null> = ({ state }) => {
const { selection } = state
Expand Down Expand Up @@ -64,8 +64,13 @@ export class FloatingMenuView {
this.element.addEventListener('mousedown', this.mousedownHandler, { capture: true })
this.editor.on('focus', this.focusHandler)
this.editor.on('blur', this.blurHandler)
this.createTooltip(tippyOptions)
this.element.style.visibility = 'visible'

// We create tippy asynchronously to make sure that `editor.options.element`
// has already been moved to the right position in the DOM
requestAnimationFrame(() => {
this.createTooltip(tippyOptions)
})
}

mousedownHandler = () => {
Expand Down Expand Up @@ -95,7 +100,7 @@ export class FloatingMenuView {
}

createTooltip(options: Partial<Props> = {}) {
this.tippy = tippy(this.view.dom, {
this.tippy = tippy(this.editor.options.element, {
duration: 0,
getReferenceClientRect: null,
content: this.element,
Expand Down Expand Up @@ -130,23 +135,23 @@ export class FloatingMenuView {
return
}

this.tippy.setProps({
this.tippy?.setProps({
getReferenceClientRect: () => posToDOMRect(view, from, to),
})

this.show()
}

show() {
this.tippy.show()
this.tippy?.show()
}

hide() {
this.tippy.hide()
this.tippy?.hide()
}

destroy() {
this.tippy.destroy()
this.tippy?.destroy()
this.element.removeEventListener('mousedown', this.mousedownHandler)
this.editor.off('focus', this.focusHandler)
this.editor.off('blur', this.blurHandler)
Expand Down
7 changes: 1 addition & 6 deletions packages/react/src/EditorContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,7 @@ export class PureEditorContent extends React.Component<EditorContentProps, Edito

editor.contentComponent = this

// TODO: alternative to setTimeout?
setTimeout(() => {
if (!editor.isDestroyed) {
editor.createNodeViews()
}
}, 0)
editor.createNodeViews()
}
}

Expand Down