@portabletext/editor 1.44.1 → 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 (53) 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 +166 -272
  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 +167 -273
  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 +2 -2
  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 +2 -2
  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 +1 -1
  28. package/src/behavior-actions/behavior.actions.ts +0 -18
  29. package/src/behaviors/{behavior.internal.annotation.ts → behavior.abstract.annotation.ts} +1 -1
  30. package/src/behaviors/{behavior.internal.decorator.ts → behavior.abstract.decorator.ts} +1 -1
  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 +14 -12
  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 +153 -153
  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/with-plugins.ts +1 -4
  49. package/src/internal-utils/slate-children-to-blocks.ts +49 -0
  50. package/src/internal-utils/slate-utils.ts +6 -8
  51. package/src/type-utils.ts +2 -0
  52. package/src/behavior-actions/behavior.action.move.block-down.ts +0 -48
  53. 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.1",
3
+ "version": "1.44.2",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -32,8 +32,6 @@ import {insertSpanActionImplementation} from './behavior.action.insert-span'
32
32
  import {insertBlockActionImplementation} from './behavior.action.insert.block'
33
33
  import {insertTextActionImplementation} from './behavior.action.insert.text'
34
34
  import {moveBlockActionImplementation} from './behavior.action.move.block'
35
- import {moveBlockDownActionImplementation} from './behavior.action.move.block-down'
36
- import {moveBlockUpActionImplementation} from './behavior.action.move.block-up'
37
35
  import {noopActionImplementation} from './behavior.action.noop'
38
36
  import {selectActionImplementation} from './behavior.action.select'
39
37
 
@@ -83,8 +81,6 @@ const behaviorActionImplementations: BehaviorActionImplementations = {
83
81
  'insert.text': insertTextActionImplementation,
84
82
  'effect': effectActionImplementation,
85
83
  'move.block': moveBlockActionImplementation,
86
- 'move.block down': moveBlockDownActionImplementation,
87
- 'move.block up': moveBlockUpActionImplementation,
88
84
  'noop': noopActionImplementation,
89
85
  'select': selectActionImplementation,
90
86
  }
@@ -260,20 +256,6 @@ export function performAction({
260
256
  })
261
257
  break
262
258
  }
263
- case 'move.block down': {
264
- behaviorActionImplementations['move.block down']({
265
- context,
266
- action,
267
- })
268
- break
269
- }
270
- case 'move.block up': {
271
- behaviorActionImplementations['move.block up']({
272
- context,
273
- action,
274
- })
275
- break
276
- }
277
259
  case 'noop': {
278
260
  behaviorActionImplementations.noop({
279
261
  context,
@@ -2,7 +2,7 @@ import {isActiveAnnotation} from '../selectors'
2
2
  import {raise} from './behavior.types.action'
3
3
  import {defineBehavior} from './behavior.types.behavior'
4
4
 
5
- export const internalAnnotationBehaviors = [
5
+ export const abstractAnnotationBehaviors = [
6
6
  defineBehavior({
7
7
  on: 'annotation.toggle',
8
8
  guard: ({snapshot, event}) =>
@@ -3,7 +3,7 @@ import {blockOffsetsToSelection} from '../utils'
3
3
  import {raise} from './behavior.types.action'
4
4
  import {defineBehavior} from './behavior.types.behavior'
5
5
 
6
- export const internalDecoratorBehaviors = [
6
+ export const abstractDecoratorBehaviors = [
7
7
  defineBehavior({
8
8
  on: 'decorator.toggle',
9
9
  guard: ({snapshot, event}) => isActiveDecorator(event.decorator)(snapshot),
@@ -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,13 +1,14 @@
1
1
  import {isTextBlock} from '../internal-utils/parse-blocks'
2
2
  import * as selectors from '../selectors'
3
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 {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'
11
12
  import {raise} from './behavior.types.action'
12
13
  import {defineBehavior} from './behavior.types.behavior'
13
14
 
@@ -442,12 +443,13 @@ export const defaultBehaviors = [
442
443
  ],
443
444
  ],
444
445
  }),
445
- ...internalAnnotationBehaviors,
446
- ...internalDecoratorBehaviors,
447
- ...internalInsertBehaviors,
448
- ...internalListItemBehaviors,
449
- ...internalStyleBehaviors,
450
- ...internalSelectBehaviors,
446
+ ...abstractAnnotationBehaviors,
447
+ ...abstractDecoratorBehaviors,
448
+ ...abstractInsertBehaviors,
449
+ ...abstractListItemBehaviors,
450
+ ...abstractMoveBehaviors,
451
+ ...abstractStyleBehaviors,
452
+ ...abstractSelectBehaviors,
451
453
  raiseDeserializationSuccessOrFailure,
452
454
  raiseSerializationSuccessOrFailure,
453
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
  }