@portabletext/editor 2.17.1 → 2.18.0

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 (46) hide show
  1. package/lib/_chunks-dts/{behavior.types.action.d.cts → index.d.ts} +9 -9
  2. package/lib/_chunks-es/util.get-text-block-text.js +1 -2
  3. package/lib/_chunks-es/util.get-text-block-text.js.map +1 -1
  4. package/lib/behaviors/index.d.ts +1 -1
  5. package/lib/index.d.ts +1 -1
  6. package/lib/plugins/index.d.ts +2 -2
  7. package/lib/selectors/index.d.ts +2 -2
  8. package/lib/utils/index.d.ts +3 -3
  9. package/package.json +18 -30
  10. package/src/test/vitest/step-definitions.tsx +2 -0
  11. package/src/test/vitest/test-editor.tsx +18 -4
  12. package/src/utils/key-generator.ts +1 -3
  13. package/lib/_chunks-cjs/selector.get-selection-text.cjs +0 -89
  14. package/lib/_chunks-cjs/selector.get-selection-text.cjs.map +0 -1
  15. package/lib/_chunks-cjs/selector.get-text-before.cjs +0 -34
  16. package/lib/_chunks-cjs/selector.get-text-before.cjs.map +0 -1
  17. package/lib/_chunks-cjs/selector.is-at-the-start-of-block.cjs +0 -875
  18. package/lib/_chunks-cjs/selector.is-at-the-start-of-block.cjs.map +0 -1
  19. package/lib/_chunks-cjs/use-editor.cjs +0 -28
  20. package/lib/_chunks-cjs/use-editor.cjs.map +0 -1
  21. package/lib/_chunks-cjs/util.child-selection-point-to-block-offset.cjs +0 -79
  22. package/lib/_chunks-cjs/util.child-selection-point-to-block-offset.cjs.map +0 -1
  23. package/lib/_chunks-cjs/util.get-text-block-text.cjs +0 -477
  24. package/lib/_chunks-cjs/util.get-text-block-text.cjs.map +0 -1
  25. package/lib/_chunks-cjs/util.is-empty-text-block.cjs +0 -38
  26. package/lib/_chunks-cjs/util.is-empty-text-block.cjs.map +0 -1
  27. package/lib/_chunks-cjs/util.merge-text-blocks.cjs +0 -23
  28. package/lib/_chunks-cjs/util.merge-text-blocks.cjs.map +0 -1
  29. package/lib/_chunks-cjs/util.slice-text-block.cjs +0 -63
  30. package/lib/_chunks-cjs/util.slice-text-block.cjs.map +0 -1
  31. package/lib/_chunks-dts/behavior.types.action.d.ts +0 -3650
  32. package/lib/behaviors/index.cjs +0 -35
  33. package/lib/behaviors/index.cjs.map +0 -1
  34. package/lib/behaviors/index.d.cts +0 -2
  35. package/lib/index.cjs +0 -12952
  36. package/lib/index.cjs.map +0 -1
  37. package/lib/index.d.cts +0 -2
  38. package/lib/plugins/index.cjs +0 -826
  39. package/lib/plugins/index.cjs.map +0 -1
  40. package/lib/plugins/index.d.cts +0 -196
  41. package/lib/selectors/index.cjs +0 -243
  42. package/lib/selectors/index.cjs.map +0 -1
  43. package/lib/selectors/index.d.cts +0 -357
  44. package/lib/utils/index.cjs +0 -97
  45. package/lib/utils/index.cjs.map +0 -1
  46. package/lib/utils/index.d.cts +0 -186
