annotations work and a re saved

This commit is contained in:
Dobromir Popov
2025-10-18 23:26:54 +03:00
parent 38d6a01f8e
commit 7646137f11
7 changed files with 771 additions and 60 deletions

View File

@@ -6,13 +6,14 @@ class AnnotationManager {
constructor(chartManager) {
this.chartManager = chartManager;
this.pendingAnnotation = null;
this.editingAnnotation = null;
this.enabled = true;
console.log('AnnotationManager initialized');
}
/**
* Handle chart click for marking entry/exit
* Handle chart click for marking entry/exit or editing
*/
handleChartClick(clickData) {
if (!this.enabled) {
@@ -20,6 +21,12 @@ class AnnotationManager {
return;
}
// Check if we're editing an existing annotation
if (this.editingAnnotation) {
this.handleEditClick(clickData);
return;
}
if (!this.pendingAnnotation) {
// Mark entry point
this.markEntry(clickData);
@@ -29,6 +36,95 @@ class AnnotationManager {
}
}
/**
* Handle click while editing an annotation
*/
handleEditClick(clickData) {
const editing = this.editingAnnotation;
const original = editing.original;
if (editing.editMode === 'entry') {
// Update entry point
const newAnnotation = {
...original,
entry: {
timestamp: clickData.timestamp,
price: clickData.price,
index: clickData.index
}
};
// Recalculate P&L
const entryPrice = newAnnotation.entry.price;
const exitPrice = newAnnotation.exit.price;
newAnnotation.direction = exitPrice > entryPrice ? 'LONG' : 'SHORT';
newAnnotation.profit_loss_pct = ((exitPrice - entryPrice) / entryPrice) * 100;
// Delete old annotation and save new one
this.deleteAndSaveAnnotation(editing.annotation_id, newAnnotation);
} else if (editing.editMode === 'exit') {
// Update exit point
const newAnnotation = {
...original,
exit: {
timestamp: clickData.timestamp,
price: clickData.price,
index: clickData.index
}
};
// Recalculate P&L
const entryPrice = newAnnotation.entry.price;
const exitPrice = newAnnotation.exit.price;
newAnnotation.direction = exitPrice > entryPrice ? 'LONG' : 'SHORT';
newAnnotation.profit_loss_pct = ((exitPrice - entryPrice) / entryPrice) * 100;
// Delete old annotation and save new one
this.deleteAndSaveAnnotation(editing.annotation_id, newAnnotation);
}
// Clear editing mode
this.editingAnnotation = null;
window.showSuccess('Annotation updated');
}
/**
* Delete old annotation and save updated one
*/
deleteAndSaveAnnotation(oldId, newAnnotation) {
// Delete old
fetch('/api/delete-annotation', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({annotation_id: oldId})
})
.then(() => {
// Save new
return fetch('/api/save-annotation', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(newAnnotation)
});
})
.then(response => response.json())
.then(data => {
if (data.success) {
// Update app state
window.appState.annotations = window.appState.annotations.filter(a => a.annotation_id !== oldId);
window.appState.annotations.push(data.annotation);
// Update UI
window.renderAnnotationsList(window.appState.annotations);
this.chartManager.removeAnnotation(oldId);
this.chartManager.addAnnotation(data.annotation);
}
})
.catch(error => {
window.showError('Failed to update annotation: ' + error.message);
});
}
/**
* Mark entry point
*/