@portabletext/editor 2.13.6 → 2.13.7
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/selector.is-active-style.cjs +2 -2
- package/lib/_chunks-cjs/selector.is-active-style.cjs.map +1 -1
- package/lib/_chunks-dts/behavior.types.action.d.ts +53 -53
- package/lib/_chunks-es/selector.is-active-style.js +2 -2
- package/lib/_chunks-es/selector.is-active-style.js.map +1 -1
- package/lib/index.cjs +0 -65
- package/lib/index.cjs.map +1 -1
- package/lib/index.js +1 -66
- package/lib/index.js.map +1 -1
- package/package.json +3 -3
- package/src/editor/plugins/createWithPortableTextMarkModel.ts +1 -118
- package/src/selectors/selector.get-mark-state.test.ts +63 -0
- package/src/selectors/selector.get-mark-state.ts +2 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@portabletext/editor",
|
|
3
|
-
"version": "2.13.
|
|
3
|
+
"version": "2.13.7",
|
|
4
4
|
"description": "Portable Text Editor made in React",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sanity",
|
|
@@ -86,9 +86,9 @@
|
|
|
86
86
|
"slate-react": "0.117.4",
|
|
87
87
|
"xstate": "^5.22.1",
|
|
88
88
|
"@portabletext/block-tools": "^3.5.10",
|
|
89
|
+
"@portabletext/keyboard-shortcuts": "^1.1.1",
|
|
89
90
|
"@portabletext/patches": "^1.1.8",
|
|
90
|
-
"@portabletext/schema": "^1.2.0"
|
|
91
|
-
"@portabletext/keyboard-shortcuts": "^1.1.1"
|
|
91
|
+
"@portabletext/schema": "^1.2.0"
|
|
92
92
|
},
|
|
93
93
|
"devDependencies": {
|
|
94
94
|
"@sanity/diff-match-patch": "^3.2.0",
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import {
|
|
7
|
+
import {isTextBlock} from '@portabletext/schema'
|
|
8
8
|
import type {PortableTextObject, PortableTextSpan} from '@sanity/types'
|
|
9
9
|
import {isEqual, uniq} from 'lodash'
|
|
10
10
|
import {Editor, Element, Node, Path, Range, Text, Transforms} from 'slate'
|
|
@@ -12,7 +12,6 @@ import {debugWithName} from '../../internal-utils/debug'
|
|
|
12
12
|
import {getNextSpan, getPreviousSpan} from '../../internal-utils/sibling-utils'
|
|
13
13
|
import type {BehaviorOperationImplementation} from '../../operations/behavior.operations'
|
|
14
14
|
import {getActiveDecorators} from '../../selectors/selector.get-active-decorators'
|
|
15
|
-
import {getMarkState} from '../../selectors/selector.get-mark-state'
|
|
16
15
|
import type {PortableTextSlateEditor} from '../../types/editor'
|
|
17
16
|
import type {EditorActor} from '../editor-machine'
|
|
18
17
|
import {getEditorSnapshot} from '../editor-selector'
|
|
@@ -325,122 +324,6 @@ export function createWithPortableTextMarkModel(
|
|
|
325
324
|
}
|
|
326
325
|
}
|
|
327
326
|
|
|
328
|
-
if (op.type === 'insert_node') {
|
|
329
|
-
const {selection} = editor
|
|
330
|
-
|
|
331
|
-
if (selection) {
|
|
332
|
-
const [_block, blockPath] = Editor.node(editor, selection, {depth: 1})
|
|
333
|
-
const previousSpan = getPreviousSpan({
|
|
334
|
-
editor,
|
|
335
|
-
blockPath,
|
|
336
|
-
spanPath: op.path,
|
|
337
|
-
})
|
|
338
|
-
const previousSpanAnnotations = previousSpan
|
|
339
|
-
? previousSpan.marks?.filter((mark) => !decorators.includes(mark))
|
|
340
|
-
: []
|
|
341
|
-
|
|
342
|
-
const nextSpan = getNextSpan({
|
|
343
|
-
editor,
|
|
344
|
-
blockPath,
|
|
345
|
-
spanPath: [op.path[0], op.path[1] - 1],
|
|
346
|
-
})
|
|
347
|
-
const nextSpanAnnotations = nextSpan
|
|
348
|
-
? nextSpan.marks?.filter((mark) => !decorators.includes(mark))
|
|
349
|
-
: []
|
|
350
|
-
|
|
351
|
-
const annotationsEnding =
|
|
352
|
-
previousSpanAnnotations?.filter(
|
|
353
|
-
(annotation) => !nextSpanAnnotations?.includes(annotation),
|
|
354
|
-
) ?? []
|
|
355
|
-
const atTheEndOfAnnotation = annotationsEnding.length > 0
|
|
356
|
-
|
|
357
|
-
if (
|
|
358
|
-
atTheEndOfAnnotation &&
|
|
359
|
-
isSpan(editorActor.getSnapshot().context, op.node) &&
|
|
360
|
-
op.node.marks?.some((mark) => annotationsEnding.includes(mark))
|
|
361
|
-
) {
|
|
362
|
-
Transforms.insertNodes(editor, {
|
|
363
|
-
...op.node,
|
|
364
|
-
_key: editorActor.getSnapshot().context.keyGenerator(),
|
|
365
|
-
marks:
|
|
366
|
-
op.node.marks?.filter(
|
|
367
|
-
(mark) => !annotationsEnding.includes(mark),
|
|
368
|
-
) ?? [],
|
|
369
|
-
})
|
|
370
|
-
return
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
const annotationsStarting =
|
|
374
|
-
nextSpanAnnotations?.filter(
|
|
375
|
-
(annotation) => !previousSpanAnnotations?.includes(annotation),
|
|
376
|
-
) ?? []
|
|
377
|
-
const atTheStartOfAnnotation = annotationsStarting.length > 0
|
|
378
|
-
|
|
379
|
-
if (
|
|
380
|
-
atTheStartOfAnnotation &&
|
|
381
|
-
isSpan(editorActor.getSnapshot().context, op.node) &&
|
|
382
|
-
op.node.marks?.some((mark) => annotationsStarting.includes(mark))
|
|
383
|
-
) {
|
|
384
|
-
Transforms.insertNodes(editor, {
|
|
385
|
-
...op.node,
|
|
386
|
-
_key: editorActor.getSnapshot().context.keyGenerator(),
|
|
387
|
-
marks:
|
|
388
|
-
op.node.marks?.filter(
|
|
389
|
-
(mark) => !annotationsStarting.includes(mark),
|
|
390
|
-
) ?? [],
|
|
391
|
-
})
|
|
392
|
-
return
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
const nextSpanDecorators =
|
|
396
|
-
nextSpan?.marks?.filter((mark) => decorators.includes(mark)) ?? []
|
|
397
|
-
const decoratorStarting = nextSpanDecorators.length > 0
|
|
398
|
-
|
|
399
|
-
if (
|
|
400
|
-
decoratorStarting &&
|
|
401
|
-
atTheEndOfAnnotation &&
|
|
402
|
-
!atTheStartOfAnnotation &&
|
|
403
|
-
isSpan(editorActor.getSnapshot().context, op.node) &&
|
|
404
|
-
op.node.marks?.length === 0
|
|
405
|
-
) {
|
|
406
|
-
Transforms.insertNodes(editor, {
|
|
407
|
-
...op.node,
|
|
408
|
-
_key: editorActor.getSnapshot().context.keyGenerator(),
|
|
409
|
-
marks: nextSpanDecorators,
|
|
410
|
-
})
|
|
411
|
-
return
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
if (op.type === 'insert_text') {
|
|
417
|
-
const snapshot = getEditorSnapshot({
|
|
418
|
-
editorActorSnapshot: editorActor.getSnapshot(),
|
|
419
|
-
slateEditorInstance: editor,
|
|
420
|
-
})
|
|
421
|
-
|
|
422
|
-
const markState = getMarkState(snapshot)
|
|
423
|
-
|
|
424
|
-
if (!markState) {
|
|
425
|
-
apply(op)
|
|
426
|
-
return
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
if (markState.state === 'unchanged') {
|
|
430
|
-
apply(op)
|
|
431
|
-
return
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
Transforms.insertNodes(editor, {
|
|
435
|
-
_type: 'span',
|
|
436
|
-
_key: editorActor.getSnapshot().context.keyGenerator(),
|
|
437
|
-
text: op.text,
|
|
438
|
-
marks: markState.marks,
|
|
439
|
-
})
|
|
440
|
-
|
|
441
|
-
return
|
|
442
|
-
}
|
|
443
|
-
|
|
444
327
|
if (op.type === 'remove_text') {
|
|
445
328
|
const {selection} = editor
|
|
446
329
|
|
|
@@ -73,4 +73,67 @@ describe(getMarkState.name, () => {
|
|
|
73
73
|
previousMarks: [linkKey, 'strong'],
|
|
74
74
|
})
|
|
75
75
|
})
|
|
76
|
+
|
|
77
|
+
test('Scenario: Caret after annotation inside decorator', () => {
|
|
78
|
+
const keyGenerator = createTestKeyGenerator()
|
|
79
|
+
const blockKey = keyGenerator()
|
|
80
|
+
const fooSpanKey = keyGenerator()
|
|
81
|
+
const barSpanKey = keyGenerator()
|
|
82
|
+
const bazSpanKey = keyGenerator()
|
|
83
|
+
const linkKey = keyGenerator()
|
|
84
|
+
const snapshot = createTestSnapshot({
|
|
85
|
+
context: {
|
|
86
|
+
keyGenerator,
|
|
87
|
+
value: [
|
|
88
|
+
{
|
|
89
|
+
_type: 'block',
|
|
90
|
+
_key: blockKey,
|
|
91
|
+
children: [
|
|
92
|
+
{
|
|
93
|
+
_type: 'span',
|
|
94
|
+
_key: fooSpanKey,
|
|
95
|
+
text: 'foo ',
|
|
96
|
+
marks: ['strong'],
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
_type: 'span',
|
|
100
|
+
_key: barSpanKey,
|
|
101
|
+
text: 'bar',
|
|
102
|
+
marks: [linkKey, 'strong'],
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
_type: 'span',
|
|
106
|
+
_key: bazSpanKey,
|
|
107
|
+
text: ' baz',
|
|
108
|
+
marks: ['strong'],
|
|
109
|
+
},
|
|
110
|
+
],
|
|
111
|
+
},
|
|
112
|
+
],
|
|
113
|
+
selection: {
|
|
114
|
+
anchor: {
|
|
115
|
+
path: [{_key: blockKey}, 'children', {_key: bazSpanKey}],
|
|
116
|
+
offset: 0,
|
|
117
|
+
},
|
|
118
|
+
focus: {
|
|
119
|
+
path: [{_key: blockKey}, 'children', {_key: bazSpanKey}],
|
|
120
|
+
offset: 0,
|
|
121
|
+
},
|
|
122
|
+
backward: false,
|
|
123
|
+
},
|
|
124
|
+
schema: compileSchema(
|
|
125
|
+
defineSchema({
|
|
126
|
+
annotations: [{name: 'link'}],
|
|
127
|
+
decorators: [{name: 'strong'}],
|
|
128
|
+
}),
|
|
129
|
+
),
|
|
130
|
+
},
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
expect(getMarkState(snapshot)).toEqual({
|
|
134
|
+
state: 'changed',
|
|
135
|
+
marks: ['strong'],
|
|
136
|
+
previousMarks: [linkKey, 'strong'],
|
|
137
|
+
})
|
|
138
|
+
})
|
|
76
139
|
})
|
|
@@ -236,8 +236,8 @@ export const getMarkState: EditorSelector<MarkState | undefined> = (
|
|
|
236
236
|
if (previousSpanHasAnnotations) {
|
|
237
237
|
return {
|
|
238
238
|
state: 'changed',
|
|
239
|
-
|
|
240
|
-
|
|
239
|
+
marks,
|
|
240
|
+
previousMarks: previousSpan?.node.marks ?? [],
|
|
241
241
|
}
|
|
242
242
|
} else {
|
|
243
243
|
return {
|