@portabletext/editor 1.44.0 → 1.44.2

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 (55) 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 +323 -490
  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 +325 -492
  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 +8390 -16283
  12. package/lib/behaviors/index.d.ts +8390 -16283
  13. package/lib/behaviors/index.js +4 -4
  14. package/lib/behaviors/index.js.map +1 -1
  15. package/lib/index.cjs +2 -2
  16. package/lib/index.cjs.map +1 -1
  17. package/lib/index.d.cts +2237 -15969
  18. package/lib/index.d.ts +2237 -15969
  19. package/lib/index.js +2 -2
  20. package/lib/index.js.map +1 -1
  21. package/lib/plugins/index.d.cts +2088 -15821
  22. package/lib/plugins/index.d.ts +2088 -15821
  23. package/lib/selectors/index.d.cts +1738 -15472
  24. package/lib/selectors/index.d.ts +1738 -15472
  25. package/lib/utils/index.d.cts +1738 -15472
  26. package/lib/utils/index.d.ts +1738 -15472
  27. package/package.json +2 -2
  28. package/src/behavior-actions/behavior.actions.ts +1 -39
  29. package/src/behaviors/behavior.abstract.annotation.ts +26 -0
  30. package/src/behaviors/behavior.abstract.decorator.ts +47 -0
  31. package/src/behaviors/{behavior.internal.insert.ts → behavior.abstract.insert.ts} +1 -1
  32. package/src/behaviors/{behavior.internal.list-item.ts → behavior.abstract.list-item.ts} +1 -1
  33. package/src/behaviors/behavior.abstract.move.ts +78 -0
  34. package/src/behaviors/{behavior.internal.select.ts → behavior.abstract.select.ts} +1 -1
  35. package/src/behaviors/{behavior.internal.style.ts → behavior.abstract.style.ts} +1 -1
  36. package/src/behaviors/behavior.code-editor.ts +13 -8
  37. package/src/behaviors/behavior.default.ts +15 -78
  38. package/src/behaviors/behavior.perform-event.ts +4 -4
  39. package/src/behaviors/behavior.types.action.ts +3 -3
  40. package/src/behaviors/behavior.types.behavior.ts +4 -3
  41. package/src/behaviors/behavior.types.event.ts +164 -160
  42. package/src/editor/components/Leaf.tsx +2 -2
  43. package/src/editor/create-editor.ts +37 -11
  44. package/src/editor/editor-machine.ts +4 -76
  45. package/src/editor/editor-selector.ts +5 -2
  46. package/src/editor/editor-snapshot.ts +2 -7
  47. package/src/editor/plugins/create-with-event-listeners.ts +1 -64
  48. package/src/editor/plugins/createWithEditableAPI.ts +0 -30
  49. package/src/editor/plugins/createWithPortableTextMarkModel.ts +0 -29
  50. package/src/editor/plugins/with-plugins.ts +1 -4
  51. package/src/internal-utils/slate-children-to-blocks.ts +49 -0
  52. package/src/internal-utils/slate-utils.ts +6 -8
  53. package/src/type-utils.ts +2 -0
  54. package/src/behavior-actions/behavior.action.move.block-down.ts +0 -48
  55. package/src/behavior-actions/behavior.action.move.block-up.ts +0 -53
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/editor",
3
- "version": "1.44.0",
3
+ "version": "1.44.2",
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,
@@ -36,8 +32,6 @@ import {insertSpanActionImplementation} from './behavior.action.insert-span'
36
32
  import {insertBlockActionImplementation} from './behavior.action.insert.block'
37
33
  import {insertTextActionImplementation} from './behavior.action.insert.text'
38
34
  import {moveBlockActionImplementation} from './behavior.action.move.block'
39
- import {moveBlockDownActionImplementation} from './behavior.action.move.block-down'
40
- import {moveBlockUpActionImplementation} from './behavior.action.move.block-up'
41
35
  import {noopActionImplementation} from './behavior.action.noop'
42
36
  import {selectActionImplementation} from './behavior.action.select'
43
37
 
