@portabletext/editor 1.44.0 → 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/editor",
3
- "version": "1.44.0",
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,
@@ -66,13 +62,11 @@ type BehaviorActionImplementations = {
66
62
  const behaviorActionImplementations: BehaviorActionImplementations = {
67
63
  'annotation.add': addAnnotationActionImplementation,
68
64
  'annotation.remove': removeAnnotationActionImplementation,
69
- 'annotation.toggle': toggleAnnotationActionImplementation,
70
65
  'block.set': blockSetBehaviorActionImplementation,
71
66
  'block.unset': blockUnsetBehaviorActionImplementation,
72
67
  'blur': blurActionImplementation,
73
68
  'decorator.add': decoratorAddActionImplementation,
74
69
  'decorator.remove': removeDecoratorActionImplementation,
75
- 'decorator.toggle': toggleDecoratorActionImplementation,
76
70
  'focus': focusActionImplementation,
77
71
  'delete': deleteActionImplementation,
78
72
  'delete.backward': deleteBackwardActionImplementation,
@@ -119,13 +113,6 @@ export function performAction({
119
113
  })
120
114
  break
121
115
  }
122
- case 'annotation.toggle': {
123
- behaviorActionImplementations['annotation.toggle']({
124
- context,
125
- action,
126
- })
127
- break
128
- }
129
116
  case 'block.set': {
130
117
  behaviorActionImplementations['block.set']({
131
118
  context,
@@ -161,13 +148,6 @@ export function performAction({
161
148
  })
162
149
  break
163
150
  }
164
- case 'decorator.toggle': {
165
- behaviorActionImplementations['decorator.toggle']({
166
- context,
167
- action,
168
- })
169
- break
170
- }
171
151
  case 'delete': {
172
152
  behaviorActionImplementations.delete({
173
153
  context,
@@ -1,7 +1,9 @@
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'
5
7
  import {internalInsertBehaviors} from './behavior.internal.insert'
6
8
  import {internalListItemBehaviors} from './behavior.internal.list-item'
7
9
  import {internalSelectBehaviors} from './behavior.internal.select'
@@ -9,71 +11,6 @@ import {internalStyleBehaviors} from './behavior.internal.style'
9
11
  import {raise} from './behavior.types.action'
10
12
  import {defineBehavior} from './behavior.types.behavior'
11
13
 
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
14
  const raiseDeserializationSuccessOrFailure = defineBehavior({
78
15
  on: 'deserialize',
79
16
  guard: ({snapshot, event}) => {
@@ -505,14 +442,12 @@ export const defaultBehaviors = [
505
442
  ],
506
443
  ],
507
444
  }),
445
+ ...internalAnnotationBehaviors,
446
+ ...internalDecoratorBehaviors,
508
447
  ...internalInsertBehaviors,
509
448
  ...internalListItemBehaviors,
510
449
  ...internalStyleBehaviors,
511
450
  ...internalSelectBehaviors,
512
- toggleAnnotationOff,
513
- toggleAnnotationOn,
514
- toggleDecoratorOff,
515
- toggleDecoratorOn,
516
451
  raiseDeserializationSuccessOrFailure,
517
452
  raiseSerializationSuccessOrFailure,
518
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
+ ]
@@ -51,6 +51,8 @@ export type ExternalBehaviorEvent =
51
51
  | PickFromUnion<
52
52
  InternalBehaviorEvent,
53
53
  'type',
54
+ | 'annotation.toggle'
55
+ | 'decorator.toggle'
54
56
  | 'insert.blocks'
55
57
  | 'list item.add'
56
58
  | 'list item.remove'
@@ -98,13 +100,6 @@ export type SyntheticBehaviorEvent =
98
100
  name: string
99
101
  }
100
102
  }
101
- | {
102
- type: SyntheticBehaviorEventType<'annotation', 'toggle'>
103
- annotation: {
104
- name: string
105
- value: {[prop: string]: unknown}
106
- }
107
- }
108
103
  | {
109
104
  type: SyntheticBehaviorEventType<'block', 'set'>
110
105
  at: [KeyedSegment]
@@ -127,11 +122,6 @@ export type SyntheticBehaviorEvent =
127
122
  type: SyntheticBehaviorEventType<'decorator', 'remove'>
128
123
  decorator: string
129
124
  }
130
- | {
131
- type: SyntheticBehaviorEventType<'decorator', 'toggle'>
132
- decorator: string
133
- offsets?: {anchor: BlockOffset; focus: BlockOffset}
134
- }
135
125
  | {
136
126
  type: SyntheticBehaviorEventType<'delete'>
137
127
  selection: NonNullable<EditorSelection>
@@ -225,6 +215,8 @@ export function isKeyboardBehaviorEvent(
225
215
  **************************************/
226
216
 
227
217
  type InternalBehaviorEventNamespace =
218
+ | 'annotation'
219
+ | 'decorator'
228
220
  | 'deserialize'
229
221
  | 'deserialization'
230
222
  | 'list item'
@@ -240,6 +232,18 @@ type InternalBehaviorEventType<
240
232
  > = TType extends '' ? `${TNamespace}` : `${TNamespace}.${TType}`
241
233
 
242
234
  export type InternalBehaviorEvent =
235
+ | {
236
+ type: InternalBehaviorEventType<'annotation', 'toggle'>
237
+ annotation: {
238
+ name: string
239
+ value: {[prop: string]: unknown}
240
+ }
241
+ }
242
+ | {
243
+ type: InternalBehaviorEventType<'decorator', 'toggle'>
244
+ decorator: string
245
+ offsets?: {anchor: BlockOffset; focus: BlockOffset}
246
+ }
243
247
  | {
244
248
  type: InternalBehaviorEventType<'deserialize'>
245
249
  originEvent:
@@ -899,33 +899,3 @@ export const removeAnnotationActionImplementation: BehaviorActionImplementation<
899
899
  }
900
900
  }
901
901
  }
902
-
903
- export const toggleAnnotationActionImplementation: BehaviorActionImplementation<
904
- 'annotation.toggle',
905
- AddedAnnotationPaths | undefined
906
- > = ({context, action}) => {
907
- const isActive = isAnnotationActive({
908
- editor: action.editor,
909
- annotation: {name: action.annotation.name},
910
- })
911
-
912
- if (isActive) {
913
- removeAnnotationActionImplementation({
914
- context,
915
- action: {
916
- type: 'annotation.remove',
917
- annotation: action.annotation,
918
- editor: action.editor,
919
- },
920
- })
921
- } else {
922
- return addAnnotationActionImplementation({
923
- context,
924
- action: {
925
- type: 'annotation.add',
926
- annotation: action.annotation,
927
- editor: action.editor,
928
- },
929
- })
930
- }
931
- }
@@ -8,7 +8,6 @@ import {isPortableTextBlock, isPortableTextSpan} from '@portabletext/toolkit'
8
8
  import type {PortableTextObject, PortableTextSpan} from '@sanity/types'
9
9
  import {isEqual, uniq} from 'lodash'
10
10
  import {Editor, Element, Node, Path, Range, Text, Transforms} from 'slate'
11
- import {decoratorAddActionImplementation} from '../../behavior-actions/behavior.action.decorator.add'
12
11
  import type {BehaviorActionImplementation} from '../../behavior-actions/behavior.actions'
13
12
  import {debugWithName} from '../../internal-utils/debug'
14
13
  import {getNextSpan, getPreviousSpan} from '../../internal-utils/sibling-utils'
@@ -778,31 +777,3 @@ export function isDecoratorActive({
778
777
  }.marks || []
779
778
  ).includes(decorator)
780
779
  }
781
-
782
- export const toggleDecoratorActionImplementation: BehaviorActionImplementation<
783
- 'decorator.toggle'
784
- > = ({context, action}) => {
785
- const isActive = isDecoratorActive({
786
- editor: action.editor,
787
- decorator: action.decorator,
788
- })
789
-
790
- if (isActive) {
791
- removeDecoratorActionImplementation({
792
- context,
793
- action: {
794
- type: 'decorator.remove',
795
- editor: action.editor,
796
- decorator: action.decorator,
797
- },
798
- })
799
- } else {
800
- decoratorAddActionImplementation({
801
- context,
802
- action: {
803
- ...action,
804
- type: 'decorator.add',
805
- },
806
- })
807
- }
808
- }