@portabletext/editor 1.2.0 → 1.2.1

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
@@ -5,7 +5,7 @@ import { useRef, useState, useMemo, useEffect, useCallback, createContext, useCo
5
5
  import { Editor, Element as Element$1, Range, Point, Text, Path, Transforms, Node, Operation, createEditor, deleteBackward, deleteForward, insertText } from "slate";
6
6
  import { useSlateStatic, ReactEditor, useSelected, withReact, Slate, useSlate, Editable } from "slate-react";
7
7
  import debug$m from "debug";
8
- import { isKeySegment, isPortableTextTextBlock, isPortableTextSpan, isPortableTextListBlock } from "@sanity/types";
8
+ import { isKeySegment, isPortableTextTextBlock, isPortableTextSpan as isPortableTextSpan$1, isPortableTextListBlock } from "@sanity/types";
9
9
  import { styled } from "styled-components";
10
10
  import uniq from "lodash/uniq.js";
11
11
  import { Subject } from "rxjs";
@@ -839,7 +839,7 @@ function getFocusChild(context) {
839
839
  }
840
840
  function getFocusSpan(context) {
841
841
  const focusChild = getFocusChild(context);
842
- return focusChild && isPortableTextSpan(focusChild.node) ? { node: focusChild.node, path: focusChild.path } : void 0;
842
+ return focusChild && isPortableTextSpan$1(focusChild.node) ? { node: focusChild.node, path: focusChild.path } : void 0;
843
843
  }
844
844
  function getSelectionStartBlock(context) {
845
845
  const key = context.selection.backward ? isKeySegment(context.selection.focus.path[0]) ? context.selection.focus.path[0]._key : void 0 : isKeySegment(context.selection.anchor.path[0]) ? context.selection.anchor.path[0]._key : void 0, node = key ? context.value.find((block) => block._key === key) : void 0;
@@ -1551,14 +1551,14 @@ function createWithEditableAPI(editorActor, portableTextEditor, types) {
1551
1551
  })
1552
1552
  ];
1553
1553
  if (spans.length === 0 || spans.some(
1554
- ([span]) => !isPortableTextSpan(span) || !span.marks || span.marks?.length === 0
1554
+ ([span]) => !isPortableTextSpan$1(span) || !span.marks || span.marks?.length === 0
1555
1555
  ))
1556
1556
  return !1;
1557
1557
  const selectionMarkDefs = spans.reduce((accMarkDefs, [, path]) => {
1558
1558
  const [block] = Editor.node(editor, path, { depth: 1 });
1559
1559
  return editor.isTextBlock(block) && block.markDefs ? [...accMarkDefs, ...block.markDefs] : accMarkDefs;
1560
1560
  }, []);
1561
- return spans.every(([span]) => isPortableTextSpan(span) ? span.marks?.map(
1561
+ return spans.every(([span]) => isPortableTextSpan$1(span) ? span.marks?.map(
1562
1562
  (markKey) => selectionMarkDefs.find((def) => def?._key === markKey)?._type
1563
1563
  )?.includes(annotationType) : !1);
1564
1564
  } catch {
@@ -3365,6 +3365,9 @@ function createWithPortableTextLists(types) {
3365
3365
  }, editor;
3366
3366
  };
3367
3367
  }
