@portabletext/editor 1.44.1 → 1.44.3

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 (60) hide show
  1. package/lib/_chunks-cjs/behavior.core.cjs +3 -3
  2. package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
  3. package/lib/_chunks-cjs/editor-provider.cjs +315 -368
  4. package/lib/_chunks-cjs/editor-provider.cjs.map +1 -1
  5. package/lib/_chunks-es/behavior.core.js +3 -3
  6. package/lib/_chunks-es/behavior.core.js.map +1 -1
  7. package/lib/_chunks-es/editor-provider.js +317 -370
  8. package/lib/_chunks-es/editor-provider.js.map +1 -1
  9. package/lib/behaviors/index.cjs +2 -2
  10. package/lib/behaviors/index.cjs.map +1 -1
  11. package/lib/behaviors/index.d.cts +7951 -15846
  12. package/lib/behaviors/index.d.ts +7951 -15846
  13. package/lib/behaviors/index.js +4 -4
  14. package/lib/behaviors/index.js.map +1 -1
  15. package/lib/index.cjs +19 -7
  16. package/lib/index.cjs.map +1 -1
  17. package/lib/index.d.cts +1742 -15478
  18. package/lib/index.d.ts +1742 -15478
  19. package/lib/index.js +20 -8
  20. package/lib/index.js.map +1 -1
  21. package/lib/plugins/index.d.cts +1742 -15479
  22. package/lib/plugins/index.d.ts +1742 -15479
  23. package/lib/selectors/index.d.cts +1739 -15475
  24. package/lib/selectors/index.d.ts +1739 -15475
  25. package/lib/utils/index.d.cts +1739 -15475
  26. package/lib/utils/index.d.ts +1739 -15475
  27. package/package.json +9 -9
  28. package/src/behavior-actions/behavior.action.decorator.add.ts +7 -2
  29. package/src/behavior-actions/behavior.actions.ts +0 -18
  30. package/src/behaviors/{behavior.internal.annotation.ts → behavior.abstract.annotation.ts} +1 -1
  31. package/src/behaviors/{behavior.internal.decorator.ts → behavior.abstract.decorator.ts} +1 -1
  32. package/src/behaviors/{behavior.internal.insert.ts → behavior.abstract.insert.ts} +1 -1
  33. package/src/behaviors/{behavior.internal.list-item.ts → behavior.abstract.list-item.ts} +1 -1
  34. package/src/behaviors/behavior.abstract.move.ts +78 -0
  35. package/src/behaviors/{behavior.internal.select.ts → behavior.abstract.select.ts} +1 -1
  36. package/src/behaviors/{behavior.internal.style.ts → behavior.abstract.style.ts} +1 -1
  37. package/src/behaviors/behavior.code-editor.ts +13 -8
  38. package/src/behaviors/behavior.default.ts +14 -12
  39. package/src/behaviors/behavior.perform-event.ts +4 -4
  40. package/src/behaviors/behavior.types.action.ts +3 -3
  41. package/src/behaviors/behavior.types.behavior.ts +4 -3
  42. package/src/behaviors/behavior.types.event.ts +153 -153
  43. package/src/editor/Editable.tsx +17 -17
  44. package/src/editor/components/Leaf.tsx +2 -2
  45. package/src/editor/create-editor.ts +37 -11
  46. package/src/editor/editor-machine.ts +4 -76
  47. package/src/editor/editor-selector.ts +5 -2
  48. package/src/editor/editor-snapshot.ts +10 -9
  49. package/src/editor/plugins/create-with-event-listeners.ts +6 -75
  50. package/src/editor/plugins/createWithEditableAPI.ts +32 -44
  51. package/src/editor/plugins/createWithPortableTextSelections.ts +8 -13
  52. package/src/editor/plugins/with-plugins.ts +3 -8
  53. package/src/internal-utils/event-position.ts +10 -7
  54. package/src/internal-utils/paths.ts +1 -37
  55. package/src/internal-utils/ranges.ts +3 -37
  56. package/src/internal-utils/slate-children-to-blocks.ts +49 -0
  57. package/src/internal-utils/slate-utils.ts +108 -9
  58. package/src/type-utils.ts +2 -0
  59. package/src/behavior-actions/behavior.action.move.block-down.ts +0 -48
  60. package/src/behavior-actions/behavior.action.move.block-up.ts +0 -53