@@ -1,875 +0,0 @@
1
- "use strict";
2
- var types = require("@sanity/types"), selector_getSelectionText = require("./selector.get-selection-text.cjs"), util_isEmptyTextBlock = require("./util.is-empty-text-block.cjs"), schema = require("@portabletext/schema"), util_getTextBlockText = require("./util.get-text-block-text.cjs");
3
- const getFocusInlineObject = (snapshot) => {
4
- const focusChild = selector_getSelectionText.getFocusChild(snapshot);
5
- return focusChild && !types.isPortableTextSpan(focusChild.node) ? {
6
- node: focusChild.node,
7
- path: focusChild.path
8
- } : void 0;
9
- }, getSelectedBlocks = (snapshot) => {
10
- if (!snapshot.context.selection)
11
- return [];
12
- const selectedBlocks = [], startPoint = util_getTextBlockText.getSelectionStartPoint(snapshot.context.selection), endPoint = util_getTextBlockText.getSelectionEndPoint(snapshot.context.selection), startKey = util_getTextBlockText.getBlockKeyFromSelectionPoint(startPoint), endKey = util_getTextBlockText.getBlockKeyFromSelectionPoint(endPoint);
13
- if (!startKey || !endKey)
14
- return selectedBlocks;
15
- const startBlockIndex = snapshot.blockIndexMap.get(startKey), endBlockIndex = snapshot.blockIndexMap.get(endKey);
16
- if (startBlockIndex === void 0 || endBlockIndex === void 0)
17
- return selectedBlocks;
18
- const slicedValue = snapshot.context.value.slice(startBlockIndex, endBlockIndex + 1);
19
- for (const block of slicedValue) {
20
- if (block._key === startKey) {
21
- if (selectedBlocks.push({
22
- node: block,
23
- path: [{
24
- _key: block._key
25
- }]
26
- }), startKey === endKey)
27
- break;
28
- continue;
29
- }
30
- if (block._key === endKey) {
31
- selectedBlocks.push({
32
- node: block,
33
- path: [{
34
- _key: block._key
35
- }]
36
- });
37
- break;
38
- }
39
- selectedBlocks.length > 0 && selectedBlocks.push({
40
- node: block,
41
- path: [{
42
- _key: block._key
43
- }]
44
- });
45
- }
46
- return selectedBlocks;
47
- }, getSelectionEndBlock = (snapshot) => {
48
- const endPoint = util_getTextBlockText.getSelectionEndPoint(snapshot.context.selection);
49
- if (endPoint)
50
- return selector_getSelectionText.getFocusBlock({
51
- ...snapshot,
52
- context: {
53
- ...snapshot.context,
54
- selection: {
55
- anchor: endPoint,
56
- focus: endPoint
57
- }
58
- }
59
- });
60
- }, getSelectionStartBlock = (snapshot) => {
61
- const startPoint = util_getTextBlockText.getSelectionStartPoint(snapshot.context.selection);
62
- if (startPoint)
63
- return selector_getSelectionText.getFocusBlock({
64
- ...snapshot,
65
- context: {
66
- ...snapshot.context,
67
- selection: {
68
- anchor: startPoint,
69
- focus: startPoint
70
- }
71
- }
72
- });
73
- }, getSelectionEndPoint = (snapshot) => {
74
- if (snapshot.context.selection)
75
- return snapshot.context.selection.backward ? snapshot.context.selection.anchor : snapshot.context.selection.focus;
76
- };
77
- function isPointAfterSelection(point) {
78
- return (snapshot) => {
79
- if (!snapshot.context.selection)
80
- return !1;
81
- const endPoint = util_getTextBlockText.getSelectionEndPoint(snapshot.context.selection), endBlockKey = util_getTextBlockText.getBlockKeyFromSelectionPoint(endPoint), endChildKey = util_getTextBlockText.getChildKeyFromSelectionPoint(endPoint), pointBlockKey = util_getTextBlockText.getBlockKeyFromSelectionPoint(point), pointChildKey = util_getTextBlockText.getChildKeyFromSelectionPoint(point);
82
- if (!pointBlockKey || !endBlockKey)
83
- return !1;
84
- const pointBlockIndex = snapshot.blockIndexMap.get(pointBlockKey), endBlockIndex = snapshot.blockIndexMap.get(endBlockKey);
85
- if (pointBlockIndex === void 0 || endBlockIndex === void 0)
86
- return !1;
87
- if (pointBlockIndex > endBlockIndex)
88
- return !0;
89
- if (pointBlockIndex < endBlockIndex)
90
- return !1;
91
- const pointBlock = snapshot.context.value.at(pointBlockIndex);
92
- if (!pointBlock || !schema.isTextBlock(snapshot.context, pointBlock))
93
- return !1;
94
- let pointChildIndex, endChildIndex, childIndex = -1;
95
- for (const child of pointBlock.children) {
96
- if (childIndex++, child._key === pointChildKey && child._key === endChildKey)
97
- return point.offset > endPoint.offset;
98
- if (child._key === pointChildKey && (pointChildIndex = childIndex), child._key === endChildKey && (endChildIndex = childIndex), pointChildIndex !== void 0 && endChildIndex !== void 0)
99
- break;
100
- }
101
- return pointChildIndex === void 0 || endChildIndex === void 0 ? !1 : pointChildIndex > endChildIndex;
102
- };
103
- }
104
- function isPointBeforeSelection(point) {
105
- return (snapshot) => {
106
- if (!snapshot.context.selection)
107
- return !1;
108
- const startPoint = util_getTextBlockText.getSelectionStartPoint(snapshot.context.selection), startBlockKey = util_getTextBlockText.getBlockKeyFromSelectionPoint(startPoint), startChildKey = util_getTextBlockText.getChildKeyFromSelectionPoint(startPoint), pointBlockKey = util_getTextBlockText.getBlockKeyFromSelectionPoint(point), pointChildKey = util_getTextBlockText.getChildKeyFromSelectionPoint(point);
109
- if (!pointBlockKey || !startBlockKey)
110
- return !1;
111
- const startBlockIndex = snapshot.blockIndexMap.get(startBlockKey), pointBlockIndex = snapshot.blockIndexMap.get(pointBlockKey);
112
- if (startBlockIndex === void 0 || pointBlockIndex === void 0)
113
- return !1;
114
- if (pointBlockIndex < startBlockIndex)
115
- return !0;
116
- if (pointBlockIndex > startBlockIndex)
117
- return !1;
118
- const pointBlock = snapshot.context.value.at(pointBlockIndex);
119
- if (!pointBlock || !schema.isTextBlock(snapshot.context, pointBlock))
120
- return !1;
121
- let pointChildIndex, startChildIndex, childIndex = -1;
122
- for (const child of pointBlock.children) {
123
- if (childIndex++, child._key === pointChildKey && child._key === startChildKey)
124
- return point.offset < startPoint.offset;
125
- if (child._key === pointChildKey && (pointChildIndex = childIndex), child._key === startChildKey && (startChildIndex = childIndex), pointChildIndex !== void 0 && startChildIndex !== void 0)
126
- break;
127
- }
128
- return pointChildIndex === void 0 || startChildIndex === void 0 ? !1 : pointChildIndex < startChildIndex;
129
- };
130
- }
131
- function isOverlappingSelection(selection) {
132
- return (snapshot) => {
133
- if (!selection || !snapshot.context.selection)
134
- return !1;
135
- const selectionStartPoint = selector_getSelectionText.getSelectionStartPoint({
136
- context: {
137
- ...snapshot.context,
138
- selection
139
- }
140
- }), selectionEndPoint = getSelectionEndPoint({
141
- context: {
142
- ...snapshot.context,
143
- selection
144
- }
145
- }), originalSelectionStartPoint = selector_getSelectionText.getSelectionStartPoint(snapshot), originalSelectionEndPoint = getSelectionEndPoint(snapshot);
146
- if (!selectionStartPoint || !selectionEndPoint || !originalSelectionStartPoint || !originalSelectionEndPoint)
147
- return !1;
148
- const startPointEqualToOriginalStartPoint = util_isEmptyTextBlock.isEqualSelectionPoints(selectionStartPoint, originalSelectionStartPoint), endPointEqualToOriginalEndPoint = util_isEmptyTextBlock.isEqualSelectionPoints(selectionEndPoint, originalSelectionEndPoint);
149
- if (startPointEqualToOriginalStartPoint && endPointEqualToOriginalEndPoint)
150
- return !0;
151
- const startPointBeforeSelection = isPointBeforeSelection(selectionStartPoint)(snapshot), startPointAfterSelection = isPointAfterSelection(selectionStartPoint)(snapshot), endPointBeforeSelection = isPointBeforeSelection(selectionEndPoint)(snapshot), endPointAfterSelection = isPointAfterSelection(selectionEndPoint)(snapshot), originalStartPointBeforeStartPoint = isPointBeforeSelection(originalSelectionStartPoint)({
152
- ...snapshot,
153
- context: {
154
- ...snapshot.context,
155
- selection: {
156
- anchor: selectionStartPoint,
157
- focus: selectionStartPoint
158
- }
159
- }
160
- }), originalStartPointAfterStartPoint = isPointAfterSelection(originalSelectionStartPoint)({
161
- ...snapshot,
162
- context: {
163
- ...snapshot.context,
164
- selection: {
165
- anchor: selectionStartPoint,
166
- focus: selectionStartPoint
167
- }
168
- }
169
- }), originalEndPointBeforeEndPoint = isPointBeforeSelection(originalSelectionEndPoint)({
170
- ...snapshot,
171
- context: {
172
- ...snapshot.context,
173
- selection: {
174
- anchor: selectionEndPoint,
175
- focus: selectionEndPoint
176
- }
177
- }
178
- }), originalEndPointAfterEndPoint = isPointAfterSelection(originalSelectionEndPoint)({
179
- ...snapshot,
180
- context: {
181
- ...snapshot.context,
182
- selection: {
183
- anchor: selectionEndPoint,
184
- focus: selectionEndPoint
185
- }
186
- }
187
- }), startPointEqualToOriginalEndPoint = util_isEmptyTextBlock.isEqualSelectionPoints(selectionStartPoint, originalSelectionEndPoint), endPointEqualToOriginalStartPoint = util_isEmptyTextBlock.isEqualSelectionPoints(selectionEndPoint, originalSelectionStartPoint);
188
- return !endPointEqualToOriginalStartPoint && !startPointEqualToOriginalEndPoint && !originalStartPointBeforeStartPoint && !originalStartPointAfterStartPoint && !originalEndPointBeforeEndPoint && !originalEndPointAfterEndPoint || endPointBeforeSelection && !endPointEqualToOriginalStartPoint || startPointAfterSelection && !startPointEqualToOriginalEndPoint ? !1 : !originalStartPointBeforeStartPoint && originalStartPointAfterStartPoint && !originalEndPointBeforeEndPoint && originalEndPointAfterEndPoint ? !endPointEqualToOriginalStartPoint : originalStartPointBeforeStartPoint && !originalStartPointAfterStartPoint && originalEndPointBeforeEndPoint && !originalEndPointAfterEndPoint ? !startPointEqualToOriginalEndPoint : !startPointAfterSelection || !startPointBeforeSelection || !endPointAfterSelection || !endPointBeforeSelection;
189
- };
190
- }
191
- const isSelectingEntireBlocks = (snapshot) => {
192
- if (!snapshot.context.selection)
193
- return !1;
194
- 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);
195
- if (!startBlock || !endBlock)
196
- return !1;
197
- const startBlockStartPoint = util_getTextBlockText.getBlockStartPoint({
198
- context: snapshot.context,
199
- block: startBlock
200
- }), endBlockEndPoint = util_isEmptyTextBlock.getBlockEndPoint({
201
- context: snapshot.context,
202
- block: endBlock
203
- });
204
- return util_isEmptyTextBlock.isEqualSelectionPoints(startBlockStartPoint, startPoint) && util_isEmptyTextBlock.isEqualSelectionPoints(endBlockEndPoint, endPoint);
205
- };
206
- function isBlockPath(path) {
207
- const firstSegment = path.at(0);
208
- return path.length === 1 && firstSegment !== void 0 && isRecord(firstSegment) && "_key" in firstSegment && typeof firstSegment._key == "string";
209
- }
210
- function isRecord(value) {
211
- return !!value && (typeof value == "object" || typeof value == "function");
212
- }
213
- function isSelectionExpanded(selection) {
214
- return selection ? !util_isEmptyTextBlock.isSelectionCollapsed(selection) : !1;
215
- }
216
- const getNextSpan = (snapshot) => {
217
- const selectionEndBlock = getSelectionEndBlock(snapshot), selectionEndPoint = getSelectionEndPoint(snapshot);
218
- if (!selectionEndBlock || !selectionEndPoint || !schema.isTextBlock(snapshot.context, selectionEndBlock.node))
219
- return;
220
- const selectionEndPointChildKey = util_getTextBlockText.getChildKeyFromSelectionPoint(selectionEndPoint);
221
- let endPointChildFound = !1, nextSpan;
222
- for (const child of selectionEndBlock.node.children) {
223
- if (child._key === selectionEndPointChildKey) {
224
- endPointChildFound = !0;
225
- continue;
226
- }
227
- if (schema.isSpan(snapshot.context, child) && endPointChildFound) {
228
- nextSpan = {
229
- node: child,
230
- path: [...selectionEndBlock.path, "children", {
231
- _key: child._key
232
- }]
233
- };
234
- break;
235
- }
236
- }
237
- return nextSpan;
238
- }, getPreviousSpan = (snapshot) => {
239
- const selectionStartBlock = getSelectionStartBlock(snapshot), selectionStartPoint = selector_getSelectionText.getSelectionStartPoint(snapshot);
240
- if (!selectionStartBlock || !selectionStartPoint || !schema.isTextBlock(snapshot.context, selectionStartBlock.node))
241
- return;
242
- const selectionStartPointChildKey = util_getTextBlockText.getChildKeyFromSelectionPoint(selectionStartPoint);
243
- let previousSpan;
244
- for (const child of selectionStartBlock.node.children) {
245
- if (child._key === selectionStartPointChildKey)
246
- break;
247
- schema.isSpan(snapshot.context, child) && (previousSpan = {
248
- node: child,
249
- path: [...selectionStartBlock.path, "children", {
250
- _key: child._key
251
- }]
252
- });
253
- }
254
- return previousSpan;
255
- }, getSelectedSpans = (snapshot) => {
256
- if (!snapshot.context.selection)
257
- return [];
258
- const selectedSpans = [], startPoint = selector_getSelectionText.getSelectionStartPoint(snapshot), endPoint = getSelectionEndPoint(snapshot);
259
- if (!startPoint || !endPoint)
260
- return selectedSpans;
261
- const startBlockKey = util_getTextBlockText.getBlockKeyFromSelectionPoint(startPoint), endBlockKey = util_getTextBlockText.getBlockKeyFromSelectionPoint(endPoint), startSpanKey = util_getTextBlockText.getChildKeyFromSelectionPoint(startPoint), endSpanKey = util_getTextBlockText.getChildKeyFromSelectionPoint(endPoint);
262
- if (!startBlockKey || !endBlockKey)
263
- return selectedSpans;
264
- const startBlockIndex = snapshot.blockIndexMap.get(startBlockKey), endBlockIndex = snapshot.blockIndexMap.get(endBlockKey);
265
- if (startBlockIndex === void 0 || endBlockIndex === void 0)
266
- return selectedSpans;
267
- const slicedValue = snapshot.context.value.slice(startBlockIndex, endBlockIndex + 1);
268
- let startBlockFound = !1;
269
- for (const block of slicedValue)
270
- if (block._key === startBlockKey && (startBlockFound = !0), !!schema.isTextBlock(snapshot.context, block)) {
271
- if (block._key === startBlockKey) {
272
- for (const child of block.children)
273
- if (schema.isSpan(snapshot.context, child)) {
274
- if (startSpanKey && child._key === startSpanKey) {
275
- if (startPoint.offset < child.text.length && selectedSpans.push({
276
- node: child,
277
- path: [{
278
- _key: block._key
279
- }, "children", {
280
- _key: child._key
281
- }]
282
- }), startSpanKey === endSpanKey)
283
- break;
284
- continue;
285
- }
286
- if (endSpanKey && child._key === endSpanKey) {
287
- endPoint.offset > 0 && selectedSpans.push({
288
- node: child,
289
- path: [{
290
- _key: block._key
291
- }, "children", {
292
- _key: child._key
293
- }]
294
- });
295
- break;
296
- }
297
- selectedSpans.length > 0 && selectedSpans.push({
298
- node: child,
299
- path: [{
300
- _key: block._key
301
- }, "children", {
302
- _key: child._key
303
- }]
304
- });
305
- }
306
- if (startBlockKey === endBlockKey)
307
- break;
308
- continue;
309
- }
310
- if (block._key === endBlockKey) {
311
- for (const child of block.children)
312
- if (schema.isSpan(snapshot.context, child)) {
313
- if (endSpanKey && child._key === endSpanKey) {
314
- endPoint.offset > 0 && selectedSpans.push({
315
- node: child,
316
- path: [{
317
- _key: block._key
318
- }, "children", {
319
- _key: child._key
320
- }]
321
- });
322
- break;
323
- }
324
- selectedSpans.push({
325
- node: child,
326
- path: [{
327
- _key: block._key
328
- }, "children", {
329
- _key: child._key
330
- }]
331
- });
332
- }
333
- break;
334
- }
335
- if (startBlockFound)
336
- for (const child of block.children)
337
- schema.isSpan(snapshot.context, child) && selectedSpans.push({
338
- node: child,
339
- path: [{
340
- _key: block._key
341
- }, "children", {
342
- _key: child._key
343
- }]
344
- });
345
- }
346
- return selectedSpans;
347
- }, getMarkState = (snapshot) => {
348
- if (!snapshot.context.selection)
349
- return;
350
- let selection = snapshot.context.selection;
351
- if (!selector_getSelectionText.getFocusTextBlock(snapshot))
352
- return;
353
- if (isBlockPath(selection.anchor.path)) {
354
- const spanSelectionPoint = util_getTextBlockText.blockOffsetToSpanSelectionPoint({
355
- context: snapshot.context,
356
- blockOffset: {
357
- path: selection.anchor.path,
358
- offset: selection.anchor.offset
359
- },
360
- direction: selection.backward ? "backward" : "forward"
361
- });
362
- selection = spanSelectionPoint ? {
363
- ...selection,
364
- anchor: spanSelectionPoint
365
- } : selection;
366
- }
367
- if (isBlockPath(selection.focus.path)) {
368
- const spanSelectionPoint = util_getTextBlockText.blockOffsetToSpanSelectionPoint({
369
- context: snapshot.context,
370
- blockOffset: {
371
- path: selection.focus.path,
372
- offset: selection.focus.offset
373
- },
374
- direction: selection.backward ? "backward" : "forward"
375
- });
376
- selection = spanSelectionPoint ? {
377
- ...selection,
378
- focus: spanSelectionPoint
379
- } : selection;
380
- }
381
- const focusSpan = selector_getSelectionText.getFocusSpan({
382
- ...snapshot,
383
- context: {
384
- ...snapshot.context,
385
- selection
386
- }
387
- });
388
- if (!focusSpan)
389
- return;
390
- if (isSelectionExpanded(selection)) {
391
- const selectedSpans = getSelectedSpans({
392
- ...snapshot,
393
- context: {
394
- ...snapshot.context,
395
- selection
396
- }
397
- });
398
- let index = 0, marks2 = [];
399
- for (const span of selectedSpans) {
400
- if (index === 0)
401
- marks2 = span.node.marks ?? [];
402
- else {
403
- if (span.node.marks?.length === 0) {
404
- marks2 = [];
405
- continue;
406
- }
407
- marks2 = marks2.filter((mark) => (span.node.marks ?? []).some((spanMark) => spanMark === mark));
408
- }
409
- index++;
410
- }
411
- return {
412
- state: "unchanged",
413
- marks: marks2
414
- };
415
- }
416
- const decorators = snapshot.context.schema.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({
417
- ...snapshot,
418
- context: {
419
- ...snapshot.context,
420
- selection
421
- }
422
- }), nextSpan = getNextSpan({
423
- ...snapshot,
424
- context: {
425
- ...snapshot.context,
426
- selection
427
- }
428
- }), 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));
429
- if (spanHasAnnotations && !spanIsEmpty) {
430
- if (atTheBeginningOfSpan) {
431
- if (previousSpanHasSameMarks)
432
- return {
433
- state: "changed",
434
- previousMarks: marks,
435
- marks: previousSpan?.node.marks ?? []
436
- };
437
- if (previousSpanHasSameAnnotations)
438
- return {
439
- state: "changed",
440
- previousMarks: marks,
441
- marks: previousSpan?.node.marks ?? []
442
- };
443
- if (previousSpanHasSameAnnotation)
444
- return {
445
- state: "unchanged",
446
- marks: focusSpan.node.marks ?? []
447
- };
448
- if (!previousSpan)
449
- return {
450
- state: "changed",
451
- previousMarks: marks,
452
- marks: []
453
- };
454
- }
455
- if (atTheEndOfSpan) {
456
- if (!nextSpan)
457
- return {
458
- state: "changed",
459
- previousMarks: marks,
460
- marks: []
461
- };
462
- if (nextSpanAnnotations.length > 0 && !nextSpanSharesSomeAnnotations)
463
- return {
464
- state: "changed",
465
- previousMarks: marks,
466
- marks: []
467
- };
468
- if (nextSpanSharesSomeAnnotations && nextSpanAnnotations.length < spanAnnotations.length || !nextSpanSharesSomeAnnotations)
469
- return {
470
- state: "changed",
471
- previousMarks: marks,
472
- marks: nextSpan?.node.marks ?? []
473
- };
474
- }
475
- }
476
- return atTheBeginningOfSpan && !spanIsEmpty && previousSpan ? previousSpanHasAnnotations ? {
477
- state: "changed",
478
- marks,
479
- previousMarks: previousSpan?.node.marks ?? []
480
- } : {
481
- state: "changed",
482
- previousMarks: marks,
483
- marks: (previousSpan?.node.marks ?? []).filter((mark) => decorators.includes(mark))
484
- } : {
485
- state: "unchanged",
486
- marks
487
- };
488
- };
489
- function getActiveDecorators(snapshot) {
490
- const schema2 = snapshot.context.schema, decoratorState = snapshot.decoratorState, markState = getMarkState(snapshot), decorators = schema2.decorators.map((decorator) => decorator.name);
491
- let activeDecorators = (markState?.marks ?? []).filter((mark) => decorators.includes(mark));
492
- for (const decorator in decoratorState)
493
- decoratorState[decorator] === !1 ? activeDecorators = activeDecorators.filter((activeDecorator) => activeDecorator !== decorator) : decoratorState[decorator] === !0 && (activeDecorators.includes(decorator) || activeDecorators.push(decorator));
494
- return activeDecorators;
495
- }
496
- const getTrimmedSelection = (snapshot) => {
497
- if (!snapshot.context.selection)
498
- return snapshot.context.selection;
499
- const startPoint = util_getTextBlockText.getSelectionStartPoint(snapshot.context.selection), endPoint = util_getTextBlockText.getSelectionEndPoint(snapshot.context.selection), startBlockKey = util_getTextBlockText.getBlockKeyFromSelectionPoint(startPoint), startChildKey = util_getTextBlockText.getChildKeyFromSelectionPoint(startPoint), endBlockKey = util_getTextBlockText.getBlockKeyFromSelectionPoint(endPoint), endChildKey = util_getTextBlockText.getChildKeyFromSelectionPoint(endPoint);
500
- if (!startBlockKey || !endBlockKey)
501
- return snapshot.context.selection;
502
- const startBlockIndex = snapshot.blockIndexMap.get(startBlockKey), endBlockIndex = snapshot.blockIndexMap.get(endBlockKey);
503
- if (startBlockIndex === void 0 || endBlockIndex === void 0)
504
- return snapshot.context.selection;
505
- const slicedValue = snapshot.context.value.slice(startBlockIndex, endBlockIndex + 1);
506
- let startBlockFound = !1, adjustedStartPoint, trimStartPoint = !1, adjustedEndPoint, trimEndPoint = !1, previousPotentialEndpoint;
507
- for (const block of slicedValue)
508
- if (!(block._key === startBlockKey && (startBlockFound = !0, schema.isTextBlock(snapshot.context, block) && util_isEmptyTextBlock.isEmptyTextBlock(snapshot.context, block))) && startBlockFound && schema.isTextBlock(snapshot.context, block)) {
509
- if (block._key === endBlockKey && util_isEmptyTextBlock.isEmptyTextBlock(snapshot.context, block))
510
- break;
511
- for (const child of block.children) {
512
- if (child._key === endChildKey && (!schema.isSpan(snapshot.context, child) || endPoint.offset === 0)) {
513
- adjustedEndPoint = previousPotentialEndpoint ? {
514
- path: [{
515
- _key: previousPotentialEndpoint.blockKey
516
- }, "children", {
517
- _key: previousPotentialEndpoint.span._key
518
- }],
519
- offset: previousPotentialEndpoint.span.text.length
520
- } : void 0, trimEndPoint = !0;
521
- break;
522
- }
523
- if (trimStartPoint) {
524
- const lonelySpan = schema.isSpan(snapshot.context, child) && block.children.length === 1;
525
- (schema.isSpan(snapshot.context, child) && child.text.length > 0 || lonelySpan) && (adjustedStartPoint = {
526
- path: [{
527
- _key: block._key
528
- }, "children", {
529
- _key: child._key
530
- }],
531
- offset: 0
532
- }, previousPotentialEndpoint = {
533
- blockKey: block._key,
534
- span: child
535
- }, trimStartPoint = !1);
536
- continue;
537
- }
538
- if (child._key === startChildKey) {
539
- if (!schema.isSpan(snapshot.context, child)) {
540
- trimStartPoint = !0;
541
- continue;
542
- }
543
- if (startPoint.offset === child.text.length) {
544
- trimStartPoint = !0, previousPotentialEndpoint = child.text.length > 0 ? {
545
- blockKey: block._key,
546
- span: child
547
- } : previousPotentialEndpoint;
548
- continue;
549
- }
550
- }
551
- previousPotentialEndpoint = schema.isSpan(snapshot.context, child) && child.text.length > 0 ? {
552
- blockKey: block._key,
553
- span: child
554
- } : previousPotentialEndpoint;
555
- }
556
- if (block._key === endBlockKey)
557
- break;
558
- }
559
- const trimmedSelection = snapshot.context.selection.backward ? {
560
- anchor: trimEndPoint && adjustedEndPoint ? adjustedEndPoint : endPoint,
561
- focus: adjustedStartPoint ?? startPoint,
562
- backward: !0
563
- } : {
564
- anchor: adjustedStartPoint ?? startPoint,
565
- focus: trimEndPoint && adjustedEndPoint ? adjustedEndPoint : endPoint
566
- };
567
- if (selector_getSelectionText.isSelectionCollapsed({
568
- context: {
569
- ...snapshot.context,
570
- selection: trimmedSelection
571
- }
572
- })) {
573
- const focusTextBlock = selector_getSelectionText.getFocusTextBlock({
574
- ...snapshot,
575
- context: {
576
- ...snapshot.context,
577
- selection: trimmedSelection
578
- }
579
- });
580
- if (focusTextBlock && !util_isEmptyTextBlock.isEmptyTextBlock(snapshot.context, focusTextBlock.node))
581
- return null;
582
- }
583
- return trimmedSelection;
584
- }, getActiveAnnotations = (snapshot) => {
585
- if (!snapshot.context.selection)
586
- return [];
587
- const selectedBlocks = getSelectedBlocks(snapshot), activeAnnotations = (getMarkState(snapshot)?.marks ?? []).filter((mark) => !snapshot.context.schema.decorators.map((decorator) => decorator.name).includes(mark));
588
- return selectedBlocks.flatMap((block) => schema.isTextBlock(snapshot.context, block.node) ? block.node.markDefs ?? [] : []).filter((markDef) => activeAnnotations.includes(markDef._key));
589
- }, getActiveListItem = (snapshot) => {
590
- if (!snapshot.context.selection)
591
- return;
592
- const selectedTextBlocks = getSelectedBlocks(snapshot).map((block) => block.node).filter((block) => schema.isTextBlock(snapshot.context, block)), firstTextBlock = selectedTextBlocks.at(0);
593
- if (!firstTextBlock)
594
- return;
595
- const firstListItem = firstTextBlock.listItem;
596
- if (firstListItem && selectedTextBlocks.every((block) => block.listItem === firstListItem))
597
- return firstListItem;
598
- }, getActiveStyle = (snapshot) => {
599
- if (!snapshot.context.selection)
600
- return;
601
- const selectedTextBlocks = getSelectedBlocks(snapshot).map((block) => block.node).filter((block) => schema.isTextBlock(snapshot.context, block)), firstTextBlock = selectedTextBlocks.at(0);
602
- if (!firstTextBlock)
603
- return;
604
- const firstStyle = firstTextBlock.style;
605
- if (firstStyle && selectedTextBlocks.every((block) => block.style === firstStyle))
606
- return firstStyle;
607
- }, getNextInlineObject = (snapshot) => {
608
- const focusTextBlock = selector_getSelectionText.getFocusTextBlock(snapshot), selectionEndPoint = getSelectionEndPoint(snapshot), selectionEndPointChildKey = selectionEndPoint && types.isKeySegment(selectionEndPoint.path[2]) ? selectionEndPoint.path[2]._key : void 0;
609
- if (!focusTextBlock || !selectionEndPointChildKey)
610
- return;
611
- let endPointChildFound = !1, inlineObject;
612
- for (const child of focusTextBlock.node.children) {
613
- if (child._key === selectionEndPointChildKey) {
614
- endPointChildFound = !0;
615
- continue;
616
- }
617
- if (!schema.isSpan(snapshot.context, child) && endPointChildFound) {
618
- inlineObject = {
619
- node: child,
620
- path: [...focusTextBlock.path, "children", {
621
- _key: child._key
622
- }]
623
- };
624
- break;
625
- }
626
- }
627
- return inlineObject;
628
- }, getCaretWordSelection = (snapshot) => {
629
- if (!snapshot.context.selection || !selector_getSelectionText.isSelectionCollapsed(snapshot))
630
- return null;
631
- const focusTextBlock = selector_getSelectionText.getFocusTextBlock(snapshot), selectionStartPoint = selector_getSelectionText.getSelectionStartPoint(snapshot), selectionStartOffset = selectionStartPoint ? util_getTextBlockText.spanSelectionPointToBlockOffset({
632
- context: snapshot.context,
633
- selectionPoint: selectionStartPoint
634
- }) : void 0;
635
- if (!focusTextBlock || !selectionStartPoint || !selectionStartOffset)
636
- return null;
637
- const previousInlineObject = selector_getSelectionText.getPreviousInlineObject(snapshot), blockStartPoint = util_getTextBlockText.getBlockStartPoint({
638
- context: snapshot.context,
639
- block: focusTextBlock
640
- }), textDirectlyBefore = selector_getSelectionText.getSelectionText({
641
- ...snapshot,
642
- context: {
643
- ...snapshot.context,
644
- selection: {
645
- anchor: previousInlineObject ? {
646
- path: previousInlineObject.path,
647
- offset: 0
648
- } : blockStartPoint,
649
- focus: selectionStartPoint
650
- }
651
- }
652
- }).split(/\s+/).at(-1), nextInlineObject = getNextInlineObject(snapshot), blockEndPoint = util_isEmptyTextBlock.getBlockEndPoint({
653
- context: snapshot.context,
654
- block: focusTextBlock
655
- }), textDirectlyAfter = selector_getSelectionText.getSelectionText({
656
- ...snapshot,
657
- context: {
658
- ...snapshot.context,
659
- selection: {
660
- anchor: selectionStartPoint,
661
- focus: nextInlineObject ? {
662
- path: nextInlineObject.path,
663
- offset: 0
664
- } : blockEndPoint
665
- }
666
- }
667
- }).split(/\s+/).at(0);
668
- if ((textDirectlyBefore === void 0 || textDirectlyBefore === "") && (textDirectlyAfter === void 0 || textDirectlyAfter === ""))
669
- return null;
670
- const caretWordStartOffset = textDirectlyBefore ? {
671
- ...selectionStartOffset,
672
- offset: selectionStartOffset.offset - textDirectlyBefore.length
673
- } : selectionStartOffset, caretWordEndOffset = textDirectlyAfter ? {
674
- ...selectionStartOffset,
675
- offset: selectionStartOffset.offset + textDirectlyAfter.length
676
- } : selectionStartOffset, caretWordStartSelectionPoint = util_getTextBlockText.blockOffsetToSpanSelectionPoint({
677
- context: snapshot.context,
678
- blockOffset: caretWordStartOffset,
679
- direction: "backward"
680
- }), caretWordEndSelectionPoint = util_getTextBlockText.blockOffsetToSpanSelectionPoint({
681
- context: snapshot.context,
682
- blockOffset: caretWordEndOffset,
683
- direction: "forward"
684
- });
685
- if (!caretWordStartSelectionPoint || !caretWordEndSelectionPoint)
686
- return null;
687
- const caretWordSelection = {
688
- anchor: caretWordStartSelectionPoint,
689
- focus: caretWordEndSelectionPoint
690
- };
691
- return selector_getSelectionText.isSelectionExpanded({
692
- context: {
693
- ...snapshot.context,
694
- selection: caretWordSelection
695
- }
696
- }) ? caretWordSelection : null;
697
- }, getFirstBlock = (snapshot) => {
698
- const node = snapshot.context.value[0];
699
- return node ? {
700
- node,
701
- path: [{
702
- _key: node._key
703
- }]
704
- } : void 0;
705
- }, getFocusBlockObject = (snapshot) => {
706
- const focusBlock = selector_getSelectionText.getFocusBlock(snapshot);
707
- return focusBlock && !schema.isTextBlock(snapshot.context, focusBlock.node) ? {
708
- node: focusBlock.node,
709
- path: focusBlock.path
710
- } : void 0;
711
- }, getFocusListBlock = (snapshot) => {
712
- const focusTextBlock = selector_getSelectionText.getFocusTextBlock(snapshot);
713
- return focusTextBlock && util_getTextBlockText.isListBlock(snapshot.context, focusTextBlock.node) ? {
714
- node: focusTextBlock.node,
715
- path: focusTextBlock.path
716
- } : void 0;
717
- }, getLastBlock = (snapshot) => {
718
- const node = snapshot.context.value[snapshot.context.value.length - 1] ? snapshot.context.value[snapshot.context.value.length - 1] : void 0;
719
- return node ? {
720
- node,
721
- path: [{
722
- _key: node._key
723
- }]
724
- } : void 0;
725
- }, getNextBlock = (snapshot) => {
726
- const selectionEndBlock = getSelectionEndBlock(snapshot);
727
- if (!selectionEndBlock)
728
- return;
729
- const index = snapshot.blockIndexMap.get(selectionEndBlock.node._key);
730
- if (index === void 0 || index === snapshot.context.value.length - 1)
731
- return;
732
- const nextBlock = snapshot.context.value.at(index + 1);
733
- return nextBlock ? {
734
- node: nextBlock,
735
- path: [{
736
- _key: nextBlock._key
737
- }]
738
- } : void 0;
739
- }, getPreviousBlock = (snapshot) => {
740
- const selectionStartBlock = getSelectionStartBlock(snapshot);
741
- if (!selectionStartBlock)
742
- return;
743
- const index = snapshot.blockIndexMap.get(selectionStartBlock.node._key);
744
- if (index === void 0 || index === 0)
745
- return;
746
- const previousBlock = snapshot.context.value.at(index - 1);
747
- return previousBlock ? {
748
- node: previousBlock,
749
- path: [{
750
- _key: previousBlock._key
751
- }]
752
- } : void 0;
753
- }, getSelectedTextBlocks = (snapshot) => {
754
- if (!snapshot.context.selection)
755
- return [];
756
- const selectedTextBlocks = [], startPoint = util_getTextBlockText.getSelectionStartPoint(snapshot.context.selection), endPoint = util_getTextBlockText.getSelectionEndPoint(snapshot.context.selection), startBlockKey = util_getTextBlockText.getBlockKeyFromSelectionPoint(startPoint), endBlockKey = util_getTextBlockText.getBlockKeyFromSelectionPoint(endPoint);
757
- if (!startBlockKey || !endBlockKey)
758
- return selectedTextBlocks;
759
- const startBlockIndex = snapshot.blockIndexMap.get(startBlockKey), endBlockIndex = snapshot.blockIndexMap.get(endBlockKey);
760
- if (startBlockIndex === void 0 || endBlockIndex === void 0)
761
- return selectedTextBlocks;
762
- const slicedValue = snapshot.context.value.slice(startBlockIndex, endBlockIndex + 1);
763
- for (const block of slicedValue) {
764
- if (block._key === startBlockKey) {
765
- if (schema.isTextBlock(snapshot.context, block) && selectedTextBlocks.push({
766
- node: block,
767
- path: [{
768
- _key: block._key
769
- }]
770
- }), startBlockKey === endBlockKey)
771
- break;
772
- continue;
773
- }
774
- if (block._key === endBlockKey) {
775
- schema.isTextBlock(snapshot.context, block) && selectedTextBlocks.push({
776
- node: block,
777
- path: [{
778
- _key: block._key
779
- }]
780
- });
781
- break;
782
- }
783
- selectedTextBlocks.length > 0 && schema.isTextBlock(snapshot.context, block) && selectedTextBlocks.push({
784
- node: block,
785
- path: [{
786
- _key: block._key
787
- }]
788
- });
789
- }
790
- return selectedTextBlocks;
791
- };
792
- function getActiveAnnotationsMarks(snapshot) {
793
- const schema2 = snapshot.context.schema;
794
- return (getMarkState(snapshot)?.marks ?? []).filter((mark) => !schema2.decorators.map((decorator) => decorator.name).includes(mark));
795
- }
796
- function isActiveAnnotation(annotation, options) {
797
- return (snapshot) => {
798
- if ((options?.mode ?? "full") === "partial")
799
- return selector_getSelectionText.getSelectedValue(snapshot).flatMap((block) => schema.isTextBlock(snapshot.context, block) ? block.markDefs ?? [] : []).some((markDef) => markDef._type === annotation);
800
- const selectionMarkDefs = getSelectedBlocks(snapshot).flatMap((block) => schema.isTextBlock(snapshot.context, block.node) ? block.node.markDefs ?? [] : []), activeAnnotations = getActiveAnnotationsMarks(snapshot);
801
- return selectionMarkDefs.filter((markDef) => markDef._type === annotation && activeAnnotations.includes(markDef._key)).length > 0;
802
- };
803
- }
804
- function isActiveDecorator(decorator) {
805
- return (snapshot) => {
806
- if (selector_getSelectionText.isSelectionExpanded(snapshot)) {
807
- const selectedSpans = getSelectedSpans(snapshot);
808
- return selectedSpans.length > 0 && selectedSpans.every((span) => span.node.marks?.includes(decorator));
809
- }
810
- return getActiveDecorators(snapshot).includes(decorator);
811
- };
812
- }
813
- function isActiveListItem(listItem) {
814
- return (snapshot) => getActiveListItem(snapshot) === listItem;
815
- }
816
- function isActiveStyle(style) {
817
- return (snapshot) => getActiveStyle(snapshot) === style;
818
- }
819
- function isAtTheEndOfBlock(block) {
820
- return (snapshot) => {
821
- if (!snapshot.context.selection || !selector_getSelectionText.isSelectionCollapsed(snapshot))
822
- return !1;
823
- const blockEndPoint = util_isEmptyTextBlock.getBlockEndPoint({
824
- context: snapshot.context,
825
- block
826
- });
827
- return util_isEmptyTextBlock.isEqualSelectionPoints(snapshot.context.selection.focus, blockEndPoint);
828
- };
829
- }
830
- function isAtTheStartOfBlock(block) {
831
- return (snapshot) => {
832
- if (!snapshot.context.selection || !selector_getSelectionText.isSelectionCollapsed(snapshot))
833
- return !1;
834
- const blockStartPoint = util_getTextBlockText.getBlockStartPoint({
835
- context: snapshot.context,
836
- block
837
- });
838
- return util_isEmptyTextBlock.isEqualSelectionPoints(snapshot.context.selection.focus, blockStartPoint);
839
- };
840
- }
841
- exports.getActiveAnnotations = getActiveAnnotations;
842
- exports.getActiveAnnotationsMarks = getActiveAnnotationsMarks;
843
- exports.getActiveDecorators = getActiveDecorators;
844
- exports.getActiveListItem = getActiveListItem;
845
- exports.getActiveStyle = getActiveStyle;
846
- exports.getCaretWordSelection = getCaretWordSelection;
847
- exports.getFirstBlock = getFirstBlock;
848
- exports.getFocusBlockObject = getFocusBlockObject;
849
- exports.getFocusInlineObject = getFocusInlineObject;
850
- exports.getFocusListBlock = getFocusListBlock;
851
- exports.getLastBlock = getLastBlock;
852
- exports.getMarkState = getMarkState;
853
- exports.getNextBlock = getNextBlock;
854
- exports.getNextInlineObject = getNextInlineObject;
855
- exports.getNextSpan = getNextSpan;
856
- exports.getPreviousBlock = getPreviousBlock;
857
- exports.getPreviousSpan = getPreviousSpan;
858
- exports.getSelectedBlocks = getSelectedBlocks;
859
- exports.getSelectedSpans = getSelectedSpans;
860
- exports.getSelectedTextBlocks = getSelectedTextBlocks;
861
- exports.getSelectionEndBlock = getSelectionEndBlock;
862
- exports.getSelectionEndPoint = getSelectionEndPoint;
863
- exports.getSelectionStartBlock = getSelectionStartBlock;
864
- exports.getTrimmedSelection = getTrimmedSelection;
865
- exports.isActiveAnnotation = isActiveAnnotation;
866
- exports.isActiveDecorator = isActiveDecorator;
867
- exports.isActiveListItem = isActiveListItem;
868
- exports.isActiveStyle = isActiveStyle;
869
- exports.isAtTheEndOfBlock = isAtTheEndOfBlock;
870
- exports.isAtTheStartOfBlock = isAtTheStartOfBlock;
871
- exports.isOverlappingSelection = isOverlappingSelection;
872
- exports.isPointAfterSelection = isPointAfterSelection;
873
- exports.isPointBeforeSelection = isPointBeforeSelection;
874
- exports.isSelectingEntireBlocks = isSelectingEntireBlocks;
875
- //# sourceMappingURL=selector.is-at-the-start-of-block.cjs.map