Skip to content

Commit

Permalink
#2423 fix hotkey for functional groups
Browse files Browse the repository at this point in the history
  • Loading branch information
AnastasiiaPlyako committed May 8, 2023
1 parent 08c5f80 commit f12f007
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
6 changes: 6 additions & 0 deletions packages/ketcher-core/src/domain/entities/sgroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,12 @@ export class SGroup {
return !expanded && isSGroup
}

static getAtomsSGroupWithoutAttachmentPoint(sGroup: SGroup, struct: Struct) {
const atomsSGroup = SGroup.getAtoms(struct, sGroup)
const attachmentPointId = sGroup?.getAttAtomId(struct)
return atomsSGroup.filter((atomId) => atomId !== attachmentPointId)
}

/**
* @returns `undefined`: if it's salt or solvent
*/
Expand Down
47 changes: 39 additions & 8 deletions packages/ketcher-react/src/script/editor/tool/atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {

import Editor from '../Editor'
import utils from '../shared/utils'
import { getGroupIdsFromItemArrays } from './helper/getGroupIdsFromItems'

class AtomTool {
editor: Editor
Expand All @@ -48,18 +49,48 @@ class AtomTool {
this.editor.hoverIcon.fill = ElementColor[atomProps.label] ?? '#000000'
this.editor.hoverIcon.updatePosition()

if (editor.selection()) {
if (editor.selection()?.atoms) {
const action = fromAtomsAttrs(
editor.render.ctab,
editor.selection().atoms,
atomProps,
true
const selected = editor.selection()
const selectedAtoms = selected?.atoms

if (selected) {
if (selectedAtoms) {
const action = new Action()
const struct = editor.render.ctab
const atomsInSGroups: number[] = []
const selectedSGroupsId =
selected && getGroupIdsFromItemArrays(struct.molecule, selected)
if (selectedSGroupsId?.length) {
const sgroups = struct.sgroups
selectedSGroupsId.forEach((sGroupId) => {
if (SGroup.isContractedSGroup(sGroupId, sgroups)) {
const sGroupItem = sgroups.get(sGroupId).item
const atomsWithoutAttachmentPoint =
SGroup.getAtomsSGroupWithoutAttachmentPoint(sGroupItem, struct)
atomsInSGroups.push(...atomsWithoutAttachmentPoint)
action.mergeWith(fromSgroupDeletion(struct, sGroupId))
action.mergeWith(
fromFragmentDeletion(struct, {
atoms: atomsWithoutAttachmentPoint,
bonds: SGroup.getBonds(struct, sGroupItem)
})
)
}
})
}
action.mergeWith(
fromAtomsAttrs(
struct,
selectedAtoms.filter(
(selectAtomId) => !atomsInSGroups.includes(selectAtomId)
),
atomProps,
true
)
)
editor.update(action)
editor.selection(null)
this.editor.hoverIcon.hide()
}

this.isNotActiveTool = true
}
}
Expand Down

0 comments on commit f12f007

Please sign in to comment.