@portabletext/editor 3.3.2 → 3.3.4
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-dts/index.d.ts +7 -1
- package/package.json +16 -19
- package/src/behaviors/_exports/index.ts +0 -1
- package/src/behaviors/behavior.abstract.annotation.ts +0 -77
- package/src/behaviors/behavior.abstract.decorator.ts +0 -39
- package/src/behaviors/behavior.abstract.delete.ts +0 -273
- package/src/behaviors/behavior.abstract.deserialize.ts +0 -232
- package/src/behaviors/behavior.abstract.insert.ts +0 -525
- package/src/behaviors/behavior.abstract.keyboard.ts +0 -189
- package/src/behaviors/behavior.abstract.list-item.ts +0 -70
- package/src/behaviors/behavior.abstract.move.ts +0 -79
- package/src/behaviors/behavior.abstract.select.ts +0 -118
- package/src/behaviors/behavior.abstract.serialize.ts +0 -96
- package/src/behaviors/behavior.abstract.split.ts +0 -170
- package/src/behaviors/behavior.abstract.style.ts +0 -55
- package/src/behaviors/behavior.abstract.ts +0 -139
- package/src/behaviors/behavior.config.ts +0 -7
- package/src/behaviors/behavior.core.annotations.ts +0 -62
- package/src/behaviors/behavior.core.block-element.ts +0 -116
- package/src/behaviors/behavior.core.block-objects.ts +0 -285
- package/src/behaviors/behavior.core.decorators.ts +0 -44
- package/src/behaviors/behavior.core.dnd.ts +0 -356
- package/src/behaviors/behavior.core.insert-break.ts +0 -266
- package/src/behaviors/behavior.core.insert.ts +0 -52
- package/src/behaviors/behavior.core.lists.ts +0 -691
- package/src/behaviors/behavior.core.ts +0 -44
- package/src/behaviors/behavior.perform-event.ts +0 -354
- package/src/behaviors/behavior.types.action.ts +0 -102
- package/src/behaviors/behavior.types.behavior.ts +0 -83
- package/src/behaviors/behavior.types.event.ts +0 -667
- package/src/behaviors/behavior.types.guard.ts +0 -11
- package/src/behaviors/index.ts +0 -17
- package/src/converters/converter.json.ts +0 -53
- package/src/converters/converter.portable-text.deserialize.test.ts +0 -680
- package/src/converters/converter.portable-text.ts +0 -75
- package/src/converters/converter.text-html.deserialize.test.ts +0 -406
- package/src/converters/converter.text-html.serialize.test.ts +0 -246
- package/src/converters/converter.text-html.ts +0 -87
- package/src/converters/converter.text-markdown.ts +0 -67
- package/src/converters/converter.text-plain.test.ts +0 -245
- package/src/converters/converter.text-plain.ts +0 -126
- package/src/converters/converter.types.ts +0 -72
- package/src/converters/converters.core.ts +0 -18
- package/src/editor/Editable.tsx +0 -1012
- package/src/editor/PortableTextEditor.tsx +0 -791
- package/src/editor/components/drop-indicator.tsx +0 -17
- package/src/editor/components/render-block-object.tsx +0 -121
- package/src/editor/components/render-default-object.tsx +0 -21
- package/src/editor/components/render-element.tsx +0 -88
- package/src/editor/components/render-inline-object.tsx +0 -129
- package/src/editor/components/render-leaf.tsx +0 -64
- package/src/editor/components/render-span.tsx +0 -303
- package/src/editor/components/render-text-block.tsx +0 -265
- package/src/editor/components/render-text.tsx +0 -18
- package/src/editor/components/use-core-block-element-behaviors.ts +0 -39
- package/src/editor/create-editor.ts +0 -323
- package/src/editor/create-slate-editor.tsx +0 -64
- package/src/editor/editor-actor-context.ts +0 -4
- package/src/editor/editor-context.tsx +0 -7
- package/src/editor/editor-dom.ts +0 -220
- package/src/editor/editor-machine.ts +0 -751
- package/src/editor/editor-provider.tsx +0 -111
- package/src/editor/editor-schema.ts +0 -6
- package/src/editor/editor-selector.ts +0 -89
- package/src/editor/editor-snapshot.ts +0 -68
- package/src/editor/event-to-change.tsx +0 -49
- package/src/editor/hooks/usePortableTextEditor.ts +0 -25
- package/src/editor/hooks/usePortableTextEditorSelection.tsx +0 -28
- package/src/editor/mutation-machine.ts +0 -322
- package/src/editor/plugins/create-with-event-listeners.ts +0 -271
- package/src/editor/plugins/createWithEditableAPI.ts +0 -529
- package/src/editor/plugins/createWithHotKeys.ts +0 -68
- package/src/editor/plugins/createWithObjectKeys.ts +0 -289
- package/src/editor/plugins/createWithPatches.ts +0 -272
- package/src/editor/plugins/createWithPortableTextMarkModel.ts +0 -559
- package/src/editor/plugins/createWithSchemaTypes.ts +0 -121
- package/src/editor/plugins/slate-plugin.update-selection.ts +0 -51
- package/src/editor/plugins/slate-plugin.update-value.ts +0 -46
- package/src/editor/plugins/with-plugins.ts +0 -69
- package/src/editor/range-decorations-machine.ts +0 -421
- package/src/editor/relay-actor-context.ts +0 -4
- package/src/editor/relay-machine.ts +0 -152
- package/src/editor/sync-machine.ts +0 -961
- package/src/editor/use-editor.ts +0 -27
- package/src/editor/validate-selection-machine.test.ts +0 -47
- package/src/editor/validate-selection-machine.ts +0 -149
- package/src/editor/weakMaps.ts +0 -15
- package/src/editor/with-normalizing-node.ts +0 -14
- package/src/editor/with-performing-behavior-operation.ts +0 -21
- package/src/editor/withChanges.ts +0 -13
- package/src/editor/without-normalizing-conditional.ts +0 -13
- package/src/editor/withoutPatching.ts +0 -14
- package/src/editor.ts +0 -59
- package/src/history/behavior.operation.history.redo.ts +0 -67
- package/src/history/behavior.operation.history.undo.ts +0 -71
- package/src/history/event.history.undo.test.tsx +0 -672
- package/src/history/history.preserving-keys.test.tsx +0 -112
- package/src/history/remote-patches.ts +0 -20
- package/src/history/slate-plugin.history.ts +0 -142
- package/src/history/slate-plugin.redoing.ts +0 -21
- package/src/history/slate-plugin.undoing.ts +0 -21
- package/src/history/slate-plugin.without-history.ts +0 -23
- package/src/history/transform-operation.ts +0 -245
- package/src/history/undo-redo-collaboration.test.tsx +0 -541
- package/src/history/undo-redo.feature +0 -125
- package/src/history/undo-redo.test.tsx +0 -195
- package/src/history/undo-step.ts +0 -148
- package/src/index.ts +0 -98
- package/src/internal-utils/__tests__/ranges.test.ts +0 -23
- package/src/internal-utils/__tests__/values.test.ts +0 -110
- package/src/internal-utils/apply-operation-to-portable-text.test.ts +0 -1861
- package/src/internal-utils/apply-operation-to-portable-text.ts +0 -615
- package/src/internal-utils/applyPatch.ts +0 -644
- package/src/internal-utils/block-keys.ts +0 -9
- package/src/internal-utils/build-index-maps.test.ts +0 -464
- package/src/internal-utils/build-index-maps.ts +0 -131
- package/src/internal-utils/collapse-selection.ts +0 -36
- package/src/internal-utils/compound-client-rect.ts +0 -28
- package/src/internal-utils/create-placeholder-block.ts +0 -21
- package/src/internal-utils/create-test-snapshot.ts +0 -28
- package/src/internal-utils/debug.ts +0 -12
- package/src/internal-utils/editor-selection.test.ts +0 -44
- package/src/internal-utils/editor-selection.ts +0 -56
- package/src/internal-utils/event-position.ts +0 -318
- package/src/internal-utils/global-scope.ts +0 -27
- package/src/internal-utils/globally-scoped-context.ts +0 -39
- package/src/internal-utils/is-hotkey.test.ts +0 -114
- package/src/internal-utils/is-hotkey.ts +0 -209
- package/src/internal-utils/mime-type.ts +0 -1
- package/src/internal-utils/move-range-by-operation.ts +0 -19
- package/src/internal-utils/operation-to-patches.test.ts +0 -522
- package/src/internal-utils/operation-to-patches.ts +0 -571
- package/src/internal-utils/portable-text-node.ts +0 -209
- package/src/internal-utils/schema.ts +0 -8
- package/src/internal-utils/selection-block-keys.ts +0 -20
- package/src/internal-utils/selection-focus-text.ts +0 -40
- package/src/internal-utils/selection-text.test.ts +0 -32
- package/src/internal-utils/selection-text.ts +0 -21
- package/src/internal-utils/selection.ts +0 -77
- package/src/internal-utils/sibling-utils.ts +0 -55
- package/src/internal-utils/slate-utils.test.tsx +0 -121
- package/src/internal-utils/slate-utils.ts +0 -417
- package/src/internal-utils/split-string.ts +0 -12
- package/src/internal-utils/stop-actor.ts +0 -43
- package/src/internal-utils/string-overlap.test.ts +0 -14
- package/src/internal-utils/string-overlap.ts +0 -28
- package/src/internal-utils/string-utils.ts +0 -7
- package/src/internal-utils/text-block-key.test.ts +0 -41
- package/src/internal-utils/text-block-key.ts +0 -26
- package/src/internal-utils/text-marks.test.ts +0 -41
- package/src/internal-utils/text-marks.ts +0 -22
- package/src/internal-utils/text-selection.test.ts +0 -211
- package/src/internal-utils/text-selection.ts +0 -121
- package/src/internal-utils/to-slate-range.test.ts +0 -278
- package/src/internal-utils/to-slate-range.ts +0 -171
- package/src/internal-utils/validateValue.ts +0 -443
- package/src/internal-utils/value-annotations.ts +0 -33
- package/src/internal-utils/values.test.ts +0 -282
- package/src/internal-utils/values.ts +0 -266
- package/src/keyboard-shortcuts/default-keyboard-shortcuts.ts +0 -146
- package/src/operations/behavior.operation.annotation.add.ts +0 -99
- package/src/operations/behavior.operation.annotation.remove.ts +0 -150
- package/src/operations/behavior.operation.block.set.ts +0 -104
- package/src/operations/behavior.operation.block.unset.ts +0 -54
- package/src/operations/behavior.operation.child.set.ts +0 -107
- package/src/operations/behavior.operation.child.unset.ts +0 -116
- package/src/operations/behavior.operation.decorator.add.ts +0 -131
- package/src/operations/behavior.operation.delete.ts +0 -294
- package/src/operations/behavior.operation.insert.block.ts +0 -495
- package/src/operations/behavior.operation.insert.child.ts +0 -129
- package/src/operations/behavior.operation.insert.text.ts +0 -8
- package/src/operations/behavior.operation.move.backward.ts +0 -12
- package/src/operations/behavior.operation.move.block.ts +0 -44
- package/src/operations/behavior.operation.move.forward.ts +0 -11
- package/src/operations/behavior.operation.select.ts +0 -27
- package/src/operations/behavior.operations.ts +0 -221
- package/src/plugins/_exports/index.ts +0 -1
- package/src/plugins/index.ts +0 -3
- package/src/plugins/plugin.behavior.tsx +0 -24
- package/src/plugins/plugin.editor-ref.tsx +0 -17
- package/src/plugins/plugin.event-listener.tsx +0 -68
- package/src/plugins/plugin.internal.auto-close-brackets.test.tsx +0 -71
- package/src/plugins/plugin.internal.auto-close-brackets.ts +0 -62
- package/src/plugins/plugin.internal.change-ref.tsx +0 -19
- package/src/plugins/plugin.internal.portable-text-editor-ref.tsx +0 -16
- package/src/plugins/plugin.internal.slate-editor-ref.tsx +0 -15
- package/src/priority/priority.core.ts +0 -3
- package/src/priority/priority.sort.test.ts +0 -319
- package/src/priority/priority.sort.ts +0 -123
- package/src/priority/priority.types.ts +0 -24
- package/src/selectors/_exports/index.ts +0 -1
- package/src/selectors/drag-selection.test.ts +0 -578
- package/src/selectors/drag-selection.ts +0 -118
- package/src/selectors/index.ts +0 -54
- package/src/selectors/selector.get-active-annotation-marks.ts +0 -12
- package/src/selectors/selector.get-active-annotations.ts +0 -36
- package/src/selectors/selector.get-active-decorators.ts +0 -29
- package/src/selectors/selector.get-active-list-item.ts +0 -38
- package/src/selectors/selector.get-active-style.ts +0 -38
- package/src/selectors/selector.get-anchor-block.ts +0 -22
- package/src/selectors/selector.get-anchor-child.ts +0 -36
- package/src/selectors/selector.get-anchor-span.ts +0 -17
- package/src/selectors/selector.get-anchor-text-block.ts +0 -18
- package/src/selectors/selector.get-block-offsets.ts +0 -34
- package/src/selectors/selector.get-caret-word-selection.test.ts +0 -284
- package/src/selectors/selector.get-caret-word-selection.ts +0 -134
- package/src/selectors/selector.get-first-block.ts +0 -14
- package/src/selectors/selector.get-focus-block-object.ts +0 -18
- package/src/selectors/selector.get-focus-block.ts +0 -23
- package/src/selectors/selector.get-focus-child.ts +0 -36
- package/src/selectors/selector.get-focus-inline-object.ts +0 -17
- package/src/selectors/selector.get-focus-list-block.ts +0 -18
- package/src/selectors/selector.get-focus-span.ts +0 -18
- package/src/selectors/selector.get-focus-text-block.ts +0 -18
- package/src/selectors/selector.get-last-block.ts +0 -16
- package/src/selectors/selector.get-mark-state.test.ts +0 -325
- package/src/selectors/selector.get-mark-state.ts +0 -263
- package/src/selectors/selector.get-next-block.ts +0 -29
- package/src/selectors/selector.get-next-inline-object.ts +0 -53
- package/src/selectors/selector.get-next-inline-objects.ts +0 -50
- package/src/selectors/selector.get-next-span.ts +0 -56
- package/src/selectors/selector.get-previous-block.ts +0 -29
- package/src/selectors/selector.get-previous-inline-object.ts +0 -50
- package/src/selectors/selector.get-previous-inline-objects.ts +0 -47
- package/src/selectors/selector.get-previous-span.ts +0 -53
- package/src/selectors/selector.get-selected-blocks.ts +0 -61
- package/src/selectors/selector.get-selected-spans.test.ts +0 -347
- package/src/selectors/selector.get-selected-spans.ts +0 -155
- package/src/selectors/selector.get-selected-text-blocks.ts +0 -73
- package/src/selectors/selector.get-selected-value.test.ts +0 -834
- package/src/selectors/selector.get-selected-value.ts +0 -66
- package/src/selectors/selector.get-selection-end-block.ts +0 -33
- package/src/selectors/selector.get-selection-end-child.ts +0 -33
- package/src/selectors/selector.get-selection-end-point.ts +0 -17
- package/src/selectors/selector.get-selection-start-block.ts +0 -33
- package/src/selectors/selector.get-selection-start-child.ts +0 -33
- package/src/selectors/selector.get-selection-start-point.ts +0 -17
- package/src/selectors/selector.get-selection-text.test.ts +0 -421
- package/src/selectors/selector.get-selection-text.ts +0 -27
- package/src/selectors/selector.get-selection.ts +0 -9
- package/src/selectors/selector.get-text-after.ts +0 -46
- package/src/selectors/selector.get-text-before.ts +0 -46
- package/src/selectors/selector.get-value.ts +0 -11
- package/src/selectors/selector.is-active-annotation.test.ts +0 -320
- package/src/selectors/selector.is-active-annotation.ts +0 -52
- package/src/selectors/selector.is-active-decorator.test.ts +0 -136
- package/src/selectors/selector.is-active-decorator.ts +0 -24
- package/src/selectors/selector.is-active-list-item.ts +0 -13
- package/src/selectors/selector.is-active-style.ts +0 -13
- package/src/selectors/selector.is-at-the-end-of-block.ts +0 -30
- package/src/selectors/selector.is-at-the-start-of-block.ts +0 -30
- package/src/selectors/selector.is-overlapping-selection.test.ts +0 -304
- package/src/selectors/selector.is-overlapping-selection.ts +0 -181
- package/src/selectors/selector.is-point-after-selection.ts +0 -97
- package/src/selectors/selector.is-point-before-selection.ts +0 -97
- package/src/selectors/selector.is-selecting-entire-blocks.ts +0 -43
- package/src/selectors/selector.is-selection-collapsed.ts +0 -17
- package/src/selectors/selector.is-selection-expanded.test.ts +0 -63
- package/src/selectors/selector.is-selection-expanded.ts +0 -9
- package/src/test/_exports/index.ts +0 -1
- package/src/test/gherkin-parameter-types.ts +0 -112
- package/src/test/index.ts +0 -1
- package/src/test/vitest/_exports/index.ts +0 -1
- package/src/test/vitest/index.ts +0 -3
- package/src/test/vitest/step-context.ts +0 -13
- package/src/test/vitest/step-definitions.tsx +0 -960
- package/src/test/vitest/test-editor.tsx +0 -198
- package/src/type-utils.ts +0 -29
- package/src/types/block-offset.ts +0 -9
- package/src/types/block-with-optional-key.ts +0 -25
- package/src/types/editor.ts +0 -509
- package/src/types/options.ts +0 -13
- package/src/types/paths.ts +0 -35
- package/src/types/slate-editor.ts +0 -50
- package/src/types/slate.ts +0 -27
- package/src/utils/_exports/index.ts +0 -1
- package/src/utils/asserters.ts +0 -9
- package/src/utils/index.ts +0 -24
- package/src/utils/key-generator.ts +0 -33
- package/src/utils/parse-blocks.test.ts +0 -836
- package/src/utils/parse-blocks.ts +0 -504
- package/src/utils/util.at-the-beginning-of-block.ts +0 -32
- package/src/utils/util.block-offset-to-block-selection-point.ts +0 -28
- package/src/utils/util.block-offset-to-selection-point.ts +0 -33
- package/src/utils/util.block-offset.test.ts +0 -375
- package/src/utils/util.block-offset.ts +0 -136
- package/src/utils/util.block-offsets-to-selection.ts +0 -38
- package/src/utils/util.child-selection-point-to-block-offset.ts +0 -51
- package/src/utils/util.get-block-end-point.ts +0 -35
- package/src/utils/util.get-block-start-point.ts +0 -31
- package/src/utils/util.get-selection-end-point.ts +0 -20
- package/src/utils/util.get-selection-start-point.ts +0 -20
- package/src/utils/util.get-text-block-text.ts +0 -8
- package/src/utils/util.is-empty-text-block.ts +0 -21
- package/src/utils/util.is-equal-selection-points.ts +0 -13
- package/src/utils/util.is-equal-selections.ts +0 -20
- package/src/utils/util.is-keyed-segment.ts +0 -8
- package/src/utils/util.is-selection-collapsed.ts +0 -16
- package/src/utils/util.is-selection-expanded.ts +0 -13
- package/src/utils/util.merge-text-blocks.ts +0 -40
- package/src/utils/util.reverse-selection.ts +0 -26
- package/src/utils/util.selection-point-to-block-offset.ts +0 -30
- package/src/utils/util.selection-point.ts +0 -22
- package/src/utils/util.slice-blocks.ts +0 -221
- package/src/utils/util.slice-text-block.test.ts +0 -190
- package/src/utils/util.slice-text-block.ts +0 -89
- package/src/utils/util.split-text-block.ts +0 -54
|
@@ -1,289 +0,0 @@
|
|
|
1
|
-
import {isSpan, isTextBlock} from '@portabletext/schema'
|
|
2
|
-
import {isEqual} from 'lodash'
|
|
3
|
-
import {Editor, Element, Node, Path, Transforms} from 'slate'
|
|
4
|
-
import {isRedoing} from '../../history/slate-plugin.redoing'
|
|
5
|
-
import {isUndoing} from '../../history/slate-plugin.undoing'
|
|
6
|
-
import type {PortableTextSlateEditor} from '../../types/slate-editor'
|
|
7
|
-
import type {EditorActor} from '../editor-machine'
|
|
8
|
-
import {withNormalizeNode} from '../with-normalizing-node'
|
|
9
|
-
import {isChangingRemotely} from '../withChanges'
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* This plugin makes sure that every new node in the editor get a new _key prop when created
|
|
13
|
-
*
|
|
14
|
-
*/
|
|
15
|
-
export function createWithObjectKeys(editorActor: EditorActor) {
|
|
16
|
-
return function withKeys(
|
|
17
|
-
editor: PortableTextSlateEditor,
|
|
18
|
-
): PortableTextSlateEditor {
|
|
19
|
-
const {apply, normalizeNode} = editor
|
|
20
|
-
|
|
21
|
-
// The default behavior is to always generate a new key here.
|
|
22
|
-
// For example, when undoing and redoing we want to retain the keys, but
|
|
23
|
-
// when we create a new bold span by splitting a non-bold-span we want the produced node to get a new key.
|
|
24
|
-
editor.apply = (operation) => {
|
|
25
|
-
/**
|
|
26
|
-
* We don't want to run any side effects when the editor is processing
|
|
27
|
-
* remote changes.
|
|
28
|
-
*/
|
|
29
|
-
if (isChangingRemotely(editor)) {
|
|
30
|
-
apply(operation)
|
|
31
|
-
return
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* We don't want to run any side effects when the editor is undoing or
|
|
36
|
-
* redoing operations.
|
|
37
|
-
*/
|
|
38
|
-
if (isUndoing(editor) || isRedoing(editor)) {
|
|
39
|
-
apply(operation)
|
|
40
|
-
return
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
if (operation.type === 'split_node') {
|
|
44
|
-
const existingKeys = [...Node.descendants(editor)].map(
|
|
45
|
-
([node]) => node._key,
|
|
46
|
-
)
|
|
47
|
-
|
|
48
|
-
apply({
|
|
49
|
-
...operation,
|
|
50
|
-
properties: {
|
|
51
|
-
...operation.properties,
|
|
52
|
-
_key:
|
|
53
|
-
operation.properties._key === undefined ||
|
|
54
|
-
existingKeys.includes(operation.properties._key)
|
|
55
|
-
? editorActor.getSnapshot().context.keyGenerator()
|
|
56
|
-
: operation.properties._key,
|
|
57
|
-
},
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
return
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (operation.type === 'insert_node') {
|
|
64
|
-
if (!Editor.isEditor(operation.node)) {
|
|
65
|
-
const existingKeys = [...Node.descendants(editor)].map(
|
|
66
|
-
([node]) => node._key,
|
|
67
|
-
)
|
|
68
|
-
|
|
69
|
-
apply({
|
|
70
|
-
...operation,
|
|
71
|
-
node: {
|
|
72
|
-
...operation.node,
|
|
73
|
-
_key:
|
|
74
|
-
operation.node._key === undefined ||
|
|
75
|
-
existingKeys.includes(operation.node._key)
|
|
76
|
-
? editorActor.getSnapshot().context.keyGenerator()
|
|
77
|
-
: operation.node._key,
|
|
78
|
-
},
|
|
79
|
-
})
|
|
80
|
-
|
|
81
|
-
return
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (operation.type === 'merge_node') {
|
|
86
|
-
const index = operation.path[operation.path.length - 1]
|
|
87
|
-
const prevPath = Path.previous(operation.path)
|
|
88
|
-
const prevIndex = prevPath[prevPath.length - 1]
|
|
89
|
-
|
|
90
|
-
if (operation.path.length !== 1 || prevPath.length !== 1) {
|
|
91
|
-
apply(operation)
|
|
92
|
-
return
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
const block = editor.value.at(index)
|
|
96
|
-
const previousBlock = editor.value.at(prevIndex)
|
|
97
|
-
|
|
98
|
-
if (!block || !previousBlock) {
|
|
99
|
-
apply(operation)
|
|
100
|
-
return
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
if (
|
|
104
|
-
!isTextBlock(editorActor.getSnapshot().context, block) ||
|
|
105
|
-
!isTextBlock(editorActor.getSnapshot().context, previousBlock)
|
|
106
|
-
) {
|
|
107
|
-
apply(operation)
|
|
108
|
-
return
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// If we are merging two text blocks, then we need to make sure there
|
|
112
|
-
// are no duplicate keys in the blocks. Therefore, we assign new keys
|
|
113
|
-
// to any child or markDef that shares key with other children or
|
|
114
|
-
// markDefs in the previous block.
|
|
115
|
-
const previousBlockChildKeys = previousBlock.children.map(
|
|
116
|
-
(child) => child._key,
|
|
117
|
-
)
|
|
118
|
-
const previousBlockMarkDefKeys =
|
|
119
|
-
previousBlock.markDefs?.map((markDef) => markDef._key) ?? []
|
|
120
|
-
|
|
121
|
-
// Assign new keys to markDefs with duplicate keys and keep track of
|
|
122
|
-
// the mapping between the old and new keys
|
|
123
|
-
const markDefKeyMap = new Map<string, string>()
|
|
124
|
-
const adjustedMarkDefs = block.markDefs?.map((markDef) => {
|
|
125
|
-
if (previousBlockMarkDefKeys.includes(markDef._key)) {
|
|
126
|
-
const newKey = editorActor.getSnapshot().context.keyGenerator()
|
|
127
|
-
markDefKeyMap.set(markDef._key, newKey)
|
|
128
|
-
return {
|
|
129
|
-
...markDef,
|
|
130
|
-
_key: newKey,
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
return markDef
|
|
135
|
-
})
|
|
136
|
-
|
|
137
|
-
// Assign new keys to spans with duplicate keys and update any markDef
|
|
138
|
-
// key if needed
|
|
139
|
-
let childIndex = 0
|
|
140
|
-
for (const child of block.children) {
|
|
141
|
-
if (isSpan(editorActor.getSnapshot().context, child)) {
|
|
142
|
-
const marks =
|
|
143
|
-
child.marks?.map((mark) => {
|
|
144
|
-
const markDefKey = markDefKeyMap.get(mark)
|
|
145
|
-
|
|
146
|
-
if (markDefKey) {
|
|
147
|
-
return markDefKey
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
return mark
|
|
151
|
-
}) ?? []
|
|
152
|
-
|
|
153
|
-
if (!isEqual(child.marks, marks)) {
|
|
154
|
-
Transforms.setNodes(
|
|
155
|
-
editor,
|
|
156
|
-
{
|
|
157
|
-
marks,
|
|
158
|
-
},
|
|
159
|
-
{
|
|
160
|
-
at: [index, childIndex],
|
|
161
|
-
},
|
|
162
|
-
)
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
if (previousBlockChildKeys.includes(child._key)) {
|
|
167
|
-
Transforms.setNodes(
|
|
168
|
-
editor,
|
|
169
|
-
{
|
|
170
|
-
_key: editorActor.getSnapshot().context.keyGenerator(),
|
|
171
|
-
},
|
|
172
|
-
{
|
|
173
|
-
at: [index, childIndex],
|
|
174
|
-
},
|
|
175
|
-
)
|
|
176
|
-
}
|
|
177
|
-
childIndex++
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
apply({
|
|
181
|
-
...operation,
|
|
182
|
-
properties: {
|
|
183
|
-
...operation.properties,
|
|
184
|
-
// Make sure the adjusted markDefs are carried along for the merge
|
|
185
|
-
// operation
|
|
186
|
-
markDefs: adjustedMarkDefs,
|
|
187
|
-
},
|
|
188
|
-
})
|
|
189
|
-
return
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
apply(operation)
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
editor.normalizeNode = (entry) => {
|
|
196
|
-
const [node, path] = entry
|
|
197
|
-
|
|
198
|
-
if (Element.isElement(node)) {
|
|
199
|
-
const [parent] = Editor.parent(editor, path)
|
|
200
|
-
|
|
201
|
-
if (parent && Editor.isEditor(parent)) {
|
|
202
|
-
const blockKeys = new Set<string>()
|
|
203
|
-
|
|
204
|
-
for (const sibling of parent.children) {
|
|
205
|
-
if (sibling._key && blockKeys.has(sibling._key)) {
|
|
206
|
-
const _key = editorActor.getSnapshot().context.keyGenerator()
|
|
207
|
-
|
|
208
|
-
blockKeys.add(_key)
|
|
209
|
-
|
|
210
|
-
withNormalizeNode(editor, () => {
|
|
211
|
-
Transforms.setNodes(editor, {_key}, {at: path})
|
|
212
|
-
})
|
|
213
|
-
|
|
214
|
-
return
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
if (!sibling._key) {
|
|
218
|
-
const _key = editorActor.getSnapshot().context.keyGenerator()
|
|
219
|
-
|
|
220
|
-
blockKeys.add(_key)
|
|
221
|
-
|
|
222
|
-
withNormalizeNode(editor, () => {
|
|
223
|
-
Transforms.setNodes(editor, {_key}, {at: path})
|
|
224
|
-
})
|
|
225
|
-
|
|
226
|
-
return
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
blockKeys.add(sibling._key)
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
if (
|
|
235
|
-
Element.isElement(node) &&
|
|
236
|
-
node._type === editorActor.getSnapshot().context.schema.block.name
|
|
237
|
-
) {
|
|
238
|
-
// Set key on block itself
|
|
239
|
-
if (!node._key) {
|
|
240
|
-
withNormalizeNode(editor, () => {
|
|
241
|
-
Transforms.setNodes(
|
|
242
|
-
editor,
|
|
243
|
-
{_key: editorActor.getSnapshot().context.keyGenerator()},
|
|
244
|
-
{at: path},
|
|
245
|
-
)
|
|
246
|
-
})
|
|
247
|
-
return
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
// Set unique keys on it's children
|
|
251
|
-
const childKeys = new Set<string>()
|
|
252
|
-
|
|
253
|
-
for (const [child, childPath] of Node.children(editor, path)) {
|
|
254
|
-
if (child._key && childKeys.has(child._key)) {
|
|
255
|
-
const _key = editorActor.getSnapshot().context.keyGenerator()
|
|
256
|
-
|
|
257
|
-
childKeys.add(_key)
|
|
258
|
-
|
|
259
|
-
withNormalizeNode(editor, () => {
|
|
260
|
-
Transforms.setNodes(editor, {_key}, {at: childPath})
|
|
261
|
-
})
|
|
262
|
-
|
|
263
|
-
return
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
if (!child._key) {
|
|
267
|
-
const _key = editorActor.getSnapshot().context.keyGenerator()
|
|
268
|
-
|
|
269
|
-
childKeys.add(_key)
|
|
270
|
-
|
|
271
|
-
withNormalizeNode(editor, () => {
|
|
272
|
-
Transforms.setNodes(editor, {_key}, {at: childPath})
|
|
273
|
-
})
|
|
274
|
-
|
|
275
|
-
return
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
childKeys.add(child._key)
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
withNormalizeNode(editor, () => {
|
|
283
|
-
normalizeNode(entry)
|
|
284
|
-
})
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
return editor
|
|
288
|
-
}
|
|
289
|
-
}
|
|
@@ -1,272 +0,0 @@
|
|
|
1
|
-
import {insert, setIfMissing, unset, type Patch} from '@portabletext/patches'
|
|
2
|
-
import type {PortableTextBlock} from '@portabletext/schema'
|
|
3
|
-
import {Editor, type Operation} from 'slate'
|
|
4
|
-
import {pluginWithoutHistory} from '../../history/slate-plugin.without-history'
|
|
5
|
-
import {getCurrentUndoStepId} from '../../history/undo-step'
|
|
6
|
-
import {createApplyPatch} from '../../internal-utils/applyPatch'
|
|
7
|
-
import {debugWithName} from '../../internal-utils/debug'
|
|
8
|
-
import {
|
|
9
|
-
insertNodePatch,
|
|
10
|
-
insertTextPatch,
|
|
11
|
-
mergeNodePatch,
|
|
12
|
-
moveNodePatch,
|
|
13
|
-
removeNodePatch,
|
|
14
|
-
removeTextPatch,
|
|
15
|
-
setNodePatch,
|
|
16
|
-
splitNodePatch,
|
|
17
|
-
} from '../../internal-utils/operation-to-patches'
|
|
18
|
-
import {isEqualToEmptyEditor} from '../../internal-utils/values'
|
|
19
|
-
import type {PortableTextSlateEditor} from '../../types/slate-editor'
|
|
20
|
-
import type {EditorActor} from '../editor-machine'
|
|
21
|
-
import type {RelayActor} from '../relay-machine'
|
|
22
|
-
import {IS_PROCESSING_REMOTE_CHANGES} from '../weakMaps'
|
|
23
|
-
import {withRemoteChanges} from '../withChanges'
|
|
24
|
-
import {isPatching, PATCHING, withoutPatching} from '../withoutPatching'
|
|
25
|
-
|
|
26
|
-
const debug = debugWithName('plugin:withPatches')
|
|
27
|
-
const debugVerbose = false
|
|
28
|
-
|
|
29
|
-
interface Options {
|
|
30
|
-
editorActor: EditorActor
|
|
31
|
-
relayActor: RelayActor
|
|
32
|
-
subscriptions: Array<() => () => void>
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function createWithPatches({
|
|
36
|
-
editorActor,
|
|
37
|
-
relayActor,
|
|
38
|
-
subscriptions,
|
|
39
|
-
}: Options): (editor: PortableTextSlateEditor) => PortableTextSlateEditor {
|
|
40
|
-
// The previous editor value are needed to figure out the _key of deleted nodes
|
|
41
|
-
// The editor.value would no longer contain that information if the node is already deleted.
|
|
42
|
-
let previousValue: PortableTextBlock[]
|
|
43
|
-
|
|
44
|
-
const applyPatch = createApplyPatch(editorActor.getSnapshot().context)
|
|
45
|
-
|
|
46
|
-
return function withPatches(editor: PortableTextSlateEditor) {
|
|
47
|
-
IS_PROCESSING_REMOTE_CHANGES.set(editor, false)
|
|
48
|
-
PATCHING.set(editor, true)
|
|
49
|
-
previousValue = [...editor.value]
|
|
50
|
-
|
|
51
|
-
const {apply} = editor
|
|
52
|
-
let bufferedPatches: Patch[] = []
|
|
53
|
-
|
|
54
|
-
const handleBufferedRemotePatches = () => {
|
|
55
|
-
if (bufferedPatches.length === 0) {
|
|
56
|
-
return
|
|
57
|
-
}
|
|
58
|
-
const patches = bufferedPatches
|
|
59
|
-
bufferedPatches = []
|
|
60
|
-
let changed = false
|
|
61
|
-
|
|
62
|
-
withRemoteChanges(editor, () => {
|
|
63
|
-
Editor.withoutNormalizing(editor, () => {
|
|
64
|
-
withoutPatching(editor, () => {
|
|
65
|
-
pluginWithoutHistory(editor, () => {
|
|
66
|
-
for (const patch of patches) {
|
|
67
|
-
if (debug.enabled) {
|
|
68
|
-
debug(`Handling remote patch ${JSON.stringify(patch)}`)
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
try {
|
|
72
|
-
changed = applyPatch(editor, patch)
|
|
73
|
-
} catch (error) {
|
|
74
|
-
console.error(
|
|
75
|
-
`Applying patch ${JSON.stringify(patch)} failed due to: ${error.message}`,
|
|
76
|
-
)
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
})
|
|
80
|
-
})
|
|
81
|
-
})
|
|
82
|
-
if (changed) {
|
|
83
|
-
editor.normalize()
|
|
84
|
-
editor.onChange()
|
|
85
|
-
}
|
|
86
|
-
})
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const handlePatches = ({patches}: {patches: Patch[]}) => {
|
|
90
|
-
const remotePatches = patches.filter((p) => p.origin !== 'local')
|
|
91
|
-
if (remotePatches.length === 0) {
|
|
92
|
-
return
|
|
93
|
-
}
|
|
94
|
-
bufferedPatches = bufferedPatches.concat(remotePatches)
|
|
95
|
-
handleBufferedRemotePatches()
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
subscriptions.push(() => {
|
|
99
|
-
debug('Subscribing to remote patches')
|
|
100
|
-
const sub = editorActor.on('patches', handlePatches)
|
|
101
|
-
return () => {
|
|
102
|
-
debug('Unsubscribing to remote patches')
|
|
103
|
-
sub.unsubscribe()
|
|
104
|
-
}
|
|
105
|
-
})
|
|
106
|
-
|
|
107
|
-
editor.apply = (operation: Operation): void | Editor => {
|
|
108
|
-
let patches: Patch[] = []
|
|
109
|
-
|
|
110
|
-
// Update previous children here before we apply
|
|
111
|
-
previousValue = editor.value
|
|
112
|
-
|
|
113
|
-
const editorWasEmpty = isEqualToEmptyEditor(
|
|
114
|
-
editorActor.getSnapshot().context.initialValue,
|
|
115
|
-
previousValue,
|
|
116
|
-
editorActor.getSnapshot().context.schema,
|
|
117
|
-
)
|
|
118
|
-
|
|
119
|
-
// Apply the operation
|
|
120
|
-
apply(operation)
|
|
121
|
-
|
|
122
|
-
const editorIsEmpty = isEqualToEmptyEditor(
|
|
123
|
-
editorActor.getSnapshot().context.initialValue,
|
|
124
|
-
editor.value,
|
|
125
|
-
editorActor.getSnapshot().context.schema,
|
|
126
|
-
)
|
|
127
|
-
|
|
128
|
-
if (!isPatching(editor)) {
|
|
129
|
-
if (debugVerbose && debug.enabled) {
|
|
130
|
-
debug(
|
|
131
|
-
`Editor is not producing patch for operation ${operation.type}`,
|
|
132
|
-
operation,
|
|
133
|
-
)
|
|
134
|
-
}
|
|
135
|
-
return editor
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// If the editor was empty and now isn't, insert the placeholder into it.
|
|
139
|
-
if (
|
|
140
|
-
editorWasEmpty &&
|
|
141
|
-
!editorIsEmpty &&
|
|
142
|
-
operation.type !== 'set_selection'
|
|
143
|
-
) {
|
|
144
|
-
patches.push(insert(previousValue, 'before', [0]))
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
switch (operation.type) {
|
|
148
|
-
case 'insert_text':
|
|
149
|
-
patches = [
|
|
150
|
-
...patches,
|
|
151
|
-
...insertTextPatch(
|
|
152
|
-
editorActor.getSnapshot().context.schema,
|
|
153
|
-
editor.children,
|
|
154
|
-
operation,
|
|
155
|
-
previousValue,
|
|
156
|
-
),
|
|
157
|
-
]
|
|
158
|
-
break
|
|
159
|
-
case 'remove_text':
|
|
160
|
-
patches = [
|
|
161
|
-
...patches,
|
|
162
|
-
...removeTextPatch(
|
|
163
|
-
editorActor.getSnapshot().context.schema,
|
|
164
|
-
editor.children,
|
|
165
|
-
operation,
|
|
166
|
-
previousValue,
|
|
167
|
-
),
|
|
168
|
-
]
|
|
169
|
-
break
|
|
170
|
-
case 'remove_node':
|
|
171
|
-
patches = [
|
|
172
|
-
...patches,
|
|
173
|
-
...removeNodePatch(
|
|
174
|
-
editorActor.getSnapshot().context.schema,
|
|
175
|
-
previousValue,
|
|
176
|
-
operation,
|
|
177
|
-
),
|
|
178
|
-
]
|
|
179
|
-
break
|
|
180
|
-
case 'split_node':
|
|
181
|
-
patches = [
|
|
182
|
-
...patches,
|
|
183
|
-
...splitNodePatch(
|
|
184
|
-
editorActor.getSnapshot().context.schema,
|
|
185
|
-
editor.children,
|
|
186
|
-
operation,
|
|
187
|
-
previousValue,
|
|
188
|
-
),
|
|
189
|
-
]
|
|
190
|
-
break
|
|
191
|
-
case 'insert_node':
|
|
192
|
-
patches = [
|
|
193
|
-
...patches,
|
|
194
|
-
...insertNodePatch(
|
|
195
|
-
editorActor.getSnapshot().context.schema,
|
|
196
|
-
editor.children,
|
|
197
|
-
operation,
|
|
198
|
-
previousValue,
|
|
199
|
-
),
|
|
200
|
-
]
|
|
201
|
-
break
|
|
202
|
-
case 'set_node':
|
|
203
|
-
patches = [
|
|
204
|
-
...patches,
|
|
205
|
-
...setNodePatch(
|
|
206
|
-
editorActor.getSnapshot().context.schema,
|
|
207
|
-
editor.children,
|
|
208
|
-
operation,
|
|
209
|
-
),
|
|
210
|
-
]
|
|
211
|
-
break
|
|
212
|
-
case 'merge_node':
|
|
213
|
-
patches = [
|
|
214
|
-
...patches,
|
|
215
|
-
...mergeNodePatch(
|
|
216
|
-
editorActor.getSnapshot().context.schema,
|
|
217
|
-
editor.children,
|
|
218
|
-
operation,
|
|
219
|
-
previousValue,
|
|
220
|
-
),
|
|
221
|
-
]
|
|
222
|
-
break
|
|
223
|
-
case 'move_node':
|
|
224
|
-
patches = [
|
|
225
|
-
...patches,
|
|
226
|
-
...moveNodePatch(
|
|
227
|
-
editorActor.getSnapshot().context.schema,
|
|
228
|
-
previousValue,
|
|
229
|
-
operation,
|
|
230
|
-
),
|
|
231
|
-
]
|
|
232
|
-
break
|
|
233
|
-
default:
|
|
234
|
-
// Do nothing
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
// Unset the value if a operation made the editor empty
|
|
238
|
-
if (
|
|
239
|
-
!editorWasEmpty &&
|
|
240
|
-
editorIsEmpty &&
|
|
241
|
-
['merge_node', 'set_node', 'remove_text', 'remove_node'].includes(
|
|
242
|
-
operation.type,
|
|
243
|
-
)
|
|
244
|
-
) {
|
|
245
|
-
patches = [...patches, unset([])]
|
|
246
|
-
relayActor.send({
|
|
247
|
-
type: 'unset',
|
|
248
|
-
previousValue,
|
|
249
|
-
})
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
// Prepend patches with setIfMissing if going from empty editor to something involving a patch.
|
|
253
|
-
if (editorWasEmpty && patches.length > 0) {
|
|
254
|
-
patches = [setIfMissing([], []), ...patches]
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
// Emit all patches
|
|
258
|
-
if (patches.length > 0) {
|
|
259
|
-
for (const patch of patches) {
|
|
260
|
-
editorActor.send({
|
|
261
|
-
type: 'internal.patch',
|
|
262
|
-
patch: {...patch, origin: 'local'},
|
|
263
|
-
operationId: getCurrentUndoStepId(editor),
|
|
264
|
-
value: editor.value,
|
|
265
|
-
})
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
return editor
|
|
269
|
-
}
|
|
270
|
-
return editor
|
|
271
|
-
}
|
|
272
|
-
}
|