@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
package/package.json
CHANGED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {keyIs} from '../internal-utils/key-is'
|
|
2
|
+
import {raise} from './behavior.types.action'
|
|
3
|
+
import {defineBehavior} from './behavior.types.behavior'
|
|
4
|
+
|
|
5
|
+
export const abstractKeyboardBehaviors = [
|
|
6
|
+
/**
|
|
7
|
+
* On WebKit, Shift+Enter results in an `insertParagraph` input event rather
|
|
8
|
+
* than an `insertLineBreak` input event. This Behavior makes sure we catch
|
|
9
|
+
* that `keyboard.keydown` event beforehand and raise an `insert.soft break` manually.
|
|
10
|
+
*/
|
|
11
|
+
defineBehavior({
|
|
12
|
+
on: 'keyboard.keydown',
|
|
13
|
+
guard: ({event}) => keyIs.lineBreak(event.originEvent),
|
|
14
|
+
actions: [() => [raise({type: 'insert.soft break'})]],
|
|
15
|
+
}),
|
|
16
|
+
]
|
|
@@ -7,12 +7,12 @@ import {abstractAnnotationBehaviors} from './behavior.abstract.annotation'
|
|
|
7
7
|
import {abstractDecoratorBehaviors} from './behavior.abstract.decorator'
|
|
8
8
|
import {abstractDeleteBehaviors} from './behavior.abstract.delete'
|
|
9
9
|
import {abstractInsertBehaviors} from './behavior.abstract.insert'
|
|
10
|
+
import {abstractKeyboardBehaviors} from './behavior.abstract.keyboard'
|
|
10
11
|
import {abstractListItemBehaviors} from './behavior.abstract.list-item'
|
|
11
12
|
import {abstractMoveBehaviors} from './behavior.abstract.move'
|
|
12
13
|
import {abstractSelectBehaviors} from './behavior.abstract.select'
|
|
13
14
|
import {abstractSplitBehaviors} from './behavior.abstract.split'
|
|
14
15
|
import {abstractStyleBehaviors} from './behavior.abstract.style'
|
|
15
|
-
import {raiseInsertSoftBreak} from './behavior.default.raise-soft-break'
|
|
16
16
|
import {raise} from './behavior.types.action'
|
|
17
17
|
import {defineBehavior} from './behavior.types.behavior'
|
|
18
18
|
|
|
@@ -102,7 +102,7 @@ const raiseSerializationSuccessOrFailure = defineBehavior({
|
|
|
102
102
|
],
|
|
103
103
|
})
|
|
104
104
|
|
|
105
|
-
export const
|
|
105
|
+
export const abstractBehaviors = [
|
|
106
106
|
defineBehavior({
|
|
107
107
|
on: 'clipboard.copy',
|
|
108
108
|
guard: ({snapshot}) => {
|
|
@@ -451,6 +451,7 @@ export const defaultBehaviors = [
|
|
|
451
451
|
...abstractDecoratorBehaviors,
|
|
452
452
|
...abstractDeleteBehaviors,
|
|
453
453
|
...abstractInsertBehaviors,
|
|
454
|
+
...abstractKeyboardBehaviors,
|
|
454
455
|
...abstractListItemBehaviors,
|
|
455
456
|
...abstractMoveBehaviors,
|
|
456
457
|
...abstractStyleBehaviors,
|
|
@@ -458,5 +459,4 @@ export const defaultBehaviors = [
|
|
|
458
459
|
...abstractSplitBehaviors,
|
|
459
460
|
raiseDeserializationSuccessOrFailure,
|
|
460
461
|
raiseSerializationSuccessOrFailure,
|
|
461
|
-
raiseInsertSoftBreak,
|
|
462
462
|
]
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import {corePriority} from '../priority/priority.core'
|
|
1
2
|
import {coreAnnotationBehaviors} from './behavior.core.annotations'
|
|
2
3
|
import {coreBlockObjectBehaviors} from './behavior.core.block-objects'
|
|
3
4
|
import {coreDecoratorBehaviors} from './behavior.core.decorators'
|
|
@@ -5,10 +6,7 @@ import {coreDndBehaviors} from './behavior.core.dnd'
|
|
|
5
6
|
import {coreInsertBreakBehaviors} from './behavior.core.insert-break'
|
|
6
7
|
import {coreListBehaviors} from './behavior.core.lists'
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
* @beta
|
|
10
|
-
*/
|
|
11
|
-
export const coreBehaviors = [
|
|
9
|
+
export const coreBehaviorsConfig = [
|
|
12
10
|
coreAnnotationBehaviors.addAnnotationOnCollapsedSelection,
|
|
13
11
|
coreDecoratorBehaviors.strongShortcut,
|
|
14
12
|
coreDecoratorBehaviors.emShortcut,
|
|
@@ -31,4 +29,7 @@ export const coreBehaviors = [
|
|
|
31
29
|
coreInsertBreakBehaviors.breakingAtTheStartOfTextBlock,
|
|
32
30
|
coreInsertBreakBehaviors.breakingEntireDocument,
|
|
33
31
|
coreInsertBreakBehaviors.breakingEntireBlocks,
|
|
34
|
-
]
|
|
32
|
+
].map((behavior) => ({
|
|
33
|
+
behavior,
|
|
34
|
+
priority: corePriority,
|
|
35
|
+
}))
|
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
import {performAction} from '../behavior-actions/behavior.actions'
|
|
2
1
|
import type {EditorSchema} from '../editor/editor-schema'
|
|
3
2
|
import type {EditorSnapshot} from '../editor/editor-snapshot'
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
withUndoStep,
|
|
7
|
-
} from '../editor/with-applying-behavior-actions'
|
|
3
|
+
import {withApplyingBehaviorOperations} from '../editor/with-applying-behavior-operations'
|
|
4
|
+
import {withUndoStep} from '../editor/with-undo-step'
|
|
8
5
|
import {debugWithName} from '../internal-utils/debug'
|
|
6
|
+
import {performOperation} from '../operations/behavior.operations'
|
|
9
7
|
import type {PortableTextSlateEditor} from '../types/editor'
|
|
10
|
-
import {
|
|
8
|
+
import {abstractBehaviors} from './behavior.abstract'
|
|
11
9
|
import type {BehaviorAction} from './behavior.types.action'
|
|
12
10
|
import type {Behavior} from './behavior.types.behavior'
|
|
13
11
|
import {
|
|
@@ -59,7 +57,7 @@ export function performEvent({
|
|
|
59
57
|
|
|
60
58
|
const eventBehaviors = [
|
|
61
59
|
...remainingEventBehaviors,
|
|
62
|
-
...
|
|
60
|
+
...abstractBehaviors,
|
|
63
61
|
].filter((behavior) => {
|
|
64
62
|
// Catches all events
|
|
65
63
|
if (behavior.on === '*') {
|
|
@@ -100,30 +98,19 @@ export function performEvent({
|
|
|
100
98
|
if (eventBehaviors.length === 0 && isSyntheticBehaviorEvent(event)) {
|
|
101
99
|
nativeEvent?.preventDefault()
|
|
102
100
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
...event,
|
|
117
|
-
editor,
|
|
118
|
-
},
|
|
119
|
-
})
|
|
120
|
-
} catch (error) {
|
|
121
|
-
console.error(
|
|
122
|
-
new Error(
|
|
123
|
-
`Executing "${event.type}" failed due to: ${error.message}`,
|
|
124
|
-
),
|
|
125
|
-
)
|
|
126
|
-
}
|
|
101
|
+
withApplyingBehaviorOperations(editor, () => {
|
|
102
|
+
debug(`(execute:${eventCategory(event)})`, JSON.stringify(event, null, 2))
|
|
103
|
+
|
|
104
|
+
performOperation({
|
|
105
|
+
context: {
|
|
106
|
+
keyGenerator,
|
|
107
|
+
schema,
|
|
108
|
+
},
|
|
109
|
+
operation: {
|
|
110
|
+
...event,
|
|
111
|
+
editor,
|
|
112
|
+
},
|
|
113
|
+
})
|
|
127
114
|
})
|
|
128
115
|
|
|
129
116
|
editor.onChange()
|
|
@@ -339,27 +326,16 @@ export function performEvent({
|
|
|
339
326
|
if (!defaultBehaviorOverwritten && isSyntheticBehaviorEvent(event)) {
|
|
340
327
|
nativeEvent?.preventDefault()
|
|
341
328
|
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
debug(
|
|
345
|
-
`(execute:${eventCategory(event)})`,
|
|
346
|
-
JSON.stringify(event, null, 2),
|
|
347
|
-
)
|
|
329
|
+
withApplyingBehaviorOperations(editor, () => {
|
|
330
|
+
debug(`(execute:${eventCategory(event)})`, JSON.stringify(event, null, 2))
|
|
348
331
|
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
} catch (error) {
|
|
357
|
-
console.error(
|
|
358
|
-
new Error(
|
|
359
|
-
`Executing "${event.type}" failed due to: ${error.message}`,
|
|
360
|
-
),
|
|
361
|
-
)
|
|
362
|
-
}
|
|
332
|
+
performOperation({
|
|
333
|
+
context: {keyGenerator, schema},
|
|
334
|
+
operation: {
|
|
335
|
+
...event,
|
|
336
|
+
editor,
|
|
337
|
+
},
|
|
338
|
+
})
|
|
363
339
|
})
|
|
364
340
|
|
|
365
341
|
editor.onChange()
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import type {EditorSnapshot} from '../editor/editor-snapshot'
|
|
2
|
-
import type {
|
|
3
|
-
import type {PortableTextSlateEditor} from '../types/editor'
|
|
2
|
+
import type {PickFromUnion} from '../type-utils'
|
|
4
3
|
import type {
|
|
5
|
-
AbstractBehaviorEventType,
|
|
6
4
|
CustomBehaviorEvent,
|
|
7
5
|
NativeBehaviorEvent,
|
|
8
6
|
SyntheticBehaviorEvent,
|
|
@@ -75,11 +73,3 @@ export type BehaviorActionSet<TBehaviorEvent, TGuardResponse> = (
|
|
|
75
73
|
},
|
|
76
74
|
guardResponse: TGuardResponse,
|
|
77
75
|
) => Array<BehaviorAction>
|
|
78
|
-
|
|
79
|
-
export type InternalBehaviorAction = OmitFromUnion<
|
|
80
|
-
SyntheticBehaviorEvent,
|
|
81
|
-
'type',
|
|
82
|
-
AbstractBehaviorEventType
|
|
83
|
-
> & {
|
|
84
|
-
editor: PortableTextSlateEditor
|
|
85
|
-
}
|
|
@@ -16,10 +16,10 @@ import {Subject} from 'rxjs'
|
|
|
16
16
|
import {Slate} from 'slate-react'
|
|
17
17
|
import {useEffectEvent} from 'use-effect-event'
|
|
18
18
|
import {createActor} from 'xstate'
|
|
19
|
-
import type {AddedAnnotationPaths} from '../behavior-actions/behavior.action.annotation.add'
|
|
20
19
|
import {createCoreConverters} from '../converters/converters.core'
|
|
21
20
|
import {debugWithName} from '../internal-utils/debug'
|
|
22
21
|
import {compileType} from '../internal-utils/schema'
|
|
22
|
+
import type {AddedAnnotationPaths} from '../operations/behavior.operation.annotation.add'
|
|
23
23
|
import type {
|
|
24
24
|
EditableAPI,
|
|
25
25
|
EditableAPIDeleteOptions,
|
|
@@ -26,6 +26,8 @@ import {debugWithName} from '../../internal-utils/debug'
|
|
|
26
26
|
import type {EventPositionBlock} from '../../internal-utils/event-position'
|
|
27
27
|
import {fromSlateValue} from '../../internal-utils/values'
|
|
28
28
|
import {KEY_TO_VALUE_ELEMENT} from '../../internal-utils/weakMaps'
|
|
29
|
+
import {corePriority} from '../../priority/priority.core'
|
|
30
|
+
import {createEditorPriority} from '../../priority/priority.types'
|
|
29
31
|
import * as selectors from '../../selectors'
|
|
30
32
|
import type {
|
|
31
33
|
BlockRenderProps,
|
|
@@ -150,15 +152,27 @@ export const Element: FunctionComponent<ElementProps> = ({
|
|
|
150
152
|
],
|
|
151
153
|
})
|
|
152
154
|
|
|
155
|
+
const priority = createEditorPriority({
|
|
156
|
+
reference: {
|
|
157
|
+
priority: corePriority,
|
|
158
|
+
importance: 'lower',
|
|
159
|
+
},
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
const behaviorConfig = {
|
|
163
|
+
behavior,
|
|
164
|
+
priority,
|
|
165
|
+
}
|
|
166
|
+
|
|
153
167
|
editorActor.send({
|
|
154
168
|
type: 'add behavior',
|
|
155
|
-
|
|
169
|
+
behaviorConfig,
|
|
156
170
|
})
|
|
157
171
|
|
|
158
172
|
return () => {
|
|
159
173
|
editorActor.send({
|
|
160
174
|
type: 'remove behavior',
|
|
161
|
-
|
|
175
|
+
behaviorConfig,
|
|
162
176
|
})
|
|
163
177
|
}
|
|
164
178
|
}, [editorActor, element._key])
|
|
@@ -182,15 +196,27 @@ export const Element: FunctionComponent<ElementProps> = ({
|
|
|
182
196
|
],
|
|
183
197
|
})
|
|
184
198
|
|
|
199
|
+
const priority = createEditorPriority({
|
|
200
|
+
reference: {
|
|
201
|
+
priority: corePriority,
|
|
202
|
+
importance: 'lower',
|
|
203
|
+
},
|
|
204
|
+
})
|
|
205
|
+
|
|
206
|
+
const behaviorConfig = {
|
|
207
|
+
behavior,
|
|
208
|
+
priority,
|
|
209
|
+
}
|
|
210
|
+
|
|
185
211
|
editorActor.send({
|
|
186
212
|
type: 'add behavior',
|
|
187
|
-
|
|
213
|
+
behaviorConfig,
|
|
188
214
|
})
|
|
189
215
|
|
|
190
216
|
return () => {
|
|
191
217
|
editorActor.send({
|
|
192
218
|
type: 'remove behavior',
|
|
193
|
-
|
|
219
|
+
behaviorConfig,
|
|
194
220
|
})
|
|
195
221
|
}
|
|
196
222
|
}, [editorActor])
|
|
@@ -8,6 +8,8 @@ import type {Behavior} from '../behaviors/behavior.types.behavior'
|
|
|
8
8
|
import type {ExternalBehaviorEvent} from '../behaviors/behavior.types.event'
|
|
9
9
|
import {createCoreConverters} from '../converters/converters.core'
|
|
10
10
|
import {compileType} from '../internal-utils/schema'
|
|
11
|
+
import {corePriority} from '../priority/priority.core'
|
|
12
|
+
import {createEditorPriority} from '../priority/priority.types'
|
|
11
13
|
import type {EditableAPI} from '../types/editor'
|
|
12
14
|
import {createSlateEditor, type SlateEditor} from './create-slate-editor'
|
|
13
15
|
import type {
|
|
@@ -117,23 +119,33 @@ export function createInternalEditor(editorActor: EditorActor): InternalEditor {
|
|
|
117
119
|
editorActorSnapshot: editorActor.getSnapshot(),
|
|
118
120
|
slateEditorInstance: slateEditor.instance,
|
|
119
121
|
}),
|
|
120
|
-
registerBehavior: (
|
|
122
|
+
registerBehavior: (behaviorConfig) => {
|
|
123
|
+
const priority = createEditorPriority({
|
|
124
|
+
name: 'custom',
|
|
125
|
+
reference: {
|
|
126
|
+
priority: corePriority,
|
|
127
|
+
importance: 'higher',
|
|
128
|
+
},
|
|
129
|
+
})
|
|
130
|
+
const behaviorConfigWithPriority = {
|
|
131
|
+
...behaviorConfig,
|
|
132
|
+
priority,
|
|
133
|
+
}
|
|
134
|
+
|
|
121
135
|
editorActor.send({
|
|
122
136
|
type: 'add behavior',
|
|
123
|
-
|
|
137
|
+
behaviorConfig: behaviorConfigWithPriority,
|
|
124
138
|
})
|
|
125
139
|
|
|
126
140
|
return () => {
|
|
127
141
|
editorActor.send({
|
|
128
142
|
type: 'remove behavior',
|
|
129
|
-
|
|
143
|
+
behaviorConfig: behaviorConfigWithPriority,
|
|
130
144
|
})
|
|
131
145
|
}
|
|
132
146
|
},
|
|
133
147
|
send: (event) => {
|
|
134
148
|
switch (event.type) {
|
|
135
|
-
case 'add behavior':
|
|
136
|
-
case 'remove behavior':
|
|
137
149
|
case 'update key generator':
|
|
138
150
|
case 'update readOnly':
|
|
139
151
|
case 'patches':
|
|
@@ -11,12 +11,13 @@ import {
|
|
|
11
11
|
setup,
|
|
12
12
|
type ActorRefFrom,
|
|
13
13
|
} from 'xstate'
|
|
14
|
-
import {
|
|
14
|
+
import type {BehaviorConfig} from '../behaviors/behavior.config'
|
|
15
|
+
import {coreBehaviorsConfig} from '../behaviors/behavior.core'
|
|
15
16
|
import {performEvent} from '../behaviors/behavior.perform-event'
|
|
16
|
-
import type {Behavior} from '../behaviors/behavior.types.behavior'
|
|
17
17
|
import type {BehaviorEvent} from '../behaviors/behavior.types.event'
|
|
18
18
|
import type {Converter} from '../converters/converter.types'
|
|
19
19
|
import type {EventPosition} from '../internal-utils/event-position'
|
|
20
|
+
import {sortByPriority} from '../priority/priority.sort'
|
|
20
21
|
import type {NamespaceEvent} from '../type-utils'
|
|
21
22
|
import type {
|
|
22
23
|
EditorSelection,
|
|
@@ -55,14 +56,6 @@ export type MutationEvent = {
|
|
|
55
56
|
* @public
|
|
56
57
|
*/
|
|
57
58
|
export type ExternalEditorEvent =
|
|
58
|
-
| {
|
|
59
|
-
type: 'add behavior'
|
|
60
|
-
behavior: Behavior
|
|
61
|
-
}
|
|
62
|
-
| {
|
|
63
|
-
type: 'remove behavior'
|
|
64
|
-
behavior: Behavior
|
|
65
|
-
}
|
|
66
59
|
| {
|
|
67
60
|
type: 'update readOnly'
|
|
68
61
|
readOnly: boolean
|
|
@@ -140,7 +133,7 @@ type PatchEvent = {
|
|
|
140
133
|
}
|
|
141
134
|
|
|
142
135
|
type InternalPatchEvent = NamespaceEvent<PatchEvent, 'internal'> & {
|
|
143
|
-
|
|
136
|
+
operationId?: string
|
|
144
137
|
value: Array<PortableTextBlock>
|
|
145
138
|
}
|
|
146
139
|
|
|
@@ -160,6 +153,14 @@ export type HasTag = ReturnType<EditorActor['getSnapshot']>['hasTag']
|
|
|
160
153
|
*/
|
|
161
154
|
export type InternalEditorEvent =
|
|
162
155
|
| ExternalEditorEvent
|
|
156
|
+
| {
|
|
157
|
+
type: 'add behavior'
|
|
158
|
+
behaviorConfig: BehaviorConfig
|
|
159
|
+
}
|
|
160
|
+
| {
|
|
161
|
+
type: 'remove behavior'
|
|
162
|
+
behaviorConfig: BehaviorConfig
|
|
163
|
+
}
|
|
163
164
|
| {
|
|
164
165
|
type: 'blur'
|
|
165
166
|
editor: PortableTextSlateEditor
|
|
@@ -213,7 +214,7 @@ export type InternalEditorEmittedEvent =
|
|
|
213
214
|
export const editorMachine = setup({
|
|
214
215
|
types: {
|
|
215
216
|
context: {} as {
|
|
216
|
-
behaviors: Set<
|
|
217
|
+
behaviors: Set<BehaviorConfig>
|
|
217
218
|
converters: Set<Converter>
|
|
218
219
|
getLegacySchema: () => PortableTextMemberSchemaTypes
|
|
219
220
|
keyGenerator: () => string
|
|
@@ -248,14 +249,14 @@ export const editorMachine = setup({
|
|
|
248
249
|
behaviors: ({context, event}) => {
|
|
249
250
|
assertEvent(event, 'add behavior')
|
|
250
251
|
|
|
251
|
-
return new Set([...context.behaviors, event.
|
|
252
|
+
return new Set([...context.behaviors, event.behaviorConfig])
|
|
252
253
|
},
|
|
253
254
|
}),
|
|
254
255
|
'remove behavior from context': assign({
|
|
255
256
|
behaviors: ({context, event}) => {
|
|
256
257
|
assertEvent(event, 'remove behavior')
|
|
257
258
|
|
|
258
|
-
context.behaviors.delete(event.
|
|
259
|
+
context.behaviors.delete(event.behaviorConfig)
|
|
259
260
|
|
|
260
261
|
return new Set([...context.behaviors])
|
|
261
262
|
},
|
|
@@ -342,10 +343,15 @@ export const editorMachine = setup({
|
|
|
342
343
|
assertEvent(event, ['behavior event'])
|
|
343
344
|
|
|
344
345
|
try {
|
|
346
|
+
const behaviors = sortByPriority([
|
|
347
|
+
...context.behaviors.values(),
|
|
348
|
+
...coreBehaviorsConfig,
|
|
349
|
+
]).map((config) => config.behavior)
|
|
350
|
+
|
|
345
351
|
performEvent({
|
|
346
352
|
mode: 'raise',
|
|
347
|
-
behaviors
|
|
348
|
-
remainingEventBehaviors:
|
|
353
|
+
behaviors,
|
|
354
|
+
remainingEventBehaviors: behaviors,
|
|
349
355
|
event: event.behaviorEvent,
|
|
350
356
|
editor: event.editor,
|
|
351
357
|
keyGenerator: context.keyGenerator,
|
|
@@ -383,7 +389,7 @@ export const editorMachine = setup({
|
|
|
383
389
|
}).createMachine({
|
|
384
390
|
id: 'editor',
|
|
385
391
|
context: ({input}) => ({
|
|
386
|
-
behaviors: new Set([
|
|
392
|
+
behaviors: new Set([]),
|
|
387
393
|
converters: new Set(input.converters ?? []),
|
|
388
394
|
getLegacySchema: input.getLegacySchema,
|
|
389
395
|
keyGenerator: input.keyGenerator,
|
|
@@ -23,7 +23,7 @@ export const mutationMachine = setup({
|
|
|
23
23
|
types: {
|
|
24
24
|
context: {} as {
|
|
25
25
|
pendingMutations: Array<{
|
|
26
|
-
|
|
26
|
+
operationId?: string
|
|
27
27
|
value: Array<PortableTextBlock> | undefined
|
|
28
28
|
patches: Array<Patch>
|
|
29
29
|
}>
|
|
@@ -34,7 +34,7 @@ export const mutationMachine = setup({
|
|
|
34
34
|
| {
|
|
35
35
|
type: 'patch'
|
|
36
36
|
patch: Patch
|
|
37
|
-
|
|
37
|
+
operationId?: string
|
|
38
38
|
value: Array<PortableTextBlock>
|
|
39
39
|
}
|
|
40
40
|
| {
|
|
@@ -78,7 +78,7 @@ export const mutationMachine = setup({
|
|
|
78
78
|
if (context.pendingMutations.length === 0) {
|
|
79
79
|
return [
|
|
80
80
|
{
|
|
81
|
-
|
|
81
|
+
operationId: event.operationId,
|
|
82
82
|
value: event.value,
|
|
83
83
|
patches: [event.patch],
|
|
84
84
|
},
|
|
@@ -87,17 +87,17 @@ export const mutationMachine = setup({
|
|
|
87
87
|
|
|
88
88
|
const lastBulk = context.pendingMutations.at(-1)
|
|
89
89
|
|
|
90
|
-
if (lastBulk && lastBulk.
|
|
90
|
+
if (lastBulk && lastBulk.operationId === event.operationId) {
|
|
91
91
|
return context.pendingMutations.slice(0, -1).concat({
|
|
92
92
|
value: event.value,
|
|
93
|
-
|
|
93
|
+
operationId: lastBulk.operationId,
|
|
94
94
|
patches: [...lastBulk.patches, event.patch],
|
|
95
95
|
})
|
|
96
96
|
}
|
|
97
97
|
|
|
98
98
|
return context.pendingMutations.concat({
|
|
99
99
|
value: event.value,
|
|
100
|
-
|
|
100
|
+
operationId: event.operationId,
|
|
101
101
|
patches: [event.patch],
|
|
102
102
|
})
|
|
103
103
|
},
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {Editor} from 'slate'
|
|
2
|
-
import {insertTextActionImplementation} from '../../behavior-actions/behavior.action.insert.text'
|
|
3
|
-
import {performAction} from '../../behavior-actions/behavior.actions'
|
|
4
2
|
import {slateRangeToSelection} from '../../internal-utils/slate-utils'
|
|
3
|
+
import {insertTextOperationImplementation} from '../../operations/behavior.operation.insert.text'
|
|
4
|
+
import {performOperation} from '../../operations/behavior.operations'
|
|
5
5
|
import type {EditorActor} from '../editor-machine'
|
|
6
|
-
import {
|
|
6
|
+
import {isApplyingBehaviorOperations} from '../with-applying-behavior-operations'
|
|
7
7
|
|
|
8
8
|
export function createWithEventListeners(editorActor: EditorActor) {
|
|
9
9
|
return function withEventListeners(editor: Editor) {
|
|
@@ -11,12 +11,11 @@ export function createWithEventListeners(editorActor: EditorActor) {
|
|
|
11
11
|
return editor
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
const {
|
|
15
|
-
editor
|
|
14
|
+
const {insertText, select} = editor
|
|
16
15
|
|
|
17
16
|
editor.deleteBackward = (unit) => {
|
|
18
|
-
if (
|
|
19
|
-
deleteBackward(
|
|
17
|
+
if (isApplyingBehaviorOperations(editor)) {
|
|
18
|
+
console.error('Unexpected call to .deleteBackward(...)')
|
|
20
19
|
return
|
|
21
20
|
}
|
|
22
21
|
|
|
@@ -32,8 +31,8 @@ export function createWithEventListeners(editorActor: EditorActor) {
|
|
|
32
31
|
}
|
|
33
32
|
|
|
34
33
|
editor.deleteForward = (unit) => {
|
|
35
|
-
if (
|
|
36
|
-
deleteForward(
|
|
34
|
+
if (isApplyingBehaviorOperations(editor)) {
|
|
35
|
+
console.error('Unexpected call to .deleteForward(...)')
|
|
37
36
|
return
|
|
38
37
|
}
|
|
39
38
|
|
|
@@ -49,8 +48,8 @@ export function createWithEventListeners(editorActor: EditorActor) {
|
|
|
49
48
|
}
|
|
50
49
|
|
|
51
50
|
editor.insertBreak = () => {
|
|
52
|
-
if (
|
|
53
|
-
insertBreak()
|
|
51
|
+
if (isApplyingBehaviorOperations(editor)) {
|
|
52
|
+
console.error('Unexpected call to .insertBreak(...)')
|
|
54
53
|
return
|
|
55
54
|
}
|
|
56
55
|
|
|
@@ -65,8 +64,9 @@ export function createWithEventListeners(editorActor: EditorActor) {
|
|
|
65
64
|
}
|
|
66
65
|
|
|
67
66
|
editor.insertData = (dataTransfer) => {
|
|
68
|
-
if (
|
|
69
|
-
|
|
67
|
+
if (isApplyingBehaviorOperations(editor)) {
|
|
68
|
+
console.error('Unexpected call to .insertData(...)')
|
|
69
|
+
return
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
editorActor.send({
|
|
@@ -82,13 +82,13 @@ export function createWithEventListeners(editorActor: EditorActor) {
|
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
editor.insertSoftBreak = () => {
|
|
85
|
-
if (
|
|
86
|
-
|
|
85
|
+
if (isApplyingBehaviorOperations(editor)) {
|
|
86
|
+
insertTextOperationImplementation({
|
|
87
87
|
context: {
|
|
88
88
|
keyGenerator: editorActor.getSnapshot().context.keyGenerator,
|
|
89
89
|
schema: editorActor.getSnapshot().context.schema,
|
|
90
90
|
},
|
|
91
|
-
|
|
91
|
+
operation: {type: 'insert.text', text: '\n', editor},
|
|
92
92
|
})
|
|
93
93
|
return
|
|
94
94
|
}
|
|
@@ -104,7 +104,7 @@ export function createWithEventListeners(editorActor: EditorActor) {
|
|
|
104
104
|
}
|
|
105
105
|
|
|
106
106
|
editor.insertText = (text, options) => {
|
|
107
|
-
if (
|
|
107
|
+
if (isApplyingBehaviorOperations(editor)) {
|
|
108
108
|
insertText(text, options)
|
|
109
109
|
return
|
|
110
110
|
}
|
|
@@ -121,13 +121,13 @@ export function createWithEventListeners(editorActor: EditorActor) {
|
|
|
121
121
|
}
|
|
122
122
|
|
|
123
123
|
editor.redo = () => {
|
|
124
|
-
if (
|
|
125
|
-
|
|
124
|
+
if (isApplyingBehaviorOperations(editor)) {
|
|
125
|
+
performOperation({
|
|
126
126
|
context: {
|
|
127
127
|
keyGenerator: editorActor.getSnapshot().context.keyGenerator,
|
|
128
128
|
schema: editorActor.getSnapshot().context.schema,
|
|
129
129
|
},
|
|
130
|
-
|
|
130
|
+
operation: {
|
|
131
131
|
type: 'history.redo',
|
|
132
132
|
editor,
|
|
133
133
|
},
|
|
@@ -146,7 +146,7 @@ export function createWithEventListeners(editorActor: EditorActor) {
|
|
|
146
146
|
}
|
|
147
147
|
|
|
148
148
|
editor.select = (location) => {
|
|
149
|
-
if (
|
|
149
|
+
if (isApplyingBehaviorOperations(editor)) {
|
|
150
150
|
select(location)
|
|
151
151
|
return
|
|
152
152
|
}
|
|
@@ -169,18 +169,18 @@ export function createWithEventListeners(editorActor: EditorActor) {
|
|
|
169
169
|
}
|
|
170
170
|
|
|
171
171
|
editor.setFragmentData = () => {
|
|
172
|
-
console.
|
|
172
|
+
console.error('Unexpected call to .setFragmentData(...)')
|
|
173
173
|
return
|
|
174
174
|
}
|
|
175
175
|
|
|
176
176
|
editor.undo = () => {
|
|
177
|
-
if (
|
|
178
|
-
|
|
177
|
+
if (isApplyingBehaviorOperations(editor)) {
|
|
178
|
+
performOperation({
|
|
179
179
|
context: {
|
|
180
180
|
keyGenerator: editorActor.getSnapshot().context.keyGenerator,
|
|
181
181
|
schema: editorActor.getSnapshot().context.schema,
|
|
182
182
|
},
|
|
183
|
-
|
|
183
|
+
operation: {
|
|
184
184
|
type: 'history.undo',
|
|
185
185
|
editor,
|
|
186
186
|
},
|
|
@@ -16,7 +16,6 @@ import {
|
|
|
16
16
|
} from 'slate'
|
|
17
17
|
import type {DOMNode} from 'slate-dom'
|
|
18
18
|
import {ReactEditor} from 'slate-react'
|
|
19
|
-
import {addAnnotationActionImplementation} from '../../behavior-actions/behavior.action.annotation.add'
|
|
20
19
|
import {debugWithName} from '../../internal-utils/debug'
|
|
21
20
|
import {toSlateRange} from '../../internal-utils/ranges'
|
|
22
21
|
import {
|
|
@@ -29,6 +28,7 @@ import {
|
|
|
29
28
|
KEY_TO_VALUE_ELEMENT,
|
|
30
29
|
SLATE_TO_PORTABLE_TEXT_RANGE,
|
|
31
30
|
} from '../../internal-utils/weakMaps'
|
|
31
|
+
import {addAnnotationOperationImplementation} from '../../operations/behavior.operation.annotation.add'
|
|
32
32
|
import type {
|
|
33
33
|
EditableAPI,
|
|
34
34
|
EditableAPIDeleteOptions,
|
|
@@ -400,12 +400,12 @@ export function createEditableAPI(
|
|
|
400
400
|
let paths: ReturnType<EditableAPI['addAnnotation']> = undefined
|
|
401
401
|
|
|
402
402
|
Editor.withoutNormalizing(editor, () => {
|
|
403
|
-
paths =
|
|
403
|
+
paths = addAnnotationOperationImplementation({
|
|
404
404
|
context: {
|
|
405
405
|
keyGenerator: editorActor.getSnapshot().context.keyGenerator,
|
|
406
406
|
schema: types,
|
|
407
407
|
},
|
|
408
|
-
|
|
408
|
+
operation: {
|
|
409
409
|
type: 'annotation.add',
|
|
410
410
|
annotation: {name: type.name, value: value ?? {}},
|
|
411
411
|
editor,
|