@portabletext/editor 1.0.14 → 1.0.16

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
@@ -2778,19 +2778,27 @@ function createWithPortableTextMarkModel(types, change$, keyGenerator) {
2778
2778
  change$.next({ type: "selection", selection: ptRange });
2779
2779
  };
2780
2780
  return editor.normalizeNode = (nodeEntry) => {
2781
- normalizeNode(nodeEntry), editor.operations.some(
2782
- (op) => [
2783
- "insert_node",
2784
- "insert_text",
2785
- "merge_node",
2786
- "remove_node",
2787
- "remove_text",
2788
- "set_node"
2789
- ].includes(op.type)
2790
- ) && mergeSpans(editor);
2791
2781
  const [node, path] = nodeEntry, isSpan = Text.isText(node) && node._type === types.span.name, isTextBlock = editor.isTextBlock(node);
2782
+ if (editor.isTextBlock(node)) {
2783
+ const children = Node.children(editor, path);
2784
+ for (const [child, childPath] of children) {
2785
+ const nextNode = node.children[childPath[1] + 1];
2786
+ if (editor.isTextSpan(child) && editor.isTextSpan(nextNode) && isEqual(child.marks, nextNode.marks)) {
2787
+ debug$c(
2788
+ "Merging spans",
2789
+ JSON.stringify(child, null, 2),
2790
+ JSON.stringify(nextNode, null, 2)
2791
+ ), Transforms.mergeNodes(editor, { at: [childPath[0], childPath[1] + 1], voids: !0 });
2792
+ return;
2793
+ }
2794
+ }
2795
+ }
2792
2796
  if (isSpan || isTextBlock) {
2793
- if (isSpan && !Array.isArray(node.marks) && (debug$c("Adding .marks to span node"), Transforms.setNodes(editor, { marks: [] }, { at: path }), editor.onChange()), isSpan && (node.marks || []).length > 0) {
2797
+ if (isSpan && !Array.isArray(node.marks)) {
2798
+ debug$c("Adding .marks to span node"), Transforms.setNodes(editor, { marks: [] }, { at: path });
2799
+ return;
2800
+ }
2801
+ if (isSpan && (node.marks || []).length > 0) {
2794
2802
  const spanMarks = node.marks || EMPTY_MARKS$1, annotationMarks = spanMarks.filter(
2795
2803
  (mark) => !types.decorators.map((dec) => dec.value).includes(mark)
2796
2804
  );
@@ -2798,11 +2806,14 @@ function createWithPortableTextMarkModel(types, change$, keyGenerator) {
2798
2806
  const [block] = Editor.node(editor, Path.parent(path)), orphanedMarks = editor.isTextBlock(block) && annotationMarks.filter(
2799
2807
  (mark) => !block.markDefs?.find((def) => def._key === mark)
2800
2808
  ) || [];
2801
- orphanedMarks.length > 0 && (debug$c("Removing orphaned .marks from span node"), Transforms.setNodes(
2802
- editor,
2803
- { marks: spanMarks.filter((mark) => !orphanedMarks.includes(mark)) },
2804
- { at: path }
2805
- ), editor.onChange());
2809
+ if (orphanedMarks.length > 0) {
2810
+ debug$c("Removing orphaned .marks from span node"), Transforms.setNodes(
2811
+ editor,
2812
+ { marks: spanMarks.filter((mark) => !orphanedMarks.includes(mark)) },
2813
+ { at: path }
2814
+ );
2815
+ return;
2816
+ }
2806
2817
  }
2807
2818
  }
2808
2819
  for (const op of editor.operations) {
@@ -2810,7 +2821,10 @@ function createWithPortableTextMarkModel(types, change$, keyGenerator) {
2810
2821
  const [targetBlock, targetPath] = Editor.node(editor, [op.path[0] - 1]);
2811
2822
  if (debug$c("Copying markDefs over to merged block", op), editor.isTextBlock(targetBlock)) {
2812
2823
  const oldDefs = Array.isArray(targetBlock.markDefs) && targetBlock.markDefs || [], newMarkDefs = uniq([...oldDefs, ...op.properties.markDefs]);
2813
- isEqual(newMarkDefs, targetBlock.markDefs) || (Transforms.setNodes(editor, { markDefs: newMarkDefs }, { at: targetPath, voids: !1 }), editor.onChange());
2824
+ if (!isEqual(newMarkDefs, targetBlock.markDefs)) {
2825
+ Transforms.setNodes(editor, { markDefs: newMarkDefs }, { at: targetPath, voids: !1 });
2826
+ return;
2827
+ }
2814
2828
  }
2815
2829
  }
2816
2830
  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) {
@@ -2821,32 +2835,42 @@ function createWithPortableTextMarkModel(types, change$, keyGenerator) {
2821
2835
  editor,
2822
2836
  { markDefs: uniq([...oldDefs, ...op.properties.markDefs]) },
2823
2837
  { at: targetPath, voids: !1 }
2824
- ), editor.onChange();
2838
+ );
2839
+ return;
2825
2840
  }
2826
2841
  }
2827
2842
  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) {
2828
2843
  const [child, childPath] = Editor.node(editor, [op.path[0] + 1, 0]);
2829
- Text.isText(child) && child.text === "" && Array.isArray(child.marks) && child.marks.length > 0 && (Transforms.setNodes(editor, { marks: [] }, { at: childPath, voids: !1 }), editor.onChange());
2844
+ if (Text.isText(child) && child.text === "" && Array.isArray(child.marks) && child.marks.length > 0) {
2845
+ Transforms.setNodes(editor, { marks: [] }, { at: childPath, voids: !1 });
2846
+ return;
2847
+ }
2830
2848
  }
