@portabletext/editor 1.15.1 → 1.15.3
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 +496 -0
- package/lib/_chunks-cjs/behavior.core.cjs.map +1 -0
- package/lib/_chunks-cjs/selector.get-text-before.cjs +0 -231
- package/lib/_chunks-cjs/selector.get-text-before.cjs.map +1 -1
- package/lib/_chunks-cjs/selectors.cjs +234 -0
- package/lib/_chunks-cjs/selectors.cjs.map +1 -0
- package/lib/_chunks-es/behavior.core.js +498 -0
- package/lib/_chunks-es/behavior.core.js.map +1 -0
- package/lib/_chunks-es/selector.get-text-before.js +2 -233
- package/lib/_chunks-es/selector.get-text-before.js.map +1 -1
- package/lib/_chunks-es/selectors.js +235 -0
- package/lib/_chunks-es/selectors.js.map +1 -0
- package/lib/behaviors/index.cjs +460 -0
- package/lib/behaviors/index.cjs.map +1 -0
- package/lib/behaviors/index.d.cts +869 -0
- package/lib/behaviors/index.d.ts +869 -0
- package/lib/behaviors/index.js +464 -0
- package/lib/behaviors/index.js.map +1 -0
- package/lib/index.cjs +51 -1035
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +146 -669
- package/lib/index.d.ts +146 -669
- package/lib/index.js +44 -1027
- package/lib/index.js.map +1 -1
- package/lib/selectors/index.cjs +16 -16
- package/lib/selectors/index.cjs.map +1 -1
- package/lib/selectors/index.d.cts +8 -12
- package/lib/selectors/index.d.ts +8 -12
- package/lib/selectors/index.js +3 -2
- package/lib/selectors/index.js.map +1 -1
- package/package.json +11 -5
- package/src/{editor/behavior → behavior-actions}/behavior.action-utils.insert-block.ts +4 -6
- package/src/{editor/behavior → behavior-actions}/behavior.action.insert-block-object.ts +1 -1
- package/src/{editor/behavior → behavior-actions}/behavior.action.insert-break.ts +1 -1
- package/src/{editor/behavior → behavior-actions}/behavior.action.insert-inline-object.ts +1 -1
- package/src/{editor/behavior → behavior-actions}/behavior.action.list-item.ts +2 -2
- package/src/{editor/behavior → behavior-actions}/behavior.action.text-block.set.ts +1 -1
- package/src/{editor/behavior → behavior-actions}/behavior.action.text-block.unset.ts +1 -1
- package/src/{editor/behavior → behavior-actions}/behavior.actions.ts +20 -20
- package/src/{editor/behavior → behavior-actions}/behavior.guards.ts +2 -6
- package/src/{editor/behavior → behaviors}/behavior.code-editor.ts +4 -4
- package/src/{editor/behavior → behaviors}/behavior.core.block-objects.ts +6 -6
- package/src/{editor/behavior → behaviors}/behavior.core.decorators.ts +6 -16
- package/src/{editor/behavior → behaviors}/behavior.core.lists.ts +17 -17
- package/src/{editor/behavior → behaviors}/behavior.links.ts +5 -5
- package/src/{editor/behavior → behaviors}/behavior.markdown.ts +25 -43
- package/src/{editor/behavior → behaviors}/behavior.types.ts +39 -46
- package/src/behaviors/index.ts +29 -0
- package/src/editor/PortableTextEditor.tsx +2 -2
- package/src/{utils/getPortableTextMemberSchemaTypes.ts → editor/create-editor-schema.ts} +3 -3
- package/src/editor/create-editor.ts +28 -48
- package/src/editor/define-schema.ts +8 -3
- package/src/editor/editor-machine.ts +21 -36
- package/src/editor/editor-selector.ts +1 -13
- package/src/editor/editor-snapshot.ts +4 -6
- package/src/editor/plugins/createWithEditableAPI.ts +3 -3
- package/src/editor/plugins/createWithPortableTextMarkModel.ts +1 -1
- package/src/editor/utils/utils.block-offset.ts +1 -1
- package/src/index.ts +14 -31
- package/src/selectors/index.ts +2 -4
- package/src/selectors/selector.get-active-list-item.ts +1 -1
- package/src/selectors/selectors.ts +1 -1
- package/src/type-utils.ts +17 -0
- package/src/utils/__tests__/operationToPatches.test.ts +3 -3
- package/src/utils/__tests__/patchToOperations.test.ts +3 -3
- package/src/utils/__tests__/values.test.ts +2 -2
- /package/src/{editor/behavior → behavior-actions}/behavior.action.insert-span.ts +0 -0
- /package/src/{editor/behavior → behavior-actions}/behavior.action.style.ts +0 -0
- /package/src/{editor/behavior → behaviors}/behavior.core.ts +0 -0
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import {isPortableTextTextBlock} from '@sanity/types'
|
|
2
|
-
import {
|
|
2
|
+
import type {EditorSchema} from '../editor/define-schema'
|
|
3
|
+
import {getTextBlockText} from '../editor/utils/utils'
|
|
4
|
+
import {spanSelectionPointToBlockOffset} from '../editor/utils/utils.block-offset'
|
|
5
|
+
import {getBlockTextBefore} from '../selectors/selector.get-text-before'
|
|
3
6
|
import {
|
|
4
7
|
getFocusBlock,
|
|
5
8
|
getFocusSpan,
|
|
6
9
|
getFocusTextBlock,
|
|
7
10
|
selectionIsCollapsed,
|
|
8
|
-
} from '
|
|
9
|
-
import type {PortableTextMemberSchemaTypes} from '../../types/editor'
|
|
10
|
-
import {getTextBlockText} from '../utils/utils'
|
|
11
|
-
import {spanSelectionPointToBlockOffset} from '../utils/utils.block-offset'
|
|
11
|
+
} from '../selectors/selectors'
|
|
12
12
|
import {defineBehavior} from './behavior.types'
|
|
13
13
|
|
|
14
14
|
/**
|
|
@@ -16,24 +16,16 @@ import {defineBehavior} from './behavior.types'
|
|
|
16
16
|
*/
|
|
17
17
|
export type MarkdownBehaviorsConfig = {
|
|
18
18
|
horizontalRuleObject?: (context: {
|
|
19
|
-
schema:
|
|
19
|
+
schema: EditorSchema
|
|
20
20
|
}) => {name: string; value?: {[prop: string]: unknown}} | undefined
|
|
21
|
-
defaultStyle?: (context: {
|
|
22
|
-
schema: PortableTextMemberSchemaTypes
|
|
23
|
-
}) => string | undefined
|
|
21
|
+
defaultStyle?: (context: {schema: EditorSchema}) => string | undefined
|
|
24
22
|
headingStyle?: (context: {
|
|
25
|
-
schema:
|
|
23
|
+
schema: EditorSchema
|
|
26
24
|
level: number
|
|
27
25
|
}) => string | undefined
|
|
28
|
-
blockquoteStyle?: (context: {
|
|
29
|
-
|
|
30
|
-
}) => string | undefined
|
|
31
|
-
unorderedListStyle?: (context: {
|
|
32
|
-
schema: PortableTextMemberSchemaTypes
|
|
33
|
-
}) => string | undefined
|
|
34
|
-
orderedListStyle?: (context: {
|
|
35
|
-
schema: PortableTextMemberSchemaTypes
|
|
36
|
-
}) => string | undefined
|
|
26
|
+
blockquoteStyle?: (context: {schema: EditorSchema}) => string | undefined
|
|
27
|
+
unorderedListStyle?: (context: {schema: EditorSchema}) => string | undefined
|
|
28
|
+
orderedListStyle?: (context: {schema: EditorSchema}) => string | undefined
|
|
37
29
|
}
|
|
38
30
|
|
|
39
31
|
/**
|
|
@@ -65,7 +57,7 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
|
|
|
65
57
|
'children',
|
|
66
58
|
{_key: focusSpan.node._key},
|
|
67
59
|
],
|
|
68
|
-
offset: context.selection
|
|
60
|
+
offset: context.selection?.focus.offset ?? 0,
|
|
69
61
|
},
|
|
70
62
|
})
|
|
71
63
|
|
|
@@ -95,7 +87,7 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
|
|
|
95
87
|
text: ' ',
|
|
96
88
|
},
|
|
97
89
|
],
|
|
98
|
-
({focusTextBlock, style}) => [
|
|
90
|
+
(_, {focusTextBlock, style}) => [
|
|
99
91
|
{
|
|
100
92
|
type: 'text block.unset',
|
|
101
93
|
props: ['listItem', 'level'],
|
|
@@ -163,13 +155,13 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
|
|
|
163
155
|
return false
|
|
164
156
|
},
|
|
165
157
|
actions: [
|
|
166
|
-
({hrCharacter}) => [
|
|
158
|
+
(_, {hrCharacter}) => [
|
|
167
159
|
{
|
|
168
160
|
type: 'insert.text',
|
|
169
161
|
text: hrCharacter,
|
|
170
162
|
},
|
|
171
163
|
],
|
|
172
|
-
({hrObject, hrBlockOffsets}) => [
|
|
164
|
+
(_, {hrObject, hrBlockOffsets}) => [
|
|
173
165
|
{
|
|
174
166
|
type: 'insert.block object',
|
|
175
167
|
placement: 'before',
|
|
@@ -198,13 +190,13 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
|
|
|
198
190
|
return {hrCharacters, hrObject, focusBlock}
|
|
199
191
|
},
|
|
200
192
|
actions: [
|
|
201
|
-
({hrCharacters}) => [
|
|
193
|
+
(_, {hrCharacters}) => [
|
|
202
194
|
{
|
|
203
195
|
type: 'insert.text',
|
|
204
196
|
text: hrCharacters,
|
|
205
197
|
},
|
|
206
198
|
],
|
|
207
|
-
({hrObject, focusBlock}) =>
|
|
199
|
+
(_, {hrObject, focusBlock}) =>
|
|
208
200
|
isPortableTextTextBlock(focusBlock.node)
|
|
209
201
|
? [
|
|
210
202
|
{
|
|
@@ -253,7 +245,7 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
|
|
|
253
245
|
'children',
|
|
254
246
|
{_key: focusSpan.node._key},
|
|
255
247
|
],
|
|
256
|
-
offset: context.selection
|
|
248
|
+
offset: context.selection?.focus.offset ?? 0,
|
|
257
249
|
},
|
|
258
250
|
})
|
|
259
251
|
|
|
@@ -288,13 +280,8 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
|
|
|
288
280
|
return false
|
|
289
281
|
},
|
|
290
282
|
actions: [
|
|
291
|
-
() => [
|
|
292
|
-
|
|
293
|
-
type: 'insert.text',
|
|
294
|
-
text: ' ',
|
|
295
|
-
},
|
|
296
|
-
],
|
|
297
|
-
({focusTextBlock, style, level}) => [
|
|
283
|
+
({event}) => [event],
|
|
284
|
+
(_, {focusTextBlock, style, level}) => [
|
|
298
285
|
{
|
|
299
286
|
type: 'text block.unset',
|
|
300
287
|
props: ['listItem', 'level'],
|
|
@@ -332,7 +319,7 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
|
|
|
332
319
|
|
|
333
320
|
const atTheBeginningOfBLock =
|
|
334
321
|
focusTextBlock.node.children[0]._key === focusSpan.node._key &&
|
|
335
|
-
context.selection
|
|
322
|
+
context.selection?.focus.offset === 0
|
|
336
323
|
|
|
337
324
|
const defaultStyle = config.defaultStyle?.(context)
|
|
338
325
|
|
|
@@ -347,7 +334,7 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
|
|
|
347
334
|
return false
|
|
348
335
|
},
|
|
349
336
|
actions: [
|
|
350
|
-
({defaultStyle, focusTextBlock}) => [
|
|
337
|
+
(_, {defaultStyle, focusTextBlock}) => [
|
|
351
338
|
{
|
|
352
339
|
type: 'text block.set',
|
|
353
340
|
style: defaultStyle,
|
|
@@ -381,7 +368,7 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
|
|
|
381
368
|
'children',
|
|
382
369
|
{_key: focusSpan.node._key},
|
|
383
370
|
],
|
|
384
|
-
offset: context.selection
|
|
371
|
+
offset: context.selection?.focus.offset ?? 0,
|
|
385
372
|
},
|
|
386
373
|
})
|
|
387
374
|
|
|
@@ -430,13 +417,8 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
|
|
|
430
417
|
return false
|
|
431
418
|
},
|
|
432
419
|
actions: [
|
|
433
|
-
() => [
|
|
434
|
-
|
|
435
|
-
type: 'insert.text',
|
|
436
|
-
text: ' ',
|
|
437
|
-
},
|
|
438
|
-
],
|
|
439
|
-
({focusTextBlock, style, listItem, listItemLength}) => [
|
|
420
|
+
({event}) => [event],
|
|
421
|
+
(_, {focusTextBlock, style, listItem, listItemLength}) => [
|
|
440
422
|
{
|
|
441
423
|
type: 'text block.set',
|
|
442
424
|
listItem,
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import type {KeyedSegment, PortableTextTextBlock} from '@sanity/types'
|
|
2
2
|
import type {TextUnit} from 'slate'
|
|
3
3
|
import type {TextInsertTextOptions} from 'slate/dist/interfaces/transforms/text'
|
|
4
|
-
import type {
|
|
5
|
-
import type {
|
|
4
|
+
import type {EditorContext} from '../editor/editor-snapshot'
|
|
5
|
+
import type {PickFromUnion} from '../type-utils'
|
|
6
|
+
import type {EditorSelection, PortableTextSlateEditor} from '../types/editor'
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* @alpha
|
|
@@ -70,10 +71,10 @@ export type SyntheticBehaviorEvent =
|
|
|
70
71
|
}
|
|
71
72
|
}
|
|
72
73
|
| {
|
|
73
|
-
type: 'insert.
|
|
74
|
+
type: 'insert.break'
|
|
74
75
|
}
|
|
75
76
|
| {
|
|
76
|
-
type: 'insert.break'
|
|
77
|
+
type: 'insert.soft break'
|
|
77
78
|
}
|
|
78
79
|
| {
|
|
79
80
|
type: 'insert.text'
|
|
@@ -116,20 +117,6 @@ export type NativeBehaviorEvent =
|
|
|
116
117
|
data: DataTransfer
|
|
117
118
|
}
|
|
118
119
|
|
|
119
|
-
/**
|
|
120
|
-
* @alpha
|
|
121
|
-
*/
|
|
122
|
-
export type BehaviorGuard<
|
|
123
|
-
TAnyBehaviorEvent extends BehaviorEvent,
|
|
124
|
-
TGuardResponse,
|
|
125
|
-
> = ({
|
|
126
|
-
context,
|
|
127
|
-
event,
|
|
128
|
-
}: {
|
|
129
|
-
context: EditorContext
|
|
130
|
-
event: TAnyBehaviorEvent
|
|
131
|
-
}) => TGuardResponse | false
|
|
132
|
-
|
|
133
120
|
/**
|
|
134
121
|
* @alpha
|
|
135
122
|
*/
|
|
@@ -189,17 +176,17 @@ export type BehaviorActionIntend =
|
|
|
189
176
|
effect: () => void
|
|
190
177
|
}
|
|
191
178
|
| {
|
|
192
|
-
type: '
|
|
193
|
-
selection: EditorSelection
|
|
179
|
+
type: 'reselect'
|
|
194
180
|
}
|
|
195
181
|
| {
|
|
196
|
-
type: 'select
|
|
182
|
+
type: 'select'
|
|
183
|
+
selection: EditorSelection
|
|
197
184
|
}
|
|
198
185
|
| {
|
|
199
|
-
type: 'select
|
|
186
|
+
type: 'select.previous block'
|
|
200
187
|
}
|
|
201
188
|
| {
|
|
202
|
-
type: '
|
|
189
|
+
type: 'select.next block'
|
|
203
190
|
}
|
|
204
191
|
| {
|
|
205
192
|
type: 'style.add'
|
|
@@ -238,32 +225,56 @@ export type BehaviorEvent = SyntheticBehaviorEvent | NativeBehaviorEvent
|
|
|
238
225
|
* @alpha
|
|
239
226
|
*/
|
|
240
227
|
export type Behavior<
|
|
241
|
-
|
|
228
|
+
TBehaviorEventType extends BehaviorEvent['type'] = BehaviorEvent['type'],
|
|
242
229
|
TGuardResponse = true,
|
|
243
230
|
> = {
|
|
244
231
|
/**
|
|
245
232
|
* The internal editor event that triggers this behavior.
|
|
246
233
|
*/
|
|
247
|
-
on:
|
|
234
|
+
on: TBehaviorEventType
|
|
248
235
|
/**
|
|
249
236
|
* Predicate function that determines if the behavior should be executed.
|
|
250
237
|
* Returning a non-nullable value from the guard will pass the value to the
|
|
251
238
|
* actions and execute them.
|
|
252
239
|
*/
|
|
253
240
|
guard?: BehaviorGuard<
|
|
254
|
-
PickFromUnion<BehaviorEvent, 'type',
|
|
241
|
+
PickFromUnion<BehaviorEvent, 'type', TBehaviorEventType>,
|
|
255
242
|
TGuardResponse
|
|
256
243
|
>
|
|
257
244
|
/**
|
|
258
245
|
* Array of behavior action sets.
|
|
259
246
|
*/
|
|
260
|
-
actions: Array<BehaviorActionIntendSet<TGuardResponse>>
|
|
247
|
+
actions: Array<BehaviorActionIntendSet<TBehaviorEventType, TGuardResponse>>
|
|
261
248
|
}
|
|
262
249
|
|
|
263
250
|
/**
|
|
264
251
|
* @alpha
|
|
265
252
|
*/
|
|
266
|
-
export type
|
|
253
|
+
export type BehaviorGuard<
|
|
254
|
+
TBehaviorEvent extends BehaviorEvent,
|
|
255
|
+
TGuardResponse,
|
|
256
|
+
> = ({
|
|
257
|
+
context,
|
|
258
|
+
event,
|
|
259
|
+
}: {
|
|
260
|
+
context: EditorContext
|
|
261
|
+
event: TBehaviorEvent
|
|
262
|
+
}) => TGuardResponse | false
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* @alpha
|
|
266
|
+
*/
|
|
267
|
+
export type BehaviorActionIntendSet<
|
|
268
|
+
TBehaviorEventType extends BehaviorEvent['type'] = BehaviorEvent['type'],
|
|
269
|
+
TGuardResponse = true,
|
|
270
|
+
> = (
|
|
271
|
+
{
|
|
272
|
+
context,
|
|
273
|
+
event,
|
|
274
|
+
}: {
|
|
275
|
+
context: EditorContext
|
|
276
|
+
event: PickFromUnion<BehaviorEvent, 'type', TBehaviorEventType>
|
|
277
|
+
},
|
|
267
278
|
guardResponse: TGuardResponse,
|
|
268
279
|
) => Array<BehaviorActionIntend>
|
|
269
280
|
|
|
@@ -284,21 +295,3 @@ export type BlockOffset = {
|
|
|
284
295
|
path: [KeyedSegment]
|
|
285
296
|
offset: number
|
|
286
297
|
}
|
|
287
|
-
|
|
288
|
-
/**
|
|
289
|
-
* @alpha
|
|
290
|
-
*/
|
|
291
|
-
export type PickFromUnion<
|
|
292
|
-
TUnion,
|
|
293
|
-
TTagKey extends keyof TUnion,
|
|
294
|
-
TPickedTags extends TUnion[TTagKey],
|
|
295
|
-
> = TUnion extends Record<TTagKey, TPickedTags> ? TUnion : never
|
|
296
|
-
|
|
297
|
-
/**
|
|
298
|
-
* @alpha
|
|
299
|
-
*/
|
|
300
|
-
export type OmitFromUnion<
|
|
301
|
-
TUnion,
|
|
302
|
-
TTagKey extends keyof TUnion,
|
|
303
|
-
TOmittedTags extends TUnion[TTagKey],
|
|
304
|
-
> = TUnion extends Record<TTagKey, TOmittedTags> ? never : TUnion
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export type {EditorSchema} from '../editor/define-schema'
|
|
2
|
+
export type {EditorContext} from '../editor/editor-snapshot'
|
|
3
|
+
export type {PickFromUnion} from '../type-utils'
|
|
4
|
+
export type {
|
|
5
|
+
EditorSelection,
|
|
6
|
+
EditorSelectionPoint,
|
|
7
|
+
PortableTextMemberSchemaTypes,
|
|
8
|
+
} from '../types/editor'
|
|
9
|
+
export {
|
|
10
|
+
createCodeEditorBehaviors,
|
|
11
|
+
type CodeEditorBehaviorsConfig,
|
|
12
|
+
} from './behavior.code-editor'
|
|
13
|
+
export {coreBehavior, coreBehaviors} from './behavior.core'
|
|
14
|
+
export {createLinkBehaviors, type LinkBehaviorsConfig} from './behavior.links'
|
|
15
|
+
export {
|
|
16
|
+
createMarkdownBehaviors,
|
|
17
|
+
type MarkdownBehaviorsConfig,
|
|
18
|
+
} from './behavior.markdown'
|
|
19
|
+
export {
|
|
20
|
+
defineBehavior,
|
|
21
|
+
type Behavior,
|
|
22
|
+
type BehaviorActionIntend,
|
|
23
|
+
type BehaviorActionIntendSet,
|
|
24
|
+
type BehaviorEvent,
|
|
25
|
+
type BehaviorGuard,
|
|
26
|
+
type BlockOffset,
|
|
27
|
+
type NativeBehaviorEvent,
|
|
28
|
+
type SyntheticBehaviorEvent,
|
|
29
|
+
} from './behavior.types'
|
|
@@ -25,10 +25,10 @@ import type {
|
|
|
25
25
|
PortableTextMemberSchemaTypes,
|
|
26
26
|
} from '../types/editor'
|
|
27
27
|
import {debugWithName} from '../utils/debug'
|
|
28
|
-
import {getPortableTextMemberSchemaTypes} from '../utils/getPortableTextMemberSchemaTypes'
|
|
29
28
|
import {compileType} from '../utils/schema'
|
|
30
29
|
import {Synchronizer} from './components/Synchronizer'
|
|
31
30
|
import {createEditor, type Editor} from './create-editor'
|
|
31
|
+
import {createEditorSchema} from './create-editor-schema'
|
|
32
32
|
import {EditorActorContext} from './editor-actor-context'
|
|
33
33
|
import type {EditorActor} from './editor-machine'
|
|
34
34
|
import {PortableTextEditorContext} from './hooks/usePortableTextEditor'
|
|
@@ -158,7 +158,7 @@ export class PortableTextEditor extends Component<
|
|
|
158
158
|
!prevProps.editor &&
|
|
159
159
|
this.props.schemaType !== prevProps.schemaType
|
|
160
160
|
) {
|
|
161
|
-
this.schemaTypes =
|
|
161
|
+
this.schemaTypes = createEditorSchema(
|
|
162
162
|
this.props.schemaType.hasOwnProperty('jsonType')
|
|
163
163
|
? this.props.schemaType
|
|
164
164
|
: compileType(this.props.schemaType),
|
|
@@ -6,11 +6,11 @@ import type {
|
|
|
6
6
|
SchemaType,
|
|
7
7
|
SpanSchemaType,
|
|
8
8
|
} from '@sanity/types'
|
|
9
|
-
import type {
|
|
9
|
+
import type {EditorSchema} from './define-schema'
|
|
10
10
|
|
|
11
|
-
export function
|
|
11
|
+
export function createEditorSchema(
|
|
12
12
|
portableTextType: ArraySchemaType<PortableTextBlock>,
|
|
13
|
-
):
|
|
13
|
+
): EditorSchema {
|
|
14
14
|
if (!portableTextType) {
|
|
15
15
|
throw new Error("Parameter 'portabletextType' missing (required)")
|
|
16
16
|
}
|
|
@@ -11,10 +11,11 @@ import {
|
|
|
11
11
|
type EventObject,
|
|
12
12
|
type Snapshot,
|
|
13
13
|
} from 'xstate'
|
|
14
|
+
import type {Behavior} from '../behaviors/behavior.types'
|
|
15
|
+
import type {PickFromUnion} from '../type-utils'
|
|
14
16
|
import type {EditableAPI} from '../types/editor'
|
|
15
|
-
import {getPortableTextMemberSchemaTypes} from '../utils/getPortableTextMemberSchemaTypes'
|
|
16
17
|
import {compileType} from '../utils/schema'
|
|
17
|
-
import
|
|
18
|
+
import {createEditorSchema} from './create-editor-schema'
|
|
18
19
|
import {createSlateEditor, type SlateEditor} from './create-slate-editor'
|
|
19
20
|
import {compileSchemaDefinition, type SchemaDefinition} from './define-schema'
|
|
20
21
|
import {
|
|
@@ -81,64 +82,21 @@ export type Editor = {
|
|
|
81
82
|
}
|
|
82
83
|
}
|
|
83
84
|
|
|
84
|
-
/**
|
|
85
|
-
* @internal
|
|
86
|
-
*/
|
|
87
85
|
export function createEditor(config: EditorConfig): Editor {
|
|
88
86
|
const editorActor = createActor(editorMachine, {
|
|
89
87
|
input: editorConfigToMachineInput(config),
|
|
90
88
|
})
|
|
91
|
-
|
|
92
89
|
editorActor.start()
|
|
93
90
|
|
|
94
|
-
|
|
95
|
-
const editable = createEditableAPI(slateEditor.instance, editorActor)
|
|
96
|
-
|
|
97
|
-
return {
|
|
98
|
-
send: (event) => {
|
|
99
|
-
editorActor.send(event)
|
|
100
|
-
},
|
|
101
|
-
on: (event, listener) =>
|
|
102
|
-
editorActor.on(
|
|
103
|
-
event,
|
|
104
|
-
// @ts-expect-error
|
|
105
|
-
listener,
|
|
106
|
-
),
|
|
107
|
-
_internal: {
|
|
108
|
-
editable,
|
|
109
|
-
editorActor,
|
|
110
|
-
slateEditor,
|
|
111
|
-
},
|
|
112
|
-
}
|
|
91
|
+
return createEditorFromActor(editorActor)
|
|
113
92
|
}
|
|
114
93
|
|
|
115
94
|
export function useCreateEditor(config: EditorConfig): Editor {
|
|
116
95
|
const editorActor = useActorRef(editorMachine, {
|
|
117
96
|
input: editorConfigToMachineInput(config),
|
|
118
97
|
})
|
|
119
|
-
const editor: Editor = useMemo(() => {
|
|
120
|
-
const slateEditor = createSlateEditor({editorActor})
|
|
121
|
-
const editable = createEditableAPI(slateEditor.instance, editorActor)
|
|
122
|
-
|
|
123
|
-
return {
|
|
124
|
-
send: (event: EditorEvent) => {
|
|
125
|
-
editorActor.send(event)
|
|
126
|
-
},
|
|
127
|
-
on: (event, listener) =>
|
|
128
|
-
editorActor.on(
|
|
129
|
-
event,
|
|
130
|
-
// @ts-expect-error
|
|
131
|
-
listener,
|
|
132
|
-
),
|
|
133
|
-
_internal: {
|
|
134
|
-
editable,
|
|
135
|
-
editorActor,
|
|
136
|
-
slateEditor,
|
|
137
|
-
},
|
|
138
|
-
}
|
|
139
|
-
}, [editorActor])
|
|
140
98
|
|
|
141
|
-
return
|
|
99
|
+
return useMemo(() => createEditorFromActor(editorActor), [editorActor])
|
|
142
100
|
}
|
|
143
101
|
|
|
144
102
|
function editorConfigToMachineInput(config: EditorConfig) {
|
|
@@ -149,7 +107,7 @@ function editorConfigToMachineInput(config: EditorConfig) {
|
|
|
149
107
|
readOnly: config.readOnly,
|
|
150
108
|
schema: config.schemaDefinition
|
|
151
109
|
? compileSchemaDefinition(config.schemaDefinition)
|
|
152
|
-
:
|
|
110
|
+
: createEditorSchema(
|
|
153
111
|
config.schema.hasOwnProperty('jsonType')
|
|
154
112
|
? config.schema
|
|
155
113
|
: compileType(config.schema),
|
|
@@ -157,3 +115,25 @@ function editorConfigToMachineInput(config: EditorConfig) {
|
|
|
157
115
|
value: config.initialValue,
|
|
158
116
|
} as const
|
|
159
117
|
}
|
|
118
|
+
|
|
119
|
+
function createEditorFromActor(editorActor: EditorActor): Editor {
|
|
120
|
+
const slateEditor = createSlateEditor({editorActor})
|
|
121
|
+
const editable = createEditableAPI(slateEditor.instance, editorActor)
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
send: (event) => {
|
|
125
|
+
editorActor.send(event)
|
|
126
|
+
},
|
|
127
|
+
on: (event, listener) =>
|
|
128
|
+
editorActor.on(
|
|
129
|
+
event,
|
|
130
|
+
// @ts-expect-error
|
|
131
|
+
listener,
|
|
132
|
+
),
|
|
133
|
+
_internal: {
|
|
134
|
+
editable,
|
|
135
|
+
editorActor,
|
|
136
|
+
slateEditor,
|
|
137
|
+
},
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
} from '@sanity/types'
|
|
8
8
|
import startCase from 'lodash.startcase'
|
|
9
9
|
import type {PortableTextMemberSchemaTypes} from '../types/editor'
|
|
10
|
-
import {
|
|
10
|
+
import {createEditorSchema} from './create-editor-schema'
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* @alpha
|
|
@@ -41,6 +41,11 @@ export function defineSchema<const TSchemaDefinition extends SchemaDefinition>(
|
|
|
41
41
|
return definition
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
+
/**
|
|
45
|
+
* @alpha
|
|
46
|
+
*/
|
|
47
|
+
export type EditorSchema = PortableTextMemberSchemaTypes
|
|
48
|
+
|
|
44
49
|
export function compileSchemaDefinition<
|
|
45
50
|
TSchemaDefinition extends SchemaDefinition,
|
|
46
51
|
>(definition?: TSchemaDefinition) {
|
|
@@ -111,7 +116,7 @@ export function compileSchemaDefinition<
|
|
|
111
116
|
types: [portableTextSchema, ...blockObjects, ...inlineObjects],
|
|
112
117
|
}).get('portable-text')
|
|
113
118
|
|
|
114
|
-
const pteSchema =
|
|
119
|
+
const pteSchema = createEditorSchema(schema)
|
|
115
120
|
|
|
116
121
|
return {
|
|
117
122
|
...pteSchema,
|
|
@@ -127,5 +132,5 @@ export function compileSchemaDefinition<
|
|
|
127
132
|
} as ObjectSchemaType)
|
|
128
133
|
: blockObject,
|
|
129
134
|
),
|
|
130
|
-
} satisfies
|
|
135
|
+
} satisfies EditorSchema
|
|
131
136
|
}
|
|
@@ -10,27 +10,26 @@ import {
|
|
|
10
10
|
setup,
|
|
11
11
|
type ActorRefFrom,
|
|
12
12
|
} from 'xstate'
|
|
13
|
+
import {performAction} from '../behavior-actions/behavior.actions'
|
|
14
|
+
import {coreBehaviors} from '../behaviors/behavior.core'
|
|
15
|
+
import type {
|
|
16
|
+
Behavior,
|
|
17
|
+
BehaviorAction,
|
|
18
|
+
BehaviorActionIntend,
|
|
19
|
+
NativeBehaviorEvent,
|
|
20
|
+
SyntheticBehaviorEvent,
|
|
21
|
+
} from '../behaviors/behavior.types'
|
|
22
|
+
import type {OmitFromUnion, PickFromUnion} from '../type-utils'
|
|
13
23
|
import type {
|
|
14
24
|
EditorSelection,
|
|
15
25
|
InvalidValueResolution,
|
|
16
|
-
PortableTextMemberSchemaTypes,
|
|
17
26
|
PortableTextSlateEditor,
|
|
18
27
|
} from '../types/editor'
|
|
19
28
|
import debug from '../utils/debug'
|
|
20
29
|
import {toPortableTextRange} from '../utils/ranges'
|
|
21
30
|
import {fromSlateValue} from '../utils/values'
|
|
22
31
|
import {KEY_TO_VALUE_ELEMENT} from '../utils/weakMaps'
|
|
23
|
-
import {
|
|
24
|
-
import {coreBehaviors} from './behavior/behavior.core'
|
|
25
|
-
import type {
|
|
26
|
-
Behavior,
|
|
27
|
-
BehaviorAction,
|
|
28
|
-
BehaviorActionIntend,
|
|
29
|
-
NativeBehaviorEvent,
|
|
30
|
-
OmitFromUnion,
|
|
31
|
-
PickFromUnion,
|
|
32
|
-
SyntheticBehaviorEvent,
|
|
33
|
-
} from './behavior/behavior.types'
|
|
32
|
+
import type {EditorSchema} from './define-schema'
|
|
34
33
|
import type {EditorContext} from './editor-snapshot'
|
|
35
34
|
|
|
36
35
|
export * from 'xstate/guards'
|
|
@@ -82,7 +81,7 @@ export type InternalEditorEvent =
|
|
|
82
81
|
}
|
|
83
82
|
| {
|
|
84
83
|
type: 'update schema'
|
|
85
|
-
schema:
|
|
84
|
+
schema: EditorSchema
|
|
86
85
|
}
|
|
87
86
|
| {
|
|
88
87
|
type: 'update behaviors'
|
|
@@ -180,10 +179,10 @@ export const editorMachine = setup({
|
|
|
180
179
|
behaviors: Array<Behavior>
|
|
181
180
|
keyGenerator: () => string
|
|
182
181
|
pendingEvents: Array<PatchEvent | MutationEvent>
|
|
183
|
-
schema:
|
|
182
|
+
schema: EditorSchema
|
|
184
183
|
readOnly: boolean
|
|
185
184
|
maxBlocks: number | undefined
|
|
186
|
-
selection:
|
|
185
|
+
selection: EditorSelection
|
|
187
186
|
value: Array<PortableTextBlock> | undefined
|
|
188
187
|
},
|
|
189
188
|
events: {} as InternalEditorEvent,
|
|
@@ -193,7 +192,7 @@ export const editorMachine = setup({
|
|
|
193
192
|
keyGenerator: () => string
|
|
194
193
|
maxBlocks?: number
|
|
195
194
|
readOnly?: boolean
|
|
196
|
-
schema:
|
|
195
|
+
schema: EditorSchema
|
|
197
196
|
value?: Array<PortableTextBlock>
|
|
198
197
|
},
|
|
199
198
|
},
|
|
@@ -276,23 +275,6 @@ export const editorMachine = setup({
|
|
|
276
275
|
context.schema,
|
|
277
276
|
)
|
|
278
277
|
|
|
279
|
-
if (!selection) {
|
|
280
|
-
console.warn(
|
|
281
|
-
`Unable to handle event ${event.type} due to missing selection`,
|
|
282
|
-
)
|
|
283
|
-
|
|
284
|
-
if (!defaultAction) {
|
|
285
|
-
return
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
enqueue.raise({
|
|
289
|
-
type: 'behavior action intends',
|
|
290
|
-
editor: event.editor,
|
|
291
|
-
actionIntends: [defaultAction],
|
|
292
|
-
})
|
|
293
|
-
return
|
|
294
|
-
}
|
|
295
|
-
|
|
296
278
|
const editorContext = {
|
|
297
279
|
keyGenerator: context.keyGenerator,
|
|
298
280
|
schema: context.schema,
|
|
@@ -315,7 +297,10 @@ export const editorMachine = setup({
|
|
|
315
297
|
}
|
|
316
298
|
|
|
317
299
|
const actionIntendSets = eventBehavior.actions.map((actionSet) =>
|
|
318
|
-
actionSet(
|
|
300
|
+
actionSet(
|
|
301
|
+
{context: editorContext, event: event.behaviorEvent},
|
|
302
|
+
shouldRun,
|
|
303
|
+
),
|
|
319
304
|
)
|
|
320
305
|
|
|
321
306
|
for (const actionIntends of actionIntendSets) {
|
|
@@ -359,7 +344,7 @@ export const editorMachine = setup({
|
|
|
359
344
|
keyGenerator: input.keyGenerator,
|
|
360
345
|
pendingEvents: [],
|
|
361
346
|
schema: input.schema,
|
|
362
|
-
selection:
|
|
347
|
+
selection: null,
|
|
363
348
|
readOnly: input.readOnly ?? false,
|
|
364
349
|
maxBlocks: input.maxBlocks,
|
|
365
350
|
value: input.value,
|
|
@@ -408,7 +393,7 @@ export const editorMachine = setup({
|
|
|
408
393
|
'error': {actions: emit(({event}) => event)},
|
|
409
394
|
'selection': {
|
|
410
395
|
actions: [
|
|
411
|
-
assign({selection: ({event}) => event.selection
|
|
396
|
+
assign({selection: ({event}) => event.selection}),
|
|
412
397
|
emit(({event}) => event),
|
|
413
398
|
],
|
|
414
399
|
},
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import {useSelector} from '@xstate/react'
|
|
2
|
-
import type {EditorSelection} from '../types/editor'
|
|
3
2
|
import type {Editor} from './create-editor'
|
|
4
3
|
import type {EditorSnapshot} from './editor-snapshot'
|
|
5
4
|
import {getValue} from './get-value'
|
|
@@ -11,18 +10,7 @@ function defaultCompare<T>(a: T, b: T) {
|
|
|
11
10
|
/**
|
|
12
11
|
* @alpha
|
|
13
12
|
*/
|
|
14
|
-
export type
|
|
15
|
-
context: Omit<EditorSnapshot['context'], 'selection'> & {
|
|
16
|
-
selection?: NonNullable<EditorSelection>
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* @alpha
|
|
22
|
-
*/
|
|
23
|
-
export type EditorSelector<TSelected> = (
|
|
24
|
-
snapshot: EditorSelectorSnapshot,
|
|
25
|
-
) => TSelected
|
|
13
|
+
export type EditorSelector<TSelected> = (snapshot: EditorSnapshot) => TSelected
|
|
26
14
|
|
|
27
15
|
/**
|
|
28
16
|
* @alpha
|