@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,947 +0,0 @@
1
- import { rangeEdges, comparePoints$1 as comparePoints, isInline, isSpanNode, isObject, getEnclosingBlock, resolveContainerAt, hasNode, getBlock, getNodes, getSibling, getPathSubSchema, isTextBlockNode, isEditableContainer } from "./get-path-sub-schema.js";
2
- import { getSelectionStartPoint as getSelectionStartPoint$1, getSelectionEndPoint as getSelectionEndPoint$1, sliceBlocks, isSelectionCollapsed as isSelectionCollapsed$1, getBlockStartPoint, getBlockEndPoint, isEqualSelectionPoints, blockOffsetToSpanSelectionPoint, spanSelectionPointToBlockOffset, isListBlock } from "./util.slice-blocks.js";
3
- import { getNode, getNodeChildren, isKeyedSegment, getParent, getChildren, parentPath } from "./get-parent.js";
4
- import { isSpan, isTextBlock } from "@portabletext/schema";
5
- function rangesOverlap(rangeA, rangeB, root) {
6
- const [startA, endA] = rangeEdges(rangeA, root), [startB, endB] = rangeEdges(rangeB, root);
7
- return comparePoints(startA, endB, root) <= 0 && comparePoints(startB, endA, root) <= 0;
8
- }
9
- function getInline(snapshot, path) {
10
- const entry = getNode(snapshot, path);
11
- if (!(!entry || !isInline(snapshot, entry.path)) && !(!isSpanNode({
12
- schema: snapshot.context.schema
13
- }, entry.node) && !isObject(snapshot, entry.node)))
14
- return {
15
- node: entry.node,
16
- path: entry.path
17
- };
18
- }
19
- const getFocusChild = (snapshot) => {
20
- const selection = snapshot.context.selection;
21
- if (selection)
22
- return getInline(snapshot, selection.focus.path);
23
- }, getFocusInlineObject = (snapshot) => {
24
- const focusChild = getFocusChild(snapshot);
25
- return focusChild && !isSpanNode(snapshot.context, focusChild.node) ? {
26
- node: focusChild.node,
27
- path: focusChild.path
28
- } : void 0;
29
- }, getFocusSpan = (snapshot) => {
30
- const focusChild = getFocusChild(snapshot);
31
- return focusChild && isSpan(snapshot.context, focusChild.node) ? {
32
- node: focusChild.node,
33
- path: focusChild.path
34
- } : void 0;
35
- }, getFocusBlock = (snapshot) => {
36
- const selection = snapshot.context.selection;
37
- if (selection)
38
- return getEnclosingBlock(snapshot, selection.focus.path);
39
- }, getFocusTextBlock = (snapshot) => {
40
- const focusBlock = getFocusBlock(snapshot);
41
- return focusBlock && isTextBlock(snapshot.context, focusBlock.node) ? {
42
- node: focusBlock.node,
43
- path: focusBlock.path
44
- } : void 0;
45
- };
46
- function getRootAcceptedTypes(schema) {
47
- return /* @__PURE__ */ new Set([schema.block.name, ...schema.blockObjects.map((blockObject) => blockObject.name)]);
48
- }
49
- const getSelectedValue = (snapshot) => {
50
- const selection = snapshot.context.selection;
51
- if (!selection)
52
- return [];
53
- const startPoint = getSelectionStartPoint$1(selection), endPoint = getSelectionEndPoint$1(selection);
54
- return !startPoint || !endPoint ? [] : sliceArray({
55
- context: snapshot.context,
56
- blocks: snapshot.context.value,
57
- pathPrefix: [],
58
- fieldNameInPrefix: void 0,
59
- startEdge: startPoint,
60
- endEdge: endPoint
61
- });
62
- };
63
- function sliceArray({
64
- context,
65
- blocks,
66
- pathPrefix,
67
- fieldNameInPrefix,
68
- startEdge,
69
- endEdge,
70
- parent
71
- }) {
72
- if (blocks.length === 0)
73
- return [];
74
- const startIdx = resolveIndex(blocks, pathPrefix, startEdge), endIdx = resolveIndex(blocks, pathPrefix, endEdge);
75
- if (startIdx === -1 || endIdx === -1)
76
- return [];
77
- const [lo, hi, loEdge, hiEdge] = startIdx <= endIdx ? [startIdx, endIdx, startEdge, endEdge] : [endIdx, startIdx, endEdge, startEdge], result = [];
78
- for (let i = lo; i <= hi; i++) {
79
- const block = blocks[i], blockPath = [...pathPrefix, ...fieldNameInPrefix ? [fieldNameInPrefix] : [], {
80
- _key: block._key
81
- }], startPointForBlock = i === lo ? loEdge : "array-start", endPointForBlock = i === hi ? hiEdge : "array-end", startInside = edgeIsInside(startPointForBlock, blockPath), endInside = edgeIsInside(endPointForBlock, blockPath);
82
- if (!startInside && !endInside) {
83
- result.push(block);
84
- continue;
85
- }
86
- if (isTextBlock({
87
- schema: context.schema
88
- }, block)) {
89
- const sliced = sliceBoundaryTextBlock({
90
- context,
91
- block,
92
- blockPath,
93
- startEdge: startPointForBlock,
94
- endEdge: endPointForBlock
95
- });
96
- sliced && result.push(sliced);
97
- continue;
98
- }
99
- const childInfo = getNodeChildren(context, block, parent);
100
- if (!childInfo) {
101
- result.push(block);
102
- continue;
103
- }
104
- const innerSliced = sliceArray({
105
- context,
106
- blocks: childInfo.children,
107
- pathPrefix: blockPath,
108
- fieldNameInPrefix: childInfo.fieldName,
109
- startEdge: startPointForBlock,
110
- endEdge: endPointForBlock,
111
- parent: childInfo.parent
112
- }), updatedBlock = {
113
- ...block
114
- };
115
- updatedBlock[childInfo.fieldName] = innerSliced, result.push(updatedBlock);
116
- }
117
- return result;
118
- }
119
- function resolveIndex(blocks, pathPrefix, edge) {
120
- return edge === "array-start" ? 0 : edge === "array-end" ? blocks.length - 1 : findBlockIndexForPoint(blocks, pathPrefix, edge);
121
- }
122
- function findBlockIndexForPoint(blocks, pathPrefix, point) {
123
- let nodeSegmentIndex = pathPrefix.length;
124
- nodeSegmentIndex < point.path.length && typeof point.path[nodeSegmentIndex] == "string" && nodeSegmentIndex++;
125
- const segment = point.path[nodeSegmentIndex];
126
- return segment === void 0 ? -1 : isKeyedSegment(segment) ? blocks.findIndex((block) => block._key === segment._key) : typeof segment == "number" && segment >= 0 && segment < blocks.length ? segment : -1;
127
- }
128
- function edgeIsInside(edge, blockPath) {
129
- return edge === "array-start" || edge === "array-end" ? !1 : edge.path.length > blockPath.length;
130
- }
131
- function sliceBoundaryTextBlock({
132
- context,
133
- block,
134
- blockPath,
135
- startEdge,
136
- endEdge
137
- }) {
138
- if (!isTextBlock({
139
- schema: context.schema
140
- }, block))
141
- return block;
142
- const firstChild = block.children[0], lastChild = block.children[block.children.length - 1], blockRelativeStart = stripPrefix(startEdge, blockPath, block, {
143
- fallback: "block-start",
144
- firstChild
145
- }), blockRelativeEnd = stripPrefix(endEdge, blockPath, block, {
146
- fallback: "block-end",
147
- lastChild,
148
- context
149
- });
150
- return sliceBlocks({
151
- context: {
152
- ...context,
153
- selection: {
154
- anchor: blockRelativeStart,
155
- focus: blockRelativeEnd
156
- }
157
- },
158
- blocks: [block]
159
- })[0];
160
- }
161
- function stripPrefix(edge, blockPath, block, opts) {
162
- return edge !== "array-start" && edge !== "array-end" && edge.path.length > blockPath.length ? {
163
- path: edge.path.slice(blockPath.length - 1),
164
- offset: edge.offset
165
- } : opts.fallback === "block-start" ? {
166
- path: [{
167
- _key: block._key
168
- }, "children", {
169
- _key: opts.firstChild?._key ?? ""
170
- }],
171
- offset: 0
172
- } : {
173
- path: [{
174
- _key: block._key
175
- }, "children", {
176
- _key: opts.lastChild?._key ?? ""
177
- }],
178
- offset: opts.lastChild && opts.context && isSpan({
179
- schema: opts.context.schema
180
- }, opts.lastChild) ? opts.lastChild.text.length : 0
181
- };
182
- }
183
- const getFragment = (snapshot) => {
184
- const envelope = getSelectedValue(snapshot);
185
- if (envelope.length === 0)
186
- return [];
187
- const selection = snapshot.context.selection;
188
- if (selection && selection.anchor.path.length <= 1 && selection.focus.path.length <= 1)
189
- return envelope.map((block) => ({
190
- node: block,
191
- path: [{
192
- _key: block._key
193
- }]
194
- }));
195
- const {
196
- schema,
197
- containers,
198
- value
199
- } = snapshot.context, rootAcceptedTypes = getRootAcceptedTypes(schema), textBlockName = schema.block.name;
200
- let lastRootValid = envelope, lastRootValidPrefix = [], current = envelope;
201
- const pathPrefix = [];
202
- for (; current.length === 1; ) {
203
- const single = current[0], singlePath = [...pathPrefix, {
204
- _key: single._key
205
- }], container = resolveContainerAt(containers, value, singlePath);
206
- if (!container || !("field" in container))
207
- break;
208
- const children = single[container.field.name];
209
- if (!Array.isArray(children) || children.length === 0)
210
- break;
211
- const childBlocks = children;
212
- if (pathPrefix.push({
213
- _key: single._key
214
- }, container.field.name), current = childBlocks, childBlocks.every((block) => block._type === textBlockName || rootAcceptedTypes.has(block._type))) {
215
- lastRootValid = childBlocks, lastRootValidPrefix = [...pathPrefix];
216
- continue;
217
- }
218
- if (childBlocks.length !== 1)
219
- break;
220
- }
221
- return lastRootValid.map((block) => ({
222
- node: block,
223
- path: [...lastRootValidPrefix, {
224
- _key: block._key
225
- }]
226
- }));
227
- }, getSelectionEndBlock = (snapshot) => {
228
- const endPoint = getSelectionEndPoint$1(snapshot.context.selection);
229
- if (endPoint)
230
- return getFocusBlock({
231
- ...snapshot,
232
- context: {
233
- ...snapshot.context,
234
- selection: {
235
- anchor: endPoint,
236
- focus: endPoint
237
- }
238
- }
239
- });
240
- }, getSelectionStartBlock = (snapshot) => {
241
- const startPoint = getSelectionStartPoint$1(snapshot.context.selection);
242
- if (startPoint)
243
- return getFocusBlock({
244
- ...snapshot,
245
- context: {
246
- ...snapshot.context,
247
- selection: {
248
- anchor: startPoint,
249
- focus: startPoint
250
- }
251
- }
252
- });
253
- };
254
- function isOverlappingSelection(selection) {
255
- return (snapshot) => {
256
- const editorSelection = snapshot.context.selection;
257
- return !selection || !editorSelection || !hasNode(snapshot, selection.anchor.path) || !hasNode(snapshot, selection.focus.path) || !hasNode(snapshot, editorSelection.anchor.path) || !hasNode(snapshot, editorSelection.focus.path) ? !1 : rangesOverlap(selection, editorSelection, {
258
- value: snapshot.context.value
259
- });
260
- };
261
- }
262
- const isSelectionCollapsed = (snapshot) => isSelectionCollapsed$1(snapshot.context.selection), isSelectionExpanded$1 = (snapshot) => snapshot.context.selection !== null && !isSelectionCollapsed(snapshot), getSelectedBlocks = (snapshot) => {
263
- const selection = snapshot.context.selection;
264
- if (!selection)
265
- return [];
266
- const startPoint = getSelectionStartPoint$1(selection), endPoint = getSelectionEndPoint$1(selection);
267
- if (!startPoint || !endPoint)
268
- return [];
269
- const startRootKey = startPoint.path.find(isKeyedSegment)?._key, endRootKey = endPoint.path.find(isKeyedSegment)?._key;
270
- if (startRootKey && startRootKey === endRootKey) {
271
- const block = getBlock(snapshot, [{
272
- _key: startRootKey
273
- }]);
274
- if (block)
275
- return [{
276
- node: block.node,
277
- path: block.path
278
- }];
279
- }
280
- const result = [];
281
- for (const entry of getNodes(snapshot, {
282
- from: startPoint.path,
283
- to: endPoint.path,
284
- match: (_, path) => path.length === 1
285
- })) {
286
- const block = getBlock(snapshot, entry.path);
287
- block && result.push({
288
- node: block.node,
289
- path: block.path
290
- });
291
- }
292
- return result;
293
- }, isSelectingEntireBlocks = (snapshot) => {
294
- if (!snapshot.context.selection)
295
- return !1;
296
- const startPoint = snapshot.context.selection.backward ? snapshot.context.selection.focus : snapshot.context.selection.anchor, endPoint = snapshot.context.selection.backward ? snapshot.context.selection.anchor : snapshot.context.selection.focus, startBlock = getSelectionStartBlock(snapshot), endBlock = getSelectionEndBlock(snapshot);
297
- if (!startBlock || !endBlock)
298
- return !1;
299
- const startBlockStartPoint = getBlockStartPoint({
300
- context: snapshot.context,
301
- block: startBlock
302
- }), endBlockEndPoint = getBlockEndPoint({
303
- context: snapshot.context,
304
- block: endBlock
305
- });
306
- return isEqualSelectionPoints(startBlockStartPoint, startPoint) && isEqualSelectionPoints(endBlockEndPoint, endPoint);
307
- };
308
- function isSelectionExpanded(selection) {
309
- return selection ? !isSelectionCollapsed$1(selection) : !1;
310
- }
311
- const getSelectionEndPoint = (snapshot) => {
312
- if (snapshot.context.selection)
313
- return snapshot.context.selection.backward ? snapshot.context.selection.anchor : snapshot.context.selection.focus;
314
- }, getNextSpan = (snapshot) => {
315
- const point = getSelectionEndPoint(snapshot);
316
- if (point)
317
- return getSibling(snapshot, point.path, {
318
- direction: "next",
319
- match: (node) => isSpan(snapshot.context, node)
320
- });
321
- }, getSelectionStartPoint = (snapshot) => {
322
- if (snapshot.context.selection)
323
- return snapshot.context.selection.backward ? snapshot.context.selection.focus : snapshot.context.selection.anchor;
324
- }, getPreviousSpan = (snapshot) => {
325
- const point = getSelectionStartPoint(snapshot);
326
- if (point)
327
- return getSibling(snapshot, point.path, {
328
- direction: "previous",
329
- match: (node) => isSpan(snapshot.context, node)
330
- });
331
- };
332
- function getSelectedChildren(options) {
333
- const filter = options?.filter;
334
- return (snapshot) => {
335
- const startPoint = getSelectionStartPoint$1(snapshot.context.selection), endPoint = getSelectionEndPoint$1(snapshot.context.selection);
336
- if (!startPoint || !endPoint)
337
- return [];
338
- const startChildKey = startPoint.path.findLast(isKeyedSegment)?._key, endChildKey = endPoint.path.findLast(isKeyedSegment)?._key, result = [];
339
- if (startChildKey && startChildKey === endChildKey && isInline(snapshot, startPoint.path)) {
340
- const node = getNode(snapshot, startPoint.path);
341
- if (node) {
342
- const child = node.node;
343
- let skip = !1;
344
- child._key === startChildKey && isSpan(snapshot.context, child) && (startPoint.offset >= child.text.length && (skip = !0), endPoint.offset <= 0 && (skip = !0)), !skip && (!filter || filter(child)) && result.push({
345
- node: child,
346
- path: node.path
347
- });
348
- }
349
- return result;
350
- }
351
- for (const entry of getNodes(snapshot, {
352
- from: startPoint.path,
353
- to: endPoint.path,
354
- match: (_, path) => isInline(snapshot, path)
355
- })) {
356
- const child = entry.node;
357
- child._key === startChildKey && isSpan(snapshot.context, child) && startPoint.offset >= child.text.length || child._key === endChildKey && isSpan(snapshot.context, child) && endPoint.offset <= 0 || filter && !filter(child) || result.push({
358
- node: child,
359
- path: entry.path
360
- });
361
- }
362
- return result;
363
- };
364
- }
365
- const getSelectedSpans = (snapshot) => snapshot.context.selection ? getSelectedChildren({
366
- filter: (child) => isSpan(snapshot.context, child)
367
- })(snapshot) : [], getMarkState = (snapshot) => {
368
- if (!snapshot.context.selection)
369
- return;
370
- let selection = snapshot.context.selection;
371
- if (getBlock(snapshot, selection.anchor.path)) {
372
- const spanSelectionPoint = blockOffsetToSpanSelectionPoint({
373
- snapshot,
374
- blockOffset: {
375
- path: selection.anchor.path,
376
- offset: selection.anchor.offset
377
- },
378
- direction: selection.backward ? "backward" : "forward"
379
- });
380
- selection = spanSelectionPoint ? {
381
- ...selection,
382
- anchor: spanSelectionPoint
383
- } : selection;
384
- }
385
- if (getBlock(snapshot, selection.focus.path)) {
386
- const spanSelectionPoint = blockOffsetToSpanSelectionPoint({
387
- snapshot,
388
- blockOffset: {
389
- path: selection.focus.path,
390
- offset: selection.focus.offset
391
- },
392
- direction: selection.backward ? "backward" : "forward"
393
- });
394
- selection = spanSelectionPoint ? {
395
- ...selection,
396
- focus: spanSelectionPoint
397
- } : selection;
398
- }
399
- const focusSpan = getFocusSpan({
400
- ...snapshot,
401
- context: {
402
- ...snapshot.context,
403
- selection
404
- }
405
- });
406
- if (!focusSpan)
407
- return;
408
- if (isSelectionExpanded(selection)) {
409
- const spanInfo = getSelectedSpans({
410
- ...snapshot,
411
- context: {
412
- ...snapshot.context,
413
- selection
414
- }
415
- }).map((span) => {
416
- const block = getParent(snapshot, span.path, {
417
- match: (node) => isTextBlock({
418
- schema: snapshot.context.schema
419
- }, node)
420
- });
421
- return {
422
- marks: span.node.marks ?? [],
423
- decoratorNames: getPathSubSchema(snapshot, span.path).decorators.map((decorator) => decorator.name),
424
- markDefKeys: (block?.node.markDefs ?? []).map((markDef) => markDef._key)
425
- };
426
- }), candidateMarks = /* @__PURE__ */ new Set();
427
- for (const {
428
- marks: spanMarks
429
- } of spanInfo)
430
- for (const mark of spanMarks)
431
- candidateMarks.add(mark);
432
- const marks2 = [];
433
- for (const candidate of candidateMarks) {
434
- const isDecoratorSomewhere = spanInfo.some(({
435
- decoratorNames
436
- }) => decoratorNames.includes(candidate));
437
- let active = !0;
438
- for (const {
439
- marks: spanMarks,
440
- decoratorNames,
441
- markDefKeys
442
- } of spanInfo)
443
- if ((isDecoratorSomewhere ? decoratorNames.includes(candidate) : markDefKeys.includes(candidate)) && !spanMarks.includes(candidate)) {
444
- active = !1;
445
- break;
446
- }
447
- active && marks2.push(candidate);
448
- }
449
- return {
450
- state: "unchanged",
451
- marks: marks2
452
- };
453
- }
454
- const decorators = getPathSubSchema(snapshot, focusSpan.path).decorators.map((decorator) => decorator.name), marks = focusSpan.node.marks ?? [], marksWithoutAnnotations = marks.filter((mark) => decorators.includes(mark)), spanHasAnnotations = marks.length > marksWithoutAnnotations.length, spanIsEmpty = focusSpan.node.text.length === 0, atTheBeginningOfSpan = snapshot.context.selection.anchor.offset === 0, atTheEndOfSpan = snapshot.context.selection.anchor.offset === focusSpan.node.text.length, previousSpan = getPreviousSpan({
455
- ...snapshot,
456
- context: {
457
- ...snapshot.context,
458
- selection
459
- }
460
- }), nextSpan = getNextSpan({
461
- ...snapshot,
462
- context: {
463
- ...snapshot.context,
464
- selection
465
- }
466
- }), nextSpanAnnotations = nextSpan?.node?.marks?.filter((mark) => !decorators.includes(mark)) ?? [], spanAnnotations = marks.filter((mark) => !decorators.includes(mark)), previousSpanHasAnnotations = previousSpan ? previousSpan.node.marks?.some((mark) => !decorators.includes(mark)) : !1, previousSpanHasSameAnnotations = previousSpan ? previousSpan.node.marks?.filter((mark) => !decorators.includes(mark)).every((mark) => marks.includes(mark)) : !1, previousSpanHasSameAnnotation = previousSpan ? previousSpan.node.marks?.some((mark) => !decorators.includes(mark) && marks.includes(mark)) : !1, previousSpanHasSameMarks = previousSpan ? previousSpan.node.marks?.every((mark) => marks.includes(mark)) : !1, nextSpanSharesSomeAnnotations = spanAnnotations.some((mark) => nextSpanAnnotations?.includes(mark));
467
- if (spanHasAnnotations && !spanIsEmpty) {
468
- if (atTheBeginningOfSpan) {
469
- if (previousSpanHasSameMarks)
470
- return {
471
- state: "changed",
472
- previousMarks: marks,
473
- marks: previousSpan?.node.marks ?? []
474
- };
475
- if (previousSpanHasSameAnnotations)
476
- return {
477
- state: "changed",
478
- previousMarks: marks,
479
- marks: previousSpan?.node.marks ?? []
480
- };
481
- if (previousSpanHasSameAnnotation)
482
- return {
483
- state: "unchanged",
484
- marks: focusSpan.node.marks ?? []
485
- };
486
- if (!previousSpan)
487
- return {
488
- state: "changed",
489
- previousMarks: marks,
490
- marks: []
491
- };
492
- }
493
- if (atTheEndOfSpan) {
494
- if (!nextSpan)
495
- return {
496
- state: "changed",
497
- previousMarks: marks,
498
- marks: []
499
- };
500
- if (nextSpanAnnotations.length > 0 && !nextSpanSharesSomeAnnotations)
501
- return {
502
- state: "changed",
503
- previousMarks: marks,
504
- marks: []
505
- };
506
- if (nextSpanSharesSomeAnnotations && nextSpanAnnotations.length < spanAnnotations.length || !nextSpanSharesSomeAnnotations)
507
- return {
508
- state: "changed",
509
- previousMarks: marks,
510
- marks: nextSpan?.node.marks ?? []
511
- };
512
- }
513
- }
514
- return atTheBeginningOfSpan && !spanIsEmpty && previousSpan ? previousSpanHasAnnotations ? {
515
- state: "changed",
516
- marks,
517
- previousMarks: previousSpan?.node.marks ?? []
518
- } : {
519
- state: "changed",
520
- previousMarks: marks,
521
- marks: (previousSpan?.node.marks ?? []).filter((mark) => decorators.includes(mark))
522
- } : {
523
- state: "unchanged",
524
- marks
525
- };
526
- };
527
- function getActiveAnnotationsMarks(snapshot) {
528
- const schema = snapshot.context.schema;
529
- return (getMarkState(snapshot)?.marks ?? []).filter((mark) => !schema.decorators.map((decorator) => decorator.name).includes(mark));
530
- }
531
- const getSelectedTextBlocks = (snapshot) => {
532
- const selection = snapshot.context.selection;
533
- if (!selection)
534
- return [];
535
- const startPoint = getSelectionStartPoint$1(selection), endPoint = getSelectionEndPoint$1(selection);
536
- if (!startPoint || !endPoint)
537
- return [];
538
- const startBlock = getEnclosingBlock(snapshot, startPoint.path), endBlock = getEnclosingBlock(snapshot, endPoint.path);
539
- if (startBlock && endBlock && startBlock.node._key === endBlock.node._key && isTextBlock(snapshot.context, startBlock.node))
540
- return [{
541
- node: startBlock.node,
542
- path: startBlock.path
543
- }];
544
- const result = [];
545
- for (const entry of getNodes(snapshot, {
546
- from: startPoint.path,
547
- to: endPoint.path,
548
- match: (node) => isTextBlock(snapshot.context, node)
549
- }))
550
- isTextBlock(snapshot.context, entry.node) && result.push({
551
- node: entry.node,
552
- path: entry.path
553
- });
554
- return result;
555
- };
556
- function getActiveDecorators(snapshot) {
557
- const decoratorState = snapshot.decoratorState, markState = getMarkState(snapshot), selectedBlocks = getSelectedTextBlocks(snapshot), decorators = /* @__PURE__ */ new Set();
558
- for (const block of selectedBlocks)
559
- for (const decorator of getPathSubSchema(snapshot, block.path).decorators)
560
- decorators.add(decorator.name);
561
- if (decorators.size === 0)
562
- for (const decorator of snapshot.context.schema.decorators)
563
- decorators.add(decorator.name);
564
- let activeDecorators = (markState?.marks ?? []).filter((mark) => decorators.has(mark));
565
- for (const decorator in decoratorState)
566
- decoratorState[decorator] === !1 ? activeDecorators = activeDecorators.filter((activeDecorator) => activeDecorator !== decorator) : decoratorState[decorator] === !0 && (activeDecorators.includes(decorator) || activeDecorators.push(decorator));
567
- return activeDecorators;
568
- }
569
- function isActiveAnnotation(annotation, options) {
570
- return (snapshot) => {
571
- if ((options?.mode ?? "full") === "partial" && isSelectionExpanded$1(snapshot))
572
- return getSelectedValue(snapshot).some((block) => {
573
- if (!isTextBlock(snapshot.context, block))
574
- return !1;
575
- const annotationKeys = new Set((block.markDefs ?? []).filter((markDef) => markDef._type === annotation).map((markDef) => markDef._key));
576
- return annotationKeys.size === 0 ? !1 : block.children.some((child) => isSpan(snapshot.context, child) && // Spans the selection only touches at a zero-width boundary
577
- // survive the slice with empty text and intact `marks`,
578
- // keeping their definitions alive through the slice's
579
- // unused-definition pruning. A boundary touch selects nothing
580
- // of the annotation, so those spans don't count.
581
- child.text.length > 0 && child.marks?.some((mark) => annotationKeys.has(mark)));
582
- });
583
- const selectionMarkDefs = getSelectedTextBlocks(snapshot).flatMap((block) => block.node.markDefs ?? []), activeAnnotations = getActiveAnnotationsMarks(snapshot);
584
- return selectionMarkDefs.filter((markDef) => markDef._type === annotation && activeAnnotations.includes(markDef._key)).length > 0;
585
- };
586
- }
587
- const getApplicableSchema = (snapshot) => {
588
- if (!snapshot.context.selection)
589
- return EMPTY;
590
- const focusBlock = getFocusBlock(snapshot), focusSub = focusBlock ? getPathSubSchema(snapshot, focusBlock.path) : void 0, insertion = focusSub ? {
591
- blockObjects: namesOfArray(focusSub.blockObjects),
592
- inlineObjects: namesOfArray(focusSub.inlineObjects)
593
- } : {
594
- blockObjects: EMPTY_SET,
595
- inlineObjects: EMPTY_SET
596
- }, textBlocks = getSelectedTextBlocks(snapshot);
597
- if (textBlocks.length === 0)
598
- return {
599
- decorators: EMPTY_SET,
600
- annotations: EMPTY_SET,
601
- lists: EMPTY_SET,
602
- styles: EMPTY_SET,
603
- ...insertion
604
- };
605
- let textApplicable = textCategoryNames(getPathSubSchema(snapshot, textBlocks[0].path));
606
- for (let i = 1; i < textBlocks.length; i++)
607
- textApplicable = unionTextCategories(textApplicable, textCategoryNames(getPathSubSchema(snapshot, textBlocks[i].path)));
608
- return {
609
- ...textApplicable,
610
- ...insertion
611
- };
612
- };
613
- function compareApplicableSchema(a, b) {
614
- return a === b ? !0 : sameSet(a.decorators, b.decorators) && sameSet(a.annotations, b.annotations) && sameSet(a.lists, b.lists) && sameSet(a.styles, b.styles) && sameSet(a.blockObjects, b.blockObjects) && sameSet(a.inlineObjects, b.inlineObjects);
615
- }
616
- const EMPTY_SET = Object.freeze(/* @__PURE__ */ new Set()), EMPTY = Object.freeze({
617
- decorators: EMPTY_SET,
618
- annotations: EMPTY_SET,
619
- lists: EMPTY_SET,
620
- styles: EMPTY_SET,
621
- blockObjects: EMPTY_SET,
622
- inlineObjects: EMPTY_SET
623
- });
624
- function textCategoryNames(schema) {
625
- return {
626
- decorators: namesOfArray(schema.decorators),
627
- annotations: namesOfArray(schema.annotations),
628
- lists: namesOfArray(schema.lists),
629
- styles: namesOfArray(schema.styles)
630
- };
631
- }
632
- function unionTextCategories(a, b) {
633
- return {
634
- decorators: unionSet(a.decorators, b.decorators),
635
- annotations: unionSet(a.annotations, b.annotations),
636
- lists: unionSet(a.lists, b.lists),
637
- styles: unionSet(a.styles, b.styles)
638
- };
639
- }
640
- function namesOfArray(entries) {
641
- return new Set(entries.map((entry) => entry.name));
642
- }
643
- function unionSet(a, b) {
644
- const result = new Set(a);
645
- for (const item of b)
646
- result.add(item);
647
- return result;
648
- }
649
- function sameSet(a, b) {
650
- if (a === b)
651
- return !0;
652
- if (a.size !== b.size)
653
- return !1;
654
- for (const item of a)
655
- if (!b.has(item))
656
- return !1;
657
- return !0;
658
- }
659
- const getActiveAnnotations = (snapshot) => {
660
- if (!snapshot.context.selection)
661
- return [];
662
- const selectedBlocks = getSelectedTextBlocks(snapshot), markState = getMarkState(snapshot), decoratorNames = /* @__PURE__ */ new Set();
663
- for (const block of selectedBlocks)
664
- for (const decorator of getPathSubSchema(snapshot, block.path).decorators)
665
- decoratorNames.add(decorator.name);
666
- if (decoratorNames.size === 0)
667
- for (const decorator of snapshot.context.schema.decorators)
668
- decoratorNames.add(decorator.name);
669
- const activeAnnotations = (markState?.marks ?? []).filter((mark) => !decoratorNames.has(mark));
670
- return selectedBlocks.flatMap((block) => block.node.markDefs ?? []).filter((markDef) => activeAnnotations.includes(markDef._key));
671
- }, getActiveListItem = (snapshot) => {
672
- if (!snapshot.context.selection)
673
- return;
674
- const selectedTextBlocks = getSelectedTextBlocks(snapshot), firstTextBlock = selectedTextBlocks.at(0);
675
- if (!firstTextBlock)
676
- return;
677
- const firstListItem = firstTextBlock.node.listItem;
678
- if (firstListItem && selectedTextBlocks.filter((block) => getPathSubSchema(snapshot, block.path).lists.some((l) => l.name === firstListItem)).every((block) => block.node.listItem === firstListItem))
679
- return firstListItem;
680
- }, getActiveStyle = (snapshot) => {
681
- if (!snapshot.context.selection)
682
- return;
683
- const selectedTextBlocks = getSelectedTextBlocks(snapshot), firstTextBlock = selectedTextBlocks.at(0);
684
- if (!firstTextBlock)
685
- return;
686
- const firstStyle = firstTextBlock.node.style ?? getPathSubSchema(snapshot, firstTextBlock.path).styles.at(0)?.name;
687
- if (firstStyle && selectedTextBlocks.filter((block) => getPathSubSchema(snapshot, block.path).styles.some((s) => s.name === firstStyle)).every((block) => (block.node.style ?? getPathSubSchema(snapshot, block.path).styles.at(0)?.name) === firstStyle))
688
- return firstStyle;
689
- }, getNextInlineObject = (snapshot) => {
690
- const point = getSelectionEndPoint(snapshot);
691
- return point ? getSibling(snapshot, point.path, {
692
- direction: "next",
693
- match: (node) => isObject(snapshot, node)
694
- }) : void 0;
695
- }, getPreviousInlineObject = (snapshot) => {
696
- const point = getSelectionStartPoint(snapshot);
697
- return point ? getSibling(snapshot, point.path, {
698
- direction: "previous",
699
- match: (node) => isObject(snapshot, node)
700
- }) : void 0;
701
- }, getSelectionText = (snapshot) => {
702
- const selectedValue = getSelectedValue(snapshot);
703
- return collectText(snapshot.context, selectedValue);
704
- };
705
- function collectText(context, blocks, parent) {
706
- let text = "";
707
- for (const block of blocks) {
708
- if (isTextBlock(context, block)) {
709
- for (const child of block.children)
710
- isSpan(context, child) && (text += child.text);
711
- continue;
712
- }
713
- const childInfo = getNodeChildren(context, block, parent);
714
- childInfo && (text += collectText(context, childInfo.children, childInfo.parent));
715
- }
716
- return text;
717
- }
718
- const getCaretWordSelection = (snapshot) => {
719
- if (!snapshot.context.selection || !isSelectionCollapsed(snapshot))
720
- return null;
721
- const focusTextBlock = getFocusTextBlock(snapshot), selectionStartPoint = getSelectionStartPoint(snapshot), selectionStartOffset = selectionStartPoint ? spanSelectionPointToBlockOffset({
722
- snapshot,
723
- selectionPoint: selectionStartPoint
724
- }) : void 0;
725
- if (!focusTextBlock || !selectionStartPoint || !selectionStartOffset)
726
- return null;
727
- const previousInlineObject = getPreviousInlineObject(snapshot), blockStartPoint = getBlockStartPoint({
728
- context: snapshot.context,
729
- block: focusTextBlock
730
- }), textDirectlyBefore = getSelectionText({
731
- ...snapshot,
732
- context: {
733
- ...snapshot.context,
734
- selection: {
735
- anchor: previousInlineObject ? {
736
- path: previousInlineObject.path,
737
- offset: 0
738
- } : blockStartPoint,
739
- focus: selectionStartPoint
740
- }
741
- }
742
- }).split(/\s+/).at(-1), nextInlineObject = getNextInlineObject(snapshot), blockEndPoint = getBlockEndPoint({
743
- context: snapshot.context,
744
- block: focusTextBlock
745
- }), textDirectlyAfter = getSelectionText({
746
- ...snapshot,
747
- context: {
748
- ...snapshot.context,
749
- selection: {
750
- anchor: selectionStartPoint,
751
- focus: nextInlineObject ? {
752
- path: nextInlineObject.path,
753
- offset: 0
754
- } : blockEndPoint
755
- }
756
- }
757
- }).split(/\s+/).at(0);
758
- if ((textDirectlyBefore === void 0 || textDirectlyBefore === "") && (textDirectlyAfter === void 0 || textDirectlyAfter === ""))
759
- return null;
760
- const caretWordStartOffset = textDirectlyBefore ? {
761
- ...selectionStartOffset,
762
- offset: selectionStartOffset.offset - textDirectlyBefore.length
763
- } : selectionStartOffset, caretWordEndOffset = textDirectlyAfter ? {
764
- ...selectionStartOffset,
765
- offset: selectionStartOffset.offset + textDirectlyAfter.length
766
- } : selectionStartOffset, caretWordStartSelectionPoint = blockOffsetToSpanSelectionPoint({
767
- snapshot,
768
- blockOffset: caretWordStartOffset,
769
- direction: "backward"
770
- }), caretWordEndSelectionPoint = blockOffsetToSpanSelectionPoint({
771
- snapshot,
772
- blockOffset: caretWordEndOffset,
773
- direction: "forward"
774
- });
775
- if (!caretWordStartSelectionPoint || !caretWordEndSelectionPoint)
776
- return null;
777
- const caretWordSelection = {
778
- anchor: caretWordStartSelectionPoint,
779
- focus: caretWordEndSelectionPoint
780
- };
781
- return isSelectionExpanded$1({
782
- ...snapshot,
783
- context: {
784
- ...snapshot.context,
785
- selection: caretWordSelection
786
- }
787
- }) ? caretWordSelection : null;
788
- }, getFocusBlockObject = (snapshot) => {
789
- const focusBlock = getFocusBlock(snapshot);
790
- if (focusBlock && !isTextBlockNode(snapshot.context, focusBlock.node) && !isEditableContainer(snapshot, focusBlock.node, focusBlock.path))
791
- return {
792
- node: focusBlock.node,
793
- path: focusBlock.path
794
- };
795
- }, getFocusListBlock = (snapshot) => {
796
- const focusTextBlock = getFocusTextBlock(snapshot);
797
- return focusTextBlock && isListBlock(snapshot.context, focusTextBlock.node) ? {
798
- node: focusTextBlock.node,
799
- path: focusTextBlock.path
800
- } : void 0;
801
- }, getLastBlock = (snapshot) => {
802
- const focusBlock = getFocusBlock(snapshot);
803
- if (focusBlock) {
804
- const last = getChildren(snapshot, parentPath(focusBlock.path)).at(-1);
805
- if (last)
806
- return getBlock(snapshot, last.path);
807
- }
808
- const node = snapshot.context.value.at(-1);
809
- return node ? {
810
- node,
811
- path: [{
812
- _key: node._key
813
- }]
814
- } : void 0;
815
- }, getNextBlock = (snapshot) => {
816
- const selectionEndBlock = getSelectionEndBlock(snapshot);
817
- if (!selectionEndBlock)
818
- return;
819
- const next = getSibling(snapshot, selectionEndBlock.path, {
820
- direction: "next"
821
- });
822
- if (next)
823
- return getBlock(snapshot, next.path);
824
- }, getPreviousBlock = (snapshot) => {
825
- const selectionStartBlock = getSelectionStartBlock(snapshot);
826
- if (!selectionStartBlock)
827
- return;
828
- const previous = getSibling(snapshot, selectionStartBlock.path, {
829
- direction: "previous"
830
- });
831
- if (previous)
832
- return getBlock(snapshot, previous.path);
833
- }, getSelectionEndChild = (snapshot) => {
834
- const endPoint = getSelectionEndPoint$1(snapshot.context.selection);
835
- if (endPoint)
836
- return getFocusChild({
837
- ...snapshot,
838
- context: {
839
- ...snapshot.context,
840
- selection: {
841
- anchor: endPoint,
842
- focus: endPoint
843
- }
844
- }
845
- });
846
- }, getSelectionStartChild = (snapshot) => {
847
- const startPoint = getSelectionStartPoint$1(snapshot.context.selection);
848
- if (startPoint)
849
- return getFocusChild({
850
- ...snapshot,
851
- context: {
852
- ...snapshot.context,
853
- selection: {
854
- anchor: startPoint,
855
- focus: startPoint
856
- }
857
- }
858
- });
859
- };
860
- function isActiveDecorator(decorator) {
861
- return (snapshot) => {
862
- if (isSelectionExpanded$1(snapshot)) {
863
- const inScopeSpans = getSelectedSpans(snapshot).filter((span) => getPathSubSchema(snapshot, span.path).decorators.some((d) => d.name === decorator));
864
- return inScopeSpans.length > 0 && inScopeSpans.every((span) => span.node.marks?.includes(decorator));
865
- }
866
- return getActiveDecorators(snapshot).includes(decorator);
867
- };
868
- }
869
- function isActiveListItem(listItem) {
870
- return (snapshot) => getActiveListItem(snapshot) === listItem;
871
- }
872
- function isActiveStyle(style) {
873
- return (snapshot) => getActiveStyle(snapshot) === style;
874
- }
875
- function isAtTheEndOfBlock(block) {
876
- return (snapshot) => {
877
- if (!snapshot.context.selection || !isSelectionCollapsed(snapshot))
878
- return !1;
879
- const blockEndPoint = getBlockEndPoint({
880
- context: snapshot.context,
881
- block
882
- });
883
- return isEqualSelectionPoints(snapshot.context.selection.focus, blockEndPoint);
884
- };
885
- }
886
- function isAtTheStartOfBlock(block) {
887
- return (snapshot) => {
888
- if (!snapshot.context.selection || !isSelectionCollapsed(snapshot))
889
- return !1;
890
- const blockStartPoint = getBlockStartPoint({
891
- context: snapshot.context,
892
- block
893
- });
894
- return isEqualSelectionPoints(snapshot.context.selection.focus, blockStartPoint);
895
- };
896
- }
897
- export {
898
- compareApplicableSchema,
899
- getActiveAnnotations,
900
- getActiveAnnotationsMarks,
901
- getActiveDecorators,
902
- getActiveListItem,
903
- getActiveStyle,
904
- getApplicableSchema,
905
- getCaretWordSelection,
906
- getFocusBlock,
907
- getFocusBlockObject,
908
- getFocusChild,
909
- getFocusInlineObject,
910
- getFocusListBlock,
911
- getFocusSpan,
912
- getFocusTextBlock,
913
- getFragment,
914
- getInline,
915
- getLastBlock,
916
- getMarkState,
917
- getNextBlock,
918
- getNextInlineObject,
919
- getNextSpan,
920
- getPreviousBlock,
921
- getPreviousInlineObject,
922
- getPreviousSpan,
923
- getRootAcceptedTypes,
924
- getSelectedBlocks,
925
- getSelectedSpans,
926
- getSelectedTextBlocks,
927
- getSelectedValue,
928
- getSelectionEndBlock,
929
- getSelectionEndChild,
930
- getSelectionEndPoint,
931
- getSelectionStartBlock,
932
- getSelectionStartChild,
933
- getSelectionStartPoint,
934
- getSelectionText,
935
- isActiveAnnotation,
936
- isActiveDecorator,
937
- isActiveListItem,
938
- isActiveStyle,
939
- isAtTheEndOfBlock,
940
- isAtTheStartOfBlock,
941
- isOverlappingSelection,
942
- isSelectingEntireBlocks,
943
- isSelectionCollapsed,
944
- isSelectionExpanded$1 as isSelectionExpanded,
945
- rangesOverlap
946
- };
947
- //# sourceMappingURL=selector.is-at-the-start-of-block.js.map