@portabletext/editor 1.48.13 → 1.48.14

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 (67) hide show
  1. package/lib/_chunks-cjs/editor-provider.cjs +633 -628
  2. package/lib/_chunks-cjs/editor-provider.cjs.map +1 -1
  3. package/lib/_chunks-es/editor-provider.js +634 -629
  4. package/lib/_chunks-es/editor-provider.js.map +1 -1
  5. package/lib/behaviors/index.cjs.map +1 -1
  6. package/lib/behaviors/index.d.cts +4 -2564
  7. package/lib/behaviors/index.d.ts +4 -2564
  8. package/lib/behaviors/index.js.map +1 -1
  9. package/lib/index.d.cts +5 -2564
  10. package/lib/index.d.ts +5 -2564
  11. package/lib/plugins/index.cjs +1 -7
  12. package/lib/plugins/index.cjs.map +1 -1
  13. package/lib/plugins/index.d.cts +4 -2569
  14. package/lib/plugins/index.d.ts +4 -2569
  15. package/lib/plugins/index.js +2 -8
  16. package/lib/plugins/index.js.map +1 -1
  17. package/lib/selectors/index.d.cts +3 -2563
  18. package/lib/selectors/index.d.ts +3 -2563
  19. package/lib/utils/index.d.cts +5 -2564
  20. package/lib/utils/index.d.ts +5 -2564
  21. package/package.json +1 -1
  22. package/src/behaviors/behavior.abstract.keyboard.ts +16 -0
  23. package/src/behaviors/{behavior.default.ts → behavior.abstract.ts} +3 -3
  24. package/src/behaviors/behavior.core.ts +0 -3
  25. package/src/behaviors/behavior.perform-event.ts +27 -51
  26. package/src/behaviors/behavior.types.action.ts +1 -11
  27. package/src/editor/PortableTextEditor.tsx +1 -1
  28. package/src/editor/editor-machine.ts +7 -4
  29. package/src/editor/mutation-machine.ts +6 -6
  30. package/src/editor/plugins/create-with-event-listeners.ts +25 -25
  31. package/src/editor/plugins/createWithEditableAPI.ts +3 -3
  32. package/src/editor/plugins/createWithPatches.ts +13 -5
  33. package/src/editor/plugins/createWithPortableTextMarkModel.ts +5 -5
  34. package/src/editor/plugins/createWithUndoRedo.ts +8 -8
  35. package/src/editor/with-applying-behavior-operations.ts +18 -0
  36. package/src/editor/{with-applying-behavior-actions.ts → with-undo-step.ts} +1 -19
  37. package/src/index.ts +1 -1
  38. package/src/{behavior-actions/behavior.action.annotation.add.ts → operations/behavior.operation.annotation.add.ts} +7 -7
  39. package/src/{behavior-actions/behavior.action.annotation.remove.ts → operations/behavior.operation.annotation.remove.ts} +6 -6
  40. package/src/{behavior-actions/behavior.action.block.set.ts → operations/behavior.operation.block.set.ts} +14 -14
  41. package/src/{behavior-actions/behavior.action.block.unset.ts → operations/behavior.operation.block.unset.ts} +19 -17
  42. package/src/{behavior-actions/behavior.action.decorator.add.ts → operations/behavior.operation.decorator.add.ts} +10 -10
  43. package/src/operations/behavior.operation.delete.backward.ts +8 -0
  44. package/src/operations/behavior.operation.delete.block.ts +24 -0
  45. package/src/operations/behavior.operation.delete.forward.ts +8 -0
  46. package/src/{behavior-actions/behavior.action.delete.ts → operations/behavior.operation.delete.ts} +8 -8
  47. package/src/{behavior-actions/behavior.action.insert-inline-object.ts → operations/behavior.operation.insert-inline-object.ts} +11 -11
  48. package/src/{behavior-actions/behavior.action.insert-span.ts → operations/behavior.operation.insert-span.ts} +15 -15
  49. package/src/{behavior-actions/behavior.action.insert.block.ts → operations/behavior.operation.insert.block.ts} +8 -8
  50. package/src/operations/behavior.operation.insert.text.ts +17 -0
  51. package/src/operations/behavior.operation.move.backward.ts +12 -0
  52. package/src/operations/behavior.operation.move.block.ts +16 -0
  53. package/src/operations/behavior.operation.move.forward.ts +11 -0
  54. package/src/operations/behavior.operation.select.ts +15 -0
  55. package/src/operations/behavior.operations.ts +239 -0
  56. package/src/plugins/index.ts +0 -1
  57. package/src/behavior-actions/behavior.action.delete.backward.ts +0 -7
  58. package/src/behavior-actions/behavior.action.delete.block.ts +0 -24
  59. package/src/behavior-actions/behavior.action.delete.forward.ts +0 -7
  60. package/src/behavior-actions/behavior.action.insert.text.ts +0 -17
  61. package/src/behavior-actions/behavior.action.move.backward.ts +0 -12
  62. package/src/behavior-actions/behavior.action.move.block.ts +0 -16
  63. package/src/behavior-actions/behavior.action.move.forward.ts +0 -11
  64. package/src/behavior-actions/behavior.action.select.ts +0 -15
  65. package/src/behavior-actions/behavior.actions.ts +0 -219
  66. package/src/behaviors/behavior.default.raise-soft-break.ts +0 -14
  67. package/src/plugins/plugin.core.tsx +0 -9
