@portabletext/editor 1.48.13 → 1.48.15
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 +682 -609
- package/lib/_chunks-cjs/editor-provider.cjs.map +1 -1
- package/lib/_chunks-es/editor-provider.js +683 -610
- package/lib/_chunks-es/editor-provider.js.map +1 -1
- package/lib/behaviors/index.cjs.map +1 -1
- package/lib/behaviors/index.d.cts +226 -2772
- package/lib/behaviors/index.d.ts +226 -2772
- package/lib/behaviors/index.js.map +1 -1
- package/lib/index.cjs +22 -6
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +227 -2780
- package/lib/index.d.ts +227 -2780
- package/lib/index.js +23 -7
- package/lib/index.js.map +1 -1
- package/lib/plugins/index.cjs +1 -7
- package/lib/plugins/index.cjs.map +1 -1
- package/lib/plugins/index.d.cts +226 -2785
- package/lib/plugins/index.d.ts +226 -2785
- package/lib/plugins/index.js +2 -8
- package/lib/plugins/index.js.map +1 -1
- package/lib/selectors/index.d.cts +225 -2771
- package/lib/selectors/index.d.ts +225 -2771
- package/lib/utils/index.d.cts +227 -2772
- package/lib/utils/index.d.ts +227 -2772
- 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.config.ts +7 -0
- package/src/behaviors/behavior.core.ts +6 -5
- 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/components/Element.tsx +30 -4
- package/src/editor/create-editor.ts +17 -5
- package/src/editor/editor-machine.ts +23 -17
- 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/priority/priority.core.ts +3 -0
- package/src/priority/priority.sort.test.ts +319 -0
- package/src/priority/priority.sort.ts +121 -0
- package/src/priority/priority.types.ts +24 -0
- 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
|
@@ -27,7 +27,7 @@ import {
|
|
|
27
27
|
} from '../../internal-utils/withoutPatching'
|
|
28
28
|
import type {PortableTextSlateEditor} from '../../types/editor'
|
|
29
29
|
import type {EditorActor} from '../editor-machine'
|
|
30
|
-
import {
|
|
30
|
+
import {getCurrentOperationId} from '../with-applying-behavior-operations'
|
|
31
31
|
import {withoutSaving} from './createWithUndoRedo'
|
|
32
32
|
|
|
33
33
|
const debug = debugWithName('plugin:withPatches')
|
|
@@ -108,15 +108,23 @@ export function createWithPatches({
|
|
|
108
108
|
const patches = bufferedPatches
|
|
109
109
|
bufferedPatches = []
|
|
110
110
|
let changed = false
|
|
111
|
+
|
|
111
112
|
withRemoteChanges(editor, () => {
|
|
112
113
|
Editor.withoutNormalizing(editor, () => {
|
|
113
114
|
withoutPatching(editor, () => {
|
|
114
115
|
withoutSaving(editor, () => {
|
|
115
|
-
|
|
116
|
+
for (const patch of patches) {
|
|
116
117
|
if (debug.enabled)
|
|
117
118
|
debug(`Handling remote patch ${JSON.stringify(patch)}`)
|
|
118
|
-
|
|
119
|
-
|
|
119
|
+
|
|
120
|
+
try {
|
|
121
|
+
changed = applyPatch(editor, patch)
|
|
122
|
+
} catch (error) {
|
|
123
|
+
console.error(
|
|
124
|
+
`Applying patch ${JSON.stringify(patch)} failed due to: ${error.message}`,
|
|
125
|
+
)
|
|
126
|
+
}
|
|
127
|
+
}
|
|
120
128
|
})
|
|
121
129
|
})
|
|
122
130
|
})
|
|
@@ -293,7 +301,7 @@ export function createWithPatches({
|
|
|
293
301
|
editorActor.send({
|
|
294
302
|
type: 'internal.patch',
|
|
295
303
|
patch: {...patch, origin: 'local'},
|
|
296
|
-
|
|
304
|
+
operationId: getCurrentOperationId(editor),
|
|
297
305
|
value: fromSlateValue(
|
|
298
306
|
editor.children,
|
|
299
307
|
editorActor.getSnapshot().context.schema.block.name,
|
|
@@ -8,11 +8,11 @@ import {isPortableTextBlock, isPortableTextSpan} from '@portabletext/toolkit'
|
|
|
8
8
|
import type {PortableTextObject, PortableTextSpan} from '@sanity/types'
|
|
9
9
|
import {isEqual, uniq} from 'lodash'
|
|
10
10
|
import {Editor, Element, Node, Path, Range, Text, Transforms} from 'slate'
|
|
11
|
-
import type {BehaviorActionImplementation} from '../../behavior-actions/behavior.actions'
|
|
12
11
|
import {debugWithName} from '../../internal-utils/debug'
|
|
13
12
|
import {getNextSpan, getPreviousSpan} from '../../internal-utils/sibling-utils'
|
|
14
13
|
import {isChangingRemotely} from '../../internal-utils/withChanges'
|
|
15
14
|
import {isRedoing, isUndoing} from '../../internal-utils/withUndoRedo'
|
|
15
|
+
import type {BehaviorOperationImplementation} from '../../operations/behavior.operations'
|
|
16
16
|
import type {PortableTextSlateEditor} from '../../types/editor'
|
|
17
17
|
import type {EditorActor} from '../editor-machine'
|
|
18
18
|
|
|
@@ -660,11 +660,11 @@ export function createWithPortableTextMarkModel(
|
|
|
660
660
|
}
|
|
661
661
|
}
|
|
662
662
|
|
|
663
|
-
export const
|
|
663
|
+
export const removeDecoratorOperationImplementation: BehaviorOperationImplementation<
|
|
664
664
|
'decorator.remove'
|
|
665
|
-
> = ({
|
|
666
|
-
const editor =
|
|
667
|
-
const mark =
|
|
665
|
+
> = ({operation}) => {
|
|
666
|
+
const editor = operation.editor
|
|
667
|
+
const mark = operation.decorator
|
|
668
668
|
const {selection} = editor
|
|
669
669
|
|
|
670
670
|
if (selection) {
|
|
@@ -20,7 +20,6 @@ import {
|
|
|
20
20
|
type Descendant,
|
|
21
21
|
type SelectionOperation,
|
|
22
22
|
} from 'slate'
|
|
23
|
-
import type {BehaviorActionImplementation} from '../../behavior-actions/behavior.actions'
|
|
24
23
|
import {debugWithName} from '../../internal-utils/debug'
|
|
25
24
|
import {fromSlateValue} from '../../internal-utils/values'
|
|
26
25
|
import {isChangingRemotely} from '../../internal-utils/withChanges'
|
|
@@ -32,9 +31,10 @@ import {
|
|
|
32
31
|
withRedoing,
|
|
33
32
|
withUndoing,
|
|
34
33
|
} from '../../internal-utils/withUndoRedo'
|
|
34
|
+
import type {BehaviorOperationImplementation} from '../../operations/behavior.operations'
|
|
35
35
|
import type {PortableTextSlateEditor} from '../../types/editor'
|
|
36
36
|
import type {EditorActor} from '../editor-machine'
|
|
37
|
-
import {getCurrentUndoStepId} from '../with-
|
|
37
|
+
import {getCurrentUndoStepId} from '../with-undo-step'
|
|
38
38
|
|
|
39
39
|
const debug = debugWithName('plugin:withUndoRedo')
|
|
40
40
|
const debugVerbose = debug.enabled && false
|
|
@@ -203,10 +203,10 @@ export function createWithUndoRedo(
|
|
|
203
203
|
}
|
|
204
204
|
}
|
|
205
205
|
|
|
206
|
-
export const
|
|
206
|
+
export const historyUndoOperationImplementation: BehaviorOperationImplementation<
|
|
207
207
|
'history.undo'
|
|
208
|
-
> = ({
|
|
209
|
-
const editor =
|
|
208
|
+
> = ({operation}) => {
|
|
209
|
+
const editor = operation.editor
|
|
210
210
|
const {undos} = editor.history
|
|
211
211
|
const remotePatches = getRemotePatches(editor)
|
|
212
212
|
|
|
@@ -261,10 +261,10 @@ export const historyUndoActionImplementation: BehaviorActionImplementation<
|
|
|
261
261
|
}
|
|
262
262
|
}
|
|
263
263
|
|
|
264
|
-
export const
|
|
264
|
+
export const historyRedoOperationImplementation: BehaviorOperationImplementation<
|
|
265
265
|
'history.redo'
|
|
266
|
-
> = ({
|
|
267
|
-
const editor =
|
|
266
|
+
> = ({operation}) => {
|
|
267
|
+
const editor = operation.editor
|
|
268
268
|
const {redos} = editor.history
|
|
269
269
|
const remotePatches = getRemotePatches(editor)
|
|
270
270
|
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {Editor} from 'slate'
|
|
2
|
+
import {defaultKeyGenerator} from './key-generator'
|
|
3
|
+
|
|
4
|
+
const CURRENT_OPERATION_ID: WeakMap<Editor, string | undefined> = new WeakMap()
|
|
5
|
+
|
|
6
|
+
export function withApplyingBehaviorOperations(editor: Editor, fn: () => void) {
|
|
7
|
+
CURRENT_OPERATION_ID.set(editor, defaultKeyGenerator())
|
|
8
|
+
Editor.withoutNormalizing(editor, fn)
|
|
9
|
+
CURRENT_OPERATION_ID.set(editor, undefined)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function getCurrentOperationId(editor: Editor) {
|
|
13
|
+
return CURRENT_OPERATION_ID.get(editor)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function isApplyingBehaviorOperations(editor: Editor) {
|
|
17
|
+
return getCurrentOperationId(editor) !== undefined
|
|
18
|
+
}
|
|
@@ -1,24 +1,6 @@
|
|
|
1
|
-
import {Editor} from 'slate'
|
|
1
|
+
import type {Editor} from 'slate'
|
|
2
2
|
import {defaultKeyGenerator} from './key-generator'
|
|
3
3
|
|
|
4
|
-
const CURRENT_ACTION_ID: WeakMap<Editor, string | undefined> = new WeakMap()
|
|
5
|
-
|
|
6
|
-
export function withApplyingBehaviorActions(editor: Editor, fn: () => void) {
|
|
7
|
-
CURRENT_ACTION_ID.set(editor, defaultKeyGenerator())
|
|
8
|
-
Editor.withoutNormalizing(editor, fn)
|
|
9
|
-
CURRENT_ACTION_ID.set(editor, undefined)
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export function getCurrentActionId(editor: Editor) {
|
|
13
|
-
return CURRENT_ACTION_ID.get(editor)
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function isApplyingBehaviorActions(editor: Editor) {
|
|
17
|
-
return getCurrentActionId(editor) !== undefined
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
////////
|
|
21
|
-
|
|
22
4
|
const CURRENT_UNDO_STEP: WeakMap<Editor, {undoStepId: string} | undefined> =
|
|
23
5
|
new WeakMap()
|
|
24
6
|
|
package/src/index.ts
CHANGED
|
@@ -4,7 +4,7 @@ export type {
|
|
|
4
4
|
PortableTextChild,
|
|
5
5
|
PortableTextSpan,
|
|
6
6
|
} from '@sanity/types'
|
|
7
|
-
export type {AddedAnnotationPaths} from './
|
|
7
|
+
export type {AddedAnnotationPaths} from './operations/behavior.operation.annotation.add'
|
|
8
8
|
export {EditorEventListener} from './editor-event-listener'
|
|
9
9
|
export type {Editor, EditorConfig, EditorEvent} from './editor/create-editor'
|
|
10
10
|
export {
|
|
@@ -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
|
}
|