$createNodeSelection,
$createParagraphNode, $createRangeSelection,
$getRoot,
- $getSelection, $isDecoratorNode,
+ $getSelection, $isBlockElementNode, $isDecoratorNode,
$isElementNode,
$isTextNode,
$setSelection,
ElementFormatType,
ElementNode, LexicalEditor,
LexicalNode,
- TextFormatType
+ TextFormatType, TextNode
} from "lexical";
import {$findMatchingParent, $getNearestBlockElementAncestorOrThrow} from "@lexical/utils";
import {LexicalElementNodeCreator, LexicalNodeMatcher} from "../nodes";
}
export function $insertNewBlockNodesAtSelection(nodes: LexicalNode[], insertAfter: boolean = true) {
- const selection = $getSelection();
- const blockElement = selection ? $getNearestBlockElementAncestorOrThrow(selection.getNodes()[0]) : null;
+ const selectionNodes = $getSelection()?.getNodes() || [];
+ const blockElement = selectionNodes.length > 0 ? $getNearestNodeBlockParent(selectionNodes[0]) : null;
if (blockElement) {
if (insertAfter) {
$setSelection(nodeSelection);
}
+function getFirstTextNodeInNodes(nodes: LexicalNode[]): TextNode|null {
+ for (const node of nodes) {
+ if ($isTextNode(node)) {
+ return node;
+ }
+
+ if ($isElementNode(node)) {
+ const children = node.getChildren();
+ const textNode = getFirstTextNodeInNodes(children);
+ if (textNode !== null) {
+ return textNode;
+ }
+ }
+ }
+
+ return null;
+}
+
+function getLastTextNodeInNodes(nodes: LexicalNode[]): TextNode|null {
+ const revNodes = [...nodes].reverse();
+ for (const node of revNodes) {
+ if ($isTextNode(node)) {
+ return node;
+ }
+
+ if ($isElementNode(node)) {
+ const children = [...node.getChildren()].reverse();
+ const textNode = getLastTextNodeInNodes(children);
+ if (textNode !== null) {
+ return textNode;
+ }
+ }
+ }
+
+ return null;
+}
+
+export function $selectNodes(nodes: LexicalNode[]) {
+ if (nodes.length === 0) {
+ return;
+ }
+
+ const selection = $createRangeSelection();
+ const firstText = getFirstTextNodeInNodes(nodes);
+ const lastText = getLastTextNodeInNodes(nodes);
+ if (firstText && lastText) {
+ selection.setTextNodeRange(firstText, 0, lastText, lastText.getTextContentSize() || 0)
+ $setSelection(selection);
+ }
+}
+
export function $toggleSelection(editor: LexicalEditor) {
const lastSelection = getLastSelection(editor);
return false;
}
+export function $selectionContainsDirection(selection: BaseSelection | null, direction: 'rtl'|'ltr'): boolean {
+
+ const nodes = [
+ ...(selection?.getNodes() || []),
+ ...$getBlockElementNodesInSelection(selection)
+ ];
+
+ for (const node of nodes) {
+ if ($isBlockElementNode(node) && node.getDirection() === direction) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
export function $getBlockElementNodesInSelection(selection: BaseSelection | null): ElementNode[] {
if (!selection) {
return [];