@portabletext/editor 1.21.0 → 1.21.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.21.0",
3
+ "version": "1.21.2",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -93,11 +93,11 @@
93
93
  "@vitejs/plugin-react": "^4.3.4",
94
94
  "@vitest/browser": "^2.1.8",
95
95
  "@vitest/coverage-istanbul": "^2.1.8",
96
- "babel-plugin-react-compiler": "19.0.0-beta-63e3235-20250105",
96
+ "babel-plugin-react-compiler": "19.0.0-beta-e552027-20250112",
97
97
  "eslint": "8.57.1",
98
- "eslint-plugin-react-compiler": "19.0.0-beta-63e3235-20250105",
98
+ "eslint-plugin-react-compiler": "19.0.0-beta-e552027-20250112",
99
99
  "eslint-plugin-react-hooks": "^5.1.0",
100
- "jsdom": "^25.0.1",
100
+ "jsdom": "^26.0.0",
101
101
  "react": "^19.0.0",
102
102
  "react-dom": "^19.0.0",
103
103
  "rxjs": "^7.8.1",
@@ -79,6 +79,7 @@ export type InternalEditorEvent =
79
79
  type: 'behavior event'
80
80
  behaviorEvent: SyntheticBehaviorEvent | NativeBehaviorEvent
81
81
  editor: PortableTextSlateEditor
82
+ defaultActionCallback?: () => void
82
83
  nativeEvent?: {preventDefault: () => void}
83
84
  }
84
85
  | {
@@ -290,22 +291,51 @@ export const editorMachine = setup({
290
291
  ...event.behaviorEvent,
291
292
  editor: event.editor,
292
293
  } satisfies BehaviorAction)
294
+ const defaultActionCallback =
295
+ event.type === 'behavior event'
296
+ ? event.defaultActionCallback
297
+ : undefined
293
298
 
294
299
  const eventBehaviors = [...context.behaviors.values()].filter(
295
300
  (behavior) => behavior.on === event.behaviorEvent.type,
296
301
  )
297
302
 
298
303
  if (eventBehaviors.length === 0) {
304
+ if (defaultActionCallback) {
305
+ withApplyingBehaviorActions(event.editor, () => {
306
+ Editor.withoutNormalizing(event.editor, () => {
307
+ try {
308
+ defaultActionCallback()
309
+ } catch (error) {
310
+ console.error(
311
+ new Error(
312
+ `Performing action "${event.behaviorEvent.type}" failed due to: ${error.message}`,
313
+ ),
314
+ )
315
+ }
316
+ })
317
+ })
318
+ return
319
+ }
320
+
299
321
  if (!defaultAction) {
300
322
  return
301
323
  }
302
324
 
303
325
  withApplyingBehaviorActions(event.editor, () => {
304
326
  Editor.withoutNormalizing(event.editor, () => {
305
- performAction({
306
- context,
307
- action: defaultAction,
308
- })
327
+ try {
328
+ performAction({
329
+ context,
330
+ action: defaultAction,
331
+ })
332
+ } catch (error) {
333
+ console.error(
334
+ new Error(
335
+ `Performing action "${defaultAction.type}" as a result of "${event.behaviorEvent.type}" failed due to: ${error.message}`,
336
+ ),
337
+ )
338
+ }
309
339
  })
310
340
  })
311
341
  event.editor.onChange()
@@ -388,7 +418,16 @@ export const editorMachine = setup({
388
418
  editor: event.editor,
389
419
  }
390
420
 
391
- performAction({context, action})
421
+ try {
422
+ performAction({context, action})
423
+ } catch (error) {
424
+ console.error(
425
+ new Error(
426
+ `Performing action "${action.type}" as a result of "${event.behaviorEvent.type}" failed due to: ${error.message}`,
427
+ ),
428
+ )
429
+ break
430
+ }
392
431
  }
393
432
  })
394
433
  })
