@portabletext/editor 1.47.0 → 1.47.2
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 +379 -326
- 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 +380 -327
- 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 +70 -28
- package/lib/behaviors/index.d.ts +70 -28
- package/lib/behaviors/index.js.map +1 -1
- package/lib/index.cjs +12 -14
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +61 -3
- package/lib/index.d.ts +61 -3
- package/lib/index.js +12 -14
- package/lib/index.js.map +1 -1
- package/lib/plugins/index.cjs.map +1 -1
- package/lib/plugins/index.d.cts +68 -23
- package/lib/plugins/index.d.ts +68 -23
- package/lib/plugins/index.js.map +1 -1
- package/lib/selectors/index.d.cts +69 -27
- package/lib/selectors/index.d.ts +69 -27
- package/lib/utils/index.d.cts +74 -29
- package/lib/utils/index.d.ts +74 -29
- package/package.json +6 -6
- package/src/behavior-actions/behavior.action.insert.block.ts +1 -1
- package/src/behavior-actions/behavior.action.split.block.ts +1 -1
- 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/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 +18 -13
- 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
|
@@ -21,7 +21,7 @@ import type {
|
|
|
21
21
|
InvalidValueResolution,
|
|
22
22
|
PortableTextSlateEditor,
|
|
23
23
|
} from '../types/editor'
|
|
24
|
-
import type {EditorSchema} from './
|
|
24
|
+
import type {EditorSchema} from './editor-schema'
|
|
25
25
|
import {createEditorSnapshot} from './editor-snapshot'
|
|
26
26
|
|
|
27
27
|
export * from 'xstate/guards'
|
|
@@ -2,8 +2,8 @@ import {Schema as SanitySchema} from '@sanity/schema'
|
|
|
2
2
|
import {defineField, defineType, type ObjectSchemaType} from '@sanity/types'
|
|
3
3
|
import startCase from 'lodash.startcase'
|
|
4
4
|
import type {PortableTextMemberSchemaTypes} from '../types/editor'
|
|
5
|
-
import {createEditorSchema} from './create-editor-schema'
|
|
6
5
|
import {defaultKeyGenerator} from './key-generator'
|
|
6
|
+
import {createLegacySchema} from './legacy-schema'
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* @public
|
|
@@ -88,11 +88,120 @@ const defaultObjectTitles: Record<string, string> = {
|
|
|
88
88
|
/**
|
|
89
89
|
* @public
|
|
90
90
|
*/
|
|
91
|
-
export type EditorSchema =
|
|
91
|
+
export type EditorSchema = {
|
|
92
|
+
annotations: ReadonlyArray<
|
|
93
|
+
BaseDefinition & {
|
|
94
|
+
fields: ReadonlyArray<{name: string; type: string}>
|
|
95
|
+
}
|
|
96
|
+
>
|
|
97
|
+
block: {
|
|
98
|
+
name: string
|
|
99
|
+
}
|
|
100
|
+
blockObjects: ReadonlyArray<
|
|
101
|
+
BaseDefinition & {
|
|
102
|
+
fields: ReadonlyArray<{name: string; type: string}>
|
|
103
|
+
}
|
|
104
|
+
>
|
|
105
|
+
decorators: ReadonlyArray<
|
|
106
|
+
BaseDefinition & {
|
|
107
|
+
/**
|
|
108
|
+
* @deprecated
|
|
109
|
+
* Use `name` instead
|
|
110
|
+
*/
|
|
111
|
+
value: string
|
|
112
|
+
}
|
|
113
|
+
>
|
|
114
|
+
inlineObjects: ReadonlyArray<
|
|
115
|
+
BaseDefinition & {
|
|
116
|
+
fields: ReadonlyArray<{name: string; type: string}>
|
|
117
|
+
}
|
|
118
|
+
>
|
|
119
|
+
span: {
|
|
120
|
+
name: string
|
|
121
|
+
}
|
|
122
|
+
styles: ReadonlyArray<
|
|
123
|
+
BaseDefinition & {
|
|
124
|
+
/**
|
|
125
|
+
* @deprecated
|
|
126
|
+
* Use `name` instead
|
|
127
|
+
*/
|
|
128
|
+
value: string
|
|
129
|
+
}
|
|
130
|
+
>
|
|
131
|
+
lists: ReadonlyArray<
|
|
132
|
+
BaseDefinition & {
|
|
133
|
+
/**
|
|
134
|
+
* @deprecated
|
|
135
|
+
* Use `name` instead
|
|
136
|
+
*/
|
|
137
|
+
value: string
|
|
138
|
+
}
|
|
139
|
+
>
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function legacySchemaToEditorSchema(
|
|
143
|
+
schema: PortableTextMemberSchemaTypes,
|
|
144
|
+
): EditorSchema {
|
|
145
|
+
return {
|
|
146
|
+
annotations: schema.annotations.map((annotation) => ({
|
|
147
|
+
name: annotation.name,
|
|
148
|
+
fields: annotation.fields.map((field) => ({
|
|
149
|
+
name: field.name,
|
|
150
|
+
type: field.type.jsonType,
|
|
151
|
+
})),
|
|
152
|
+
title: annotation.title,
|
|
153
|
+
})),
|
|
154
|
+
block: {
|
|
155
|
+
name: schema.block.name,
|
|
156
|
+
},
|
|
157
|
+
blockObjects: schema.blockObjects.map((blockObject) => ({
|
|
158
|
+
name: blockObject.name,
|
|
159
|
+
fields: blockObject.fields.map((field) => ({
|
|
160
|
+
name: field.name,
|
|
161
|
+
type: field.type.jsonType,
|
|
162
|
+
})),
|
|
163
|
+
title: blockObject.title,
|
|
164
|
+
})),
|
|
165
|
+
decorators: schema.decorators.map((decorator) => ({
|
|
166
|
+
name: decorator.value,
|
|
167
|
+
title: decorator.title,
|
|
168
|
+
value: decorator.value,
|
|
169
|
+
})),
|
|
170
|
+
inlineObjects: schema.inlineObjects.map((inlineObject) => ({
|
|
171
|
+
name: inlineObject.name,
|
|
172
|
+
fields: inlineObject.fields.map((field) => ({
|
|
173
|
+
name: field.name,
|
|
174
|
+
type: field.type.jsonType,
|
|
175
|
+
})),
|
|
176
|
+
title: inlineObject.title,
|
|
177
|
+
})),
|
|
178
|
+
span: {
|
|
179
|
+
name: schema.span.name,
|
|
180
|
+
},
|
|
181
|
+
styles: schema.styles.map((style) => ({
|
|
182
|
+
name: style.value,
|
|
183
|
+
title: style.title,
|
|
184
|
+
value: style.value,
|
|
185
|
+
})),
|
|
186
|
+
lists: schema.lists.map((list) => ({
|
|
187
|
+
name: list.value,
|
|
188
|
+
title: list.title,
|
|
189
|
+
value: list.value,
|
|
190
|
+
})),
|
|
191
|
+
}
|
|
192
|
+
}
|
|
92
193
|
|
|
93
194
|
export function compileSchemaDefinition<
|
|
94
195
|
TSchemaDefinition extends SchemaDefinition,
|
|
95
|
-
>(definition
|
|
196
|
+
>(definition: TSchemaDefinition): EditorSchema {
|
|
197
|
+
return legacySchemaToEditorSchema(
|
|
198
|
+
compileSchemaDefinitionToLegacySchema(definition),
|
|
199
|
+
)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export function compileSchemaDefinitionToLegacySchema<
|
|
203
|
+
TSchemaDefinition extends SchemaDefinition,
|
|
204
|
+
>(definition?: TSchemaDefinition): PortableTextMemberSchemaTypes {
|
|
96
205
|
const blockObjects =
|
|
97
206
|
definition?.blockObjects?.map((blockObject) =>
|
|
98
207
|
defineType({
|
|
@@ -179,7 +288,7 @@ export function compileSchemaDefinition<
|
|
|
179
288
|
types: [portableTextSchema, ...blockObjects, ...inlineObjects],
|
|
180
289
|
}).get('portable-text')
|
|
181
290
|
|
|
182
|
-
const pteSchema =
|
|
291
|
+
const pteSchema = createLegacySchema(schema)
|
|
183
292
|
|
|
184
293
|
return {
|
|
185
294
|
...pteSchema,
|
|
@@ -203,5 +312,5 @@ export function compileSchemaDefinition<
|
|
|
203
312
|
} as ObjectSchemaType)
|
|
204
313
|
: inlineObject,
|
|
205
314
|
),
|
|
206
|
-
} satisfies
|
|
315
|
+
} satisfies PortableTextMemberSchemaTypes
|
|
207
316
|
}
|
|
@@ -4,8 +4,8 @@ import type {EventPosition} from '../internal-utils/event-position'
|
|
|
4
4
|
import {slateChildrenToBlocks} from '../internal-utils/slate-children-to-blocks'
|
|
5
5
|
import {slateRangeToSelection} from '../internal-utils/slate-utils'
|
|
6
6
|
import type {EditorSelection, PortableTextSlateEditor} from '../types/editor'
|
|
7
|
-
import type {EditorSchema} from './define-schema'
|
|
8
7
|
import type {HasTag} from './editor-machine'
|
|
8
|
+
import type {EditorSchema} from './editor-schema'
|
|
9
9
|
import {getActiveDecorators} from './get-active-decorators'
|
|
10
10
|
|
|
11
11
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {Editor} from 'slate'
|
|
2
2
|
import type {PortableTextSlateEditor} from '../types/editor'
|
|
3
|
-
import type {EditorSchema} from './
|
|
3
|
+
import type {EditorSchema} from './editor-schema'
|
|
4
4
|
|
|
5
5
|
export function getActiveDecorators({
|
|
6
6
|
schema,
|
|
@@ -9,7 +9,7 @@ export function getActiveDecorators({
|
|
|
9
9
|
schema: EditorSchema
|
|
10
10
|
slateEditorInstance: PortableTextSlateEditor
|
|
11
11
|
}) {
|
|
12
|
-
const decorators = schema.decorators.map((decorator) => decorator.
|
|
12
|
+
const decorators = schema.decorators.map((decorator) => decorator.name)
|
|
13
13
|
|
|
14
14
|
const marks =
|
|
15
15
|
{
|
|
@@ -6,11 +6,11 @@ import type {
|
|
|
6
6
|
SchemaType,
|
|
7
7
|
SpanSchemaType,
|
|
8
8
|
} from '@sanity/types'
|
|
9
|
-
import type {
|
|
9
|
+
import type {PortableTextMemberSchemaTypes} from '../types/editor'
|
|
10
10
|
|
|
11
|
-
export function
|
|
11
|
+
export function createLegacySchema(
|
|
12
12
|
portableTextType: ArraySchemaType<PortableTextBlock>,
|
|
13
|
-
):
|
|
13
|
+
): PortableTextMemberSchemaTypes {
|
|
14
14
|
if (!portableTextType) {
|
|
15
15
|
throw new Error("Parameter 'portabletextType' missing (required)")
|
|
16
16
|
}
|
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
type AnyEventObject,
|
|
15
15
|
} from 'xstate'
|
|
16
16
|
import type {PortableTextSlateEditor} from '../types/editor'
|
|
17
|
-
import type {EditorSchema} from './
|
|
17
|
+
import type {EditorSchema} from './editor-schema'
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
20
|
* Makes sure editor mutation events are debounced
|
|
@@ -1,20 +1,14 @@
|
|
|
1
1
|
import {Editor, Element, Node, Transforms} from 'slate'
|
|
2
2
|
import {isChangingRemotely} from '../../internal-utils/withChanges'
|
|
3
3
|
import {isRedoing, isUndoing} from '../../internal-utils/withUndoRedo'
|
|
4
|
-
import type {
|
|
5
|
-
PortableTextMemberSchemaTypes,
|
|
6
|
-
PortableTextSlateEditor,
|
|
7
|
-
} from '../../types/editor'
|
|
4
|
+
import type {PortableTextSlateEditor} from '../../types/editor'
|
|
8
5
|
import type {EditorActor} from '../editor-machine'
|
|
9
6
|
|
|
10
7
|
/**
|
|
11
8
|
* This plugin makes sure that every new node in the editor get a new _key prop when created
|
|
12
9
|
*
|
|
13
10
|
*/
|
|
14
|
-
export function createWithObjectKeys(
|
|
15
|
-
editorActor: EditorActor,
|
|
16
|
-
schemaTypes: PortableTextMemberSchemaTypes,
|
|
17
|
-
) {
|
|
11
|
+
export function createWithObjectKeys(editorActor: EditorActor) {
|
|
18
12
|
return function withKeys(
|
|
19
13
|
editor: PortableTextSlateEditor,
|
|
20
14
|
): PortableTextSlateEditor {
|
|
@@ -89,7 +83,10 @@ export function createWithObjectKeys(
|
|
|
89
83
|
|
|
90
84
|
editor.normalizeNode = (entry) => {
|
|
91
85
|
const [node, path] = entry
|
|
92
|
-
if (
|
|
86
|
+
if (
|
|
87
|
+
Element.isElement(node) &&
|
|
88
|
+
node._type === editorActor.getSnapshot().context.schema.block.name
|
|
89
|
+
) {
|
|
93
90
|
// Set key on block itself
|
|
94
91
|
if (!node._key) {
|
|
95
92
|
editorActor.send({type: 'normalizing'})
|
|
@@ -25,10 +25,7 @@ import {
|
|
|
25
25
|
PATCHING,
|
|
26
26
|
withoutPatching,
|
|
27
27
|
} from '../../internal-utils/withoutPatching'
|
|
28
|
-
import type {
|
|
29
|
-
PortableTextMemberSchemaTypes,
|
|
30
|
-
PortableTextSlateEditor,
|
|
31
|
-
} from '../../types/editor'
|
|
28
|
+
import type {PortableTextSlateEditor} from '../../types/editor'
|
|
32
29
|
import type {EditorActor} from '../editor-machine'
|
|
33
30
|
import {getCurrentActionId} from '../with-applying-behavior-actions'
|
|
34
31
|
import {withoutSaving} from './createWithUndoRedo'
|
|
@@ -82,21 +79,19 @@ export interface PatchFunctions {
|
|
|
82
79
|
interface Options {
|
|
83
80
|
editorActor: EditorActor
|
|
84
81
|
patchFunctions: PatchFunctions
|
|
85
|
-
schemaTypes: PortableTextMemberSchemaTypes
|
|
86
82
|
subscriptions: Array<() => () => void>
|
|
87
83
|
}
|
|
88
84
|
|
|
89
85
|
export function createWithPatches({
|
|
90
86
|
editorActor,
|
|
91
87
|
patchFunctions,
|
|
92
|
-
schemaTypes,
|
|
93
88
|
subscriptions,
|
|
94
89
|
}: Options): (editor: PortableTextSlateEditor) => PortableTextSlateEditor {
|
|
95
90
|
// The previous editor children are needed to figure out the _key of deleted nodes
|
|
96
91
|
// The editor.children would no longer contain that information if the node is already deleted.
|
|
97
92
|
let previousChildren: Descendant[]
|
|
98
93
|
|
|
99
|
-
const applyPatch = createApplyPatch(
|
|
94
|
+
const applyPatch = createApplyPatch(editorActor.getSnapshot().context.schema)
|
|
100
95
|
|
|
101
96
|
return function withPatches(editor: PortableTextSlateEditor) {
|
|
102
97
|
IS_PROCESSING_REMOTE_CHANGES.set(editor, false)
|
|
@@ -156,12 +151,18 @@ export function createWithPatches({
|
|
|
156
151
|
// Update previous children here before we apply
|
|
157
152
|
previousChildren = editor.children
|
|
158
153
|
|
|
159
|
-
const editorWasEmpty = isEqualToEmptyEditor(
|
|
154
|
+
const editorWasEmpty = isEqualToEmptyEditor(
|
|
155
|
+
previousChildren,
|
|
156
|
+
editorActor.getSnapshot().context.schema,
|
|
157
|
+
)
|
|
160
158
|
|
|
161
159
|
// Apply the operation
|
|
162
160
|
apply(operation)
|
|
163
161
|
|
|
164
|
-
const editorIsEmpty = isEqualToEmptyEditor(
|
|
162
|
+
const editorIsEmpty = isEqualToEmptyEditor(
|
|
163
|
+
editor.children,
|
|
164
|
+
editorActor.getSnapshot().context.schema,
|
|
165
|
+
)
|
|
165
166
|
|
|
166
167
|
if (!isPatching(editor)) {
|
|
167
168
|
if (debugVerbose && debug.enabled)
|
|
@@ -275,7 +276,7 @@ export function createWithPatches({
|
|
|
275
276
|
type: 'notify.unset',
|
|
276
277
|
previousValue: fromSlateValue(
|
|
277
278
|
previousChildren,
|
|
278
|
-
|
|
279
|
+
editorActor.getSnapshot().context.schema.block.name,
|
|
279
280
|
KEY_TO_VALUE_ELEMENT.get(editor),
|
|
280
281
|
),
|
|
281
282
|
})
|
|
@@ -295,7 +296,7 @@ export function createWithPatches({
|
|
|
295
296
|
actionId: getCurrentActionId(editor),
|
|
296
297
|
value: fromSlateValue(
|
|
297
298
|
editor.children,
|
|
298
|
-
|
|
299
|
+
editorActor.getSnapshot().context.schema.block.name,
|
|
299
300
|
KEY_TO_VALUE_ELEMENT.get(editor),
|
|
300
301
|
),
|
|
301
302
|
})
|
|
@@ -1,18 +1,14 @@
|
|
|
1
1
|
import {Editor, Path, Text as SlateText, Transforms} from 'slate'
|
|
2
2
|
import {debugWithName} from '../../internal-utils/debug'
|
|
3
|
-
import type {
|
|
4
|
-
PortableTextMemberSchemaTypes,
|
|
5
|
-
PortableTextSlateEditor,
|
|
6
|
-
} from '../../types/editor'
|
|
3
|
+
import type {PortableTextSlateEditor} from '../../types/editor'
|
|
7
4
|
import type {EditorActor} from '../editor-machine'
|
|
8
5
|
|
|
9
6
|
const debug = debugWithName('plugin:withPortableTextBlockStyle')
|
|
10
7
|
|
|
11
8
|
export function createWithPortableTextBlockStyle(
|
|
12
9
|
editorActor: EditorActor,
|
|
13
|
-
types: PortableTextMemberSchemaTypes,
|
|
14
10
|
): (editor: PortableTextSlateEditor) => PortableTextSlateEditor {
|
|
15
|
-
const defaultStyle =
|
|
11
|
+
const defaultStyle = editorActor.getSnapshot().context.schema.styles[0].name
|
|
16
12
|
return function withPortableTextBlockStyle(
|
|
17
13
|
editor: PortableTextSlateEditor,
|
|
18
14
|
): PortableTextSlateEditor {
|
|
@@ -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 {
|