@@ -1,7 +1,7 @@
1
1
  import type {Path} from '@sanity/types'
2
2
  import {Editor, Node, Range, Text, Transforms} from 'slate'
3
3
  import {parseAnnotation} from '../internal-utils/parse-blocks'
4
- import type {BehaviorActionImplementation} from './behavior.actions'
4
+ import type {BehaviorOperationImplementation} from './behavior.operations'
5
5
 
6
6
  /**
7
7
  * @public
@@ -21,14 +21,14 @@ export type AddedAnnotationPaths = {
21
21
  spanPath: Path
22
22
  }
23
23
 
24
- export const addAnnotationActionImplementation: BehaviorActionImplementation<
24
+ export const addAnnotationOperationImplementation: BehaviorOperationImplementation<
25
25
  'annotation.add',
26
26
  AddedAnnotationPaths | undefined
27
- > = ({context, action}) => {
27
+ > = ({context, operation}) => {
28
28
  const parsedAnnotation = parseAnnotation({
29
29
  annotation: {
30
- _type: action.annotation.name,
31
- ...action.annotation.value,
30
+ _type: operation.annotation.name,
31
+ ...operation.annotation.value,
32
32
  },
33
33
  context,
34
34
  options: {refreshKeys: false},
@@ -36,11 +36,11 @@ export const addAnnotationActionImplementation: BehaviorActionImplementation<
36
36
 
37
37
  if (!parsedAnnotation) {
38
38
  throw new Error(
39
- `Failed to parse annotation ${JSON.stringify(action.annotation)}`,
39
+ `Failed to parse annotation ${JSON.stringify(operation.annotation)}`,
40
40
  )
41
41
  }
42
42
 
43
- const editor = action.editor
43
+ const editor = operation.editor
44
44
 
45
45
  if (!editor.selection || Range.isCollapsed(editor.selection)) {
46
46
  return
@@ -1,11 +1,11 @@
1
1
  import type {PortableTextSpan} from '@sanity/types'
2
2
  import {Editor, Node, Path, Range, Transforms} from 'slate'
3
- import type {BehaviorActionImplementation} from './behavior.actions'
3
+ import type {BehaviorOperationImplementation} from './behavior.operations'
4
4
 
5
- export const removeAnnotationActionImplementation: BehaviorActionImplementation<
5
+ export const removeAnnotationOperationImplementation: BehaviorOperationImplementation<
6
6
  'annotation.remove'
7
- > = ({action}) => {
8
- const editor = action.editor
7
+ > = ({operation}) => {
8
+ const editor = operation.editor
9
9
 
10
10
  if (!editor.selection) {
11
11
  return
@@ -22,7 +22,7 @@ export const removeAnnotationActionImplementation: BehaviorActionImplementation<
22
22
 
23
23
  const markDefs = block.markDefs ?? []
24
24
  const potentialAnnotations = markDefs.filter(
25
- (markDef) => markDef._type === action.annotation.name,
25
+ (markDef) => markDef._type === operation.annotation.name,
26
26
  )
27
27
 
28
28
  const [selectedChild, selectedChildPath] = Editor.node(
@@ -132,7 +132,7 @@ export const removeAnnotationActionImplementation: BehaviorActionImplementation<
132
132
  const marks = child.marks ?? []
133
133
  const marksWithoutAnnotation = marks.filter((mark) => {
134
134
  const markDef = markDefs.find((markDef) => markDef._key === mark)
135
- return markDef?._type !== action.annotation.name
135
+ return markDef?._type !== operation.annotation.name
136
136
  })
137
137
 
138
138
  if (marksWithoutAnnotation.length !== marks.length) {
@@ -3,43 +3,43 @@ import {parseBlock} from '../internal-utils/parse-blocks'
3
3
  import {toSlateRange} from '../internal-utils/ranges'
4
4
  import {fromSlateValue, toSlateValue} from '../internal-utils/values'
5
5
  import {KEY_TO_VALUE_ELEMENT} from '../internal-utils/weakMaps'
6
- import type {BehaviorActionImplementation} from './behavior.actions'
6
+ import type {BehaviorOperationImplementation} from './behavior.operations'
7
7
 
8
- export const blockSetBehaviorActionImplementation: BehaviorActionImplementation<
8
+ export const blockSetOperationImplementation: BehaviorOperationImplementation<
9
9
  'block.set'
10
- > = ({context, action}) => {
10
+ > = ({context, operation}) => {
11
11
  const location = toSlateRange(
12
12
  {
13
- anchor: {path: action.at, offset: 0},
14
- focus: {path: action.at, offset: 0},
13
+ anchor: {path: operation.at, offset: 0},
14
+ focus: {path: operation.at, offset: 0},
15
15
  },
16
- action.editor,
16
+ operation.editor,
17
17
  )
18
18
 
19
19
  if (!location) {
20
20
  throw new Error(
21
- `Unable to convert ${JSON.stringify(action.at)} into a Slate Range`,
21
+ `Unable to convert ${JSON.stringify(operation.at)} into a Slate Range`,
22
22
  )
23
23
  }
24
24
 
25
- const blockEntry = Editor.node(action.editor, location, {depth: 1})
25
+ const blockEntry = Editor.node(operation.editor, location, {depth: 1})
26
26
  const block = blockEntry?.[0]
27
27
 
28
28
  if (!block) {
29
- throw new Error(`Unable to find block at ${JSON.stringify(action.at)}`)
29
+ throw new Error(`Unable to find block at ${JSON.stringify(operation.at)}`)
30
30
  }
31
31
 
32
32
  const parsedBlock = fromSlateValue(
33
33
  [block],
34
34
  context.schema.block.name,
35
- KEY_TO_VALUE_ELEMENT.get(action.editor),
35
+ KEY_TO_VALUE_ELEMENT.get(operation.editor),
36
36
  ).at(0)
37
37
 
38
38
  if (!parsedBlock) {
39
- throw new Error(`Unable to parse block at ${JSON.stringify(action.at)}`)
39
+ throw new Error(`Unable to parse block at ${JSON.stringify(operation.at)}`)
40
40
  }
41
41
 
42
- const {_type, ...filteredProps} = action.props
42
+ const {_type, ...filteredProps} = operation.props
43
43
 
44
44
  const updatedBlock = parseBlock({
45
45
  context,
@@ -51,7 +51,7 @@ export const blockSetBehaviorActionImplementation: BehaviorActionImplementation<
51
51
  })
52
52
 
53
53
  if (!updatedBlock) {
54
- throw new Error(`Unable to update block at ${JSON.stringify(action.at)}`)
54
+ throw new Error(`Unable to update block at ${JSON.stringify(operation.at)}`)
55
55
  }
56
56
 
57
57
  const slateBlock = toSlateValue([updatedBlock], {
@@ -62,5 +62,5 @@ export const blockSetBehaviorActionImplementation: BehaviorActionImplementation<
62
62
  throw new Error(`Unable to convert block to Slate value`)
63
63
  }
64
64
 
65
- Transforms.setNodes(action.editor, slateBlock, {at: location})
65
+ Transforms.setNodes(operation.editor, slateBlock, {at: location})
66
66
  }
@@ -4,44 +4,44 @@ import {isTextBlock, parseBlock} from '../internal-utils/parse-blocks'
4
4
  import {toSlateRange} from '../internal-utils/ranges'
5
5
  import {fromSlateValue} from '../internal-utils/values'
6
6
  import {KEY_TO_VALUE_ELEMENT} from '../internal-utils/weakMaps'
7
- import type {BehaviorActionImplementation} from './behavior.actions'
7
+ import type {BehaviorOperationImplementation} from './behavior.operations'
8
8
 
9
- export const blockUnsetBehaviorActionImplementation: BehaviorActionImplementation<
9
+ export const blockUnsetOperationImplementation: BehaviorOperationImplementation<
10
10
  'block.unset'
11
- > = ({context, action}) => {
11
+ > = ({context, operation}) => {
12
12
  const location = toSlateRange(
13
13
  {
14
- anchor: {path: action.at, offset: 0},
15
- focus: {path: action.at, offset: 0},
14
+ anchor: {path: operation.at, offset: 0},
15
+ focus: {path: operation.at, offset: 0},
16
16
  },
17
- action.editor,
17
+ operation.editor,
18
18
  )
19
19
 
20
20
  if (!location) {
21
21
  throw new Error(
22
- `Unable to convert ${JSON.stringify(action.at)} into a Slate Range`,
22
+ `Unable to convert ${JSON.stringify(operation.at)} into a Slate Range`,
23
23
  )
24
24
  }
25
25
 
26
- const blockEntry = Editor.node(action.editor, location, {depth: 1})
26
+ const blockEntry = Editor.node(operation.editor, location, {depth: 1})
27
27
  const block = blockEntry?.[0]
28
28
 
29
29
  if (!block) {
30
- throw new Error(`Unable to find block at ${JSON.stringify(action.at)}`)
30
+ throw new Error(`Unable to find block at ${JSON.stringify(operation.at)}`)
31
31
  }
32
32
 
33
33
  const parsedBlock = fromSlateValue(
34
34
  [block],
35
35
  context.schema.block.name,
36
- KEY_TO_VALUE_ELEMENT.get(action.editor),
36
+ KEY_TO_VALUE_ELEMENT.get(operation.editor),
37
37
  ).at(0)
38
38
 
39
39
  if (!parsedBlock) {
40
- throw new Error(`Unable to parse block at ${JSON.stringify(action.at)}`)
40
+ throw new Error(`Unable to parse block at ${JSON.stringify(operation.at)}`)
41
41
  }
42
42
 
43
43
  if (isTextBlock(context, parsedBlock)) {
44
- const propsToRemove = action.props.filter((prop) => prop !== '_type')
44
+ const propsToRemove = operation.props.filter((prop) => prop !== '_type')
45
45
 
46
46
  const updatedTextBlock = parseBlock({
47
47
  context,
@@ -50,7 +50,9 @@ export const blockUnsetBehaviorActionImplementation: BehaviorActionImplementatio
50
50
  })
51
51
 
52
52
  if (!updatedTextBlock) {
53
- throw new Error(`Unable to update block at ${JSON.stringify(action.at)}`)
53
+ throw new Error(
54
+ `Unable to update block at ${JSON.stringify(operation.at)}`,
55
+ )
54
56
  }
55
57
 
56
58
  const propsToSet: Record<string, unknown> = {}
@@ -63,7 +65,7 @@ export const blockUnsetBehaviorActionImplementation: BehaviorActionImplementatio
63
65
  }
64
66
  }
65
67
 
66
- Transforms.setNodes(action.editor, propsToSet, {at: location})
68
+ Transforms.setNodes(operation.editor, propsToSet, {at: location})
67
69
 
68
70
  return
69
71
  }
@@ -72,19 +74,19 @@ export const blockUnsetBehaviorActionImplementation: BehaviorActionImplementatio
72
74
  context,
73
75
  block: omit(
74
76
  parsedBlock,
75
- action.props.filter((prop) => prop !== '_type'),
77
+ operation.props.filter((prop) => prop !== '_type'),
76
78
  ),
77
79
  options: {refreshKeys: false},
78
80
  })
79
81
 
80
82
  if (!updatedBlockObject) {
81
- throw new Error(`Unable to update block at ${JSON.stringify(action.at)}`)
83
+ throw new Error(`Unable to update block at ${JSON.stringify(operation.at)}`)
82
84
  }
83
85
 
84
86
  const {_type, _key, ...props} = updatedBlockObject
85
87
 
86
88
  Transforms.setNodes(
87
- action.editor,
89
+ operation.editor,
88
90
  {
89
91
  _type,
90
92
  _key,
@@ -5,36 +5,36 @@ import {fromSlateValue} from '../internal-utils/values'
5
5
  import {KEY_TO_VALUE_ELEMENT} from '../internal-utils/weakMaps'
6
6
  import * as selectors from '../selectors'
7
7
  import * as utils from '../utils'
8
- import type {BehaviorActionImplementation} from './behavior.actions'
8
+ import type {BehaviorOperationImplementation} from './behavior.operations'
9
9
 
10
- export const decoratorAddActionImplementation: BehaviorActionImplementation<
10
+ export const decoratorAddOperationImplementation: BehaviorOperationImplementation<
11
11
  'decorator.add'
12
- > = ({context, action}) => {
13
- const editor = action.editor
14
- const mark = action.decorator
12
+ > = ({context, operation}) => {
13
+ const editor = operation.editor
14
+ const mark = operation.decorator
15
15
  const value = fromSlateValue(
16
16
  editor.children,
17
17
  context.schema.block.name,
18
18
  KEY_TO_VALUE_ELEMENT.get(editor),
19
19
  )
20
20
 
21
- const manualAnchor = action.at?.anchor
21
+ const manualAnchor = operation.at?.anchor
22
22
  ? utils.blockOffsetToSpanSelectionPoint({
23
23
  context: {
24
24
  ...context,
25
25
  value,
26
26
  },
27
- blockOffset: action.at.anchor,
27
+ blockOffset: operation.at.anchor,
28
28
  direction: 'backward',
29
29
  })
30
30
  : undefined
31
- const manualFocus = action.at?.focus
31
+ const manualFocus = operation.at?.focus
32
32
  ? utils.blockOffsetToSpanSelectionPoint({
33
33
  context: {
34
34
  ...context,
35
35
  value,
36
36
  },
37
- blockOffset: action.at.focus,
37
+ blockOffset: operation.at.focus,
38
38
  direction: 'forward',
39
39
  })
40
40
  : undefined
@@ -47,7 +47,7 @@ export const decoratorAddActionImplementation: BehaviorActionImplementation<
47
47
  : undefined
48
48
 
49
49
  const selection = manualSelection
50
- ? (toSlateRange(manualSelection, action.editor) ?? editor.selection)
50
+ ? (toSlateRange(manualSelection, operation.editor) ?? editor.selection)
51
51
  : editor.selection
52
52
 
53
53
  if (!selection) {
@@ -0,0 +1,8 @@
1
+ import {deleteBackward} from 'slate'
2
+ import type {BehaviorOperationImplementation} from './behavior.operations'
3
+
4
+ export const deleteBackwardOperationImplementation: BehaviorOperationImplementation<
5
+ 'delete.backward'
6
+ > = ({operation}) => {
7
+ deleteBackward(operation.editor, operation.unit)
8
+ }
@@ -0,0 +1,24 @@
1
+ import {Transforms} from 'slate'
2
+ import {toSlateRange} from '../internal-utils/ranges'
3
+ import type {BehaviorOperationImplementation} from './behavior.operations'
4
+
5
+ export const deleteBlockOperationImplementation: BehaviorOperationImplementation<
6
+ 'delete.block'
7
+ > = ({operation}) => {
8
+ const range = toSlateRange(
9
+ {
10
+ anchor: {path: operation.at, offset: 0},
11
+ focus: {path: operation.at, offset: 0},
12
+ },
13
+ operation.editor,
14
+ )
15
+
16
+ if (!range) {
17
+ console.error('Unable to find Slate range from selection points')
18
+ return
19
+ }
20
+
21
+ Transforms.removeNodes(operation.editor, {
22
+ at: range,
23
+ })
24
+ }
@@ -0,0 +1,8 @@
1
+ import {deleteForward} from 'slate'
2
+ import type {BehaviorOperationImplementation} from './behavior.operations'
3
+
4
+ export const deleteForwardOperationImplementation: BehaviorOperationImplementation<
5
+ 'delete.forward'
6
+ > = ({operation}) => {
7
+ deleteForward(operation.editor, operation.unit)
8
+ }
@@ -1,25 +1,25 @@
1
1
  import {Range} from 'slate'
2
2
  import {toSlateRange} from '../internal-utils/ranges'
3
3
  import {getFocusBlock, getFocusChild} from '../internal-utils/slate-utils'
4
- import type {BehaviorActionImplementation} from './behavior.actions'
4
+ import type {BehaviorOperationImplementation} from './behavior.operations'
5
5
 
6
- export const deleteActionImplementation: BehaviorActionImplementation<
6
+ export const deleteOperationImplementation: BehaviorOperationImplementation<
7
7
  'delete'
8
- > = ({context, action}) => {
9
- const range = toSlateRange(action.at, action.editor)
8
+ > = ({context, operation}) => {
9
+ const range = toSlateRange(operation.at, operation.editor)
10
10
 
11
11
  if (!range) {
12
12
  throw new Error(
13
- `Failed to get Slate Range for selection ${JSON.stringify(action.at)}`,
13
+ `Failed to get Slate Range for selection ${JSON.stringify(operation.at)}`,
14
14
  )
15
15
  }
16
16
 
17
17
  if (Range.isCollapsed(range)) {
18
18
  const [focusBlock] = getFocusBlock({
19
- editor: {...action.editor, selection: range},
19
+ editor: {...operation.editor, selection: range},
20
20
  })
21
21
  const [focusChild] = getFocusChild({
22
- editor: {...action.editor, selection: range},
22
+ editor: {...operation.editor, selection: range},
23
23
  })
24
24
 
25
25
  if (
@@ -32,5 +32,5 @@ export const deleteActionImplementation: BehaviorActionImplementation<
32
32
  }
33
33
  }
34
34
 
35
- action.editor.delete({at: range})
35
+ operation.editor.delete({at: range})
36
36
  }
@@ -1,35 +1,35 @@
1
1
  import {Editor, Transforms, type Element} from 'slate'
2
2
  import {parseInlineObject} from '../internal-utils/parse-blocks'
3
3
  import {toSlateValue} from '../internal-utils/values'
4
- import type {BehaviorActionImplementation} from './behavior.actions'
4
+ import type {BehaviorOperationImplementation} from './behavior.operations'
5
5
 
6
- export const insertInlineObjectActionImplementation: BehaviorActionImplementation<
6
+ export const insertInlineObjectOperationImplementation: BehaviorOperationImplementation<
7
7
  'insert.inline object'
8
- > = ({context, action}) => {
8
+ > = ({context, operation}) => {
9
9
  const parsedInlineObject = parseInlineObject({
10
10
  context,
11
11
  inlineObject: {
12
- _type: action.inlineObject.name,
13
- ...(action.inlineObject.value ?? {}),
12
+ _type: operation.inlineObject.name,
13
+ ...(operation.inlineObject.value ?? {}),
14
14
  },
15
15
  options: {refreshKeys: false},
16
16
  })
17
17
 
18
18
  if (!parsedInlineObject) {
19
19
  throw new Error(
20
- `Failed to parse inline object ${JSON.stringify(action.inlineObject)}`,
20
+ `Failed to parse inline object ${JSON.stringify(operation.inlineObject)}`,
21
21
  )
22
22
  }
23
23
 
24
- if (!action.editor.selection) {
24
+ if (!operation.editor.selection) {
25
25
  console.error('Unable to insert inline object without selection')
26
26
  return
27
27
  }
28
28
 
29
29
  const [focusTextBlock] = Array.from(
30
- Editor.nodes(action.editor, {
31
- at: action.editor.selection.focus.path,
32
- match: (node) => action.editor.isTextBlock(node),
30
+ Editor.nodes(operation.editor, {
31
+ at: operation.editor.selection.focus.path,
32
+ match: (node) => operation.editor.isTextBlock(node),
33
33
  }),
34
34
  ).at(0) ?? [undefined, undefined]
35
35
 
@@ -55,5 +55,5 @@ export const insertInlineObjectActionImplementation: BehaviorActionImplementatio
55
55
  return
56
56
  }
57
57
 
58
- Transforms.insertNodes(action.editor, child)
58
+ Transforms.insertNodes(operation.editor, child)
59
59
  }
@@ -1,29 +1,29 @@
1
1
  import {Editor, Transforms} from 'slate'
2
- import type {BehaviorActionImplementation} from './behavior.actions'
2
+ import type {BehaviorOperationImplementation} from './behavior.operations'
3
3
 
4
- export const insertSpanActionImplementation: BehaviorActionImplementation<
4
+ export const insertSpanOperationImplementation: BehaviorOperationImplementation<
5
5
  'insert.span'
6
- > = ({context, action}) => {
7
- if (!action.editor.selection) {
8
- console.error('Unable to perform action without selection', action)
6
+ > = ({context, operation}) => {
7
+ if (!operation.editor.selection) {
8
+ console.error('Unable to perform action without selection', operation)
9
9
  return
10
10
  }
11
11
 
12
12
  const [focusBlock, focusBlockPath] = Array.from(
13
- Editor.nodes(action.editor, {
14
- at: action.editor.selection.focus.path,
15
- match: (node) => action.editor.isTextBlock(node),
13
+ Editor.nodes(operation.editor, {
14
+ at: operation.editor.selection.focus.path,
15
+ match: (node) => operation.editor.isTextBlock(node),
16
16
  }),
17
17
  )[0] ?? [undefined, undefined]
18
18
 
19
19
  if (!focusBlock || !focusBlockPath) {
20
- console.error('Unable to perform action without focus block', action)
20
+ console.error('Unable to perform action without focus block', operation)
21
21
  return
22
22
  }
23
23
 
24
24
  const markDefs = focusBlock.markDefs ?? []
25
- const annotations = action.annotations
26
- ? action.annotations.map((annotation) => ({
25
+ const annotations = operation.annotations
26
+ ? operation.annotations.map((annotation) => ({
27
27
  _type: annotation.name,
28
28
  _key: context.keyGenerator(),
29
29
  ...annotation.value,
@@ -31,18 +31,18 @@ export const insertSpanActionImplementation: BehaviorActionImplementation<
31
31
  : undefined
32
32
 
33
33
  if (annotations && annotations.length > 0) {
34
- Transforms.setNodes(action.editor, {
34
+ Transforms.setNodes(operation.editor, {
35
35
  markDefs: [...markDefs, ...annotations],
36
36
  })
37
37
  }
38
38
 
39
- Transforms.insertNodes(action.editor, {
39
+ Transforms.insertNodes(operation.editor, {
40
40
  _type: 'span',
41
41
  _key: context.keyGenerator(),
42
- text: action.text,
42
+ text: operation.text,
43
43
  marks: [
44
44
  ...(annotations?.map((annotation) => annotation._key) ?? []),
45
- ...(action.decorators ?? []),
45
+ ...(operation.decorators ?? []),
46
46
  ],
47
47
  })
48
48
  }
@@ -11,19 +11,19 @@ import {
11
11
  } from '../internal-utils/slate-utils'
12
12
  import {isEqualToEmptyEditor, toSlateValue} from '../internal-utils/values'
13
13
  import type {PortableTextSlateEditor} from '../types/editor'
14
- import type {BehaviorActionImplementation} from './behavior.actions'
14
+ import type {BehaviorOperationImplementation} from './behavior.operations'
15
15
 
16
- export const insertBlockActionImplementation: BehaviorActionImplementation<
16
+ export const insertBlockOperationImplementation: BehaviorOperationImplementation<
17
17
  'insert.block'
18
- > = ({context, action}) => {
18
+ > = ({context, operation}) => {
19
19
  const parsedBlock = parseBlock({
20
- block: action.block,
20
+ block: operation.block,
21
21
  context,
22
22
  options: {refreshKeys: false},
23
23
  })
24
24
 
25
25
  if (!parsedBlock) {
26
- throw new Error(`Failed to parse block ${JSON.stringify(action.block)}`)
26
+ throw new Error(`Failed to parse block ${JSON.stringify(operation.block)}`)
27
27
  }
28
28
 
29
29
  const fragment = toSlateValue([parsedBlock], {schemaTypes: context.schema})[0]
@@ -36,9 +36,9 @@ export const insertBlockActionImplementation: BehaviorActionImplementation<
36
36
 
37
37
  insertBlock({
38
38
  block: fragment,
39
- placement: action.placement,
40
- select: action.select ?? 'start',
41
- editor: action.editor,
39
+ placement: operation.placement,
40
+ select: operation.select ?? 'start',
41
+ editor: operation.editor,
42
42
  schema: context.schema,
43
43
  })
44
44
  }
@@ -0,0 +1,17 @@
1
+ import {Transforms} from 'slate'
2
+ import type {BehaviorOperationImplementation} from './behavior.operations'
3
+
4
+ export const insertTextOperationImplementation: BehaviorOperationImplementation<
5
+ 'insert.text'
6
+ > = ({operation}) => {
7
+ if (operation.editor.marks) {
8
+ Transforms.insertNodes(operation.editor, {
9
+ text: operation.text,
10
+ ...operation.editor.marks,
11
+ })
12
+ } else {
13
+ Transforms.insertText(operation.editor, operation.text)
14
+ }
15
+
16
+ operation.editor.marks = null
17
+ }
@@ -0,0 +1,12 @@
1
+ import {Transforms} from 'slate'
2
+ import type {BehaviorOperationImplementation} from './behavior.operations'
3
+
4
+ export const moveBackwardOperationImplementation: BehaviorOperationImplementation<
5
+ 'move.backward'
6
+ > = ({operation}) => {
7
+ Transforms.move(operation.editor, {
8
+ unit: 'character',
9
+ distance: operation.distance,
10
+ reverse: true,
11
+ })
12
+ }
@@ -0,0 +1,16 @@
1
+ import {Transforms} from 'slate'
2
+ import {toSlatePath} from '../internal-utils/paths'
3
+ import type {BehaviorOperationImplementation} from './behavior.operations'
4
+
5
+ export const moveBlockOperationImplementation: BehaviorOperationImplementation<
6
+ 'move.block'
7
+ > = ({operation}) => {
8
+ const at = [toSlatePath(operation.at, operation.editor)[0]]
9
+ const to = [toSlatePath(operation.to, operation.editor)[0]]
10
+
11
+ Transforms.moveNodes(operation.editor, {
12
+ at,
13
+ to,
14
+ mode: 'highest',
15
+ })
16
+ }
@@ -0,0 +1,11 @@
1
+ import {Transforms} from 'slate'
2
+ import type {BehaviorOperationImplementation} from './behavior.operations'
3
+
4
+ export const moveForwardOperationImplementation: BehaviorOperationImplementation<
5
+ 'move.forward'
6
+ > = ({operation}) => {
7
+ Transforms.move(operation.editor, {
8
+ unit: 'character',
9
+ distance: operation.distance,
10
+ })
11
+ }
@@ -0,0 +1,15 @@
1
+ import {Transforms} from 'slate'
2
+ import {toSlateRange} from '../internal-utils/ranges'
3
+ import type {BehaviorOperationImplementation} from './behavior.operations'
4
+
5
+ export const selectOperationImplementation: BehaviorOperationImplementation<
6
+ 'select'
7
+ > = ({operation}) => {
8
+ const newSelection = toSlateRange(operation.at, operation.editor)
9
+
10
+ if (newSelection) {
11
+ Transforms.select(operation.editor, newSelection)
12
+ } else {
13
+ Transforms.deselect(operation.editor)
14
+ }
15
+ }