Skip to content

Commit

Permalink
Cursor position after adding preset in sequence mode causes an incorr…
Browse files Browse the repository at this point in the history
…ect sequence formation

#6272
  • Loading branch information
rrodionov91 committed Feb 6, 2025
1 parent db15485 commit 07bb8af
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
20 changes: 20 additions & 0 deletions packages/ketcher-core/src/application/editor/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,16 @@ export class CoreEditor {
}

private onSelectMonomer(monomer: MonomerItemType) {
if (
this.mode instanceof SequenceMode &&
!this.isSequenceEditMode &&
(SequenceRenderer.chainsCollection.length === 0 ||
(SequenceRenderer.chainsCollection.length === 1 &&
SequenceRenderer.chainsCollection.chains[0]?.isEmpty))
) {
this.mode.turnOnEditMode();
}

if (this.mode instanceof SequenceMode) {
this.mode.insertMonomerFromLibrary(monomer);
} else {
Expand All @@ -433,6 +443,16 @@ export class CoreEditor {
}

private onSelectRNAPreset(preset: IRnaPreset) {
if (
this.mode instanceof SequenceMode &&
!this.isSequenceEditMode &&
(SequenceRenderer.chainsCollection.length === 0 ||
(SequenceRenderer.chainsCollection.length === 1 &&
SequenceRenderer.chainsCollection.chains[0]?.isEmpty))
) {
this.mode.turnOnEditMode();
}

if (this.mode instanceof SequenceMode) {
this.mode.insertPresetFromLibrary(preset);
} else {
Expand Down
18 changes: 8 additions & 10 deletions packages/ketcher-core/src/application/editor/modes/SequenceMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1410,7 +1410,6 @@ export class SequenceMode extends BaseMode {
const history = new EditorHistory(editor);
const modelChanges = new Command();
const selections = SequenceRenderer.selections;
const wasCanvasEmptyBeforeInsertion = SequenceRenderer.isEmptyCanvas();

if (selections.length > 0) {
if (
Expand Down Expand Up @@ -1448,7 +1447,7 @@ export class SequenceMode extends BaseMode {
} else {
this.replaceSelectionsWithMonomer(selections, monomerItem);
}
} else {
} else if (editor.isSequenceEditMode) {
const newNodePosition = this.getNewNodePosition();

const newMonomer = editor.drawingEntitiesManager.createMonomer(
Expand Down Expand Up @@ -1480,14 +1479,11 @@ export class SequenceMode extends BaseMode {

modelChanges.addOperation(new ReinitializeModeOperation());
editor.renderersContainer.update(modelChanges);
SequenceRenderer.moveCaretForward();
SequenceRenderer.setCaretPositionNextToMonomer(
newMonomerSequenceNode.lastMonomerInNode,
);
history.update(modelChanges);
}

if (wasCanvasEmptyBeforeInsertion) {
this.turnOnEditMode();
SequenceRenderer.moveCaretForward();
}
}

private createRnaPresetNode(preset: IRnaPreset, position: Vec2) {
Expand Down Expand Up @@ -1687,7 +1683,7 @@ export class SequenceMode extends BaseMode {
} else {
this.replaceSelectionsWithPreset(selections, preset);
}
} else {
} else if (editor.isSequenceEditMode) {
const newNodePosition = this.getNewNodePosition();

const newPresetNode = this.createRnaPresetNode(preset, newNodePosition);
Expand All @@ -1713,7 +1709,9 @@ export class SequenceMode extends BaseMode {

modelChanges.addOperation(new ReinitializeModeOperation());
editor.renderersContainer.update(modelChanges);
SequenceRenderer.moveCaretForward();
SequenceRenderer.setCaretPositionNextToMonomer(
newPresetNode.lastMonomerInNode,
);
history.update(modelChanges);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ export class SequenceRenderer {
let newCaretPosition = -1;

SequenceRenderer.forEachNode(({ node, nodeIndexOverall }) => {
if (node.monomer === monomer) {
if (node.monomers.includes(monomer)) {
newCaretPosition = nodeIndexOverall;
}
});
Expand All @@ -375,7 +375,7 @@ export class SequenceRenderer {
let newCaretPosition = -1;

SequenceRenderer.forEachNode(({ node, nodeIndexOverall }) => {
if (node.monomer === monomer) {
if (node.monomers.includes(monomer)) {
newCaretPosition = nodeIndexOverall;
}
});
Expand Down Expand Up @@ -925,7 +925,7 @@ export class SequenceRenderer {
let rendererToReturn;

SequenceRenderer.forEachNode(({ node }) => {
if (node.monomer === monomer) {
if (node.monomers.includes(monomer)) {
rendererToReturn = node.renderer;
}
});
Expand Down

0 comments on commit 07bb8af

Please sign in to comment.