@portabletext/editor 2.13.5 → 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.
@@ -1,5 +1,5 @@
1
1
  import { BlockOffset, BlockPath, ChildPath, EditorContext, EditorSelection, EditorSelectionPoint } from "../_chunks-dts/behavior.types.action.cjs";
2
- import * as _sanity_types9 from "@sanity/types";
2
+ import * as _sanity_types8 from "@sanity/types";
3
3
  import { KeyedSegment, PortableTextBlock, PortableTextTextBlock } from "@sanity/types";
4
4
  import { isSpan, isTextBlock } from "@portabletext/schema";
5
5
  /**
@@ -143,7 +143,7 @@ declare function mergeTextBlocks({
143
143
  context: Pick<EditorContext, 'keyGenerator' | 'schema'>;
144
144
  targetBlock: PortableTextTextBlock;
145
145
  incomingBlock: PortableTextTextBlock;
146
- }): PortableTextTextBlock<_sanity_types9.PortableTextObject | _sanity_types9.PortableTextSpan>;
146
+ }): PortableTextTextBlock<_sanity_types8.PortableTextObject | _sanity_types8.PortableTextSpan>;
147
147
  /**
148
148
  * @public
149
149
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@portabletext/editor",
3
- "version": "2.13.5",
3
+ "version": "2.13.7",
4
4
  "description": "Portable Text Editor made in React",
5
5
  "keywords": [
6
6
  "sanity",
@@ -85,9 +85,9 @@
85
85
  "slate-dom": "^0.118.1",
86
86
  "slate-react": "0.117.4",
87
87
  "xstate": "^5.22.1",
88
- "@portabletext/block-tools": "^3.5.9",
89
- "@portabletext/patches": "^1.1.8",
88
+ "@portabletext/block-tools": "^3.5.10",
90
89
  "@portabletext/keyboard-shortcuts": "^1.1.1",
90
+ "@portabletext/patches": "^1.1.8",
91
91
  "@portabletext/schema": "^1.2.0"
92
92
  },
93
93
  "devDependencies": {
@@ -98,8 +98,9 @@
98
98
  "@types/debug": "^4.1.12",
99
99
  "@types/lodash": "^4.17.20",
100
100
  "@types/lodash.startcase": "^4.4.9",
101
- "@types/react": "^19.1.13",
102
- "@types/react-dom": "^19.1.9",
101
+ "@types/node": "^20",
102
+ "@types/react": "^19.2.0",
103
+ "@types/react-dom": "^19.2.0",
103
104
  "@vitejs/plugin-react": "^4.7.0",
104
105
  "@vitest/browser": "^3.2.4",
105
106
  "@vitest/coverage-istanbul": "^3.2.4",
@@ -108,8 +109,8 @@
108
109
  "eslint-formatter-gha": "^1.6.0",
109
110
  "eslint-plugin-react-hooks": "6.0.0-rc.2",
110
111
  "jsdom": "^27.0.0",
111
- "react": "^19.1.1",
112
- "react-dom": "^19.1.1",
112
+ "react": "^19.2.0",
113
+ "react-dom": "^19.2.0",
113
114
  "rxjs": "^7.8.2",
114
115
  "typescript": "5.9.3",
115
116
  "typescript-eslint": "^8.44.1",
@@ -4,7 +4,7 @@
4
4
  *
5
5
  */
6
6
 
7
- import {isSpan, isTextBlock} from '@portabletext/schema'
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
- previousMarks: previousSpan.node.marks ?? [],
240
- marks: [],
239
+ marks,
240
+ previousMarks: previousSpan?.node.marks ?? [],
241
241
  }
242
242
  } else {
243
243
  return {