-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add right-click menu for atoms (#1981)
* Add new dependency react-contexify * Add new right-click menu for bonds Resolves: #1872 * Fix stylelint and typo * Divide context menu items into two components * Change error handler to do nothing * Remove the word `bond` out of context submenu item * Extract common util functions to `utils.ts` * Rename `ci` to `closestItem` * Fix context menu not hiding when click left/bottom toolbar * Make context menu id more readable * Simplify usage of `fromBondsAttrs` * Add right click menu for atoms Depends on: #1896 Resolves: #1899 * Add atom single/batch operations * Update logic in bond operations (more details in PR description) * Make `changeAtomsStereoAction` static for invoking outside the class * Refactor single atom stereo edit * Update display orders of context menu items Order before: - Edit selected bond(s) - Bond type - Delete selected bond(s) - Edit selected atom(s) - Enhanced stereochemistry - Delete selected atom(s) Order after: - Edit selected bond(s) - Edit selected atom(s) - Bond type - Enhanced stereochemistry - Delete selected bond(s) - Delete selected atom(s) * Update z-index of context menu
- Loading branch information
Showing
9 changed files
with
494 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 6 additions & 2 deletions
8
packages/ketcher-react/src/script/ui/views/components/ContextMenu/contextMenu.types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
export type ContextMenuItemProps = { | ||
import type { ItemProps, SubMenuProps } from 'react-contexify' | ||
|
||
export type ContextMenuShowProps = { | ||
selected: boolean | ||
closestItem: any | ||
} | ||
|
||
export type ContextMenuItemData = unknown | ||
export type ItemData = unknown | ||
export type CustomItemProps = Omit<ItemProps, 'children'> | ||
export type CustomSubMenuProps = Omit<SubMenuProps, 'children' | 'label'> |
199 changes: 199 additions & 0 deletions
199
...es/ketcher-react/src/script/ui/views/components/ContextMenu/items/AtomBatchOperations.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
/**************************************************************************** | ||
* Copyright 2022 EPAM Systems | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
***************************************************************************/ | ||
|
||
import { | ||
Action, | ||
findStereoAtoms, | ||
fromAtomsAttrs, | ||
fromOneAtomDeletion | ||
} from 'ketcher-core' | ||
import { useCallback, useRef } from 'react' | ||
import type { PredicateParams } from 'react-contexify' | ||
import { Item } from 'react-contexify' | ||
import 'react-contexify/ReactContexify.css' | ||
import { useAppContext } from 'src/hooks' | ||
import Editor from 'src/script/editor' | ||
import EnhancedStereoTool from 'src/script/editor/tool/enhanced-stereo' | ||
import { toElement } from 'src/script/ui/data/convert/structconv' | ||
import type { | ||
ItemData, | ||
ContextMenuShowProps, | ||
CustomItemProps | ||
} from '../contextMenu.types' | ||
import { noOperation } from './utils' | ||
|
||
const isHidden = ({ props }: PredicateParams<ContextMenuShowProps, ItemData>) => | ||
!props?.selected | ||
|
||
const useDisabled = () => { | ||
const { getKetcherInstance } = useAppContext() | ||
|
||
const isDisabled = useCallback( | ||
({ | ||
props, | ||
triggerEvent | ||
}: PredicateParams<ContextMenuShowProps, ItemData>) => { | ||
if (isHidden({ props, triggerEvent })) { | ||
return true | ||
} | ||
|
||
const editor = getKetcherInstance().editor as Editor | ||
const selectedAtomIds = editor.selection()?.atoms | ||
|
||
if (Array.isArray(selectedAtomIds) && selectedAtomIds.length !== 0) { | ||
return false | ||
} | ||
|
||
return true | ||
}, | ||
[getKetcherInstance] | ||
) | ||
|
||
return isDisabled | ||
} | ||
|
||
export const AtomBatchEdit: React.FC<CustomItemProps> = (props) => { | ||
const { getKetcherInstance } = useAppContext() | ||
const isDisabled = useDisabled() | ||
|
||
const handleClick = useCallback(async () => { | ||
const editor = getKetcherInstance().editor as Editor | ||
const defaultAtom = toElement({ | ||
label: 'C', | ||
charge: 0, | ||
isotope: 0, | ||
explicitValence: -1, | ||
radical: 0, | ||
invRet: 0, | ||
ringBondCount: 0, | ||
substitutionCount: 0, | ||
hCount: 0, | ||
stereoParity: 0 | ||
}) | ||
|
||
try { | ||
const newAtom = await editor.event.elementEdit.dispatch(defaultAtom) | ||
const selectedAtomIds = editor.selection()?.atoms | ||
|
||
editor.update( | ||
fromAtomsAttrs(editor.render.ctab, selectedAtomIds, newAtom, false) | ||
) | ||
} catch (error) { | ||
noOperation() | ||
} | ||
}, [getKetcherInstance]) | ||
|
||
return ( | ||
<Item | ||
{...props} | ||
hidden={isHidden} | ||
disabled={isDisabled} | ||
onClick={handleClick} | ||
> | ||
Edit selected atom(s) | ||
</Item> | ||
) | ||
} | ||
|
||
export const AtomStereoBatchEdit: React.FC<CustomItemProps> = (props) => { | ||
const { getKetcherInstance } = useAppContext() | ||
const isDisabled = useDisabled() | ||
const stereoAtomIdsRef = useRef<number[] | undefined>() | ||
|
||
const handleClick = useCallback(async () => { | ||
if (!stereoAtomIdsRef.current) { | ||
return | ||
} | ||
|
||
const editor = getKetcherInstance().editor as Editor | ||
|
||
try { | ||
const action = await EnhancedStereoTool.changeAtomsStereoAction( | ||
editor, | ||
stereoAtomIdsRef.current | ||
) | ||
|
||
action && editor.update(action) | ||
} catch (error) { | ||
noOperation() | ||
} | ||
}, [getKetcherInstance]) | ||
|
||
const isStereoDisabled = useCallback( | ||
({ | ||
props, | ||
triggerEvent | ||
}: PredicateParams<ContextMenuShowProps, ItemData>) => { | ||
if (isDisabled({ props, triggerEvent })) { | ||
return true | ||
} | ||
const editor = getKetcherInstance().editor as Editor | ||
const selectedAtomIds = editor.selection()?.atoms | ||
|
||
const stereoAtomIds: undefined | number[] = findStereoAtoms( | ||
editor.struct(), | ||
selectedAtomIds | ||
) | ||
stereoAtomIdsRef.current = stereoAtomIds | ||
|
||
if (Array.isArray(stereoAtomIds) && stereoAtomIds.length !== 0) { | ||
return false | ||
} | ||
|
||
return true | ||
}, | ||
[getKetcherInstance, isDisabled] | ||
) | ||
|
||
return ( | ||
<Item | ||
{...props} | ||
hidden={isHidden} | ||
disabled={isStereoDisabled} | ||
onClick={handleClick} | ||
> | ||
Enhanced stereochemistry | ||
</Item> | ||
) | ||
} | ||
|
||
export const AtomBatchDelete: React.FC<CustomItemProps> = (props) => { | ||
const { getKetcherInstance } = useAppContext() | ||
const isDisabled = useDisabled() | ||
|
||
const handleClick = useCallback(() => { | ||
const editor = getKetcherInstance().editor as Editor | ||
const action = new Action() | ||
const selectedAtomIds = editor.selection()?.atoms | ||
|
||
selectedAtomIds?.forEach((atomId) => { | ||
action.mergeWith(fromOneAtomDeletion(editor.render.ctab, atomId)) | ||
}) | ||
|
||
editor.update(action) | ||
}, [getKetcherInstance]) | ||
|
||
return ( | ||
<Item | ||
{...props} | ||
hidden={isHidden} | ||
disabled={isDisabled} | ||
onClick={handleClick} | ||
> | ||
Delete selected atom(s) | ||
</Item> | ||
) | ||
} |
Oops, something went wrong.