@portabletext/editor 1.46.0 → 1.47.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/_chunks-cjs/behavior.core.cjs +7 -7
- package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
- package/lib/_chunks-cjs/behavior.markdown.cjs.map +1 -1
- package/lib/_chunks-cjs/editor-provider.cjs +411 -327
- package/lib/_chunks-cjs/editor-provider.cjs.map +1 -1
- package/lib/_chunks-cjs/parse-blocks.cjs +4 -4
- package/lib/_chunks-cjs/parse-blocks.cjs.map +1 -1
- package/lib/_chunks-cjs/selector.is-overlapping-selection.cjs.map +1 -1
- package/lib/_chunks-es/behavior.core.js +7 -7
- package/lib/_chunks-es/behavior.core.js.map +1 -1
- package/lib/_chunks-es/behavior.markdown.js.map +1 -1
- package/lib/_chunks-es/editor-provider.js +412 -328
- package/lib/_chunks-es/editor-provider.js.map +1 -1
- package/lib/_chunks-es/parse-blocks.js +4 -4
- package/lib/_chunks-es/parse-blocks.js.map +1 -1
- package/lib/_chunks-es/selector.is-overlapping-selection.js.map +1 -1
- package/lib/behaviors/index.cjs.map +1 -1
- package/lib/behaviors/index.d.cts +590 -28
- package/lib/behaviors/index.d.ts +590 -28
- package/lib/behaviors/index.js.map +1 -1
- package/lib/index.cjs +4 -4
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +71 -3
- package/lib/index.d.ts +71 -3
- package/lib/index.js +4 -4
- package/lib/index.js.map +1 -1
- package/lib/plugins/index.cjs.map +1 -1
- package/lib/plugins/index.d.cts +78 -23
- package/lib/plugins/index.d.ts +78 -23
- package/lib/plugins/index.js.map +1 -1
- package/lib/selectors/index.d.cts +79 -27
- package/lib/selectors/index.d.ts +79 -27
- package/lib/utils/index.d.cts +84 -29
- package/lib/utils/index.d.ts +84 -29
- package/package.json +6 -6
- package/src/behavior-actions/behavior.action.insert.block.ts +1 -1
- package/src/behavior-actions/behavior.action.move.backward.ts +12 -0
- package/src/behavior-actions/behavior.action.move.forward.ts +11 -0
- package/src/behavior-actions/behavior.action.split.block.ts +1 -1
- package/src/behavior-actions/behavior.actions.ts +18 -0
- package/src/behavior-actions/behavior.guards.ts +1 -1
- package/src/behaviors/behavior.core.decorators.ts +4 -4
- package/src/behaviors/behavior.core.insert-break.ts +4 -4
- package/src/behaviors/behavior.decorator-pair.ts +1 -1
- package/src/behaviors/behavior.links.ts +1 -1
- package/src/behaviors/behavior.markdown.ts +1 -1
- package/src/behaviors/behavior.perform-event.ts +1 -1
- package/src/behaviors/behavior.types.event.ts +10 -0
- package/src/converters/converter.portable-text.deserialize.test.ts +2 -3
- package/src/converters/converter.text-html.deserialize.test.ts +62 -5
- package/src/converters/converter.text-html.serialize.test.ts +9 -5
- package/src/converters/converter.text-html.ts +66 -64
- package/src/converters/converter.text-plain.test.ts +9 -4
- package/src/converters/converter.text-plain.ts +91 -87
- package/src/converters/converters.core.ts +13 -8
- package/src/editor/Editable.tsx +10 -5
- package/src/editor/PortableTextEditor.tsx +5 -5
- package/src/editor/__tests__/PortableTextEditorTester.tsx +4 -3
- package/src/editor/__tests__/insert-block.test.tsx +10 -10
- package/src/editor/__tests__/self-solving.test.tsx +2 -2
- package/src/editor/components/Synchronizer.tsx +1 -1
- package/src/editor/create-editor.ts +51 -18
- package/src/editor/editor-machine.ts +1 -1
- package/src/editor/{define-schema.ts → editor-schema.ts} +114 -5
- package/src/editor/editor-snapshot.ts +1 -1
- package/src/editor/get-active-decorators.ts +2 -2
- package/src/editor/{create-editor-schema.ts → legacy-schema.ts} +3 -3
- package/src/editor/mutation-machine.ts +1 -1
- package/src/editor/plugins/createWithObjectKeys.ts +6 -9
- package/src/editor/plugins/createWithPatches.ts +12 -11
- package/src/editor/plugins/createWithPortableTextBlockStyle.ts +2 -6
- package/src/editor/plugins/createWithPortableTextMarkModel.ts +15 -12
- package/src/editor/plugins/createWithSchemaTypes.ts +24 -16
- package/src/editor/plugins/createWithUndoRedo.ts +3 -4
- package/src/editor/plugins/createWithUtils.ts +12 -10
- package/src/editor/plugins/with-plugins.ts +5 -15
- package/src/editor/range-decorations-machine.ts +1 -1
- package/src/editor/sync-machine.ts +1 -1
- package/src/index.ts +2 -2
- package/src/internal-utils/__tests__/operationToPatches.test.ts +12 -10
- package/src/internal-utils/__tests__/patchToOperations.test.ts +3 -2
- package/src/internal-utils/__tests__/values.test.ts +3 -2
- package/src/internal-utils/applyPatch.ts +7 -9
- package/src/internal-utils/create-test-snapshot.ts +1 -1
- package/src/internal-utils/drag-selection.test.ts +1 -1
- package/src/internal-utils/operationToPatches.ts +4 -6
- package/src/internal-utils/parse-blocks.test.ts +1 -1
- package/src/internal-utils/parse-blocks.ts +8 -7
- package/src/internal-utils/slate-children-to-blocks.ts +1 -1
- package/src/internal-utils/slate-utils.ts +1 -1
- package/src/internal-utils/validateValue.ts +4 -6
- package/src/internal-utils/values.ts +5 -5
- package/src/plugins/plugin.decorator-shortcut.ts +1 -1
- package/src/plugins/plugin.markdown.test.tsx +1 -1
- package/src/plugins/plugin.markdown.tsx +1 -1
- package/src/selectors/selector.get-selection-text.test.ts +1 -1
- package/src/selectors/selector.get-trimmed-selection.test.ts +1 -1
|
@@ -13,21 +13,19 @@ import {debugWithName} from '../../internal-utils/debug'
|
|
|
13
13
|
import {getNextSpan, getPreviousSpan} from '../../internal-utils/sibling-utils'
|
|
14
14
|
import {isChangingRemotely} from '../../internal-utils/withChanges'
|
|
15
15
|
import {isRedoing, isUndoing} from '../../internal-utils/withUndoRedo'
|
|
16
|
-
import type {
|
|
17
|
-
PortableTextMemberSchemaTypes,
|
|
18
|
-
PortableTextSlateEditor,
|
|
19
|
-
} from '../../types/editor'
|
|
16
|
+
import type {PortableTextSlateEditor} from '../../types/editor'
|
|
20
17
|
import type {EditorActor} from '../editor-machine'
|
|
21
18
|
|
|
22
19
|
const debug = debugWithName('plugin:withPortableTextMarkModel')
|
|
23
20
|
|
|
24
21
|
export function createWithPortableTextMarkModel(
|
|
25
22
|
editorActor: EditorActor,
|
|
26
|
-
types: PortableTextMemberSchemaTypes,
|
|
27
23
|
): (editor: PortableTextSlateEditor) => PortableTextSlateEditor {
|
|
28
24
|
return function withPortableTextMarkModel(editor: PortableTextSlateEditor) {
|
|
29
25
|
const {apply, normalizeNode} = editor
|
|
30
|
-
const decorators =
|
|
26
|
+
const decorators = editorActor
|
|
27
|
+
.getSnapshot()
|
|
28
|
+
.context.schema.decorators.map((t) => t.name)
|
|
31
29
|
|
|
32
30
|
// Extend Slate's default normalization. Merge spans with same set of .marks when doing merge_node operations, and clean up markDefs / marks
|
|
33
31
|
editor.normalizeNode = (nodeEntry) => {
|
|
@@ -89,7 +87,9 @@ export function createWithPortableTextMarkModel(
|
|
|
89
87
|
if (editor.isTextSpan(node)) {
|
|
90
88
|
const blockPath = Path.parent(path)
|
|
91
89
|
const [block] = Editor.node(editor, blockPath)
|
|
92
|
-
const decorators =
|
|
90
|
+
const decorators = editorActor
|
|
91
|
+
.getSnapshot()
|
|
92
|
+
.context.schema.decorators.map((decorator) => decorator.name)
|
|
93
93
|
const annotations = node.marks?.filter(
|
|
94
94
|
(mark) => !decorators.includes(mark),
|
|
95
95
|
)
|
|
@@ -113,7 +113,9 @@ export function createWithPortableTextMarkModel(
|
|
|
113
113
|
* Remove orphaned annotations from child spans of block nodes
|
|
114
114
|
*/
|
|
115
115
|
if (editor.isTextBlock(node)) {
|
|
116
|
-
const decorators =
|
|
116
|
+
const decorators = editorActor
|
|
117
|
+
.getSnapshot()
|
|
118
|
+
.context.schema.decorators.map((decorator) => decorator.name)
|
|
117
119
|
|
|
118
120
|
for (const [child, childPath] of Node.children(editor, path)) {
|
|
119
121
|
if (editor.isTextSpan(child)) {
|
|
@@ -152,9 +154,9 @@ export function createWithPortableTextMarkModel(
|
|
|
152
154
|
const [block] = Editor.node(editor, blockPath)
|
|
153
155
|
|
|
154
156
|
if (editor.isTextBlock(block)) {
|
|
155
|
-
const decorators =
|
|
156
|
-
(
|
|
157
|
-
|
|
157
|
+
const decorators = editorActor
|
|
158
|
+
.getSnapshot()
|
|
159
|
+
.context.schema.decorators.map((decorator) => decorator.name)
|
|
158
160
|
const marks = node.marks ?? []
|
|
159
161
|
const orphanedAnnotations = marks.filter((mark) => {
|
|
160
162
|
return (
|
|
@@ -627,7 +629,8 @@ export function createWithPortableTextMarkModel(
|
|
|
627
629
|
op.type === 'merge_node' &&
|
|
628
630
|
op.path.length === 1 &&
|
|
629
631
|
'markDefs' in op.properties &&
|
|
630
|
-
op.properties._type ===
|
|
632
|
+
op.properties._type ===
|
|
633
|
+
editorActor.getSnapshot().context.schema.block.name &&
|
|
631
634
|
Array.isArray(op.properties.markDefs) &&
|
|
632
635
|
op.properties.markDefs.length > 0 &&
|
|
633
636
|
op.path[0] - 1 >= 0
|
|
@@ -8,10 +8,7 @@ import {
|
|
|
8
8
|
} from '@sanity/types'
|
|
9
9
|
import {Transforms, type Element} from 'slate'
|
|
10
10
|
import {debugWithName} from '../../internal-utils/debug'
|
|
11
|
-
import type {
|
|
12
|
-
PortableTextMemberSchemaTypes,
|
|
13
|
-
PortableTextSlateEditor,
|
|
14
|
-
} from '../../types/editor'
|
|
11
|
+
import type {PortableTextSlateEditor} from '../../types/editor'
|
|
15
12
|
import type {EditorActor} from '../editor-machine'
|
|
16
13
|
|
|
17
14
|
const debug = debugWithName('plugin:withSchemaTypes')
|
|
@@ -21,40 +18,47 @@ const debug = debugWithName('plugin:withSchemaTypes')
|
|
|
21
18
|
*/
|
|
22
19
|
export function createWithSchemaTypes({
|
|
23
20
|
editorActor,
|
|
24
|
-
schemaTypes,
|
|
25
21
|
}: {
|
|
26
22
|
editorActor: EditorActor
|
|
27
|
-
schemaTypes: PortableTextMemberSchemaTypes
|
|
28
23
|
}) {
|
|
29
24
|
return function withSchemaTypes(
|
|
30
25
|
editor: PortableTextSlateEditor,
|
|
31
26
|
): PortableTextSlateEditor {
|
|
32
27
|
editor.isTextBlock = (value: unknown): value is PortableTextTextBlock => {
|
|
33
28
|
return (
|
|
34
|
-
isPortableTextTextBlock(value) &&
|
|
29
|
+
isPortableTextTextBlock(value) &&
|
|
30
|
+
value._type === editorActor.getSnapshot().context.schema.block.name
|
|
35
31
|
)
|
|
36
32
|
}
|
|
37
33
|
editor.isTextSpan = (value: unknown): value is PortableTextSpan => {
|
|
38
|
-
return
|
|
34
|
+
return (
|
|
35
|
+
isPortableTextSpan(value) &&
|
|
36
|
+
value._type === editorActor.getSnapshot().context.schema.span.name
|
|
37
|
+
)
|
|
39
38
|
}
|
|
40
39
|
editor.isListBlock = (value: unknown): value is PortableTextListBlock => {
|
|
41
40
|
return (
|
|
42
|
-
isPortableTextListBlock(value) &&
|
|
41
|
+
isPortableTextListBlock(value) &&
|
|
42
|
+
value._type === editorActor.getSnapshot().context.schema.block.name
|
|
43
43
|
)
|
|
44
44
|
}
|
|
45
45
|
editor.isVoid = (element: Element): boolean => {
|
|
46
46
|
return (
|
|
47
|
-
|
|
48
|
-
(
|
|
49
|
-
.
|
|
47
|
+
editorActor.getSnapshot().context.schema.block.name !== element._type &&
|
|
48
|
+
(editorActor
|
|
49
|
+
.getSnapshot()
|
|
50
|
+
.context.schema.blockObjects.map((obj) => obj.name)
|
|
50
51
|
.includes(element._type) ||
|
|
51
|
-
|
|
52
|
-
.
|
|
52
|
+
editorActor
|
|
53
|
+
.getSnapshot()
|
|
54
|
+
.context.schema.inlineObjects.map((obj) => obj.name)
|
|
53
55
|
.includes(element._type))
|
|
54
56
|
)
|
|
55
57
|
}
|
|
56
58
|
editor.isInline = (element: Element): boolean => {
|
|
57
|
-
const inlineSchemaTypes =
|
|
59
|
+
const inlineSchemaTypes = editorActor
|
|
60
|
+
.getSnapshot()
|
|
61
|
+
.context.schema.inlineObjects.map((obj) => obj.name)
|
|
58
62
|
return (
|
|
59
63
|
inlineSchemaTypes.includes(element._type) &&
|
|
60
64
|
'__inline' in element &&
|
|
@@ -76,7 +80,11 @@ export function createWithSchemaTypes({
|
|
|
76
80
|
editorActor.send({type: 'normalizing'})
|
|
77
81
|
Transforms.setNodes(
|
|
78
82
|
editor,
|
|
79
|
-
{
|
|
83
|
+
{
|
|
84
|
+
...span,
|
|
85
|
+
_type: editorActor.getSnapshot().context.schema.span.name,
|
|
86
|
+
_key: key,
|
|
87
|
+
},
|
|
80
88
|
{at: path},
|
|
81
89
|
)
|
|
82
90
|
editorActor.send({type: 'done normalizing'})
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
DIFF_INSERT,
|
|
11
11
|
parsePatch,
|
|
12
12
|
} from '@sanity/diff-match-patch'
|
|
13
|
-
import type {
|
|
13
|
+
import type {PortableTextBlock} from '@sanity/types'
|
|
14
14
|
import {flatten, isEqual} from 'lodash'
|
|
15
15
|
import {
|
|
16
16
|
Editor,
|
|
@@ -58,7 +58,6 @@ const isSaving = (editor: Editor): boolean | undefined => {
|
|
|
58
58
|
|
|
59
59
|
export interface Options {
|
|
60
60
|
editorActor: EditorActor
|
|
61
|
-
blockSchemaType: ObjectSchemaType
|
|
62
61
|
subscriptions: Array<() => () => void>
|
|
63
62
|
}
|
|
64
63
|
|
|
@@ -72,12 +71,12 @@ const getRemotePatches = (editor: Editor) => {
|
|
|
72
71
|
export function createWithUndoRedo(
|
|
73
72
|
options: Options,
|
|
74
73
|
): (editor: PortableTextSlateEditor) => PortableTextSlateEditor {
|
|
75
|
-
const {editorActor
|
|
74
|
+
const {editorActor} = options
|
|
76
75
|
|
|
77
76
|
return (editor: PortableTextSlateEditor) => {
|
|
78
77
|
let previousSnapshot: PortableTextBlock[] | undefined = fromSlateValue(
|
|
79
78
|
editor.children,
|
|
80
|
-
|
|
79
|
+
editorActor.getSnapshot().context.schema.block.name,
|
|
81
80
|
)
|
|
82
81
|
const remotePatches = getRemotePatches(editor)
|
|
83
82
|
let previousBehaviorActionSetId = getCurrentBehaviorActionSetId(editor)
|
|
@@ -1,20 +1,16 @@
|
|
|
1
1
|
import {toSlateValue} from '../../internal-utils/values'
|
|
2
|
-
import type {
|
|
3
|
-
PortableTextMemberSchemaTypes,
|
|
4
|
-
PortableTextSlateEditor,
|
|
5
|
-
} from '../../types/editor'
|
|
2
|
+
import type {PortableTextSlateEditor} from '../../types/editor'
|
|
6
3
|
import type {EditorActor} from '../editor-machine'
|
|
7
4
|
|
|
8
5
|
interface Options {
|
|
9
6
|
editorActor: EditorActor
|
|
10
|
-
schemaTypes: PortableTextMemberSchemaTypes
|
|
11
7
|
}
|
|
12
8
|
|
|
13
9
|
/**
|
|
14
10
|
* This plugin makes various util commands available in the editor
|
|
15
11
|
*
|
|
16
12
|
*/
|
|
17
|
-
export function createWithUtils({editorActor
|
|
13
|
+
export function createWithUtils({editorActor}: Options) {
|
|
18
14
|
return function withUtils(
|
|
19
15
|
editor: PortableTextSlateEditor,
|
|
20
16
|
): PortableTextSlateEditor {
|
|
@@ -26,9 +22,11 @@ export function createWithUtils({editorActor, schemaTypes}: Options) {
|
|
|
26
22
|
const block = toSlateValue(
|
|
27
23
|
[
|
|
28
24
|
{
|
|
29
|
-
_type:
|
|
25
|
+
_type: editorActor.getSnapshot().context.schema.block.name,
|
|
30
26
|
_key: editorActor.getSnapshot().context.keyGenerator(),
|
|
31
|
-
style:
|
|
27
|
+
style:
|
|
28
|
+
editorActor.getSnapshot().context.schema.styles[0].name ||
|
|
29
|
+
'normal',
|
|
32
30
|
...(options.listItem ? {listItem: options.listItem} : {}),
|
|
33
31
|
...(options.level ? {level: options.level} : {}),
|
|
34
32
|
markDefs: [],
|
|
@@ -38,13 +36,17 @@ export function createWithUtils({editorActor, schemaTypes}: Options) {
|
|
|
38
36
|
_key: editorActor.getSnapshot().context.keyGenerator(),
|
|
39
37
|
text: '',
|
|
40
38
|
marks: options.decorators.filter((decorator) =>
|
|
41
|
-
|
|
39
|
+
editorActor
|
|
40
|
+
.getSnapshot()
|
|
41
|
+
.context.schema.decorators.find(
|
|
42
|
+
({name}) => name === decorator,
|
|
43
|
+
),
|
|
42
44
|
),
|
|
43
45
|
},
|
|
44
46
|
],
|
|
45
47
|
},
|
|
46
48
|
],
|
|
47
|
-
{schemaTypes},
|
|
49
|
+
{schemaTypes: editorActor.getSnapshot().context.schema},
|
|
48
50
|
)[0]
|
|
49
51
|
return block
|
|
50
52
|
}
|
|
@@ -31,39 +31,29 @@ export const withPlugins = <T extends Editor>(
|
|
|
31
31
|
): PortableTextSlateEditor => {
|
|
32
32
|
const e = editor as T & PortableTextSlateEditor
|
|
33
33
|
const {editorActor} = options
|
|
34
|
-
const
|
|
35
|
-
const
|
|
36
|
-
const withObjectKeys = createWithObjectKeys(editorActor, schemaTypes)
|
|
34
|
+
const operationToPatches = createOperationToPatches(editorActor)
|
|
35
|
+
const withObjectKeys = createWithObjectKeys(editorActor)
|
|
37
36
|
const withSchemaTypes = createWithSchemaTypes({
|
|
38
37
|
editorActor,
|
|
39
|
-
schemaTypes,
|
|
40
38
|
})
|
|
41
39
|
const withPatches = createWithPatches({
|
|
42
40
|
editorActor,
|
|
43
41
|
patchFunctions: operationToPatches,
|
|
44
|
-
schemaTypes,
|
|
45
42
|
subscriptions: options.subscriptions,
|
|
46
43
|
})
|
|
47
44
|
const withMaxBlocks = createWithMaxBlocks(editorActor)
|
|
48
45
|
const withUndoRedo = createWithUndoRedo({
|
|
49
46
|
editorActor,
|
|
50
|
-
blockSchemaType: schemaTypes.block,
|
|
51
47
|
subscriptions: options.subscriptions,
|
|
52
48
|
})
|
|
53
|
-
const withPortableTextMarkModel = createWithPortableTextMarkModel(
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
)
|
|
57
|
-
const withPortableTextBlockStyle = createWithPortableTextBlockStyle(
|
|
58
|
-
editorActor,
|
|
59
|
-
schemaTypes,
|
|
60
|
-
)
|
|
49
|
+
const withPortableTextMarkModel = createWithPortableTextMarkModel(editorActor)
|
|
50
|
+
const withPortableTextBlockStyle =
|
|
51
|
+
createWithPortableTextBlockStyle(editorActor)
|
|
61
52
|
|
|
62
53
|
const withPlaceholderBlock = createWithPlaceholderBlock(editorActor)
|
|
63
54
|
|
|
64
55
|
const withUtils = createWithUtils({
|
|
65
56
|
editorActor,
|
|
66
|
-
schemaTypes,
|
|
67
57
|
})
|
|
68
58
|
const withPortableTextSelections =
|
|
69
59
|
createWithPortableTextSelections(editorActor)
|
|
@@ -17,11 +17,11 @@ import {
|
|
|
17
17
|
type AnyEventObject,
|
|
18
18
|
type CallbackLogicFunction,
|
|
19
19
|
} from 'xstate'
|
|
20
|
-
import type {EditorSchema} from '..'
|
|
21
20
|
import {moveRangeByOperation, toSlateRange} from '../internal-utils/ranges'
|
|
22
21
|
import {slateRangeToSelection} from '../internal-utils/slate-utils'
|
|
23
22
|
import {isEqualToEmptyEditor} from '../internal-utils/values'
|
|
24
23
|
import type {PortableTextSlateEditor, RangeDecoration} from '../types/editor'
|
|
24
|
+
import type {EditorSchema} from './editor-schema'
|
|
25
25
|
|
|
26
26
|
const slateOperationCallback: CallbackLogicFunction<
|
|
27
27
|
AnyEventObject,
|
|
@@ -24,7 +24,7 @@ import type {
|
|
|
24
24
|
InvalidValueResolution,
|
|
25
25
|
PortableTextSlateEditor,
|
|
26
26
|
} from '../types/editor'
|
|
27
|
-
import type {EditorSchema} from './
|
|
27
|
+
import type {EditorSchema} from './editor-schema'
|
|
28
28
|
import {withoutSaving} from './plugins/createWithUndoRedo'
|
|
29
29
|
|
|
30
30
|
const debug = debugWithName('sync machine')
|
package/src/index.ts
CHANGED
|
@@ -11,8 +11,8 @@ export {
|
|
|
11
11
|
defineSchema,
|
|
12
12
|
type BaseDefinition,
|
|
13
13
|
type SchemaDefinition,
|
|
14
|
-
} from './editor/
|
|
15
|
-
export type {EditorSchema} from './editor/
|
|
14
|
+
} from './editor/editor-schema'
|
|
15
|
+
export type {EditorSchema} from './editor/editor-schema'
|
|
16
16
|
export {PortableTextEditable} from './editor/Editable'
|
|
17
17
|
export type {PortableTextEditableProps} from './editor/Editable'
|
|
18
18
|
export type {
|
|
@@ -4,24 +4,26 @@ import {beforeEach, describe, expect, it} from 'vitest'
|
|
|
4
4
|
import {createActor} from 'xstate'
|
|
5
5
|
import {coreBehaviors} from '../../behaviors/behavior.core'
|
|
6
6
|
import {schemaType} from '../../editor/__tests__/PortableTextEditorTester'
|
|
7
|
-
import {createEditorSchema} from '../../editor/create-editor-schema'
|
|
8
7
|
import {editorMachine} from '../../editor/editor-machine'
|
|
8
|
+
import {legacySchemaToEditorSchema} from '../../editor/editor-schema'
|
|
9
9
|
import {defaultKeyGenerator} from '../../editor/key-generator'
|
|
10
|
+
import {createLegacySchema} from '../../editor/legacy-schema'
|
|
10
11
|
import {withPlugins} from '../../editor/plugins/with-plugins'
|
|
11
12
|
import {createOperationToPatches} from '../operationToPatches'
|
|
12
13
|
|
|
13
|
-
const schemaTypes =
|
|
14
|
+
const schemaTypes = legacySchemaToEditorSchema(createLegacySchema(schemaType))
|
|
15
|
+
const editorActor = createActor(editorMachine, {
|
|
16
|
+
input: {
|
|
17
|
+
behaviors: coreBehaviors,
|
|
18
|
+
schema: schemaTypes,
|
|
19
|
+
keyGenerator: defaultKeyGenerator,
|
|
20
|
+
},
|
|
21
|
+
})
|
|
14
22
|
|
|
15
|
-
const operationToPatches = createOperationToPatches(
|
|
23
|
+
const operationToPatches = createOperationToPatches(editorActor)
|
|
16
24
|
|
|
17
25
|
const editor = withPlugins(createEditor(), {
|
|
18
|
-
editorActor
|
|
19
|
-
input: {
|
|
20
|
-
behaviors: coreBehaviors,
|
|
21
|
-
schema: schemaTypes,
|
|
22
|
-
keyGenerator: defaultKeyGenerator,
|
|
23
|
-
},
|
|
24
|
-
}),
|
|
26
|
+
editorActor,
|
|
25
27
|
subscriptions: [],
|
|
26
28
|
})
|
|
27
29
|
|
|
@@ -4,14 +4,15 @@ import {beforeEach, describe, expect, it} from 'vitest'
|
|
|
4
4
|
import {createActor} from 'xstate'
|
|
5
5
|
import {coreBehaviors} from '../../behaviors/behavior.core'
|
|
6
6
|
import {schemaType} from '../../editor/__tests__/PortableTextEditorTester'
|
|
7
|
-
import {createEditorSchema} from '../../editor/create-editor-schema'
|
|
8
7
|
import {editorMachine} from '../../editor/editor-machine'
|
|
8
|
+
import {legacySchemaToEditorSchema} from '../../editor/editor-schema'
|
|
9
9
|
import {defaultKeyGenerator} from '../../editor/key-generator'
|
|
10
|
+
import {createLegacySchema} from '../../editor/legacy-schema'
|
|
10
11
|
import {withPlugins} from '../../editor/plugins/with-plugins'
|
|
11
12
|
import {createApplyPatch} from '../applyPatch'
|
|
12
13
|
import {VOID_CHILD_KEY} from '../values'
|
|
13
14
|
|
|
14
|
-
const schemaTypes =
|
|
15
|
+
const schemaTypes = legacySchemaToEditorSchema(createLegacySchema(schemaType))
|
|
15
16
|
|
|
16
17
|
const patchToOperations = createApplyPatch(schemaTypes)
|
|
17
18
|
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import {describe, expect, it} from 'vitest'
|
|
2
2
|
import {schemaType} from '../../editor/__tests__/PortableTextEditorTester'
|
|
3
|
-
import {
|
|
3
|
+
import {legacySchemaToEditorSchema} from '../../editor/editor-schema'
|
|
4
|
+
import {createLegacySchema} from '../../editor/legacy-schema'
|
|
4
5
|
import {fromSlateValue, toSlateValue} from '../values'
|
|
5
6
|
|
|
6
|
-
const schemaTypes =
|
|
7
|
+
const schemaTypes = legacySchemaToEditorSchema(createLegacySchema(schemaType))
|
|
7
8
|
|
|
8
9
|
describe('toSlateValue', () => {
|
|
9
10
|
it('checks undefined', () => {
|
|
@@ -30,10 +30,8 @@ import {
|
|
|
30
30
|
type Node,
|
|
31
31
|
type Path as SlatePath,
|
|
32
32
|
} from 'slate'
|
|
33
|
-
import type {
|
|
34
|
-
|
|
35
|
-
PortableTextSlateEditor,
|
|
36
|
-
} from '../types/editor'
|
|
33
|
+
import type {EditorSchema} from '../editor/editor-schema'
|
|
34
|
+
import type {PortableTextSlateEditor} from '../types/editor'
|
|
37
35
|
import {debugWithName} from './debug'
|
|
38
36
|
import {toSlateValue} from './values'
|
|
39
37
|
import {KEY_TO_SLATE_ELEMENT} from './weakMaps'
|
|
@@ -45,7 +43,7 @@ const debugVerbose = debug.enabled && true
|
|
|
45
43
|
* Creates a function that can apply a patch onto a PortableTextSlateEditor.
|
|
46
44
|
*/
|
|
47
45
|
export function createApplyPatch(
|
|
48
|
-
|
|
46
|
+
schema: EditorSchema,
|
|
49
47
|
): (editor: PortableTextSlateEditor, patch: Patch) => boolean {
|
|
50
48
|
return (editor: PortableTextSlateEditor, patch: Patch): boolean => {
|
|
51
49
|
let changed = false
|
|
@@ -61,7 +59,7 @@ export function createApplyPatch(
|
|
|
61
59
|
try {
|
|
62
60
|
switch (patch.type) {
|
|
63
61
|
case 'insert':
|
|
64
|
-
changed = insertPatch(editor, patch,
|
|
62
|
+
changed = insertPatch(editor, patch, schema)
|
|
65
63
|
break
|
|
66
64
|
case 'unset':
|
|
67
65
|
changed = unsetPatch(editor, patch)
|
|
@@ -148,7 +146,7 @@ export function diffMatchPatch(
|
|
|
148
146
|
function insertPatch(
|
|
149
147
|
editor: PortableTextSlateEditor,
|
|
150
148
|
patch: InsertPatch,
|
|
151
|
-
|
|
149
|
+
schema: EditorSchema,
|
|
152
150
|
) {
|
|
153
151
|
const {
|
|
154
152
|
block: targetBlock,
|
|
@@ -169,7 +167,7 @@ function insertPatch(
|
|
|
169
167
|
const {items, position} = patch
|
|
170
168
|
const blocksToInsert = toSlateValue(
|
|
171
169
|
items as PortableTextBlock[],
|
|
172
|
-
{schemaTypes},
|
|
170
|
+
{schemaTypes: schema},
|
|
173
171
|
KEY_TO_SLATE_ELEMENT.get(editor),
|
|
174
172
|
) as Descendant[]
|
|
175
173
|
const targetBlockIndex = targetBlockPath[0]
|
|
@@ -191,7 +189,7 @@ function insertPatch(
|
|
|
191
189
|
targetBlock &&
|
|
192
190
|
toSlateValue(
|
|
193
191
|
[{...targetBlock, children: items as PortableTextChild[]}],
|
|
194
|
-
{schemaTypes},
|
|
192
|
+
{schemaTypes: schema},
|
|
195
193
|
KEY_TO_SLATE_ELEMENT.get(editor),
|
|
196
194
|
)
|
|
197
195
|
const targetChildIndex = targetChildPath[1]
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type {EditorSnapshot} from '..'
|
|
2
|
-
import {compileSchemaDefinition, defineSchema} from '../editor/
|
|
2
|
+
import {compileSchemaDefinition, defineSchema} from '../editor/editor-schema'
|
|
3
3
|
import {createTestKeyGenerator} from './test-key-generator'
|
|
4
4
|
|
|
5
5
|
export function createTestSnapshot(snapshot: {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {describe, expect, test} from 'vitest'
|
|
2
|
-
import {compileSchemaDefinition, defineSchema} from '../editor/
|
|
2
|
+
import {compileSchemaDefinition, defineSchema} from '../editor/editor-schema'
|
|
3
3
|
import type {EditorSelection} from '../types/editor'
|
|
4
4
|
import {createTestSnapshot} from './create-test-snapshot'
|
|
5
5
|
import {getDragSelection} from './drag-selection'
|
|
@@ -21,20 +21,18 @@ import {
|
|
|
21
21
|
type SetNodeOperation,
|
|
22
22
|
type SplitNodeOperation,
|
|
23
23
|
} from 'slate'
|
|
24
|
+
import type {EditorActor} from '../editor/editor-machine'
|
|
24
25
|
import type {PatchFunctions} from '../editor/plugins/createWithPatches'
|
|
25
|
-
import type {
|
|
26
|
-
PortableTextMemberSchemaTypes,
|
|
27
|
-
PortableTextSlateEditor,
|
|
28
|
-
} from '../types/editor'
|
|
26
|
+
import type {PortableTextSlateEditor} from '../types/editor'
|
|
29
27
|
import {debugWithName} from './debug'
|
|
30
28
|
import {fromSlateValue} from './values'
|
|
31
29
|
|
|
32
30
|
const debug = debugWithName('operationToPatches')
|
|
33
31
|
|
|
34
32
|
export function createOperationToPatches(
|
|
35
|
-
|
|
33
|
+
editorActor: EditorActor,
|
|
36
34
|
): PatchFunctions {
|
|
37
|
-
const textBlockName =
|
|
35
|
+
const textBlockName = editorActor.getSnapshot().context.schema.block.name
|
|
38
36
|
function insertTextPatch(
|
|
39
37
|
editor: PortableTextSlateEditor,
|
|
40
38
|
operation: InsertTextOperation,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {describe, expect, test} from 'vitest'
|
|
2
|
-
import {compileSchemaDefinition, defineSchema} from '../editor/
|
|
2
|
+
import {compileSchemaDefinition, defineSchema} from '../editor/editor-schema'
|
|
3
3
|
import {parseBlock, parseSpan} from './parse-blocks'
|
|
4
4
|
import {createTestKeyGenerator} from './test-key-generator'
|
|
5
5
|
|
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
ObjectSchemaType,
|
|
3
2
|
PortableTextBlock,
|
|
4
3
|
PortableTextObject,
|
|
5
4
|
PortableTextSpan,
|
|
6
5
|
PortableTextTextBlock,
|
|
7
6
|
TypedObject,
|
|
8
7
|
} from '@sanity/types'
|
|
9
|
-
import type {EditorSchema} from '../editor/
|
|
8
|
+
import type {EditorSchema} from '../editor/editor-schema'
|
|
10
9
|
import type {EditorContext} from '../editor/editor-snapshot'
|
|
11
10
|
import {isTypedObject} from './asserters'
|
|
12
11
|
|
|
@@ -192,9 +191,9 @@ function parseTextBlock({
|
|
|
192
191
|
*/
|
|
193
192
|
if (
|
|
194
193
|
typeof parsedBlock.style !== 'string' ||
|
|
195
|
-
!context.schema.styles.find((style) => style.
|
|
194
|
+
!context.schema.styles.find((style) => style.name === block.style)
|
|
196
195
|
) {
|
|
197
|
-
const defaultStyle = context.schema.styles.at(0)?.
|
|
196
|
+
const defaultStyle = context.schema.styles.at(0)?.name
|
|
198
197
|
|
|
199
198
|
if (defaultStyle !== undefined) {
|
|
200
199
|
parsedBlock.style = defaultStyle
|
|
@@ -208,7 +207,7 @@ function parseTextBlock({
|
|
|
208
207
|
*/
|
|
209
208
|
if (
|
|
210
209
|
typeof parsedBlock.listItem !== 'string' ||
|
|
211
|
-
!context.schema.lists.find((list) => list.
|
|
210
|
+
!context.schema.lists.find((list) => list.name === block.listItem)
|
|
212
211
|
) {
|
|
213
212
|
delete parsedBlock.listItem
|
|
214
213
|
}
|
|
@@ -272,7 +271,7 @@ export function parseSpan({
|
|
|
272
271
|
}
|
|
273
272
|
|
|
274
273
|
if (
|
|
275
|
-
context.schema.decorators.some((decorator) => decorator.
|
|
274
|
+
context.schema.decorators.some((decorator) => decorator.name === mark)
|
|
276
275
|
) {
|
|
277
276
|
return [mark]
|
|
278
277
|
}
|
|
@@ -362,7 +361,9 @@ function parseObject({
|
|
|
362
361
|
options,
|
|
363
362
|
}: {
|
|
364
363
|
object: TypedObject
|
|
365
|
-
context: Pick<EditorContext, 'keyGenerator'> & {
|
|
364
|
+
context: Pick<EditorContext, 'keyGenerator'> & {
|
|
365
|
+
schemaType: EditorSchema['blockObjects'][0]
|
|
366
|
+
}
|
|
366
367
|
options: {refreshKeys: boolean}
|
|
367
368
|
}): PortableTextObject {
|
|
368
369
|
// Validates all props on the object and only takes those that match
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type {PortableTextBlock} from '@sanity/types'
|
|
2
2
|
import type {Descendant} from 'slate'
|
|
3
|
-
import type {EditorSchema} from '../editor/
|
|
3
|
+
import type {EditorSchema} from '../editor/editor-schema'
|
|
4
4
|
|
|
5
5
|
export function slateChildrenToBlocks(
|
|
6
6
|
schema: EditorSchema,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {Editor, Element, Node, Range, type Path, type Point} from 'slate'
|
|
2
|
-
import type {EditorSchema} from '../editor/
|
|
2
|
+
import type {EditorSchema} from '../editor/editor-schema'
|
|
3
3
|
import type {EditorSelection, PortableTextSlateEditor} from '../types/editor'
|
|
4
4
|
import {fromSlateValue} from './values'
|
|
5
5
|
|
|
@@ -6,10 +6,8 @@ import {
|
|
|
6
6
|
type PortableTextTextBlock,
|
|
7
7
|
} from '@sanity/types'
|
|
8
8
|
import {flatten, isPlainObject, uniq} from 'lodash'
|
|
9
|
-
import type {
|
|
10
|
-
|
|
11
|
-
PortableTextMemberSchemaTypes,
|
|
12
|
-
} from '../types/editor'
|
|
9
|
+
import type {EditorSchema} from '../editor/editor-schema'
|
|
10
|
+
import type {InvalidValueResolution} from '../types/editor'
|
|
13
11
|
|
|
14
12
|
export interface Validation {
|
|
15
13
|
valid: boolean
|
|
@@ -19,7 +17,7 @@ export interface Validation {
|
|
|
19
17
|
|
|
20
18
|
export function validateValue(
|
|
21
19
|
value: PortableTextBlock[] | undefined,
|
|
22
|
-
types:
|
|
20
|
+
types: EditorSchema,
|
|
23
21
|
keyGenerator: () => string,
|
|
24
22
|
): Validation {
|
|
25
23
|
let resolution: InvalidValueResolution | null = null
|
|
@@ -270,7 +268,7 @@ export function validateValue(
|
|
|
270
268
|
|
|
271
269
|
// Test that every annotation mark used has a definition
|
|
272
270
|
const annotationMarks = allUsedMarks.filter(
|
|
273
|
-
(mark) => !types.decorators.map((dec) => dec.
|
|
271
|
+
(mark) => !types.decorators.map((dec) => dec.name).includes(mark),
|
|
274
272
|
)
|
|
275
273
|
const orphanedMarks = annotationMarks.filter(
|
|
276
274
|
(mark) =>
|
|
@@ -7,7 +7,7 @@ import type {
|
|
|
7
7
|
} from '@sanity/types'
|
|
8
8
|
import {isEqual} from 'lodash'
|
|
9
9
|
import {Element, Text, type Descendant, type Node} from 'slate'
|
|
10
|
-
import type {
|
|
10
|
+
import type {EditorSchema} from '../editor/editor-schema'
|
|
11
11
|
|
|
12
12
|
export const EMPTY_MARKDEFS: PortableTextObject[] = []
|
|
13
13
|
|
|
@@ -31,7 +31,7 @@ function keepObjectEquality(
|
|
|
31
31
|
|
|
32
32
|
export function toSlateValue(
|
|
33
33
|
value: PortableTextBlock[] | undefined,
|
|
34
|
-
{schemaTypes}: {schemaTypes:
|
|
34
|
+
{schemaTypes}: {schemaTypes: EditorSchema},
|
|
35
35
|
keyMap: Record<string, any> = {},
|
|
36
36
|
): Descendant[] {
|
|
37
37
|
if (value && Array.isArray(value)) {
|
|
@@ -88,7 +88,7 @@ export function toSlateValue(
|
|
|
88
88
|
}
|
|
89
89
|
// TODO: remove this when we have a better way to handle missing style
|
|
90
90
|
if (hasMissingStyle) {
|
|
91
|
-
rest.style = schemaTypes.styles[0].
|
|
91
|
+
rest.style = schemaTypes.styles[0].name
|
|
92
92
|
}
|
|
93
93
|
return keepObjectEquality({_type, _key, ...rest, children}, keyMap)
|
|
94
94
|
}
|
|
@@ -167,7 +167,7 @@ export function fromSlateValue(
|
|
|
167
167
|
|
|
168
168
|
export function isEqualToEmptyEditor(
|
|
169
169
|
children: Descendant[] | PortableTextBlock[],
|
|
170
|
-
schemaTypes:
|
|
170
|
+
schemaTypes: EditorSchema,
|
|
171
171
|
): boolean {
|
|
172
172
|
return (
|
|
173
173
|
children === undefined ||
|
|
@@ -178,7 +178,7 @@ export function isEqualToEmptyEditor(
|
|
|
178
178
|
Element.isElement(children[0]) &&
|
|
179
179
|
children[0]._type === schemaTypes.block.name &&
|
|
180
180
|
'style' in children[0] &&
|
|
181
|
-
children[0].style === schemaTypes.styles[0].
|
|
181
|
+
children[0].style === schemaTypes.styles[0].name &&
|
|
182
182
|
!('listItem' in children[0]) &&
|
|
183
183
|
Array.isArray(children[0].children) &&
|
|
184
184
|
children[0].children.length === 1 &&
|
|
@@ -11,8 +11,8 @@ import {createDecoratorPairBehavior} from '../behaviors/behavior.decorator-pair'
|
|
|
11
11
|
import {effect, execute} from '../behaviors/behavior.types.action'
|
|
12
12
|
import {defineBehavior} from '../behaviors/behavior.types.behavior'
|
|
13
13
|
import type {Editor} from '../editor/create-editor'
|
|
14
|
-
import type {EditorSchema} from '../editor/define-schema'
|
|
15
14
|
import {useEditor} from '../editor/editor-provider'
|
|
15
|
+
import type {EditorSchema} from '../editor/editor-schema'
|
|
16
16
|
import type {BlockOffset} from '../types/block-offset'
|
|
17
17
|
import * as utils from '../utils'
|
|
18
18
|
|