@portabletext/editor 1.14.1 → 1.14.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/editor",
3
- "version": "1.14.1",
3
+ "version": "1.14.2",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -64,27 +64,27 @@
64
64
  },
65
65
  "devDependencies": {
66
66
  "@portabletext/toolkit": "^2.0.16",
67
- "@sanity/block-tools": "^3.66.0",
67
+ "@sanity/block-tools": "^3.66.1",
68
68
  "@sanity/diff-match-patch": "^3.1.1",
69
69
  "@sanity/pkg-utils": "^6.11.14",
70
- "@sanity/schema": "^3.66.0",
71
- "@sanity/types": "^3.66.0",
70
+ "@sanity/schema": "^3.66.1",
71
+ "@sanity/types": "^3.66.1",
72
72
  "@testing-library/jest-dom": "^6.6.3",
73
- "@testing-library/react": "^16.0.1",
73
+ "@testing-library/react": "^16.1.0",
74
74
  "@types/debug": "^4.1.5",
75
75
  "@types/lodash": "^4.17.13",
76
76
  "@types/lodash.startcase": "^4.4.9",
77
77
  "@types/react": "^18.3.14",
78
78
  "@types/react-dom": "^18.3.2",
79
- "@typescript-eslint/eslint-plugin": "^8.16.0",
80
- "@typescript-eslint/parser": "^8.16.0",
79
+ "@typescript-eslint/eslint-plugin": "^8.17.0",
80
+ "@typescript-eslint/parser": "^8.17.0",
81
81
  "@vitejs/plugin-react": "^4.3.4",
82
- "@vitest/browser": "^2.1.5",
82
+ "@vitest/browser": "^2.1.8",
83
83
  "@vitest/coverage-istanbul": "^2.1.8",
84
84
  "babel-plugin-react-compiler": "19.0.0-beta-df7b47d-20241124",
85
85
  "eslint": "8.57.1",
86
86
  "eslint-plugin-react-compiler": "19.0.0-beta-df7b47d-20241124",
87
- "eslint-plugin-react-hooks": "^5.0.0",
87
+ "eslint-plugin-react-hooks": "^5.1.0",
88
88
  "jsdom": "^25.0.1",
89
89
  "react": "^18.3.1",
90
90
  "react-dom": "^18.3.1",
@@ -92,14 +92,14 @@
92
92
  "styled-components": "^6.1.13",
93
93
  "typescript": "5.7.2",
94
94
  "vite": "^5.4.11",
95
- "vitest": "^2.1.5",
95
+ "vitest": "^2.1.8",
96
96
  "vitest-browser-react": "^0.0.4",
97
97
  "racejar": "1.1.0"
98
98
  },
