@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.js CHANGED
@@ -2762,19 +2762,27 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2762
2762
  change$.next({ type: "selection", selection: ptRange });
2763
2763
  };
2764
2764
  return editor.normalizeNode = (nodeEntry) => {
2765
- normalizeNode(nodeEntry), editor.operations.some(
2766
- (op) => [
2767
- "insert_node",
2768
- "insert_text",
2769
- "merge_node",
2770
- "remove_node",
2771
- "remove_text",
2772
- "set_node"
2773
- ].includes(op.type)
2774
- ) && mergeSpans(editor);
2775
2765
  const [node, path] = nodeEntry, isSpan = slate.Text.isText(node) && node._type === types2.span.name, isTextBlock = editor.isTextBlock(node);
2766
+ if (editor.isTextBlock(node)) {
2767
+ const children = slate.Node.children(editor, path);
2768
+ for (const [child, childPath] of children) {
2769
+ const nextNode = node.children[childPath[1] + 1];
2770
+ if (editor.isTextSpan(child) && editor.isTextSpan(nextNode) && isEqual__default.default(child.marks, nextNode.marks)) {
2771
+ debug$c(
2772
+ "Merging spans",
2773
+ JSON.stringify(child, null, 2),
2774
+ JSON.stringify(nextNode, null, 2)
2775
+ ), slate.Transforms.mergeNodes(editor, { at: [childPath[0], childPath[1] + 1], voids: !0 });
2776
+ return;
2777
+ }
2778
+ }
2779
+ }
2776
2780
  if (isSpan || isTextBlock) {
2777
- if (isSpan && !Array.isArray(node.marks) && (debug$c("Adding .marks to span node"), slate.Transforms.setNodes(editor, { marks: [] }, { at: path }), editor.onChange()), isSpan && (node.marks || []).length > 0) {
2781
+ if (isSpan && !Array.isArray(node.marks)) {
2782
+ debug$c("Adding .marks to span node"), slate.Transforms.setNodes(editor, { marks: [] }, { at: path });
2783
+ return;
2784
+ }
2785
+ if (isSpan && (node.marks || []).length > 0) {
2778
2786
  const spanMarks = node.marks || EMPTY_MARKS$1, annotationMarks = spanMarks.filter(
2779
2787
  (mark) => !types2.decorators.map((dec) => dec.value).includes(mark)
2780
2788
  );
@@ -2782,11 +2790,14 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2782
2790
  const [block] = slate.Editor.node(editor, slate.Path.parent(path)), orphanedMarks = editor.isTextBlock(block) && annotationMarks.filter(
2783
2791
  (mark) => !block.markDefs?.find((def) => def._key === mark)
2784
2792
  ) || [];
2785
- orphanedMarks.length > 0 && (debug$c("Removing orphaned .marks from span node"), slate.Transforms.setNodes(
2786
- editor,
2787
- { marks: spanMarks.filter((mark) => !orphanedMarks.includes(mark)) },
2788
- { at: path }
2789
- ), editor.onChange());
2793
+ if (orphanedMarks.length > 0) {
2794
+ debug$c("Removing orphaned .marks from span node"), slate.Transforms.setNodes(
2795
+ editor,
2796
+ { marks: spanMarks.filter((mark) => !orphanedMarks.includes(mark)) },
2797
+ { at: path }
2798
+ );
2799
+ return;
2800
+ }
2790
2801
  }
2791
2802
  }
2792
2803
  for (const op of editor.operations) {
@@ -2794,7 +2805,10 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2794
2805
  const [targetBlock, targetPath] = slate.Editor.node(editor, [op.path[0] - 1]);
2795
2806
  if (debug$c("Copying markDefs over to merged block", op), editor.isTextBlock(targetBlock)) {
2796
2807
  const oldDefs = Array.isArray(targetBlock.markDefs) && targetBlock.markDefs || [], newMarkDefs = uniq__default.default([...oldDefs, ...op.properties.markDefs]);
2797
- isEqual__default.default(newMarkDefs, targetBlock.markDefs) || (slate.Transforms.setNodes(editor, { markDefs: newMarkDefs }, { at: targetPath, voids: !1 }), editor.onChange());
2808
+ if (!isEqual__default.default(newMarkDefs, targetBlock.markDefs)) {
2809
+ slate.Transforms.setNodes(editor, { markDefs: newMarkDefs }, { at: targetPath, voids: !1 });
2810
+ return;
2811
+ }
2798
2812
  }
2799
2813
  }
2800
2814
  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) {
@@ -2805,32 +2819,46 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2805
2819
  editor,
2806
2820
  { markDefs: uniq__default.default([...oldDefs, ...op.properties.markDefs]) },
2807
2821
  { at: targetPath, voids: !1 }
2808
- ), editor.onChange();
2822
+ );
2823
+ return;
2809
2824
  }
2810
2825
  }
2811
2826
  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) {
2812
2827
  const [child, childPath] = slate.Editor.node(editor, [op.path[0] + 1, 0]);
2813
- slate.Text.isText(child) && child.text === "" && Array.isArray(child.marks) && child.marks.length > 0 && (slate.Transforms.setNodes(editor, { marks: [] }, { at: childPath, voids: !1 }), editor.onChange());
2828
+ if (slate.Text.isText(child) && child.text === "" && Array.isArray(child.marks) && child.marks.length > 0) {
2829
+ slate.Transforms.setNodes(editor, { marks: [] }, { at: childPath, voids: !1 });
2830
+ return;
2831
+ }
2814
2832
  }
