Skip to content

Commit

Permalink
#4297 - Macro: When removing nucleotides from the canvas, the preview…
Browse files Browse the repository at this point in the history
… of the nucleotide does not disappear, if you leave the cursor over it

- added removing preview on mousemove and keypress
- added getting canvas and canvasWrapper from ZoomTool to find it in DOM once instead of on each renderer creation
- added async rendering after each keypress event handled in sequence edit mode
  • Loading branch information
rrodionov91 committed Mar 21, 2024
1 parent c3f9d1f commit e2921f7
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 16 deletions.
4 changes: 2 additions & 2 deletions packages/ketcher-core/src/application/editor/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ export class CoreEditor {

private setupKeyboardEvents() {
this.setupHotKeysEvents();
document.addEventListener('keydown', (event: KeyboardEvent) => {
this.mode.onKeyDown(event);
document.addEventListener('keydown', async (event: KeyboardEvent) => {
await this.mode.onKeyDown(event);
});
}

Expand Down
2 changes: 2 additions & 0 deletions packages/ketcher-core/src/application/editor/editorEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function resetEditorEvents() {
editSequence: new Subscription(),
startNewSequence: new Subscription(),
mouseOverSequenceItem: new Subscription(),
mouseOnMoveSequenceItem: new Subscription(),
mouseLeaveSequenceItem: new Subscription(),
changeSequenceTypeEnterMode: new Subscription(),
toggleSequenceEditMode: new Subscription(),
Expand All @@ -58,6 +59,7 @@ export const renderersEvents: ToolEventHandlerName[] = [
'editSequence',
'startNewSequence',
'mouseOverSequenceItem',
'mouseOnMoveSequenceItem',
'mouseLeaveSequenceItem',
'changeSequenceTypeEnterMode',
'toggleSequenceEditMode',
Expand Down
16 changes: 11 additions & 5 deletions packages/ketcher-core/src/application/editor/modes/SequenceMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,22 @@ export class SequenceMode extends BaseMode {
editor.events.toggleSequenceEditMode.dispatch(false);
}

public onKeyDown(event: KeyboardEvent) {
public async onKeyDown(event: KeyboardEvent) {
if (!this.isEditMode) {
return;
}
const hotKeys = initHotKeys(this.keyboardEventHandlers);
const shortcutKey = keyNorm.lookup(hotKeys, event);

this.keyboardEventHandlers[shortcutKey]?.handler(event);
await new Promise<void>((resolve) => {
setTimeout(() => {
const hotKeys = initHotKeys(this.keyboardEventHandlers);
const shortcutKey = keyNorm.lookup(hotKeys, event);
const editor = CoreEditor.provideEditorInstance();

return true;
this.keyboardEventHandlers[shortcutKey]?.handler(event);
editor.events.mouseLeaveSequenceItem.dispatch();
resolve();
}, 0);
});
}

public startNewSequence() {
Expand Down
2 changes: 2 additions & 0 deletions packages/ketcher-core/src/application/editor/tools/Tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ interface ToolEventHandler {

mouseOverSequenceItem?(event: Event): void;

mouseOnMoveSequenceItem?(event: Event): void;

mouseLeaveSequenceItem?(event: Event): void;

changeSequenceTypeEnterMode?(event: Event): void;
Expand Down
4 changes: 2 additions & 2 deletions packages/ketcher-core/src/application/editor/tools/Zoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ const AUTO_SCROLL_OFFSET_X = 10;
const AUTO_SCROLL_OFFSET_Y = 10;

class ZoomTool implements BaseTool {
private canvas: D3SvgElementSelection<SVGSVGElement, void>;
private canvasWrapper: D3SvgElementSelection<SVGSVGElement, void>;
public canvas: D3SvgElementSelection<SVGSVGElement, void>;
public canvasWrapper: D3SvgElementSelection<SVGSVGElement, void>;
private zoom!: ZoomBehavior<SVGSVGElement, void> | null;
private zoomLevel: number;
private zoomTransform: ZoomTransform;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { select } from 'd3';
import { DrawingEntity } from 'domain/entities/DrawingEntity';
import { D3SvgElementSelection } from 'application/render/types';
import { provideEditorSettings } from 'application/editor/editorSettings';
import {
canvasSelector,
drawnStructuresSelector,
} from 'application/editor/constants';
import ZoomTool from 'application/editor/tools/Zoom';

export interface IBaseRenderer {
show(theme): void;
Expand All @@ -31,8 +27,8 @@ export abstract class BaseRenderer implements IBaseRenderer {

protected canvas: D3SvgElementSelection<SVGSVGElement, void>;
protected constructor(public drawingEntity: DrawingEntity) {
this.canvasWrapper = select(canvasSelector);
this.canvas = select(drawnStructuresSelector);
this.canvasWrapper = ZoomTool.instance.canvasWrapper;
this.canvas = ZoomTool.instance.canvas;
}

protected get editorSettings() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ export abstract class BaseSequenceItemRenderer extends BaseSequenceRenderer {
this.textElement.on('mouseover', (event) => {
this.editorEvents.mouseOverSequenceItem.dispatch(event);
});
this.textElement.on('mousemove', (event) => {
this.editorEvents.mouseOnMoveSequenceItem.dispatch(event);
});
this.textElement.on('mouseleave', (event) => {
this.editorEvents.mouseLeaveSequenceItem.dispatch(event);
});
Expand Down
3 changes: 3 additions & 0 deletions packages/ketcher-macromolecules/src/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -296,10 +296,13 @@ function Editor({ theme, togglerComponent }: EditorProps) {
}
};
editor?.events.mouseOnMoveMonomer.add(onMoveHandler);
editor?.events.mouseOnMoveSequenceItem.add(onMoveHandler);

return () => {
editor?.events.mouseOverMonomer.remove(handleOpenPreview);
editor?.events.mouseLeaveMonomer.remove(handleClosePreview);
editor?.events.mouseOnMoveMonomer.remove(onMoveHandler);
editor?.events.mouseOnMoveSequenceItem.remove(onMoveHandler);
editor?.events.mouseOverSequenceItem.remove(handleOpenPreview);
editor?.events.mouseLeaveSequenceItem.remove(handleClosePreview);
};
Expand Down

0 comments on commit e2921f7

Please sign in to comment.