@portabletext/editor 1.0.19 → 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 (72) hide show
  1. package/lib/index.d.mts +140 -66
  2. package/lib/index.d.ts +140 -66
  3. package/lib/index.esm.js +1125 -362
  4. package/lib/index.esm.js.map +1 -1
  5. package/lib/index.js +1125 -362
  6. package/lib/index.js.map +1 -1
  7. package/lib/index.mjs +1125 -362
  8. package/lib/index.mjs.map +1 -1
  9. package/package.json +2 -2
  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 -17
  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 -124
  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 +213 -46
  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 -9
  71. package/src/utils/weakMaps.ts +18 -8
  72. package/src/utils/withChanges.ts +4 -2
@@ -1,7 +1,6 @@
1
1
  import {describe, expect, it, jest} from '@jest/globals'
2
2
  import {fireEvent, render, waitFor} from '@testing-library/react'
3
3
  import {createRef, type RefObject} from 'react'
4
-
5
4
  import {PortableTextEditor} from '../PortableTextEditor'
6
5
  import {PortableTextEditorTester, schemaType} from './PortableTextEditorTester'
7
6
  import {getEditableElement} from './utils'
@@ -47,7 +46,10 @@ describe('adds empty text block if its needed', () => {
47
46
 
48
47
  await waitFor(() => {
49
48
  if (editorRef.current) {
50
- expect(onChange).toHaveBeenCalledWith({type: 'value', value: initialValue})
49
+ expect(onChange).toHaveBeenCalledWith({
50
+ type: 'value',
51
+ value: initialValue,
52
+ })
51
53
  expect(onChange).toHaveBeenCalledWith({type: 'ready'})
52
54
  }
53
55
  })
@@ -59,7 +61,10 @@ describe('adds empty text block if its needed', () => {
59
61
  PortableTextEditor.focus(editorRef.current)
60
62
  PortableTextEditor.select(editorRef.current, initialSelection)
61
63
  fireEvent.click(element)
62
- expect(PortableTextEditor.getValue(editorRef.current)).toEqual([initialValue[0], newBlock])
64
+ expect(PortableTextEditor.getValue(editorRef.current)).toEqual([
65
+ initialValue[0],
66
+ newBlock,
67
+ ])
63
68
  }
64
69
  })
65
70
  })
@@ -107,7 +112,9 @@ describe('adds empty text block if its needed', () => {
107
112
  PortableTextEditor.focus(editorRef.current)
108
113
  PortableTextEditor.select(editorRef.current, initialSelection)
109
114
  fireEvent.click(element)
110
- expect(PortableTextEditor.getValue(editorRef.current)).toEqual(initialValue)
115
+ expect(PortableTextEditor.getValue(editorRef.current)).toEqual(
116
+ initialValue,
117
+ )
111
118
  }
112
119
  })
113
120
  })
@@ -156,7 +163,10 @@ describe('adds empty text block if its needed', () => {
156
163
 
157
164
  await waitFor(() => {
158
165
  if (editorRef.current) {
159
- expect(onChange).toHaveBeenCalledWith({type: 'value', value: initialValue})
166
+ expect(onChange).toHaveBeenCalledWith({
167
+ type: 'value',
168
+ value: initialValue,
169
+ })
160
170
  expect(onChange).toHaveBeenCalledWith({type: 'ready'})
161
171
  }
162
172
  })
@@ -168,7 +178,9 @@ describe('adds empty text block if its needed', () => {
168
178
  PortableTextEditor.focus(editorRef.current)
169
179
  PortableTextEditor.select(editorRef.current, initialSelection)
170
180
  fireEvent.click(element)
171
- expect(PortableTextEditor.getValue(editorRef.current)).toEqual(initialValue)
181
+ expect(PortableTextEditor.getValue(editorRef.current)).toEqual(
182
+ initialValue,
183
+ )
172
184
  }
173
185
  })
174
186
  })
