@portabletext/editor 3.3.3 → 3.3.5
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 +21 -1667
- package/lib/index.js +511 -9010
- package/lib/index.js.map +1 -1
- package/package.json +14 -15
- 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 -107
- 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,51 +0,0 @@
|
|
|
1
|
-
import {slateRangeToSelection} from '../../internal-utils/slate-utils'
|
|
2
|
-
import type {PortableTextSlateEditor} from '../../types/slate-editor'
|
|
3
|
-
import type {EditorActor} from '../editor-machine'
|
|
4
|
-
import {SLATE_TO_PORTABLE_TEXT_RANGE} from '../weakMaps'
|
|
5
|
-
|
|
6
|
-
export function pluginUpdateSelection({
|
|
7
|
-
editor,
|
|
8
|
-
editorActor,
|
|
9
|
-
}: {
|
|
10
|
-
editor: PortableTextSlateEditor
|
|
11
|
-
editorActor: EditorActor
|
|
12
|
-
}) {
|
|
13
|
-
const updateSelection = () => {
|
|
14
|
-
if (editor.selection) {
|
|
15
|
-
const existingSelection = SLATE_TO_PORTABLE_TEXT_RANGE.get(
|
|
16
|
-
editor.selection,
|
|
17
|
-
)
|
|
18
|
-
|
|
19
|
-
if (existingSelection) {
|
|
20
|
-
editorActor.send({
|
|
21
|
-
type: 'update selection',
|
|
22
|
-
selection: existingSelection,
|
|
23
|
-
})
|
|
24
|
-
} else {
|
|
25
|
-
const selection = slateRangeToSelection({
|
|
26
|
-
schema: editorActor.getSnapshot().context.schema,
|
|
27
|
-
editor,
|
|
28
|
-
range: editor.selection,
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
SLATE_TO_PORTABLE_TEXT_RANGE.set(editor.selection, selection)
|
|
32
|
-
|
|
33
|
-
editorActor.send({type: 'update selection', selection})
|
|
34
|
-
}
|
|
35
|
-
} else {
|
|
36
|
-
editorActor.send({type: 'update selection', selection: null})
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const {onChange} = editor
|
|
41
|
-
|
|
42
|
-
editor.onChange = () => {
|
|
43
|
-
onChange()
|
|
44
|
-
|
|
45
|
-
if (!editorActor.getSnapshot().matches({setup: 'setting up'})) {
|
|
46
|
-
updateSelection()
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return editor
|
|
51
|
-
}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import {applyOperationToPortableText} from '../../internal-utils/apply-operation-to-portable-text'
|
|
2
|
-
import {buildIndexMaps} from '../../internal-utils/build-index-maps'
|
|
3
|
-
import type {PortableTextSlateEditor} from '../../types/slate-editor'
|
|
4
|
-
import type {EditorContext} from '../editor-snapshot'
|
|
5
|
-
|
|
6
|
-
export function pluginUpdateValue(
|
|
7
|
-
context: Pick<EditorContext, 'keyGenerator' | 'schema'>,
|
|
8
|
-
editor: PortableTextSlateEditor,
|
|
9
|
-
) {
|
|
10
|
-
const {apply} = editor
|
|
11
|
-
|
|
12
|
-
editor.apply = (operation) => {
|
|
13
|
-
if (operation.type === 'set_selection') {
|
|
14
|
-
apply(operation)
|
|
15
|
-
return
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
editor.value = applyOperationToPortableText(
|
|
19
|
-
context,
|
|
20
|
-
editor.value,
|
|
21
|
-
operation,
|
|
22
|
-
)
|
|
23
|
-
|
|
24
|
-
if (operation.type === 'insert_text' || operation.type === 'remove_text') {
|
|
25
|
-
// Inserting and removing text has no effect on index maps so there is
|
|
26
|
-
// no need to rebuild those.
|
|
27
|
-
apply(operation)
|
|
28
|
-
return
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
buildIndexMaps(
|
|
32
|
-
{
|
|
33
|
-
schema: context.schema,
|
|
34
|
-
value: editor.value,
|
|
35
|
-
},
|
|
36
|
-
{
|
|
37
|
-
blockIndexMap: editor.blockIndexMap,
|
|
38
|
-
listIndexMap: editor.listIndexMap,
|
|
39
|
-
},
|
|
40
|
-
)
|
|
41
|
-
|
|
42
|
-
apply(operation)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return editor
|
|
46
|
-
}
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import type {BaseOperation, Editor, Node, NodeEntry} from 'slate'
|
|
2
|
-
import {pluginHistory} from '../../history/slate-plugin.history'
|
|
3
|
-
import type {PortableTextSlateEditor} from '../../types/slate-editor'
|
|
4
|
-
import type {EditorActor} from '../editor-machine'
|
|
5
|
-
import type {RelayActor} from '../relay-machine'
|
|
6
|
-
import {createWithEventListeners} from './create-with-event-listeners'
|
|
7
|
-
import {createWithObjectKeys} from './createWithObjectKeys'
|
|
8
|
-
import {createWithPatches} from './createWithPatches'
|
|
9
|
-
import {createWithPortableTextMarkModel} from './createWithPortableTextMarkModel'
|
|
10
|
-
import {createWithSchemaTypes} from './createWithSchemaTypes'
|
|
11
|
-
import {pluginUpdateSelection} from './slate-plugin.update-selection'
|
|
12
|
-
import {pluginUpdateValue} from './slate-plugin.update-value'
|
|
13
|
-
|
|
14
|
-
export interface OriginalEditorFunctions {
|
|
15
|
-
apply: (operation: BaseOperation) => void
|
|
16
|
-
onChange: () => void
|
|
17
|
-
normalizeNode: (entry: NodeEntry<Node>) => void
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
type PluginsOptions = {
|
|
21
|
-
editorActor: EditorActor
|
|
22
|
-
relayActor: RelayActor
|
|
23
|
-
subscriptions: Array<() => () => void>
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export const withPlugins = <T extends Editor>(
|
|
27
|
-
editor: T,
|
|
28
|
-
options: PluginsOptions,
|
|
29
|
-
): PortableTextSlateEditor => {
|
|
30
|
-
const e = editor as T & PortableTextSlateEditor
|
|
31
|
-
const {editorActor, relayActor} = options
|
|
32
|
-
const withObjectKeys = createWithObjectKeys(editorActor)
|
|
33
|
-
const withSchemaTypes = createWithSchemaTypes({
|
|
34
|
-
editorActor,
|
|
35
|
-
})
|
|
36
|
-
const withPatches = createWithPatches({
|
|
37
|
-
editorActor,
|
|
38
|
-
relayActor,
|
|
39
|
-
subscriptions: options.subscriptions,
|
|
40
|
-
})
|
|
41
|
-
const withUndoRedo = pluginHistory({
|
|
42
|
-
editorActor,
|
|
43
|
-
subscriptions: options.subscriptions,
|
|
44
|
-
})
|
|
45
|
-
const withPortableTextMarkModel = createWithPortableTextMarkModel(editorActor)
|
|
46
|
-
|
|
47
|
-
const withEventListeners = createWithEventListeners(editorActor)
|
|
48
|
-
|
|
49
|
-
// Ordering is important here, selection dealing last, data manipulation in the middle and core model stuff first.
|
|
50
|
-
return withEventListeners(
|
|
51
|
-
withSchemaTypes(
|
|
52
|
-
withObjectKeys(
|
|
53
|
-
withPortableTextMarkModel(
|
|
54
|
-
withUndoRedo(
|
|
55
|
-
withPatches(
|
|
56
|
-
pluginUpdateValue(
|
|
57
|
-
editorActor.getSnapshot().context,
|
|
58
|
-
pluginUpdateSelection({
|
|
59
|
-
editorActor,
|
|
60
|
-
editor: e,
|
|
61
|
-
}),
|
|
62
|
-
),
|
|
63
|
-
),
|
|
64
|
-
),
|
|
65
|
-
),
|
|
66
|
-
),
|
|
67
|
-
),
|
|
68
|
-
)
|
|
69
|
-
}
|
|
@@ -1,421 +0,0 @@
|
|
|
1
|
-
import {isEqual} from 'lodash'
|
|
2
|
-
import {
|
|
3
|
-
Element,
|
|
4
|
-
Path,
|
|
5
|
-
Range,
|
|
6
|
-
type BaseRange,
|
|
7
|
-
type NodeEntry,
|
|
8
|
-
type Operation,
|
|
9
|
-
} from 'slate'
|
|
10
|
-
import {
|
|
11
|
-
and,
|
|
12
|
-
assign,
|
|
13
|
-
fromCallback,
|
|
14
|
-
setup,
|
|
15
|
-
type AnyEventObject,
|
|
16
|
-
type CallbackLogicFunction,
|
|
17
|
-
} from 'xstate'
|
|
18
|
-
import {moveRangeByOperation} from '../internal-utils/move-range-by-operation'
|
|
19
|
-
import {slateRangeToSelection} from '../internal-utils/slate-utils'
|
|
20
|
-
import {toSlateRange} from '../internal-utils/to-slate-range'
|
|
21
|
-
import type {RangeDecoration} from '../types/editor'
|
|
22
|
-
import type {PortableTextSlateEditor} from '../types/slate-editor'
|
|
23
|
-
import {isEmptyTextBlock} from '../utils'
|
|
24
|
-
import type {EditorSchema} from './editor-schema'
|
|
25
|
-
|
|
26
|
-
const slateOperationCallback: CallbackLogicFunction<
|
|
27
|
-
AnyEventObject,
|
|
28
|
-
{type: 'slate operation'; operation: Operation},
|
|
29
|
-
{slateEditor: PortableTextSlateEditor}
|
|
30
|
-
> = ({input, sendBack}) => {
|
|
31
|
-
const originalApply = input.slateEditor.apply
|
|
32
|
-
|
|
33
|
-
input.slateEditor.apply = (op) => {
|
|
34
|
-
if (op.type !== 'set_selection') {
|
|
35
|
-
sendBack({type: 'slate operation', operation: op})
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
originalApply(op)
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return () => {
|
|
42
|
-
input.slateEditor.apply = originalApply
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export type DecoratedRange = BaseRange & {rangeDecoration: RangeDecoration}
|
|
47
|
-
|
|
48
|
-
export const rangeDecorationsMachine = setup({
|
|
49
|
-
types: {
|
|
50
|
-
context: {} as {
|
|
51
|
-
pendingRangeDecorations: Array<RangeDecoration>
|
|
52
|
-
skipSetup: boolean
|
|
53
|
-
readOnly: boolean
|
|
54
|
-
schema: EditorSchema
|
|
55
|
-
slateEditor: PortableTextSlateEditor
|
|
56
|
-
decorate: {fn: (nodeEntry: NodeEntry) => Array<BaseRange>}
|
|
57
|
-
},
|
|
58
|
-
input: {} as {
|
|
59
|
-
rangeDecorations: Array<RangeDecoration>
|
|
60
|
-
readOnly: boolean
|
|
61
|
-
schema: EditorSchema
|
|
62
|
-
skipSetup: boolean
|
|
63
|
-
slateEditor: PortableTextSlateEditor
|
|
64
|
-
},
|
|
65
|
-
events: {} as
|
|
66
|
-
| {
|
|
67
|
-
type: 'ready'
|
|
68
|
-
}
|
|
69
|
-
| {
|
|
70
|
-
type: 'range decorations updated'
|
|
71
|
-
rangeDecorations: Array<RangeDecoration>
|
|
72
|
-
}
|
|
73
|
-
| {
|
|
74
|
-
type: 'slate operation'
|
|
75
|
-
operation: Operation
|
|
76
|
-
}
|
|
77
|
-
| {
|
|
78
|
-
type: 'update read only'
|
|
79
|
-
readOnly: boolean
|
|
80
|
-
},
|
|
81
|
-
},
|
|
82
|
-
actions: {
|
|
83
|
-
'update pending range decorations': assign({
|
|
84
|
-
pendingRangeDecorations: ({context, event}) => {
|
|
85
|
-
if (event.type !== 'range decorations updated') {
|
|
86
|
-
return context.pendingRangeDecorations
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return event.rangeDecorations
|
|
90
|
-
},
|
|
91
|
-
}),
|
|
92
|
-
'set up initial range decorations': ({context}) => {
|
|
93
|
-
const rangeDecorationState: Array<DecoratedRange> = []
|
|
94
|
-
|
|
95
|
-
for (const rangeDecoration of context.pendingRangeDecorations) {
|
|
96
|
-
const slateRange = toSlateRange({
|
|
97
|
-
context: {
|
|
98
|
-
schema: context.schema,
|
|
99
|
-
value: context.slateEditor.value,
|
|
100
|
-
selection: rangeDecoration.selection,
|
|
101
|
-
},
|
|
102
|
-
blockIndexMap: context.slateEditor.blockIndexMap,
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
if (!Range.isRange(slateRange)) {
|
|
106
|
-
rangeDecoration.onMoved?.({
|
|
107
|
-
newSelection: null,
|
|
108
|
-
rangeDecoration,
|
|
109
|
-
origin: 'local',
|
|
110
|
-
})
|
|
111
|
-
continue
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
rangeDecorationState.push({
|
|
115
|
-
rangeDecoration,
|
|
116
|
-
...slateRange,
|
|
117
|
-
})
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
context.slateEditor.decoratedRanges = rangeDecorationState
|
|
121
|
-
},
|
|
122
|
-
'update range decorations': ({context, event}) => {
|
|
123
|
-
if (event.type !== 'range decorations updated') {
|
|
124
|
-
return
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
const rangeDecorationState: Array<DecoratedRange> = []
|
|
128
|
-
|
|
129
|
-
for (const rangeDecoration of event.rangeDecorations) {
|
|
130
|
-
const slateRange = toSlateRange({
|
|
131
|
-
context: {
|
|
132
|
-
schema: context.schema,
|
|
133
|
-
value: context.slateEditor.value,
|
|
134
|
-
selection: rangeDecoration.selection,
|
|
135
|
-
},
|
|
136
|
-
blockIndexMap: context.slateEditor.blockIndexMap,
|
|
137
|
-
})
|
|
138
|
-
|
|
139
|
-
if (!Range.isRange(slateRange)) {
|
|
140
|
-
rangeDecoration.onMoved?.({
|
|
141
|
-
newSelection: null,
|
|
142
|
-
rangeDecoration,
|
|
143
|
-
origin: 'local',
|
|
144
|
-
})
|
|
145
|
-
continue
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
rangeDecorationState.push({
|
|
149
|
-
rangeDecoration,
|
|
150
|
-
...slateRange,
|
|
151
|
-
})
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
context.slateEditor.decoratedRanges = rangeDecorationState
|
|
155
|
-
},
|
|
156
|
-
|
|
157
|
-
'move range decorations': ({context, event}) => {
|
|
158
|
-
if (event.type !== 'slate operation') {
|
|
159
|
-
return
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
const rangeDecorationState: Array<DecoratedRange> = []
|
|
163
|
-
|
|
164
|
-
for (const decoratedRange of context.slateEditor.decoratedRanges) {
|
|
165
|
-
const slateRange = toSlateRange({
|
|
166
|
-
context: {
|
|
167
|
-
schema: context.schema,
|
|
168
|
-
value: context.slateEditor.value,
|
|
169
|
-
selection: decoratedRange.rangeDecoration.selection,
|
|
170
|
-
},
|
|
171
|
-
blockIndexMap: context.slateEditor.blockIndexMap,
|
|
172
|
-
})
|
|
173
|
-
|
|
174
|
-
if (!Range.isRange(slateRange)) {
|
|
175
|
-
decoratedRange.rangeDecoration.onMoved?.({
|
|
176
|
-
newSelection: null,
|
|
177
|
-
rangeDecoration: decoratedRange.rangeDecoration,
|
|
178
|
-
origin: 'local',
|
|
179
|
-
})
|
|
180
|
-
continue
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
let newRange: BaseRange | null | undefined
|
|
184
|
-
|
|
185
|
-
newRange = moveRangeByOperation(slateRange, event.operation)
|
|
186
|
-
if (
|
|
187
|
-
(newRange && newRange !== slateRange) ||
|
|
188
|
-
(newRange === null && slateRange)
|
|
189
|
-
) {
|
|
190
|
-
const newRangeSelection = newRange
|
|
191
|
-
? slateRangeToSelection({
|
|
192
|
-
schema: context.schema,
|
|
193
|
-
editor: context.slateEditor,
|
|
194
|
-
range: newRange,
|
|
195
|
-
})
|
|
196
|
-
: null
|
|
197
|
-
|
|
198
|
-
decoratedRange.rangeDecoration.onMoved?.({
|
|
199
|
-
newSelection: newRangeSelection,
|
|
200
|
-
rangeDecoration: decoratedRange.rangeDecoration,
|
|
201
|
-
origin: 'local',
|
|
202
|
-
})
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
// If the newRange is null, it means that the range is not valid anymore and should be removed
|
|
206
|
-
// If it's undefined, it means that the slateRange is still valid and should be kept
|
|
207
|
-
if (newRange !== null) {
|
|
208
|
-
rangeDecorationState.push({
|
|
209
|
-
...(newRange || slateRange),
|
|
210
|
-
rangeDecoration: {
|
|
211
|
-
...decoratedRange.rangeDecoration,
|
|
212
|
-
selection: slateRangeToSelection({
|
|
213
|
-
schema: context.schema,
|
|
214
|
-
editor: context.slateEditor,
|
|
215
|
-
range: newRange,
|
|
216
|
-
}),
|
|
217
|
-
},
|
|
218
|
-
})
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
context.slateEditor.decoratedRanges = rangeDecorationState
|
|
223
|
-
},
|
|
224
|
-
'assign readOnly': assign({
|
|
225
|
-
readOnly: ({context, event}) => {
|
|
226
|
-
if (event.type !== 'update read only') {
|
|
227
|
-
return context.readOnly
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
return event.readOnly
|
|
231
|
-
},
|
|
232
|
-
}),
|
|
233
|
-
'update decorate': assign({
|
|
234
|
-
decorate: ({context}) => {
|
|
235
|
-
return {
|
|
236
|
-
fn: createDecorate(context.schema, context.slateEditor),
|
|
237
|
-
}
|
|
238
|
-
},
|
|
239
|
-
}),
|
|
240
|
-
},
|
|
241
|
-
actors: {
|
|
242
|
-
'slate operation listener': fromCallback(slateOperationCallback),
|
|
243
|
-
},
|
|
244
|
-
guards: {
|
|
245
|
-
'has pending range decorations': ({context}) =>
|
|
246
|
-
context.pendingRangeDecorations.length > 0,
|
|
247
|
-
'has range decorations': ({context}) =>
|
|
248
|
-
context.slateEditor.decoratedRanges.length > 0,
|
|
249
|
-
'has different decorations': ({context, event}) => {
|
|
250
|
-
if (event.type !== 'range decorations updated') {
|
|
251
|
-
return false
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
const existingRangeDecorations = context.slateEditor.decoratedRanges.map(
|
|
255
|
-
(decoratedRange) => ({
|
|
256
|
-
anchor: decoratedRange.rangeDecoration.selection?.anchor,
|
|
257
|
-
focus: decoratedRange.rangeDecoration.selection?.focus,
|
|
258
|
-
payload: decoratedRange.rangeDecoration.payload,
|
|
259
|
-
}),
|
|
260
|
-
)
|
|
261
|
-
|
|
262
|
-
const newRangeDecorations = event.rangeDecorations.map(
|
|
263
|
-
(rangeDecoration) => ({
|
|
264
|
-
anchor: rangeDecoration.selection?.anchor,
|
|
265
|
-
focus: rangeDecoration.selection?.focus,
|
|
266
|
-
payload: rangeDecoration.payload,
|
|
267
|
-
}),
|
|
268
|
-
)
|
|
269
|
-
|
|
270
|
-
const different = !isEqual(existingRangeDecorations, newRangeDecorations)
|
|
271
|
-
|
|
272
|
-
return different
|
|
273
|
-
},
|
|
274
|
-
'not read only': ({context}) => !context.readOnly,
|
|
275
|
-
'should skip setup': ({context}) => context.skipSetup,
|
|
276
|
-
},
|
|
277
|
-
}).createMachine({
|
|
278
|
-
id: 'range decorations',
|
|
279
|
-
context: ({input}) => ({
|
|
280
|
-
readOnly: input.readOnly,
|
|
281
|
-
pendingRangeDecorations: input.rangeDecorations,
|
|
282
|
-
decoratedRanges: [],
|
|
283
|
-
skipSetup: input.skipSetup,
|
|
284
|
-
schema: input.schema,
|
|
285
|
-
slateEditor: input.slateEditor,
|
|
286
|
-
decorate: {
|
|
287
|
-
fn: createDecorate(input.schema, input.slateEditor),
|
|
288
|
-
},
|
|
289
|
-
}),
|
|
290
|
-
invoke: {
|
|
291
|
-
src: 'slate operation listener',
|
|
292
|
-
input: ({context}) => ({slateEditor: context.slateEditor}),
|
|
293
|
-
},
|
|
294
|
-
on: {
|
|
295
|
-
'update read only': {
|
|
296
|
-
actions: ['assign readOnly'],
|
|
297
|
-
},
|
|
298
|
-
},
|
|
299
|
-
initial: 'setting up',
|
|
300
|
-
states: {
|
|
301
|
-
'setting up': {
|
|
302
|
-
always: [
|
|
303
|
-
{
|
|
304
|
-
guard: and(['should skip setup', 'has pending range decorations']),
|
|
305
|
-
target: 'ready',
|
|
306
|
-
actions: ['set up initial range decorations', 'update decorate'],
|
|
307
|
-
},
|
|
308
|
-
{
|
|
309
|
-
guard: 'should skip setup',
|
|
310
|
-
target: 'ready',
|
|
311
|
-
},
|
|
312
|
-
],
|
|
313
|
-
on: {
|
|
314
|
-
'range decorations updated': {
|
|
315
|
-
actions: ['update pending range decorations'],
|
|
316
|
-
},
|
|
317
|
-
'ready': [
|
|
318
|
-
{
|
|
319
|
-
target: 'ready',
|
|
320
|
-
guard: 'has pending range decorations',
|
|
321
|
-
actions: ['set up initial range decorations', 'update decorate'],
|
|
322
|
-
},
|
|
323
|
-
{
|
|
324
|
-
target: 'ready',
|
|
325
|
-
},
|
|
326
|
-
],
|
|
327
|
-
},
|
|
328
|
-
},
|
|
329
|
-
'ready': {
|
|
330
|
-
initial: 'idle',
|
|
331
|
-
on: {
|
|
332
|
-
'range decorations updated': {
|
|
333
|
-
target: '.idle',
|
|
334
|
-
guard: 'has different decorations',
|
|
335
|
-
actions: ['update range decorations', 'update decorate'],
|
|
336
|
-
},
|
|
337
|
-
},
|
|
338
|
-
states: {
|
|
339
|
-
'idle': {
|
|
340
|
-
on: {
|
|
341
|
-
'slate operation': {
|
|
342
|
-
target: 'moving range decorations',
|
|
343
|
-
guard: and(['has range decorations', 'not read only']),
|
|
344
|
-
},
|
|
345
|
-
},
|
|
346
|
-
},
|
|
347
|
-
'moving range decorations': {
|
|
348
|
-
entry: ['move range decorations'],
|
|
349
|
-
always: {
|
|
350
|
-
target: 'idle',
|
|
351
|
-
},
|
|
352
|
-
},
|
|
353
|
-
},
|
|
354
|
-
},
|
|
355
|
-
},
|
|
356
|
-
})
|
|
357
|
-
|
|
358
|
-
function createDecorate(
|
|
359
|
-
schema: EditorSchema,
|
|
360
|
-
slateEditor: PortableTextSlateEditor,
|
|
361
|
-
) {
|
|
362
|
-
return function decorate([node, path]: NodeEntry): Array<BaseRange> {
|
|
363
|
-
const defaultStyle = schema.styles.at(0)?.name
|
|
364
|
-
const editorOnlyContainsEmptyParagraph =
|
|
365
|
-
slateEditor.value.length === 1 &&
|
|
366
|
-
isEmptyTextBlock({schema}, slateEditor.value[0]) &&
|
|
367
|
-
(!slateEditor.value[0].style ||
|
|
368
|
-
slateEditor.value[0].style === defaultStyle) &&
|
|
369
|
-
!slateEditor.value[0].listItem
|
|
370
|
-
|
|
371
|
-
if (editorOnlyContainsEmptyParagraph) {
|
|
372
|
-
return [
|
|
373
|
-
{
|
|
374
|
-
anchor: {
|
|
375
|
-
path: [0, 0],
|
|
376
|
-
offset: 0,
|
|
377
|
-
},
|
|
378
|
-
focus: {
|
|
379
|
-
path: [0, 0],
|
|
380
|
-
offset: 0,
|
|
381
|
-
},
|
|
382
|
-
placeholder: true,
|
|
383
|
-
} as BaseRange,
|
|
384
|
-
]
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
// Editor node has a path length of 0 (should never be decorated)
|
|
388
|
-
if (path.length === 0) {
|
|
389
|
-
return []
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
if (!Element.isElement(node) || node.children.length === 0) {
|
|
393
|
-
return []
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
const blockIndex = path.at(0)
|
|
397
|
-
|
|
398
|
-
if (blockIndex === undefined) {
|
|
399
|
-
return []
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
return slateEditor.decoratedRanges.filter((decoratedRange) => {
|
|
403
|
-
// Special case in order to only return one decoration for collapsed ranges
|
|
404
|
-
if (Range.isCollapsed(decoratedRange)) {
|
|
405
|
-
// Collapsed ranges should only be decorated if they are on a block child level (length 2)
|
|
406
|
-
return node.children.some(
|
|
407
|
-
(_, childIndex) =>
|
|
408
|
-
Path.equals(decoratedRange.anchor.path, [blockIndex, childIndex]) &&
|
|
409
|
-
Path.equals(decoratedRange.focus.path, [blockIndex, childIndex]),
|
|
410
|
-
)
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
return (
|
|
414
|
-
Range.intersection(decoratedRange, {
|
|
415
|
-
anchor: {path, offset: 0},
|
|
416
|
-
focus: {path, offset: 0},
|
|
417
|
-
}) || Range.includes(decoratedRange, path)
|
|
418
|
-
)
|
|
419
|
-
})
|
|
420
|
-
}
|
|
421
|
-
}
|