2815
2833
  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) {
2816
2834
  const [block, blockPath] = slate.Editor.node(editor, [op.path[0]]);
2817
- 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) && (slate.Transforms.setNodes(editor, { markDefs: [] }, { at: blockPath }), editor.onChange());
2835
+ 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)) {
2836
+ slate.Transforms.setNodes(editor, { markDefs: [] }, { at: blockPath });
2837
+ return;
2838
+ }
2818
2839
  }
2819
2840
  }
2820
- isSpan && Array.isArray(node.marks) && (!node.marks || node.marks.length > 0 && node.text === "") && (slate.Transforms.setNodes(editor, { marks: [] }, { at: path, voids: !1 }), editor.onChange());
2841
+ if (isSpan && Array.isArray(node.marks) && (!node.marks || node.marks.length > 0 && node.text === "")) {
2842
+ slate.Transforms.setNodes(editor, { marks: [] }, { at: path, voids: !1 });
2843
+ return;
2844
+ }
2821
2845
  }
2822
2846
  if (editor.isTextBlock(node) && !editor.operations.some(
2823
2847
  (op) => op.type === "merge_node" && "markDefs" in op.properties && op.path.length === 1
2824
2848
  )) {
2825
2849
  const newMarkDefs = (node.markDefs || []).filter((def) => node.children.find((child) => slate.Text.isText(child) && Array.isArray(child.marks) && child.marks.includes(def._key)));
2826
- node.markDefs && !isEqual__default.default(newMarkDefs, node.markDefs) && (debug$c("Removing markDef not in use"), slate.Transforms.setNodes(
2827
- editor,
2828
- {
2829
- markDefs: newMarkDefs
2830
- },
2831
- { at: path }
2832
- ), editor.onChange());
2850
+ if (node.markDefs && !isEqual__default.default(newMarkDefs, node.markDefs)) {
2851
+ debug$c("Removing markDef not in use"), slate.Transforms.setNodes(
2852
+ editor,
2853
+ {
2854
+ markDefs: newMarkDefs
2855
+ },
2856
+ { at: path }
2857
+ );
2858
+ return;
2859
+ }
2833
2860
  }
2861
+ normalizeNode(nodeEntry);
2834
2862
  }, editor.apply = (op) => {
2835
2863
  if (isChangingRemotely(editor)) {
2836
2864
  apply2(op);
@@ -2902,15 +2930,11 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2902
2930
  apply2(op);
2903
2931
  }, editor.addMark = (mark) => {
2904
2932
  if (editor.selection) {
2905
- if (slate.Range.isExpanded(editor.selection)) {
2906
- slate.Transforms.setNodes(editor, {}, { match: slate.Text.isText, split: !0 });
2907
- const splitTextNodes = [
2908
- ...slate.Editor.nodes(editor, { at: editor.selection, match: slate.Text.isText })
2909
- ];
2910
- if (splitTextNodes.every((node) => node[0].marks?.includes(mark)))
2911
- return editor.removeMark(mark), editor;
2933
+ if (slate.Range.isExpanded(editor.selection))
2912
2934
  slate.Editor.withoutNormalizing(editor, () => {
2913
- splitTextNodes.forEach(([node, path]) => {
2935
+ slate.Transforms.setNodes(editor, {}, { match: slate.Text.isText, split: !0 });
2936
+ const splitTextNodes = slate.Range.isRange(editor.selection) ? [...slate.Editor.nodes(editor, { at: editor.selection, match: slate.Text.isText })] : [];
2937
+ splitTextNodes.length > 1 && splitTextNodes.every((node) => node[0].marks?.includes(mark)) ? editor.removeMark(mark) : splitTextNodes.forEach(([node, path]) => {
2914
2938
  const marks = [
2915
2939
  ...(Array.isArray(node.marks) ? node.marks : []).filter(
2916
2940
  (eMark) => eMark !== mark
@@ -2923,8 +2947,8 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2923
2947
  { at: path, match: slate.Text.isText, split: !0, hanging: !0 }
2924
2948
  );
2925
2949
  });
2926
- }), slate.Editor.normalize(editor);
2927
- } else {
2950
+ });
2951
+ else {
2928
2952
  const existingMarks = {
2929
2953
  ...slate.Editor.marks(editor) || {}
2930
2954
  }.marks || [], marks = {
@@ -2985,25 +3009,6 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2985
3009
  editor.pteIsMarkActive(mark) ? (debug$c(`Remove mark '${mark}'`), slate.Editor.removeMark(editor, mark)) : (debug$c(`Add mark '${mark}'`), slate.Editor.addMark(editor, mark, !0));
2986
3010
  }, editor;
2987
3011
  };
2988
- function mergeSpans(editor) {
2989
- const { selection } = editor;
2990
- if (selection) {
2991
- const textNodesInSelection = Array.from(
2992
- slate.Editor.nodes(editor, {
2993
- at: slate.Editor.range(editor, [selection.anchor.path[0]], [selection.focus.path[0]]),
2994
- match: slate.Text.isText,
2995
- reverse: !0
2996
- })
2997
- );
2998
- for (const [node, path] of textNodesInSelection) {
2999
- const [parent] = path.length > 1 ? slate.Editor.node(editor, slate.Path.parent(path)) : [void 0], nextPath = [path[0], path[1] + 1];
3000
- if (editor.isTextBlock(parent)) {
3001
- const nextNode = parent.children[nextPath[1]];
3002
- slate.Text.isText(nextNode) && isEqual__default.default(nextNode.marks, node.marks) && (debug$c("Merging spans"), slate.Transforms.mergeNodes(editor, { at: nextPath, voids: !0 }), editor.onChange());
3003
- }
3004
- }
3005
- }
3006
- }
3007
3012
  }
3008
3013
  const debug$b = debugWithName("plugin:withPortableTextSelections"), debugVerbose$2 = debug$b.enabled && !1;
3009
3014
  function createWithPortableTextSelections(change$, types2) {