@@ -217,7 +229,10 @@ describe('adds empty text block if its needed', () => {
217
229
 
218
230
  await waitFor(() => {
219
231
  if (editorRef.current) {
220
- expect(onChange).toHaveBeenCalledWith({type: 'value', value: initialValue})
232
+ expect(onChange).toHaveBeenCalledWith({
233
+ type: 'value',
234
+ value: initialValue,
235
+ })
221
236
  expect(onChange).toHaveBeenCalledWith({type: 'ready'})
222
237
  }
223
238
  })
@@ -225,13 +240,17 @@ describe('adds empty text block if its needed', () => {
225
240
  const element = await getEditableElement(component)
226
241
 
227
242
  const editor = editorRef.current
228
- const inlineType = editor?.schemaTypes.inlineObjects.find((t) => t.name === 'someObject')
243
+ const inlineType = editor?.schemaTypes.inlineObjects.find(
244
+ (t) => t.name === 'someObject',
245
+ )
229
246
  await waitFor(async () => {
230
247
  if (editor && inlineType && element) {
231
248
  PortableTextEditor.focus(editor)
232
249
  PortableTextEditor.select(editor, initialSelection)
233
250
  fireEvent.click(element)
234
- expect(PortableTextEditor.getValue(editor)).toEqual(initialValue.concat(newBlock))
251
+ expect(PortableTextEditor.getValue(editor)).toEqual(
252
+ initialValue.concat(newBlock),
253
+ )
235
254
  }
236
255
  })
237
256
  })
@@ -3,14 +3,17 @@ import {Schema} from '@sanity/schema'
3
3
  import {type PortableTextBlock} from '@sanity/types'
4
4
  import {render, waitFor} from '@testing-library/react'
5
5
  import {createRef, type RefObject} from 'react'
6
-
7
6
  import {type EditorChange, type EditorSelection} from '../../types/editor'
8
7
  import {PortableTextEditable} from '../Editable'
9
8
  import {PortableTextEditor} from '../PortableTextEditor'
10
9
 
11
10
  const schema = Schema.compile({
12
11
  types: [
13
- {name: 'portable-text', type: 'array', of: [{type: 'block'}, {type: 'image'}]},
12
+ {
13
+ name: 'portable-text',
14
+ type: 'array',
15
+ of: [{type: 'block'}, {type: 'image'}],
16
+ },
14
17
  {name: 'image', type: 'object'},
15
18
  ],
16
19
  }).get('portable-text')
@@ -49,7 +52,10 @@ describe(PortableTextEditor.insertBlock.name, () => {
49
52
  // Given an empty text block
50
53
  await waitFor(() => {
51
54
  if (editorRef.current) {
52
- expect(onChange).toHaveBeenCalledWith({type: 'value', value: initialValue})
55
+ expect(onChange).toHaveBeenCalledWith({
56
+ type: 'value',
57
+ value: initialValue,
58
+ })
53
59
  expect(onChange).toHaveBeenCalledWith({type: 'ready'})
54
60
  }
55
61
  })
@@ -94,6 +100,7 @@ describe(PortableTextEditor.insertBlock.name, () => {
94
100
  marks: [],
95
101
  },
96
102
  ],
103
+ markDefs: [],
97
104
  style: 'normal',
98
105
  }
99
106
  const initialValue: Array<PortableTextBlock> = [nonEmptyTextBlock]
@@ -114,7 +121,10 @@ describe(PortableTextEditor.insertBlock.name, () => {
114
121
  // Given an non-empty text block
115
122
  await waitFor(() => {
116
123
  if (editorRef.current) {
117
- expect(onChange).toHaveBeenCalledWith({type: 'value', value: initialValue})
124
+ expect(onChange).toHaveBeenCalledWith({
125
+ type: 'value',
126
+ value: initialValue,
127
+ })
118
128
  expect(onChange).toHaveBeenCalledWith({type: 'ready'})
119
129
  }
120
130
  })
@@ -184,7 +194,10 @@ describe(PortableTextEditor.insertBlock.name, () => {
184
194
  // Given an empty text block followed by an image
185
195
  await waitFor(() => {
186
196
  if (editorRef.current) {
187
- expect(onChange).toHaveBeenCalledWith({type: 'value', value: initialValue})
197
+ expect(onChange).toHaveBeenCalledWith({
198
+ type: 'value',
199
+ value: initialValue,
200
+ })
188
201
  expect(onChange).toHaveBeenCalledWith({type: 'ready'})
189
202
  }
190
203
  })
@@ -202,7 +215,10 @@ describe(PortableTextEditor.insertBlock.name, () => {
202
215
  })
203
216
  await waitFor(() => {
204
217
  if (editorRef.current) {
205
- expect(onChange).toHaveBeenCalledWith({type: 'selection', selection: initialSelection})
218
+ expect(onChange).toHaveBeenCalledWith({
219
+ type: 'selection',
220
+ selection: initialSelection,
221
+ })
206
222
  }
207
223
  })
208
224
 
@@ -2,7 +2,6 @@ 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
5
  import {PortableTextEditor} from '../PortableTextEditor'
7
6
  import {PortableTextEditorTester, schemaType} from './PortableTextEditorTester'
8
7
 
@@ -35,7 +34,10 @@ describe('when PTE would display warnings, instead it self solves', () => {
35
34
  />,
36
35
  )
37
36
  await waitFor(() => {
38
- expect(onChange).toHaveBeenCalledWith({type: 'value', value: initialValue})
37
+ expect(onChange).toHaveBeenCalledWith({
38
+ type: 'value',
39
+ value: initialValue,
40
+ })
39
41
  expect(onChange).toHaveBeenCalledWith({type: 'ready'})
40
42
  })
41
43
  await waitFor(() => {
@@ -60,7 +62,7 @@ describe('when PTE would display warnings, instead it self solves', () => {
60
62
  })
61
63
  })
62
64
 
63
- it('allows missing .markDefs', async () => {
65
+ it('self-solves missing .markDefs', async () => {
64
66
  const editorRef: RefObject<PortableTextEditor> = createRef()
65
67
  const initialValue = [
66
68
  {
@@ -88,7 +90,10 @@ describe('when PTE would display warnings, instead it self solves', () => {
88
90
  />,
89
91
  )
90
92
  await waitFor(() => {
91
- expect(onChange).toHaveBeenCalledWith({type: 'value', value: initialValue})
93
+ expect(onChange).toHaveBeenCalledWith({
94
+ type: 'value',
95
+ value: initialValue,
96
+ })
92
97
  expect(onChange).toHaveBeenCalledWith({type: 'ready'})
93
98
  })
94
99
  await waitFor(() => {
@@ -106,6 +111,7 @@ describe('when PTE would display warnings, instead it self solves', () => {
106
111
  marks: [],
107
112
  },
108
113
  ],
114
+ markDefs: [],
109
115
  style: 'normal',
110
116
  },
111
117
  ])
@@ -141,7 +147,10 @@ describe('when PTE would display warnings, instead it self solves', () => {
141
147
  />,
142
148
  )
143
149
  await waitFor(() => {
144
- expect(onChange).toHaveBeenCalledWith({type: 'value', value: initialValue})
150
+ expect(onChange).toHaveBeenCalledWith({
151
+ type: 'value',
152
+ value: initialValue,
153
+ })
145
154
  expect(onChange).toHaveBeenCalledWith({type: 'ready'})
146
155
  })
147
156
  await waitFor(() => {
@@ -210,7 +219,10 @@ describe('when PTE would display warnings, instead it self solves', () => {
210
219
  />,
211
220
  )
212
221
  await waitFor(() => {
213
- expect(onChange).toHaveBeenCalledWith({type: 'value', value: initialValue})
222
+ expect(onChange).toHaveBeenCalledWith({
223
+ type: 'value',
224
+ value: initialValue,
225
+ })
214
226
  expect(onChange).toHaveBeenCalledWith({type: 'ready'})
215
227
  })
216
228
  await waitFor(() => {
@@ -271,7 +283,10 @@ describe('when PTE would display warnings, instead it self solves', () => {
271
283
  />,
272
284
  )
273
285
  await waitFor(() => {
274
- expect(onChange).toHaveBeenCalledWith({type: 'value', value: initialValue})
286
+ expect(onChange).toHaveBeenCalledWith({
287
+ type: 'value',
288
+ value: initialValue,
289
+ })
275
290
  expect(onChange).toHaveBeenCalledWith({type: 'ready'})
276
291
  })
277
292
  await waitFor(() => {
@@ -297,59 +312,6 @@ describe('when PTE would display warnings, instead it self solves', () => {
297
312
  })
298
313
  })
299
314
 
300
- it('allows missing .markDefs', async () => {
301
- const editorRef: RefObject<PortableTextEditor> = createRef()
302
- const initialValue = [
303
- {
304
- _key: 'abc',
305
- _type: 'myTestBlockType',
306
- children: [
307
- {
308
- _key: 'def',
309
- _type: 'span',
310
- marks: [],
311
- text: 'No markDefs',
312
- },
313
- ],
314
- style: 'normal',
315
- },
316
- ]
317
-
318
- const onChange = jest.fn()
319
- render(
320
- <PortableTextEditorTester
321
- onChange={onChange}
322
- ref={editorRef}
323
- schemaType={schemaType}
324
- value={initialValue}
325
- />,
326
- )
327
- await waitFor(() => {
328
- expect(onChange).toHaveBeenCalledWith({type: 'value', value: initialValue})
329
- expect(onChange).toHaveBeenCalledWith({type: 'ready'})
330
- })
331
- await waitFor(() => {
332
- if (editorRef.current) {
333
- PortableTextEditor.focus(editorRef.current)
334
- expect(PortableTextEditor.getValue(editorRef.current)).toEqual([
335
- {
336
- _key: 'abc',
337
- _type: 'myTestBlockType',
338
- children: [
339
- {
340
- _key: 'def',
341
- _type: 'span',
342
- text: 'No markDefs',
343
- marks: [],
344
- },
345
- ],
346
- style: 'normal',
347
- },
348
- ])
349
- }
350
- })
351
- })
352
-
353
315
  it('allows empty array of blocks', async () => {
354
316
  const editorRef: RefObject<PortableTextEditor> = createRef()
355
317
  const initialValue = [] as PortableTextBlock[]
@@ -364,7 +326,10 @@ describe('when PTE would display warnings, instead it self solves', () => {
364
326
  />,
365
327
  )
366
328
  await waitFor(() => {
367
- expect(onChange).toHaveBeenCalledWith({type: 'value', value: initialValue})
329
+ expect(onChange).toHaveBeenCalledWith({
330
+ type: 'value',
331
+ value: initialValue,
332
+ })
368
333
  expect(onChange).toHaveBeenCalledWith({type: 'ready'})
369
334
  })
370
335
  await waitFor(() => {
@@ -382,7 +347,10 @@ describe('when PTE would display warnings, instead it self solves', () => {
382
347
  }
383
348
  })
384
349
  await waitFor(() => {
385
- expect(onChange).toHaveBeenCalledWith({type: 'value', value: initialValue})
350
+ expect(onChange).toHaveBeenCalledWith({
351
+ type: 'value',
352
+ value: initialValue,
353
+ })
386
354
  expect(onChange).toHaveBeenCalledWith({type: 'ready'})
387
355
  })
388
356
  })
@@ -3,7 +3,10 @@ import {fireEvent, type render} from '@testing-library/react'
3
3
  import {parseHotkey} from 'is-hotkey-esm'
4
4
  import {act} from 'react'
5
5
 
6
- export async function triggerKeyboardEvent(hotkey: string, element: Element): Promise<void> {
6
+ export async function triggerKeyboardEvent(
7
+ hotkey: string,
8
+ element: Element,
9
+ ): Promise<void> {
7
10
  const eventProps = parseHotkey(hotkey)
8
11
  const values = hotkey.split('+')
9
12
 
@@ -19,9 +22,13 @@ export async function triggerKeyboardEvent(hotkey: string, element: Element): Pr
19
22
  )
20
23
  }
21
24
 
22
- export async function getEditableElement(component: ReturnType<typeof render>): Promise<Element> {
25
+ export async function getEditableElement(
26
+ component: ReturnType<typeof render>,
27
+ ): Promise<Element> {
23
28
  await act(async () => component)
24
- const element = component.container.querySelector('[data-slate-editor="true"]')
29
+ const element = component.container.querySelector(
30
+ '[data-slate-editor="true"]',
31
+ )
25
32
  if (!element) {
26
33
  throw new Error('Could not find element')
27
34
  }
@@ -1,16 +1,15 @@
1
1
  import {
2
- type DragEvent,
3
- type MutableRefObject,
4
- type ReactNode,
5
2
  useCallback,
6
3
  useEffect,
7
4
  useMemo,
8
5
  useRef,
9
6
  useState,
7
+ type DragEvent,
8
+ type MutableRefObject,
9
+ type ReactNode,
10
10
  } from 'react'
11
- import {Editor, type Element as SlateElement, Path, Transforms} from 'slate'
11
+ import {Editor, Path, Transforms, type Element as SlateElement} from 'slate'
12
12
  import {ReactEditor, useSlateStatic} from 'slate-react'
13
-
14
13
  import {debugWithName} from '../../utils/debug'
15
14
  import {
16
15
  IS_DRAGGING,
@@ -36,17 +35,31 @@ export interface DraggableBlockProps {
36
35
  * Implements drag and drop functionality on editor block nodes
37
36
  * @internal
38
37
  */
39
- export const DraggableBlock = ({children, element, readOnly, blockRef}: DraggableBlockProps) => {
38
+ export const DraggableBlock = ({
39
+ children,
40
+ element,
41
+ readOnly,
42
+ blockRef,
43
+ }: DraggableBlockProps) => {
40
44
  const editor = useSlateStatic()
41
45
  const dragGhostRef: MutableRefObject<undefined | HTMLElement> = useRef()
42
46
  const [isDragOver, setIsDragOver] = useState(false)
43
- const isVoid = useMemo(() => Editor.isVoid(editor, element), [editor, element])
44
- const isInline = useMemo(() => Editor.isInline(editor, element), [editor, element])
47
+ const isVoid = useMemo(
48
+ () => Editor.isVoid(editor, element),
49
+ [editor, element],
50
+ )
51
+ const isInline = useMemo(
52
+ () => Editor.isInline(editor, element),
53
+ [editor, element],
54
+ )
45
55
 
46
56
  const [blockElement, setBlockElement] = useState<HTMLElement | null>(null)
47
57
 
48
58
  useEffect(
49
- () => setBlockElement(blockRef ? blockRef.current : ReactEditor.toDOMNode(editor, element)),
59
+ () =>
60
+ setBlockElement(
61
+ blockRef ? blockRef.current : ReactEditor.toDOMNode(editor, element),
62
+ ),
50
63
  [editor, element, blockRef],
51
64
  )
52
65
 
@@ -122,7 +135,11 @@ export const DraggableBlock = ({children, element, readOnly, blockRef}: Draggabl
122
135
  )}`,
123
136
  )
124
137
  }
125
- if (dragPosition === 'top' && isBefore && targetPath[0] !== editor.children.length - 1) {
138
+ if (
139
+ dragPosition === 'top' &&
140
+ isBefore &&
141
+ targetPath[0] !== editor.children.length - 1
142
+ ) {
126
143
  const originalPath = targetPath
127
144
  targetPath = Path.previous(targetPath)
128
145
  debug(
@@ -201,7 +218,9 @@ export const DraggableBlock = ({children, element, readOnly, blockRef}: Draggabl
201
218
  // drag ghost by adding a truthy data attribute 'data-pt-drag-ghost-element' to a HTML element.
202
219
  if (blockElement && blockElement instanceof HTMLElement) {
203
220
  let dragGhost = blockElement.cloneNode(true) as HTMLElement
204
- const customGhost = dragGhost.querySelector('[data-pt-drag-ghost-element]')
221
+ const customGhost = dragGhost.querySelector(
222
+ '[data-pt-drag-ghost-element]',
223
+ )
205
224
  if (customGhost) {
206
225
  dragGhost = customGhost as HTMLElement
207
226
  }
@@ -232,12 +251,16 @@ export const DraggableBlock = ({children, element, readOnly, blockRef}: Draggabl
232
251
  isDragOver && editor.children[0] === IS_DRAGGING_ELEMENT_TARGET.get(editor)
233
252
  const isDraggingOverLastBlock =
234
253
  isDragOver &&
235
- editor.children[editor.children.length - 1] === IS_DRAGGING_ELEMENT_TARGET.get(editor)
254
+ editor.children[editor.children.length - 1] ===
255
+ IS_DRAGGING_ELEMENT_TARGET.get(editor)
236
256
  const dragPosition = IS_DRAGGING_BLOCK_TARGET_POSITION.get(editor)
237
257
 
238
258
  const isDraggingOverTop =
239
259
  isDraggingOverFirstBlock ||
240
- (isDragOver && !isDraggingOverFirstBlock && !isDraggingOverLastBlock && dragPosition === 'top')
260
+ (isDragOver &&
261
+ !isDraggingOverFirstBlock &&
262
+ !isDraggingOverLastBlock &&
263
+ dragPosition === 'top')
241
264
  const isDraggingOverBottom =
242
265
  isDraggingOverLastBlock ||
243
266
  (isDragOver &&
@@ -6,10 +6,14 @@ import {
6
6
  type PortableTextObject,
7
7
  type PortableTextTextBlock,
8
8
  } from '@sanity/types'
9
- import {type FunctionComponent, type ReactElement, useMemo, useRef} from 'react'
10
- import {Editor, Element as SlateElement, Range} from 'slate'
11
- import {ReactEditor, type RenderElementProps, useSelected, useSlateStatic} from 'slate-react'
12
-
9
+ import {useMemo, useRef, type FunctionComponent, type ReactElement} from 'react'
10
+ import {Editor, Range, Element as SlateElement} from 'slate'
11
+ import {
12
+ ReactEditor,
13
+ useSelected,
14
+ useSlateStatic,
15
+ type RenderElementProps,
16
+ } from 'slate-react'
13
17
  import {
14
18
  type BlockRenderProps,
15
19
  type PortableTextMemberSchemaTypes,
@@ -22,7 +26,11 @@ import {debugWithName} from '../../utils/debug'
22
26
  import {fromSlateValue} from '../../utils/values'
23
27
  import {KEY_TO_VALUE_ELEMENT} from '../../utils/weakMaps'
24
28
  import ObjectNode from '../nodes/DefaultObject'
25
- import {DefaultBlockObject, DefaultListItem, DefaultListItemInner} from '../nodes/index'
29
+ import {
30
+ DefaultBlockObject,
31
+ DefaultListItem,
32
+ DefaultListItemInner,
33
+ } from '../nodes/index'
26
34
  import {DraggableBlock} from './DraggableBlock'
27
35
 
28
36
  const debug = debugWithName('components:Element')
@@ -67,10 +75,17 @@ export const Element: FunctionComponent<ElementProps> = ({
67
75
  const selected = useSelected()
68
76
  const blockRef = useRef<HTMLDivElement | null>(null)
69
77
  const inlineBlockObjectRef = useRef(null)
70
- const focused = (selected && editor.selection && Range.isCollapsed(editor.selection)) || false
78
+ const focused =
79
+ (selected && editor.selection && Range.isCollapsed(editor.selection)) ||
80
+ false
71
81
 
72
82
  const value = useMemo(
73
- () => fromSlateValue([element], schemaTypes.block.name, KEY_TO_VALUE_ELEMENT.get(editor))[0],
83
+ () =>
84
+ fromSlateValue(
85
+ [element],
86
+ schemaTypes.block.name,
87
+ KEY_TO_VALUE_ELEMENT.get(editor),
88
+ )[0],
74
89
  [editor, element, schemaTypes.block.name],
75
90
  )
76
91
 
@@ -92,12 +107,18 @@ export const Element: FunctionComponent<ElementProps> = ({
92
107
  if (editor.isInline(element)) {
93
108
  const path = ReactEditor.findPath(editor, element)
94
109
  const [block] = Editor.node(editor, path, {depth: 1})
95
- const schemaType = schemaTypes.inlineObjects.find((_type) => _type.name === element._type)
110
+ const schemaType = schemaTypes.inlineObjects.find(
111
+ (_type) => _type.name === element._type,
112
+ )
96
113
  if (!schemaType) {
97
114
  throw new Error('Could not find type for inline block element')
98
115
  }
99
116
  if (SlateElement.isElement(block)) {
100
- const elmPath: Path = [{_key: block._key}, 'children', {_key: element._key}]
117
+ const elmPath: Path = [
118
+ {_key: block._key},
119
+ 'children',
120
+ {_key: element._key},
121
+ ]
101
122
  if (debugRenders) {
102
123
  debug(`Render ${element._key} (inline object)`)
103
124
  }
@@ -144,7 +165,9 @@ export const Element: FunctionComponent<ElementProps> = ({
144
165
  }
145
166
  const style = ('style' in element && element.style) || 'normal'
146
167
  className = `pt-block pt-text-block pt-text-block-style-${style}`
147
- const blockStyleType = schemaTypes.styles.find((item) => item.value === style)
168
+ const blockStyleType = schemaTypes.styles.find(
169
+ (item) => item.value === style,
170
+ )
148
171
  if (renderStyle && blockStyleType) {
149
172
  renderedBlock = renderStyle({
150
173
  block: element as PortableTextTextBlock,
@@ -165,7 +188,9 @@ export const Element: FunctionComponent<ElementProps> = ({
165
188
  className += ` pt-list-item pt-list-item-${element.listItem} pt-list-item-level-${level || 1}`
166
189
  }
167
190
  if (editor.isListBlock(value) && isListItem && element.listItem) {
168
- const listType = schemaTypes.lists.find((item) => item.value === element.listItem)
191
+ const listType = schemaTypes.lists.find(
192
+ (item) => item.value === element.listItem,
193
+ )
169
194
  if (renderListItem && listType) {
170
195
  renderedBlock = renderListItem({
171
196
  block: value,
@@ -206,7 +231,9 @@ export const Element: FunctionComponent<ElementProps> = ({
206
231
  {
207
232
  enumerable: false,
208
233
  get() {
209
- console.warn("Property 'type' is deprecated, use 'schemaType' instead.")
234
+ console.warn(
235
+ "Property 'type' is deprecated, use 'schemaType' instead.",
236
+ )
210
237
  return schemaTypes.block
211
238
  },
212
239
  },
@@ -216,16 +243,29 @@ export const Element: FunctionComponent<ElementProps> = ({
216
243
  ? renderBlock(renderProps as BlockRenderProps)
217
244
  : children
218
245
  return (
219
- <div key={element._key} {...attributes} className={className} spellCheck={spellCheck}>
220
- <DraggableBlock element={element} readOnly={readOnly} blockRef={blockRef}>
246
+ <div
247
+ key={element._key}
248
+ {...attributes}
249
+ className={className}
250
+ spellCheck={spellCheck}
251
+ >
252
+ <DraggableBlock
253
+ element={element}
254
+ readOnly={readOnly}
255
+ blockRef={blockRef}
256
+ >
221
257
  <div ref={blockRef}>{propsOrDefaultRendered}</div>
222
258
  </DraggableBlock>
223
259
  </div>
224
260
  )
225
261
  }
226
- const schemaType = schemaTypes.blockObjects.find((_type) => _type.name === element._type)
262
+ const schemaType = schemaTypes.blockObjects.find(
263
+ (_type) => _type.name === element._type,
264
+ )
227
265
  if (!schemaType) {
228
- throw new Error(`Could not find schema type for block element of _type ${element._type}`)
266
+ throw new Error(
267
+ `Could not find schema type for block element of _type ${element._type}`,
268
+ )
229
269
  }
230
270
  if (debugRenders) {
231
271
  debug(`Render ${element._key} (object block)`)
@@ -252,7 +292,9 @@ export const Element: FunctionComponent<ElementProps> = ({
252
292
  {
253
293
  enumerable: false,
254
294
  get() {
255
- console.warn("Property 'type' is deprecated, use 'schemaType' instead.")
295
+ console.warn(
296
+ "Property 'type' is deprecated, use 'schemaType' instead.",
297
+ )
256
298
  return schemaType
257
299
  },
258
300
  },