@@ -38,13 +38,10 @@ import {debugWithName} from '../internal-utils/debug'
38
38
  import {getDragSelection} from '../internal-utils/drag-selection'
39
39
  import {getEventPosition} from '../internal-utils/event-position'
40
40
  import {parseBlocks} from '../internal-utils/parse-blocks'
41
- import {
42
- moveRangeByOperation,
43
- toPortableTextRange,
44
- toSlateRange,
45
- } from '../internal-utils/ranges'
41
+ import {moveRangeByOperation, toSlateRange} from '../internal-utils/ranges'
46
42
  import {normalizeSelection} from '../internal-utils/selection'
47
43
  import {getSelectionDomNodes} from '../internal-utils/selection-elements'
44
+ import {slateRangeToSelection} from '../internal-utils/slate-utils'
48
45
  import {fromSlateValue, isEqualToEmptyEditor} from '../internal-utils/values'
49
46
  import * as selectors from '../selectors'
50
47
  import type {
@@ -341,12 +338,13 @@ export const PortableTextEditable = forwardRef<
341
338
  (newRange && newRange !== slateRange) ||
342
339
  (newRange === null && slateRange)
343
340
  ) {
344
- const value = PortableTextEditor.getValue(portableTextEditor)
345
- const newRangeSelection = toPortableTextRange(
346
- value,
347
- newRange,
348
- schemaTypes,
349
- )
341
+ const newRangeSelection = newRange
342
+ ? slateRangeToSelection({
343
+ schema: schemaTypes,
344
+ editor: slateEditor,
345
+ range: newRange,
346
+ })
347
+ : null
350
348
  if (rangeDecorationItem.onMoved) {
351
349
  rangeDecorationItem.onMoved({
352
350
  newSelection: newRangeSelection,
@@ -379,7 +377,7 @@ export const PortableTextEditable = forwardRef<
379
377
  return rangeDecorationState
380
378
  })
381
379
  },
382
- [portableTextEditor, rangeDecorations, schemaTypes, slateEditor],
380
+ [rangeDecorations, schemaTypes, slateEditor],
383
381
  )
384
382
 
385
383
  // Restore selection from props when the editor has been initialized properly with it's value
