@portabletext/editor 1.50.2 → 1.50.3

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.
@@ -617,6 +617,9 @@ declare type EditorEmittedEvent =
617
617
  event: FocusEvent_2<HTMLDivElement, Element>
618
618
  }
619
619
  | {
620
+ /**
621
+ * @deprecated
622
+ */
620
623
  type: 'done loading'
621
624
  }
622
625
  | {
@@ -633,6 +636,9 @@ declare type EditorEmittedEvent =
633
636
  value: Array<PortableTextBlock> | undefined
634
637
  }
635
638
  | {
639
+ /**
640
+ * @deprecated
641
+ */
636
642
  type: 'loading'
637
643
  }
638
644
  | MutationEvent
@@ -617,6 +617,9 @@ declare type EditorEmittedEvent =
617
617
  event: FocusEvent_2<HTMLDivElement, Element>
618
618
  }
619
619
  | {
620
+ /**
621
+ * @deprecated
622
+ */
620
623
  type: 'done loading'
621
624
  }
622
625
  | {
@@ -633,6 +636,9 @@ declare type EditorEmittedEvent =
633
636
  value: Array<PortableTextBlock> | undefined
634
637
  }
635
638
  | {
639
+ /**
640
+ * @deprecated
641
+ */
636
642
  type: 'loading'
637
643
  }
638
644
  | MutationEvent
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/editor",
3
- "version": "1.50.2",
3
+ "version": "1.50.3",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -68,8 +68,8 @@
68
68
  ],
69
69
  "dependencies": {
70
70
  "@portabletext/to-html": "^2.0.14",
71
- "@xstate/react": "^5.0.3",
72
- "debug": "^4.4.0",
71
+ "@xstate/react": "^5.0.4",
72
+ "debug": "^4.4.1",
73
73
  "get-random-values-esm": "^1.0.2",
74
74
  "lodash": "^4.17.21",
75
75
  "lodash.startcase": "^4.4.0",
@@ -78,23 +78,23 @@
78
78
  "slate-dom": "^0.114.0",
79
79
  "slate-react": "0.114.2",
80
80
  "use-effect-event": "^1.0.2",
81
- "xstate": "^5.19.2",
82
- "@portabletext/block-tools": "1.1.26",
83
- "@portabletext/patches": "1.1.3"
81
+ "xstate": "^5.19.3",
82
+ "@portabletext/patches": "1.1.3",
83
+ "@portabletext/block-tools": "1.1.27"
84
84
  },
85
85
  "devDependencies": {
86
86
  "@portabletext/toolkit": "^2.0.17",
87
87
  "@sanity/diff-match-patch": "^3.2.0",
88
88
  "@sanity/pkg-utils": "^7.2.2",
89
- "@sanity/schema": "^3.88.3",
90
- "@sanity/types": "^3.88.3",
89
+ "@sanity/schema": "^3.89.0",
90
+ "@sanity/types": "^3.89.0",
91
91
  "@testing-library/jest-dom": "^6.6.3",
92
92
  "@testing-library/react": "^16.3.0",
93
93
  "@types/debug": "^4.1.12",
94
94
  "@types/lodash": "^4.17.16",
95
95
  "@types/lodash.startcase": "^4.4.9",
96
- "@types/react": "^19.1.3",
97
- "@types/react-dom": "^19.1.3",
96
+ "@types/react": "^19.1.4",
97
+ "@types/react-dom": "^19.1.5",
98
98
  "@typescript-eslint/eslint-plugin": "^8.32.1",
99
99
  "@typescript-eslint/parser": "^8.32.1",
100
100
  "@vitejs/plugin-react": "^4.4.1",
@@ -110,12 +110,12 @@
110
110
  "typescript": "5.8.3",
111
111
  "vite": "^6.2.5",
112
112
  "vitest": "^3.1.3",
113
- "vitest-browser-react": "^0.1.1",
113
+ "vitest-browser-react": "^0.2.0",
114
114
  "racejar": "1.2.5"
115
115
  },
