@portabletext/editor 1.1.7 → 1.1.8

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.
@@ -47,12 +47,12 @@ export function createWithInsertBreak(
47
47
 
48
48
  if (editor.isTextBlock(focusBlock)) {
49
49
  const [start, end] = Range.edges(editor.selection)
50
- const isEndAtStartOfBlock = isEqual(end, {
50
+ const atTheStartOfBlock = isEqual(end, {
51
51
  path: [...focusBlockPath, 0],
52
52
  offset: 0,
53
53
  })
54
54
 
55
- if (isEndAtStartOfBlock && Range.isCollapsed(editor.selection)) {
55
+ if (atTheStartOfBlock && Range.isCollapsed(editor.selection)) {
56
56
  Editor.insertNode(
57
57
  editor,
58
58
  editor.pteCreateTextBlock({
@@ -67,49 +67,37 @@ export function createWithInsertBreak(
67
67
  focus: {path: [nextBlockPath, 0], offset: 0},
68
68
  })
69
69
 
70
- editor.onChange()
71
70
  return
72
71
  }
73
72
 
74
73
  const lastFocusBlockChild =
75
74
  focusBlock.children[focusBlock.children.length - 1]
76
- const isStartAtEndOfBlock = isEqual(start, {
75
+ const atTheEndOfBlock = isEqual(start, {
77
76
  path: [...focusBlockPath, focusBlock.children.length - 1],
78
77
  offset: editor.isTextSpan(lastFocusBlockChild)
79
78
  ? lastFocusBlockChild.text.length
80
79
  : 0,
81
80
  })
82
81
 
83
- if (
84
- isStartAtEndOfBlock &&
85
- Range.isCollapsed(editor.selection) &&
86
- focusDecorators.length > 0 &&
87
- focusAnnotations.length > 0
88
- ) {
89
- Editor.withoutNormalizing(editor, () => {
90
- if (!editor.selection) {
91
- return
92
- }
93
-
94
- Editor.insertNode(
95
- editor,
96
- editor.pteCreateTextBlock({
97
- decorators: [],
98
- }),
99
- )
82
+ if (atTheEndOfBlock && Range.isCollapsed(editor.selection)) {
83
+ Editor.insertNode(
84
+ editor,
85
+ editor.pteCreateTextBlock({
86
+ decorators: [],
87
+ }),
88
+ )
100
89
 
101
- const [nextBlockPath] = Path.next(focusBlockPath)
90
+ const [nextBlockPath] = Path.next(focusBlockPath)
102
91
 
103
- Transforms.setSelection(editor, {
104
- anchor: {path: [nextBlockPath, 0], offset: 0},
105
- focus: {path: [nextBlockPath, 0], offset: 0},
106
- })
92
+ Transforms.setSelection(editor, {
93
+ anchor: {path: [nextBlockPath, 0], offset: 0},
94
+ focus: {path: [nextBlockPath, 0], offset: 0},
107
95
  })
108
- editor.onChange()
96
+
109
97
  return
110
98
  }
111
99
 
112
- const isInTheMiddleOfNode = !isEndAtStartOfBlock && !isStartAtEndOfBlock
100
+ const isInTheMiddleOfNode = !atTheStartOfBlock && !atTheEndOfBlock
113
101
 
114
102
  if (isInTheMiddleOfNode) {
115
103
  Editor.withoutNormalizing(editor, () => {
@@ -47,8 +47,9 @@ export const withPlugins = <T extends Editor>(
47
47
  options: createEditorOptions,
48
48
  ): {editor: PortableTextSlateEditor; subscribe: () => () => void} => {
49
49
  const e = editor as T & PortableTextSlateEditor
50
- const {portableTextEditor, patches$, readOnly, maxBlocks} = options
51
- const {editorActor, schemaTypes} = portableTextEditor
50
+ const {editorActor, portableTextEditor, patches$, readOnly, maxBlocks} =
51
+ options
52
+ const {schemaTypes} = portableTextEditor
52
53
  e.subscriptions = []
53
54
  if (e.destroy) {
54
55
  e.destroy()
@@ -1,4 +1,5 @@
1
1
  import type {BaseSyntheticEvent} from 'react'
2
+ import type {EditorActor} from '../editor/editor-machine'
2
3
  import type {PortableTextEditor} from '../editor/PortableTextEditor'
3
4
  import type {PatchObservable} from './editor'
4
5
 
@@ -6,6 +7,7 @@ import type {PatchObservable} from './editor'
6
7
  * @internal
7
8
  */
8
9
  export type createEditorOptions = {
10
+ editorActor: EditorActor
9
11
  patches$?: PatchObservable
10
12
  portableTextEditor: PortableTextEditor
11
13
  readOnly: boolean
@@ -1,8 +1,15 @@
1
1
  import type {PortableTextTextBlock} from '@sanity/types'
2
2
  import {createEditor, type Descendant} from 'slate'
3
3
  import {beforeEach, describe, expect, it} from 'vitest'
4
- import {PortableTextEditor, type PortableTextEditorProps} from '../..'
4
+ import {createActor} from 'xstate'
5
+ import {
6
+ editorMachine,
7
+ PortableTextEditor,
8
+ type PortableTextEditorProps,
9
+ } from '../..'
5
10
  import {schemaType} from '../../editor/__tests__/PortableTextEditorTester'
11
+ import {coreBehaviors} from '../../editor/behavior/behavior.core'
12
+ import {defaultKeyGenerator} from '../../editor/key-generator'
6
13
  import {withPlugins} from '../../editor/plugins'
7
14
  import {getPortableTextMemberSchemaTypes} from '../getPortableTextMemberSchemaTypes'
8
15
  import {createOperationToPatches} from '../operationToPatches'
@@ -12,6 +19,13 @@ const portableTextFeatures = getPortableTextMemberSchemaTypes(schemaType)
12
19
  const operationToPatches = createOperationToPatches(portableTextFeatures)
13
20
 
14
21
  const {editor} = withPlugins(createEditor(), {
22
+ editorActor: createActor(editorMachine, {
23
+ input: {
24
+ behaviors: coreBehaviors,
25
+ schema: schemaType,
26
+ keyGenerator: defaultKeyGenerator,
27
+ },
28
+ }),
15
29
  portableTextEditor: new PortableTextEditor({
16
30
  schemaType,
17
31
  } as PortableTextEditorProps),
@@ -2,8 +2,11 @@ import type {Patch} from '@portabletext/patches'
2
2
  import {noop} from 'lodash'
3
3
  import {createEditor, type Descendant} from 'slate'
4
4
  import {beforeEach, describe, expect, it} from 'vitest'
5
- import {PortableTextEditor} from '../..'
5
+ import {createActor} from 'xstate'
6
+ import {editorMachine, PortableTextEditor} from '../..'
6
7
  import {schemaType} from '../../editor/__tests__/PortableTextEditorTester'
8
+ import {coreBehaviors} from '../../editor/behavior/behavior.core'
9
+ import {defaultKeyGenerator} from '../../editor/key-generator'
7
10
  import {withPlugins} from '../../editor/plugins'
8
11
  import {createApplyPatch} from '../applyPatch'
9
12
  import {getPortableTextMemberSchemaTypes} from '../getPortableTextMemberSchemaTypes'
@@ -15,6 +18,13 @@ const patchToOperations = createApplyPatch(schemaTypes)
15
18
  const portableTextEditor = new PortableTextEditor({schemaType, onChange: noop})
16
19
 
17
20
  const {editor} = withPlugins(createEditor(), {
21
+ editorActor: createActor(editorMachine, {
22
+ input: {
23
+ behaviors: coreBehaviors,
24
+ schema: schemaTypes,
25
+ keyGenerator: defaultKeyGenerator,
26
+ },
27
+ }),
18
28
  portableTextEditor,
19
29
  readOnly: false,
20
30
  })