@portabletext/editor 1.1.12-canary.2 → 1.1.12-canary.3
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/index.esm.js +179 -206
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +179 -206
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +179 -206
- package/lib/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/editor/Editable.tsx +82 -84
- package/src/editor/behavior/behavior.core.block-objects.ts +106 -0
- package/src/editor/behavior/behavior.core.lists.ts +76 -0
- package/src/editor/behavior/behavior.core.ts +4 -102
- package/src/editor/components/Synchronizer.tsx +22 -20
- package/src/editor/withSyncRangeDecorations.tsx +0 -20
package/package.json
CHANGED
package/src/editor/Editable.tsx
CHANGED
|
@@ -22,7 +22,6 @@ import {
|
|
|
22
22
|
Path,
|
|
23
23
|
Range as SlateRange,
|
|
24
24
|
Transforms,
|
|
25
|
-
type BaseEditor,
|
|
26
25
|
type BaseRange,
|
|
27
26
|
type NodeEntry,
|
|
28
27
|
type Operation,
|
|
@@ -39,7 +38,6 @@ import type {
|
|
|
39
38
|
EditorSelection,
|
|
40
39
|
OnCopyFn,
|
|
41
40
|
OnPasteFn,
|
|
42
|
-
PortableTextSlateEditor,
|
|
43
41
|
RangeDecoration,
|
|
44
42
|
RenderAnnotationFunction,
|
|
45
43
|
RenderBlockFunction,
|
|
@@ -71,7 +69,6 @@ import {usePortableTextEditor} from './hooks/usePortableTextEditor'
|
|
|
71
69
|
import {usePortableTextEditorReadOnlyStatus} from './hooks/usePortableTextReadOnly'
|
|
72
70
|
import {createWithHotkeys, createWithInsertData} from './plugins'
|
|
73
71
|
import {PortableTextEditor} from './PortableTextEditor'
|
|
74
|
-
import {withSyncRangeDecorations} from './withSyncRangeDecorations'
|
|
75
72
|
|
|
76
73
|
const debug = debugWithName('component:Editable')
|
|
77
74
|
|
|
@@ -166,29 +163,27 @@ export const PortableTextEditable = forwardRef<
|
|
|
166
163
|
|
|
167
164
|
const blockTypeName = schemaTypes.block.name
|
|
168
165
|
|
|
166
|
+
// React/UI-specific plugins
|
|
167
|
+
const withInsertData = useMemo(
|
|
168
|
+
() => createWithInsertData(editorActor, schemaTypes),
|
|
169
|
+
[editorActor, schemaTypes],
|
|
170
|
+
)
|
|
171
|
+
const withHotKeys = useMemo(
|
|
172
|
+
() => createWithHotkeys(portableTextEditor, hotkeys),
|
|
173
|
+
[hotkeys, portableTextEditor],
|
|
174
|
+
)
|
|
175
|
+
|
|
169
176
|
// Output a minimal React editor inside Editable when in readOnly mode.
|
|
170
177
|
// NOTE: make sure all the plugins used here can be safely run over again at any point.
|
|
171
178
|
// There will be a problem if they redefine editor methods and then calling the original method within themselves.
|
|
172
179
|
useMemo(() => {
|
|
173
|
-
// React/UI-specific plugins
|
|
174
|
-
const withInsertData = createWithInsertData(editorActor, schemaTypes)
|
|
175
|
-
|
|
176
180
|
if (readOnly) {
|
|
177
181
|
debug('Editable is in read only mode')
|
|
178
182
|
return withInsertData(slateEditor)
|
|
179
183
|
}
|
|
180
|
-
const withHotKeys = createWithHotkeys(portableTextEditor, hotkeys)
|
|
181
|
-
|
|
182
184
|
debug('Editable is in edit mode')
|
|
183
185
|
return withInsertData(withHotKeys(slateEditor))
|
|
184
|
-
}, [
|
|
185
|
-
editorActor,
|
|
186
|
-
hotkeys,
|
|
187
|
-
portableTextEditor,
|
|
188
|
-
readOnly,
|
|
189
|
-
slateEditor,
|
|
190
|
-
schemaTypes,
|
|
191
|
-
])
|
|
186
|
+
}, [readOnly, slateEditor, withHotKeys, withInsertData])
|
|
192
187
|
|
|
193
188
|
const renderElement = useCallback(
|
|
194
189
|
(eProps: RenderElementProps) => (
|
|
@@ -386,6 +381,9 @@ export const PortableTextEditable = forwardRef<
|
|
|
386
381
|
}
|
|
387
382
|
}, [hasInvalidValue, propsSelection, restoreSelectionFromProps])
|
|
388
383
|
|
|
384
|
+
// Store reference to original apply function (see below for usage in useEffect)
|
|
385
|
+
const originalApply = useMemo(() => slateEditor.apply, [slateEditor])
|
|
386
|
+
|
|
389
387
|
const [syncedRangeDecorations, setSyncedRangeDecorations] = useState(false)
|
|
390
388
|
useEffect(() => {
|
|
391
389
|
if (!syncedRangeDecorations) {
|
|
@@ -404,9 +402,16 @@ export const PortableTextEditable = forwardRef<
|
|
|
404
402
|
|
|
405
403
|
// Sync range decorations after an operation is applied
|
|
406
404
|
useEffect(() => {
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
405
|
+
slateEditor.apply = (op: Operation) => {
|
|
406
|
+
originalApply(op)
|
|
407
|
+
if (op.type !== 'set_selection') {
|
|
408
|
+
syncRangeDecorations(op)
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
return () => {
|
|
412
|
+
slateEditor.apply = originalApply
|
|
413
|
+
}
|
|
414
|
+
}, [originalApply, slateEditor, syncRangeDecorations])
|
|
410
415
|
|
|
411
416
|
// Handle from props onCopy function
|
|
412
417
|
const handleCopy = useCallback(
|
|
@@ -555,7 +560,64 @@ export const PortableTextEditable = forwardRef<
|
|
|
555
560
|
[onBeforeInput],
|
|
556
561
|
)
|
|
557
562
|
|
|
558
|
-
|
|
563
|
+
// This function will handle unexpected DOM changes inside the Editable rendering,
|
|
564
|
+
// and make sure that we can maintain a stable slateEditor.selection when that happens.
|
|
565
|
+
//
|
|
566
|
+
// For example, if this Editable is rendered inside something that might re-render
|
|
567
|
+
// this component (hidden contexts) while the user is still actively changing the
|
|
568
|
+
// contentEditable, this could interfere with the intermediate DOM selection,
|
|
569
|
+
// which again could be picked up by ReactEditor's event listeners.
|
|
570
|
+
// If that range is invalid at that point, the slate.editorSelection could be
|
|
571
|
+
// set either wrong, or invalid, to which slateEditor will throw exceptions
|
|
572
|
+
// that are impossible to recover properly from or result in a wrong selection.
|
|
573
|
+
//
|
|
574
|
+
// Also the other way around, when the ReactEditor will try to create a DOM Range
|
|
575
|
+
// from the current slateEditor.selection, it may throw unrecoverable errors
|
|
576
|
+
// if the current editor.selection is invalid according to the DOM.
|
|
577
|
+
// If this is the case, default to selecting the top of the document, if the
|
|
578
|
+
// user already had a selection.
|
|
579
|
+
const validateSelection = useCallback(() => {
|
|
580
|
+
if (!slateEditor.selection) {
|
|
581
|
+
return
|
|
582
|
+
}
|
|
583
|
+
const root = ReactEditor.findDocumentOrShadowRoot(slateEditor)
|
|
584
|
+
const {activeElement} = root
|
|
585
|
+
// Return if the editor isn't the active element
|
|
586
|
+
if (ref.current !== activeElement) {
|
|
587
|
+
return
|
|
588
|
+
}
|
|
589
|
+
const window = ReactEditor.getWindow(slateEditor)
|
|
590
|
+
const domSelection = window.getSelection()
|
|
591
|
+
if (!domSelection || domSelection.rangeCount === 0) {
|
|
592
|
+
return
|
|
593
|
+
}
|
|
594
|
+
const existingDOMRange = domSelection.getRangeAt(0)
|
|
595
|
+
try {
|
|
596
|
+
const newDOMRange = ReactEditor.toDOMRange(
|
|
597
|
+
slateEditor,
|
|
598
|
+
slateEditor.selection,
|
|
599
|
+
)
|
|
600
|
+
if (
|
|
601
|
+
newDOMRange.startOffset !== existingDOMRange.startOffset ||
|
|
602
|
+
newDOMRange.endOffset !== existingDOMRange.endOffset
|
|
603
|
+
) {
|
|
604
|
+
debug('DOM range out of sync, validating selection')
|
|
605
|
+
// Remove all ranges temporary
|
|
606
|
+
domSelection?.removeAllRanges()
|
|
607
|
+
// Set the correct range
|
|
608
|
+
domSelection.addRange(newDOMRange)
|
|
609
|
+
}
|
|
610
|
+
} catch {
|
|
611
|
+
debug(`Could not resolve selection, selecting top document`)
|
|
612
|
+
// Deselect the editor
|
|
613
|
+
Transforms.deselect(slateEditor)
|
|
614
|
+
// Select top document if there is a top block to select
|
|
615
|
+
if (slateEditor.children.length > 0) {
|
|
616
|
+
Transforms.select(slateEditor, [0, 0])
|
|
617
|
+
}
|
|
618
|
+
slateEditor.onChange()
|
|
619
|
+
}
|
|
620
|
+
}, [ref, slateEditor])
|
|
559
621
|
|
|
560
622
|
// Observe mutations (child list and subtree) to this component's DOM,
|
|
561
623
|
// and make sure the editor selection is valid when that happens.
|
|
@@ -691,67 +753,3 @@ export const PortableTextEditable = forwardRef<
|
|
|
691
753
|
})
|
|
692
754
|
|
|
693
755
|
PortableTextEditable.displayName = 'ForwardRef(PortableTextEditable)'
|
|
694
|
-
|
|
695
|
-
function useValidateSelection(
|
|
696
|
-
ref: MutableRefObject<HTMLDivElement | null>,
|
|
697
|
-
slateEditor: BaseEditor & ReactEditor & PortableTextSlateEditor,
|
|
698
|
-
) {
|
|
699
|
-
// This function will handle unexpected DOM changes inside the Editable rendering,
|
|
700
|
-
// and make sure that we can maintain a stable slateEditor.selection when that happens.
|
|
701
|
-
//
|
|
702
|
-
// For example, if this Editable is rendered inside something that might re-render
|
|
703
|
-
// this component (hidden contexts) while the user is still actively changing the
|
|
704
|
-
// contentEditable, this could interfere with the intermediate DOM selection,
|
|
705
|
-
// which again could be picked up by ReactEditor's event listeners.
|
|
706
|
-
// If that range is invalid at that point, the slate.editorSelection could be
|
|
707
|
-
// set either wrong, or invalid, to which slateEditor will throw exceptions
|
|
708
|
-
// that are impossible to recover properly from or result in a wrong selection.
|
|
709
|
-
//
|
|
710
|
-
// Also the other way around, when the ReactEditor will try to create a DOM Range
|
|
711
|
-
// from the current slateEditor.selection, it may throw unrecoverable errors
|
|
712
|
-
// if the current editor.selection is invalid according to the DOM.
|
|
713
|
-
// If this is the case, default to selecting the top of the document, if the
|
|
714
|
-
// user already had a selection.
|
|
715
|
-
return useCallback(() => {
|
|
716
|
-
if (!slateEditor.selection) {
|
|
717
|
-
return
|
|
718
|
-
}
|
|
719
|
-
const root = ReactEditor.findDocumentOrShadowRoot(slateEditor)
|
|
720
|
-
const {activeElement} = root
|
|
721
|
-
// Return if the editor isn't the active element
|
|
722
|
-
if (ref.current !== activeElement) {
|
|
723
|
-
return
|
|
724
|
-
}
|
|
725
|
-
const window = ReactEditor.getWindow(slateEditor)
|
|
726
|
-
const domSelection = window.getSelection()
|
|
727
|
-
if (!domSelection || domSelection.rangeCount === 0) {
|
|
728
|
-
return
|
|
729
|
-
}
|
|
730
|
-
const existingDOMRange = domSelection.getRangeAt(0)
|
|
731
|
-
try {
|
|
732
|
-
const newDOMRange = ReactEditor.toDOMRange(
|
|
733
|
-
slateEditor,
|
|
734
|
-
slateEditor.selection,
|
|
735
|
-
)
|
|
736
|
-
if (
|
|
737
|
-
newDOMRange.startOffset !== existingDOMRange.startOffset ||
|
|
738
|
-
newDOMRange.endOffset !== existingDOMRange.endOffset
|
|
739
|
-
) {
|
|
740
|
-
debug('DOM range out of sync, validating selection')
|
|
741
|
-
// Remove all ranges temporary
|
|
742
|
-
domSelection?.removeAllRanges()
|
|
743
|
-
// Set the correct range
|
|
744
|
-
domSelection.addRange(newDOMRange)
|
|
745
|
-
}
|
|
746
|
-
} catch {
|
|
747
|
-
debug(`Could not resolve selection, selecting top document`)
|
|
748
|
-
// Deselect the editor
|
|
749
|
-
Transforms.deselect(slateEditor)
|
|
750
|
-
// Select top document if there is a top block to select
|
|
751
|
-
if (slateEditor.children.length > 0) {
|
|
752
|
-
Transforms.select(slateEditor, [0, 0])
|
|
753
|
-
}
|
|
754
|
-
slateEditor.onChange()
|
|
755
|
-
}
|
|
756
|
-
}, [ref, slateEditor])
|
|
757
|
-
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import {isPortableTextTextBlock} from '@sanity/types'
|
|
2
|
+
import {defineBehavior} from './behavior.types'
|
|
3
|
+
import {
|
|
4
|
+
getFocusBlockObject,
|
|
5
|
+
getFocusTextBlock,
|
|
6
|
+
getNextBlock,
|
|
7
|
+
getPreviousBlock,
|
|
8
|
+
isEmptyTextBlock,
|
|
9
|
+
selectionIsCollapsed,
|
|
10
|
+
} from './behavior.utils'
|
|
11
|
+
|
|
12
|
+
const breakingVoidBlock = defineBehavior({
|
|
13
|
+
on: 'insert break',
|
|
14
|
+
guard: ({context}) => {
|
|
15
|
+
const focusBlockObject = getFocusBlockObject(context)
|
|
16
|
+
|
|
17
|
+
return !!focusBlockObject
|
|
18
|
+
},
|
|
19
|
+
actions: [() => [{type: 'insert text block', decorators: []}]],
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
const deletingEmptyTextBlockAfterBlockObject = defineBehavior({
|
|
23
|
+
on: 'delete backward',
|
|
24
|
+
guard: ({context}) => {
|
|
25
|
+
const focusTextBlock = getFocusTextBlock(context)
|
|
26
|
+
const selectionCollapsed = selectionIsCollapsed(context)
|
|
27
|
+
const previousBlock = getPreviousBlock(context)
|
|
28
|
+
|
|
29
|
+
if (!focusTextBlock || !selectionCollapsed || !previousBlock) {
|
|
30
|
+
return false
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (
|
|
34
|
+
isEmptyTextBlock(focusTextBlock.node) &&
|
|
35
|
+
!isPortableTextTextBlock(previousBlock.node)
|
|
36
|
+
) {
|
|
37
|
+
return {focusTextBlock, previousBlock}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return false
|
|
41
|
+
},
|
|
42
|
+
actions: [
|
|
43
|
+
(_, {focusTextBlock, previousBlock}) => [
|
|
44
|
+
{
|
|
45
|
+
type: 'delete',
|
|
46
|
+
selection: {
|
|
47
|
+
anchor: {path: focusTextBlock.path, offset: 0},
|
|
48
|
+
focus: {path: focusTextBlock.path, offset: 0},
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
type: 'select',
|
|
53
|
+
selection: {
|
|
54
|
+
anchor: {path: previousBlock.path, offset: 0},
|
|
55
|
+
focus: {path: previousBlock.path, offset: 0},
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
],
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
const deletingEmptyTextBlockBeforeBlockObject = defineBehavior({
|
|
63
|
+
on: 'delete forward',
|
|
64
|
+
guard: ({context}) => {
|
|
65
|
+
const focusTextBlock = getFocusTextBlock(context)
|
|
66
|
+
const selectionCollapsed = selectionIsCollapsed(context)
|
|
67
|
+
const nextBlock = getNextBlock(context)
|
|
68
|
+
|
|
69
|
+
if (!focusTextBlock || !selectionCollapsed || !nextBlock) {
|
|
70
|
+
return false
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (
|
|
74
|
+
isEmptyTextBlock(focusTextBlock.node) &&
|
|
75
|
+
!isPortableTextTextBlock(nextBlock.node)
|
|
76
|
+
) {
|
|
77
|
+
return {focusTextBlock, nextBlock}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return false
|
|
81
|
+
},
|
|
82
|
+
actions: [
|
|
83
|
+
(_, {focusTextBlock, nextBlock}) => [
|
|
84
|
+
{
|
|
85
|
+
type: 'delete',
|
|
86
|
+
selection: {
|
|
87
|
+
anchor: {path: focusTextBlock.path, offset: 0},
|
|
88
|
+
focus: {path: focusTextBlock.path, offset: 0},
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
type: 'select',
|
|
93
|
+
selection: {
|
|
94
|
+
anchor: {path: nextBlock.path, offset: 0},
|
|
95
|
+
focus: {path: nextBlock.path, offset: 0},
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
],
|
|
99
|
+
],
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
export const coreBlockObjectBehaviors = [
|
|
103
|
+
breakingVoidBlock,
|
|
104
|
+
deletingEmptyTextBlockAfterBlockObject,
|
|
105
|
+
deletingEmptyTextBlockBeforeBlockObject,
|
|
106
|
+
]
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import {defineBehavior} from './behavior.types'
|
|
2
|
+
import {
|
|
3
|
+
getFocusSpan,
|
|
4
|
+
getFocusTextBlock,
|
|
5
|
+
selectionIsCollapsed,
|
|
6
|
+
} from './behavior.utils'
|
|
7
|
+
|
|
8
|
+
const clearListOnBackspace = defineBehavior({
|
|
9
|
+
on: 'delete backward',
|
|
10
|
+
guard: ({context}) => {
|
|
11
|
+
const selectionCollapsed = selectionIsCollapsed(context)
|
|
12
|
+
const focusTextBlock = getFocusTextBlock(context)
|
|
13
|
+
const focusSpan = getFocusSpan(context)
|
|
14
|
+
|
|
15
|
+
if (!selectionCollapsed || !focusTextBlock || !focusSpan) {
|
|
16
|
+
return false
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const atTheBeginningOfBLock =
|
|
20
|
+
focusTextBlock.node.children[0]._key === focusSpan.node._key &&
|
|
21
|
+
context.selection.focus.offset === 0
|
|
22
|
+
|
|
23
|
+
if (atTheBeginningOfBLock && focusTextBlock.node.level === 1) {
|
|
24
|
+
return {focusTextBlock}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return false
|
|
28
|
+
},
|
|
29
|
+
actions: [
|
|
30
|
+
(_, {focusTextBlock}) => [
|
|
31
|
+
{
|
|
32
|
+
type: 'unset block',
|
|
33
|
+
props: ['listItem', 'level'],
|
|
34
|
+
paths: [focusTextBlock.path],
|
|
35
|
+
},
|
|
36
|
+
],
|
|
37
|
+
],
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
const unindentListOnBackspace = defineBehavior({
|
|
41
|
+
on: 'delete backward',
|
|
42
|
+
guard: ({context}) => {
|
|
43
|
+
const selectionCollapsed = selectionIsCollapsed(context)
|
|
44
|
+
const focusTextBlock = getFocusTextBlock(context)
|
|
45
|
+
const focusSpan = getFocusSpan(context)
|
|
46
|
+
|
|
47
|
+
if (!selectionCollapsed || !focusTextBlock || !focusSpan) {
|
|
48
|
+
return false
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const atTheBeginningOfBLock =
|
|
52
|
+
focusTextBlock.node.children[0]._key === focusSpan.node._key &&
|
|
53
|
+
context.selection.focus.offset === 0
|
|
54
|
+
|
|
55
|
+
if (
|
|
56
|
+
atTheBeginningOfBLock &&
|
|
57
|
+
focusTextBlock.node.level !== undefined &&
|
|
58
|
+
focusTextBlock.node.level > 1
|
|
59
|
+
) {
|
|
60
|
+
return {focusTextBlock, level: focusTextBlock.node.level - 1}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return false
|
|
64
|
+
},
|
|
65
|
+
actions: [
|
|
66
|
+
(_, {focusTextBlock, level}) => [
|
|
67
|
+
{
|
|
68
|
+
type: 'set block',
|
|
69
|
+
level,
|
|
70
|
+
paths: [focusTextBlock.path],
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
],
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
export const coreListBehaviors = [clearListOnBackspace, unindentListOnBackspace]
|
|
@@ -1,112 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {coreBlockObjectBehaviors} from './behavior.core.block-objects'
|
|
2
|
+
import {coreListBehaviors} from './behavior.core.lists'
|
|
2
3
|
import {defineBehavior} from './behavior.types'
|
|
3
|
-
import {
|
|
4
|
-
getFocusBlockObject,
|
|
5
|
-
getFocusTextBlock,
|
|
6
|
-
getNextBlock,
|
|
7
|
-
getPreviousBlock,
|
|
8
|
-
isEmptyTextBlock,
|
|
9
|
-
selectionIsCollapsed,
|
|
10
|
-
} from './behavior.utils'
|
|
11
4
|
|
|
12
5
|
const softReturn = defineBehavior({
|
|
13
6
|
on: 'insert soft break',
|
|
14
7
|
actions: [() => [{type: 'insert text', text: '\n'}]],
|
|
15
8
|
})
|
|
16
9
|
|
|
17
|
-
const breakingVoidBlock = defineBehavior({
|
|
18
|
-
on: 'insert break',
|
|
19
|
-
guard: ({context}) => {
|
|
20
|
-
const focusBlockObject = getFocusBlockObject(context)
|
|
21
|
-
|
|
22
|
-
return !!focusBlockObject
|
|
23
|
-
},
|
|
24
|
-
actions: [() => [{type: 'insert text block', decorators: []}]],
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
const deletingEmptyTextBlockAfterBlockObject = defineBehavior({
|
|
28
|
-
on: 'delete backward',
|
|
29
|
-
guard: ({context}) => {
|
|
30
|
-
const focusTextBlock = getFocusTextBlock(context)
|
|
31
|
-
const selectionCollapsed = selectionIsCollapsed(context)
|
|
32
|
-
const previousBlock = getPreviousBlock(context)
|
|
33
|
-
|
|
34
|
-
if (!focusTextBlock || !selectionCollapsed || !previousBlock) {
|
|
35
|
-
return false
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (
|
|
39
|
-
isEmptyTextBlock(focusTextBlock.node) &&
|
|
40
|
-
!isPortableTextTextBlock(previousBlock.node)
|
|
41
|
-
) {
|
|
42
|
-
return {focusTextBlock, previousBlock}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return false
|
|
46
|
-
},
|
|
47
|
-
actions: [
|
|
48
|
-
(_, {focusTextBlock, previousBlock}) => [
|
|
49
|
-
{
|
|
50
|
-
type: 'delete',
|
|
51
|
-
selection: {
|
|
52
|
-
anchor: {path: focusTextBlock.path, offset: 0},
|
|
53
|
-
focus: {path: focusTextBlock.path, offset: 0},
|
|
54
|
-
},
|
|
55
|
-
},
|
|
56
|
-
{
|
|
57
|
-
type: 'select',
|
|
58
|
-
selection: {
|
|
59
|
-
anchor: {path: previousBlock.path, offset: 0},
|
|
60
|
-
focus: {path: previousBlock.path, offset: 0},
|
|
61
|
-
},
|
|
62
|
-
},
|
|
63
|
-
],
|
|
64
|
-
],
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
const deletingEmptyTextBlockBeforeBlockObject = defineBehavior({
|
|
68
|
-
on: 'delete forward',
|
|
69
|
-
guard: ({context}) => {
|
|
70
|
-
const focusTextBlock = getFocusTextBlock(context)
|
|
71
|
-
const selectionCollapsed = selectionIsCollapsed(context)
|
|
72
|
-
const nextBlock = getNextBlock(context)
|
|
73
|
-
|
|
74
|
-
if (!focusTextBlock || !selectionCollapsed || !nextBlock) {
|
|
75
|
-
return false
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (
|
|
79
|
-
isEmptyTextBlock(focusTextBlock.node) &&
|
|
80
|
-
!isPortableTextTextBlock(nextBlock.node)
|
|
81
|
-
) {
|
|
82
|
-
return {focusTextBlock, nextBlock}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return false
|
|
86
|
-
},
|
|
87
|
-
actions: [
|
|
88
|
-
(_, {focusTextBlock, nextBlock}) => [
|
|
89
|
-
{
|
|
90
|
-
type: 'delete',
|
|
91
|
-
selection: {
|
|
92
|
-
anchor: {path: focusTextBlock.path, offset: 0},
|
|
93
|
-
focus: {path: focusTextBlock.path, offset: 0},
|
|
94
|
-
},
|
|
95
|
-
},
|
|
96
|
-
{
|
|
97
|
-
type: 'select',
|
|
98
|
-
selection: {
|
|
99
|
-
anchor: {path: nextBlock.path, offset: 0},
|
|
100
|
-
focus: {path: nextBlock.path, offset: 0},
|
|
101
|
-
},
|
|
102
|
-
},
|
|
103
|
-
],
|
|
104
|
-
],
|
|
105
|
-
})
|
|
106
|
-
|
|
107
10
|
export const coreBehaviors = [
|
|
108
11
|
softReturn,
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
deletingEmptyTextBlockBeforeBlockObject,
|
|
12
|
+
...coreBlockObjectBehaviors,
|
|
13
|
+
...coreListBehaviors,
|
|
112
14
|
]
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type {Patch} from '@portabletext/patches'
|
|
2
2
|
import type {PortableTextBlock} from '@sanity/types'
|
|
3
3
|
import {throttle} from 'lodash'
|
|
4
|
-
import {useCallback, useEffect, useRef} from 'react'
|
|
4
|
+
import {useCallback, useEffect, useMemo, useRef} from 'react'
|
|
5
5
|
import {Editor} from 'slate'
|
|
6
6
|
import {useSlate} from 'slate-react'
|
|
7
7
|
import {useEffectEvent} from 'use-effect-event'
|
|
@@ -69,24 +69,8 @@ export function Synchronizer(props: SynchronizerProps) {
|
|
|
69
69
|
IS_PROCESSING_LOCAL_CHANGES.set(slateEditor, false)
|
|
70
70
|
}, [editorActor, slateEditor, getValue])
|
|
71
71
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
return () => {
|
|
75
|
-
onFlushPendingPatches()
|
|
76
|
-
}
|
|
77
|
-
}, [onFlushPendingPatches])
|
|
78
|
-
|
|
79
|
-
// We want to ensure that _when_ `props.onChange` is called, it uses the current value.
|
|
80
|
-
// But we don't want to have the `useEffect` run setup + teardown + setup every time the prop might change, as that's unnecessary.
|
|
81
|
-
// So we use our own polyfill that lets us use an upcoming React hook that solves this exact problem.
|
|
82
|
-
// https://19.react.dev/learn/separating-events-from-effects#declaring-an-effect-event
|
|
83
|
-
const handleChange = useEffectEvent((change: EditorChange) =>
|
|
84
|
-
onChange(change),
|
|
85
|
-
)
|
|
86
|
-
|
|
87
|
-
// Subscribe to, and handle changes from the editor
|
|
88
|
-
useEffect(() => {
|
|
89
|
-
const onFlushPendingPatchesThrottled = throttle(
|
|
72
|
+
const onFlushPendingPatchesThrottled = useMemo(() => {
|
|
73
|
+
return throttle(
|
|
90
74
|
() => {
|
|
91
75
|
// If the editor is normalizing (each operation) it means that it's not in the middle of a bigger transform,
|
|
92
76
|
// and we can flush these changes immediately.
|
|
@@ -103,7 +87,25 @@ export function Synchronizer(props: SynchronizerProps) {
|
|
|
103
87
|
trailing: true,
|
|
104
88
|
},
|
|
105
89
|
)
|
|
90
|
+
}, [onFlushPendingPatches, slateEditor])
|
|
106
91
|
|
|
92
|
+
// Flush pending patches immediately on unmount
|
|
93
|
+
useEffect(() => {
|
|
94
|
+
return () => {
|
|
95
|
+
onFlushPendingPatches()
|
|
96
|
+
}
|
|
97
|
+
}, [onFlushPendingPatches])
|
|
98
|
+
|
|
99
|
+
// We want to ensure that _when_ `props.onChange` is called, it uses the current value.
|
|
100
|
+
// But we don't want to have the `useEffect` run setup + teardown + setup every time the prop might change, as that's unnecessary.
|
|
101
|
+
// So we use our own polyfill that lets us use an upcoming React hook that solves this exact problem.
|
|
102
|
+
// https://19.react.dev/learn/separating-events-from-effects#declaring-an-effect-event
|
|
103
|
+
const handleChange = useEffectEvent((change: EditorChange) =>
|
|
104
|
+
onChange(change),
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
// Subscribe to, and handle changes from the editor
|
|
108
|
+
useEffect(() => {
|
|
107
109
|
debug('Subscribing to editor changes')
|
|
108
110
|
const sub = editorActor.on('*', (event) => {
|
|
109
111
|
switch (event.type) {
|
|
@@ -156,7 +158,7 @@ export function Synchronizer(props: SynchronizerProps) {
|
|
|
156
158
|
debug('Unsubscribing to changes')
|
|
157
159
|
sub.unsubscribe()
|
|
158
160
|
}
|
|
159
|
-
}, [
|
|
161
|
+
}, [handleChange, editorActor, onFlushPendingPatchesThrottled, slateEditor])
|
|
160
162
|
|
|
161
163
|
// Sync the value when going online
|
|
162
164
|
const handleOnline = useCallback(() => {
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type {BaseEditor, Operation} from 'slate'
|
|
2
|
-
import type {ReactEditor} from 'slate-react'
|
|
3
|
-
import type {PortableTextSlateEditor} from '../types/editor'
|
|
4
|
-
|
|
5
|
-
// React Compiler considers `slateEditor` as immutable, and opts-out if we do this inline in a useEffect, doing it in a function moves it out of the scope, and opts-in again for the rest of the component.
|
|
6
|
-
export function withSyncRangeDecorations(
|
|
7
|
-
slateEditor: BaseEditor & ReactEditor & PortableTextSlateEditor,
|
|
8
|
-
syncRangeDecorations: (operation?: Operation) => void,
|
|
9
|
-
) {
|
|
10
|
-
const originalApply = slateEditor.apply
|
|
11
|
-
slateEditor.apply = (op: Operation) => {
|
|
12
|
-
originalApply(op)
|
|
13
|
-
if (op.type !== 'set_selection') {
|
|
14
|
-
syncRangeDecorations(op)
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
return () => {
|
|
18
|
-
slateEditor.apply = originalApply
|
|
19
|
-
}
|
|
20
|
-
}
|