@portabletext/editor 7.8.2 → 7.9.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.
- package/lib/_chunks-dts/behavior.types.action.d.ts +6 -6
- package/lib/_chunks-es/get-parent.js +18 -6
- package/lib/_chunks-es/get-parent.js.map +1 -1
- package/lib/traversal/index.d.ts +31 -6
- package/lib/traversal/index.d.ts.map +1 -1
- package/lib/traversal/index.js +23 -2
- package/lib/traversal/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as _portabletext_schema6 from "@portabletext/schema";
|
|
2
2
|
import { AnnotationDefinition, AnnotationSchemaType, AnnotationSchemaType as AnnotationSchemaType$1, BaseDefinition, BlockObjectDefinition, BlockObjectSchemaType, BlockObjectSchemaType as BlockObjectSchemaType$1, DecoratorDefinition, DecoratorSchemaType, DecoratorSchemaType as DecoratorSchemaType$1, FieldDefinition, FieldDefinition as FieldDefinition$1, InlineObjectDefinition, InlineObjectSchemaType, InlineObjectSchemaType as InlineObjectSchemaType$1, ListDefinition, ListSchemaType, ListSchemaType as ListSchemaType$1, OfDefinition, PortableTextBlock, PortableTextBlock as PortableTextBlock$1, PortableTextChild, PortableTextChild as PortableTextChild$1, PortableTextObject, PortableTextObject as PortableTextObject$1, PortableTextSpan, PortableTextSpan as PortableTextSpan$1, PortableTextTextBlock, PortableTextTextBlock as PortableTextTextBlock$1, Schema, SchemaDefinition, SchemaDefinition as SchemaDefinition$1, StyleDefinition, StyleSchemaType, StyleSchemaType as StyleSchemaType$1, TypedObject, defineSchema } from "@portabletext/schema";
|
|
3
3
|
import * as xstate31 from "xstate";
|
|
4
4
|
import { ActorRefFrom } from "xstate";
|
|
5
|
-
import * as
|
|
5
|
+
import * as react7 from "react";
|
|
6
6
|
import React$1, { BaseSyntheticEvent, ClipboardEvent as ClipboardEvent$1, FocusEvent, JSX, PropsWithChildren, ReactElement, RefObject, TextareaHTMLAttributes } from "react";
|
|
7
7
|
import { Patch, Patch as Patch$1 } from "@portabletext/patches";
|
|
8
8
|
type MIMEType = `${string}/${string}`;
|
|
@@ -839,7 +839,7 @@ declare class PortableTextEditor {
|
|
|
839
839
|
*/
|
|
840
840
|
static delete: (editor: PortableTextEditor, selection: EditorSelection, options?: EditableAPIDeleteOptions) => void;
|
|
841
841
|
static findDOMNode: (editor: PortableTextEditor, element: PortableTextBlock | PortableTextChild) => Node | undefined;
|
|
842
|
-
static findByPath: (editor: PortableTextEditor, path: Path) => [
|
|
842
|
+
static findByPath: (editor: PortableTextEditor, path: Path) => [_portabletext_schema6.PortableTextTextBlock<PortableTextObject | _portabletext_schema6.PortableTextSpan> | PortableTextObject | _portabletext_schema6.PortableTextSpan | undefined, Path | undefined];
|
|
843
843
|
/**
|
|
844
844
|
* @deprecated
|
|
845
845
|
* Use `editor.send(...)` instead
|
|
@@ -1177,7 +1177,7 @@ type PortableTextEditableProps = Omit<TextareaHTMLAttributes<HTMLDivElement>, 'o
|
|
|
1177
1177
|
* ```
|
|
1178
1178
|
* @group Components
|
|
1179
1179
|
*/
|
|
1180
|
-
declare const PortableTextEditable:
|
|
1180
|
+
declare const PortableTextEditable: react7.ForwardRefExoticComponent<Omit<PortableTextEditableProps, "ref"> & react7.RefAttributes<Omit<HTMLDivElement, "as" | "onPaste" | "onBeforeInput">>>;
|
|
1181
1181
|
/** @beta */
|
|
1182
1182
|
interface EditableAPIDeleteOptions {
|
|
1183
1183
|
mode?: 'blocks' | 'children' | 'selected';
|
|
@@ -2506,7 +2506,7 @@ declare const editorMachine: xstate31.StateMachine<{
|
|
|
2506
2506
|
initialValue?: Array<PortableTextBlock>;
|
|
2507
2507
|
}, xstate31.NonReducibleUnknown, InternalPatchEvent | MutationEvent | PatchesEvent | {
|
|
2508
2508
|
type: "blurred";
|
|
2509
|
-
event:
|
|
2509
|
+
event: react7.FocusEvent<HTMLDivElement, Element>;
|
|
2510
2510
|
} | {
|
|
2511
2511
|
type: "done loading";
|
|
2512
2512
|
} | {
|
|
@@ -2518,7 +2518,7 @@ declare const editorMachine: xstate31.StateMachine<{
|
|
|
2518
2518
|
data: unknown;
|
|
2519
2519
|
} | {
|
|
2520
2520
|
type: "focused";
|
|
2521
|
-
event:
|
|
2521
|
+
event: react7.FocusEvent<HTMLDivElement, Element>;
|
|
2522
2522
|
} | {
|
|
2523
2523
|
type: "invalid value";
|
|
2524
2524
|
resolution: InvalidValueResolution | null;
|
|
@@ -95,13 +95,21 @@ function getNode(snapshot, path) {
|
|
|
95
95
|
context,
|
|
96
96
|
blockIndexMap
|
|
97
97
|
} = snapshot;
|
|
98
|
-
let currentChildren = context.value, node, currentParent;
|
|
98
|
+
let currentChildren = context.value, currentFieldName, node, currentParent;
|
|
99
99
|
const resolvedPath = [];
|
|
100
100
|
for (let i = 0; i < path.length; i++) {
|
|
101
101
|
const segment = path[i];
|
|
102
102
|
if (typeof segment == "string") {
|
|
103
|
-
|
|
104
|
-
|
|
103
|
+
if (currentFieldName !== void 0 && segment === currentFieldName) {
|
|
104
|
+
resolvedPath.push(segment);
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
for (let j = i + 1; j < path.length; j++) {
|
|
108
|
+
const s = path[j];
|
|
109
|
+
if (isKeyedSegment(s) || typeof s == "number")
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
break;
|
|
105
113
|
}
|
|
106
114
|
if (isKeyedSegment(segment)) {
|
|
107
115
|
resolvedPath.push(segment);
|
|
@@ -129,14 +137,18 @@ function getNode(snapshot, path) {
|
|
|
129
137
|
const next = getNodeChildren(context, node, currentParent);
|
|
130
138
|
if (!next)
|
|
131
139
|
return;
|
|
132
|
-
currentChildren = next.children, currentParent = next.parent;
|
|
133
|
-
}
|
|
140
|
+
currentChildren = next.children, currentFieldName = next.fieldName, currentParent = next.parent;
|
|
141
|
+
} else
|
|
142
|
+
currentFieldName = void 0;
|
|
134
143
|
}
|
|
135
|
-
if (node)
|
|
144
|
+
if (node) {
|
|
145
|
+
for (; resolvedPath.length > 0 && typeof resolvedPath[resolvedPath.length - 1] == "string"; )
|
|
146
|
+
resolvedPath.pop();
|
|
136
147
|
return {
|
|
137
148
|
node,
|
|
138
149
|
path: resolvedPath
|
|
139
150
|
};
|
|
151
|
+
}
|
|
140
152
|
}
|
|
141
153
|
function parentPath(path) {
|
|
142
154
|
if (path.length === 0)
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"get-parent.js","sources":["../../src/utils/util.is-keyed-segment.ts","../../src/paths/serialize-path.ts","../../src/utils/asserters.ts","../../src/traversal/get-container-children.ts","../../src/traversal/get-children.ts","../../src/traversal/get-node.ts","../../src/engine/path/parent-path.ts","../../src/traversal/get-parent.ts"],"sourcesContent":["import type {KeyedSegment} from '../types/paths'\n\n/**\n * @public\n */\nexport function isKeyedSegment(segment: unknown): segment is KeyedSegment {\n return typeof segment === 'object' && segment !== null && '_key' in segment\n}\n","import type {Path} from '../types/paths'\nimport {isKeyedSegment} from '../utils/util.is-keyed-segment'\n\n/**\n * Serialize a keyed path to a string using Sanity's bracket notation.\n *\n * - `[{_key: 'k0'}]` -> `[_key==\"k0\"]`\n * - `[{_key: 'k0'}, 'children', {_key: 's0'}]` -> `[_key==\"k0\"].children[_key==\"s0\"]`\n * - `[{_key: 't0'}, 'rows', {_key: 'r0'}, 'cells', {_key: 'c0'}, 'content', {_key: 'b0'}, 'children', {_key: 's0'}]` -> `[_key==\"t0\"].rows[_key==\"r0\"].cells[_key==\"c0\"].content[_key==\"b0\"].children[_key==\"s0\"]`\n */\nexport function serializePath(path: Path): string {\n return path.reduce<string>((result, segment, index) => {\n if (isKeyedSegment(segment)) {\n return `${result}[_key==\"${segment._key}\"]`\n }\n\n const separator = index === 0 ? '' : '.'\n return `${result}${separator}${segment}`\n }, '')\n}\n","import type {TypedObject} from '@portabletext/schema'\n\nexport function isTypedObject(object: unknown): object is TypedObject {\n return isRecord(object) && typeof object['_type'] === 'string'\n}\n\nexport function isRecord(value: unknown): value is Record<string, unknown> {\n return !!value && (typeof value === 'object' || typeof value === 'function')\n}\n","import type {Node} from '../engine/interfaces/node'\nimport type {\n Containers,\n RegisteredContainer,\n} from '../schema/resolve-containers'\n\n/**\n * Resolve a container node's editable child array.\n *\n * Returns `{children, container}` for a node registered as a container:\n * `children` is the node's editable child array, and `container` is the\n * node's own container registration. Read `container.field.name` for the\n * path segment that reaches `children`, and thread `container` back in as\n * `parent` when descending into them.\n *\n * Returns `undefined` for anything that is not a container: text blocks,\n * spans, leaves, and unregistered objects. It is node-based and resolves\n * in one step with no path re-walk, so recursive descent over containers\n * is linear in nesting depth. The caller seeds the document root from\n * `context.value` itself.\n *\n * @beta\n */\nexport function getContainerChildren(\n containers: Containers,\n node: Node,\n parent?: RegisteredContainer,\n):\n | {\n children: Array<Node>\n container: RegisteredContainer\n }\n | undefined {\n const resolved = resolveNodeContainer(containers, parent, node)\n\n if (!resolved) {\n return undefined\n }\n\n const fieldValue = (node as Record<string, unknown>)[resolved.field.name]\n\n if (!Array.isArray(fieldValue)) {\n return undefined\n }\n\n return {\n children: fieldValue as Array<Node>,\n container: resolved,\n }\n}\n\n/**\n * Pick the positional override from `parent.of` if present; fall back\n * to the top-level entry. Returns only `RegisteredContainer` entries\n * since leaves do not have editable children.\n */\nfunction resolveNodeContainer(\n containers: Containers,\n parent: RegisteredContainer | undefined,\n node: Node,\n): RegisteredContainer | undefined {\n if (parent?.of) {\n for (const entry of parent.of) {\n if (entry.type === node._type) {\n // Only return container entries; leaves have no editable children.\n if ('field' in entry) {\n return entry\n }\n return undefined\n }\n }\n }\n return containers.get(node._type)\n}\n","import {isTextBlock} from '@portabletext/schema'\nimport type {EditorSchema} from '../editor/editor-schema'\nimport type {Node} from '../engine/interfaces/node'\nimport type {Path} from '../engine/interfaces/path'\nimport {serializePath} from '../paths/serialize-path'\nimport type {\n Containers,\n RegisteredContainer,\n} from '../schema/resolve-containers'\nimport {isTypedObject} from '../utils/asserters'\nimport {isKeyedSegment} from '../utils/util.is-keyed-segment'\nimport {getContainerChildren} from './get-container-children'\nimport type {TraversalSnapshot} from './traversal-snapshot'\n\n/**\n * Get the children of a node at a given path.\n *\n * @beta\n */\nexport function getChildren(\n snapshot: TraversalSnapshot,\n path: Path,\n): Array<{node: Node; path: Path}> {\n let currentChildren: Array<Node> = snapshot.context.value\n let currentFieldName = 'value'\n let currentPath: Path = []\n let isRoot = true\n let currentParent: RegisteredContainer | undefined\n\n for (const segment of path) {\n if (typeof segment === 'string') {\n continue\n }\n\n let node: Node | undefined\n if (isKeyedSegment(segment)) {\n // Resolve via `blockIndexMap` (O(1)) and fall back to a linear scan on a\n // miss or when the snapshot's map disagrees with the value, mirroring\n // `getNode`. The candidate path is this segment's full keyed path.\n const candidatePath: Path = isRoot\n ? [{_key: segment._key}]\n : [...currentPath, currentFieldName, {_key: segment._key}]\n const index = snapshot.blockIndexMap.get(serializePath(candidatePath))\n node =\n index !== undefined && currentChildren[index]?._key === segment._key\n ? currentChildren[index]\n : currentChildren.find((child) => child._key === segment._key)\n } else if (typeof segment === 'number') {\n node = currentChildren.at(segment)\n }\n\n if (!node) {\n return []\n }\n\n currentPath = isRoot\n ? [{_key: node._key}]\n : [...currentPath, currentFieldName, {_key: node._key}]\n isRoot = false\n\n const next = getNodeChildren(snapshot.context, node, currentParent)\n\n if (!next) {\n return []\n }\n\n currentChildren = next.children\n currentFieldName = next.fieldName\n currentParent = next.parent\n }\n\n return currentChildren.map((child) => ({\n node: child,\n path: isRoot\n ? [{_key: child._key}]\n : [...currentPath, currentFieldName, {_key: child._key}],\n }))\n}\n\n/**\n * Resolve a node's editable child array.\n *\n * When `parent` is provided and its `of` declares a positional entry\n * matching `node._type`, that positional entry's `field` is used.\n * Otherwise the top-level `containers.get(node._type)` provides the\n * fallback.\n *\n * The returned `parent` is the resolved container entry for `node`\n * itself (used by the caller to thread further descent).\n *\n * Internal descent kernel shared by the positional traversal utilities\n * (`getChildren`, `getNode`, `getNodes`, `getAncestors`); it folds the\n * text-block, container, and root-document cases. Public consumers that\n * only need to descend containers use {@link getContainerChildren}.\n */\nexport function getNodeChildren(\n context: {\n schema: EditorSchema\n containers: Containers\n },\n node: Node | {value: Array<Node>},\n parent?: RegisteredContainer,\n):\n | {\n children: Array<Node>\n fieldName: string\n parent: RegisteredContainer | undefined\n }\n | undefined {\n // Text blocks store children in .children\n if (isTextBlock(context, node)) {\n return {\n children: node.children,\n fieldName: 'children',\n parent: undefined,\n }\n }\n\n if (isTypedObject(node)) {\n const result = getContainerChildren(context.containers, node, parent)\n\n if (result) {\n return {\n children: result.children,\n fieldName: result.container.field.name,\n parent: result.container,\n }\n }\n }\n\n // Root context: has .value array but no _key or _type\n if (\n 'value' in node &&\n Array.isArray(node['value']) &&\n !('_key' in node) &&\n !('_type' in node)\n ) {\n return {\n children: node['value'] as Array<Node>,\n fieldName: 'value',\n parent: undefined,\n }\n }\n\n return undefined\n}\n","import type {Node} from '../engine/interfaces/node'\nimport type {Path} from '../engine/interfaces/path'\nimport {serializePath} from '../paths/serialize-path'\nimport type {RegisteredContainer} from '../schema/resolve-containers'\nimport {isKeyedSegment} from '../utils/util.is-keyed-segment'\nimport {getNodeChildren} from './get-children'\nimport type {TraversalSnapshot} from './traversal-snapshot'\n\n/**\n * Get the node at a given path.\n *\n * The path can be either keyed (KeyedSegment + field name strings) or\n * indexed (numbers). Keyed segments are resolved by matching `_key`,\n * field name strings are skipped (they're structural), and numbers\n * are resolved by index.\n *\n * The returned path is always fully keyed, even if the input path\n * contained numeric indices.\n *\n * @beta\n */\nexport function getNode(\n snapshot: TraversalSnapshot,\n path: Path,\n): {node: Node; path: Path} | undefined {\n if (path.length === 0) {\n return undefined\n }\n\n const {context, blockIndexMap} = snapshot\n let currentChildren: Array<Node> = context.value\n let node: Node | undefined\n let currentParent: RegisteredContainer | undefined\n const resolvedPath: Path = []\n\n for (let i = 0; i < path.length; i++) {\n const segment = path[i]\n\n if (typeof segment === 'string') {\n resolvedPath.push(segment)\n continue\n }\n\n if (isKeyedSegment(segment)) {\n resolvedPath.push(segment)\n const index = blockIndexMap.get(serializePath(resolvedPath))\n if (\n index !== undefined &&\n currentChildren[index]?._key === segment._key\n ) {\n node = currentChildren[index]\n } else {\n // The map can miss (unkeyed transient nodes, e.g. `{_type:'table'}`\n // inserted by a remote patch before normalize mints a key) or\n // disagree with the traversed value (snapshots that pair the live\n // map with a pre-apply value, e.g. `textPatch`). Fall back to a\n // linear scan in both cases.\n node = currentChildren.find((child) => child._key === segment._key)\n if (node && node._key !== undefined) {\n resolvedPath[resolvedPath.length - 1] = {_key: node._key}\n }\n }\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 undefined\n }\n\n if (!node) {\n return undefined\n }\n\n let hasMoreSegments = false\n for (let j = i + 1; j < path.length; j++) {\n const s = path[j]\n if (isKeyedSegment(s) || typeof s === 'number') {\n hasMoreSegments = true\n break\n }\n }\n\n if (hasMoreSegments) {\n const next = getNodeChildren(context, node, currentParent)\n\n if (!next) {\n return undefined\n }\n\n currentChildren = next.children\n currentParent = next.parent\n }\n }\n\n if (!node) {\n return undefined\n }\n\n return {node, path: resolvedPath}\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 {PortableTextBlock} from '@portabletext/schema'\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 * A parent has children, so it is always a `PortableTextBlock` (text block\n * or object node).\n *\n * When `match` is provided and the parent does not satisfy it, returns\n * `undefined`.\n *\n * @beta\n */\nexport function getParent<TMatch extends PortableTextBlock>(\n snapshot: TraversalSnapshot,\n path: Path,\n options: {\n match: (node: PortableTextBlock, path: Path) => node is TMatch\n },\n): {node: TMatch; path: Path} | undefined\n/**\n * @beta\n */\nexport function getParent(\n snapshot: TraversalSnapshot,\n path: Path,\n options?: {\n match?: (node: PortableTextBlock, path: Path) => boolean\n },\n): {node: PortableTextBlock; path: Path} | undefined\nexport function getParent(\n snapshot: TraversalSnapshot,\n path: Path,\n options?: {\n match?: (node: PortableTextBlock, path: Path) => boolean\n },\n): {node: PortableTextBlock; 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 const entry = getNode(snapshot, parent)\n\n if (!entry) {\n return undefined\n }\n\n const result = {node: entry.node as PortableTextBlock, path: entry.path}\n\n if (options?.match && !options.match(result.node, result.path)) {\n return undefined\n }\n\n return result\n}\n"],"names":["isKeyedSegment","segment","serializePath","path","reduce","result","index","_key","isTypedObject","object","isRecord","value","getContainerChildren","containers","node","parent","resolved","resolveNodeContainer","fieldValue","field","name","Array","isArray","children","container","of","entry","type","_type","get","getChildren","snapshot","currentChildren","context","currentFieldName","currentPath","isRoot","currentParent","candidatePath","blockIndexMap","undefined","find","child","at","next","getNodeChildren","fieldName","map","isTextBlock","getNode","length","resolvedPath","i","push","hasMoreSegments","j","s","parentPath","Error","lastNodeIndex","slice","getParent","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A2D,SAGmD;AACnD,MAAI3D,KAAK+C,WAAW;AAClB;AAGF,QAAMnC,SAAS0C,WAAWtD,IAAI;AAE9B,MAAIY,OAAOmC,WAAW;AACpB;AAGF,QAAMxB,QAAQuB,QAAQlB,UAAUhB,MAAM;AAEtC,MAAI,CAACW;AACH;AAGF,QAAMrB,SAAS;AAAA,IAACS,MAAMY,MAAMZ;AAAAA,IAA2BX,MAAMuB,MAAMvB;AAAAA,EAAAA;AAEnE,MAAI2D,EAAAA,SAASC,SAAS,CAACD,QAAQC,MAAM1D,OAAOS,MAAMT,OAAOF,IAAI;AAI7D,WAAOE;AACT;"}
|
|
1
|
+
{"version":3,"file":"get-parent.js","sources":["../../src/utils/util.is-keyed-segment.ts","../../src/paths/serialize-path.ts","../../src/utils/asserters.ts","../../src/traversal/get-container-children.ts","../../src/traversal/get-children.ts","../../src/traversal/get-node.ts","../../src/engine/path/parent-path.ts","../../src/traversal/get-parent.ts"],"sourcesContent":["import type {KeyedSegment} from '../types/paths'\n\n/**\n * @public\n */\nexport function isKeyedSegment(segment: unknown): segment is KeyedSegment {\n return typeof segment === 'object' && segment !== null && '_key' in segment\n}\n","import type {Path} from '../types/paths'\nimport {isKeyedSegment} from '../utils/util.is-keyed-segment'\n\n/**\n * Serialize a keyed path to a string using Sanity's bracket notation.\n *\n * - `[{_key: 'k0'}]` -> `[_key==\"k0\"]`\n * - `[{_key: 'k0'}, 'children', {_key: 's0'}]` -> `[_key==\"k0\"].children[_key==\"s0\"]`\n * - `[{_key: 't0'}, 'rows', {_key: 'r0'}, 'cells', {_key: 'c0'}, 'content', {_key: 'b0'}, 'children', {_key: 's0'}]` -> `[_key==\"t0\"].rows[_key==\"r0\"].cells[_key==\"c0\"].content[_key==\"b0\"].children[_key==\"s0\"]`\n */\nexport function serializePath(path: Path): string {\n return path.reduce<string>((result, segment, index) => {\n if (isKeyedSegment(segment)) {\n return `${result}[_key==\"${segment._key}\"]`\n }\n\n const separator = index === 0 ? '' : '.'\n return `${result}${separator}${segment}`\n }, '')\n}\n","import type {TypedObject} from '@portabletext/schema'\n\nexport function isTypedObject(object: unknown): object is TypedObject {\n return isRecord(object) && typeof object['_type'] === 'string'\n}\n\nexport function isRecord(value: unknown): value is Record<string, unknown> {\n return !!value && (typeof value === 'object' || typeof value === 'function')\n}\n","import type {Node} from '../engine/interfaces/node'\nimport type {\n Containers,\n RegisteredContainer,\n} from '../schema/resolve-containers'\n\n/**\n * Resolve a container node's editable child array.\n *\n * Returns `{children, container}` for a node registered as a container:\n * `children` is the node's editable child array, and `container` is the\n * node's own container registration. Read `container.field.name` for the\n * path segment that reaches `children`, and thread `container` back in as\n * `parent` when descending into them.\n *\n * Returns `undefined` for anything that is not a container: text blocks,\n * spans, leaves, and unregistered objects. It is node-based and resolves\n * in one step with no path re-walk, so recursive descent over containers\n * is linear in nesting depth. The caller seeds the document root from\n * `context.value` itself.\n *\n * @beta\n */\nexport function getContainerChildren(\n containers: Containers,\n node: Node,\n parent?: RegisteredContainer,\n):\n | {\n children: Array<Node>\n container: RegisteredContainer\n }\n | undefined {\n const resolved = resolveNodeContainer(containers, parent, node)\n\n if (!resolved) {\n return undefined\n }\n\n const fieldValue = (node as Record<string, unknown>)[resolved.field.name]\n\n if (!Array.isArray(fieldValue)) {\n return undefined\n }\n\n return {\n children: fieldValue as Array<Node>,\n container: resolved,\n }\n}\n\n/**\n * Pick the positional override from `parent.of` if present; fall back\n * to the top-level entry. Returns only `RegisteredContainer` entries\n * since leaves do not have editable children.\n */\nfunction resolveNodeContainer(\n containers: Containers,\n parent: RegisteredContainer | undefined,\n node: Node,\n): RegisteredContainer | undefined {\n if (parent?.of) {\n for (const entry of parent.of) {\n if (entry.type === node._type) {\n // Only return container entries; leaves have no editable children.\n if ('field' in entry) {\n return entry\n }\n return undefined\n }\n }\n }\n return containers.get(node._type)\n}\n","import {isTextBlock} from '@portabletext/schema'\nimport type {EditorSchema} from '../editor/editor-schema'\nimport type {Node} from '../engine/interfaces/node'\nimport type {Path} from '../engine/interfaces/path'\nimport {serializePath} from '../paths/serialize-path'\nimport type {\n Containers,\n RegisteredContainer,\n} from '../schema/resolve-containers'\nimport {isTypedObject} from '../utils/asserters'\nimport {isKeyedSegment} from '../utils/util.is-keyed-segment'\nimport {getContainerChildren} from './get-container-children'\nimport type {TraversalSnapshot} from './traversal-snapshot'\n\n/**\n * Get the children of a node at a given path.\n *\n * @beta\n */\nexport function getChildren(\n snapshot: TraversalSnapshot,\n path: Path,\n): Array<{node: Node; path: Path}> {\n let currentChildren: Array<Node> = snapshot.context.value\n let currentFieldName = 'value'\n let currentPath: Path = []\n let isRoot = true\n let currentParent: RegisteredContainer | undefined\n\n for (const segment of path) {\n if (typeof segment === 'string') {\n continue\n }\n\n let node: Node | undefined\n if (isKeyedSegment(segment)) {\n // Resolve via `blockIndexMap` (O(1)) and fall back to a linear scan on a\n // miss or when the snapshot's map disagrees with the value, mirroring\n // `getNode`. The candidate path is this segment's full keyed path.\n const candidatePath: Path = isRoot\n ? [{_key: segment._key}]\n : [...currentPath, currentFieldName, {_key: segment._key}]\n const index = snapshot.blockIndexMap.get(serializePath(candidatePath))\n node =\n index !== undefined && currentChildren[index]?._key === segment._key\n ? currentChildren[index]\n : currentChildren.find((child) => child._key === segment._key)\n } else if (typeof segment === 'number') {\n node = currentChildren.at(segment)\n }\n\n if (!node) {\n return []\n }\n\n currentPath = isRoot\n ? [{_key: node._key}]\n : [...currentPath, currentFieldName, {_key: node._key}]\n isRoot = false\n\n const next = getNodeChildren(snapshot.context, node, currentParent)\n\n if (!next) {\n return []\n }\n\n currentChildren = next.children\n currentFieldName = next.fieldName\n currentParent = next.parent\n }\n\n return currentChildren.map((child) => ({\n node: child,\n path: isRoot\n ? [{_key: child._key}]\n : [...currentPath, currentFieldName, {_key: child._key}],\n }))\n}\n\n/**\n * Resolve a node's editable child array.\n *\n * When `parent` is provided and its `of` declares a positional entry\n * matching `node._type`, that positional entry's `field` is used.\n * Otherwise the top-level `containers.get(node._type)` provides the\n * fallback.\n *\n * The returned `parent` is the resolved container entry for `node`\n * itself (used by the caller to thread further descent).\n *\n * Internal descent kernel shared by the positional traversal utilities\n * (`getChildren`, `getNode`, `getNodes`, `getAncestors`); it folds the\n * text-block, container, and root-document cases. Public consumers that\n * only need to descend containers use {@link getContainerChildren}.\n */\nexport function getNodeChildren(\n context: {\n schema: EditorSchema\n containers: Containers\n },\n node: Node | {value: Array<Node>},\n parent?: RegisteredContainer,\n):\n | {\n children: Array<Node>\n fieldName: string\n parent: RegisteredContainer | undefined\n }\n | undefined {\n // Text blocks store children in .children\n if (isTextBlock(context, node)) {\n return {\n children: node.children,\n fieldName: 'children',\n parent: undefined,\n }\n }\n\n if (isTypedObject(node)) {\n const result = getContainerChildren(context.containers, node, parent)\n\n if (result) {\n return {\n children: result.children,\n fieldName: result.container.field.name,\n parent: result.container,\n }\n }\n }\n\n // Root context: has .value array but no _key or _type\n if (\n 'value' in node &&\n Array.isArray(node['value']) &&\n !('_key' in node) &&\n !('_type' in node)\n ) {\n return {\n children: node['value'] as Array<Node>,\n fieldName: 'value',\n parent: undefined,\n }\n }\n\n return undefined\n}\n","import type {Node} from '../engine/interfaces/node'\nimport type {Path} from '../engine/interfaces/path'\nimport {serializePath} from '../paths/serialize-path'\nimport type {RegisteredContainer} from '../schema/resolve-containers'\nimport {isKeyedSegment} from '../utils/util.is-keyed-segment'\nimport {getNodeChildren} from './get-children'\nimport type {TraversalSnapshot} from './traversal-snapshot'\n\n/**\n * Get the node at a given path.\n *\n * The path can be either keyed (KeyedSegment + field name strings) or\n * indexed (numbers). Keyed segments are resolved by matching `_key`,\n * field name strings name a structural descent into the previous\n * node's children, and numbers are resolved by index.\n *\n * The returned `path` always identifies the returned node: it's fully\n * keyed (numeric indices are converted to `KeyedSegment`s) and any\n * trailing segments in the input that point outside the value tree —\n * e.g. an object node's primitive field, or an annotation reached via\n * `'markDefs'` on a text block — are stripped so that\n * `getNode(snapshot, entry.path).node === entry.node`.\n *\n * The walk stops when a string segment names a field that isn't the\n * current node's structural child array. Annotations live in\n * `markDefs` on a text block, alongside `children` rather than inside\n * it, so `getNode` resolves an annotation path to the enclosing text\n * block. Use `getAnnotation` to resolve the annotation itself.\n *\n * @beta\n */\nexport function getNode(\n snapshot: TraversalSnapshot,\n path: Path,\n): {node: Node; path: Path} | undefined {\n if (path.length === 0) {\n return undefined\n }\n\n const {context, blockIndexMap} = snapshot\n let currentChildren: Array<Node> = context.value\n let currentFieldName: string | undefined\n let node: Node | undefined\n let currentParent: RegisteredContainer | undefined\n const resolvedPath: Path = []\n\n for (let i = 0; i < path.length; i++) {\n const segment = path[i]\n\n if (typeof segment === 'string') {\n // A string segment names a structural descent. If it matches the\n // field name produced by the previous node's `getNodeChildren`,\n // it's part of the value-tree descent — push it and continue.\n // Otherwise the string names a sidecar field (markDefs on a text\n // block, a primitive field on an object). If the input keeps\n // digging past it with more keyed/numeric segments, the caller\n // wanted a node inside the sidecar and `getNode` can't reach it\n // (use `getAnnotation` for annotations); return undefined.\n // Otherwise the rest is just trailing field names — let the loop\n // exit and the post-loop strip remove them.\n if (currentFieldName !== undefined && segment === currentFieldName) {\n resolvedPath.push(segment)\n continue\n }\n for (let j = i + 1; j < path.length; j++) {\n const s = path[j]\n if (isKeyedSegment(s) || typeof s === 'number') {\n return undefined\n }\n }\n break\n }\n\n if (isKeyedSegment(segment)) {\n resolvedPath.push(segment)\n const index = blockIndexMap.get(serializePath(resolvedPath))\n if (\n index !== undefined &&\n currentChildren[index]?._key === segment._key\n ) {\n node = currentChildren[index]\n } else {\n // The map can miss (unkeyed transient nodes, e.g. `{_type:'table'}`\n // inserted by a remote patch before normalize mints a key) or\n // disagree with the traversed value (snapshots that pair the live\n // map with a pre-apply value, e.g. `textPatch`). Fall back to a\n // linear scan in both cases.\n node = currentChildren.find((child) => child._key === segment._key)\n if (node && node._key !== undefined) {\n resolvedPath[resolvedPath.length - 1] = {_key: node._key}\n }\n }\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 undefined\n }\n\n if (!node) {\n return undefined\n }\n\n let hasMoreSegments = false\n for (let j = i + 1; j < path.length; j++) {\n const s = path[j]\n if (isKeyedSegment(s) || typeof s === 'number') {\n hasMoreSegments = true\n break\n }\n }\n\n if (hasMoreSegments) {\n const next = getNodeChildren(context, node, currentParent)\n\n if (!next) {\n return undefined\n }\n\n currentChildren = next.children\n currentFieldName = next.fieldName\n currentParent = next.parent\n } else {\n currentFieldName = undefined\n }\n }\n\n if (!node) {\n return undefined\n }\n\n // Strip trailing field-name segments. The walker may have pushed\n // matching field names during structural descent; if the deepest\n // reached keyed segment was the last keyed segment in the input,\n // any further field names that followed are part of the input but\n // don't identify the returned node.\n while (\n resolvedPath.length > 0 &&\n typeof resolvedPath[resolvedPath.length - 1] === 'string'\n ) {\n resolvedPath.pop()\n }\n\n return {node, path: resolvedPath}\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 {PortableTextBlock} from '@portabletext/schema'\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 * A parent has children, so it is always a `PortableTextBlock` (text block\n * or object node).\n *\n * When `match` is provided and the parent does not satisfy it, returns\n * `undefined`.\n *\n * @beta\n */\nexport function getParent<TMatch extends PortableTextBlock>(\n snapshot: TraversalSnapshot,\n path: Path,\n options: {\n match: (node: PortableTextBlock, path: Path) => node is TMatch\n },\n): {node: TMatch; path: Path} | undefined\n/**\n * @beta\n */\nexport function getParent(\n snapshot: TraversalSnapshot,\n path: Path,\n options?: {\n match?: (node: PortableTextBlock, path: Path) => boolean\n },\n): {node: PortableTextBlock; path: Path} | undefined\nexport function getParent(\n snapshot: TraversalSnapshot,\n path: Path,\n options?: {\n match?: (node: PortableTextBlock, path: Path) => boolean\n },\n): {node: PortableTextBlock; 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 const entry = getNode(snapshot, parent)\n\n if (!entry) {\n return undefined\n }\n\n const result = {node: entry.node as PortableTextBlock, path: entry.path}\n\n if (options?.match && !options.match(result.node, result.path)) {\n return undefined\n }\n\n return result\n}\n"],"names":["isKeyedSegment","segment","serializePath","path","reduce","result","index","_key","isTypedObject","object","isRecord","value","getContainerChildren","containers","node","parent","resolved","resolveNodeContainer","fieldValue","field","name","Array","isArray","children","container","of","entry","type","_type","get","getChildren","snapshot","currentChildren","context","currentFieldName","currentPath","isRoot","currentParent","candidatePath","blockIndexMap","undefined","find","child","at","next","getNodeChildren","fieldName","map","isTextBlock","getNode","length","resolvedPath","i","push","j","s","hasMoreSegments","pop","parentPath","Error","lastNodeIndex","slice","getParent","options","match"],"mappings":";AAKO,SAASA,eAAeC,SAA2C;AACxE,SAAO,OAAOA,WAAY,YAAYA,YAAY,QAAQ,UAAUA;AACtE;ACGO,SAASC,cAAcC,MAAoB;AAChD,SAAOA,KAAKC,OAAe,CAACC,QAAQJ,SAASK,UACvCN,eAAeC,OAAO,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AA;AAGT,QAAMvD,SAASF,KAAK0D,MAAM,GAAGD,aAAa;AAE1C,SAAIvD,OAAO6C,SAAS,KAAK,OAAO7C,OAAOA,OAAO6C,SAAS,CAAC,KAAM,WACrD7C,OAAOwD,MAAM,GAAG,EAAE,IAGpBxD;AACT;ACHO,SAASyD,UACd/B,UACA5B,MACA4D,SAGmD;AACnD,MAAI5D,KAAK+C,WAAW;AAClB;AAGF,QAAMnC,SAAS2C,WAAWvD,IAAI;AAE9B,MAAIY,OAAOmC,WAAW;AACpB;AAGF,QAAMxB,QAAQuB,QAAQlB,UAAUhB,MAAM;AAEtC,MAAI,CAACW;AACH;AAGF,QAAMrB,SAAS;AAAA,IAACS,MAAMY,MAAMZ;AAAAA,IAA2BX,MAAMuB,MAAMvB;AAAAA,EAAAA;AAEnE,MAAI4D,EAAAA,SAASC,SAAS,CAACD,QAAQC,MAAM3D,OAAOS,MAAMT,OAAOF,IAAI;AAI7D,WAAOE;AACT;"}
|
package/lib/traversal/index.d.ts
CHANGED
|
@@ -48,6 +48,21 @@ declare function getAncestors(snapshot: TraversalSnapshot, path: Path): Array<{
|
|
|
48
48
|
node: PortableTextBlock;
|
|
49
49
|
path: Path;
|
|
50
50
|
}>;
|
|
51
|
+
/**
|
|
52
|
+
* Get the annotation at a given path.
|
|
53
|
+
*
|
|
54
|
+
* Annotations live in `markDefs` on a text block, alongside `children`
|
|
55
|
+
* rather than inside it, so they aren't reachable through `getNode`.
|
|
56
|
+
* `getAnnotation` resolves a path of the shape
|
|
57
|
+
* `[..., {_key: block}, 'markDefs', {_key: annotation}, ...]` to the
|
|
58
|
+
* annotation node on the enclosing text block.
|
|
59
|
+
*
|
|
60
|
+
* @beta
|
|
61
|
+
*/
|
|
62
|
+
declare function getAnnotation(snapshot: TraversalSnapshot, path: Path): {
|
|
63
|
+
node: PortableTextObject;
|
|
64
|
+
path: Path;
|
|
65
|
+
} | undefined;
|
|
51
66
|
/**
|
|
52
67
|
* Determine if a node at the given path is a block.
|
|
53
68
|
*
|
|
@@ -172,11 +187,21 @@ declare function getLeaf(snapshot: TraversalSnapshot, path: Path, options: {
|
|
|
172
187
|
*
|
|
173
188
|
* The path can be either keyed (KeyedSegment + field name strings) or
|
|
174
189
|
* indexed (numbers). Keyed segments are resolved by matching `_key`,
|
|
175
|
-
* field name strings
|
|
176
|
-
* are resolved by index.
|
|
177
|
-
*
|
|
178
|
-
* The returned path
|
|
179
|
-
*
|
|
190
|
+
* field name strings name a structural descent into the previous
|
|
191
|
+
* node's children, and numbers are resolved by index.
|
|
192
|
+
*
|
|
193
|
+
* The returned `path` always identifies the returned node: it's fully
|
|
194
|
+
* keyed (numeric indices are converted to `KeyedSegment`s) and any
|
|
195
|
+
* trailing segments in the input that point outside the value tree —
|
|
196
|
+
* e.g. an object node's primitive field, or an annotation reached via
|
|
197
|
+
* `'markDefs'` on a text block — are stripped so that
|
|
198
|
+
* `getNode(snapshot, entry.path).node === entry.node`.
|
|
199
|
+
*
|
|
200
|
+
* The walk stops when a string segment names a field that isn't the
|
|
201
|
+
* current node's structural child array. Annotations live in
|
|
202
|
+
* `markDefs` on a text block, alongside `children` rather than inside
|
|
203
|
+
* it, so `getNode` resolves an annotation path to the enclosing text
|
|
204
|
+
* block. Use `getAnnotation` to resolve the annotation itself.
|
|
180
205
|
*
|
|
181
206
|
* @beta
|
|
182
207
|
*/
|
|
@@ -367,5 +392,5 @@ declare function isLeafObject(snapshot: TraversalSnapshot, node: unknown, path:
|
|
|
367
392
|
* @beta
|
|
368
393
|
*/
|
|
369
394
|
declare function isObject(snapshot: TraversalSnapshot, node: unknown): node is PortableTextObject;
|
|
370
|
-
export { comparePoints, getAncestor, getAncestors, getBlock, getChildren, getContainerChildren, getEnclosingBlock, getFirstChild, getLastChild, getLeaf, getNode, getParent, getPathSubSchema, getSibling, getSpan, getText, getTextBlock, getUnionSchema, hasNode, isBlock, isInline, isLeafObject, isObject, pathContains, rangeIntersects };
|
|
395
|
+
export { comparePoints, getAncestor, getAncestors, getAnnotation, getBlock, getChildren, getContainerChildren, getEnclosingBlock, getFirstChild, getLastChild, getLeaf, getNode, getParent, getPathSubSchema, getSibling, getSpan, getText, getTextBlock, getUnionSchema, hasNode, isBlock, isInline, isLeafObject, isObject, pathContains, rangeIntersects };
|
|
371
396
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/traversal/get-ancestor.ts","../../src/traversal/get-ancestors.ts","../../src/traversal/is-block.ts","../../src/traversal/get-children.ts","../../src/traversal/get-container-children.ts","../../src/traversal/get-enclosing-block.ts","../../src/traversal/get-first-child.ts","../../src/traversal/get-last-child.ts","../../src/traversal/get-leaf.ts","../../src/traversal/get-node.ts","../../src/traversal/get-parent.ts","../../src/traversal/get-path-sub-schema.ts","../../src/traversal/get-sibling.ts","../../src/traversal/compare-points.ts","../../src/traversal/path-contains.ts","../../src/traversal/range-intersects.ts","../../src/traversal/get-span.ts","../../src/traversal/get-text.ts","../../src/traversal/get-text-block.ts","../../src/traversal/get-union-schema.ts","../../src/traversal/has-node.ts","../../src/traversal/is-inline.ts","../../src/traversal/is-leaf-object.ts","../../src/traversal/is-object.ts"],"sourcesContent":[],"mappings":";;AAgBA;;;;;;;;;;AAWA;AACY,iBAZI,WAYJ,CAAA,eAZ+B,iBAY/B,CAAA,CAAA,QAAA,EAXA,iBAWA,EAAA,IAAA,EAVJ,IAUI,EAAA,OAAA,EAAA;EACJ,KAAA,EAAA,CAAA,IAAA,EATU,iBASV,EAAA,IAAA,EATmC,IASnC,EAAA,GAAA,IAAA,IAToD,MASpD;EAEU,IAAA,CAAA,EAAA,QAAA,GAAA,SAAA;CAAyB,CAAA,EAAA;EAGjC,IAAA,EAXA,MAWA;EAAyB,IAAA,EAXX,IAWW;CAAI,GAAA,SAAA;;ACTvC;;AAEQ,iBDAQ,WAAA,CCAR,QAAA,EDCI,iBCDJ,EAAA,IAAA,EDEA,ICFA,EAAA,OAAA,EAAA;EACQ,KAAA,EAAA,CAAA,IAAA,EDGE,iBCHF,EAAA,IAAA,EDG2B,ICH3B,EAAA,GAAA,OAAA;EAAyB,IAAA,CAAA,EAAA,QAAA,GAAA,SAAA;CAAtC,CAAA,EAAA;EAAK,IAAA,EDME,iBCNF;QDM2B;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../src/traversal/get-ancestor.ts","../../src/traversal/get-ancestors.ts","../../src/traversal/get-annotation.ts","../../src/traversal/is-block.ts","../../src/traversal/get-children.ts","../../src/traversal/get-container-children.ts","../../src/traversal/get-enclosing-block.ts","../../src/traversal/get-first-child.ts","../../src/traversal/get-last-child.ts","../../src/traversal/get-leaf.ts","../../src/traversal/get-node.ts","../../src/traversal/get-parent.ts","../../src/traversal/get-path-sub-schema.ts","../../src/traversal/get-sibling.ts","../../src/traversal/compare-points.ts","../../src/traversal/path-contains.ts","../../src/traversal/range-intersects.ts","../../src/traversal/get-span.ts","../../src/traversal/get-text.ts","../../src/traversal/get-text-block.ts","../../src/traversal/get-union-schema.ts","../../src/traversal/has-node.ts","../../src/traversal/is-inline.ts","../../src/traversal/is-leaf-object.ts","../../src/traversal/is-object.ts"],"sourcesContent":[],"mappings":";;AAgBA;;;;;;;;;;AAWA;AACY,iBAZI,WAYJ,CAAA,eAZ+B,iBAY/B,CAAA,CAAA,QAAA,EAXA,iBAWA,EAAA,IAAA,EAVJ,IAUI,EAAA,OAAA,EAAA;EACJ,KAAA,EAAA,CAAA,IAAA,EATU,iBASV,EAAA,IAAA,EATmC,IASnC,EAAA,GAAA,IAAA,IAToD,MASpD;EAEU,IAAA,CAAA,EAAA,QAAA,GAAA,SAAA;CAAyB,CAAA,EAAA;EAGjC,IAAA,EAXA,MAWA;EAAyB,IAAA,EAXX,IAWW;CAAI,GAAA,SAAA;;ACTvC;;AAEQ,iBDAQ,WAAA,CCAR,QAAA,EDCI,iBCDJ,EAAA,IAAA,EDEA,ICFA,EAAA,OAAA,EAAA;EACQ,KAAA,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CyB,MAAA,EmBDF,MnBCE,EAAA,UAAA,EmBDkB,UnBClB,CAAA,EmBD+B,MnBC/B;;ADZzC;;;;AAIkB,iBqBXF,OAAA,CrBWE,QAAA,EqBXgB,iBrBWhB,EAAA,IAAA,EqBXyC,IrBWzC,CAAA,EAAA,OAAA;;AAJlB;;;;;;;AAOU,iBsBXM,QAAA,CtBWN,QAAA,EsBXyB,iBtBWzB,EAAA,IAAA,EsBXkD,ItBWlD,CAAA,EAAA,OAAA;AAPV;;;;;;;;AAOwB,iBuBTR,YAAA,CvBSQ,QAAA,EuBRZ,iBvBQY,EAAA,IAAA,EAAA,OAAA,EAAA,IAAA,EuBNhB,IvBMgB,CAAA,EAAA,IAAA,IuBLb,kBvBKa;;AAPxB;;;;AAIkB,iBwBXF,QAAA,CxBWE,QAAA,EwBVN,iBxBUM,EAAA,IAAA,EAAA,OAAA,CAAA,EAAA,IAAA,IwBRP,kBxBQO"}
|
package/lib/traversal/index.js
CHANGED
|
@@ -1,7 +1,27 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getTextBlock } from "../_chunks-es/get-first-child.js";
|
|
2
|
+
import { getAncestor, getFirstChild, getLeaf, getSpan, getText, getUnionSchema, isLeafObject, pathContains, rangeIntersects } from "../_chunks-es/get-first-child.js";
|
|
2
3
|
import { comparePoints, getAncestors, getBlock, getEnclosingBlock, getPathSubSchema, getSibling, hasNode, isBlock, isInline, isObject } from "../_chunks-es/get-path-sub-schema.js";
|
|
3
|
-
import { getChildren } from "../_chunks-es/get-parent.js";
|
|
4
|
+
import { isKeyedSegment, getChildren } from "../_chunks-es/get-parent.js";
|
|
4
5
|
import { getContainerChildren, getNode, getParent } from "../_chunks-es/get-parent.js";
|
|
6
|
+
function getAnnotation(snapshot, path) {
|
|
7
|
+
for (let i = 0; i < path.length - 1; i++) {
|
|
8
|
+
if (path[i] !== "markDefs")
|
|
9
|
+
continue;
|
|
10
|
+
const annotationSegment = path[i + 1];
|
|
11
|
+
if (!isKeyedSegment(annotationSegment))
|
|
12
|
+
continue;
|
|
13
|
+
const block = getTextBlock(snapshot, path.slice(0, i));
|
|
14
|
+
if (!block)
|
|
15
|
+
continue;
|
|
16
|
+
const annotation = block.node.markDefs?.find((markDef) => markDef._key === annotationSegment._key);
|
|
17
|
+
return annotation ? {
|
|
18
|
+
node: annotation,
|
|
19
|
+
path: [...block.path, "markDefs", {
|
|
20
|
+
_key: annotation._key
|
|
21
|
+
}]
|
|
22
|
+
} : void 0;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
5
25
|
function getLastChild(snapshot, path) {
|
|
6
26
|
return getChildren(snapshot, path).at(-1);
|
|
7
27
|
}
|
|
@@ -9,6 +29,7 @@ export {
|
|
|
9
29
|
comparePoints,
|
|
10
30
|
getAncestor,
|
|
11
31
|
getAncestors,
|
|
32
|
+
getAnnotation,
|
|
12
33
|
getBlock,
|
|
13
34
|
getChildren,
|
|
14
35
|
getContainerChildren,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../src/traversal/get-last-child.ts"],"sourcesContent":["import type {Node} from '../engine/interfaces/node'\nimport type {Path} from '../engine/interfaces/path'\nimport {getChildren} from './get-children'\nimport type {TraversalSnapshot} from './traversal-snapshot'\n\n/**\n * Get the last child of a node at a given path.\n *\n * @beta\n */\nexport function getLastChild(\n snapshot: TraversalSnapshot,\n path: Path,\n): {node: Node; path: Path} | undefined {\n const children = getChildren(snapshot, path)\n\n return children.at(-1)\n}\n"],"names":["
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/traversal/get-annotation.ts","../../src/traversal/get-last-child.ts"],"sourcesContent":["import type {PortableTextObject} from '@portabletext/schema'\nimport type {Path} from '../engine/interfaces/path'\nimport {isKeyedSegment} from '../utils/util.is-keyed-segment'\nimport {getTextBlock} from './get-text-block'\nimport type {TraversalSnapshot} from './traversal-snapshot'\n\n/**\n * Get the annotation at a given path.\n *\n * Annotations live in `markDefs` on a text block, alongside `children`\n * rather than inside it, so they aren't reachable through `getNode`.\n * `getAnnotation` resolves a path of the shape\n * `[..., {_key: block}, 'markDefs', {_key: annotation}, ...]` to the\n * annotation node on the enclosing text block.\n *\n * @beta\n */\nexport function getAnnotation(\n snapshot: TraversalSnapshot,\n path: Path,\n): {node: PortableTextObject; path: Path} | undefined {\n // A path may cross multiple `'markDefs'` segments if a consumer\n // registers a container whose array field is named `markDefs`. Try\n // each position and pick the one whose prefix resolves to a text\n // block.\n for (let i = 0; i < path.length - 1; i++) {\n if (path[i] !== 'markDefs') {\n continue\n }\n\n const annotationSegment = path[i + 1]\n\n if (!isKeyedSegment(annotationSegment)) {\n continue\n }\n\n const block = getTextBlock(snapshot, path.slice(0, i))\n\n if (!block) {\n continue\n }\n\n const annotation = block.node.markDefs?.find(\n (markDef) => markDef._key === annotationSegment._key,\n )\n\n if (!annotation) {\n return undefined\n }\n\n return {\n node: annotation,\n path: [...block.path, 'markDefs', {_key: annotation._key}],\n }\n }\n\n return undefined\n}\n","import type {Node} from '../engine/interfaces/node'\nimport type {Path} from '../engine/interfaces/path'\nimport {getChildren} from './get-children'\nimport type {TraversalSnapshot} from './traversal-snapshot'\n\n/**\n * Get the last child of a node at a given path.\n *\n * @beta\n */\nexport function getLastChild(\n snapshot: TraversalSnapshot,\n path: Path,\n): {node: Node; path: Path} | undefined {\n const children = getChildren(snapshot, path)\n\n return children.at(-1)\n}\n"],"names":["getAnnotation","snapshot","path","i","length","annotationSegment","isKeyedSegment","block","getTextBlock","slice","annotation","node","markDefs","find","markDef","_key","getLastChild","getChildren","at"],"mappings":";;;;;AAiBO,SAASA,cACdC,UACAC,MACoD;AAKpD,WAASC,IAAI,GAAGA,IAAID,KAAKE,SAAS,GAAGD,KAAK;AACxC,QAAID,KAAKC,CAAC,MAAM;AACd;AAGF,UAAME,oBAAoBH,KAAKC,IAAI,CAAC;AAEpC,QAAI,CAACG,eAAeD,iBAAiB;AACnC;AAGF,UAAME,QAAQC,aAAaP,UAAUC,KAAKO,MAAM,GAAGN,CAAC,CAAC;AAErD,QAAI,CAACI;AACH;AAGF,UAAMG,aAAaH,MAAMI,KAAKC,UAAUC,KACrCC,CAAAA,YAAYA,QAAQC,SAASV,kBAAkBU,IAClD;AAEA,WAAKL,aAIE;AAAA,MACLC,MAAMD;AAAAA,MACNR,MAAM,CAAC,GAAGK,MAAML,MAAM,YAAY;AAAA,QAACa,MAAML,WAAWK;AAAAA,MAAAA,CAAK;AAAA,IAAA,IALzD;AAAA,EAOJ;AAGF;AC/CO,SAASC,aACdf,UACAC,MACsC;AAGtC,SAFiBe,YAAYhB,UAAUC,IAAI,EAE3BgB,GAAG,EAAE;AACvB;"}
|