@portabletext/editor 1.48.7 → 1.48.8

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 (128) hide show
  1. package/lib/_chunks-cjs/behavior.core.cjs +20 -8
  2. package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
  3. package/lib/_chunks-cjs/behavior.markdown.cjs +5 -5
  4. package/lib/_chunks-cjs/behavior.markdown.cjs.map +1 -1
  5. package/lib/_chunks-cjs/editor-provider.cjs +121 -90
  6. package/lib/_chunks-cjs/editor-provider.cjs.map +1 -1
  7. package/lib/_chunks-cjs/selector.get-focus-inline-object.cjs +2 -2
  8. package/lib/_chunks-cjs/selector.get-focus-inline-object.cjs.map +1 -1
  9. package/lib/_chunks-cjs/selector.get-text-before.cjs +7 -4
  10. package/lib/_chunks-cjs/selector.get-text-before.cjs.map +1 -1
  11. package/lib/_chunks-cjs/selector.is-overlapping-selection.cjs +63 -47
  12. package/lib/_chunks-cjs/selector.is-overlapping-selection.cjs.map +1 -1
  13. package/lib/_chunks-cjs/selector.is-selecting-entire-blocks.cjs +29 -23
  14. package/lib/_chunks-cjs/selector.is-selecting-entire-blocks.cjs.map +1 -1
  15. package/lib/_chunks-cjs/util.merge-text-blocks.cjs +3 -3
  16. package/lib/_chunks-cjs/util.merge-text-blocks.cjs.map +1 -1
  17. package/lib/_chunks-cjs/util.selection-point-to-block-offset.cjs +15 -15
  18. package/lib/_chunks-cjs/util.selection-point-to-block-offset.cjs.map +1 -1
  19. package/lib/_chunks-cjs/util.slice-blocks.cjs +258 -38
  20. package/lib/_chunks-cjs/util.slice-blocks.cjs.map +1 -1
  21. package/lib/_chunks-es/behavior.core.js +21 -10
  22. package/lib/_chunks-es/behavior.core.js.map +1 -1
  23. package/lib/_chunks-es/behavior.markdown.js +5 -6
  24. package/lib/_chunks-es/behavior.markdown.js.map +1 -1
  25. package/lib/_chunks-es/editor-provider.js +75 -46
  26. package/lib/_chunks-es/editor-provider.js.map +1 -1
  27. package/lib/_chunks-es/selector.get-focus-inline-object.js +1 -2
  28. package/lib/_chunks-es/selector.get-focus-inline-object.js.map +1 -1
  29. package/lib/_chunks-es/selector.get-text-before.js +7 -4
  30. package/lib/_chunks-es/selector.get-text-before.js.map +1 -1
  31. package/lib/_chunks-es/selector.is-overlapping-selection.js +61 -46
  32. package/lib/_chunks-es/selector.is-overlapping-selection.js.map +1 -1
  33. package/lib/_chunks-es/selector.is-selecting-entire-blocks.js +30 -26
  34. package/lib/_chunks-es/selector.is-selecting-entire-blocks.js.map +1 -1
  35. package/lib/_chunks-es/util.merge-text-blocks.js +1 -1
  36. package/lib/_chunks-es/util.selection-point-to-block-offset.js +15 -16
  37. package/lib/_chunks-es/util.selection-point-to-block-offset.js.map +1 -1
  38. package/lib/_chunks-es/util.slice-blocks.js +258 -38
  39. package/lib/_chunks-es/util.slice-blocks.js.map +1 -1
  40. package/lib/behaviors/index.d.cts +2552 -251
  41. package/lib/behaviors/index.d.ts +2552 -251
  42. package/lib/index.cjs +45 -21
  43. package/lib/index.cjs.map +1 -1
  44. package/lib/index.d.cts +2350 -51
  45. package/lib/index.d.ts +2350 -51
  46. package/lib/index.js +44 -21
  47. package/lib/index.js.map +1 -1
  48. package/lib/plugins/index.cjs +16 -7
  49. package/lib/plugins/index.cjs.map +1 -1
  50. package/lib/plugins/index.d.cts +2324 -24
  51. package/lib/plugins/index.d.ts +2324 -24
  52. package/lib/plugins/index.js +16 -7
  53. package/lib/plugins/index.js.map +1 -1
  54. package/lib/selectors/index.cjs +5 -5
  55. package/lib/selectors/index.cjs.map +1 -1
  56. package/lib/selectors/index.d.cts +2321 -28
  57. package/lib/selectors/index.d.ts +2321 -28
  58. package/lib/selectors/index.js +5 -6
  59. package/lib/selectors/index.js.map +1 -1
  60. package/lib/utils/index.cjs +30 -24
  61. package/lib/utils/index.cjs.map +1 -1
  62. package/lib/utils/index.d.cts +2350 -44
  63. package/lib/utils/index.d.ts +2350 -44
  64. package/lib/utils/index.js +30 -24
  65. package/lib/utils/index.js.map +1 -1
  66. package/package.json +1 -2
  67. package/src/behavior-actions/behavior.action.block.unset.ts +1 -1
  68. package/src/behavior-actions/behavior.action.decorator.add.ts +20 -5
  69. package/src/behaviors/behavior.abstract.decorator.ts +1 -1
  70. package/src/behaviors/behavior.abstract.delete.ts +1 -1
  71. package/src/behaviors/behavior.abstract.insert.ts +2 -2
  72. package/src/behaviors/behavior.abstract.select.ts +16 -4
  73. package/src/behaviors/behavior.abstract.split.ts +9 -6
  74. package/src/behaviors/behavior.core.block-objects.ts +5 -5
  75. package/src/behaviors/behavior.core.insert-break.ts +16 -4
  76. package/src/behaviors/behavior.core.lists.ts +4 -6
  77. package/src/behaviors/behavior.decorator-pair.ts +13 -4
  78. package/src/behaviors/behavior.default.ts +1 -1
  79. package/src/behaviors/behavior.markdown.ts +5 -5
  80. package/src/converters/converter.portable-text.ts +1 -1
  81. package/src/converters/converter.text-html.ts +1 -1
  82. package/src/converters/converter.text-plain.ts +4 -4
  83. package/src/editor/plugins/__tests__/withEditableAPIGetFragment.test.tsx +10 -2
  84. package/src/editor/plugins/createWithSchemaTypes.ts +12 -19
  85. package/src/internal-utils/__tests__/dmpToOperations.test.ts +13 -12
  86. package/src/internal-utils/drag-selection.ts +16 -4
  87. package/src/internal-utils/event-position.ts +20 -8
  88. package/src/internal-utils/parse-blocks.ts +17 -5
  89. package/src/internal-utils/validateValue.ts +6 -6
  90. package/src/plugins/plugin.decorator-shortcut.ts +2 -2
  91. package/src/selectors/selector.get-active-annotations.ts +5 -2
  92. package/src/selectors/selector.get-active-list-item.ts +4 -3
  93. package/src/selectors/selector.get-active-style.ts +4 -3
  94. package/src/selectors/selector.get-anchor-text-block.ts +3 -6
  95. package/src/selectors/selector.get-block-offsets.ts +2 -2
  96. package/src/selectors/selector.get-caret-word-selection.ts +11 -5
  97. package/src/selectors/selector.get-selected-slice.ts +1 -1
  98. package/src/selectors/selector.get-selected-spans.ts +11 -15
  99. package/src/selectors/selector.get-selected-text-blocks.ts +3 -3
  100. package/src/selectors/selector.get-selection-text.ts +3 -3
  101. package/src/selectors/selector.get-text-before.ts +5 -2
  102. package/src/selectors/selector.get-trimmed-selection.ts +20 -14
  103. package/src/selectors/selector.is-active-annotation.ts +4 -2
  104. package/src/selectors/selector.is-active-decorator.test.ts +3 -3
  105. package/src/selectors/selector.is-at-the-end-of-block.ts +4 -1
  106. package/src/selectors/selector.is-at-the-start-of-block.ts +4 -1
  107. package/src/selectors/selector.is-point-after-selection.ts +7 -6
  108. package/src/selectors/selector.is-point-before-selection.ts +7 -6
  109. package/src/selectors/selector.is-selecting-entire-blocks.ts +8 -2
  110. package/src/selectors/selectors.ts +25 -28
  111. package/src/utils/util.block-offset-to-block-selection-point.ts +4 -4
  112. package/src/utils/util.block-offset-to-selection-point.ts +5 -5
  113. package/src/utils/util.block-offset.test.ts +219 -156
  114. package/src/utils/util.block-offset.ts +14 -17
  115. package/src/utils/util.block-offsets-to-selection.ts +5 -5
  116. package/src/utils/util.child-selection-point-to-block-offset.ts +7 -10
  117. package/src/utils/util.get-block-end-point.ts +15 -15
  118. package/src/utils/util.get-block-start-point.ts +13 -12
  119. package/src/utils/util.is-empty-text-block.ts +9 -8
  120. package/src/utils/util.selection-point-to-block-offset.ts +4 -4
  121. package/src/utils/util.slice-blocks.test.ts +178 -121
  122. package/src/utils/util.slice-blocks.ts +25 -24
  123. package/src/utils/util.split-text-block.ts +18 -12
  124. package/lib/_chunks-cjs/parse-blocks.cjs +0 -205
  125. package/lib/_chunks-cjs/parse-blocks.cjs.map +0 -1
  126. package/lib/_chunks-es/parse-blocks.js +0 -206
  127. package/lib/_chunks-es/parse-blocks.js.map +0 -1
  128. package/src/behavior-actions/behavior.guards.ts +0 -24
