X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/efec752985465227958c48eb5fb90faf95b66fb3..refs/pull/5313/head:/resources/js/wysiwyg/utils/nodes.ts diff --git a/resources/js/wysiwyg/utils/nodes.ts b/resources/js/wysiwyg/utils/nodes.ts index 8e6c66610..2dd99d369 100644 --- a/resources/js/wysiwyg/utils/nodes.ts +++ b/resources/js/wysiwyg/utils/nodes.ts @@ -1,8 +1,18 @@ -import {$getRoot, $isTextNode, LexicalEditor, LexicalNode} from "lexical"; +import { + $getRoot, + $isDecoratorNode, + $isElementNode, $isRootNode, + $isTextNode, + ElementNode, + LexicalEditor, + LexicalNode +} from "lexical"; import {LexicalNodeMatcher} from "../nodes"; import {$createCustomParagraphNode} from "../nodes/custom-paragraph"; import {$generateNodesFromDOM} from "@lexical/html"; import {htmlToDom} from "./dom"; +import {NodeHasAlignment, NodeHasInset} from "../nodes/_common"; +import {$findMatchingParent} from "@lexical/utils"; function wrapTextNodes(nodes: LexicalNode[]): LexicalNode[] { return nodes.map(node => { @@ -31,6 +41,26 @@ export function $getParentOfType(node: LexicalNode, matcher: LexicalNodeMatcher) return null; } +export function $getAllNodesOfType(matcher: LexicalNodeMatcher, root?: ElementNode): LexicalNode[] { + if (!root) { + root = $getRoot(); + } + + const matches = []; + + for (const child of root.getChildren()) { + if (matcher(child)) { + matches.push(child); + } + + if ($isElementNode(child)) { + matches.push(...$getAllNodesOfType(matcher, child)); + } + } + + return matches; +} + /** * Get the nearest root/block level node for the given position. */ @@ -50,4 +80,24 @@ export function $getNearestBlockNodeForCoords(editor: LexicalEditor, x: number, } return null; +} + +export function $getNearestNodeBlockParent(node: LexicalNode): LexicalNode|null { + const isBlockNode = (node: LexicalNode): boolean => { + return ($isElementNode(node) || $isDecoratorNode(node)) && !node.isInline() && !$isRootNode(node); + }; + + if (isBlockNode(node)) { + return node; + } + + return $findMatchingParent(node, isBlockNode); +} + +export function nodeHasAlignment(node: object): node is NodeHasAlignment { + return '__alignment' in node; +} + +export function nodeHasInset(node: object): node is NodeHasInset { + return '__inset' in node; } \ No newline at end of file