@portabletext/editor 7.0.6 → 7.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"get-path-sub-schema.js","sources":["../../src/node-traversal/has-node.ts","../../src/engine/node/is-text-block-node.ts","../../src/schema/resolve-container-at.ts","../../src/engine/path/is-ancestor-path.ts","../../src/node-traversal/get-nodes.ts","../../src/engine/path/parent-path.ts","../../src/node-traversal/get-sibling.ts","../../src/engine/node/is-span-node.ts","../../src/node-traversal/get-parent.ts","../../src/node-traversal/is-block.ts","../../src/node-traversal/is-inline.ts","../../src/node-traversal/get-enclosing-block.ts","../../src/schema/descend-to-parent.ts","../../src/schema/get-enclosing-container.ts","../../src/traversal/get-path-sub-schema.ts"],"sourcesContent":["import type {Path} from '../engine/interfaces/path'\nimport {getNode} from './get-node'\nimport type {TraversalSnapshot} from './traversal-snapshot'\n\n/**\n * Check if a node exists at a given path.\n *\n * @beta\n */\nexport function hasNode(snapshot: TraversalSnapshot, path: Path): boolean {\n return getNode(snapshot, path) !== undefined\n}\n","import type {PortableTextObject, PortableTextSpan} from '@portabletext/schema'\nimport type {EditorSchema} from '../../editor/editor-schema'\nimport {isTypedObject} from '../../utils/asserters'\n\ntype TextBlockNode = {\n _type: string\n _key: string\n children?: Array<PortableTextSpan | PortableTextObject>\n markDefs?: Array<PortableTextObject>\n style?: string\n listItem?: string\n level?: number\n}\n\n/**\n * Checks if a node is a text block based on `_type` alone, without requiring\n * `children` to be present. This is needed to identify text blocks before\n * normalization has had a chance to add the missing `children` property.\n */\nexport function isTextBlockNode(\n context: {schema: EditorSchema},\n node: unknown,\n): node is TextBlockNode {\n return isTypedObject(node) && node._type === context.schema.block.name\n}\n","import type {Node} from '../engine/interfaces/node'\nimport type {Path} from '../engine/interfaces/path'\nimport {isKeyedSegment} from '../utils/util.is-keyed-segment'\nimport type {\n Containers,\n RegisteredContainer,\n RegisteredPositional,\n} from './container-types'\n\n/**\n * Walk the editor value following `path` and return the\n * {@link RegisteredContainer} or {@link RegisteredPositional} that applies\n * at `path`'s target position.\n *\n * Resolution rules at each step:\n *\n * 1. **Positional override.** If the current parent declares the\n * child's `_type` in its `of`, the positional entry wins.\n * Used to resolve same-`_type` registered under different\n * parents with different `field` values.\n *\n * 2. **Global fallback.** If the parent has no positional override,\n * fall back to the top-level entry for `_type` in\n * `containers`.\n *\n * 3. **Chain validity.** If any ancestor along the path has no\n * resolved container entry (unregistered or not reachable as a\n * container at its position), return `undefined`.\n *\n * Returns `undefined` when the target's `_type` is not registered\n * at this position. Returns a {@link RegisteredPositional} when the target\n * resolves to a leaf in a positional `of` (terminal node with no\n * editable children).\n *\n * @alpha\n */\nexport function resolveContainerAt(\n containers: Containers,\n value: ReadonlyArray<Node>,\n path: Path,\n): RegisteredContainer | RegisteredPositional | undefined {\n const keyedIndices: Array<number> = []\n for (let index = 0; index < path.length; index++) {\n if (isKeyedSegment(path[index])) {\n keyedIndices.push(index)\n }\n }\n if (keyedIndices.length === 0) {\n return undefined\n }\n\n let currentChildren: ReadonlyArray<Node> = value\n let parent: RegisteredContainer | undefined\n let resolved: RegisteredContainer | RegisteredPositional | undefined\n const targetKeyedIndex = keyedIndices[keyedIndices.length - 1]!\n\n let segmentIndex = 0\n while (segmentIndex <= targetKeyedIndex) {\n const segment = path[segmentIndex]!\n if (typeof segment === 'string') {\n segmentIndex++\n continue\n }\n\n let node: Node | undefined\n if (isKeyedSegment(segment)) {\n node = currentChildren.find((child) => child._key === segment._key)\n } else if (typeof segment === 'number') {\n node = currentChildren.at(segment)\n } else {\n return undefined\n }\n if (!node) {\n return undefined\n }\n\n resolved = resolveNodeEntry(containers, parent, node)\n if (!resolved) {\n return undefined\n }\n\n if (segmentIndex < targetKeyedIndex) {\n // Walk one more level. The resolved entry must be a container\n // (have children) for descent to continue.\n if (!('field' in resolved)) {\n return undefined\n }\n const fieldValue = (node as Record<string, unknown>)[resolved.field.name]\n if (!Array.isArray(fieldValue)) {\n return undefined\n }\n parent = resolved\n currentChildren = fieldValue as Array<Node>\n }\n segmentIndex++\n }\n\n return resolved\n}\n\nfunction resolveNodeEntry(\n containers: Containers,\n parent: RegisteredContainer | undefined,\n node: Node,\n): RegisteredContainer | RegisteredPositional | undefined {\n if (parent?.of) {\n for (const entry of parent.of) {\n if (entry.type === node._type) {\n return entry\n }\n }\n }\n return containers.get(node._type)\n}\n","import {isKeyedSegment} from '../../utils/util.is-keyed-segment'\nimport type {Path} from '../interfaces/path'\n\nexport function isAncestorPath(path: Path, another: Path): boolean {\n if (path.length >= another.length) {\n return false\n }\n\n for (let i = 0; i < path.length; i++) {\n const segment = path[i]\n const otherSegment = another[i]\n\n if (isKeyedSegment(segment) && isKeyedSegment(otherSegment)) {\n if (segment._key !== otherSegment._key) {\n return false\n }\n } else if (segment !== otherSegment) {\n return false\n }\n }\n\n return true\n}\n","import type {EditorSchema} from '../editor/editor-schema'\nimport type {Node} from '../engine/interfaces/node'\nimport type {Path} from '../engine/interfaces/path'\nimport {isAncestorPath} from '../engine/path/is-ancestor-path'\nimport type {\n Containers,\n RegisteredContainer,\n} from '../schema/resolve-containers'\nimport {isKeyedSegment} from '../utils/util.is-keyed-segment'\nimport {getChildren, getNodeChildren} from './get-children'\nimport type {TraversalSnapshot} from './traversal-snapshot'\n\n/**\n * Get the descendant nodes of the node at a given path.\n *\n * When `from` and `to` are provided, performs a range-bounded DFS traversal,\n * yielding only nodes between `from` and `to` (inclusive). Both paths are\n * always in document order: `from` is the earlier path, `to` is the later\n * path. The `reverse` flag controls iteration direction within that range.\n *\n * When `match` is provided, only yields nodes where the predicate returns true.\n * The traversal still visits all nodes in range - `match` is a filter, not a\n * traversal control.\n *\n * When `at` is provided, traverses descendants of the node at that path\n * instead of the root.\n */\nexport function* getNodes(\n snapshot: TraversalSnapshot,\n options: {\n at?: Path\n from?: Path\n to?: Path\n match?: (node: Node, path: Path) => boolean\n reverse?: boolean\n } = {},\n): Generator<{node: Node; path: Path}, void, undefined> {\n const {at = [], from, to, match, reverse = false} = options\n\n if (from === undefined && to === undefined) {\n yield* getNodesSimple(snapshot, at, {match, reverse})\n return\n }\n\n yield* getNodesInRange(snapshot, at, {from, to, match, reverse})\n}\n\n/**\n * Get descendant nodes of a standalone node (not in the editor tree).\n * Used for cases like getDirtyPaths where the node hasn't been inserted yet.\n */\nexport function* getNodeDescendants(\n context: {\n schema: EditorSchema\n containers: Containers\n },\n node: Node | {value: Array<Node>},\n): Generator<{node: Node; path: Path}, void, undefined> {\n // The editor root wrapper ({value: [...]}) is not a real node, so its field\n // name is not part of paths. For standalone nodes (a real {_key, _type, ...}\n // passed in by callers like getDirtyPaths), the field name IS part of the\n // path.\n const isRoot = !('_key' in node) && !('_type' in node)\n yield* walkStandalone(context, node, [], isRoot)\n}\n\nfunction* walkStandalone(\n context: {\n schema: EditorSchema\n containers: Containers\n },\n node: Node | {value: Array<Node>},\n path: Path,\n isRoot: boolean,\n parent?: RegisteredContainer,\n): Generator<{node: Node; path: Path}, void, undefined> {\n const next = getNodeChildren(context, node, parent)\n if (!next) {\n return\n }\n\n for (const child of next.children) {\n const childPath: Path = isRoot\n ? [{_key: child._key}]\n : [...path, next.fieldName, {_key: child._key}]\n yield {node: child, path: childPath}\n yield* walkStandalone(context, child, childPath, false, next.parent)\n }\n}\n\n/**\n * Simple recursive DFS - the original behavior.\n * Yields all descendants of the node at `path`.\n */\nfunction* getNodesSimple(\n snapshot: TraversalSnapshot,\n path: Path,\n options: {\n match?: (node: Node, path: Path) => boolean\n reverse?: boolean\n },\n): Generator<{node: Node; path: Path}, void, undefined> {\n const {match, reverse = false} = options\n\n const children = getChildren(snapshot, path)\n\n const entries = reverse ? [...children].reverse() : children\n\n for (const entry of entries) {\n if (!match || match(entry.node, entry.path)) {\n yield entry\n }\n\n yield* getNodesSimple(snapshot, entry.path, options)\n }\n}\n\n/**\n * Compare two keyed paths in document order. Returns -1, 0, or 1.\n *\n * Descends both paths from the root in a single pass, advancing\n * `currentNode` and `currentChildren` together so each level costs\n * one keyed-segment scan instead of an O(depth) walk from root.\n *\n * Uses `blockIndexMap` for O(1) lookup at the root level. Deeper\n * levels fall back to a linear scan of the current sibling array.\n */\nfunction comparePathsInTree(\n snapshot: TraversalSnapshot,\n pathA: Path,\n pathB: Path,\n): -1 | 0 | 1 {\n const keysA = pathA.filter(isKeyedSegment)\n const keysB = pathB.filter(isKeyedSegment)\n\n const {context} = snapshot\n let currentChildren: Array<Node> = context.value\n let currentParent: RegisteredContainer | undefined\n let isRootLevel = true\n\n const minDepth = Math.min(keysA.length, keysB.length)\n\n for (let depth = 0; depth < minDepth; depth++) {\n const keyA = keysA[depth]!\n const keyB = keysB[depth]!\n\n if (keyA._key === keyB._key) {\n // Same node at this depth: descend into its children for the next\n // iteration. The root level can short-circuit via blockIndexMap;\n // deeper levels scan the current sibling array.\n let matchedNode: Node | undefined\n if (isRootLevel && snapshot.blockIndexMap.has(keyA._key)) {\n const index = snapshot.blockIndexMap.get(keyA._key)\n if (index !== undefined) {\n matchedNode = currentChildren[index]\n }\n } else {\n matchedNode = currentChildren.find((c) => c._key === keyA._key)\n }\n if (!matchedNode) {\n return 0\n }\n const next = getNodeChildren(context, matchedNode, currentParent)\n if (!next) {\n return 0\n }\n currentChildren = next.children\n currentParent = next.parent\n\n isRootLevel = false\n continue\n }\n\n if (isRootLevel) {\n const indexA = snapshot.blockIndexMap.get(keyA._key) ?? -1\n const indexB = snapshot.blockIndexMap.get(keyB._key) ?? -1\n if (indexA !== -1 && indexB !== -1) {\n if (indexA < indexB) {\n return -1\n }\n if (indexA > indexB) {\n return 1\n }\n return 0\n }\n }\n\n let indexA = -1\n let indexB = -1\n for (let i = 0; i < currentChildren.length; i++) {\n const sibling = currentChildren[i]!\n if (sibling._key === keyA._key) {\n indexA = i\n }\n if (sibling._key === keyB._key) {\n indexB = i\n }\n if (indexA !== -1 && indexB !== -1) {\n break\n }\n }\n\n if (indexA < indexB) {\n return -1\n }\n if (indexA > indexB) {\n return 1\n }\n\n return 0\n }\n\n // One path is a prefix of the other (ancestor relationship)\n // In DFS order, shorter path (ancestor) comes first\n if (keysA.length < keysB.length) {\n return -1\n }\n if (keysA.length > keysB.length) {\n return 1\n }\n\n return 0\n}\n\n/**\n * Range-bounded recursive DFS traversal.\n *\n * `from` and `to` are always in document order (from is earlier, to is\n * later), regardless of traversal direction.\n */\nfunction* getNodesInRange(\n snapshot: TraversalSnapshot,\n path: Path,\n options: {\n from?: Path\n to?: Path\n match?: (node: Node, path: Path) => boolean\n reverse?: boolean\n },\n): Generator<{node: Node; path: Path}, void, undefined> {\n const {from, to, match, reverse = false} = options\n\n const children = getChildren(snapshot, path)\n const entries = reverse ? [...children].reverse() : children\n\n for (const entry of entries) {\n if (canStopTraversal(snapshot, entry.path, from, to, reverse)) {\n return\n }\n\n if (!couldContainInRangeNodes(snapshot, entry.path, from, to)) {\n continue\n }\n\n if (isInRange(snapshot, entry.path, from, to)) {\n if (!match || match(entry.node, entry.path)) {\n yield entry\n }\n }\n\n yield* getNodesInRange(snapshot, entry.path, options)\n }\n}\n\n/**\n * Check if a node is within the [from, to] range in document order.\n * Both bounds are inclusive. Ancestor nodes of from or to are also\n * considered in range since they contain the range boundary.\n */\nfunction isInRange(\n snapshot: TraversalSnapshot,\n nodePath: Path,\n from: Path | undefined,\n to: Path | undefined,\n): boolean {\n if (\n from !== undefined &&\n comparePathsInTree(snapshot, nodePath, from) === -1\n ) {\n if (!isAncestorPath(nodePath, from)) {\n return false\n }\n }\n\n if (to !== undefined && comparePathsInTree(snapshot, nodePath, to) === 1) {\n if (!isAncestorPath(nodePath, to)) {\n return false\n }\n }\n\n return true\n}\n\n/**\n * Check if a subtree rooted at `nodePath` could contain any nodes in the\n * [from, to] range.\n */\nfunction couldContainInRangeNodes(\n snapshot: TraversalSnapshot,\n nodePath: Path,\n from: Path | undefined,\n to: Path | undefined,\n): boolean {\n if (isInRange(snapshot, nodePath, from, to)) {\n return true\n }\n\n if (from !== undefined && isAncestorPath(nodePath, from)) {\n return true\n }\n\n if (to !== undefined && isAncestorPath(nodePath, to)) {\n return true\n }\n\n return false\n}\n\n/**\n * Check if all remaining nodes in iteration order will be outside the range.\n */\nfunction canStopTraversal(\n snapshot: TraversalSnapshot,\n nodePath: Path,\n from: Path | undefined,\n to: Path | undefined,\n reverse: boolean,\n): boolean {\n if (reverse) {\n if (from === undefined) {\n return false\n }\n\n return (\n comparePathsInTree(snapshot, nodePath, from) === -1 &&\n !isAncestorPath(nodePath, from)\n )\n }\n\n if (to === undefined) {\n return false\n }\n\n return comparePathsInTree(snapshot, nodePath, to) === 1\n}\n","import {isKeyedSegment} from '../../utils/util.is-keyed-segment'\nimport type {Path} from '../interfaces/path'\n\n/**\n * Get the parent path of a path.\n *\n * Drops the last node segment (keyed or numeric) and the preceding field\n * name string.\n *\n * [{_key:'b1'}, 'children', {_key:'s1'}] → [{_key:'b1'}]\n * [{_key:'b1'}, 'children', 0] → [{_key:'b1'}]\n * [{_key:'b1'}] → []\n */\nexport function parentPath(path: Path): Path {\n if (path.length === 0) {\n throw new Error(`Cannot get the parent path of the root path [${path}].`)\n }\n\n let lastNodeIndex = -1\n for (let i = path.length - 1; i >= 0; i--) {\n if (isKeyedSegment(path[i]) || typeof path[i] === 'number') {\n lastNodeIndex = i\n break\n }\n }\n\n if (lastNodeIndex === -1) {\n return []\n }\n\n const result = path.slice(0, lastNodeIndex)\n\n if (result.length > 0 && typeof result[result.length - 1] === 'string') {\n return result.slice(0, -1)\n }\n\n return result\n}\n","import type {Node} from '../engine/interfaces/node'\nimport type {Path} from '../engine/interfaces/path'\nimport {parentPath} from '../engine/path/parent-path'\nimport {isKeyedSegment} from '../utils/util.is-keyed-segment'\nimport {getChildren} from './get-children'\nimport type {TraversalSnapshot} from './traversal-snapshot'\n\n/**\n * Get the next or previous sibling of the node at a given path.\n *\n * @beta\n */\nexport function getSibling(\n snapshot: TraversalSnapshot,\n path: Path,\n direction: 'next' | 'previous',\n): {node: Node; path: Path} | undefined {\n if (path.length === 0) {\n return undefined\n }\n\n const lastSegment = path.at(-1)\n\n if (!isKeyedSegment(lastSegment)) {\n return undefined\n }\n\n const parent = parentPath(path)\n const children = getChildren(snapshot, parent)\n\n const currentIndex = children.findIndex(\n (child) => child.node._key === lastSegment._key,\n )\n\n if (currentIndex === -1) {\n return undefined\n }\n\n const siblingIndex =\n direction === 'next' ? currentIndex + 1 : currentIndex - 1\n\n if (siblingIndex < 0 || siblingIndex >= children.length) {\n return undefined\n }\n\n return children[siblingIndex]\n}\n","import type {EditorSchema} from '../../editor/editor-schema'\nimport {isTypedObject} from '../../utils/asserters'\n\nexport type SpanNode = {\n _type: string\n _key: string\n text?: string\n marks?: Array<string>\n}\n\n/**\n * Checks if a node is a span based on `_type` alone, without requiring `text`\n * to be present. This is needed to identify spans before normalization has had\n * a chance to add the missing `text` property.\n */\nexport function isSpanNode(\n context: {schema: EditorSchema},\n node: unknown,\n): node is SpanNode {\n return isTypedObject(node) && node._type === context.schema.span.name\n}\n","import type {Node} from '../engine/interfaces/node'\nimport type {Path} from '../engine/interfaces/path'\nimport {parentPath} from '../engine/path/parent-path'\nimport {getNode} from './get-node'\nimport type {TraversalSnapshot} from './traversal-snapshot'\n\n/**\n * Get the parent of a node at a given path.\n *\n * @beta\n */\nexport function getParent(\n snapshot: TraversalSnapshot,\n path: Path,\n): {node: Node; path: Path} | undefined {\n if (path.length === 0) {\n return undefined\n }\n\n const parent = parentPath(path)\n\n if (parent.length === 0) {\n return undefined\n }\n\n return getNode(snapshot, parent)\n}\n","import type {PortableTextBlock} from '@portabletext/schema'\nimport type {Path} from '../engine/interfaces/path'\nimport {isSpanNode} from '../engine/node/is-span-node'\nimport {isTextBlockNode} from '../engine/node/is-text-block-node'\nimport {getNode} from './get-node'\nimport {getParent} from './get-parent'\nimport type {TraversalSnapshot} from './traversal-snapshot'\n\n/**\n * Determine if a node at the given path is a block.\n *\n * A node is a block if its parent is not a text block. Top-level nodes\n * (direct children of the editor) are always blocks. Children of text blocks\n * (spans and inline objects) are not blocks. Children of containers are\n * blocks within that container.\n *\n * @beta\n */\nexport function isBlock(snapshot: TraversalSnapshot, path: Path): boolean {\n const parent = getParent(snapshot, path)\n\n if (!parent) {\n return true\n }\n\n return !isTextBlockNode({schema: snapshot.context.schema}, parent.node)\n}\n\n/**\n * Get the node at the given path if it is a block.\n *\n * Returns the node narrowed to PortableTextBlock, or undefined if the node\n * doesn't exist or is not a block.\n *\n * @beta\n */\nexport function getBlock(\n snapshot: TraversalSnapshot,\n path: Path,\n): {node: PortableTextBlock; path: Path} | undefined {\n const entry = getNode(snapshot, path)\n\n if (!entry) {\n return undefined\n }\n\n if (!isBlock(snapshot, path)) {\n return undefined\n }\n\n // Narrow the type: a block is never a span (spans always have a text block\n // parent, so isBlock returns false for them).\n if (isSpanNode({schema: snapshot.context.schema}, entry.node)) {\n return undefined\n }\n\n // Node minus PortableTextSpan = PortableTextTextBlock | PortableTextObject = PortableTextBlock\n return {node: entry.node, path: entry.path}\n}\n","import type {Path} from '../engine/interfaces/path'\nimport {isBlock} from './is-block'\nimport type {TraversalSnapshot} from './traversal-snapshot'\n\n/**\n * Determine if a node at the given path is inline.\n *\n * A node is inline if its parent is a text block. This is the inverse of\n * `isBlock`. Top-level nodes are never inline.\n *\n * @beta\n */\nexport function isInline(snapshot: TraversalSnapshot, path: Path): boolean {\n return !isBlock(snapshot, path)\n}\n","import type {PortableTextBlock} from '@portabletext/schema'\nimport type {Path} from '../engine/interfaces/path'\nimport {getAncestors} from './get-ancestors'\nimport {getBlock, isBlock} from './is-block'\nimport type {TraversalSnapshot} from './traversal-snapshot'\n\n/**\n * Walk up from a path to find the nearest enclosing block.\n *\n * Returns the node at the path if it is a block, otherwise the first ancestor\n * that is a block. Works at any depth — inside a container this returns the\n * container-internal block, not the outer container.\n *\n * @beta\n */\nexport function getEnclosingBlock(\n snapshot: TraversalSnapshot,\n path: Path,\n): {node: PortableTextBlock; path: Path} | undefined {\n const direct = getBlock(snapshot, path)\n\n if (direct) {\n return direct\n }\n\n for (const ancestor of getAncestors(snapshot, path)) {\n if (isBlock(snapshot, ancestor.path)) {\n const block = getBlock(snapshot, ancestor.path)\n\n if (block) {\n return block\n }\n }\n }\n\n return undefined\n}\n","import type {Path} from '../engine/interfaces/path'\nimport {isObjectNode} from '../engine/node/is-object-node'\nimport {getAncestors} from '../node-traversal/get-ancestors'\nimport type {TraversalSnapshot} from '../node-traversal/traversal-snapshot'\nimport type {RegisteredContainer} from './container-types'\nimport {resolveContainerAt} from './resolve-container-at'\n\n/**\n * Descent primitive: return the immediate parent\n * {@link RegisteredContainer} of the node at `path` (and that parent's\n * path), or `undefined` when the target's immediate parent is the\n * editor root, when no object-node ancestor is a registered container,\n * or when descent hits an ancestor whose `_type` is not registered.\n *\n * Walks ancestors and resolves each object-node ancestor positionally\n * via {@link resolveContainerAt}. Text-block and span ancestors are\n * skipped - \"container\" here means the enclosing object container,\n * not the text-block holding spans.\n */\nexport function descendToParent(\n snapshot: TraversalSnapshot,\n path: Path,\n): {parent: RegisteredContainer; parentPath: Path} | undefined {\n const ancestors = getAncestors(snapshot, path)\n for (const ancestor of ancestors) {\n if (!isObjectNode({schema: snapshot.context.schema}, ancestor.node)) {\n continue\n }\n const resolved = resolveContainerAt(\n snapshot.context.containers,\n snapshot.context.value,\n ancestor.path,\n )\n if (!resolved || !('field' in resolved)) {\n return undefined\n }\n return {parent: resolved, parentPath: ancestor.path}\n }\n return undefined\n}\n","import type {OfDefinition} from '@portabletext/schema'\nimport type {Path} from '../engine/interfaces/path'\nimport type {TraversalSnapshot} from '../node-traversal/traversal-snapshot'\nimport {descendToParent} from './descend-to-parent'\n\n/**\n * Return the immediate registered-container ancestor of `path` along\n * with its `of` array (the schema definitions accepted at this position).\n *\n * Position-aware: nested-only registrations (e.g. `cell` registered\n * only inside `table.row.of`) are recognized via the same descent\n * primitive used by all parent-aware traversal.\n *\n * Returns `undefined` when `path` has no registered-container ancestor\n * (i.e. is at the document root) or when descent hits a leaf-resolved\n * ancestor.\n */\nexport function getEnclosingContainer(\n snapshot: TraversalSnapshot,\n path: Path,\n):\n | {\n of: ReadonlyArray<OfDefinition>\n path: Path\n }\n | undefined {\n const descent = descendToParent(snapshot, path)\n if (!descent) {\n return undefined\n }\n return {\n of: descent.parent.field.of,\n path: descent.parentPath,\n }\n}\n","import {getSubSchema, type Schema} from '@portabletext/schema'\nimport type {Path} from '../engine/interfaces/path'\nimport type {TraversalSnapshot} from '../node-traversal/traversal-snapshot'\nimport {getEnclosingContainer} from '../schema/get-enclosing-container'\n\n/**\n * Return the `Schema` view that applies at a given path.\n *\n * For paths at the root of the document, or for paths where no ancestor is\n * a registered container, returns the top-level schema. For paths inside a\n * container, walks ancestors to find the nearest container and returns the\n * sub-schema derived from its `of` declaration.\n *\n * @beta\n */\nexport function getPathSubSchema(\n snapshot: TraversalSnapshot,\n path: Path,\n): Schema {\n const enclosing = getEnclosingContainer(snapshot, path)\n\n if (!enclosing) {\n return snapshot.context.schema\n }\n\n return getSubSchema(snapshot.context.schema, enclosing.of)\n}\n"],"names":["hasNode","snapshot","path","getNode","undefined","isTextBlockNode","context","node","isTypedObject","_type","schema","block","name","resolveContainerAt","containers","value","keyedIndices","index","length","isKeyedSegment","push","currentChildren","parent","resolved","targetKeyedIndex","segmentIndex","segment","find","child","_key","at","resolveNodeEntry","fieldValue","field","Array","isArray","of","entry","type","get","isAncestorPath","another","i","otherSegment","getNodes","options","from","to","match","reverse","getNodesSimple","getNodesInRange","children","getChildren","entries","comparePathsInTree","pathA","pathB","keysA","filter","keysB","currentParent","isRootLevel","minDepth","Math","min","depth","keyA","keyB","matchedNode","blockIndexMap","has","c","next","getNodeChildren","indexA","indexB","sibling","canStopTraversal","couldContainInRangeNodes","isInRange","nodePath","parentPath","Error","lastNodeIndex","result","slice","getSibling","direction","lastSegment","currentIndex","findIndex","siblingIndex","isSpanNode","span","getParent","isBlock","getBlock","isInline","getEnclosingBlock","direct","ancestor","getAncestors","descendToParent","ancestors","isObjectNode","getE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AEtC,MAAIkG;AACF,WAAOA;AAGT,aAAWC,YAAYC,aAAarG,UAAUC,IAAI;AAChD,QAAI8F,QAAQ/F,UAAUoG,SAASnG,IAAI,GAAG;AACpC,YAAMS,QAAQsF,SAAShG,UAAUoG,SAASnG,IAAI;AAE9C,UAAIS;AACF,eAAOA;AAAAA,IAEX;AAIJ;ACjBO,SAAS4F,gBACdtG,UACAC,MAC6D;AAC7D,QAAMsG,YAAYF,aAAarG,UAAUC,IAAI;AAC7C,aAAWmG,YAAYG,WAAW;AAChC,QAAI,CAACC,aAAa;AAAA,MAAC/F,QAAQT,SAASK,QAAQI;AAAAA,IAAAA,GAAS2F,SAAS9F,IAAI;AAChE;AAEF,UAAMgB,WAAWV,mBACfZ,SAASK,QAAQQ,YACjBb,SAASK,QAAQS,OACjBsF,SAASnG,IACX;AACA,WAAI,CAACqB,YAAY,EAAE,WAAWA,YAC5B,SAEK;AAAA,MAACD,QAAQC;AAAAA,MAAU2D,YAAYmB,SAASnG;AAAAA,IAAAA;AAAAA,EACjD;AAEF;ACtBO,SAASwG,sBACdzG,UACAC,MAMY;AACZ,QAAMyG,UAAUJ,gBAAgBtG,UAAUC,IAAI;AAC9C,MAAKyG;AAGL,WAAO;AAAA,MACLvE,IAAIuE,QAAQrF,OAAOW,MAAMG;AAAAA,MACzBlC,MAAMyG,QAAQzB;AAAAA,IAAAA;AAElB;ACnBO,SAAS0B,iBACd3G,UACAC,MACQ;AACR,QAAM2G,YAAYH,sBAAsBzG,UAAUC,IAAI;AAEtD,SAAK2G,YAIEC,aAAa7G,SAASK,QAAQI,QAAQmG,UAAUzE,EAAE,IAHhDnC,SAASK,QAAQI;AAI5B;"}
1
+ {"version":3,"file":"get-path-sub-schema.js","sources":["../../src/traversal/get-ancestors.ts","../../src/traversal/has-node.ts","../../src/schema/resolve-container-at.ts","../../src/schema/is-editable-container.ts","../../src/traversal/is-object.ts","../../src/engine/node/is-text-block-node.ts","../../src/engine/path/is-ancestor-path.ts","../../src/traversal/get-nodes.ts","../../src/traversal/get-sibling.ts","../../src/engine/node/is-span-node.ts","../../src/traversal/is-block.ts","../../src/traversal/is-inline.ts","../../src/traversal/get-enclosing-block.ts","../../src/schema/descend-to-parent.ts","../../src/schema/get-enclosing-container.ts","../../src/traversal/get-path-sub-schema.ts"],"sourcesContent":["import type {PortableTextBlock} from '@portabletext/schema'\nimport type {Node} from '../engine/interfaces/node'\nimport type {Path} from '../engine/interfaces/path'\nimport {isKeyedSegment} from '../utils/util.is-keyed-segment'\nimport {getNodeChildren} from './get-children'\nimport type {TraversalSnapshot} from './traversal-snapshot'\n\n/**\n * Get all ancestors of the node at a given path, from nearest to furthest.\n *\n * For a path like [{_key:'t1'}, 'rows', {_key:'r1'}, 'cells', {_key:'c1'}],\n * the ancestors are (nearest first):\n * [{_key:'t1'}, 'rows', {_key:'r1'}]\n * [{_key:'t1'}]\n *\n * Walks from root to the target in a single pass collecting each ancestor\n * as it goes.\n *\n * Every ancestor is a `PortableTextBlock` — only text blocks and object\n * nodes can contain children.\n *\n * @beta\n */\nexport function getAncestors(\n snapshot: TraversalSnapshot,\n path: Path,\n): Array<{node: PortableTextBlock; path: Path}> {\n // Collect keyed-segment indices to know where each ancestor's path ends.\n const keyedIndices: Array<number> = []\n for (let i = 0; i < path.length; i++) {\n if (isKeyedSegment(path[i])) {\n keyedIndices.push(i)\n }\n }\n\n // Need at least 2 keyed segments to have an ancestor (the last is self).\n if (keyedIndices.length <= 1) {\n return []\n }\n\n const {context, blockIndexMap} = snapshot\n let currentChildren: Array<Node> = context.value\n let isRootLevel = true\n let currentParent:\n | import('../schema/resolve-containers').RegisteredContainer\n | undefined\n\n const ancestorsByDepth: Array<{node: PortableTextBlock; path: Path}> = []\n const resolvedPath: Path = []\n\n // Descend once. We walk only as far as the second-to-last keyed segment;\n // the last keyed segment is the target itself, which is not an ancestor.\n const targetKeyedIndex = keyedIndices[keyedIndices.length - 1]!\n\n let segmentIndex = 0\n while (segmentIndex < targetKeyedIndex) {\n const segment = path[segmentIndex]!\n\n if (typeof segment === 'string') {\n resolvedPath.push(segment)\n segmentIndex++\n continue\n }\n\n let node: Node | undefined\n if (isKeyedSegment(segment)) {\n // Production snapshots maintain `blockIndexMap` in lockstep with\n // `context.value` so this fast path always fires. Some test\n // fixtures still pass empty or stale maps, which is the debt this\n // size check is working around - see /specs/snapshot-invariants.md.\n // When the fixtures are aligned, drop the guard and use the map\n // directly.\n if (isRootLevel && blockIndexMap.size === currentChildren.length) {\n const index = blockIndexMap.get(segment._key)\n node =\n index !== undefined\n ? currentChildren[index]\n : currentChildren.find((child) => child._key === segment._key)\n } else {\n node = currentChildren.find((child) => child._key === segment._key)\n }\n resolvedPath.push(segment)\n isRootLevel = false\n } else if (typeof segment === 'number') {\n node = currentChildren.at(segment)\n if (node) {\n resolvedPath.push({_key: node._key})\n }\n } else {\n return []\n }\n\n if (!node) {\n return []\n }\n\n // Descend with positional awareness. `getNodeChildren` checks the\n // current parent's `of` for a positional override before falling\n // back to the top-level `containers` map - so same-`_type`\n // registered under different parents with different `field`\n // resolves to the right entry at this position.\n const next = getNodeChildren(context, node, currentParent)\n if (!next) {\n return []\n }\n\n // An ancestor has children, so it is never a span. The narrowing\n // from `Node` to `PortableTextBlock` (text block | object) is safe.\n ancestorsByDepth.push({\n node: node as PortableTextBlock,\n path: resolvedPath.slice(),\n })\n\n currentChildren = next.children\n currentParent = next.parent\n segmentIndex++\n }\n\n // Return nearest-first (reverse of document order at the call site).\n return ancestorsByDepth.reverse()\n}\n","import type {Path} from '../engine/interfaces/path'\nimport {getNode} from './get-node'\nimport type {TraversalSnapshot} from './traversal-snapshot'\n\n/**\n * Check if a node exists at a given path.\n *\n * @beta\n */\nexport function hasNode(snapshot: TraversalSnapshot, path: Path): boolean {\n return getNode(snapshot, path) !== undefined\n}\n","import type {Node} from '../engine/interfaces/node'\nimport type {Path} from '../engine/interfaces/path'\nimport {isKeyedSegment} from '../utils/util.is-keyed-segment'\nimport type {\n Containers,\n RegisteredContainer,\n RegisteredPositional,\n} from './container-types'\n\n/**\n * Walk the editor value following `path` and return the\n * {@link RegisteredContainer} or {@link RegisteredPositional} that applies\n * at `path`'s target position.\n *\n * Resolution rules at each step:\n *\n * 1. **Positional override.** If the current parent declares the\n * child's `_type` in its `of`, the positional entry wins.\n * Used to resolve same-`_type` registered under different\n * parents with different `field` values.\n *\n * 2. **Global fallback.** If the parent has no positional override,\n * fall back to the top-level entry for `_type` in\n * `containers`.\n *\n * 3. **Chain validity.** If any ancestor along the path has no\n * resolved container entry (unregistered or not reachable as a\n * container at its position), return `undefined`.\n *\n * Returns `undefined` when the target's `_type` is not registered\n * at this position. Returns a {@link RegisteredPositional} when the target\n * resolves to a leaf in a positional `of` (terminal node with no\n * editable children).\n *\n * @alpha\n */\nexport function resolveContainerAt(\n containers: Containers,\n value: ReadonlyArray<Node>,\n path: Path,\n): RegisteredContainer | RegisteredPositional | undefined {\n const keyedIndices: Array<number> = []\n for (let index = 0; index < path.length; index++) {\n if (isKeyedSegment(path[index])) {\n keyedIndices.push(index)\n }\n }\n if (keyedIndices.length === 0) {\n return undefined\n }\n\n let currentChildren: ReadonlyArray<Node> = value\n let parent: RegisteredContainer | undefined\n let resolved: RegisteredContainer | RegisteredPositional | undefined\n const targetKeyedIndex = keyedIndices[keyedIndices.length - 1]!\n\n let segmentIndex = 0\n while (segmentIndex <= targetKeyedIndex) {\n const segment = path[segmentIndex]!\n if (typeof segment === 'string') {\n segmentIndex++\n continue\n }\n\n let node: Node | undefined\n if (isKeyedSegment(segment)) {\n node = currentChildren.find((child) => child._key === segment._key)\n } else if (typeof segment === 'number') {\n node = currentChildren.at(segment)\n } else {\n return undefined\n }\n if (!node) {\n return undefined\n }\n\n resolved = resolveNodeEntry(containers, parent, node)\n if (!resolved) {\n return undefined\n }\n\n if (segmentIndex < targetKeyedIndex) {\n // Walk one more level. The resolved entry must be a container\n // (have children) for descent to continue.\n if (!('field' in resolved)) {\n return undefined\n }\n const fieldValue = (node as Record<string, unknown>)[resolved.field.name]\n if (!Array.isArray(fieldValue)) {\n return undefined\n }\n parent = resolved\n currentChildren = fieldValue as Array<Node>\n }\n segmentIndex++\n }\n\n return resolved\n}\n\nfunction resolveNodeEntry(\n containers: Containers,\n parent: RegisteredContainer | undefined,\n node: Node,\n): RegisteredContainer | RegisteredPositional | undefined {\n if (parent?.of) {\n for (const entry of parent.of) {\n if (entry.type === node._type) {\n return entry\n }\n }\n }\n return containers.get(node._type)\n}\n","import type {Node} from '../engine/interfaces/node'\nimport type {Path} from '../engine/interfaces/path'\nimport type {TraversalSnapshot} from '../traversal/traversal-snapshot'\nimport {resolveContainerAt} from './resolve-container-at'\n\n/**\n * Check if a node at the given path is a registered editable container.\n *\n * Position-aware: {@link resolveContainerAt} descends from the editor\n * root threading the resolved parent at each step, so positionally-\n * registered containers (e.g. `cell` registered only inside\n * `table.of`) are recognized when reached through their declared\n * parent.\n */\nexport function isEditableContainer(\n snapshot: TraversalSnapshot,\n _node: Node,\n path: Path,\n): boolean {\n if (snapshot.context.containers.size === 0) {\n return false\n }\n\n // `resolveContainerAt` aborts on the first unregistered object-node\n // ancestor (chain validity falls out of the single descent), so the\n // single call below answers both \"is the node here a container?\" and\n // \"is the ancestor chain valid?\" in one walk.\n const resolved = resolveContainerAt(\n snapshot.context.containers,\n snapshot.context.value,\n path,\n )\n return !!(resolved && 'field' in resolved)\n}\n","import type {PortableTextObject} from '@portabletext/schema'\nimport {isTypedObject} from '../utils/asserters'\nimport type {TraversalSnapshot} from './traversal-snapshot'\n\n/**\n * Check if a node is an object node (not a text block or span).\n *\n * @beta\n */\nexport function isObject(\n snapshot: TraversalSnapshot,\n node: unknown,\n): node is PortableTextObject {\n return (\n isTypedObject(node) &&\n node._type !== snapshot.context.schema.block.name &&\n node._type !== snapshot.context.schema.span.name\n )\n}\n","import type {PortableTextObject, PortableTextSpan} from '@portabletext/schema'\nimport type {EditorSchema} from '../../editor/editor-schema'\nimport {isTypedObject} from '../../utils/asserters'\n\ntype TextBlockNode = {\n _type: string\n _key: string\n children?: Array<PortableTextSpan | PortableTextObject>\n markDefs?: Array<PortableTextObject>\n style?: string\n listItem?: string\n level?: number\n}\n\n/**\n * Checks if a node is a text block based on `_type` alone, without requiring\n * `children` to be present. This is needed to identify text blocks before\n * normalization has had a chance to add the missing `children` property.\n */\nexport function isTextBlockNode(\n context: {schema: EditorSchema},\n node: unknown,\n): node is TextBlockNode {\n return isTypedObject(node) && node._type === context.schema.block.name\n}\n","import {isKeyedSegment} from '../../utils/util.is-keyed-segment'\nimport type {Path} from '../interfaces/path'\n\nexport function isAncestorPath(path: Path, another: Path): boolean {\n if (path.length >= another.length) {\n return false\n }\n\n for (let i = 0; i < path.length; i++) {\n const segment = path[i]\n const otherSegment = another[i]\n\n if (isKeyedSegment(segment) && isKeyedSegment(otherSegment)) {\n if (segment._key !== otherSegment._key) {\n return false\n }\n } else if (segment !== otherSegment) {\n return false\n }\n }\n\n return true\n}\n","import type {EditorSchema} from '../editor/editor-schema'\nimport type {Node} from '../engine/interfaces/node'\nimport type {Path} from '../engine/interfaces/path'\nimport {isAncestorPath} from '../engine/path/is-ancestor-path'\nimport type {\n Containers,\n RegisteredContainer,\n} from '../schema/resolve-containers'\nimport {isKeyedSegment} from '../utils/util.is-keyed-segment'\nimport {getChildren, getNodeChildren} from './get-children'\nimport type {TraversalSnapshot} from './traversal-snapshot'\n\n/**\n * Get the descendant nodes of the node at a given path.\n *\n * When `from` and `to` are provided, performs a range-bounded DFS traversal,\n * yielding only nodes between `from` and `to` (inclusive). Both paths are\n * always in document order: `from` is the earlier path, `to` is the later\n * path. The `reverse` flag controls iteration direction within that range.\n *\n * When `match` is provided, only yields nodes where the predicate returns true.\n * The traversal still visits all nodes in range - `match` is a filter, not a\n * traversal control.\n *\n * When `at` is provided, traverses descendants of the node at that path\n * instead of the root.\n */\nexport function* getNodes(\n snapshot: TraversalSnapshot,\n options: {\n at?: Path\n from?: Path\n to?: Path\n match?: (node: Node, path: Path) => boolean\n reverse?: boolean\n } = {},\n): Generator<{node: Node; path: Path}, void, undefined> {\n const {at = [], from, to, match, reverse = false} = options\n\n if (from === undefined && to === undefined) {\n yield* getNodesSimple(snapshot, at, {match, reverse})\n return\n }\n\n yield* getNodesInRange(snapshot, at, {from, to, match, reverse})\n}\n\n/**\n * Get descendant nodes of a standalone node (not in the editor tree).\n * Used for cases like getDirtyPaths where the node hasn't been inserted yet.\n */\nexport function* getNodeDescendants(\n context: {\n schema: EditorSchema\n containers: Containers\n },\n node: Node | {value: Array<Node>},\n): Generator<{node: Node; path: Path}, void, undefined> {\n // The editor root wrapper ({value: [...]}) is not a real node, so its field\n // name is not part of paths. For standalone nodes (a real {_key, _type, ...}\n // passed in by callers like getDirtyPaths), the field name IS part of the\n // path.\n const isRoot = !('_key' in node) && !('_type' in node)\n yield* walkStandalone(context, node, [], isRoot)\n}\n\nfunction* walkStandalone(\n context: {\n schema: EditorSchema\n containers: Containers\n },\n node: Node | {value: Array<Node>},\n path: Path,\n isRoot: boolean,\n parent?: RegisteredContainer,\n): Generator<{node: Node; path: Path}, void, undefined> {\n const next = getNodeChildren(context, node, parent)\n if (!next) {\n return\n }\n\n for (const child of next.children) {\n const childPath: Path = isRoot\n ? [{_key: child._key}]\n : [...path, next.fieldName, {_key: child._key}]\n yield {node: child, path: childPath}\n yield* walkStandalone(context, child, childPath, false, next.parent)\n }\n}\n\n/**\n * Simple recursive DFS - the original behavior.\n * Yields all descendants of the node at `path`.\n */\nfunction* getNodesSimple(\n snapshot: TraversalSnapshot,\n path: Path,\n options: {\n match?: (node: Node, path: Path) => boolean\n reverse?: boolean\n },\n): Generator<{node: Node; path: Path}, void, undefined> {\n const {match, reverse = false} = options\n\n const children = getChildren(snapshot, path)\n\n const entries = reverse ? [...children].reverse() : children\n\n for (const entry of entries) {\n if (!match || match(entry.node, entry.path)) {\n yield entry\n }\n\n yield* getNodesSimple(snapshot, entry.path, options)\n }\n}\n\n/**\n * Compare two keyed paths in document order. Returns -1, 0, or 1.\n *\n * Descends both paths from the root in a single pass, advancing\n * `currentNode` and `currentChildren` together so each level costs\n * one keyed-segment scan instead of an O(depth) walk from root.\n *\n * Uses `blockIndexMap` for O(1) lookup at the root level. Deeper\n * levels fall back to a linear scan of the current sibling array.\n */\nfunction comparePathsInTree(\n snapshot: TraversalSnapshot,\n pathA: Path,\n pathB: Path,\n): -1 | 0 | 1 {\n const keysA = pathA.filter(isKeyedSegment)\n const keysB = pathB.filter(isKeyedSegment)\n\n const {context} = snapshot\n let currentChildren: Array<Node> = context.value\n let currentParent: RegisteredContainer | undefined\n let isRootLevel = true\n\n const minDepth = Math.min(keysA.length, keysB.length)\n\n for (let depth = 0; depth < minDepth; depth++) {\n const keyA = keysA[depth]!\n const keyB = keysB[depth]!\n\n if (keyA._key === keyB._key) {\n // Same node at this depth: descend into its children for the next\n // iteration. The root level can short-circuit via blockIndexMap;\n // deeper levels scan the current sibling array.\n let matchedNode: Node | undefined\n if (isRootLevel && snapshot.blockIndexMap.has(keyA._key)) {\n const index = snapshot.blockIndexMap.get(keyA._key)\n if (index !== undefined) {\n matchedNode = currentChildren[index]\n }\n } else {\n matchedNode = currentChildren.find((c) => c._key === keyA._key)\n }\n if (!matchedNode) {\n return 0\n }\n const next = getNodeChildren(context, matchedNode, currentParent)\n if (!next) {\n return 0\n }\n currentChildren = next.children\n currentParent = next.parent\n\n isRootLevel = false\n continue\n }\n\n if (isRootLevel) {\n const indexA = snapshot.blockIndexMap.get(keyA._key) ?? -1\n const indexB = snapshot.blockIndexMap.get(keyB._key) ?? -1\n if (indexA !== -1 && indexB !== -1) {\n if (indexA < indexB) {\n return -1\n }\n if (indexA > indexB) {\n return 1\n }\n return 0\n }\n }\n\n let indexA = -1\n let indexB = -1\n for (let i = 0; i < currentChildren.length; i++) {\n const sibling = currentChildren[i]!\n if (sibling._key === keyA._key) {\n indexA = i\n }\n if (sibling._key === keyB._key) {\n indexB = i\n }\n if (indexA !== -1 && indexB !== -1) {\n break\n }\n }\n\n if (indexA < indexB) {\n return -1\n }\n if (indexA > indexB) {\n return 1\n }\n\n return 0\n }\n\n // One path is a prefix of the other (ancestor relationship)\n // In DFS order, shorter path (ancestor) comes first\n if (keysA.length < keysB.length) {\n return -1\n }\n if (keysA.length > keysB.length) {\n return 1\n }\n\n return 0\n}\n\n/**\n * Range-bounded recursive DFS traversal.\n *\n * `from` and `to` are always in document order (from is earlier, to is\n * later), regardless of traversal direction.\n */\nfunction* getNodesInRange(\n snapshot: TraversalSnapshot,\n path: Path,\n options: {\n from?: Path\n to?: Path\n match?: (node: Node, path: Path) => boolean\n reverse?: boolean\n },\n): Generator<{node: Node; path: Path}, void, undefined> {\n const {from, to, match, reverse = false} = options\n\n const children = getChildren(snapshot, path)\n const entries = reverse ? [...children].reverse() : children\n\n for (const entry of entries) {\n if (canStopTraversal(snapshot, entry.path, from, to, reverse)) {\n return\n }\n\n if (!couldContainInRangeNodes(snapshot, entry.path, from, to)) {\n continue\n }\n\n if (isInRange(snapshot, entry.path, from, to)) {\n if (!match || match(entry.node, entry.path)) {\n yield entry\n }\n }\n\n yield* getNodesInRange(snapshot, entry.path, options)\n }\n}\n\n/**\n * Check if a node is within the [from, to] range in document order.\n * Both bounds are inclusive. Ancestor nodes of from or to are also\n * considered in range since they contain the range boundary.\n */\nfunction isInRange(\n snapshot: TraversalSnapshot,\n nodePath: Path,\n from: Path | undefined,\n to: Path | undefined,\n): boolean {\n if (\n from !== undefined &&\n comparePathsInTree(snapshot, nodePath, from) === -1\n ) {\n if (!isAncestorPath(nodePath, from)) {\n return false\n }\n }\n\n if (to !== undefined && comparePathsInTree(snapshot, nodePath, to) === 1) {\n if (!isAncestorPath(nodePath, to)) {\n return false\n }\n }\n\n return true\n}\n\n/**\n * Check if a subtree rooted at `nodePath` could contain any nodes in the\n * [from, to] range.\n */\nfunction couldContainInRangeNodes(\n snapshot: TraversalSnapshot,\n nodePath: Path,\n from: Path | undefined,\n to: Path | undefined,\n): boolean {\n if (isInRange(snapshot, nodePath, from, to)) {\n return true\n }\n\n if (from !== undefined && isAncestorPath(nodePath, from)) {\n return true\n }\n\n if (to !== undefined && isAncestorPath(nodePath, to)) {\n return true\n }\n\n return false\n}\n\n/**\n * Check if all remaining nodes in iteration order will be outside the range.\n */\nfunction canStopTraversal(\n snapshot: TraversalSnapshot,\n nodePath: Path,\n from: Path | undefined,\n to: Path | undefined,\n reverse: boolean,\n): boolean {\n if (reverse) {\n if (from === undefined) {\n return false\n }\n\n return (\n comparePathsInTree(snapshot, nodePath, from) === -1 &&\n !isAncestorPath(nodePath, from)\n )\n }\n\n if (to === undefined) {\n return false\n }\n\n return comparePathsInTree(snapshot, nodePath, to) === 1\n}\n","import type {Node} from '../engine/interfaces/node'\nimport type {Path} from '../engine/interfaces/path'\nimport {parentPath} from '../engine/path/parent-path'\nimport {isKeyedSegment} from '../utils/util.is-keyed-segment'\nimport {getChildren} from './get-children'\nimport type {TraversalSnapshot} from './traversal-snapshot'\n\n/**\n * Get a sibling of the node at a given path.\n *\n * Without `match`, returns the immediate next or previous sibling.\n * With `match`, returns the first sibling in `direction` that satisfies\n * the predicate.\n *\n * When `match` is a type predicate, the returned `node` narrows to that type.\n *\n * @beta\n */\nexport function getSibling<TMatch extends Node>(\n snapshot: TraversalSnapshot,\n path: Path,\n options: {\n direction: 'next' | 'previous'\n match: (node: Node, path: Path) => node is TMatch\n },\n): {node: TMatch; path: Path} | undefined\n/**\n * @beta\n */\nexport function getSibling(\n snapshot: TraversalSnapshot,\n path: Path,\n options: {\n direction: 'next' | 'previous'\n match?: (node: Node, path: Path) => boolean\n },\n): {node: Node; path: Path} | undefined\nexport function getSibling(\n snapshot: TraversalSnapshot,\n path: Path,\n options: {\n direction: 'next' | 'previous'\n match?: (node: Node, path: Path) => boolean\n },\n): {node: Node; path: Path} | undefined {\n const {direction, match} = options\n\n if (path.length === 0) {\n return undefined\n }\n\n const lastSegment = path.at(-1)\n\n if (!isKeyedSegment(lastSegment)) {\n return undefined\n }\n\n const parent = parentPath(path)\n const children = getChildren(snapshot, parent)\n\n const currentIndex = children.findIndex(\n (child) => child.node._key === lastSegment._key,\n )\n\n if (currentIndex === -1) {\n return undefined\n }\n\n if (!match) {\n const siblingIndex =\n direction === 'next' ? currentIndex + 1 : currentIndex - 1\n\n if (siblingIndex < 0 || siblingIndex >= children.length) {\n return undefined\n }\n\n return children[siblingIndex]\n }\n\n const candidates =\n direction === 'next'\n ? children.slice(currentIndex + 1)\n : children.slice(0, currentIndex).reverse()\n\n return candidates.find((child) => match(child.node, child.path))\n}\n","import type {EditorSchema} from '../../editor/editor-schema'\nimport {isTypedObject} from '../../utils/asserters'\n\nexport type SpanNode = {\n _type: string\n _key: string\n text?: string\n marks?: Array<string>\n}\n\n/**\n * Checks if a node is a span based on `_type` alone, without requiring `text`\n * to be present. This is needed to identify spans before normalization has had\n * a chance to add the missing `text` property.\n */\nexport function isSpanNode(\n context: {schema: EditorSchema},\n node: unknown,\n): node is SpanNode {\n return isTypedObject(node) && node._type === context.schema.span.name\n}\n","import type {PortableTextBlock} from '@portabletext/schema'\nimport type {Path} from '../engine/interfaces/path'\nimport {isSpanNode} from '../engine/node/is-span-node'\nimport {isTextBlockNode} from '../engine/node/is-text-block-node'\nimport {getNode} from './get-node'\nimport {getParent} from './get-parent'\nimport type {TraversalSnapshot} from './traversal-snapshot'\n\n/**\n * Determine if a node at the given path is a block.\n *\n * A node is a block if its parent is not a text block. Top-level nodes\n * (direct children of the editor) are always blocks. Children of text blocks\n * (spans and inline objects) are not blocks. Children of containers are\n * blocks within that container.\n *\n * @beta\n */\nexport function isBlock(snapshot: TraversalSnapshot, path: Path): boolean {\n const parent = getParent(snapshot, path)\n\n if (!parent) {\n return true\n }\n\n return !isTextBlockNode({schema: snapshot.context.schema}, parent.node)\n}\n\n/**\n * Get the node at the given path if it is a block.\n *\n * Returns the node narrowed to PortableTextBlock, or undefined if the node\n * doesn't exist or is not a block.\n *\n * @beta\n */\nexport function getBlock(\n snapshot: TraversalSnapshot,\n path: Path,\n): {node: PortableTextBlock; path: Path} | undefined {\n const entry = getNode(snapshot, path)\n\n if (!entry) {\n return undefined\n }\n\n if (!isBlock(snapshot, path)) {\n return undefined\n }\n\n if (isSpanNode({schema: snapshot.context.schema}, entry.node)) {\n return undefined\n }\n\n return {node: entry.node, path: entry.path}\n}\n","import type {Path} from '../engine/interfaces/path'\nimport {isBlock} from './is-block'\nimport type {TraversalSnapshot} from './traversal-snapshot'\n\n/**\n * Determine if a node at the given path is inline.\n *\n * A node is inline if its parent is a text block. This is the inverse of\n * `isBlock`. Top-level nodes are never inline.\n *\n * @beta\n */\nexport function isInline(snapshot: TraversalSnapshot, path: Path): boolean {\n return !isBlock(snapshot, path)\n}\n","import type {PortableTextBlock} from '@portabletext/schema'\nimport type {Path} from '../engine/interfaces/path'\nimport {getAncestors} from './get-ancestors'\nimport {getBlock} from './is-block'\nimport type {TraversalSnapshot} from './traversal-snapshot'\n\n/**\n * Walk up from a path to find the nearest enclosing block.\n *\n * Returns the node at the path if it is a block, otherwise the first ancestor\n * that is a block. Works at any depth — inside a container this returns the\n * container-internal block, not the outer container.\n *\n * With `match`, returns the first enclosing block that also satisfies the\n * predicate. When `match` is a type predicate, the returned `node` narrows\n * to that type.\n *\n * `mode: 'lowest'` (default) returns the innermost enclosing block; the node\n * at the path itself counts. `mode: 'highest'` returns the outermost\n * ancestor that matches, falling back to the node at the path only if no\n * ancestor does.\n *\n * @beta\n */\nexport function getEnclosingBlock<TMatch extends PortableTextBlock>(\n snapshot: TraversalSnapshot,\n path: Path,\n options: {\n match: (node: PortableTextBlock, path: Path) => node is TMatch\n mode?: 'lowest' | 'highest'\n },\n): {node: TMatch; path: Path} | undefined\n/**\n * @beta\n */\nexport function getEnclosingBlock(\n snapshot: TraversalSnapshot,\n path: Path,\n options?: {\n match?: (node: PortableTextBlock, path: Path) => boolean\n mode?: 'lowest' | 'highest'\n },\n): {node: PortableTextBlock; path: Path} | undefined\nexport function getEnclosingBlock(\n snapshot: TraversalSnapshot,\n path: Path,\n options?: {\n match?: (node: PortableTextBlock, path: Path) => boolean\n mode?: 'lowest' | 'highest'\n },\n): {node: PortableTextBlock; path: Path} | undefined {\n const match = options?.match\n const mode = options?.mode ?? 'lowest'\n\n if (mode === 'highest') {\n const ancestors = getAncestors(snapshot, path)\n\n for (const ancestor of [...ancestors].reverse()) {\n if (!match || match(ancestor.node, ancestor.path)) {\n return ancestor\n }\n }\n\n const direct = getBlock(snapshot, path)\n\n if (direct && (!match || match(direct.node, direct.path))) {\n return direct\n }\n\n return undefined\n }\n\n const direct = getBlock(snapshot, path)\n\n if (direct && (!match || match(direct.node, direct.path))) {\n return direct\n }\n\n for (const ancestor of getAncestors(snapshot, path)) {\n if (!match || match(ancestor.node, ancestor.path)) {\n return ancestor\n }\n }\n\n return undefined\n}\n","import type {Path} from '../engine/interfaces/path'\nimport {getAncestors} from '../traversal/get-ancestors'\nimport {isObject} from '../traversal/is-object'\nimport type {TraversalSnapshot} from '../traversal/traversal-snapshot'\nimport type {RegisteredContainer} from './container-types'\nimport {resolveContainerAt} from './resolve-container-at'\n\n/**\n * Descent primitive: return the immediate parent\n * {@link RegisteredContainer} of the node at `path` (and that parent's\n * path), or `undefined` when the target's immediate parent is the\n * editor root, when no object-node ancestor is a registered container,\n * or when descent hits an ancestor whose `_type` is not registered.\n *\n * Walks ancestors and resolves each object-node ancestor positionally\n * via {@link resolveContainerAt}. Text-block and span ancestors are\n * skipped - \"container\" here means the enclosing object container,\n * not the text-block holding spans.\n */\nexport function descendToParent(\n snapshot: TraversalSnapshot,\n path: Path,\n): {parent: RegisteredContainer; parentPath: Path} | undefined {\n const ancestors = getAncestors(snapshot, path)\n for (const ancestor of ancestors) {\n if (!isObject(snapshot, ancestor.node)) {\n continue\n }\n const resolved = resolveContainerAt(\n snapshot.context.containers,\n snapshot.context.value,\n ancestor.path,\n )\n if (!resolved || !('field' in resolved)) {\n return undefined\n }\n return {parent: resolved, parentPath: ancestor.path}\n }\n return undefined\n}\n","import type {OfDefinition} from '@portabletext/schema'\nimport type {Path} from '../engine/interfaces/path'\nimport type {TraversalSnapshot} from '../traversal/traversal-snapshot'\nimport {descendToParent} from './descend-to-parent'\n\n/**\n * Return the immediate registered-container ancestor of `path` along\n * with its `of` array (the schema definitions accepted at this position).\n *\n * Position-aware: nested-only registrations (e.g. `cell` registered\n * only inside `table.row.of`) are recognized via the same descent\n * primitive used by all parent-aware traversal.\n *\n * Returns `undefined` when `path` has no registered-container ancestor\n * (i.e. is at the document root) or when descent hits a leaf-resolved\n * ancestor.\n */\nexport function getEnclosingContainer(\n snapshot: TraversalSnapshot,\n path: Path,\n):\n | {\n of: ReadonlyArray<OfDefinition>\n path: Path\n }\n | undefined {\n const descent = descendToParent(snapshot, path)\n if (!descent) {\n return undefined\n }\n return {\n of: descent.parent.field.of,\n path: descent.parentPath,\n }\n}\n","import {getSubSchema, type Schema} from '@portabletext/schema'\nimport type {Path} from '../engine/interfaces/path'\nimport {getEnclosingContainer} from '../schema/get-enclosing-container'\nimport type {TraversalSnapshot} from './traversal-snapshot'\n\n/**\n * Return the `Schema` view that applies at a given path.\n *\n * For paths at the root of the document, or for paths where no ancestor is\n * a registered container, returns the top-level schema. For paths inside a\n * container, walks ancestors to find the nearest container and returns the\n * sub-schema derived from its `of` declaration.\n *\n * @beta\n */\nexport function getPathSubSchema(\n snapshot: TraversalSnapshot,\n path: Path,\n): Schema {\n const enclosing = getEnclosingContainer(snapshot, path)\n\n if (!enclosing) {\n return snapshot.context.schema\n }\n\n return getSubSchema(snapshot.context.schema, enclosing.of)\n}\n"],"names":["getAncestors","snapshot","path","keyedIndices","i","length","isKeyedSegment","push","context","blockIndexMap","currentChildren","value","isRootLevel","currentParent","ancestorsByDepth","resolvedPath","targetKeyedIndex","segmentIndex","segment","node","size","index","get","_key","undefined","find","child","at","next","getNodeChildren","slice","children","parent","reverse","hasNode","getNode","resolveContainerAt","containers","resolved","resolveNodeEntry","fieldValue","field","name","Array","isArray","of","entry","type","_type","isEditableContainer","_node","isObject","isTypedObject","schema","block","span","isTextBlockNode","isAncestorPath","another","otherSegment","getNodes","options","from","to","match","getNodesSimple","getNodesInRange","getChildren","entries","comparePathsInTree","pathA","pathB","keysA","filter","keysB","minDepth","Math","min","depth","keyA","keyB","matchedNode","has","c","indexA","indexB","sibling","canStopTraversal","couldContainInRangeNodes","isInRange","nodePath","getSibling","direction","lastSegment","parentPath","currentIndex","findIndex","siblingIndex","isSpanNode","isBlock","getParent","getBlock","isInline","getEnclosingBlock","mode","ancestors","ancestor","direct","descendToParent","getEnclosingContainer","descent","getPathSubSchema","enclosing","getSubSchema"],"mappings":";;AAuBO,SAASA,aACdC,UACAC,MAC8C;AAE9C,QAAMC,eAA8B,CAAA;AACpC,WAASC,IAAI,GAAGA,IAAIF,KAAKG,QAAQD;AAC3BE,mBAAeJ,KAAKE,CAAC,CAAC,KACxBD,aAAaI,KAAKH,CAAC;AAKvB,MAAID,aAAaE,UAAU;AACzB,WAAO,CAAA;AAGT,QAAM;AAAA,IAACG;AAAAA,IAASC;AAAAA,EAAAA,IAAiBR;AACjC,MAAIS,kBAA+BF,QAAQG,OACvCC,cAAc,IACdC;AAIJ,QAAMC,mBAAiE,IACjEC,eAAqB,CAAA,GAIrBC,mBAAmBb,aAAaA,aAAaE,SAAS,CAAC;AAE7D,MAAIY,eAAe;AACnB,SAAOA,eAAeD,oBAAkB;AACtC,UAAME,UAAUhB,KAAKe,YAAY;AAEjC,QAAI,OAAOC,WAAY,UAAU;AAC/BH,mBAAaR,KAAKW,OAAO,GACzBD;AACA;AAAA,IACF;AAEA,QAAIE;AACJ,QAAIb,eAAeY,OAAO,GAAG;AAO3B,UAAIN,eAAeH,cAAcW,SAASV,gBAAgBL,QAAQ;AAChE,cAAMgB,QAAQZ,cAAca,IAAIJ,QAAQK,IAAI;AAC5CJ,eACEE,UAAUG,SACNd,gBAAgBW,KAAK,IACrBX,gBAAgBe,KAAMC,CAAAA,UAAUA,MAAMH,SAASL,QAAQK,IAAI;AAAA,MACnE;AACEJ,eAAOT,gBA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G,aAAaC,UAAUC,IAAI;AAC7C,aAAWwG,YAAYD,WAAW;AAChC,QAAI,CAACtD,SAASlD,UAAUyG,SAASvF,IAAI;AACnC;AAEF,UAAMmB,WAAWF,mBACfnC,SAASO,QAAQ6B,YACjBpC,SAASO,QAAQG,OACjB+F,SAASxG,IACX;AACA,WAAI,CAACoC,YAAY,EAAE,WAAWA,YAC5B,SAEK;AAAA,MAACN,QAAQM;AAAAA,MAAUwD,YAAYY,SAASxG;AAAAA,IAAAA;AAAAA,EACjD;AAEF;ACtBO,SAAS2G,sBACd5G,UACAC,MAMY;AACZ,QAAM4G,UAAUF,gBAAgB3G,UAAUC,IAAI;AAC9C,MAAK4G;AAGL,WAAO;AAAA,MACLjE,IAAIiE,QAAQ9E,OAAOS,MAAMI;AAAAA,MACzB3C,MAAM4G,QAAQhB;AAAAA,IAAAA;AAElB;ACnBO,SAASiB,iBACd9G,UACAC,MACQ;AACR,QAAM8G,YAAYH,sBAAsB5G,UAAUC,IAAI;AAEtD,SAAK8G,YAIEC,aAAahH,SAASO,QAAQ6C,QAAQ2D,UAAUnE,EAAE,IAHhD5C,SAASO,QAAQ6C;AAI5B;"}
@@ -1,6 +1,6 @@
1
- import { resolveContainerAt, isInline, isSpanNode, getEnclosingBlock, hasNode, getBlock, getNodes, parentPath, getPathSubSchema, isTextBlockNode, getSibling } from "./get-path-sub-schema.js";
2
- import { isKeyedSegment, getNode, isObjectNode, getNodeChildren, getChildren } from "./get-node.js";
3
- import { isEqualPaths, getSelectionStartPoint as getSelectionStartPoint$1, getSelectionEndPoint as getSelectionEndPoint$1, sliceBlocks, getBlockStartPoint, getBlockEndPoint, isEqualSelectionPoints, isSelectionCollapsed as isSelectionCollapsed$1, blockOffsetToSpanSelectionPoint, getAncestorTextBlock, spanSelectionPointToBlockOffset, isListBlock } from "./util.slice-blocks.js";
1
+ import { isInline, isSpanNode, isObject, getEnclosingBlock, resolveContainerAt, hasNode, getBlock, getNodes, getSibling, getPathSubSchema, isTextBlockNode, isEditableContainer } from "./get-path-sub-schema.js";
2
+ import { isKeyedSegment, getNode, getNodeChildren, getParent, getChildren, parentPath } from "./get-parent.js";
3
+ import { isEqualPaths, getSelectionStartPoint as getSelectionStartPoint$1, getSelectionEndPoint as getSelectionEndPoint$1, sliceBlocks, getBlockStartPoint, getBlockEndPoint, isEqualSelectionPoints, isSelectionCollapsed as isSelectionCollapsed$1, blockOffsetToSpanSelectionPoint, spanSelectionPointToBlockOffset, isListBlock } from "./util.slice-blocks.js";
4
4
  import { isSpan, isTextBlock } from "@portabletext/schema";
