|
1 | 1 | import { Logical, MouseEventParams, Time } from "lightweight-charts"; |
2 | 2 | import { defaultShapeOptions, Point } from "./classes"; |
3 | | -import { chart, fillOpacityElement, lineSeries, seriesPricePrecision, state } from "./data"; |
| 3 | +import { chart, fillOpacityElement, lineSeries, seriesPricePrecision, shapeDrawingSelectionElement, state } from "./data"; |
4 | 4 | import { HoveredObject, ShapeDrawing } from "../shape-drawing"; |
5 | 5 |
|
| 6 | +const selectedButtonColor = '#aaa'; |
| 7 | +const deselectedButtonColor = '#ccc'; |
| 8 | + |
6 | 9 | export function shapeDrawingSelection(event: MouseEvent) { |
7 | 10 | if (!(event.target instanceof HTMLButtonElement)) { |
8 | 11 | return; |
9 | 12 | } |
10 | 13 |
|
| 14 | + if (state.currentlySelectedShape) { |
| 15 | + selectShape(null); |
| 16 | + } |
| 17 | + |
11 | 18 | if (state.shapeToDraw === event.target.textContent) { // Deselect it |
12 | | - event.target.style.backgroundColor = '#ccc'; |
| 19 | + event.target.style.backgroundColor = deselectedButtonColor; |
13 | 20 | state.shapeToDraw = ''; |
14 | 21 | state.edgeCount = 0; |
15 | 22 | state.shapeOptions = defaultShapeOptions; |
16 | 23 | return; |
17 | 24 | } |
18 | 25 |
|
19 | | - event.target.style.backgroundColor = '#aaa'; |
| 26 | + if (state.shapeToDraw !== '') { |
| 27 | + // Fetch the shapeToDraw button by iterating through all buttons and finding the one with the same textContent |
| 28 | + const shapeToDrawButton = Array.from(shapeDrawingSelectionElement.children).find( |
| 29 | + button => (button as HTMLButtonElement).textContent === state.shapeToDraw |
| 30 | + ); |
| 31 | + if (shapeToDrawButton) { |
| 32 | + (shapeToDrawButton as HTMLButtonElement).style.backgroundColor = deselectedButtonColor; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + event.target.style.backgroundColor = selectedButtonColor; |
20 | 37 |
|
21 | 38 | state.shapeToDraw = event.target.textContent!; |
22 | 39 |
|
@@ -195,6 +212,17 @@ export function chartCrosshairMoveEvent(event: MouseEventParams<Time>) { |
195 | 212 | export function keyUpEvent(event: KeyboardEvent) { |
196 | 213 | switch (event.key) { |
197 | 214 | case 'Escape': |
| 215 | + if (state.shapeToDraw !== '') { |
| 216 | + // Fetch the shapeToDraw button by iterating through all buttons and finding the one with the same textContent |
| 217 | + const shapeToDrawButton = Array.from(shapeDrawingSelectionElement.children).find( |
| 218 | + button => (button as HTMLButtonElement).textContent === state.shapeToDraw |
| 219 | + ); |
| 220 | + if (shapeToDrawButton) { |
| 221 | + (shapeToDrawButton as HTMLButtonElement).style.backgroundColor = deselectedButtonColor; |
| 222 | + } |
| 223 | + state.shapeToDraw = ''; |
| 224 | + } |
| 225 | + |
198 | 226 | if (state.currentlySelectedShape) { |
199 | 227 | selectShape(null); |
200 | 228 | } |
|
0 commit comments