mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-12-02 13:58:57 +08:00
26 lines
832 B
TypeScript
26 lines
832 B
TypeScript
|
import {$getRoot, LexicalEditor} from "lexical";
|
||
|
import {$generateHtmlFromNodes, $generateNodesFromDOM} from "@lexical/html";
|
||
|
|
||
|
|
||
|
export function setEditorContentFromHtml(editor: LexicalEditor, html: string) {
|
||
|
const parser = new DOMParser();
|
||
|
const dom = parser.parseFromString(html, 'text/html');
|
||
|
|
||
|
editor.update(() => {
|
||
|
const nodes = $generateNodesFromDOM(editor, dom);
|
||
|
const root = $getRoot();
|
||
|
for (const child of root.getChildren()) {
|
||
|
child.remove(true);
|
||
|
}
|
||
|
root.append(...nodes);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
export function getEditorContentAsHtml(editor: LexicalEditor): Promise<string> {
|
||
|
return new Promise((resolve, reject) => {
|
||
|
editor.getEditorState().read(() => {
|
||
|
const html = $generateHtmlFromNodes(editor);
|
||
|
resolve(html);
|
||
|
});
|
||
|
});
|
||
|
}
|