Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "#2257 - Unable add a bond to a Function Group" #2319

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions packages/ketcher-core/src/application/editor/actions/bond.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ import { atomForNewBond, atomGetAttr } from './utils'
import {
fromAtomMerge,
fromStereoAtomAttrs,
mergeFragmentsIfNeeded
mergeFragmentsIfNeeded,
mergeSgroups
} from './atom'

import { Action } from './action'
Expand Down Expand Up @@ -80,8 +81,7 @@ export function fromBondAddition(
begin.fragment = frid
begin = (action.addOp(new AtomAdd(begin, pos).perform(restruct)) as AtomAdd)
.data.aid
if (typeof end === 'number')
mergeFragmentsIfNeeded(action, restruct, begin, end)
if (typeof end === 'number') mergeSgroups(action, restruct, [begin], end)
pos = pos2
} else if (atomGetAttr(restruct, begin, 'label') === '*') {
action.addOp(new AtomAttr(begin, 'label', 'C').perform(restruct))
Expand All @@ -92,8 +92,7 @@ export function fromBondAddition(
// TODO: <op>.data.aid here is a hack, need a better way to access the id of a created atom
end = (action.addOp(new AtomAdd(end, pos).perform(restruct)) as AtomAdd)
.data.aid
if (typeof begin === 'number')
mergeFragmentsIfNeeded(action, restruct, end, begin)
if (typeof begin === 'number') mergeSgroups(action, restruct, [end], begin)
} else if (atomGetAttr(restruct, end, 'label') === '*') {
action.addOp(new AtomAttr(end, 'label', 'C').perform(restruct))
}
Expand Down
14 changes: 2 additions & 12 deletions packages/ketcher-react/src/script/editor/tool/bond.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ class BondTool {
const struct = this.editor.render.ctab
const molecule = struct.molecule
const functionalGroups = molecule.functionalGroups
const itemsToFind = ['atoms', 'bonds', 'functionalGroups']
const ci = this.editor.findItem(event, itemsToFind)
const ci = this.editor.findItem(event, ['atoms', 'bonds'])
const atomResult: Array<number> = []
const bondResult: Array<number> = []
const result: Array<number> = []
Expand Down Expand Up @@ -108,7 +107,7 @@ class BondTool {
this.editor.selection(null)
this.dragCtx = {
xy0: rnd.page2obj(event),
item: ci
item: this.editor.findItem(event, ['atoms', 'bonds'])
}
if (!this.dragCtx.item)
// ci.type == 'Canvas'
Expand Down Expand Up @@ -317,15 +316,6 @@ class BondTool {
this.editor.update(
bondChangingAction(rnd.ctab, dragCtx.item.id, bond, bondProps)
)
} else if (dragCtx.item.map === 'functionalGroups') {
const groupId = dragCtx.item.id
const group = struct.sgroups.get(groupId)
const attAtomId = group?.getAttAtomId(struct)

this.editor.update(
fromBondAddition(rnd.ctab, this.bondProps, attAtomId, undefined)[0]
)
delete this.dragCtx.existedBond
}
delete this.dragCtx
}
Expand Down
32 changes: 5 additions & 27 deletions packages/ketcher-react/src/script/editor/tool/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import {
fromItemsFuse,
getHoverToFuse,
getItemsToFuse,
FunctionalGroup,
Struct
FunctionalGroup
} from 'ketcher-core'

import { atomLongtapEvent } from './atom'
Expand All @@ -45,9 +44,7 @@ class ChainTool {
const molecule = struct.molecule
const functionalGroups = molecule.functionalGroups
const rnd = this.editor.render

const itemsToFind = ['atoms', 'bonds', 'functionalGroups']
const ci = this.editor.findItem(event, itemsToFind)
const ci = this.editor.findItem(event, ['atoms', 'bonds'])
const atomResult: Array<number> = []
const bondResult: Array<number> = []
const result: Array<number> = []
Expand Down Expand Up @@ -138,19 +135,14 @@ class ChainTool {

editor.selection(null)

if (
!dragCtx.item ||
['atoms', 'functionalGroups'].includes(dragCtx.item.map)
) {
if (!dragCtx.item || dragCtx.item.map === 'atoms') {
if (dragCtx.action) {
dragCtx.action.perform(restruct)
}

const atomId = getTargetAtomId(dragCtx.item, this.editor.struct())

const atoms = restruct.molecule.atoms

const pos0 = atomId !== null ? atoms.get(atomId)?.pp : dragCtx.xy0
const pos0 = dragCtx.item ? atoms.get(dragCtx.item.id)?.pp : dragCtx.xy0

const pos1 = editor.render.page2obj(event)
const sectCount = Math.ceil(Vec2.diff(pos1, pos0).length())
Expand All @@ -164,7 +156,7 @@ class ChainTool {
pos0,
angle,
sectCount,
atomId !== null ? atomId : null
dragCtx.item ? dragCtx.item.id : null
)

editor.event.message.dispatch({
Expand Down Expand Up @@ -269,18 +261,4 @@ class ChainTool {
}
}

function getTargetAtomId(
closestItem: { id: number; map: string },
struct: Struct
): number | null {
const { id, map } = closestItem
if (map === 'atoms') return id
if (map === 'functionalGroups') {
const sgroup = struct.sgroups.get(id)
const atomId = sgroup?.getAttAtomId(struct)
return atomId !== undefined ? atomId : null
}
return null
}

export default ChainTool