@portabletext/editor 1.19.0 → 1.21.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 (132) hide show
  1. package/lib/_chunks-cjs/behavior.core.cjs +8 -68
  2. package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
  3. package/lib/_chunks-cjs/selector.get-text-before.cjs +11 -39
  4. package/lib/_chunks-cjs/selector.get-text-before.cjs.map +1 -1
  5. package/lib/_chunks-cjs/selector.is-selection-collapsed.cjs.map +1 -1
  6. package/lib/_chunks-cjs/util.get-block-start-point.cjs +30 -0
  7. package/lib/_chunks-cjs/util.get-block-start-point.cjs.map +1 -0
  8. package/lib/_chunks-cjs/util.is-empty-text-block.cjs +71 -0
  9. package/lib/_chunks-cjs/util.is-empty-text-block.cjs.map +1 -0
  10. package/lib/_chunks-cjs/util.is-keyed-segment.cjs +6 -0
  11. package/lib/_chunks-cjs/util.is-keyed-segment.cjs.map +1 -0
  12. package/lib/_chunks-es/behavior.core.js +7 -66
  13. package/lib/_chunks-es/behavior.core.js.map +1 -1
  14. package/lib/_chunks-es/selector.get-text-before.js +5 -31
  15. package/lib/_chunks-es/selector.get-text-before.js.map +1 -1
  16. package/lib/_chunks-es/selector.is-selection-collapsed.js.map +1 -1
  17. package/lib/_chunks-es/util.get-block-start-point.js +31 -0
  18. package/lib/_chunks-es/util.get-block-start-point.js.map +1 -0
  19. package/lib/_chunks-es/util.is-empty-text-block.js +73 -0
  20. package/lib/_chunks-es/util.is-empty-text-block.js.map +1 -0
  21. package/lib/_chunks-es/util.is-keyed-segment.js +7 -0
  22. package/lib/_chunks-es/util.is-keyed-segment.js.map +1 -0
  23. package/lib/behaviors/index.cjs +7 -7
  24. package/lib/behaviors/index.cjs.map +1 -1
  25. package/lib/behaviors/index.d.cts +179 -119
  26. package/lib/behaviors/index.d.ts +179 -119
  27. package/lib/behaviors/index.js +3 -2
  28. package/lib/behaviors/index.js.map +1 -1
  29. package/lib/index.cjs +72 -177
  30. package/lib/index.cjs.map +1 -1
  31. package/lib/index.d.cts +1061 -140
  32. package/lib/index.d.ts +1061 -140
  33. package/lib/index.js +72 -177
  34. package/lib/index.js.map +1 -1
  35. package/lib/selectors/index.cjs +73 -1
  36. package/lib/selectors/index.cjs.map +1 -1
  37. package/lib/selectors/index.d.cts +14 -0
  38. package/lib/selectors/index.d.ts +14 -0
  39. package/lib/selectors/index.js +73 -0
  40. package/lib/selectors/index.js.map +1 -1
  41. package/lib/utils/index.cjs +11 -0
  42. package/lib/utils/index.cjs.map +1 -0
  43. package/lib/utils/index.d.cts +87 -0
  44. package/lib/utils/index.d.ts +87 -0
  45. package/lib/utils/index.js +13 -0
  46. package/lib/utils/index.js.map +1 -0
  47. package/package.json +18 -14
  48. package/src/behavior-actions/behavior.action-utils.insert-block.ts +1 -1
  49. package/src/behavior-actions/behavior.action.insert-block-object.ts +1 -1
  50. package/src/behavior-actions/behavior.action.insert-inline-object.ts +1 -1
  51. package/src/behavior-actions/behavior.action.text-block.set.ts +1 -1
  52. package/src/behavior-actions/behavior.action.text-block.unset.ts +1 -1
  53. package/src/behavior-actions/behavior.actions.ts +5 -5
  54. package/src/behaviors/behavior.code-editor.ts +1 -1
  55. package/src/behaviors/behavior.core.block-objects.ts +2 -2
  56. package/src/behaviors/behavior.core.decorators.ts +1 -1
  57. package/src/behaviors/behavior.core.lists.ts +2 -2
  58. package/src/behaviors/behavior.emoji-picker.ts +1 -1
  59. package/src/behaviors/behavior.links.ts +1 -1
  60. package/src/behaviors/behavior.markdown.ts +2 -2
  61. package/src/behaviors/behavior.types.ts +52 -19
  62. package/src/behaviors/index.ts +1 -0
  63. package/src/editor/Editable.tsx +12 -12
  64. package/src/editor/PortableTextEditor.tsx +2 -2
  65. package/src/editor/components/DraggableBlock.tsx +2 -2
  66. package/src/editor/components/Element.tsx +20 -26
  67. package/src/editor/components/Leaf.tsx +1 -1
  68. package/src/editor/components/Synchronizer.tsx +1 -1
  69. package/src/editor/create-editor.ts +39 -20
  70. package/src/editor/create-slate-editor.tsx +5 -2
  71. package/src/editor/editor-machine.ts +72 -18
  72. package/src/editor/get-value.ts +2 -2
  73. package/src/editor/hooks/usePortableTextEditorSelection.tsx +1 -1
  74. package/src/editor/mutation-machine.ts +2 -2
  75. package/src/editor/nodes/DefaultObject.tsx +2 -2
  76. package/src/editor/plugins/create-with-event-listeners.ts +11 -3
  77. package/src/editor/plugins/createWithEditableAPI.ts +7 -7
  78. package/src/editor/plugins/createWithHotKeys.ts +2 -2
  79. package/src/editor/plugins/createWithInsertData.ts +7 -7
  80. package/src/editor/plugins/createWithMaxBlocks.ts +2 -2
  81. package/src/editor/plugins/createWithObjectKeys.ts +2 -2
  82. package/src/editor/plugins/createWithPatches.ts +10 -10
  83. package/src/editor/plugins/createWithPlaceholderBlock.ts +3 -3
  84. package/src/editor/plugins/createWithPortableTextBlockStyle.ts +1 -1
  85. package/src/editor/plugins/createWithPortableTextMarkModel.ts +4 -4
  86. package/src/editor/plugins/createWithPortableTextSelections.ts +6 -6
  87. package/src/editor/plugins/createWithSchemaTypes.ts +1 -1
  88. package/src/editor/plugins/createWithUndoRedo.ts +5 -5
  89. package/src/editor/plugins/createWithUtils.ts +2 -2
  90. package/src/editor/plugins/with-plugins.ts +1 -1
  91. package/src/editor/sync-machine.ts +8 -5
  92. package/src/index.ts +5 -1
  93. package/src/selectors/index.ts +2 -0
  94. package/src/selectors/selector.get-selection-text.test.ts +75 -0
  95. package/src/selectors/selector.get-selection-text.ts +3 -3
  96. package/src/selectors/selector.get-text-before.ts +7 -4
  97. package/src/selectors/selector.is-point-after-selection.ts +82 -0
  98. package/src/selectors/selector.is-point-before-selection.ts +82 -0
  99. package/src/utils/_exports/index.ts +1 -0
  100. package/src/utils/index.ts +11 -0
  101. package/src/{editor/utils/utils.block-offset.test.ts → utils/util.block-offset.test.ts} +1 -1
  102. package/src/{editor/utils/utils.block-offset.ts → utils/util.block-offset.ts} +23 -7
  103. package/src/{editor/utils/utils.get-start-point.ts → utils/util.get-block-start-point.ts} +5 -2
  104. package/src/utils/util.get-text-block-text.ts +8 -0
  105. package/src/{editor/utils/utils.ts → utils/util.is-empty-text-block.ts} +4 -5
  106. package/src/{editor/utils/utils.is-keyed-segment.ts → utils/util.is-keyed-segment.ts} +3 -0
  107. package/src/{editor/utils/utils.reverse-selection.ts → utils/util.reverse-selection.ts} +4 -1
  108. package/src/editor/nodes/index.ts +0 -189
  109. /package/src/{utils → internal-utils}/__tests__/dmpToOperations.test.ts +0 -0
  110. /package/src/{utils → internal-utils}/__tests__/operationToPatches.test.ts +0 -0
  111. /package/src/{utils → internal-utils}/__tests__/patchToOperations.test.ts +0 -0
  112. /package/src/{utils → internal-utils}/__tests__/ranges.test.ts +0 -0
  113. /package/src/{utils → internal-utils}/__tests__/valueNormalization.test.tsx +0 -0
  114. /package/src/{utils → internal-utils}/__tests__/values.test.ts +0 -0
  115. /package/src/{utils → internal-utils}/applyPatch.ts +0 -0
  116. /package/src/{utils → internal-utils}/debug.ts +0 -0
  117. /package/src/{utils → internal-utils}/is-hotkey.test.ts +0 -0
  118. /package/src/{utils → internal-utils}/is-hotkey.ts +0 -0
  119. /package/src/{utils → internal-utils}/looks-like-url.test.ts +0 -0
  120. /package/src/{utils → internal-utils}/looks-like-url.ts +0 -0
  121. /package/src/{utils → internal-utils}/operationToPatches.ts +0 -0
  122. /package/src/{utils → internal-utils}/paths.ts +0 -0
  123. /package/src/{utils → internal-utils}/ranges.ts +0 -0
  124. /package/src/{utils → internal-utils}/schema.ts +0 -0
  125. /package/src/{utils → internal-utils}/selection.ts +0 -0
  126. /package/src/{utils → internal-utils}/sibling-utils.ts +0 -0
  127. /package/src/{utils → internal-utils}/validateValue.ts +0 -0
  128. /package/src/{utils → internal-utils}/values.ts +0 -0
  129. /package/src/{utils → internal-utils}/weakMaps.ts +0 -0
  130. /package/src/{utils → internal-utils}/withChanges.ts +0 -0
  131. /package/src/{utils → internal-utils}/withUndoRedo.ts +0 -0
  132. /package/src/{utils → internal-utils}/withoutPatching.ts +0 -0
