]> BookStack Code Mirror - bookstack/blobdiff - resources/js/wysiwyg/helpers.ts
Lexical: Added button icon system
[bookstack] / resources / js / wysiwyg / helpers.ts
index 737666ffa91037b533530ecd6ac4f11f2d2b1272..d7cd23a359c8edc92e3a3b36e963310e56d7beb2 100644 (file)
@@ -3,32 +3,54 @@ import {
     $getSelection,
     $isTextNode,
     BaseSelection,
-    ElementFormatType,
-    LexicalEditor, TextFormatType
+    LexicalEditor, LexicalNode, TextFormatType
 } from "lexical";
 import {LexicalElementNodeCreator, LexicalNodeMatcher} from "./nodes";
 import {$getNearestBlockElementAncestorOrThrow} from "@lexical/utils";
 import {$setBlocksType} from "@lexical/selection";
-import {TextNodeThemeClasses} from "lexical/LexicalEditor";
+
+export function el(tag: string, attrs: Record<string, string|null> = {}, children: (string|HTMLElement)[] = []): HTMLElement {
+    const el = document.createElement(tag);
+    const attrKeys = Object.keys(attrs);
+    for (const attr of attrKeys) {
+        if (attrs[attr] !== null) {
+            el.setAttribute(attr, attrs[attr] as string);
+        }
+    }
+
+    for (const child of children) {
+        if (typeof child === 'string') {
+            el.append(document.createTextNode(child));
+        } else {
+            el.append(child);
+        }
+    }
+
+    return el;
+}
 
 export function selectionContainsNodeType(selection: BaseSelection|null, matcher: LexicalNodeMatcher): boolean {
+    return getNodeFromSelection(selection, matcher) !== null;
+}
+
+export function getNodeFromSelection(selection: BaseSelection|null, matcher: LexicalNodeMatcher): LexicalNode|null {
     if (!selection) {
-        return false;
+        return null;
     }
 
     for (const node of selection.getNodes()) {
         if (matcher(node)) {
-            return true;
+            return node;
         }
 
         for (const parent of node.getParents()) {
             if (matcher(parent)) {
-                return true;
+                return parent;
             }
         }
     }
 
-    return false;
+    return null;
 }
 
 export function selectionContainsTextFormat(selection: BaseSelection|null, format: TextFormatType): boolean {