@portabletext/editor
Version:
Portable Text Editor made in React
235 lines • 7.58 kB
TypeScript
import { D as Path, a as Containers, n as TraversalSnapshot, r as Node } from "../_chunks-dts/resolve-containers.js";
import { PortableTextBlock, PortableTextSpan, PortableTextTextBlock, Schema } from "@portabletext/schema";
/**
* Find the first ancestor of the node at a given path that matches a predicate.
* Does not check the node at the path itself, only its ancestors.
*
* When `match` is a type predicate, the returned `node` narrows to that type.
*
* @beta
*/
declare function getAncestor<TMatch extends Node>(snapshot: TraversalSnapshot, path: Path, match: (node: Node, path: Path) => node is TMatch): {
node: TMatch;
path: Path;
} | undefined;
/**
* @beta
*/
declare function getAncestor(snapshot: TraversalSnapshot, path: Path, match: (node: Node, path: Path) => boolean): {
node: Node;
path: Path;
} | undefined;
/**
* Get all ancestors of the node at a given path, from nearest to furthest.
*
* For a path like [{_key:'t1'}, 'rows', {_key:'r1'}, 'cells', {_key:'c1'}],
* the ancestors are (nearest first):
* [{_key:'t1'}, 'rows', {_key:'r1'}]
* [{_key:'t1'}]
*
* Walks from root to the target in a single pass collecting each ancestor
* as it goes.
*
* @beta
*/
declare function getAncestors(snapshot: TraversalSnapshot, path: Path): Array<{
node: Node;
path: Path;
}>;
/**
* Determine if a node at the given path is a block.
*
* A node is a block if its parent is not a text block. Top-level nodes
* (direct children of the editor) are always blocks. Children of text blocks
* (spans and inline objects) are not blocks. Children of containers are
* blocks within that container.
*
* @beta
*/
declare function isBlock(snapshot: TraversalSnapshot, path: Path): boolean;
/**
* Get the node at the given path if it is a block.
*
* Returns the node narrowed to PortableTextBlock, or undefined if the node
* doesn't exist or is not a block.
*
* @beta
*/
declare function getBlock(snapshot: TraversalSnapshot, path: Path): {
node: PortableTextBlock;
path: Path;
} | undefined;
/**
* Get the children of a node at a given path.
*
* @beta
*/
declare function getChildren(snapshot: TraversalSnapshot, path: Path): Array<{
node: Node;
path: Path;
}>;
/**
* Walk up from a path to find the nearest enclosing block.
*
* Returns the node at the path if it is a block, otherwise the first ancestor
* that is a block. Works at any depth — inside a container this returns the
* container-internal block, not the outer container.
*
* @beta
*/
declare function getEnclosingBlock(snapshot: TraversalSnapshot, path: Path): {
node: PortableTextBlock;
path: Path;
} | undefined;
/**
* Get the first child of a node at a given path.
*
* @beta
*/
declare function getFirstChild(snapshot: TraversalSnapshot, path: Path): {
node: Node;
path: Path;
} | undefined;
/**
* Get the last child of a node at a given path.
*
* @beta
*/
declare function getLastChild(snapshot: TraversalSnapshot, path: Path): {
node: Node;
path: Path;
} | undefined;
/**
* Get the deepest leaf node starting from a path, walking toward either the
* start or end edge. A leaf is any node that has no children according to the
* traversal context.
*
* @beta
*/
declare function getLeaf(snapshot: TraversalSnapshot, path: Path, options: {
edge: 'start' | 'end';
}): {
node: Node;
path: Path;
} | undefined;
/**
* Get the node at a given path.
*
* The path can be either keyed (KeyedSegment + field name strings) or
* indexed (numbers). Keyed segments are resolved by matching `_key`,
* field name strings are skipped (they're structural), and numbers
* are resolved by index.
*
* The returned path is always fully keyed, even if the input path
* contained numeric indices.
*
* @beta
*/
declare function getNode(snapshot: TraversalSnapshot, path: Path): {
node: Node;
path: Path;
} | undefined;
/**
* Get the parent of a node at a given path.
*
* @beta
*/
declare function getParent(snapshot: TraversalSnapshot, path: Path): {
node: Node;
path: Path;
} | undefined;
/**
* Return the `Schema` view that applies at a given path.
*
* For paths at the root of the document, or for paths where no ancestor is
* a registered container, returns the top-level schema. For paths inside a
* container, walks ancestors to find the nearest container and returns the
* sub-schema derived from its `of` declaration.
*
* @beta
*/
declare function getPathSubSchema(snapshot: TraversalSnapshot, path: Path): Schema;
/**
* Get the next or previous sibling of the node at a given path.
*
* @beta
*/
declare function getSibling(snapshot: TraversalSnapshot, path: Path, direction: 'next' | 'previous'): {
node: Node;
path: Path;
} | undefined;
/**
* Get the span node at a given path.
*
* @beta
*/
declare function getSpanNode(snapshot: TraversalSnapshot, path: Path): {
node: PortableTextSpan;
path: Path;
} | undefined;
/**
* Get the concatenated text content of the node at a given path.
*
* @beta
*/
declare function getText(snapshot: TraversalSnapshot, path: Path): string | undefined;
/**
* Get the text block node at a given path.
*
* @beta
*/
declare function getTextBlockNode(snapshot: TraversalSnapshot, path: Path): {
node: PortableTextTextBlock;
path: Path;
} | undefined;
/**
* Return a `Schema` that contains every named member declared anywhere
* in the editor's schema graph that is reachable from a position where text
* is edited - the root schema merged with the sub-schema of every registered
* container whose field accepts text blocks, deduped by name. Useful for
* rendering a static toolbar whose buttons stay stable across selection
* moves while still reflecting everything that could plausibly be edited or
* inserted somewhere.
*
* Containers whose field does NOT accept text blocks (e.g. a `table`
* container whose `rows` field only accepts `row` objects, or a `row`
* container whose `cells` field only accepts `cell` objects) are
* **structural**: their immediate `of` types are organizational, not
* insertable user content. Those structural types are excluded from the
* union. Their nested text-block-accepting descendants (e.g. a `cell`
* that contains a `content` field of `{type: 'block'}`) are reached via
* those descendants' own container registration.
*
* Pair with `getPathSubSchema` (or a path-based intersection across a
* range) to determine which of the union's members are applicable at the
* current selection.
*
* @beta
*/
declare function getUnionSchema(schema: Schema, containers: Containers): Schema;
/**
* Check if a node exists at a given path.
*
* @beta
*/
declare function hasNode(snapshot: TraversalSnapshot, path: Path): boolean;
/**
* Determine if a node at the given path is inline.
*
* A node is inline if its parent is a text block. This is the inverse of
* `isBlock`. Top-level nodes are never inline.
*
* @beta
*/
declare function isInline(snapshot: TraversalSnapshot, path: Path): boolean;
/**
* Determine if a node at the given path is a leaf.
*
* A leaf node cannot have children. Spans and non-editable object nodes are
* leaves. Text blocks and editable container objects are not.
*
* @beta
*/
declare function isLeaf(snapshot: TraversalSnapshot, path: Path): boolean;
export { getAncestor, getAncestors, getBlock, getChildren, getEnclosingBlock, getFirstChild, getLastChild, getLeaf, getNode, getParent, getPathSubSchema, getSibling, getSpanNode, getText, getTextBlockNode, getUnionSchema, hasNode, isBlock, isInline, isLeaf };
//# sourceMappingURL=index.d.ts.map