2 $createParagraphNode, $getRoot,
5 BaseSelection, ElementNode,
6 LexicalEditor, LexicalNode, TextFormatType
8 import {LexicalElementNodeCreator, LexicalNodeMatcher} from "./nodes";
9 import {$getNearestBlockElementAncestorOrThrow} from "@lexical/utils";
10 import {$setBlocksType} from "@lexical/selection";
11 import {$createDetailsNode} from "./nodes/details";
13 export function el(tag: string, attrs: Record<string, string|null> = {}, children: (string|HTMLElement)[] = []): HTMLElement {
14 const el = document.createElement(tag);
15 const attrKeys = Object.keys(attrs);
16 for (const attr of attrKeys) {
17 if (attrs[attr] !== null) {
18 el.setAttribute(attr, attrs[attr] as string);
22 for (const child of children) {
23 if (typeof child === 'string') {
24 el.append(document.createTextNode(child));
33 export function selectionContainsNodeType(selection: BaseSelection|null, matcher: LexicalNodeMatcher): boolean {
34 return getNodeFromSelection(selection, matcher) !== null;
37 export function getNodeFromSelection(selection: BaseSelection|null, matcher: LexicalNodeMatcher): LexicalNode|null {
42 for (const node of selection.getNodes()) {
47 for (const parent of node.getParents()) {
48 if (matcher(parent)) {
57 export function selectionContainsTextFormat(selection: BaseSelection|null, format: TextFormatType): boolean {
62 for (const node of selection.getNodes()) {
63 if ($isTextNode(node) && node.hasFormat(format)) {
71 export function toggleSelectionBlockNodeType(editor: LexicalEditor, matcher: LexicalNodeMatcher, creator: LexicalElementNodeCreator) {
73 const selection = $getSelection();
74 const blockElement = selection ? $getNearestBlockElementAncestorOrThrow(selection.getNodes()[0]) : null;
75 if (selection && matcher(blockElement)) {
76 $setBlocksType(selection, $createParagraphNode);
78 $setBlocksType(selection, creator);
83 export function insertNewBlockNodeAtSelection(node: LexicalNode) {
84 const selection = $getSelection();
85 const blockElement = selection ? $getNearestBlockElementAncestorOrThrow(selection.getNodes()[0]) : null;
88 blockElement.insertAfter(node);
90 $getRoot().append(node);