@portabletext/editor 1.51.0 → 1.52.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 (37) hide show
  1. package/lib/_chunks-es/selector.is-selection-expanded.js +1 -1
  2. package/lib/behaviors/index.cjs.map +1 -1
  3. package/lib/behaviors/index.d.cts +26 -5
  4. package/lib/behaviors/index.d.ts +26 -5
  5. package/lib/behaviors/index.js.map +1 -1
  6. package/lib/index.cjs +434 -309
  7. package/lib/index.cjs.map +1 -1
  8. package/lib/index.d.cts +360 -31
  9. package/lib/index.d.ts +360 -31
  10. package/lib/index.js +455 -330
  11. package/lib/index.js.map +1 -1
  12. package/lib/plugins/index.d.cts +26 -5
  13. package/lib/plugins/index.d.ts +26 -5
  14. package/lib/selectors/index.d.cts +0 -14
  15. package/lib/selectors/index.d.ts +0 -14
  16. package/lib/utils/index.d.cts +0 -14
  17. package/lib/utils/index.d.ts +0 -14
  18. package/package.json +3 -3
  19. package/src/behaviors/behavior.abstract.delete.ts +0 -1
  20. package/src/behaviors/behavior.abstract.ts +0 -113
  21. package/src/behaviors/behavior.core.block-element.ts +9 -3
  22. package/src/behaviors/behavior.core.dnd.ts +328 -1
  23. package/src/behaviors/behavior.perform-event.ts +10 -0
  24. package/src/behaviors/behavior.types.action.ts +2 -0
  25. package/src/behaviors/behavior.types.event.ts +4 -0
  26. package/src/behaviors/behavior.types.guard.ts +2 -0
  27. package/src/converters/converter.portable-text.ts +2 -7
  28. package/src/converters/converter.text-html.ts +1 -3
  29. package/src/converters/converter.text-plain.ts +3 -5
  30. package/src/editor/Editable.tsx +6 -133
  31. package/src/editor/editor-machine.ts +15 -8
  32. package/src/editor/editor-selector.ts +0 -1
  33. package/src/editor/editor-snapshot.ts +0 -13
  34. package/src/internal-utils/create-test-snapshot.ts +0 -1
  35. package/src/internal-utils/event-position.ts +41 -27
  36. package/src/internal-utils/selection-elements.ts +108 -0
  37. package/src/operations/behavior.operation.decorator.add.ts +0 -1
@@ -266,6 +266,7 @@ declare type BehaviorActionSet<TBehaviorEvent, TGuardResponse> = (
266
266
  payload: {
267
267
  snapshot: EditorSnapshot
268
268
  event: TBehaviorEvent
269
+ dom: EditorDom
269
270
  },
270
271
  guardResponse: TGuardResponse,
271
272
  ) => Array<BehaviorAction>
@@ -289,6 +290,7 @@ declare type BehaviorEventTypeNamespace =
289
290
  declare type BehaviorGuard<TBehaviorEvent, TGuardResponse> = (payload: {
290
291
  snapshot: EditorSnapshot
291
292
  event: TBehaviorEvent
293
+ dom: EditorDom
292
294
  }) => TGuardResponse | false
293
295
 
294
296
  /**
@@ -423,6 +425,8 @@ declare type DragBehaviorEvent =
423
425
  | {
424
426
  type: StrictExtract<NativeBehaviorEventType, 'drag.dragstart'>
425
427
  originEvent: {
428
+ clientX: number
429
+ clientY: number
426
430
  dataTransfer: DataTransfer
427
431
  }
428
432
  position: Pick<EventPosition, 'selection'>
@@ -451,6 +455,7 @@ declare type DragBehaviorEvent =
451
455
  originEvent: {
452
456
  dataTransfer: DataTransfer
453
457
  }
458
+ dragOrigin?: Pick<EventPosition, 'selection'>
454
459
  position: EventPosition
455
460
  }
456
461
  | {
@@ -458,6 +463,7 @@ declare type DragBehaviorEvent =
458
463
  originEvent: {
459
464
  dataTransfer: DataTransfer
460
465
  }
466
+ dragOrigin?: Pick<EventPosition, 'selection'>
461
467
  position: EventPosition
462
468
  }
463
469
  | {
@@ -492,6 +498,26 @@ declare type EditorContext = {
492
498
  value: Array<PortableTextBlock>
493
499
  }
494
500
 
501
+ declare type EditorDom = {
502
+ getBlockNodes: (snapshot: EditorSnapshot) => Array<Node>
503
+ getChildNodes: (snapshot: EditorSnapshot) => Array<Node>
504
+ /**
505
+ * Let the Editor set the drag ghost. This is to be sure that it will get
506
+ * properly removed again when the drag ends.
507
+ */
508
+ setDragGhost: ({
509
+ event,
510
+ ghost,
511
+ }: {
512
+ event: PickFromUnion<BehaviorEvent, 'type', 'drag.dragstart'>
513
+ ghost: {
514
+ element: HTMLElement
515
+ x: number
516
+ y: number
517
+ }
518
+ }) => void
519
+ }
520
+
495
521
  /**
496
522
  * @public
497
523
  */