@@ -402,16 +441,41 @@ export const editorMachine = setup({
402
441
  }
403
442
 
404
443
  if (!behaviorOverwritten) {
444
+ if (defaultActionCallback) {
445
+ withApplyingBehaviorActions(event.editor, () => {
446
+ Editor.withoutNormalizing(event.editor, () => {
447
+ try {
448
+ defaultActionCallback()
449
+ } catch (error) {
450
+ console.error(
451
+ new Error(
452
+ `Performing "${event.behaviorEvent.type}" failed due to: ${error.message}`,
453
+ ),
454
+ )
455
+ }
456
+ })
457
+ })
458
+ return
459
+ }
460
+
405
461
  if (!defaultAction) {
406
462
  return
407
463
  }
408
464
 
409
465
  withApplyingBehaviorActions(event.editor, () => {
410
466
  Editor.withoutNormalizing(event.editor, () => {
411
- performAction({
412
- context,
413
- action: defaultAction,
414
- })
467
+ try {
468
+ performAction({
469
+ context,
470
+ action: defaultAction,
471
+ })
472
+ } catch (error) {
473
+ console.error(
474
+ new Error(
475
+ `Performing action "${defaultAction.type}" as a result of "${event.behaviorEvent.type}" failed due to: ${error.message}`,
476
+ ),
477
+ )
478
+ }
415
479
  })
416
480
  })
417
481
  event.editor.onChange()
@@ -142,9 +142,21 @@ export function createWithEventListeners(
142
142
  }
143
143
  })
144
144
 
145
- const {select} = editor
145
+ const {
146
+ deleteBackward,
147
+ deleteForward,
148
+ insertBreak,
149
+ insertSoftBreak,
150
+ insertText,
151
+ select,
152
+ } = editor
146
153
 
147
154
  editor.deleteBackward = (unit) => {
155
+ if (isApplyingBehaviorActions(editor)) {
156
+ deleteBackward(unit)
157
+ return
158
+ }
159
+
148
160
  editorActor.send({
149
161
  type: 'behavior event',
150
162
  behaviorEvent: {
@@ -157,6 +169,11 @@ export function createWithEventListeners(
157
169
  }
158
170
 
159
171
  editor.deleteForward = (unit) => {
172
+ if (isApplyingBehaviorActions(editor)) {
173
+ deleteForward(unit)
174
+ return
175
+ }
176
+
160
177
  editorActor.send({
161
178
  type: 'behavior event',
162
179
  behaviorEvent: {
@@ -169,6 +186,11 @@ export function createWithEventListeners(
169
186
  }
170
187
 
171
188
  editor.insertBreak = () => {
189
+ if (isApplyingBehaviorActions(editor)) {
190
+ insertBreak()
191
+ return
192
+ }
193
+
172
194
  editorActor.send({
173
195
  type: 'behavior event',
174
196
  behaviorEvent: {
@@ -180,6 +202,11 @@ export function createWithEventListeners(
180
202
  }
181
203
 
182
204
  editor.insertSoftBreak = () => {
205
+ if (isApplyingBehaviorActions(editor)) {
206
+ insertSoftBreak()
207
+ return
208
+ }
209
+
183
210
  editorActor.send({
184
211
  type: 'behavior event',
185
212
  behaviorEvent: {
@@ -191,6 +218,11 @@ export function createWithEventListeners(
191
218
  }
192
219
 
193
220
  editor.insertText = (text, options) => {
221
+ if (isApplyingBehaviorActions(editor)) {
222
+ insertText(text, options)
223
+ return
224
+ }
225
+
194
226
  editorActor.send({
195
227
  type: 'behavior event',
196
228
  behaviorEvent: {
@@ -199,6 +231,9 @@ export function createWithEventListeners(
199
231
  options,
200
232
  },
201
233
  editor,
234
+ defaultActionCallback: () => {
235
+ insertText(text, options)
236
+ },
202
237
  })
203
238
  return
204
239
  }
@@ -226,6 +261,9 @@ export function createWithEventListeners(
226
261
  ),
227
262
  },
228
263
  editor,
264
+ defaultActionCallback: () => {
265
+ select(location)
266
+ },
229
267
  })
230
268
  return
231
269
  }