@portabletext/editor 1.31.2 → 1.32.0
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/behavior.core.cjs.map +1 -1
- package/lib/_chunks-cjs/plugin.event-listener.cjs +1425 -1349
- package/lib/_chunks-cjs/plugin.event-listener.cjs.map +1 -1
- package/lib/_chunks-es/behavior.core.js.map +1 -1
- package/lib/_chunks-es/plugin.event-listener.js +1424 -1348
- package/lib/_chunks-es/plugin.event-listener.js.map +1 -1
- package/lib/behaviors/index.d.cts +26 -0
- package/lib/behaviors/index.d.ts +26 -0
- package/lib/index.d.cts +1068 -66
- package/lib/index.d.ts +1068 -66
- package/lib/plugins/index.d.cts +1068 -66
- package/lib/plugins/index.d.ts +1068 -66
- package/package.json +1 -1
- package/src/behavior-actions/behavior.actions.ts +20 -0
- package/src/behaviors/behavior.types.ts +6 -0
- package/src/editor/editor-machine.ts +80 -85
- package/src/editor/plugins/create-with-event-listeners.ts +51 -0
- package/src/editor/plugins/createWithEditableAPI.ts +18 -2
- package/src/editor/plugins/createWithUndoRedo.ts +132 -107
- package/src/editor/with-applying-behavior-actions.ts +27 -3
package/package.json
CHANGED
|
@@ -21,6 +21,10 @@ import {
|
|
|
21
21
|
removeDecoratorActionImplementation,
|
|
22
22
|
toggleDecoratorActionImplementation,
|
|
23
23
|
} from '../editor/plugins/createWithPortableTextMarkModel'
|
|
24
|
+
import {
|
|
25
|
+
historyRedoActionImplementation,
|
|
26
|
+
historyUndoActionImplementation,
|
|
27
|
+
} from '../editor/plugins/createWithUndoRedo'
|
|
24
28
|
import {toSlatePath} from '../internal-utils/paths'
|
|
25
29
|
import {toSlateRange} from '../internal-utils/ranges'
|
|
26
30
|
import {fromSlateValue, toSlateValue} from '../internal-utils/values'
|
|
@@ -168,6 +172,8 @@ const behaviorActionImplementations: BehaviorActionImplementations = {
|
|
|
168
172
|
},
|
|
169
173
|
})
|
|
170
174
|
},
|
|
175
|
+
'history.redo': historyRedoActionImplementation,
|
|
176
|
+
'history.undo': historyUndoActionImplementation,
|
|
171
177
|
'insert.block': insertBlockActionImplementation,
|
|
172
178
|
'insert.blocks': insertBlocksActionImplementation,
|
|
173
179
|
'insert.block object': insertBlockObjectActionImplementation,
|
|
@@ -470,6 +476,20 @@ function performDefaultAction({
|
|
|
470
476
|
})
|
|
471
477
|
break
|
|
472
478
|
}
|
|
479
|
+
case 'history.redo': {
|
|
480
|
+
behaviorActionImplementations['history.redo']({
|
|
481
|
+
context,
|
|
482
|
+
action,
|
|
483
|
+
})
|
|
484
|
+
break
|
|
485
|
+
}
|
|
486
|
+
case 'history.undo': {
|
|
487
|
+
behaviorActionImplementations['history.undo']({
|
|
488
|
+
context,
|
|
489
|
+
action,
|
|
490
|
+
})
|
|
491
|
+
break
|
|
492
|
+
}
|
|
473
493
|
case 'insert.block': {
|
|
474
494
|
behaviorActionImplementations['insert.block']({
|
|
475
495
|
context,
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type {Patch} from '@portabletext/patches'
|
|
2
2
|
import type {PortableTextBlock} from '@sanity/types'
|
|
3
3
|
import type {FocusEvent} from 'react'
|
|
4
|
-
import {Editor} from 'slate'
|
|
5
4
|
import {
|
|
6
5
|
assertEvent,
|
|
7
6
|
assign,
|
|
@@ -30,7 +29,10 @@ import type {
|
|
|
30
29
|
} from '../types/editor'
|
|
31
30
|
import type {EditorSchema} from './define-schema'
|
|
32
31
|
import {createEditorSnapshot} from './editor-snapshot'
|
|
33
|
-
import {
|
|
32
|
+
import {
|
|
33
|
+
withApplyingBehaviorActionIntendSet,
|
|
34
|
+
withApplyingBehaviorActions,
|
|
35
|
+
} from './with-applying-behavior-actions'
|
|
34
36
|
|
|
35
37
|
export * from 'xstate/guards'
|
|
36
38
|
|
|
@@ -315,17 +317,15 @@ export const editorMachine = setup({
|
|
|
315
317
|
if (eventBehaviors.length === 0) {
|
|
316
318
|
if (defaultActionCallback) {
|
|
317
319
|
withApplyingBehaviorActions(event.editor, () => {
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
}
|
|
328
|
-
})
|
|
320
|
+
try {
|
|
321
|
+
defaultActionCallback()
|
|
322
|
+
} catch (error) {
|
|
323
|
+
console.error(
|
|
324
|
+
new Error(
|
|
325
|
+
`Performing action "${event.behaviorEvent.type}" failed due to: ${error.message}`,
|
|
326
|
+
),
|
|
327
|
+
)
|
|
328
|
+
}
|
|
329
329
|
})
|
|
330
330
|
return
|
|
331
331
|
}
|
|
@@ -335,20 +335,18 @@ export const editorMachine = setup({
|
|
|
335
335
|
}
|
|
336
336
|
|
|
337
337
|
withApplyingBehaviorActions(event.editor, () => {
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
}
|
|
351
|
-
})
|
|
338
|
+
try {
|
|
339
|
+
performAction({
|
|
340
|
+
context,
|
|
341
|
+
action: defaultAction,
|
|
342
|
+
})
|
|
343
|
+
} catch (error) {
|
|
344
|
+
console.error(
|
|
345
|
+
new Error(
|
|
346
|
+
`Performing action "${defaultAction.type}" as a result of "${event.behaviorEvent.type}" failed due to: ${error.message}`,
|
|
347
|
+
),
|
|
348
|
+
)
|
|
349
|
+
}
|
|
352
350
|
})
|
|
353
351
|
event.editor.onChange()
|
|
354
352
|
return
|
|
@@ -390,43 +388,41 @@ export const editorMachine = setup({
|
|
|
390
388
|
(actionIntend) => actionIntend.type !== 'effect',
|
|
391
389
|
))
|
|
392
390
|
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
if (actionIntend.
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
})
|
|
409
|
-
}
|
|
410
|
-
continue
|
|
391
|
+
withApplyingBehaviorActionIntendSet(event.editor, () => {
|
|
392
|
+
for (const actionIntend of actionIntends) {
|
|
393
|
+
if (actionIntend.type === 'raise') {
|
|
394
|
+
if (isCustomBehaviorEvent(actionIntend.event)) {
|
|
395
|
+
enqueue.raise({
|
|
396
|
+
type: 'custom behavior event',
|
|
397
|
+
behaviorEvent: actionIntend.event as CustomBehaviorEvent,
|
|
398
|
+
editor: event.editor,
|
|
399
|
+
})
|
|
400
|
+
} else {
|
|
401
|
+
enqueue.raise({
|
|
402
|
+
type: 'behavior event',
|
|
403
|
+
behaviorEvent: actionIntend.event,
|
|
404
|
+
editor: event.editor,
|
|
405
|
+
})
|
|
411
406
|
}
|
|
407
|
+
continue
|
|
408
|
+
}
|
|
412
409
|
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
410
|
+
const action = {
|
|
411
|
+
...actionIntend,
|
|
412
|
+
editor: event.editor,
|
|
413
|
+
}
|
|
417
414
|
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
}
|
|
415
|
+
try {
|
|
416
|
+
performAction({context, action})
|
|
417
|
+
} catch (error) {
|
|
418
|
+
console.error(
|
|
419
|
+
new Error(
|
|
420
|
+
`Performing action "${action.type}" as a result of "${event.behaviorEvent.type}" failed due to: ${error.message}`,
|
|
421
|
+
),
|
|
422
|
+
)
|
|
423
|
+
break
|
|
428
424
|
}
|
|
429
|
-
}
|
|
425
|
+
}
|
|
430
426
|
})
|
|
431
427
|
event.editor.onChange()
|
|
432
428
|
}
|
|
@@ -440,17 +436,15 @@ export const editorMachine = setup({
|
|
|
440
436
|
if (!behaviorOverwritten) {
|
|
441
437
|
if (defaultActionCallback) {
|
|
442
438
|
withApplyingBehaviorActions(event.editor, () => {
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
}
|
|
453
|
-
})
|
|
439
|
+
try {
|
|
440
|
+
defaultActionCallback()
|
|
441
|
+
} catch (error) {
|
|
442
|
+
console.error(
|
|
443
|
+
new Error(
|
|
444
|
+
`Performing "${event.behaviorEvent.type}" failed due to: ${error.message}`,
|
|
445
|
+
),
|
|
446
|
+
)
|
|
447
|
+
}
|
|
454
448
|
})
|
|
455
449
|
return
|
|
456
450
|
}
|
|
@@ -460,20 +454,18 @@ export const editorMachine = setup({
|
|
|
460
454
|
}
|
|
461
455
|
|
|
462
456
|
withApplyingBehaviorActions(event.editor, () => {
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
}
|
|
476
|
-
})
|
|
457
|
+
try {
|
|
458
|
+
performAction({
|
|
459
|
+
context,
|
|
460
|
+
action: defaultAction,
|
|
461
|
+
})
|
|
462
|
+
} catch (error) {
|
|
463
|
+
console.error(
|
|
464
|
+
new Error(
|
|
465
|
+
`Performing action "${defaultAction.type}" as a result of "${event.behaviorEvent.type}" failed due to: ${error.message}`,
|
|
466
|
+
),
|
|
467
|
+
)
|
|
468
|
+
}
|
|
477
469
|
})
|
|
478
470
|
event.editor.onChange()
|
|
479
471
|
}
|
|
@@ -610,6 +602,9 @@ export const editorMachine = setup({
|
|
|
610
602
|
'focus': {
|
|
611
603
|
actions: emit(({event}) => event),
|
|
612
604
|
},
|
|
605
|
+
'history.*': {
|
|
606
|
+
actions: emit(({event}) => event),
|
|
607
|
+
},
|
|
613
608
|
'insert.*': {
|
|
614
609
|
actions: emit(({event}) => event),
|
|
615
610
|
},
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {Editor} from 'slate'
|
|
2
2
|
import {insertSoftBreakActionImplementation} from '../../behavior-actions/behavior.action.insert-break'
|
|
3
|
+
import {performAction} from '../../behavior-actions/behavior.actions'
|
|
3
4
|
import {toPortableTextRange} from '../../internal-utils/ranges'
|
|
4
5
|
import {fromSlateValue} from '../../internal-utils/values'
|
|
5
6
|
import {KEY_TO_VALUE_ELEMENT} from '../../internal-utils/weakMaps'
|
|
@@ -179,6 +180,31 @@ export function createWithEventListeners(
|
|
|
179
180
|
return
|
|
180
181
|
}
|
|
181
182
|
|
|
183
|
+
editor.redo = () => {
|
|
184
|
+
if (isApplyingBehaviorActions(editor)) {
|
|
185
|
+
performAction({
|
|
186
|
+
context: {
|
|
187
|
+
keyGenerator: editorActor.getSnapshot().context.keyGenerator,
|
|
188
|
+
schema: editorActor.getSnapshot().context.schema,
|
|
189
|
+
},
|
|
190
|
+
action: {
|
|
191
|
+
type: 'history.redo',
|
|
192
|
+
editor,
|
|
193
|
+
},
|
|
194
|
+
})
|
|
195
|
+
return
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
editorActor.send({
|
|
199
|
+
type: 'behavior event',
|
|
200
|
+
behaviorEvent: {
|
|
201
|
+
type: 'history.redo',
|
|
202
|
+
},
|
|
203
|
+
editor,
|
|
204
|
+
})
|
|
205
|
+
return
|
|
206
|
+
}
|
|
207
|
+
|
|
182
208
|
editor.select = (location) => {
|
|
183
209
|
if (isApplyingBehaviorActions(editor)) {
|
|
184
210
|
select(location)
|
|
@@ -234,6 +260,31 @@ export function createWithEventListeners(
|
|
|
234
260
|
return
|
|
235
261
|
}
|
|
236
262
|
|
|
263
|
+
editor.undo = () => {
|
|
264
|
+
if (isApplyingBehaviorActions(editor)) {
|
|
265
|
+
performAction({
|
|
266
|
+
context: {
|
|
267
|
+
keyGenerator: editorActor.getSnapshot().context.keyGenerator,
|
|
268
|
+
schema: editorActor.getSnapshot().context.schema,
|
|
269
|
+
},
|
|
270
|
+
action: {
|
|
271
|
+
type: 'history.undo',
|
|
272
|
+
editor,
|
|
273
|
+
},
|
|
274
|
+
})
|
|
275
|
+
return
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
editorActor.send({
|
|
279
|
+
type: 'behavior event',
|
|
280
|
+
behaviorEvent: {
|
|
281
|
+
type: 'history.undo',
|
|
282
|
+
},
|
|
283
|
+
editor,
|
|
284
|
+
})
|
|
285
|
+
return
|
|
286
|
+
}
|
|
287
|
+
|
|
237
288
|
return editor
|
|
238
289
|
}
|
|
239
290
|
}
|
|
@@ -111,8 +111,24 @@ export function createEditableAPI(
|
|
|
111
111
|
}.marks || []
|
|
112
112
|
)
|
|
113
113
|
},
|
|
114
|
-
undo: (): void =>
|
|
115
|
-
|
|
114
|
+
undo: (): void => {
|
|
115
|
+
editorActor.send({
|
|
116
|
+
type: 'behavior event',
|
|
117
|
+
behaviorEvent: {
|
|
118
|
+
type: 'history.undo',
|
|
119
|
+
},
|
|
120
|
+
editor,
|
|
121
|
+
})
|
|
122
|
+
},
|
|
123
|
+
redo: (): void => {
|
|
124
|
+
editorActor.send({
|
|
125
|
+
type: 'behavior event',
|
|
126
|
+
behaviorEvent: {
|
|
127
|
+
type: 'history.redo',
|
|
128
|
+
},
|
|
129
|
+
editor,
|
|
130
|
+
})
|
|
131
|
+
},
|
|
116
132
|
select: (selection: EditorSelection): void => {
|
|
117
133
|
const slateSelection = toSlateRange(selection, editor)
|
|
118
134
|
if (slateSelection) {
|