Type Editor / @type-editor/util / find-parent / findParent
Function: findParent()
ts
function findParent(selection, predicate): FindParentResult;Defined in: find-parent.ts:24
Finds the nearest ancestor node in the document tree that satisfies the given predicate.
The search starts from the common parent of the selection and traverses upward through the document hierarchy until a matching node is found or the root is reached.
Parameters
| Parameter | Type | Description |
|---|---|---|
selection | PmSelection | The current editor selection to search from. |
predicate | FindCallbackFunction | A callback function that tests each ancestor node. Should return true when the desired node is found. |
Returns
The matching parent node with its resolved position, or null if no match is found.
Example
typescript
// Find the nearest list item ancestor
const listItem = findParent(
selection,
(node) => node.type.name === "list_item",
);