Skip to content

Commit

Permalink
#2420 - hotkey del can not delete functional groups and salts abbrevi…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
Maxim Novikov authored and Maxim Novikov committed Apr 12, 2023
1 parent c3fa228 commit 6af9092
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
33 changes: 20 additions & 13 deletions packages/ketcher-core/src/application/editor/actions/erase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { removeAtomFromSgroupIfNeeded, removeSgroupIfNeeded } from './sgroup'

import { Action } from './action'
import assert from 'assert'
import { atomGetDegree } from './utils'
import { atomGetDegree, formatSelection } from './utils'
import { fromBondStereoUpdate } from '../actions/bond'
import { fromFragmentSplit } from './fragment'

Expand Down Expand Up @@ -84,23 +84,30 @@ export function fromOneBondDeletion(restruct, id) {
return action
}

export function fromFragmentDeletion(restruct, selection) {
assert(!!selection != null)
export function fromFragmentDeletion(restruct, rawSelection) {
assert(!!rawSelection != null)

let action = new Action()
const atomsToRemove: Array<number> = []
const frids: Array<number> = []

selection = {
// TODO: refactor me
atoms: selection.atoms || [],
bonds: selection.bonds || [],
rxnPluses: selection.rxnPluses || [],
rxnArrows: selection.rxnArrows || [],
sgroupData: selection.sgroupData || [],
simpleObjects: selection.simpleObjects || [],
texts: selection.texts || []
}
const selection = formatSelection(rawSelection)

selection.sgroups.forEach((sgroupId) => {
const sgroup = restruct.sgroups.get(sgroupId)
const sgroupAtoms = sgroup.item.atoms

selection.atoms = selection.atoms.concat(sgroupAtoms) as any[]

Object.values(restruct.molecule.bonds).forEach((bond: any, bid) => {
if (
sgroupAtoms.indexOf(bond.begin) >= 0 &&
sgroupAtoms.indexOf(bond.end) >= 0
) {
selection.bonds.push(bid)
}
})
})

selection.atoms.forEach((aid) => {
restruct.molecule.atomGetNeighbors(aid).forEach((nei) => {
Expand Down
21 changes: 10 additions & 11 deletions packages/ketcher-core/src/application/editor/actions/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Bond, Vec2 } from 'domain/entities'

import closest from '../shared/closest'
import { difference } from 'lodash'
import { selectionKeys } from '../shared/constants'

export function atomGetAttr(restruct, aid, name) {
return restruct.molecule.atoms.get(aid)[name]
Expand All @@ -44,22 +45,20 @@ export function findStereoAtoms(struct, aids: number[] | undefined): number[] {
}

export function structSelection(struct) {
return [
'atoms',
'bonds',
'frags',
'sgroups',
'rgroups',
'rxnArrows',
'rxnPluses',
'simpleObjects',
'texts'
].reduce((res, key) => {
return selectionKeys.reduce((res, key) => {
res[key] = Array.from(struct[key].keys())
return res
}, {})
}

export function formatSelection(selection): any {
return selectionKeys.reduce((res, key) => {
res[key] = selection[key] || []

return res
}, {})
}

// Get new atom id/label and pos for bond being added to existing atom
export function atomForNewBond(restruct, id, bond?) {
// eslint-disable-line max-statements
Expand Down
12 changes: 12 additions & 0 deletions packages/ketcher-core/src/application/editor/shared/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,15 @@ export const SgContexts = {
Atom: 'Atom',
Group: 'Group'
}

export const selectionKeys = [
'atoms',
'bonds',
'frags',
'sgroups',
'rgroups',
'rxnArrows',
'rxnPluses',
'simpleObjects',
'texts'
]

0 comments on commit 6af9092

Please sign in to comment.