@portabletext/editor 1.15.2 → 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 +5 -3
- package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
- package/lib/_chunks-cjs/selectors.cjs.map +1 -1
- package/lib/_chunks-es/behavior.core.js +5 -3
- package/lib/_chunks-es/behavior.core.js.map +1 -1
- package/lib/_chunks-es/selectors.js.map +1 -1
- package/lib/behaviors/index.cjs +12 -12
- package/lib/behaviors/index.cjs.map +1 -1
- package/lib/behaviors/index.d.cts +39 -45
- package/lib/behaviors/index.d.ts +39 -45
- package/lib/behaviors/index.js +12 -12
- package/lib/behaviors/index.js.map +1 -1
- package/lib/index.cjs +13 -27
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +129 -133
- package/lib/index.d.ts +129 -133
- package/lib/index.js +13 -27
- package/lib/index.js.map +1 -1
- package/lib/selectors/index.d.cts +8 -12
- package/lib/selectors/index.d.ts +8 -12
- package/package.json +2 -2
- package/src/behavior-actions/behavior.action-utils.insert-block.ts +3 -5
- package/src/behavior-actions/behavior.actions.ts +6 -6
- package/src/behavior-actions/behavior.guards.ts +2 -6
- package/src/behaviors/behavior.core.block-objects.ts +1 -1
- package/src/behaviors/behavior.core.lists.ts +2 -2
- package/src/behaviors/behavior.links.ts +2 -2
- package/src/behaviors/behavior.markdown.ts +11 -19
- package/src/behaviors/behavior.types.ts +7 -7
- package/src/behaviors/index.ts +1 -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 +2 -2
- package/src/editor/define-schema.ts +8 -3
- package/src/editor/editor-machine.ts +7 -24
- package/src/editor/editor-selector.ts +1 -13
- package/src/editor/editor-snapshot.ts +4 -6
- package/src/index.ts +3 -5
- package/src/selectors/index.ts +2 -4
- package/src/utils/__tests__/operationToPatches.test.ts +2 -2
- package/src/utils/__tests__/patchToOperations.test.ts +2 -2
- package/src/utils/__tests__/values.test.ts +2 -2
|
@@ -20,11 +20,16 @@ import {
|
|
|
20
20
|
*/
|
|
21
21
|
export declare type EditorContext = {
|
|
22
22
|
keyGenerator: () => string
|
|
23
|
-
schema:
|
|
24
|
-
selection:
|
|
23
|
+
schema: EditorSchema
|
|
24
|
+
selection: EditorSelection
|
|
25
25
|
value: Array<PortableTextBlock>
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
/**
|
|
29
|
+
* @alpha
|
|
30
|
+
*/
|
|
31
|
+
export declare type EditorSchema = PortableTextMemberSchemaTypes
|
|
32
|
+
|
|
28
33
|
/** @beta */
|
|
29
34
|
export declare type EditorSelection = {
|
|
30
35
|
anchor: EditorSelectionPoint
|
|
@@ -42,18 +47,9 @@ export declare type EditorSelectionPoint = {
|
|
|
42
47
|
* @alpha
|
|
43
48
|
*/
|
|
44
49
|
export declare type EditorSelector<TSelected> = (
|
|
45
|
-
snapshot:
|
|
50
|
+
snapshot: EditorSnapshot,
|
|
46
51
|
) => TSelected
|
|
47
52
|
|
|
48
|
-
/**
|
|
49
|
-
* @alpha
|
|
50
|
-
*/
|
|
51
|
-
export declare type EditorSelectorSnapshot = {
|
|
52
|
-
context: Omit<EditorSnapshot['context'], 'selection'> & {
|
|
53
|
-
selection?: NonNullable<EditorSelection>
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
53
|
/**
|
|
58
54
|
* @alpha
|
|
59
55
|
*/
|
package/lib/selectors/index.d.ts
CHANGED
|
@@ -20,11 +20,16 @@ import {
|
|
|
20
20
|
*/
|
|
21
21
|
export declare type EditorContext = {
|
|
22
22
|
keyGenerator: () => string
|
|
23
|
-
schema:
|
|
24
|
-
selection:
|
|
23
|
+
schema: EditorSchema
|
|
24
|
+
selection: EditorSelection
|
|
25
25
|
value: Array<PortableTextBlock>
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
/**
|
|
29
|
+
* @alpha
|
|
30
|
+
*/
|
|
31
|
+
export declare type EditorSchema = PortableTextMemberSchemaTypes
|
|
32
|
+
|
|
28
33
|
/** @beta */
|
|
29
34
|
export declare type EditorSelection = {
|
|
30
35
|
anchor: EditorSelectionPoint
|
|
@@ -42,18 +47,9 @@ export declare type EditorSelectionPoint = {
|
|
|
42
47
|
* @alpha
|
|
43
48
|
*/
|
|
44
49
|
export declare type EditorSelector<TSelected> = (
|
|
45
|
-
snapshot:
|
|
50
|
+
snapshot: EditorSnapshot,
|
|
46
51
|
) => TSelected
|
|
47
52
|
|
|
48
|
-
/**
|
|
49
|
-
* @alpha
|
|
50
|
-
*/
|
|
51
|
-
export declare type EditorSelectorSnapshot = {
|
|
52
|
-
context: Omit<EditorSnapshot['context'], 'selection'> & {
|
|
53
|
-
selection?: NonNullable<EditorSelection>
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
53
|
/**
|
|
58
54
|
* @alpha
|
|
59
55
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@portabletext/editor",
|
|
3
|
-
"version": "1.15.
|
|
3
|
+
"version": "1.15.3",
|
|
4
4
|
"description": "Portable Text Editor made in React",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sanity",
|
|
@@ -72,7 +72,7 @@
|
|
|
72
72
|
"@portabletext/toolkit": "^2.0.16",
|
|
73
73
|
"@sanity/block-tools": "^3.66.1",
|
|
74
74
|
"@sanity/diff-match-patch": "^3.1.1",
|
|
75
|
-
"@sanity/pkg-utils": "^6.11.
|
|
75
|
+
"@sanity/pkg-utils": "^6.11.15",
|
|
76
76
|
"@sanity/schema": "^3.66.1",
|
|
77
77
|
"@sanity/types": "^3.66.1",
|
|
78
78
|
"@testing-library/jest-dom": "^6.6.3",
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import {Editor, Transforms, type Descendant} from 'slate'
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
PortableTextSlateEditor,
|
|
5
|
-
} from '../types/editor'
|
|
2
|
+
import type {EditorSchema} from '../editor/define-schema'
|
|
3
|
+
import type {PortableTextSlateEditor} from '../types/editor'
|
|
6
4
|
import {isEqualToEmptyEditor} from '../utils/values'
|
|
7
5
|
|
|
8
6
|
export function insertBlock({
|
|
@@ -14,7 +12,7 @@ export function insertBlock({
|
|
|
14
12
|
block: Descendant
|
|
15
13
|
placement: 'auto' | 'after' | 'before'
|
|
16
14
|
editor: PortableTextSlateEditor
|
|
17
|
-
schema:
|
|
15
|
+
schema: EditorSchema
|
|
18
16
|
}) {
|
|
19
17
|
if (!editor.selection) {
|
|
20
18
|
const lastBlock = Array.from(
|
|
@@ -233,7 +233,7 @@ const behaviorActionImplementations: BehaviorActionImplementations = {
|
|
|
233
233
|
Transforms.deselect(action.editor)
|
|
234
234
|
}
|
|
235
235
|
},
|
|
236
|
-
'select
|
|
236
|
+
'select.previous block': ({action}) => {
|
|
237
237
|
if (!action.editor.selection) {
|
|
238
238
|
console.error('Unable to select previous block without a selection')
|
|
239
239
|
return
|
|
@@ -250,7 +250,7 @@ const behaviorActionImplementations: BehaviorActionImplementations = {
|
|
|
250
250
|
|
|
251
251
|
Transforms.select(action.editor, previousBlockPath)
|
|
252
252
|
},
|
|
253
|
-
'select
|
|
253
|
+
'select.next block': ({action}) => {
|
|
254
254
|
if (!action.editor.selection) {
|
|
255
255
|
console.error('Unable to select next block without a selection')
|
|
256
256
|
return
|
|
@@ -370,15 +370,15 @@ export function performAction({
|
|
|
370
370
|
})
|
|
371
371
|
break
|
|
372
372
|
}
|
|
373
|
-
case 'select
|
|
374
|
-
behaviorActionImplementations['select
|
|
373
|
+
case 'select.previous block': {
|
|
374
|
+
behaviorActionImplementations['select.previous block']({
|
|
375
375
|
context,
|
|
376
376
|
action,
|
|
377
377
|
})
|
|
378
378
|
break
|
|
379
379
|
}
|
|
380
|
-
case 'select
|
|
381
|
-
behaviorActionImplementations['select
|
|
380
|
+
case 'select.next block': {
|
|
381
|
+
behaviorActionImplementations['select.next block']({
|
|
382
382
|
context,
|
|
383
383
|
action,
|
|
384
384
|
})
|
|
@@ -4,18 +4,14 @@ import {
|
|
|
4
4
|
type PortableTextListBlock,
|
|
5
5
|
type PortableTextTextBlock,
|
|
6
6
|
} from '@sanity/types'
|
|
7
|
-
import type {
|
|
7
|
+
import type {EditorSchema} from '../editor/define-schema'
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* @alpha
|
|
11
11
|
*/
|
|
12
12
|
export type BehaviorGuards = ReturnType<typeof createGuards>
|
|
13
13
|
|
|
14
|
-
export function createGuards({
|
|
15
|
-
schema,
|
|
16
|
-
}: {
|
|
17
|
-
schema: PortableTextMemberSchemaTypes
|
|
18
|
-
}) {
|
|
14
|
+
export function createGuards({schema}: {schema: EditorSchema}) {
|
|
19
15
|
function isListBlock(block: unknown): block is PortableTextListBlock {
|
|
20
16
|
return isPortableTextListBlock(block) && block._type === schema.block.name
|
|
21
17
|
}
|
|
@@ -25,7 +25,7 @@ const clearListOnBackspace = defineBehavior({
|
|
|
25
25
|
|
|
26
26
|
const atTheBeginningOfBLock =
|
|
27
27
|
focusTextBlock.node.children[0]._key === focusSpan.node._key &&
|
|
28
|
-
context.selection
|
|
28
|
+
context.selection?.focus.offset === 0
|
|
29
29
|
|
|
30
30
|
if (atTheBeginningOfBLock && focusTextBlock.node.level === 1) {
|
|
31
31
|
return {focusTextBlock}
|
|
@@ -57,7 +57,7 @@ const unindentListOnBackspace = defineBehavior({
|
|
|
57
57
|
|
|
58
58
|
const atTheBeginningOfBLock =
|
|
59
59
|
focusTextBlock.node.children[0]._key === focusSpan.node._key &&
|
|
60
|
-
context.selection
|
|
60
|
+
context.selection?.focus.offset === 0
|
|
61
61
|
|
|
62
62
|
if (
|
|
63
63
|
atTheBeginningOfBLock &&
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import type {EditorSchema} from '../editor/define-schema'
|
|
1
2
|
import {getFocusSpan, selectionIsCollapsed} from '../selectors/selectors'
|
|
2
|
-
import type {PortableTextMemberSchemaTypes} from '../types/editor'
|
|
3
3
|
import {defineBehavior} from './behavior.types'
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -7,7 +7,7 @@ import {defineBehavior} from './behavior.types'
|
|
|
7
7
|
*/
|
|
8
8
|
export type LinkBehaviorsConfig = {
|
|
9
9
|
linkAnnotation?: (context: {
|
|
10
|
-
schema:
|
|
10
|
+
schema: EditorSchema
|
|
11
11
|
url: string
|
|
12
12
|
}) => {name: string; value: {[prop: string]: unknown}} | undefined
|
|
13
13
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {isPortableTextTextBlock} from '@sanity/types'
|
|
2
|
+
import type {EditorSchema} from '../editor/define-schema'
|
|
2
3
|
import {getTextBlockText} from '../editor/utils/utils'
|
|
3
4
|
import {spanSelectionPointToBlockOffset} from '../editor/utils/utils.block-offset'
|
|
4
5
|
import {getBlockTextBefore} from '../selectors/selector.get-text-before'
|
|
@@ -8,7 +9,6 @@ import {
|
|
|
8
9
|
getFocusTextBlock,
|
|
9
10
|
selectionIsCollapsed,
|
|
10
11
|
} from '../selectors/selectors'
|
|
11
|
-
import type {PortableTextMemberSchemaTypes} from '../types/editor'
|
|
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
|
|
|
@@ -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
|
|
|
@@ -327,7 +319,7 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
|
|
|
327
319
|
|
|
328
320
|
const atTheBeginningOfBLock =
|
|
329
321
|
focusTextBlock.node.children[0]._key === focusSpan.node._key &&
|
|
330
|
-
context.selection
|
|
322
|
+
context.selection?.focus.offset === 0
|
|
331
323
|
|
|
332
324
|
const defaultStyle = config.defaultStyle?.(context)
|
|
333
325
|
|
|
@@ -376,7 +368,7 @@ export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
|
|
|
376
368
|
'children',
|
|
377
369
|
{_key: focusSpan.node._key},
|
|
378
370
|
],
|
|
379
|
-
offset: context.selection
|
|
371
|
+
offset: context.selection?.focus.offset ?? 0,
|
|
380
372
|
},
|
|
381
373
|
})
|
|
382
374
|
|
|
@@ -71,10 +71,10 @@ export type SyntheticBehaviorEvent =
|
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
73
|
| {
|
|
74
|
-
type: 'insert.
|
|
74
|
+
type: 'insert.break'
|
|
75
75
|
}
|
|
76
76
|
| {
|
|
77
|
-
type: 'insert.break'
|
|
77
|
+
type: 'insert.soft break'
|
|
78
78
|
}
|
|
79
79
|
| {
|
|
80
80
|
type: 'insert.text'
|
|
@@ -176,17 +176,17 @@ export type BehaviorActionIntend =
|
|
|
176
176
|
effect: () => void
|
|
177
177
|
}
|
|
178
178
|
| {
|
|
179
|
-
type: '
|
|
180
|
-
selection: EditorSelection
|
|
179
|
+
type: 'reselect'
|
|
181
180
|
}
|
|
182
181
|
| {
|
|
183
|
-
type: 'select
|
|
182
|
+
type: 'select'
|
|
183
|
+
selection: EditorSelection
|
|
184
184
|
}
|
|
185
185
|
| {
|
|
186
|
-
type: 'select
|
|
186
|
+
type: 'select.previous block'
|
|
187
187
|
}
|
|
188
188
|
| {
|
|
189
|
-
type: '
|
|
189
|
+
type: 'select.next block'
|
|
190
190
|
}
|
|
191
191
|
| {
|
|
192
192
|
type: 'style.add'
|
package/src/behaviors/index.ts
CHANGED
|
@@ -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
|
}
|
|
@@ -14,8 +14,8 @@ import {
|
|
|
14
14
|
import type {Behavior} from '../behaviors/behavior.types'
|
|
15
15
|
import type {PickFromUnion} from '../type-utils'
|
|
16
16
|
import type {EditableAPI} from '../types/editor'
|
|
17
|
-
import {getPortableTextMemberSchemaTypes} from '../utils/getPortableTextMemberSchemaTypes'
|
|
18
17
|
import {compileType} from '../utils/schema'
|
|
18
|
+
import {createEditorSchema} from './create-editor-schema'
|
|
19
19
|
import {createSlateEditor, type SlateEditor} from './create-slate-editor'
|
|
20
20
|
import {compileSchemaDefinition, type SchemaDefinition} from './define-schema'
|
|
21
21
|
import {
|
|
@@ -107,7 +107,7 @@ function editorConfigToMachineInput(config: EditorConfig) {
|
|
|
107
107
|
readOnly: config.readOnly,
|
|
108
108
|
schema: config.schemaDefinition
|
|
109
109
|
? compileSchemaDefinition(config.schemaDefinition)
|
|
110
|
-
:
|
|
110
|
+
: createEditorSchema(
|
|
111
111
|
config.schema.hasOwnProperty('jsonType')
|
|
112
112
|
? config.schema
|
|
113
113
|
: compileType(config.schema),
|
|
@@ -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
|
}
|
|
@@ -23,13 +23,13 @@ import type {OmitFromUnion, PickFromUnion} from '../type-utils'
|
|
|
23
23
|
import type {
|
|
24
24
|
EditorSelection,
|
|
25
25
|
InvalidValueResolution,
|
|
26
|
-
PortableTextMemberSchemaTypes,
|
|
27
26
|
PortableTextSlateEditor,
|
|
28
27
|
} from '../types/editor'
|
|
29
28
|
import debug from '../utils/debug'
|
|
30
29
|
import {toPortableTextRange} from '../utils/ranges'
|
|
31
30
|
import {fromSlateValue} from '../utils/values'
|
|
32
31
|
import {KEY_TO_VALUE_ELEMENT} from '../utils/weakMaps'
|
|
32
|
+
import type {EditorSchema} from './define-schema'
|
|
33
33
|
import type {EditorContext} from './editor-snapshot'
|
|
34
34
|
|
|
35
35
|
export * from 'xstate/guards'
|
|
@@ -81,7 +81,7 @@ export type InternalEditorEvent =
|
|
|
81
81
|
}
|
|
82
82
|
| {
|
|
83
83
|
type: 'update schema'
|
|
84
|
-
schema:
|
|
84
|
+
schema: EditorSchema
|
|
85
85
|
}
|
|
86
86
|
| {
|
|
87
87
|
type: 'update behaviors'
|
|
@@ -179,10 +179,10 @@ export const editorMachine = setup({
|
|
|
179
179
|
behaviors: Array<Behavior>
|
|
180
180
|
keyGenerator: () => string
|
|
181
181
|
pendingEvents: Array<PatchEvent | MutationEvent>
|
|
182
|
-
schema:
|
|
182
|
+
schema: EditorSchema
|
|
183
183
|
readOnly: boolean
|
|
184
184
|
maxBlocks: number | undefined
|
|
185
|
-
selection:
|
|
185
|
+
selection: EditorSelection
|
|
186
186
|
value: Array<PortableTextBlock> | undefined
|
|
187
187
|
},
|
|
188
188
|
events: {} as InternalEditorEvent,
|
|
@@ -192,7 +192,7 @@ export const editorMachine = setup({
|
|
|
192
192
|
keyGenerator: () => string
|
|
193
193
|
maxBlocks?: number
|
|
194
194
|
readOnly?: boolean
|
|
195
|
-
schema:
|
|
195
|
+
schema: EditorSchema
|
|
196
196
|
value?: Array<PortableTextBlock>
|
|
197
197
|
},
|
|
198
198
|
},
|
|
@@ -275,23 +275,6 @@ export const editorMachine = setup({
|
|
|
275
275
|
context.schema,
|
|
276
276
|
)
|
|
277
277
|
|
|
278
|
-
if (!selection) {
|
|
279
|
-
console.warn(
|
|
280
|
-
`Unable to handle event ${event.type} due to missing selection`,
|
|
281
|
-
)
|
|
282
|
-
|
|
283
|
-
if (!defaultAction) {
|
|
284
|
-
return
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
enqueue.raise({
|
|
288
|
-
type: 'behavior action intends',
|
|
289
|
-
editor: event.editor,
|
|
290
|
-
actionIntends: [defaultAction],
|
|
291
|
-
})
|
|
292
|
-
return
|
|
293
|
-
}
|
|
294
|
-
|
|
295
278
|
const editorContext = {
|
|
296
279
|
keyGenerator: context.keyGenerator,
|
|
297
280
|
schema: context.schema,
|
|
@@ -361,7 +344,7 @@ export const editorMachine = setup({
|
|
|
361
344
|
keyGenerator: input.keyGenerator,
|
|
362
345
|
pendingEvents: [],
|
|
363
346
|
schema: input.schema,
|
|
364
|
-
selection:
|
|
347
|
+
selection: null,
|
|
365
348
|
readOnly: input.readOnly ?? false,
|
|
366
349
|
maxBlocks: input.maxBlocks,
|
|
367
350
|
value: input.value,
|
|
@@ -410,7 +393,7 @@ export const editorMachine = setup({
|
|
|
410
393
|
'error': {actions: emit(({event}) => event)},
|
|
411
394
|
'selection': {
|
|
412
395
|
actions: [
|
|
413
|
-
assign({selection: ({event}) => event.selection
|
|
396
|
+
assign({selection: ({event}) => event.selection}),
|
|
414
397
|
emit(({event}) => event),
|
|
415
398
|
],
|
|
416
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
|
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
import type {PortableTextBlock} from '@sanity/types'
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
PortableTextMemberSchemaTypes,
|
|
5
|
-
} from '../types/editor'
|
|
2
|
+
import type {EditorSelection} from '../types/editor'
|
|
3
|
+
import type {EditorSchema} from './define-schema'
|
|
6
4
|
|
|
7
5
|
/**
|
|
8
6
|
* @alpha
|
|
9
7
|
*/
|
|
10
8
|
export type EditorContext = {
|
|
11
9
|
keyGenerator: () => string
|
|
12
|
-
schema:
|
|
13
|
-
selection:
|
|
10
|
+
schema: EditorSchema
|
|
11
|
+
selection: EditorSelection
|
|
14
12
|
value: Array<PortableTextBlock>
|
|
15
13
|
}
|
|
16
14
|
|
package/src/index.ts
CHANGED
|
@@ -17,6 +17,8 @@ export {
|
|
|
17
17
|
type BaseDefinition,
|
|
18
18
|
type SchemaDefinition,
|
|
19
19
|
} from './editor/define-schema'
|
|
20
|
+
export type {PortableTextMemberSchemaTypes} from './types/editor'
|
|
21
|
+
export type {EditorSchema} from './editor/define-schema'
|
|
20
22
|
export {PortableTextEditable} from './editor/Editable'
|
|
21
23
|
export type {PortableTextEditableProps} from './editor/Editable'
|
|
22
24
|
export {EditorEventListener} from './editor/editor-event-listener'
|
|
@@ -35,11 +37,7 @@ export {
|
|
|
35
37
|
useEditor,
|
|
36
38
|
type EditorProviderProps,
|
|
37
39
|
} from './editor/editor-provider'
|
|
38
|
-
export {
|
|
39
|
-
useEditorSelector,
|
|
40
|
-
type EditorSelector,
|
|
41
|
-
type EditorSelectorSnapshot,
|
|
42
|
-
} from './editor/editor-selector'
|
|
40
|
+
export {useEditorSelector, type EditorSelector} from './editor/editor-selector'
|
|
43
41
|
export type {EditorContext, EditorSnapshot} from './editor/editor-snapshot'
|
|
44
42
|
export {usePortableTextEditor} from './editor/hooks/usePortableTextEditor'
|
|
45
43
|
export {usePortableTextEditorSelection} from './editor/hooks/usePortableTextEditorSelection'
|
package/src/selectors/index.ts
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
export type {
|
|
2
|
-
EditorSelector,
|
|
3
|
-
EditorSelectorSnapshot,
|
|
4
|
-
} from '../editor/editor-selector'
|
|
1
|
+
export type {EditorSelector} from '../editor/editor-selector'
|
|
5
2
|
export type {EditorContext, EditorSnapshot} from '../editor/editor-snapshot'
|
|
6
3
|
export type {
|
|
7
4
|
EditorSelection,
|
|
@@ -13,3 +10,4 @@ export {getActiveListItem} from './selector.get-active-list-item'
|
|
|
13
10
|
export {getSelectionText} from './selector.get-selection-text'
|
|
14
11
|
export {getBlockTextBefore} from './selector.get-text-before'
|
|
15
12
|
export * from './selectors'
|
|
13
|
+
export type {EditorSchema} from '../editor/define-schema'
|
|
@@ -4,13 +4,13 @@ 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'
|
|
7
8
|
import {editorMachine} from '../../editor/editor-machine'
|
|
8
9
|
import {defaultKeyGenerator} from '../../editor/key-generator'
|
|
9
10
|
import {withPlugins} from '../../editor/plugins/with-plugins'
|
|
10
|
-
import {getPortableTextMemberSchemaTypes} from '../getPortableTextMemberSchemaTypes'
|
|
11
11
|
import {createOperationToPatches} from '../operationToPatches'
|
|
12
12
|
|
|
13
|
-
const schemaTypes =
|
|
13
|
+
const schemaTypes = createEditorSchema(schemaType)
|
|
14
14
|
|
|
15
15
|
const operationToPatches = createOperationToPatches(schemaTypes)
|
|
16
16
|
|
|
@@ -4,14 +4,14 @@ 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'
|
|
7
8
|
import {editorMachine} from '../../editor/editor-machine'
|
|
8
9
|
import {defaultKeyGenerator} from '../../editor/key-generator'
|
|
9
10
|
import {withPlugins} from '../../editor/plugins/with-plugins'
|
|
10
11
|
import {createApplyPatch} from '../applyPatch'
|
|
11
|
-
import {getPortableTextMemberSchemaTypes} from '../getPortableTextMemberSchemaTypes'
|
|
12
12
|
import {VOID_CHILD_KEY} from '../values'
|
|
13
13
|
|
|
14
|
-
const schemaTypes =
|
|
14
|
+
const schemaTypes = createEditorSchema(schemaType)
|
|
15
15
|
|
|
16
16
|
const patchToOperations = createApplyPatch(schemaTypes)
|
|
17
17
|
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {describe, expect, it} from 'vitest'
|
|
2
2
|
import {schemaType} from '../../editor/__tests__/PortableTextEditorTester'
|
|
3
|
-
import {
|
|
3
|
+
import {createEditorSchema} from '../../editor/create-editor-schema'
|
|
4
4
|
import {fromSlateValue, toSlateValue} from '../values'
|
|
5
5
|
|
|
6
|
-
const schemaTypes =
|
|
6
|
+
const schemaTypes = createEditorSchema(schemaType)
|
|
7
7
|
|
|
8
8
|
describe('toSlateValue', () => {
|
|
9
9
|
it('checks undefined', () => {
|