@portabletext/editor 7.10.16 → 7.10.17
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 → behavior.types.action-CYtiRDck.d.ts} +260 -59
- package/lib/behavior.types.action-CYtiRDck.d.ts.map +1 -0
- package/lib/behavior.types.behavior-B_77VtQR.js +159 -0
- package/lib/behavior.types.behavior-B_77VtQR.js.map +1 -0
- package/lib/behaviors/index.d.ts +1 -1
- package/lib/behaviors/index.js +2 -35
- package/lib/get-container-BeXYKpoi.js +222 -0
- package/lib/get-container-BeXYKpoi.js.map +1 -0
- package/lib/get-parent-51Kzgaf9.js +237 -0
- package/lib/get-parent-51Kzgaf9.js.map +1 -0
- package/lib/get-path-sub-schema--2I4NJbG.js +513 -0
- package/lib/get-path-sub-schema--2I4NJbG.js.map +1 -0
- package/lib/index.d.ts +2 -67
- package/lib/index.js +16743 -18963
- package/lib/index.js.map +1 -1
- package/lib/plugins/index.d.ts +1 -2
- package/lib/plugins/index.d.ts.map +1 -1
- package/lib/plugins/index.js +103 -41
- package/lib/plugins/index.js.map +1 -1
- package/lib/selector.is-at-the-start-of-block-gJIMWRpU.js +822 -0
- package/lib/selector.is-at-the-start-of-block-gJIMWRpU.js.map +1 -0
- package/lib/selectors/index.d.ts +1 -3
- package/lib/selectors/index.d.ts.map +1 -1
- package/lib/selectors/index.js +256 -215
- package/lib/selectors/index.js.map +1 -1
- package/lib/traversal/index.d.ts +1 -1
- package/lib/traversal/index.d.ts.map +1 -1
- package/lib/traversal/index.js +33 -52
- package/lib/traversal/index.js.map +1 -1
- package/lib/use-editor-CwNeVwVC.js +47 -0
- package/lib/use-editor-CwNeVwVC.js.map +1 -0
- package/lib/util.is-equal-selections-BF4WJtz5.js +25 -0
- package/lib/util.is-equal-selections-BF4WJtz5.js.map +1 -0
- package/lib/util.slice-blocks-DQnr9X2s.js +633 -0
- package/lib/util.slice-blocks-DQnr9X2s.js.map +1 -0
- package/lib/utils/index.d.ts +11 -39
- package/lib/utils/index.d.ts.map +1 -1
- package/lib/utils/index.js +87 -123
- package/lib/utils/index.js.map +1 -1
- package/package.json +18 -15
- package/lib/_chunks-dts/behavior.types.action.d.ts.map +0 -1
- package/lib/_chunks-dts/block-offset.d.ts +0 -10
- package/lib/_chunks-dts/block-offset.d.ts.map +0 -1
- package/lib/_chunks-dts/editor-selector.d.ts +0 -33
- package/lib/_chunks-dts/editor-selector.d.ts.map +0 -1
- package/lib/_chunks-dts/editor.d.ts +0 -99
- package/lib/_chunks-dts/editor.d.ts.map +0 -1
- package/lib/_chunks-es/get-container.js +0 -187
- package/lib/_chunks-es/get-container.js.map +0 -1
- package/lib/_chunks-es/get-parent.js +0 -195
- package/lib/_chunks-es/get-parent.js.map +0 -1
- package/lib/_chunks-es/get-path-sub-schema.js +0 -399
- package/lib/_chunks-es/get-path-sub-schema.js.map +0 -1
- package/lib/_chunks-es/selector.is-at-the-start-of-block.js +0 -947
- package/lib/_chunks-es/selector.is-at-the-start-of-block.js.map +0 -1
- package/lib/_chunks-es/use-editor.js +0 -20
- package/lib/_chunks-es/use-editor.js.map +0 -1
- package/lib/_chunks-es/util.is-equal-selections.js +0 -20
- package/lib/_chunks-es/util.is-equal-selections.js.map +0 -1
- package/lib/_chunks-es/util.slice-blocks.js +0 -720
- package/lib/_chunks-es/util.slice-blocks.js.map +0 -1
- package/lib/behaviors/index.js.map +0 -1
- package/lib/index.d.ts.map +0 -1
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import { isTextBlock } from "@portabletext/schema";
|
|
2
|
+
/**
|
|
3
|
+
* @public
|
|
4
|
+
*/
|
|
5
|
+
function isKeyedSegment(segment) {
|
|
6
|
+
return typeof segment == "object" && !!segment && "_key" in segment;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Serialize a keyed path to a string using Sanity's bracket notation.
|
|
10
|
+
*
|
|
11
|
+
* - `[{_key: 'k0'}]` -> `[_key=="k0"]`
|
|
12
|
+
* - `[{_key: 'k0'}, 'children', {_key: 's0'}]` -> `[_key=="k0"].children[_key=="s0"]`
|
|
13
|
+
* - `[{_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"]`
|
|
14
|
+
*/
|
|
15
|
+
function serializePath(path) {
|
|
16
|
+
return path.reduce((result, segment, index) => isKeyedSegment(segment) ? `${result}[_key=="${segment._key}"]` : `${result}${index === 0 ? "" : "."}${segment}`, "");
|
|
17
|
+
}
|
|
18
|
+
function isTypedObject(object) {
|
|
19
|
+
return isRecord(object) && typeof object._type == "string";
|
|
20
|
+
}
|
|
21
|
+
function isRecord(value) {
|
|
22
|
+
return !!value && (typeof value == "object" || typeof value == "function");
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Resolve a container node's editable child array.
|
|
26
|
+
*
|
|
27
|
+
* Returns `{children, container}` for a node registered as a container:
|
|
28
|
+
* `children` is the node's editable child array, and `container` is the
|
|
29
|
+
* node's own container registration. Read `container.field.name` for the
|
|
30
|
+
* path segment that reaches `children`, and thread `container` back in as
|
|
31
|
+
* `parent` when descending into them.
|
|
32
|
+
*
|
|
33
|
+
* Returns `undefined` for anything that is not a container: text blocks,
|
|
34
|
+
* spans, leaves, and unregistered objects. It is node-based and resolves
|
|
35
|
+
* in one step with no path re-walk, so recursive descent over containers
|
|
36
|
+
* is linear in nesting depth. The caller seeds the document root from
|
|
37
|
+
* `context.value` itself.
|
|
38
|
+
*
|
|
39
|
+
* @beta
|
|
40
|
+
*/
|
|
41
|
+
function getContainerChildren(containers, node, parent) {
|
|
42
|
+
let resolved = resolveNodeContainer(containers, parent, node);
|
|
43
|
+
if (!resolved) return;
|
|
44
|
+
let fieldValue = node[resolved.field.name];
|
|
45
|
+
if (Array.isArray(fieldValue)) return {
|
|
46
|
+
children: fieldValue,
|
|
47
|
+
container: resolved
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Pick the positional override from `parent.of` if present; fall back
|
|
52
|
+
* to the top-level entry. Returns only `RegisteredContainer` entries
|
|
53
|
+
* since leaves do not have editable children.
|
|
54
|
+
*/
|
|
55
|
+
function resolveNodeContainer(containers, parent, node) {
|
|
56
|
+
if (parent?.of) {
|
|
57
|
+
for (let entry of parent.of) if (entry.type === node._type) return "field" in entry ? entry : void 0;
|
|
58
|
+
}
|
|
59
|
+
return containers.get(node._type);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Get the children of a node at a given path.
|
|
63
|
+
*
|
|
64
|
+
* @beta
|
|
65
|
+
*/
|
|
66
|
+
function getChildren(snapshot, path) {
|
|
67
|
+
let currentChildren = snapshot.context.value, currentFieldName = "value", currentPath = [], isRoot = !0, currentParent;
|
|
68
|
+
for (let segment of path) {
|
|
69
|
+
if (typeof segment == "string") continue;
|
|
70
|
+
let node;
|
|
71
|
+
if (isKeyedSegment(segment)) {
|
|
72
|
+
let candidatePath = isRoot ? [{ _key: segment._key }] : [
|
|
73
|
+
...currentPath,
|
|
74
|
+
currentFieldName,
|
|
75
|
+
{ _key: segment._key }
|
|
76
|
+
], index = snapshot.blockIndexMap.get(serializePath(candidatePath));
|
|
77
|
+
node = index !== void 0 && currentChildren[index]?._key === segment._key ? currentChildren[index] : currentChildren.find((child) => child._key === segment._key);
|
|
78
|
+
} else typeof segment == "number" && (node = currentChildren.at(segment));
|
|
79
|
+
if (!node) return [];
|
|
80
|
+
currentPath = isRoot ? [{ _key: node._key }] : [
|
|
81
|
+
...currentPath,
|
|
82
|
+
currentFieldName,
|
|
83
|
+
{ _key: node._key }
|
|
84
|
+
], isRoot = !1;
|
|
85
|
+
let next = getNodeChildren(snapshot.context, node, currentParent);
|
|
86
|
+
if (!next) return [];
|
|
87
|
+
currentChildren = next.children, currentFieldName = next.fieldName, currentParent = next.parent;
|
|
88
|
+
}
|
|
89
|
+
return currentChildren.map((child) => ({
|
|
90
|
+
node: child,
|
|
91
|
+
path: isRoot ? [{ _key: child._key }] : [
|
|
92
|
+
...currentPath,
|
|
93
|
+
currentFieldName,
|
|
94
|
+
{ _key: child._key }
|
|
95
|
+
]
|
|
96
|
+
}));
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Resolve a node's editable child array.
|
|
100
|
+
*
|
|
101
|
+
* When `parent` is provided and its `of` declares a positional entry
|
|
102
|
+
* matching `node._type`, that positional entry's `field` is used.
|
|
103
|
+
* Otherwise the top-level `containers.get(node._type)` provides the
|
|
104
|
+
* fallback.
|
|
105
|
+
*
|
|
106
|
+
* The returned `parent` is the resolved container entry for `node`
|
|
107
|
+
* itself (used by the caller to thread further descent).
|
|
108
|
+
*
|
|
109
|
+
* Internal descent kernel shared by the positional traversal utilities
|
|
110
|
+
* (`getChildren`, `getNode`, `getNodes`, `getAncestors`); it folds the
|
|
111
|
+
* text-block, container, and root-document cases. Public consumers that
|
|
112
|
+
* only need to descend containers use {@link getContainerChildren}.
|
|
113
|
+
*/
|
|
114
|
+
function getNodeChildren(context, node, parent) {
|
|
115
|
+
if (isTextBlock(context, node)) return {
|
|
116
|
+
children: node.children,
|
|
117
|
+
fieldName: "children",
|
|
118
|
+
parent: void 0
|
|
119
|
+
};
|
|
120
|
+
if (isTypedObject(node)) {
|
|
121
|
+
let result = getContainerChildren(context.containers, node, parent);
|
|
122
|
+
if (result) return {
|
|
123
|
+
children: result.children,
|
|
124
|
+
fieldName: result.container.field.name,
|
|
125
|
+
parent: result.container
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
if ("value" in node && Array.isArray(node.value) && !("_key" in node) && !("_type" in node)) return {
|
|
129
|
+
children: node.value,
|
|
130
|
+
fieldName: "value",
|
|
131
|
+
parent: void 0
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Get the node at a given path.
|
|
136
|
+
*
|
|
137
|
+
* The path can be either keyed (KeyedSegment + field name strings) or
|
|
138
|
+
* indexed (numbers). Keyed segments are resolved by matching `_key`,
|
|
139
|
+
* field name strings name a structural descent into the previous
|
|
140
|
+
* node's children, and numbers are resolved by index.
|
|
141
|
+
*
|
|
142
|
+
* The returned `path` always identifies the returned node: it's fully
|
|
143
|
+
* keyed (numeric indices are converted to `KeyedSegment`s) and any
|
|
144
|
+
* trailing segments in the input that point outside the value tree —
|
|
145
|
+
* e.g. an object node's primitive field, or an annotation reached via
|
|
146
|
+
* `'markDefs'` on a text block — are stripped so that
|
|
147
|
+
* `getNode(snapshot, entry.path).node === entry.node`.
|
|
148
|
+
*
|
|
149
|
+
* The walk stops when a string segment names a field that isn't the
|
|
150
|
+
* current node's structural child array. Annotations live in
|
|
151
|
+
* `markDefs` on a text block, alongside `children` rather than inside
|
|
152
|
+
* it, so `getNode` resolves an annotation path to the enclosing text
|
|
153
|
+
* block. Use `getAnnotation` to resolve the annotation itself.
|
|
154
|
+
*
|
|
155
|
+
* @beta
|
|
156
|
+
*/
|
|
157
|
+
function getNode(snapshot, path) {
|
|
158
|
+
if (path.length === 0) return;
|
|
159
|
+
let { context, blockIndexMap } = snapshot, currentChildren = context.value, currentFieldName, node, currentParent, resolvedPath = [];
|
|
160
|
+
for (let i = 0; i < path.length; i++) {
|
|
161
|
+
let segment = path[i];
|
|
162
|
+
if (typeof segment == "string") {
|
|
163
|
+
if (currentFieldName !== void 0 && segment === currentFieldName) {
|
|
164
|
+
resolvedPath.push(segment);
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
for (let j = i + 1; j < path.length; j++) {
|
|
168
|
+
let s = path[j];
|
|
169
|
+
if (isKeyedSegment(s) || typeof s == "number") return;
|
|
170
|
+
}
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
if (isKeyedSegment(segment)) {
|
|
174
|
+
resolvedPath.push(segment);
|
|
175
|
+
let index = blockIndexMap.get(serializePath(resolvedPath));
|
|
176
|
+
index !== void 0 && currentChildren[index]?._key === segment._key ? node = currentChildren[index] : (node = currentChildren.find((child) => child._key === segment._key), node && node._key !== void 0 && (resolvedPath[resolvedPath.length - 1] = { _key: node._key }));
|
|
177
|
+
} else if (typeof segment == "number") node = currentChildren.at(segment), node && resolvedPath.push({ _key: node._key });
|
|
178
|
+
else return;
|
|
179
|
+
if (!node) return;
|
|
180
|
+
let hasMoreSegments = !1;
|
|
181
|
+
for (let j = i + 1; j < path.length; j++) {
|
|
182
|
+
let s = path[j];
|
|
183
|
+
if (isKeyedSegment(s) || typeof s == "number") {
|
|
184
|
+
hasMoreSegments = !0;
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (hasMoreSegments) {
|
|
189
|
+
let next = getNodeChildren(context, node, currentParent);
|
|
190
|
+
if (!next) return;
|
|
191
|
+
currentChildren = next.children, currentFieldName = next.fieldName, currentParent = next.parent;
|
|
192
|
+
} else currentFieldName = void 0;
|
|
193
|
+
}
|
|
194
|
+
if (node) {
|
|
195
|
+
for (; resolvedPath.length > 0 && typeof resolvedPath[resolvedPath.length - 1] == "string";) resolvedPath.pop();
|
|
196
|
+
return {
|
|
197
|
+
node,
|
|
198
|
+
path: resolvedPath
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Get the parent path of a path.
|
|
204
|
+
*
|
|
205
|
+
* Drops the last node segment (keyed or numeric) and the preceding field
|
|
206
|
+
* name string.
|
|
207
|
+
*
|
|
208
|
+
* [{_key:'b1'}, 'children', {_key:'s1'}] → [{_key:'b1'}]
|
|
209
|
+
* [{_key:'b1'}, 'children', 0] → [{_key:'b1'}]
|
|
210
|
+
* [{_key:'b1'}] → []
|
|
211
|
+
*/
|
|
212
|
+
function parentPath(path) {
|
|
213
|
+
if (path.length === 0) throw Error(`Cannot get the parent path of the root path [${path}].`);
|
|
214
|
+
let lastNodeIndex = -1;
|
|
215
|
+
for (let i = path.length - 1; i >= 0; i--) if (isKeyedSegment(path[i]) || typeof path[i] == "number") {
|
|
216
|
+
lastNodeIndex = i;
|
|
217
|
+
break;
|
|
218
|
+
}
|
|
219
|
+
if (lastNodeIndex === -1) return [];
|
|
220
|
+
let result = path.slice(0, lastNodeIndex);
|
|
221
|
+
return result.length > 0 && typeof result[result.length - 1] == "string" ? result.slice(0, -1) : result;
|
|
222
|
+
}
|
|
223
|
+
function getParent(snapshot, path, options) {
|
|
224
|
+
if (path.length === 0) return;
|
|
225
|
+
let parent = parentPath(path);
|
|
226
|
+
if (parent.length === 0) return;
|
|
227
|
+
let entry = getNode(snapshot, parent);
|
|
228
|
+
if (!entry) return;
|
|
229
|
+
let result = {
|
|
230
|
+
node: entry.node,
|
|
231
|
+
path: entry.path
|
|
232
|
+
};
|
|
233
|
+
if (!(options?.match && !options.match(result.node, result.path))) return result;
|
|
234
|
+
}
|
|
235
|
+
export { getNodeChildren as a, isTypedObject as c, getChildren as i, serializePath as l, parentPath as n, getContainerChildren as o, getNode as r, isRecord as s, getParent as t, isKeyedSegment as u };
|
|
236
|
+
|
|
237
|
+
//# sourceMappingURL=get-parent-51Kzgaf9.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"get-parent-51Kzgaf9.js","names":["KeyedSegment","isKeyedSegment","segment","Path","isKeyedSegment","serializePath","path","reduce","result","segment","index","_key","separator","TypedObject","isTypedObject","object","isRecord","value","Record","Node","Containers","RegisteredContainer","getContainerChildren","containers","node","parent","children","Array","container","resolved","resolveNodeContainer","undefined","fieldValue","Record","field","name","isArray","of","entry","type","_type","get","isTextBlock","EditorSchema","Node","Path","serializePath","Containers","RegisteredContainer","isTypedObject","isKeyedSegment","getContainerChildren","TraversalSnapshot","getChildren","snapshot","path","Array","node","currentChildren","context","value","currentFieldName","currentPath","isRoot","currentParent","segment","candidatePath","_key","index","blockIndexMap","get","undefined","find","child","at","next","getNodeChildren","children","fieldName","parent","map","schema","containers","result","container","field","name","isArray","Node","Path","serializePath","RegisteredContainer","isKeyedSegment","getNodeChildren","TraversalSnapshot","getNode","snapshot","path","node","length","undefined","context","blockIndexMap","currentChildren","Array","value","currentFieldName","currentParent","resolvedPath","i","segment","push","j","s","index","get","_key","find","child","at","hasMoreSegments","next","children","fieldName","parent","pop","isKeyedSegment","Path","parentPath","path","length","Error","lastNodeIndex","i","result","slice","PortableTextBlock","Path","parentPath","getNode","TraversalSnapshot","getParent","snapshot","path","options","match","node","TMatch","length","undefined","parent","entry","result"],"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"],"mappings":";;;;AAKA,SAAgBC,eAAeC,SAA2C;CACxE,OAAO,OAAOA,WAAY,cAAYA,WAAoB,UAAUA;AACtE;;;;;;;;ACGA,SAAgBG,cAAcC,MAAoB;CAChD,OAAOA,KAAKC,QAAgBC,QAAQC,SAASC,UACvCN,eAAeK,OAAO,IACjB,GAAGD,OAAM,UAAWC,QAAQE,KAAI,MAIlC,GAAGH,SADQE,UAAU,IAAI,KAAK,MACND,WAC9B,EAAE;AACP;ACjBA,SAAgBK,cAAcC,QAAwC;CACpE,OAAOC,SAASD,MAAM,KAAK,OAAOA,OAAO,SAAa;AACxD;AAEA,SAAgBC,SAASC,OAAkD;CACzE,OAAO,CAAC,CAACA,UAAU,OAAOA,SAAU,YAAY,OAAOA,SAAU;AACnE;;;;;;;;;;;;;;;;;;ACeA,SAAgBK,qBACdC,YACAC,MACAC,QAMY;CACZ,IAAMI,WAAWC,qBAAqBP,YAAYE,QAAQD,IAAI;CAE9D,IAAI,CAACK,UACH;CAGF,IA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