@portabletext/editor 1.17.1 → 1.18.0
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/README.md +228 -261
- package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
- package/lib/_chunks-cjs/selector.get-text-before.cjs.map +1 -1
- package/lib/_chunks-cjs/selector.is-selection-collapsed.cjs.map +1 -1
- package/lib/_chunks-es/behavior.core.js.map +1 -1
- package/lib/_chunks-es/selector.get-text-before.js.map +1 -1
- package/lib/_chunks-es/selector.is-selection-collapsed.js.map +1 -1
- package/lib/behaviors/index.cjs +4 -1
- package/lib/behaviors/index.cjs.map +1 -1
- package/lib/behaviors/index.d.cts +23 -23
- package/lib/behaviors/index.d.ts +23 -23
- package/lib/behaviors/index.js +4 -1
- package/lib/behaviors/index.js.map +1 -1
- package/lib/index.cjs +78 -69
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +123 -753
- package/lib/index.d.ts +123 -753
- package/lib/index.js +78 -69
- package/lib/index.js.map +1 -1
- package/lib/selectors/index.cjs.map +1 -1
- package/lib/selectors/index.d.cts +30 -30
- package/lib/selectors/index.d.ts +30 -30
- package/lib/selectors/index.js.map +1 -1
- package/package.json +8 -8
- package/src/behaviors/behavior.code-editor.ts +2 -2
- package/src/behaviors/behavior.core.ts +2 -2
- package/src/behaviors/behavior.emoji-picker.ts +8 -3
- package/src/behaviors/behavior.links.ts +2 -2
- package/src/behaviors/behavior.markdown.ts +2 -2
- package/src/behaviors/behavior.types.ts +10 -10
- package/src/editor/PortableTextEditor.tsx +2 -0
- package/src/editor/__tests__/self-solving.test.tsx +14 -0
- package/src/editor/components/Synchronizer.tsx +6 -1
- package/src/editor/create-editor.ts +14 -3
- package/src/editor/define-schema.ts +4 -4
- package/src/editor/editor-event-listener.tsx +1 -1
- package/src/editor/editor-machine.ts +42 -52
- package/src/editor/editor-provider.tsx +3 -3
- package/src/editor/editor-selector.ts +31 -14
- package/src/editor/editor-snapshot.ts +2 -2
- package/src/editor/get-value.ts +12 -5
- package/src/editor/hooks/usePortableTextEditor.ts +1 -0
- package/src/editor/hooks/usePortableTextEditorSelection.tsx +1 -0
- package/src/selectors/selector.get-active-list-item.ts +1 -1
- package/src/selectors/selector.get-active-style.ts +1 -1
- package/src/selectors/selector.get-selected-spans.ts +1 -1
- package/src/selectors/selector.get-selection-text.ts +1 -1
- package/src/selectors/selector.get-text-before.ts +1 -1
- package/src/selectors/selector.is-active-annotation.ts +1 -1
- package/src/selectors/selector.is-active-decorator.ts +1 -1
- package/src/selectors/selector.is-active-list-item.ts +1 -1
- package/src/selectors/selector.is-active-style.ts +1 -1
- package/src/selectors/selector.is-selection-collapsed.ts +1 -1
- package/src/selectors/selector.is-selection-expanded.ts +1 -1
- package/src/selectors/selectors.ts +13 -13
- package/src/types/editor.ts +2 -2
|
@@ -9,7 +9,7 @@ const softReturn = defineBehavior({
|
|
|
9
9
|
})
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
* @
|
|
12
|
+
* @beta
|
|
13
13
|
*/
|
|
14
14
|
export const coreBehaviors = [
|
|
15
15
|
softReturn,
|
|
@@ -29,7 +29,7 @@ export const coreBehaviors = [
|
|
|
29
29
|
]
|
|
30
30
|
|
|
31
31
|
/**
|
|
32
|
-
* @
|
|
32
|
+
* @beta
|
|
33
33
|
*/
|
|
34
34
|
export const coreBehavior = {
|
|
35
35
|
softReturn,
|
|
@@ -8,7 +8,7 @@ const incompleteEmojiRegEx = /:([a-zA-Z-_0-9]+)$/
|
|
|
8
8
|
const emojiRegEx = /:([a-zA-Z-_0-9]+):$/
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
-
* @
|
|
11
|
+
* @beta
|
|
12
12
|
*/
|
|
13
13
|
export type EmojiPickerBehaviorsConfig<TEmojiMatch> = {
|
|
14
14
|
/**
|
|
@@ -24,7 +24,7 @@ export type EmojiPickerBehaviorsConfig<TEmojiMatch> = {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
|
-
* @
|
|
27
|
+
* @beta
|
|
28
28
|
*/
|
|
29
29
|
export function createEmojiPickerBehaviors<TEmojiMatch>(
|
|
30
30
|
config: EmojiPickerBehaviorsConfig<TEmojiMatch>,
|
|
@@ -146,6 +146,12 @@ export function createEmojiPickerBehaviors<TEmojiMatch>(
|
|
|
146
146
|
defineBehavior({
|
|
147
147
|
on: 'key.down',
|
|
148
148
|
guard: ({context, event}) => {
|
|
149
|
+
const matches = emojiPickerActor.getSnapshot().context.matches
|
|
150
|
+
|
|
151
|
+
if (matches.length === 0) {
|
|
152
|
+
return false
|
|
153
|
+
}
|
|
154
|
+
|
|
149
155
|
const isShift = isHotkey('Shift', event.keyboardEvent)
|
|
150
156
|
const isColon = event.keyboardEvent.key === ':'
|
|
151
157
|
const isEmojiChar = emojiCharRegEx.test(event.keyboardEvent.key)
|
|
@@ -158,7 +164,6 @@ export function createEmojiPickerBehaviors<TEmojiMatch>(
|
|
|
158
164
|
const isArrowUp = isHotkey('ArrowUp', event.keyboardEvent)
|
|
159
165
|
const isEnter = isHotkey('Enter', event.keyboardEvent)
|
|
160
166
|
const isTab = isHotkey('Tab', event.keyboardEvent)
|
|
161
|
-
const matches = emojiPickerActor.getSnapshot().context.matches
|
|
162
167
|
|
|
163
168
|
if (isEnter || isTab) {
|
|
164
169
|
const selectedIndex =
|
|
@@ -3,7 +3,7 @@ import * as selectors from '../selectors'
|
|
|
3
3
|
import {defineBehavior} from './behavior.types'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* @
|
|
6
|
+
* @beta
|
|
7
7
|
*/
|
|
8
8
|
export type LinkBehaviorsConfig = {
|
|
9
9
|
linkAnnotation?: (context: {
|
|
@@ -13,7 +13,7 @@ export type LinkBehaviorsConfig = {
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
|
-
* @
|
|
16
|
+
* @beta
|
|
17
17
|
*/
|
|
18
18
|
export function createLinkBehaviors(config: LinkBehaviorsConfig) {
|
|
19
19
|
const pasteLinkOnSelection = defineBehavior({
|
|
@@ -7,7 +7,7 @@ import {getBlockTextBefore} from '../selectors/selector.get-text-before'
|
|
|
7
7
|
import {defineBehavior} from './behavior.types'
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
|
-
* @
|
|
10
|
+
* @beta
|
|
11
11
|
*/
|
|
12
12
|
export type MarkdownBehaviorsConfig = {
|
|
13
13
|
horizontalRuleObject?: (context: {
|
|
@@ -24,7 +24,7 @@ export type MarkdownBehaviorsConfig = {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
/**
|
|
27
|
-
* @
|
|
27
|
+
* @beta
|
|
28
28
|
*/
|
|
29
29
|
export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
|
|
30
30
|
const automaticBlockquoteOnSpace = defineBehavior({
|
|
@@ -6,7 +6,7 @@ import type {PickFromUnion} from '../type-utils'
|
|
|
6
6
|
import type {EditorSelection, PortableTextSlateEditor} from '../types/editor'
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
|
-
* @
|
|
9
|
+
* @beta
|
|
10
10
|
*/
|
|
11
11
|
export type SyntheticBehaviorEvent =
|
|
12
12
|
| {
|
|
@@ -91,7 +91,7 @@ export type SyntheticBehaviorEvent =
|
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
/**
|
|
94
|
-
* @
|
|
94
|
+
* @beta
|
|
95
95
|
*/
|
|
96
96
|
export type NativeBehaviorEvent =
|
|
97
97
|
| {
|
|
@@ -118,7 +118,7 @@ export type NativeBehaviorEvent =
|
|
|
118
118
|
}
|
|
119
119
|
|
|
120
120
|
/**
|
|
121
|
-
* @
|
|
121
|
+
* @beta
|
|
122
122
|
*/
|
|
123
123
|
export type BehaviorActionIntend =
|
|
124
124
|
| SyntheticBehaviorEvent
|
|
@@ -210,19 +210,19 @@ export type BehaviorActionIntend =
|
|
|
210
210
|
}
|
|
211
211
|
|
|
212
212
|
/**
|
|
213
|
-
* @
|
|
213
|
+
* @beta
|
|
214
214
|
*/
|
|
215
215
|
export type BehaviorAction = BehaviorActionIntend & {
|
|
216
216
|
editor: PortableTextSlateEditor
|
|
217
217
|
}
|
|
218
218
|
|
|
219
219
|
/**
|
|
220
|
-
* @
|
|
220
|
+
* @beta
|
|
221
221
|
*/
|
|
222
222
|
export type BehaviorEvent = SyntheticBehaviorEvent | NativeBehaviorEvent
|
|
223
223
|
|
|
224
224
|
/**
|
|
225
|
-
* @
|
|
225
|
+
* @beta
|
|
226
226
|
*/
|
|
227
227
|
export type Behavior<
|
|
228
228
|
TBehaviorEventType extends BehaviorEvent['type'] = BehaviorEvent['type'],
|
|
@@ -248,7 +248,7 @@ export type Behavior<
|
|
|
248
248
|
}
|
|
249
249
|
|
|
250
250
|
/**
|
|
251
|
-
* @
|
|
251
|
+
* @beta
|
|
252
252
|
*/
|
|
253
253
|
export type BehaviorGuard<
|
|
254
254
|
TBehaviorEvent extends BehaviorEvent,
|
|
@@ -262,7 +262,7 @@ export type BehaviorGuard<
|
|
|
262
262
|
}) => TGuardResponse | false
|
|
263
263
|
|
|
264
264
|
/**
|
|
265
|
-
* @
|
|
265
|
+
* @beta
|
|
266
266
|
*/
|
|
267
267
|
export type BehaviorActionIntendSet<
|
|
268
268
|
TBehaviorEventType extends BehaviorEvent['type'] = BehaviorEvent['type'],
|
|
@@ -279,7 +279,7 @@ export type BehaviorActionIntendSet<
|
|
|
279
279
|
) => Array<BehaviorActionIntend>
|
|
280
280
|
|
|
281
281
|
/**
|
|
282
|
-
* @
|
|
282
|
+
* @beta
|
|
283
283
|
*/
|
|
284
284
|
export function defineBehavior<
|
|
285
285
|
TAnyBehaviorEventType extends BehaviorEvent['type'],
|
|
@@ -289,7 +289,7 @@ export function defineBehavior<
|
|
|
289
289
|
}
|
|
290
290
|
|
|
291
291
|
/**
|
|
292
|
-
* @
|
|
292
|
+
* @beta
|
|
293
293
|
*/
|
|
294
294
|
export type BlockOffset = {
|
|
295
295
|
path: [KeyedSegment]
|
|
@@ -42,6 +42,7 @@ const debug = debugWithName('component:PortableTextEditor')
|
|
|
42
42
|
* Props for the PortableTextEditor component
|
|
43
43
|
*
|
|
44
44
|
* @public
|
|
45
|
+
* @deprecated Use `EditorProvider` instead
|
|
45
46
|
*/
|
|
46
47
|
export type PortableTextEditorProps<
|
|
47
48
|
TEditor extends Editor | undefined = undefined,
|
|
@@ -106,6 +107,7 @@ export type PortableTextEditorProps<
|
|
|
106
107
|
/**
|
|
107
108
|
* The main Portable Text Editor component.
|
|
108
109
|
* @public
|
|
110
|
+
* @deprecated Use `EditorProvider` instead
|
|
109
111
|
*/
|
|
110
112
|
export class PortableTextEditor extends Component<
|
|
111
113
|
PortableTextEditorProps<Editor | undefined>
|
|
@@ -169,6 +169,20 @@ describe('Feature: Self-solving', () => {
|
|
|
169
169
|
markDefs: [],
|
|
170
170
|
}),
|
|
171
171
|
],
|
|
172
|
+
value: [
|
|
173
|
+
block({
|
|
174
|
+
_key: 'b1',
|
|
175
|
+
children: [
|
|
176
|
+
span({
|
|
177
|
+
_key: 's1',
|
|
178
|
+
text: 'foo',
|
|
179
|
+
marks: ['strong'],
|
|
180
|
+
}),
|
|
181
|
+
],
|
|
182
|
+
style: 'normal',
|
|
183
|
+
markDefs: [],
|
|
184
|
+
}),
|
|
185
|
+
],
|
|
172
186
|
})
|
|
173
187
|
}
|
|
174
188
|
})
|
|
@@ -51,7 +51,12 @@ export function Synchronizer(props: SynchronizerProps) {
|
|
|
51
51
|
}
|
|
52
52
|
if (event.type === 'mutation') {
|
|
53
53
|
syncActorRef.send({type: 'mutation'})
|
|
54
|
-
editorActor.send(
|
|
54
|
+
editorActor.send({
|
|
55
|
+
type: 'mutation',
|
|
56
|
+
patches: event.patches,
|
|
57
|
+
snapshot: event.snapshot,
|
|
58
|
+
value: event.snapshot,
|
|
59
|
+
})
|
|
55
60
|
}
|
|
56
61
|
})
|
|
57
62
|
|
|
@@ -24,13 +24,18 @@ import {
|
|
|
24
24
|
type EditorEmittedEvent,
|
|
25
25
|
type InternalEditorEvent,
|
|
26
26
|
} from './editor-machine'
|
|
27
|
+
import {getEditorSnapshot} from './editor-selector'
|
|
28
|
+
import type {EditorSnapshot} from './editor-snapshot'
|
|
27
29
|
import {defaultKeyGenerator} from './key-generator'
|
|
28
30
|
import {createEditableAPI} from './plugins/createWithEditableAPI'
|
|
29
31
|
|
|
30
32
|
/**
|
|
31
|
-
* @
|
|
33
|
+
* @public
|
|
32
34
|
*/
|
|
33
35
|
export type EditorConfig = {
|
|
36
|
+
/**
|
|
37
|
+
* @beta
|
|
38
|
+
*/
|
|
34
39
|
behaviors?: Array<Behavior>
|
|
35
40
|
keyGenerator?: () => string
|
|
36
41
|
/**
|
|
@@ -51,7 +56,7 @@ export type EditorConfig = {
|
|
|
51
56
|
)
|
|
52
57
|
|
|
53
58
|
/**
|
|
54
|
-
* @
|
|
59
|
+
* @public
|
|
55
60
|
*/
|
|
56
61
|
export type EditorEvent = PickFromUnion<
|
|
57
62
|
InternalEditorEvent,
|
|
@@ -75,9 +80,10 @@ export type EditorEvent = PickFromUnion<
|
|
|
75
80
|
>
|
|
76
81
|
|
|
77
82
|
/**
|
|
78
|
-
* @
|
|
83
|
+
* @public
|
|
79
84
|
*/
|
|
80
85
|
export type Editor = {
|
|
86
|
+
getSnapshot: () => EditorSnapshot
|
|
81
87
|
send: (event: EditorEvent) => void
|
|
82
88
|
on: ActorRef<Snapshot<unknown>, EventObject, EditorEmittedEvent>['on']
|
|
83
89
|
_internal: {
|
|
@@ -126,6 +132,11 @@ function createEditorFromActor(editorActor: EditorActor): Editor {
|
|
|
126
132
|
const editable = createEditableAPI(slateEditor.instance, editorActor)
|
|
127
133
|
|
|
128
134
|
return {
|
|
135
|
+
getSnapshot: () =>
|
|
136
|
+
getEditorSnapshot({
|
|
137
|
+
editorActorSnapshot: editorActor.getSnapshot(),
|
|
138
|
+
slateEditorInstance: slateEditor.instance,
|
|
139
|
+
}),
|
|
129
140
|
send: (event) => {
|
|
130
141
|
editorActor.send(event)
|
|
131
142
|
},
|
|
@@ -10,7 +10,7 @@ import type {PortableTextMemberSchemaTypes} from '../types/editor'
|
|
|
10
10
|
import {createEditorSchema} from './create-editor-schema'
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
* @
|
|
13
|
+
* @public
|
|
14
14
|
*/
|
|
15
15
|
export type BaseDefinition = {
|
|
16
16
|
name: string
|
|
@@ -19,7 +19,7 @@ export type BaseDefinition = {
|
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
|
-
* @
|
|
22
|
+
* @public
|
|
23
23
|
*/
|
|
24
24
|
export type SchemaDefinition<
|
|
25
25
|
TBaseDefinition extends BaseDefinition = BaseDefinition,
|
|
@@ -33,7 +33,7 @@ export type SchemaDefinition<
|
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
/**
|
|
36
|
-
* @
|
|
36
|
+
* @public
|
|
37
37
|
*/
|
|
38
38
|
export function defineSchema<const TSchemaDefinition extends SchemaDefinition>(
|
|
39
39
|
definition: TSchemaDefinition,
|
|
@@ -42,7 +42,7 @@ export function defineSchema<const TSchemaDefinition extends SchemaDefinition>(
|
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
/**
|
|
45
|
-
* @
|
|
45
|
+
* @public
|
|
46
46
|
*/
|
|
47
47
|
export type EditorSchema = PortableTextMemberSchemaTypes
|
|
48
48
|
|
|
@@ -15,7 +15,6 @@ import {coreBehaviors} from '../behaviors/behavior.core'
|
|
|
15
15
|
import type {
|
|
16
16
|
Behavior,
|
|
17
17
|
BehaviorAction,
|
|
18
|
-
BehaviorActionIntend,
|
|
19
18
|
NativeBehaviorEvent,
|
|
20
19
|
SyntheticBehaviorEvent,
|
|
21
20
|
} from '../behaviors/behavior.types'
|
|
@@ -59,7 +58,11 @@ export type PatchesEvent = {
|
|
|
59
58
|
export type MutationEvent = {
|
|
60
59
|
type: 'mutation'
|
|
61
60
|
patches: Array<Patch>
|
|
61
|
+
/**
|
|
62
|
+
* @deprecated Use `value` instead
|
|
63
|
+
*/
|
|
62
64
|
snapshot: Array<PortableTextBlock> | undefined
|
|
65
|
+
value: Array<PortableTextBlock> | undefined
|
|
63
66
|
}
|
|
64
67
|
|
|
65
68
|
/**
|
|
@@ -75,11 +78,6 @@ export type InternalEditorEvent =
|
|
|
75
78
|
editor: PortableTextSlateEditor
|
|
76
79
|
nativeEvent?: {preventDefault: () => void}
|
|
77
80
|
}
|
|
78
|
-
| {
|
|
79
|
-
type: 'behavior action intends'
|
|
80
|
-
editor: PortableTextSlateEditor
|
|
81
|
-
actionIntends: Array<BehaviorActionIntend>
|
|
82
|
-
}
|
|
83
81
|
| {
|
|
84
82
|
type: 'update readOnly'
|
|
85
83
|
readOnly: boolean
|
|
@@ -107,7 +105,7 @@ export type InternalEditorEvent =
|
|
|
107
105
|
>
|
|
108
106
|
|
|
109
107
|
/**
|
|
110
|
-
* @
|
|
108
|
+
* @public
|
|
111
109
|
*/
|
|
112
110
|
export type EditorEmittedEvent = PickFromUnion<
|
|
113
111
|
InternalEditorEmittedEvent,
|
|
@@ -265,11 +263,13 @@ export const editorMachine = setup({
|
|
|
265
263
|
return
|
|
266
264
|
}
|
|
267
265
|
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
266
|
+
Editor.withoutNormalizing(event.editor, () => {
|
|
267
|
+
performAction({
|
|
268
|
+
context,
|
|
269
|
+
action: defaultAction,
|
|
270
|
+
})
|
|
272
271
|
})
|
|
272
|
+
event.editor.onChange()
|
|
273
273
|
return
|
|
274
274
|
}
|
|
275
275
|
|
|
@@ -324,11 +324,32 @@ export const editorMachine = setup({
|
|
|
324
324
|
(actionIntend) => actionIntend.type !== 'effect',
|
|
325
325
|
))
|
|
326
326
|
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
327
|
+
Editor.withoutNormalizing(event.editor, () => {
|
|
328
|
+
for (const actionIntend of actionIntends) {
|
|
329
|
+
const action = {
|
|
330
|
+
...actionIntend,
|
|
331
|
+
editor: event.editor,
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
performAction({context, action})
|
|
335
|
+
}
|
|
331
336
|
})
|
|
337
|
+
event.editor.onChange()
|
|
338
|
+
|
|
339
|
+
if (
|
|
340
|
+
actionIntends.some(
|
|
341
|
+
(actionIntend) => actionIntend.type === 'reselect',
|
|
342
|
+
)
|
|
343
|
+
) {
|
|
344
|
+
enqueue.raise({
|
|
345
|
+
type: 'selection',
|
|
346
|
+
selection: toPortableTextRange(
|
|
347
|
+
event.editor.children,
|
|
348
|
+
event.editor.selection,
|
|
349
|
+
context.schema,
|
|
350
|
+
),
|
|
351
|
+
})
|
|
352
|
+
}
|
|
332
353
|
}
|
|
333
354
|
|
|
334
355
|
if (behaviorOverwritten) {
|
|
@@ -342,11 +363,13 @@ export const editorMachine = setup({
|
|
|
342
363
|
return
|
|
343
364
|
}
|
|
344
365
|
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
366
|
+
Editor.withoutNormalizing(event.editor, () => {
|
|
367
|
+
performAction({
|
|
368
|
+
context,
|
|
369
|
+
action: defaultAction,
|
|
370
|
+
})
|
|
349
371
|
})
|
|
372
|
+
event.editor.onChange()
|
|
350
373
|
}
|
|
351
374
|
}),
|
|
352
375
|
},
|
|
@@ -384,39 +407,6 @@ export const editorMachine = setup({
|
|
|
384
407
|
'update maxBlocks': {
|
|
385
408
|
actions: assign({maxBlocks: ({event}) => event.maxBlocks}),
|
|
386
409
|
},
|
|
387
|
-
'behavior action intends': {
|
|
388
|
-
actions: [
|
|
389
|
-
({context, event}) => {
|
|
390
|
-
Editor.withoutNormalizing(event.editor, () => {
|
|
391
|
-
for (const actionIntend of event.actionIntends) {
|
|
392
|
-
const action = {
|
|
393
|
-
...actionIntend,
|
|
394
|
-
editor: event.editor,
|
|
395
|
-
}
|
|
396
|
-
|
|
397
|
-
performAction({context, action})
|
|
398
|
-
}
|
|
399
|
-
})
|
|
400
|
-
event.editor.onChange()
|
|
401
|
-
},
|
|
402
|
-
enqueueActions(({context, event, enqueue}) => {
|
|
403
|
-
if (
|
|
404
|
-
event.actionIntends.some(
|
|
405
|
-
(actionIntend) => actionIntend.type === 'reselect',
|
|
406
|
-
)
|
|
407
|
-
) {
|
|
408
|
-
enqueue.raise({
|
|
409
|
-
type: 'selection',
|
|
410
|
-
selection: toPortableTextRange(
|
|
411
|
-
event.editor.children,
|
|
412
|
-
event.editor.selection,
|
|
413
|
-
context.schema,
|
|
414
|
-
),
|
|
415
|
-
})
|
|
416
|
-
}
|
|
417
|
-
}),
|
|
418
|
-
],
|
|
419
|
-
},
|
|
420
410
|
},
|
|
421
411
|
type: 'parallel',
|
|
422
412
|
states: {
|
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
const EditorContext = React.createContext<Editor | undefined>(undefined)
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
|
-
* @
|
|
17
|
+
* @public
|
|
18
18
|
*/
|
|
19
19
|
export type EditorProviderProps = {
|
|
20
20
|
initialConfig: EditorConfig
|
|
@@ -22,7 +22,7 @@ export type EditorProviderProps = {
|
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
|
-
* @
|
|
25
|
+
* @public
|
|
26
26
|
*/
|
|
27
27
|
export function EditorProvider(props: EditorProviderProps) {
|
|
28
28
|
const editor = useCreateEditor(props.initialConfig)
|
|
@@ -65,7 +65,7 @@ export function EditorProvider(props: EditorProviderProps) {
|
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
/**
|
|
68
|
-
* @
|
|
68
|
+
* @public
|
|
69
69
|
*/
|
|
70
70
|
export function useEditor() {
|
|
71
71
|
const editor = React.useContext(EditorContext)
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import {useSelector} from '@xstate/react'
|
|
2
|
+
import type {PortableTextSlateEditor} from '../types/editor'
|
|
2
3
|
import type {Editor} from './create-editor'
|
|
4
|
+
import type {EditorActor} from './editor-machine'
|
|
3
5
|
import type {EditorSnapshot} from './editor-snapshot'
|
|
4
6
|
import {getActiveDecorators} from './get-active-decorators'
|
|
5
7
|
import {getValue} from './get-value'
|
|
@@ -9,12 +11,12 @@ function defaultCompare<T>(a: T, b: T) {
|
|
|
9
11
|
}
|
|
10
12
|
|
|
11
13
|
/**
|
|
12
|
-
* @
|
|
14
|
+
* @public
|
|
13
15
|
*/
|
|
14
16
|
export type EditorSelector<TSelected> = (snapshot: EditorSnapshot) => TSelected
|
|
15
17
|
|
|
16
18
|
/**
|
|
17
|
-
* @
|
|
19
|
+
* @public
|
|
18
20
|
*/
|
|
19
21
|
export function useEditorSelector<TSelected>(
|
|
20
22
|
editor: Editor,
|
|
@@ -23,20 +25,35 @@ export function useEditorSelector<TSelected>(
|
|
|
23
25
|
) {
|
|
24
26
|
return useSelector(
|
|
25
27
|
editor._internal.editorActor,
|
|
26
|
-
(
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}),
|
|
32
|
-
keyGenerator: snapshot.context.keyGenerator,
|
|
33
|
-
schema: snapshot.context.schema,
|
|
34
|
-
selection: snapshot.context.selection,
|
|
35
|
-
value: getValue(editor),
|
|
36
|
-
}
|
|
28
|
+
(editorActorSnapshot) => {
|
|
29
|
+
const snapshot = getEditorSnapshot({
|
|
30
|
+
editorActorSnapshot,
|
|
31
|
+
slateEditorInstance: editor._internal.slateEditor.instance,
|
|
32
|
+
})
|
|
37
33
|
|
|
38
|
-
return selector(
|
|
34
|
+
return selector(snapshot)
|
|
39
35
|
},
|
|
40
36
|
compare,
|
|
41
37
|
)
|
|
42
38
|
}
|
|
39
|
+
|
|
40
|
+
export function getEditorSnapshot({
|
|
41
|
+
editorActorSnapshot,
|
|
42
|
+
slateEditorInstance,
|
|
43
|
+
}: {
|
|
44
|
+
editorActorSnapshot: ReturnType<EditorActor['getSnapshot']>
|
|
45
|
+
slateEditorInstance: PortableTextSlateEditor
|
|
46
|
+
}): EditorSnapshot {
|
|
47
|
+
return {
|
|
48
|
+
context: {
|
|
49
|
+
activeDecorators: getActiveDecorators({
|
|
50
|
+
schema: editorActorSnapshot.context.schema,
|
|
51
|
+
slateEditorInstance,
|
|
52
|
+
}),
|
|
53
|
+
keyGenerator: editorActorSnapshot.context.keyGenerator,
|
|
54
|
+
schema: editorActorSnapshot.context.schema,
|
|
55
|
+
selection: editorActorSnapshot.context.selection,
|
|
56
|
+
value: getValue({editorActorSnapshot, slateEditorInstance}),
|
|
57
|
+
},
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -3,7 +3,7 @@ import type {EditorSelection} from '../types/editor'
|
|
|
3
3
|
import type {EditorSchema} from './define-schema'
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
|
-
* @
|
|
6
|
+
* @public
|
|
7
7
|
*/
|
|
8
8
|
export type EditorContext = {
|
|
9
9
|
activeDecorators: Array<string>
|
|
@@ -14,7 +14,7 @@ export type EditorContext = {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
|
-
* @
|
|
17
|
+
* @public
|
|
18
18
|
*/
|
|
19
19
|
export type EditorSnapshot = {
|
|
20
20
|
context: EditorContext
|
package/src/editor/get-value.ts
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
|
+
import type {PortableTextSlateEditor} from '../types/editor'
|
|
1
2
|
import {fromSlateValue} from '../utils/values'
|
|
2
3
|
import {KEY_TO_VALUE_ELEMENT} from '../utils/weakMaps'
|
|
3
|
-
import type {
|
|
4
|
+
import type {EditorActor} from './editor-machine'
|
|
4
5
|
|
|
5
|
-
export function getValue(
|
|
6
|
+
export function getValue({
|
|
7
|
+
editorActorSnapshot,
|
|
8
|
+
slateEditorInstance,
|
|
9
|
+
}: {
|
|
10
|
+
editorActorSnapshot: ReturnType<EditorActor['getSnapshot']>
|
|
11
|
+
slateEditorInstance: PortableTextSlateEditor
|
|
12
|
+
}) {
|
|
6
13
|
return fromSlateValue(
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
KEY_TO_VALUE_ELEMENT.get(
|
|
14
|
+
slateEditorInstance.children,
|
|
15
|
+
editorActorSnapshot.context.schema.block.name,
|
|
16
|
+
KEY_TO_VALUE_ELEMENT.get(slateEditorInstance),
|
|
10
17
|
)
|
|
11
18
|
}
|
|
@@ -4,7 +4,7 @@ import {isKeyedSegment} from '../editor/utils/utils.is-keyed-segment'
|
|
|
4
4
|
import {reverseSelection} from '../editor/utils/utils.reverse-selection'
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* @
|
|
7
|
+
* @public
|
|
8
8
|
*/
|
|
9
9
|
export const getSelectionText: EditorSelector<string> = ({context}) => {
|
|
10
10
|
let text = ''
|