Skip to content

Commit

Permalink
Macro: #3498 - When switching from the Macromolecules module to the M…
Browse files Browse the repository at this point in the history
…olecules module an application crashes (#3552)

* #3498 - Macro: When switching from the Macromolecules module to the Molecules module an application crashes
- added handling the cae when monomer doesn't have specific attachment point but we added bond to it.
  • Loading branch information
rrodionov91 authored Nov 8, 2023
1 parent 7df38b1 commit d723b1d
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 22 deletions.
23 changes: 16 additions & 7 deletions packages/ketcher-core/src/application/editor/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { PolymerBondRenderer } from 'application/render/renderers';
import { Editor } from 'application/editor/editor.types';
import { MacromoleculesConverter } from 'application/editor/MacromoleculesConverter';
import { BaseMonomer } from 'domain/entities/BaseMonomer';
import { ketcherProvider } from 'application/utils';

interface ICoreEditorConstructorParams {
theme;
Expand Down Expand Up @@ -63,7 +64,8 @@ export class CoreEditor {
this.zoomTool = ZoomTool.initInstance(this.drawingEntitiesManager);
// eslint-disable-next-line @typescript-eslint/no-this-alias
editor = this;
this.micromoleculesEditor = global.ketcher.editor;
const ketcher = ketcherProvider.getKetcher();
this.micromoleculesEditor = ketcher?.editor;
this.switchToMacromolecules();
}

Expand Down Expand Up @@ -270,22 +272,29 @@ export class CoreEditor {
this.unsubscribeEvents();
const struct = this.micromoleculesEditor.struct();
const reStruct = this.micromoleculesEditor.render.ctab;
MacromoleculesConverter.convertDrawingEntitiesToStruct(
this.drawingEntitiesManager,
struct,
reStruct,
);
const { conversionErrorMessage } =
MacromoleculesConverter.convertDrawingEntitiesToStruct(
this.drawingEntitiesManager,
struct,
reStruct,
);
reStruct.render.setMolecule(struct);
if (conversionErrorMessage) {
const ketcher = ketcherProvider.getKetcher();

ketcher.editor.setMacromoleculeConvertionError(conversionErrorMessage);
}
}

private switchToMacromolecules() {
const struct = this.micromoleculesEditor?.struct() || new Struct();
const ketcher = ketcherProvider.getKetcher();
const { modelChanges } =
MacromoleculesConverter.convertStructToDrawingEntities(
struct,
this.drawingEntitiesManager,
);
this.renderersContainer.update(modelChanges);
global.ketcher.editor?.clear();
ketcher?.editor.clear();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,28 +141,40 @@ export class MacromoleculesConverter {
}
});

let conversionErrorMessage = '';

drawingEntitiesManager.polymerBonds.forEach((polymerBond) => {
assert(polymerBond.secondMonomer);
const beginAtom = this.findAttachmentPointAtom(
monomerToSgroup.get(polymerBond.firstMonomer) as SGroup,
polymerBond,
polymerBond.firstMonomer,
struct,
);
const endAtom = this.findAttachmentPointAtom(
monomerToSgroup.get(polymerBond.secondMonomer) as SGroup,
polymerBond,
polymerBond.secondMonomer,
struct,
);

if (!beginAtom || !endAtom) {
conversionErrorMessage =
'There is no atom for provided attachment point. Bond between monomers was not created.';

return;
}

const bond = new Bond({
type: Bond.PATTERN.TYPE.SINGLE,
begin: this.findAttachmentPointAtom(
monomerToSgroup.get(polymerBond.firstMonomer) as SGroup,
polymerBond,
polymerBond.firstMonomer,
struct,
),
end: this.findAttachmentPointAtom(
monomerToSgroup.get(polymerBond.secondMonomer) as SGroup,
polymerBond,
polymerBond.secondMonomer,
struct,
),
begin: beginAtom,
end: endAtom,
});
const bondId = struct.bonds.add(bond);
reStruct?.bonds.set(bondId, new ReBond(bond));
});

return { struct, reStruct };
return { struct, reStruct, conversionErrorMessage };
}

