@portabletext/editor 1.52.1 → 1.52.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/editor",
3
- "version": "1.52.1",
3
+ "version": "1.52.3",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -15,7 +15,7 @@ import {
15
15
  type MutableRefObject,
16
16
  type TextareaHTMLAttributes,
17
17
  } from 'react'
18
- import {Editor, Transforms, type Text} from 'slate'
18
+ import {Transforms, type Text} from 'slate'
19
19
  import {
20
20
  ReactEditor,
21
21
  Editable as SlateEditable,
@@ -52,7 +52,6 @@ import {RenderText, type RenderTextProps} from './components/render-text'
52
52
  import {EditorActorContext} from './editor-actor-context'
53
53
  import {usePortableTextEditor} from './hooks/usePortableTextEditor'
54
54
  import {createWithHotkeys} from './plugins/createWithHotKeys'
55
- import {PortableTextEditor} from './PortableTextEditor'
56
55
  import {
57
56
  createDecorate,
58
57
  rangeDecorationsMachine,
@@ -532,17 +531,19 @@ export const PortableTextEditable = forwardRef<
532
531
  if (onFocus) {
533
532
  onFocus(event)
534
533
  }
534
+
535
535
  if (!event.isDefaultPrevented()) {
536
- const selection = PortableTextEditor.getSelection(portableTextEditor)
537
- // Create an editor selection if it does'nt exist
538
- if (selection === null) {
539
- Transforms.select(slateEditor, Editor.start(slateEditor, []))
540
- slateEditor.onChange()
541
- }
542
536
  relayActor.send({type: 'focused', event})
543
- const newSelection = PortableTextEditor.getSelection(portableTextEditor)
544
- // If the selection is the same, emit it explicitly here as there is no actual onChange event triggered.
545
- if (selection === newSelection) {
537
+
538
+ const selection = slateEditor.selection
539
+ ? slateRangeToSelection({
540
+ schema: editorActor.getSnapshot().context.schema,
541
+ editor: slateEditor,
542
+ range: slateEditor.selection,
543
+ })
544
+ : null
545
+
546
+ if (selection) {
546
547
  editorActor.send({
547
548
  type: 'update selection',
548
549
  selection,
@@ -550,7 +551,7 @@ export const PortableTextEditable = forwardRef<
550
551
  }
551
552
  }
552
553
  },
553
- [editorActor, onFocus, slateEditor, portableTextEditor, relayActor],
554
+ [editorActor, onFocus, slateEditor, relayActor],
554
555
  )
555
556
 
556
557
  const handleClick = useCallback(
@@ -7,7 +7,6 @@ import {createWithMaxBlocks} from './createWithMaxBlocks'
7
7
  import {createWithObjectKeys} from './createWithObjectKeys'
8
8
  import {createWithPatches} from './createWithPatches'
9
9
  import {createWithPlaceholderBlock} from './createWithPlaceholderBlock'
10
- import {createWithPortableTextBlockStyle} from './createWithPortableTextBlockStyle'
11
10
  import {createWithPortableTextMarkModel} from './createWithPortableTextMarkModel'
12
11
  import {createWithPortableTextSelections} from './createWithPortableTextSelections'
13
12
  import {createWithSchemaTypes} from './createWithSchemaTypes'
@@ -49,8 +48,6 @@ export const withPlugins = <T extends Editor>(
49
48
  subscriptions: options.subscriptions,
50
49
  })
51
50
  const withPortableTextMarkModel = createWithPortableTextMarkModel(editorActor)
52
- const withPortableTextBlockStyle =
53
- createWithPortableTextBlockStyle(editorActor)
54
51
 
55
52
  const withPlaceholderBlock = createWithPlaceholderBlock(editorActor)
56
53
 
@@ -66,19 +63,17 @@ export const withPlugins = <T extends Editor>(
66
63
  withSchemaTypes(
67
64
  withObjectKeys(
68
65
  withPortableTextMarkModel(
69
- withPortableTextBlockStyle(
70
- withPlaceholderBlock(
71
- withUtils(
72
- withMaxBlocks(
73
- withUndoRedo(
74
- withPatches(
75
- withPortableTextSelections(
76
- pluginUpdateValue(
66
+ withPlaceholderBlock(
67
+ withUtils(
68
+ withMaxBlocks(
69
+ withUndoRedo(
70
+ withPatches(
71
+ withPortableTextSelections(
72
+ pluginUpdateValue(
73
+ editorActor.getSnapshot().context,
74
+ pluginUpdateMarkState(
77
75
  editorActor.getSnapshot().context,
78
- pluginUpdateMarkState(
79
- editorActor.getSnapshot().context,
80
- e,
81
- ),
76
+ e,
82
77
  ),
83
78
  ),
84
79
  ),
@@ -1,3 +1,4 @@
1
+ import type {PortableTextSpan} from '@sanity/types'
1
2
  import type {EditorContext} from '../editor/editor-snapshot'
2
3
 
3
4
  export function createPlaceholderBlock(
@@ -14,7 +15,7 @@ export function createPlaceholderBlock(
14
15
  _key: context.keyGenerator(),
15
16
  text: '',
16
17
  marks: [],
17
- },
18
+ } as PortableTextSpan,
18
19
  ],
19
20
  }
20
21
  }
@@ -1,4 +1,5 @@
1
1
  import {Transforms} from 'slate'
2
+ import {createPlaceholderBlock} from '../internal-utils/create-placeholder-block'
2
3
  import {toSlateRange} from '../internal-utils/ranges'
3
4
  import {getBlockPath} from '../internal-utils/slate-utils'
4
5
  import {getBlockKeyFromSelectionPoint} from '../selection/selection-point'
@@ -6,7 +7,7 @@ import type {BehaviorOperationImplementation} from './behavior.operations'
6
7
 
7
8
  export const deleteOperationImplementation: BehaviorOperationImplementation<
8
9
  'delete'
9
- > = ({operation}) => {
10
+ > = ({context, operation}) => {
10
11
  const anchorBlockKey = getBlockKeyFromSelectionPoint(operation.at.anchor)
11
12
  const focusBlockKey = getBlockKeyFromSelectionPoint(operation.at.focus)
12
13
 
@@ -36,6 +37,10 @@ export const deleteOperationImplementation: BehaviorOperationImplementation<
36
37
  at: [anchorBlockPath[0]],
37
38
  })
38
39
 
40
+ if (operation.editor.children.length === 0) {
41
+ Transforms.insertNodes(operation.editor, createPlaceholderBlock(context))
42
+ }
43
+
39
44
  return
40
45
  }
41
46
 
@@ -1,51 +0,0 @@
1
- import {Editor, Path, Text as SlateText, Transforms} from 'slate'
2
- import {debugWithName} from '../../internal-utils/debug'
3
- import type {PortableTextSlateEditor} from '../../types/editor'
4
- import type {EditorActor} from '../editor-machine'
5
-
6
- const debug = debugWithName('plugin:withPortableTextBlockStyle')
7
-
8
- export function createWithPortableTextBlockStyle(
9
- editorActor: EditorActor,
10
- ): (editor: PortableTextSlateEditor) => PortableTextSlateEditor {
11
- const defaultStyle = editorActor.getSnapshot().context.schema.styles[0].name
12
- return function withPortableTextBlockStyle(
13
- editor: PortableTextSlateEditor,
14
- ): PortableTextSlateEditor {
15
- // Extend Slate's default normalization to reset split node to normal style
16
- // if there is no text at the right end of the split.
17
- const {normalizeNode} = editor
18
-
19
- editor.normalizeNode = (nodeEntry) => {
20
- const [, path] = nodeEntry
21
-
22
- for (const op of editor.operations) {
23
- if (
24
- op.type === 'split_node' &&
25
- op.path.length === 1 &&
26
- editor.isTextBlock(op.properties) &&
27
- op.properties.style !== defaultStyle &&
28
- op.path[0] === path[0] &&
29
- !Path.equals(path, op.path)
30
- ) {
31
- const [child] = Editor.node(editor, [op.path[0] + 1, 0])
32
- if (SlateText.isText(child) && child.text === '') {
33
- debug(`Normalizing split node to ${defaultStyle} style`, op)
34
- editorActor.send({type: 'normalizing'})
35
- Transforms.setNodes(
36
- editor,
37
- {style: defaultStyle},
38
- {at: [op.path[0] + 1], voids: false},
39
- )
40
- editorActor.send({type: 'done normalizing'})
41
- return
42
- }
43
- }
44
- }
45
-
46
- normalizeNode(nodeEntry)
47
- }
48
-
49
- return editor
50
- }
51
- }