2024-07-23 22:35:18 +08:00
|
|
|
import {$getRoot, $getSelection, $isTextNode, LexicalEditor, LexicalNode, RootNode} from "lexical";
|
2024-06-12 21:01:36 +08:00
|
|
|
import {$generateHtmlFromNodes, $generateNodesFromDOM} from "@lexical/html";
|
2024-07-01 22:10:22 +08:00
|
|
|
import {$createCustomParagraphNode} from "./nodes/custom-paragraph";
|
2024-06-12 21:01:36 +08:00
|
|
|
|
2024-07-23 22:35:18 +08:00
|
|
|
function htmlToDom(html: string): Document {
|
|
|
|
const parser = new DOMParser();
|
|
|
|
return parser.parseFromString(html, 'text/html');
|
|
|
|
}
|
|
|
|
|
|
|
|
function wrapTextNodes(nodes: LexicalNode[]): LexicalNode[] {
|
|
|
|
return nodes.map(node => {
|
|
|
|
if ($isTextNode(node)) {
|
|
|
|
const paragraph = $createCustomParagraphNode();
|
|
|
|
paragraph.append(node);
|
|
|
|
return paragraph;
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function appendNodesToRoot(root: RootNode, nodes: LexicalNode[]) {
|
|
|
|
root.append(...wrapTextNodes(nodes));
|
|
|
|
}
|
2024-06-12 21:01:36 +08:00
|
|
|
|
|
|
|
export function setEditorContentFromHtml(editor: LexicalEditor, html: string) {
|
2024-07-23 22:35:18 +08:00
|
|
|
const dom = htmlToDom(html);
|
2024-06-12 21:01:36 +08:00
|
|
|
|
|
|
|
editor.update(() => {
|
2024-07-01 22:10:22 +08:00
|
|
|
// Empty existing
|
2024-06-12 21:01:36 +08:00
|
|
|
const root = $getRoot();
|
|
|
|
for (const child of root.getChildren()) {
|
|
|
|
child.remove(true);
|
|
|
|
}
|
2024-07-01 22:10:22 +08:00
|
|
|
|
|
|
|
const nodes = $generateNodesFromDOM(editor, dom);
|
2024-07-23 22:35:18 +08:00
|
|
|
root.append(...wrapTextNodes(nodes));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function appendHtmlToEditor(editor: LexicalEditor, html: string) {
|
|
|
|
const dom = htmlToDom(html);
|
2024-07-01 22:10:22 +08:00
|
|
|
|
2024-07-23 22:35:18 +08:00
|
|
|
editor.update(() => {
|
|
|
|
const root = $getRoot();
|
|
|
|
const nodes = $generateNodesFromDOM(editor, dom);
|
|
|
|
root.append(...wrapTextNodes(nodes));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function prependHtmlToEditor(editor: LexicalEditor, html: string) {
|
|
|
|
const dom = htmlToDom(html);
|
|
|
|
|
|
|
|
editor.update(() => {
|
|
|
|
const root = $getRoot();
|
|
|
|
const nodes = wrapTextNodes($generateNodesFromDOM(editor, dom));
|
|
|
|
let reference = root.getChildren()[0];
|
|
|
|
for (let i = nodes.length - 1; i >= 0; i--) {
|
|
|
|
if (reference) {
|
|
|
|
reference.insertBefore(nodes[i]);
|
|
|
|
} else {
|
|
|
|
root.append(nodes[i])
|
2024-07-01 22:10:22 +08:00
|
|
|
}
|
2024-07-23 22:35:18 +08:00
|
|
|
reference = nodes[i];
|
2024-07-01 22:10:22 +08:00
|
|
|
}
|
2024-07-23 22:35:18 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function insertHtmlIntoEditor(editor: LexicalEditor, html: string) {
|
|
|
|
const dom = htmlToDom(html);
|
|
|
|
editor.update(() => {
|
|
|
|
const selection = $getSelection();
|
|
|
|
const nodes = wrapTextNodes($generateNodesFromDOM(editor, dom));
|
2024-07-01 22:10:22 +08:00
|
|
|
|
2024-07-23 22:35:18 +08:00
|
|
|
const reference = selection?.getNodes()[0];
|
|
|
|
const referencesParents = reference?.getParents() || [];
|
|
|
|
const topLevel = referencesParents[referencesParents.length - 1];
|
|
|
|
if (topLevel && reference) {
|
|
|
|
for (let i = nodes.length - 1; i >= 0; i--) {
|
|
|
|
reference.insertAfter(nodes[i]);
|
|
|
|
}
|
|
|
|
}
|
2024-06-12 21:01:36 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getEditorContentAsHtml(editor: LexicalEditor): Promise<string> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
editor.getEditorState().read(() => {
|
|
|
|
const html = $generateHtmlFromNodes(editor);
|
|
|
|
resolve(html);
|
|
|
|
});
|
|
|
|
});
|
2024-07-23 22:35:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function focusEditor(editor: LexicalEditor) {
|
|
|
|
editor.focus(() => {}, {defaultSelection: "rootStart"});
|
2024-06-12 21:01:36 +08:00
|
|
|
}
|