@portabletext/editor 1.0.18 → 1.1.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.
Files changed (75) hide show
  1. package/lib/index.d.mts +140 -66
  2. package/lib/index.d.ts +140 -66
  3. package/lib/index.esm.js +1164 -410
  4. package/lib/index.esm.js.map +1 -1
  5. package/lib/index.js +1164 -410
  6. package/lib/index.js.map +1 -1
  7. package/lib/index.mjs +1164 -410
  8. package/lib/index.mjs.map +1 -1
  9. package/package.json +8 -4
  10. package/src/editor/Editable.tsx +107 -36
  11. package/src/editor/PortableTextEditor.tsx +47 -12
  12. package/src/editor/__tests__/PortableTextEditor.test.tsx +42 -15
  13. package/src/editor/__tests__/PortableTextEditorTester.tsx +50 -38
  14. package/src/editor/__tests__/RangeDecorations.test.tsx +0 -1
  15. package/src/editor/__tests__/handleClick.test.tsx +28 -9
  16. package/src/editor/__tests__/insert-block.test.tsx +22 -6
  17. package/src/editor/__tests__/pteWarningsSelfSolving.test.tsx +30 -62
  18. package/src/editor/__tests__/utils.ts +10 -3
  19. package/src/editor/components/DraggableBlock.tsx +36 -13
  20. package/src/editor/components/Element.tsx +59 -17
  21. package/src/editor/components/Leaf.tsx +106 -68
  22. package/src/editor/components/SlateContainer.tsx +12 -5
  23. package/src/editor/components/Synchronizer.tsx +5 -2
  24. package/src/editor/hooks/usePortableTextEditor.ts +2 -2
  25. package/src/editor/hooks/usePortableTextEditorSelection.tsx +9 -3
  26. package/src/editor/hooks/useSyncValue.test.tsx +9 -4
  27. package/src/editor/hooks/useSyncValue.ts +199 -130
  28. package/src/editor/nodes/DefaultAnnotation.tsx +6 -3
  29. package/src/editor/plugins/__tests__/createWithInsertData.test.tsx +25 -7
  30. package/src/editor/plugins/__tests__/withEditableAPIDelete.test.tsx +26 -9
  31. package/src/editor/plugins/__tests__/withEditableAPIGetFragment.test.tsx +15 -5
  32. package/src/editor/plugins/__tests__/withEditableAPIInsert.test.tsx +60 -19
  33. package/src/editor/plugins/__tests__/withEditableAPISelectionsOverlapping.test.tsx +4 -2
  34. package/src/editor/plugins/__tests__/withPortableTextLists.test.tsx +4 -2
  35. package/src/editor/plugins/__tests__/withPortableTextMarkModel.test.tsx +61 -550
  36. package/src/editor/plugins/__tests__/withPortableTextSelections.test.tsx +6 -3
  37. package/src/editor/plugins/__tests__/withUndoRedo.test.tsx +30 -13
  38. package/src/editor/plugins/createWithEditableAPI.ts +354 -115
  39. package/src/editor/plugins/createWithHotKeys.ts +41 -121
  40. package/src/editor/plugins/createWithInsertBreak.ts +166 -27
  41. package/src/editor/plugins/createWithInsertData.ts +60 -23
  42. package/src/editor/plugins/createWithMaxBlocks.ts +5 -2
  43. package/src/editor/plugins/createWithObjectKeys.ts +7 -3
  44. package/src/editor/plugins/createWithPatches.ts +60 -16
  45. package/src/editor/plugins/createWithPlaceholderBlock.ts +7 -3
  46. package/src/editor/plugins/createWithPortableTextBlockStyle.ts +17 -7
  47. package/src/editor/plugins/createWithPortableTextLists.ts +21 -8
  48. package/src/editor/plugins/createWithPortableTextMarkModel.ts +301 -155
  49. package/src/editor/plugins/createWithPortableTextSelections.ts +4 -2
  50. package/src/editor/plugins/createWithSchemaTypes.ts +25 -9
  51. package/src/editor/plugins/createWithUndoRedo.ts +107 -24
  52. package/src/editor/plugins/createWithUtils.ts +32 -10
  53. package/src/editor/plugins/index.ts +31 -10
  54. package/src/types/editor.ts +44 -15
  55. package/src/types/options.ts +4 -2
  56. package/src/types/slate.ts +2 -2
  57. package/src/utils/__tests__/dmpToOperations.test.ts +38 -13
  58. package/src/utils/__tests__/operationToPatches.test.ts +3 -2
  59. package/src/utils/__tests__/patchToOperations.test.ts +15 -4
  60. package/src/utils/__tests__/ranges.test.ts +8 -3
  61. package/src/utils/__tests__/valueNormalization.test.tsx +12 -4
  62. package/src/utils/__tests__/values.test.ts +0 -1
  63. package/src/utils/applyPatch.ts +71 -20
  64. package/src/utils/getPortableTextMemberSchemaTypes.ts +30 -15
  65. package/src/utils/operationToPatches.ts +126 -43
  66. package/src/utils/paths.ts +24 -7
  67. package/src/utils/ranges.ts +12 -5
  68. package/src/utils/selection.ts +19 -7
  69. package/src/utils/validateValue.ts +118 -45
  70. package/src/utils/values.ts +31 -10
  71. package/src/utils/weakMaps.ts +18 -8
  72. package/src/utils/withChanges.ts +4 -2
  73. package/src/editor/plugins/__tests__/withHotkeys.test.tsx +0 -212
  74. package/src/editor/plugins/__tests__/withInsertBreak.test.tsx +0 -220
  75. package/src/editor/plugins/__tests__/withPlaceholderBlock.test.tsx +0 -133