@@ -1,8 +1,11 @@
1
1
  import {createEditor, type Descendant} from 'slate'
2
2
  import {withReact} from 'slate-react'
3
+ import {debugWithName} from '../internal-utils/debug'
4
+ import {
5
+ KEY_TO_SLATE_ELEMENT,
6
+ KEY_TO_VALUE_ELEMENT,
7
+ } from '../internal-utils/weakMaps'
3
8
  import type {PortableTextSlateEditor} from '../types/editor'
4
- import {debugWithName} from '../utils/debug'
5
- import {KEY_TO_SLATE_ELEMENT, KEY_TO_VALUE_ELEMENT} from '../utils/weakMaps'
6
9
  import type {EditorActor} from './editor-machine'
7
10
  import {withPlugins} from './plugins/with-plugins'
8
11
 
@@ -12,21 +12,23 @@ import {
12
12
  } from 'xstate'
13
13
  import {performAction} from '../behavior-actions/behavior.actions'
14
14
  import {coreBehaviors} from '../behaviors/behavior.core'
15
- import type {
16
- Behavior,
17
- BehaviorAction,
18
- NativeBehaviorEvent,
19
- SyntheticBehaviorEvent,
15
+ import {
16
+ isCustomBehaviorEvent,
17
+ type Behavior,
18
+ type BehaviorAction,
19
+ type CustomBehaviorEvent,
20
+ type NativeBehaviorEvent,
21
+ type SyntheticBehaviorEvent,
20
22
  } from '../behaviors/behavior.types'
23
+ import {toPortableTextRange} from '../internal-utils/ranges'
24
+ import {fromSlateValue} from '../internal-utils/values'
25
+ import {KEY_TO_VALUE_ELEMENT} from '../internal-utils/weakMaps'
21
26
  import type {OmitFromUnion, PickFromUnion} from '../type-utils'
22
27
  import type {
23
28
  EditorSelection,
24
29
  InvalidValueResolution,
25
30
  PortableTextSlateEditor,
26
31
  } from '../types/editor'
27
- import {toPortableTextRange} from '../utils/ranges'
28
- import {fromSlateValue} from '../utils/values'
29
- import {KEY_TO_VALUE_ELEMENT} from '../utils/weakMaps'
30
32
  import type {EditorSchema} from './define-schema'
31
33
  import type {EditorContext} from './editor-snapshot'
32
34
  import {getActiveDecorators} from './get-active-decorators'
@@ -79,6 +81,21 @@ export type InternalEditorEvent =
79
81
  editor: PortableTextSlateEditor
80
82
  nativeEvent?: {preventDefault: () => void}
81
83
  }