116
116
  "peerDependencies": {
117
- "@sanity/schema": "^3.88.3",
118
- "@sanity/types": "^3.88.3",
117
+ "@sanity/schema": "^3.89.0",
118
+ "@sanity/types": "^3.89.0",
119
119
  "react": "^16.9 || ^17 || ^18 || ^19",
120
120
  "rxjs": "^7.8.2"
121
121
  },
@@ -1,19 +1,17 @@
1
1
  import {insert, setIfMissing, unset, type Patch} from '@portabletext/patches'
2
- import {
3
- Editor,
4
- type Descendant,
5
- type InsertNodeOperation,
6
- type InsertTextOperation,
7
- type MergeNodeOperation,
8
- type MoveNodeOperation,
9
- type Operation,
10
- type RemoveNodeOperation,
11
- type RemoveTextOperation,
12
- type SetNodeOperation,
13
- type SplitNodeOperation,
14
- } from 'slate'
2
+ import {Editor, type Descendant, type Operation} from 'slate'
15
3
  import {createApplyPatch} from '../../internal-utils/applyPatch'
16
4
  import {debugWithName} from '../../internal-utils/debug'
5
+ import {
6
+ insertNodePatch,
7
+ insertTextPatch,
8
+ mergeNodePatch,
9
+ moveNodePatch,
10
+ removeNodePatch,
11
+ removeTextPatch,
12
+ setNodePatch,
13
+ splitNodePatch,
14
+ } from '../../internal-utils/operation-to-patches'
17
15
  import {fromSlateValue, isEqualToEmptyEditor} from '../../internal-utils/values'