99
99
  "peerDependencies": {
100
- "@sanity/block-tools": "^3.66.0",
101
- "@sanity/schema": "^3.66.0",
102
- "@sanity/types": "^3.66.0",
100
+ "@sanity/block-tools": "^3.66.1",
101
+ "@sanity/schema": "^3.66.1",
102
+ "@sanity/types": "^3.66.1",
103
103
  "react": "^16.9 || ^17 || ^18",
104
104
  "rxjs": "^7.8.1",
105
105
  "styled-components": "^6.1.13"
@@ -45,8 +45,8 @@ import {textBlockSetActionImplementation} from './behavior.action.text-block.set
45
45
  import {textBlockUnsetActionImplementation} from './behavior.action.text-block.unset'
46
46
  import type {
47
47
  BehaviorAction,
48
- BehaviorEvent,
49
48
  PickFromUnion,
49
+ SyntheticBehaviorEvent,
50
50
  } from './behavior.types'
51
51
 
52
52
  export type BehaviorActionImplementationContext = Pick<
@@ -82,7 +82,6 @@ const behaviorActionImplementations: BehaviorActionImplementations = {
82
82
  'focus': ({action}) => {
83
83
  ReactEditor.focus(action.editor)
84
84
  },
85
- 'copy': () => {},
86
85
  'delete.backward': ({action}) => {
87
86
  deleteBackward(action.editor, action.unit)
88
87
  },
@@ -186,8 +185,6 @@ const behaviorActionImplementations: BehaviorActionImplementations = {
186
185
  'effect': ({action}) => {
187
186
  action.effect()
188
187
  },
189
- 'key.down': () => {},
190
- 'key.up': () => {},
191
188
  'list item.add': addListItemActionImplementation,
192
189
  'list item.remove': removeListItemActionImplementation,
193
190
  'list item.toggle': toggleListItemActionImplementation,
@@ -227,7 +224,6 @@ const behaviorActionImplementations: BehaviorActionImplementations = {
227
224
  })
228
225
  },
229
226
  'noop': () => {},
230
- 'paste': () => {},
231
227
  'select': ({action}) => {
232
228
  const newSelection = toSlateRange(action.selection, action.editor)
233
229
 
@@ -434,7 +430,7 @@ function performDefaultAction({
434
430
  action,
435
431
  }: {
436
432
  context: BehaviorActionImplementationContext
437
- action: PickFromUnion<BehaviorAction, 'type', BehaviorEvent['type']>
433
+ action: PickFromUnion<BehaviorAction, 'type', SyntheticBehaviorEvent['type']>
438
434
  }) {
439
435
  switch (action.type) {
440
436
  case 'annotation.add': {
@@ -465,13 +461,6 @@ function performDefaultAction({
465
461
  })
466
462
  break
467
463
  }
468
- case 'copy': {
469
- behaviorActionImplementations.copy({
470
- context,
471
- action,
472
- })
473
- break
474
- }
475
464
  case 'decorator.add': {
476
465
  behaviorActionImplementations['decorator.add']({
477
466
  context,
@@ -549,20 +538,6 @@ function performDefaultAction({
549
538
  })
550
539
  break
551
540
  }
552
- case 'key.down': {
553
- behaviorActionImplementations['key.down']({
554
- context,
555
- action,
556
- })
557
- break
558
- }
559
- case 'key.up': {
560
- behaviorActionImplementations['key.up']({
561
- context,
562
- action,
563
- })
564
- break
565
- }
566
541
  case 'list item.toggle': {
567
542
  behaviorActionImplementations['list item.toggle']({
568
543
  context,
@@ -570,13 +545,6 @@ function performDefaultAction({
570
545
  })
571
546
  break
572
547
  }
573
- case 'paste': {
574
- behaviorActionImplementations.paste({
575
- context,
576
- action,
577
- })
578
- break
579
- }
580
548
  default: {
581
549
  behaviorActionImplementations['style.toggle']({
582
550
  context,
@@ -7,7 +7,7 @@ import type {EditorContext} from '../editor-snapshot'
7
7
  /**
8
8
  * @alpha
9
9
  */
10
- export type BehaviorEvent =
10
+ export type SyntheticBehaviorEvent =
11
11
  | {
12
12
  type: 'annotation.add'
13
13
  annotation: {
@@ -31,10 +31,6 @@ export type BehaviorEvent =
31
31
  | {
32
32
  type: 'blur'
33
33
  }
34
- | {
35
- type: 'copy'
36
- data: DataTransfer
37
- }
38
34
  | {
39
35
  type: 'decorator.add'
40
36
  decorator: string
@@ -85,7 +81,20 @@ export type BehaviorEvent =
85
81
  options?: TextInsertTextOptions
86
82
  }
87
83
  | {
88
- type: 'paste'
84
+ type: 'list item.toggle'
85
+ listItem: string
86
+ }
87
+ | {
88
+ type: 'style.toggle'
89
+ style: string
90
+ }
91
+
92
+ /**
93
+ * @alpha
94
+ */
95
+ export type NativeBehaviorEvent =
96
+ | {
97
+ type: 'copy'
89
98
  data: DataTransfer
90
99
  }
91
100
  | {
@@ -103,33 +112,29 @@ export type BehaviorEvent =
103
112
  >
104
113
  }
105
114
  | {
106
- type: 'list item.toggle'
107
- listItem: string
108
- }
109
- | {
110
- type: 'style.toggle'
111
- style: string
115
+ type: 'paste'
116
+ data: DataTransfer
112
117
  }
113
118
 
114
119
  /**
115
120
  * @alpha
116
121
  */
117
122
  export type BehaviorGuard<
118
- TBehaviorEvent extends BehaviorEvent,
123
+ TAnyBehaviorEvent extends BehaviorEvent,
119
124
  TGuardResponse,
120
125
  > = ({
121
126
  context,
122
127
  event,
123
128
  }: {
124
129
  context: EditorContext
125
- event: TBehaviorEvent
130
+ event: TAnyBehaviorEvent
126
131
  }) => TGuardResponse | false
127
132
 
128
133
  /**
129
134
  * @alpha
130
135
  */
131
136
  export type BehaviorActionIntend =
132
- | BehaviorEvent
137
+ | SyntheticBehaviorEvent
133
138
  | {
134
139
  type: 'insert.span'
135
140
  text: string
@@ -224,24 +229,29 @@ export type BehaviorAction = BehaviorActionIntend & {
224
229
  editor: PortableTextSlateEditor
225
230
  }
226
231
 
232
+ /**
233
+ * @alpha
234
+ */
235
+ export type BehaviorEvent = SyntheticBehaviorEvent | NativeBehaviorEvent
236
+
227
237
  /**
228
238
  * @alpha
229
239
  */
230
240
  export type Behavior<
231
- TBehaviorEventType extends BehaviorEvent['type'] = BehaviorEvent['type'],
241
+ TAnyBehaviorEventType extends BehaviorEvent['type'] = BehaviorEvent['type'],
232
242
  TGuardResponse = true,
233
243
  > = {
234
244
  /**
235
245
  * The internal editor event that triggers this behavior.
236
246
  */
237
- on: TBehaviorEventType
247
+ on: TAnyBehaviorEventType
238
248
  /**
239
249
  * Predicate function that determines if the behavior should be executed.
240
250
  * Returning a non-nullable value from the guard will pass the value to the
241
251
  * actions and execute them.
242
252
  */
243
253
  guard?: BehaviorGuard<
244
- PickFromUnion<BehaviorEvent, 'type', TBehaviorEventType>,
254
+ PickFromUnion<BehaviorEvent, 'type', TAnyBehaviorEventType>,
245
255
  TGuardResponse
246
256
  >
247
257
  /**
@@ -255,21 +265,15 @@ export type Behavior<
255
265
  */
256
266
  export type BehaviorActionIntendSet<TGuardResponse = true> = (
257
267
  guardResponse: TGuardResponse,
258
- ) => Array<
259
- OmitFromUnion<
260
- BehaviorActionIntend,
261
- 'type',
262
- 'copy' | 'key.down' | 'key.up' | 'paste'
263
- >
264
- >
268
+ ) => Array<BehaviorActionIntend>
265
269
 
266
270
  /**
267
271
  * @alpha
268
272
  */
269
273
  export function defineBehavior<
270
- TBehaviorEventType extends BehaviorEvent['type'],
274
+ TAnyBehaviorEventType extends BehaviorEvent['type'],
271
275
  TGuardResponse = true,
272
- >(behavior: Behavior<TBehaviorEventType, TGuardResponse>): Behavior {
276
+ >(behavior: Behavior<TAnyBehaviorEventType, TGuardResponse>): Behavior {
273
277
  return behavior as unknown as Behavior
274
278
  }
275
279
 
@@ -26,9 +26,10 @@ import type {
26
26
  Behavior,
27
27
  BehaviorAction,
28
28
  BehaviorActionIntend,
29
- BehaviorEvent,
29
+ NativeBehaviorEvent,
30
30
  OmitFromUnion,
31
31
  PickFromUnion,
32
+ SyntheticBehaviorEvent,
32
33
  } from './behavior/behavior.types'
33
34
  import type {EditorContext} from './editor-snapshot'
34
35
 
@@ -70,7 +71,7 @@ export type InternalEditorEvent =
70
71
  | {type: 'done normalizing'}
71
72
  | {
72
73
  type: 'behavior event'
73
- behaviorEvent: BehaviorEvent
74
+ behaviorEvent: SyntheticBehaviorEvent | NativeBehaviorEvent
74
75
  editor: PortableTextSlateEditor
75
76
  nativeEvent?: {preventDefault: () => void}
76
77
  }
@@ -154,7 +155,7 @@ export type InternalEditorEmittedEvent =
154
155
  | {type: 'done loading'}
155
156
  | {type: 'readOnly toggled'; readOnly: boolean}
156
157
  | PickFromUnion<
157
- BehaviorEvent,
158
+ SyntheticBehaviorEvent,
158
159
  'type',
159
160
  | 'annotation.add'
160
161
  | 'annotation.remove'
@@ -236,16 +237,26 @@ export const editorMachine = setup({
236
237
 
237
238
  debug('Behavior event', event)
238
239
 
239
- const defaultAction = {
240
- ...event.behaviorEvent,
241
- editor: event.editor,
242
- } satisfies BehaviorAction
240
+ const defaultAction =
241
+ event.behaviorEvent.type === 'copy' ||
242
+ event.behaviorEvent.type === 'key.down' ||
243
+ event.behaviorEvent.type === 'key.up' ||
244
+ event.behaviorEvent.type === 'paste'
245
+ ? undefined
246
+ : ({
247
+ ...event.behaviorEvent,
248
+ editor: event.editor,
249
+ } satisfies BehaviorAction)
243
250
 
244
251
  const eventBehaviors = context.behaviors.filter(
245
252
  (behavior) => behavior.on === event.behaviorEvent.type,
246
253
  )
247
254
 
248
255
  if (eventBehaviors.length === 0) {
256
+ if (!defaultAction) {
257
+ return
258
+ }
259
+
249
260
  enqueue.raise({
250
261
  type: 'behavior action intends',
251
262
  editor: event.editor,
@@ -269,6 +280,11 @@ export const editorMachine = setup({
269
280
  console.warn(
270
281
  `Unable to handle event ${event.type} due to missing selection`,
271
282
  )
283
+
284
+ if (!defaultAction) {
285
+ return
286
+ }
287
+
272
288
  enqueue.raise({
273
289
  type: 'behavior action intends',
274
290
  editor: event.editor,
@@ -324,6 +340,10 @@ export const editorMachine = setup({
324
340
  }
325
341
 
326
342
  if (!behaviorOverwritten) {
343
+ if (!defaultAction) {
344
+ return
345
+ }
346
+
327
347
  enqueue.raise({
328
348
  type: 'behavior action intends',
329
349
  editor: event.editor,
package/src/index.ts CHANGED
@@ -15,14 +15,16 @@ export {
15
15
  } from './editor/behavior/behavior.markdown'
16
16
  export {
17
17
  defineBehavior,
18
+ type BehaviorEvent,
18
19
  type Behavior,
19
20
  type BehaviorActionIntend,
20
21
  type BehaviorActionIntendSet,
21
- type BehaviorEvent,
22
+ type SyntheticBehaviorEvent,
22
23
  type BehaviorGuard,
24
+ type BlockOffset,
25
+ type NativeBehaviorEvent,
23
26
  type OmitFromUnion,
24
27
  type PickFromUnion,
25
- type BlockOffset,
26
28
  } from './editor/behavior/behavior.types'
27
29
  export type {Editor, EditorConfig, EditorEvent} from './editor/create-editor'
28
30
  export type {SlateEditor} from './editor/create-slate-editor'