]> BookStack Code Mirror - bookstack/blob - resources/js/wysiwyg/actions.ts
Lexical: Added view/edit source code button/form/action
[bookstack] / resources / js / wysiwyg / actions.ts
1 import {$getRoot, LexicalEditor} from "lexical";
2 import {$generateHtmlFromNodes, $generateNodesFromDOM} from "@lexical/html";
3
4
5 export function setEditorContentFromHtml(editor: LexicalEditor, html: string) {
6     const parser = new DOMParser();
7     const dom = parser.parseFromString(html, 'text/html');
8
9     editor.update(() => {
10         const nodes = $generateNodesFromDOM(editor, dom);
11         const root = $getRoot();
12         for (const child of root.getChildren()) {
13             child.remove(true);
14         }
15         root.append(...nodes);
16     });
17 }
18
19 export function getEditorContentAsHtml(editor: LexicalEditor): Promise<string> {
20     return new Promise((resolve, reject) => {
21         editor.getEditorState().read(() => {
22             const html = $generateHtmlFromNodes(editor);
23             resolve(html);
24         });
25     });
26 }