Skip to content

prefill html content inside lexical/react editor #2320

Answered by maltesa
harish-sethuraman asked this question in Q&A
Discussion options

You must be logged in to vote

I wrote this Plugin that loads my legacy HTML Content. Maybe that helps:

import type { IRichText } from '@/components/RichTextEditor/types'
import { $generateNodesFromDOM } from '@lexical/html'
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'
import { $getRoot, CLEAR_HISTORY_COMMAND } from 'lexical'
import { useEffect } from 'react'

interface Props {
  state?: string | IRichText
}

export function RestoreEditorStatePlugin({ state }: Props) {
  const [editor] = useLexicalComposerContext()

  useEffect(() => {
    if (typeof state === 'string') {
      // Restore from HTML (Quilljs legacy)
      editor.update(() => {
        const parser = new DOMParser()

Replies: 2 comments 26 replies

Comment options

You must be logged in to vote
10 replies
@harish-sethuraman
Comment options

@harish-sethuraman
Comment options

@harish-sethuraman
Comment options

@tejas-hosamani
Comment options

@tejas-hosamani
Comment options

Comment options

You must be logged in to vote
16 replies
@maltesa
Comment options

@lwhiteley
Comment options

@maltesa
Comment options

@lwhiteley
Comment options

@shammahk
Comment options

Answer selected by thegreatercurve
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
9 participants