@portabletext/editor 1.31.2 → 1.32.0

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.31.2",
3
+ "version": "1.32.0",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -21,6 +21,10 @@ import {
21
21
  removeDecoratorActionImplementation,
22
22
  toggleDecoratorActionImplementation,
23
23
  } from '../editor/plugins/createWithPortableTextMarkModel'
24
+ import {
25
+ historyRedoActionImplementation,
26
+ historyUndoActionImplementation,
27
+ } from '../editor/plugins/createWithUndoRedo'
24
28
  import {toSlatePath} from '../internal-utils/paths'
25
29
  import {toSlateRange} from '../internal-utils/ranges'
26
30
  import {fromSlateValue, toSlateValue} from '../internal-utils/values'
@@ -168,6 +172,8 @@ const behaviorActionImplementations: BehaviorActionImplementations = {
168
172
  },
169
173
  })
170
174
  },
175
+ 'history.redo': historyRedoActionImplementation,
176
+ 'history.undo': historyUndoActionImplementation,
171
177
  'insert.block': insertBlockActionImplementation,
172
178
  'insert.blocks': insertBlocksActionImplementation,
173
179
  'insert.block object': insertBlockObjectActionImplementation,
@@ -470,6 +476,20 @@ function performDefaultAction({
470
476
  })
471
477
  break
472
478
  }
