@portabletext/editor 7.10.16 → 7.10.18
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 +25 -20
- 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,633 @@
|
|
|
1
|
+
import { c as isTypedObject, r as getNode, s as isRecord, t as getParent, u as isKeyedSegment } from "./get-parent-51Kzgaf9.js";
|
|
2
|
+
import { getSubSchema, isSpan, isTextBlock } from "@portabletext/schema";
|
|
3
|
+
/**
|
|
4
|
+
* Pure text-offset arithmetic over a node's children array, shared by
|
|
5
|
+
* the snapshot-aware block-offset utils (`util.block-offset.ts`) and
|
|
6
|
+
* the schema-free point transform (`engine/point/transform-point.ts`).
|
|
7
|
+
* Span-ness is the caller's concern, expressed as an accessor that
|
|
8
|
+
* returns the child's text when the child occupies text offsets and
|
|
9
|
+
* `undefined` when it does not (inline objects, blocks).
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* The text offset of `(childKey, offsetInChild)` within `children`:
|
|
13
|
+
* the total text of the children before that child, plus the offset
|
|
14
|
+
* into it. `undefined` when the child is missing or carries no text.
|
|
15
|
+
*/
|
|
16
|
+
function textOffsetOfChild(children, getSpanText, childKey, offsetInChild) {
|
|
17
|
+
let precedingTextLength = 0;
|
|
18
|
+
for (let child of children) {
|
|
19
|
+
let text = getSpanText(child);
|
|
20
|
+
if (keyOf(child) === childKey) return text === void 0 ? void 0 : precedingTextLength + offsetInChild;
|
|
21
|
+
text !== void 0 && (precedingTextLength += text.length);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The child and child-local offset at `textOffset` within `children`.
|
|
26
|
+
* Forward-boundary convention: an offset landing exactly on a span
|
|
27
|
+
* boundary stays at the end of the earlier span. `undefined` when the
|
|
28
|
+
* offset lies beyond the children's total text.
|
|
29
|
+
*/
|
|
30
|
+
function childAtTextOffset(children, getSpanText, textOffset) {
|
|
31
|
+
let remainingOffset = textOffset;
|
|
32
|
+
for (let child of children) {
|
|
33
|
+
let text = getSpanText(child);
|
|
34
|
+
if (text !== void 0) {
|
|
35
|
+
if (remainingOffset <= text.length) {
|
|
36
|
+
let key = keyOf(child);
|
|
37
|
+
return key === void 0 ? void 0 : {
|
|
38
|
+
key,
|
|
39
|
+
offset: remainingOffset
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
remainingOffset -= text.length;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function keyOf(child) {
|
|
47
|
+
if (typeof child == "object" && child && "_key" in child) {
|
|
48
|
+
let key = child._key;
|
|
49
|
+
return typeof key == "string" ? key : void 0;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* @public
|
|
54
|
+
*/
|
|
55
|
+
function blockOffsetToSpanSelectionPoint({ snapshot, blockOffset, direction }) {
|
|
56
|
+
let blockEntry = getNode(snapshot, blockOffset.path);
|
|
57
|
+
if (!blockEntry || !isTextBlock(snapshot.context, blockEntry.node)) return;
|
|
58
|
+
let block = blockEntry.node, blockPath = blockEntry.path;
|
|
59
|
+
if (direction === "forward") {
|
|
60
|
+
let placed = childAtTextOffset(block.children, (child) => isSpan(snapshot.context, child) ? child.text : void 0, blockOffset.offset);
|
|
61
|
+
return placed ? {
|
|
62
|
+
path: [
|
|
63
|
+
...blockPath,
|
|
64
|
+
"children",
|
|
65
|
+
{ _key: placed.key }
|
|
66
|
+
],
|
|
67
|
+
offset: placed.offset
|
|
68
|
+
} : void 0;
|
|
69
|
+
}
|
|
70
|
+
let offsetLeft = blockOffset.offset, selectionPoint, skippedInlineObject = !1;
|
|
71
|
+
for (let child of block.children) {
|
|
72
|
+
if (!isSpan(snapshot.context, child)) {
|
|
73
|
+
skippedInlineObject = !0;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (offsetLeft === 0 && selectionPoint && !skippedInlineObject) break;
|
|
77
|
+
if (offsetLeft > child.text.length) {
|
|
78
|
+
offsetLeft -= child.text.length;
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
if (offsetLeft <= child.text.length && (selectionPoint = {
|
|
82
|
+
path: [
|
|
83
|
+
...blockPath,
|
|
84
|
+
"children",
|
|
85
|
+
{ _key: child._key }
|
|
86
|
+
],
|
|
87
|
+
offset: offsetLeft
|
|
88
|
+
}, offsetLeft -= child.text.length, offsetLeft !== 0)) break;
|
|
89
|
+
}
|
|
90
|
+
return selectionPoint;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* @public
|
|
94
|
+
*/
|
|
95
|
+
function spanSelectionPointToBlockOffset({ snapshot, selectionPoint }) {
|
|
96
|
+
let spanSegment = selectionPoint.path.at(-1);
|
|
97
|
+
if (!isKeyedSegment(spanSegment)) return;
|
|
98
|
+
let textBlock = getParent(snapshot, selectionPoint.path, { match: (node) => isTextBlock({ schema: snapshot.context.schema }, node) });
|
|
99
|
+
if (!textBlock) return;
|
|
100
|
+
let offset = textOffsetOfChild(textBlock.node.children, (child) => isSpan(snapshot.context, child) ? child.text : void 0, spanSegment._key, selectionPoint.offset);
|
|
101
|
+
return offset === void 0 ? void 0 : {
|
|
102
|
+
path: textBlock.path,
|
|
103
|
+
offset
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function isEqualPathSegments(segA, segB) {
|
|
107
|
+
return segA === segB ? !0 : segA === void 0 || segB === void 0 ? !1 : (typeof segA == "string" || typeof segA == "number") && (typeof segB == "string" || typeof segB == "number") ? segA === segB : isKeyedSegment(segA) && isKeyedSegment(segB) ? segA._key === segB._key : Array.isArray(segA) && Array.isArray(segB) ? segA[0] === segB[0] && segA[1] === segB[1] : !1;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* @public
|
|
111
|
+
*/
|
|
112
|
+
function isEqualPaths(a, b) {
|
|
113
|
+
if (a.length !== b.length) return !1;
|
|
114
|
+
for (let i = 0; i < a.length; i++) if (!isEqualPathSegments(a[i], b[i])) return !1;
|
|
115
|
+
return !0;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* @public
|
|
119
|
+
*/
|
|
120
|
+
function isEqualSelectionPoints(a, b) {
|
|
121
|
+
return a.offset === b.offset && isEqualPaths(a.path, b.path);
|
|
122
|
+
}
|
|
123
|
+
function getBlockKeyFromSelectionPoint(point) {
|
|
124
|
+
let blockPathSegment = point.path.at(0);
|
|
125
|
+
if (isKeyedSegment(blockPathSegment)) return blockPathSegment._key;
|
|
126
|
+
}
|
|
127
|
+
function getChildKeyFromSelectionPoint(point) {
|
|
128
|
+
let childPathSegment = point.path.at(2);
|
|
129
|
+
if (isKeyedSegment(childPathSegment)) return childPathSegment._key;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* @public
|
|
133
|
+
*/
|
|
134
|
+
function getBlockEndPoint({ context, block }) {
|
|
135
|
+
if (isTextBlock(context, block.node)) {
|
|
136
|
+
let lastChild = block.node.children[block.node.children.length - 1];
|
|
137
|
+
if (lastChild) return {
|
|
138
|
+
path: [
|
|
139
|
+
...block.path,
|
|
140
|
+
"children",
|
|
141
|
+
{ _key: lastChild._key }
|
|
142
|
+
],
|
|
143
|
+
offset: isSpan(context, lastChild) ? lastChild.text.length : 0
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
return {
|
|
147
|
+
path: block.path,
|
|
148
|
+
offset: 0
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* @public
|
|
153
|
+
*/
|
|
154
|
+
function getBlockStartPoint({ context, block }) {
|
|
155
|
+
if (isTextBlock(context, block.node)) {
|
|
156
|
+
let firstChild = block.node.children[0];
|
|
157
|
+
return {
|
|
158
|
+
path: [
|
|
159
|
+
...block.path,
|
|
160
|
+
"children",
|
|
161
|
+
{ _key: firstChild?._key ?? "" }
|
|
162
|
+
],
|
|
163
|
+
offset: 0
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
return {
|
|
167
|
+
path: block.path,
|
|
168
|
+
offset: 0
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* @public
|
|
173
|
+
*/
|
|
174
|
+
function isSelectionCollapsed(selection) {
|
|
175
|
+
return selection ? isEqualPaths(selection.anchor.path, selection.focus.path) && selection.anchor.offset === selection.focus.offset : !1;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* @public
|
|
179
|
+
*/
|
|
180
|
+
function getSelectionEndPoint(selection) {
|
|
181
|
+
return selection ? selection.backward ? selection.anchor : selection.focus : null;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* @public
|
|
185
|
+
*/
|
|
186
|
+
function getSelectionStartPoint(selection) {
|
|
187
|
+
return selection ? selection.backward ? selection.focus : selection.anchor : null;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* @public
|
|
191
|
+
*/
|
|
192
|
+
const defaultKeyGenerator = () => randomKey(12), getByteHexTable = (() => {
|
|
193
|
+
let table;
|
|
194
|
+
return () => {
|
|
195
|
+
if (table) return table;
|
|
196
|
+
table = [];
|
|
197
|
+
for (let i = 0; i < 256; ++i) table[i] = (i + 256).toString(16).slice(1);
|
|
198
|
+
return table;
|
|
199
|
+
};
|
|
200
|
+
})();
|
|
201
|
+
function whatwgRNG(length = 16) {
|
|
202
|
+
let rnds8 = new Uint8Array(length);
|
|
203
|
+
return crypto.getRandomValues(rnds8), rnds8;
|
|
204
|
+
}
|
|
205
|
+
function randomKey(length) {
|
|
206
|
+
let table = getByteHexTable();
|
|
207
|
+
return whatwgRNG(length).reduce((str, n) => str + table[n], "").slice(0, length);
|
|
208
|
+
}
|
|
209
|
+
function parseBlocks({ schema, keyGenerator, blocks, options }) {
|
|
210
|
+
return Array.isArray(blocks) ? blocks.flatMap((block) => {
|
|
211
|
+
let parsedBlock = parseBlockInternal({
|
|
212
|
+
schema,
|
|
213
|
+
keyGenerator,
|
|
214
|
+
block,
|
|
215
|
+
options
|
|
216
|
+
});
|
|
217
|
+
return parsedBlock ? [parsedBlock] : [];
|
|
218
|
+
}) : [];
|
|
219
|
+
}
|
|
220
|
+
function parseBlock({ schema, keyGenerator, block, options }) {
|
|
221
|
+
return parseBlockInternal({
|
|
222
|
+
schema,
|
|
223
|
+
keyGenerator,
|
|
224
|
+
block,
|
|
225
|
+
options
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
function parseSpan({ span, schema, keyGenerator, markDefKeyMap, options }) {
|
|
229
|
+
return parseSpanInternal({
|
|
230
|
+
span,
|
|
231
|
+
schema,
|
|
232
|
+
keyGenerator,
|
|
233
|
+
markDefKeyMap,
|
|
234
|
+
options
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
function parseInlineObject({ inlineObject, schema, keyGenerator, options }) {
|
|
238
|
+
return parseInlineObjectInternal({
|
|
239
|
+
inlineObject,
|
|
240
|
+
schema,
|
|
241
|
+
keyGenerator,
|
|
242
|
+
options
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
function parseMarkDefs({ schema, keyGenerator, markDefs, options }) {
|
|
246
|
+
return parseMarkDefsInternal({
|
|
247
|
+
schema,
|
|
248
|
+
keyGenerator,
|
|
249
|
+
markDefs,
|
|
250
|
+
options
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
function parseBlockInternal({ schema, keyGenerator, block, options }) {
|
|
254
|
+
return parseTextBlock({
|
|
255
|
+
block,
|
|
256
|
+
schema,
|
|
257
|
+
keyGenerator,
|
|
258
|
+
options
|
|
259
|
+
}) ?? parseBlockObject({
|
|
260
|
+
blockObject: block,
|
|
261
|
+
schema,
|
|
262
|
+
keyGenerator,
|
|
263
|
+
options
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
function parseBlockObject({ blockObject, schema, keyGenerator, options }) {
|
|
267
|
+
if (!isTypedObject(blockObject)) return;
|
|
268
|
+
let schemaType = schema.blockObjects.find(({ name }) => name === blockObject._type);
|
|
269
|
+
if (schemaType) return parseObject({
|
|
270
|
+
object: blockObject,
|
|
271
|
+
schema,
|
|
272
|
+
keyGenerator,
|
|
273
|
+
schemaType,
|
|
274
|
+
options
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
function isListBlock(context, block) {
|
|
278
|
+
return isTextBlock(context, block) && block.level !== void 0 && block.listItem !== void 0;
|
|
279
|
+
}
|
|
280
|
+
function parseTextBlock({ block, schema, keyGenerator, options }) {
|
|
281
|
+
if (!isTypedObject(block) || block._type !== schema.block.name) return;
|
|
282
|
+
let customFields = {};
|
|
283
|
+
for (let key of Object.keys(block)) key !== "_type" && key !== "_key" && key !== "children" && key !== "markDefs" && key !== "style" && key !== "listItem" && key !== "level" && (options.validateFields ? (schema.block.fields ?? []).some((f) => f.name === key) && (customFields[key] = block[key]) : customFields[key] = block[key]);
|
|
284
|
+
let _key = typeof block._key == "string" ? block._key : keyGenerator(), { markDefs, markDefKeyMap } = parseMarkDefsInternal({
|
|
285
|
+
schema,
|
|
286
|
+
keyGenerator,
|
|
287
|
+
markDefs: block.markDefs,
|
|
288
|
+
options
|
|
289
|
+
}), parsedChildren = (Array.isArray(block.children) ? block.children : []).map((child) => parseChildInternal({
|
|
290
|
+
child,
|
|
291
|
+
schema,
|
|
292
|
+
keyGenerator,
|
|
293
|
+
markDefKeyMap,
|
|
294
|
+
options
|
|
295
|
+
})).filter((child) => child !== void 0), marks = parsedChildren.flatMap((child) => child.marks ?? []), children = parsedChildren.length > 0 ? parsedChildren : [{
|
|
296
|
+
_key: keyGenerator(),
|
|
297
|
+
_type: schema.span.name,
|
|
298
|
+
text: "",
|
|
299
|
+
marks: []
|
|
300
|
+
}], normalizedChildren = options.normalize ? children.reduce((normalizedChildren, child, index) => {
|
|
301
|
+
if (isSpan({ schema }, child)) return [...normalizedChildren, child];
|
|
302
|
+
let previousChild = normalizedChildren.at(-1);
|
|
303
|
+
return !previousChild || !isSpan({ schema }, previousChild) ? [
|
|
304
|
+
...normalizedChildren,
|
|
305
|
+
{
|
|
306
|
+
_key: keyGenerator(),
|
|
307
|
+
_type: schema.span.name,
|
|
308
|
+
text: "",
|
|
309
|
+
marks: []
|
|
310
|
+
},
|
|
311
|
+
child,
|
|
312
|
+
...index === children.length - 1 ? [{
|
|
313
|
+
_key: keyGenerator(),
|
|
314
|
+
_type: schema.span.name,
|
|
315
|
+
text: "",
|
|
316
|
+
marks: []
|
|
317
|
+
}] : []
|
|
318
|
+
] : [...normalizedChildren, child];
|
|
319
|
+
}, []) : children, parsedBlock = {
|
|
320
|
+
_type: schema.block.name,
|
|
321
|
+
_key,
|
|
322
|
+
children: normalizedChildren,
|
|
323
|
+
...customFields
|
|
324
|
+
};
|
|
325
|
+
return typeof block.markDefs == "object" && block.markDefs !== null && (parsedBlock.markDefs = options.removeUnusedMarkDefs ? markDefs.filter((markDef) => marks.includes(markDef._key)) : markDefs), typeof block.style == "string" && schema.styles.find((style) => style.name === block.style) && (parsedBlock.style = block.style), typeof block.listItem == "string" && schema.lists.find((list) => list.name === block.listItem) && (parsedBlock.listItem = block.listItem), typeof block.level == "number" && (parsedBlock.level = block.level), parsedBlock;
|
|
326
|
+
}
|
|
327
|
+
function parseMarkDefsInternal({ schema, keyGenerator, markDefs, options }) {
|
|
328
|
+
let unparsedMarkDefs = Array.isArray(markDefs) ? markDefs : [], markDefKeyMap = /* @__PURE__ */ new Map();
|
|
329
|
+
return {
|
|
330
|
+
markDefs: unparsedMarkDefs.flatMap((markDef) => {
|
|
331
|
+
if (!isTypedObject(markDef)) return [];
|
|
332
|
+
let schemaType = schema.annotations.find(({ name }) => name === markDef._type);
|
|
333
|
+
if (!schemaType || typeof markDef._key != "string") return [];
|
|
334
|
+
let parsedAnnotation = parseObject({
|
|
335
|
+
object: markDef,
|
|
336
|
+
schema,
|
|
337
|
+
keyGenerator,
|
|
338
|
+
schemaType,
|
|
339
|
+
options
|
|
340
|
+
});
|
|
341
|
+
return parsedAnnotation ? (markDefKeyMap.set(markDef._key, parsedAnnotation._key), [parsedAnnotation]) : [];
|
|
342
|
+
}),
|
|
343
|
+
markDefKeyMap
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
function parseChildInternal({ child, schema, keyGenerator, markDefKeyMap, options }) {
|
|
347
|
+
return parseSpanInternal({
|
|
348
|
+
span: child,
|
|
349
|
+
schema,
|
|
350
|
+
keyGenerator,
|
|
351
|
+
markDefKeyMap,
|
|
352
|
+
options
|
|
353
|
+
}) ?? parseInlineObjectInternal({
|
|
354
|
+
inlineObject: child,
|
|
355
|
+
schema,
|
|
356
|
+
keyGenerator,
|
|
357
|
+
options
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
function parseSpanInternal({ span, schema, keyGenerator, markDefKeyMap, options }) {
|
|
361
|
+
if (!isRecord(span)) return;
|
|
362
|
+
let customFields = {};
|
|
363
|
+
for (let key of Object.keys(span)) key !== "_type" && key !== "_key" && key !== "text" && key !== "marks" && (customFields[key] = span[key]);
|
|
364
|
+
let marks = (Array.isArray(span.marks) ? span.marks : []).flatMap((mark) => {
|
|
365
|
+
if (typeof mark != "string") return [];
|
|
366
|
+
let markDefKey = markDefKeyMap.get(mark);
|
|
367
|
+
return markDefKey === void 0 ? schema.decorators.some((decorator) => decorator.name === mark) ? [mark] : [] : [markDefKey];
|
|
368
|
+
});
|
|
369
|
+
if (typeof span._type != "string" || span._type === schema.span.name) return typeof span._type == "string" ? {
|
|
370
|
+
_type: schema.span.name,
|
|
371
|
+
_key: typeof span._key == "string" ? span._key : keyGenerator(),
|
|
372
|
+
text: typeof span.text == "string" ? span.text : "",
|
|
373
|
+
marks,
|
|
374
|
+
...options.validateFields ? {} : customFields
|
|
375
|
+
} : typeof span.text == "string" ? {
|
|
376
|
+
_type: schema.span.name,
|
|
377
|
+
_key: typeof span._key == "string" ? span._key : keyGenerator(),
|
|
378
|
+
text: span.text,
|
|
379
|
+
marks,
|
|
380
|
+
...options.validateFields ? {} : customFields
|
|
381
|
+
} : void 0;
|
|
382
|
+
}
|
|
383
|
+
function parseInlineObjectInternal({ inlineObject, schema, keyGenerator, options }) {
|
|
384
|
+
if (!isTypedObject(inlineObject)) return;
|
|
385
|
+
let schemaType = schema.inlineObjects.find(({ name }) => name === inlineObject._type);
|
|
386
|
+
if (schemaType) return parseObject({
|
|
387
|
+
object: inlineObject,
|
|
388
|
+
schema,
|
|
389
|
+
keyGenerator,
|
|
390
|
+
schemaType,
|
|
391
|
+
options
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
function parseAnnotation({ annotation, schema, keyGenerator, options }) {
|
|
395
|
+
if (!isTypedObject(annotation)) return;
|
|
396
|
+
let schemaType = schema.annotations.find(({ name }) => name === annotation._type);
|
|
397
|
+
if (schemaType) return parseObject({
|
|
398
|
+
object: annotation,
|
|
399
|
+
schema,
|
|
400
|
+
keyGenerator,
|
|
401
|
+
schemaType,
|
|
402
|
+
options
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* Resolve an `of` member against a runtime `_type`. Returns the schema-type
|
|
407
|
+
* to use for parsing the item, or `undefined` if no member matches.
|
|
408
|
+
*
|
|
409
|
+
* Three forms (one branch each):
|
|
410
|
+
* - Inline declaration `{type: 'object', name: 'X', fields: [...]}` -- name
|
|
411
|
+
* matches `_type`, fields are inline.
|
|
412
|
+
* - Reference `{type: 'X'}` -- looks up in the ancestor chain first
|
|
413
|
+
* (inline-declared types are visible to descendants), then in the schema's
|
|
414
|
+
* root `blockObjects`. Mirrors `resolve-containers.ts`'s reference
|
|
415
|
+
* resolution.
|
|
416
|
+
* - `{type: 'block'}` -- not a non-PTE object member, returns `undefined`.
|
|
417
|
+
*/
|
|
418
|
+
function resolveOfMember(of, typeName, schema, ancestorFields) {
|
|
419
|
+
for (let member of of) if (member.type !== "block") {
|
|
420
|
+
if (member.type === "object" && "name" in member && member.name) {
|
|
421
|
+
if (member.name === typeName && "fields" in member && member.fields) return {
|
|
422
|
+
name: member.name,
|
|
423
|
+
fields: member.fields
|
|
424
|
+
};
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
if (member.type === typeName) {
|
|
428
|
+
let ancestorMatch = ancestorFields.get(typeName);
|
|
429
|
+
if (ancestorMatch) return {
|
|
430
|
+
name: typeName,
|
|
431
|
+
fields: ancestorMatch
|
|
432
|
+
};
|
|
433
|
+
let rootMatch = schema.blockObjects.find((blockObject) => blockObject.name === typeName);
|
|
434
|
+
return rootMatch && "fields" in rootMatch && rootMatch.fields ? {
|
|
435
|
+
name: rootMatch.name,
|
|
436
|
+
fields: rootMatch.fields
|
|
437
|
+
} : void 0;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Parse an object against a `{name, fields}` schema type. Validates top-level
|
|
443
|
+
* fields and recurses into any array field whose `of` contains a block-like
|
|
444
|
+
* member -- parsing the nested blocks against a child `Schema` built from
|
|
445
|
+
* that `of`.
|
|
446
|
+
*/
|
|
447
|
+
function parseObject({ object, schema, keyGenerator, schemaType, ancestorFields, options }) {
|
|
448
|
+
let { _key, ...customFields } = object, fieldsByName = new Map(schemaType.fields.map((field) => [field.name, field])), nextAncestors = new Map(ancestorFields ?? []);
|
|
449
|
+
nextAncestors.set(schemaType.name, schemaType.fields);
|
|
450
|
+
let values = {};
|
|
451
|
+
for (let [key, value] of Object.entries(customFields)) {
|
|
452
|
+
if (key === "_type" || value === void 0) continue;
|
|
453
|
+
let field = fieldsByName.get(key);
|
|
454
|
+
if (!(options.validateFields && !field)) {
|
|
455
|
+
if (field && field.type === "array" && field.of && Array.isArray(value)) {
|
|
456
|
+
values[key] = parseContainerFieldValue({
|
|
457
|
+
schema,
|
|
458
|
+
keyGenerator,
|
|
459
|
+
of: field.of,
|
|
460
|
+
value,
|
|
461
|
+
ancestorFields: nextAncestors,
|
|
462
|
+
options
|
|
463
|
+
});
|
|
464
|
+
continue;
|
|
465
|
+
}
|
|
466
|
+
values[key] = value;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
return {
|
|
470
|
+
_type: schemaType.name,
|
|
471
|
+
_key: typeof _key == "string" ? _key : keyGenerator(),
|
|
472
|
+
...values
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* Parse the value of an array field whose `of` declares what's allowed at
|
|
477
|
+
* that position. Each item is dispatched per its own type:
|
|
478
|
+
*
|
|
479
|
+
* - Text blocks (`_type === schema.block.name`) are parsed against a child
|
|
480
|
+
* sub-schema derived from the `{type: 'block'}` member of `of` (which
|
|
481
|
+
* carries the styles, decorators, annotations, lists, and inline objects
|
|
482
|
+
* allowed at this position).
|
|
483
|
+
*
|
|
484
|
+
* - Block objects (and structural objects sitting between containers and
|
|
485
|
+
* text blocks) are parsed via `resolveOfMember` against the `of` array,
|
|
486
|
+
* with `ancestorFields` carrying inline type declarations down to bare
|
|
487
|
+
* references like `{type: 'list'}`. This lets nested containers resolve
|
|
488
|
+
* to their inline shapes at any depth.
|
|
489
|
+
*
|
|
490
|
+
* Items that don't resolve are passed through unchanged.
|
|
491
|
+
*/
|
|
492
|
+
function parseContainerFieldValue({ schema, keyGenerator, of, value, ancestorFields, options }) {
|
|
493
|
+
let childBlockSubSchema = of.some((member) => member.type === "block") ? getSubSchema(schema, of) : void 0;
|
|
494
|
+
return value.flatMap((item) => {
|
|
495
|
+
if (!isTypedObject(item)) return [item];
|
|
496
|
+
if (childBlockSubSchema && item._type === childBlockSubSchema.block.name) {
|
|
497
|
+
let parsed = parseTextBlock({
|
|
498
|
+
block: item,
|
|
499
|
+
schema: childBlockSubSchema,
|
|
500
|
+
keyGenerator,
|
|
501
|
+
options: {
|
|
502
|
+
normalize: !1,
|
|
503
|
+
removeUnusedMarkDefs: !1,
|
|
504
|
+
validateFields: options.validateFields
|
|
505
|
+
}
|
|
506
|
+
});
|
|
507
|
+
return parsed ? [parsed] : [];
|
|
508
|
+
}
|
|
509
|
+
let schemaType = resolveOfMember(of, item._type, schema, ancestorFields);
|
|
510
|
+
return schemaType ? [parseObject({
|
|
511
|
+
object: item,
|
|
512
|
+
schema,
|
|
513
|
+
keyGenerator,
|
|
514
|
+
schemaType: {
|
|
515
|
+
name: schemaType.name,
|
|
516
|
+
fields: schemaType.fields
|
|
517
|
+
},
|
|
518
|
+
ancestorFields,
|
|
519
|
+
options
|
|
520
|
+
})] : [item];
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* @public
|
|
525
|
+
*/
|
|
526
|
+
function sliceBlocks({ context, blocks }) {
|
|
527
|
+
let slice = [];
|
|
528
|
+
if (!context.selection) return slice;
|
|
529
|
+
let startBlock, middleBlocks = [], endBlock, startPoint = getSelectionStartPoint(context.selection), endPoint = getSelectionEndPoint(context.selection), startBlockKey = getBlockKeyFromSelectionPoint(startPoint), startChildKey = getChildKeyFromSelectionPoint(startPoint), endBlockKey = getBlockKeyFromSelectionPoint(endPoint), endChildKey = getChildKeyFromSelectionPoint(endPoint);
|
|
530
|
+
if (!startBlockKey || !endBlockKey) return slice;
|
|
531
|
+
for (let block of blocks) {
|
|
532
|
+
if (!isTextBlock(context, block) && block._key === startBlockKey && block._key === endBlockKey) {
|
|
533
|
+
startBlock = block;
|
|
534
|
+
break;
|
|
535
|
+
}
|
|
536
|
+
if (block._key === startBlockKey) {
|
|
537
|
+
if (!isTextBlock(context, block)) {
|
|
538
|
+
startBlock = block;
|
|
539
|
+
continue;
|
|
540
|
+
}
|
|
541
|
+
if (startChildKey) {
|
|
542
|
+
for (let child of block.children) {
|
|
543
|
+
if (child._key === startChildKey) {
|
|
544
|
+
if (isSpan(context, child)) {
|
|
545
|
+
let text = child._key === endChildKey ? child.text.slice(startPoint.offset, endPoint.offset) : child.text.slice(startPoint.offset);
|
|
546
|
+
startBlock = {
|
|
547
|
+
...block,
|
|
548
|
+
children: [{
|
|
549
|
+
...child,
|
|
550
|
+
text
|
|
551
|
+
}]
|
|
552
|
+
};
|
|
553
|
+
} else startBlock = {
|
|
554
|
+
...block,
|
|
555
|
+
children: [child]
|
|
556
|
+
};
|
|
557
|
+
if (block._key === endBlockKey && startChildKey === endChildKey) break;
|
|
558
|
+
continue;
|
|
559
|
+
}
|
|
560
|
+
if (startBlock && isTextBlock(context, startBlock) && (endChildKey && child._key === endChildKey && isSpan(context, child) ? startBlock.children.push({
|
|
561
|
+
...child,
|
|
562
|
+
text: child.text.slice(0, endPoint.offset)
|
|
563
|
+
}) : startBlock.children.push(child), block._key === endBlockKey && endChildKey && child._key === endChildKey)) break;
|
|
564
|
+
}
|
|
565
|
+
if (startBlockKey === endBlockKey) break;
|
|
566
|
+
continue;
|
|
567
|
+
}
|
|
568
|
+
if (startBlock = block, startBlockKey === endBlockKey) break;
|
|
569
|
+
}
|
|
570
|
+
if (block._key === endBlockKey) {
|
|
571
|
+
if (!isTextBlock(context, block)) {
|
|
572
|
+
endBlock = block;
|
|
573
|
+
break;
|
|
574
|
+
}
|
|
575
|
+
if (endChildKey) {
|
|
576
|
+
endBlock = {
|
|
577
|
+
...block,
|
|
578
|
+
children: []
|
|
579
|
+
};
|
|
580
|
+
for (let child of block.children) if (endBlock && isTextBlock(context, endBlock)) {
|
|
581
|
+
if (child._key === endChildKey && isSpan(context, child)) {
|
|
582
|
+
endBlock.children.push({
|
|
583
|
+
...child,
|
|
584
|
+
text: child.text.slice(0, endPoint.offset)
|
|
585
|
+
});
|
|
586
|
+
break;
|
|
587
|
+
}
|
|
588
|
+
if (endBlock.children.push(child), endChildKey && child._key === endChildKey) break;
|
|
589
|
+
}
|
|
590
|
+
break;
|
|
591
|
+
}
|
|
592
|
+
endBlock = block;
|
|
593
|
+
break;
|
|
594
|
+
}
|
|
595
|
+
startBlock && middleBlocks.push(parseBlock({
|
|
596
|
+
keyGenerator: context.keyGenerator ?? defaultKeyGenerator,
|
|
597
|
+
block,
|
|
598
|
+
options: {
|
|
599
|
+
normalize: !1,
|
|
600
|
+
removeUnusedMarkDefs: !0,
|
|
601
|
+
validateFields: !1
|
|
602
|
+
},
|
|
603
|
+
schema: context.schema
|
|
604
|
+
}) ?? block);
|
|
605
|
+
}
|
|
606
|
+
let parsedStartBlock = startBlock ? parseBlock({
|
|
607
|
+
keyGenerator: context.keyGenerator ?? defaultKeyGenerator,
|
|
608
|
+
block: startBlock,
|
|
609
|
+
options: {
|
|
610
|
+
normalize: !1,
|
|
611
|
+
removeUnusedMarkDefs: !0,
|
|
612
|
+
validateFields: !1
|
|
613
|
+
},
|
|
614
|
+
schema: context.schema
|
|
615
|
+
}) : void 0, parsedEndBlock = endBlock ? parseBlock({
|
|
616
|
+
keyGenerator: context.keyGenerator ?? defaultKeyGenerator,
|
|
617
|
+
block: endBlock,
|
|
618
|
+
options: {
|
|
619
|
+
normalize: !1,
|
|
620
|
+
removeUnusedMarkDefs: !0,
|
|
621
|
+
validateFields: !1
|
|
622
|
+
},
|
|
623
|
+
schema: context.schema
|
|
624
|
+
}) : void 0;
|
|
625
|
+
return [
|
|
626
|
+
...parsedStartBlock ? [parsedStartBlock] : [],
|
|
627
|
+
...middleBlocks,
|
|
628
|
+
...parsedEndBlock ? [parsedEndBlock] : []
|
|
629
|
+
];
|
|
630
|
+
}
|
|
631
|
+
export { isEqualPaths as _, parseBlocks as a, childAtTextOffset as b, parseSpan as c, getSelectionEndPoint as d, isSelectionCollapsed as f, isEqualSelectionPoints as g, getBlockKeyFromSelectionPoint as h, parseBlock as i, defaultKeyGenerator as l, getBlockEndPoint as m, isListBlock as n, parseInlineObject as o, getBlockStartPoint as p, parseAnnotation as r, parseMarkDefs as s, sliceBlocks as t, getSelectionStartPoint as u, blockOffsetToSpanSelectionPoint as v, textOffsetOfChild as x, spanSelectionPointToBlockOffset as y };
|
|
632
|
+
|
|
633
|
+
//# sourceMappingURL=util.slice-blocks-DQnr9X2s.js.map
|