@atlaskit/editor-common
Version:
A package that contains common classes and components for editor and renderer
97 lines (96 loc) • 4.43 kB
TypeScript
import type { NodeRange, Node as PMNode, ResolvedPos } from '@atlaskit/editor-prosemirror/model';
import type { EditorState, Transaction } from '@atlaskit/editor-prosemirror/state';
import { type Selection } from '@atlaskit/editor-prosemirror/state';
export declare const isSelectionAtStartOfNode: ($pos: ResolvedPos, parentNode?: PMNode) => boolean;
export declare const isSelectionAtEndOfNode: ($pos: ResolvedPos, parentNode?: PMNode) => boolean;
export declare function atTheEndOfDoc(state: EditorState): boolean;
export declare function atTheBeginningOfDoc(state: EditorState): boolean;
export declare function atTheEndOfBlock(state: EditorState): boolean;
export declare function atTheBeginningOfBlock(state: EditorState): boolean;
export declare function selectionIsAtTheBeginningOfBlock(selection: Selection): boolean;
export declare function startPositionOfParent(resolvedPos: ResolvedPos): number;
export declare function endPositionOfParent(resolvedPos: ResolvedPos): number;
/**
*
* @param $anchor Resolved selection anchor position
* @param $head Resolved selection head position
* @returns An expanded selection encompassing surrounding nodes. Hoists up to the shared depth anchor/head depths differ.
*/
export declare const expandSelectionBounds: ($anchor: ResolvedPos, $head: ResolvedPos) => {
$anchor: ResolvedPos;
$head: ResolvedPos;
};
/**
* Delete what is selected in the given transaction.
* @param tr the transaction to delete the selection from
* @param selectionToUse optional selection to delete instead of the transaction's current selection
* @returns the updated transaction
*/
export declare const deleteSelectedRange: (tr: Transaction, selectionToUse?: Selection) => Transaction;
/**
* This expands the given $from and $to resolved positions to the block boundaries
* spanning all nodes in the range up to the nearest common ancestor.
*
* By default, it will further expand the range when encountering specific node types
* that require full block selection (like lists and tables). A custom predicate
* can be provided to modify this behavior.
*
* @param $from The resolved start position
* @param $to The resolved end position
* @param predicate A predicate to determine if parent node is acceptable (see prosemirror-model/blockRange)
* @returns An object containing the expanded $from and $to resolved positions
*/
export declare const expandToBlockRange: ($from: ResolvedPos, $to: ResolvedPos, predicate?: (node: PMNode) => boolean) => {
$from: ResolvedPos;
$to: ResolvedPos;
range?: undefined;
} | {
$from: ResolvedPos;
$to: ResolvedPos;
range: NodeRange;
};
/**
* Expands a given selection to a block range, considering specific node types that require expansion.
*
* E.g. if the selection starts/ends at list items or table cells, the selection will be expanded
* to encompass the entire list or table.
*
* Used mostly for block menu / drag handle related selections, where we want to ensure the selection
* being acted upon covers the entire block range selected by the user.
*
* @param selection The selection to expand
* @returns The expanded selection
*/
export declare const expandSelectionToBlockRange: ({ $from, $to, }: Selection) => {
$from: ResolvedPos;
$to: ResolvedPos;
range?: undefined;
} | {
$from: ResolvedPos;
$to: ResolvedPos;
range: NodeRange;
};
export declare const isMultiBlockRange: (range: NodeRange) => boolean;
/**
* Determines if a selection contains multiple block nodes.
*/
export declare function isMultiBlockSelection(selection: Selection): boolean;
/**
* Extracts the source nodes from a selection range.
*
* This function expands the given selection to its block range boundaries and returns
* an array of the nodes contained within that range. It handles special cases like
* list nodes, where the slice positions are adjusted to include the list wrapper.
*
* @param tr - The transaction containing the document
* @param selection - The selection to extract nodes from
* @returns An array of ProseMirror nodes within the expanded selection range
*
* @example
* ```typescript
* const selection = tr.selection;
* const nodes = getSourceNodesFromSelectionRange(tr, selection);
* // nodes will contain all block-level nodes in the selection
* ```
*/
export declare function getSourceNodesFromSelectionRange(tr: Transaction, selection: Selection): PMNode[];