Skip to content

Commit

Permalink
feat: provide coordinates in idcode
Browse files Browse the repository at this point in the history
  • Loading branch information
tpoisseau committed Oct 15, 2024
1 parent a099f97 commit f609534
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions lib/canvas_editor/init/canvas_editor_element.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function initCanvasEditorElement(CanvasEditor, Molecule, ReactionEncoder) {
*/
setMolecule(molecule) {
this.fragment = molecule.isFragment();
this.idcode = molecule.getIDCode();
this.idcode = `${molecule.getIDCode()} ${molecule.getIDCoordinates()}`;

this.#editor.setMolecule(molecule);
}
Expand All @@ -91,7 +91,13 @@ function initCanvasEditorElement(CanvasEditor, Molecule, ReactionEncoder) {
*/
setReaction(reaction) {
this.fragment = reaction.isFragment();
this.idcode = ReactionEncoder.encode(reaction);
this.idcode = ReactionEncoder.encode(
reaction,
true,
ReactionEncoder.INCLUDE_MAPPING |
ReactionEncoder.INCLUDE_COORDS |
ReactionEncoder.RETAIN_REACTANT_AND_PRODUCT_ORDER,
);

this.#editor.setReaction(reaction);
}
Expand Down Expand Up @@ -146,11 +152,27 @@ function initCanvasEditorElement(CanvasEditor, Molecule, ReactionEncoder) {
}
}

/**
* @param {string} idcodeAndCoordinates
*/
#moleculeFromIdCode(idcodeAndCoordinates) {
const index = idcodeAndCoordinates.indexOf(' ');

if (index === -1) {
return Molecule.fromIDCode(idcodeAndCoordinates);
}

const idcode = idcodeAndCoordinates.slice(0, index);
const coordinates = idcodeAndCoordinates.slice(index + 1);

return Molecule.fromIDCode(idcode, coordinates);
}

/**
* @this {CanvasEditorElement}
*/
#initMolecule() {
const molecule = Molecule.fromIDCode(this.idcode);
const molecule = this.#moleculeFromIdCode(this.idcode);
molecule.setFragment(this.fragment);

this.#editor.setMolecule(molecule);
Expand All @@ -160,7 +182,7 @@ function initCanvasEditorElement(CanvasEditor, Molecule, ReactionEncoder) {
* @this {CanvasEditorElement}
*/
#initReaction() {
const reaction = ReactionEncoder.decode(this.idcode);
const reaction = ReactionEncoder.decode(this.idcode, true);
reaction.setFragment(this.fragment);

this.#editor.setReaction(reaction);
Expand Down Expand Up @@ -198,13 +220,19 @@ function initCanvasEditorElement(CanvasEditor, Molecule, ReactionEncoder) {
switch (this.mode) {
case CanvasEditorElement.MODE.MOLECULE: {
const molecule = this.getMolecule();
this.idcode = molecule.getIDCode();
this.idcode = `${molecule.getIDCode()} ${molecule.getIDCoordinates()}`;
this.fragment = molecule.isFragment();
break;
}
case CanvasEditorElement.MODE.REACTION: {
const reaction = this.getReaction();
this.idcode = ReactionEncoder.encode(reaction);
this.idcode = ReactionEncoder.encode(
reaction,
true,
ReactionEncoder.INCLUDE_MAPPING |
ReactionEncoder.INCLUDE_COORDS |
ReactionEncoder.RETAIN_REACTANT_AND_PRODUCT_ORDER,
);
this.fragment = reaction.isFragment();
break;
}
Expand Down

0 comments on commit f609534

Please sign in to comment.