@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.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,42 @@ 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());
2821
2841
  }
2822
2842
  if (editor.isTextBlock(node) && !editor.operations.some(
2823
2843
  (op) => op.type === "merge_node" && "markDefs" in op.properties && op.path.length === 1
2824
2844
  )) {
2825
2845
  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());
2846
+ if (node.markDefs && !isEqual__default.default(newMarkDefs, node.markDefs)) {
2847
+ debug$c("Removing markDef not in use"), slate.Transforms.setNodes(
2848
+ editor,
2849
+ {
2850
+ markDefs: newMarkDefs
2851
+ },
2852
+ { at: path }
2853
+ );
2854
+ return;
2855
+ }
2833
2856
  }
2857
+ normalizeNode(nodeEntry);
2834
2858
  }, editor.apply = (op) => {
2835
2859
  if (isChangingRemotely(editor)) {
2836
2860
  apply2(op);
@@ -2897,20 +2921,22 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2897
2921
  }), editor.onChange();
2898
2922
  return;
2899
2923
  }
2924
+ if (node.marks !== void 0 && node.marks.length > 0 && deletingAllText) {
2925
+ slate.Editor.withoutNormalizing(editor, () => {
2926
+ apply2(op), slate.Transforms.setNodes(editor, { marks: [] }, { at: op.path });
2927
+ }), editor.onChange();
2928
+ return;
2929
+ }
2900
2930
  }
2901
2931
  }
2902
2932
  apply2(op);
2903
2933
  }, editor.addMark = (mark) => {
2904
2934
  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;
2935
+ if (slate.Range.isExpanded(editor.selection))
2912
2936
  slate.Editor.withoutNormalizing(editor, () => {
2913
- splitTextNodes.forEach(([node, path]) => {
2937
+ slate.Transforms.setNodes(editor, {}, { match: slate.Text.isText, split: !0 });
2938
+ const splitTextNodes = slate.Range.isRange(editor.selection) ? [...slate.Editor.nodes(editor, { at: editor.selection, match: slate.Text.isText })] : [];
2939
+ splitTextNodes.length > 1 && splitTextNodes.every((node) => node[0].marks?.includes(mark)) ? editor.removeMark(mark) : splitTextNodes.forEach(([node, path]) => {
2914
2940
  const marks = [
2915
2941
  ...(Array.isArray(node.marks) ? node.marks : []).filter(
2916
2942
  (eMark) => eMark !== mark
@@ -2923,8 +2949,8 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2923
2949
  { at: path, match: slate.Text.isText, split: !0, hanging: !0 }
2924
2950
  );
2925
2951
  });
2926
- }), slate.Editor.normalize(editor);
2927
- } else {
2952
+ });
2953
+ else {
2928
2954
  const existingMarks = {
2929
2955
  ...slate.Editor.marks(editor) || {}
2930
2956
  }.marks || [], marks = {
@@ -2985,25 +3011,6 @@ function createWithPortableTextMarkModel(types2, change$, keyGenerator) {
2985
3011
  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
3012
  }, editor;
2987
3013
  };
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
3014
  }
3008
3015
  const debug$b = debugWithName("plugin:withPortableTextSelections"), debugVerbose$2 = debug$b.enabled && !1;
3009
3016
  function createWithPortableTextSelections(change$, types2) {