@portabletext/editor 1.48.13 → 1.48.14

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.
Files changed (67) hide show
  1. package/lib/_chunks-cjs/editor-provider.cjs +633 -628
  2. package/lib/_chunks-cjs/editor-provider.cjs.map +1 -1
  3. package/lib/_chunks-es/editor-provider.js +634 -629
  4. package/lib/_chunks-es/editor-provider.js.map +1 -1
  5. package/lib/behaviors/index.cjs.map +1 -1
  6. package/lib/behaviors/index.d.cts +4 -2564
  7. package/lib/behaviors/index.d.ts +4 -2564
  8. package/lib/behaviors/index.js.map +1 -1
  9. package/lib/index.d.cts +5 -2564
  10. package/lib/index.d.ts +5 -2564
  11. package/lib/plugins/index.cjs +1 -7
  12. package/lib/plugins/index.cjs.map +1 -1
  13. package/lib/plugins/index.d.cts +4 -2569
  14. package/lib/plugins/index.d.ts +4 -2569
  15. package/lib/plugins/index.js +2 -8
  16. package/lib/plugins/index.js.map +1 -1
  17. package/lib/selectors/index.d.cts +3 -2563
  18. package/lib/selectors/index.d.ts +3 -2563
  19. package/lib/utils/index.d.cts +5 -2564
  20. package/lib/utils/index.d.ts +5 -2564
  21. package/package.json +1 -1
  22. package/src/behaviors/behavior.abstract.keyboard.ts +16 -0
  23. package/src/behaviors/{behavior.default.ts → behavior.abstract.ts} +3 -3
  24. package/src/behaviors/behavior.core.ts +0 -3
  25. package/src/behaviors/behavior.perform-event.ts +27 -51
  26. package/src/behaviors/behavior.types.action.ts +1 -11
  27. package/src/editor/PortableTextEditor.tsx +1 -1
  28. package/src/editor/editor-machine.ts +7 -4
  29. package/src/editor/mutation-machine.ts +6 -6
  30. package/src/editor/plugins/create-with-event-listeners.ts +25 -25
  31. package/src/editor/plugins/createWithEditableAPI.ts +3 -3
  32. package/src/editor/plugins/createWithPatches.ts +13 -5
  33. package/src/editor/plugins/createWithPortableTextMarkModel.ts +5 -5
  34. package/src/editor/plugins/createWithUndoRedo.ts +8 -8
  35. package/src/editor/with-applying-behavior-operations.ts +18 -0
  36. package/src/editor/{with-applying-behavior-actions.ts → with-undo-step.ts} +1 -19
  37. package/src/index.ts +1 -1
  38. package/src/{behavior-actions/behavior.action.annotation.add.ts → operations/behavior.operation.annotation.add.ts} +7 -7
  39. package/src/{behavior-actions/behavior.action.annotation.remove.ts → operations/behavior.operation.annotation.remove.ts} +6 -6
  40. package/src/{behavior-actions/behavior.action.block.set.ts → operations/behavior.operation.block.set.ts} +14 -14
  41. package/src/{behavior-actions/behavior.action.block.unset.ts → operations/behavior.operation.block.unset.ts} +19 -17
  42. package/src/{behavior-actions/behavior.action.decorator.add.ts → operations/behavior.operation.decorator.add.ts} +10 -10
  43. package/src/operations/behavior.operation.delete.backward.ts +8 -0
  44. package/src/operations/behavior.operation.delete.block.ts +24 -0
  45. package/src/operations/behavior.operation.delete.forward.ts +8 -0
  46. package/src/{behavior-actions/behavior.action.delete.ts → operations/behavior.operation.delete.ts} +8 -8
  47. package/src/{behavior-actions/behavior.action.insert-inline-object.ts → operations/behavior.operation.insert-inline-object.ts} +11 -11
  48. package/src/{behavior-actions/behavior.action.insert-span.ts → operations/behavior.operation.insert-span.ts} +15 -15
  49. package/src/{behavior-actions/behavior.action.insert.block.ts → operations/behavior.operation.insert.block.ts} +8 -8
  50. package/src/operations/behavior.operation.insert.text.ts +17 -0
  51. package/src/operations/behavior.operation.move.backward.ts +12 -0
  52. package/src/operations/behavior.operation.move.block.ts +16 -0
  53. package/src/operations/behavior.operation.move.forward.ts +11 -0
  54. package/src/operations/behavior.operation.select.ts +15 -0
  55. package/src/operations/behavior.operations.ts +239 -0
  56. package/src/plugins/index.ts +0 -1
  57. package/src/behavior-actions/behavior.action.delete.backward.ts +0 -7
  58. package/src/behavior-actions/behavior.action.delete.block.ts +0 -24
  59. package/src/behavior-actions/behavior.action.delete.forward.ts +0 -7
  60. package/src/behavior-actions/behavior.action.insert.text.ts +0 -17
  61. package/src/behavior-actions/behavior.action.move.backward.ts +0 -12
  62. package/src/behavior-actions/behavior.action.move.block.ts +0 -16
  63. package/src/behavior-actions/behavior.action.move.forward.ts +0 -11
  64. package/src/behavior-actions/behavior.action.select.ts +0 -15
  65. package/src/behavior-actions/behavior.actions.ts +0 -219
  66. package/src/behaviors/behavior.default.raise-soft-break.ts +0 -14
  67. package/src/plugins/plugin.core.tsx +0 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/editor",
