@portabletext/editor 1.1.2 → 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)
@@ -2441,7 +2469,6 @@ function toInt(num) {
2441
2469
  }
2442
2470
  const debug$i = debugWithName("applyPatches"), debugVerbose$4 = debug$i.enabled && !0;
2443
2471
  function createApplyPatch(schemaTypes) {
2444
- let previousPatch;
2445
2472
  return (editor, patch) => {
2446
2473
  let changed = !1;
2447
2474
  debugVerbose$4 && (debug$i(
@@ -2455,7 +2482,7 @@ NEW PATCH =============================================================`
2455
2482
  changed = insertPatch(editor, patch, schemaTypes);
2456
2483
  break;
2457
2484
  case "unset":
2458
- changed = unsetPatch(editor, patch, previousPatch);
2485
+ changed = unsetPatch(editor, patch);
2459
2486
  break;
2460
2487
  case "set":
2461
2488
  changed = setPatch(editor, patch);
@@ -2469,7 +2496,7 @@ NEW PATCH =============================================================`
2469
2496
  } catch (err) {
2470
2497
  console.error(err);
2471
2498
  }
2472
- return previousPatch = patch, changed;
2499
+ return changed;
2473
2500
  };
2474
2501
  }
2475
2502
  function diffMatchPatch(editor, patch) {
@@ -2584,11 +2611,11 @@ function setPatch(editor, patch) {
2584
2611
  }
2585
2612
  return debugState(editor, "after"), !0;
2586
2613
  }
2587
- function unsetPatch(editor, patch, previousPatch) {
2614
+ function unsetPatch(editor, patch) {
2588
2615
  if (patch.path.length === 0) {
2589
2616
  debug$i("Removing everything"), debugState(editor, "before");
2590
2617
  const previousSelection = editor.selection;
2591
- return slate.Transforms.deselect(editor), editor.children.forEach((c, i) => {
2618
+ return slate.Transforms.deselect(editor), editor.children.forEach((_child, i) => {
2592
2619
  slate.Transforms.removeNodes(editor, { at: [i] });
2593
2620
  }), slate.Transforms.insertNodes(editor, editor.pteCreateTextBlock({ decorators: [] })), previousSelection && slate.Transforms.select(editor, {
2594
2621
  anchor: { path: [0, 0], offset: 0 },
@@ -3354,29 +3381,67 @@ function createWithPortableTextMarkModel(editorActor, types2, keyGenerator) {
3354
3381
  return;
3355
3382
  }
3356
3383
  if (op.type === "insert_text") {
3357
- const { selection } = editor;
3358
- if (selection && slate.Range.isCollapsed(selection) && slate.Editor.marks(editor)?.marks?.some(
3359
- (mark) => !decorators.includes(mark)
3360
- )) {
3361
- const [node] = Array.from(
3384
+ const { selection } = editor, collapsedSelection = selection ? slate.Range.isCollapsed(selection) : !1;
3385
+ if (selection && collapsedSelection) {
3386
+ const [_block, blockPath] = slate.Editor.node(editor, selection, {
3387
+ depth: 1
3388
+ }), [span, spanPath] = Array.from(
3362
3389
  slate.Editor.nodes(editor, {
3363
3390
  mode: "lowest",
3364
3391
  at: selection.focus,
3365
- match: (n) => n._type === types2.span.name,
3392
+ match: (n) => editor.isTextSpan(n),
3366
3393
  voids: !1
3367
3394
  })
3368
- )[0] || [void 0];
3369
- if (slate.Text.isText(node) && node.text.length === selection.focus.offset && Array.isArray(node.marks) && node.marks.length > 0) {
3370
- const marksWithoutAnnotationMarks = ({
3371
- ...slate.Editor.marks(editor) || {}
3372
- }.marks || []).filter((mark) => decorators.includes(mark));
3373
- slate.Transforms.insertNodes(editor, {
3374
- _type: "span",
3375
- _key: keyGenerator(),
3376
- text: op.text,
3377
- marks: marksWithoutAnnotationMarks
3378
- }), debug$c("Inserting text at end of annotation");
3379
- return;
3395
+ )[0] ?? [void 0, void 0], marks = span.marks ?? [], marksWithoutAnnotations = marks.filter(
3396
+ (mark) => decorators.includes(mark)
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
+ }
3380
3445
  }
3381
3446
  }
3382
3447
  }
@@ -4964,7 +5029,7 @@ class PortableTextEditor extends react.Component {
4964
5029
  static insertBlock = (editor, type, value) => editor.editable?.insertBlock(type, value);
4965
5030
  static insertBreak = (editor) => editor.editable?.insertBreak();
4966
5031
  static isVoid = (editor, element) => editor.editable?.isVoid(element);
4967
- static isObjectPath = (editor, path) => {
5032
+ static isObjectPath = (_editor, path) => {
4968
5033
  if (!path || !Array.isArray(path)) return !1;
4969
5034
  const isChildObjectEditPath = path.length > 3 && path[1] === "children";
4970
5035
  return path.length > 1 && path[1] !== "children" || isChildObjectEditPath;
@@ -5465,7 +5530,7 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
5465
5530
  [props, slateEditor]
5466
5531
  ), scrollSelectionIntoViewToSlate = react.useMemo(() => {
5467
5532
  if (scrollSelectionIntoView !== void 0)
5468
- return scrollSelectionIntoView === null ? noop__default.default : (editor, domRange) => {
5533
+ return scrollSelectionIntoView === null ? noop__default.default : (_editor, domRange) => {
5469
5534
  scrollSelectionIntoView(portableTextEditor, domRange);
5470
5535
  };
5471
5536
  }, [portableTextEditor, scrollSelectionIntoView]), decorate = react.useCallback(