@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.esm.js CHANGED
@@ -870,24 +870,38 @@ function createOperationToPatches(types) {
870
870
  return [unset([{ _key: block._key }])];
871
871
  throw new Error("Block not found");
872
872
  } else if (editor.isTextBlock(block) && operation.path.length === 2) {
873
- const spanToRemove = editor.isTextBlock(block) && block.children && block.children[operation.path[1]];
874
- return spanToRemove ? [unset([{ _key: block._key }, "children", { _key: spanToRemove._key }])] : (debug$k("Span not found in editor trying to remove node"), []);
873
+ const spanToRemove = block.children[operation.path[1]];
874
+ return spanToRemove ? block.children.filter((span) => span._key === operation.node._key).length > 1 ? (console.warn(
875
+ `Multiple spans have \`_key\` ${operation.node._key}. It's ambiguous which one to remove.`,
876
+ JSON.stringify(block, null, 2)
877
+ ), []) : [unset([{ _key: block._key }, "children", { _key: spanToRemove._key }])] : (debug$k("Span not found in editor trying to remove node"), []);
875
878
  } else
876
879
  return debug$k("Not creating patch inside object block"), [];
877
880
  }
878
881
  function mergeNodePatch(editor, operation, beforeValue) {
879
- const patches = [], block = beforeValue[operation.path[0]], targetBlock = editor.children[operation.path[0]];
882
+ const patches = [], block = beforeValue[operation.path[0]], updatedBlock = editor.children[operation.path[0]];
880
883
  if (operation.path.length === 1)
881
884
  if (block?._key) {
882
885
  const newBlock = fromSlateValue([editor.children[operation.path[0] - 1]], textBlockName)[0];
883
886
  patches.push(set(newBlock, [{ _key: newBlock._key }])), patches.push(unset([{ _key: block._key }]));
884
887
  } else
885
888
  throw new Error("Target key not found!");
886
- else if (operation.path.length === 2 && editor.isTextBlock(targetBlock)) {
887
- const mergedSpan = editor.isTextBlock(block) && block.children[operation.path[1]] || void 0, targetSpan = targetBlock.children[operation.path[1] - 1];
888
- editor.isTextSpan(targetSpan) && (patches.push(
889
- set(targetSpan.text, [{ _key: block._key }, "children", { _key: targetSpan._key }, "text"])
890
- ), mergedSpan && patches.push(unset([{ _key: block._key }, "children", { _key: mergedSpan._key }])));
889
+ else if (editor.isTextBlock(block) && editor.isTextBlock(updatedBlock) && operation.path.length === 2) {
890
+ 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;
891
+ updatedSpan && (block.children.filter((span) => span._key === updatedSpan._key).length === 1 ? patches.push(
892
+ set(updatedSpan.text, [
893
+ { _key: block._key },
894
+ "children",
895
+ { _key: updatedSpan._key },
896
+ "text"
897
+ ])
898
+ ) : console.warn(
899
+ `Multiple spans have \`_key\` ${updatedSpan._key}. It's ambiguous which one to update.`,
900
+ JSON.stringify(block, null, 2)
901
+ )), removedSpan && (block.children.filter((span) => span._key === removedSpan._key).length === 1 ? patches.push(unset([{ _key: block._key }, "children", { _key: removedSpan._key }])) : console.warn(
902
+ `Multiple spans have \`_key\` ${removedSpan._key}. It's ambiguous which one to remove.`,
903
+ JSON.stringify(block, null, 2)
904
+ ));
891
905
  } else
892
906
  debug$k("Void nodes can't be merged, not creating any patches");
893
907
  return patches;
@@ -1176,13 +1190,7 @@ function createWithEditableAPI(portableTextEditor, types, keyGenerator) {
1176
1190
  at: editor.selection,
1177
1191
  match: (n) => n._type === types.span.name
1178
1192
  }
1179
- ), editor.onChange(), editor.selection && Transforms.insertNodes(
1180
- editor,
1181
- [{ _type: "span", text: "", marks: [], _key: keyGenerator() }],
1182
- {
1183
- at: Range.end(editor.selection)
1184
- }
1185
- );
1193
+ ), editor.onChange();
1186
1194
  const newPortableTextEditorSelection = toPortableTextRange(
1187
1195
  fromSlateValue(editor.children, types.block.name, KEY_TO_VALUE_ELEMENT.get(editor)),
1188
1196
  editor.selection,