From 397d99eed137647c2107df208a4cd66b921d356b Mon Sep 17 00:00:00 2001 From: Nikita_Vozisov Date: Thu, 8 Dec 2022 00:00:46 +0300 Subject: [PATCH] =?UTF-8?q?=20=20#1820=20=E2=80=93=20fixed=20dragging=20st?= =?UTF-8?q?ructure=20on=20salt=20or=20solvent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/domain/entities/sgroup.ts | 24 ++++++++++++++++ .../src/script/editor/tool/select.ts | 28 +++++++++++++++++-- 2 files changed, 50 insertions(+), 2 deletions(-) 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) {