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> = {}, children: (string|HTMLElement)[] = []): HTMLElement {
13 const el = document.createElement(tag);
14 const attrKeys = Object.keys(attrs);
15 for (const attr of attrKeys) {
16 el.setAttribute(attr, attrs[attr]);
19 for (const child of children) {
20 if (typeof child === 'string') {
21 el.append(document.createTextNode(child));
30 export function selectionContainsNodeType(selection: BaseSelection|null, matcher: LexicalNodeMatcher): boolean {
31 return getNodeFromSelection(selection, matcher) !== null;
34 export function getNodeFromSelection(selection: BaseSelection|null, matcher: LexicalNodeMatcher): LexicalNode|null {
39 for (const node of selection.getNodes()) {
44 for (const parent of node.getParents()) {
45 if (matcher(parent)) {
54 export function selectionContainsTextFormat(selection: BaseSelection|null, format: TextFormatType): boolean {
59 for (const node of selection.getNodes()) {
60 if ($isTextNode(node) && node.hasFormat(format)) {
68 export function toggleSelectionBlockNodeType(editor: LexicalEditor, matcher: LexicalNodeMatcher, creator: LexicalElementNodeCreator) {
70 const selection = $getSelection();
71 const blockElement = selection ? $getNearestBlockElementAncestorOrThrow(selection.getNodes()[0]) : null;
72 if (selection && matcher(blockElement)) {
73 $setBlocksType(selection, $createParagraphNode);
75 $setBlocksType(selection, creator);