3
- "version": "1.48.13",
3
+ "version": "1.48.14",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -0,0 +1,16 @@
1
+ import {keyIs} from '../internal-utils/key-is'
2
+ import {raise} from './behavior.types.action'
3
+ import {defineBehavior} from './behavior.types.behavior'
4
+
5
+ export const abstractKeyboardBehaviors = [
6
+ /**
7
+ * On WebKit, Shift+Enter results in an `insertParagraph` input event rather
8
+ * than an `insertLineBreak` input event. This Behavior makes sure we catch
9
+ * that `keyboard.keydown` event beforehand and raise an `insert.soft break` manually.
10
+ */
11
+ defineBehavior({
12
+ on: 'keyboard.keydown',
13
+ guard: ({event}) => keyIs.lineBreak(event.originEvent),
14
+ actions: [() => [raise({type: 'insert.soft break'})]],
15
+ }),
16
+ ]
@@ -7,12 +7,12 @@ import {abstractAnnotationBehaviors} from './behavior.abstract.annotation'
7
7
  import {abstractDecoratorBehaviors} from './behavior.abstract.decorator'
8
8
  import {abstractDeleteBehaviors} from './behavior.abstract.delete'
9
9
  import {abstractInsertBehaviors} from './behavior.abstract.insert'
10
+ import {abstractKeyboardBehaviors} from './behavior.abstract.keyboard'
10
11
  import {abstractListItemBehaviors} from './behavior.abstract.list-item'
11
12
  import {abstractMoveBehaviors} from './behavior.abstract.move'
12
13
  import {abstractSelectBehaviors} from './behavior.abstract.select'
13
14
  import {abstractSplitBehaviors} from './behavior.abstract.split'
14
15
  import {abstractStyleBehaviors} from './behavior.abstract.style'
15
- import {raiseInsertSoftBreak} from './behavior.default.raise-soft-break'
16
16
  import {raise} from './behavior.types.action'
17
17
  import {defineBehavior} from './behavior.types.behavior'
18
18
 
@@ -102,7 +102,7 @@ const raiseSerializationSuccessOrFailure = defineBehavior({
102
102
  ],
103
103
  })
104
104
 
