Skip to content

Commit

Permalink
Fix salts and solvents merging with other structures by pasting
Browse files Browse the repository at this point in the history
  • Loading branch information
yuleicul committed Jan 18, 2023
1 parent 86dea5e commit 829fbf2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,21 @@
* limitations under the License.
***************************************************************************/

import { ReSGroup } from 'application/render/restruct'
import { SGroup } from 'domain/entities'
import closest from '../shared/closest'
import utils from '../shared/utils'
import { Action } from './action'
import { fromAtomMerge } from './atom'
import { fromBondsMerge } from './bond'
import utils from '../shared/utils'
import { PasteItems } from './paste'

export function fromItemsFuse(restruct, items) {
type MergeMap = {
atoms: Map<number, number>
bonds: Map<number, number>
}

export function fromItemsFuse(restruct, items: MergeMap | null) {
let action = new Action()

if (!items) return action
Expand All @@ -40,18 +49,22 @@ export function fromItemsFuse(restruct, items) {
return action
}

export function getItemsToFuse(editor, items) {
export function getItemsToFuse(editor, items: PasteItems) {
const struct = editor.render.ctab.molecule

const mergeItems = items || {
atoms: Array.from(struct.atoms.keys()),
bonds: Array.from(struct.bonds.keys())
}

return closestToMerge(
struct,
editor.findMerge(mergeItems, ['atoms', 'bonds'])
)
const closestMap = closest.merge(
editor.render.ctab,
mergeItems,
['atoms', 'bonds'],
editor.render.options
) as MergeMap

return closestToMerge(editor, closestMap)
}

export function getHoverToFuse(items) {
Expand All @@ -65,19 +78,11 @@ export function getHoverToFuse(items) {
return { map: 'merge', id: +Date.now(), items: hoverItems }
}

/**
* @param struct
* @param closestMap {{
* atoms: Map<number, number>,
* bonds: Map<number, number>
* }}
* @return {{
* atoms: Map<number, number>,
* bonds: Map<number, number>
* }}
*/
function closestToMerge(struct, closestMap) {
const mergeMap = {
function closestToMerge(editor, closestMap: MergeMap) {
const struct = editor.render.ctab.molecule
const sgroups = editor.render.ctab.sgroups

let mergeMap: MergeMap = {
atoms: new Map(closestMap.atoms),
bonds: new Map(closestMap.bonds)
}
Expand All @@ -94,7 +99,41 @@ function closestToMerge(struct, closestMap) {
}
})

mergeMap = omitAtomsAndBondsOfSaltsAndSolventsFromMergeMap(mergeMap, sgroups)

if (mergeMap.atoms.size === 0 && mergeMap.bonds.size === 0) return null

return mergeMap
}

function omitAtomsAndBondsOfSaltsAndSolventsFromMergeMap(
mergeMap: MergeMap,
sgroups: Map<number, ReSGroup>
) {
const sgroupsOnCanvas = Array.from(sgroups.values()).map(({ item }) => item)

const newMergeMap: MergeMap = {
atoms: new Map<number, number>(),
bonds: new Map<number, number>()
}

mergeMap.atoms.forEach((destinationAtomId, sourceAtomId) => {
if (
!SGroup.isAtomInSaltOrSolvent(destinationAtomId, sgroupsOnCanvas) &&
!SGroup.isAtomInSaltOrSolvent(sourceAtomId, sgroupsOnCanvas)
) {
newMergeMap.atoms.set(destinationAtomId, sourceAtomId)
}
})

mergeMap.bonds.forEach((destinationBondId, sourceBondId) => {
if (
!SGroup.isBondInSaltOrSolvent(destinationBondId, sgroupsOnCanvas) &&
!SGroup.isBondInSaltOrSolvent(sourceBondId, sgroupsOnCanvas)
) {
newMergeMap.bonds.set(destinationBondId, sourceBondId)
}
})

return newMergeMap
}
9 changes: 7 additions & 2 deletions packages/ketcher-core/src/application/editor/actions/paste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ import { Action } from './action'
import { Vec2 } from 'domain/entities'
import { fromSgroupAddition } from './sgroup'

export type PasteItems = {
atoms: number[]
bonds: number[]
}

export function fromPaste(restruct, pstruct, point, angle = 0) {
const xy0 = getStructCenter(pstruct)
const offset = Vec2.diff(point, xy0)
Expand All @@ -41,7 +46,7 @@ export function fromPaste(restruct, pstruct, point, angle = 0) {
const aidMap = new Map()
const fridMap = new Map()

const pasteItems: any = {
const pasteItems: PasteItems = {
// only atoms and bonds now
atoms: [],
bonds: []
Expand Down Expand Up @@ -158,7 +163,7 @@ export function fromPaste(restruct, pstruct, point, angle = 0) {
})

action.operations.reverse()
return [action, pasteItems]
return [action, pasteItems] as const
}

function getStructCenter(struct) {
Expand Down

0 comments on commit 829fbf2

Please sign in to comment.