@@ -1,8 +1,10 @@
1
1
  import {describe, expect, it, jest} from '@jest/globals'
2
2
  import {render, waitFor} from '@testing-library/react'
3
3
  import {createRef, type RefObject} from 'react'
4
-
5
- import {PortableTextEditorTester, schemaType} from '../../__tests__/PortableTextEditorTester'
4
+ import {
5
+ PortableTextEditorTester,
6
+ schemaType,
7
+ } from '../../__tests__/PortableTextEditorTester'
6
8
  import {PortableTextEditor} from '../../PortableTextEditor'
7
9
 
8
10
  const initialValue = [
@@ -43,8 +45,14 @@ const emptyTextBlock = [
43
45
  },
44
46
  ]
45
47
  const emptyBlockSelection = {
46
- focus: {path: [{_key: 'emptyBlock'}, 'children', {_key: 'emptySpan'}], offset: 0},
47
- anchor: {path: [{_key: 'emptyBlock'}, 'children', {_key: 'emptySpan'}], offset: 0},
48
+ focus: {
49
+ path: [{_key: 'emptyBlock'}, 'children', {_key: 'emptySpan'}],
50
+ offset: 0,
51
+ },
52
+ anchor: {
53
+ path: [{_key: 'emptyBlock'}, 'children', {_key: 'emptySpan'}],
54
+ offset: 0,
55
+ },
48
56
  }
49
57
 