@@ -66,13 +60,11 @@ type BehaviorActionImplementations = {
66
60
  const behaviorActionImplementations: BehaviorActionImplementations = {
67
61
  'annotation.add': addAnnotationActionImplementation,
68
62
  'annotation.remove': removeAnnotationActionImplementation,
69
- 'annotation.toggle': toggleAnnotationActionImplementation,
70
63
  'block.set': blockSetBehaviorActionImplementation,
71
64
  'block.unset': blockUnsetBehaviorActionImplementation,
72
65
  'blur': blurActionImplementation,
73
66
  'decorator.add': decoratorAddActionImplementation,
74
67
  'decorator.remove': removeDecoratorActionImplementation,
75
- 'decorator.toggle': toggleDecoratorActionImplementation,
76
68
  'focus': focusActionImplementation,
77
69
  'delete': deleteActionImplementation,
78
70
  'delete.backward': deleteBackwardActionImplementation,
@@ -89,8 +81,6 @@ const behaviorActionImplementations: BehaviorActionImplementations = {
89
81
  'insert.text': insertTextActionImplementation,
90
82
  'effect': effectActionImplementation,
91
83
  'move.block': moveBlockActionImplementation,
92
- 'move.block down': moveBlockDownActionImplementation,
93
- 'move.block up': moveBlockUpActionImplementation,
94
84
  'noop': noopActionImplementation,
95
85
  'select': selectActionImplementation,
96
86
  }
@@ -119,13 +109,6 @@ export function performAction({
119
109
  })
120
110
  break
121
111
  }
122
- case 'annotation.toggle': {
123
- behaviorActionImplementations['annotation.toggle']({
124
- context,
125
- action,
126
- })
127
- break
128
- }
129
112
  case 'block.set': {
130
113
  behaviorActionImplementations['block.set']({
131
114
  context,
@@ -161,13 +144,6 @@ export function performAction({
161
144
  })
162
145
  break
163
146
  }
164
- case 'decorator.toggle': {
165
- behaviorActionImplementations['decorator.toggle']({
166
- context,
167
- action,
168
- })
169
- break
170
- }
171
147
  case 'delete': {
172
148
  behaviorActionImplementations.delete({
173
149
  context,
@@ -280,20 +256,6 @@ export function performAction({
280
256
  })
281
257
  break
282
258
  }
283
- case 'move.block down': {
284
- behaviorActionImplementations['move.block down']({
285
- context,
286
- action,
287
- })
288
- break
289
- }
290
- case 'move.block up': {
291
- behaviorActionImplementations['move.block up']({
292
- context,
293
- action,
294
- })
295
- break
296
- }
297
259
  case 'noop': {
298
260
  behaviorActionImplementations.noop({
299
261
  context,
@@ -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 abstractAnnotationBehaviors = [
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 abstractDecoratorBehaviors = [
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
+ ]
@@ -2,7 +2,7 @@ import {getFocusTextBlock} from '../selectors'
2
2
  import {raise} from './behavior.types.action'
3
3
  import {defineBehavior} from './behavior.types.behavior'
4
4
 
5
- export const internalInsertBehaviors = [
5
+ export const abstractInsertBehaviors = [
6
6
  defineBehavior({
7
7
  on: 'insert.blocks',
8
8
  guard: ({event}) => event.placement === 'before',
@@ -2,7 +2,7 @@ import {getSelectedTextBlocks, isActiveListItem} from '../selectors'
2
2
  import {raise} from './behavior.types.action'
3
3
  import {defineBehavior} from './behavior.types.behavior'
4
4
 
5
- export const internalListItemBehaviors = [
5
+ export const abstractListItemBehaviors = [
6
6
  defineBehavior({
7
7
  on: 'list item.add',
8
8
  guard: ({snapshot}) => {
@@ -0,0 +1,78 @@
1
+ import {getNextBlock, getPreviousBlock} from '../selectors'
2
+ import {raise} from './behavior.types.action'
3
+ import {defineBehavior} from './behavior.types.behavior'
4
+
5
+ export const abstractMoveBehaviors = [
6
+ defineBehavior({
7
+ on: 'move.block up',
8
+ guard: ({snapshot, event}) => {
9
+ const previousBlock = getPreviousBlock({
10
+ ...snapshot,
11
+ context: {
12
+ ...snapshot.context,
13
+ selection: {
14
+ anchor: {
15
+ path: event.at,
16
+ offset: 0,
17
+ },
18
+ focus: {
19
+ path: event.at,
20
+ offset: 0,
21
+ },
22
+ },
23
+ },
24
+ })
25
+
26
+ if (previousBlock) {
27
+ return {previousBlock}
28
+ }
29
+
30
+ return false
31
+ },
32
+ actions: [
33
+ ({event}, {previousBlock}) => [
34
+ raise({
35
+ type: 'move.block',
36
+ at: event.at,
37
+ to: previousBlock.path,
38
+ }),
39
+ ],
40
+ ],
41
+ }),
42
+ defineBehavior({
43
+ on: 'move.block down',
44
+ guard: ({snapshot, event}) => {
45
+ const nextBlock = getNextBlock({
46
+ ...snapshot,
47
+ context: {
48
+ ...snapshot.context,
49
+ selection: {
50
+ anchor: {
51
+ path: event.at,
52
+ offset: 0,
53
+ },
54
+ focus: {
55
+ path: event.at,
56
+ offset: 0,
57
+ },
58
+ },
59
+ },
60
+ })
61
+
62
+ if (nextBlock) {
63
+ return {nextBlock}
64
+ }
65
+
66
+ return false
67
+ },
68
+ actions: [
69
+ ({event}, {nextBlock}) => [
70
+ raise({
71
+ type: 'move.block',
72
+ at: event.at,
73
+ to: nextBlock.path,
74
+ }),
75
+ ],
76
+ ],
77
+ }),
78
+ ]
@@ -3,7 +3,7 @@ import {getBlockEndPoint, getBlockStartPoint} from '../utils'
3
3
  import {raise} from './behavior.types.action'
4
4
  import {defineBehavior} from './behavior.types.behavior'
5
5
 
6
- export const internalSelectBehaviors = [
6
+ export const abstractSelectBehaviors = [
7
7
  defineBehavior({
8
8
  on: 'select.previous block',
9
9
  guard: ({snapshot, event}) => {
@@ -2,7 +2,7 @@ import {getSelectedTextBlocks, isActiveStyle} from '../selectors'
2
2
  import {raise} from './behavior.types.action'
3
3
  import {defineBehavior} from './behavior.types.behavior'
4
4
 
5
- export const internalStyleBehaviors = [
5
+ export const abstractStyleBehaviors = [
6
6
  defineBehavior({
7
7
  on: 'style.add',
8
8
  guard: ({snapshot}) => {
@@ -1,5 +1,6 @@
1
1
  import {isHotkey} from '../internal-utils/is-hotkey'
2
2
  import * as selectors from '../selectors'
3
+ import {raise} from './behavior.types.action'
3
4
  import {defineBehavior} from './behavior.types.behavior'
4
5
 
5
6
  /**
@@ -35,10 +36,12 @@ export function createCodeEditorBehaviors(config: CodeEditorBehaviorsConfig) {
35
36
  },
36
37
  actions: [
37
38
  (_, {paths}) =>
38
- paths.map((at) => ({
39
- type: 'move.block up',
40
- at,
41
- })),
39
+ paths.map((at) =>
40
+ raise({
41
+ type: 'move.block up',
42
+ at,
43
+ }),
44
+ ),
42
45
  ],
43
46
  }),
44
47
  defineBehavior({
@@ -62,10 +65,12 @@ export function createCodeEditorBehaviors(config: CodeEditorBehaviorsConfig) {
62
65
  },
63
66
  actions: [
64
67
  (_, {paths}) =>
65
- paths.map((at) => ({
66
- type: 'move.block down',
67
- at,
68
- })),
68
+ paths.map((at) =>
69
+ raise({
70
+ type: 'move.block down',
71
+ at,
72
+ }),
73
+ ),
69
74
  ],
70
75
  }),
71
76
  ]
@@ -1,79 +1,17 @@
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
+ import {abstractAnnotationBehaviors} from './behavior.abstract.annotation'
5
+ import {abstractDecoratorBehaviors} from './behavior.abstract.decorator'
6
+ import {abstractInsertBehaviors} from './behavior.abstract.insert'
7
+ import {abstractListItemBehaviors} from './behavior.abstract.list-item'
8
+ import {abstractMoveBehaviors} from './behavior.abstract.move'
9
+ import {abstractSelectBehaviors} from './behavior.abstract.select'
10
+ import {abstractStyleBehaviors} from './behavior.abstract.style'
4
11
  import {raiseInsertSoftBreak} from './behavior.default.raise-soft-break'
5
- import {internalInsertBehaviors} from './behavior.internal.insert'
6
- import {internalListItemBehaviors} from './behavior.internal.list-item'
7
- import {internalSelectBehaviors} from './behavior.internal.select'
8
- import {internalStyleBehaviors} from './behavior.internal.style'
9
12
  import {raise} from './behavior.types.action'
10
13
  import {defineBehavior} from './behavior.types.behavior'
11
14
 
12
- const toggleAnnotationOff = defineBehavior({
13
- on: 'annotation.toggle',
14
- guard: ({snapshot, event}) =>
15
- selectors.isActiveAnnotation(event.annotation.name)(snapshot),
16
- actions: [
17
- ({event}) => [
18
- raise({type: 'annotation.remove', annotation: event.annotation}),
19
- ],
20
- ],
21
- })
22
-
23
- const toggleAnnotationOn = defineBehavior({
24
- on: 'annotation.toggle',
25
- guard: ({snapshot, event}) =>
26
- !selectors.isActiveAnnotation(event.annotation.name)(snapshot),
27
- actions: [
28
- ({event}) => [
29
- raise({type: 'annotation.add', annotation: event.annotation}),
30
- ],
31
- ],
32
- })
33
-
34
- const toggleDecoratorOff = defineBehavior({
35
- on: 'decorator.toggle',
36
- guard: ({snapshot, event}) =>
37
- selectors.isActiveDecorator(event.decorator)(snapshot),
38
- actions: [
39
- ({event}) => [
40
- raise({type: 'decorator.remove', decorator: event.decorator}),
41
- ],
42
- ],
43
- })
44
-
45
- const toggleDecoratorOn = defineBehavior({
46
- on: 'decorator.toggle',
47
- guard: ({snapshot, event}) => {
48
- const manualSelection = event.offsets
49
- ? blockOffsetsToSelection({
50
- value: snapshot.context.value,
51
- offsets: event.offsets,
52
- })
53
- : null
54
-
55
- if (manualSelection) {
56
- return !selectors.isActiveDecorator(event.decorator)({
57
- ...snapshot,
58
- context: {
59
- ...snapshot.context,
60
- selection: manualSelection,
61
- },
62
- })
63
- }
64
-
65
- return !selectors.isActiveDecorator(event.decorator)(snapshot)
66
- },
67
- actions: [
68
- ({event}) => [
69
- raise({
70
- ...event,
71
- type: 'decorator.add',
72
- }),
73
- ],
74
- ],
75
- })
76
-
77
15
  const raiseDeserializationSuccessOrFailure = defineBehavior({
78
16
  on: 'deserialize',
79
17
  guard: ({snapshot, event}) => {
@@ -505,14 +443,13 @@ export const defaultBehaviors = [
505
443
  ],
506
444
  ],
507
445
  }),
508
- ...internalInsertBehaviors,
509
- ...internalListItemBehaviors,
510
- ...internalStyleBehaviors,
511
- ...internalSelectBehaviors,
512
- toggleAnnotationOff,
513
- toggleAnnotationOn,
514
- toggleDecoratorOff,
515
- toggleDecoratorOn,
446
+ ...abstractAnnotationBehaviors,
447
+ ...abstractDecoratorBehaviors,
448
+ ...abstractInsertBehaviors,
449
+ ...abstractListItemBehaviors,
450
+ ...abstractMoveBehaviors,
451
+ ...abstractStyleBehaviors,
452
+ ...abstractSelectBehaviors,
516
453
  raiseDeserializationSuccessOrFailure,
517
454
  raiseSerializationSuccessOrFailure,
518
455
  raiseInsertSoftBreak,
@@ -10,8 +10,8 @@ import {debugWithName} from '../internal-utils/debug'
10
10
  import type {PortableTextSlateEditor} from '../types/editor'
11
11
  import type {InternalBehaviorAction} from './behavior.types.action'
12
12
  import {
13
+ isAbstractBehaviorEvent,
13
14
  isCustomBehaviorEvent,
14
- isInternalBehaviorEvent,
15
15
  isNativeBehaviorEvent,
16
16
  } from './behavior.types.event'
17
17
 
@@ -20,8 +20,8 @@ const debug = debugWithName('behaviors:event')
20
20
  function eventCategory(event: BehaviorEvent) {
21
21
  return isNativeBehaviorEvent(event)
22
22
  ? 'native'
23
- : isInternalBehaviorEvent(event)
24
- ? 'internal'
23
+ : isAbstractBehaviorEvent(event)
24
+ ? 'abstract'
25
25
  : isCustomBehaviorEvent(event)
26
26
  ? 'custom'
27
27
  : 'synthetic'
@@ -55,7 +55,7 @@ export function performEvent({
55
55
  const defaultAction =
56
56
  isCustomBehaviorEvent(event) ||
57
57
  isNativeBehaviorEvent(event) ||
58
- isInternalBehaviorEvent(event)
58
+ isAbstractBehaviorEvent(event)
59
59
  ? undefined
60
60
  : ({
61
61
  ...event,
@@ -2,8 +2,8 @@ import type {EditorContext, EditorSnapshot} from '../editor/editor-snapshot'
2
2
  import type {OmitFromUnion, PickFromUnion} from '../type-utils'
3
3
  import type {PortableTextSlateEditor} from '../types/editor'
4
4
  import type {
5
+ AbstractBehaviorEvent,
5
6
  CustomBehaviorEvent,
6
- InternalBehaviorEvent,
7
7
  SyntheticBehaviorEvent,
8
8
  } from './behavior.types.event'
9
9
 
@@ -15,7 +15,7 @@ export type BehaviorAction =
15
15
  | {
16
16
  type: 'raise'
17
17
  event:
18
- | InternalBehaviorEvent
18
+ | AbstractBehaviorEvent
19
19
  | SyntheticBehaviorEvent
20
20
  | CustomBehaviorEvent
21
21
  }
@@ -31,7 +31,7 @@ export type BehaviorAction =
31
31
  * @beta
32
32
  */
33
33
  export function raise(
34
- event: InternalBehaviorEvent | SyntheticBehaviorEvent | CustomBehaviorEvent,
34
+ event: AbstractBehaviorEvent | SyntheticBehaviorEvent | CustomBehaviorEvent,
35
35
  ): PickFromUnion<BehaviorAction, 'type', 'raise'> {
36
36
  return {type: 'raise', event}
37
37
  }
@@ -23,17 +23,18 @@ export type Behavior<
23
23
  ResolveBehaviorEvent<TBehaviorEventType> = ResolveBehaviorEvent<TBehaviorEventType>,
24
24
  > = {
25
25
  /**
26
- * The internal editor event that triggers this behavior.
26
+ * Editor Event that triggers this Behavior.
27
27
  */
28
28
  on: TBehaviorEventType
29
29
  /**
30
- * Predicate function that determines if the behavior should be executed.
30
+ * Predicate function that determines if the Behavior should be executed.
31
31
  * Returning a non-nullable value from the guard will pass the value to the
32
32
  * actions and execute them.
33
33
  */
34
34
  guard?: BehaviorGuard<TBehaviorEvent, TGuardResponse>
35
35
  /**
36
- * Array of behavior action sets.
36
+ * Array of Behavior Action sets.
37
+ * Each set represents a step in the history stack.
37
38
  */
38
39
  actions: Array<BehaviorActionSet<TBehaviorEvent, TGuardResponse>>
39
40
  }