Type Editor / @type-editor/tables / utils/query / findCellRange
Function: findCellRange()
ts
function findCellRange(
selection,
anchorHit?,
headHit?,
): [ResolvedPos, ResolvedPos];Defined in: tables/src/utils/query.ts:139
Finds the anchor and head cell positions for a table cell selection.
This function attempts to determine the cell range for a selection using the following strategy:
- If no hit points are provided and the selection is already a CellSelection, returns the existing anchor and head cells.
- Otherwise, uses the provided hit points (or falls back to the selection's anchor/head) to find the corresponding cells.
- Validates that both cells are in the same table before returning.
Parameters
| Parameter | Type | Description |
|---|---|---|
selection | PmSelection | The current editor selection. |
anchorHit? | number | Optional position to use as the anchor hit point. Falls back to headHit or selection.anchor if not provided. |
headHit? | number | Optional position to use as the head hit point. Falls back to anchorHit or selection.head if not provided. |
Returns
[ResolvedPos, ResolvedPos]
A tuple of [anchorCell, headCell] resolved positions if both cells are found in the same table, or null if no valid cell range can be determined.
Example
typescript
// Get cell range from existing cell selection
const range = findCellRange(state.selection);
// Get cell range using specific hit points
const range = findCellRange(state.selection, mouseDownPos, mouseMovePos);
if (range) {
const [$anchorCell, $headCell] = range;
// Create a new cell selection...
}