@portabletext/editor 7.10.15 → 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.
Files changed (63) hide show
  1. package/lib/{_chunks-dts/behavior.types.action.d.ts → behavior.types.action-CYtiRDck.d.ts} +260 -59
  2. package/lib/behavior.types.action-CYtiRDck.d.ts.map +1 -0
  3. package/lib/behavior.types.behavior-B_77VtQR.js +159 -0
  4. package/lib/behavior.types.behavior-B_77VtQR.js.map +1 -0
  5. package/lib/behaviors/index.d.ts +1 -1
  6. package/lib/behaviors/index.js +2 -35
  7. package/lib/get-container-BeXYKpoi.js +222 -0
  8. package/lib/get-container-BeXYKpoi.js.map +1 -0
  9. package/lib/get-parent-51Kzgaf9.js +237 -0
  10. package/lib/get-parent-51Kzgaf9.js.map +1 -0
  11. package/lib/get-path-sub-schema--2I4NJbG.js +513 -0
  12. package/lib/get-path-sub-schema--2I4NJbG.js.map +1 -0
  13. package/lib/index.d.ts +2 -67
  14. package/lib/index.js +16743 -18963
  15. package/lib/index.js.map +1 -1
  16. package/lib/plugins/index.d.ts +1 -2
  17. package/lib/plugins/index.d.ts.map +1 -1
  18. package/lib/plugins/index.js +103 -41
  19. package/lib/plugins/index.js.map +1 -1
  20. package/lib/selector.is-at-the-start-of-block-gJIMWRpU.js +822 -0
  21. package/lib/selector.is-at-the-start-of-block-gJIMWRpU.js.map +1 -0
  22. package/lib/selectors/index.d.ts +1 -3
  23. package/lib/selectors/index.d.ts.map +1 -1
  24. package/lib/selectors/index.js +256 -215
  25. package/lib/selectors/index.js.map +1 -1
  26. package/lib/traversal/index.d.ts +1 -1
  27. package/lib/traversal/index.d.ts.map +1 -1
  28. package/lib/traversal/index.js +33 -52
  29. package/lib/traversal/index.js.map +1 -1
  30. package/lib/use-editor-CwNeVwVC.js +47 -0
  31. package/lib/use-editor-CwNeVwVC.js.map +1 -0
  32. package/lib/util.is-equal-selections-BF4WJtz5.js +25 -0
  33. package/lib/util.is-equal-selections-BF4WJtz5.js.map +1 -0
  34. package/lib/util.slice-blocks-DQnr9X2s.js +633 -0
  35. package/lib/util.slice-blocks-DQnr9X2s.js.map +1 -0
  36. package/lib/utils/index.d.ts +11 -39
  37. package/lib/utils/index.d.ts.map +1 -1
  38. package/lib/utils/index.js +87 -123
  39. package/lib/utils/index.js.map +1 -1
  40. package/package.json +18 -15
  41. package/lib/_chunks-dts/behavior.types.action.d.ts.map +0 -1
  42. package/lib/_chunks-dts/block-offset.d.ts +0 -10
  43. package/lib/_chunks-dts/block-offset.d.ts.map +0 -1
  44. package/lib/_chunks-dts/editor-selector.d.ts +0 -33
  45. package/lib/_chunks-dts/editor-selector.d.ts.map +0 -1
  46. package/lib/_chunks-dts/editor.d.ts +0 -99
  47. package/lib/_chunks-dts/editor.d.ts.map +0 -1
  48. package/lib/_chunks-es/get-container.js +0 -187
  49. package/lib/_chunks-es/get-container.js.map +0 -1
  50. package/lib/_chunks-es/get-parent.js +0 -195
  51. package/lib/_chunks-es/get-parent.js.map +0 -1
  52. package/lib/_chunks-es/get-path-sub-schema.js +0 -399
  53. package/lib/_chunks-es/get-path-sub-schema.js.map +0 -1
  54. package/lib/_chunks-es/selector.is-at-the-start-of-block.js +0 -947
  55. package/lib/_chunks-es/selector.is-at-the-start-of-block.js.map +0 -1
  56. package/lib/_chunks-es/use-editor.js +0 -20
  57. package/lib/_chunks-es/use-editor.js.map +0 -1
  58. package/lib/_chunks-es/util.is-equal-selections.js +0 -20
  59. package/lib/_chunks-es/util.is-equal-selections.js.map +0 -1
  60. package/lib/_chunks-es/util.slice-blocks.js +0 -720
  61. package/lib/_chunks-es/util.slice-blocks.js.map +0 -1
  62. package/lib/behaviors/index.js.map +0 -1
  63. package/lib/index.d.ts.map +0 -1
