1 import {$createParagraphNode, $getRoot, $isTextNode, LexicalEditor} from "lexical";
2 import {$generateHtmlFromNodes, $generateNodesFromDOM} from "@lexical/html";
3 import {$createCustomParagraphNode} from "./nodes/custom-paragraph";
6 export function setEditorContentFromHtml(editor: LexicalEditor, html: string) {
7 const parser = new DOMParser();
8 const dom = parser.parseFromString(html, 'text/html');
13 const root = $getRoot();
14 for (const child of root.getChildren()) {
18 const nodes = $generateNodesFromDOM(editor, dom);
20 // Wrap top-level text nodes
21 for (let i = 0; i < nodes.length; i++) {
22 const node = nodes[i];
23 if ($isTextNode(node)) {
24 const paragraph = $createCustomParagraphNode();
25 paragraph.append(node);
30 root.append(...nodes);
34 export function getEditorContentAsHtml(editor: LexicalEditor): Promise<string> {
35 return new Promise((resolve, reject) => {
36 editor.getEditorState().read(() => {
37 const html = $generateHtmlFromNodes(editor);