Skip to content

Commit

Permalink
#3129 – refactor after review
Browse files Browse the repository at this point in the history
  • Loading branch information
Nitvex committed Sep 26, 2023
1 parent 64e38a3 commit c33ade3
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions packages/ketcher-react/src/script/editor/tool/templatePreview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,46 +99,58 @@ class TemplatePreview {
movePreview(event: PointerEvent) {
this.position = this.editor.render.page2obj(event);

const ci = this.getPreviewTarget();
const isMouseAwayFromAtomsAndBonds = !ci;
const previewTarget = this.getPreviewTarget();
const isMouseAwayFromAtomsAndBonds = !previewTarget;
const isPreviewTargetChanged =
ci && this.lastPreviewId !== getUniqueCiId(ci);
previewTarget && this.lastPreviewId !== getUniqueCiId(previewTarget);

const shouldHidePreview =
isMouseAwayFromAtomsAndBonds || isPreviewTargetChanged;

const shouldShowPreview =
ci && !this.connectedPreviewAction && !this.connectedPreviewTimeout;
previewTarget &&
!this.connectedPreviewAction &&
!this.connectedPreviewTimeout;

if (shouldHidePreview) {
this.hideConnectedPreview();
}

if (shouldShowPreview) {
this.lastPreviewId = getUniqueCiId(ci);
this.lastPreviewId = getUniqueCiId(previewTarget);
this.connectedPreviewTimeout = setTimeout(() => {
this.previousPosition = this.position;
this.hideFloatingPreview();
this.showConnectedPreview(event, ci);
this.showConnectedPreview(event, previewTarget);
}, PREVIEW_DELAY);
} else if (shouldHidePreview) {
if (!this.floatingPreview) {
this.showFloatingPreview(this.position);
this.previousPosition = this.position;
this.editor.render.update(false, null, { resizeCanvas: false });
} else {
const dist = this.position.sub(this.previousPosition);
this.previousPosition = this.position;
fromMultipleMove(this.restruct, this.floatingPreview, dist);
this.editor.render.update(false, null, { resizeCanvas: false });
this.hoverFusedItems(ci, event);
this.moveFloatingPreview(previewTarget, event);
}
}
}

private hoverFusedItems(ci: ClosestItemType | null, event: PointerEvent) {
private moveFloatingPreview(
previewTarget: ClosestItemType | null,
event: PointerEvent,
) {
const dist = this.position.sub(this.previousPosition);
this.previousPosition = this.position;
fromMultipleMove(this.restruct, this.floatingPreview, dist);
this.editor.render.update(false, null, { resizeCanvas: false });
this.hoverFusedItems(previewTarget, event);
}

private hoverFusedItems(
closestItem: ClosestItemType | null,
event: PointerEvent,
) {
if (this.mode === 'fg') {
this.editor.hover(ci, null, event);
this.editor.hover(closestItem, null, event);
} else {
const mergeItems = getItemsToFuse(this.editor, this.floatingPreview);
this.editor.hover(getHoverToFuse(mergeItems));
Expand Down

0 comments on commit c33ade3

Please sign in to comment.