1 import {$getRoot, $isElementNode, $isTextNode, ElementNode, LexicalEditor, LexicalNode} from "lexical";
2 import {LexicalNodeMatcher} from "../nodes";
3 import {$createCustomParagraphNode} from "../nodes/custom-paragraph";
4 import {$generateNodesFromDOM} from "@lexical/html";
5 import {htmlToDom} from "./dom";
6 import {NodeHasAlignment} from "../nodes/_common";
8 function wrapTextNodes(nodes: LexicalNode[]): LexicalNode[] {
9 return nodes.map(node => {
10 if ($isTextNode(node)) {
11 const paragraph = $createCustomParagraphNode();
12 paragraph.append(node);
19 export function $htmlToBlockNodes(editor: LexicalEditor, html: string): LexicalNode[] {
20 const dom = htmlToDom(html);
21 const nodes = $generateNodesFromDOM(editor, dom);
22 return wrapTextNodes(nodes);
25 export function $getParentOfType(node: LexicalNode, matcher: LexicalNodeMatcher): LexicalNode | null {
26 for (const parent of node.getParents()) {
27 if (matcher(parent)) {
35 export function $getAllNodesOfType(matcher: LexicalNodeMatcher, root?: ElementNode): LexicalNode[] {
42 for (const child of root.getChildren()) {
47 if ($isElementNode(child)) {
48 matches.push(...$getAllNodesOfType(matcher, child));
56 * Get the nearest root/block level node for the given position.
58 export function $getNearestBlockNodeForCoords(editor: LexicalEditor, x: number, y: number): LexicalNode | null {
59 // TODO - Take into account x for floated blocks?
60 const rootNodes = $getRoot().getChildren();
61 for (const node of rootNodes) {
62 const nodeDom = editor.getElementByKey(node.__key);
67 const bounds = nodeDom.getBoundingClientRect();
68 if (y <= bounds.bottom) {
76 export function nodeHasAlignment(node: object): node is NodeHasAlignment {
77 return '__alignment' in node;