@portabletext/editor 1.0.19 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.d.mts +142 -67
- package/lib/index.d.ts +142 -67
- package/lib/index.esm.js +1130 -371
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +1130 -371
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +1130 -371
- package/lib/index.mjs.map +1 -1
- package/package.json +4 -18
- package/src/editor/Editable.tsx +128 -55
- package/src/editor/PortableTextEditor.tsx +66 -32
- package/src/editor/__tests__/PortableTextEditor.test.tsx +44 -18
- package/src/editor/__tests__/PortableTextEditorTester.tsx +50 -38
- package/src/editor/__tests__/RangeDecorations.test.tsx +4 -6
- package/src/editor/__tests__/handleClick.test.tsx +28 -9
- package/src/editor/__tests__/insert-block.test.tsx +24 -8
- package/src/editor/__tests__/pteWarningsSelfSolving.test.tsx +31 -63
- package/src/editor/__tests__/utils.ts +10 -4
- package/src/editor/components/DraggableBlock.tsx +36 -13
- package/src/editor/components/Element.tsx +73 -33
- package/src/editor/components/Leaf.tsx +114 -76
- package/src/editor/components/SlateContainer.tsx +14 -7
- package/src/editor/components/Synchronizer.tsx +8 -5
- package/src/editor/hooks/usePortableTextEditor.ts +3 -3
- package/src/editor/hooks/usePortableTextEditorSelection.tsx +10 -4
- package/src/editor/hooks/useSyncValue.test.tsx +9 -4
- package/src/editor/hooks/useSyncValue.ts +198 -133
- package/src/editor/nodes/DefaultAnnotation.tsx +6 -4
- package/src/editor/nodes/DefaultObject.tsx +1 -1
- package/src/editor/plugins/__tests__/createWithInsertData.test.tsx +23 -8
- package/src/editor/plugins/__tests__/withEditableAPIDelete.test.tsx +26 -9
- package/src/editor/plugins/__tests__/withEditableAPIGetFragment.test.tsx +15 -5
- package/src/editor/plugins/__tests__/withEditableAPIInsert.test.tsx +60 -19
- package/src/editor/plugins/__tests__/withEditableAPISelectionsOverlapping.test.tsx +5 -3
- package/src/editor/plugins/__tests__/withPortableTextLists.test.tsx +4 -2
- package/src/editor/plugins/__tests__/withPortableTextMarkModel.test.tsx +61 -19
- package/src/editor/plugins/__tests__/withPortableTextSelections.test.tsx +6 -3
- package/src/editor/plugins/__tests__/withUndoRedo.test.tsx +30 -13
- package/src/editor/plugins/createWithEditableAPI.ts +361 -131
- package/src/editor/plugins/createWithHotKeys.ts +46 -130
- package/src/editor/plugins/createWithInsertBreak.ts +167 -28
- package/src/editor/plugins/createWithInsertData.ts +66 -30
- package/src/editor/plugins/createWithMaxBlocks.ts +6 -3
- package/src/editor/plugins/createWithObjectKeys.ts +7 -3
- package/src/editor/plugins/createWithPatches.ts +66 -24
- package/src/editor/plugins/createWithPlaceholderBlock.ts +9 -5
- package/src/editor/plugins/createWithPortableTextBlockStyle.ts +17 -7
- package/src/editor/plugins/createWithPortableTextLists.ts +21 -9
- package/src/editor/plugins/createWithPortableTextMarkModel.ts +217 -52
- package/src/editor/plugins/createWithPortableTextSelections.ts +11 -9
- package/src/editor/plugins/createWithSchemaTypes.ts +26 -10
- package/src/editor/plugins/createWithUndoRedo.ts +106 -27
- package/src/editor/plugins/createWithUtils.ts +33 -11
- package/src/editor/plugins/index.ts +34 -13
- package/src/types/editor.ts +73 -44
- package/src/types/options.ts +7 -5
- package/src/types/slate.ts +6 -6
- package/src/utils/__tests__/dmpToOperations.test.ts +41 -16
- package/src/utils/__tests__/operationToPatches.test.ts +4 -3
- package/src/utils/__tests__/patchToOperations.test.ts +16 -5
- package/src/utils/__tests__/ranges.test.ts +9 -4
- package/src/utils/__tests__/valueNormalization.test.tsx +12 -4
- package/src/utils/__tests__/values.test.ts +0 -1
- package/src/utils/applyPatch.ts +78 -29
- package/src/utils/getPortableTextMemberSchemaTypes.ts +38 -23
- package/src/utils/operationToPatches.ts +123 -44
- package/src/utils/paths.ts +26 -9
- package/src/utils/ranges.ts +16 -10
- package/src/utils/selection.ts +21 -9
- package/src/utils/ucs2Indices.ts +2 -2
- package/src/utils/validateValue.ts +118 -45
- package/src/utils/values.ts +38 -17
- package/src/utils/weakMaps.ts +20 -10
- package/src/utils/withChanges.ts +5 -3
- package/src/utils/withUndoRedo.ts +1 -1
- package/src/utils/withoutPatching.ts +1 -1
|
@@ -3,16 +3,32 @@
|
|
|
3
3
|
* The undo/redo steps are rebased against incoming patches since the step occurred.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import
|
|
7
|
-
import {
|
|
8
|
-
|
|
6
|
+
import type {Patch} from '@portabletext/patches'
|
|
7
|
+
import {
|
|
8
|
+
DIFF_DELETE,
|
|
9
|
+
DIFF_EQUAL,
|
|
10
|
+
DIFF_INSERT,
|
|
11
|
+
parsePatch,
|
|
12
|
+
} from '@sanity/diff-match-patch'
|
|
13
|
+
import type {ObjectSchemaType, PortableTextBlock} from '@sanity/types'
|
|
9
14
|
import {flatten, isEqual} from 'lodash'
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
|
|
15
|
+
import {
|
|
16
|
+
Editor,
|
|
17
|
+
Operation,
|
|
18
|
+
Path,
|
|
19
|
+
Transforms,
|
|
20
|
+
type Descendant,
|
|
21
|
+
type SelectionOperation,
|
|
22
|
+
} from 'slate'
|
|
23
|
+
import type {PatchObservable, PortableTextSlateEditor} from '../../types/editor'
|
|
13
24
|
import {debugWithName} from '../../utils/debug'
|
|
14
25
|
import {fromSlateValue} from '../../utils/values'
|
|
15
|
-
import {
|
|
26
|
+
import {
|
|
27
|
+
setIsRedoing,
|
|
28
|
+
setIsUndoing,
|
|
29
|
+
withRedoing,
|
|
30
|
+
withUndoing,
|
|
31
|
+
} from '../../utils/withUndoRedo'
|
|
16
32
|
|
|
17
33
|
const debug = debugWithName('plugin:withUndoRedo')
|
|
18
34
|
const debugVerbose = debug.enabled && false
|
|
@@ -66,14 +82,21 @@ export function createWithUndoRedo(
|
|
|
66
82
|
patches.forEach((patch) => {
|
|
67
83
|
if (!reset && patch.origin !== 'local' && remotePatches) {
|
|
68
84
|
if (patch.type === 'unset' && patch.path.length === 0) {
|
|
69
|
-
debug(
|
|
85
|
+
debug(
|
|
86
|
+
'Someone else cleared the content, resetting undo/redo history',
|
|
87
|
+
)
|
|
70
88
|
editor.history = {undos: [], redos: []}
|
|
71
89
|
remotePatches.splice(0, remotePatches.length)
|
|
72
90
|
SAVING.set(editor, true)
|
|
73
91
|
reset = true
|
|
74
92
|
return
|
|
75
93
|
}
|
|
76
|
-
remotePatches.push({
|
|
94
|
+
remotePatches.push({
|
|
95
|
+
patch,
|
|
96
|
+
time: new Date(),
|
|
97
|
+
snapshot,
|
|
98
|
+
previousSnapshot,
|
|
99
|
+
})
|
|
77
100
|
}
|
|
78
101
|
})
|
|
79
102
|
previousSnapshot = snapshot
|
|
@@ -94,7 +117,8 @@ export function createWithUndoRedo(
|
|
|
94
117
|
const {operations, history} = editor
|
|
95
118
|
const {undos} = history
|
|
96
119
|
const step = undos[undos.length - 1]
|
|
97
|
-
const lastOp =
|
|
120
|
+
const lastOp =
|
|
121
|
+
step && step.operations && step.operations[step.operations.length - 1]
|
|
98
122
|
const overwrite = shouldOverwrite(op, lastOp)
|
|
99
123
|
const save = isSaving(editor)
|
|
100
124
|
|
|
@@ -110,7 +134,12 @@ export function createWithUndoRedo(
|
|
|
110
134
|
step.operations.push(op)
|
|
111
135
|
} else {
|
|
112
136
|
const newStep = {
|
|
113
|
-
operations: [
|
|
137
|
+
operations: [
|
|
138
|
+
...(editor.selection === null
|
|
139
|
+
? []
|
|
140
|
+
: [createSelectOperation(editor)]),
|
|
141
|
+
op,
|
|
142
|
+
],
|
|
114
143
|
timestamp: new Date(),
|
|
115
144
|
}
|
|
116
145
|
undos.push(newStep)
|
|
@@ -137,16 +166,26 @@ export function createWithUndoRedo(
|
|
|
137
166
|
const step = undos[undos.length - 1]
|
|
138
167
|
debug('Undoing', step)
|
|
139
168
|
if (step.operations.length > 0) {
|
|
140
|
-
const otherPatches = remotePatches.filter(
|
|
169
|
+
const otherPatches = remotePatches.filter(
|
|
170
|
+
(item) => item.time >= step.timestamp,
|
|
171
|
+
)
|
|
141
172
|
let transformedOperations = step.operations
|
|
142
173
|
otherPatches.forEach((item) => {
|
|
143
174
|
transformedOperations = flatten(
|
|
144
175
|
transformedOperations.map((op) =>
|
|
145
|
-
transformOperation(
|
|
176
|
+
transformOperation(
|
|
177
|
+
editor,
|
|
178
|
+
item.patch,
|
|
179
|
+
op,
|
|
180
|
+
item.snapshot,
|
|
181
|
+
item.previousSnapshot,
|
|
182
|
+
),
|
|
146
183
|
),
|
|
147
184
|
)
|
|
148
185
|
})
|
|
149
|
-
const reversedOperations = transformedOperations
|
|
186
|
+
const reversedOperations = transformedOperations
|
|
187
|
+
.map(Operation.inverse)
|
|
188
|
+
.reverse()
|
|
150
189
|
|
|
151
190
|
try {
|
|
152
191
|
Editor.withoutNormalizing(editor, () => {
|
|
@@ -185,12 +224,20 @@ export function createWithUndoRedo(
|
|
|
185
224
|
const step = redos[redos.length - 1]
|
|
186
225
|
debug('Redoing', step)
|
|
187
226
|
if (step.operations.length > 0) {
|
|
188
|
-
const otherPatches = remotePatches.filter(
|
|
227
|
+
const otherPatches = remotePatches.filter(
|
|
228
|
+
(item) => item.time >= step.timestamp,
|
|
229
|
+
)
|
|
189
230
|
let transformedOperations = step.operations
|
|
190
231
|
otherPatches.forEach((item) => {
|
|
191
232
|
transformedOperations = flatten(
|
|
192
233
|
transformedOperations.map((op) =>
|
|
193
|
-
transformOperation(
|
|
234
|
+
transformOperation(
|
|
235
|
+
editor,
|
|
236
|
+
item.patch,
|
|
237
|
+
op,
|
|
238
|
+
item.snapshot,
|
|
239
|
+
item.previousSnapshot,
|
|
240
|
+
),
|
|
194
241
|
),
|
|
195
242
|
)
|
|
196
243
|
})
|
|
@@ -198,7 +245,6 @@ export function createWithUndoRedo(
|
|
|
198
245
|
Editor.withoutNormalizing(editor, () => {
|
|
199
246
|
withRedoing(editor, () => {
|
|
200
247
|
withoutSaving(editor, () => {
|
|
201
|
-
// eslint-disable-next-line max-nested-callbacks
|
|
202
248
|
transformedOperations.forEach((op) => {
|
|
203
249
|
editor.apply(op)
|
|
204
250
|
})
|
|
@@ -240,7 +286,9 @@ function transformOperation(
|
|
|
240
286
|
previousSnapshot: PortableTextBlock[] | undefined,
|
|
241
287
|
): Operation[] {
|
|
242
288
|
if (debugVerbose) {
|
|
243
|
-
debug(
|
|
289
|
+
debug(
|
|
290
|
+
`Adjusting '${operation.type}' operation paths for '${patch.type}' patch`,
|
|
291
|
+
)
|
|
244
292
|
debug(`Operation ${JSON.stringify(operation)}`)
|
|
245
293
|
debug(`Patch ${JSON.stringify(patch)}`)
|
|
246
294
|
}
|
|
@@ -254,7 +302,13 @@ function transformOperation(
|
|
|
254
302
|
debug(
|
|
255
303
|
`Adjusting block path (+${patch.items.length}) for '${transformedOperation.type}' operation and patch '${patch.type}'`,
|
|
256
304
|
)
|
|
257
|
-
return [
|
|
305
|
+
return [
|
|
306
|
+
adjustBlockPath(
|
|
307
|
+
transformedOperation,
|
|
308
|
+
patch.items.length,
|
|
309
|
+
insertBlockIndex,
|
|
310
|
+
),
|
|
311
|
+
]
|
|
258
312
|
}
|
|
259
313
|
|
|
260
314
|
if (patch.type === 'unset' && patch.path.length === 1) {
|
|
@@ -281,13 +335,21 @@ function transformOperation(
|
|
|
281
335
|
|
|
282
336
|
// Someone reset the whole value
|
|
283
337
|
if (patch.type === 'unset' && patch.path.length === 0) {
|
|
284
|
-
debug(
|
|
338
|
+
debug(
|
|
339
|
+
`Adjusting selection for unset everything patch and ${operation.type} operation`,
|
|
340
|
+
)
|
|
285
341
|
return []
|
|
286
342
|
}
|
|
287
343
|
|
|
288
344
|
if (patch.type === 'diffMatchPatch') {
|
|
289
|
-
const operationTargetBlock = findOperationTargetBlock(
|
|
290
|
-
|
|
345
|
+
const operationTargetBlock = findOperationTargetBlock(
|
|
346
|
+
editor,
|
|
347
|
+
transformedOperation,
|
|
348
|
+
)
|
|
349
|
+
if (
|
|
350
|
+
!operationTargetBlock ||
|
|
351
|
+
!isEqual({_key: operationTargetBlock._key}, patch.path[0])
|
|
352
|
+
) {
|
|
291
353
|
return [transformedOperation]
|
|
292
354
|
}
|
|
293
355
|
const diffPatches = parsePatch(patch.value)
|
|
@@ -318,7 +380,10 @@ function transformOperation(
|
|
|
318
380
|
}
|
|
319
381
|
// Adjust accordingly if someone removed text in the same node before us
|
|
320
382
|
if (transformedOperation.type === 'remove_text') {
|
|
321
|
-
if (
|
|
383
|
+
if (
|
|
384
|
+
changedOffset <=
|
|
385
|
+
transformedOperation.offset - transformedOperation.text.length
|
|
386
|
+
) {
|
|
322
387
|
transformedOperation.offset += adjustOffsetBy
|
|
323
388
|
}
|
|
324
389
|
}
|
|
@@ -365,7 +430,11 @@ function transformOperation(
|
|
|
365
430
|
/**
|
|
366
431
|
* Adjust the block path for a operation
|
|
367
432
|
*/
|
|
368
|
-
function adjustBlockPath(
|
|
433
|
+
function adjustBlockPath(
|
|
434
|
+
operation: Operation,
|
|
435
|
+
level: number,
|
|
436
|
+
blockIndex: number,
|
|
437
|
+
): Operation {
|
|
369
438
|
const transformedOperation = {...operation}
|
|
370
439
|
if (
|
|
371
440
|
blockIndex >= 0 &&
|
|
@@ -374,7 +443,10 @@ function adjustBlockPath(operation: Operation, level: number, blockIndex: number
|
|
|
374
443
|
transformedOperation.path[0] >= blockIndex + level &&
|
|
375
444
|
transformedOperation.path[0] + level > -1
|
|
376
445
|
) {
|
|
377
|
-
const newPath = [
|
|
446
|
+
const newPath = [
|
|
447
|
+
transformedOperation.path[0] + level,
|
|
448
|
+
...transformedOperation.path.slice(1),
|
|
449
|
+
]
|
|
378
450
|
transformedOperation.path = newPath
|
|
379
451
|
}
|
|
380
452
|
if (transformedOperation.type === 'set_selection') {
|
|
@@ -393,7 +465,11 @@ function adjustBlockPath(operation: Operation, level: number, blockIndex: number
|
|
|
393
465
|
if ((currentFocus && currentAnchor) || (newFocus && newAnchor)) {
|
|
394
466
|
const points = [currentFocus, currentAnchor, newFocus, newAnchor]
|
|
395
467
|
points.forEach((point) => {
|
|
396
|
-
if (
|
|
468
|
+
if (
|
|
469
|
+
point &&
|
|
470
|
+
point.path[0] >= blockIndex + level &&
|
|
471
|
+
point.path[0] + level > -1
|
|
472
|
+
) {
|
|
397
473
|
point.path = [point.path[0] + level, ...point.path.slice(1)]
|
|
398
474
|
}
|
|
399
475
|
})
|
|
@@ -449,7 +525,10 @@ const shouldMerge = (op: Operation, prev: Operation | undefined): boolean => {
|
|
|
449
525
|
return false
|
|
450
526
|
}
|
|
451
527
|
|
|
452
|
-
const shouldOverwrite = (
|
|
528
|
+
const shouldOverwrite = (
|
|
529
|
+
op: Operation,
|
|
530
|
+
prev: Operation | undefined,
|
|
531
|
+
): boolean => {
|
|
453
532
|
if (prev && op.type === 'set_selection' && prev.type === 'set_selection') {
|
|
454
533
|
return true
|
|
455
534
|
}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import {Editor, Range, Text, Transforms} from 'slate'
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
import type {
|
|
3
|
+
PortableTextMemberSchemaTypes,
|
|
4
|
+
PortableTextSlateEditor,
|
|
5
|
+
} from '../../types/editor'
|
|
4
6
|
import {debugWithName} from '../../utils/debug'
|
|
5
7
|
import {toSlateValue} from '../../utils/values'
|
|
6
|
-
import
|
|
8
|
+
import type {PortableTextEditor} from '../PortableTextEditor'
|
|
7
9
|
|
|
8
10
|
const debug = debugWithName('plugin:withUtils')
|
|
9
11
|
|
|
@@ -16,8 +18,14 @@ interface Options {
|
|
|
16
18
|
* This plugin makes various util commands available in the editor
|
|
17
19
|
*
|
|
18
20
|
*/
|
|
19
|
-
export function createWithUtils({
|
|
20
|
-
|
|
21
|
+
export function createWithUtils({
|
|
22
|
+
schemaTypes,
|
|
23
|
+
keyGenerator,
|
|
24
|
+
portableTextEditor,
|
|
25
|
+
}: Options) {
|
|
26
|
+
return function withUtils(
|
|
27
|
+
editor: PortableTextSlateEditor,
|
|
28
|
+
): PortableTextSlateEditor {
|
|
21
29
|
// Expands the the selection to wrap around the word the focus is at
|
|
22
30
|
editor.pteExpandToWord = () => {
|
|
23
31
|
const {selection} = editor
|
|
@@ -37,12 +45,24 @@ export function createWithUtils({schemaTypes, keyGenerator, portableTextEditor}:
|
|
|
37
45
|
.reverse()
|
|
38
46
|
.findIndex((str) => isEmpty(str))
|
|
39
47
|
const newStartOffset =
|
|
40
|
-
whiteSpaceBeforeIndex > -1
|
|
41
|
-
|
|
48
|
+
whiteSpaceBeforeIndex > -1
|
|
49
|
+
? charsBefore.length - whiteSpaceBeforeIndex
|
|
50
|
+
: 0
|
|
51
|
+
const whiteSpaceAfterIndex = charsAfter
|
|
52
|
+
.split('')
|
|
53
|
+
.findIndex((obj) => isEmpty(obj))
|
|
42
54
|
const newEndOffset =
|
|
43
55
|
charsBefore.length +
|
|
44
|
-
(whiteSpaceAfterIndex > -1
|
|
45
|
-
|
|
56
|
+
(whiteSpaceAfterIndex > -1
|
|
57
|
+
? whiteSpaceAfterIndex
|
|
58
|
+
: charsAfter.length + 1)
|
|
59
|
+
if (
|
|
60
|
+
!(
|
|
61
|
+
newStartOffset === newEndOffset ||
|
|
62
|
+
Number.isNaN(newStartOffset) ||
|
|
63
|
+
Number.isNaN(newEndOffset)
|
|
64
|
+
)
|
|
65
|
+
) {
|
|
46
66
|
debug('pteExpandToWord: Expanding to focused word')
|
|
47
67
|
Transforms.setSelection(editor, {
|
|
48
68
|
anchor: {...selection.anchor, offset: newStartOffset},
|
|
@@ -54,7 +74,7 @@ export function createWithUtils({schemaTypes, keyGenerator, portableTextEditor}:
|
|
|
54
74
|
}
|
|
55
75
|
}
|
|
56
76
|
|
|
57
|
-
editor.
|
|
77
|
+
editor.pteCreateTextBlock = (options: {decorators: Array<string>}) => {
|
|
58
78
|
const block = toSlateValue(
|
|
59
79
|
[
|
|
60
80
|
{
|
|
@@ -67,7 +87,9 @@ export function createWithUtils({schemaTypes, keyGenerator, portableTextEditor}:
|
|
|
67
87
|
_type: 'span',
|
|
68
88
|
_key: keyGenerator(),
|
|
69
89
|
text: '',
|
|
70
|
-
marks:
|
|
90
|
+
marks: options.decorators.filter((decorator) =>
|
|
91
|
+
schemaTypes.decorators.find(({value}) => value === decorator),
|
|
92
|
+
),
|
|
71
93
|
},
|
|
72
94
|
],
|
|
73
95
|
},
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import {noop} from 'lodash'
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
import
|
|
5
|
-
import {type createEditorOptions} from '../../types/options'
|
|
2
|
+
import type {BaseOperation, Editor, Node, NodeEntry} from 'slate'
|
|
3
|
+
import type {PortableTextSlateEditor} from '../../types/editor'
|
|
4
|
+
import type {createEditorOptions} from '../../types/options'
|
|
6
5
|
import {createOperationToPatches} from '../../utils/operationToPatches'
|
|
7
6
|
import {createWithEditableAPI} from './createWithEditableAPI'
|
|
8
7
|
import {createWithInsertBreak} from './createWithInsertBreak'
|
|
@@ -38,14 +37,18 @@ export interface OriginalEditorFunctions {
|
|
|
38
37
|
normalizeNode: (entry: NodeEntry<Node>) => void
|
|
39
38
|
}
|
|
40
39
|
|
|
41
|
-
const originalFnMap = new WeakMap<
|
|
40
|
+
const originalFnMap = new WeakMap<
|
|
41
|
+
PortableTextSlateEditor,
|
|
42
|
+
OriginalEditorFunctions
|
|
43
|
+
>()
|
|
42
44
|
|
|
43
45
|
export const withPlugins = <T extends Editor>(
|
|
44
46
|
editor: T,
|
|
45
47
|
options: createEditorOptions,
|
|
46
48
|
): {editor: PortableTextSlateEditor; subscribe: () => () => void} => {
|
|
47
49
|
const e = editor as T & PortableTextSlateEditor
|
|
48
|
-
const {keyGenerator, portableTextEditor, patches$, readOnly, maxBlocks} =
|
|
50
|
+
const {keyGenerator, portableTextEditor, patches$, readOnly, maxBlocks} =
|
|
51
|
+
options
|
|
49
52
|
const {schemaTypes, change$} = portableTextEditor
|
|
50
53
|
e.subscriptions = []
|
|
51
54
|
if (e.destroy) {
|
|
@@ -62,7 +65,11 @@ export const withPlugins = <T extends Editor>(
|
|
|
62
65
|
const operationToPatches = createOperationToPatches(schemaTypes)
|
|
63
66
|
const withObjectKeys = createWithObjectKeys(schemaTypes, keyGenerator)
|
|
64
67
|
const withSchemaTypes = createWithSchemaTypes({schemaTypes, keyGenerator})
|
|
65
|
-
const withEditableAPI = createWithEditableAPI(
|
|
68
|
+
const withEditableAPI = createWithEditableAPI(
|
|
69
|
+
portableTextEditor,
|
|
70
|
+
schemaTypes,
|
|
71
|
+
keyGenerator,
|
|
72
|
+
)
|
|
66
73
|
const withPatches = createWithPatches({
|
|
67
74
|
change$,
|
|
68
75
|
keyGenerator,
|
|
@@ -83,14 +90,22 @@ export const withPlugins = <T extends Editor>(
|
|
|
83
90
|
change$,
|
|
84
91
|
keyGenerator,
|
|
85
92
|
)
|
|
86
|
-
const withPortableTextBlockStyle =
|
|
93
|
+
const withPortableTextBlockStyle =
|
|
94
|
+
createWithPortableTextBlockStyle(schemaTypes)
|
|
87
95
|
|
|
88
96
|
const withPlaceholderBlock = createWithPlaceholderBlock()
|
|
89
97
|
|
|
90
|
-
const withInsertBreak = createWithInsertBreak(schemaTypes)
|
|
98
|
+
const withInsertBreak = createWithInsertBreak(schemaTypes, keyGenerator)
|
|
91
99
|
|
|
92
|
-
const withUtils = createWithUtils({
|
|
93
|
-
|
|
100
|
+
const withUtils = createWithUtils({
|
|
101
|
+
keyGenerator,
|
|
102
|
+
schemaTypes,
|
|
103
|
+
portableTextEditor,
|
|
104
|
+
})
|
|
105
|
+
const withPortableTextSelections = createWithPortableTextSelections(
|
|
106
|
+
change$,
|
|
107
|
+
schemaTypes,
|
|
108
|
+
)
|
|
94
109
|
|
|
95
110
|
e.destroy = () => {
|
|
96
111
|
const originalFunctions = originalFnMap.get(e)
|
|
@@ -111,7 +126,9 @@ export const withPlugins = <T extends Editor>(
|
|
|
111
126
|
withUtils(
|
|
112
127
|
withPlaceholderBlock(
|
|
113
128
|
withPortableTextLists(
|
|
114
|
-
withPortableTextSelections(
|
|
129
|
+
withPortableTextSelections(
|
|
130
|
+
withEditableAPI(withInsertBreak(e)),
|
|
131
|
+
),
|
|
115
132
|
),
|
|
116
133
|
),
|
|
117
134
|
),
|
|
@@ -134,7 +151,11 @@ export const withPlugins = <T extends Editor>(
|
|
|
134
151
|
withUtils(
|
|
135
152
|
withMaxBlocks(
|
|
136
153
|
withUndoRedo(
|
|
137
|
-
withPatches(
|
|
154
|
+
withPatches(
|
|
155
|
+
withPortableTextSelections(
|
|
156
|
+
withEditableAPI(withInsertBreak(e)),
|
|
157
|
+
),
|
|
158
|
+
),
|
|
138
159
|
),
|
|
139
160
|
),
|
|
140
161
|
),
|
package/src/types/editor.ts
CHANGED
|
@@ -1,36 +1,39 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
1
|
+
import type {Patch} from '@portabletext/patches'
|
|
2
|
+
import type {
|
|
3
|
+
ArraySchemaType,
|
|
4
|
+
BlockDecoratorDefinition,
|
|
5
|
+
BlockListDefinition,
|
|
6
|
+
BlockSchemaType,
|
|
7
|
+
BlockStyleDefinition,
|
|
8
|
+
ObjectSchemaType,
|
|
9
|
+
Path,
|
|
10
|
+
PortableTextBlock,
|
|
11
|
+
PortableTextChild,
|
|
12
|
+
PortableTextListBlock,
|
|
13
|
+
PortableTextObject,
|
|
14
|
+
PortableTextSpan,
|
|
15
|
+
PortableTextTextBlock,
|
|
16
|
+
SpanSchemaType,
|
|
17
|
+
TypedObject,
|
|
18
18
|
} from '@sanity/types'
|
|
19
|
-
import {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
19
|
+
import type {
|
|
20
|
+
ClipboardEvent,
|
|
21
|
+
FocusEvent,
|
|
22
|
+
KeyboardEvent,
|
|
23
|
+
PropsWithChildren,
|
|
24
|
+
ReactElement,
|
|
25
|
+
RefObject,
|
|
26
26
|
} from 'react'
|
|
27
|
-
import
|
|
28
|
-
import
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
import
|
|
27
|
+
import type {Observable, Subject} from 'rxjs'
|
|
28
|
+
import type {
|
|
29
|
+
Descendant,
|
|
30
|
+
Node as SlateNode,
|
|
31
|
+
Operation as SlateOperation,
|
|
32
|
+
} from 'slate'
|
|
33
|
+
import type {ReactEditor} from 'slate-react'
|
|
34
|
+
import type {DOMNode} from 'slate-react/dist/utils/dom'
|
|
35
|
+
import type {PortableTextEditableProps} from '../editor/Editable'
|
|
36
|
+
import type {PortableTextEditor} from '../editor/PortableTextEditor'
|
|
34
37
|
|
|
35
38
|
/** @beta */
|
|
36
39
|
export interface EditableAPIDeleteOptions {
|
|
@@ -44,11 +47,20 @@ export interface EditableAPI {
|
|
|
44
47
|
addAnnotation: (
|
|
45
48
|
type: ObjectSchemaType,
|
|
46
49
|
value?: {[prop: string]: unknown},
|
|
47
|
-
) =>
|
|
50
|
+
) =>
|
|
51
|
+
| {markDefPath: Path; markDefPaths: Array<Path>; spanPath: Path}
|
|
52
|
+
| undefined
|
|
48
53
|
blur: () => void
|
|
49
|
-
delete: (
|
|
50
|
-
|
|
51
|
-
|
|
54
|
+
delete: (
|
|
55
|
+
selection: EditorSelection,
|
|
56
|
+
options?: EditableAPIDeleteOptions,
|
|
57
|
+
) => void
|
|
58
|
+
findByPath: (
|
|
59
|
+
path: Path,
|
|
60
|
+
) => [PortableTextBlock | PortableTextChild | undefined, Path | undefined]
|
|
61
|
+
findDOMNode: (
|
|
62
|
+
element: PortableTextBlock | PortableTextChild,
|
|
63
|
+
) => DOMNode | undefined
|
|
52
64
|
focus: () => void
|
|
53
65
|
focusBlock: () => PortableTextBlock | undefined
|
|
54
66
|
focusChild: () => PortableTextChild | undefined
|
|
@@ -57,13 +69,22 @@ export interface EditableAPI {
|
|
|
57
69
|
getValue: () => PortableTextBlock[] | undefined
|
|
58
70
|
hasBlockStyle: (style: string) => boolean
|
|
59
71
|
hasListStyle: (listStyle: string) => boolean
|
|
60
|
-
insertBlock: (
|
|
61
|
-
|
|
72
|
+
insertBlock: (
|
|
73
|
+
type: BlockSchemaType | ObjectSchemaType,
|
|
74
|
+
value?: {[prop: string]: unknown},
|
|
75
|
+
) => Path
|
|
76
|
+
insertChild: (
|
|
77
|
+
type: SpanSchemaType | ObjectSchemaType,
|
|
78
|
+
value?: {[prop: string]: unknown},
|
|
79
|
+
) => Path
|
|
62
80
|
insertBreak: () => void
|
|
63
81
|
isCollapsedSelection: () => boolean
|
|
64
82
|
isExpandedSelection: () => boolean
|
|
65
83
|
isMarkActive: (mark: string) => boolean
|
|
66
|
-
isSelectionsOverlapping: (
|
|
84
|
+
isSelectionsOverlapping: (
|
|
85
|
+
selectionA: EditorSelection,
|
|
86
|
+
selectionB: EditorSelection,
|
|
87
|
+
) => boolean
|
|
67
88
|
isVoid: (element: PortableTextBlock | PortableTextChild) => boolean
|
|
68
89
|
marks: () => string[]
|
|
69
90
|
redo: () => void
|
|
@@ -199,9 +220,9 @@ export interface PortableTextSlateEditor extends ReactEditor {
|
|
|
199
220
|
pteWithHotKeys: (event: KeyboardEvent<HTMLDivElement>) => void
|
|
200
221
|
|
|
201
222
|
/**
|
|
202
|
-
* Helper function that creates
|
|
223
|
+
* Helper function that creates a text block
|
|
203
224
|
*/
|
|
204
|
-
|
|
225
|
+
pteCreateTextBlock: (options: {decorators: Array<string>}) => Descendant
|
|
205
226
|
|
|
206
227
|
/**
|
|
207
228
|
* Undo
|
|
@@ -498,10 +519,14 @@ export type RenderBlockFunction = (props: BlockRenderProps) => JSX.Element
|
|
|
498
519
|
export type RenderChildFunction = (props: BlockChildRenderProps) => JSX.Element
|
|
499
520
|
|
|
500
521
|
/** @beta */
|
|
501
|
-
export type RenderEditableFunction = (
|
|
522
|
+
export type RenderEditableFunction = (
|
|
523
|
+
props: PortableTextEditableProps,
|
|
524
|
+
) => JSX.Element
|
|
502
525
|
|
|
503
526
|
/** @beta */
|
|
504
|
-
export type RenderAnnotationFunction = (
|
|
527
|
+
export type RenderAnnotationFunction = (
|
|
528
|
+
props: BlockAnnotationRenderProps,
|
|
529
|
+
) => JSX.Element
|
|
505
530
|
|
|
506
531
|
/** @beta */
|
|
507
532
|
export type RenderPlaceholderFunction = () => React.ReactNode
|
|
@@ -522,10 +547,14 @@ export interface BlockStyleRenderProps {
|
|
|
522
547
|
}
|
|
523
548
|
|
|
524
549
|
/** @beta */
|
|
525
|
-
export type RenderListItemFunction = (
|
|
550
|
+
export type RenderListItemFunction = (
|
|
551
|
+
props: BlockListItemRenderProps,
|
|
552
|
+
) => JSX.Element
|
|
526
553
|
|
|
527
554
|
/** @beta */
|
|
528
|
-
export type RenderDecoratorFunction = (
|
|
555
|
+
export type RenderDecoratorFunction = (
|
|
556
|
+
props: BlockDecoratorRenderProps,
|
|
557
|
+
) => JSX.Element
|
|
529
558
|
|
|
530
559
|
/** @beta */
|
|
531
560
|
export type ScrollSelectionIntoViewFunction = (
|
package/src/types/options.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
import
|
|
4
|
-
import {type PatchObservable} from './editor'
|
|
1
|
+
import type {BaseSyntheticEvent} from 'react'
|
|
2
|
+
import type {PortableTextEditor} from '../editor/PortableTextEditor'
|
|
3
|
+
import type {PatchObservable} from './editor'
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
6
|
* @internal
|
|
@@ -19,5 +18,8 @@ export type createEditorOptions = {
|
|
|
19
18
|
*/
|
|
20
19
|
export type HotkeyOptions = {
|
|
21
20
|
marks?: Record<string, string>
|
|
22
|
-
custom?: Record<
|
|
21
|
+
custom?: Record<
|
|
22
|
+
string,
|
|
23
|
+
(event: BaseSyntheticEvent, editor: PortableTextEditor) => void
|
|
24
|
+
>
|
|
23
25
|
}
|
package/src/types/slate.ts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
import {type PortableTextSlateEditor} from '..'
|
|
1
|
+
import type {PortableTextSpan, PortableTextTextBlock} from '@sanity/types'
|
|
2
|
+
import type {BaseEditor, Descendant} from 'slate'
|
|
3
|
+
import type {ReactEditor} from 'slate-react'
|
|
4
|
+
import type {PortableTextSlateEditor} from '..'
|
|
6
5
|
|
|
7
6
|
export interface VoidElement {
|
|
8
7
|
_type: string
|
|
@@ -12,7 +11,8 @@ export interface VoidElement {
|
|
|
12
11
|
value: Record<string, unknown>
|
|
13
12
|
}
|
|
14
13
|
|
|
15
|
-
export interface SlateTextBlock
|
|
14
|
+
export interface SlateTextBlock
|
|
15
|
+
extends Omit<PortableTextTextBlock, 'children'> {
|
|
16
16
|
children: Descendant[]
|
|
17
17
|
}
|
|
18
18
|
|