From 12e87321ab8fcfdd693200d3dfbefa064042c71a Mon Sep 17 00:00:00 2001 From: Ilya Asiyuk Date: Thu, 16 May 2024 17:23:12 +0400 Subject: [PATCH] #4501 - Macro: The system allows you to paste on canvas while modifying nucleotides in the RNA builder --- .../src/application/editor/Editor.ts | 11 +++++-- .../src/application/editor/modes/BaseMode.ts | 6 +++- .../editor/tools/SelectRectangle.ts | 9 +----- .../ketcher-core/src/application/ketcher.ts | 19 ++---------- .../sequence/BaseSequenceItemRenderer.ts | 11 ++----- .../renderers/sequence/SequenceRenderer.ts | 5 +--- .../ketcher-macromolecules/src/Editor.tsx | 18 +++++------ .../LayoutModeButton/LayoutModeButton.tsx | 15 +++++----- .../SequenceTypeDropdown.tsx | 15 +++++----- .../components/contextMenu/RNAContextMenu.tsx | 11 ++++--- .../SequenceItemContextMenu.tsx | 13 ++++---- .../monomerLibrary/MonomerLibrary.tsx | 15 +++++----- .../RnaBuilder/RnaAccordion/RnaAccordion.tsx | 11 +++++-- .../RnaBuilder/RnaEditor/RnaEditor.tsx | 11 ++++--- .../RnaEditorExpanded.test.tsx | 11 +------ .../RnaEditorExpanded/RnaEditorExpanded.tsx | 11 +++++-- .../RnaEditorExpanded.test.tsx.snap | 2 +- ...ts => useDisabledForSequenceMode.test.tsx} | 30 ++++++++----------- .../hooks/useDisabledForSequenceMode.ts | 7 +++-- .../src/hooks/stateHooks.ts | 14 +++++---- .../src/state/common/editorSlice.ts | 4 +++ 21 files changed, 119 insertions(+), 130 deletions(-) rename packages/ketcher-macromolecules/src/components/monomerLibrary/monomerLibraryItem/hooks/{useDisabledForSequenceMode.test.ts => useDisabledForSequenceMode.test.tsx} (84%) diff --git a/packages/ketcher-core/src/application/editor/Editor.ts b/packages/ketcher-core/src/application/editor/Editor.ts index ea9a77dd5c..1bbab5e76b 100644 --- a/packages/ketcher-core/src/application/editor/Editor.ts +++ b/packages/ketcher-core/src/application/editor/Editor.ts @@ -339,12 +339,19 @@ export class CoreEditor { } public get isSequenceEditMode() { - return this.mode instanceof SequenceMode && this.mode.isEditMode; + return this?.mode instanceof SequenceMode && this?.mode.isEditMode; } public get isSequenceEditInRNABuilderMode() { return ( - this.mode instanceof SequenceMode && this.mode.isEditInRNABuilderMode + this?.mode instanceof SequenceMode && this?.mode.isEditInRNABuilderMode + ); + } + + public get isSequenceAnyEditMode() { + return ( + this?.mode instanceof SequenceMode && + (this?.mode.isEditMode || this?.mode.isEditInRNABuilderMode) ); } diff --git a/packages/ketcher-core/src/application/editor/modes/BaseMode.ts b/packages/ketcher-core/src/application/editor/modes/BaseMode.ts index f3ee085175..5313209475 100644 --- a/packages/ketcher-core/src/application/editor/modes/BaseMode.ts +++ b/packages/ketcher-core/src/application/editor/modes/BaseMode.ts @@ -141,8 +141,12 @@ export abstract class BaseMode { async onPaste(event: ClipboardEvent) { if (!this.checkIfTargetIsInput(event)) { if (isClipboardAPIAvailable()) { - if (this._pasteIsInProgress) return; + const isSequenceEditInRNABuilderMode = + CoreEditor.provideEditorInstance().isSequenceEditInRNABuilderMode; + + if (isSequenceEditInRNABuilderMode || this._pasteIsInProgress) return; this._pasteIsInProgress = true; + const clipboardData = await navigator.clipboard.read(); this.pasteFromClipboard(clipboardData).finally(() => { this._pasteIsInProgress = false; diff --git a/packages/ketcher-core/src/application/editor/tools/SelectRectangle.ts b/packages/ketcher-core/src/application/editor/tools/SelectRectangle.ts index 83e62202ea..b5472d94cc 100644 --- a/packages/ketcher-core/src/application/editor/tools/SelectRectangle.ts +++ b/packages/ketcher-core/src/application/editor/tools/SelectRectangle.ts @@ -137,14 +137,7 @@ class SelectRectangle implements BaseTool { } mousedown(event) { - const editor = CoreEditor.provideEditorInstance(); - - if ( - editor.mode instanceof SequenceMode && - (editor.mode.isEditMode || editor.mode.isEditInRNABuilderMode) - ) { - return; - } + if (CoreEditor.provideEditorInstance().isSequenceAnyEditMode) return; const renderer = event.target.__data__; this.mousePositionAfterMove = this.editor.lastCursorPositionOfCanvas; diff --git a/packages/ketcher-core/src/application/ketcher.ts b/packages/ketcher-core/src/application/ketcher.ts index f39f256135..be8772357e 100644 --- a/packages/ketcher-core/src/application/ketcher.ts +++ b/packages/ketcher-core/src/application/ketcher.ts @@ -18,12 +18,7 @@ import { saveAs } from 'file-saver'; import { FormatterFactory, SupportedFormat } from './formatters'; import { GenerateImageOptions, StructService } from 'domain/services'; -import { - CoreEditor, - Editor, - defaultBondThickness, - SequenceMode, -} from './editor'; +import { CoreEditor, Editor, defaultBondThickness } from './editor'; import { Indigo } from 'application/indigo'; import { KetSerializer, MolfileFormat } from 'domain/serializers'; import { SGroup, Struct } from 'domain/entities'; @@ -351,11 +346,7 @@ export class Ketcher { } async setMolecule(structStr: string): Promise { - const editor = CoreEditor.provideEditorInstance(); - if ( - editor?.mode instanceof SequenceMode && - editor?.mode.isEditInRNABuilderMode - ) + if (CoreEditor.provideEditorInstance()?.isSequenceEditInRNABuilderMode) return; runAsyncAction(async () => { @@ -380,11 +371,7 @@ export class Ketcher { } async addFragment(structStr: string): Promise { - const editor = CoreEditor.provideEditorInstance(); - if ( - editor?.mode instanceof SequenceMode && - editor?.mode.isEditInRNABuilderMode - ) + if (CoreEditor.provideEditorInstance()?.isSequenceEditInRNABuilderMode) return; runAsyncAction(async () => { diff --git a/packages/ketcher-core/src/application/render/renderers/sequence/BaseSequenceItemRenderer.ts b/packages/ketcher-core/src/application/render/renderers/sequence/BaseSequenceItemRenderer.ts index fa0abb2129..963f2c902c 100644 --- a/packages/ketcher-core/src/application/render/renderers/sequence/BaseSequenceItemRenderer.ts +++ b/packages/ketcher-core/src/application/render/renderers/sequence/BaseSequenceItemRenderer.ts @@ -4,7 +4,6 @@ import { SubChainNode } from 'domain/entities/monomer-chains/types'; import { BaseSequenceRenderer } from 'application/render/renderers/sequence/BaseSequenceRenderer'; import { BaseSubChain } from 'domain/entities/monomer-chains/BaseSubChain'; import { CoreEditor } from 'application/editor/internal'; -import { SequenceMode } from 'application/editor/modes'; import { EmptySequenceNode } from 'domain/entities/EmptySequenceNode'; import { editorEvents } from 'application/editor/editorEvents'; import assert from 'assert'; @@ -74,17 +73,11 @@ export abstract class BaseSequenceItemRenderer extends BaseSequenceRenderer { } protected get isSequenceEditModeTurnedOn() { - const editor = CoreEditor.provideEditorInstance(); - - return editor.mode instanceof SequenceMode && editor.mode.isEditMode; + return CoreEditor.provideEditorInstance().isSequenceEditMode; } protected get isSequenceEditInRnaBuilderModeTurnedOn() { - const editor = CoreEditor.provideEditorInstance(); - - return ( - editor.mode instanceof SequenceMode && editor.mode.isEditInRNABuilderMode - ); + return CoreEditor.provideEditorInstance().isSequenceEditInRNABuilderMode; } private appendRootElement() { diff --git a/packages/ketcher-core/src/application/render/renderers/sequence/SequenceRenderer.ts b/packages/ketcher-core/src/application/render/renderers/sequence/SequenceRenderer.ts index c138e601d0..7e012b8e1a 100644 --- a/packages/ketcher-core/src/application/render/renderers/sequence/SequenceRenderer.ts +++ b/packages/ketcher-core/src/application/render/renderers/sequence/SequenceRenderer.ts @@ -25,7 +25,6 @@ import { Chain } from 'domain/entities/monomer-chains/Chain'; import { EmptySubChain } from 'domain/entities/monomer-chains/EmptySubChain'; import { SubChainNode } from 'domain/entities/monomer-chains/types'; import { CoreEditor } from 'application/editor/internal'; -import { SequenceMode } from 'application/editor/modes'; import { RestoreSequenceCaretPositionOperation } from 'application/editor/operations/modes'; import assert from 'assert'; import { BaseSubChain } from 'domain/entities/monomer-chains/BaseSubChain'; @@ -80,9 +79,7 @@ export class SequenceRenderer { let currentMonomerIndexInChain = 0; let currentMonomerIndexOverall = 0; - const editor = CoreEditor.provideEditorInstance(); - const isEditMode = - editor.mode instanceof SequenceMode && editor.mode.isEditMode; + const isEditMode = CoreEditor.provideEditorInstance().isSequenceEditMode; if (isEditMode) { chainsCollection.chains.forEach((chain) => { diff --git a/packages/ketcher-macromolecules/src/Editor.tsx b/packages/ketcher-macromolecules/src/Editor.tsx index ee87884959..ffd9ac8ae3 100644 --- a/packages/ketcher-macromolecules/src/Editor.tsx +++ b/packages/ketcher-macromolecules/src/Editor.tsx @@ -24,7 +24,6 @@ import { Nucleotide, Nucleoside, NodeSelection, - SequenceMode, } from 'ketcher-core'; import { store } from 'state'; import { @@ -45,6 +44,7 @@ import { destroyEditor, selectEditor, selectEditorActiveTool, + selectIsSequenceEditInRNABuilderMode, selectTool, showPreview, } from 'state/common'; @@ -367,7 +367,9 @@ function Editor({ theme, togglerComponent }: EditorProps) { - + { if (modalComponentList[name]) { @@ -427,12 +431,8 @@ function MenuComponent() { if (name === 'clear') { dispatch(selectTool('select-rectangle')); editor.events.selectTool.dispatch('select-rectangle'); - if ( - editor.mode instanceof SequenceMode && - editor.mode.isEditInRNABuilderMode - ) { + if (isSequenceEditInRNABuilderMode) resetRnaBuilderAfterSequenceUpdate(dispatch, editor); - } } else { dispatch(selectTool(name)); } diff --git a/packages/ketcher-macromolecules/src/components/LayoutModeButton/LayoutModeButton.tsx b/packages/ketcher-macromolecules/src/components/LayoutModeButton/LayoutModeButton.tsx index c7204e1b84..d40863ede0 100644 --- a/packages/ketcher-macromolecules/src/components/LayoutModeButton/LayoutModeButton.tsx +++ b/packages/ketcher-macromolecules/src/components/LayoutModeButton/LayoutModeButton.tsx @@ -17,18 +17,19 @@ import { useEffect, useState } from 'react'; import { Menu } from 'components/menu'; import { MenuContext } from '../../contexts'; +import { useAppSelector, useLayoutMode } from 'hooks'; import { - useAppSelector, - useLayoutMode, - useSequenceEditInRNABuilderMode, -} from 'hooks'; -import { selectEditor } from 'state/common'; + selectEditor, + selectIsSequenceEditInRNABuilderMode, +} from 'state/common'; export const LayoutModeButton = () => { const [activeMode, setActiveMode] = useState('flex-layout-mode'); const editor = useAppSelector(selectEditor); const layoutMode = useLayoutMode(); - const isSequenceEditInRNABuilderMode = useSequenceEditInRNABuilderMode(); + const isSequenceEditInRNABuilderMode = useAppSelector( + selectIsSequenceEditInRNABuilderMode, + ); const menuContext = { isActive: (mode) => activeMode === mode, @@ -51,7 +52,7 @@ export const LayoutModeButton = () => { { ); const [isSequenceMode, setIsSequenceMode] = useState(false); const editor = useAppSelector(selectEditor); + const isSequenceEditInRNABuilderMode = useAppSelector( + selectIsSequenceEditInRNABuilderMode, + ); const layoutMode = useLayoutMode(); - const isSequenceEditInRNABuilderMode = useSequenceEditInRNABuilderMode(); - const isDisabled = isSequenceEditInRNABuilderMode; + const isDisabled = !!isSequenceEditInRNABuilderMode; const dropdownOptions = [ { id: SequenceType.RNA, label: 'RNA' }, diff --git a/packages/ketcher-macromolecules/src/components/contextMenu/RNAContextMenu.tsx b/packages/ketcher-macromolecules/src/components/contextMenu/RNAContextMenu.tsx index d970fad2ed..1c442083f8 100644 --- a/packages/ketcher-macromolecules/src/components/contextMenu/RNAContextMenu.tsx +++ b/packages/ketcher-macromolecules/src/components/contextMenu/RNAContextMenu.tsx @@ -1,10 +1,6 @@ import { Item, ItemParams, Separator } from 'react-contexify'; import { openModal } from 'state/modal'; -import { - useAppDispatch, - useAppSelector, - useSequenceEditInRNABuilderMode, -} from 'hooks'; +import { useAppDispatch, useAppSelector } from 'hooks'; import { ReactElement } from 'react'; import { CONTEXT_MENU_ID } from './types'; import { selectCurrentTabIndex, setSelectedTabIndex } from 'state/library'; @@ -12,6 +8,7 @@ import { selectActivePresetForContextMenu } from 'state/rna-builder'; import { StyledMenu } from './styles'; import { createPortal } from 'react-dom'; import { KETCHER_MACROMOLECULES_ROOT_NODE_SELECTOR } from 'ketcher-react'; +import { selectIsSequenceEditInRNABuilderMode } from 'state/common'; export const RNAContextMenu = () => { const RNA_TAB_INDEX = 2; @@ -20,7 +17,9 @@ export const RNAContextMenu = () => { selectActivePresetForContextMenu, ); const selectedTabIndex = useAppSelector(selectCurrentTabIndex); - const isSequenceEditInRNABuilderMode = useSequenceEditInRNABuilderMode(); + const isSequenceEditInRNABuilderMode = useAppSelector( + selectIsSequenceEditInRNABuilderMode, + ); const RNAMenus = [ { name: 'duplicateandedit', title: 'Duplicate and Edit...' }, { name: 'edit', title: 'Edit...', seperator: true }, diff --git a/packages/ketcher-macromolecules/src/components/contextMenu/SequenceItemContextMenu/SequenceItemContextMenu.tsx b/packages/ketcher-macromolecules/src/components/contextMenu/SequenceItemContextMenu/SequenceItemContextMenu.tsx index ff64c51b08..7d571448ae 100644 --- a/packages/ketcher-macromolecules/src/components/contextMenu/SequenceItemContextMenu/SequenceItemContextMenu.tsx +++ b/packages/ketcher-macromolecules/src/components/contextMenu/SequenceItemContextMenu/SequenceItemContextMenu.tsx @@ -4,12 +4,11 @@ import { CONTEXT_MENU_ID } from '../types'; import { StyledMenu } from '../styles'; import { createPortal } from 'react-dom'; import { KETCHER_MACROMOLECULES_ROOT_NODE_SELECTOR } from 'ketcher-react'; +import { useAppDispatch, useAppSelector } from 'hooks'; import { - useAppDispatch, - useAppSelector, - useSequenceEditInRNABuilderMode, -} from 'hooks'; -import { selectEditor } from 'state/common'; + selectEditor, + selectIsSequenceEditInRNABuilderMode, +} from 'state/common'; import { BaseSequenceItemRenderer, NodesSelection } from 'ketcher-core'; import { setSelectedTabIndex } from 'state/library'; import { @@ -34,7 +33,9 @@ export const SequenceItemContextMenu = ({ const editor = useAppSelector(selectEditor); const dispatch = useAppDispatch(); const menuProps = generateSequenceContextMenuProps(selections); - const isSequenceEditInRNABuilderMode = useSequenceEditInRNABuilderMode(); + const isSequenceEditInRNABuilderMode = useAppSelector( + selectIsSequenceEditInRNABuilderMode, + ); const menuItems = [ { diff --git a/packages/ketcher-macromolecules/src/components/monomerLibrary/MonomerLibrary.tsx b/packages/ketcher-macromolecules/src/components/monomerLibrary/MonomerLibrary.tsx index 3949800d38..c20c98a0f7 100644 --- a/packages/ketcher-macromolecules/src/components/monomerLibrary/MonomerLibrary.tsx +++ b/packages/ketcher-macromolecules/src/components/monomerLibrary/MonomerLibrary.tsx @@ -16,12 +16,7 @@ import React, { ChangeEvent, useRef } from 'react'; import { Tabs } from 'components/shared/Tabs'; import { tabsContent } from 'components/monomerLibrary/tabsContent'; -import { - useAppDispatch, - useAppSelector, - useLayoutMode, - useSequenceEditInRNABuilderMode, -} from 'hooks'; +import { useAppDispatch, useAppSelector, useLayoutMode } from 'hooks'; import { setSearchFilter } from 'state/library'; import { Icon } from 'ketcher-react'; import { IRnaPreset } from './RnaBuilder/types'; @@ -42,11 +37,15 @@ import { const COPY = '_Copy'; -const MonomerLibrary = React.memo(() => { +type MonomerLibraryProps = { + isSequenceEditInRNABuilderMode?: boolean; +}; + +const MonomerLibrary = React.memo((props: MonomerLibraryProps) => { const presetsRef = useRef([]); const dispatch = useAppDispatch(); const layoutMode = useLayoutMode(); - const isSequenceEditInRNABuilderMode = useSequenceEditInRNABuilderMode(); + const isSequenceEditInRNABuilderMode = props?.isSequenceEditInRNABuilderMode; const isSequenceMode = layoutMode === 'sequence-layout-mode'; const isDisabledTabs = isSequenceMode; const isDisabledTabsPanels = diff --git a/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaAccordion/RnaAccordion.tsx b/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaAccordion/RnaAccordion.tsx index 9746302ab5..025cbf4bd0 100644 --- a/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaAccordion/RnaAccordion.tsx +++ b/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaAccordion/RnaAccordion.tsx @@ -15,7 +15,7 @@ ***************************************************************************/ import { MonomerGroup } from 'components/monomerLibrary/monomerLibraryGroup'; -import { useAppSelector, useSequenceEditInRNABuilderMode } from 'hooks'; +import { useAppSelector } from 'hooks'; import { IconName } from 'ketcher-react'; import { useEffect, useState } from 'react'; import { @@ -55,7 +55,10 @@ import { import { useDispatch } from 'react-redux'; import { IRnaPreset } from '../types'; import { MonomerItemType } from 'ketcher-core'; -import { selectEditor } from 'state/common'; +import { + selectEditor, + selectIsSequenceEditInRNABuilderMode, +} from 'state/common'; import { RnaPresetGroup } from 'components/monomerLibrary/RnaPresetGroup/RnaPresetGroup'; interface IGroupsDataItem { @@ -79,7 +82,9 @@ export const RnaAccordion = ({ libraryName, duplicatePreset, editPreset }) => { const isActivePresetNewAndEmpty = useAppSelector( selectIsActivePresetNewAndEmpty, ); - const isSequenceEditInRNABuilderMode = useSequenceEditInRNABuilderMode(); + const isSequenceEditInRNABuilderMode = useAppSelector( + selectIsSequenceEditInRNABuilderMode, + ); const [expandedAccordion, setExpandedAccordion] = useState(activeRnaBuilderItem); diff --git a/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaEditor/RnaEditor.tsx b/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaEditor/RnaEditor.tsx index ab2f73f33c..708dda27c0 100644 --- a/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaEditor/RnaEditor.tsx +++ b/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaEditor/RnaEditor.tsx @@ -23,11 +23,7 @@ import { RnaEditorContainer, StyledHeader, } from './styles'; -import { - useAppDispatch, - useAppSelector, - useSequenceEditInRNABuilderMode, -} from 'hooks'; +import { useAppDispatch, useAppSelector } from 'hooks'; import { createNewPreset, RnaBuilderPresetsItem, @@ -38,6 +34,7 @@ import { setIsEditMode, } from 'state/rna-builder'; import { scrollToElement } from 'helpers/dom'; +import { selectIsSequenceEditInRNABuilderMode } from 'state/common'; export const scrollToSelectedPreset = (presetName) => { scrollToElement(`[data-rna-preset-item-name="${presetName}"]`); @@ -50,7 +47,9 @@ export const scrollToSelectedMonomer = (monomerId) => { export const RnaEditor = ({ duplicatePreset }) => { const activePreset = useAppSelector(selectActivePreset); const isEditMode = useAppSelector(selectIsEditMode); - const isSequenceEditInRNABuilderMode = useSequenceEditInRNABuilderMode(); + const isSequenceEditInRNABuilderMode = useAppSelector( + selectIsSequenceEditInRNABuilderMode, + ); const activePresetFullName = selectPresetFullName(activePreset); const dispatch = useAppDispatch(); diff --git a/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaEditor/RnaEditorExpanded/RnaEditorExpanded.test.tsx b/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaEditor/RnaEditorExpanded/RnaEditorExpanded.test.tsx index 1de79f4bda..830b6a7b00 100644 --- a/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaEditor/RnaEditorExpanded/RnaEditorExpanded.test.tsx +++ b/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaEditor/RnaEditorExpanded/RnaEditorExpanded.test.tsx @@ -2,15 +2,6 @@ import { Entities } from 'ketcher-core'; import { fireEvent, render, screen } from '@testing-library/react'; import { RnaEditorExpanded } from 'components/monomerLibrary/RnaBuilder/RnaEditor/RnaEditorExpanded/RnaEditorExpanded'; import { EmptyFunction } from 'helpers'; -import { useSequenceEditInRNABuilderMode } from 'hooks'; - -jest.mock('../../../../../hooks/stateHooks', () => ({ - ...jest.requireActual('../../../../../hooks/stateHooks'), - useSequenceEditInRNABuilderMode: jest.fn(), -})); - -const mockUseSequenceEditInRNABuilderMode = - useSequenceEditInRNABuilderMode as jest.Mock; describe('Test Rna Editor Expanded component', () => { it('should render correctly in edit mode', async () => { @@ -42,11 +33,11 @@ describe('Test Rna Editor Expanded component', () => { }); it('should render correctly in edit mode with modification of sequence', async () => { - mockUseSequenceEditInRNABuilderMode.mockReturnValue(true); render( withThemeAndStoreProvider( , { + editor: { editor: { isSequenceEditInRNABuilderMode: true } }, rnaBuilder: { activePreset: {}, sequenceSelectionName: '2 nucleotides', diff --git a/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaEditor/RnaEditorExpanded/RnaEditorExpanded.tsx b/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaEditor/RnaEditorExpanded/RnaEditorExpanded.tsx index 399ce4170e..204f23ad06 100644 --- a/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaEditor/RnaEditorExpanded/RnaEditorExpanded.tsx +++ b/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaEditor/RnaEditorExpanded/RnaEditorExpanded.tsx @@ -50,13 +50,16 @@ import { setSequenceSelectionName, selectIsActivePresetNewAndEmpty, } from 'state/rna-builder'; -import { useAppSelector, useSequenceEditInRNABuilderMode } from 'hooks'; +import { useAppSelector } from 'hooks'; import { scrollToSelectedMonomer, scrollToSelectedPreset, } from 'components/monomerLibrary/RnaBuilder/RnaEditor/RnaEditor'; import { getMonomerUniqueKey } from 'state/library'; -import { selectEditor } from 'state/common'; +import { + selectEditor, + selectIsSequenceEditInRNABuilderMode, +} from 'state/common'; import { ChangeEvent, useEffect, useState } from 'react'; import { generateSequenceSelectionGroupNames, @@ -109,7 +112,9 @@ export const RnaEditorExpanded = ({ // For sequence edit in RNA Builder mode const sequenceSelection = useAppSelector(selectSequenceSelection); const sequenceSelectionName = useAppSelector(selectSequenceSelectionName); - const isSequenceEditInRNABuilderMode = useSequenceEditInRNABuilderMode(); + const isSequenceEditInRNABuilderMode = useAppSelector( + selectIsSequenceEditInRNABuilderMode, + ); const [isSequenceSelectionUpdated, setIsSequenceSelectionUpdated] = useState(false); const [sequenceSelectionGroupNames, setSequenceSelectionGroupNames] = diff --git a/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaEditor/RnaEditorExpanded/__snapshots__/RnaEditorExpanded.test.tsx.snap b/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaEditor/RnaEditorExpanded/__snapshots__/RnaEditorExpanded.test.tsx.snap index 2079144e90..08ed800f56 100644 --- a/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaEditor/RnaEditorExpanded/__snapshots__/RnaEditorExpanded.test.tsx.snap +++ b/packages/ketcher-macromolecules/src/components/monomerLibrary/RnaBuilder/RnaEditor/RnaEditorExpanded/__snapshots__/RnaEditorExpanded.test.tsx.snap @@ -347,7 +347,7 @@ exports[`Test Rna Editor Expanded component should render correctly in edit mode exports[`Test Rna Editor Expanded component should render correctly in view mode 1`] = `
({ useSelector: jest.fn(), })); -jest.mock('../../../../hooks/stateHooks', () => ({ - ...jest.requireActual('../../../../hooks/stateHooks'), - useSequenceEditInRNABuilderMode: jest.fn(), +jest.mock('hooks', () => ({ + useAppSelector: jest.fn(), })); -const mockUseSequenceEditInRNABuilderMode = - useSequenceEditInRNABuilderMode as jest.Mock; - const mockUseSelector = useSelector as jest.Mock; +const mockUseAppSelector = useAppSelector as jest.Mock; const monomer: MonomerItemType = { label: 'for test', @@ -31,7 +28,7 @@ const monomer: MonomerItemType = { describe('useDisabledForSequenceMode hook', () => { it('should return false if isSequenceEditInRNABuilderMode is false', () => { - mockUseSequenceEditInRNABuilderMode.mockReturnValue(false); + mockUseAppSelector.mockReturnValue(false); const { result } = renderHook(() => useDisabledForSequenceMode(monomer, MonomerGroups.SUGARS), ); @@ -40,7 +37,7 @@ describe('useDisabledForSequenceMode hook', () => { describe('for Bases', () => { it('should return false if there is R1', () => { - mockUseSequenceEditInRNABuilderMode.mockReturnValue(true); + mockUseAppSelector.mockReturnValue(true); monomer.props.MonomerCaps = { R1: 'H' }; const { result } = renderHook(() => useDisabledForSequenceMode(monomer, MonomerGroups.BASES), @@ -49,7 +46,7 @@ describe('useDisabledForSequenceMode hook', () => { }); it('should return true if there is no R1', () => { - mockUseSequenceEditInRNABuilderMode.mockReturnValue(true); + mockUseAppSelector.mockReturnValue(true); monomer.props.MonomerCaps = {}; const { result } = renderHook(() => useDisabledForSequenceMode(monomer, MonomerGroups.BASES), @@ -60,7 +57,7 @@ describe('useDisabledForSequenceMode hook', () => { describe('for Phosphates', () => { it('should return false if there is R1 and R2', () => { - mockUseSequenceEditInRNABuilderMode.mockReturnValue(true); + mockUseAppSelector.mockReturnValue(true); monomer.props.MonomerCaps = { R1: 'H', R2: 'H' }; const { result } = renderHook(() => useDisabledForSequenceMode(monomer, MonomerGroups.PHOSPHATES), @@ -69,8 +66,7 @@ describe('useDisabledForSequenceMode hook', () => { }); it('should return true if there is no R1 or R2', () => { - mockUseSequenceEditInRNABuilderMode.mockReturnValue(true); - + mockUseAppSelector.mockReturnValue(true); // Without R2 monomer.props.MonomerCaps = { R1: 'H' }; const { result: result1 } = renderHook(() => @@ -89,7 +85,7 @@ describe('useDisabledForSequenceMode hook', () => { describe('for Sugars', () => { it('should return false if there is R2 and R3 and isSequenceFirstsOnlyNucleoelementsSelected is true', () => { - mockUseSequenceEditInRNABuilderMode.mockReturnValue(true); + mockUseAppSelector.mockReturnValue(true); mockUseSelector.mockImplementation(() => true); monomer.props.MonomerCaps = { R2: 'H', R3: 'H' }; const { result } = renderHook(() => @@ -99,7 +95,7 @@ describe('useDisabledForSequenceMode hook', () => { }); it('should return true if there is no R2 or R3 and isSequenceFirstsOnlyNucleoelementsSelected is true', () => { - mockUseSequenceEditInRNABuilderMode.mockReturnValue(true); + mockUseAppSelector.mockReturnValue(true); mockUseSelector.mockImplementation(() => true); monomer.props.MonomerCaps = { R2: 'H' }; @@ -118,7 +114,7 @@ describe('useDisabledForSequenceMode hook', () => { }); it('should return false if there is R1, R2, R3 and isSequenceFirstsOnlyNucleoelementsSelected is false', () => { - mockUseSequenceEditInRNABuilderMode.mockReturnValue(true); + mockUseAppSelector.mockReturnValue(true); mockUseSelector.mockImplementation(() => false); monomer.props.MonomerCaps = { R1: 'H', R2: 'H', R3: 'H' }; const { result } = renderHook(() => @@ -128,7 +124,7 @@ describe('useDisabledForSequenceMode hook', () => { }); it('should return true if there is no R1 or R2 or R3 and isSequenceFirstsOnlyNucleoelementsSelected is false', () => { - mockUseSequenceEditInRNABuilderMode.mockReturnValue(true); + mockUseAppSelector.mockReturnValue(true); mockUseSelector.mockImplementation(() => true); // Without R3 diff --git a/packages/ketcher-macromolecules/src/components/monomerLibrary/monomerLibraryItem/hooks/useDisabledForSequenceMode.ts b/packages/ketcher-macromolecules/src/components/monomerLibrary/monomerLibraryItem/hooks/useDisabledForSequenceMode.ts index 377b2cb7d7..3f27397541 100644 --- a/packages/ketcher-macromolecules/src/components/monomerLibrary/monomerLibraryItem/hooks/useDisabledForSequenceMode.ts +++ b/packages/ketcher-macromolecules/src/components/monomerLibrary/monomerLibraryItem/hooks/useDisabledForSequenceMode.ts @@ -2,13 +2,16 @@ import { MonomerGroups, MonomerItemType } from 'ketcher-core'; import { useSelector } from 'react-redux'; import { useEffect, useState } from 'react'; import { selectIsSequenceFirstsOnlyNucleotidesSelected } from 'state/rna-builder'; -import { useSequenceEditInRNABuilderMode } from 'hooks'; +import { useAppSelector } from 'hooks'; +import { selectIsSequenceEditInRNABuilderMode } from 'state/common'; const useDisabledForSequenceMode = ( item: MonomerItemType, groupName?: MonomerGroups, ) => { - const isSequenceEditInRNABuilderMode = useSequenceEditInRNABuilderMode(); + const isSequenceEditInRNABuilderMode = useAppSelector( + selectIsSequenceEditInRNABuilderMode, + ); const isSequenceFirstsOnlyNucleoelementsSelected = useSelector( selectIsSequenceFirstsOnlyNucleotidesSelected, ); diff --git a/packages/ketcher-macromolecules/src/hooks/stateHooks.ts b/packages/ketcher-macromolecules/src/hooks/stateHooks.ts index 7a5750ae0f..dc0c029fbd 100644 --- a/packages/ketcher-macromolecules/src/hooks/stateHooks.ts +++ b/packages/ketcher-macromolecules/src/hooks/stateHooks.ts @@ -17,8 +17,11 @@ import { useCallback, useEffect, useState } from 'react'; import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux'; import { AppDispatch, RootState } from 'state'; -import { selectEditor } from 'state/common'; -import { LayoutMode, SequenceMode } from 'ketcher-core'; +import { + selectEditor, + selectIsSequenceEditInRNABuilderMode, +} from 'state/common'; +import { LayoutMode } from 'ketcher-core'; export const useAppDispatch = () => useDispatch(); export const useAppSelector: TypedUseSelectorHook = useSelector; @@ -46,10 +49,11 @@ export function useLayoutMode() { export function useSequenceEditInRNABuilderMode() { const editor = useAppSelector(selectEditor); - const isSequenceEditInRNABuilderModeInitial = - editor?.mode instanceof SequenceMode && editor?.mode.isEditInRNABuilderMode; + const isSequenceEditInRNABuilderModeInitial = useAppSelector( + selectIsSequenceEditInRNABuilderMode, + ); const [isSequenceEditInRNABuilderMode, setIsSequenceEditInRNABuilderMode] = - useState(isSequenceEditInRNABuilderModeInitial); + useState(isSequenceEditInRNABuilderModeInitial); const onSequenceEditInRNABuilderModeChange = (value: boolean) => { setIsSequenceEditInRNABuilderMode(value); diff --git a/packages/ketcher-macromolecules/src/state/common/editorSlice.ts b/packages/ketcher-macromolecules/src/state/common/editorSlice.ts index 99096df600..beb43d12ec 100644 --- a/packages/ketcher-macromolecules/src/state/common/editorSlice.ts +++ b/packages/ketcher-macromolecules/src/state/common/editorSlice.ts @@ -101,4 +101,8 @@ export const selectEditorActiveTool = (state: RootState) => export const selectEditor = (state: RootState): CoreEditor => state.editor.editor; +export const selectIsSequenceEditInRNABuilderMode = ( + state: RootState, +): boolean => state.editor.editor?.isSequenceEditInRNABuilderMode; + export const editorReducer = editorSlice.reducer;