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

#5990 – Extend Ketcher API to allow updating monomers library #6020

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
55 changes: 50 additions & 5 deletions packages/ketcher-core/src/application/editor/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ interface ICoreEditorConstructorParams {
theme;
canvas: SVGSVGElement;
mode?: BaseMode;
monomersLibraryUpdate?: string | JSON;
}

function isMouseMainButtonPressed(event: MouseEvent) {
Expand Down Expand Up @@ -97,7 +98,12 @@ export class CoreEditor {
private pasteEventHandler: (event: ClipboardEvent) => void = () => {};
private keydownEventHandler: (event: KeyboardEvent) => void = () => {};

constructor({ theme, canvas, mode }: ICoreEditorConstructorParams) {
constructor({
theme,
canvas,
mode,
monomersLibraryUpdate,
}: ICoreEditorConstructorParams) {
this._type = EditorType.Macromolecules;
this.theme = theme;
this.canvas = canvas;
Expand All @@ -109,6 +115,9 @@ export class CoreEditor {
this.events = editorEvents;
this.setMonomersLibrary(monomersDataRaw);
this._monomersLibraryParsedJson = JSON.parse(monomersDataRaw);
if (monomersLibraryUpdate) {
this.updateMonomersLibrary(monomersLibraryUpdate);
}
this.subscribeEvents();
this.renderersContainer = new RenderersManager({ theme });
this.drawingEntitiesManager = new DrawingEntitiesManager();
Expand All @@ -131,13 +140,49 @@ export class CoreEditor {
return editor;
}

private setMonomersLibrary(monomersDataRaw: string) {
const monomersLibraryParsedJson = JSON.parse(monomersDataRaw);
this._monomersLibraryParsedJson = monomersLibraryParsedJson;
private parseMonomersLibrary(monomersDataRaw: string | JSON) {
const monomersLibraryParsedJson =
typeof monomersDataRaw === 'string'
? JSON.parse(monomersDataRaw)
: monomersDataRaw;
const serializer = new KetSerializer();
this._monomersLibrary = serializer.convertMonomersLibrary(
const monomersLibrary = serializer.convertMonomersLibrary(
monomersLibraryParsedJson,
);

return { monomersLibraryParsedJson, monomersLibrary };
}

private setMonomersLibrary(monomersDataRaw: string) {
const { monomersLibraryParsedJson, monomersLibrary } =
this.parseMonomersLibrary(monomersDataRaw);
this._monomersLibraryParsedJson = monomersLibraryParsedJson;
this._monomersLibrary = monomersLibrary;
}

public updateMonomersLibrary(monomersDataRaw: string | JSON) {
const { monomersLibraryParsedJson, monomersLibrary } =
this.parseMonomersLibrary(monomersDataRaw);

this._monomersLibraryParsedJson = monomersLibraryParsedJson;
monomersLibrary.forEach((newMonomer) => {
const existingMonomerIndex = this._monomersLibrary.findIndex(
(monomer) => {
return (
monomer?.props?.MonomerName === newMonomer?.props?.MonomerName &&
monomer?.props?.MonomerClass === newMonomer?.props?.MonomerClass
);
},
);

if (existingMonomerIndex !== -1) {
this._monomersLibrary[existingMonomerIndex] = newMonomer;
} else {
this._monomersLibrary.push(newMonomer);
}
});

this.events.updateMonomersLibrary.dispatch();
}

public get monomersLibraryParsedJson() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177269,4 +177269,4 @@
}
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export function resetEditorEvents() {
doubleClickOnSequenceItem: new Subscription(),
openConfirmationDialog: new Subscription(),
mouseUpAtom: new Subscription(),
updateMonomersLibrary: new Subscription(),
};
}
resetEditorEvents();
Expand Down
15 changes: 15 additions & 0 deletions packages/ketcher-core/src/application/ketcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
import {
deleteAllEntitiesOnCanvas,
getStructure,
ketcherProvider,
parseAndAddMacromoleculesOnCanvas,
prepareStructToRender,
} from './utils';
Expand Down Expand Up @@ -535,4 +536,18 @@ export class Ketcher {
public sendCustomAction(name: string) {
this.eventBus.emit('CUSTOM_BUTTON_PRESSED', name);
}

public updateMonomersLibrary(rawMonomersData: string | JSON) {
const editor = CoreEditor.provideEditorInstance();

ketcherProvider.getKetcher();

if (!editor) {
throw new Error(
'Updating monomer library in small molecules mode is not allowed, please switch to macromolecules mode',
);
}

editor.updateMonomersLibrary(rawMonomersData);
}
}
26 changes: 18 additions & 8 deletions packages/ketcher-macromolecules/src/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,21 @@ import { EditorEvents } from './EditorEvents';

const muiTheme = createTheme(muiOverrides);

interface EditorContainerProps {
onInit?: () => void;
interface EditorProps {
theme?: DeepPartial<EditorTheme>;
togglerComponent?: JSX.Element;
monomersLibraryUpdate?: string | JSON;
}

interface EditorProps {
theme?: DeepPartial<EditorTheme>;
togglerComponent?: JSX.Element;
interface EditorContainerProps extends EditorProps {
onInit?: () => void;
}

function EditorContainer({
onInit,
theme,
togglerComponent,
monomersLibraryUpdate,
}: EditorContainerProps) {
const rootElRef = useRef<HTMLDivElement>(null);
const editorTheme: EditorTheme = theme
Expand All @@ -126,14 +126,22 @@ function EditorContainer({
<ThemeProvider theme={mergedTheme}>
<Global styles={getGlobalStyles} />
<EditorWrapper ref={rootElRef} className={EditorClassName}>
<Editor theme={editorTheme} togglerComponent={togglerComponent} />
<Editor
theme={editorTheme}
togglerComponent={togglerComponent}
monomersLibraryUpdate={monomersLibraryUpdate}
/>
</EditorWrapper>
</ThemeProvider>
</Provider>
);
}

function Editor({ theme, togglerComponent }: EditorProps) {
function Editor({
theme,
togglerComponent,
monomersLibraryUpdate,
}: EditorProps) {
const dispatch = useAppDispatch();
const canvasRef = useRef<SVGSVGElement>(null);
const errorTooltipText = useAppSelector(selectErrorTooltipText);
Expand All @@ -151,7 +159,9 @@ function Editor({ theme, togglerComponent }: EditorProps) {
});

useEffect(() => {
dispatch(createEditor({ theme, canvas: canvasRef.current }));
dispatch(
createEditor({ theme, canvas: canvasRef.current, monomersLibraryUpdate }),
);

return () => {
dispatch(destroyEditor(null));
Expand Down
13 changes: 13 additions & 0 deletions packages/ketcher-macromolecules/src/EditorEvents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
PreviewType,
} from 'state/types';
import { calculateBondPreviewPosition } from 'ketcher-react';
import { loadMonomerLibrary } from 'state/library';

const noPreviewTools = ['bond-single'];

Expand All @@ -55,6 +56,18 @@ export const EditorEvents = () => {
const dispatch = useAppDispatch();
const presets = useAppSelector(selectAllPresets);

const handleMonomersLibraryUpdate = useCallback(() => {
dispatch(loadMonomerLibrary(editor?.monomersLibrary));
}, [editor]);

useEffect(() => {
editor?.events.updateMonomersLibrary.add(handleMonomersLibraryUpdate);

return () => {
editor?.events.updateMonomersLibrary.remove(handleMonomersLibraryUpdate);
};
}, [editor]);

useEffect(() => {
const handler = (toolName: string) => {
if (toolName !== activeTool) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export const editorSlice: Slice = createSlice({
action: PayloadAction<{
theme: DeepPartial<ThemeType>;
canvas: SVGSVGElement;
monomersLibraryUpdate?: string | JSON;
}>,
) => {
state.editor = new CoreEditor({
Expand All @@ -78,6 +79,7 @@ export const editorSlice: Slice = createSlice({
mode: state.editorLayoutMode
? new modesMap[state.editorLayoutMode]()
: undefined,
monomersLibraryUpdate: action.payload.monomersLibraryUpdate,
});
},
destroyEditor: (state) => {
Expand Down
Loading