facebook / lexical

Lexical is an extensible text editor framework that provides excellent reliability, accessibility and performance.
https://lexical.dev
MIT License
19.09k stars 1.61k forks source link

Feature: set default value without focusing #4734

Open zhanghan3 opened 1 year ago

zhanghan3 commented 1 year ago

I am trying setting default value for lexcial editor from html string. I know that I can achieve by this:

const dom = generateDomFromString(value);
const nodes = $generateNodesFromDOM(editor, dom);
$getRoot().select();
$insertNodes(nodes);

or

$getRoot().clear()
$getRoot().append(node)

But they all need to focus on editor. And there is another element needed be focused.

Is there any another way that I can set default value without focusing on editor. 

acywatson commented 1 year ago

But they all need to focus on editor

Why do you think this? I don't think this should be the case.

zhanghan3 commented 1 year ago

But they all need to focus on editor

Why do you think this? I don't think this should be the case.

Sorry for not clear. I mean if I use any of two way above, the cursor will be focused in the editor.

My question is: After page is initialized, can I set default value for the editor and focus the cursor on other input fields?

fantactuka commented 1 year ago

If you set selection while inserting nodes (either via rootNode.select() or elsewhere), you can call $setSelection(null) in the end to reset selection and avoid auto-focusing

anilanar commented 11 months ago

I think rootNode.clear() is stealing focus too, which is unexpected.

thorn0 commented 1 month ago

There should exist a way to call functions like $patchStyleText without focusing the editor. As an example of this problem, try changing text color on the playground by entering a hex value. The moment you enter the last hex character, the focus is stolen. Calling $setSelection(null) is not an option since we need to preserve the selection for further color changes while the color picker is open.

zhanghan3 commented 2 weeks ago

If you set selection while inserting nodes (either via rootNode.select() or elsewhere), you can call $setSelection(null) in the end to reset selection and avoid auto-focusing

I think this can help not focus on Lexical but this may also result in not focusing on the input element I am expecting. Here are two: Lexical editor and common input with autofocus props. If I set selection and then reset, there might not be any element be focused I think.

etrepum commented 2 weeks ago

There are two selections: lexical's selection, and the DOM's selection. When you manipulate lexical's selection nothing happens to the DOM's selection until reconciliation (after the batch of update calls).