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

ReactEditor.focus does not work after inserting a new node #4961

Open
sodenn opened this issue Apr 22, 2022 · 9 comments
Open

ReactEditor.focus does not work after inserting a new node #4961

sodenn opened this issue Apr 22, 2022 · 9 comments
Labels

Comments

@sodenn
Copy link

sodenn commented Apr 22, 2022

Description
When using ReactEditor.focus after inserting a new Node via Transforms.insertNodes, the mouse cursor is not placed at the expected location.

Recording
Focusing works in [email protected] and before:
2022-04-22 23 10 42

Focusing no longer works in [email protected]
2022-04-22 23 11 08

Sandbox
[email protected] and before: https://codesandbox.io/s/elastic-nova-2nw62t?file=/src/App.js
[email protected]: https://codesandbox.io/s/suspicious-mestorf-e9306h?file=/src/App.js
The sandboxes are based on the Mentions example: https://github.com/ianstormtaylor/slate/blob/main/site/examples/mentions.tsx

Steps
To reproduce the behavior:

  1. Type "@A" to open the menu
  2. Select a menu item with a mouse click

Expectation
The cursor should be placed at the end of the text in the editor.

Environment

  • Slate Version: 0.77.2
  • Operating System: OSX
  • Browser: Chrome
@sodenn sodenn added the bug label Apr 22, 2022
e1himself added a commit to prezly/slate that referenced this issue May 12, 2022
As the newest version has a bug preventing to
focus editor after inserting a new node.

See ianstormtaylor/slate#4961
@ryanmitts
Copy link
Contributor

ryanmitts commented Jun 29, 2022

Change Transforms.move(editor)

to

Transforms.move(editor, {
    distance: 1,
    unit: "offset"
});

@Nikhil22
Copy link

Nikhil22 commented Dec 5, 2022

@ryanmitts That doesn't work for me. My issue is related, and what I want to do is even simpler -- Move the cursor the end of the document.

@jacksteves
Copy link

jacksteves commented Dec 22, 2022

@Nikhil22 How about Transforms.select(editor, Editor.end(editor, []))?

@TheSohaibAhmed
Copy link

TheSohaibAhmed commented Mar 15, 2023

I seem to have maybe potentially found the fix for this.

Here's what I understood the problem to be: essentially, if the place that I clicked on was not directly the editor, then it lost focus (i.e. ReactEditor.isFocused() was false), but if you try to refocus it, it gave me error "Cannot resolve a DOM node from Slate node: undefined" -- which meant, yeah: if the v0.77 isn't adding the slate node after the Transforms.insertNodes then this'd be a problem.

How I created a slate node

  1. I changed selection to the newly added node (which didn't show the cursor -- as seen in the video OP posted above)
  2. Using the suggestion above, i did "Transform.move(...unit: "offset", edge:"focus") <-- this, i think creates the slade node.
  3. At this point, select the newly added node again (don't focus the editor; somehow that still doesn't work).

You'll get your selection.

Here's the code, when I was adding a new paragraph after the one I was on (i.e. if I was on [0, 0, 16]--in the middle of a para), I wanted to create [0,1] and jump to [0,1,0]):

1. Insert node
Transforms.insertNodes(...)

2. Select the new location's first index (for you this might be diff)
**Transforms.select(editor, {
     anchor: { offset: 0, path: [currentElIndex + 1, 0] },
     focus: { offset: 0, path: [currentElIndex + 1, 0] },
   });**
   
3. Move to next offset (which shouldn't exist technically);
   **Transforms.move(editor, {
     distance: 1,
     unit: "offset",
     edge: "focus",
   });**
   
 4. Select the originally intended location
   **Transforms.select(editor, {
     anchor: { offset: 0, path: [currentElIndex + 1, 0] },
     focus: { offset: 0, path: [currentElIndex + 1, 0] },
   });**

@jmilus
Copy link

jmilus commented Jun 11, 2023

How is this still an unresolved issue? if there's a function for ".focus" it shouldn't require workarounds. Seems like a pretty crucial function, considering people are constantly clicking on format buttons and thus blurring the editor.

@AlexeyShaykov
Copy link

AlexeyShaykov commented Apr 25, 2024

Hello everyone, I have same problem with focus after insert new node

`
const fn = () => {
Transforms.insertNodes(editor, [{ type: 'paragraph', children: [{ text: 'here text' }] }]);

setTimeout(() => {
console.log(editor.selection)
// anchor:
// offset: 0
// path: [1, 0]
// focus:
// offset: 0
// path: [1, 0]
ReactEditor.focus(editor);
}, 10);
}
`
image

As u see, after insert new node my editor.selection show that now selection on second paragraph (path: [1, 0]), but when I call ReactEditor.focus(editor); cursor move to first paragraph. Why so?

@tommydangerous
Copy link

@Nikhil22 How about Transforms.select(editor, Editor.end(editor, []))?

this!

@gaoqs
Copy link

gaoqs commented Sep 23, 2024

ReactEditor.focus

const end = Editor.end(editor, []);
ReactEditor.deselect(editor as ReactEditor);
Transforms.select(editor, { anchor: end, focus: end });
ReactEditor.focus(editor as ReactEditor);

deselect first and then select, works for me

@cwooldridge1
Copy link

This is hacky as hell but I was working with a web view and it really was not wanting to focus, I essentially created a listener that kept track of the last focused input element, then to focus at a given point I focused the last element and then used @gaoqs solution. This very hacky manual focusing of the page I believe is only needed do to the web view losing focus.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

10 participants