@@ -1,195 +0,0 @@
1
- import { isTextBlock } from "@portabletext/schema";
2
- function isKeyedSegment(segment) {
3
- return typeof segment == "object" && segment !== null && "_key" in segment;
4
- }
5
- function serializePath(path) {
6
- return path.reduce((result, segment, index) => isKeyedSegment(segment) ? `${result}[_key=="${segment._key}"]` : `${result}${index === 0 ? "" : "."}${segment}`, "");
7
- }
8
- function isTypedObject(object) {
9
- return isRecord(object) && typeof object._type == "string";
10
- }
11
- function isRecord(value) {
12
- return !!value && (typeof value == "object" || typeof value == "function");
13
- }
14
- function getContainerChildren(containers, node, parent) {
15
- const resolved = resolveNodeContainer(containers, parent, node);
16
- if (!resolved)
17
- return;
18
- const fieldValue = node[resolved.field.name];
19
- if (Array.isArray(fieldValue))
20
- return {
21
- children: fieldValue,
22
- container: resolved
23
- };
24
- }
25
- function resolveNodeContainer(containers, parent, node) {
26
- if (parent?.of) {
27
- for (const entry of parent.of)
28
- if (entry.type === node._type)
29
- return "field" in entry ? entry : void 0;
30
- }
31
- return containers.get(node._type);
32
- }
33
- function getChildren(snapshot, path) {
34
- let currentChildren = snapshot.context.value, currentFieldName = "value", currentPath = [], isRoot = !0, currentParent;
35
- for (const segment of path) {
36
- if (typeof segment == "string")
37
- continue;
38
- let node;
39
- if (isKeyedSegment(segment)) {
40
- const candidatePath = isRoot ? [{
41
- _key: segment._key
42
- }] : [...currentPath, currentFieldName, {
43
- _key: segment._key
44
- }], index = snapshot.blockIndexMap.get(serializePath(candidatePath));
45
- node = index !== void 0 && currentChildren[index]?._key === segment._key ? currentChildren[index] : currentChildren.find((child) => child._key === segment._key);
46
- } else typeof segment == "number" && (node = currentChildren.at(segment));
47
- if (!node)
48
- return [];
49
- currentPath = isRoot ? [{
50
- _key: node._key
51
- }] : [...currentPath, currentFieldName, {
52
- _key: node._key
53
- }], isRoot = !1;
54
- const next = getNodeChildren(snapshot.context, node, currentParent);
55
- if (!next)
56
- return [];
57
- currentChildren = next.children, currentFieldName = next.fieldName, currentParent = next.parent;
58
- }
59
- return currentChildren.map((child) => ({
60
- node: child,
61
- path: isRoot ? [{
62
- _key: child._key
63
- }] : [...currentPath, currentFieldName, {
64
- _key: child._key
65
- }]
66
- }));
67
- }
68
- function getNodeChildren(context, node, parent) {
69
- if (isTextBlock(context, node))
70
- return {
71
- children: node.children,
72
- fieldName: "children",
73
- parent: void 0
74
- };
75
- if (isTypedObject(node)) {
76
- const result = getContainerChildren(context.containers, node, parent);
77
- if (result)
78
- return {
79
- children: result.children,
80
- fieldName: result.container.field.name,
81
- parent: result.container
82
- };
83
- }
84
- if ("value" in node && Array.isArray(node.value) && !("_key" in node) && !("_type" in node))
85
- return {
86
- children: node.value,
87
- fieldName: "value",
88
- parent: void 0
89
- };
90
- }
91
- function getNode(snapshot, path) {
92
- if (path.length === 0)
93
- return;
94
- const {
95
- context,
96
- blockIndexMap
97
- } = snapshot;
98
- let currentChildren = context.value, currentFieldName, node, currentParent;
99
- const resolvedPath = [];
100
- for (let i = 0; i < path.length; i++) {
101
- const segment = path[i];
102
- if (typeof segment == "string") {
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;
113
- }
114
- if (isKeyedSegment(segment)) {
115
- resolvedPath.push(segment);
116
- const index = blockIndexMap.get(serializePath(resolvedPath));
117
- 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] = {
118
- _key: node._key
119
- }));
120
- } else if (typeof segment == "number")
121
- node = currentChildren.at(segment), node && resolvedPath.push({
122
- _key: node._key
123
- });
124
- else
125
- return;
126
- if (!node)
127
- return;
128
- let hasMoreSegments = !1;
129
- for (let j = i + 1; j < path.length; j++) {
130
- const s = path[j];
131
- if (isKeyedSegment(s) || typeof s == "number") {
132
- hasMoreSegments = !0;
133
- break;
134
- }
135
- }
136
- if (hasMoreSegments) {
137
- const next = getNodeChildren(context, node, currentParent);
138
- if (!next)
139
- return;
140
- currentChildren = next.children, currentFieldName = next.fieldName, currentParent = next.parent;
141
- } else
142
- currentFieldName = void 0;
143
- }
144
- if (node) {
145
- for (; resolvedPath.length > 0 && typeof resolvedPath[resolvedPath.length - 1] == "string"; )
146
- resolvedPath.pop();
147
- return {
148
- node,
149
- path: resolvedPath
150
- };
151
- }
152
- }
153
- function parentPath(path) {
154
- if (path.length === 0)
155
- throw new Error(`Cannot get the parent path of the root path [${path}].`);
156
- let lastNodeIndex = -1;
157
- for (let i = path.length - 1; i >= 0; i--)
158
- if (isKeyedSegment(path[i]) || typeof path[i] == "number") {
159
- lastNodeIndex = i;
160
- break;
161
- }
162
- if (lastNodeIndex === -1)
163
- return [];
164
- const result = path.slice(0, lastNodeIndex);
165
- return result.length > 0 && typeof result[result.length - 1] == "string" ? result.slice(0, -1) : result;
166
- }
167
- function getParent(snapshot, path, options) {
168
- if (path.length === 0)
169
- return;
170
- const parent = parentPath(path);
171
- if (parent.length === 0)
172
- return;
173
- const entry = getNode(snapshot, parent);
174
- if (!entry)
175
- return;
176
- const result = {
177
- node: entry.node,
178
- path: entry.path
179
- };
180
- if (!(options?.match && !options.match(result.node, result.path)))
181
- return result;
182
- }
183
- export {
184
- getChildren,
185
- getContainerChildren,
186
- getNode,
187
- getNodeChildren,
188
- getParent,
189
- isKeyedSegment,
190
- isRecord,
191
- isTypedObject,
192
- parentPath,
193
- serializePath
194
- };
195
- //# sourceMappingURL=get-parent.js.map
@@ -1 +0,0 @@
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,IACjB,GAAGI,MAAM,WAAWJ,QAAQM,IAAI,OAIlC,GAAGF,MAAM,GADEC,UAAU,IAAI,KAAK,GACT,GAAGL,OAAO,IACrC,EAAE;AACP;ACjBO,SAASO,cAAcC,QAAwC;AACpE,SAAOC,SAASD,MAAM,KAAK,OAAOA,OAAO,SAAa;AACxD;AAEO,SAASC,SAASC,OAAkD;AACzE,SAAO,CAAC,CAACA,UAAU,OAAOA,SAAU,YAAY,OAAOA,SAAU;AACnE;ACeO,SAASC,qBACdC,YACAC,MACAC,QAMY;AACZ,QAAMC,WAAWC,qBAAqBJ,YAAYE,QAAQD,IAAI;AAE9D,MAAI,CAACE;AACH;AAGF,QAAME,aAAcJ,KAAiCE,SAASG,MAAMC,IAAI;AAExE,MAAKC,MAAMC,QAAQJ,UAAU;AAI7B,WAAO;AAAA,MACLK,UAAUL;AAAAA,MACVM,WAAWR;AAAAA,IAAAA;AAEf;AAOA,SAASC,qBACPJ,YACAE,QACAD,MACiC;AACjC,MAAIC,QAAQU;AACV,eAAWC,SAASX,OAAOU;AACzB,UAAIC,MAAMC,SAASb,KAAKc;AAEtB,eAAI,WAAWF,QACNA,QAET;AAAA;AAIN,SAAOb,WAAWgB,IAAIf,KAAKc,KA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aD,SAAS,CAAC,KAAM;AAEjDC,mBAAaM,IAAAA;AAGf,WAAO;AAAA,MAAC3C;AAAAA,MAAMX,MAAMgD;AAAAA,IAAAA;AAAAA,EAAY;AAClC;ACrIO,SAASO,WAAWvD,MAAkB;AAC3C,MAAIA,KAAK+C,WAAW;AAClB,UAAM,IAAIS,MAAM,gDAAgDxD,IAAI,IAAI;AAG1E,MAAIyD,gBAAgB;AACpB,WAASR,IAAIjD,KAAK+C,SAAS,GAAGE,KAAK,GAAGA;AACpC,QAAIpD,eAAeG,KAAKiD,CAAC,CAAC,KAAK,OAAOjD,KAAKiD,CAAC,KAAM,UAAU;AAC1DQ,sBAAgBR;AAChB;AAAA,IACF;AAGF,MAAIQ,kBAAkB;AACpB,WAAO,CAAA;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;"}
@@ -1,399 +0,0 @@
1
- import { isKeyedSegment, serializePath, getNodeChildren, getNode, isTypedObject, getChildren, parentPath, getParent } from "./get-parent.js";
2
- import { getSubSchema } from "@portabletext/schema";
3
- function getAncestors(snapshot, path) {
4
- const keyedIndices = [];
5
- for (let i = 0; i < path.length; i++)
6
- isKeyedSegment(path[i]) && keyedIndices.push(i);
7
- if (keyedIndices.length <= 1)
8
- return [];
9
- const {
10
- context,
11
- blockIndexMap
12
- } = snapshot;
13
- let currentChildren = context.value, currentParent;
14
- const ancestorsByDepth = [], resolvedPath = [], targetKeyedIndex = keyedIndices[keyedIndices.length - 1];
15
- let segmentIndex = 0;
16
- for (; segmentIndex < targetKeyedIndex; ) {
17
- const segment = path[segmentIndex];
18
- if (typeof segment == "string") {
19
- resolvedPath.push(segment), segmentIndex++;
20
- continue;
21
- }
22
- let node;
23
- if (isKeyedSegment(segment)) {
24
- resolvedPath.push(segment);
25
- const index = blockIndexMap.get(serializePath(resolvedPath));
26
- 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] = {
27
- _key: node._key
28
- }));
29
- } else if (typeof segment == "number")
30
- node = currentChildren.at(segment), node && resolvedPath.push({
31
- _key: node._key
32
- });
33
- else
34
- return [];
35
- if (!node)
36
- return [];
37
- const next = getNodeChildren(context, node, currentParent);
38
- if (!next)
39
- return [];
40
- ancestorsByDepth.push({
41
- node,
42
- path: resolvedPath.slice()
43
- }), currentChildren = next.children, currentParent = next.parent, segmentIndex++;
44
- }
45
- return ancestorsByDepth.reverse();
46
- }
47
- function hasNode(snapshot, path) {
48
- return getNode(snapshot, path) !== void 0;
49
- }
50
- function resolveContainerAt(containers, value, path) {
51
- const keyedIndices = [];
52
- for (let index = 0; index < path.length; index++)
53
- isKeyedSegment(path[index]) && keyedIndices.push(index);
54
- if (keyedIndices.length === 0)
55
- return;
56
- let currentChildren = value, parent, resolved;
57
- const targetKeyedIndex = keyedIndices[keyedIndices.length - 1];
58
- let segmentIndex = 0;
59
- for (; segmentIndex <= targetKeyedIndex; ) {
60
- const segment = path[segmentIndex];
61
- if (typeof segment == "string") {
62
- segmentIndex++;
63
- continue;
64
- }
65
- let node;
66
- if (isKeyedSegment(segment))
67
- node = currentChildren.find((child) => child._key === segment._key);
68
- else if (typeof segment == "number")
69
- node = currentChildren.at(segment);
70
- else
71
- return;
72
- if (!node || (resolved = resolveNodeEntry(containers, parent, node), !resolved))
73
- return;
74
- if (segmentIndex < targetKeyedIndex) {
75
- if (!("field" in resolved))
76
- return;
77
- const fieldValue = node[resolved.field.name];
78
- if (!Array.isArray(fieldValue))
79
- return;
80
- parent = resolved, currentChildren = fieldValue;
81
- }
82
- segmentIndex++;
83
- }
84
- return resolved;
85
- }
86
- function resolveNodeEntry(containers, parent, node) {
87
- if (parent?.of) {
88
- for (const entry of parent.of)
89
- if (entry.type === node._type)
90
- return entry;
91
- }
92
- return containers.get(node._type);
93
- }
94
- function isEditableContainer(snapshot, _node, path) {
95
- if (snapshot.context.containers.size === 0)
96
- return !1;
97
- const resolved = resolveContainerAt(snapshot.context.containers, snapshot.context.value, path);
98
- return !!(resolved && "field" in resolved);
99
- }
100
- function isObject(snapshot, node) {
101
- return isTypedObject(node) && node._type !== snapshot.context.schema.block.name && node._type !== snapshot.context.schema.span.name;
102
- }
103
- function comparePaths(path, another, root) {
104
- const min = Math.min(path.length, another.length);
105
- let currentChildren = root?.value, currentNode;
106
- for (let i = 0; i < min; i++) {
107
- const segment = path[i], otherSegment = another[i];
108
- if (isKeyedSegment(segment) && isKeyedSegment(otherSegment)) {
109
- if (segment._key === otherSegment._key) {
110
- currentChildren && (currentNode = currentChildren.find((c) => c._key === segment._key), currentChildren = void 0);
111
- continue;
112
- }
113
- if (currentChildren) {
114
- const segmentIndex = currentChildren.findIndex((c) => c._key === segment._key), otherSegmentIndex = currentChildren.findIndex((c) => c._key === otherSegment._key);
115
- if (segmentIndex !== -1 && otherSegmentIndex !== -1)
116
- return segmentIndex < otherSegmentIndex ? -1 : 1;
117
- }
118
- if (segment._key < otherSegment._key)
119
- return -1;
120
- if (segment._key > otherSegment._key)
121
- return 1;
122
- continue;
123
- }
124
- if (typeof segment == "string" && typeof otherSegment == "string") {
125
- if (segment === otherSegment) {
126
- if (currentNode) {
127
- const fieldValue = currentNode[segment];
128
- currentChildren = Array.isArray(fieldValue) ? fieldValue : void 0, currentNode = void 0;
129
- }
130
- continue;
131
- }
132
- if (segment < otherSegment)
133
- return -1;
134
- if (segment > otherSegment)
135
- return 1;
136
- continue;
137
- }
138
- if (typeof segment == "number" && typeof otherSegment == "number") {
139
- if (segment < otherSegment)
140
- return -1;
141
- if (segment > otherSegment)
142
- return 1;
143
- continue;
144
- }
145
- break;
146
- }
147
- return 0;
148
- }
149
- function comparePoints$1(point, another, root) {
150
- const result = comparePaths(point.path, another.path, root);
151
- return result === 0 ? point.offset < another.offset ? -1 : point.offset > another.offset ? 1 : 0 : result;
152
- }
153
- function isAfterPoint(point, another, root) {
154
- return comparePoints$1(point, another, root) === 1;
155
- }
156
- function isBackwardRange(range, root) {
157
- const {
158
- anchor,
159
- focus
160
- } = range;
161
- return isAfterPoint(anchor, focus, root);
162
- }
163
- function rangeEdges(range, root) {
164
- const {
165
- anchor,
166
- focus
167
- } = range;
168
- return isBackwardRange(range, root) ? [focus, anchor] : [anchor, focus];
169
- }
170
- function isTextBlockNode(context, node) {
171
- return isTypedObject(node) && node._type === context.schema.block.name;
172
- }
173
- function isAncestorPath(path, another) {
174
- if (path.length >= another.length)
175
- return !1;
176
- for (let i = 0; i < path.length; i++) {
177
- const segment = path[i], otherSegment = another[i];
178
- if (isKeyedSegment(segment) && isKeyedSegment(otherSegment)) {
179
- if (segment._key !== otherSegment._key)
180
- return !1;
181
- } else if (segment !== otherSegment)
182
- return !1;
183
- }
184
- return !0;
185
- }
186
- function resolveChildEntryIndex(blockIndexMap, children, childPath) {
187
- const lastSegment = childPath[childPath.length - 1];
188
- if (typeof lastSegment == "number")
189
- return lastSegment >= 0 && lastSegment < children.length ? lastSegment : -1;
190
- if (!isKeyedSegment(lastSegment))
191
- return -1;
192
- let fullyKeyed = !0;
193
- for (let segmentIndex = 0; segmentIndex < childPath.length - 1; segmentIndex++) {
194
- const segment = childPath[segmentIndex];
195
- if (typeof segment != "string" && !isKeyedSegment(segment)) {
196
- fullyKeyed = !1;
197
- break;
198
- }
199
- }
200
- if (fullyKeyed) {
201
- const mappedIndex = blockIndexMap.get(serializePath(childPath));
202
- if (mappedIndex !== void 0 && children[mappedIndex]?.node._key === lastSegment._key)
203
- return mappedIndex;
204
- }
205
- return children.findIndex((child) => child.node._key === lastSegment._key);
206
- }
207
- function* getNodes(snapshot, options = {}) {
208
- const {
209
- at = [],
210
- from,
211
- to,
212
- match,
213
- reverse = !1
214
- } = options;
215
- if (from === void 0 && to === void 0) {
216
- yield* getNodesSimple(snapshot, at, {
217
- match,
218
- reverse
219
- });
220
- return;
221
- }
222
- yield* getNodesInRange(snapshot, at, {
223
- from,
224
- to,
225
- match,
226
- reverse
227
- });
228
- }
229
- function* getNodesSimple(snapshot, path, options) {
230
- const {
231
- match,
232
- reverse = !1
233
- } = options, children = getChildren(snapshot, path), entries = reverse ? [...children].reverse() : children;
234
- for (const entry of entries)
235
- (!match || match(entry.node, entry.path)) && (yield entry), yield* getNodesSimple(snapshot, entry.path, options);
236
- }
237
- function comparePathsInTree(snapshot, pathA, pathB) {
238
- const minLength = Math.min(pathA.length, pathB.length), prefix = [];
239
- for (let i = 0; i < minLength; i++) {
240
- const segA = pathA[i], segB = pathB[i];
241
- if (typeof segA == "string" || typeof segB == "string" || typeof segA == "number" || typeof segB == "number" || Array.isArray(segA) || Array.isArray(segB)) {
242
- prefix.push(segA);
243
- continue;
244
- }
245
- if (segA._key === segB._key) {
246
- prefix.push(segA);
247
- continue;
248
- }
249
- const indexA = snapshot.blockIndexMap.get(serializePath([...prefix, segA])) ?? -1, indexB = snapshot.blockIndexMap.get(serializePath([...prefix, segB])) ?? -1;
250
- return indexA < indexB ? -1 : indexA > indexB ? 1 : 0;
251
- }
252
- return pathA.length < pathB.length ? -1 : pathA.length > pathB.length ? 1 : 0;
253
- }
254
- function* getNodesInRange(snapshot, path, options) {
255
- const {
256
- from,
257
- to,
258
- match,
259
- reverse = !1
260
- } = options, children = getChildren(snapshot, path), startIndex = from === void 0 ? 0 : boundaryChildIndex(snapshot, children, from) ?? 0, endIndex = to === void 0 ? children.length - 1 : boundaryChildIndex(snapshot, children, to) ?? children.length - 1, window = children.slice(startIndex, endIndex + 1), entries = reverse ? window.reverse() : window;
261
- for (const entry of entries) {
262
- if (canStopTraversal(snapshot, entry.path, from, to, reverse))
263
- return;
264
- couldContainInRangeNodes(snapshot, entry.path, from, to) && (isInRange(snapshot, entry.path, from, to) && (!match || match(entry.node, entry.path)) && (yield entry), yield* getNodesInRange(snapshot, entry.path, options));
265
- }
266
- }
267
- function boundaryChildIndex(snapshot, children, boundary) {
268
- const firstChild = children[0];
269
- if (!firstChild)
270
- return;
271
- const childPathLength = firstChild.path.length;
272
- if (boundary.length < childPathLength)
273
- return;
274
- const sharedPrefix = firstChild.path.slice(0, -1);
275
- if (sharedPrefix.length > 0 && !isAncestorPath(sharedPrefix, boundary))
276
- return;
277
- const resolvedIndex = resolveChildEntryIndex(snapshot.blockIndexMap, children, boundary.slice(0, childPathLength));
278
- return resolvedIndex === -1 ? void 0 : resolvedIndex;
279
- }
280
- function isInRange(snapshot, nodePath, from, to) {
281
- return !(from !== void 0 && comparePathsInTree(snapshot, nodePath, from) === -1 && !isAncestorPath(nodePath, from) || to !== void 0 && comparePathsInTree(snapshot, nodePath, to) === 1 && !isAncestorPath(nodePath, to));
282
- }
283
- function couldContainInRangeNodes(snapshot, nodePath, from, to) {
284
- return !!(isInRange(snapshot, nodePath, from, to) || from !== void 0 && isAncestorPath(nodePath, from) || to !== void 0 && isAncestorPath(nodePath, to));
285
- }
286
- function canStopTraversal(snapshot, nodePath, from, to, reverse) {
287
- return reverse ? from === void 0 ? !1 : comparePathsInTree(snapshot, nodePath, from) === -1 && !isAncestorPath(nodePath, from) : to === void 0 ? !1 : comparePathsInTree(snapshot, nodePath, to) === 1;
288
- }
289
- function getSibling(snapshot, path, options) {
290
- const {
291
- direction,
292
- match
293
- } = options;
294
- if (path.length === 0)
295
- return;
296
- const parent = parentPath(path), children = getChildren(snapshot, parent), currentIndex = resolveChildEntryIndex(snapshot.blockIndexMap, children, path);
297
- if (currentIndex !== -1) {
298
- if (!match) {
299
- const siblingIndex = direction === "next" ? currentIndex + 1 : currentIndex - 1;
300
- return siblingIndex < 0 || siblingIndex >= children.length ? void 0 : children[siblingIndex];
301
- }
302
- return (direction === "next" ? children.slice(currentIndex + 1) : children.slice(0, currentIndex).reverse()).find((child) => match(child.node, child.path));
303
- }
304
- }
305
- function isSpanNode(context, node) {
306
- return isTypedObject(node) && node._type === context.schema.span.name;
307
- }
308
- function isBlock(snapshot, path) {
309
- const parent = getParent(snapshot, path);
310
- return parent ? !isTextBlockNode({
311
- schema: snapshot.context.schema
312
- }, parent.node) : !0;
313
- }
314
- function getBlock(snapshot, path) {
315
- const entry = getNode(snapshot, path);
316
- if (entry && isBlock(snapshot, path) && !isSpanNode({
317
- schema: snapshot.context.schema
318
- }, entry.node))
319
- return {
320
- node: entry.node,
321
- path: entry.path
322
- };
323
- }
324
- function isInline(snapshot, path) {
325
- return !isBlock(snapshot, path);
326
- }
327
- function getEnclosingBlock(snapshot, path, options) {
328
- const match = options?.match;
329
- if ((options?.mode ?? "lowest") === "highest") {
330
- const ancestors = getAncestors(snapshot, path);
331
- for (const ancestor of [...ancestors].reverse())
332
- if (!match || match(ancestor.node, ancestor.path))
333
- return ancestor;
334
- const direct2 = getBlock(snapshot, path);
335
- return direct2 && (!match || match(direct2.node, direct2.path)) ? direct2 : void 0;
336
- }
337
- const direct = getBlock(snapshot, path);
338
- if (direct && (!match || match(direct.node, direct.path)))
339
- return direct;
340
- for (const ancestor of getAncestors(snapshot, path))
341
- if (!match || match(ancestor.node, ancestor.path))
342
- return ancestor;
343
- }
344
- function comparePoints(snapshot, pointA, pointB) {
345
- const pathComparison = comparePaths(pointA.path, pointB.path, {
346
- value: snapshot.context.value
347
- });
348
- return pathComparison !== 0 ? pathComparison : pointA.offset < pointB.offset ? -1 : pointA.offset > pointB.offset ? 1 : 0;
349
- }
350
- function descendToParent(snapshot, path) {
351
- const ancestors = getAncestors(snapshot, path);
352
- for (const ancestor of ancestors) {
353
- if (!isObject(snapshot, ancestor.node))
354
- continue;
355
- const resolved = resolveContainerAt(snapshot.context.containers, snapshot.context.value, ancestor.path);
356
- return !resolved || !("field" in resolved) ? void 0 : {
357
- parent: resolved,
358
- parentPath: ancestor.path
359
- };
360
- }
361
- }
362
- function getEnclosingContainer(snapshot, path) {
363
- const descent = descendToParent(snapshot, path);
364
- if (descent)
365
- return {
366
- of: descent.parent.field.of,
367
- path: descent.parentPath
368
- };
369
- }
370
- function getPathSubSchema(snapshot, path) {
371
- const enclosing = getEnclosingContainer(snapshot, path);
372
- return enclosing ? getSubSchema(snapshot.context.schema, enclosing.of) : snapshot.context.schema;
373
- }
374
- export {
375
- comparePaths,
376
- comparePoints,
377
- comparePoints$1,
378
- getAncestors,
379
- getBlock,
380
- getEnclosingBlock,
381
- getEnclosingContainer,
382
- getNodes,
383
- getPathSubSchema,
384
- getSibling,
385
- hasNode,
386
- isAfterPoint,
387
- isAncestorPath,
388
- isBackwardRange,
389
- isBlock,
390
- isEditableContainer,
391
- isInline,
392
- isObject,
393
- isSpanNode,
394
- isTextBlockNode,
395
- rangeEdges,
396
- resolveChildEntryIndex,
397
- resolveContainerAt
398
- };
399
- //# sourceMappingURL=get-path-sub-schema.js.map