50
58
  describe('plugin:withEditableAPI: .insertChild()', () => {
@@ -63,7 +71,10 @@ describe('plugin:withEditableAPI: .insertChild()', () => {
63
71
 
64
72
  await waitFor(() => {
65
73
  if (editorRef.current) {
66
- expect(onChange).toHaveBeenCalledWith({type: 'value', value: initialValue})
74
+ expect(onChange).toHaveBeenCalledWith({
75
+ type: 'value',
76
+ value: initialValue,
77
+ })
67
78
  expect(onChange).toHaveBeenCalledWith({type: 'ready'})
68
79
  }
69
80
  })
@@ -80,7 +91,9 @@ describe('plugin:withEditableAPI: .insertChild()', () => {
80
91
  const inlineType = editorRef.current.schemaTypes.inlineObjects.find(
81
92
  (t) => t.name === 'someObject',
82
93
  )!
83
- PortableTextEditor.insertChild(editorRef.current, inlineType, {color: 'red'})
94
+ PortableTextEditor.insertChild(editorRef.current, inlineType, {
95
+ color: 'red',
96
+ })
84
97
  }
85
98
  })
86
99
 
@@ -118,9 +131,13 @@ describe('plugin:withEditableAPI: .insertChild()', () => {
118
131
 
119
132
  await waitFor(() => {
120
133
  if (editorRef.current) {
121
- PortableTextEditor.insertChild(editorRef.current, editorRef.current.schemaTypes.span, {
122
- text: ' ',
123
- })
134
+ PortableTextEditor.insertChild(
135
+ editorRef.current,
136
+ editorRef.current.schemaTypes.span,
137
+ {
138
+ text: ' ',
139
+ },
140
+ )
124
141
  }
125
142
  })
126
143
 
@@ -214,7 +231,9 @@ describe('plugin:withEditableAPI: .insertBlock()', () => {
214
231
  (t) => t.name === 'someObject',
215
232
  )!
216
233
 
217
- PortableTextEditor.insertBlock(editorRef.current, someObject, {color: 'red'})
234
+ PortableTextEditor.insertBlock(editorRef.current, someObject, {
235
+ color: 'red',
236
+ })
218
237
  }
219
238
  })
220
239
 
@@ -242,7 +261,10 @@ describe('plugin:withEditableAPI: .insertBlock()', () => {
242
261
 
243
262
  await waitFor(() => {
244
263
  if (editorRef.current) {
245
- expect(onChange).toHaveBeenCalledWith({type: 'value', value: initialValue})
264
+ expect(onChange).toHaveBeenCalledWith({
265
+ type: 'value',
266
+ value: initialValue,
267
+ })
246
268
  expect(onChange).toHaveBeenCalledWith({type: 'ready'})
247
269
  }
248
270
  })
@@ -260,7 +282,9 @@ describe('plugin:withEditableAPI: .insertBlock()', () => {
260
282
  (t) => t.name === 'someObject',
261
283
  )!
262
284
 
263
- PortableTextEditor.insertBlock(editorRef.current, someObject, {color: 'red'})
285
+ PortableTextEditor.insertBlock(editorRef.current, someObject, {
286
+ color: 'red',
287
+ })
264
288
  }
265
289
  })
266
290
 
@@ -289,7 +313,10 @@ describe('plugin:withEditableAPI: .insertBlock()', () => {
289
313
 
290
314
  await waitFor(() => {
291
315
  if (editorRef.current) {
292
- expect(onChange).toHaveBeenCalledWith({type: 'value', value: initialValue})
316
+ expect(onChange).toHaveBeenCalledWith({
317
+ type: 'value',
318
+ value: initialValue,
319
+ })
293
320
  expect(onChange).toHaveBeenCalledWith({type: 'ready'})
294
321
  }
295
322
  })
@@ -309,7 +336,9 @@ describe('plugin:withEditableAPI: .insertBlock()', () => {
309
336
  const someObject = editorRef.current.schemaTypes.inlineObjects.find(
310
337
  (t) => t.name === 'someObject',
311
338
  )!
312
- PortableTextEditor.insertBlock(editorRef.current, someObject, {color: 'red'})
339
+ PortableTextEditor.insertBlock(editorRef.current, someObject, {
340
+ color: 'red',
341
+ })
313
342
  }
314
343
  })
315
344
 
@@ -325,7 +354,10 @@ describe('plugin:withEditableAPI: .insertBlock()', () => {
325
354
 
326
355
  it('should not add empty blank blocks: non text block', async () => {
327
356
  const editorRef: RefObject<PortableTextEditor> = createRef()
328
- const value = [...initialValue, {_key: 'b', _type: 'someObject', color: 'red'}]
357
+ const value = [
358
+ ...initialValue,
359
+ {_key: 'b', _type: 'someObject', color: 'red'},
360
+ ]
329
361
  const onChange = jest.fn()
330
362
 
331
363
  render(
@@ -360,7 +392,9 @@ describe('plugin:withEditableAPI: .insertBlock()', () => {
360
392
  const someObject = editorRef.current.schemaTypes.inlineObjects.find(
361
393
  (t) => t.name === 'someObject',
362
394
  )!
363
- PortableTextEditor.insertBlock(editorRef.current, someObject, {color: 'yellow'})
395
+ PortableTextEditor.insertBlock(editorRef.current, someObject, {
396
+ color: 'yellow',
397
+ })
364
398
  }
365
399
  })
366
400
 
@@ -376,7 +410,10 @@ describe('plugin:withEditableAPI: .insertBlock()', () => {
376
410
 
377
411
  it('should not add empty blank blocks: in between blocks', async () => {
378
412
  const editorRef: RefObject<PortableTextEditor> = createRef()
379
- const value = [...initialValue, {_key: 'b', _type: 'someObject', color: 'red'}]
413
+ const value = [
414
+ ...initialValue,
415
+ {_key: 'b', _type: 'someObject', color: 'red'},
416
+ ]
380
417
  const onChange = jest.fn()
381
418
 
382
419
  render(
@@ -408,7 +445,9 @@ describe('plugin:withEditableAPI: .insertBlock()', () => {
408
445
  const someObject = editorRef.current.schemaTypes.inlineObjects.find(
409
446
  (t) => t.name === 'someObject',
410
447
  )!
411
- PortableTextEditor.insertBlock(editorRef.current, someObject, {color: 'yellow'})
448
+ PortableTextEditor.insertBlock(editorRef.current, someObject, {
449
+ color: 'yellow',
450
+ })
412
451
  }
413
452
  })
414
453
 
@@ -455,7 +494,9 @@ describe('plugin:withEditableAPI: .insertBlock()', () => {
455
494
  const someObject = editorRef.current.schemaTypes.inlineObjects.find(
456
495
  (t) => t.name === 'someObject',
457
496
  )!
458
- PortableTextEditor.insertBlock(editorRef.current, someObject, {color: 'yellow'})
497
+ PortableTextEditor.insertBlock(editorRef.current, someObject, {
498
+ color: 'yellow',
499
+ })
459
500
  }
460
501
  })
461
502
 
@@ -2,8 +2,10 @@ import {describe, expect, it, jest} from '@jest/globals'
2
2
  import {type PortableTextBlock} from '@sanity/types'
3
3
  import {render, waitFor} from '@testing-library/react'
4
4
  import {createRef, type RefObject} from 'react'
5
-
6
- import {PortableTextEditorTester, schemaType} from '../../__tests__/PortableTextEditorTester'
5
+ import {
6
+ PortableTextEditorTester,
7
+ schemaType,
8
+ } from '../../__tests__/PortableTextEditorTester'
7
9
  import {PortableTextEditor} from '../../PortableTextEditor'
8
10
 
9
11
  const INITIAL_VALUE: PortableTextBlock[] = [
@@ -1,8 +1,10 @@
1
1
  import {describe, expect, it, jest} from '@jest/globals'
2
2
  import {render, waitFor} from '@testing-library/react'
3
3
  import {createRef, type RefObject} from 'react'
4
-
5
- import {PortableTextEditorTester, schemaType} from '../../__tests__/PortableTextEditorTester'
4
+ import {
5
+ PortableTextEditorTester,
6
+ schemaType,
7
+ } from '../../__tests__/PortableTextEditorTester'
6
8
  import {PortableTextEditor} from '../../PortableTextEditor'
7
9
 
8
10
  describe('plugin:withPortableTextLists', () => {