@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.esm.js CHANGED
@@ -1577,19 +1577,29 @@ function createWithInsertBreak(types, keyGenerator) {
1577
1577
  insertBreak();
1578
1578
  return;
1579
1579
  }
1580
- const focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = Node.descendant(editor, focusBlockPath);
1580
+ const [focusSpan] = Array.from(
1581
+ Editor.nodes(editor, {
1582
+ mode: "lowest",
1583
+ at: editor.selection.focus,
1584
+ match: (n) => editor.isTextSpan(n),
1585
+ voids: !1
1586
+ })
1587
+ )[0] ?? [void 0], focusDecorators = focusSpan.marks?.filter(
1588
+ (mark) => types.decorators.some((decorator) => decorator.value === mark)
1589
+ ) ?? [], focusAnnotations = focusSpan.marks?.filter(
1590
+ (mark) => !types.decorators.some((decorator) => decorator.value === mark)
1591
+ ) ?? [], focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = Node.descendant(editor, focusBlockPath);
1581
1592
  if (editor.isTextBlock(focusBlock)) {
1582
1593
  const [start, end] = Range.edges(editor.selection), isEndAtStartOfBlock = isEqual(end, {
1583
1594
  path: [...focusBlockPath, 0],
1584
1595
  offset: 0
1585
1596
  });
1586
1597
  if (isEndAtStartOfBlock && Range.isCollapsed(editor.selection)) {
1587
- const focusDecorators = editor.isTextSpan(focusBlock.children[0]) ? (focusBlock.children[0].marks ?? []).filter(
1588
- (mark) => types.decorators.some((decorator) => decorator.value === mark)
1589
- ) : [];
1590
1598
  Editor.insertNode(
1591
1599
  editor,
1592
- editor.pteCreateTextBlock({ decorators: focusDecorators })
1600
+ editor.pteCreateTextBlock({
1601
+ decorators: focusAnnotations.length === 0 ? focusDecorators : []
1602
+ })
1593
1603
  );
1594
1604
  const [nextBlockPath] = Path.next(focusBlockPath);
1595
1605
  Transforms.select(editor, {
@@ -1602,6 +1612,24 @@ function createWithInsertBreak(types, keyGenerator) {
1602
1612
  path: [...focusBlockPath, focusBlock.children.length - 1],
1603
1613
  offset: editor.isTextSpan(lastFocusBlockChild) ? lastFocusBlockChild.text.length : 0
1604
1614
  });
1615
+ if (isStartAtEndOfBlock && Range.isCollapsed(editor.selection) && focusDecorators.length > 0 && focusAnnotations.length > 0) {
1616
+ Editor.withoutNormalizing(editor, () => {
1617
+ if (!editor.selection)
1618
+ return;
1619
+ Editor.insertNode(
1620
+ editor,
1621
+ editor.pteCreateTextBlock({
1622
+ decorators: []
1623
+ })
1624
+ );
1625
+ const [nextBlockPath] = Path.next(focusBlockPath);
1626
+ Transforms.setSelection(editor, {
1627
+ anchor: { path: [nextBlockPath, 0], offset: 0 },
1628
+ focus: { path: [nextBlockPath, 0], offset: 0 }
1629
+ });
1630
+ }), editor.onChange();
1631
+ return;
1632
+ }
1605
1633
  if (!isEndAtStartOfBlock && !isStartAtEndOfBlock) {
1606
1634
  Editor.withoutNormalizing(editor, () => {
1607
1635
  if (!editor.selection)
@@ -3372,24 +3400,65 @@ function createWithPortableTextMarkModel(editorActor, types, keyGenerator) {
3372
3400
  if (op.type === "insert_text") {
3373
3401
  const { selection } = editor, collapsedSelection = selection ? Range.isCollapsed(selection) : !1;
3374
3402
  if (selection && collapsedSelection) {
3375
- const [span] = Array.from(
3403
+ const [_block, blockPath] = Editor.node(editor, selection, {
3404
+ depth: 1
3405
+ }), [span, spanPath] = Array.from(
3376
3406
  Editor.nodes(editor, {
3377
3407
  mode: "lowest",
3378
3408
  at: selection.focus,
3379
3409
  match: (n) => editor.isTextSpan(n),
3380
3410
  voids: !1
3381
3411
  })
3382
- )[0], marks = span.marks ?? [], marksWithoutAnnotations = marks.filter(
3412
+ )[0] ?? [void 0, void 0], marks = span.marks ?? [], marksWithoutAnnotations = marks.filter(
3383
3413
  (mark) => decorators.includes(mark)
3384
- );
3385
- if (marks.length > marksWithoutAnnotations.length && (selection.anchor.offset === 0 || span.text.length === selection.focus.offset)) {
3386
- Transforms.insertNodes(editor, {
3387
- _type: "span",
3388
- _key: keyGenerator(),
3389
- text: op.text,
3390
- marks: marksWithoutAnnotations
3391
- }), debug$c("Inserting text at end of annotation");
3392
- return;
3414
+ ), spanHasAnnotations = marks.length > marksWithoutAnnotations.length, spanIsEmpty = span.text.length === 0, atTheBeginningOfSpan = selection.anchor.offset === 0, atTheEndOfSpan = selection.anchor.offset === span.text.length;
3415
+ let previousSpan, nextSpan;
3416
+ for (const [child, childPath] of Node.children(editor, blockPath, {
3417
+ reverse: !0
3418
+ }))
3419
+ if (editor.isTextSpan(child) && Path.isBefore(childPath, spanPath)) {
3420
+ previousSpan = child;
3421
+ break;
3422
+ }
3423
+ for (const [child, childPath] of Node.children(editor, blockPath))
3424
+ if (editor.isTextSpan(child) && Path.isAfter(childPath, spanPath)) {
3425
+ nextSpan = child;
3426
+ break;
3427
+ }
3428
+ const previousSpanHasSameAnnotation = previousSpan ? previousSpan.marks?.some(
3429
+ (mark) => !decorators.includes(mark) && marks.includes(mark)
3430
+ ) : !1, previousSpanHasSameMarks = previousSpan ? previousSpan.marks?.every((mark) => marks.includes(mark)) : !1, nextSpanHasSameAnnotation = nextSpan ? nextSpan.marks?.some(
3431
+ (mark) => !decorators.includes(mark) && marks.includes(mark)
3432
+ ) : !1, nextSpanHasSameMarks = nextSpan ? nextSpan.marks?.every((mark) => marks.includes(mark)) : !1;
3433
+ if (spanHasAnnotations && !spanIsEmpty) {
3434
+ if (atTheBeginningOfSpan) {
3435
+ previousSpanHasSameMarks ? Transforms.insertNodes(editor, {
3436
+ _type: "span",
3437
+ _key: keyGenerator(),
3438
+ text: op.text,
3439
+ marks: previousSpan?.marks ?? []
3440
+ }) : previousSpanHasSameAnnotation ? apply2(op) : Transforms.insertNodes(editor, {
3441
+ _type: "span",
3442
+ _key: keyGenerator(),
3443
+ text: op.text,
3444
+ marks: []
3445
+ });
3446
+ return;
3447
+ }
3448
+ if (atTheEndOfSpan) {
3449
+ nextSpanHasSameMarks ? Transforms.insertNodes(editor, {
3450
+ _type: "span",
3451
+ _key: keyGenerator(),
3452
+ text: op.text,
3453
+ marks: nextSpan?.marks ?? []
3454
+ }) : nextSpanHasSameAnnotation ? apply2(op) : Transforms.insertNodes(editor, {
3455
+ _type: "span",
3456
+ _key: keyGenerator(),
3457
+ text: op.text,
3458
+ marks: []
3459
+ });
3460
+ return;
3461
+ }
3393
3462
  }
3394
3463
  }
3395
3464
  }