5 $getSelection, $isDecoratorNode,
9 BaseSelection, DecoratorNode,
11 ElementNode, LexicalEditor,
15 import {$findMatchingParent, $getNearestBlockElementAncestorOrThrow} from "@lexical/utils";
16 import {LexicalElementNodeCreator, LexicalNodeMatcher} from "../nodes";
17 import {$setBlocksType} from "@lexical/selection";
19 import {$getParentOfType, nodeHasAlignment} from "./nodes";
20 import {$createCustomParagraphNode} from "../nodes/custom-paragraph";
21 import {CommonBlockAlignment} from "../nodes/_common";
23 const lastSelectionByEditor = new WeakMap<LexicalEditor, BaseSelection|null>;
25 export function getLastSelection(editor: LexicalEditor): BaseSelection|null {
26 return lastSelectionByEditor.get(editor) || null;
29 export function setLastSelection(editor: LexicalEditor, selection: BaseSelection|null): void {
30 lastSelectionByEditor.set(editor, selection);
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 const matchedParent = $getParentOfType(node, matcher);
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(matcher: LexicalNodeMatcher, creator: LexicalElementNodeCreator) {
71 const selection = $getSelection();
72 const blockElement = selection ? $getNearestBlockElementAncestorOrThrow(selection.getNodes()[0]) : null;
73 if (selection && matcher(blockElement)) {
74 $setBlocksType(selection, $createCustomParagraphNode);
76 $setBlocksType(selection, creator);
80 export function $insertNewBlockNodeAtSelection(node: LexicalNode, insertAfter: boolean = true) {
81 $insertNewBlockNodesAtSelection([node], insertAfter);
84 export function $insertNewBlockNodesAtSelection(nodes: LexicalNode[], insertAfter: boolean = true) {
85 const selection = $getSelection();
86 const blockElement = selection ? $getNearestBlockElementAncestorOrThrow(selection.getNodes()[0]) : null;
90 for (let i = nodes.length - 1; i >= 0; i--) {
91 blockElement.insertAfter(nodes[i]);
94 for (const node of nodes) {
95 blockElement.insertBefore(node);
99 $getRoot().append(...nodes);
103 export function $selectSingleNode(node: LexicalNode) {
104 const nodeSelection = $createNodeSelection();
105 nodeSelection.add(node.getKey());
106 $setSelection(nodeSelection);
109 export function $selectionContainsNode(selection: BaseSelection | null, node: LexicalNode): boolean {
114 const key = node.getKey();
115 for (const node of selection.getNodes()) {
116 if (node.getKey() === key) {
124 export function $selectionContainsAlignment(selection: BaseSelection | null, alignment: CommonBlockAlignment): boolean {
125 const nodes = $getBlockElementNodesInSelection(selection);
126 for (const node of nodes) {
127 if (nodeHasAlignment(node) && node.getAlignment() === alignment) {
135 export function $getBlockElementNodesInSelection(selection: BaseSelection | null): ElementNode[] {
140 const blockNodes: Map<string, ElementNode> = new Map();
141 for (const node of selection.getNodes()) {
142 const blockElement = $findMatchingParent(node, (node) => {
143 return $isElementNode(node) && !node.isInline();
144 }) as ElementNode | null;
147 blockNodes.set(blockElement.getKey(), blockElement);
151 return Array.from(blockNodes.values());
154 export function $getDecoratorNodesInSelection(selection: BaseSelection | null): DecoratorNode<any>[] {
159 return selection.getNodes().filter(node => $isDecoratorNode(node));