Skip to content

Question: convert multiparagraph plain text to lexical editor state on backend #3850

Answered by kgkg
kgkg asked this question in Q&A
Discussion options

You must be logged in to vote

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:

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(

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@sherrellbc
Comment options

@alicercedigital
Comment options

@eparker-wavefin
Comment options

@ZelCloud
Comment options

Answer selected by kgkg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
enhancement Improvement over existing feature
6 participants
Converted from issue

This discussion was converted from issue #3848 on February 08, 2023 14:39.