@portabletext/editor 1.48.4 → 1.48.6

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.48.4",
3
+ "version": "1.48.6",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -76,7 +76,7 @@
76
76
  "react-compiler-runtime": "19.1.0-rc.1",
77
77
  "slate": "0.114.0",
78
78
  "slate-dom": "^0.114.0",
79
- "slate-react": "0.114.0",
79
+ "slate-react": "0.114.2",
80
80
  "use-effect-event": "^1.0.2",
81
81
  "xstate": "^5.19.2",
82
82
  "@portabletext/block-tools": "1.1.22",
@@ -94,7 +94,7 @@
94
94
  "@types/lodash": "^4.17.16",
95
95
  "@types/lodash.startcase": "^4.4.9",
96
96
  "@types/react": "^19.1.2",
97
- "@types/react-dom": "^19.1.2",
97
+ "@types/react-dom": "^19.1.3",
98
98
  "@typescript-eslint/eslint-plugin": "^8.29.0",
99
99
  "@typescript-eslint/parser": "^8.29.0",
100
100
  "@vitejs/plugin-react": "^4.4.1",
@@ -1,4 +1,3 @@
1
- import {omit} from 'lodash'
2
1
  import type {InternalBehaviorAction} from '../behaviors/behavior.types.action'
3
2
  import type {EditorContext} from '../editor/editor-snapshot'
4
3
  import {removeDecoratorActionImplementation} from '../editor/plugins/createWithPortableTextMarkModel'
@@ -6,7 +5,6 @@ import {
6
5
  historyRedoActionImplementation,
7
6
  historyUndoActionImplementation,
8
7
  } from '../editor/plugins/createWithUndoRedo'
9
- import {debugWithName} from '../internal-utils/debug'
10
8
  import type {PickFromUnion} from '../type-utils'
11
9
  import {addAnnotationActionImplementation} from './behavior.action.annotation.add'
12
10
  import {removeAnnotationActionImplementation} from './behavior.action.annotation.remove'
@@ -27,8 +25,6 @@ import {moveBlockActionImplementation} from './behavior.action.move.block'
27
25
  import {moveForwardActionImplementation} from './behavior.action.move.forward'
28
26
  import {selectActionImplementation} from './behavior.action.select'
29
27
 
30
- const debug = debugWithName('behaviors:action')
31
-
32
28
  export type BehaviorActionImplementationContext = Pick<
33
29
  EditorContext,
34
30
  'keyGenerator' | 'schema'
