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

#5242 - Replace phosphate at the end with petide causes cycled polymer #5271

Merged
merged 2 commits into from
Aug 9, 2024
Merged
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
75 changes: 51 additions & 24 deletions packages/ketcher-core/src/application/editor/modes/SequenceMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1086,10 +1086,13 @@ export class SequenceMode extends BaseMode {
previousSelectionNode?: SubChainNode,
) {
const editor = CoreEditor.provideEditorInstance();
const nextNode = SequenceRenderer.getNextNode(selection.node);
const nextNode = SequenceRenderer.getNextNodeInSameChain(selection.node);
const position = selection.node.monomer.position;

const sideChainConnections = this.preserveSideChainConnections(selection);
const hasPreviousNodeInChain =
selection.node.firstMonomerInNode.attachmentPointsToBonds.R1;
const hasNextNodeInChain =
selection.node.lastMonomerInNode.attachmentPointsToBonds.R2;

selection.node.monomers.forEach((monomer) => {
modelChanges.merge(editor.drawingEntitiesManager.deleteMonomer(monomer));
Expand All @@ -1111,8 +1114,10 @@ export class SequenceMode extends BaseMode {
modelChanges.merge(
this.insertNewSequenceFragment(
newMonomerSequenceNode,
nextNode,
nextNode || null,
previousSelectionNode,
Boolean(hasPreviousNodeInChain),
Boolean(hasNextNodeInChain),
),
);

Expand Down Expand Up @@ -1156,11 +1161,15 @@ export class SequenceMode extends BaseMode {
const modelChanges = new Command();

selections.forEach((selectionRange) => {
let previousReplacedNode = SequenceRenderer.getNodeByPointer(
selectionRange[0].nodeIndexOverall - 1,
let previousReplacedNode = SequenceRenderer.getPreviousNodeInSameChain(
selectionRange[0].node,
);

selectionRange.forEach((nodeSelection) => {
if (nodeSelection.node instanceof EmptySequenceNode) {
return;
}

previousReplacedNode = this.replaceSelectionWithMonomer(
monomerItem,
nodeSelection,
Expand Down Expand Up @@ -1362,8 +1371,12 @@ export class SequenceMode extends BaseMode {
previousSelectionNode?: SubChainNode,
) {
const editor = CoreEditor.provideEditorInstance();
const nextNode = SequenceRenderer.getNextNode(selection.node);
const nextNode = SequenceRenderer.getNextNodeInSameChain(selection.node);
const position = selection.node.monomer.position;
const hasPreviousNodeInChain =
selection.node.firstMonomerInNode.attachmentPointsToBonds.R1;
const hasNextNodeInChain =
selection.node.lastMonomerInNode.attachmentPointsToBonds.R2;

const sideChainConnections = this.preserveSideChainConnections(selection);

Expand Down Expand Up @@ -1417,8 +1430,10 @@ export class SequenceMode extends BaseMode {
modelChanges.merge(
this.insertNewSequenceFragment(
newPresetNode,
nextNode,
nextNode || null,
previousSelectionNode,
Boolean(hasPreviousNodeInChain),
Boolean(hasNextNodeInChain),
),
);

Expand Down Expand Up @@ -1465,11 +1480,15 @@ export class SequenceMode extends BaseMode {
const modelChanges = new Command();

selections.forEach((selectionRange) => {
let previousReplacedNode = SequenceRenderer.getNodeByPointer(
selectionRange[0].nodeIndexOverall - 1,
let previousReplacedNode = SequenceRenderer.getPreviousNodeInSameChain(
selectionRange[0].node,
);

selectionRange.forEach((nodeSelection) => {
if (nodeSelection.node instanceof EmptySequenceNode) {
return;
}

previousReplacedNode = this.replaceSelectionWithPreset(
preset,
nodeSelection,
Expand Down Expand Up @@ -1629,8 +1648,10 @@ export class SequenceMode extends BaseMode {

private insertNewSequenceFragment(
chainsCollectionOrNode: ChainsCollection | SubChainNode,
nextNodeToConnect?: SubChainNode,
nextNodeToConnect?: SubChainNode | null,
previousNodeToConnect?: SubChainNode,
needConnectWithPreviousNodeInChain = true,
needConnectWithNextNodeInChain = true,
) {
const chainsCollection =
chainsCollectionOrNode instanceof ChainsCollection
Expand All @@ -1639,7 +1660,9 @@ export class SequenceMode extends BaseMode {
new Chain().addNode(chainsCollectionOrNode),
);
const currentNode =
nextNodeToConnect || SequenceRenderer.currentEdittingNode;
nextNodeToConnect === null
? undefined
: nextNodeToConnect || SequenceRenderer.currentEdittingNode;
const previousNodeInSameChain =
previousNodeToConnect || SequenceRenderer.previousNodeInSameChain;
const modelChanges = new Command();
Expand All @@ -1649,20 +1672,24 @@ export class SequenceMode extends BaseMode {

this.deleteBondToNextNodeInChain(previousNodeInSameChain, modelChanges);

this.connectNodes(
previousNodeInSameChain,
firstNodeOfNewFragment,
modelChanges,
newNodePosition,
currentNode,
);
if (needConnectWithPreviousNodeInChain) {
this.connectNodes(
previousNodeInSameChain,
firstNodeOfNewFragment,
modelChanges,
newNodePosition,
currentNode,
);
}

this.connectNodes(
lastNodeOfNewFragment,
currentNode,
modelChanges,
newNodePosition,
);
if (needConnectWithNextNodeInChain) {
this.connectNodes(
lastNodeOfNewFragment,
currentNode,
modelChanges,
newNodePosition,
);
}

return modelChanges;
}
Expand Down
Loading