2831
2849
  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) {
2832
2850
  const [block, blockPath] = Editor.node(editor, [op.path[0]]);
2833
- 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) && (Transforms.setNodes(editor, { markDefs: [] }, { at: blockPath }), editor.onChange());
2851
+ 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)) {
2852
+ Transforms.setNodes(editor, { markDefs: [] }, { at: blockPath });
2853
+ return;
2854
+ }
2834
2855
  }
2835
2856
  }
2836
- isSpan && Array.isArray(node.marks) && (!node.marks || node.marks.length > 0 && node.text === "") && (Transforms.setNodes(editor, { marks: [] }, { at: path, voids: !1 }), editor.onChange());
2837
2857
  }
2838
2858
  if (editor.isTextBlock(node) && !editor.operations.some(
2839
2859
  (op) => op.type === "merge_node" && "markDefs" in op.properties && op.path.length === 1
2840
2860
  )) {
2841
2861
  const newMarkDefs = (node.markDefs || []).filter((def) => node.children.find((child) => Text.isText(child) && Array.isArray(child.marks) && child.marks.includes(def._key)));
2842
- node.markDefs && !isEqual(newMarkDefs, node.markDefs) && (debug$c("Removing markDef not in use"), Transforms.setNodes(
2843
- editor,
2844
- {
2845
- markDefs: newMarkDefs
2846
- },
2847
- { at: path }
2848
- ), editor.onChange());
2862
+ if (node.markDefs && !isEqual(newMarkDefs, node.markDefs)) {
2863
+ debug$c("Removing markDef not in use"), Transforms.setNodes(
2864
+ editor,
2865
+ {
2866
+ markDefs: newMarkDefs
2867
+ },
2868
+ { at: path }
2869
+ );
2870
+ return;
2871
+ }
2849
2872
  }
2873
+ normalizeNode(nodeEntry);
2850
2874
  }, editor.apply = (op) => {
2851
2875
  if (isChangingRemotely(editor)) {
2852
2876
  apply2(op);
@@ -2913,20 +2937,22 @@ function createWithPortableTextMarkModel(types, change$, keyGenerator) {
2913
2937
  }), editor.onChange();
2914
2938
  return;
2915
2939
  }
