@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.
- package/lib/_chunks-cjs/editor-provider.cjs +633 -628
- package/lib/_chunks-cjs/editor-provider.cjs.map +1 -1
- package/lib/_chunks-es/editor-provider.js +634 -629
- package/lib/_chunks-es/editor-provider.js.map +1 -1
- package/lib/behaviors/index.cjs.map +1 -1
- package/lib/behaviors/index.d.cts +4 -2564
- package/lib/behaviors/index.d.ts +4 -2564
- package/lib/behaviors/index.js.map +1 -1
- package/lib/index.d.cts +5 -2564
- package/lib/index.d.ts +5 -2564
- package/lib/plugins/index.cjs +1 -7
- package/lib/plugins/index.cjs.map +1 -1
- package/lib/plugins/index.d.cts +4 -2569
- package/lib/plugins/index.d.ts +4 -2569
- package/lib/plugins/index.js +2 -8
- package/lib/plugins/index.js.map +1 -1
- package/lib/selectors/index.d.cts +3 -2563
- package/lib/selectors/index.d.ts +3 -2563
- package/lib/utils/index.d.cts +5 -2564
- package/lib/utils/index.d.ts +5 -2564
- package/package.json +1 -1
- package/src/behaviors/behavior.abstract.keyboard.ts +16 -0
- package/src/behaviors/{behavior.default.ts → behavior.abstract.ts} +3 -3
- package/src/behaviors/behavior.core.ts +0 -3
- package/src/behaviors/behavior.perform-event.ts +27 -51
- package/src/behaviors/behavior.types.action.ts +1 -11
- package/src/editor/PortableTextEditor.tsx +1 -1
- package/src/editor/editor-machine.ts +7 -4
- package/src/editor/mutation-machine.ts +6 -6
- package/src/editor/plugins/create-with-event-listeners.ts +25 -25
- package/src/editor/plugins/createWithEditableAPI.ts +3 -3
- package/src/editor/plugins/createWithPatches.ts +13 -5
- package/src/editor/plugins/createWithPortableTextMarkModel.ts +5 -5
- package/src/editor/plugins/createWithUndoRedo.ts +8 -8
- package/src/editor/with-applying-behavior-operations.ts +18 -0
- package/src/editor/{with-applying-behavior-actions.ts → with-undo-step.ts} +1 -19
- package/src/index.ts +1 -1
- package/src/{behavior-actions/behavior.action.annotation.add.ts → operations/behavior.operation.annotation.add.ts} +7 -7
- package/src/{behavior-actions/behavior.action.annotation.remove.ts → operations/behavior.operation.annotation.remove.ts} +6 -6
- package/src/{behavior-actions/behavior.action.block.set.ts → operations/behavior.operation.block.set.ts} +14 -14
- package/src/{behavior-actions/behavior.action.block.unset.ts → operations/behavior.operation.block.unset.ts} +19 -17
- package/src/{behavior-actions/behavior.action.decorator.add.ts → operations/behavior.operation.decorator.add.ts} +10 -10
- package/src/operations/behavior.operation.delete.backward.ts +8 -0
- package/src/operations/behavior.operation.delete.block.ts +24 -0
- package/src/operations/behavior.operation.delete.forward.ts +8 -0
- package/src/{behavior-actions/behavior.action.delete.ts → operations/behavior.operation.delete.ts} +8 -8
- package/src/{behavior-actions/behavior.action.insert-inline-object.ts → operations/behavior.operation.insert-inline-object.ts} +11 -11
- package/src/{behavior-actions/behavior.action.insert-span.ts → operations/behavior.operation.insert-span.ts} +15 -15
- package/src/{behavior-actions/behavior.action.insert.block.ts → operations/behavior.operation.insert.block.ts} +8 -8
- package/src/operations/behavior.operation.insert.text.ts +17 -0
- package/src/operations/behavior.operation.move.backward.ts +12 -0
- package/src/operations/behavior.operation.move.block.ts +16 -0
- package/src/operations/behavior.operation.move.forward.ts +11 -0
- package/src/operations/behavior.operation.select.ts +15 -0
- package/src/operations/behavior.operations.ts +239 -0
- package/src/plugins/index.ts +0 -1
- package/src/behavior-actions/behavior.action.delete.backward.ts +0 -7
- package/src/behavior-actions/behavior.action.delete.block.ts +0 -24
- package/src/behavior-actions/behavior.action.delete.forward.ts +0 -7
- package/src/behavior-actions/behavior.action.insert.text.ts +0 -17
- package/src/behavior-actions/behavior.action.move.backward.ts +0 -12
- package/src/behavior-actions/behavior.action.move.block.ts +0 -16
- package/src/behavior-actions/behavior.action.move.forward.ts +0 -11
- package/src/behavior-actions/behavior.action.select.ts +0 -15
- package/src/behavior-actions/behavior.actions.ts +0 -219
- package/src/behaviors/behavior.default.raise-soft-break.ts +0 -14
- 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 {
|
|
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
|
|
24
|
+
export const addAnnotationOperationImplementation: BehaviorOperationImplementation<
|
|
25
25
|
'annotation.add',
|
|
26
26
|
AddedAnnotationPaths | undefined
|
|
27
|
-
> = ({context,
|
|
27
|
+
> = ({context, operation}) => {
|
|
28
28
|
const parsedAnnotation = parseAnnotation({
|
|
29
29
|
annotation: {
|
|
30
|
-
_type:
|
|
31
|
-
...
|
|
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(
|
|
39
|
+
`Failed to parse annotation ${JSON.stringify(operation.annotation)}`,
|
|
40
40
|
)
|
|
41
41
|
}
|
|
42
42
|
|
|
43
|
-
const 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 {
|
|
3
|
+
import type {BehaviorOperationImplementation} from './behavior.operations'
|
|
4
4
|
|
|
5
|
-
export const
|
|
5
|
+
export const removeAnnotationOperationImplementation: BehaviorOperationImplementation<
|
|
6
6
|
'annotation.remove'
|
|
7
|
-
> = ({
|
|
8
|
-
const 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 ===
|
|
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 !==
|
|
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 {
|
|
6
|
+
import type {BehaviorOperationImplementation} from './behavior.operations'
|
|
7
7
|
|
|
8
|
-
export const
|
|
8
|
+
export const blockSetOperationImplementation: BehaviorOperationImplementation<
|
|
9
9
|
'block.set'
|
|
10
|
-
> = ({context,
|
|
10
|
+
> = ({context, operation}) => {
|
|
11
11
|
const location = toSlateRange(
|
|
12
12
|
{
|
|
13
|
-
anchor: {path:
|
|
14
|
-
focus: {path:
|
|
13
|
+
anchor: {path: operation.at, offset: 0},
|
|
14
|
+
focus: {path: operation.at, offset: 0},
|
|
15
15
|
},
|
|
16
|
-
|
|
16
|
+
operation.editor,
|
|
17
17
|
)
|
|
18
18
|
|
|
19
19
|
if (!location) {
|
|
20
20
|
throw new Error(
|
|
21
|
-
`Unable to convert ${JSON.stringify(
|
|
21
|
+
`Unable to convert ${JSON.stringify(operation.at)} into a Slate Range`,
|
|
22
22
|
)
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
const blockEntry = Editor.node(
|
|
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(
|
|
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(
|
|
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(
|
|
39
|
+
throw new Error(`Unable to parse block at ${JSON.stringify(operation.at)}`)
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
const {_type, ...filteredProps} =
|
|
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(
|
|
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(
|
|
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 {
|
|
7
|
+
import type {BehaviorOperationImplementation} from './behavior.operations'
|
|
8
8
|
|
|
9
|
-
export const
|
|
9
|
+
export const blockUnsetOperationImplementation: BehaviorOperationImplementation<
|
|
10
10
|
'block.unset'
|
|
11
|
-
> = ({context,
|
|
11
|
+
> = ({context, operation}) => {
|
|
12
12
|
const location = toSlateRange(
|
|
13
13
|
{
|
|
14
|
-
anchor: {path:
|
|
15
|
-
focus: {path:
|
|
14
|
+
anchor: {path: operation.at, offset: 0},
|
|
15
|
+
focus: {path: operation.at, offset: 0},
|
|
16
16
|
},
|
|
17
|
-
|
|
17
|
+
operation.editor,
|
|
18
18
|
)
|
|
19
19
|
|
|
20
20
|
if (!location) {
|
|
21
21
|
throw new Error(
|
|
22
|
-
`Unable to convert ${JSON.stringify(
|
|
22
|
+
`Unable to convert ${JSON.stringify(operation.at)} into a Slate Range`,
|
|
23
23
|
)
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
const blockEntry = Editor.node(
|
|
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(
|
|
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(
|
|
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(
|
|
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 =
|
|
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(
|
|
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(
|
|
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
|
-
|
|
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(
|
|
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
|
-
|
|
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 {
|
|
8
|
+
import type {BehaviorOperationImplementation} from './behavior.operations'
|
|
9
9
|
|
|
10
|
-
export const
|
|
10
|
+
export const decoratorAddOperationImplementation: BehaviorOperationImplementation<
|
|
11
11
|
'decorator.add'
|
|
12
|
-
> = ({context,
|
|
13
|
-
const editor =
|
|
14
|
-
const mark =
|
|
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 =
|
|
21
|
+
const manualAnchor = operation.at?.anchor
|
|
22
22
|
? utils.blockOffsetToSpanSelectionPoint({
|
|
23
23
|
context: {
|
|
24
24
|
...context,
|
|
25
25
|
value,
|
|
26
26
|
},
|
|
27
|
-
blockOffset:
|
|
27
|
+
blockOffset: operation.at.anchor,
|
|
28
28
|
direction: 'backward',
|
|
29
29
|
})
|
|
30
30
|
: undefined
|
|
31
|
-
const manualFocus =
|
|
31
|
+
const manualFocus = operation.at?.focus
|
|
32
32
|
? utils.blockOffsetToSpanSelectionPoint({
|
|
33
33
|
context: {
|
|
34
34
|
...context,
|
|
35
35
|
value,
|
|
36
36
|
},
|
|
37
|
-
blockOffset:
|
|
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,
|
|
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
|
+
}
|
package/src/{behavior-actions/behavior.action.delete.ts → operations/behavior.operation.delete.ts}
RENAMED
|
@@ -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 {
|
|
4
|
+
import type {BehaviorOperationImplementation} from './behavior.operations'
|
|
5
5
|
|
|
6
|
-
export const
|
|
6
|
+
export const deleteOperationImplementation: BehaviorOperationImplementation<
|
|
7
7
|
'delete'
|
|
8
|
-
> = ({context,
|
|
9
|
-
const range = toSlateRange(
|
|
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(
|
|
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: {...
|
|
19
|
+
editor: {...operation.editor, selection: range},
|
|
20
20
|
})
|
|
21
21
|
const [focusChild] = getFocusChild({
|
|
22
|
-
editor: {...
|
|
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
|
-
|
|
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 {
|
|
4
|
+
import type {BehaviorOperationImplementation} from './behavior.operations'
|
|
5
5
|
|
|
6
|
-
export const
|
|
6
|
+
export const insertInlineObjectOperationImplementation: BehaviorOperationImplementation<
|
|
7
7
|
'insert.inline object'
|
|
8
|
-
> = ({context,
|
|
8
|
+
> = ({context, operation}) => {
|
|
9
9
|
const parsedInlineObject = parseInlineObject({
|
|
10
10
|
context,
|
|
11
11
|
inlineObject: {
|
|
12
|
-
_type:
|
|
13
|
-
...(
|
|
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(
|
|
20
|
+
`Failed to parse inline object ${JSON.stringify(operation.inlineObject)}`,
|
|
21
21
|
)
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
if (!
|
|
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(
|
|
31
|
-
at:
|
|
32
|
-
match: (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(
|
|
58
|
+
Transforms.insertNodes(operation.editor, child)
|
|
59
59
|
}
|
|
@@ -1,29 +1,29 @@
|
|
|
1
1
|
import {Editor, Transforms} from 'slate'
|
|
2
|
-
import type {
|
|
2
|
+
import type {BehaviorOperationImplementation} from './behavior.operations'
|
|
3
3
|
|
|
4
|
-
export const
|
|
4
|
+
export const insertSpanOperationImplementation: BehaviorOperationImplementation<
|
|
5
5
|
'insert.span'
|
|
6
|
-
> = ({context,
|
|
7
|
-
if (!
|
|
8
|
-
console.error('Unable to perform action without selection',
|
|
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(
|
|
14
|
-
at:
|
|
15
|
-
match: (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',
|
|
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 =
|
|
26
|
-
?
|
|
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(
|
|
34
|
+
Transforms.setNodes(operation.editor, {
|
|
35
35
|
markDefs: [...markDefs, ...annotations],
|
|
36
36
|
})
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
Transforms.insertNodes(
|
|
39
|
+
Transforms.insertNodes(operation.editor, {
|
|
40
40
|
_type: 'span',
|
|
41
41
|
_key: context.keyGenerator(),
|
|
42
|
-
text:
|
|
42
|
+
text: operation.text,
|
|
43
43
|
marks: [
|
|
44
44
|
...(annotations?.map((annotation) => annotation._key) ?? []),
|
|
45
|
-
...(
|
|
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 {
|
|
14
|
+
import type {BehaviorOperationImplementation} from './behavior.operations'
|
|
15
15
|
|
|
16
|
-
export const
|
|
16
|
+
export const insertBlockOperationImplementation: BehaviorOperationImplementation<
|
|
17
17
|
'insert.block'
|
|
18
|
-
> = ({context,
|
|
18
|
+
> = ({context, operation}) => {
|
|
19
19
|
const parsedBlock = parseBlock({
|
|
20
|
-
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(
|
|
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:
|
|
40
|
-
select:
|
|
41
|
-
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
|
+
}
|