105
- export const defaultBehaviors = [
105
+ export const abstractBehaviors = [
106
106
  defineBehavior({
107
107
  on: 'clipboard.copy',
108
108
  guard: ({snapshot}) => {
@@ -451,6 +451,7 @@ export const defaultBehaviors = [
451
451
  ...abstractDecoratorBehaviors,
452
452
  ...abstractDeleteBehaviors,
453
453
  ...abstractInsertBehaviors,
454
+ ...abstractKeyboardBehaviors,
454
455
  ...abstractListItemBehaviors,
455
456
  ...abstractMoveBehaviors,
456
457
  ...abstractStyleBehaviors,
@@ -458,5 +459,4 @@ export const defaultBehaviors = [
458
459
  ...abstractSplitBehaviors,
459
460
  raiseDeserializationSuccessOrFailure,
460
461
  raiseSerializationSuccessOrFailure,
461
- raiseInsertSoftBreak,
462
462
  ]
@@ -5,9 +5,6 @@ import {coreDndBehaviors} from './behavior.core.dnd'
5
5
  import {coreInsertBreakBehaviors} from './behavior.core.insert-break'
6
6
  import {coreListBehaviors} from './behavior.core.lists'
7
7
 
8
- /**
9
- * @beta
10
- */
11
8
  export const coreBehaviors = [
12
9
  coreAnnotationBehaviors.addAnnotationOnCollapsedSelection,
13
10
  coreDecoratorBehaviors.strongShortcut,
@@ -1,13 +1,11 @@
1
- import {performAction} from '../behavior-actions/behavior.actions'
2
1
  import type {EditorSchema} from '../editor/editor-schema'
3
2
  import type {EditorSnapshot} from '../editor/editor-snapshot'
4
- import {
5
- withApplyingBehaviorActions,
6
- withUndoStep,
7
- } from '../editor/with-applying-behavior-actions'
3
+ import {withApplyingBehaviorOperations} from '../editor/with-applying-behavior-operations'
4
+ import {withUndoStep} from '../editor/with-undo-step'
8
5
  import {debugWithName} from '../internal-utils/debug'
6
+ import {performOperation} from '../operations/behavior.operations'
9
7
  import type {PortableTextSlateEditor} from '../types/editor'
10
- import {defaultBehaviors} from './behavior.default'
8
+ import {abstractBehaviors} from './behavior.abstract'
11
9
  import type {BehaviorAction} from './behavior.types.action'
12
10
  import type {Behavior} from './behavior.types.behavior'
13
11
  import {
@@ -59,7 +57,7 @@ export function performEvent({
59
57
 
60
58
  const eventBehaviors = [
61
59
  ...remainingEventBehaviors,
62
- ...defaultBehaviors,
60
+ ...abstractBehaviors,
63
61
  ].filter((behavior) => {
64
62
  // Catches all events
65
63
  if (behavior.on === '*') {
@@ -100,30 +98,19 @@ export function performEvent({
100
98
  if (eventBehaviors.length === 0 && isSyntheticBehaviorEvent(event)) {
101
99
  nativeEvent?.preventDefault()
102
100
 
103
- withApplyingBehaviorActions(editor, () => {
104
- try {
105
- debug(
106
- `(execute:${eventCategory(event)})`,
107
- JSON.stringify(event, null, 2),
108
- )
109
-
110
- performAction({
111
- context: {
112
- keyGenerator,
113
- schema,
114
- },
115
- action: {
116
- ...event,
117
- editor,
118
- },
119
- })
120
- } catch (error) {
121
- console.error(
122
- new Error(
123
- `Executing "${event.type}" failed due to: ${error.message}`,
124
- ),
125
- )
126
- }
101
+ withApplyingBehaviorOperations(editor, () => {
102
+ debug(`(execute:${eventCategory(event)})`, JSON.stringify(event, null, 2))
103
+
104
+ performOperation({
105
+ context: {
106
+ keyGenerator,
107
+ schema,
108
+ },
109
+ operation: {
110
+ ...event,
111
+ editor,
112
+ },
113
+ })
127
114
  })
128
115
 
129
116
  editor.onChange()
@@ -339,27 +326,16 @@ export function performEvent({
339
326
  if (!defaultBehaviorOverwritten && isSyntheticBehaviorEvent(event)) {
340
327
  nativeEvent?.preventDefault()
341
328
 
342
- withApplyingBehaviorActions(editor, () => {
343
- try {
344
- debug(
345
- `(execute:${eventCategory(event)})`,
346
- JSON.stringify(event, null, 2),
347
- )
329
+ withApplyingBehaviorOperations(editor, () => {
330
+ debug(`(execute:${eventCategory(event)})`, JSON.stringify(event, null, 2))
348
331
 
349
- performAction({
350
- context: {keyGenerator, schema},
351
- action: {
352
- ...event,
353
- editor,
354
- },
355
- })
356
- } catch (error) {
357
- console.error(
358
- new Error(
359
- `Executing "${event.type}" failed due to: ${error.message}`,
360
- ),
361
- )
362
- }
332
+ performOperation({
333
+ context: {keyGenerator, schema},
334
+ operation: {
335
+ ...event,
336
+ editor,
337
+ },
338
+ })
363
339
  })
364
340
 
365
341
  editor.onChange()
@@ -1,8 +1,6 @@
1
1
  import type {EditorSnapshot} from '../editor/editor-snapshot'
2
- import type {OmitFromUnion, PickFromUnion} from '../type-utils'
3
- import type {PortableTextSlateEditor} from '../types/editor'
2
+ import type {PickFromUnion} from '../type-utils'
4
3
  import type {
5
- AbstractBehaviorEventType,
6
4
  CustomBehaviorEvent,
7
5
  NativeBehaviorEvent,
8
6
  SyntheticBehaviorEvent,
@@ -75,11 +73,3 @@ export type BehaviorActionSet<TBehaviorEvent, TGuardResponse> = (
75
73
  },
76
74
  guardResponse: TGuardResponse,
77
75
  ) => Array<BehaviorAction>
78
-
79
- export type InternalBehaviorAction = OmitFromUnion<
80
- SyntheticBehaviorEvent,
81
- 'type',
82
- AbstractBehaviorEventType
83
- > & {
84
- editor: PortableTextSlateEditor
85
- }
@@ -16,10 +16,10 @@ import {Subject} from 'rxjs'
16
16
  import {Slate} from 'slate-react'
17
17
  import {useEffectEvent} from 'use-effect-event'
18
18
  import {createActor} from 'xstate'
19
- import type {AddedAnnotationPaths} from '../behavior-actions/behavior.action.annotation.add'
20
19
  import {createCoreConverters} from '../converters/converters.core'
21
20
  import {debugWithName} from '../internal-utils/debug'
22
21
  import {compileType} from '../internal-utils/schema'
22
+ import type {AddedAnnotationPaths} from '../operations/behavior.operation.annotation.add'
23
23
  import type {
24
24
  EditableAPI,
25
25
  EditableAPIDeleteOptions,
@@ -140,7 +140,7 @@ type PatchEvent = {
140
140
  }
141
141
 
142
142
  type InternalPatchEvent = NamespaceEvent<PatchEvent, 'internal'> & {
143
- actionId?: string
143
+ operationId?: string
144
144
  value: Array<PortableTextBlock>
145
145
  }
146
146
 
@@ -344,8 +344,11 @@ export const editorMachine = setup({
344
344
  try {
345
345
  performEvent({
346
346
  mode: 'raise',
347
- behaviors: [...context.behaviors.values()],
348
- remainingEventBehaviors: [...context.behaviors.values()],
347
+ behaviors: [...context.behaviors.values(), ...coreBehaviors],
348
+ remainingEventBehaviors: [
349
+ ...context.behaviors.values(),
350
+ ...coreBehaviors,
351
+ ],
349
352
  event: event.behaviorEvent,
350
353
  editor: event.editor,
351
354
  keyGenerator: context.keyGenerator,
@@ -383,7 +386,7 @@ export const editorMachine = setup({
383
386
  }).createMachine({
384
387
  id: 'editor',
385
388
  context: ({input}) => ({
386
- behaviors: new Set([...coreBehaviors]),
389
+ behaviors: new Set([]),
387
390
  converters: new Set(input.converters ?? []),
388
391
  getLegacySchema: input.getLegacySchema,
389
392
  keyGenerator: input.keyGenerator,
@@ -23,7 +23,7 @@ export const mutationMachine = setup({
23
23
  types: {
24
24
  context: {} as {
25
25
  pendingMutations: Array<{
26
- actionId?: string
26
+ operationId?: string
27
27
  value: Array<PortableTextBlock> | undefined
28
28
  patches: Array<Patch>
29
29
  }>
@@ -34,7 +34,7 @@ export const mutationMachine = setup({
34
34
  | {
35
35
  type: 'patch'
36
36
  patch: Patch
37
- actionId?: string
37
+ operationId?: string
38
38
  value: Array<PortableTextBlock>
39
39
  }
40
40
  | {
@@ -78,7 +78,7 @@ export const mutationMachine = setup({
78
78
  if (context.pendingMutations.length === 0) {
79
79
  return [
80
80
  {
81
- actionId: event.actionId,
81
+ operationId: event.operationId,
82
82
  value: event.value,
83
83
  patches: [event.patch],
84
84
  },
@@ -87,17 +87,17 @@ export const mutationMachine = setup({
87
87
 
88
88
  const lastBulk = context.pendingMutations.at(-1)
89
89
 
90
- if (lastBulk && lastBulk.actionId === event.actionId) {
90
+ if (lastBulk && lastBulk.operationId === event.operationId) {
91
91
  return context.pendingMutations.slice(0, -1).concat({
92
92
  value: event.value,
93
- actionId: lastBulk.actionId,
93
+ operationId: lastBulk.operationId,
94
94
  patches: [...lastBulk.patches, event.patch],
95
95
  })
96
96
  }
97
97
 
98
98
  return context.pendingMutations.concat({
99
99
  value: event.value,
100
- actionId: event.actionId,
100
+ operationId: event.operationId,
101
101
  patches: [event.patch],
102
102
  })
103
103
  },
@@ -1,9 +1,9 @@
1
1
  import {Editor} from 'slate'
2
- import {insertTextActionImplementation} from '../../behavior-actions/behavior.action.insert.text'
3
- import {performAction} from '../../behavior-actions/behavior.actions'
4
2
  import {slateRangeToSelection} from '../../internal-utils/slate-utils'
3
+ import {insertTextOperationImplementation} from '../../operations/behavior.operation.insert.text'
4
+ import {performOperation} from '../../operations/behavior.operations'
5
5
  import type {EditorActor} from '../editor-machine'
6
- import {isApplyingBehaviorActions} from '../with-applying-behavior-actions'
6
+ import {isApplyingBehaviorOperations} from '../with-applying-behavior-operations'
7
7
 
8
8
  export function createWithEventListeners(editorActor: EditorActor) {
9
9
  return function withEventListeners(editor: Editor) {
@@ -11,12 +11,11 @@ export function createWithEventListeners(editorActor: EditorActor) {
11
11
  return editor
12
12
  }
13
13
 
14
- const {deleteBackward, deleteForward, insertBreak, insertText, select} =
15
- editor
14
+ const {insertText, select} = editor
16
15
 
17
16
  editor.deleteBackward = (unit) => {
18
- if (isApplyingBehaviorActions(editor)) {
19
- deleteBackward(unit)
17
+ if (isApplyingBehaviorOperations(editor)) {
18
+ console.error('Unexpected call to .deleteBackward(...)')
20
19
  return
21
20
  }
22
21
 
@@ -32,8 +31,8 @@ export function createWithEventListeners(editorActor: EditorActor) {
32
31
  }
33
32
 
34
33
  editor.deleteForward = (unit) => {
35
- if (isApplyingBehaviorActions(editor)) {
36
- deleteForward(unit)
34
+ if (isApplyingBehaviorOperations(editor)) {
35
+ console.error('Unexpected call to .deleteForward(...)')
37
36
  return
38
37
  }
39
38
 
@@ -49,8 +48,8 @@ export function createWithEventListeners(editorActor: EditorActor) {
49
48
  }
50
49
 
51
50
  editor.insertBreak = () => {
52
- if (isApplyingBehaviorActions(editor)) {
53
- insertBreak()
51
+ if (isApplyingBehaviorOperations(editor)) {
52
+ console.error('Unexpected call to .insertBreak(...)')
54
53
  return
55
54
  }
56
55
 
@@ -65,8 +64,9 @@ export function createWithEventListeners(editorActor: EditorActor) {
65
64
  }
66
65
 
67
66
  editor.insertData = (dataTransfer) => {
68
- if (isApplyingBehaviorActions(editor)) {
69
- throw new Error('Unexpected call to .insertData(...)')
67
+ if (isApplyingBehaviorOperations(editor)) {
68
+ console.error('Unexpected call to .insertData(...)')
69
+ return
70
70
  }
71
71
 
72
72
  editorActor.send({
@@ -82,13 +82,13 @@ export function createWithEventListeners(editorActor: EditorActor) {
82
82
  }
83
83
 
84
84
  editor.insertSoftBreak = () => {
85
- if (isApplyingBehaviorActions(editor)) {
86
- insertTextActionImplementation({
85
+ if (isApplyingBehaviorOperations(editor)) {
86
+ insertTextOperationImplementation({
87
87
  context: {
88
88
  keyGenerator: editorActor.getSnapshot().context.keyGenerator,
89
89
  schema: editorActor.getSnapshot().context.schema,
90
90
  },
91
- action: {type: 'insert.text', text: '\n', editor},
91
+ operation: {type: 'insert.text', text: '\n', editor},
92
92
  })
93
93
  return
94
94
  }
@@ -104,7 +104,7 @@ export function createWithEventListeners(editorActor: EditorActor) {
104
104
  }
105
105
 
106
106
  editor.insertText = (text, options) => {
107
- if (isApplyingBehaviorActions(editor)) {
107
+ if (isApplyingBehaviorOperations(editor)) {
108
108
  insertText(text, options)
109
109
  return
110
110
  }
@@ -121,13 +121,13 @@ export function createWithEventListeners(editorActor: EditorActor) {
121
121
  }
122
122
 
123
123
  editor.redo = () => {
124
- if (isApplyingBehaviorActions(editor)) {
125
- performAction({
124
+ if (isApplyingBehaviorOperations(editor)) {
125
+ performOperation({
126
126
  context: {
127
127
  keyGenerator: editorActor.getSnapshot().context.keyGenerator,
128
128
  schema: editorActor.getSnapshot().context.schema,
129
129
  },
130
- action: {
130
+ operation: {
131
131
  type: 'history.redo',
132
132
  editor,
133
133
  },
@@ -146,7 +146,7 @@ export function createWithEventListeners(editorActor: EditorActor) {
146
146
  }
147
147
 
148
148
  editor.select = (location) => {
149
- if (isApplyingBehaviorActions(editor)) {
149
+ if (isApplyingBehaviorOperations(editor)) {
150
150
  select(location)
151
151
  return
152
152
  }
@@ -169,18 +169,18 @@ export function createWithEventListeners(editorActor: EditorActor) {
169
169
  }
170
170
 
171
171
  editor.setFragmentData = () => {
172
- console.warn('Unexpected call to .setFragmentData(...)')
172
+ console.error('Unexpected call to .setFragmentData(...)')
173
173
  return
174
174
  }
175
175
 
176
176
  editor.undo = () => {
177
- if (isApplyingBehaviorActions(editor)) {
178
- performAction({
177
+ if (isApplyingBehaviorOperations(editor)) {
178
+ performOperation({
179
179
  context: {
180
180
  keyGenerator: editorActor.getSnapshot().context.keyGenerator,
181
181
  schema: editorActor.getSnapshot().context.schema,
182
182
  },
183
- action: {
183
+ operation: {
184
184
  type: 'history.undo',
185
185
  editor,
186
186
  },
@@ -16,7 +16,6 @@ import {
16
16
  } from 'slate'
17
17
  import type {DOMNode} from 'slate-dom'
18
18
  import {ReactEditor} from 'slate-react'
19
- import {addAnnotationActionImplementation} from '../../behavior-actions/behavior.action.annotation.add'
20
19
  import {debugWithName} from '../../internal-utils/debug'
21
20
  import {toSlateRange} from '../../internal-utils/ranges'
22
21
  import {
@@ -29,6 +28,7 @@ import {
29
28
  KEY_TO_VALUE_ELEMENT,
30
29
  SLATE_TO_PORTABLE_TEXT_RANGE,
31
30
  } from '../../internal-utils/weakMaps'
31
+ import {addAnnotationOperationImplementation} from '../../operations/behavior.operation.annotation.add'
32
32
  import type {
33
33
  EditableAPI,
34
34
  EditableAPIDeleteOptions,
@@ -400,12 +400,12 @@ export function createEditableAPI(
400
400
  let paths: ReturnType<EditableAPI['addAnnotation']> = undefined
401
401
 
402
402
  Editor.withoutNormalizing(editor, () => {
403
- paths = addAnnotationActionImplementation({
403
+ paths = addAnnotationOperationImplementation({
404
404
  context: {
405
405
  keyGenerator: editorActor.getSnapshot().context.keyGenerator,
406
406
  schema: types,
407
407
  },
408
- action: {
408
+ operation: {
409
409
  type: 'annotation.add',
410
410
  annotation: {name: type.name, value: value ?? {}},
411
411
  editor,
@@ -27,7 +27,7 @@ import {
27
27
  } from '../../internal-utils/withoutPatching'
28
28
  import type {PortableTextSlateEditor} from '../../types/editor'
29
29
  import type {EditorActor} from '../editor-machine'
30
- import {getCurrentActionId} from '../with-applying-behavior-actions'
30
+ import {getCurrentOperationId} from '../with-applying-behavior-operations'
31
31
  import {withoutSaving} from './createWithUndoRedo'
32
32
 
33
33
  const debug = debugWithName('plugin:withPatches')
@@ -108,15 +108,23 @@ export function createWithPatches({
108
108
  const patches = bufferedPatches
109
109
  bufferedPatches = []
110
110
  let changed = false
111
+
111
112
  withRemoteChanges(editor, () => {
112
113
  Editor.withoutNormalizing(editor, () => {
113
114
  withoutPatching(editor, () => {
114
115
  withoutSaving(editor, () => {
115
- patches.forEach((patch) => {
116
+ for (const patch of patches) {
116
117
  if (debug.enabled)
117
118
  debug(`Handling remote patch ${JSON.stringify(patch)}`)
118
- changed = applyPatch(editor, patch)
119
- })
119
+
120
+ try {
121
+ changed = applyPatch(editor, patch)
122
+ } catch (error) {
123
+ console.error(
124
+ `Applying patch ${JSON.stringify(patch)} failed due to: ${error.message}`,
125
+ )
126
+ }
127
+ }
120
128
  })
121
129
  })
122
130
  })
@@ -293,7 +301,7 @@ export function createWithPatches({
293
301
  editorActor.send({
294
302
  type: 'internal.patch',
295
303
  patch: {...patch, origin: 'local'},
296
- actionId: getCurrentActionId(editor),
304
+ operationId: getCurrentOperationId(editor),
297
305
  value: fromSlateValue(
298
306
  editor.children,
299
307
  editorActor.getSnapshot().context.schema.block.name,
@@ -8,11 +8,11 @@ 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 type {BehaviorActionImplementation} from '../../behavior-actions/behavior.actions'
12
11
  import {debugWithName} from '../../internal-utils/debug'
13
12
  import {getNextSpan, getPreviousSpan} from '../../internal-utils/sibling-utils'
14
13
  import {isChangingRemotely} from '../../internal-utils/withChanges'
15
14
  import {isRedoing, isUndoing} from '../../internal-utils/withUndoRedo'
15
+ import type {BehaviorOperationImplementation} from '../../operations/behavior.operations'
16
16
  import type {PortableTextSlateEditor} from '../../types/editor'
17
17
  import type {EditorActor} from '../editor-machine'
18
18
 
@@ -660,11 +660,11 @@ export function createWithPortableTextMarkModel(
660
660
  }
661
661
  }
662
662
 
663
- export const removeDecoratorActionImplementation: BehaviorActionImplementation<
663
+ export const removeDecoratorOperationImplementation: BehaviorOperationImplementation<
664
664
  'decorator.remove'
665
- > = ({action}) => {
666
- const editor = action.editor
667
- const mark = action.decorator
665
+ > = ({operation}) => {
666
+ const editor = operation.editor
667
+ const mark = operation.decorator
668
668
  const {selection} = editor
669
669
 
670
670
  if (selection) {
@@ -20,7 +20,6 @@ import {
20
20
  type Descendant,
21
21
  type SelectionOperation,
22
22
  } from 'slate'
23
- import type {BehaviorActionImplementation} from '../../behavior-actions/behavior.actions'
24
23
  import {debugWithName} from '../../internal-utils/debug'
25
24
  import {fromSlateValue} from '../../internal-utils/values'
26
25
  import {isChangingRemotely} from '../../internal-utils/withChanges'
@@ -32,9 +31,10 @@ import {
32
31
  withRedoing,
33
32
  withUndoing,
34
33
  } from '../../internal-utils/withUndoRedo'
34
+ import type {BehaviorOperationImplementation} from '../../operations/behavior.operations'
35
35
  import type {PortableTextSlateEditor} from '../../types/editor'
36
36
  import type {EditorActor} from '../editor-machine'
37
- import {getCurrentUndoStepId} from '../with-applying-behavior-actions'
37
+ import {getCurrentUndoStepId} from '../with-undo-step'
38
38
 
39
39
  const debug = debugWithName('plugin:withUndoRedo')
40
40
  const debugVerbose = debug.enabled && false
@@ -203,10 +203,10 @@ export function createWithUndoRedo(
203
203
  }
204
204
  }
205
205
 
206
- export const historyUndoActionImplementation: BehaviorActionImplementation<
206
+ export const historyUndoOperationImplementation: BehaviorOperationImplementation<
207
207
  'history.undo'
208
- > = ({action}) => {
209
- const editor = action.editor
208
+ > = ({operation}) => {
209
+ const editor = operation.editor
210
210
  const {undos} = editor.history
211
211
  const remotePatches = getRemotePatches(editor)
212
212
 
@@ -261,10 +261,10 @@ export const historyUndoActionImplementation: BehaviorActionImplementation<
261
261
  }
262
262
  }
263
263
 
264
- export const historyRedoActionImplementation: BehaviorActionImplementation<
264
+ export const historyRedoOperationImplementation: BehaviorOperationImplementation<
265
265
  'history.redo'
266
- > = ({action}) => {
267
- const editor = action.editor
266
+ > = ({operation}) => {
267
+ const editor = operation.editor
268
268
  const {redos} = editor.history
269
269
  const remotePatches = getRemotePatches(editor)
270
270
 
@@ -0,0 +1,18 @@
1
+ import {Editor} from 'slate'
2
+ import {defaultKeyGenerator} from './key-generator'
3
+
4
+ const CURRENT_OPERATION_ID: WeakMap<Editor, string | undefined> = new WeakMap()
5
+
6
+ export function withApplyingBehaviorOperations(editor: Editor, fn: () => void) {
7
+ CURRENT_OPERATION_ID.set(editor, defaultKeyGenerator())
8
+ Editor.withoutNormalizing(editor, fn)
9
+ CURRENT_OPERATION_ID.set(editor, undefined)
10
+ }
11
+
12
+ export function getCurrentOperationId(editor: Editor) {
13
+ return CURRENT_OPERATION_ID.get(editor)
14
+ }
15
+
16
+ export function isApplyingBehaviorOperations(editor: Editor) {
17
+ return getCurrentOperationId(editor) !== undefined
18
+ }
@@ -1,24 +1,6 @@
1
- import {Editor} from 'slate'
1
+ import type {Editor} from 'slate'
2
2
  import {defaultKeyGenerator} from './key-generator'
3
3
 
4
- const CURRENT_ACTION_ID: WeakMap<Editor, string | undefined> = new WeakMap()
5
-
6
- export function withApplyingBehaviorActions(editor: Editor, fn: () => void) {
7
- CURRENT_ACTION_ID.set(editor, defaultKeyGenerator())
8
- Editor.withoutNormalizing(editor, fn)
9
- CURRENT_ACTION_ID.set(editor, undefined)
10
- }
11
-
12
- export function getCurrentActionId(editor: Editor) {
13
- return CURRENT_ACTION_ID.get(editor)
14
- }
15
-
16
- export function isApplyingBehaviorActions(editor: Editor) {
17
- return getCurrentActionId(editor) !== undefined
18
- }
19
-
20
- ////////
21
-
22
4
  const CURRENT_UNDO_STEP: WeakMap<Editor, {undoStepId: string} | undefined> =
23
5
  new WeakMap()
24
6
 
package/src/index.ts CHANGED
@@ -4,7 +4,7 @@ export type {
4
4
  PortableTextChild,
5
5
  PortableTextSpan,
6
6
  } from '@sanity/types'
7
- export type {AddedAnnotationPaths} from './behavior-actions/behavior.action.annotation.add'
7
+ export type {AddedAnnotationPaths} from './operations/behavior.operation.annotation.add'
8
8
  export {EditorEventListener} from './editor-event-listener'
9
9
  export type {Editor, EditorConfig, EditorEvent} from './editor/create-editor'
10
10
  export {