@@ -516,11 +514,13 @@ export const PortableTextEditable = forwardRef<
516
514
  const handlePaste = useCallback(
517
515
  (event: ClipboardEvent<HTMLDivElement>): Promise<void> | void => {
518
516
  const value = PortableTextEditor.getValue(portableTextEditor)
519
- const ptRange = toPortableTextRange(
520
- value,
521
- slateEditor.selection,
522
- schemaTypes,
523
- )
517
+ const ptRange = slateEditor.selection
518
+ ? slateRangeToSelection({
519
+ schema: schemaTypes,
520
+ editor: slateEditor,
521
+ range: slateEditor.selection,
522
+ })
523
+ : null
524
524
  const path = ptRange?.focus.path || []
525
525
  const onPasteResult = onPaste?.({event, value, path, schemaTypes})
526
526
 
@@ -144,12 +144,12 @@ export const Leaf = (props: LeafProps) => {
144
144
  return undefined
145
145
  }
146
146
 
147
- const onBlur = editorActor.on('blur', () => {
147
+ const onBlur = editorActor.on('blurred', () => {
148
148
  setFocused(false)
149
149
  setSelected(false)
150
150
  })
151
151
 
152
- const onFocus = editorActor.on('focus', () => {
152
+ const onFocus = editorActor.on('focused', () => {
153
153
  const sel = PortableTextEditor.getSelection(portableTextEditor)
154
154
  if (
155
155
  sel &&
@@ -12,11 +12,7 @@ import {
12
12
  type Snapshot,
13
13
  } from 'xstate'
14
14
  import type {Behavior} from '../behaviors/behavior.types.behavior'
15
- import type {
16
- CustomBehaviorEvent,
17
- ExternalBehaviorEvent,
18
- SyntheticBehaviorEvent,
19
- } from '../behaviors/behavior.types.event'
15
+ import type {ExternalBehaviorEvent} from '../behaviors/behavior.types.event'
20
16
  import {coreConverters} from '../converters/converters.core'
21
17
  import {compileType} from '../internal-utils/schema'
22
18
  import type {EditableAPI} from '../types/editor'
@@ -63,11 +59,7 @@ export type EditorConfig = {
63
59
  /**
64
60
  * @public
65
61
  */
66
- export type EditorEvent =
67
- | ExternalEditorEvent
68
- | ExternalBehaviorEvent
69
- | SyntheticBehaviorEvent
70
- | CustomBehaviorEvent
62
+ export type EditorEvent = ExternalEditorEvent | ExternalBehaviorEvent
71
63
 
72
64
  /**
73
65
  * @public
@@ -154,7 +146,41 @@ function createInternalEditorFromActor(
154
146
  }
155
147
  },
156
148
  send: (event) => {
157
- editorActor.send(event)
149
+ switch (event.type) {
150
+ case 'add behavior':
151
+ case 'remove behavior':
152
+ case 'update behaviors':
153
+ case 'update key generator':
154
+ case 'update readOnly':
155
+ case 'patches':
156
+ case 'update value':
157
+ case 'update schema':
158
+ case 'update maxBlocks':
159
+ editorActor.send(event)
160
+ break
161
+
162
+ case 'insert.block object':
163
+ editorActor.send({
164
+ type: 'behavior event',
165
+ behaviorEvent: {
166
+ type: 'insert.block',
167
+ block: {
168
+ _type: event.blockObject.name,
169
+ ...(event.blockObject.value ?? {}),
170
+ },
171
+ placement: event.placement,
172
+ },
173
+ editor: slateEditor.instance,
174
+ })
175
+ break
176
+
177
+ default:
178
+ editorActor.send({
179
+ type: 'behavior event',
180
+ behaviorEvent: event,
181
+ editor: slateEditor.instance,
182
+ })
183
+ }
158
184
  },
159
185
  on: (event, listener) => {
160
186
  const subscription = editorActor.on(event, (event) => {
@@ -13,13 +13,7 @@ import {coreBehaviors} from '../behaviors/behavior.core'
13
13
  import {defaultBehaviors} from '../behaviors/behavior.default'
14
14
  import {performEvent} from '../behaviors/behavior.perform-event'
15
15
  import type {Behavior} from '../behaviors/behavior.types.behavior'
16
- import type {
17
- CustomBehaviorEvent,
18
- ExternalBehaviorEvent,
19
- InternalBehaviorEvent,
20
- NativeBehaviorEvent,
21
- SyntheticBehaviorEvent,
22
- } from '../behaviors/behavior.types.event'
16
+ import type {BehaviorEvent} from '../behaviors/behavior.types.event'
23
17
  import type {Converter} from '../converters/converter.types'
24
18
  import type {EventPosition} from '../internal-utils/event-position'
25
19
  import type {NamespaceEvent} from '../type-utils'
@@ -167,6 +161,7 @@ export type HasTag = ReturnType<EditorActor['getSnapshot']>['hasTag']
167
161
  * @internal
168
162
  */
169
163
  export type InternalEditorEvent =
164
+ | ExternalEditorEvent
170
165
  | {
171
166
  type: 'normalizing'
172
167
  }
@@ -178,28 +173,15 @@ export type InternalEditorEvent =
178
173
  }
179
174
  | {
180
175
  type: 'behavior event'
181
- behaviorEvent:
182
- | InternalBehaviorEvent
183
- | SyntheticBehaviorEvent
184
- | NativeBehaviorEvent
176
+ behaviorEvent: BehaviorEvent
185
177
  editor: PortableTextSlateEditor
186
178
  defaultActionCallback?: () => void
187
179
  nativeEvent?: {preventDefault: () => void}
188
180
  }
189
- | {
190
- type: 'custom behavior event'
191
- behaviorEvent: CustomBehaviorEvent
192
- editor: PortableTextSlateEditor
193
- nativeEvent?: {preventDefault: () => void}
194
- }
195
- | CustomBehaviorEvent
196
- | ExternalBehaviorEvent
197
- | ExternalEditorEvent
198
181
  | MutationEvent
199
182
  | InternalPatchEvent
200
183
  | NamespaceEvent<EditorEmittedEvent, 'notify'>
201
184
  | NamespaceEvent<UnsetEvent, 'notify'>
202
- | SyntheticBehaviorEvent
203
185
  | {
204
186
  type: 'dragstart'
205
187
  origin: Pick<EventPosition, 'selection'>
@@ -213,15 +195,9 @@ export type InternalEditorEvent =
213
195
  */
214
196
  export type InternalEditorEmittedEvent =
215
197
  | EditorEmittedEvent
216
- | ExternalBehaviorEvent
217
198
  | InternalPatchEvent
218
199
  | PatchesEvent
219
200
  | UnsetEvent
220
- | {
221
- type: 'custom.*'
222
- event: CustomBehaviorEvent
223
- }
224
- | SyntheticBehaviorEvent
225
201
 
226
202
  /**
227
203
  * @internal
@@ -318,7 +294,7 @@ export const editorMachine = setup({
318
294
  pendingEvents: [],
319
295
  }),
320
296
  'handle behavior event': ({context, event, self}) => {
321
- assertEvent(event, ['behavior event', 'custom behavior event'])
297
+ assertEvent(event, ['behavior event'])
322
298
 
323
299
  performEvent({
324
300
  behaviors: [...context.behaviors.values(), ...defaultBehaviors],
@@ -447,54 +423,6 @@ export const editorMachine = setup({
447
423
  'behavior event': {
448
424
  actions: 'handle behavior event',
449
425
  },
450
- 'custom behavior event': {
451
- actions: 'handle behavior event',
452
- },
453
- 'annotation.*': {
454
- actions: emit(({event}) => event),
455
- },
456
- 'block.*': {
457
- actions: emit(({event}) => event),
458
- },
459
- 'blur': {
460
- actions: emit(({event}) => event),
461
- },
462
- 'custom.*': {
463
- actions: emit(({event}) => ({type: 'custom.*', event})),
464
- },
465
- 'decorator.*': {
466
- actions: emit(({event}) => event),
467
- },
468
- 'delete.*': {
469
- actions: emit(({event}) => event),
470
- },
471
- 'focus': {
472
- actions: emit(({event}) => event),
473
- },
474
- 'history.*': {
475
- actions: emit(({event}) => event),
476
- },
477
- 'insert.*': {
478
- actions: emit(({event}) => event),
479
- },
480
- 'list item.*': {
481
- actions: emit(({event}) => event),
482
- },
483
- 'move.*': {
484
- actions: emit(({event}) => event),
485
- },
486
- 'select': {
487
- actions: emit(({event}) => event),
488
- },
489
- 'select.*': {
490
- actions: emit(({event}) => event),
491
- },
492
- 'style.*': {
493
- actions: emit(({event}) => event),
494
- },
495
- 'text block.*': {
496
- actions: emit(({event}) => event),
497
- },
498
426
  },
499
427
  initial: 'idle',
500
428
  states: {
@@ -1,10 +1,10 @@
1
1
  import {useSelector} from '@xstate/react'
2
+ import {slateChildrenToBlocks} from '../internal-utils/slate-children-to-blocks'
2
3
  import type {PortableTextSlateEditor} from '../types/editor'
3
4
  import type {Editor, InternalEditor} from './create-editor'
4
5
  import type {EditorActor} from './editor-machine'
5
6
  import type {EditorSnapshot} from './editor-snapshot'
6
7
  import {getActiveDecorators} from './get-active-decorators'
7
- import {getValue} from './get-value'
8
8
 
9
9
  function defaultCompare<T>(a: T, b: T) {
10
10
  return a === b
@@ -77,7 +77,10 @@ export function getEditorSnapshot({
77
77
  readOnly: editorActorSnapshot.matches({'edit mode': 'read only'}),
78
78
  schema: editorActorSnapshot.context.schema,
79
79
  selection: editorActorSnapshot.context.selection,
80
- value: getValue({editorActorSnapshot, slateEditorInstance}),
80
+ value: slateChildrenToBlocks(
81
+ editorActorSnapshot.context.schema,
82
+ slateEditorInstance.children,
83
+ ),
81
84
  },
82
85
  beta: {
83
86
  hasTag: (tag) => editorActorSnapshot.hasTag(tag),
@@ -1,9 +1,8 @@
1
1
  import type {PortableTextBlock} from '@sanity/types'
2
2
  import type {Converter} from '../converters/converter.types'
3
3
  import type {EventPosition} from '../internal-utils/event-position'
4
- import {toPortableTextRange} from '../internal-utils/ranges'
5
- import {fromSlateValue} from '../internal-utils/values'
6
- import {KEY_TO_VALUE_ELEMENT} from '../internal-utils/weakMaps'
4
+ import {slateChildrenToBlocks} from '../internal-utils/slate-children-to-blocks'
5
+ import {slateRangeToSelection} from '../internal-utils/slate-utils'
7
6
  import type {EditorSelection, PortableTextSlateEditor} from '../types/editor'
8
7
  import type {EditorSchema} from './define-schema'
9
8
  import type {HasTag} from './editor-machine'
@@ -62,12 +61,14 @@ export function createEditorSnapshot({
62
61
  }
63
62
  | undefined
64
63
  }) {
65
- const value = fromSlateValue(
66
- editor.children,
67
- schema.block.name,
68
- KEY_TO_VALUE_ELEMENT.get(editor),
69
- )
70
- const selection = toPortableTextRange(value, editor.selection, schema)
64
+ const value = slateChildrenToBlocks(schema, editor.children)
65
+ const selection = editor.selection
66
+ ? slateRangeToSelection({
67
+ schema,
68
+ editor,
69
+ range: editor.selection,
70
+ })
71
+ : null
71
72
 
72
73
  const context = {
73
74
  activeDecorators: getActiveDecorators({
@@ -1,81 +1,16 @@
1
1
  import {Editor} from 'slate'
2
2
  import {insertSoftBreakActionImplementation} from '../../behavior-actions/behavior.action.insert-break'
3
3
  import {performAction} from '../../behavior-actions/behavior.actions'
4
- import {toPortableTextRange} from '../../internal-utils/ranges'
5
- import {fromSlateValue} from '../../internal-utils/values'
6
- import {KEY_TO_VALUE_ELEMENT} from '../../internal-utils/weakMaps'
4
+ import {slateRangeToSelection} from '../../internal-utils/slate-utils'
7
5
  import type {EditorActor} from '../editor-machine'
8
6
  import {isApplyingBehaviorActions} from '../with-applying-behavior-actions'
9
7
 
10
- export function createWithEventListeners(
11
- editorActor: EditorActor,
12
- subscriptions: Array<() => () => void>,
13
- ) {
8
+ export function createWithEventListeners(editorActor: EditorActor) {
14
9
  return function withEventListeners(editor: Editor) {
15
10
  if (editorActor.getSnapshot().context.maxBlocks !== undefined) {
16
11
  return editor
17
12
  }
18
13
 
19
- subscriptions.push(() => {
20
- const subscription = editorActor.on('*', (event) => {
21
- switch (event.type) {
22
- // These events are not relevant for Behaviors
23
- case 'blurred':
24
- case 'done loading':
25
- case 'editable':
26
- case 'error':
27
- case 'focused':
28
- case 'invalid value':
29
- case 'loading':
30
- case 'mutation':
31
- case 'patch':
32
- case 'internal.patch':
33
- case 'patches':
34
- case 'read only':
35
- case 'ready':
36
- case 'selection':
37
- case 'value changed':
38
- case 'unset':
39
- break
40
-
41
- case 'custom.*':
42
- editorActor.send({
43
- type: 'custom behavior event',
44
- behaviorEvent: event.event,
45
- editor,
46
- })
47
- break
48
-
49
- case 'insert.block object':
50
- editorActor.send({
51
- type: 'behavior event',
52
- behaviorEvent: {
53
- type: 'insert.block',
54
- block: {
55
- _type: event.blockObject.name,
56
- ...(event.blockObject.value ?? {}),
57
- },
58
- placement: event.placement,
59
- },
60
- editor,
61
- })
62
- break
63
-
64
- default:
65
- editorActor.send({
66
- type: 'behavior event',
67
- behaviorEvent: event,
68
- editor,
69
- })
70
- break
71
- }
72
- })
73
-
74
- return () => {
75
- subscription.unsubscribe()
76
- }
77
- })
78
-
79
14
  const {deleteBackward, deleteForward, insertBreak, insertText, select} =
80
15
  editor
81
16
 
@@ -225,15 +160,11 @@ export function createWithEventListeners(
225
160
  type: 'behavior event',
226
161
  behaviorEvent: {
227
162
  type: 'select',
228
- selection: toPortableTextRange(
229
- fromSlateValue(
230
- editor.children,
231
- editorActor.getSnapshot().context.schema.block.name,
232
- KEY_TO_VALUE_ELEMENT.get(editor),
233
- ),
163
+ selection: slateRangeToSelection({
164
+ schema: editorActor.getSnapshot().context.schema,
165
+ editor,
234
166
  range,
235
- editorActor.getSnapshot().context.schema,
236
- ),
167
+ }),
237
168
  },
238
169
  editor,
239
170
  defaultActionCallback: () => {
@@ -20,8 +20,12 @@ import type {DOMNode} from 'slate-dom'
20
20
  import {ReactEditor} from 'slate-react'
21
21
  import type {BehaviorActionImplementation} from '../../behavior-actions/behavior.actions'
22
22
  import {debugWithName} from '../../internal-utils/debug'
23
- import {toPortableTextRange, toSlateRange} from '../../internal-utils/ranges'
24
- import {isListItemActive, isStyleActive} from '../../internal-utils/slate-utils'
23
+ import {toSlateRange} from '../../internal-utils/ranges'
24
+ import {
25
+ isListItemActive,
26
+ isStyleActive,
27
+ slateRangeToSelection,
28
+ } from '../../internal-utils/slate-utils'
25
29
  import {fromSlateValue, toSlateValue} from '../../internal-utils/values'
26
30
  import {
27
31
  KEY_TO_VALUE_ELEMENT,
@@ -187,17 +191,13 @@ export function createEditableAPI(
187
191
  editor,
188
192
  })
189
193
 
190
- return (
191
- toPortableTextRange(
192
- fromSlateValue(
193
- editor.children,
194
- types.block.name,
195
- KEY_TO_VALUE_ELEMENT.get(editor),
196
- ),
197
- editor.selection,
198
- types,
199
- )?.focus.path ?? []
200
- )
194
+ return editor.selection
195
+ ? (slateRangeToSelection({
196
+ schema: editorActor.getSnapshot().context.schema,
197
+ editor,
198
+ range: editor.selection,
199
+ })?.focus.path ?? [])
200
+ : []
201
201
  }
202
202
 
203
203
  if (!editor.selection) {
@@ -256,17 +256,13 @@ export function createEditableAPI(
256
256
  })
257
257
  editor.onChange()
258
258
 
259
- return (
260
- toPortableTextRange(
261
- fromSlateValue(
262
- editor.children,
263
- types.block.name,
264
- KEY_TO_VALUE_ELEMENT.get(editor),
265
- ),
266
- editor.selection,
267
- types,
268
- )?.focus.path || []
269
- )
259
+ return editor.selection
260
+ ? (slateRangeToSelection({
261
+ schema: editorActor.getSnapshot().context.schema,
262
+ editor,
263
+ range: editor.selection,
264
+ })?.focus.path ?? [])
265
+ : []
270
266
  },
271
267
  insertBlock: <TSchemaType extends {name: string}>(
272
268
  type: TSchemaType,
@@ -285,17 +281,13 @@ export function createEditableAPI(
285
281
  editor,
286
282
  })
287
283
 
288
- return (
289
- toPortableTextRange(
290
- fromSlateValue(
291
- editor.children,
292
- types.block.name,
293
- KEY_TO_VALUE_ELEMENT.get(editor),
294
- ),
295
- editor.selection,
296
- types,
297
- )?.focus.path ?? []
298
- )
284
+ return editor.selection
285
+ ? (slateRangeToSelection({
286
+ schema: editorActor.getSnapshot().context.schema,
287
+ editor,
288
+ range: editor.selection,
289
+ })?.focus.path ?? [])
290
+ : []
299
291
  },
300
292
  hasBlockStyle: (style: string): boolean => {
301
293
  try {
@@ -511,15 +503,11 @@ export function createEditableAPI(
511
503
  if (existing) {
512
504
  return existing
513
505
  }
514
- ptRange = toPortableTextRange(
515
- fromSlateValue(
516
- editor.children,
517
- types.block.name,
518
- KEY_TO_VALUE_ELEMENT.get(editor),
519
- ),
520
- editor.selection,
521
- types,
522
- )
506
+ ptRange = slateRangeToSelection({
507
+ schema: editorActor.getSnapshot().context.schema,
508
+ editor,
509
+ range: editor.selection,
510
+ })
523
511
  SLATE_TO_PORTABLE_TEXT_RANGE.set(editor.selection, ptRange)
524
512
  }
525
513
  return ptRange
@@ -1,15 +1,8 @@
1
1
  import type {BaseRange} from 'slate'
2
2
  import {debugWithName} from '../../internal-utils/debug'
3
- import {
4
- toPortableTextRange,
5
- type ObjectWithKeyAndType,
6
- } from '../../internal-utils/ranges'
3
+ import {slateRangeToSelection} from '../../internal-utils/slate-utils'
7
4
  import {SLATE_TO_PORTABLE_TEXT_RANGE} from '../../internal-utils/weakMaps'
8
- import type {
9
- EditorSelection,
10
- PortableTextMemberSchemaTypes,
11
- PortableTextSlateEditor,
12
- } from '../../types/editor'
5
+ import type {EditorSelection, PortableTextSlateEditor} from '../../types/editor'
13
6
  import type {EditorActor} from '../editor-machine'
14
7
 
15
8
  const debug = debugWithName('plugin:withPortableTextSelections')
@@ -18,7 +11,6 @@ const debugVerbose = debug.enabled && false
18
11
  // This plugin will make sure that we emit a PT selection whenever the editor has changed.
19
12
  export function createWithPortableTextSelections(
20
13
  editorActor: EditorActor,
21
- types: PortableTextMemberSchemaTypes,
22
14
  ): (editor: PortableTextSlateEditor) => PortableTextSlateEditor {
23
15
  let prevSelection: BaseRange | null = null
24
16
  return function withPortableTextSelections(
@@ -26,14 +18,17 @@ export function createWithPortableTextSelections(
26
18
  ): PortableTextSlateEditor {
27
19
  const emitPortableTextSelection = () => {
28
20
  if (prevSelection !== editor.selection) {
29
- let ptRange: EditorSelection = null
21
+ let ptRange: EditorSelection | null = null
30
22
  if (editor.selection) {
31
23
  const existing = SLATE_TO_PORTABLE_TEXT_RANGE.get(editor.selection)
32
24
  if (existing) {
33
25
  ptRange = existing
34
26
  } else {
35
- const value = editor.children satisfies ObjectWithKeyAndType[]
36
- ptRange = toPortableTextRange(value, editor.selection, types)
27
+ ptRange = slateRangeToSelection({
28
+ schema: editorActor.getSnapshot().context.schema,
29
+ editor,
30
+ range: editor.selection,
31
+ })
37
32
  SLATE_TO_PORTABLE_TEXT_RANGE.set(editor.selection, ptRange)
38
33
  }
39
34
  }
@@ -65,14 +65,9 @@ export const withPlugins = <T extends Editor>(
65
65
  editorActor,
66
66
  schemaTypes,
67
67
  })
68
- const withPortableTextSelections = createWithPortableTextSelections(
69
- editorActor,
70
- schemaTypes,
71
- )
72
- const withEventListeners = createWithEventListeners(
73
- editorActor,
74
- options.subscriptions,
75
- )
68
+ const withPortableTextSelections =
69
+ createWithPortableTextSelections(editorActor)
70
+ const withEventListeners = createWithEventListeners(editorActor)
76
71
 
77
72
  // Ordering is important here, selection dealing last, data manipulation in the middle and core model stuff first.
78
73
  return withEventListeners(
@@ -3,9 +3,12 @@ import {DOMEditor, isDOMNode} from 'slate-dom'
3
3
  import type {EditorSchema, EditorSelection} from '..'
4
4
  import type {PortableTextSlateEditor} from '../types/editor'
5
5
  import * as utils from '../utils'
6
- import {toPortableTextRange} from './ranges'
7
- import {getFirstBlock, getLastBlock, getNodeBlock} from './slate-utils'
8
- import {fromSlateValue} from './values'
6
+ import {
7
+ getFirstBlock,
8
+ getLastBlock,
9
+ getNodeBlock,
10
+ slateRangeToSelection,
11
+ } from './slate-utils'
9
12
 
10
13
  export type EventPosition = {
11
14
  block: 'start' | 'end'
@@ -175,11 +178,11 @@ export function getEventSelection({
175
178
  const range = getSlateRangeFromEvent(slateEditor, event)
176
179
 
177
180
  const selection = range
178
- ? toPortableTextRange(
179
- fromSlateValue(slateEditor.children, schema.block.name),
180
- range,
181
+ ? slateRangeToSelection({
181
182
  schema,
182
- )
183
+ editor: slateEditor,
184
+ range,
185
+ })
183
186
  : null
184
187
 
185
188
  return selection