@portabletext/editor 1.0.18 → 1.0.19

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
@@ -122,7 +122,7 @@ function normalizeSelection(selection, value) {
122
122
  const { anchor, focus } = selection;
123
123
  return anchor && value.find((blk) => isEqual({ _key: blk._key }, anchor.path[0])) && (newAnchor = normalizePoint(anchor, value)), focus && value.find((blk) => isEqual({ _key: blk._key }, focus.path[0])) && (newFocus = normalizePoint(focus, value)), newAnchor && newFocus ? { anchor: newAnchor, focus: newFocus, backward: selection.backward } : null;
124
124
  }
125
- const EMPTY_MARKDEFS = [], EMPTY_MARKS$1 = [], VOID_CHILD_KEY = "void-child";
125
+ const EMPTY_MARKDEFS = [], VOID_CHILD_KEY = "void-child";
126
126
  function keepObjectEquality(object, keyMap) {
127
127
  const value = keyMap[object._key];
128
128
  return value && isEqual(object, value) ? value : (keyMap[object._key] = object, object);
@@ -1168,9 +1168,10 @@ function createWithEditableAPI(portableTextEditor, types, keyGenerator) {
1168
1168
  const [block] = Editor.node(editor, originalSelection.focus, { depth: 1 });
1169
1169
  if (!editor.isTextBlock(block))
1170
1170
  return;
1171
- Range.isCollapsed(originalSelection) && (editor.pteExpandToWord(), editor.onChange());
1172
1171
  const [textNode] = Editor.node(editor, originalSelection.focus, { depth: 2 });
1173
- editor.selection && (Editor.withoutNormalizing(editor, () => {
1172
+ if (!isPortableTextSpan$1(textNode) || textNode.text === "")
1173
+ return;
1174
+ Range.isCollapsed(originalSelection) && (editor.pteExpandToWord(), editor.onChange()), editor.selection && (Editor.withoutNormalizing(editor, () => {
1174
1175
  const annotationKey = keyGenerator();
1175
1176
  Transforms.setNodes(
1176
1177
  editor,
@@ -2786,7 +2787,7 @@ function createWithPortableTextMarkModel(types, change$, keyGenerator) {
2786
2787
  change$.next({ type: "selection", selection: ptRange });
2787
2788
  };
2788
2789
  return editor.normalizeNode = (nodeEntry) => {
2789
- const [node, path] = nodeEntry, isSpan = Text.isText(node) && node._type === types.span.name, isTextBlock = editor.isTextBlock(node);
2790
+ const [node, path] = nodeEntry;
2790
2791
  if (editor.isTextBlock(node)) {
2791
2792
  const children = Node.children(editor, path);
2792
2793
  for (const [child, childPath] of children) {
@@ -2801,65 +2802,47 @@ function createWithPortableTextMarkModel(types, change$, keyGenerator) {
2801
2802
  }
2802
2803
  }
2803
2804
  }
2804
- if (isSpan || isTextBlock) {
2805
- if (isSpan && !Array.isArray(node.marks)) {
2806
- debug$c("Adding .marks to span node"), Transforms.setNodes(editor, { marks: [] }, { at: path });
2807
- return;
2808
- }
2809
- if (isSpan && (node.marks || []).length > 0) {
2810
- const spanMarks = node.marks || EMPTY_MARKS$1, annotationMarks = spanMarks.filter(
2811
- (mark) => !types.decorators.map((dec) => dec.value).includes(mark)
2805
+ if (editor.isTextSpan(node) && !Array.isArray(node.marks)) {
2806
+ debug$c("Adding .marks to span node"), Transforms.setNodes(editor, { marks: [] }, { at: path });
2807
+ return;
2808
+ }
2809
+ if (editor.isTextSpan(node)) {
2810
+ const blockPath = Path.parent(path), [block] = Editor.node(editor, blockPath), decorators2 = types.decorators.map((decorator) => decorator.value), annotations = node.marks?.filter((mark) => !decorators2.includes(mark));
2811
+ if (editor.isTextBlock(block) && node.text === "" && annotations && annotations.length > 0) {
2812
+ debug$c("Removing annotations from empty span node"), Transforms.setNodes(
2813
+ editor,
2814
+ { marks: node.marks?.filter((mark) => decorators2.includes(mark)) },
2815
+ { at: path }
2812
2816
  );
2813
- if (annotationMarks.length > 0) {
2814
- const [block] = Editor.node(editor, Path.parent(path)), orphanedMarks = editor.isTextBlock(block) && annotationMarks.filter(
2815
- (mark) => !block.markDefs?.find((def) => def._key === mark)
2816
- ) || [];
2817
- if (orphanedMarks.length > 0) {
2818
- debug$c("Removing orphaned .marks from span node"), Transforms.setNodes(
2819
- editor,
2820
- { marks: spanMarks.filter((mark) => !orphanedMarks.includes(mark)) },
2821
- { at: path }
2822
- );
2823
- return;
2824
- }
2825
- }
2817
+ return;
2826
2818
  }
2827
- for (const op of editor.operations) {
2828
- if (op.type === "merge_node" && op.path.length === 1 && "markDefs" in op.properties && op.properties._type === types.block.name && Array.isArray(op.properties.markDefs) && op.properties.markDefs.length > 0 && op.path[0] - 1 >= 0) {
2829
- const [targetBlock, targetPath] = Editor.node(editor, [op.path[0] - 1]);
2830
- if (debug$c("Copying markDefs over to merged block", op), editor.isTextBlock(targetBlock)) {
2831
- const oldDefs = Array.isArray(targetBlock.markDefs) && targetBlock.markDefs || [], newMarkDefs = uniq([...oldDefs, ...op.properties.markDefs]);
2832
- if (!isEqual(newMarkDefs, targetBlock.markDefs)) {
2833
- Transforms.setNodes(editor, { markDefs: newMarkDefs }, { at: targetPath, voids: !1 });
2834
- return;
2835
- }
2836
- }
2837
- }
2838
- if (op.type === "split_node" && op.path.length === 1 && Element$1.isElementProps(op.properties) && op.properties._type === types.block.name && "markDefs" in op.properties && Array.isArray(op.properties.markDefs) && op.properties.markDefs.length > 0 && op.path[0] + 1 < editor.children.length) {
2839
- const [targetBlock, targetPath] = Editor.node(editor, [op.path[0] + 1]);
2840
- if (debug$c("Copying markDefs over to split block", op), editor.isTextBlock(targetBlock)) {
2841
- const oldDefs = Array.isArray(targetBlock.markDefs) && targetBlock.markDefs || [];
2842
- Transforms.setNodes(
2819
+ }
2820
+ if (editor.isTextBlock(node)) {
2821
+ const decorators2 = types.decorators.map((decorator) => decorator.value);
2822
+ for (const [child, childPath] of Node.children(editor, path))
2823
+ if (editor.isTextSpan(child)) {
2824
+ const marks = child.marks ?? [], orphanedAnnotations = marks.filter((mark) => !decorators2.includes(mark) && !node.markDefs?.find((def) => def._key === mark));
2825
+ if (orphanedAnnotations.length > 0) {
2826
+ debug$c("Removing orphaned annotations from span node"), Transforms.setNodes(
2843
2827
  editor,
2844
- { markDefs: uniq([...oldDefs, ...op.properties.markDefs]) },
2845
- { at: targetPath, voids: !1 }
2828
+ { marks: marks.filter((mark) => !orphanedAnnotations.includes(mark)) },
2829
+ { at: childPath }
2846
2830
  );
2847
2831
  return;
2848
2832
  }
2849
2833
  }
2850
- if (op.type === "split_node" && op.path.length === 2 && op.properties._type === types.span.name && "marks" in op.properties && Array.isArray(op.properties.marks) && op.properties.marks.length > 0 && op.path[0] + 1 < editor.children.length) {
2851
- const [child, childPath] = Editor.node(editor, [op.path[0] + 1, 0]);
2852
- if (Text.isText(child) && child.text === "" && Array.isArray(child.marks) && child.marks.length > 0) {
2853
- Transforms.setNodes(editor, { marks: [] }, { at: childPath, voids: !1 });
2854
- return;
2855
- }
2856
- }
2857
- if (op.type === "split_node" && op.path.length === 1 && op.properties._type === types.block.name && "markDefs" in op.properties && Array.isArray(op.properties.markDefs) && op.properties.markDefs.length > 0) {
2858
- const [block, blockPath] = Editor.node(editor, [op.path[0]]);
2859
- if (editor.isTextBlock(block) && block.children.length === 1 && block.markDefs && block.markDefs.length > 0 && Text.isText(block.children[0]) && block.children[0].text === "" && (!block.children[0].marks || block.children[0].marks.length === 0)) {
2860
- Transforms.setNodes(editor, { markDefs: [] }, { at: blockPath });
2861
- return;
2862
- }
2834
+ }
2835
+ if (editor.isTextSpan(node)) {
2836
+ const blockPath = Path.parent(path), [block] = Editor.node(editor, blockPath);
2837
+ if (editor.isTextBlock(block)) {
2838
+ const decorators2 = types.decorators.map((decorator) => decorator.value), marks = node.marks ?? [], orphanedAnnotations = marks.filter((mark) => !decorators2.includes(mark) && !block.markDefs?.find((def) => def._key === mark));
2839
+ if (orphanedAnnotations.length > 0) {
2840
+ debug$c("Removing orphaned annotations from span node"), Transforms.setNodes(
2841
+ editor,
2842
+ { marks: marks.filter((mark) => !orphanedAnnotations.includes(mark)) },
2843
+ { at: path }
2844
+ );
2845
+ return;
2863
2846
  }
2864
2847
  }
2865
2848
  }
@@ -2953,6 +2936,14 @@ function createWithPortableTextMarkModel(types, change$, keyGenerator) {
2953
2936
  }
2954
2937
  }
2955
2938
  }
2939
+ if (op.type === "merge_node" && op.path.length === 1 && "markDefs" in op.properties && op.properties._type === types.block.name && Array.isArray(op.properties.markDefs) && op.properties.markDefs.length > 0 && op.path[0] - 1 >= 0) {
2940
+ const [targetBlock, targetPath] = Editor.node(editor, [op.path[0] - 1]);
2941
+ if (editor.isTextBlock(targetBlock)) {
2942
+ const oldDefs = Array.isArray(targetBlock.markDefs) && targetBlock.markDefs || [], newMarkDefs = uniq([...oldDefs, ...op.properties.markDefs]);
2943
+ debug$c("Copying markDefs over to merged block", op), Transforms.setNodes(editor, { markDefs: newMarkDefs }, { at: targetPath, voids: !1 }), apply2(op);
2944
+ return;
2945
+ }
2946
+ }
2956
2947
  apply2(op);
2957
2948
  }, editor.addMark = (mark) => {
2958
2949
  if (editor.selection) {