diff --git a/packages/ketcher-core/src/domain/entities/sgroup.ts b/packages/ketcher-core/src/domain/entities/sgroup.ts index f85b7192fe..171cf00f9d 100644 --- a/packages/ketcher-core/src/domain/entities/sgroup.ts +++ b/packages/ketcher-core/src/domain/entities/sgroup.ts @@ -223,6 +223,30 @@ export class SGroup { return saltsAndSolvents.some(({ name }) => name === moleculeName) } + static isAtomInSaltOrSolvent( + atomId: number, + sgroupsOnCanvas: SGroup[] + ): boolean { + const onlySaltsOrSolvents = sgroupsOnCanvas.filter((sgroup) => + this.isSaltOrSolvent(sgroup.data.name) + ) + return onlySaltsOrSolvents.some(({ atoms }) => + atoms.some((atomIdInSaltOrSolvent) => atomIdInSaltOrSolvent === atomId) + ) + } + + static isBondInSaltOrSolvent( + bondId: number, + sgroupsOnCanvas: SGroup[] + ): boolean { + const onlySaltsOrSolvents = sgroupsOnCanvas.filter((sgroup) => + this.isSaltOrSolvent(sgroup.data.name) + ) + return onlySaltsOrSolvents.some(({ bonds }) => + bonds.some((bondIdInSaltOrSolvent) => bondIdInSaltOrSolvent === bondId) + ) + } + static filterAtoms(atoms: any, map: any) { const newAtoms: Array = [] for (let i = 0; i < atoms.length; ++i) { diff --git a/packages/ketcher-react/src/script/editor/tool/select.ts b/packages/ketcher-react/src/script/editor/tool/select.ts index eb9bb57cf1..067b0d3ac6 100644 --- a/packages/ketcher-react/src/script/editor/tool/select.ts +++ b/packages/ketcher-react/src/script/editor/tool/select.ts @@ -28,7 +28,8 @@ import { FunctionalGroup, fromSimpleObjectResizing, fromArrowResizing, - ReStruct + ReStruct, + ReSGroup } from 'ketcher-core' import LassoHelper from './helper/lasso' @@ -375,8 +376,12 @@ class SelectTool { if (dragCtx && dragCtx.stopTapping) dragCtx.stopTapping() const possibleSaltOrSolvent = struct.sgroups.get(actualSgroupId) + const isDraggingSaltOrSolventOnStructure = SGroup.isSaltOrSolvent( + possibleSaltOrSolvent?.item.data.name + ) if ( - SGroup.isSaltOrSolvent(possibleSaltOrSolvent?.item.data.name) && + (isDraggingSaltOrSolventOnStructure || + this.isDraggingStructureOnSaltOrSolvent(dragCtx, struct.sgroups)) && dragCtx ) { preventSaltAndSolventsMerge(struct, dragCtx, editor) @@ -586,6 +591,25 @@ class SelectTool { !event.shiftKey ? sel : selMerge(sel, editor.selection(), false) ) } + + isDraggingStructureOnSaltOrSolvent(dragCtx, sgroups: Map) { + let isDraggingOnSaltOrSolventAtom + let isDraggingOnSaltOrSolventBond + if (dragCtx?.mergeItems) { + const mergeAtoms = Array.from(dragCtx.mergeItems.atoms.values()) + const mergeBonds = Array.from(dragCtx.mergeItems.bonds.values()) + const sgroupsOnCanvas = Array.from(sgroups.values()).map( + ({ item }) => item + ) + isDraggingOnSaltOrSolventAtom = mergeAtoms.some((atomId) => + SGroup.isAtomInSaltOrSolvent(atomId as number, sgroupsOnCanvas) + ) + isDraggingOnSaltOrSolventBond = mergeBonds.some((bondId) => + SGroup.isBondInSaltOrSolvent(bondId as number, sgroupsOnCanvas) + ) + } + return isDraggingOnSaltOrSolventAtom || isDraggingOnSaltOrSolventBond + } } function closestToSel(ci) {