@portabletext/editor 1.1.3 → 1.1.4

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.js CHANGED
@@ -1560,19 +1560,29 @@ function createWithInsertBreak(types2, keyGenerator) {
1560
1560
  insertBreak();
1561
1561
  return;
1562
1562
  }
1563
- const focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = slate.Node.descendant(editor, focusBlockPath);
1563
+ const [focusSpan] = Array.from(
1564
+ slate.Editor.nodes(editor, {
1565
+ mode: "lowest",
1566
+ at: editor.selection.focus,
1567
+ match: (n) => editor.isTextSpan(n),
1568
+ voids: !1
1569
+ })
1570
+ )[0] ?? [void 0], focusDecorators = focusSpan.marks?.filter(
1571
+ (mark) => types2.decorators.some((decorator) => decorator.value === mark)
1572
+ ) ?? [], focusAnnotations = focusSpan.marks?.filter(
1573
+ (mark) => !types2.decorators.some((decorator) => decorator.value === mark)
1574
+ ) ?? [], focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = slate.Node.descendant(editor, focusBlockPath);
1564
1575
  if (editor.isTextBlock(focusBlock)) {
1565
1576
  const [start, end] = slate.Range.edges(editor.selection), isEndAtStartOfBlock = isEqual__default.default(end, {
1566
1577
  path: [...focusBlockPath, 0],
1567
1578
  offset: 0
1568
1579
  });
1569
1580
  if (isEndAtStartOfBlock && slate.Range.isCollapsed(editor.selection)) {
1570
- const focusDecorators = editor.isTextSpan(focusBlock.children[0]) ? (focusBlock.children[0].marks ?? []).filter(
1571
- (mark) => types2.decorators.some((decorator) => decorator.value === mark)
1572
- ) : [];
1573
1581
  slate.Editor.insertNode(
1574
1582
  editor,
1575
- editor.pteCreateTextBlock({ decorators: focusDecorators })
1583
+ editor.pteCreateTextBlock({
1584
+ decorators: focusAnnotations.length === 0 ? focusDecorators : []
1585
+ })
1576
1586
  );
1577
1587
  const [nextBlockPath] = slate.Path.next(focusBlockPath);
1578
1588
  slate.Transforms.select(editor, {
@@ -1585,6 +1595,24 @@ function createWithInsertBreak(types2, keyGenerator) {
1585
1595
  path: [...focusBlockPath, focusBlock.children.length - 1],
1586
1596
  offset: editor.isTextSpan(lastFocusBlockChild) ? lastFocusBlockChild.text.length : 0
1587
1597
  });
1598
+ if (isStartAtEndOfBlock && slate.Range.isCollapsed(editor.selection) && focusDecorators.length > 0 && focusAnnotations.length > 0) {
1599
+ slate.Editor.withoutNormalizing(editor, () => {
1600
+ if (!editor.selection)
1601
+ return;
1602
+ slate.Editor.insertNode(
1603
+ editor,
1604
+ editor.pteCreateTextBlock({
1605
+ decorators: []
1606
+ })
1607
+ );
1608
+ const [nextBlockPath] = slate.Path.next(focusBlockPath);
1609
+ slate.Transforms.setSelection(editor, {
1610
+ anchor: { path: [nextBlockPath, 0], offset: 0 },
1611
+ focus: { path: [nextBlockPath, 0], offset: 0 }
1612
+ });
1613
+ }), editor.onChange();
1614
+ return;
1615
+ }
1588
1616
  if (!isEndAtStartOfBlock && !isStartAtEndOfBlock) {
1589
1617
  slate.Editor.withoutNormalizing(editor, () => {
1590
1618
  if (!editor.selection)
@@ -3355,24 +3383,65 @@ function createWithPortableTextMarkModel(editorActor, types2, keyGenerator) {
3355
3383
  if (op.type === "insert_text") {
3356
3384
  const { selection } = editor, collapsedSelection = selection ? slate.Range.isCollapsed(selection) : !1;
3357
3385
  if (selection && collapsedSelection) {
3358
- const [span] = Array.from(
3386
+ const [_block, blockPath] = slate.Editor.node(editor, selection, {
3387
+ depth: 1
3388
+ }), [span, spanPath] = Array.from(
3359
3389
  slate.Editor.nodes(editor, {
3360
3390
  mode: "lowest",
3361
3391
  at: selection.focus,
3362
3392
  match: (n) => editor.isTextSpan(n),
3363
3393
  voids: !1
3364
3394
  })
3365
- )[0], marks = span.marks ?? [], marksWithoutAnnotations = marks.filter(
3395
+ )[0] ?? [void 0, void 0], marks = span.marks ?? [], marksWithoutAnnotations = marks.filter(
3366
3396
  (mark) => decorators.includes(mark)
3367
- );
3368
- if (marks.length > marksWithoutAnnotations.length && (selection.anchor.offset === 0 || span.text.length === selection.focus.offset)) {
3369
- slate.Transforms.insertNodes(editor, {
3370
- _type: "span",
3371
- _key: keyGenerator(),
3372
- text: op.text,
3373
- marks: marksWithoutAnnotations
3374
- }), debug$c("Inserting text at end of annotation");
3375
- return;
3397
+ ), spanHasAnnotations = marks.length > marksWithoutAnnotations.length, spanIsEmpty = span.text.length === 0, atTheBeginningOfSpan = selection.anchor.offset === 0, atTheEndOfSpan = selection.anchor.offset === span.text.length;
3398
+ let previousSpan, nextSpan;
3399
+ for (const [child, childPath] of slate.Node.children(editor, blockPath, {
3400
+ reverse: !0
3401
+ }))
3402
+ if (editor.isTextSpan(child) && slate.Path.isBefore(childPath, spanPath)) {
3403
+ previousSpan = child;
3404
+ break;
3405
+ }
3406
+ for (const [child, childPath] of slate.Node.children(editor, blockPath))
3407
+ if (editor.isTextSpan(child) && slate.Path.isAfter(childPath, spanPath)) {
3408
+ nextSpan = child;
3409
+ break;
3410
+ }
3411
+ const previousSpanHasSameAnnotation = previousSpan ? previousSpan.marks?.some(
3412
+ (mark) => !decorators.includes(mark) && marks.includes(mark)
3413
+ ) : !1, previousSpanHasSameMarks = previousSpan ? previousSpan.marks?.every((mark) => marks.includes(mark)) : !1, nextSpanHasSameAnnotation = nextSpan ? nextSpan.marks?.some(
3414
+ (mark) => !decorators.includes(mark) && marks.includes(mark)
3415
+ ) : !1, nextSpanHasSameMarks = nextSpan ? nextSpan.marks?.every((mark) => marks.includes(mark)) : !1;
3416
+ if (spanHasAnnotations && !spanIsEmpty) {
3417
+ if (atTheBeginningOfSpan) {
3418
+ previousSpanHasSameMarks ? slate.Transforms.insertNodes(editor, {
3419
+ _type: "span",
3420
+ _key: keyGenerator(),
3421
+ text: op.text,
3422
+ marks: previousSpan?.marks ?? []
3423
+ }) : previousSpanHasSameAnnotation ? apply2(op) : slate.Transforms.insertNodes(editor, {
3424
+ _type: "span",
3425
+ _key: keyGenerator(),
3426
+ text: op.text,
3427
+ marks: []
3428
+ });
3429
+ return;
3430
+ }
3431
+ if (atTheEndOfSpan) {
3432
+ nextSpanHasSameMarks ? slate.Transforms.insertNodes(editor, {
3433
+ _type: "span",
3434
+ _key: keyGenerator(),
3435
+ text: op.text,
3436
+ marks: nextSpan?.marks ?? []
3437
+ }) : nextSpanHasSameAnnotation ? apply2(op) : slate.Transforms.insertNodes(editor, {
3438
+ _type: "span",
3439
+ _key: keyGenerator(),
3440
+ text: op.text,
3441
+ marks: []
3442
+ });
3443
+ return;
3444
+ }
3376
3445
  }
3377
3446
  }
3378
3447
  }