@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
package/src/editor/use-editor.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import React from 'react'
|
|
2
|
-
import {EditorContext} from './editor-context'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @public
|
|
6
|
-
* Get the current editor context from the `EditorProvider`.
|
|
7
|
-
* Must be used inside the `EditorProvider` component.
|
|
8
|
-
* @returns The current editor object.
|
|
9
|
-
* @example
|
|
10
|
-
* ```tsx
|
|
11
|
-
* import { useEditor } from '@portabletext/editor'
|
|
12
|
-
*
|
|
13
|
-
* function MyComponent() {
|
|
14
|
-
* const editor = useEditor()
|
|
15
|
-
* }
|
|
16
|
-
* ```
|
|
17
|
-
* @group Hooks
|
|
18
|
-
*/
|
|
19
|
-
export function useEditor() {
|
|
20
|
-
const editor = React.useContext(EditorContext)
|
|
21
|
-
|
|
22
|
-
if (!editor) {
|
|
23
|
-
throw new Error('No Editor set. Use EditorProvider to set one.')
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
return editor
|
|
27
|
-
}
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import {getTersePt} from '@portabletext/test'
|
|
2
|
-
import {describe, expect, test, vi} from 'vitest'
|
|
3
|
-
import {userEvent} from 'vitest/browser'
|
|
4
|
-
import {getSelectionAfterText} from '../internal-utils/text-selection'
|
|
5
|
-
import {createTestEditor} from '../test/vitest'
|
|
6
|
-
import {validateSelectionMachine} from './validate-selection-machine'
|
|
7
|
-
|
|
8
|
-
describe(validateSelectionMachine.id, () => {
|
|
9
|
-
test('Scenario: Does not validate selection while Slate has pending operations', async () => {
|
|
10
|
-
const {editor, locator} = await createTestEditor()
|
|
11
|
-
|
|
12
|
-
await userEvent.click(locator)
|
|
13
|
-
|
|
14
|
-
editor.send({type: 'insert.text', text: 'foo'})
|
|
15
|
-
|
|
16
|
-
await vi.waitFor(() => {
|
|
17
|
-
expect(getTersePt(editor.getSnapshot().context)).toEqual(['foo'])
|
|
18
|
-
expect(editor.getSnapshot().context.selection).toEqual(
|
|
19
|
-
getSelectionAfterText(editor.getSnapshot().context, 'foo'),
|
|
20
|
-
)
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
// This event is being sent in before "foo" has been inserted in the DOM
|
|
24
|
-
// This means that when the MutationObserver is finally triggered for the
|
|
25
|
-
// "foo" insertion, "bar" will be in the Slate state but not in the DOM.
|
|
26
|
-
// This causes the selection to be out of sync and this is why we need to
|
|
27
|
-
// make sure the selection is not validated before Slate has committed all
|
|
28
|
-
// pending operations.
|
|
29
|
-
editor.send({type: 'insert.text', text: 'bar'})
|
|
30
|
-
|
|
31
|
-
await vi.waitFor(() => {
|
|
32
|
-
expect(getTersePt(editor.getSnapshot().context)).toEqual(['foobar'])
|
|
33
|
-
expect(editor.getSnapshot().context.selection).toEqual(
|
|
34
|
-
getSelectionAfterText(editor.getSnapshot().context, 'foobar'),
|
|
35
|
-
)
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
editor.send({type: 'delete.backward', unit: 'character'})
|
|
39
|
-
|
|
40
|
-
await vi.waitFor(() => {
|
|
41
|
-
expect(getTersePt(editor.getSnapshot().context)).toEqual(['fooba'])
|
|
42
|
-
expect(editor.getSnapshot().context.selection).toEqual(
|
|
43
|
-
getSelectionAfterText(editor.getSnapshot().context, 'fooba'),
|
|
44
|
-
)
|
|
45
|
-
})
|
|
46
|
-
})
|
|
47
|
-
})
|
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import {Editor, Transforms} from 'slate'
|
|
2
|
-
import {ReactEditor} from 'slate-react'
|
|
3
|
-
import {setup} from 'xstate'
|
|
4
|
-
import {debugWithName} from '../internal-utils/debug'
|
|
5
|
-
import type {PortableTextSlateEditor} from '../types/slate-editor'
|
|
6
|
-
|
|
7
|
-
const debug = debugWithName('validate selection machine')
|
|
8
|
-
|
|
9
|
-
const validateSelectionSetup = setup({
|
|
10
|
-
types: {
|
|
11
|
-
context: {} as {
|
|
12
|
-
slateEditor: PortableTextSlateEditor
|
|
13
|
-
},
|
|
14
|
-
input: {} as {
|
|
15
|
-
slateEditor: PortableTextSlateEditor
|
|
16
|
-
},
|
|
17
|
-
events: {} as {
|
|
18
|
-
type: 'validate selection'
|
|
19
|
-
editorElement: HTMLDivElement
|
|
20
|
-
},
|
|
21
|
-
},
|
|
22
|
-
guards: {
|
|
23
|
-
'pending operations': ({context}) =>
|
|
24
|
-
context.slateEditor.operations.length > 0,
|
|
25
|
-
},
|
|
26
|
-
})
|
|
27
|
-
|
|
28
|
-
const validateSelectionAction = validateSelectionSetup.createAction(
|
|
29
|
-
({context, event}) => {
|
|
30
|
-
validateSelection(context.slateEditor, event.editorElement)
|
|
31
|
-
},
|
|
32
|
-
)
|
|
33
|
-
|
|
34
|
-
export const validateSelectionMachine = validateSelectionSetup.createMachine({
|
|
35
|
-
id: 'validate selection',
|
|
36
|
-
context: ({input}) => ({
|
|
37
|
-
slateEditor: input.slateEditor,
|
|
38
|
-
}),
|
|
39
|
-
initial: 'idle',
|
|
40
|
-
states: {
|
|
41
|
-
idle: {
|
|
42
|
-
on: {
|
|
43
|
-
'validate selection': [
|
|
44
|
-
{
|
|
45
|
-
guard: 'pending operations',
|
|
46
|
-
target: 'waiting',
|
|
47
|
-
},
|
|
48
|
-
{
|
|
49
|
-
actions: [validateSelectionAction],
|
|
50
|
-
target: 'idle',
|
|
51
|
-
},
|
|
52
|
-
],
|
|
53
|
-
},
|
|
54
|
-
},
|
|
55
|
-
waiting: {
|
|
56
|
-
after: {
|
|
57
|
-
0: [
|
|
58
|
-
{
|
|
59
|
-
guard: 'pending operations',
|
|
60
|
-
target: '.',
|
|
61
|
-
reenter: true,
|
|
62
|
-
},
|
|
63
|
-
{
|
|
64
|
-
target: 'idle',
|
|
65
|
-
actions: [validateSelectionAction],
|
|
66
|
-
},
|
|
67
|
-
],
|
|
68
|
-
},
|
|
69
|
-
on: {
|
|
70
|
-
'validate selection': {
|
|
71
|
-
target: '.',
|
|
72
|
-
reenter: true,
|
|
73
|
-
},
|
|
74
|
-
},
|
|
75
|
-
},
|
|
76
|
-
},
|
|
77
|
-
})
|
|
78
|
-
|
|
79
|
-
// This function will handle unexpected DOM changes inside the Editable rendering,
|
|
80
|
-
// and make sure that we can maintain a stable slateEditor.selection when that happens.
|
|
81
|
-
//
|
|
82
|
-
// For example, if this Editable is rendered inside something that might re-render
|
|
83
|
-
// this component (hidden contexts) while the user is still actively changing the
|
|
84
|
-
// contentEditable, this could interfere with the intermediate DOM selection,
|
|
85
|
-
// which again could be picked up by ReactEditor's event listeners.
|
|
86
|
-
// If that range is invalid at that point, the slate.editorSelection could be
|
|
87
|
-
// set either wrong, or invalid, to which slateEditor will throw exceptions
|
|
88
|
-
// that are impossible to recover properly from or result in a wrong selection.
|
|
89
|
-
//
|
|
90
|
-
// Also the other way around, when the ReactEditor will try to create a DOM Range
|
|
91
|
-
// from the current slateEditor.selection, it may throw unrecoverable errors
|
|
92
|
-
// if the current editor.selection is invalid according to the DOM.
|
|
93
|
-
// If this is the case, default to selecting the top of the document, if the
|
|
94
|
-
// user already had a selection.
|
|
95
|
-
function validateSelection(
|
|
96
|
-
slateEditor: PortableTextSlateEditor,
|
|
97
|
-
editorElement: HTMLDivElement,
|
|
98
|
-
) {
|
|
99
|
-
if (!slateEditor.selection) {
|
|
100
|
-
return
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
let root: Document | ShadowRoot | undefined
|
|
104
|
-
|
|
105
|
-
try {
|
|
106
|
-
root = ReactEditor.findDocumentOrShadowRoot(slateEditor)
|
|
107
|
-
} catch {}
|
|
108
|
-
|
|
109
|
-
if (!root) {
|
|
110
|
-
// The editor has most likely been unmounted
|
|
111
|
-
return
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// Return if the editor isn't the active element
|
|
115
|
-
if (editorElement !== root.activeElement) {
|
|
116
|
-
return
|
|
117
|
-
}
|
|
118
|
-
const window = ReactEditor.getWindow(slateEditor)
|
|
119
|
-
const domSelection = window.getSelection()
|
|
120
|
-
if (!domSelection || domSelection.rangeCount === 0) {
|
|
121
|
-
return
|
|
122
|
-
}
|
|
123
|
-
const existingDOMRange = domSelection.getRangeAt(0)
|
|
124
|
-
try {
|
|
125
|
-
const newDOMRange = ReactEditor.toDOMRange(
|
|
126
|
-
slateEditor,
|
|
127
|
-
slateEditor.selection,
|
|
128
|
-
)
|
|
129
|
-
if (
|
|
130
|
-
newDOMRange.startOffset !== existingDOMRange.startOffset ||
|
|
131
|
-
newDOMRange.endOffset !== existingDOMRange.endOffset
|
|
132
|
-
) {
|
|
133
|
-
debug('DOM range out of sync, validating selection')
|
|
134
|
-
// Remove all ranges temporary
|
|
135
|
-
domSelection?.removeAllRanges()
|
|
136
|
-
// Set the correct range
|
|
137
|
-
domSelection.addRange(newDOMRange)
|
|
138
|
-
}
|
|
139
|
-
} catch {
|
|
140
|
-
debug(`Could not resolve selection, selecting top document`)
|
|
141
|
-
// Deselect the editor
|
|
142
|
-
Transforms.deselect(slateEditor)
|
|
143
|
-
// Select top document if there is a top block to select
|
|
144
|
-
if (slateEditor.children.length > 0) {
|
|
145
|
-
Transforms.select(slateEditor, Editor.start(slateEditor, []))
|
|
146
|
-
}
|
|
147
|
-
slateEditor.onChange()
|
|
148
|
-
}
|
|
149
|
-
}
|
package/src/editor/weakMaps.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type {Editor, Range} from 'slate'
|
|
2
|
-
import type {EditorSelection} from '../types/editor'
|
|
3
|
-
|
|
4
|
-
// Is the editor currently receiving remote changes that are being applied to the content?
|
|
5
|
-
export const IS_PROCESSING_REMOTE_CHANGES: WeakMap<Editor, boolean> =
|
|
6
|
-
new WeakMap()
|
|
7
|
-
|
|
8
|
-
export const KEY_TO_SLATE_ELEMENT: WeakMap<Editor, any | undefined> =
|
|
9
|
-
new WeakMap()
|
|
10
|
-
|
|
11
|
-
// Keep object relation to slate range in the portable-text-range
|
|
12
|
-
export const SLATE_TO_PORTABLE_TEXT_RANGE = new WeakMap<
|
|
13
|
-
Range,
|
|
14
|
-
EditorSelection
|
|
15
|
-
>()
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type {Editor} from 'slate'
|
|
2
|
-
|
|
3
|
-
const IS_NORMALIZING_NODE: WeakMap<Editor, boolean | undefined> = new WeakMap()
|
|
4
|
-
|
|
5
|
-
export function withNormalizeNode(editor: Editor, fn: () => void) {
|
|
6
|
-
const prev = IS_NORMALIZING_NODE.get(editor)
|
|
7
|
-
IS_NORMALIZING_NODE.set(editor, true)
|
|
8
|
-
fn()
|
|
9
|
-
IS_NORMALIZING_NODE.set(editor, prev)
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export function isNormalizingNode(editor: Editor) {
|
|
13
|
-
return IS_NORMALIZING_NODE.get(editor) ?? false
|
|
14
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import type {Editor} from 'slate'
|
|
2
|
-
|
|
3
|
-
const IS_PERFORMING_OPERATION: WeakMap<Editor, boolean | undefined> =
|
|
4
|
-
new WeakMap()
|
|
5
|
-
|
|
6
|
-
export function withPerformingBehaviorOperation(
|
|
7
|
-
editor: Editor,
|
|
8
|
-
fn: () => void,
|
|
9
|
-
) {
|
|
10
|
-
const prev = IS_PERFORMING_OPERATION.get(editor)
|
|
11
|
-
|
|
12
|
-
IS_PERFORMING_OPERATION.set(editor, true)
|
|
13
|
-
|
|
14
|
-
fn()
|
|
15
|
-
|
|
16
|
-
IS_PERFORMING_OPERATION.set(editor, prev)
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function isPerformingBehaviorOperation(editor: Editor) {
|
|
20
|
-
return IS_PERFORMING_OPERATION.get(editor) ?? false
|
|
21
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type {Editor} from 'slate'
|
|
2
|
-
import {IS_PROCESSING_REMOTE_CHANGES} from './weakMaps'
|
|
3
|
-
|
|
4
|
-
export function withRemoteChanges(editor: Editor, fn: () => void): void {
|
|
5
|
-
const prev = isChangingRemotely(editor) || false
|
|
6
|
-
IS_PROCESSING_REMOTE_CHANGES.set(editor, true)
|
|
7
|
-
fn()
|
|
8
|
-
IS_PROCESSING_REMOTE_CHANGES.set(editor, prev)
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function isChangingRemotely(editor: Editor): boolean | undefined {
|
|
12
|
-
return IS_PROCESSING_REMOTE_CHANGES.get(editor)
|
|
13
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type {Editor} from 'slate'
|
|
2
|
-
|
|
3
|
-
export const PATCHING: WeakMap<Editor, boolean | undefined> = new WeakMap()
|
|
4
|
-
|
|
5
|
-
export function withoutPatching(editor: Editor, fn: () => void): void {
|
|
6
|
-
const prev = isPatching(editor)
|
|
7
|
-
PATCHING.set(editor, false)
|
|
8
|
-
fn()
|
|
9
|
-
PATCHING.set(editor, prev)
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export function isPatching(editor: Editor): boolean | undefined {
|
|
13
|
-
return PATCHING.get(editor)
|
|
14
|
-
}
|
package/src/editor.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import type {SchemaDefinition} from '@portabletext/schema'
|
|
2
|
-
import type {
|
|
3
|
-
ArrayDefinition,
|
|
4
|
-
ArraySchemaType,
|
|
5
|
-
PortableTextBlock,
|
|
6
|
-
} from '@sanity/types'
|
|
7
|
-
import type {ActorRef, EventObject, Snapshot} from 'xstate'
|
|
8
|
-
import type {Behavior} from './behaviors/behavior.types.behavior'
|
|
9
|
-
import type {ExternalBehaviorEvent} from './behaviors/behavior.types.event'
|
|
10
|
-
import type {EditorDom} from './editor/editor-dom'
|
|
11
|
-
import type {ExternalEditorEvent} from './editor/editor-machine'
|
|
12
|
-
import type {EditorSnapshot} from './editor/editor-snapshot'
|
|
13
|
-
import type {EditorEmittedEvent} from './editor/relay-machine'
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* @public
|
|
17
|
-
*/
|
|
18
|
-
export type EditorConfig = {
|
|
19
|
-
/**
|
|
20
|
-
* @beta
|
|
21
|
-
*/
|
|
22
|
-
keyGenerator?: () => string
|
|
23
|
-
readOnly?: boolean
|
|
24
|
-
initialValue?: Array<PortableTextBlock>
|
|
25
|
-
} & (
|
|
26
|
-
| {
|
|
27
|
-
schemaDefinition: SchemaDefinition
|
|
28
|
-
schema?: undefined
|
|
29
|
-
}
|
|
30
|
-
| {
|
|
31
|
-
schemaDefinition?: undefined
|
|
32
|
-
schema: ArraySchemaType<PortableTextBlock> | ArrayDefinition
|
|
33
|
-
}
|
|
34
|
-
)
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* @public
|
|
38
|
-
*/
|
|
39
|
-
export type EditorEvent =
|
|
40
|
-
| ExternalEditorEvent
|
|
41
|
-
| ExternalBehaviorEvent
|
|
42
|
-
| {
|
|
43
|
-
type: 'update value'
|
|
44
|
-
value: Array<PortableTextBlock> | undefined
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* @public
|
|
49
|
-
*/
|
|
50
|
-
export type Editor = {
|
|
51
|
-
dom: EditorDom
|
|
52
|
-
getSnapshot: () => EditorSnapshot
|
|
53
|
-
/**
|
|
54
|
-
* @beta
|
|
55
|
-
*/
|
|
56
|
-
registerBehavior: (config: {behavior: Behavior}) => () => void
|
|
57
|
-
send: (event: EditorEvent) => void
|
|
58
|
-
on: ActorRef<Snapshot<unknown>, EventObject, EditorEmittedEvent>['on']
|
|
59
|
-
}
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import {flatten} from 'lodash'
|
|
2
|
-
import {Editor, Transforms} from 'slate'
|
|
3
|
-
import {debugWithName} from '../internal-utils/debug'
|
|
4
|
-
import type {BehaviorOperationImplementation} from '../operations/behavior.operations'
|
|
5
|
-
import {getRemotePatches} from './remote-patches'
|
|
6
|
-
import {pluginRedoing, setIsRedoing} from './slate-plugin.redoing'
|
|
7
|
-
import {
|
|
8
|
-
pluginWithoutHistory,
|
|
9
|
-
setWithHistory,
|
|
10
|
-
} from './slate-plugin.without-history'
|
|
11
|
-
import {transformOperation} from './transform-operation'
|
|
12
|
-
|
|
13
|
-
const debug = debugWithName('behavior.operation.history.redo')
|
|
14
|
-
|
|
15
|
-
export const historyRedoOperationImplementation: BehaviorOperationImplementation<
|
|
16
|
-
'history.redo'
|
|
17
|
-
> = ({operation}) => {
|
|
18
|
-
const editor = operation.editor
|
|
19
|
-
const {redos} = editor.history
|
|
20
|
-
const remotePatches = getRemotePatches(editor)
|
|
21
|
-
|
|
22
|
-
if (redos.length > 0) {
|
|
23
|
-
const step = redos[redos.length - 1]
|
|
24
|
-
debug('Redoing', step)
|
|
25
|
-
if (step.operations.length > 0) {
|
|
26
|
-
const otherPatches = remotePatches.filter(
|
|
27
|
-
(item) => item.time >= step.timestamp,
|
|
28
|
-
)
|
|
29
|
-
let transformedOperations = step.operations
|
|
30
|
-
otherPatches.forEach((item) => {
|
|
31
|
-
transformedOperations = flatten(
|
|
32
|
-
transformedOperations.map((op) =>
|
|
33
|
-
transformOperation(
|
|
34
|
-
editor,
|
|
35
|
-
item.patch,
|
|
36
|
-
op,
|
|
37
|
-
item.snapshot,
|
|
38
|
-
item.previousSnapshot,
|
|
39
|
-
),
|
|
40
|
-
),
|
|
41
|
-
)
|
|
42
|
-
})
|
|
43
|
-
try {
|
|
44
|
-
Editor.withoutNormalizing(editor, () => {
|
|
45
|
-
pluginRedoing(editor, () => {
|
|
46
|
-
pluginWithoutHistory(editor, () => {
|
|
47
|
-
transformedOperations.forEach((op) => {
|
|
48
|
-
editor.apply(op)
|
|
49
|
-
})
|
|
50
|
-
})
|
|
51
|
-
})
|
|
52
|
-
})
|
|
53
|
-
} catch (err) {
|
|
54
|
-
debug('Could not perform redo step', err)
|
|
55
|
-
remotePatches.splice(0, remotePatches.length)
|
|
56
|
-
Transforms.deselect(editor)
|
|
57
|
-
editor.history = {undos: [], redos: []}
|
|
58
|
-
setWithHistory(editor, true)
|
|
59
|
-
setIsRedoing(editor, false)
|
|
60
|
-
editor.onChange()
|
|
61
|
-
return
|
|
62
|
-
}
|
|
63
|
-
editor.history.undos.push(step)
|
|
64
|
-
editor.history.redos.pop()
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import {flatten} from 'lodash'
|
|
2
|
-
import {Editor, Operation, Transforms} from 'slate'
|
|
3
|
-
import {debugWithName} from '../internal-utils/debug'
|
|
4
|
-
import type {BehaviorOperationImplementation} from '../operations/behavior.operations'
|
|
5
|
-
import {getRemotePatches} from './remote-patches'
|
|
6
|
-
import {pluginUndoing, setIsUndoing} from './slate-plugin.undoing'
|
|
7
|
-
import {
|
|
8
|
-
pluginWithoutHistory,
|
|
9
|
-
setWithHistory,
|
|
10
|
-
} from './slate-plugin.without-history'
|
|
11
|
-
import {transformOperation} from './transform-operation'
|
|
12
|
-
|
|
13
|
-
const debug = debugWithName('behavior.operation.history.undo')
|
|
14
|
-
|
|
15
|
-
export const historyUndoOperationImplementation: BehaviorOperationImplementation<
|
|
16
|
-
'history.undo'
|
|
17
|
-
> = ({operation}) => {
|
|
18
|
-
const editor = operation.editor
|
|
19
|
-
const {undos} = editor.history
|
|
20
|
-
const remotePatches = getRemotePatches(editor)
|
|
21
|
-
|
|
22
|
-
if (undos.length > 0) {
|
|
23
|
-
const step = undos[undos.length - 1]
|
|
24
|
-
debug('Undoing', step)
|
|
25
|
-
if (step.operations.length > 0) {
|
|
26
|
-
const otherPatches = remotePatches.filter(
|
|
27
|
-
(item) => item.time >= step.timestamp,
|
|
28
|
-
)
|
|
29
|
-
let transformedOperations = step.operations
|
|
30
|
-
otherPatches.forEach((item) => {
|
|
31
|
-
transformedOperations = flatten(
|
|
32
|
-
transformedOperations.map((op) =>
|
|
33
|
-
transformOperation(
|
|
34
|
-
editor,
|
|
35
|
-
item.patch,
|
|
36
|
-
op,
|
|
37
|
-
item.snapshot,
|
|
38
|
-
item.previousSnapshot,
|
|
39
|
-
),
|
|
40
|
-
),
|
|
41
|
-
)
|
|
42
|
-
})
|
|
43
|
-
const reversedOperations = transformedOperations
|
|
44
|
-
.map(Operation.inverse)
|
|
45
|
-
.reverse()
|
|
46
|
-
|
|
47
|
-
try {
|
|
48
|
-
Editor.withoutNormalizing(editor, () => {
|
|
49
|
-
pluginUndoing(editor, () => {
|
|
50
|
-
pluginWithoutHistory(editor, () => {
|
|
51
|
-
reversedOperations.forEach((op) => {
|
|
52
|
-
editor.apply(op)
|
|
53
|
-
})
|
|
54
|
-
})
|
|
55
|
-
})
|
|
56
|
-
})
|
|
57
|
-
} catch (err) {
|
|
58
|
-
debug('Could not perform undo step', err)
|
|
59
|
-
remotePatches.splice(0, remotePatches.length)
|
|
60
|
-
Transforms.deselect(editor)
|
|
61
|
-
editor.history = {undos: [], redos: []}
|
|
62
|
-
setWithHistory(editor, true)
|
|
63
|
-
setIsUndoing(editor, false)
|
|
64
|
-
editor.onChange()
|
|
65
|
-
return
|
|
66
|
-
}
|
|
67
|
-
editor.history.redos.push(step)
|
|
68
|
-
editor.history.undos.pop()
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|