@@ -80,8 +76,6 @@ export function performAction({
80
76
  context: BehaviorActionImplementationContext
81
77
  action: InternalBehaviorAction
82
78
  }) {
83
- debug(JSON.stringify(omit(action, ['editor']), null, 2))
84
-
85
79
  switch (action.type) {
86
80
  case 'annotation.add': {
87
81
  behaviorActionImplementations['annotation.add']({
@@ -9,11 +9,11 @@ import {
9
9
  import {debugWithName} from '../internal-utils/debug'
10
10
  import type {PortableTextSlateEditor} from '../types/editor'
11
11
  import {defaultBehaviors} from './behavior.default'
12
- import type {InternalBehaviorAction} from './behavior.types.action'
13
12
  import {
14
13
  isAbstractBehaviorEvent,
15
14
  isCustomBehaviorEvent,
16
15
  isNativeBehaviorEvent,
16
+ isSyntheticBehaviorEvent,
17
17
  } from './behavior.types.event'
18
18
 
19
19
  const debug = debugWithName('behaviors:event')
@@ -55,16 +55,6 @@ export function performEvent({
55
55
  }) {
56
56
  debug(`(${mode}:${eventCategory(event)})`, JSON.stringify(event, null, 2))
57
57
 
58
- const defaultAction =
59
- isCustomBehaviorEvent(event) ||
60
- isNativeBehaviorEvent(event) ||
61
- isAbstractBehaviorEvent(event)
62
- ? undefined
63
- : ({
64
- ...event,
65
- editor,
66
- } satisfies InternalBehaviorAction)
67
-
68
58
  const eventBehaviors = [
69
59
  ...remainingEventBehaviors,
70
60
  ...defaultBehaviors,
@@ -105,24 +95,30 @@ export function performEvent({
105
95
  return behavior.on === event.type
106
96
  })
107
97
 
108
- if (eventBehaviors.length === 0) {
109
- if (!defaultAction) {
110
- return
111
- }
98
+ if (eventBehaviors.length === 0 && isSyntheticBehaviorEvent(event)) {
99
+ nativeEvent?.preventDefault()
112
100
 
113
101
  withApplyingBehaviorActions(editor, () => {
114
102
  try {
103
+ debug(
104
+ `(execute:${eventCategory(event)})`,
105
+ JSON.stringify(event, null, 2),
106
+ )
107
+
115
108
  performAction({
116
109
  context: {
117
110
  keyGenerator,
118
111
  schema,
119
112
  },
120
- action: defaultAction,
113
+ action: {
114
+ ...event,
115
+ editor,
116
+ },
121
117
  })
122
118
  } catch (error) {
123
119
  console.error(
124
120
  new Error(
125
- `Performing action "${defaultAction.type}" as a result of "${event.type}" failed due to: ${error.message}`,
121
+ `Executing "${event.type}" failed due to: ${error.message}`,
126
122
  ),
127
123
  )
128
124
  }
@@ -234,52 +230,21 @@ export function performEvent({
234
230
  continue
235
231
  }
236
232
 
237
- if (isAbstractBehaviorEvent(action.event)) {
238
- nativeEventPrevented = true
233
+ nativeEventPrevented = true
239
234
 
240
- performEvent({
241
- mode: 'execute',
242
- behaviors,
243
- remainingEventBehaviors: behaviors,
244
- event: action.event,
245
- editor,
246
- keyGenerator,
247
- schema,
248
- getSnapshot,
249
- nativeEvent: undefined,
250
- })
251
- } else {
252
- const internalAction = {
253
- ...action.event,
254
- editor,
255
- }
256
- let actionFailed = false
257
-
258
- withApplyingBehaviorActions(editor, () => {
259
- try {
260
- performAction({
261
- context: {
262
- keyGenerator,
263
- schema,
264
- },
265
- action: internalAction,
266
- })
267
- } catch (error) {
268
- console.error(
269
- new Error(
270
- `Performing action "${action.event.type}" as a result of "${event.type}" failed due to: ${error.message}`,
271
- ),
272
- )
273
- actionFailed = true
274
- }
275
- })
276
-
277
- if (actionFailed) {
278
- break
279
- }
280
-
281
- editor.onChange()
282
- }
235
+ performEvent({
236
+ mode: 'execute',
237
+ behaviors,
238
+ remainingEventBehaviors: isAbstractBehaviorEvent(action.event)
239
+ ? behaviors
240
+ : [],
241
+ event: action.event,
242
+ editor,
243
+ keyGenerator,
244
+ schema,
245
+ getSnapshot,
246
+ nativeEvent: undefined,
247
+ })
283
248
  }
284
249
  })
285
250
 
@@ -351,19 +316,27 @@ export function performEvent({
351
316
  break
352
317
  }
353
318
 
354
- if (!defaultBehaviorOverwritten && defaultAction) {
319
+ if (!defaultBehaviorOverwritten && isSyntheticBehaviorEvent(event)) {
355
320
  nativeEvent?.preventDefault()
356
321
 
357
322
  withApplyingBehaviorActions(editor, () => {
358
323
  try {
324
+ debug(
325
+ `(execute:${eventCategory(event)})`,
326
+ JSON.stringify(event, null, 2),
327
+ )
328
+
359
329
  performAction({
360
330
  context: {keyGenerator, schema},
361
- action: defaultAction,
331
+ action: {
332
+ ...event,
333
+ editor,
334
+ },
362
335
  })
363
336
  } catch (error) {
364
337
  console.error(
365
338
  new Error(
366
- `Performing action "${defaultAction.type}" as a result of "${event.type}" failed due to: ${error.message}`,
339
+ `Executing "${event.type}" failed due to: ${error.message}`,
367
340
  ),
368
341
  )
369
342
  }
@@ -2,7 +2,7 @@ import type {KeyedSegment, PortableTextBlock} from '@sanity/types'
2
2
  import type {TextUnit} from 'slate'
3
3
  import type {EventPosition} from '../internal-utils/event-position'
4
4
  import type {MIMEType} from '../internal-utils/mime-type'
5
- import type {PickFromUnion, StrictExtract} from '../type-utils'
5
+ import type {OmitFromUnion, PickFromUnion, StrictExtract} from '../type-utils'
6
6
  import type {BlockOffset} from '../types/block-offset'
7
7
  import type {BlockWithOptionalKey} from '../types/block-with-optional-key'
8
8
  import type {EditorSelection} from '../types/editor'
@@ -197,10 +197,18 @@ export type SyntheticBehaviorEvent =
197
197
 
198
198
  export type InsertPlacement = 'auto' | 'after' | 'before'
199
199
 
200
- export function isKeyboardBehaviorEvent(
200
+ export function isSyntheticBehaviorEvent(
201
201
  event: BehaviorEvent,
202
- ): event is KeyboardBehaviorEvent {
203
- return event.type.startsWith('keyboard.')
202
+ ): event is OmitFromUnion<
203
+ SyntheticBehaviorEvent,
204
+ 'type',
205
+ AbstractBehaviorEventType
206
+ > {
207
+ return (
208
+ !isCustomBehaviorEvent(event) &&
209
+ !isNativeBehaviorEvent(event) &&
210
+ !isAbstractBehaviorEvent(event)
211
+ )
204
212
  }
205
213
 
206
214
  /**************************************