2940
+ if (node.marks !== void 0 && node.marks.length > 0 && deletingAllText) {
2941
+ Editor.withoutNormalizing(editor, () => {
2942
+ apply2(op), Transforms.setNodes(editor, { marks: [] }, { at: op.path });
2943
+ }), editor.onChange();
2944
+ return;
2945
+ }
2916
2946
  }
2917
2947
  }
2918
2948
  apply2(op);
2919
2949
  }, editor.addMark = (mark) => {
2920
2950
  if (editor.selection) {
2921
- if (Range.isExpanded(editor.selection)) {
2922
- Transforms.setNodes(editor, {}, { match: Text.isText, split: !0 });
2923
- const splitTextNodes = [
2924
- ...Editor.nodes(editor, { at: editor.selection, match: Text.isText })
2925
- ];
2926
- if (splitTextNodes.every((node) => node[0].marks?.includes(mark)))
2927
- return editor.removeMark(mark), editor;
2951
+ if (Range.isExpanded(editor.selection))
2928
2952
  Editor.withoutNormalizing(editor, () => {
2929
- splitTextNodes.forEach(([node, path]) => {
2953
+ Transforms.setNodes(editor, {}, { match: Text.isText, split: !0 });
2954
+ const splitTextNodes = Range.isRange(editor.selection) ? [...Editor.nodes(editor, { at: editor.selection, match: Text.isText })] : [];
2955
+ splitTextNodes.length > 1 && splitTextNodes.every((node) => node[0].marks?.includes(mark)) ? editor.removeMark(mark) : splitTextNodes.forEach(([node, path]) => {
2930
2956
  const marks = [
2931
2957
  ...(Array.isArray(node.marks) ? node.marks : []).filter(
2932
2958
  (eMark) => eMark !== mark
@@ -2939,8 +2965,8 @@ function createWithPortableTextMarkModel(types, change$, keyGenerator) {
2939
2965
  { at: path, match: Text.isText, split: !0, hanging: !0 }
2940
2966
  );
2941
2967
  });
2942
- }), Editor.normalize(editor);
2943
- } else {
2968
+ });
2969
+ else {
2944
2970
  const existingMarks = {
2945
2971
  ...Editor.marks(editor) || {}
2946
2972
  }.marks || [], marks = {
@@ -3001,25 +3027,6 @@ function createWithPortableTextMarkModel(types, change$, keyGenerator) {
3001
3027
  editor.pteIsMarkActive(mark) ? (debug$c(`Remove mark '${mark}'`), Editor.removeMark(editor, mark)) : (debug$c(`Add mark '${mark}'`), Editor.addMark(editor, mark, !0));
3002
3028
  }, editor;
3003
3029
  };
3004
- function mergeSpans(editor) {
3005
- const { selection } = editor;
3006
- if (selection) {
3007
- const textNodesInSelection = Array.from(
3008
- Editor.nodes(editor, {
3009
- at: Editor.range(editor, [selection.anchor.path[0]], [selection.focus.path[0]]),
3010
- match: Text.isText,
3011
- reverse: !0
3012
- })
3013
- );
3014
- for (const [node, path] of textNodesInSelection) {
3015
- const [parent] = path.length > 1 ? Editor.node(editor, Path.parent(path)) : [void 0], nextPath = [path[0], path[1] + 1];
3016
- if (editor.isTextBlock(parent)) {
3017
- const nextNode = parent.children[nextPath[1]];
3018
- Text.isText(nextNode) && isEqual(nextNode.marks, node.marks) && (debug$c("Merging spans"), Transforms.mergeNodes(editor, { at: nextPath, voids: !0 }), editor.onChange());
3019
- }
3020
- }
3021
- }
3022
- }
3023
3030
  }
3024
3031
  const debug$b = debugWithName("plugin:withPortableTextSelections"), debugVerbose$2 = debug$b.enabled && !1;
3025
3032
  function createWithPortableTextSelections(change$, types) {