84
+ | {
85
+ type: 'custom behavior event'
86
+ behaviorEvent: CustomBehaviorEvent
87
+ editor: PortableTextSlateEditor
88
+ nativeEvent?: {preventDefault: () => void}
89
+ }
90
+ | CustomBehaviorEvent
91
+ | {
92
+ type: 'add behavior'
93
+ behavior: Behavior
94
+ }
95
+ | {
96
+ type: 'remove behavior'
97
+ behavior: Behavior
98
+ }
82
99
  | {
83
100
  type: 'update readOnly'
84
101
  readOnly: boolean
@@ -174,6 +191,10 @@ export type InternalEditorEmittedEvent =
174
191
  | 'focus'
175
192
  | 'style.toggle'
176
193
  >
194
+ | {
195
+ type: 'custom.*'
196
+ event: CustomBehaviorEvent
197
+ }
177
198
 
178
199
  /**
179
200
  * @internal
@@ -181,7 +202,7 @@ export type InternalEditorEmittedEvent =
181
202
  export const editorMachine = setup({
182
203
  types: {
183
204
  context: {} as {
184
- behaviors: Array<Behavior>
205
+ behaviors: Set<Behavior>
185
206
  keyGenerator: () => string
186
207
  pendingEvents: Array<PatchEvent | MutationEvent>
187
208
  schema: EditorSchema
@@ -202,10 +223,26 @@ export const editorMachine = setup({
202
223
  },
203
224
  },
204
225
  actions: {
226
+ 'add behavior to context': assign({
227
+ behaviors: ({context, event}) => {
228
+ assertEvent(event, 'add behavior')
229
+
230
+ return new Set([...context.behaviors, event.behavior])
231
+ },
232
+ }),
233
+ 'remove behavior from context': assign({
234
+ behaviors: ({context, event}) => {
235
+ assertEvent(event, 'remove behavior')
236
+
237
+ context.behaviors.delete(event.behavior)
238
+
239
+ return new Set([...context.behaviors])
240
+ },
241
+ }),
205
242
  'assign behaviors': assign({
206
243
  behaviors: ({event}) => {
207
244
  assertEvent(event, 'update behaviors')
208
- return event.behaviors
245
+ return new Set(event.behaviors)
209
246
  },
210
247
  }),
211
248
  'assign schema': assign({
@@ -240,9 +277,10 @@ export const editorMachine = setup({
240
277
  pendingEvents: [],
241
278
  }),
242
279
  'handle behavior event': enqueueActions(({context, event, enqueue}) => {
243
- assertEvent(event, ['behavior event'])
280
+ assertEvent(event, ['behavior event', 'custom behavior event'])
244
281
 
245
282
  const defaultAction =
283
+ event.type === 'custom behavior event' ||
246
284
  event.behaviorEvent.type === 'copy' ||
247
285
  event.behaviorEvent.type === 'key.down' ||
248
286
  event.behaviorEvent.type === 'key.up' ||
@@ -253,7 +291,7 @@ export const editorMachine = setup({
253
291
  editor: event.editor,
254
292
  } satisfies BehaviorAction)
255
293
 
256
- const eventBehaviors = context.behaviors.filter(
294
+ const eventBehaviors = [...context.behaviors.values()].filter(
257
295
  (behavior) => behavior.on === event.behaviorEvent.type,
258
296
  )
259
297
 
@@ -329,11 +367,19 @@ export const editorMachine = setup({
329
367
  Editor.withoutNormalizing(event.editor, () => {
330
368
  for (const actionIntend of actionIntends) {
331
369
  if (actionIntend.type === 'raise') {
332
- enqueue.raise({
333
- type: 'behavior event',
334
- behaviorEvent: actionIntend.event,
335
- editor: event.editor,
336
- })
370
+ if (isCustomBehaviorEvent(actionIntend.event)) {
371
+ enqueue.raise({
372
+ type: 'custom behavior event',
373
+ behaviorEvent: actionIntend.event as CustomBehaviorEvent,
374
+ editor: event.editor,
375
+ })
376
+ } else {
377
+ enqueue.raise({
378
+ type: 'behavior event',
379
+ behaviorEvent: actionIntend.event,
380
+ editor: event.editor,
381
+ })
382
+ }
337
383
  continue
338
384
  }
339
385
 
@@ -375,7 +421,7 @@ export const editorMachine = setup({
375
421
  }).createMachine({
376
422
  id: 'editor',
377
423
  context: ({input}) => ({
378
- behaviors: input.behaviors ?? coreBehaviors,
424
+ behaviors: new Set(input.behaviors ?? coreBehaviors),
379
425
  keyGenerator: input.keyGenerator,
380
426
  pendingEvents: [],
381
427
  schema: input.schema,
@@ -385,6 +431,8 @@ export const editorMachine = setup({
385
431
  value: input.value,
386
432
  }),
387
433
  on: {
434
+ 'add behavior': {actions: 'add behavior to context'},
435
+ 'remove behavior': {actions: 'remove behavior from context'},
388
436
  'unset': {actions: emit(({event}) => event)},
389
437
  'value changed': {actions: emit(({event}) => event)},
390
438
  'invalid value': {actions: emit(({event}) => event)},
@@ -449,12 +497,18 @@ export const editorMachine = setup({
449
497
  'behavior event': {
450
498
  actions: 'handle behavior event',
451
499
  },
500
+ 'custom behavior event': {
501
+ actions: 'handle behavior event',
502
+ },
452
503
  'annotation.*': {
453
504
  actions: emit(({event}) => event),
454
505
  },
455
506
  'blur': {
456
507
  actions: emit(({event}) => event),
457
508
  },
509
+ 'custom.*': {
510
+ actions: emit(({event}) => ({type: 'custom.*', event})),
511
+ },
458
512
  'decorator.*': {
459
513
  actions: emit(({event}) => event),
460
514
  },
@@ -1,6 +1,6 @@
1
+ import {fromSlateValue} from '../internal-utils/values'
2
+ import {KEY_TO_VALUE_ELEMENT} from '../internal-utils/weakMaps'
1
3
  import type {PortableTextSlateEditor} from '../types/editor'
2
- import {fromSlateValue} from '../utils/values'
3
- import {KEY_TO_VALUE_ELEMENT} from '../utils/weakMaps'
4
4
  import type {EditorActor} from './editor-machine'
5
5
 
6
6
  export function getValue({
@@ -5,8 +5,8 @@ import {
5
5
  useEffect,
6
6
  useState,
7
7
  } from 'react'
8
+ import {debugWithName} from '../../internal-utils/debug'
8
9
  import type {EditorSelection} from '../../types/editor'
9
- import {debugWithName} from '../../utils/debug'
10
10
  import type {EditorActor} from '../editor-machine'
11
11
 
12
12
  /**
@@ -2,9 +2,9 @@ import type {Patch} from '@portabletext/patches'
2
2
  import type {PortableTextBlock} from '@sanity/types'
3
3
  import {Editor} from 'slate'
4
4
  import {assign, emit, setup} from 'xstate'
5
+ import {fromSlateValue} from '../internal-utils/values'
6
+ import {KEY_TO_VALUE_ELEMENT} from '../internal-utils/weakMaps'
5
7
  import type {PortableTextSlateEditor} from '../types/editor'
6
- import {fromSlateValue} from '../utils/values'
7
- import {KEY_TO_VALUE_ELEMENT} from '../utils/weakMaps'
8
8
  import type {EditorSchema} from './define-schema'
9
9
 
10
10
  const FLUSH_PATCHES_THROTTLED_MS = process.env.NODE_ENV === 'test' ? 500 : 1000
@@ -7,8 +7,8 @@ type Props = {
7
7
 
8
8
  const DefaultObject = (props: Props): JSX.Element => {
9
9
  return (
10
- <div>
11
- <pre>{JSON.stringify(props.value, null, 2)}</pre>
10
+ <div style={{userSelect: 'none'}}>
11
+ [{props.value._type}: {props.value._key}]
12
12
  </div>
13
13
  )
14
14
  }
@@ -1,7 +1,7 @@
1
1
  import {Editor} from 'slate'
2
- import {toPortableTextRange} from '../../utils/ranges'
3
- import {fromSlateValue} from '../../utils/values'
4
- import {KEY_TO_VALUE_ELEMENT} from '../../utils/weakMaps'
2
+ import {toPortableTextRange} from '../../internal-utils/ranges'
3
+ import {fromSlateValue} from '../../internal-utils/values'
4
+ import {KEY_TO_VALUE_ELEMENT} from '../../internal-utils/weakMaps'
5
5
  import type {EditorActor} from '../editor-machine'
6
6
  import {isApplyingBehaviorActions} from '../with-applying-behavior-actions'
7
7
 
@@ -49,6 +49,14 @@ export function createWithEventListeners(
49
49
  })
50
50
  break
51
51
  }
52
+ case 'custom.*': {
53
+ editorActor.send({
54
+ type: 'custom behavior event',
55
+ behaviorEvent: event.event,
56
+ editor,
57
+ })
58
+ break
59
+ }
52
60
  case 'decorator.toggle': {
53
61
  editorActor.send({
54
62
  type: 'behavior event',
@@ -21,19 +21,19 @@ import {ReactEditor} from 'slate-react'
21
21
  import {isListItemActive} from '../../behavior-actions/behavior.action.list-item'
22
22
  import {isStyleActive} from '../../behavior-actions/behavior.action.style'
23
23
  import type {BehaviorActionImplementation} from '../../behavior-actions/behavior.actions'
24
+ import {debugWithName} from '../../internal-utils/debug'
25
+ import {toPortableTextRange, toSlateRange} from '../../internal-utils/ranges'
26
+ import {fromSlateValue, toSlateValue} from '../../internal-utils/values'
27
+ import {
28
+ KEY_TO_VALUE_ELEMENT,
29
+ SLATE_TO_PORTABLE_TEXT_RANGE,
30
+ } from '../../internal-utils/weakMaps'
24
31
  import type {
25
32
  EditableAPI,
26
33
  EditableAPIDeleteOptions,
27
34
  EditorSelection,
28
35
  PortableTextSlateEditor,
29
36
  } from '../../types/editor'
30
- import {debugWithName} from '../../utils/debug'
31
- import {toPortableTextRange, toSlateRange} from '../../utils/ranges'
32
- import {fromSlateValue, toSlateValue} from '../../utils/values'
33
- import {
34
- KEY_TO_VALUE_ELEMENT,
35
- SLATE_TO_PORTABLE_TEXT_RANGE,
36
- } from '../../utils/weakMaps'
37
37
  import type {EditorActor} from '../editor-machine'
38
38
  import {isDecoratorActive} from './createWithPortableTextMarkModel'
39
39
 
@@ -1,9 +1,9 @@
1
1
  import type {KeyboardEvent} from 'react'
2
2
  import type {ReactEditor} from 'slate-react'
3
+ import {debugWithName} from '../../internal-utils/debug'
4
+ import {isHotkey} from '../../internal-utils/is-hotkey'
3
5
  import type {PortableTextSlateEditor} from '../../types/editor'
4
6
  import type {HotkeyOptions} from '../../types/options'
5
- import {debugWithName} from '../../utils/debug'
6
- import {isHotkey} from '../../utils/is-hotkey'
7
7
  import type {EditorActor} from '../editor-machine'
8
8
  import type {PortableTextEditor} from '../PortableTextEditor'
9
9
 
@@ -3,17 +3,17 @@ import type {PortableTextBlock, PortableTextChild} from '@sanity/types'
3
3
  import {isEqual, uniq} from 'lodash'
4
4
  import {Editor, Range, Transforms, type Descendant, type Node} from 'slate'
5
5
  import {ReactEditor} from 'slate-react'
6
- import type {
7
- PortableTextMemberSchemaTypes,
8
- PortableTextSlateEditor,
9
- } from '../../types/editor'
10
- import {debugWithName} from '../../utils/debug'
11
- import {validateValue} from '../../utils/validateValue'
6
+ import {debugWithName} from '../../internal-utils/debug'
7
+ import {validateValue} from '../../internal-utils/validateValue'
12
8
  import {
13
9
  fromSlateValue,
14
10
  isEqualToEmptyEditor,
15
11
  toSlateValue,
16
- } from '../../utils/values'
12
+ } from '../../internal-utils/values'
13
+ import type {
14
+ PortableTextMemberSchemaTypes,
15
+ PortableTextSlateEditor,
16
+ } from '../../types/editor'
17
17
  import type {EditorActor} from '../editor-machine'
18
18
 
19
19
  const debug = debugWithName('plugin:withInsertData')
@@ -1,6 +1,6 @@
1
+ import {isChangingRemotely} from '../../internal-utils/withChanges'
2
+ import {isRedoing, isUndoing} from '../../internal-utils/withUndoRedo'
1
3
  import type {PortableTextSlateEditor} from '../../types/editor'
2
- import {isChangingRemotely} from '../../utils/withChanges'
3
- import {isRedoing, isUndoing} from '../../utils/withUndoRedo'
4
4
  import type {EditorActor} from '../editor-machine'
5
5
 
6
6
  /**
@@ -1,10 +1,10 @@
1
1
  import {Editor, Element, Node, Transforms} from 'slate'
2
+ import {isChangingRemotely} from '../../internal-utils/withChanges'
3
+ import {isRedoing, isUndoing} from '../../internal-utils/withUndoRedo'
2
4
  import type {
3
5
  PortableTextMemberSchemaTypes,
4
6
  PortableTextSlateEditor,
5
7
  } from '../../types/editor'
6
- import {isChangingRemotely} from '../../utils/withChanges'
7
- import {isRedoing, isUndoing} from '../../utils/withUndoRedo'
8
8
  import type {EditorActor} from '../editor-machine'
9
9
 
10
10
  /**
@@ -12,23 +12,23 @@ import {
12
12
  type SetNodeOperation,
13
13
  type SplitNodeOperation,
14
14
  } from 'slate'
15
- import type {
16
- PortableTextMemberSchemaTypes,
17
- PortableTextSlateEditor,
18
- } from '../../types/editor'
19
- import {createApplyPatch} from '../../utils/applyPatch'
20
- import {debugWithName} from '../../utils/debug'
21
- import {fromSlateValue, isEqualToEmptyEditor} from '../../utils/values'
15
+ import {createApplyPatch} from '../../internal-utils/applyPatch'
16
+ import {debugWithName} from '../../internal-utils/debug'
17
+ import {fromSlateValue, isEqualToEmptyEditor} from '../../internal-utils/values'
22
18
  import {
23
19
  IS_PROCESSING_REMOTE_CHANGES,
24
20
  KEY_TO_VALUE_ELEMENT,
25
- } from '../../utils/weakMaps'
26
- import {withRemoteChanges} from '../../utils/withChanges'
21
+ } from '../../internal-utils/weakMaps'
22
+ import {withRemoteChanges} from '../../internal-utils/withChanges'
27
23
  import {
28
24
  isPatching,
29
25
  PATCHING,
30
26
  withoutPatching,
31
- } from '../../utils/withoutPatching'
27
+ } from '../../internal-utils/withoutPatching'
28
+ import type {
29
+ PortableTextMemberSchemaTypes,
30
+ PortableTextSlateEditor,
31
+ } from '../../types/editor'
32
32
  import type {EditorActor} from '../editor-machine'
33
33
  import {withoutSaving} from './createWithUndoRedo'
34
34
 
@@ -1,9 +1,9 @@
1
1
  import {Editor, Path} from 'slate'
2
+ import {debugWithName} from '../../internal-utils/debug'
3
+ import {isChangingRemotely} from '../../internal-utils/withChanges'
4
+ import {isRedoing, isUndoing} from '../../internal-utils/withUndoRedo'
2
5
  import type {PortableTextSlateEditor} from '../../types/editor'
3
6
  import type {SlateTextBlock, VoidElement} from '../../types/slate'
4
- import {debugWithName} from '../../utils/debug'
5
- import {isChangingRemotely} from '../../utils/withChanges'
6
- import {isRedoing, isUndoing} from '../../utils/withUndoRedo'
7
7
  import type {EditorActor} from '../editor-machine'
8
8
 
9
9
  const debug = debugWithName('plugin:withPlaceholderBlock')
@@ -1,9 +1,9 @@
1
1
  import {Editor, Path, Text as SlateText, Transforms} from 'slate'
2
+ import {debugWithName} from '../../internal-utils/debug'
2
3
  import type {
3
4
  PortableTextMemberSchemaTypes,
4
5
  PortableTextSlateEditor,
5
6
  } from '../../types/editor'
6
- import {debugWithName} from '../../utils/debug'
7
7
  import type {EditorActor} from '../editor-machine'
8
8
 
9
9
  const debug = debugWithName('plugin:withPortableTextBlockStyle')
@@ -9,14 +9,14 @@ import type {PortableTextObject, PortableTextSpan} from '@sanity/types'
9
9
  import {isEqual, uniq} from 'lodash'
10
10
  import {Editor, Element, Node, Path, Range, Text, Transforms} from 'slate'
11
11
  import type {BehaviorActionImplementation} from '../../behavior-actions/behavior.actions'
12
+ import {debugWithName} from '../../internal-utils/debug'
13
+ import {getNextSpan, getPreviousSpan} from '../../internal-utils/sibling-utils'
14
+ import {isChangingRemotely} from '../../internal-utils/withChanges'
15
+ import {isRedoing, isUndoing} from '../../internal-utils/withUndoRedo'
12
16
  import type {
13
17
  PortableTextMemberSchemaTypes,
14
18
  PortableTextSlateEditor,
15
19
  } from '../../types/editor'
16
- import {debugWithName} from '../../utils/debug'
17
- import {getNextSpan, getPreviousSpan} from '../../utils/sibling-utils'
18
- import {isChangingRemotely} from '../../utils/withChanges'
19
- import {isRedoing, isUndoing} from '../../utils/withUndoRedo'
20
20
  import type {EditorActor} from '../editor-machine'
21
21
 
22
22
  const debug = debugWithName('plugin:withPortableTextMarkModel')
@@ -1,15 +1,15 @@
1
1
  import type {BaseRange} from 'slate'
2
+ import {debugWithName} from '../../internal-utils/debug'
3
+ import {
4
+ toPortableTextRange,
5
+ type ObjectWithKeyAndType,
6
+ } from '../../internal-utils/ranges'
7
+ import {SLATE_TO_PORTABLE_TEXT_RANGE} from '../../internal-utils/weakMaps'
2
8
  import type {
3
9
  EditorSelection,
4
10
  PortableTextMemberSchemaTypes,
5
11
  PortableTextSlateEditor,
6
12
  } from '../../types/editor'
7
- import {debugWithName} from '../../utils/debug'
8
- import {
9
- toPortableTextRange,
10
- type ObjectWithKeyAndType,
11
- } from '../../utils/ranges'
12
- import {SLATE_TO_PORTABLE_TEXT_RANGE} from '../../utils/weakMaps'
13
13
  import type {EditorActor} from '../editor-machine'
14
14
 
15
15
  const debug = debugWithName('plugin:withPortableTextSelections')
@@ -7,11 +7,11 @@ import {
7
7
  type PortableTextTextBlock,
8
8
  } from '@sanity/types'
9
9
  import {Transforms, type Element} from 'slate'
10
+ import {debugWithName} from '../../internal-utils/debug'
10
11
  import type {
11
12
  PortableTextMemberSchemaTypes,
12
13
  PortableTextSlateEditor,
13
14
  } from '../../types/editor'
14
- import {debugWithName} from '../../utils/debug'
15
15
  import type {EditorActor} from '../editor-machine'
16
16
 
17
17
  const debug = debugWithName('plugin:withSchemaTypes')
@@ -20,10 +20,9 @@ import {
20
20
  type Descendant,
21
21
  type SelectionOperation,
22
22
  } from 'slate'
23
- import type {PortableTextSlateEditor} from '../../types/editor'
24
- import {debugWithName} from '../../utils/debug'
25
- import {fromSlateValue} from '../../utils/values'
26
- import {isChangingRemotely} from '../../utils/withChanges'
23
+ import {debugWithName} from '../../internal-utils/debug'
24
+ import {fromSlateValue} from '../../internal-utils/values'
25
+ import {isChangingRemotely} from '../../internal-utils/withChanges'
27
26
  import {
28
27
  isRedoing,
29
28
  isUndoing,
@@ -31,7 +30,8 @@ import {
31
30
  setIsUndoing,
32
31
  withRedoing,
33
32
  withUndoing,
34
- } from '../../utils/withUndoRedo'
33
+ } from '../../internal-utils/withUndoRedo'
34
+ import type {PortableTextSlateEditor} from '../../types/editor'
35
35
  import type {EditorActor} from '../editor-machine'
36
36
 
37
37
  const debug = debugWithName('plugin:withUndoRedo')
@@ -1,10 +1,10 @@
1
1
  import {Editor, Range, Text, Transforms} from 'slate'
2
+ import {debugWithName} from '../../internal-utils/debug'
3
+ import {toSlateValue} from '../../internal-utils/values'
2
4
  import type {
3
5
  PortableTextMemberSchemaTypes,
4
6
  PortableTextSlateEditor,
5
7
  } from '../../types/editor'
6
- import {debugWithName} from '../../utils/debug'
7
- import {toSlateValue} from '../../utils/values'
8
8
  import type {EditorActor} from '../editor-machine'
9
9
 
10
10
  const debug = debugWithName('plugin:withUtils')
@@ -1,6 +1,6 @@
1
1
  import type {BaseOperation, Editor, Node, NodeEntry} from 'slate'
2
+ import {createOperationToPatches} from '../../internal-utils/operationToPatches'
2
3
  import type {PortableTextSlateEditor} from '../../types/editor'
3
- import {createOperationToPatches} from '../../utils/operationToPatches'
4
4
  import type {EditorActor} from '../editor-machine'
5
5
  import {createWithEventListeners} from './create-with-event-listeners'
6
6
  import {createWithMaxBlocks} from './createWithMaxBlocks'
@@ -11,16 +11,19 @@ import {
11
11
  type AnyEventObject,
12
12
  type CallbackLogicFunction,
13
13
  } from 'xstate'
14
+ import {debugWithName} from '../internal-utils/debug'
15
+ import {validateValue} from '../internal-utils/validateValue'
16
+ import {toSlateValue, VOID_CHILD_KEY} from '../internal-utils/values'
17
+ import {
18
+ isChangingRemotely,
19
+ withRemoteChanges,
20
+ } from '../internal-utils/withChanges'
21
+ import {withoutPatching} from '../internal-utils/withoutPatching'
14
22
  import type {PickFromUnion} from '../type-utils'
15
23
  import type {
16
24
  InvalidValueResolution,
17
25
  PortableTextSlateEditor,
18
26
  } from '../types/editor'
19
- import {debugWithName} from '../utils/debug'
20
- import {validateValue} from '../utils/validateValue'
21
- import {toSlateValue, VOID_CHILD_KEY} from '../utils/values'
22
- import {isChangingRemotely, withRemoteChanges} from '../utils/withChanges'
23
- import {withoutPatching} from '../utils/withoutPatching'
24
27
  import type {EditorSchema} from './define-schema'
25
28
  import {withoutSaving} from './plugins/createWithUndoRedo'
26
29
 
package/src/index.ts CHANGED
@@ -1,5 +1,9 @@
1
1
  export type {Patch} from '@portabletext/patches'
2
- export type {PortableTextBlock, PortableTextChild} from '@sanity/types'
2
+ export type {
3
+ PortableTextBlock,
4
+ PortableTextChild,
5
+ PortableTextSpan,
6
+ } from '@sanity/types'
3
7
  export type {
4
8
  Behavior,
5
9
  BehaviorActionIntend,
@@ -16,6 +16,8 @@ export {isActiveAnnotation} from './selector.is-active-annotation'
16
16
  export {isActiveDecorator} from './selector.is-active-decorator'
17
17
  export {isActiveListItem} from './selector.is-active-list-item'
18
18
  export {isActiveStyle} from './selector.is-active-style'
19
+ export {isPointAfterSelection} from './selector.is-point-after-selection'
20
+ export {isPointBeforeSelection} from './selector.is-point-before-selection'
19
21
  export {isSelectionCollapsed} from './selector.is-selection-collapsed'
20
22
  export {isSelectionExpanded} from './selector.is-selection-expanded'
21
23
  export * from './selectors'
@@ -0,0 +1,75 @@
1
+ import {expect, test} from 'vitest'
2
+ import type {EditorSchema, EditorSelection, EditorSnapshot} from '.'
3
+ import {getSelectionText} from './selector.get-selection-text'
4
+
5
+ test(getSelectionText.name, () => {
6
+ function snapshot(selection: EditorSelection): EditorSnapshot {
7
+ return {
8
+ context: {
9
+ schema: {} as EditorSchema,
10
+ keyGenerator: () => '',
11
+ activeDecorators: [],
12
+ value: [
13
+ {
14
+ _type: 'block',
15
+ _key: 'e0-k8',
16
+ style: 'normal',
17
+ markDefs: [],
18
+ children: [
19
+ {
20
+ _type: 'span',
21
+ _key: 'e0-k7',
22
+ text: ':b',
23
+ marks: [],
24
+ },
25
+ {
26
+ text: 'a',
27
+ _type: 'span',
28
+ _key: 'e0-k9',
29
+ marks: ['strong'],
30
+ },
31
+ {
32
+ text: 'r',
33
+ marks: [],
34
+ _type: 'span',
35
+ _key: 'e0-k10',
36
+ },
37
+ ],
38
+ },
39
+ ],
40
+ selection,
41
+ },
42
+ }
43
+ }
44
+
45
+ expect(
46
+ getSelectionText(
47
+ snapshot({
48
+ anchor: {
49
+ path: [
50
+ {
51
+ _key: 'e0-k8',
52
+ },
53
+ 'children',
54
+ {
55
+ _key: 'e0-k7',
56
+ },
57
+ ],
58
+ offset: 0,
59
+ },
60
+ focus: {
61
+ path: [
62
+ {
63
+ _key: 'e0-k8',
64
+ },
65
+ 'children',
66
+ {
67
+ _key: 'e0-k10',
68
+ },
69
+ ],
70
+ offset: 1,
71
+ },
72
+ }),
73
+ ),
74
+ ).toBe(':bar')
75
+ })