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} from "./nodes";
20 import {$createCustomParagraphNode} from "../nodes/custom-paragraph";
22 const lastSelectionByEditor = new WeakMap<LexicalEditor, BaseSelection|null>;
24 export function getLastSelection(editor: LexicalEditor): BaseSelection|null {
25 return lastSelectionByEditor.get(editor) || null;
28 export function setLastSelection(editor: LexicalEditor, selection: BaseSelection|null): void {
29 lastSelectionByEditor.set(editor, selection);
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 const matchedParent = $getParentOfType(node, matcher);
55 export function $selectionContainsTextFormat(selection: BaseSelection | null, format: TextFormatType): boolean {
60 for (const node of selection.getNodes()) {
61 if ($isTextNode(node) && node.hasFormat(format)) {
69 export function $toggleSelectionBlockNodeType(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, $createCustomParagraphNode);
75 $setBlocksType(selection, creator);
79 export function $insertNewBlockNodeAtSelection(node: LexicalNode, insertAfter: boolean = true) {
80 $insertNewBlockNodesAtSelection([node], insertAfter);
83 export function $insertNewBlockNodesAtSelection(nodes: LexicalNode[], insertAfter: boolean = true) {
84 const selection = $getSelection();
85 const blockElement = selection ? $getNearestBlockElementAncestorOrThrow(selection.getNodes()[0]) : null;
89 for (let i = nodes.length - 1; i >= 0; i--) {
90 blockElement.insertAfter(nodes[i]);
93 for (const node of nodes) {
94 blockElement.insertBefore(node);
98 $getRoot().append(...nodes);
102 export function $selectSingleNode(node: LexicalNode) {
103 const nodeSelection = $createNodeSelection();
104 nodeSelection.add(node.getKey());
105 $setSelection(nodeSelection);
108 export function $selectionContainsNode(selection: BaseSelection | null, node: LexicalNode): boolean {
113 const key = node.getKey();
114 for (const node of selection.getNodes()) {
115 if (node.getKey() === key) {
123 export function $selectionContainsElementFormat(selection: BaseSelection | null, format: ElementFormatType): boolean {
124 const nodes = $getBlockElementNodesInSelection(selection);
125 for (const node of nodes) {
126 if (node.getFormatType() === format) {
134 export function $getBlockElementNodesInSelection(selection: BaseSelection | null): ElementNode[] {
139 const blockNodes: Map<string, ElementNode> = new Map();
140 for (const node of selection.getNodes()) {
141 const blockElement = $findMatchingParent(node, (node) => {
142 return $isElementNode(node) && !node.isInline();
143 }) as ElementNode | null;
146 blockNodes.set(blockElement.getKey(), blockElement);
150 return Array.from(blockNodes.values());