Skip to content

Commit

Permalink
- in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
rrodionov91 committed Oct 13, 2023
1 parent 60f8577 commit 7ba8b23
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 28 deletions.
4 changes: 2 additions & 2 deletions packages/ketcher-core/src/application/editor/tools/Bond.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ class PolymerBond implements BaseTool {
}

public mouseLeavePolymerBond(event) {
if (this.bondRenderer) return;

const renderer: PolymerBondRenderer = event.target.__data__;
if (this.bondRenderer || !renderer.polymerBond) return;

const modelChanges =
this.editor.drawingEntitiesManager.hidePolymerBondInformation(
renderer.polymerBond,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@ export abstract class BaseMonomerRenderer extends BaseRenderer {
private isSnakeBondForAttachmentPoint(
attachmentPointName: AttachmentPointName,
) {
return this.monomer.attachmentPointsToBonds[attachmentPointName]?.renderer
?.isSnake;
return (
this.monomer.attachmentPointsToBonds[attachmentPointName]?.renderer
?.isSnake &&
!this.monomer.attachmentPointsToBonds[
attachmentPointName
]?.renderer?.isMonomersOnSameHorizontalLine()
);
}

public get center() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class PolymerBondRenderer extends BaseRenderer {
private editorEvents: typeof editorEvents;
private selectionElement;
private path = '';

private previousStateOfIsMonomersOnSameHorisontalLine: boolean | undefined;
constructor(public polymerBond: PolymerBond) {
super(polymerBond as DrawingEntity);
this.polymerBond.setRenderer(this);
Expand Down Expand Up @@ -85,13 +85,23 @@ export class PolymerBondRenderer extends BaseRenderer {
}

public moveSelection() {
assert(this.rootElement);
this.moveStart();
this.moveEnd();
if (
this.previousStateOfIsMonomersOnSameHorisontalLine !==
this.isMonomersOnSameHorizontalLine()
) {
this.remove();
this.show();
} else {
assert(this.rootElement);
this.moveStart();
this.moveEnd();
}
this.previousStateOfIsMonomersOnSameHorisontalLine =
this.isMonomersOnSameHorizontalLine();
}

public appendBond(rootElement) {
if (this.isSnake) {
if (this.isSnake && !this.isMonomersOnSameHorizontalLine()) {
this.appendSnakeBond(rootElement);
} else {
this.appendBondGraph(rootElement);
Expand Down Expand Up @@ -136,10 +146,6 @@ export class PolymerBondRenderer extends BaseRenderer {
}

private updateSnakeBondPath(startPosition, endPosition) {
if (this.isMonomersOnSameHorizontalLine()) {
this.addRandomLine(startPosition, endPosition);
return;
}
if (this.isSecondMonomerBottomRight(startPosition, endPosition)) {
this.addLine(
LINE_DIRECTION.Horizontal,
Expand Down Expand Up @@ -437,7 +443,7 @@ export class PolymerBondRenderer extends BaseRenderer {
}

public moveEnd() {
if (this.isSnake) {
if (this.isSnake && !this.isMonomersOnSameHorizontalLine()) {
this.moveSnakeBondEnd();
} else {
this.moveGraphBondEnd();
Expand Down Expand Up @@ -474,7 +480,7 @@ export class PolymerBondRenderer extends BaseRenderer {
}

public moveStart() {
if (this.isSnake) {
if (this.isSnake && !this.isMonomersOnSameHorizontalLine()) {
this.moveSnakeBondStart();
} else {
this.moveGraphBondStart();
Expand Down Expand Up @@ -511,7 +517,7 @@ export class PolymerBondRenderer extends BaseRenderer {
}

protected appendHoverAreaElement() {
if (this.isSnake) {
if (this.isSnake && !this.isMonomersOnSameHorizontalLine()) {
(<D3SvgElementSelection<SVGPathElement, void> | undefined>(
this.hoverAreaElement
)) = this.rootElement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class RenderersManager {

public finishPolymerBondCreation(polymerBond) {
assert(polymerBond.secondMonomer);
polymerBond.renderer?.moveEnd();
polymerBond.renderer?.moveSelection();
polymerBond.renderer?.redrawHover();
polymerBond.firstMonomer.renderer?.redrawAttachmentPoints();
polymerBond.firstMonomer.renderer?.drawSelection();
Expand Down
62 changes: 51 additions & 11 deletions packages/ketcher-core/src/domain/entities/DrawingEntitiesManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,7 @@ export class DrawingEntitiesManager {
polymerBond.firstMonomer.removePotentialBonds();
polymerBond.secondMonomer.removePotentialBonds();

polymerBond.moveBondEndAbsolute(
secondMonomer.renderer.center.x,
secondMonomer.renderer.center.y,
);
polymerBond.moveToLinkedMonomers();

polymerBond.firstMonomer.turnOffSelection();
polymerBond.firstMonomer.turnOffHover();
Expand Down Expand Up @@ -379,7 +376,6 @@ export class DrawingEntitiesManager {
polymerBond?: PolymerBond,
) {
const command = new Command();
assert(polymerBond);
if (
polymerBond &&
polymerBond.firstMonomer.getPotentialAttachmentPointByBond(
Expand Down Expand Up @@ -530,6 +526,27 @@ export class DrawingEntitiesManager {
return command;
}

private findChainByMonomer(
monomer: BaseMonomer,
monomerChain = [],
previousMonomer?: BaseMonomer,
) {
monomerChain.push(monomer);
for (const attachmentPointName in monomer.attachmentPointsToBonds) {
const polymerBond = monomer.attachmentPointsToBonds[attachmentPointName];
if (polymerBond) {
const nextMonomer =
monomer === polymerBond.firstMonomer
? polymerBond.secondMonomer
: polymerBond.firstMonomer;
if (previousMonomer !== nextMonomer) {
this.findChainByMonomer(nextMonomer, monomerChain, monomer);
}
}
}
return monomerChain;
}

private rearrangeChain(
monomer: BaseMonomer,
initialPosition: Vec2,
Expand Down Expand Up @@ -564,12 +581,14 @@ export class DrawingEntitiesManager {
polymerBond.secondMonomer === monomer
? polymerBond.firstMonomer
: polymerBond.secondMonomer;
if (attachmentPointName === 'R1' || nextMonomer === lastMonomer) {
if (nextMonomer === lastMonomer) {
continue;
}
if (
attachmentPointName === 'R2' &&
nextMonomer.getAttachmentPointByBond(polymerBond) === 'R1'
(attachmentPointName === 'R2' &&
nextMonomer.getAttachmentPointByBond(polymerBond) === 'R1') ||
(attachmentPointName === 'R1' &&
nextMonomer.getAttachmentPointByBond(polymerBond) === 'R2')
) {
const isMonomerFitCanvas =
newPosition.x +
Expand Down Expand Up @@ -658,14 +677,35 @@ export class DrawingEntitiesManager {
) === 'R1'
);
});

firstMonomersInChains.sort((monomer1, monomer2) => {
if (
monomer2.position.x + monomer2.position.y <
monomer1.position.x + monomer1.position.y
) {
return 1;
} else {
return -1;
} else {
return 1;
}
});

const filteredFirstMonomersInChains = [];

firstMonomersInChains.forEach((monomer, monomerIndex) => {
const currentMonomerChain = this.findChainByMonomer(monomer);
let isFirstMonomerInChain = true;
firstMonomersInChains.forEach(
(potentialFirstMonomer, potentialFirstMonomerIndex) => {
if (
potentialFirstMonomerIndex > monomerIndex &&
currentMonomerChain.includes(potentialFirstMonomer)
) {
isFirstMonomerInChain = false;
}
},
);
if (isFirstMonomerInChain) {
filteredFirstMonomersInChains.push(monomer);
}
});
const command = new Command();
Expand All @@ -674,7 +714,7 @@ export class DrawingEntitiesManager {
y: MONOMER_START_Y_POSITION,
});

firstMonomersInChains.forEach((monomer, monomerIndex) => {
filteredFirstMonomersInChains.reverse().forEach((monomer, monomerIndex) => {
const rearrangeResult = this.rearrangeChain(
monomer,
lastPosition,
Expand Down

0 comments on commit 7ba8b23

Please sign in to comment.