3 $createParagraphNode, $createRangeSelection,
5 $getSelection, $isBlockElementNode, $isDecoratorNode,
9 BaseSelection, DecoratorNode,
10 ElementNode, LexicalEditor,
12 TextFormatType, TextNode
14 import {$getNearestBlockElementAncestorOrThrow} from "@lexical/utils";
15 import {LexicalElementNodeCreator, LexicalNodeMatcher} from "../nodes";
16 import {$setBlocksType} from "@lexical/selection";
18 import {$getNearestNodeBlockParent, $getParentOfType, nodeHasAlignment} from "./nodes";
19 import {CommonBlockAlignment} from "../nodes/_common";
21 const lastSelectionByEditor = new WeakMap<LexicalEditor, BaseSelection|null>;
23 export function getLastSelection(editor: LexicalEditor): BaseSelection|null {
24 return lastSelectionByEditor.get(editor) || null;
27 export function setLastSelection(editor: LexicalEditor, selection: BaseSelection|null): void {
28 lastSelectionByEditor.set(editor, selection);
31 export function $selectionContainsNodeType(selection: BaseSelection | null, matcher: LexicalNodeMatcher): boolean {
32 return $getNodeFromSelection(selection, matcher) !== null;
35 export function $getNodeFromSelection(selection: BaseSelection | null, matcher: LexicalNodeMatcher): LexicalNode | null {
40 for (const node of selection.getNodes()) {
45 const matchedParent = $getParentOfType(node, matcher);
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(matcher: LexicalNodeMatcher, creator: LexicalElementNodeCreator) {
69 const selection = $getSelection();
70 const blockElement = selection ? $getNearestBlockElementAncestorOrThrow(selection.getNodes()[0]) : null;
71 if (selection && matcher(blockElement)) {
72 $setBlocksType(selection, $createParagraphNode);
74 $setBlocksType(selection, creator);
78 export function $insertNewBlockNodeAtSelection(node: LexicalNode, insertAfter: boolean = true) {
79 $insertNewBlockNodesAtSelection([node], insertAfter);
82 export function $insertNewBlockNodesAtSelection(nodes: LexicalNode[], insertAfter: boolean = true) {
83 const selectionNodes = $getSelection()?.getNodes() || [];
84 const blockElement = selectionNodes.length > 0 ? $getNearestNodeBlockParent(selectionNodes[0]) : null;
88 for (let i = nodes.length - 1; i >= 0; i--) {
89 blockElement.insertAfter(nodes[i]);
92 for (const node of nodes) {
93 blockElement.insertBefore(node);
97 $getRoot().append(...nodes);
101 export function $selectSingleNode(node: LexicalNode) {
102 const nodeSelection = $createNodeSelection();
103 nodeSelection.add(node.getKey());
104 $setSelection(nodeSelection);
107 function getFirstTextNodeInNodes(nodes: LexicalNode[]): TextNode|null {
108 for (const node of nodes) {
109 if ($isTextNode(node)) {
113 if ($isElementNode(node)) {
114 const children = node.getChildren();
115 const textNode = getFirstTextNodeInNodes(children);
116 if (textNode !== null) {
125 function getLastTextNodeInNodes(nodes: LexicalNode[]): TextNode|null {
126 const revNodes = [...nodes].reverse();
127 for (const node of revNodes) {
128 if ($isTextNode(node)) {
132 if ($isElementNode(node)) {
133 const children = [...node.getChildren()].reverse();
134 const textNode = getLastTextNodeInNodes(children);
135 if (textNode !== null) {
144 export function $selectNodes(nodes: LexicalNode[]) {
145 if (nodes.length === 0) {
149 const selection = $createRangeSelection();
150 const firstText = getFirstTextNodeInNodes(nodes);
151 const lastText = getLastTextNodeInNodes(nodes);
152 if (firstText && lastText) {
153 selection.setTextNodeRange(firstText, 0, lastText, lastText.getTextContentSize() || 0)
154 $setSelection(selection);
158 export function $toggleSelection(editor: LexicalEditor) {
159 const lastSelection = getLastSelection(editor);
162 window.requestAnimationFrame(() => {
163 editor.update(() => {
164 $setSelection(lastSelection.clone());
170 export function $selectionContainsNode(selection: BaseSelection | null, node: LexicalNode): boolean {
175 const key = node.getKey();
176 for (const node of selection.getNodes()) {
177 if (node.getKey() === key) {
185 export function $selectionContainsAlignment(selection: BaseSelection | null, alignment: CommonBlockAlignment): boolean {
188 ...(selection?.getNodes() || []),
189 ...$getBlockElementNodesInSelection(selection)
191 for (const node of nodes) {
192 if (nodeHasAlignment(node) && node.getAlignment() === alignment) {
200 export function $selectionContainsDirection(selection: BaseSelection | null, direction: 'rtl'|'ltr'): boolean {
203 ...(selection?.getNodes() || []),
204 ...$getBlockElementNodesInSelection(selection)
207 for (const node of nodes) {
208 if ($isBlockElementNode(node) && node.getDirection() === direction) {
216 export function $getBlockElementNodesInSelection(selection: BaseSelection | null): ElementNode[] {
221 const blockNodes: Map<string, ElementNode> = new Map();
222 for (const node of selection.getNodes()) {
223 const blockElement = $getNearestNodeBlockParent(node);
224 if ($isElementNode(blockElement)) {
225 blockNodes.set(blockElement.getKey(), blockElement);
229 return Array.from(blockNodes.values());
232 export function $getDecoratorNodesInSelection(selection: BaseSelection | null): DecoratorNode<any>[] {
237 return selection.getNodes().filter(node => $isDecoratorNode(node));