@@ -648,11 +674,6 @@ declare type EditorSnapshot = {
648
674
  beta: {
649
675
  activeAnnotations: Array<string>
650
676
  activeDecorators: Array<string>
651
- internalDrag:
652
- | {
653
- origin: Pick<EventPosition, 'selection'>
654
- }
655
- | undefined
656
677
  }
657
678
  }
658
679
 
@@ -266,6 +266,7 @@ declare type BehaviorActionSet<TBehaviorEvent, TGuardResponse> = (
266
266
  payload: {
267
267
  snapshot: EditorSnapshot
268
268
  event: TBehaviorEvent
269
+ dom: EditorDom
269
270
  },
270
271
  guardResponse: TGuardResponse,
271
272
  ) => Array<BehaviorAction>
@@ -289,6 +290,7 @@ declare type BehaviorEventTypeNamespace =
289
290
  declare type BehaviorGuard<TBehaviorEvent, TGuardResponse> = (payload: {
290
291
  snapshot: EditorSnapshot
291
292
  event: TBehaviorEvent
293
+ dom: EditorDom
292
294
  }) => TGuardResponse | false
293
295
 
294
296
  /**
@@ -423,6 +425,8 @@ declare type DragBehaviorEvent =
423
425
  | {
424
426
  type: StrictExtract<NativeBehaviorEventType, 'drag.dragstart'>
425
427
  originEvent: {
428
+ clientX: number
429
+ clientY: number
426
430
  dataTransfer: DataTransfer
427
431
  }
428
432
  position: Pick<EventPosition, 'selection'>
@@ -451,6 +455,7 @@ declare type DragBehaviorEvent =
451
455
  originEvent: {
452
456
  dataTransfer: DataTransfer
453
457
  }
458
+ dragOrigin?: Pick<EventPosition, 'selection'>
454
459
  position: EventPosition
455
460
  }
456
461
  | {
@@ -458,6 +463,7 @@ declare type DragBehaviorEvent =
458
463
  originEvent: {
459
464
  dataTransfer: DataTransfer
460
465
  }
466
+ dragOrigin?: Pick<EventPosition, 'selection'>
461
467
  position: EventPosition
462
468
  }
463
469
  | {
@@ -492,6 +498,26 @@ declare type EditorContext = {
492
498
  value: Array<PortableTextBlock>
493
499
  }
494
500
 
501
+ declare type EditorDom = {
502
+ getBlockNodes: (snapshot: EditorSnapshot) => Array<Node>
503
+ getChildNodes: (snapshot: EditorSnapshot) => Array<Node>
504
+ /**
505
+ * Let the Editor set the drag ghost. This is to be sure that it will get
506
+ * properly removed again when the drag ends.
507
+ */
508
+ setDragGhost: ({
509
+ event,
510
+ ghost,
511
+ }: {
512
+ event: PickFromUnion<BehaviorEvent, 'type', 'drag.dragstart'>
513
+ ghost: {
514
+ element: HTMLElement
515
+ x: number
516
+ y: number
517
+ }
518
+ }) => void
519
+ }
520
+
495
521
  /**
496
522
  * @public
497
523
  */
@@ -648,11 +674,6 @@ declare type EditorSnapshot = {
648
674
  beta: {
649
675
  activeAnnotations: Array<string>
650
676
  activeDecorators: Array<string>
651
- internalDrag:
652
- | {
653
- origin: Pick<EventPosition, 'selection'>
654
- }
655
- | undefined
656
677
  }
657
678
  }
658
679
 