private static convertMonomerMicromoleculeToMonomer(
Expand Down
3 changes: 3 additions & 0 deletions packages/ketcher-core/src/application/editor/editor.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,7 @@ export interface Editor {
render: Render;
// supposed to be RotateController from 'ketcher-react' package
rotateController: any;
macromoleculeConvertionError: string | null | undefined;
setMacromoleculeConvertionError: (errorMessage: string) => void;
clearMacromoleculeConvertionError: () => void;
}
9 changes: 9 additions & 0 deletions packages/ketcher-react/src/script/editor/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ class Editor implements KetcherEditor {
};

lastEvent: any;
macromoleculeConvertionError: string | null | undefined;

constructor(clientArea, options) {
this.render = new Render(
Expand Down Expand Up @@ -669,6 +670,14 @@ class Editor implements KetcherEditor {
this.update(action);
this.render.update(true);
}

setMacromoleculeConvertionError(errorMessage: string) {
this.macromoleculeConvertionError = errorMessage;
}

clearMacromoleculeConvertionError() {
this.macromoleculeConvertionError = null;
}
}

/**
Expand Down
4 changes: 4 additions & 0 deletions packages/ketcher-react/src/script/ui/state/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { generateCommonProperties } from './utils';
import { saveSettings } from '../options';
import { memoizedDebounce } from '../../utils';
import { updateFloatingTools } from '../floatingTools';
import { openInfoModalWithCustomMessage } from '../shared';

export default function initEditor(dispatch, getState) {
const updateAction = debounce(100, () => dispatch({ type: 'UPDATE' }));
Expand Down Expand Up @@ -238,5 +239,8 @@ export default function initEditor(dispatch, getState) {

onZoomIn: updateAction,
onZoomOut: updateAction,

onShowMacromoleculesErrorMessage: (payload) =>
dispatch(openInfoModalWithCustomMessage(payload)),
};
}
10 changes: 10 additions & 0 deletions packages/ketcher-react/src/script/ui/state/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,13 @@ export function openInfoModal(command: 'Paste' | 'Copy' | 'Cut'): {
data: { name: 'info-modal', prop: { message: command } },
};
}

export function openInfoModalWithCustomMessage(message: string): {
type: 'MODAL_OPEN';
data: { name: 'info-modal'; prop: { customText: string } };
} {
return {
type: 'MODAL_OPEN',
data: { name: 'info-modal', prop: { customText: message } },
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import Cursor from '../Cursor';
import { ContextMenu, ContextMenuTrigger } from '../ContextMenu';

import InfoPanel from './InfoPanel';
import { KetcherLogger } from 'ketcher-core';
import { KetcherLogger, ketcherProvider } from 'ketcher-core';
import { getSmoothScrollDelta } from './helpers';
import InfoTooltip from './InfoTooltip';

Expand Down Expand Up @@ -151,6 +151,13 @@ class StructEditor extends Component {
this.editor = new Editor(this.editorRef.current, {
...this.props.options,
});
const ketcher = ketcherProvider.getKetcher();
if (ketcher?.editor.macromoleculeConvertionError) {
this.props.onShowMacromoleculesErrorMessage(
ketcher.editor.macromoleculeConvertionError,
);
ketcher.editor.clearMacromoleculeConvertionError();
}
setupEditor(this.editor, this.props);
if (this.props.onInit) this.props.onInit(this.editor);

Expand Down Expand Up @@ -282,6 +289,7 @@ class StructEditor extends Component {
onApiSettings,
showAttachmentPoints = true,
onUpdateFloatingTools,
onShowMacromoleculesErrorMessage,
/* eslint-enable @typescript-eslint/no-unused-vars */
...props
} = this.props;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ function ErrorInfoModal(props) {
headerContent={headerContent}
>
<div>
{isPasteError ? <PasteErrorModalBody /> : defaultCutCopyMessage}
{props.customText ||
(isPasteError ? <PasteErrorModalBody /> : defaultCutCopyMessage)}
</div>
</Dialog>
);
Expand Down

0 comments on commit d723b1d

Please sign in to comment.