Open darsee opened 1 year ago
I fix it in PR
Hope it will help.
The code is small, you can edit file in your local file, which path is node_modules/@lexical/markdown/LexicalMarkdown.dev.js#290
After rerun the start
, you can check it yourself.
Hey @aquarius-wing, thanks for the PR!
I don't think it solves the issue for me though - the problem in the scenario above is that the editor is focused when it loads but I am expecting it not to be focused until I click it. Your PR (if I understand correctly) causes the editor to be focused even if the markdown conversion doesn't match on anything - I'm not sure if this behaviour would be expected.
So, is it possible that I misunderstood your meaning? I thought you wanted the focus to be only when text is inputted. So, you mean the opposite, that even when '# text' is inputted, it should not be focused? I'm quite confused. 😂
even when '# text' is inputted, it should not be focused?
Yes, exactly 😄 I expect the editor not to be focused until I click it.
I feel that it should be up to the lexical official to determine whether a separate configuration item is needed to control whether to auto-focus.
Hi @darsee!
I ran into a very similar problem with $convertFromMarkdownString
recently.
I was able to work around it by:
useEffect(() => {
// Set the editor to not editable - it won't take focus while in that state
editor.setEditable(false);
const unregister = editor.registerNodeTransform(TextNode, (textNode) => {
console.log("node transformed", textNode);
});
// Set the editor back to editable a frame later
requestAnimationFrame(() => editor.setEditable(true));
return unregister;
}, [editor]);
Using this pattern in the code sandbox seems to work!
Probably not a perfect solution, but hopefully it's able to help you with a short-term solution
Hey @danchurch-alphasights!
Thanks a lot for the input - this looks like a very clever workaround.
I need to see if I can apply it to my actual use case where registerNodeTransform
is called inside useAutoLink
in LexicalAutoLinkPlugin
:
My current workaround is to conditionally use AutoLinkPlugin
only if the editor is focused (which we track in state for other reasons anyway) but that feels very hacky.
Any updates here? This is very irritating issue as registerNodeTransform is focusing the editor. The workaround mentioned above are hacky and leads to more issues where we have to track focus state of the editor to ensure consistent behaviour.
+1 looks like there is no good way to set initial value without focusing the editor
Hey, try this
export const LoadInitialContent: React.FC<{ initHtml: string }> = ({ initHtml = '' }) => {
const [editor] = useLexicalComposerContext();
const isFirstRender = useRef(true);
useLayoutEffect(() => {
editor.update(() => {
if (isFirstRender.current && !!initHtml) {
isFirstRender.current = false;
const parser = new DOMParser();
const dom = parser.parseFromString(initHtml, 'text/html');
const nodes = $generateNodesFromDOM(editor, dom);
const root = $getRoot();
root.clear();
$setSelection(null);
root.append(...nodes);
editor.dispatchCommand(CLEAR_HISTORY_COMMAND, undefined);
}
});
}, [initHtml, isFirstRender.current]);
return null;
};
In the codesandbox linked below, the editor is focused on mount when the
initialConfig
editorState
converts a markdown heading andregisterNodeTransform
has been used. Is this expected?Lexical version: 0.10.0
Steps To Reproduce
value
totest
(uncomment line 27)value
back to# test
and comment out<TestPlugin />
Link to code example:
https://codesandbox.io/s/lexical-editor-focused-after-node-transform-sfm1wn?file=/src/Editor.js
The current behavior
On reloading the browser preview, the editor is initially focused (cursor is visible).
The expected behavior
I expect the editor not to be initially focused unless using
@lexical/react/LexicalAutoFocusPlugin
?