@portabletext/editor 1.0.14 → 1.0.15

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,46 @@ 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());
2857
+ if (isSpan && Array.isArray(node.marks) && (!node.marks || node.marks.length > 0 && node.text === "")) {
2858
+ Transforms.setNodes(editor, { marks: [] }, { at: path, voids: !1 });
2859
+ return;
2860
+ }
2837
2861
  }
2838
2862
  if (editor.isTextBlock(node) && !editor.operations.some(
2839
2863
  (op) => op.type === "merge_node" && "markDefs" in op.properties && op.path.length === 1
2840
2864
  )) {
2841
2865
  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());
2866
+ if (node.markDefs && !isEqual(newMarkDefs, node.markDefs)) {
2867
+ debug$c("Removing markDef not in use"), Transforms.setNodes(
2868
+ editor,
2869
+ {
2870
+ markDefs: newMarkDefs
2871
+ },
2872
+ { at: path }
2873
+ );
2874
+ return;
2875
+ }
2849
2876
  }
2877
+ normalizeNode(nodeEntry);
2850
2878
  }, editor.apply = (op) => {
2851
2879
  if (isChangingRemotely(editor)) {
2852
2880
  apply2(op);
@@ -2918,15 +2946,11 @@ function createWithPortableTextMarkModel(types, change$, keyGenerator) {
2918
2946
  apply2(op);
2919
2947
  }, editor.addMark = (mark) => {
2920
2948
  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;
2949
+ if (Range.isExpanded(editor.selection))
2928
2950
  Editor.withoutNormalizing(editor, () => {
2929
- splitTextNodes.forEach(([node, path]) => {
2951
+ Transforms.setNodes(editor, {}, { match: Text.isText, split: !0 });
2952
+ const splitTextNodes = Range.isRange(editor.selection) ? [...Editor.nodes(editor, { at: editor.selection, match: Text.isText })] : [];
2953
+ splitTextNodes.length > 1 && splitTextNodes.every((node) => node[0].marks?.includes(mark)) ? editor.removeMark(mark) : splitTextNodes.forEach(([node, path]) => {
2930
2954
  const marks = [
2931
2955
  ...(Array.isArray(node.marks) ? node.marks : []).filter(
2932
2956
  (eMark) => eMark !== mark
@@ -2939,8 +2963,8 @@ function createWithPortableTextMarkModel(types, change$, keyGenerator) {
2939
2963
  { at: path, match: Text.isText, split: !0, hanging: !0 }
2940
2964
  );
2941
2965
  });
2942
- }), Editor.normalize(editor);
2943
- } else {
2966
+ });
2967
+ else {
2944
2968
  const existingMarks = {
2945
2969
  ...Editor.marks(editor) || {}
2946
2970
  }.marks || [], marks = {
@@ -3001,25 +3025,6 @@ function createWithPortableTextMarkModel(types, change$, keyGenerator) {
3001
3025
  editor.pteIsMarkActive(mark) ? (debug$c(`Remove mark '${mark}'`), Editor.removeMark(editor, mark)) : (debug$c(`Add mark '${mark}'`), Editor.addMark(editor, mark, !0));
3002
3026
  }, editor;
3003
3027
  };
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
3028
  }
3024
3029
  const debug$b = debugWithName("plugin:withPortableTextSelections"), debugVerbose$2 = debug$b.enabled && !1;
3025
3030
  function createWithPortableTextSelections(change$, types) {