@@ -15,7 +15,10 @@ export function isAtTheEndOfBlock(block: {
15
15
  return false
16
16
  }
17
17
 
18
- const blockEndPoint = utils.getBlockEndPoint(block)
18
+ const blockEndPoint = utils.getBlockEndPoint({
19
+ context: snapshot.context,
20
+ block,
21
+ })
19
22
 
20
23
  return utils.isEqualSelectionPoints(
21
24
  snapshot.context.selection.focus,
@@ -15,7 +15,10 @@ export function isAtTheStartOfBlock(block: {
15
15
  return false
16
16
  }
17
17
 
18
- const blockStartPoint = utils.getBlockStartPoint(block)
18
+ const blockStartPoint = utils.getBlockStartPoint({
19
+ context: snapshot.context,
20
+ block,
21
+ })
19
22
 
20
23
  return utils.isEqualSelectionPoints(
21
24
  snapshot.context.selection.focus,
@@ -1,6 +1,7 @@
1
- import {isKeySegment, isPortableTextTextBlock} from '@sanity/types'
2
1
  import type {EditorSelector} from '../editor/editor-selector'
2
+ import {isTextBlock} from '../internal-utils/parse-blocks'
3
3
  import type {EditorSelectionPoint} from '../types/editor'
4
+ import {isKeyedSegment} from '../utils/util.is-keyed-segment'
4
5
  import {reverseSelection} from '../utils/util.reverse-selection'
5
6
 
6
7
  /**
@@ -18,17 +19,17 @@ export function isPointAfterSelection(
18
19
  ? reverseSelection(snapshot.context.selection)
19
20
  : snapshot.context.selection
20
21
 
21
- const pointBlockKey = isKeySegment(point.path[0])
22
+ const pointBlockKey = isKeyedSegment(point.path[0])
22
23
  ? point.path[0]._key
23
24
  : undefined
24
- const pointChildKey = isKeySegment(point.path[2])
25
+ const pointChildKey = isKeyedSegment(point.path[2])
25
26
  ? point.path[2]._key
26
27
  : undefined
27
28
 
28
- const endBlockKey = isKeySegment(selection.focus.path[0])
29
+ const endBlockKey = isKeyedSegment(selection.focus.path[0])
29
30
  ? selection.focus.path[0]._key
30
31
  : undefined
31
- const endChildKey = isKeySegment(selection.focus.path[2])
32
+ const endChildKey = isKeyedSegment(selection.focus.path[2])
32
33
  ? selection.focus.path[2]._key
33
34
  : undefined
34
35
 
@@ -47,7 +48,7 @@ export function isPointAfterSelection(
47
48
 
48
49
  // Both the point and the selection end in this block
49
50
 
50
- if (!isPortableTextTextBlock(block)) {
51
+ if (!isTextBlock(snapshot.context, block)) {
51
52
  break
52
53
  }
53
54
 
@@ -1,6 +1,7 @@
1
- import {isKeySegment, isPortableTextTextBlock} from '@sanity/types'
2
1
  import type {EditorSelector} from '../editor/editor-selector'
2
+ import {isTextBlock} from '../internal-utils/parse-blocks'
3
3
  import type {EditorSelectionPoint} from '../types/editor'
4
+ import {isKeyedSegment} from '../utils/util.is-keyed-segment'
4
5
  import {reverseSelection} from '../utils/util.reverse-selection'
5
6
 
6
7
  /**
@@ -18,17 +19,17 @@ export function isPointBeforeSelection(
18
19
  ? reverseSelection(snapshot.context.selection)
19
20
  : snapshot.context.selection
20
21
 
21
- const pointBlockKey = isKeySegment(point.path[0])
22
+ const pointBlockKey = isKeyedSegment(point.path[0])
22
23
  ? point.path[0]._key
23
24
  : undefined
24
- const pointChildKey = isKeySegment(point.path[2])
25
+ const pointChildKey = isKeyedSegment(point.path[2])
25
26
  ? point.path[2]._key
26
27
  : undefined
27
28
 
28
- const startBlockKey = isKeySegment(selection.anchor.path[0])
29
+ const startBlockKey = isKeyedSegment(selection.anchor.path[0])
29
30
  ? selection.anchor.path[0]._key
30
31
  : undefined
31
- const startChildKey = isKeySegment(selection.anchor.path[2])
32
+ const startChildKey = isKeyedSegment(selection.anchor.path[2])
32
33
  ? selection.anchor.path[2]._key
33
34
  : undefined
34
35
 
@@ -47,7 +48,7 @@ export function isPointBeforeSelection(
47
48
 
48
49
  // Both the point and the selection start in this block
49
50
 
50
- if (!isPortableTextTextBlock(block)) {
51
+ if (!isTextBlock(snapshot.context, block)) {
51
52
  break
52
53
  }
53
54
 
@@ -24,8 +24,14 @@ export const isSelectingEntireBlocks: EditorSelector<boolean> = (snapshot) => {
24
24
  return false
25
25
  }
26
26
 
27
- const startBlockStartPoint = utils.getBlockStartPoint(startBlock)
28
- const endBlockEndPoint = utils.getBlockEndPoint(endBlock)
27
+ const startBlockStartPoint = utils.getBlockStartPoint({
28
+ context: snapshot.context,
29
+ block: startBlock,
30
+ })
31
+ const endBlockEndPoint = utils.getBlockEndPoint({
32
+ context: snapshot.context,
33
+ block: endBlock,
34
+ })
29
35
 
30
36
  return (
31
37
  utils.isEqualSelectionPoints(startBlockStartPoint, startPoint) &&
@@ -1,16 +1,14 @@
1
- import {
2
- isKeySegment,
3
- isPortableTextSpan,
4
- isPortableTextTextBlock,
5
- type KeyedSegment,
6
- type PortableTextBlock,
7
- type PortableTextListBlock,
8
- type PortableTextObject,
9
- type PortableTextSpan,
10
- type PortableTextTextBlock,
1
+ import type {
2
+ KeyedSegment,
3
+ PortableTextBlock,
4
+ PortableTextListBlock,
5
+ PortableTextObject,
6
+ PortableTextSpan,
7
+ PortableTextTextBlock,
11
8
  } from '@sanity/types'
12
- import {createGuards} from '../behavior-actions/behavior.guards'
13
9
  import type {EditorSelector} from '../editor/editor-selector'
10
+ import {isListBlock, isSpan, isTextBlock} from '../internal-utils/parse-blocks'
11
+ import {isKeyedSegment} from '../utils/util.is-keyed-segment'
14
12
 
15
13
  /**
16
14
  * @public
@@ -19,7 +17,7 @@ export const getFocusBlock: EditorSelector<
19
17
  {node: PortableTextBlock; path: [KeyedSegment]} | undefined
20
18
  > = (snapshot) => {
21
19
  const key = snapshot.context.selection
22
- ? isKeySegment(snapshot.context.selection.focus.path[0])
20
+ ? isKeyedSegment(snapshot.context.selection.focus.path[0])
23
21
  ? snapshot.context.selection.focus.path[0]._key
24
22
  : undefined
25
23
  : undefined
@@ -37,11 +35,10 @@ export const getFocusBlock: EditorSelector<
37
35
  export const getFocusListBlock: EditorSelector<
38
36
  {node: PortableTextListBlock; path: [KeyedSegment]} | undefined
39
37
  > = (snapshot) => {
40
- const guards = createGuards(snapshot.context)
41
- const focusBlock = getFocusBlock(snapshot)
38
+ const focusTextBlock = getFocusTextBlock(snapshot)
42
39
 
43
- return focusBlock && guards.isListBlock(focusBlock.node)
44
- ? {node: focusBlock.node, path: focusBlock.path}
40
+ return focusTextBlock && isListBlock(snapshot.context, focusTextBlock.node)
41
+ ? {node: focusTextBlock.node, path: focusTextBlock.path}
45
42
  : undefined
46
43
  }
47
44
 
@@ -53,7 +50,7 @@ export const getFocusTextBlock: EditorSelector<
53
50
  > = (snapshot) => {
54
51
  const focusBlock = getFocusBlock(snapshot)
55
52
 
56
- return focusBlock && isPortableTextTextBlock(focusBlock.node)
53
+ return focusBlock && isTextBlock(snapshot.context, focusBlock.node)
57
54
  ? {node: focusBlock.node, path: focusBlock.path}
58
55
  : undefined
59
56
  }
@@ -66,7 +63,7 @@ export const getFocusBlockObject: EditorSelector<
66
63
  > = (snapshot) => {
67
64
  const focusBlock = getFocusBlock(snapshot)
68
65
 
69
- return focusBlock && !isPortableTextTextBlock(focusBlock.node)
66
+ return focusBlock && !isTextBlock(snapshot.context, focusBlock.node)
70
67
  ? {node: focusBlock.node, path: focusBlock.path}
71
68
  : undefined
72
69
  }
@@ -88,7 +85,7 @@ export const getFocusChild: EditorSelector<
88
85
  }
89
86
 
90
87
  const key = snapshot.context.selection
91
- ? isKeySegment(snapshot.context.selection.focus.path[2])
88
+ ? isKeyedSegment(snapshot.context.selection.focus.path[2])
92
89
  ? snapshot.context.selection.focus.path[2]._key
93
90
  : undefined
94
91
  : undefined
@@ -111,7 +108,7 @@ export const getFocusSpan: EditorSelector<
111
108
  > = (snapshot) => {
112
109
  const focusChild = getFocusChild(snapshot)
113
110
 
114
- return focusChild && isPortableTextSpan(focusChild.node)
111
+ return focusChild && isSpan(snapshot.context, focusChild.node)
115
112
  ? {node: focusChild.node, path: focusChild.path}
116
113
  : undefined
117
114
  }
@@ -153,17 +150,17 @@ export const getSelectedBlocks: EditorSelector<
153
150
  const selectedBlocks: Array<{node: PortableTextBlock; path: [KeyedSegment]}> =
154
151
  []
155
152
  const startKey = snapshot.context.selection.backward
156
- ? isKeySegment(snapshot.context.selection.focus.path[0])
153
+ ? isKeyedSegment(snapshot.context.selection.focus.path[0])
157
154
  ? snapshot.context.selection.focus.path[0]._key
158
155
  : undefined
159
- : isKeySegment(snapshot.context.selection.anchor.path[0])
156
+ : isKeyedSegment(snapshot.context.selection.anchor.path[0])
160
157
  ? snapshot.context.selection.anchor.path[0]._key
161
158
  : undefined
162
159
  const endKey = snapshot.context.selection.backward
163
- ? isKeySegment(snapshot.context.selection.anchor.path[0])
160
+ ? isKeyedSegment(snapshot.context.selection.anchor.path[0])
164
161
  ? snapshot.context.selection.anchor.path[0]._key
165
162
  : undefined
166
- : isKeySegment(snapshot.context.selection.focus.path[0])
163
+ : isKeyedSegment(snapshot.context.selection.focus.path[0])
167
164
  ? snapshot.context.selection.focus.path[0]._key
168
165
  : undefined
169
166
 
@@ -209,10 +206,10 @@ export const getSelectionStartBlock: EditorSelector<
209
206
  }
210
207
 
211
208
  const key = snapshot.context.selection.backward
212
- ? isKeySegment(snapshot.context.selection.focus.path[0])
209
+ ? isKeyedSegment(snapshot.context.selection.focus.path[0])
213
210
  ? snapshot.context.selection.focus.path[0]._key
214
211
  : undefined
215
- : isKeySegment(snapshot.context.selection.anchor.path[0])
212
+ : isKeyedSegment(snapshot.context.selection.anchor.path[0])
216
213
  ? snapshot.context.selection.anchor.path[0]._key
217
214
  : undefined
218
215
 
@@ -238,10 +235,10 @@ export const getSelectionEndBlock: EditorSelector<
238
235
  }
239
236
 
240
237
  const key = snapshot.context.selection.backward
241
- ? isKeySegment(snapshot.context.selection.anchor.path[0])
238
+ ? isKeyedSegment(snapshot.context.selection.anchor.path[0])
242
239
  ? snapshot.context.selection.anchor.path[0]._key
243
240
  : undefined
244
- : isKeySegment(snapshot.context.selection.focus.path[0])
241
+ : isKeyedSegment(snapshot.context.selection.focus.path[0])
245
242
  ? snapshot.context.selection.focus.path[0]._key
246
243
  : undefined
247
244
 
@@ -1,4 +1,4 @@
1
- import type {PortableTextBlock} from '@sanity/types'
1
+ import type {EditorContext} from '../editor/editor-snapshot'
2
2
  import type {BlockOffset} from '../types/block-offset'
3
3
  import type {EditorSelectionPoint} from '../types/editor'
4
4
 
@@ -6,15 +6,15 @@ import type {EditorSelectionPoint} from '../types/editor'
6
6
  * @public
7
7
  */
8
8
  export function blockOffsetToBlockSelectionPoint({
9
- value,
9
+ context,
10
10
  blockOffset,
11
11
  }: {
12
- value: Array<PortableTextBlock>
12
+ context: Pick<EditorContext, 'value'>
13
13
  blockOffset: BlockOffset
14
14
  }): EditorSelectionPoint | undefined {
15
15
  let selectionPoint: EditorSelectionPoint | undefined
16
16
 
17
- for (const block of value) {
17
+ for (const block of context.value) {
18
18
  if (block._key === blockOffset.path[0]._key) {
19
19
  selectionPoint = {
20
20
  path: [{_key: block._key}],
@@ -1,4 +1,4 @@
1
- import type {PortableTextBlock} from '@sanity/types'
1
+ import type {EditorContext} from '../editor/editor-snapshot'
2
2
  import type {BlockOffset} from '../types/block-offset'
3
3
  import type {EditorSelectionPoint} from '../types/editor'
4
4
  import {blockOffsetToSpanSelectionPoint} from './util.block-offset'
@@ -8,23 +8,23 @@ import {blockOffsetToBlockSelectionPoint} from './util.block-offset-to-block-sel
8
8
  * @public
9
9
  */
10
10
  export function blockOffsetToSelectionPoint({
11
- value,
11
+ context,
12
12
  blockOffset,
13
13
  direction,
14
14
  }: {
15
- value: Array<PortableTextBlock>
15
+ context: Pick<EditorContext, 'schema' | 'value'>
16
16
  blockOffset: BlockOffset
17
17
  direction: 'forward' | 'backward'
18
18
  }): EditorSelectionPoint | undefined {
19
19
  const spanSelectionPoint = blockOffsetToSpanSelectionPoint({
20
- value,
20
+ context,
21
21
  blockOffset,
22
22
  direction,
23
23
  })
24
24
 
25
25
  if (!spanSelectionPoint) {
26
26
  return blockOffsetToBlockSelectionPoint({
27
- value,
27
+ context,
28
28
  blockOffset,
29
29
  })
30
30
  }