6 LexicalEditor, LexicalNode, TextFormatType
8 import {LexicalElementNodeCreator, LexicalNodeMatcher} from "./nodes";
9 import {$getNearestBlockElementAncestorOrThrow} from "@lexical/utils";
10 import {$setBlocksType} from "@lexical/selection";
12 export function el(tag: string, attrs: Record<string, string|null> = {}, children: (string|HTMLElement)[] = []): HTMLElement {
13 const el = document.createElement(tag);
14 const attrKeys = Object.keys(attrs);
15 for (const attr of attrKeys) {
16 if (attrs[attr] !== null) {
17 el.setAttribute(attr, attrs[attr] as string);
21 for (const child of children) {
22 if (typeof child === 'string') {
23 el.append(document.createTextNode(child));
32 export function selectionContainsNodeType(selection: BaseSelection|null, matcher: LexicalNodeMatcher): boolean {
33 return getNodeFromSelection(selection, matcher) !== null;
36 export function getNodeFromSelection(selection: BaseSelection|null, matcher: LexicalNodeMatcher): LexicalNode|null {
41 for (const node of selection.getNodes()) {
46 for (const parent of node.getParents()) {
47 if (matcher(parent)) {
56 export function selectionContainsTextFormat(selection: BaseSelection|null, format: TextFormatType): boolean {
61 for (const node of selection.getNodes()) {
62 if ($isTextNode(node) && node.hasFormat(format)) {
70 export function toggleSelectionBlockNodeType(editor: LexicalEditor, matcher: LexicalNodeMatcher, creator: LexicalElementNodeCreator) {
72 const selection = $getSelection();
73 const blockElement = selection ? $getNearestBlockElementAncestorOrThrow(selection.getNodes()[0]) : null;
74 if (selection && matcher(blockElement)) {
75 $setBlocksType(selection, $createParagraphNode);
77 $setBlocksType(selection, creator);