@portabletext/editor 1.0.16 → 1.0.18

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
@@ -854,24 +854,38 @@ function createOperationToPatches(types2) {
854
854
  return [patches.unset([{ _key: block._key }])];
855
855
  throw new Error("Block not found");
856
856
  } else if (editor.isTextBlock(block) && operation.path.length === 2) {
857
- const spanToRemove = editor.isTextBlock(block) && block.children && block.children[operation.path[1]];
858
- return spanToRemove ? [patches.unset([{ _key: block._key }, "children", { _key: spanToRemove._key }])] : (debug$k("Span not found in editor trying to remove node"), []);
857
+ const spanToRemove = block.children[operation.path[1]];
858
+ return spanToRemove ? block.children.filter((span) => span._key === operation.node._key).length > 1 ? (console.warn(
859
+ `Multiple spans have \`_key\` ${operation.node._key}. It's ambiguous which one to remove.`,
860
+ JSON.stringify(block, null, 2)
861
+ ), []) : [patches.unset([{ _key: block._key }, "children", { _key: spanToRemove._key }])] : (debug$k("Span not found in editor trying to remove node"), []);
859
862
  } else
860
863
  return debug$k("Not creating patch inside object block"), [];
861
864
  }
862
865
  function mergeNodePatch(editor, operation, beforeValue) {
863
- const patches$1 = [], block = beforeValue[operation.path[0]], targetBlock = editor.children[operation.path[0]];
866
+ const patches$1 = [], block = beforeValue[operation.path[0]], updatedBlock = editor.children[operation.path[0]];
864
867
  if (operation.path.length === 1)
865
868
  if (block?._key) {
866
869
  const newBlock = fromSlateValue([editor.children[operation.path[0] - 1]], textBlockName)[0];
867
870
  patches$1.push(patches.set(newBlock, [{ _key: newBlock._key }])), patches$1.push(patches.unset([{ _key: block._key }]));
868
871
  } else
869
872
  throw new Error("Target key not found!");
870
- else if (operation.path.length === 2 && editor.isTextBlock(targetBlock)) {
871
- const mergedSpan = editor.isTextBlock(block) && block.children[operation.path[1]] || void 0, targetSpan = targetBlock.children[operation.path[1] - 1];
872
- editor.isTextSpan(targetSpan) && (patches$1.push(
873
- patches.set(targetSpan.text, [{ _key: block._key }, "children", { _key: targetSpan._key }, "text"])
874
- ), mergedSpan && patches$1.push(patches.unset([{ _key: block._key }, "children", { _key: mergedSpan._key }])));
873
+ else if (editor.isTextBlock(block) && editor.isTextBlock(updatedBlock) && operation.path.length === 2) {
874
+ const updatedSpan = updatedBlock.children[operation.path[1] - 1] && editor.isTextSpan(updatedBlock.children[operation.path[1] - 1]) ? updatedBlock.children[operation.path[1] - 1] : void 0, removedSpan = block.children[operation.path[1]] && editor.isTextSpan(block.children[operation.path[1]]) ? block.children[operation.path[1]] : void 0;
875
+ updatedSpan && (block.children.filter((span) => span._key === updatedSpan._key).length === 1 ? patches$1.push(
876
+ patches.set(updatedSpan.text, [
877
+ { _key: block._key },
878
+ "children",
879
+ { _key: updatedSpan._key },
880
+ "text"
881
+ ])
882
+ ) : console.warn(
883
+ `Multiple spans have \`_key\` ${updatedSpan._key}. It's ambiguous which one to update.`,
884
+ JSON.stringify(block, null, 2)
885
+ )), removedSpan && (block.children.filter((span) => span._key === removedSpan._key).length === 1 ? patches$1.push(patches.unset([{ _key: block._key }, "children", { _key: removedSpan._key }])) : console.warn(
886
+ `Multiple spans have \`_key\` ${removedSpan._key}. It's ambiguous which one to remove.`,
887
+ JSON.stringify(block, null, 2)
888
+ ));
875
889
  } else
876
890
  debug$k("Void nodes can't be merged, not creating any patches");
877
891
  return patches$1;
@@ -1160,13 +1174,7 @@ function createWithEditableAPI(portableTextEditor, types$1, keyGenerator) {
1160
1174
  at: editor.selection,
1161
1175
  match: (n) => n._type === types$1.span.name
1162
1176
  }
1163
- ), editor.onChange(), editor.selection && slate.Transforms.insertNodes(
1164
- editor,
1165
- [{ _type: "span", text: "", marks: [], _key: keyGenerator() }],
1166
- {
1167
- at: slate.Range.end(editor.selection)
1168
- }
1169
- );
1177
+ ), editor.onChange();
1170
1178
  const newPortableTextEditorSelection = toPortableTextRange(
1171
1179
  fromSlateValue(editor.children, types$1.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
1172
1180
  editor.selection,