@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.d.mts CHANGED
@@ -2260,7 +2260,7 @@ export declare class PortableTextEditor extends Component<PortableTextEditorProp
2260
2260
  editor: PortableTextEditor,
2261
2261
  element: PortableTextBlock | PortableTextChild,
2262
2262
  ) => boolean | undefined
2263
- static isObjectPath: (editor: PortableTextEditor, path: Path) => boolean
2263
+ static isObjectPath: (_editor: PortableTextEditor, path: Path) => boolean
2264
2264
  static marks: (editor: PortableTextEditor) => string[] | undefined
2265
2265
  static select: (
2266
2266
  editor: PortableTextEditor,
package/lib/index.d.ts CHANGED
@@ -2260,7 +2260,7 @@ export declare class PortableTextEditor extends Component<PortableTextEditorProp
2260
2260
  editor: PortableTextEditor,
2261
2261
  element: PortableTextBlock | PortableTextChild,
2262
2262
  ) => boolean | undefined
2263
- static isObjectPath: (editor: PortableTextEditor, path: Path) => boolean
2263
+ static isObjectPath: (_editor: PortableTextEditor, path: Path) => boolean
2264
2264
  static marks: (editor: PortableTextEditor) => string[] | undefined
2265
2265
  static select: (
2266
2266
  editor: PortableTextEditor,
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)
@@ -2458,7 +2486,6 @@ function toInt(num) {
2458
2486
  }
2459
2487
  const debug$i = debugWithName("applyPatches"), debugVerbose$4 = debug$i.enabled && !0;
2460
2488
  function createApplyPatch(schemaTypes) {
2461
- let previousPatch;
2462
2489
  return (editor, patch) => {
2463
2490
  let changed = !1;
2464
2491
  debugVerbose$4 && (debug$i(
@@ -2472,7 +2499,7 @@ NEW PATCH =============================================================`
2472
2499
  changed = insertPatch(editor, patch, schemaTypes);
2473
2500
  break;
2474
2501
  case "unset":
2475
- changed = unsetPatch(editor, patch, previousPatch);
2502
+ changed = unsetPatch(editor, patch);
2476
2503
  break;
2477
2504
  case "set":
2478
2505
  changed = setPatch(editor, patch);
@@ -2486,7 +2513,7 @@ NEW PATCH =============================================================`
2486
2513
  } catch (err) {
2487
2514
  console.error(err);
2488
2515
  }
2489
- return previousPatch = patch, changed;
2516
+ return changed;
2490
2517
  };
2491
2518
  }
2492
2519
  function diffMatchPatch(editor, patch) {
@@ -2601,11 +2628,11 @@ function setPatch(editor, patch) {
2601
2628
  }
2602
2629
  return debugState(editor, "after"), !0;
2603
2630
  }
2604
- function unsetPatch(editor, patch, previousPatch) {
2631
+ function unsetPatch(editor, patch) {
2605
2632
  if (patch.path.length === 0) {
2606
2633
  debug$i("Removing everything"), debugState(editor, "before");
2607
2634
  const previousSelection = editor.selection;
2608
- return Transforms.deselect(editor), editor.children.forEach((c, i) => {
2635
+ return Transforms.deselect(editor), editor.children.forEach((_child, i) => {
2609
2636
  Transforms.removeNodes(editor, { at: [i] });
2610
2637
  }), Transforms.insertNodes(editor, editor.pteCreateTextBlock({ decorators: [] })), previousSelection && Transforms.select(editor, {
2611
2638
  anchor: { path: [0, 0], offset: 0 },
@@ -3371,29 +3398,67 @@ function createWithPortableTextMarkModel(editorActor, types, keyGenerator) {
3371
3398
  return;
3372
3399
  }
3373
3400
  if (op.type === "insert_text") {
3374
- const { selection } = editor;
3375
- if (selection && Range.isCollapsed(selection) && Editor.marks(editor)?.marks?.some(
3376
- (mark) => !decorators.includes(mark)
3377
- )) {
3378
- const [node] = Array.from(
3401
+ const { selection } = editor, collapsedSelection = selection ? Range.isCollapsed(selection) : !1;
3402
+ if (selection && collapsedSelection) {
3403
+ const [_block, blockPath] = Editor.node(editor, selection, {
3404
+ depth: 1
3405
+ }), [span, spanPath] = Array.from(
3379
3406
  Editor.nodes(editor, {
3380
3407
  mode: "lowest",
3381
3408
  at: selection.focus,
3382
- match: (n) => n._type === types.span.name,
3409
+ match: (n) => editor.isTextSpan(n),
3383
3410
  voids: !1
3384
3411
  })
3385
- )[0] || [void 0];
3386
- if (Text.isText(node) && node.text.length === selection.focus.offset && Array.isArray(node.marks) && node.marks.length > 0) {
3387
- const marksWithoutAnnotationMarks = ({
3388
- ...Editor.marks(editor) || {}
3389
- }.marks || []).filter((mark) => decorators.includes(mark));
3390
- Transforms.insertNodes(editor, {
3391
- _type: "span",
3392
- _key: keyGenerator(),
3393
- text: op.text,
3394
- marks: marksWithoutAnnotationMarks
3395
- }), debug$c("Inserting text at end of annotation");
3396
- return;
3412
+ )[0] ?? [void 0, void 0], marks = span.marks ?? [], marksWithoutAnnotations = marks.filter(
3413
+ (mark) => decorators.includes(mark)
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
+ }
3397
3462
  }
3398
3463
  }
3399
3464
  }
@@ -4981,7 +5046,7 @@ class PortableTextEditor extends Component {
4981
5046
  static insertBlock = (editor, type, value) => editor.editable?.insertBlock(type, value);
4982
5047
  static insertBreak = (editor) => editor.editable?.insertBreak();
4983
5048
  static isVoid = (editor, element) => editor.editable?.isVoid(element);
4984
- static isObjectPath = (editor, path) => {
5049
+ static isObjectPath = (_editor, path) => {
4985
5050
  if (!path || !Array.isArray(path)) return !1;
4986
5051
  const isChildObjectEditPath = path.length > 3 && path[1] === "children";
4987
5052
  return path.length > 1 && path[1] !== "children" || isChildObjectEditPath;
@@ -5482,7 +5547,7 @@ const debug$1 = debugWithName("components:Leaf"), EMPTY_MARKS = [], Leaf = (prop
5482
5547
  [props, slateEditor]
5483
5548
  ), scrollSelectionIntoViewToSlate = useMemo(() => {
5484
5549
  if (scrollSelectionIntoView !== void 0)
5485
- return scrollSelectionIntoView === null ? noop : (editor, domRange) => {
5550
+ return scrollSelectionIntoView === null ? noop : (_editor, domRange) => {
5486
5551
  scrollSelectionIntoView(portableTextEditor, domRange);
5487
5552
  };
5488
5553
  }, [portableTextEditor, scrollSelectionIntoView]), decorate = useCallback(