@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
|
@@ -1,720 +0,0 @@
|
|
|
1
|
-
import { isTextBlock, isSpan, getSubSchema } from "@portabletext/schema";
|
|
2
|
-
import { getNode, isKeyedSegment, getParent, isTypedObject, isRecord } from "./get-parent.js";
|
|
3
|
-
function textOffsetOfChild(children, getSpanText, childKey, offsetInChild) {
|
|
4
|
-
let precedingTextLength = 0;
|
|
5
|
-
for (const child of children) {
|
|
6
|
-
const text = getSpanText(child);
|
|
7
|
-
if (keyOf(child) === childKey)
|
|
8
|
-
return text === void 0 ? void 0 : precedingTextLength + offsetInChild;
|
|
9
|
-
text !== void 0 && (precedingTextLength += text.length);
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
function childAtTextOffset(children, getSpanText, textOffset) {
|
|
13
|
-
let remainingOffset = textOffset;
|
|
14
|
-
for (const child of children) {
|
|
15
|
-
const text = getSpanText(child);
|
|
16
|
-
if (text !== void 0) {
|
|
17
|
-
if (remainingOffset <= text.length) {
|
|
18
|
-
const key = keyOf(child);
|
|
19
|
-
return key === void 0 ? void 0 : {
|
|
20
|
-
key,
|
|
21
|
-
offset: remainingOffset
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
remainingOffset -= text.length;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
function keyOf(child) {
|
|
29
|
-
if (child !== null && typeof child == "object" && "_key" in child) {
|
|
30
|
-
const key = child._key;
|
|
31
|
-
return typeof key == "string" ? key : void 0;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
function blockOffsetToSpanSelectionPoint({
|
|
35
|
-
snapshot,
|
|
36
|
-
blockOffset,
|
|
37
|
-
direction
|
|
38
|
-
}) {
|
|
39
|
-
const blockEntry = getNode(snapshot, blockOffset.path);
|
|
40
|
-
if (!blockEntry || !isTextBlock(snapshot.context, blockEntry.node))
|
|
41
|
-
return;
|
|
42
|
-
const block = blockEntry.node, blockPath = blockEntry.path;
|
|
43
|
-
if (direction === "forward") {
|
|
44
|
-
const placed = childAtTextOffset(block.children, (child) => isSpan(snapshot.context, child) ? child.text : void 0, blockOffset.offset);
|
|
45
|
-
return placed ? {
|
|
46
|
-
path: [...blockPath, "children", {
|
|
47
|
-
_key: placed.key
|
|
48
|
-
}],
|
|
49
|
-
offset: placed.offset
|
|
50
|
-
} : void 0;
|
|
51
|
-
}
|
|
52
|
-
let offsetLeft = blockOffset.offset, selectionPoint, skippedInlineObject = !1;
|
|
53
|
-
for (const child of block.children) {
|
|
54
|
-
if (!isSpan(snapshot.context, child)) {
|
|
55
|
-
skippedInlineObject = !0;
|
|
56
|
-
continue;
|
|
57
|
-
}
|
|
58
|
-
if (offsetLeft === 0 && selectionPoint && !skippedInlineObject)
|
|
59
|
-
break;
|
|
60
|
-
if (offsetLeft > child.text.length) {
|
|
61
|
-
offsetLeft -= child.text.length;
|
|
62
|
-
continue;
|
|
63
|
-
}
|
|
64
|
-
if (offsetLeft <= child.text.length && (selectionPoint = {
|
|
65
|
-
path: [...blockPath, "children", {
|
|
66
|
-
_key: child._key
|
|
67
|
-
}],
|
|
68
|
-
offset: offsetLeft
|
|
69
|
-
}, offsetLeft -= child.text.length, offsetLeft !== 0))
|
|
70
|
-
break;
|
|
71
|
-
}
|
|
72
|
-
return selectionPoint;
|
|
73
|
-
}
|
|
74
|
-
function spanSelectionPointToBlockOffset({
|
|
75
|
-
snapshot,
|
|
76
|
-
selectionPoint
|
|
77
|
-
}) {
|
|
78
|
-
const spanSegment = selectionPoint.path.at(-1);
|
|
79
|
-
if (!isKeyedSegment(spanSegment))
|
|
80
|
-
return;
|
|
81
|
-
const textBlock = getParent(snapshot, selectionPoint.path, {
|
|
82
|
-
match: (node) => isTextBlock({
|
|
83
|
-
schema: snapshot.context.schema
|
|
84
|
-
}, node)
|
|
85
|
-
});
|
|
86
|
-
if (!textBlock)
|
|
87
|
-
return;
|
|
88
|
-
const offset = textOffsetOfChild(textBlock.node.children, (child) => isSpan(snapshot.context, child) ? child.text : void 0, spanSegment._key, selectionPoint.offset);
|
|
89
|
-
return offset === void 0 ? void 0 : {
|
|
90
|
-
path: textBlock.path,
|
|
91
|
-
offset
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
function isEqualPathSegments(segA, segB) {
|
|
95
|
-
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;
|
|
96
|
-
}
|
|
97
|
-
function isEqualPaths(a, b) {
|
|
98
|
-
if (a.length !== b.length)
|
|
99
|
-
return !1;
|
|
100
|
-
for (let i = 0; i < a.length; i++)
|
|
101
|
-
if (!isEqualPathSegments(a[i], b[i]))
|
|
102
|
-
return !1;
|
|
103
|
-
return !0;
|
|
104
|
-
}
|
|
105
|
-
function isEqualSelectionPoints(a, b) {
|
|
106
|
-
return a.offset === b.offset && isEqualPaths(a.path, b.path);
|
|
107
|
-
}
|
|
108
|
-
function getBlockKeyFromSelectionPoint(point) {
|
|
109
|
-
const blockPathSegment = point.path.at(0);
|
|
110
|
-
if (isKeyedSegment(blockPathSegment))
|
|
111
|
-
return blockPathSegment._key;
|
|
112
|
-
}
|
|
113
|
-
function getChildKeyFromSelectionPoint(point) {
|
|
114
|
-
const childPathSegment = point.path.at(2);
|
|
115
|
-
if (isKeyedSegment(childPathSegment))
|
|
116
|
-
return childPathSegment._key;
|
|
117
|
-
}
|
|
118
|
-
function getBlockEndPoint({
|
|
119
|
-
context,
|
|
120
|
-
block
|
|
121
|
-
}) {
|
|
122
|
-
if (isTextBlock(context, block.node)) {
|
|
123
|
-
const lastChild = block.node.children[block.node.children.length - 1];
|
|
124
|
-
if (lastChild)
|
|
125
|
-
return {
|
|
126
|
-
path: [...block.path, "children", {
|
|
127
|
-
_key: lastChild._key
|
|
128
|
-
}],
|
|
129
|
-
offset: isSpan(context, lastChild) ? lastChild.text.length : 0
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
return {
|
|
133
|
-
path: block.path,
|
|
134
|
-
offset: 0
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
function getBlockStartPoint({
|
|
138
|
-
context,
|
|
139
|
-
block
|
|
140
|
-
}) {
|
|
141
|
-
if (isTextBlock(context, block.node)) {
|
|
142
|
-
const firstChild = block.node.children[0];
|
|
143
|
-
return {
|
|
144
|
-
path: [...block.path, "children", {
|
|
145
|
-
_key: firstChild?._key ?? ""
|
|
146
|
-
}],
|
|
147
|
-
offset: 0
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
return {
|
|
151
|
-
path: block.path,
|
|
152
|
-
offset: 0
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
function isSelectionCollapsed(selection) {
|
|
156
|
-
return selection ? isEqualPaths(selection.anchor.path, selection.focus.path) && selection.anchor.offset === selection.focus.offset : !1;
|
|
157
|
-
}
|
|
158
|
-
function getSelectionEndPoint(selection) {
|
|
159
|
-
return selection ? selection.backward ? selection.anchor : selection.focus : null;
|
|
160
|
-
}
|
|
161
|
-
function getSelectionStartPoint(selection) {
|
|
162
|
-
return selection ? selection.backward ? selection.focus : selection.anchor : null;
|
|
163
|
-
}
|
|
164
|
-
const defaultKeyGenerator = () => randomKey(12), getByteHexTable = /* @__PURE__ */ (() => {
|
|
165
|
-
let table;
|
|
166
|
-
return () => {
|
|
167
|
-
if (table)
|
|
168
|
-
return table;
|
|
169
|
-
table = [];
|
|
170
|
-
for (let i = 0; i < 256; ++i)
|
|
171
|
-
table[i] = (i + 256).toString(16).slice(1);
|
|
172
|
-
return table;
|
|
173
|
-
};
|
|
174
|
-
})();
|
|
175
|
-
function whatwgRNG(length = 16) {
|
|
176
|
-
const rnds8 = new Uint8Array(length);
|
|
177
|
-
return crypto.getRandomValues(rnds8), rnds8;
|
|
178
|
-
}
|
|
179
|
-
function randomKey(length) {
|
|
180
|
-
const table = getByteHexTable();
|
|
181
|
-
return whatwgRNG(length).reduce((str, n) => str + table[n], "").slice(0, length);
|
|
182
|
-
}
|
|
183
|
-
function parseBlocks({
|
|
184
|
-
schema,
|
|
185
|
-
keyGenerator,
|
|
186
|
-
blocks,
|
|
187
|
-
options
|
|
188
|
-
}) {
|
|
189
|
-
return Array.isArray(blocks) ? blocks.flatMap((block) => {
|
|
190
|
-
const parsedBlock = parseBlockInternal({
|
|
191
|
-
schema,
|
|
192
|
-
keyGenerator,
|
|
193
|
-
block,
|
|
194
|
-
options
|
|
195
|
-
});
|
|
196
|
-
return parsedBlock ? [parsedBlock] : [];
|
|
197
|
-
}) : [];
|
|
198
|
-
}
|
|
199
|
-
function parseBlock({
|
|
200
|
-
schema,
|
|
201
|
-
keyGenerator,
|
|
202
|
-
block,
|
|
203
|
-
options
|
|
204
|
-
}) {
|
|
205
|
-
return parseBlockInternal({
|
|
206
|
-
schema,
|
|
207
|
-
keyGenerator,
|
|
208
|
-
block,
|
|
209
|
-
options
|
|
210
|
-
});
|
|
211
|
-
}
|
|
212
|
-
function parseSpan({
|
|
213
|
-
span,
|
|
214
|
-
schema,
|
|
215
|
-
keyGenerator,
|
|
216
|
-
markDefKeyMap,
|
|
217
|
-
options
|
|
218
|
-
}) {
|
|
219
|
-
return parseSpanInternal({
|
|
220
|
-
span,
|
|
221
|
-
schema,
|
|
222
|
-
keyGenerator,
|
|
223
|
-
markDefKeyMap,
|
|
224
|
-
options
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
function parseInlineObject({
|
|
228
|
-
inlineObject,
|
|
229
|
-
schema,
|
|
230
|
-
keyGenerator,
|
|
231
|
-
options
|
|
232
|
-
}) {
|
|
233
|
-
return parseInlineObjectInternal({
|
|
234
|
-
inlineObject,
|
|
235
|
-
schema,
|
|
236
|
-
keyGenerator,
|
|
237
|
-
options
|
|
238
|
-
});
|
|
239
|
-
}
|
|
240
|
-
function parseMarkDefs({
|
|
241
|
-
schema,
|
|
242
|
-
keyGenerator,
|
|
243
|
-
markDefs,
|
|
244
|
-
options
|
|
245
|
-
}) {
|
|
246
|
-
return parseMarkDefsInternal({
|
|
247
|
-
schema,
|
|
248
|
-
keyGenerator,
|
|
249
|
-
markDefs,
|
|
250
|
-
options
|
|
251
|
-
});
|
|
252
|
-
}
|
|
253
|
-
function parseBlockInternal({
|
|
254
|
-
schema,
|
|
255
|
-
keyGenerator,
|
|
256
|
-
block,
|
|
257
|
-
options
|
|
258
|
-
}) {
|
|
259
|
-
return parseTextBlock({
|
|
260
|
-
block,
|
|
261
|
-
schema,
|
|
262
|
-
keyGenerator,
|
|
263
|
-
options
|
|
264
|
-
}) ?? parseBlockObject({
|
|
265
|
-
blockObject: block,
|
|
266
|
-
schema,
|
|
267
|
-
keyGenerator,
|
|
268
|
-
options
|
|
269
|
-
});
|
|
270
|
-
}
|
|
271
|
-
function parseBlockObject({
|
|
272
|
-
blockObject,
|
|
273
|
-
schema,
|
|
274
|
-
keyGenerator,
|
|
275
|
-
options
|
|
276
|
-
}) {
|
|
277
|
-
if (!isTypedObject(blockObject))
|
|
278
|
-
return;
|
|
279
|
-
const schemaType = schema.blockObjects.find(({
|
|
280
|
-
name
|
|
281
|
-
}) => name === blockObject._type);
|
|
282
|
-
if (schemaType)
|
|
283
|
-
return parseObject({
|
|
284
|
-
object: blockObject,
|
|
285
|
-
schema,
|
|
286
|
-
keyGenerator,
|
|
287
|
-
schemaType,
|
|
288
|
-
options
|
|
289
|
-
});
|
|
290
|
-
}
|
|
291
|
-
function isListBlock(context, block) {
|
|
292
|
-
return isTextBlock(context, block) && block.level !== void 0 && block.listItem !== void 0;
|
|
293
|
-
}
|
|
294
|
-
function parseTextBlock({
|
|
295
|
-
block,
|
|
296
|
-
schema,
|
|
297
|
-
keyGenerator,
|
|
298
|
-
options
|
|
299
|
-
}) {
|
|
300
|
-
if (!isTypedObject(block) || block._type !== schema.block.name)
|
|
301
|
-
return;
|
|
302
|
-
const customFields = {};
|
|
303
|
-
for (const key of Object.keys(block))
|
|
304
|
-
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]);
|
|
305
|
-
const _key = typeof block._key == "string" ? block._key : keyGenerator(), {
|
|
306
|
-
markDefs,
|
|
307
|
-
markDefKeyMap
|
|
308
|
-
} = parseMarkDefsInternal({
|
|
309
|
-
schema,
|
|
310
|
-
keyGenerator,
|
|
311
|
-
markDefs: block.markDefs,
|
|
312
|
-
options
|
|
313
|
-
}), parsedChildren = (Array.isArray(block.children) ? block.children : []).map((child) => parseChildInternal({
|
|
314
|
-
child,
|
|
315
|
-
schema,
|
|
316
|
-
keyGenerator,
|
|
317
|
-
markDefKeyMap,
|
|
318
|
-
options
|
|
319
|
-
})).filter((child) => child !== void 0), marks = parsedChildren.flatMap((child) => child.marks ?? []), children = parsedChildren.length > 0 ? parsedChildren : [{
|
|
320
|
-
_key: keyGenerator(),
|
|
321
|
-
_type: schema.span.name,
|
|
322
|
-
text: "",
|
|
323
|
-
marks: []
|
|
324
|
-
}], normalizedChildren = options.normalize ? (
|
|
325
|
-
// Ensure that inline objects re surrounded by spans
|
|
326
|
-
children.reduce((normalizedChildren2, child, index) => {
|
|
327
|
-
if (isSpan({
|
|
328
|
-
schema
|
|
329
|
-
}, child))
|
|
330
|
-
return [...normalizedChildren2, child];
|
|
331
|
-
const previousChild = normalizedChildren2.at(-1);
|
|
332
|
-
return !previousChild || !isSpan({
|
|
333
|
-
schema
|
|
334
|
-
}, previousChild) ? [...normalizedChildren2, {
|
|
335
|
-
_key: keyGenerator(),
|
|
336
|
-
_type: schema.span.name,
|
|
337
|
-
text: "",
|
|
338
|
-
marks: []
|
|
339
|
-
}, child, ...index === children.length - 1 ? [{
|
|
340
|
-
_key: keyGenerator(),
|
|
341
|
-
_type: schema.span.name,
|
|
342
|
-
text: "",
|
|
343
|
-
marks: []
|
|
344
|
-
}] : []] : [...normalizedChildren2, child];
|
|
345
|
-
}, [])
|
|
346
|
-
) : children, parsedBlock = {
|
|
347
|
-
_type: schema.block.name,
|
|
348
|
-
_key,
|
|
349
|
-
children: normalizedChildren,
|
|
350
|
-
...customFields
|
|
351
|
-
};
|
|
352
|
-
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;
|
|
353
|
-
}
|
|
354
|
-
function parseMarkDefsInternal({
|
|
355
|
-
schema,
|
|
356
|
-
keyGenerator,
|
|
357
|
-
markDefs,
|
|
358
|
-
options
|
|
359
|
-
}) {
|
|
360
|
-
const unparsedMarkDefs = Array.isArray(markDefs) ? markDefs : [], markDefKeyMap = /* @__PURE__ */ new Map();
|
|
361
|
-
return {
|
|
362
|
-
markDefs: unparsedMarkDefs.flatMap((markDef) => {
|
|
363
|
-
if (!isTypedObject(markDef))
|
|
364
|
-
return [];
|
|
365
|
-
const schemaType = schema.annotations.find(({
|
|
366
|
-
name
|
|
367
|
-
}) => name === markDef._type);
|
|
368
|
-
if (!schemaType)
|
|
369
|
-
return [];
|
|
370
|
-
if (typeof markDef._key != "string")
|
|
371
|
-
return [];
|
|
372
|
-
const parsedAnnotation = parseObject({
|
|
373
|
-
object: markDef,
|
|
374
|
-
schema,
|
|
375
|
-
keyGenerator,
|
|
376
|
-
schemaType,
|
|
377
|
-
options
|
|
378
|
-
});
|
|
379
|
-
return parsedAnnotation ? (markDefKeyMap.set(markDef._key, parsedAnnotation._key), [parsedAnnotation]) : [];
|
|
380
|
-
}),
|
|
381
|
-
markDefKeyMap
|
|
382
|
-
};
|
|
383
|
-
}
|
|
384
|
-
function parseChildInternal({
|
|
385
|
-
child,
|
|
386
|
-
schema,
|
|
387
|
-
keyGenerator,
|
|
388
|
-
markDefKeyMap,
|
|
389
|
-
options
|
|
390
|
-
}) {
|
|
391
|
-
return parseSpanInternal({
|
|
392
|
-
span: child,
|
|
393
|
-
schema,
|
|
394
|
-
keyGenerator,
|
|
395
|
-
markDefKeyMap,
|
|
396
|
-
options
|
|
397
|
-
}) ?? parseInlineObjectInternal({
|
|
398
|
-
inlineObject: child,
|
|
399
|
-
schema,
|
|
400
|
-
keyGenerator,
|
|
401
|
-
options
|
|
402
|
-
});
|
|
403
|
-
}
|
|
404
|
-
function parseSpanInternal({
|
|
405
|
-
span,
|
|
406
|
-
schema,
|
|
407
|
-
keyGenerator,
|
|
408
|
-
markDefKeyMap,
|
|
409
|
-
options
|
|
410
|
-
}) {
|
|
411
|
-
if (!isRecord(span))
|
|
412
|
-
return;
|
|
413
|
-
const customFields = {};
|
|
414
|
-
for (const key of Object.keys(span))
|
|
415
|
-
key !== "_type" && key !== "_key" && key !== "text" && key !== "marks" && (customFields[key] = span[key]);
|
|
416
|
-
const marks = (Array.isArray(span.marks) ? span.marks : []).flatMap((mark) => {
|
|
417
|
-
if (typeof mark != "string")
|
|
418
|
-
return [];
|
|
419
|
-
const markDefKey = markDefKeyMap.get(mark);
|
|
420
|
-
return markDefKey !== void 0 ? [markDefKey] : schema.decorators.some((decorator) => decorator.name === mark) ? [mark] : [];
|
|
421
|
-
});
|
|
422
|
-
if (!(typeof span._type == "string" && span._type !== schema.span.name))
|
|
423
|
-
return typeof span._type != "string" ? typeof span.text == "string" ? {
|
|
424
|
-
_type: schema.span.name,
|
|
425
|
-
_key: typeof span._key == "string" ? span._key : keyGenerator(),
|
|
426
|
-
text: span.text,
|
|
427
|
-
marks,
|
|
428
|
-
...options.validateFields ? {} : customFields
|
|
429
|
-
} : void 0 : {
|
|
430
|
-
_type: schema.span.name,
|
|
431
|
-
_key: typeof span._key == "string" ? span._key : keyGenerator(),
|
|
432
|
-
text: typeof span.text == "string" ? span.text : "",
|
|
433
|
-
marks,
|
|
434
|
-
...options.validateFields ? {} : customFields
|
|
435
|
-
};
|
|
436
|
-
}
|
|
437
|
-
function parseInlineObjectInternal({
|
|
438
|
-
inlineObject,
|
|
439
|
-
schema,
|
|
440
|
-
keyGenerator,
|
|
441
|
-
options
|
|
442
|
-
}) {
|
|
443
|
-
if (!isTypedObject(inlineObject))
|
|
444
|
-
return;
|
|
445
|
-
const schemaType = schema.inlineObjects.find(({
|
|
446
|
-
name
|
|
447
|
-
}) => name === inlineObject._type);
|
|
448
|
-
if (schemaType)
|
|
449
|
-
return parseObject({
|
|
450
|
-
object: inlineObject,
|
|
451
|
-
schema,
|
|
452
|
-
keyGenerator,
|
|
453
|
-
schemaType,
|
|
454
|
-
options
|
|
455
|
-
});
|
|
456
|
-
}
|
|
457
|
-
function parseAnnotation({
|
|
458
|
-
annotation,
|
|
459
|
-
schema,
|
|
460
|
-
keyGenerator,
|
|
461
|
-
options
|
|
462
|
-
}) {
|
|
463
|
-
if (!isTypedObject(annotation))
|
|
464
|
-
return;
|
|
465
|
-
const schemaType = schema.annotations.find(({
|
|
466
|
-
name
|
|
467
|
-
}) => name === annotation._type);
|
|
468
|
-
if (schemaType)
|
|
469
|
-
return parseObject({
|
|
470
|
-
object: annotation,
|
|
471
|
-
schema,
|
|
472
|
-
keyGenerator,
|
|
473
|
-
schemaType,
|
|
474
|
-
options
|
|
475
|
-
});
|
|
476
|
-
}
|
|
477
|
-
function resolveOfMember(of, typeName, schema, ancestorFields) {
|
|
478
|
-
for (const member of of)
|
|
479
|
-
if (member.type !== "block") {
|
|
480
|
-
if (member.type === "object" && "name" in member && member.name) {
|
|
481
|
-
if (member.name === typeName && "fields" in member && member.fields)
|
|
482
|
-
return {
|
|
483
|
-
name: member.name,
|
|
484
|
-
fields: member.fields
|
|
485
|
-
};
|
|
486
|
-
continue;
|
|
487
|
-
}
|
|
488
|
-
if (member.type === typeName) {
|
|
489
|
-
const ancestorMatch = ancestorFields.get(typeName);
|
|
490
|
-
if (ancestorMatch)
|
|
491
|
-
return {
|
|
492
|
-
name: typeName,
|
|
493
|
-
fields: ancestorMatch
|
|
494
|
-
};
|
|
495
|
-
const rootMatch = schema.blockObjects.find((blockObject) => blockObject.name === typeName);
|
|
496
|
-
return rootMatch && "fields" in rootMatch && rootMatch.fields ? {
|
|
497
|
-
name: rootMatch.name,
|
|
498
|
-
fields: rootMatch.fields
|
|
499
|
-
} : void 0;
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
function parseObject({
|
|
504
|
-
object,
|
|
505
|
-
schema,
|
|
506
|
-
keyGenerator,
|
|
507
|
-
schemaType,
|
|
508
|
-
ancestorFields,
|
|
509
|
-
options
|
|
510
|
-
}) {
|
|
511
|
-
const {
|
|
512
|
-
_key,
|
|
513
|
-
...customFields
|
|
514
|
-
} = object, fieldsByName = new Map(schemaType.fields.map((field) => [field.name, field])), nextAncestors = new Map(ancestorFields ?? []);
|
|
515
|
-
nextAncestors.set(schemaType.name, schemaType.fields);
|
|
516
|
-
const values = {};
|
|
517
|
-
for (const [key, value] of Object.entries(customFields)) {
|
|
518
|
-
if (key === "_type" || value === void 0)
|
|
519
|
-
continue;
|
|
520
|
-
const field = fieldsByName.get(key);
|
|
521
|
-
if (!(options.validateFields && !field)) {
|
|
522
|
-
if (field && field.type === "array" && field.of && Array.isArray(value)) {
|
|
523
|
-
values[key] = parseContainerFieldValue({
|
|
524
|
-
schema,
|
|
525
|
-
keyGenerator,
|
|
526
|
-
of: field.of,
|
|
527
|
-
value,
|
|
528
|
-
ancestorFields: nextAncestors,
|
|
529
|
-
options
|
|
530
|
-
});
|
|
531
|
-
continue;
|
|
532
|
-
}
|
|
533
|
-
values[key] = value;
|
|
534
|
-
}
|
|
535
|
-
}
|
|
536
|
-
return {
|
|
537
|
-
_type: schemaType.name,
|
|
538
|
-
_key: typeof _key == "string" ? _key : keyGenerator(),
|
|
539
|
-
...values
|
|
540
|
-
};
|
|
541
|
-
}
|
|
542
|
-
function parseContainerFieldValue({
|
|
543
|
-
schema,
|
|
544
|
-
keyGenerator,
|
|
545
|
-
of,
|
|
546
|
-
value,
|
|
547
|
-
ancestorFields,
|
|
548
|
-
options
|
|
549
|
-
}) {
|
|
550
|
-
const childBlockSubSchema = of.some((member) => member.type === "block") ? getSubSchema(schema, of) : void 0;
|
|
551
|
-
return value.flatMap((item) => {
|
|
552
|
-
if (!isTypedObject(item))
|
|
553
|
-
return [item];
|
|
554
|
-
if (childBlockSubSchema && item._type === childBlockSubSchema.block.name) {
|
|
555
|
-
const parsed = parseTextBlock({
|
|
556
|
-
block: item,
|
|
557
|
-
schema: childBlockSubSchema,
|
|
558
|
-
keyGenerator,
|
|
559
|
-
options: {
|
|
560
|
-
normalize: !1,
|
|
561
|
-
removeUnusedMarkDefs: !1,
|
|
562
|
-
validateFields: options.validateFields
|
|
563
|
-
}
|
|
564
|
-
});
|
|
565
|
-
return parsed ? [parsed] : [];
|
|
566
|
-
}
|
|
567
|
-
const schemaType = resolveOfMember(of, item._type, schema, ancestorFields);
|
|
568
|
-
return schemaType ? [parseObject({
|
|
569
|
-
object: item,
|
|
570
|
-
schema,
|
|
571
|
-
keyGenerator,
|
|
572
|
-
schemaType: {
|
|
573
|
-
name: schemaType.name,
|
|
574
|
-
fields: schemaType.fields
|
|
575
|
-
},
|
|
576
|
-
ancestorFields,
|
|
577
|
-
options
|
|
578
|
-
})] : [item];
|
|
579
|
-
});
|
|
580
|
-
}
|
|
581
|
-
function sliceBlocks({
|
|
582
|
-
context,
|
|
583
|
-
blocks
|
|
584
|
-
}) {
|
|
585
|
-
const slice = [];
|
|
586
|
-
if (!context.selection)
|
|
587
|
-
return slice;
|
|
588
|
-
let startBlock;
|
|
589
|
-
const middleBlocks = [];
|
|
590
|
-
let endBlock;
|
|
591
|
-
const startPoint = getSelectionStartPoint(context.selection), endPoint = getSelectionEndPoint(context.selection), startBlockKey = getBlockKeyFromSelectionPoint(startPoint), startChildKey = getChildKeyFromSelectionPoint(startPoint), endBlockKey = getBlockKeyFromSelectionPoint(endPoint), endChildKey = getChildKeyFromSelectionPoint(endPoint);
|
|
592
|
-
if (!startBlockKey || !endBlockKey)
|
|
593
|
-
return slice;
|
|
594
|
-
for (const block of blocks) {
|
|
595
|
-
if (!isTextBlock(context, block) && block._key === startBlockKey && block._key === endBlockKey) {
|
|
596
|
-
startBlock = block;
|
|
597
|
-
break;
|
|
598
|
-
}
|
|
599
|
-
if (block._key === startBlockKey) {
|
|
600
|
-
if (!isTextBlock(context, block)) {
|
|
601
|
-
startBlock = block;
|
|
602
|
-
continue;
|
|
603
|
-
}
|
|
604
|
-
if (startChildKey) {
|
|
605
|
-
for (const child of block.children) {
|
|
606
|
-
if (child._key === startChildKey) {
|
|
607
|
-
if (isSpan(context, child)) {
|
|
608
|
-
const text = child._key === endChildKey ? child.text.slice(startPoint.offset, endPoint.offset) : child.text.slice(startPoint.offset);
|
|
609
|
-
startBlock = {
|
|
610
|
-
...block,
|
|
611
|
-
children: [{
|
|
612
|
-
...child,
|
|
613
|
-
text
|
|
614
|
-
}]
|
|
615
|
-
};
|
|
616
|
-
} else
|
|
617
|
-
startBlock = {
|
|
618
|
-
...block,
|
|
619
|
-
children: [child]
|
|
620
|
-
};
|
|
621
|
-
if (block._key === endBlockKey && startChildKey === endChildKey)
|
|
622
|
-
break;
|
|
623
|
-
continue;
|
|
624
|
-
}
|
|
625
|
-
if (startBlock && isTextBlock(context, startBlock) && (endChildKey && child._key === endChildKey && isSpan(context, child) ? startBlock.children.push({
|
|
626
|
-
...child,
|
|
627
|
-
text: child.text.slice(0, endPoint.offset)
|
|
628
|
-
}) : startBlock.children.push(child), block._key === endBlockKey && endChildKey && child._key === endChildKey))
|
|
629
|
-
break;
|
|
630
|
-
}
|
|
631
|
-
if (startBlockKey === endBlockKey)
|
|
632
|
-
break;
|
|
633
|
-
continue;
|
|
634
|
-
}
|
|
635
|
-
if (startBlock = block, startBlockKey === endBlockKey)
|
|
636
|
-
break;
|
|
637
|
-
}
|
|
638
|
-
if (block._key === endBlockKey) {
|
|
639
|
-
if (!isTextBlock(context, block)) {
|
|
640
|
-
endBlock = block;
|
|
641
|
-
break;
|
|
642
|
-
}
|
|
643
|
-
if (endChildKey) {
|
|
644
|
-
endBlock = {
|
|
645
|
-
...block,
|
|
646
|
-
children: []
|
|
647
|
-
};
|
|
648
|
-
for (const child of block.children)
|
|
649
|
-
if (endBlock && isTextBlock(context, endBlock)) {
|
|
650
|
-
if (child._key === endChildKey && isSpan(context, child)) {
|
|
651
|
-
endBlock.children.push({
|
|
652
|
-
...child,
|
|
653
|
-
text: child.text.slice(0, endPoint.offset)
|
|
654
|
-
});
|
|
655
|
-
break;
|
|
656
|
-
}
|
|
657
|
-
if (endBlock.children.push(child), endChildKey && child._key === endChildKey)
|
|
658
|
-
break;
|
|
659
|
-
}
|
|
660
|
-
break;
|
|
661
|
-
}
|
|
662
|
-
endBlock = block;
|
|
663
|
-
break;
|
|
664
|
-
}
|
|
665
|
-
startBlock && middleBlocks.push(parseBlock({
|
|
666
|
-
keyGenerator: context.keyGenerator ?? defaultKeyGenerator,
|
|
667
|
-
block,
|
|
668
|
-
options: {
|
|
669
|
-
normalize: !1,
|
|
670
|
-
removeUnusedMarkDefs: !0,
|
|
671
|
-
validateFields: !1
|
|
672
|
-
},
|
|
673
|
-
schema: context.schema
|
|
674
|
-
}) ?? block);
|
|
675
|
-
}
|
|
676
|
-
const parsedStartBlock = startBlock ? parseBlock({
|
|
677
|
-
keyGenerator: context.keyGenerator ?? defaultKeyGenerator,
|
|
678
|
-
block: startBlock,
|
|
679
|
-
options: {
|
|
680
|
-
normalize: !1,
|
|
681
|
-
removeUnusedMarkDefs: !0,
|
|
682
|
-
validateFields: !1
|
|
683
|
-
},
|
|
684
|
-
schema: context.schema
|
|
685
|
-
}) : void 0, parsedEndBlock = endBlock ? parseBlock({
|
|
686
|
-
keyGenerator: context.keyGenerator ?? defaultKeyGenerator,
|
|
687
|
-
block: endBlock,
|
|
688
|
-
options: {
|
|
689
|
-
normalize: !1,
|
|
690
|
-
removeUnusedMarkDefs: !0,
|
|
691
|
-
validateFields: !1
|
|
692
|
-
},
|
|
693
|
-
schema: context.schema
|
|
694
|
-
}) : void 0;
|
|
695
|
-
return [...parsedStartBlock ? [parsedStartBlock] : [], ...middleBlocks, ...parsedEndBlock ? [parsedEndBlock] : []];
|
|
696
|
-
}
|
|
697
|
-
export {
|
|
698
|
-
blockOffsetToSpanSelectionPoint,
|
|
699
|
-
childAtTextOffset,
|
|
700
|
-
defaultKeyGenerator,
|
|
701
|
-
getBlockEndPoint,
|
|
702
|
-
getBlockKeyFromSelectionPoint,
|
|
703
|
-
getBlockStartPoint,
|
|
704
|
-
getSelectionEndPoint,
|
|
705
|
-
getSelectionStartPoint,
|
|
706
|
-
isEqualPaths,
|
|
707
|
-
isEqualSelectionPoints,
|
|
708
|
-
isListBlock,
|
|
709
|
-
isSelectionCollapsed,
|
|
710
|
-
parseAnnotation,
|
|
711
|
-
parseBlock,
|
|
712
|
-
parseBlocks,
|
|
713
|
-
parseInlineObject,
|
|
714
|
-
parseMarkDefs,
|
|
715
|
-
parseSpan,
|
|
716
|
-
sliceBlocks,
|
|
717
|
-
spanSelectionPointToBlockOffset,
|
|
718
|
-
textOffsetOfChild
|
|
719
|
-
};
|
|
720
|
-
//# sourceMappingURL=util.slice-blocks.js.map
|