18
16
  import {
19
17
  IS_PROCESSING_REMOTE_CHANGES,
@@ -34,60 +32,15 @@ import {withoutSaving} from './createWithUndoRedo'
34
32
  const debug = debugWithName('plugin:withPatches')
35
33
  const debugVerbose = false
36
34
 
37
- export interface PatchFunctions {
38
- insertNodePatch: (
39
- editor: PortableTextSlateEditor,
40
- operation: InsertNodeOperation,
41
- previousChildren: Descendant[],
42
- ) => Patch[]
43
- insertTextPatch: (
44
- editor: PortableTextSlateEditor,
45
- operation: InsertTextOperation,
46
- previousChildren: Descendant[],
47
- ) => Patch[]
48
- mergeNodePatch: (
49
- editor: PortableTextSlateEditor,
50
- operation: MergeNodeOperation,
51
- previousChildren: Descendant[],
52
- ) => Patch[]
53
- moveNodePatch: (
54
- editor: PortableTextSlateEditor,
55
- operation: MoveNodeOperation,
56
- previousChildren: Descendant[],
57
- ) => Patch[]
58
- removeNodePatch: (
59
- editor: PortableTextSlateEditor,
60
- operation: RemoveNodeOperation,
61
- previousChildren: Descendant[],
62
- ) => Patch[]
63
- removeTextPatch: (
64
- editor: PortableTextSlateEditor,
65
- operation: RemoveTextOperation,
66
- previousChildren: Descendant[],
67
- ) => Patch[]
68
- setNodePatch: (
69
- editor: PortableTextSlateEditor,
70
- operation: SetNodeOperation,
71
- previousChildren: Descendant[],
72
- ) => Patch[]
73
- splitNodePatch: (
74
- editor: PortableTextSlateEditor,
75
- operation: SplitNodeOperation,
76
- previousChildren: Descendant[],
77
- ) => Patch[]
78
- }
79
-
80
35
  interface Options {
81
36
  editorActor: EditorActor
82
37
  relayActor: RelayActor
83
- patchFunctions: PatchFunctions
84
38
  subscriptions: Array<() => () => void>
85
39
  }
86
40
 
87
41
  export function createWithPatches({
88
42
  editorActor,
89
43
  relayActor,
90
- patchFunctions,
91
44
  subscriptions,
92
45
  }: Options): (editor: PortableTextSlateEditor) => PortableTextSlateEditor {
93
46
  // The previous editor children are needed to figure out the _key of deleted nodes
@@ -197,8 +150,9 @@ export function createWithPatches({
197
150
  case 'insert_text':
198
151
  patches = [
199
152
  ...patches,
200
- ...patchFunctions.insertTextPatch(
201
- editor,
153
+ ...insertTextPatch(
154
+ editorActor.getSnapshot().context.schema,
155
+ editor.children,
202
156
  operation,
203
157
  previousChildren,
204
158
  ),
@@ -207,8 +161,9 @@ export function createWithPatches({
207
161
  case 'remove_text':
208
162
  patches = [
209
163
  ...patches,
210
- ...patchFunctions.removeTextPatch(
211
- editor,
164
+ ...removeTextPatch(
165
+ editorActor.getSnapshot().context.schema,
166
+ editor.children,
212
167
  operation,
213
168
  previousChildren,
214
169
  ),
@@ -217,18 +172,19 @@ export function createWithPatches({
217
172
  case 'remove_node':
218
173
  patches = [
219
174
  ...patches,
220
- ...patchFunctions.removeNodePatch(
221
- editor,
222
- operation,
175
+ ...removeNodePatch(
176
+ editorActor.getSnapshot().context.schema,
223
177
  previousChildren,
178
+ operation,
224
179
  ),
225
180
  ]
226
181
  break
227
182
  case 'split_node':
228
183
  patches = [
229
184
  ...patches,
230
- ...patchFunctions.splitNodePatch(
231
- editor,
185
+ ...splitNodePatch(
186
+ editorActor.getSnapshot().context.schema,
187
+ editor.children,
232
188
  operation,
233
189
  previousChildren,
234
190
  ),
@@ -237,8 +193,9 @@ export function createWithPatches({
237
193
  case 'insert_node':
238
194
  patches = [
239
195
  ...patches,
240
- ...patchFunctions.insertNodePatch(
241
- editor,
196
+ ...insertNodePatch(
197
+ editorActor.getSnapshot().context.schema,
198
+ editor.children,
242
199
  operation,
243
200
  previousChildren,
244
201
  ),
@@ -247,14 +204,19 @@ export function createWithPatches({
247
204
  case 'set_node':
248
205
  patches = [
249
206
  ...patches,
250
- ...patchFunctions.setNodePatch(editor, operation, previousChildren),
207
+ ...setNodePatch(
208
+ editorActor.getSnapshot().context.schema,
209
+ editor.children,
210
+ operation,
211
+ ),
251
212
  ]
252
213
  break
253
214
  case 'merge_node':
254
215
  patches = [
255
216
  ...patches,
256
- ...patchFunctions.mergeNodePatch(
257
- editor,
217
+ ...mergeNodePatch(
218
+ editorActor.getSnapshot().context.schema,
219
+ editor.children,
258
220
  operation,
259
221
  previousChildren,
260
222
  ),
@@ -263,10 +225,10 @@ export function createWithPatches({
263
225
  case 'move_node':
264
226
  patches = [
265
227
  ...patches,
266
- ...patchFunctions.moveNodePatch(
267
- editor,
268
- operation,
228
+ ...moveNodePatch(
229
+ editorActor.getSnapshot().context.schema,
269
230
  previousChildren,
231
+ operation,
270
232
  ),
271
233
  ]
272
234
  break
@@ -1,5 +1,4 @@
1
1
  import type {BaseOperation, Editor, Node, NodeEntry} from 'slate'
2
- import {createOperationToPatches} from '../../internal-utils/operationToPatches'
3
2
  import type {PortableTextSlateEditor} from '../../types/editor'
4
3
  import type {EditorActor} from '../editor-machine'
5
4
  import type {RelayActor} from '../relay-machine'
@@ -33,7 +32,6 @@ export const withPlugins = <T extends Editor>(
33
32
  ): PortableTextSlateEditor => {
34
33
  const e = editor as T & PortableTextSlateEditor
35
34
  const {editorActor, relayActor} = options
36
- const operationToPatches = createOperationToPatches(editorActor)
37
35
  const withObjectKeys = createWithObjectKeys(editorActor)
38
36
  const withSchemaTypes = createWithSchemaTypes({
39
37
  editorActor,
@@ -41,7 +39,6 @@ export const withPlugins = <T extends Editor>(
41
39
  const withPatches = createWithPatches({
42
40
  editorActor,
43
41
  relayActor,
44
- patchFunctions: operationToPatches,
45
42
  subscriptions: options.subscriptions,
46
43
  })
47
44
  const withMaxBlocks = createWithMaxBlocks(editorActor)
@@ -13,6 +13,9 @@ export type EditorEmittedEvent =
13
13
  event: FocusEvent<HTMLDivElement, Element>
14
14
  }
15
15
  | {
16
+ /**
17
+ * @deprecated
18
+ */
16
19
  type: 'done loading'
17
20
  }
18
21
  | {
@@ -29,6 +32,9 @@ export type EditorEmittedEvent =
29
32
  value: Array<PortableTextBlock> | undefined
30
33
  }
31
34
  | {
35
+ /**
36
+ * @deprecated
37
+ */
32
38
  type: 'loading'
33
39
  }
34
40
  | MutationEvent
@@ -79,6 +85,9 @@ export type PatchEvent = {
79
85
  }
80
86
 
81
87
  export type UnsetEvent = {
88
+ /**
89
+ * @deprecated Use `'patch'` events instead
90
+ */
82
91
  type: 'unset'
83
92
  previousValue: Array<PortableTextBlock>
84
93
  }
@@ -2,14 +2,21 @@ import type {PortableTextTextBlock} from '@sanity/types'
2
2
  import {createEditor, type Descendant} from 'slate'
3
3
  import {beforeEach, describe, expect, it} from 'vitest'
4
4
  import {createActor} from 'xstate'
5
- import {schemaType} from '../../editor/__tests__/PortableTextEditorTester'
6
- import {editorMachine} from '../../editor/editor-machine'
7
- import {legacySchemaToEditorSchema} from '../../editor/editor-schema'
8
- import {defaultKeyGenerator} from '../../editor/key-generator'
9
- import {createLegacySchema} from '../../editor/legacy-schema'
10
- import {withPlugins} from '../../editor/plugins/with-plugins'
11
- import {relayMachine} from '../../editor/relay-machine'
12
- import {createOperationToPatches} from '../operationToPatches'
5
+ import {schemaType} from '../editor/__tests__/PortableTextEditorTester'
6
+ import {editorMachine} from '../editor/editor-machine'
7
+ import {legacySchemaToEditorSchema} from '../editor/editor-schema'
8
+ import {defaultKeyGenerator} from '../editor/key-generator'
9
+ import {createLegacySchema} from '../editor/legacy-schema'
10
+ import {withPlugins} from '../editor/plugins/with-plugins'
11
+ import {relayMachine} from '../editor/relay-machine'
12
+ import {
13
+ insertNodePatch,
14
+ insertTextPatch,
15
+ mergeNodePatch,
16
+ removeNodePatch,
17
+ removeTextPatch,
18
+ splitNodePatch,
19
+ } from './operation-to-patches'
13
20
 
14
21
  const legacySchema = createLegacySchema(schemaType)
15
22
  const schemaTypes = legacySchemaToEditorSchema(legacySchema)
@@ -21,7 +28,6 @@ const editorActor = createActor(editorMachine, {
21
28
  },
22
29
  })
23
30
  const relayActor = createActor(relayMachine)
24
- const operationToPatches = createOperationToPatches(editorActor)
25
31
 
26
32
  const editor = withPlugins(createEditor(), {
27
33
  editorActor,
@@ -58,8 +64,9 @@ describe('operationToPatches', () => {
58
64
 
59
65
  it('translates void items correctly when splitting spans', () => {
60
66
  expect(
61
- operationToPatches.splitNodePatch(
62
- editor,
67
+ splitNodePatch(
68
+ schemaTypes,
69
+ editor.children,
63
70
  {
64
71
  type: 'split_node',
65
72
  path: [0, 0],
@@ -111,8 +118,9 @@ describe('operationToPatches', () => {
111
118
 
112
119
  it('produce correct insert block patch', () => {
113
120
  expect(
114
- operationToPatches.insertNodePatch(
115
- editor,
121
+ insertNodePatch(
122
+ schemaTypes,
123
+ editor.children,
116
124
  {
117
125
  type: 'insert_node',
118
126
  path: [0],
@@ -152,8 +160,9 @@ describe('operationToPatches', () => {
152
160
  editor.children = []
153
161
  editor.onChange()
154
162
  expect(
155
- operationToPatches.insertNodePatch(
156
- editor,
163
+ insertNodePatch(
164
+ schemaTypes,
165
+ editor.children,
157
166
  {
158
167
  type: 'insert_node',
159
168
  path: [0],
@@ -194,8 +203,9 @@ describe('operationToPatches', () => {
194
203
 
195
204
  it('produce correct insert child patch', () => {
196
205
  expect(
197
- operationToPatches.insertNodePatch(
198
- editor,
206
+ insertNodePatch(
207
+ schemaTypes,
208
+ editor.children,
199
209
  {
200
210
  type: 'insert_node',
201
211
  path: [0, 3],
@@ -240,15 +250,15 @@ describe('operationToPatches', () => {
240
250
  ;(editor.children[0] as PortableTextTextBlock).children[2].text = '1'
241
251
  editor.onChange()
242
252
  expect(
243
- operationToPatches.insertTextPatch(
244
- editor,
253
+ insertTextPatch(
254
+ editorActor.getSnapshot().context.schema,
255
+ editor.children,
245
256
  {
246
257
  type: 'insert_text',
247
258
  path: [0, 2],
248
259
  text: '1',
249
260
  offset: 0,
250
261
  },
251
-
252
262
  createDefaultValue(),
253
263
  ),
254
264
  ).toMatchInlineSnapshot(`
@@ -277,8 +287,9 @@ describe('operationToPatches', () => {
277
287
  const before = createDefaultValue()
278
288
  ;(before[0] as PortableTextTextBlock).children[2].text = '1'
279
289
  expect(
280
- operationToPatches.removeTextPatch(
281
- editor,
290
+ removeTextPatch(
291
+ editorActor.getSnapshot().context.schema,
292
+ editor.children,
282
293
  {
283
294
  type: 'remove_text',
284
295
  path: [0, 2],
@@ -312,8 +323,9 @@ describe('operationToPatches', () => {
312
323
 
313
324
  it('produces correct remove child patch', () => {
314
325
  expect(
315
- operationToPatches.removeNodePatch(
316
- editor,
326
+ removeNodePatch(
327
+ editorActor.getSnapshot().context.schema,
328
+ createDefaultValue(),
317
329
  {
318
330
  type: 'remove_node',
319
331
  path: [0, 1],
@@ -325,8 +337,6 @@ describe('operationToPatches', () => {
325
337
  children: [{_type: 'span', _key: 'bogus', text: '', marks: []}],
326
338
  },
327
339
  },
328
-
329
- createDefaultValue(),
330
340
  ),
331
341
  ).toMatchInlineSnapshot(`
332
342
  [
@@ -349,16 +359,11 @@ describe('operationToPatches', () => {
349
359
  it('produce correct remove block patch', () => {
350
360
  const val = createDefaultValue()
351
361
  expect(
352
- operationToPatches.removeNodePatch(
353
- editor,
354
- {
355
- type: 'remove_node',
356
- path: [0],
357
- node: val[0],
358
- },
359
-
360
- val,
361
- ),
362
+ removeNodePatch(editorActor.getSnapshot().context.schema, val, {
363
+ type: 'remove_node',
364
+ path: [0],
365
+ node: val[0],
366
+ }),
362
367
  ).toMatchInlineSnapshot(`
363
368
  [
364
369
  {
@@ -386,15 +391,15 @@ describe('operationToPatches', () => {
386
391
  block.children[2].text = '1234'
387
392
  editor.onChange()
388
393
  expect(
389
- operationToPatches.mergeNodePatch(
390
- editor,
394
+ mergeNodePatch(
395
+ schemaTypes,
396
+ editor.children,
391
397
  {
392
398
  type: 'merge_node',
393
399
  path: [0, 3],
394
400
  position: 2,
395
401
  properties: {text: '1234'},
396
402
  },
397
-
398
403
  val,
399
404
  ),
400
405
  ).toMatchInlineSnapshot(`