5
5
  function comparePaths(path, another, root) {
6
6
  const min = Math.min(path.length, another.length);
@@ -69,12 +69,6 @@ function rangeEdges(range, root) {
69
69
  } = range;
70
70
  return isBackwardRange(range, root) ? [focus, anchor] : [anchor, focus];
71
71
  }
72
- function isEditableContainer(snapshot, _node, path) {
73
- if (snapshot.context.containers.size === 0)
74
- return !1;
75
- const resolved = resolveContainerAt(snapshot.context.containers, snapshot.context.value, path);
76
- return !!(resolved && "field" in resolved);
77
- }
78
72
  function rangesOverlap(rangeA, rangeB, root) {
79
73
  const [startA, endA] = rangeEdges(rangeA, root), [startB, endB] = rangeEdges(rangeB, root);
80
74
  return comparePoints(startA, endB, root) <= 0 && comparePoints(startB, endA, root) <= 0;
@@ -82,11 +76,9 @@ function rangesOverlap(rangeA, rangeB, root) {
82
76
  const isSelectionCollapsed = (snapshot) => snapshot.context.selection ? isEqualPaths(snapshot.context.selection.anchor.path, snapshot.context.selection.focus.path) && snapshot.context.selection.anchor.offset === snapshot.context.selection.focus.offset : !1;
83
77
  function getInline(snapshot, path) {
84
78
  const entry = getNode(snapshot, path);
85
- if (!(!entry || !isInline(snapshot, path)) && !(!isSpanNode({
79
+ if (!(!entry || !isInline(snapshot, entry.path)) && !(!isSpanNode({
86
80
  schema: snapshot.context.schema
87
- }, entry.node) && !isObjectNode({
88
- schema: snapshot.context.schema
89
- }, entry.node)))
81
+ }, entry.node) && !isObject(snapshot, entry.node)))
90
82
  return {
91
83
  node: entry.node,
92
84
  path: entry.path
@@ -383,29 +375,26 @@ function isRecord(value) {
383
375
  function isSelectionExpanded(selection) {
384
376
  return selection ? !isSelectionCollapsed$1(selection) : !1;
385
377
  }
386
- function findSibling(snapshot, path, direction, match) {
387
- if (path.length === 0)
388
- return;
389
- const lastSegment = path.at(-1);
390
- if (!isKeyedSegment(lastSegment))
391
- return;
392
- const parent = parentPath(path), children = getChildren(snapshot, parent), currentIndex = children.findIndex((child) => child.node._key === lastSegment._key);
393
- return currentIndex === -1 ? void 0 : (direction === "next" ? children.slice(currentIndex + 1) : children.slice(0, currentIndex).reverse()).find(match);
394
- }
395
378
  const getSelectionEndPoint = (snapshot) => {
396
379
  if (snapshot.context.selection)
397
380
  return snapshot.context.selection.backward ? snapshot.context.selection.anchor : snapshot.context.selection.focus;
398
381
  }, getNextSpan = (snapshot) => {
399
382
  const point = getSelectionEndPoint(snapshot);
400
383
  if (point)
401
- return findSibling(snapshot, point.path, "next", (entry) => isSpan(snapshot.context, entry.node));
384
+ return getSibling(snapshot, point.path, {
385
+ direction: "next",
386
+ match: (node) => isSpan(snapshot.context, node)
387
+ });
402
388
  }, getSelectionStartPoint = (snapshot) => {
403
389
  if (snapshot.context.selection)
404
390
  return snapshot.context.selection.backward ? snapshot.context.selection.focus : snapshot.context.selection.anchor;
405
391
  }, getPreviousSpan = (snapshot) => {
406
392
  const point = getSelectionStartPoint(snapshot);
407
393
  if (point)
408
- return findSibling(snapshot, point.path, "previous", (entry) => isSpan(snapshot.context, entry.node));
394
+ return getSibling(snapshot, point.path, {
395
+ direction: "previous",
396
+ match: (node) => isSpan(snapshot.context, node)
397
+ });
409
398
  };
410
399
  function getSelectedChildren(options) {
411
400
  const filter = options?.filter;
@@ -491,7 +480,11 @@ const getSelectedSpans = (snapshot) => snapshot.context.selection ? getSelectedC
491
480
  selection
492
481
  }
493
482
  }).map((span) => {
494
- const block = getAncestorTextBlock(snapshot, span.path);
483
+ const block = getParent(snapshot, span.path, {
484
+ match: (node) => isTextBlock({
485
+ schema: snapshot.context.schema
486
+ }, node)
487
+ });
495
488
  return {
496
489
  marks: span.node.marks ?? [],
497
490
  decoratorNames: getPathSubSchema(snapshot, span.path).decorators.map((decorator) => decorator.name),
@@ -750,14 +743,16 @@ const getActiveAnnotations = (snapshot) => {
750
743
  return firstStyle;
751
744
  }, getNextInlineObject = (snapshot) => {
752
745
  const point = getSelectionEndPoint(snapshot);
753
- return point ? findSibling(snapshot, point.path, "next", (entry) => isObjectNode({
754
- schema: snapshot.context.schema
755
- }, entry.node)) : void 0;
746
+ return point ? getSibling(snapshot, point.path, {
747
+ direction: "next",
748
+ match: (node) => isObject(snapshot, node)
749
+ }) : void 0;
756
750
  }, getPreviousInlineObject = (snapshot) => {
757
751
  const point = getSelectionStartPoint(snapshot);
758
- return point ? findSibling(snapshot, point.path, "previous", (entry) => isObjectNode({
759
- schema: snapshot.context.schema
760
- }, entry.node)) : void 0;
752
+ return point ? getSibling(snapshot, point.path, {
753
+ direction: "previous",
754
+ match: (node) => isObject(snapshot, node)
755
+ }) : void 0;
761
756
  }, getSelectionText = (snapshot) => {
762
757
  const selectedValue = getSelectedValue(snapshot);
763
758
  return collectText(snapshot.context, selectedValue);
@@ -887,14 +882,18 @@ const getCaretWordSelection = (snapshot) => {
887
882
  const selectionEndBlock = getSelectionEndBlock(snapshot);
888
883
  if (!selectionEndBlock)
889
884
  return;
890
- const next = getSibling(snapshot, selectionEndBlock.path, "next");
885
+ const next = getSibling(snapshot, selectionEndBlock.path, {
886
+ direction: "next"
887
+ });
891
888
  if (next)
892
889
  return getBlock(snapshot, next.path);
893
890
  }, getPreviousBlock = (snapshot) => {
894
891
  const selectionStartBlock = getSelectionStartBlock(snapshot);
895
892
  if (!selectionStartBlock)
896
893
  return;
897
- const previous = getSibling(snapshot, selectionStartBlock.path, "previous");
894
+ const previous = getSibling(snapshot, selectionStartBlock.path, {
895
+ direction: "previous"
896
+ });
898
897
  if (previous)
899
898
  return getBlock(snapshot, previous.path);
900
899
  }, getSelectionEndChild = (snapshot) => {
@@ -1010,7 +1009,6 @@ export {
1010
1009
  isAtTheEndOfBlock,
1011
1010
  isAtTheStartOfBlock,
1012
1011
  isBackwardRange,
1013
- isEditableContainer,
1014
1012
  isOverlappingSelection,
1015
1013
  isSelectingEntireBlocks,
1016
1014
  isSelectionCollapsed,