@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.js CHANGED
@@ -106,7 +106,7 @@ function normalizeSelection(selection, value) {
106
106
  const { anchor, focus } = selection;
107
107
  return anchor && value.find((blk) => isEqual__default.default({ _key: blk._key }, anchor.path[0])) && (newAnchor = normalizePoint(anchor, value)), focus && value.find((blk) => isEqual__default.default({ _key: blk._key }, focus.path[0])) && (newFocus = normalizePoint(focus, value)), newAnchor && newFocus ? { anchor: newAnchor, focus: newFocus, backward: selection.backward } : null;
108
108
  }
109
- const EMPTY_MARKDEFS = [], EMPTY_MARKS$1 = [], VOID_CHILD_KEY = "void-child";
109
+ const EMPTY_MARKDEFS = [], VOID_CHILD_KEY = "void-child";
110
110
  function keepObjectEquality(object, keyMap) {
111
111
  const value = keyMap[object._key];
112
112
  return value && isEqual__default.default(object, value) ? value : (keyMap[object._key] = object, object);
@@ -1152,9 +1152,10 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1152
1152
  const [block] = slate.Editor.node(editor, originalSelection.focus, { depth: 1 });
1153
1153
  if (!editor.isTextBlock(block))
1154
1154
  return;
1155
- slate.Range.isCollapsed(originalSelection) && (editor.pteExpandToWord(), editor.onChange());
1156
1155
  const [textNode] = slate.Editor.node(editor, originalSelection.focus, { depth: 2 });
1157
- editor.selection && (slate.Editor.withoutNormalizing(editor, () => {
1156
+ if (!types.isPortableTextSpan(textNode) || textNode.text === "")
1157
+ return;
1158
+ slate.Range.isCollapsed(originalSelection) && (editor.pteExpandToWord(), editor.onChange()), editor.selection && (slate.Editor.withoutNormalizing(editor, () => {
1158
1159
  const annotationKey = keyGenerator();
1159
1160
  slate.Transforms.setNodes(
1160
1161
  editor,
@@ -2770,7 +2771,7 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2770
2771
  change$.next({ type: "selection", selection: ptRange });
2771
2772
  };
2772
2773
  return editor.normalizeNode = (nodeEntry) => {
2773
- const [node, path] = nodeEntry, isSpan = slate.Text.isText(node) && node._type === types2.span.name, isTextBlock = editor.isTextBlock(node);
2774
+ const [node, path] = nodeEntry;
2774
2775
  if (editor.isTextBlock(node)) {
2775
2776
  const children = slate.Node.children(editor, path);
2776
2777
  for (const [child, childPath] of children) {
@@ -2785,65 +2786,47 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2785
2786
  }
2786
2787
  }
2787
2788
  }
2788
- if (isSpan || isTextBlock) {
2789
- if (isSpan && !Array.isArray(node.marks)) {
2790
- debug$c("Adding .marks to span node"), slate.Transforms.setNodes(editor, { marks: [] }, { at: path });
2791
- return;
2792
- }
2793
- if (isSpan && (node.marks || []).length > 0) {
2794
- const spanMarks = node.marks || EMPTY_MARKS$1, annotationMarks = spanMarks.filter(
2795
- (mark) => !types2.decorators.map((dec) => dec.value).includes(mark)
2789
+ if (editor.isTextSpan(node) && !Array.isArray(node.marks)) {
2790
+ debug$c("Adding .marks to span node"), slate.Transforms.setNodes(editor, { marks: [] }, { at: path });
2791
+ return;
2792
+ }
2793
+ if (editor.isTextSpan(node)) {
2794
+ const blockPath = slate.Path.parent(path), [block] = slate.Editor.node(editor, blockPath), decorators2 = types2.decorators.map((decorator) => decorator.value), annotations = node.marks?.filter((mark) => !decorators2.includes(mark));
2795
+ if (editor.isTextBlock(block) && node.text === "" && annotations && annotations.length > 0) {
2796
+ debug$c("Removing annotations from empty span node"), slate.Transforms.setNodes(
2797
+ editor,
2798
+ { marks: node.marks?.filter((mark) => decorators2.includes(mark)) },
2799
+ { at: path }
2796
2800
  );
2797
- if (annotationMarks.length > 0) {
2798
- const [block] = slate.Editor.node(editor, slate.Path.parent(path)), orphanedMarks = editor.isTextBlock(block) && annotationMarks.filter(
2799
- (mark) => !block.markDefs?.find((def) => def._key === mark)
2800
- ) || [];
2801
- if (orphanedMarks.length > 0) {
2802
- debug$c("Removing orphaned .marks from span node"), slate.Transforms.setNodes(
2803
- editor,
2804
- { marks: spanMarks.filter((mark) => !orphanedMarks.includes(mark)) },
2805
- { at: path }
2806
- );
2807
- return;
2808
- }
2809
- }
2801
+ return;
2810
2802
  }
2811
- for (const op of editor.operations) {
2812
- if (op.type === "merge_node" && op.path.length === 1 && "markDefs" in op.properties && op.properties._type === types2.block.name && Array.isArray(op.properties.markDefs) && op.properties.markDefs.length > 0 && op.path[0] - 1 >= 0) {
2813
- const [targetBlock, targetPath] = slate.Editor.node(editor, [op.path[0] - 1]);
2814
- if (debug$c("Copying markDefs over to merged block", op), editor.isTextBlock(targetBlock)) {
2815
- const oldDefs = Array.isArray(targetBlock.markDefs) && targetBlock.markDefs || [], newMarkDefs = uniq__default.default([...oldDefs, ...op.properties.markDefs]);
2816
- if (!isEqual__default.default(newMarkDefs, targetBlock.markDefs)) {
2817
- slate.Transforms.setNodes(editor, { markDefs: newMarkDefs }, { at: targetPath, voids: !1 });
2818
- return;
2819
- }
2820
- }
2821
- }
2822
- if (op.type === "split_node" && op.path.length === 1 && slate.Element.isElementProps(op.properties) && op.properties._type === types2.block.name && "markDefs" in op.properties && Array.isArray(op.properties.markDefs) && op.properties.markDefs.length > 0 && op.path[0] + 1 < editor.children.length) {
2823
- const [targetBlock, targetPath] = slate.Editor.node(editor, [op.path[0] + 1]);
2824
- if (debug$c("Copying markDefs over to split block", op), editor.isTextBlock(targetBlock)) {
2825
- const oldDefs = Array.isArray(targetBlock.markDefs) && targetBlock.markDefs || [];
2826
- slate.Transforms.setNodes(
2803
+ }
2804
+ if (editor.isTextBlock(node)) {
2805
+ const decorators2 = types2.decorators.map((decorator) => decorator.value);
2806
+ for (const [child, childPath] of slate.Node.children(editor, path))
2807
+ if (editor.isTextSpan(child)) {
2808
+ const marks = child.marks ?? [], orphanedAnnotations = marks.filter((mark) => !decorators2.includes(mark) && !node.markDefs?.find((def) => def._key === mark));
2809
+ if (orphanedAnnotations.length > 0) {
2810
+ debug$c("Removing orphaned annotations from span node"), slate.Transforms.setNodes(
2827
2811
  editor,
2828
- { markDefs: uniq__default.default([...oldDefs, ...op.properties.markDefs]) },
2829
- { at: targetPath, voids: !1 }
2812
+ { marks: marks.filter((mark) => !orphanedAnnotations.includes(mark)) },
2813
+ { at: childPath }
2830
2814
  );
2831
2815
  return;
2832
2816
  }
2833
2817
  }
2834
- if (op.type === "split_node" && op.path.length === 2 && op.properties._type === types2.span.name && "marks" in op.properties && Array.isArray(op.properties.marks) && op.properties.marks.length > 0 && op.path[0] + 1 < editor.children.length) {
2835
- const [child, childPath] = slate.Editor.node(editor, [op.path[0] + 1, 0]);
2836
- if (slate.Text.isText(child) && child.text === "" && Array.isArray(child.marks) && child.marks.length > 0) {
2837
- slate.Transforms.setNodes(editor, { marks: [] }, { at: childPath, voids: !1 });
2838
- return;
2839
- }
2840
- }
2841
- if (op.type === "split_node" && op.path.length === 1 && op.properties._type === types2.block.name && "markDefs" in op.properties && Array.isArray(op.properties.markDefs) && op.properties.markDefs.length > 0) {
2842
- const [block, blockPath] = slate.Editor.node(editor, [op.path[0]]);
2843
- if (editor.isTextBlock(block) && block.children.length === 1 && block.markDefs && block.markDefs.length > 0 && slate.Text.isText(block.children[0]) && block.children[0].text === "" && (!block.children[0].marks || block.children[0].marks.length === 0)) {
2844
- slate.Transforms.setNodes(editor, { markDefs: [] }, { at: blockPath });
2845
- return;
2846
- }
2818
+ }
2819
+ if (editor.isTextSpan(node)) {
2820
+ const blockPath = slate.Path.parent(path), [block] = slate.Editor.node(editor, blockPath);
2821
+ if (editor.isTextBlock(block)) {
2822
+ const decorators2 = types2.decorators.map((decorator) => decorator.value), marks = node.marks ?? [], orphanedAnnotations = marks.filter((mark) => !decorators2.includes(mark) && !block.markDefs?.find((def) => def._key === mark));
2823
+ if (orphanedAnnotations.length > 0) {
2824
+ debug$c("Removing orphaned annotations from span node"), slate.Transforms.setNodes(
2825
+ editor,
2826
+ { marks: marks.filter((mark) => !orphanedAnnotations.includes(mark)) },
2827
+ { at: path }
2828
+ );
2829
+ return;
2847
2830
  }
2848
2831
  }
2849
2832
  }
@@ -2937,6 +2920,14 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2937
2920
  }
2938
2921
  }
2939
2922
  }
2923
+ if (op.type === "merge_node" && op.path.length === 1 && "markDefs" in op.properties && op.properties._type === types2.block.name && Array.isArray(op.properties.markDefs) && op.properties.markDefs.length > 0 && op.path[0] - 1 >= 0) {
2924
+ const [targetBlock, targetPath] = slate.Editor.node(editor, [op.path[0] - 1]);
2925
+ if (editor.isTextBlock(targetBlock)) {
2926
+ const oldDefs = Array.isArray(targetBlock.markDefs) && targetBlock.markDefs || [], newMarkDefs = uniq__default.default([...oldDefs, ...op.properties.markDefs]);
2927
+ debug$c("Copying markDefs over to merged block", op), slate.Transforms.setNodes(editor, { markDefs: newMarkDefs }, { at: targetPath, voids: !1 }), apply2(op);
2928
+ return;
2929
+ }
2930
+ }
2940
2931
  apply2(op);
2941
2932
  }, editor.addMark = (mark) => {
2942
2933
  if (editor.selection) {