Skip to content

Commit 77f837f

Browse files
fix #519
1 parent 9006fc3 commit 77f837f

4 files changed

Lines changed: 14 additions & 8 deletions

File tree

dist/main.bundle.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/main.bundle.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/main.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2361,9 +2361,13 @@ async function closestIso(value: number): Promise<void> {
23612361
if (energy && name) {
23622362
const newIso: [string, number] = [name, energy];
23632363

2364-
if (prevIso !== newIso) prevIso = newIso;
2364+
const isNewToggleLine = plot.toggleLine(energy, name);
23652365

2366-
plot.toggleLine(energy, name);
2366+
// Check if there is already a line for this energy
2367+
if (prevIso !== newIso && isNewToggleLine) prevIso = newIso;
2368+
2369+
// If there was a duplicate, the line was not toggled, so we don't set prevIso
2370+
if (!isNewToggleLine) prevIso = undefined;
23672371
}
23682372
plot.updatePlot(spectrumData);
23692373
}

source/plot.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ export class SpectrumPlot {
592592
/*
593593
Add a line
594594
*/
595-
toggleLine(energy: number, name: string, enabled = true, height = -1): void {
595+
toggleLine(energy: number, name: string, enabled = true, height = -1): boolean {
596596
//name = name.replaceAll('-',''); // Remove - to save space
597597
const hovertext = energy.toFixed(2);
598598

@@ -646,15 +646,16 @@ export class SpectrumPlot {
646646
newAnno.bgcolor = this.darkMode ? this.annoBgDark : this.annoBgLight;
647647
}
648648

649+
// Check if the line or annotation already exists. If it exists, do not add it again!
649650
for (const shape of this.shapes) {
650-
if (shape.x0 === newLine.x0) return;
651+
if (shape.x0 === newLine.x0) return false;
651652
}
652653

653654
for (const anno of this.annotations) {
654-
if (anno.hovertext === newAnno.hovertext) return;
655+
if (anno.hovertext === newAnno.hovertext) return false;
655656
}
656657

657-
// Not a duplicate
658+
// Not a duplicate, add the line and annotation
658659
this.shapes.push(newLine);
659660
this.annotations.push(newAnno);
660661
} else {
@@ -665,6 +666,7 @@ export class SpectrumPlot {
665666
if (this.annotations[i].hovertext === hovertext) this.annotations.splice(parseInt(i),1);
666667
}
667668
}
669+
return true; // Return true if the line was added or removed, return false if it was already there (duplicate)
668670
}
669671
/*
670672
Clear annotations and shapes

0 commit comments

Comments
 (0)