479
+ case 'history.redo': {
480
+ behaviorActionImplementations['history.redo']({
481
+ context,
482
+ action,
483
+ })
484
+ break
485
+ }
486
+ case 'history.undo': {
487
+ behaviorActionImplementations['history.undo']({
488
+ context,
489
+ action,
490
+ })
491
+ break
492
+ }
473
493
  case 'insert.block': {
474
494
  behaviorActionImplementations['insert.block']({
475
495
  context,
@@ -90,6 +90,12 @@ export type SyntheticBehaviorEvent =
90
90
  | {
91
91
  type: 'focus'
92
92
  }
93
+ | {
94
+ type: 'history.redo'
95
+ }
96
+ | {
97
+ type: 'history.undo'
98
+ }
93
99
  | {
94
100
  type: 'insert.blocks'
95
101
  blocks: Array<PortableTextBlock>
@@ -1,7 +1,6 @@
1
1
  import type {Patch} from '@portabletext/patches'
2
2
  import type {PortableTextBlock} from '@sanity/types'
3
3
  import type {FocusEvent} from 'react'
4
- import {Editor} from 'slate'
5
4
  import {
6
5
  assertEvent,
7
6
  assign,
@@ -30,7 +29,10 @@ import type {
30
29
  } from '../types/editor'
31
30
  import type {EditorSchema} from './define-schema'
32
31
  import {createEditorSnapshot} from './editor-snapshot'
33
- import {withApplyingBehaviorActions} from './with-applying-behavior-actions'
32
+ import {
33
+ withApplyingBehaviorActionIntendSet,
34
+ withApplyingBehaviorActions,
35
+ } from './with-applying-behavior-actions'
34
36
 
35
37
  export * from 'xstate/guards'
36
38
 
@@ -315,17 +317,15 @@ export const editorMachine = setup({
315
317
  if (eventBehaviors.length === 0) {
316
318
  if (defaultActionCallback) {
317
319
  withApplyingBehaviorActions(event.editor, () => {
318
- Editor.withoutNormalizing(event.editor, () => {
319
- try {
320
- defaultActionCallback()
321
- } catch (error) {
322
- console.error(
323
- new Error(
324
- `Performing action "${event.behaviorEvent.type}" failed due to: ${error.message}`,
325
- ),
326
- )
327
- }
328
- })
320
+ try {
321
+ defaultActionCallback()
322
+ } catch (error) {
323
+ console.error(
324
+ new Error(
325
+ `Performing action "${event.behaviorEvent.type}" failed due to: ${error.message}`,
326
+ ),
327
+ )
328
+ }
329
329
  })
330
330
  return
331
331
  }
@@ -335,20 +335,18 @@ export const editorMachine = setup({
335
335
  }
336
336
 
337
337
  withApplyingBehaviorActions(event.editor, () => {
338
- Editor.withoutNormalizing(event.editor, () => {
339
- try {
340
- performAction({
341
- context,
342
- action: defaultAction,
343
- })
344
- } catch (error) {
345
- console.error(
346
- new Error(
347
- `Performing action "${defaultAction.type}" as a result of "${event.behaviorEvent.type}" failed due to: ${error.message}`,
348
- ),
349
- )
350
- }
351
- })
338
+ try {
339
+ performAction({
340
+ context,
341
+ action: defaultAction,
342
+ })
343
+ } catch (error) {
344
+ console.error(
345
+ new Error(
346
+ `Performing action "${defaultAction.type}" as a result of "${event.behaviorEvent.type}" failed due to: ${error.message}`,
347
+ ),
348
+ )
349
+ }
352
350
  })
353
351
  event.editor.onChange()
354
352
  return
@@ -390,43 +388,41 @@ export const editorMachine = setup({
390
388
  (actionIntend) => actionIntend.type !== 'effect',
391
389
  ))
392
390
 
393
- withApplyingBehaviorActions(event.editor, () => {
394
- Editor.withoutNormalizing(event.editor, () => {
395
- for (const actionIntend of actionIntends) {
396
- if (actionIntend.type === 'raise') {
397
- if (isCustomBehaviorEvent(actionIntend.event)) {
398
- enqueue.raise({
399
- type: 'custom behavior event',
400
- behaviorEvent: actionIntend.event as CustomBehaviorEvent,
401
- editor: event.editor,
402
- })
403
- } else {
404
- enqueue.raise({
405
- type: 'behavior event',
406
- behaviorEvent: actionIntend.event,
407
- editor: event.editor,
408
- })
409
- }
410
- continue
391
+ withApplyingBehaviorActionIntendSet(event.editor, () => {
392
+ for (const actionIntend of actionIntends) {
393
+ if (actionIntend.type === 'raise') {
394
+ if (isCustomBehaviorEvent(actionIntend.event)) {
395
+ enqueue.raise({
396
+ type: 'custom behavior event',
397
+ behaviorEvent: actionIntend.event as CustomBehaviorEvent,
398
+ editor: event.editor,
399
+ })
400
+ } else {
401
+ enqueue.raise({
402
+ type: 'behavior event',
403
+ behaviorEvent: actionIntend.event,
404
+ editor: event.editor,
405
+ })
411
406
  }
407
+ continue
408
+ }
412
409
 
413
- const action = {
414
- ...actionIntend,
415
- editor: event.editor,
416
- }
410
+ const action = {
411
+ ...actionIntend,
412
+ editor: event.editor,
413
+ }
417
414
 
418
- try {
419
- performAction({context, action})
420
- } catch (error) {
421
- console.error(
422
- new Error(
423
- `Performing action "${action.type}" as a result of "${event.behaviorEvent.type}" failed due to: ${error.message}`,
424
- ),
425
- )
426
- break
427
- }
415
+ try {
416
+ performAction({context, action})
417
+ } catch (error) {
418
+ console.error(
419
+ new Error(
420
+ `Performing action "${action.type}" as a result of "${event.behaviorEvent.type}" failed due to: ${error.message}`,
421
+ ),
422
+ )
423
+ break
428
424
  }
429
- })
425
+ }
430
426
  })
431
427
  event.editor.onChange()
432
428
  }
@@ -440,17 +436,15 @@ export const editorMachine = setup({
440
436
  if (!behaviorOverwritten) {
441
437
  if (defaultActionCallback) {
442
438
  withApplyingBehaviorActions(event.editor, () => {
443
- Editor.withoutNormalizing(event.editor, () => {
444
- try {
445
- defaultActionCallback()
446
- } catch (error) {
447
- console.error(
448
- new Error(
449
- `Performing "${event.behaviorEvent.type}" failed due to: ${error.message}`,
450
- ),
451
- )
452
- }
453
- })
439
+ try {
440
+ defaultActionCallback()
441
+ } catch (error) {
442
+ console.error(
443
+ new Error(
444
+ `Performing "${event.behaviorEvent.type}" failed due to: ${error.message}`,
445
+ ),
446
+ )
447
+ }
454
448
  })
455
449
  return
456
450
  }
@@ -460,20 +454,18 @@ export const editorMachine = setup({
460
454
  }
461
455
 
462
456
  withApplyingBehaviorActions(event.editor, () => {
463
- Editor.withoutNormalizing(event.editor, () => {
464
- try {
465
- performAction({
466
- context,
467
- action: defaultAction,
468
- })
469
- } catch (error) {
470
- console.error(
471
- new Error(
472
- `Performing action "${defaultAction.type}" as a result of "${event.behaviorEvent.type}" failed due to: ${error.message}`,
473
- ),
474
- )
475
- }
476
- })
457
+ try {
458
+ performAction({
459
+ context,
460
+ action: defaultAction,
461
+ })
462
+ } catch (error) {
463
+ console.error(
464
+ new Error(
465
+ `Performing action "${defaultAction.type}" as a result of "${event.behaviorEvent.type}" failed due to: ${error.message}`,
466
+ ),
467
+ )
468
+ }
477
469
  })
478
470
  event.editor.onChange()
479
471
  }
@@ -610,6 +602,9 @@ export const editorMachine = setup({
610
602
  'focus': {
611
603
  actions: emit(({event}) => event),
612
604
  },
605
+ 'history.*': {
606
+ actions: emit(({event}) => event),
607
+ },
613
608
  'insert.*': {
614
609
  actions: emit(({event}) => event),
615
610
  },
@@ -1,5 +1,6 @@
1
1
  import {Editor} from 'slate'
2
2
  import {insertSoftBreakActionImplementation} from '../../behavior-actions/behavior.action.insert-break'
3
+ import {performAction} from '../../behavior-actions/behavior.actions'
3
4
  import {toPortableTextRange} from '../../internal-utils/ranges'
4
5
  import {fromSlateValue} from '../../internal-utils/values'
5
6
  import {KEY_TO_VALUE_ELEMENT} from '../../internal-utils/weakMaps'
@@ -179,6 +180,31 @@ export function createWithEventListeners(
179
180
  return
180
181
  }
181
182
 
183
+ editor.redo = () => {
184
+ if (isApplyingBehaviorActions(editor)) {
185
+ performAction({
186
+ context: {
187
+ keyGenerator: editorActor.getSnapshot().context.keyGenerator,
188
+ schema: editorActor.getSnapshot().context.schema,
189
+ },
190
+ action: {
191
+ type: 'history.redo',
192
+ editor,
193
+ },
194
+ })
195
+ return
196
+ }
197
+
198
+ editorActor.send({
199
+ type: 'behavior event',
200
+ behaviorEvent: {
201
+ type: 'history.redo',
202
+ },
203
+ editor,
204
+ })
205
+ return
206
+ }
207
+
182
208
  editor.select = (location) => {
183
209
  if (isApplyingBehaviorActions(editor)) {
184
210
  select(location)
@@ -234,6 +260,31 @@ export function createWithEventListeners(
234
260
  return
235
261
  }
236
262
 
263
+ editor.undo = () => {
264
+ if (isApplyingBehaviorActions(editor)) {
265
+ performAction({
266
+ context: {
267
+ keyGenerator: editorActor.getSnapshot().context.keyGenerator,
268
+ schema: editorActor.getSnapshot().context.schema,
269
+ },
270
+ action: {
271
+ type: 'history.undo',
272
+ editor,
273
+ },
274
+ })
275
+ return
276
+ }
277
+
278
+ editorActor.send({
279
+ type: 'behavior event',
280
+ behaviorEvent: {
281
+ type: 'history.undo',
282
+ },
283
+ editor,
284
+ })
285
+ return
286
+ }
287
+
237
288
  return editor
238
289
  }
239
290
  }
@@ -111,8 +111,24 @@ export function createEditableAPI(
111
111
  }.marks || []
112
112
  )
113
113
  },
114
- undo: (): void => editor.undo(),
115
- redo: (): void => editor.redo(),
114
+ undo: (): void => {
115
+ editorActor.send({
116
+ type: 'behavior event',
117
+ behaviorEvent: {
118
+ type: 'history.undo',
119
+ },
120
+ editor,
121
+ })
122
+ },
123
+ redo: (): void => {
124
+ editorActor.send({
125
+ type: 'behavior event',
126
+ behaviorEvent: {
127
+ type: 'history.redo',
128
+ },
129
+ editor,
130
+ })
131
+ },
116
132
  select: (selection: EditorSelection): void => {
117
133
  const slateSelection = toSlateRange(selection, editor)
118
134
  if (slateSelection) {