Skip to content

Commit

Permalink
Backmerge: #2517 File with Superatom opens without part of structure (#…
Browse files Browse the repository at this point in the history
…2567)

* #2517 fix bug with disappearing part of structure

* #2517 move expand logic outside "fromPaste"

* #2517 fix comment
  • Loading branch information
AnastasiiaPlyako committed May 3, 2023
1 parent 5bd0ce6 commit 1a90da3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
9 changes: 9 additions & 0 deletions packages/ketcher-core/src/domain/entities/functionalGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,15 @@ export class FunctionalGroup {
return false
}

static getAttachmentPointCount(sGroup, molecule): number {
return sGroup.atoms?.reduce((count, currentAtom) => {
if (FunctionalGroup.isAttachmentPointAtom(currentAtom, molecule)) {
count++
}
return count
}, 0)
}

static isFirstAtomInFunctionalGroup(sgroups, aid): boolean {
for (const sg of sgroups.values()) {
if (FunctionalGroup.isFunctionalGroup(sg) && aid === sg.atoms[0]) {
Expand Down
28 changes: 28 additions & 0 deletions packages/ketcher-react/src/script/editor/tool/paste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import {
fromItemsFuse,
fromPaste,
fromTemplateOnAtom,
FunctionalGroup,
getHoverToFuse,
getItemsToFuse,
setExpandSGroup,
SGroup,
Struct,
Vec2
Expand Down Expand Up @@ -60,6 +62,22 @@ class PasteTool {
this.action = action
this.editor.update(this.action, true)

// todo delete after supporting expand - collapse for 2 attachment points
struct.sgroups.forEach((sgroup) => {
const countAttachmentPoint = FunctionalGroup.getAttachmentPointCount(
sgroup,
this.editor.render.ctab.molecule
)
if (countAttachmentPoint > 1) {
action.mergeWith(
setExpandSGroup(this.editor.render.ctab, sgroup.id, {
expanded: true
})
)
}
})
this.editor.update(this.action, true)

this.findItems = ['functionalGroups']
this.mergeItems = getItemsToFuse(this.editor, pasteItems)
this.editor.hover(getHoverToFuse(this.mergeItems), this)
Expand Down Expand Up @@ -155,6 +173,16 @@ class PasteTool {
this.dragCtx.action = action
this.editor.update(this.dragCtx.action, true)
} else {
// todo delete after supporting expand - collapse for 2 attachment points
this.struct.sgroups.forEach((sgroup) => {
const countAttachmentPoint = FunctionalGroup.getAttachmentPointCount(
sgroup,
this.struct
)
if (countAttachmentPoint > 1) {
sgroup.setAttr('expanded', true)
}
})
// common paste logic
const [action, pasteItems] = fromPaste(
this.editor.render.ctab,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Action, setExpandSGroup } from 'ketcher-core'
import { Action, FunctionalGroup, setExpandSGroup } from 'ketcher-core'
import { useCallback } from 'react'
import { useDispatch } from 'react-redux'
import { useAppContext } from 'src/hooks'
Expand Down Expand Up @@ -44,8 +44,21 @@ const useFunctionalGroupEoc = () => {
},
[]
)
const disabled = useCallback(({ props }: ItemEventParams) => {
const editor = getKetcherInstance().editor as Editor
const molecule = editor.render.ctab.molecule
return Boolean(
props?.functionalGroups?.every(
(functionalGroup) =>
FunctionalGroup.getAttachmentPointCount(
functionalGroup?.relatedSGroup,
molecule
) > 1
)
)
}, [])

return [handler, hidden] as const
return [handler, hidden, disabled] as const
}

export default useFunctionalGroupEoc
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@ import useFunctionalGroupEoc from '../hooks/useFunctionalGroupEoc'
import useFunctionalGroupRemove from '../hooks/useFunctionalGroupRemove'

const FunctionalGroupMenuItems: React.FC = (props) => {
const [handleExpandOrContract, ExpandOrContractHidden] =
useFunctionalGroupEoc()
const [
handleExpandOrContract,
ExpandOrContractHidden,
ExpandOrContractDisabled
] = useFunctionalGroupEoc()
const handleRemove = useFunctionalGroupRemove()

return (
<>
<Item
{...props}
disabled={(params) => ExpandOrContractDisabled(params)}
hidden={(params) => ExpandOrContractHidden(params, true)}
onClick={(params) => handleExpandOrContract(params, true)}
>
Expand Abbreviation
</Item>
<Item
{...props}
disabled={(params) => ExpandOrContractDisabled(params)}
hidden={(params) => ExpandOrContractHidden(params, false)}
onClick={(params) => handleExpandOrContract(params, false)}
>
Expand Down

0 comments on commit 1a90da3

Please sign in to comment.