@@ -184,23 +184,9 @@ declare type EditorSnapshot = {
184
184
  beta: {
185
185
  activeAnnotations: Array<string>
186
186
  activeDecorators: Array<string>
187
- internalDrag:
188
- | {
189
- origin: Pick<EventPosition, 'selection'>
190
- }
191
- | undefined
192
187
  }
193
188
  }
194
189
 
195
- declare type EventPosition = {
196
- block: 'start' | 'end'
197
- /**
198
- * Did the event origin from the editor DOM node itself or from a child node?
199
- */
200
- isEditor: boolean
201
- selection: NonNullable<EditorSelection>
202
- }
203
-
204
190
  /**
205
191
  * @public
206
192
  */
@@ -184,23 +184,9 @@ declare type EditorSnapshot = {
184
184
  beta: {
185
185
  activeAnnotations: Array<string>
186
186
  activeDecorators: Array<string>
187
- internalDrag:
188
- | {
189
- origin: Pick<EventPosition, 'selection'>
190
- }
191
- | undefined
192
187
  }
193
188
  }
194
189
 
195
- declare type EventPosition = {
196
- block: 'start' | 'end'
197
- /**
198
- * Did the event origin from the editor DOM node itself or from a child node?
199
- */
200
- isEditor: boolean
201
- selection: NonNullable<EditorSelection>
202
- }
203
-
204
190
  /**
205
191
  * @public
206
192
  */
@@ -242,23 +242,9 @@ declare type EditorSnapshot = {
242
242
  beta: {
243
243
  activeAnnotations: Array<string>
244
244
  activeDecorators: Array<string>
245
- internalDrag:
246
- | {
247
- origin: Pick<EventPosition, 'selection'>
248
- }
249
- | undefined
250
245
  }
251
246
  }
252
247
 
253
- declare type EventPosition = {
254
- block: 'start' | 'end'
255
- /**
256
- * Did the event origin from the editor DOM node itself or from a child node?
257
- */
258
- isEditor: boolean
259
- selection: NonNullable<EditorSelection>
260
- }
261
-
262
248
  /**
263
249
  * @public
264
250
  */
@@ -242,23 +242,9 @@ declare type EditorSnapshot = {
242
242
  beta: {
243
243
  activeAnnotations: Array<string>
244
244
  activeDecorators: Array<string>
245
- internalDrag:
246
- | {
247
- origin: Pick<EventPosition, 'selection'>
248
- }
249
- | undefined
250
245
  }
251
246
  }
252
247
 
