diff --git a/packages/ketcher-react/src/script/editor/tool/helper/dropAndMerge.ts b/packages/ketcher-react/src/script/editor/tool/helper/dropAndMerge.ts index 00f6fd0fc2..416c321b45 100644 --- a/packages/ketcher-react/src/script/editor/tool/helper/dropAndMerge.ts +++ b/packages/ketcher-react/src/script/editor/tool/helper/dropAndMerge.ts @@ -91,32 +91,29 @@ function extractAttachmentAtom(mergeItems: MergeItems, editor: Editor) { atoms: new Map(mergeItems.atoms), bonds: new Map(mergeItems.bonds) } - const removeNonAttachmentAtoms = new Action() - const removeSGroup = new Action() + + const action = new Action() mergeItems.atomToFunctionalGroup?.forEach((functionalGroupId, srcAtomId) => { const sGroup = struct.sgroups.get(functionalGroupId) - const attachmentAtomId = sGroup?.getAttAtomId(struct) - if (attachmentAtomId) { - const attachmentAtom = struct.atoms.get(attachmentAtomId) - if (SGroup.isAtomInContractedSGroup(attachmentAtom, struct.sgroups)) { - return - } - newMergeItems.atoms.set(srcAtomId, attachmentAtomId) + const attachmentAtomId = sGroup?.getAttAtomId(struct) - removeNonAttachmentAtoms.mergeWith( + if (attachmentAtomId !== undefined) { + const atomsToDelete = [...SGroup.getAtoms(struct, sGroup)].filter( + (atomId) => atomId !== attachmentAtomId + ) + const bondsToDelete = [...SGroup.getBonds(struct, sGroup)] + action.mergeWith(fromSgroupDeletion(reStruct, functionalGroupId)) + action.mergeWith( fromFragmentDeletion(reStruct, { - atoms: [...SGroup.getAtoms(struct, sGroup)].filter( - (atomId) => atomId !== attachmentAtomId - ), - bonds: [...SGroup.getBonds(struct, sGroup)] + atoms: atomsToDelete, + bonds: bondsToDelete }) ) - - removeSGroup.mergeWith(fromSgroupDeletion(reStruct, functionalGroupId)) + newMergeItems.atoms.set(srcAtomId, attachmentAtomId) } }) - return [newMergeItems, removeNonAttachmentAtoms] as const + return [newMergeItems, action] as const }