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 "lexical/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 $getTextNodeFromSelection(selection: BaseSelection | null): TextNode|null {
55 return $getNodeFromSelection(selection, $isTextNode) as TextNode|null;
58 export function $selectionContainsTextFormat(selection: BaseSelection | null, format: TextFormatType): boolean {
63 for (const node of selection.getNodes()) {
64 if ($isTextNode(node) && node.hasFormat(format)) {
72 export function $toggleSelectionBlockNodeType(matcher: LexicalNodeMatcher, creator: LexicalElementNodeCreator) {
73 const selection = $getSelection();
74 const blockElement = selection ? $getNearestBlockElementAncestorOrThrow(selection.getNodes()[0]) : null;
75 if (selection && matcher(blockElement)) {
76 $setBlocksType(selection, $createParagraphNode);
78 $setBlocksType(selection, creator);
82 export function $insertNewBlockNodeAtSelection(node: LexicalNode, insertAfter: boolean = true) {
83 $insertNewBlockNodesAtSelection([node], insertAfter);
86 export function $insertNewBlockNodesAtSelection(nodes: LexicalNode[], insertAfter: boolean = true) {
87 const selectionNodes = $getSelection()?.getNodes() || [];
88 const blockElement = selectionNodes.length > 0 ? $getNearestNodeBlockParent(selectionNodes[0]) : null;
92 for (let i = nodes.length - 1; i >= 0; i--) {
93 blockElement.insertAfter(nodes[i]);
96 for (const node of nodes) {
97 blockElement.insertBefore(node);
101 $getRoot().append(...nodes);
105 export function $selectSingleNode(node: LexicalNode) {
106 const nodeSelection = $createNodeSelection();
107 nodeSelection.add(node.getKey());
108 $setSelection(nodeSelection);
111 function getFirstTextNodeInNodes(nodes: LexicalNode[]): TextNode|null {
112 for (const node of nodes) {
113 if ($isTextNode(node)) {
117 if ($isElementNode(node)) {
118 const children = node.getChildren();
119 const textNode = getFirstTextNodeInNodes(children);
120 if (textNode !== null) {
129 function getLastTextNodeInNodes(nodes: LexicalNode[]): TextNode|null {
130 const revNodes = [...nodes].reverse();
131 for (const node of revNodes) {
132 if ($isTextNode(node)) {
136 if ($isElementNode(node)) {
137 const children = [...node.getChildren()].reverse();
138 const textNode = getLastTextNodeInNodes(children);
139 if (textNode !== null) {
148 export function $selectNodes(nodes: LexicalNode[]) {
149 if (nodes.length === 0) {
153 const selection = $createRangeSelection();
154 const firstText = getFirstTextNodeInNodes(nodes);
155 const lastText = getLastTextNodeInNodes(nodes);
156 if (firstText && lastText) {
157 selection.setTextNodeRange(firstText, 0, lastText, lastText.getTextContentSize() || 0)
158 $setSelection(selection);
162 export function $toggleSelection(editor: LexicalEditor) {
163 const lastSelection = getLastSelection(editor);
166 window.requestAnimationFrame(() => {
167 editor.update(() => {
168 $setSelection(lastSelection.clone());
174 export function $selectionContainsNode(selection: BaseSelection | null, node: LexicalNode): boolean {
179 const key = node.getKey();
180 for (const node of selection.getNodes()) {
181 if (node.getKey() === key) {
189 export function $selectionContainsAlignment(selection: BaseSelection | null, alignment: CommonBlockAlignment): boolean {
192 ...(selection?.getNodes() || []),
193 ...$getBlockElementNodesInSelection(selection)
195 for (const node of nodes) {
196 if (nodeHasAlignment(node) && node.getAlignment() === alignment) {
204 export function $selectionContainsDirection(selection: BaseSelection | null, direction: 'rtl'|'ltr'): boolean {
207 ...(selection?.getNodes() || []),
208 ...$getBlockElementNodesInSelection(selection)
211 for (const node of nodes) {
212 if ($isBlockElementNode(node) && node.getDirection() === direction) {
220 export function $getBlockElementNodesInSelection(selection: BaseSelection | null): ElementNode[] {
225 const blockNodes: Map<string, ElementNode> = new Map();
226 for (const node of selection.getNodes()) {
227 const blockElement = $getNearestNodeBlockParent(node);
228 if ($isElementNode(blockElement)) {
229 blockNodes.set(blockElement.getKey(), blockElement);
233 return Array.from(blockNodes.values());
236 export function $getDecoratorNodesInSelection(selection: BaseSelection | null): DecoratorNode<any>[] {
241 return selection.getNodes().filter(node => $isDecoratorNode(node));