253
- declare type EventPosition = {
254
- block: 'start' | 'end'
255
- /**
256
- * Did the event origin from the editor DOM node itself or from a child node?
257
- */
258
- isEditor: boolean
259
- selection: NonNullable<EditorSelection>
260
- }
261
-
262
248
  /**
263
249
  * @public
264
250
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/editor",
3
- "version": "1.51.0",
3
+ "version": "1.52.0",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -80,8 +80,8 @@
80
80
  "slate-react": "0.114.2",
81
81
  "use-effect-event": "^2.0.0",
82
82
  "xstate": "^5.19.4",
83
- "@portabletext/patches": "1.1.4",
84
- "@portabletext/block-tools": "1.1.29"
83
+ "@portabletext/block-tools": "1.1.29",
84
+ "@portabletext/patches": "1.1.4"
85
85
  },
86
86
  "devDependencies": {
87
87
  "@portabletext/toolkit": "^2.0.17",
@@ -80,7 +80,6 @@ export const abstractDeleteBehaviors = [
80
80
  beta: {
81
81
  activeAnnotations: [],
82
82
  activeDecorators: [],
83
- internalDrag: undefined,
84
83
  },
85
84
  context: {
86
85
  converters: [],
@@ -198,120 +198,7 @@ export const abstractBehaviors = [
198
198
  ],
199
199
  ],
200
200
  }),
201
- defineBehavior({
202
- on: 'drag.drop',
203
- guard: ({snapshot, event}) => {
204
- const dragOrigin = snapshot.beta.internalDrag?.origin
205
- const dropPosition = event.position.selection
206
- const droppingOnDragOrigin = dragOrigin
207
- ? selectors.isOverlappingSelection(dropPosition)({
208
- ...snapshot,
209
- context: {
210
- ...snapshot.context,
211
- selection: dragOrigin.selection,
212
- },
213
- })
214
- : false
215
- return droppingOnDragOrigin
216
- },
217
- actions: [],
218
- }),
219
- defineBehavior({
220
- on: 'drag.drop',
221
- actions: [
222
- ({event}) => [
223
- raise({
224
- type: 'select',
225
- at: event.position.selection,
226
- }),
227
- raise({
228
- type: 'deserialize',
229
- originEvent: event,
230
- }),
231
- ],
232
- ],
233
- }),
234
- defineBehavior({
235
- on: 'deserialization.success',
236
- guard: ({snapshot, event}) => {
237
- if (
238
- event.originEvent.type !== 'drag.drop' ||
239
- snapshot.beta.internalDrag === undefined
240
- ) {
241
- return false
242
- }
243
201
 
244
- const dragOrigin = snapshot.beta.internalDrag.origin
245
- const dropPosition = event.originEvent.position.selection
246
- const droppingOnDragOrigin = dragOrigin
247
- ? selectors.isOverlappingSelection(dropPosition)({
248
- ...snapshot,
249
- context: {
250
- ...snapshot.context,
251
- selection: dragOrigin.selection,
252
- },
253
- })
254
- : false
255
-
256
- const draggingEntireBlocks = selectors.isSelectingEntireBlocks({
257
- ...snapshot,
258
- context: {
259
- ...snapshot.context,
260
- selection: dragOrigin.selection,
261
- },
262
- })
263
-
264
- const draggedBlocks = selectors.getSelectedBlocks({
265
- ...snapshot,
266
- context: {
267
- ...snapshot.context,
268
- selection: dragOrigin.selection,
269
- },
270
- })
271
-
272
- if (!droppingOnDragOrigin) {
273
- return {
274
- draggingEntireBlocks,
275
- draggedBlocks,
276
- dragOrigin,
277
- originEvent: event.originEvent,
278
- }
279
- }
280
-
281
- return false
282
- },
283
- actions: [
284
- (
285
- {event},
286
- {draggingEntireBlocks, draggedBlocks, dragOrigin, originEvent},
287
- ) => [
288
- ...(draggingEntireBlocks
289
- ? draggedBlocks.map((block) =>
290
- raise({
291
- type: 'delete.block',
292
- at: block.path,
293
- }),
294
- )
295
- : [
296
- raise({
297
- type: 'delete',
298
- at: dragOrigin.selection,
299
- }),
300
- ]),
301
- raise({
302
- type: 'insert.blocks',
303
- blocks: event.data,
304
- placement: draggingEntireBlocks
305
- ? originEvent.position.block === 'start'
306
- ? 'before'
307
- : originEvent.position.block === 'end'
308
- ? 'after'
309
- : 'auto'
310
- : 'auto',
311
- }),
312
- ],
313
- ],
314
- }),
315
202
  /**
316
203
  * If we are pasting text/plain into a text block then we can probably
317
204
  * assume that the intended behavior is that the pasted text inherits
@@ -1,3 +1,4 @@
1
+ import {getDragSelection} from '../internal-utils/drag-selection'
1
2
  import type {EventPositionBlock} from '../internal-utils/event-position'
2
3
  import {corePriority} from '../priority/priority.core'
3
4
  import {createEditorPriority} from '../priority/priority.types'
@@ -31,17 +32,22 @@ export function createCoreBlockElementBehaviorsConfig({
31
32
  return false
32
33
  }
33
34
 
34
- const dragOrigin = snapshot.beta.internalDrag?.origin
35
+ const dragOrigin = event.dragOrigin
35
36
 
36
37
  if (!dragOrigin) {
37
38
  return false
38
39
  }
39
40
 
41
+ const dragSelection = getDragSelection({
42
+ eventSelection: dragOrigin.selection,
43
+ snapshot,
44
+ })
45
+
40
46
  const draggedBlocks = selectors.getSelectedBlocks({
41
47
  ...snapshot,
42
48
  context: {
43
49
  ...snapshot.context,
44
- selection: dragOrigin.selection,
50
+ selection: dragSelection,
45
51
  },
46
52
  })
47
53
 
@@ -55,7 +61,7 @@ export function createCoreBlockElementBehaviorsConfig({
55
61
  ...snapshot,
56
62
  context: {
57
63
  ...snapshot.context,
58
- selection: dragOrigin.selection,
64
+ selection: dragSelection,
59
65
  },
60
66
  })
61
67