@forgecharts/sdk 1.1.28 → 1.1.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +8100 -48
- package/dist/index.js.map +1 -1
- package/dist/internal.js +8851 -31
- package/dist/internal.js.map +1 -1
- package/dist/react/index.js +11558 -27
- package/dist/react/index.js.map +1 -1
- package/dist/react/internal.js +12147 -43
- package/dist/react/internal.js.map +1 -1
- package/package.json +1 -1
- package/dist/__tests__/backwardCompatibility.test.js +0 -159
- package/dist/__tests__/backwardCompatibility.test.js.map +0 -1
- package/dist/__tests__/candleInvariant.test.js +0 -415
- package/dist/__tests__/candleInvariant.test.js.map +0 -1
- package/dist/__tests__/public-api-surface.js +0 -38
- package/dist/__tests__/public-api-surface.js.map +0 -1
- package/dist/__tests__/timeframeBoundary.test.js +0 -452
- package/dist/__tests__/timeframeBoundary.test.js.map +0 -1
- package/dist/api/DrawingManager.js +0 -190
- package/dist/api/DrawingManager.js.map +0 -1
- package/dist/api/EventBus.js +0 -44
- package/dist/api/EventBus.js.map +0 -1
- package/dist/api/IndicatorDAG.js +0 -316
- package/dist/api/IndicatorDAG.js.map +0 -1
- package/dist/api/IndicatorRegistry.js +0 -39
- package/dist/api/IndicatorRegistry.js.map +0 -1
- package/dist/api/LayoutManager.js +0 -51
- package/dist/api/LayoutManager.js.map +0 -1
- package/dist/api/PaneManager.js +0 -119
- package/dist/api/PaneManager.js.map +0 -1
- package/dist/api/ReferenceAPI.js +0 -153
- package/dist/api/ReferenceAPI.js.map +0 -1
- package/dist/api/TChart.js +0 -765
- package/dist/api/TChart.js.map +0 -1
- package/dist/api/createChart.js +0 -42
- package/dist/api/createChart.js.map +0 -1
- package/dist/api/drawing tools/fib gann menu/fibRetracement.js +0 -22
- package/dist/api/drawing tools/fib gann menu/fibRetracement.js.map +0 -1
- package/dist/api/drawing tools/lines menu/crossLine.js +0 -16
- package/dist/api/drawing tools/lines menu/crossLine.js.map +0 -1
- package/dist/api/drawing tools/lines menu/disjointChannel.js +0 -59
- package/dist/api/drawing tools/lines menu/disjointChannel.js.map +0 -1
- package/dist/api/drawing tools/lines menu/extendedLine.js +0 -17
- package/dist/api/drawing tools/lines menu/extendedLine.js.map +0 -1
- package/dist/api/drawing tools/lines menu/flatTopBottom.js +0 -41
- package/dist/api/drawing tools/lines menu/flatTopBottom.js.map +0 -1
- package/dist/api/drawing tools/lines menu/horizontal.js +0 -19
- package/dist/api/drawing tools/lines menu/horizontal.js.map +0 -1
- package/dist/api/drawing tools/lines menu/horizontalRay.js +0 -20
- package/dist/api/drawing tools/lines menu/horizontalRay.js.map +0 -1
- package/dist/api/drawing tools/lines menu/infoLine.js +0 -107
- package/dist/api/drawing tools/lines menu/infoLine.js.map +0 -1
- package/dist/api/drawing tools/lines menu/insidePitchfork.js +0 -31
- package/dist/api/drawing tools/lines menu/insidePitchfork.js.map +0 -1
- package/dist/api/drawing tools/lines menu/modifiedSchiffPitchfork.js +0 -15
- package/dist/api/drawing tools/lines menu/modifiedSchiffPitchfork.js.map +0 -1
- package/dist/api/drawing tools/lines menu/parallelChannel.js +0 -43
- package/dist/api/drawing tools/lines menu/parallelChannel.js.map +0 -1
- package/dist/api/drawing tools/lines menu/pitchfork.js +0 -12
- package/dist/api/drawing tools/lines menu/pitchfork.js.map +0 -1
- package/dist/api/drawing tools/lines menu/ray.js +0 -23
- package/dist/api/drawing tools/lines menu/ray.js.map +0 -1
- package/dist/api/drawing tools/lines menu/regressionTrend.js +0 -127
- package/dist/api/drawing tools/lines menu/regressionTrend.js.map +0 -1
- package/dist/api/drawing tools/lines menu/schiffPitchfork.js +0 -15
- package/dist/api/drawing tools/lines menu/schiffPitchfork.js.map +0 -1
- package/dist/api/drawing tools/lines menu/trendAngle.js +0 -51
- package/dist/api/drawing tools/lines menu/trendAngle.js.map +0 -1
- package/dist/api/drawing tools/lines menu/trendline.js +0 -11
- package/dist/api/drawing tools/lines menu/trendline.js.map +0 -1
- package/dist/api/drawing tools/lines menu/vertical.js +0 -11
- package/dist/api/drawing tools/lines menu/vertical.js.map +0 -1
- package/dist/api/drawing tools/pointers menu/crosshair.js +0 -16
- package/dist/api/drawing tools/pointers menu/crosshair.js.map +0 -1
- package/dist/api/drawing tools/pointers menu/cursor.js +0 -15
- package/dist/api/drawing tools/pointers menu/cursor.js.map +0 -1
- package/dist/api/drawing tools/pointers menu/demonstration.js +0 -30
- package/dist/api/drawing tools/pointers menu/demonstration.js.map +0 -1
- package/dist/api/drawing tools/pointers menu/dot.js +0 -23
- package/dist/api/drawing tools/pointers menu/dot.js.map +0 -1
- package/dist/api/drawing tools/shapes menu/rectangle.js +0 -19
- package/dist/api/drawing tools/shapes menu/rectangle.js.map +0 -1
- package/dist/api/drawing tools/shapes menu/text.js +0 -25
- package/dist/api/drawing tools/shapes menu/text.js.map +0 -1
- package/dist/api/drawingUtils.js +0 -83
- package/dist/api/drawingUtils.js.map +0 -1
- package/dist/core/CanvasLayer.js +0 -56
- package/dist/core/CanvasLayer.js.map +0 -1
- package/dist/core/Chart.js +0 -839
- package/dist/core/Chart.js.map +0 -1
- package/dist/core/CoordTransform.js +0 -224
- package/dist/core/CoordTransform.js.map +0 -1
- package/dist/core/Crosshair.js +0 -186
- package/dist/core/Crosshair.js.map +0 -1
- package/dist/core/IndicatorEngine.js +0 -181
- package/dist/core/IndicatorEngine.js.map +0 -1
- package/dist/core/InteractionManager.js +0 -698
- package/dist/core/InteractionManager.js.map +0 -1
- package/dist/core/PriceScale.js +0 -113
- package/dist/core/PriceScale.js.map +0 -1
- package/dist/core/Series.js +0 -114
- package/dist/core/Series.js.map +0 -1
- package/dist/core/TimeScale.js +0 -150
- package/dist/core/TimeScale.js.map +0 -1
- package/dist/datafeed/DatafeedConnector.js +0 -268
- package/dist/datafeed/DatafeedConnector.js.map +0 -1
- package/dist/engine/CandleEngine.js +0 -318
- package/dist/engine/CandleEngine.js.map +0 -1
- package/dist/engine/__tests__/CandleEngine.test.js +0 -300
- package/dist/engine/__tests__/CandleEngine.test.js.map +0 -1
- package/dist/engine/candleInvariants.js +0 -134
- package/dist/engine/candleInvariants.js.map +0 -1
- package/dist/engine/mergeUtils.js +0 -64
- package/dist/engine/mergeUtils.js.map +0 -1
- package/dist/engine/timeframeUtils.js +0 -100
- package/dist/engine/timeframeUtils.js.map +0 -1
- package/dist/licensing/ChartRuntimeResolver.js +0 -310
- package/dist/licensing/ChartRuntimeResolver.js.map +0 -1
- package/dist/licensing/LicenseManager.js +0 -114
- package/dist/licensing/LicenseManager.js.map +0 -1
- package/dist/licensing/__tests__/ChartRuntimeResolver.test.js +0 -177
- package/dist/licensing/__tests__/ChartRuntimeResolver.test.js.map +0 -1
- package/dist/licensing/__tests__/LicenseManager.test.js +0 -153
- package/dist/licensing/__tests__/LicenseManager.test.js.map +0 -1
- package/dist/licensing/licenseTypes.js +0 -2
- package/dist/licensing/licenseTypes.js.map +0 -1
- package/dist/pine/PineCompiler.js +0 -44
- package/dist/pine/PineCompiler.js.map +0 -1
- package/dist/pine/diagnostics.js +0 -11
- package/dist/pine/diagnostics.js.map +0 -1
- package/dist/pine/index.js +0 -5
- package/dist/pine/index.js.map +0 -1
- package/dist/pine/pine-ast.js +0 -19
- package/dist/pine/pine-ast.js.map +0 -1
- package/dist/pine/pine-lexer.js +0 -249
- package/dist/pine/pine-lexer.js.map +0 -1
- package/dist/pine/pine-parser.js +0 -416
- package/dist/pine/pine-parser.js.map +0 -1
- package/dist/pine/pine-transpiler.js +0 -260
- package/dist/pine/pine-transpiler.js.map +0 -1
- package/dist/pixi/LayerName.js +0 -35
- package/dist/pixi/LayerName.js.map +0 -1
- package/dist/pixi/PixiCandlestickRenderer.js +0 -107
- package/dist/pixi/PixiCandlestickRenderer.js.map +0 -1
- package/dist/pixi/PixiChart.js +0 -367
- package/dist/pixi/PixiChart.js.map +0 -1
- package/dist/pixi/PixiCrosshairRenderer.js +0 -110
- package/dist/pixi/PixiCrosshairRenderer.js.map +0 -1
- package/dist/pixi/PixiDrawingRenderer.js +0 -111
- package/dist/pixi/PixiDrawingRenderer.js.map +0 -1
- package/dist/pixi/PixiGridRenderer.js +0 -114
- package/dist/pixi/PixiGridRenderer.js.map +0 -1
- package/dist/pixi/PixiLayerManager.js +0 -92
- package/dist/pixi/PixiLayerManager.js.map +0 -1
- package/dist/react/canvas/ChartCanvas.js +0 -604
- package/dist/react/canvas/ChartCanvas.js.map +0 -1
- package/dist/react/canvas/ChartContextMenu.js +0 -5
- package/dist/react/canvas/ChartContextMenu.js.map +0 -1
- package/dist/react/canvas/ChartSettingsDialog.js +0 -28
- package/dist/react/canvas/ChartSettingsDialog.js.map +0 -1
- package/dist/react/canvas/IndicatorLabel.js +0 -196
- package/dist/react/canvas/IndicatorLabel.js.map +0 -1
- package/dist/react/canvas/IndicatorPane.js +0 -395
- package/dist/react/canvas/IndicatorPane.js.map +0 -1
- package/dist/react/canvas/PointerOverlay.js +0 -61
- package/dist/react/canvas/PointerOverlay.js.map +0 -1
- package/dist/react/canvas/toolbars/LeftToolbar.js +0 -407
- package/dist/react/canvas/toolbars/LeftToolbar.js.map +0 -1
- package/dist/react/hooks/useChartCapabilities.js +0 -66
- package/dist/react/hooks/useChartCapabilities.js.map +0 -1
- package/dist/react/shell/ManagedAppShell.js +0 -440
- package/dist/react/shell/ManagedAppShell.js.map +0 -1
- package/dist/react/trading/TradingBridge.js +0 -73
- package/dist/react/trading/TradingBridge.js.map +0 -1
- package/dist/react/workspace/ChartWorkspace.js +0 -42
- package/dist/react/workspace/ChartWorkspace.js.map +0 -1
- package/dist/react/workspace/FloatingPanel.js +0 -82
- package/dist/react/workspace/FloatingPanel.js.map +0 -1
- package/dist/react/workspace/IndicatorsDialog.js +0 -121
- package/dist/react/workspace/IndicatorsDialog.js.map +0 -1
- package/dist/react/workspace/LayoutMenu.js +0 -113
- package/dist/react/workspace/LayoutMenu.js.map +0 -1
- package/dist/react/workspace/SymbolSearchDialog.js +0 -245
- package/dist/react/workspace/SymbolSearchDialog.js.map +0 -1
- package/dist/react/workspace/TabBar.js +0 -29
- package/dist/react/workspace/TabBar.js.map +0 -1
- package/dist/react/workspace/toolbars/BottomToolbar.js +0 -236
- package/dist/react/workspace/toolbars/BottomToolbar.js.map +0 -1
- package/dist/react/workspace/toolbars/RightToolbar.js +0 -18
- package/dist/react/workspace/toolbars/RightToolbar.js.map +0 -1
- package/dist/react/workspace/toolbars/TopToolbar.js +0 -82
- package/dist/react/workspace/toolbars/TopToolbar.js.map +0 -1
- package/dist/renderers/CandlestickRenderer.js +0 -98
- package/dist/renderers/CandlestickRenderer.js.map +0 -1
- package/dist/renderers/HistogramRenderer.js +0 -50
- package/dist/renderers/HistogramRenderer.js.map +0 -1
- package/dist/renderers/LineRenderer.js +0 -64
- package/dist/renderers/LineRenderer.js.map +0 -1
- package/dist/theme/colors.js +0 -19
- package/dist/theme/colors.js.map +0 -1
- package/dist/tools/barDivergenceCheck.js +0 -200
- package/dist/tools/barDivergenceCheck.js.map +0 -1
- package/dist/trading/TradingOverlayStore.js +0 -139
- package/dist/trading/TradingOverlayStore.js.map +0 -1
- package/dist/trading/UnmanagedIngestion.js +0 -114
- package/dist/trading/UnmanagedIngestion.js.map +0 -1
- package/dist/trading/__tests__/ManagedTradingController.test.js +0 -271
- package/dist/trading/__tests__/ManagedTradingController.test.js.map +0 -1
- package/dist/trading/__tests__/TradingOverlayStore.test.js +0 -267
- package/dist/trading/__tests__/TradingOverlayStore.test.js.map +0 -1
- package/dist/trading/__tests__/UnmanagedIngestion.test.js +0 -170
- package/dist/trading/__tests__/UnmanagedIngestion.test.js.map +0 -1
- package/dist/trading/managed/ManagedTradingController.js +0 -247
- package/dist/trading/managed/ManagedTradingController.js.map +0 -1
- package/dist/trading/managed/managedCapabilities.js +0 -79
- package/dist/trading/managed/managedCapabilities.js.map +0 -1
- package/dist/trading/managed/managedTypes.js +0 -13
- package/dist/trading/managed/managedTypes.js.map +0 -1
- package/dist/trading/tradingTypes.js +0 -8
- package/dist/trading/tradingTypes.js.map +0 -1
- package/dist/tscript/TScriptIndicator.js +0 -47
- package/dist/tscript/TScriptIndicator.js.map +0 -1
- package/dist/tscript/ast.js +0 -22
- package/dist/tscript/ast.js.map +0 -1
- package/dist/tscript/lexer.js +0 -187
- package/dist/tscript/lexer.js.map +0 -1
- package/dist/tscript/parser.js +0 -318
- package/dist/tscript/parser.js.map +0 -1
- package/dist/tscript/runtime.js +0 -475
- package/dist/tscript/runtime.js.map +0 -1
- package/dist/tscript/series.js +0 -79
- package/dist/tscript/series.js.map +0 -1
- package/dist/types/IChart.js +0 -2
- package/dist/types/IChart.js.map +0 -1
- package/dist/types/IRenderer.js +0 -2
- package/dist/types/IRenderer.js.map +0 -1
- package/dist/types/ISeries.js +0 -2
- package/dist/types/ISeries.js.map +0 -1
|
@@ -1,698 +0,0 @@
|
|
|
1
|
-
// ─── Constants ────────────────────────────────────────────────────────────────
|
|
2
|
-
/** Tolerated pixel distance for drawing hit detection. */
|
|
3
|
-
const DRAWING_HIT_PX = 6;
|
|
4
|
-
/** Tolerated pixel distance for handle hit detection. */
|
|
5
|
-
const HANDLE_HIT_PX = 8;
|
|
6
|
-
// ─── InteractionManager ───────────────────────────────────────────────────────
|
|
7
|
-
/**
|
|
8
|
-
* InteractionManager — the single owner of all DOM input events for a chart.
|
|
9
|
-
*
|
|
10
|
-
* Responsibilities:
|
|
11
|
-
* - Mouse: pan (left-drag), crosshair tracking, click detection
|
|
12
|
-
* - Wheel: zoom
|
|
13
|
-
* - Right-click: contextmenu routing
|
|
14
|
-
* - Keyboard: arrow-key pan, +/- zoom, Esc / Delete shortcuts
|
|
15
|
-
* - Hit-testing: drawings → bars → axis zones
|
|
16
|
-
* - Selection state: tracks the currently selected drawing id
|
|
17
|
-
*
|
|
18
|
-
* Usage:
|
|
19
|
-
* ```ts
|
|
20
|
-
* const interaction = new InteractionManager(
|
|
21
|
-
* canvasElement,
|
|
22
|
-
* chart.transform(),
|
|
23
|
-
* {
|
|
24
|
-
* onPan: (dx, dy) => { transform.pan(dx, dy); markDirty(); },
|
|
25
|
-
* onZoom: (f, ox) => { transform.zoomTime(f, ox); markDirty(); },
|
|
26
|
-
* onCrosshairMove: (x, y) => { crosshair.update(x, y); markDirty(); },
|
|
27
|
-
* onCrosshairHide: () => { crosshair.hide(); markDirty(); },
|
|
28
|
-
* onContextMenu: (x, y, hit) => showMenu(x, y, hit),
|
|
29
|
-
* onKeyAction: (action) => handleKey(action),
|
|
30
|
-
* },
|
|
31
|
-
* );
|
|
32
|
-
*
|
|
33
|
-
* // Call on chart resize:
|
|
34
|
-
* interaction.setTransform(chart.transform());
|
|
35
|
-
*
|
|
36
|
-
* // Feed visible bars for bar-snap hit-testing:
|
|
37
|
-
* interaction.setBars(series.data());
|
|
38
|
-
*
|
|
39
|
-
* // Feed drawings for drawing hit-testing:
|
|
40
|
-
* interaction.setDrawings(drawingManager.all());
|
|
41
|
-
*
|
|
42
|
-
* // Teardown:
|
|
43
|
-
* interaction.destroy();
|
|
44
|
-
* ```
|
|
45
|
-
*/
|
|
46
|
-
export class InteractionManager {
|
|
47
|
-
_el;
|
|
48
|
-
_transform;
|
|
49
|
-
_handlers;
|
|
50
|
-
_opts;
|
|
51
|
-
// ── Hit-test data feeds ───────────────────────────────────────────────────
|
|
52
|
-
_drawings = [];
|
|
53
|
-
_bars = [];
|
|
54
|
-
// ── Mouse state ───────────────────────────────────────────────────────────
|
|
55
|
-
_isDragging = false;
|
|
56
|
-
_dragStartX = 0;
|
|
57
|
-
_dragStartY = 0;
|
|
58
|
-
_lastDragX = 0;
|
|
59
|
-
_lastDragY = 0;
|
|
60
|
-
/** True once the pointer has moved past `clickThreshold` during a drag. */
|
|
61
|
-
_hasMoved = false;
|
|
62
|
-
/** True while the user is dragging on the price axis strip. */
|
|
63
|
-
_priceAxisDragging = false;
|
|
64
|
-
// ── Selection state ───────────────────────────────────────────────────────
|
|
65
|
-
_selectedDrawingId = null;
|
|
66
|
-
// ── Drawing tool state ────────────────────────────────────────────────────
|
|
67
|
-
_drawingMode = false;
|
|
68
|
-
/** True when the native cursor should be hidden (crosshair / dot / demonstration). */
|
|
69
|
-
_hideCursor = false;
|
|
70
|
-
/** Sets cursor style, respecting _hideCursor — 'none' always wins when hiding. */
|
|
71
|
-
_setCursor(style) {
|
|
72
|
-
this._el.style.cursor = this._hideCursor ? 'none' : style;
|
|
73
|
-
}
|
|
74
|
-
/** Set when a drawing is committed mid-mousedown; prevents the paired mouseup from auto-selecting it. */
|
|
75
|
-
_suppressNextClick = false;
|
|
76
|
-
_handleDragId = null;
|
|
77
|
-
_handleDragIdx = -1;
|
|
78
|
-
_bodyMoveId = null;
|
|
79
|
-
_bodyMovePrevX = 0;
|
|
80
|
-
_bodyMovePrevY = 0;
|
|
81
|
-
constructor(element, transform, handlers, options = {}) {
|
|
82
|
-
this._el = element;
|
|
83
|
-
this._transform = transform;
|
|
84
|
-
this._handlers = handlers;
|
|
85
|
-
this._opts = {
|
|
86
|
-
enablePan: options.enablePan ?? true,
|
|
87
|
-
enableZoom: options.enableZoom ?? true,
|
|
88
|
-
clickThreshold: options.clickThreshold ?? 4,
|
|
89
|
-
zoomStep: options.zoomStep ?? 1.1,
|
|
90
|
-
keyPanStep: options.keyPanStep ?? 40,
|
|
91
|
-
keyZoomStep: options.keyZoomStep ?? 1.1,
|
|
92
|
-
};
|
|
93
|
-
// Make container keyboard-focusable so `keydown` fires on it
|
|
94
|
-
if (element.tabIndex < 0)
|
|
95
|
-
element.tabIndex = 0;
|
|
96
|
-
element.style.outline = 'none'; // suppress browser focus ring
|
|
97
|
-
this._attach();
|
|
98
|
-
}
|
|
99
|
-
// ─── Data feeds (call after every data / drawing change) ──────────────────
|
|
100
|
-
/** Swap the transform reference after a resize. */
|
|
101
|
-
setTransform(transform) {
|
|
102
|
-
this._transform = transform;
|
|
103
|
-
}
|
|
104
|
-
/** Provide the current drawing list for hit-testing. */
|
|
105
|
-
setDrawings(drawings) {
|
|
106
|
-
this._drawings = drawings;
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* Provide the visible bars for bar-snap hit-testing on the primary series.
|
|
110
|
-
* Passing `series.data()` is fine; the manager filters by visible range.
|
|
111
|
-
*/
|
|
112
|
-
setBars(bars) {
|
|
113
|
-
this._bars = bars;
|
|
114
|
-
}
|
|
115
|
-
// ─── Selection ────────────────────────────────────────────────────────────
|
|
116
|
-
/** Id of the currently selected drawing, or `null`. */
|
|
117
|
-
get selectedDrawingId() {
|
|
118
|
-
return this._selectedDrawingId;
|
|
119
|
-
}
|
|
120
|
-
/** Programmatically deselect (e.g. on symbol / timeframe change). */
|
|
121
|
-
clearSelection() {
|
|
122
|
-
this._selectedDrawingId = null;
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Switches between drawing-tool mode (crosshair cursor, click fires onDrawClick)
|
|
126
|
-
* and normal cursor mode. Also resets any in-progress drag state.
|
|
127
|
-
*/
|
|
128
|
-
/** Hides (`true`) or shows (`false`) the native OS cursor. Used by crosshair, dot, and demonstration modes. */
|
|
129
|
-
setHideCursor(hidden) {
|
|
130
|
-
this._hideCursor = hidden;
|
|
131
|
-
if (!this._drawingMode) {
|
|
132
|
-
this._el.style.cursor = hidden ? 'none' : 'default';
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
setDrawingMode(active) {
|
|
136
|
-
if (this._drawingMode && !active) {
|
|
137
|
-
// Drawing was committed during a mousedown — suppress the paired mouseup click
|
|
138
|
-
// so it doesn't immediately auto-select the newly placed drawing.
|
|
139
|
-
this._suppressNextClick = true;
|
|
140
|
-
}
|
|
141
|
-
this._drawingMode = active;
|
|
142
|
-
this._el.style.cursor = active ? 'crosshair' : (this._hideCursor ? 'none' : 'default');
|
|
143
|
-
this._isDragging = false;
|
|
144
|
-
this._hasMoved = false;
|
|
145
|
-
this._handleDragId = null;
|
|
146
|
-
this._handleDragIdx = -1;
|
|
147
|
-
this._bodyMoveId = null;
|
|
148
|
-
}
|
|
149
|
-
// ─── Lifecycle ────────────────────────────────────────────────────────────
|
|
150
|
-
/** Removes all DOM event listeners. Must be called in the chart's `destroy()`. */
|
|
151
|
-
destroy() {
|
|
152
|
-
this._detach();
|
|
153
|
-
}
|
|
154
|
-
// ─── DOM listener registry ────────────────────────────────────────────────
|
|
155
|
-
_attach() {
|
|
156
|
-
const el = this._el;
|
|
157
|
-
// Mouse — always registered so crosshair works even with pan disabled
|
|
158
|
-
el.addEventListener('mousedown', this._onMouseDown);
|
|
159
|
-
el.addEventListener('mousemove', this._onMouseMove);
|
|
160
|
-
el.addEventListener('mouseup', this._onMouseUp);
|
|
161
|
-
el.addEventListener('mouseleave', this._onMouseLeave);
|
|
162
|
-
if (this._opts.enableZoom) {
|
|
163
|
-
el.addEventListener('wheel', this._onWheel, { passive: false });
|
|
164
|
-
}
|
|
165
|
-
el.addEventListener('contextmenu', this._onContextMenu);
|
|
166
|
-
el.addEventListener('keydown', this._onKeyDown);
|
|
167
|
-
el.addEventListener('dblclick', this._onDblClick);
|
|
168
|
-
}
|
|
169
|
-
_detach() {
|
|
170
|
-
const el = this._el;
|
|
171
|
-
el.removeEventListener('mousedown', this._onMouseDown);
|
|
172
|
-
el.removeEventListener('mousemove', this._onMouseMove);
|
|
173
|
-
el.removeEventListener('mouseup', this._onMouseUp);
|
|
174
|
-
el.removeEventListener('mouseleave', this._onMouseLeave);
|
|
175
|
-
el.removeEventListener('wheel', this._onWheel);
|
|
176
|
-
el.removeEventListener('contextmenu', this._onContextMenu);
|
|
177
|
-
el.removeEventListener('keydown', this._onKeyDown);
|
|
178
|
-
el.removeEventListener('dblclick', this._onDblClick);
|
|
179
|
-
}
|
|
180
|
-
// ─── Mouse handlers ───────────────────────────────────────────────────────
|
|
181
|
-
_onMouseDown = (e) => {
|
|
182
|
-
if (e.button !== 0)
|
|
183
|
-
return; // left button only
|
|
184
|
-
this._el.focus({ preventScroll: true });
|
|
185
|
-
const x = e.offsetX;
|
|
186
|
-
const y = e.offsetY;
|
|
187
|
-
// Drawing tool mode: route click to drawing placement
|
|
188
|
-
if (this._drawingMode) {
|
|
189
|
-
this._handlers.onDrawClick?.(x, y);
|
|
190
|
-
return;
|
|
191
|
-
}
|
|
192
|
-
// Cursor mode: check handles of the selected drawing first (highest priority)
|
|
193
|
-
if (this._selectedDrawingId !== null) {
|
|
194
|
-
for (const drawing of this._drawings) {
|
|
195
|
-
if (drawing.id !== this._selectedDrawingId || drawing.visible === false)
|
|
196
|
-
continue;
|
|
197
|
-
const hi = this._hitTestHandles(x, y, drawing);
|
|
198
|
-
if (hi >= 0) {
|
|
199
|
-
this._handleDragId = drawing.id;
|
|
200
|
-
this._handleDragIdx = hi;
|
|
201
|
-
this._setCursor('grabbing');
|
|
202
|
-
this._handlers.onHandleDragStart?.(drawing.id, hi);
|
|
203
|
-
return;
|
|
204
|
-
}
|
|
205
|
-
break; // handles checked — move on
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
// Auto-select + immediately start body drag on ANY hovered drawing.
|
|
209
|
-
// This means one mousedown is all that's needed — no separate
|
|
210
|
-
// "click to select, then click+drag" two-step required.
|
|
211
|
-
const drawingBodyHit = this._hitTestDrawings(x, y);
|
|
212
|
-
if (drawingBodyHit !== null) {
|
|
213
|
-
this._selectedDrawingId = drawingBodyHit.id;
|
|
214
|
-
this._bodyMoveId = drawingBodyHit.id;
|
|
215
|
-
this._bodyMovePrevX = x;
|
|
216
|
-
this._bodyMovePrevY = y;
|
|
217
|
-
this._setCursor('grabbing');
|
|
218
|
-
this._handlers.onBodyMoveStart?.(drawingBodyHit.id);
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
// Price axis — start vertical drag to stretch/compress price scale
|
|
222
|
-
if (x >= this._transform.plotWidth && y < this._transform.plotHeight) {
|
|
223
|
-
this._priceAxisDragging = true;
|
|
224
|
-
this._lastDragY = y;
|
|
225
|
-
this._setCursor('ns-resize');
|
|
226
|
-
return;
|
|
227
|
-
}
|
|
228
|
-
// Normal pan
|
|
229
|
-
this._isDragging = true;
|
|
230
|
-
this._hasMoved = false;
|
|
231
|
-
this._dragStartX = x;
|
|
232
|
-
this._dragStartY = y;
|
|
233
|
-
this._lastDragX = x;
|
|
234
|
-
this._lastDragY = y;
|
|
235
|
-
};
|
|
236
|
-
_onMouseMove = (e) => {
|
|
237
|
-
const x = e.offsetX;
|
|
238
|
-
const y = e.offsetY;
|
|
239
|
-
// Drawing-tool preview
|
|
240
|
-
if (this._drawingMode) {
|
|
241
|
-
this._handlers.onDrawPointerMove?.(x, y);
|
|
242
|
-
this._handlers.onCrosshairMove?.(x, y, this._hitTest(x, y));
|
|
243
|
-
return;
|
|
244
|
-
}
|
|
245
|
-
// Handle drag
|
|
246
|
-
if (this._handleDragId !== null) {
|
|
247
|
-
this._setCursor('grabbing');
|
|
248
|
-
this._handlers.onHandleDragMove?.(this._handleDragId, this._handleDragIdx, x, y);
|
|
249
|
-
return;
|
|
250
|
-
}
|
|
251
|
-
// Body move
|
|
252
|
-
if (this._bodyMoveId !== null) {
|
|
253
|
-
const dx = x - this._bodyMovePrevX;
|
|
254
|
-
const dy = y - this._bodyMovePrevY;
|
|
255
|
-
this._bodyMovePrevX = x;
|
|
256
|
-
this._bodyMovePrevY = y;
|
|
257
|
-
this._setCursor('grabbing');
|
|
258
|
-
this._handlers.onBodyMoveMove?.(this._bodyMoveId, dx, dy);
|
|
259
|
-
return;
|
|
260
|
-
}
|
|
261
|
-
// Price axis drag — zoom price scale vertically
|
|
262
|
-
if (this._priceAxisDragging) {
|
|
263
|
-
const dy = y - this._lastDragY;
|
|
264
|
-
this._lastDragY = y;
|
|
265
|
-
if (dy !== 0)
|
|
266
|
-
this._handlers.onPriceAxisDrag?.(dy);
|
|
267
|
-
this._setCursor('ns-resize');
|
|
268
|
-
return;
|
|
269
|
-
}
|
|
270
|
-
if (this._isDragging) {
|
|
271
|
-
const dx = x - this._lastDragX;
|
|
272
|
-
const dy = y - this._lastDragY;
|
|
273
|
-
const totalDist = Math.hypot(x - this._dragStartX, y - this._dragStartY);
|
|
274
|
-
if (totalDist > this._opts.clickThreshold) {
|
|
275
|
-
this._hasMoved = true;
|
|
276
|
-
}
|
|
277
|
-
if (this._hasMoved && this._opts.enablePan) {
|
|
278
|
-
this._handlers.onPan?.(dx, dy);
|
|
279
|
-
}
|
|
280
|
-
this._lastDragX = x;
|
|
281
|
-
this._lastDragY = y;
|
|
282
|
-
}
|
|
283
|
-
// Crosshair + cursor feedback
|
|
284
|
-
const hit = this._hitTest(x, y);
|
|
285
|
-
this._handlers.onCrosshairMove?.(x, y, hit);
|
|
286
|
-
if (!this._isDragging) {
|
|
287
|
-
if (x >= this._transform.plotWidth && y < this._transform.plotHeight) {
|
|
288
|
-
this._setCursor('ns-resize');
|
|
289
|
-
}
|
|
290
|
-
else if (hit.kind === 'drawingHandle') {
|
|
291
|
-
this._setCursor('grab');
|
|
292
|
-
}
|
|
293
|
-
else if (hit.kind === 'drawing') {
|
|
294
|
-
this._setCursor('grab');
|
|
295
|
-
}
|
|
296
|
-
else {
|
|
297
|
-
this._setCursor('default');
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
};
|
|
301
|
-
_onMouseUp = (e) => {
|
|
302
|
-
if (e.button !== 0)
|
|
303
|
-
return;
|
|
304
|
-
// Finish price axis drag
|
|
305
|
-
if (this._priceAxisDragging) {
|
|
306
|
-
this._priceAxisDragging = false;
|
|
307
|
-
this._setCursor('default');
|
|
308
|
-
return;
|
|
309
|
-
}
|
|
310
|
-
// Finish handle drag
|
|
311
|
-
if (this._handleDragId !== null) {
|
|
312
|
-
const id = this._handleDragId;
|
|
313
|
-
const idx = this._handleDragIdx;
|
|
314
|
-
this._handleDragId = null;
|
|
315
|
-
this._handleDragIdx = -1;
|
|
316
|
-
this._setCursor('grab');
|
|
317
|
-
this._handlers.onHandleDragEnd?.(id, idx);
|
|
318
|
-
return;
|
|
319
|
-
}
|
|
320
|
-
// Finish body move
|
|
321
|
-
if (this._bodyMoveId !== null) {
|
|
322
|
-
const id = this._bodyMoveId;
|
|
323
|
-
this._bodyMoveId = null;
|
|
324
|
-
this._setCursor('grab');
|
|
325
|
-
this._handlers.onBodyMoveEnd?.(id);
|
|
326
|
-
return;
|
|
327
|
-
}
|
|
328
|
-
const wasClick = !this._hasMoved;
|
|
329
|
-
this._isDragging = false;
|
|
330
|
-
this._hasMoved = false;
|
|
331
|
-
if (wasClick) {
|
|
332
|
-
if (this._suppressNextClick) {
|
|
333
|
-
this._suppressNextClick = false;
|
|
334
|
-
}
|
|
335
|
-
else {
|
|
336
|
-
const x = e.offsetX;
|
|
337
|
-
const y = e.offsetY;
|
|
338
|
-
const hit = this._hitTest(x, y);
|
|
339
|
-
this._handleClick(x, y, hit);
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
};
|
|
343
|
-
_onMouseLeave = () => {
|
|
344
|
-
this._isDragging = false;
|
|
345
|
-
this._hasMoved = false;
|
|
346
|
-
this._priceAxisDragging = false;
|
|
347
|
-
this._handleDragId = null;
|
|
348
|
-
this._handleDragIdx = -1;
|
|
349
|
-
this._bodyMoveId = null;
|
|
350
|
-
this._handlers.onCrosshairHide?.();
|
|
351
|
-
};
|
|
352
|
-
// ─── Wheel / zoom ─────────────────────────────────────────────────────────
|
|
353
|
-
_onWheel = (e) => {
|
|
354
|
-
e.preventDefault();
|
|
355
|
-
const x = e.offsetX;
|
|
356
|
-
const y = e.offsetY;
|
|
357
|
-
const plotW = this._transform.plotWidth;
|
|
358
|
-
const plotH = this._transform.plotHeight;
|
|
359
|
-
// Proportional zoom: normalise deltaY against one standard notch (120 px).
|
|
360
|
-
// High-resolution trackpads send many small deltas; mice send 120 at a time.
|
|
361
|
-
// Range: 0.8% per tiny trackpad nudge → 4% per full mouse-wheel notch.
|
|
362
|
-
// This keeps one notch feeling gentle while fast spinning still zooms quickly.
|
|
363
|
-
const rawAbs = Math.abs(e.deltaY);
|
|
364
|
-
const speed = Math.min(rawAbs / 120, 1); // 0–1; 1 = one standard notch
|
|
365
|
-
const magnitude = 0.008 + speed * 0.032; // 0.8% minimum → 4% per notch
|
|
366
|
-
const baseZoom = 1 + magnitude;
|
|
367
|
-
const factor = e.deltaY > 0 ? baseZoom : 1 / baseZoom;
|
|
368
|
-
// Price axis strip (right side) — zoom price scale
|
|
369
|
-
if (x >= plotW && y < plotH) {
|
|
370
|
-
this._handlers.onPriceScaleZoom?.(factor, y);
|
|
371
|
-
return;
|
|
372
|
-
}
|
|
373
|
-
// Time axis strip (bottom) — zoom time scale (expand/contract left-right)
|
|
374
|
-
if (y >= plotH) {
|
|
375
|
-
// Zoom around the horizontal centre of the plot so the chart expands
|
|
376
|
-
// and contracts symmetrically rather than anchored to the cursor.
|
|
377
|
-
this._handlers.onZoom?.(factor, plotW / 2);
|
|
378
|
-
return;
|
|
379
|
-
}
|
|
380
|
-
// Chart plot area — zoom time axis
|
|
381
|
-
this._handlers.onZoom?.(factor, x);
|
|
382
|
-
};
|
|
383
|
-
// ─── Double-click — price axis fit ──────────────────────────────────────
|
|
384
|
-
_onDblClick = (e) => {
|
|
385
|
-
// Fire onFitContent when the double-click lands on the price axis (right)
|
|
386
|
-
// or the time axis (bottom) — both reset to the default viewport.
|
|
387
|
-
const onPriceAxis = e.offsetX >= this._transform.plotWidth;
|
|
388
|
-
const onTimeAxis = e.offsetY >= this._transform.plotHeight;
|
|
389
|
-
if (onPriceAxis || onTimeAxis) {
|
|
390
|
-
e.preventDefault();
|
|
391
|
-
this._handlers.onFitContent?.();
|
|
392
|
-
}
|
|
393
|
-
};
|
|
394
|
-
// ─── Context menu ─────────────────────────────────────────────────────────
|
|
395
|
-
_onContextMenu = (e) => {
|
|
396
|
-
e.preventDefault();
|
|
397
|
-
const x = e.offsetX;
|
|
398
|
-
const y = e.offsetY;
|
|
399
|
-
const hit = this._hitTest(x, y);
|
|
400
|
-
this._handlers.onContextMenu?.(x, y, hit);
|
|
401
|
-
};
|
|
402
|
-
// ─── Keyboard ─────────────────────────────────────────────────────────────
|
|
403
|
-
_onKeyDown = (e) => {
|
|
404
|
-
const action = this._keyToAction(e.key);
|
|
405
|
-
if (action === null)
|
|
406
|
-
return;
|
|
407
|
-
// Suppress browser scroll / zoom for chart keys
|
|
408
|
-
e.preventDefault();
|
|
409
|
-
const modifiers = {
|
|
410
|
-
shift: e.shiftKey,
|
|
411
|
-
ctrl: e.ctrlKey,
|
|
412
|
-
alt: e.altKey,
|
|
413
|
-
meta: e.metaKey,
|
|
414
|
-
};
|
|
415
|
-
this._handlers.onKeyAction?.(action, modifiers);
|
|
416
|
-
};
|
|
417
|
-
_keyToAction(key) {
|
|
418
|
-
switch (key) {
|
|
419
|
-
case 'ArrowLeft': return 'panLeft';
|
|
420
|
-
case 'ArrowRight': return 'panRight';
|
|
421
|
-
case 'ArrowUp': return 'panUp';
|
|
422
|
-
case 'ArrowDown': return 'panDown';
|
|
423
|
-
case '+':
|
|
424
|
-
case '=': return 'zoomIn';
|
|
425
|
-
case '-':
|
|
426
|
-
case '_': return 'zoomOut';
|
|
427
|
-
case 'Home': return 'scrollToStart';
|
|
428
|
-
case 'End': return 'scrollToEnd';
|
|
429
|
-
case 'Escape': return 'deselect';
|
|
430
|
-
case 'Delete':
|
|
431
|
-
case 'Backspace': return 'deleteSelection';
|
|
432
|
-
default: return null;
|
|
433
|
-
}
|
|
434
|
-
}
|
|
435
|
-
// ─── Click → selection ────────────────────────────────────────────────────
|
|
436
|
-
_handleClick(x, y, hit) {
|
|
437
|
-
if (hit.kind === 'drawing') {
|
|
438
|
-
this._selectedDrawingId = hit.id;
|
|
439
|
-
this._handlers.onSelect?.(x, y, hit);
|
|
440
|
-
}
|
|
441
|
-
else if (hit.kind === 'bar') {
|
|
442
|
-
this._selectedDrawingId = null;
|
|
443
|
-
this._handlers.onSelect?.(x, y, hit);
|
|
444
|
-
}
|
|
445
|
-
else {
|
|
446
|
-
this._selectedDrawingId = null;
|
|
447
|
-
this._handlers.onDeselect?.();
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
// ─── Hit-testing ─────────────────────────────────────────────────────────
|
|
451
|
-
_hitTest(x, y) {
|
|
452
|
-
const t = this._transform;
|
|
453
|
-
// Price axis zone (right strip)
|
|
454
|
-
if (x >= t.plotWidth) {
|
|
455
|
-
return { kind: 'priceAxis' };
|
|
456
|
-
}
|
|
457
|
-
// Time axis zone (bottom strip)
|
|
458
|
-
if (y >= t.plotHeight) {
|
|
459
|
-
return { kind: 'timeAxis' };
|
|
460
|
-
}
|
|
461
|
-
// Handles of selected drawing — highest priority
|
|
462
|
-
if (this._selectedDrawingId !== null) {
|
|
463
|
-
for (const drawing of this._drawings) {
|
|
464
|
-
if (drawing.id !== this._selectedDrawingId || drawing.visible === false)
|
|
465
|
-
continue;
|
|
466
|
-
const hi = this._hitTestHandles(x, y, drawing);
|
|
467
|
-
if (hi >= 0) {
|
|
468
|
-
return { kind: 'drawingHandle', id: drawing.id, type: drawing.type, handleIndex: hi };
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
// Drawings — higher priority than bars
|
|
473
|
-
const drawingHit = this._hitTestDrawings(x, y);
|
|
474
|
-
if (drawingHit !== null) {
|
|
475
|
-
return { kind: 'drawing', id: drawingHit.id, type: drawingHit.type };
|
|
476
|
-
}
|
|
477
|
-
// Nearest visible bar
|
|
478
|
-
const barHit = this._hitTestBar(x);
|
|
479
|
-
if (barHit !== null) {
|
|
480
|
-
return { kind: 'bar', index: barHit.index, bar: barHit.bar };
|
|
481
|
-
}
|
|
482
|
-
return { kind: 'none' };
|
|
483
|
-
}
|
|
484
|
-
_hitTestHandles(x, y, drawing) {
|
|
485
|
-
const t = this._transform;
|
|
486
|
-
for (let i = 0; i < drawing.points.length; i++) {
|
|
487
|
-
const p = drawing.points[i];
|
|
488
|
-
if (Math.hypot(x - t.timeToX(p.time), y - t.priceToY(p.price)) < HANDLE_HIT_PX) {
|
|
489
|
-
return i;
|
|
490
|
-
}
|
|
491
|
-
}
|
|
492
|
-
return -1;
|
|
493
|
-
}
|
|
494
|
-
_hitTestDrawings(x, y) {
|
|
495
|
-
let bestId = null;
|
|
496
|
-
let bestType = null;
|
|
497
|
-
let bestDist = DRAWING_HIT_PX + 1;
|
|
498
|
-
for (const drawing of this._drawings) {
|
|
499
|
-
if (drawing.visible === false)
|
|
500
|
-
continue;
|
|
501
|
-
const rawDist = this._distToDrawing(x, y, drawing);
|
|
502
|
-
// Small bias for the selected drawing so it "sticks" under the cursor
|
|
503
|
-
const dist = rawDist + (drawing.id === this._selectedDrawingId ? -2 : 0);
|
|
504
|
-
if (dist < bestDist) {
|
|
505
|
-
bestDist = dist;
|
|
506
|
-
bestId = drawing.id;
|
|
507
|
-
bestType = drawing.type;
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
return bestId !== null && bestType !== null ? { id: bestId, type: bestType } : null;
|
|
511
|
-
}
|
|
512
|
-
_distToDrawing(mx, my, drawing) {
|
|
513
|
-
const t = this._transform;
|
|
514
|
-
switch (drawing.type) {
|
|
515
|
-
case 'horizontal': {
|
|
516
|
-
const p0 = drawing.points[0];
|
|
517
|
-
if (!p0)
|
|
518
|
-
return Infinity;
|
|
519
|
-
return Math.abs(my - t.priceToY(p0.price));
|
|
520
|
-
}
|
|
521
|
-
case 'vertical': {
|
|
522
|
-
const p0 = drawing.points[0];
|
|
523
|
-
if (!p0)
|
|
524
|
-
return Infinity;
|
|
525
|
-
return Math.abs(mx - t.timeToX(p0.time));
|
|
526
|
-
}
|
|
527
|
-
case 'trendline':
|
|
528
|
-
case 'fibRetracement': {
|
|
529
|
-
const p0 = drawing.points[0];
|
|
530
|
-
const p1 = drawing.points[1];
|
|
531
|
-
if (!p0 || !p1)
|
|
532
|
-
return Infinity;
|
|
533
|
-
return this._distPointToSegment(mx, my, t.timeToX(p0.time), t.priceToY(p0.price), t.timeToX(p1.time), t.priceToY(p1.price));
|
|
534
|
-
}
|
|
535
|
-
case 'ray': {
|
|
536
|
-
const p0 = drawing.points[0];
|
|
537
|
-
const p1 = drawing.points[1];
|
|
538
|
-
if (!p0 || !p1)
|
|
539
|
-
return Infinity;
|
|
540
|
-
const x0 = t.timeToX(p0.time);
|
|
541
|
-
const y0 = t.priceToY(p0.price);
|
|
542
|
-
const dx = t.timeToX(p1.time) - x0;
|
|
543
|
-
const dy = t.priceToY(p1.price) - y0;
|
|
544
|
-
// Compute exit using the same formula as the renderer
|
|
545
|
-
const pw = t.plotWidth;
|
|
546
|
-
const ph = t.plotHeight;
|
|
547
|
-
let tMin = Infinity;
|
|
548
|
-
if (dx > 0)
|
|
549
|
-
tMin = Math.min(tMin, (pw - x0) / dx);
|
|
550
|
-
else if (dx < 0)
|
|
551
|
-
tMin = Math.min(tMin, -x0 / dx);
|
|
552
|
-
if (dy > 0)
|
|
553
|
-
tMin = Math.min(tMin, (ph - y0) / dy);
|
|
554
|
-
else if (dy < 0)
|
|
555
|
-
tMin = Math.min(tMin, -y0 / dy);
|
|
556
|
-
const ex = tMin === Infinity ? x0 : x0 + dx * tMin;
|
|
557
|
-
const ey = tMin === Infinity ? y0 : y0 + dy * tMin;
|
|
558
|
-
return this._distPointToSegment(mx, my, x0, y0, ex, ey);
|
|
559
|
-
}
|
|
560
|
-
case 'rectangle': {
|
|
561
|
-
const p0 = drawing.points[0];
|
|
562
|
-
const p1 = drawing.points[1];
|
|
563
|
-
if (!p0 || !p1)
|
|
564
|
-
return Infinity;
|
|
565
|
-
const rx = Math.min(t.timeToX(p0.time), t.timeToX(p1.time));
|
|
566
|
-
const ry = Math.min(t.priceToY(p0.price), t.priceToY(p1.price));
|
|
567
|
-
const rw = Math.abs(t.timeToX(p1.time) - t.timeToX(p0.time));
|
|
568
|
-
const rh = Math.abs(t.priceToY(p1.price) - t.priceToY(p0.price));
|
|
569
|
-
return this._distToRect(mx, my, rx, ry, rw, rh);
|
|
570
|
-
}
|
|
571
|
-
case 'text': {
|
|
572
|
-
const p0 = drawing.points[0];
|
|
573
|
-
if (!p0)
|
|
574
|
-
return Infinity;
|
|
575
|
-
const px = t.timeToX(p0.time);
|
|
576
|
-
const py = t.priceToY(p0.price);
|
|
577
|
-
// Approximate bounding box
|
|
578
|
-
const fs = drawing.fontSize ?? 13;
|
|
579
|
-
return this._distToRect(mx, my, px, py - fs - 8, 80, fs + 8);
|
|
580
|
-
}
|
|
581
|
-
// ── new line types (two-anchor, infinite or extended) ──────────────
|
|
582
|
-
case 'infoLine':
|
|
583
|
-
case 'extendedLine':
|
|
584
|
-
case 'trendAngle': {
|
|
585
|
-
const p0 = drawing.points[0];
|
|
586
|
-
const p1 = drawing.points[1];
|
|
587
|
-
if (!p0 || !p1)
|
|
588
|
-
return Infinity;
|
|
589
|
-
// Approximate by segment; close enough for hit-testing
|
|
590
|
-
return this._distPointToSegment(mx, my, t.timeToX(p0.time), t.priceToY(p0.price), t.timeToX(p1.time), t.priceToY(p1.price));
|
|
591
|
-
}
|
|
592
|
-
case 'horizontalRay': {
|
|
593
|
-
const p0 = drawing.points[0];
|
|
594
|
-
if (!p0)
|
|
595
|
-
return Infinity;
|
|
596
|
-
const x0 = t.timeToX(p0.time);
|
|
597
|
-
const y = t.priceToY(p0.price);
|
|
598
|
-
if (mx < x0)
|
|
599
|
-
return Infinity;
|
|
600
|
-
return Math.abs(my - y);
|
|
601
|
-
}
|
|
602
|
-
case 'crossLine': {
|
|
603
|
-
const p0 = drawing.points[0];
|
|
604
|
-
if (!p0)
|
|
605
|
-
return Infinity;
|
|
606
|
-
const x = t.timeToX(p0.time);
|
|
607
|
-
const y = t.priceToY(p0.price);
|
|
608
|
-
return Math.min(Math.abs(my - y), Math.abs(mx - x));
|
|
609
|
-
}
|
|
610
|
-
// ── channels ──────────────────────────────────────────────────────
|
|
611
|
-
case 'parallelChannel':
|
|
612
|
-
case 'flatTopBottom':
|
|
613
|
-
case 'regressionTrend':
|
|
614
|
-
case 'disjointChannel': {
|
|
615
|
-
const p0 = drawing.points[0];
|
|
616
|
-
const p1 = drawing.points[1];
|
|
617
|
-
if (!p0 || !p1)
|
|
618
|
-
return Infinity;
|
|
619
|
-
const d0 = this._distPointToSegment(mx, my, t.timeToX(p0.time), t.priceToY(p0.price), t.timeToX(p1.time), t.priceToY(p1.price));
|
|
620
|
-
const p2 = drawing.points[2];
|
|
621
|
-
if (!p2)
|
|
622
|
-
return d0;
|
|
623
|
-
const d1 = this._distPointToSegment(mx, my, t.timeToX(p0.time), t.priceToY(p2.price), t.timeToX(p1.time), t.priceToY(p2.price));
|
|
624
|
-
return Math.min(d0, d1);
|
|
625
|
-
}
|
|
626
|
-
// ── pitchforks ────────────────────────────────────────────────────
|
|
627
|
-
case 'pitchfork':
|
|
628
|
-
case 'schiffPitchfork':
|
|
629
|
-
case 'modifiedSchiffPitchfork':
|
|
630
|
-
case 'insidePitchfork': {
|
|
631
|
-
const p0 = drawing.points[0];
|
|
632
|
-
const p1 = drawing.points[1];
|
|
633
|
-
const p2 = drawing.points[2];
|
|
634
|
-
if (!p0 || !p1)
|
|
635
|
-
return Infinity;
|
|
636
|
-
let best = this._distPointToSegment(mx, my, t.timeToX(p0.time), t.priceToY(p0.price), t.timeToX(p1.time), t.priceToY(p1.price));
|
|
637
|
-
if (p2) {
|
|
638
|
-
best = Math.min(best, this._distPointToSegment(mx, my, t.timeToX(p0.time), t.priceToY(p0.price), t.timeToX(p2.time), t.priceToY(p2.price)));
|
|
639
|
-
}
|
|
640
|
-
return best;
|
|
641
|
-
}
|
|
642
|
-
default:
|
|
643
|
-
// Fib/Gann and future drawing types are not hit-testable yet.
|
|
644
|
-
return Infinity;
|
|
645
|
-
}
|
|
646
|
-
}
|
|
647
|
-
/**
|
|
648
|
-
* Returns the nearest visible bar to x-coordinate `mx`, or `null` if no bar
|
|
649
|
-
* falls within half a bar-width.
|
|
650
|
-
*/
|
|
651
|
-
_hitTestBar(mx) {
|
|
652
|
-
const t = this._transform;
|
|
653
|
-
const { from, to } = t.timeRange;
|
|
654
|
-
const visibleSeconds = Math.max(1, to - from);
|
|
655
|
-
const barHalfPx = (t.plotWidth / visibleSeconds) / 2;
|
|
656
|
-
let bestIndex = -1;
|
|
657
|
-
let bestDist = Infinity;
|
|
658
|
-
for (let i = 0; i < this._bars.length; i++) {
|
|
659
|
-
const bar = this._bars[i];
|
|
660
|
-
if (!bar || bar.time < from || bar.time > to)
|
|
661
|
-
continue;
|
|
662
|
-
const dist = Math.abs(mx - t.timeToX(bar.time));
|
|
663
|
-
if (dist < bestDist) {
|
|
664
|
-
bestDist = dist;
|
|
665
|
-
bestIndex = i;
|
|
666
|
-
}
|
|
667
|
-
}
|
|
668
|
-
if (bestIndex < 0 || bestDist > barHalfPx)
|
|
669
|
-
return null;
|
|
670
|
-
const bar = this._bars[bestIndex];
|
|
671
|
-
if (!bar)
|
|
672
|
-
return null;
|
|
673
|
-
return { index: bestIndex, bar };
|
|
674
|
-
}
|
|
675
|
-
// ─── Geometry helpers ────────────────────────────────────────────────────
|
|
676
|
-
/** Distance from point (px, py) to line segment (x1,y1)→(x2,y2). */
|
|
677
|
-
_distPointToSegment(px, py, x1, y1, x2, y2) {
|
|
678
|
-
const dx = x2 - x1;
|
|
679
|
-
const dy = y2 - y1;
|
|
680
|
-
const lenSq = dx * dx + dy * dy;
|
|
681
|
-
if (lenSq === 0)
|
|
682
|
-
return Math.hypot(px - x1, py - y1);
|
|
683
|
-
const t = Math.max(0, Math.min(1, ((px - x1) * dx + (py - y1) * dy) / lenSq));
|
|
684
|
-
return Math.hypot(px - (x1 + t * dx), py - (y1 + t * dy));
|
|
685
|
-
}
|
|
686
|
-
/**
|
|
687
|
-
* Minimum distance from point (px, py) to the perimeter of an
|
|
688
|
-
* axis-aligned rectangle. Returns 0 if the point is inside.
|
|
689
|
-
*/
|
|
690
|
-
_distToRect(px, py, rx, ry, rw, rh) {
|
|
691
|
-
const inside = px >= rx && px <= rx + rw &&
|
|
692
|
-
py >= ry && py <= ry + rh;
|
|
693
|
-
if (inside)
|
|
694
|
-
return 0;
|
|
695
|
-
return Math.min(this._distPointToSegment(px, py, rx, ry, rx + rw, ry), this._distPointToSegment(px, py, rx + rw, ry, rx + rw, ry + rh), this._distPointToSegment(px, py, rx + rw, ry + rh, rx, ry + rh), this._distPointToSegment(px, py, rx, ry + rh, rx, ry));
|
|
696
|
-
}
|
|
697
|
-
}
|
|
698
|
-
//# sourceMappingURL=InteractionManager.js.map
|