3368
+ function isPortableTextSpan(node) {
3369
+ return node._type === "span" && "text" in node && typeof node.text == "string" && (typeof node.marks > "u" || Array.isArray(node.marks) && node.marks.every((mark) => typeof mark == "string"));
3370
+ }
3368
3371
  function isPortableTextBlock(node) {
3369
3372
  return (
3370
3373
  // A block doesn't _have_ to be named 'block' - to differentiate between
@@ -3530,6 +3533,71 @@ function createWithPortableTextMarkModel(editorActor, types) {
3530
3533
  apply2(op);
3531
3534
  return;
3532
3535
  }
3536
+ if (op.type === "set_selection" && Editor.marks(editor) && op.properties && op.newProperties && op.properties.anchor && op.properties.focus && op.newProperties.anchor && op.newProperties.focus) {
3537
+ const previousSelectionIsCollapsed = Range.isCollapsed({
3538
+ anchor: op.properties.anchor,
3539
+ focus: op.properties.focus
3540
+ }), newSelectionIsCollapsed = Range.isCollapsed({
3541
+ anchor: op.newProperties.anchor,
3542
+ focus: op.newProperties.focus
3543
+ });
3544
+ if (previousSelectionIsCollapsed && newSelectionIsCollapsed) {
3545
+ const focusSpan = Array.from(
3546
+ Editor.nodes(editor, {
3547
+ mode: "lowest",
3548
+ at: op.properties.focus,
3549
+ match: (n) => editor.isTextSpan(n),
3550
+ voids: !1
3551
+ })
3552
+ )[0]?.[0], newFocusSpan = Array.from(
3553
+ Editor.nodes(editor, {
3554
+ mode: "lowest",
3555
+ at: op.newProperties.focus,
3556
+ match: (n) => editor.isTextSpan(n),
3557
+ voids: !1
3558
+ })
3559
+ )[0]?.[0], movedToNextSpan = focusSpan && newFocusSpan && op.newProperties.focus.path[0] === op.properties.focus.path[0] && op.newProperties.focus.path[1] === op.properties.focus.path[1] + 1 && focusSpan.text.length === op.properties.focus.offset && op.newProperties.focus.offset === 0, movedToPreviousSpan = focusSpan && newFocusSpan && op.newProperties.focus.path[0] === op.properties.focus.path[0] && op.newProperties.focus.path[1] === op.properties.focus.path[1] - 1 && op.properties.focus.offset === 0 && newFocusSpan.text.length === op.newProperties.focus.offset;
3560
+ if (movedToNextSpan || movedToPreviousSpan)
3561
+ return;
3562
+ }
3563
+ }
3564
+ if (op.type === "insert_node") {
3565
+ const { selection } = editor;
3566
+ if (selection) {
3567
+ const [_block, blockPath] = Editor.node(editor, selection, { depth: 1 }), previousSpan = getPreviousSpan({
3568
+ editor,
3569
+ blockPath,
3570
+ spanPath: op.path
3571
+ }), previousSpanAnnotations = previousSpan ? previousSpan.marks?.filter((mark) => !decorators.includes(mark)) : [], nextSpan = getNextSpan({
3572
+ editor,
3573
+ blockPath,
3574
+ spanPath: [op.path[0], op.path[1] - 1]
3575
+ }), nextSpanAnnotations = nextSpan ? nextSpan.marks?.filter((mark) => !decorators.includes(mark)) : [], annotationsEnding = previousSpanAnnotations?.filter(
3576
+ (annotation) => !nextSpanAnnotations?.includes(annotation)
3577
+ ) ?? [];
3578
+ if (annotationsEnding.length > 0 && isPortableTextSpan(op.node) && op.node.marks?.some((mark) => annotationsEnding.includes(mark))) {
3579
+ Transforms.insertNodes(editor, {
3580
+ ...op.node,
3581
+ marks: op.node.marks?.filter(
3582
+ (mark) => !annotationsEnding.includes(mark)
3583
+ ) ?? []
3584
+ });
3585
+ return;
3586
+ }
3587
+ const annotationsStarting = nextSpanAnnotations?.filter(
3588
+ (annotation) => !previousSpanAnnotations?.includes(annotation)
3589
+ ) ?? [];
3590
+ if (annotationsStarting.length > 0 && isPortableTextSpan(op.node) && op.node.marks?.some((mark) => annotationsStarting.includes(mark))) {
3591
+ Transforms.insertNodes(editor, {
3592
+ ...op.node,
3593
+ marks: op.node.marks?.filter(
3594
+ (mark) => !annotationsStarting.includes(mark)
3595
+ ) ?? []
3596
+ });
3597
+ return;
3598
+ }
3599
+ }
3600
+ }
3533
3601
  if (op.type === "insert_text") {
3534
3602
  const { selection } = editor, collapsedSelection = selection ? Range.isCollapsed(selection) : !1;
3535
3603
  if (selection && collapsedSelection) {
@@ -3546,25 +3614,41 @@ function createWithPortableTextMarkModel(editorActor, types) {
3546
3614
  (mark) => decorators.includes(mark)
3547
3615
  ), spanHasAnnotations = marks.length > marksWithoutAnnotations.length, spanIsEmpty = span.text.length === 0, atTheBeginningOfSpan = selection.anchor.offset === 0, atTheEndOfSpan = selection.anchor.offset === span.text.length, previousSpan = getPreviousSpan({ editor, blockPath, spanPath }), nextSpan = getNextSpan({ editor, blockPath, spanPath }), nextSpanAnnotations = nextSpan?.marks?.filter((mark) => !decorators.includes(mark)) ?? [], spanAnnotations = marks.filter(
3548
3616
  (mark) => !decorators.includes(mark)
3549
- ), previousSpanHasSameAnnotation = previousSpan ? previousSpan.marks?.some(
3617
+ ), previousSpanHasAnnotations = previousSpan ? previousSpan.marks?.some((mark) => !decorators.includes(mark)) : !1, previousSpanHasSameAnnotations = previousSpan ? previousSpan.marks?.filter((mark) => !decorators.includes(mark)).every((mark) => marks.includes(mark)) : !1, previousSpanHasSameAnnotation = previousSpan ? previousSpan.marks?.some(
3550
3618
  (mark) => !decorators.includes(mark) && marks.includes(mark)
3551
3619
  ) : !1, previousSpanHasSameMarks = previousSpan ? previousSpan.marks?.every((mark) => marks.includes(mark)) : !1, nextSpanSharesSomeAnnotations = spanAnnotations.some(
3552
3620
  (mark) => nextSpanAnnotations?.includes(mark)
3553
3621
  );
3554
3622
  if (spanHasAnnotations && !spanIsEmpty) {
3555
3623
  if (atTheBeginningOfSpan) {
3556
- previousSpanHasSameMarks ? Transforms.insertNodes(editor, {
3557
- _type: "span",
3558
- _key: editorActor.getSnapshot().context.keyGenerator(),
3559
- text: op.text,
3560
- marks: previousSpan?.marks ?? []
3561
- }) : previousSpanHasSameAnnotation ? apply2(op) : Transforms.insertNodes(editor, {
3562
- _type: "span",
3563
- _key: editorActor.getSnapshot().context.keyGenerator(),
3564
- text: op.text,
3565
- marks: []
3566
- });
3567
- return;
3624
+ if (previousSpanHasSameMarks) {
3625
+ Transforms.insertNodes(editor, {
3626
+ _type: "span",
3627
+ _key: editorActor.getSnapshot().context.keyGenerator(),
3628
+ text: op.text,
3629
+ marks: previousSpan?.marks ?? []
3630
+ });
3631
+ return;
3632
+ } else if (previousSpanHasSameAnnotations) {
3633
+ Transforms.insertNodes(editor, {
3634
+ _type: "span",
3635
+ _key: editorActor.getSnapshot().context.keyGenerator(),
3636
+ text: op.text,
3637
+ marks: previousSpan?.marks ?? []
3638
+ });
3639
+ return;
3640
+ } else if (previousSpanHasSameAnnotation) {
3641
+ apply2(op);
3642
+ return;
3643
+ } else if (!previousSpan) {
3644
+ Transforms.insertNodes(editor, {
3645
+ _type: "span",
3646
+ _key: editorActor.getSnapshot().context.keyGenerator(),
3647
+ text: op.text,
3648
+ marks: []
3649
+ });
3650
+ return;
3651
+ }
3568
3652
  }
3569
3653
  if (atTheEndOfSpan) {
3570
3654
  if (nextSpan && nextSpanSharesSomeAnnotations && nextSpanAnnotations.length < spanAnnotations.length || !nextSpanSharesSomeAnnotations) {
@@ -3592,7 +3676,7 @@ function createWithPortableTextMarkModel(editorActor, types) {
3592
3676
  _type: "span",
3593
3677
  _key: editorActor.getSnapshot().context.keyGenerator(),
3594
3678
  text: op.text,
3595
- marks: (previousSpan.marks ?? []).filter(
3679
+ marks: previousSpanHasAnnotations ? [] : (previousSpan.marks ?? []).filter(
3596
3680
  (mark) => decorators.includes(mark)
3597
3681
  )
3598
3682
  });
@@ -3820,7 +3904,7 @@ function createWithSchemaTypes({
3820
3904
  schemaTypes
3821
3905
  }) {
3822
3906
  return function(editor) {
3823
- editor.isTextBlock = (value) => isPortableTextTextBlock(value) && value._type === schemaTypes.block.name, editor.isTextSpan = (value) => isPortableTextSpan(value) && value._type === schemaTypes.span.name, editor.isListBlock = (value) => isPortableTextListBlock(value) && value._type === schemaTypes.block.name, editor.isVoid = (element) => schemaTypes.block.name !== element._type && (schemaTypes.blockObjects.map((obj) => obj.name).includes(element._type) || schemaTypes.inlineObjects.map((obj) => obj.name).includes(element._type)), editor.isInline = (element) => schemaTypes.inlineObjects.map((obj) => obj.name).includes(element._type) && "__inline" in element && element.__inline === !0;
3907
+ editor.isTextBlock = (value) => isPortableTextTextBlock(value) && value._type === schemaTypes.block.name, editor.isTextSpan = (value) => isPortableTextSpan$1(value) && value._type === schemaTypes.span.name, editor.isListBlock = (value) => isPortableTextListBlock(value) && value._type === schemaTypes.block.name, editor.isVoid = (element) => schemaTypes.block.name !== element._type && (schemaTypes.blockObjects.map((obj) => obj.name).includes(element._type) || schemaTypes.inlineObjects.map((obj) => obj.name).includes(element._type)), editor.isInline = (element) => schemaTypes.inlineObjects.map((obj) => obj.name).includes(element._type) && "__inline" in element && element.__inline === !0;
3824
3908
  const { normalizeNode } = editor;
3825
3909
  return editor.normalizeNode = (entry) => {
3826
3910
  const [node, path] = entry;
@@ -3973,10 +4057,10 @@ function createWithHotkeys(portableTextEditor, hotkeysFromOptions) {
3973
4057
  if ((isTab || isShiftTab) && editor.selection) {
3974
4058
  const [focusChild] = Editor.node(editor, editor.selection.focus, {
3975
4059
  depth: 2
3976
- }), [focusBlock] = isPortableTextSpan(focusChild) ? Editor.node(editor, editor.selection.focus, { depth: 1 }) : [], hasAnnotationFocus = focusChild && isPortableTextTextBlock(focusBlock) && isPortableTextSpan(focusChild) && (focusChild.marks || []).filter(
4060
+ }), [focusBlock] = isPortableTextSpan$1(focusChild) ? Editor.node(editor, editor.selection.focus, { depth: 1 }) : [], hasAnnotationFocus = focusChild && isPortableTextTextBlock(focusBlock) && isPortableTextSpan$1(focusChild) && (focusChild.marks || []).filter(
3977
4061
  (m) => (focusBlock.markDefs || []).map((def) => def._key).includes(m)
3978
4062
  ).length > 0, [start] = Range.edges(editor.selection), atStartOfNode = Editor.isStart(editor, start, start.path);
3979
- focusChild && isPortableTextSpan(focusChild) && (!hasAnnotationFocus || atStartOfNode) && editor.pteIncrementBlockLevels(isShiftTab) && event.preventDefault();
4063
+ focusChild && isPortableTextSpan$1(focusChild) && (!hasAnnotationFocus || atStartOfNode) && editor.pteIncrementBlockLevels(isShiftTab) && event.preventDefault();
3980
4064
  }
3981
4065
  if (isEnter && !isShiftEnter && editor.selection) {
3982
4066
  const focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = Node.descendant(editor, focusBlockPath);