@portabletext/editor 1.43.1 → 1.44.1

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 (75) hide show
  1. package/lib/_chunks-cjs/behavior.markdown.cjs +1 -1
  2. package/lib/_chunks-cjs/editor-provider.cjs +555 -794
  3. package/lib/_chunks-cjs/editor-provider.cjs.map +1 -1
  4. package/lib/_chunks-cjs/parse-blocks.cjs +151 -0
  5. package/lib/_chunks-cjs/parse-blocks.cjs.map +1 -0
  6. package/lib/_chunks-cjs/selector.is-overlapping-selection.cjs +2 -1
  7. package/lib/_chunks-cjs/selector.is-overlapping-selection.cjs.map +1 -1
  8. package/lib/_chunks-cjs/{selector.is-active-style.cjs → selector.is-selecting-entire-blocks.cjs} +47 -11
  9. package/lib/_chunks-cjs/selector.is-selecting-entire-blocks.cjs.map +1 -0
  10. package/lib/_chunks-cjs/util.merge-text-blocks.cjs +3 -3
  11. package/lib/_chunks-cjs/util.merge-text-blocks.cjs.map +1 -1
  12. package/lib/_chunks-cjs/util.selection-point-to-block-offset.cjs +0 -149
  13. package/lib/_chunks-cjs/util.selection-point-to-block-offset.cjs.map +1 -1
  14. package/lib/_chunks-es/behavior.markdown.js +1 -1
  15. package/lib/_chunks-es/editor-provider.js +539 -777
  16. package/lib/_chunks-es/editor-provider.js.map +1 -1
  17. package/lib/_chunks-es/parse-blocks.js +152 -0
  18. package/lib/_chunks-es/parse-blocks.js.map +1 -0
  19. package/lib/_chunks-es/selector.is-overlapping-selection.js +2 -1
  20. package/lib/_chunks-es/selector.is-overlapping-selection.js.map +1 -1
  21. package/lib/_chunks-es/{selector.is-active-style.js → selector.is-selecting-entire-blocks.js} +49 -12
  22. package/lib/_chunks-es/selector.is-selecting-entire-blocks.js.map +1 -0
  23. package/lib/_chunks-es/util.merge-text-blocks.js +1 -1
  24. package/lib/_chunks-es/util.selection-point-to-block-offset.js +0 -149
  25. package/lib/_chunks-es/util.selection-point-to-block-offset.js.map +1 -1
  26. package/lib/behaviors/index.d.cts +2819 -6421
  27. package/lib/behaviors/index.d.ts +2819 -6421
  28. package/lib/index.cjs +4 -4
  29. package/lib/index.cjs.map +1 -1
  30. package/lib/index.d.cts +2496 -6025
  31. package/lib/index.d.ts +2496 -6025
  32. package/lib/index.js +3 -3
  33. package/lib/plugins/index.cjs +1 -1
  34. package/lib/plugins/index.d.cts +2496 -6025
  35. package/lib/plugins/index.d.ts +2496 -6025
  36. package/lib/plugins/index.js +1 -1
  37. package/lib/selectors/index.cjs +12 -11
  38. package/lib/selectors/index.cjs.map +1 -1
  39. package/lib/selectors/index.d.cts +2464 -6018
  40. package/lib/selectors/index.d.ts +2464 -6018
  41. package/lib/selectors/index.js +2 -1
  42. package/lib/utils/index.d.cts +2458 -6022
  43. package/lib/utils/index.d.ts +2458 -6022
  44. package/package.json +2 -2
  45. package/src/behavior-actions/behavior.actions.ts +2 -152
  46. package/src/behaviors/behavior.default.ts +52 -120
  47. package/src/behaviors/behavior.internal.annotation.ts +26 -0
  48. package/src/behaviors/behavior.internal.decorator.ts +47 -0
  49. package/src/behaviors/behavior.internal.insert.ts +118 -0
  50. package/src/behaviors/behavior.internal.list-item.ts +61 -0
  51. package/src/behaviors/behavior.internal.select.ts +62 -0
  52. package/src/behaviors/behavior.internal.style.ts +54 -0
  53. package/src/behaviors/behavior.perform-event.ts +15 -13
  54. package/src/behaviors/behavior.types.event.ts +155 -108
  55. package/src/editor/create-editor.ts +2 -2
  56. package/src/editor/editor-machine.ts +3 -4
  57. package/src/editor/plugins/createWithEditableAPI.ts +1 -32
  58. package/src/editor/plugins/createWithPortableTextMarkModel.ts +0 -29
  59. package/src/internal-utils/slate-utils.ts +52 -0
  60. package/src/plugins/plugin.internal.editor-actor-ref.tsx +15 -0
  61. package/src/plugins/plugin.internal.slate-editor-ref.tsx +15 -0
  62. package/src/selectors/index.ts +2 -1
  63. package/src/selectors/selector.get-selected-text-blocks.ts +67 -0
  64. package/lib/_chunks-cjs/selector.is-active-style.cjs.map +0 -1
  65. package/lib/_chunks-es/selector.is-active-style.js.map +0 -1
  66. package/src/behavior-actions/behavior.action.data-transfer-set.ts +0 -7
  67. package/src/behavior-actions/behavior.action.deserialization.failure.ts +0 -9
  68. package/src/behavior-actions/behavior.action.deserialization.success.ts +0 -16
  69. package/src/behavior-actions/behavior.action.insert-blocks.ts +0 -140
  70. package/src/behavior-actions/behavior.action.list-item.ts +0 -100
  71. package/src/behavior-actions/behavior.action.select.next-block.ts +0 -44
  72. package/src/behavior-actions/behavior.action.select.previous-block.ts +0 -48
  73. package/src/behavior-actions/behavior.action.serialization.failure.ts +0 -9
  74. package/src/behavior-actions/behavior.action.serialization.success.ts +0 -17
  75. package/src/behavior-actions/behavior.action.style.ts +0 -108
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/editor",
3
- "version": "1.43.1",
3
+ "version": "1.44.1",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -85,7 +85,7 @@
85
85
  "devDependencies": {
86
86
  "@portabletext/toolkit": "^2.0.17",
87
87
  "@sanity/diff-match-patch": "^3.2.0",
88
- "@sanity/pkg-utils": "^7.2.0",
88
+ "@sanity/pkg-utils": "^7.2.1",
89
89
  "@sanity/schema": "^3.81.0",
90
90
  "@sanity/types": "^3.81.0",
91
91
  "@testing-library/jest-dom": "^6.6.3",
@@ -4,12 +4,8 @@ import type {EditorContext} from '../editor/editor-snapshot'
4
4
  import {
5
5
  addAnnotationActionImplementation,
6
6
  removeAnnotationActionImplementation,
7
- toggleAnnotationActionImplementation,
8
7
  } from '../editor/plugins/createWithEditableAPI'
9
- import {
10
- removeDecoratorActionImplementation,
11
- toggleDecoratorActionImplementation,
12
- } from '../editor/plugins/createWithPortableTextMarkModel'
8
+ import {removeDecoratorActionImplementation} from '../editor/plugins/createWithPortableTextMarkModel'
13
9
  import {
14
10
  historyRedoActionImplementation,
15
11
  historyUndoActionImplementation,
@@ -19,18 +15,14 @@ import type {PickFromUnion} from '../type-utils'
19
15
  import {blockSetBehaviorActionImplementation} from './behavior.action.block.set'
20
16
  import {blockUnsetBehaviorActionImplementation} from './behavior.action.block.unset'
21
17
  import {blurActionImplementation} from './behavior.action.blur'
22
- import {dataTransferSetActionImplementation} from './behavior.action.data-transfer-set'
23
18
  import {decoratorAddActionImplementation} from './behavior.action.decorator.add'
24
19
  import {deleteActionImplementation} from './behavior.action.delete'
25
20
  import {deleteBackwardActionImplementation} from './behavior.action.delete.backward'
26
21
  import {deleteBlockActionImplementation} from './behavior.action.delete.block'
27
22
  import {deleteForwardActionImplementation} from './behavior.action.delete.forward'
28
23
  import {deleteTextActionImplementation} from './behavior.action.delete.text'
29
- import {deserializationFailureActionImplementation} from './behavior.action.deserialization.failure'
30
- import {deserializationSuccessActionImplementation} from './behavior.action.deserialization.success'
31
24
  import {effectActionImplementation} from './behavior.action.effect'
32
25
  import {focusActionImplementation} from './behavior.action.focus'
33
- import {insertBlocksActionImplementation} from './behavior.action.insert-blocks'
34
26
  import {
35
27
  insertBreakActionImplementation,
36
28
  insertSoftBreakActionImplementation,
@@ -39,25 +31,11 @@ import {insertInlineObjectActionImplementation} from './behavior.action.insert-i
39
31
  import {insertSpanActionImplementation} from './behavior.action.insert-span'
40
32
  import {insertBlockActionImplementation} from './behavior.action.insert.block'
41
33
  import {insertTextActionImplementation} from './behavior.action.insert.text'
42
- import {
43
- addListItemActionImplementation,
44
- removeListItemActionImplementation,
45
- toggleListItemActionImplementation,
46
- } from './behavior.action.list-item'
47
34
  import {moveBlockActionImplementation} from './behavior.action.move.block'
48
35
  import {moveBlockDownActionImplementation} from './behavior.action.move.block-down'
49
36
  import {moveBlockUpActionImplementation} from './behavior.action.move.block-up'
50
37
  import {noopActionImplementation} from './behavior.action.noop'
51
38
  import {selectActionImplementation} from './behavior.action.select'
52
- import {selectNextBlockActionImplementation} from './behavior.action.select.next-block'
53
- import {selectPreviousBlockActionImplementation} from './behavior.action.select.previous-block'
54
- import {serializationFailureActionImplementation} from './behavior.action.serialization.failure'
55
- import {serializationSuccessActionImplementation} from './behavior.action.serialization.success'
56
- import {
57
- addStyleActionImplementation,
58
- removeStyleActionImplementation,
59
- toggleStyleActionImplementation,
60
- } from './behavior.action.style'
61
39
 
62
40
  const debug = debugWithName('behaviors:action')
63
41
 
@@ -84,47 +62,31 @@ type BehaviorActionImplementations = {
84
62
  const behaviorActionImplementations: BehaviorActionImplementations = {
85
63
  'annotation.add': addAnnotationActionImplementation,
86
64
  'annotation.remove': removeAnnotationActionImplementation,
87
- 'annotation.toggle': toggleAnnotationActionImplementation,
88
65
  'block.set': blockSetBehaviorActionImplementation,
89
66
  'block.unset': blockUnsetBehaviorActionImplementation,
90
67
  'blur': blurActionImplementation,
91
- 'data transfer.set': dataTransferSetActionImplementation,
92
68
  'decorator.add': decoratorAddActionImplementation,
93
69
  'decorator.remove': removeDecoratorActionImplementation,
94
- 'decorator.toggle': toggleDecoratorActionImplementation,
95
70
  'focus': focusActionImplementation,
96
71
  'delete': deleteActionImplementation,
97
72
  'delete.backward': deleteBackwardActionImplementation,
98
73
  'delete.forward': deleteForwardActionImplementation,
99
74
  'delete.block': deleteBlockActionImplementation,
100
75
  'delete.text': deleteTextActionImplementation,
101
- 'deserialization.failure': deserializationFailureActionImplementation,
102
- 'deserialization.success': deserializationSuccessActionImplementation,
103
76
  'history.redo': historyRedoActionImplementation,
104
77
  'history.undo': historyUndoActionImplementation,
105
78
  'insert.block': insertBlockActionImplementation,
106
- 'insert.blocks': insertBlocksActionImplementation,
107
79
  'insert.break': insertBreakActionImplementation,
108
80
  'insert.inline object': insertInlineObjectActionImplementation,
109
81
  'insert.soft break': insertSoftBreakActionImplementation,
110
82
  'insert.span': insertSpanActionImplementation,
111
83
  'insert.text': insertTextActionImplementation,
112
84
  'effect': effectActionImplementation,
113
- 'list item.add': addListItemActionImplementation,
114
- 'list item.remove': removeListItemActionImplementation,
115
- 'list item.toggle': toggleListItemActionImplementation,
116
85
  'move.block': moveBlockActionImplementation,
117
86
  'move.block down': moveBlockDownActionImplementation,
118
87
  'move.block up': moveBlockUpActionImplementation,
119
88
  'noop': noopActionImplementation,
120
89
  'select': selectActionImplementation,
121
- 'select.previous block': selectPreviousBlockActionImplementation,
122
- 'select.next block': selectNextBlockActionImplementation,
123
- 'serialization.failure': serializationFailureActionImplementation,
124
- 'serialization.success': serializationSuccessActionImplementation,
125
- 'style.toggle': toggleStyleActionImplementation,
126
- 'style.add': addStyleActionImplementation,
127
- 'style.remove': removeStyleActionImplementation,
128
90
  }
129
91
 
130
92
  export function performAction({
@@ -151,13 +113,6 @@ export function performAction({
151
113
  })
152
114
  break
153
115
  }
154
- case 'annotation.toggle': {
155
- behaviorActionImplementations['annotation.toggle']({
156
- context,
157
- action,
158
- })
159
- break
160
- }
161
116
  case 'block.set': {
162
117
  behaviorActionImplementations['block.set']({
163
118
  context,
@@ -179,13 +134,6 @@ export function performAction({
179
134
  })
180
135
  break
181
136
  }
182
- case 'data transfer.set': {
183
- behaviorActionImplementations['data transfer.set']({
184
- context,
185
- action,
186
- })
187
- break
188
- }
189
137
  case 'decorator.add': {
190
138
  behaviorActionImplementations['decorator.add']({
191
139
  context,
@@ -200,13 +148,6 @@ export function performAction({
200
148
  })
201
149
  break
202
150
  }
203
- case 'decorator.toggle': {
204
- behaviorActionImplementations['decorator.toggle']({
205
- context,
206
- action,
207
- })
208
- break
209
- }
210
151
  case 'delete': {
211
152
  behaviorActionImplementations.delete({
212
153
  context,
@@ -242,20 +183,6 @@ export function performAction({
242
183
  })
243
184
  break
244
185
  }
245
- case 'deserialization.failure': {
246
- behaviorActionImplementations['deserialization.failure']({
247
- context,
248
- action,
249
- })
250
- break
251
- }
252
- case 'deserialization.success': {
253
- behaviorActionImplementations['deserialization.success']({
254
- context,
255
- action,
256
- })
257
- break
258
- }
259
186
  case 'effect': {
260
187
  behaviorActionImplementations.effect({
261
188
  context,
@@ -291,13 +218,6 @@ export function performAction({
291
218
  })
292
219
  break
293
220
  }
294
- case 'insert.blocks': {
295
- behaviorActionImplementations['insert.blocks']({
296
- context,
297
- action,
298
- })
299
- break
300
- }
301
221
  case 'insert.inline object': {
302
222
  behaviorActionImplementations['insert.inline object']({
303
223
  context,
@@ -333,27 +253,6 @@ export function performAction({
333
253
  })
334
254
  break
335
255
  }
336
- case 'list item.add': {
337
- behaviorActionImplementations['list item.add']({
338
- context,
339
- action,
340
- })
341
- break
342
- }
343
- case 'list item.remove': {
344
- behaviorActionImplementations['list item.remove']({
345
- context,
346
- action,
347
- })
348
- break
349
- }
350
- case 'list item.toggle': {
351
- behaviorActionImplementations['list item.toggle']({
352
- context,
353
- action,
354
- })
355
- break
356
- }
357
256
  case 'move.block': {
358
257
  behaviorActionImplementations['move.block']({
359
258
  context,
@@ -382,57 +281,8 @@ export function performAction({
382
281
  })
383
282
  break
384
283
  }
385
- case 'select': {
386
- behaviorActionImplementations.select({
387
- context,
388
- action,
389
- })
390
- break
391
- }
392
- case 'select.previous block': {
393
- behaviorActionImplementations['select.previous block']({
394
- context,
395
- action,
396
- })
397
- break
398
- }
399
- case 'select.next block': {
400
- behaviorActionImplementations['select.next block']({
401
- context,
402
- action,
403
- })
404
- break
405
- }
406
- case 'serialization.failure': {
407
- behaviorActionImplementations['serialization.failure']({
408
- context,
409
- action,
410
- })
411
- break
412
- }
413
- case 'serialization.success': {
414
- behaviorActionImplementations['serialization.success']({
415
- context,
416
- action,
417
- })
418
- break
419
- }
420
- case 'style.add': {
421
- behaviorActionImplementations['style.add']({
422
- context,
423
- action,
424
- })
425
- break
426
- }
427
- case 'style.remove': {
428
- behaviorActionImplementations['style.remove']({
429
- context,
430
- action,
431
- })
432
- break
433
- }
434
284
  default: {
435
- behaviorActionImplementations['style.toggle']({
285
+ behaviorActionImplementations.select({
436
286
  context,
437
287
  action,
438
288
  })
@@ -1,115 +1,16 @@
1
1
  import {isTextBlock} from '../internal-utils/parse-blocks'
2
2
  import * as selectors from '../selectors'
3
- import {blockOffsetsToSelection, getTextBlockText} from '../utils'
3
+ import {getTextBlockText} from '../utils'
4
4
  import {raiseInsertSoftBreak} from './behavior.default.raise-soft-break'
5
+ import {internalAnnotationBehaviors} from './behavior.internal.annotation'
6
+ import {internalDecoratorBehaviors} from './behavior.internal.decorator'
7
+ import {internalInsertBehaviors} from './behavior.internal.insert'
8
+ import {internalListItemBehaviors} from './behavior.internal.list-item'
9
+ import {internalSelectBehaviors} from './behavior.internal.select'
10
+ import {internalStyleBehaviors} from './behavior.internal.style'
5
11
  import {raise} from './behavior.types.action'
6
12
  import {defineBehavior} from './behavior.types.behavior'
7
13
 
8
- const toggleAnnotationOff = defineBehavior({
9
- on: 'annotation.toggle',
10
- guard: ({snapshot, event}) =>
11
- selectors.isActiveAnnotation(event.annotation.name)(snapshot),
12
- actions: [
13
- ({event}) => [
14
- raise({type: 'annotation.remove', annotation: event.annotation}),
15
- ],
16
- ],
17
- })
18
-
19
- const toggleAnnotationOn = defineBehavior({
20
- on: 'annotation.toggle',
21
- guard: ({snapshot, event}) =>
22
- !selectors.isActiveAnnotation(event.annotation.name)(snapshot),
23
- actions: [
24
- ({event}) => [
25
- raise({type: 'annotation.add', annotation: event.annotation}),
26
- ],
27
- ],
28
- })
29
-
30
- const toggleDecoratorOff = defineBehavior({
31
- on: 'decorator.toggle',
32
- guard: ({snapshot, event}) =>
33
- selectors.isActiveDecorator(event.decorator)(snapshot),
34
- actions: [
35
- ({event}) => [
36
- raise({type: 'decorator.remove', decorator: event.decorator}),
37
- ],
38
- ],
39
- })
40
-
41
- const toggleDecoratorOn = defineBehavior({
42
- on: 'decorator.toggle',
43
- guard: ({snapshot, event}) => {
44
- const manualSelection = event.offsets
45
- ? blockOffsetsToSelection({
46
- value: snapshot.context.value,
47
- offsets: event.offsets,
48
- })
49
- : null
50
-
51
- if (manualSelection) {
52
- return !selectors.isActiveDecorator(event.decorator)({
53
- ...snapshot,
54
- context: {
55
- ...snapshot.context,
56
- selection: manualSelection,
57
- },
58
- })
59
- }
60
-
61
- return !selectors.isActiveDecorator(event.decorator)(snapshot)
62
- },
63
- actions: [
64
- ({event}) => [
65
- raise({
66
- ...event,
67
- type: 'decorator.add',
68
- }),
69
- ],
70
- ],
71
- })
72
-
73
- const toggleListItemOff = defineBehavior({
74
- on: 'list item.toggle',
75
- guard: ({snapshot, event}) =>
76
- selectors.isActiveListItem(event.listItem)(snapshot),
77
- actions: [
78
- ({event}) => [
79
- raise({
80
- type: 'list item.remove',
81
- listItem: event.listItem,
82
- }),
83
- ],
84
- ],
85
- })
86
-
87
- const toggleListItemOn = defineBehavior({
88
- on: 'list item.toggle',
89
- guard: ({snapshot, event}) =>
90
- !selectors.isActiveListItem(event.listItem)(snapshot),
91
- actions: [
92
- ({event}) => [
93
- raise({
94
- type: 'list item.add',
95
- listItem: event.listItem,
96
- }),
97
- ],
98
- ],
99
- })
100
-
101
- const toggleStyleOff = defineBehavior({
102
- on: 'style.toggle',
103
- guard: ({snapshot, event}) => selectors.isActiveStyle(event.style)(snapshot),
104
- actions: [({event}) => [raise({type: 'style.remove', style: event.style})]],
105
- })
106
-
107
- const toggleStyleOn = defineBehavior({
108
- on: 'style.toggle',
109
- guard: ({snapshot, event}) => !selectors.isActiveStyle(event.style)(snapshot),
110
- actions: [({event}) => [raise({type: 'style.add', style: event.style})]],
111
- })
112
-
113
14
  const raiseDeserializationSuccessOrFailure = defineBehavior({
114
15
  on: 'deserialize',
115
16
  guard: ({snapshot, event}) => {
@@ -265,12 +166,30 @@ export const defaultBehaviors = [
265
166
  on: 'serialization.success',
266
167
  actions: [
267
168
  ({event}) => [
268
- raise({
269
- type: 'data transfer.set',
270
- data: event.data,
271
- dataTransfer: event.originEvent.originEvent.dataTransfer,
272
- mimeType: event.mimeType,
273
- }),
169
+ {
170
+ type: 'effect',
171
+ effect: () => {
172
+ event.originEvent.originEvent.dataTransfer.setData(
173
+ event.mimeType,
174
+ event.data,
175
+ )
176
+ },
177
+ },
178
+ ],
179
+ ],
180
+ }),
181
+ defineBehavior({
182
+ on: 'serialization.failure',
183
+ actions: [
184
+ ({event}) => [
185
+ {
186
+ type: 'effect',
187
+ effect: () => {
188
+ console.warn(
189
+ `Serialization of ${event.mimeType} failed with reason "${event.reason}"`,
190
+ )
191
+ },
192
+ },
274
193
  ],
275
194
  ],
276
195
  }),
@@ -465,6 +384,21 @@ export const defaultBehaviors = [
465
384
  ],
466
385
  ],
467
386
  }),
387
+ defineBehavior({
388
+ on: 'deserialization.failure',
389
+ actions: [
390
+ ({event}) => [
391
+ {
392
+ type: 'effect',
393
+ effect: () => {
394
+ console.warn(
395
+ `Deserialization of ${event.mimeType} failed with reason "${event.reason}"`,
396
+ )
397
+ },
398
+ },
399
+ ],
400
+ ],
401
+ }),
468
402
  defineBehavior({
469
403
  on: 'clipboard.paste',
470
404
  guard: ({snapshot}) => {
@@ -508,14 +442,12 @@ export const defaultBehaviors = [
508
442
  ],
509
443
  ],
510
444
  }),
511
- toggleAnnotationOff,
512
- toggleAnnotationOn,
513
- toggleDecoratorOff,
514
- toggleDecoratorOn,
515
- toggleListItemOff,
516
- toggleListItemOn,
517
- toggleStyleOff,
518
- toggleStyleOn,
445
+ ...internalAnnotationBehaviors,
446
+ ...internalDecoratorBehaviors,
447
+ ...internalInsertBehaviors,
448
+ ...internalListItemBehaviors,
449
+ ...internalStyleBehaviors,
450
+ ...internalSelectBehaviors,
519
451
  raiseDeserializationSuccessOrFailure,
520
452
  raiseSerializationSuccessOrFailure,
521
453
  raiseInsertSoftBreak,
@@ -0,0 +1,26 @@
1
+ import {isActiveAnnotation} from '../selectors'
2
+ import {raise} from './behavior.types.action'
3
+ import {defineBehavior} from './behavior.types.behavior'
4
+
5
+ export const internalAnnotationBehaviors = [
6
+ defineBehavior({
7
+ on: 'annotation.toggle',
8
+ guard: ({snapshot, event}) =>
9
+ isActiveAnnotation(event.annotation.name)(snapshot),
10
+ actions: [
11
+ ({event}) => [
12
+ raise({type: 'annotation.remove', annotation: event.annotation}),
13
+ ],
14
+ ],
15
+ }),
16
+ defineBehavior({
17
+ on: 'annotation.toggle',
18
+ guard: ({snapshot, event}) =>
19
+ !isActiveAnnotation(event.annotation.name)(snapshot),
20
+ actions: [
21
+ ({event}) => [
22
+ raise({type: 'annotation.add', annotation: event.annotation}),
23
+ ],
24
+ ],
25
+ }),
26
+ ]
@@ -0,0 +1,47 @@
1
+ import {isActiveDecorator} from '../selectors'
2
+ import {blockOffsetsToSelection} from '../utils'
3
+ import {raise} from './behavior.types.action'
4
+ import {defineBehavior} from './behavior.types.behavior'
5
+
6
+ export const internalDecoratorBehaviors = [
7
+ defineBehavior({
8
+ on: 'decorator.toggle',
9
+ guard: ({snapshot, event}) => isActiveDecorator(event.decorator)(snapshot),
10
+ actions: [
11
+ ({event}) => [
12
+ raise({type: 'decorator.remove', decorator: event.decorator}),
13
+ ],
14
+ ],
15
+ }),
16
+ defineBehavior({
17
+ on: 'decorator.toggle',
18
+ guard: ({snapshot, event}) => {
19
+ const manualSelection = event.offsets
20
+ ? blockOffsetsToSelection({
21
+ value: snapshot.context.value,
22
+ offsets: event.offsets,
23
+ })
24
+ : null
25
+
26
+ if (manualSelection) {
27
+ return !isActiveDecorator(event.decorator)({
28
+ ...snapshot,
29
+ context: {
30
+ ...snapshot.context,
31
+ selection: manualSelection,
32
+ },
33
+ })
34
+ }
35
+
36
+ return !isActiveDecorator(event.decorator)(snapshot)
37
+ },
38
+ actions: [
39
+ ({event}) => [
40
+ raise({
41
+ ...event,
42
+ type: 'decorator.add',
43
+ }),
44
+ ],
45
+ ],
46
+ }),
47
+ ]
@@ -0,0 +1,118 @@
1
+ import {getFocusTextBlock} from '../selectors'
2
+ import {raise} from './behavior.types.action'
3
+ import {defineBehavior} from './behavior.types.behavior'
4
+
5
+ export const internalInsertBehaviors = [
6
+ defineBehavior({
7
+ on: 'insert.blocks',
8
+ guard: ({event}) => event.placement === 'before',
9
+ actions: [
10
+ ({event}) =>
11
+ event.blocks.map((block, index) =>
12
+ raise({
13
+ type: 'insert.block',
14
+ block,
15
+ placement: index === 0 ? 'before' : 'after',
16
+ select: 'end',
17
+ }),
18
+ ),
19
+ ],
20
+ }),
21
+ defineBehavior({
22
+ on: 'insert.blocks',
23
+ guard: ({event}) => event.placement === 'after',
24
+ actions: [
25
+ ({event}) =>
26
+ event.blocks.map((block) =>
27
+ raise({
28
+ type: 'insert.block',
29
+ block,
30
+ placement: 'after',
31
+ select: 'end',
32
+ }),
33
+ ),
34
+ ],
35
+ }),
36
+ defineBehavior({
37
+ on: 'insert.blocks',
38
+ guard: ({snapshot, event}) => {
39
+ if (event.placement !== 'auto') {
40
+ return false
41
+ }
42
+
43
+ const focusTextBlock = getFocusTextBlock(snapshot)
44
+
45
+ if (!focusTextBlock) {
46
+ return false
47
+ }
48
+
49
+ return true
50
+ },
51
+ actions: [
52
+ ({event}) =>
53
+ event.blocks.length === 1
54
+ ? [
55
+ raise({
56
+ type: 'insert.block',
57
+ block: event.blocks[0],
58
+ placement: 'auto',
59
+ select: 'end',
60
+ }),
61
+ ]
62
+ : event.blocks.flatMap((block, index) =>
63
+ index === 0
64
+ ? [
65
+ raise({
66
+ type: 'insert.break',
67
+ }),
68
+ raise({
69
+ type: 'select.previous block',
70
+ select: 'end',
71
+ }),
72
+ raise({
73
+ type: 'insert.block',
74
+ block,
75
+ placement: 'auto',
76
+ select: 'end',
77
+ }),
78
+ ]
79
+ : index === event.blocks.length - 1
80
+ ? [
81
+ raise({
82
+ type: 'select.next block',
83
+ select: 'start',
84
+ }),
85
+ raise({
86
+ type: 'insert.block',
87
+ block,
88
+ placement: 'auto',
89
+ select: 'end',
90
+ }),
91
+ ]
92
+ : [
93
+ raise({
94
+ type: 'insert.block',
95
+ block,
96
+ placement: 'after',
97
+ select: 'end',
98
+ }),
99
+ ],
100
+ ),
101
+ ],
102
+ }),
103
+ defineBehavior({
104
+ on: 'insert.blocks',
105
+ guard: ({event}) => event.placement === 'auto',
106
+ actions: [
107
+ ({event}) =>
108
+ event.blocks.map((block, index) =>
109
+ raise({
110
+ type: 'insert.block',
111
+ block,
112
+ placement: index === 0 ? 'auto' : 'after',
113
+ select: 'end',
114
+ }),
115
+ ),
116
+ ],
117
+ }),
118
+ ]