-
Hello, we're using Lexical editor in frontend. We have Now, instead of saving plain text, we want to save lexical format of each of these texts on backend in order to later open them in frontend. const lexicalDescription = convertToLexical(plainTextDesciption); We were searching for a quite a long time and couldn't find a solution. We don't want to make workarounds on frontend for plain text - we want it to be lexical from the start. How can we achieve that? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Take a lookat the documentation for the editor state. https://lexical.dev/docs/concepts/editor-state Once you understand Lexical's schema, your task can be accomplished rather easily. |
Beta Was this translation helpful? Give feedback.
-
Thanks @moy2010 for giving me hope that it's possible :) In case someone is struggling with this, here is my solution: const lexicalEditor = createHeadlessEditor({
namespace: "Editor",
nodes: [],
onError: console.error
});
async function plainTextToLexicalState(text: string): Promise<string> {
return new Promise((resolve) => {
lexicalEditor.registerUpdateListener(({editorState}) => {
resolve(JSON.stringify(editorState));
});
lexicalEditor.update(() => {
const paragraph = $createParagraphNode();
const textNode = $createTextNode(text);
paragraph.append(textNode);
$getRoot()
.clear()
.append(paragraph);
});
});
} |
Beta Was this translation helpful? Give feedback.
Thanks @moy2010 for giving me hope that it's possible :)
It was not that easy without realizing how lexical editor works under the hood though.
In case someone is struggling with this, here is my solution: