gloomberb 0.20.13 → 0.20.14
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/package.json +2 -1
- package/patches/@opentui%2Fcore@0.3.2.patch +22 -4
- package/src/account/auth-dialog.tsx +22 -4
- package/src/account/auth-form.tsx +6 -1
- package/src/account/checkout-handoff.tsx +7 -14
- package/src/account/device-signin-dialog.tsx +35 -24
- package/src/api-client/crypto-markets.ts +10 -0
- package/src/api-client/errors.ts +3 -1
- package/src/api-client/money-markets.ts +7 -0
- package/src/api-client/request.ts +12 -2
- package/src/architecture/floating-promise-allowlist.json +0 -1
- package/src/brokers/signed-in/sign-in-dialog.tsx +38 -38
- package/src/cli/commands/market.ts +95 -23
- package/src/cli/commands/overview.ts +18 -4
- package/src/cli/errors.ts +3 -1
- package/src/cli/freshness.ts +7 -2
- package/src/cli/glossary.ts +10 -5
- package/src/cli/helpers.ts +8 -16
- package/src/cli/history-export.ts +4 -0
- package/src/cli/known-gaps.ts +2 -2
- package/src/cli/listing-arg.ts +79 -11
- package/src/cli/not-a-ticker.ts +4 -0
- package/src/cli/pane-functions/catalog.ts +29 -8
- package/src/cli/pane-functions/headless.ts +5 -2
- package/src/cli/pane-functions/options.ts +7 -0
- package/src/cli/pane-functions/resolver.ts +61 -15
- package/src/cli/result.ts +22 -4
- package/src/cli/symbol-args.ts +5 -3
- package/src/cli/symbol-list.ts +23 -10
- package/src/cli/symbol-rows.ts +135 -3
- package/src/cli/treasury-yields.ts +15 -1
- package/src/components/chart/composite/format.ts +2 -1
- package/src/components/chart/composite/panel-surface/axis-markers.ts +133 -0
- package/src/components/chart/composite/panel-surface/level-labels.tsx +78 -0
- package/src/components/chart/composite/panel-surface/overlays.ts +108 -0
- package/src/components/chart/composite/panel-surface/pan-coast.ts +54 -0
- package/src/components/chart/composite/panel-surface/panel-cursor.tsx +138 -0
- package/src/components/chart/composite/panel-surface/panel-levels.ts +47 -0
- package/src/components/chart/composite/panel-surface/plot-gestures.ts +403 -0
- package/src/components/chart/composite/panel-surface/plot-pointer.ts +65 -0
- package/src/components/chart/composite/panel-surface/plot-wheel.ts +102 -0
- package/src/components/chart/composite/panel-surface/pointer.ts +12 -0
- package/src/components/chart/composite/panel-surface/props.ts +79 -0
- package/src/components/chart/composite/panel-surface/tool-readout.ts +151 -0
- package/src/components/chart/composite/panel-surface/value-axis-gestures.ts +152 -0
- package/src/components/chart/composite/panel-surface.tsx +81 -1177
- package/src/components/command-bar/commands/collection-list.ts +1 -0
- package/src/components/command-bar/routes/root/results.ts +3 -2
- package/src/components/command-bar/routes/root/runtime.ts +3 -2
- package/src/components/command-bar/routes/root/selection.ts +3 -2
- package/src/components/command-bar/routes/root/shortcuts.ts +15 -0
- package/src/components/layout/pane/title.ts +10 -4
- package/src/components/pane-settings-dialog/tui.tsx +6 -3
- package/src/components/pane-settings-dialog.tsx +2 -1
- package/src/components/pane-template-wizard.tsx +2 -0
- package/src/components/ui/choice-dialog.tsx +1 -1
- package/src/components/ui/confirm-dialog.tsx +3 -2
- package/src/components/ui/frame.tsx +7 -3
- package/src/components/ui/icon.tsx +4 -2
- package/src/components/ui/list-view.tsx +8 -0
- package/src/components/ui/number-prompt-dialog.tsx +2 -1
- package/src/components/use-pane-notice-footer.tsx +3 -1
- package/src/docs/snapshot.json +2 -2
- package/src/i18n/es.ts +1 -0
- package/src/i18n/ja.ts +1 -0
- package/src/i18n/ko.ts +1 -0
- package/src/i18n/zh-cn.ts +1 -0
- package/src/i18n/zh-tw.ts +1 -0
- package/src/market-data/market/format.ts +35 -1
- package/src/market-data/quotes/contributions.ts +13 -0
- package/src/market-data/quotes/export-identity.ts +36 -1
- package/src/market-data/quotes/resolution.ts +16 -0
- package/src/plugins/builtin/account-management/password-dialog.tsx +2 -2
- package/src/plugins/builtin/analytics/risk-client.ts +12 -2
- package/src/plugins/builtin/analytics/risk-headless.ts +66 -6
- package/src/plugins/builtin/analytics/risk-math.ts +23 -6
- package/src/plugins/builtin/analytics/risk-model.ts +20 -9
- package/src/plugins/builtin/analytics/risk-pane.tsx +2 -2
- package/src/plugins/builtin/chart-composer/catalog-inventory.ts +5 -2
- package/src/plugins/builtin/chart-composer/editor.tsx +1 -0
- package/src/plugins/builtin/chart-composer/pane.tsx +1 -1
- package/src/plugins/builtin/chart-composer/quick-add.tsx +4 -1
- package/src/plugins/builtin/chart-composer/series-catalog.ts +17 -1
- package/src/plugins/builtin/cloud/plugin.tsx +2 -0
- package/src/plugins/builtin/cloud/thesis/module.tsx +2 -0
- package/src/plugins/builtin/cloud/upgrade-status-widget.tsx +6 -2
- package/src/plugins/builtin/cpi/headless.ts +6 -1
- package/src/plugins/builtin/cpi/model.ts +17 -0
- package/src/plugins/builtin/cpi/pane.tsx +3 -1
- package/src/plugins/builtin/crypto-board/client.ts +6 -1
- package/src/plugins/builtin/crypto-board/headless.ts +10 -2
- package/src/plugins/builtin/crypto-board/model.ts +89 -11
- package/src/plugins/builtin/crypto-board/pane.tsx +20 -5
- package/src/plugins/builtin/dividend-yield/client.ts +24 -2
- package/src/plugins/builtin/dividend-yield/headless.ts +4 -2
- package/src/plugins/builtin/dividend-yield/ranking.ts +2 -1
- package/src/plugins/builtin/dividend-yield/view.ts +5 -1
- package/src/plugins/builtin/earnings/history-table.ts +8 -0
- package/src/plugins/builtin/earnings-ripple/index.tsx +2 -0
- package/src/plugins/builtin/econ/calendar-model.ts +17 -1
- package/src/plugins/builtin/econ/fred-catalog.ts +33 -4
- package/src/plugins/builtin/econ-statistics/headless.ts +21 -0
- package/src/plugins/builtin/macro-day/headless.ts +21 -10
- package/src/plugins/builtin/macro-day/model.ts +40 -3
- package/src/plugins/builtin/macro-day/pane.tsx +26 -8
- package/src/plugins/builtin/macro-day/releases.ts +24 -1
- package/src/plugins/builtin/money-markets/client.ts +4 -1
- package/src/plugins/builtin/money-markets/headless.ts +15 -4
- package/src/plugins/builtin/money-markets/index.tsx +1 -1
- package/src/plugins/builtin/money-markets/pane.tsx +7 -1
- package/src/plugins/builtin/portfolio-list/cli/bulk-symbols.ts +4 -2
- package/src/plugins/builtin/portfolio-list/cli/period-report.ts +61 -11
- package/src/plugins/builtin/portfolio-list/cli/portfolio-command.ts +11 -3
- package/src/plugins/builtin/portfolio-list/cli/watchlist-command.ts +2 -0
- package/src/plugins/builtin/portfolio-list/headless.ts +33 -6
- package/src/plugins/builtin/portfolio-list/pane/index.tsx +5 -3
- package/src/plugins/builtin/portfolio-list/pane/period-pnl.ts +24 -6
- package/src/plugins/builtin/portfolio-list/period-pnl.ts +16 -1
- package/src/plugins/builtin/portfolio-list/summary/index.tsx +4 -1
- package/src/plugins/builtin/portfolio-list/summary/totals.ts +6 -4
- package/src/plugins/builtin/portfolio-list/table.tsx +25 -15
- package/src/plugins/builtin/shared/cloud-upgrade.ts +4 -2
- package/src/plugins/builtin/shared/cpi-year-gap.ts +67 -0
- package/src/plugins/builtin/ticker-detail/adr-pair/headless.ts +45 -2
- package/src/plugins/builtin/ticker-detail/headless.ts +26 -1
- package/src/plugins/builtin/ticker-detail/overview/components.tsx +4 -5
- package/src/plugins/builtin/ticker-detail/overview/model.ts +4 -1
- package/src/plugins/builtin/ticker-detail/overview-tab.tsx +10 -2
- package/src/portfolio/collection-name.ts +2 -1
- package/src/portfolio/column-values.ts +14 -4
- package/src/portfolio/listed-symbol.ts +4 -2
- package/src/portfolio/period-pnl.ts +51 -1
- package/src/renderers/dom/dialog-host.tsx +5 -5
- package/src/renderers/electrobun/view/remote/capability-invoker.ts +1 -1
- package/src/renderers/opentui/dialog-host.tsx +3 -4
- package/src/sources/gloomberb-cloud/normalizers.ts +5 -0
- package/src/sources/provider-router/history.ts +5 -2
- package/src/test-support/plugin-persistence.ts +12 -0
- package/src/tickers/company-name-listing.ts +4 -3
- package/src/tickers/company-name.ts +19 -1
- package/src/tickers/instrument-coverage.ts +27 -0
- package/src/tickers/search/home-listing.ts +38 -1
- package/src/tickers/search/index.ts +3 -2
- package/src/tickers/search/ranking.ts +21 -2
- package/src/time-series/base-series.ts +4 -0
- package/src/time-series/types.ts +5 -2
- package/src/types/financials.ts +13 -0
- package/src/types/headless.ts +2 -0
- package/src/ui/dialog-stack.ts +17 -0
- package/src/ui/dialog.tsx +6 -2
- package/src/utils/crypto-pair.ts +9 -0
- package/src/utils/exchanges.ts +13 -0
- package/src/utils/financial-statements.ts +60 -3
- package/src/utils/futures-generic.ts +14 -0
- package/src/utils/trailing-dividend.ts +13 -3
|
@@ -37,7 +37,7 @@ const YEAR_UNITS = new Set(["y", "yr", "yrs", "year", "years", "yt"]);
|
|
|
37
37
|
const MONTH_UNITS = new Set(["m", "mo", "mos", "month", "months"]);
|
|
38
38
|
/** Words that make a tenor a US Treasury yield: "US", "treasury", "yield". */
|
|
39
39
|
const NAMING_WORDS = new Set(["us", "u", "s", "usa", "ust", "usgg", "treasury", "treasuries", "tsy", "yield", "yields", "rate", "rates",
|
|
40
|
-
"note", "notes", "bill", "bills", "t"]);
|
|
40
|
+
"note", "notes", "bill", "bills", "tbill", "tbills", "t"]);
|
|
41
41
|
/** Words that may sit beside it and say nothing more. */
|
|
42
42
|
const FILLER_WORDS = new Set(["the", "on", "of", "par", "cmt", "constant", "maturity", "govt", "government"]);
|
|
43
43
|
|
|
@@ -86,6 +86,20 @@ export function treasuryYieldLine(tenor: TreasuryYieldTenor): string {
|
|
|
86
86
|
return `US ${tenor.label} Treasury yield: ${yieldCommands(tenor).map(([command, gives]) => `${command} (${gives})`).join(", ")}.`;
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
+
/** FRED's daily bill rate of a tenor Treasury sells bills at (the 4-week has no constant-maturity twin here). */
|
|
90
|
+
const BILL_RATES: ReadonlyMap<string, string> = new Map([["3-month", "DTB3"], ["6-month", "DTB6"], ["1-year", "DTB1YR"]]);
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* `US 3-month Treasury bill: gloom fn BTMM --tab bills ...` for a query that says a bill ("3 month t-bill"): the bill
|
|
94
|
+
* rates first, then the constant-maturity yield the curve reads, which is a different number. Null when it says no bill.
|
|
95
|
+
*/
|
|
96
|
+
export function treasuryBillLine(text: string, tenor: TreasuryYieldTenor): string | null {
|
|
97
|
+
const bill = BILL_RATES.get(tenor.label);
|
|
98
|
+
if (!bill || !/(^|[^a-z])(t-?bills?|bills?)([^a-z]|$)/i.test(text)) return null;
|
|
99
|
+
return `US ${tenor.label} Treasury bill: gloom fn BTMM --tab bills (bill rates), gloom fred ${bill} (daily). `
|
|
100
|
+
+ `Constant-maturity yield, the curve's basis: gloom fred ${tenor.fred}.`;
|
|
101
|
+
}
|
|
102
|
+
|
|
89
103
|
/** "Try `gloom quote \"^TNX\"`, ... for the US 10-year Treasury yield.", after "Not a ticker: US10Y." */
|
|
90
104
|
export function treasuryYieldHint(tenor: TreasuryYieldTenor): string {
|
|
91
105
|
const commands = yieldCommands(tenor).map(([command]) => `\`${command}\``);
|
|
@@ -465,7 +465,8 @@ export function formatChartLegendValue(
|
|
|
465
465
|
...(referencePrice === undefined ? {} : stablePriceOptions("USD", assetCategory, referencePrice)) });
|
|
466
466
|
}
|
|
467
467
|
if (group.startsWith("derived-unit:")) return `${compact} ${trimmed}`;
|
|
468
|
-
|
|
468
|
+
// A total in a currency reads at its scale ($223.4B), as a coin's volume, the value it traded, does too.
|
|
469
|
+
if (group.split(":")[0] === "currency-total" || (group === "volume" && unitCurrencyCode(trimmed))) {
|
|
469
470
|
const scale = ([[1e12, "T"], [1e9, "B"], [1e6, "M"], [1e3, "K"]] as const)
|
|
470
471
|
.find(([divisor]) => Math.abs(value) >= divisor);
|
|
471
472
|
if (scale) {
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { useMemo } from "react";
|
|
2
|
+
import { compositeAxisTagFormatter } from "../format";
|
|
3
|
+
import { fitAxisLabels, type ProjectedLevel } from "../levels";
|
|
4
|
+
import { axisLabelRows } from "../panel-labels";
|
|
5
|
+
import { placeAxisTicks } from "../price-axis-labels";
|
|
6
|
+
import { compositeAxisTickLabels, renderCompositeAxisText } from "../text-renderer";
|
|
7
|
+
import type { CompositeAxisDomain } from "../types";
|
|
8
|
+
import type { CompositePanelSurfaceProps } from "./props";
|
|
9
|
+
import type { ToolAnchorMarker } from "./tool-readout";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Each value axis' grid labels, and the values tagged on it: the last price,
|
|
13
|
+
* the prior close, the levels and a measure's anchor, fitted around each
|
|
14
|
+
* other so no two labels share a row.
|
|
15
|
+
*/
|
|
16
|
+
export function usePanelAxisMarkers({
|
|
17
|
+
panel,
|
|
18
|
+
colors,
|
|
19
|
+
formatAxisValue,
|
|
20
|
+
leftAxisWidth,
|
|
21
|
+
rightAxisWidth,
|
|
22
|
+
selectedLevelId,
|
|
23
|
+
levelDomain,
|
|
24
|
+
projectedLevels,
|
|
25
|
+
anchorMarker,
|
|
26
|
+
cellHeightPx,
|
|
27
|
+
fractionalViewport,
|
|
28
|
+
}: Pick<CompositePanelSurfaceProps, "panel" | "colors" | "formatAxisValue" | "leftAxisWidth" | "rightAxisWidth" | "selectedLevelId"> & {
|
|
29
|
+
levelDomain: CompositeAxisDomain | null;
|
|
30
|
+
projectedLevels: readonly ProjectedLevel[];
|
|
31
|
+
anchorMarker: ToolAnchorMarker | null;
|
|
32
|
+
cellHeightPx: number;
|
|
33
|
+
fractionalViewport: boolean;
|
|
34
|
+
}) {
|
|
35
|
+
const leftAxisLabels = useMemo(
|
|
36
|
+
() => axisLabelRows(
|
|
37
|
+
renderCompositeAxisText(panel.axes.left, panel.height, leftAxisWidth, "left", formatAxisValue),
|
|
38
|
+
),
|
|
39
|
+
[formatAxisValue, leftAxisWidth, panel],
|
|
40
|
+
);
|
|
41
|
+
const rightAxisLabels = useMemo(
|
|
42
|
+
() => axisLabelRows(
|
|
43
|
+
renderCompositeAxisText(panel.axes.right, panel.height, rightAxisWidth, "right", formatAxisValue),
|
|
44
|
+
),
|
|
45
|
+
[formatAxisValue, panel, rightAxisWidth],
|
|
46
|
+
);
|
|
47
|
+
const leftAxisTicks = useMemo(
|
|
48
|
+
() => compositeAxisTickLabels(panel.axes.left, leftAxisWidth, formatAxisValue),
|
|
49
|
+
[formatAxisValue, leftAxisWidth, panel],
|
|
50
|
+
);
|
|
51
|
+
const rightAxisTicks = useMemo(
|
|
52
|
+
() => compositeAxisTickLabels(panel.axes.right, rightAxisWidth, formatAxisValue),
|
|
53
|
+
[formatAxisValue, panel, rightAxisWidth],
|
|
54
|
+
);
|
|
55
|
+
const lastPriceMarker = useMemo(() => {
|
|
56
|
+
const marker = panel.lastPrice;
|
|
57
|
+
const domain = marker ? panel.axes[marker.axis] : undefined;
|
|
58
|
+
if (!marker || !domain) return null;
|
|
59
|
+
return {
|
|
60
|
+
side: marker.axis,
|
|
61
|
+
yRatio: marker.yRatio,
|
|
62
|
+
label: compositeAxisTagFormatter(formatAxisValue)(marker.value, domain),
|
|
63
|
+
color: marker.color,
|
|
64
|
+
};
|
|
65
|
+
}, [formatAxisValue, panel]);
|
|
66
|
+
const priorCloseMarker = useMemo(() => {
|
|
67
|
+
const marker = panel.priorClose;
|
|
68
|
+
const domain = marker ? panel.axes[marker.axis] : undefined;
|
|
69
|
+
if (!marker || !domain) return null;
|
|
70
|
+
return {
|
|
71
|
+
side: marker.axis,
|
|
72
|
+
yRatio: marker.yRatio,
|
|
73
|
+
label: compositeAxisTagFormatter(formatAxisValue)(marker.value, domain),
|
|
74
|
+
color: colors.textDim,
|
|
75
|
+
};
|
|
76
|
+
}, [colors.textDim, formatAxisValue, panel]);
|
|
77
|
+
const levelMarkers = useMemo(() => levelDomain ? [...projectedLevels]
|
|
78
|
+
// The picked level's label first, then alerts', when they compete for room.
|
|
79
|
+
.sort((left, right) => Number(right.level.id === selectedLevelId) - Number(left.level.id === selectedLevelId)
|
|
80
|
+
|| Number(left.level.editable) - Number(right.level.editable))
|
|
81
|
+
.map(({ level, yRatio }) => ({
|
|
82
|
+
id: level.id,
|
|
83
|
+
side: levelDomain.side,
|
|
84
|
+
yRatio,
|
|
85
|
+
label: level.axisLabel ?? compositeAxisTagFormatter(formatAxisValue)(level.value, levelDomain),
|
|
86
|
+
color: level.color,
|
|
87
|
+
prioritizeAxisLabel: level.prioritizeAxisLabel,
|
|
88
|
+
})) : [], [formatAxisValue, levelDomain, projectedLevels, selectedLevelId]);
|
|
89
|
+
const buildAxisMarkers = (side: "left" | "right") => {
|
|
90
|
+
if (!panel.axes[side]) return undefined;
|
|
91
|
+
const toAxisMarker = (marker: { id?: string; yRatio: number; label: string; color: string }) => ({
|
|
92
|
+
id: marker.id,
|
|
93
|
+
row: Math.round(marker.yRatio * Math.max(panel.height - 1, 0)),
|
|
94
|
+
pixelY: marker.yRatio * Math.max(panel.height * cellHeightPx - 1, 0),
|
|
95
|
+
label: marker.label,
|
|
96
|
+
color: marker.color,
|
|
97
|
+
});
|
|
98
|
+
const onSide = <T extends { side: string | null }>(marker: T | null): marker is T => (
|
|
99
|
+
!!marker && (marker.side === null || marker.side === side)
|
|
100
|
+
);
|
|
101
|
+
const fixed = [lastPriceMarker, anchorMarker].filter(onSide).map(toAxisMarker);
|
|
102
|
+
// The prior close's label yields to every level's.
|
|
103
|
+
const referenceLabels = levelMarkers.filter((marker) => marker.prioritizeAxisLabel && onSide(marker)).map(toAxisMarker);
|
|
104
|
+
const levelLabels = [...levelMarkers.filter((marker) => !marker.prioritizeAxisLabel), priorCloseMarker].filter(onSide).map(toAxisMarker);
|
|
105
|
+
// A label is a row tall, so close levels would stack into an unreadable
|
|
106
|
+
// pile and hide the last price: each keeps its line, and only the labels
|
|
107
|
+
// with a row to themselves show. Desktop badges sit between rows.
|
|
108
|
+
const grid = fractionalViewport
|
|
109
|
+
? placeAxisTicks({ ticks: side === "left" ? leftAxisTicks : rightAxisTicks, height: panel.height, cellHeightPx }).map((tick) => ({
|
|
110
|
+
...toAxisMarker({ yRatio: tick.ratio, label: tick.label, color: colors.textDim }),
|
|
111
|
+
pixelY: tick.topPercent / 100 * Math.max(panel.height * cellHeightPx - 1, 1),
|
|
112
|
+
}))
|
|
113
|
+
: [...(side === "left" ? leftAxisLabels : rightAxisLabels)].map(([row, label]) => ({ id: undefined, row, pixelY: row * cellHeightPx, label, color: colors.textDim }));
|
|
114
|
+
const rowOf = (marker: ReturnType<typeof toAxisMarker>) => fractionalViewport ? marker.pixelY / cellHeightPx : marker.row;
|
|
115
|
+
// Named guides are part of the chart's readout: ordinary ticks yield to
|
|
116
|
+
// them, while a live price or cursor still keeps its place.
|
|
117
|
+
const references = fitAxisLabels(referenceLabels, fixed, rowOf);
|
|
118
|
+
const markers = [
|
|
119
|
+
...references,
|
|
120
|
+
...fitAxisLabels(levelLabels, [...references, ...fixed, ...grid], rowOf),
|
|
121
|
+
...fixed,
|
|
122
|
+
];
|
|
123
|
+
return markers.length > 0 ? markers : undefined;
|
|
124
|
+
};
|
|
125
|
+
return {
|
|
126
|
+
leftAxisLabels,
|
|
127
|
+
rightAxisLabels,
|
|
128
|
+
leftAxisTicks,
|
|
129
|
+
rightAxisTicks,
|
|
130
|
+
leftAxisMarkers: buildAxisMarkers("left"),
|
|
131
|
+
rightAxisMarkers: buildAxisMarkers("right"),
|
|
132
|
+
};
|
|
133
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { Box, Text } from "../../../../ui";
|
|
2
|
+
import { layoutLevelLabels, levelLabelStyle, levelLabelText } from "../level-labels";
|
|
3
|
+
import { fitAxisLabels, type CompositeChartLevels, type ProjectedLevel } from "../levels";
|
|
4
|
+
import type { CompositeAxisDomain } from "../types";
|
|
5
|
+
import { usePanelCursor, type PanelCursorView } from "./panel-cursor";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A panel's level labels at the plot's right edge, which step aside where the
|
|
9
|
+
* cursor's label covers theirs on the axis. A level's label leaves its value
|
|
10
|
+
* to the axis, unless the cursor's covers it there.
|
|
11
|
+
*/
|
|
12
|
+
export function PanelLevelLabels({
|
|
13
|
+
view,
|
|
14
|
+
projectedLevels,
|
|
15
|
+
levels,
|
|
16
|
+
levelDomain,
|
|
17
|
+
plotWidth,
|
|
18
|
+
leftAxisWidth,
|
|
19
|
+
rightAxisWidth,
|
|
20
|
+
axisGap,
|
|
21
|
+
leftAxisMarkers,
|
|
22
|
+
rightAxisMarkers,
|
|
23
|
+
measuredPixelHeight,
|
|
24
|
+
background,
|
|
25
|
+
isDesktopWeb,
|
|
26
|
+
cellHeightPx,
|
|
27
|
+
cellWidthPx,
|
|
28
|
+
}: {
|
|
29
|
+
view: PanelCursorView;
|
|
30
|
+
projectedLevels: readonly ProjectedLevel[];
|
|
31
|
+
levels: CompositeChartLevels | null;
|
|
32
|
+
levelDomain: CompositeAxisDomain | null;
|
|
33
|
+
plotWidth: number;
|
|
34
|
+
leftAxisWidth: number;
|
|
35
|
+
rightAxisWidth: number;
|
|
36
|
+
axisGap: number;
|
|
37
|
+
leftAxisMarkers: readonly { id?: string }[] | undefined;
|
|
38
|
+
rightAxisMarkers: readonly { id?: string }[] | undefined;
|
|
39
|
+
/** The plot's measured height in px. */
|
|
40
|
+
measuredPixelHeight: number;
|
|
41
|
+
background: string;
|
|
42
|
+
isDesktopWeb: boolean;
|
|
43
|
+
cellHeightPx: number;
|
|
44
|
+
cellWidthPx: number;
|
|
45
|
+
}) {
|
|
46
|
+
const { panel } = view;
|
|
47
|
+
const { activeCursorYRatio, scene } = usePanelCursor(view);
|
|
48
|
+
const cursorValues = scene.cursorValues;
|
|
49
|
+
return (
|
|
50
|
+
<>
|
|
51
|
+
{(isDesktopWeb
|
|
52
|
+
? layoutLevelLabels(projectedLevels, measuredPixelHeight, cellHeightPx)
|
|
53
|
+
: fitAxisLabels(projectedLevels.filter((entry) => entry.level.label), [], (entry) => entry.yRatio * Math.max(1, panel.height - 1))
|
|
54
|
+
).map((entry) => {
|
|
55
|
+
const { level, yRatio } = entry;
|
|
56
|
+
const axisWidth = levelDomain?.side === "left" ? leftAxisWidth : rightAxisWidth;
|
|
57
|
+
const axisMarkers = levelDomain?.side === "left" ? leftAxisMarkers : rightAxisMarkers;
|
|
58
|
+
const cursorCovers = activeCursorYRatio !== null && Math.abs(activeCursorYRatio - yRatio) * panel.height < 1;
|
|
59
|
+
const axisVisible = !cursorCovers && (level.axisLabel?.length ?? 0) <= axisWidth && !!axisMarkers?.some((marker) => marker.id === level.id);
|
|
60
|
+
// A cursor on a bar at the level's own value (RETURN's on its best
|
|
61
|
+
// day) reads that value on the axis already: the label does not repeat it.
|
|
62
|
+
const cursorReadsLevel = cursorCovers && !!levels
|
|
63
|
+
&& cursorValues.some((entry) => entry.seriesId === levels.seriesId && entry.value === level.value);
|
|
64
|
+
const label = levelLabelText(level, axisVisible || cursorReadsLevel);
|
|
65
|
+
const labelStyle = isDesktopWeb ? {
|
|
66
|
+
...levelLabelStyle(entry, background), left: "auto", right: (rightAxisWidth + (rightAxisWidth ? axisGap : 0) + 1) * cellWidthPx,
|
|
67
|
+
maxWidth: Math.max(1, plotWidth - 2) * cellWidthPx,
|
|
68
|
+
} : undefined;
|
|
69
|
+
const labelWidth = Math.min(plotWidth - 2, label.length + 2);
|
|
70
|
+
return <Box key={`annotation:${level.id}`} position="absolute" left={leftAxisWidth + (leftAxisWidth ? axisGap : 0) + plotWidth - labelWidth - 1} top={yRatio * Math.max(0, panel.height - 1)} height={1}
|
|
71
|
+
width={labelWidth} flexDirection="row" justifyContent="flex-end" backgroundColor={background} zIndex={20}
|
|
72
|
+
style={labelStyle} data-gloom-role="chart-level-label">
|
|
73
|
+
<Text fg={labelStyle?.color ?? level.color} wrapMode="none" style={isDesktopWeb ? { lineHeight: 1 } : undefined}>{label}</Text>
|
|
74
|
+
</Box>;
|
|
75
|
+
})}
|
|
76
|
+
</>
|
|
77
|
+
);
|
|
78
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { useMemo } from "react";
|
|
2
|
+
import type { ChartSurfaceProps } from "../../../../ui";
|
|
3
|
+
import { colors as themeColors } from "../../../../theme/colors";
|
|
4
|
+
import { levelVectors, paintLevels, type ProjectedLevel } from "../levels";
|
|
5
|
+
import type { useCompositePanelBitmap } from "../panel-bitmap";
|
|
6
|
+
import { buildChartToolVectors, drawChartToolOverlay, type ChartToolDrag } from "../tools";
|
|
7
|
+
import type { CompositePanelSurfaceProps } from "./props";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* What draws over the plot's raster: the panel's drawings, the tool in hand
|
|
11
|
+
* and the levels. The terminal paints them into the raster's layers; the
|
|
12
|
+
* desktop and the web draw them as vectors over it.
|
|
13
|
+
*/
|
|
14
|
+
export function usePanelOverlays({
|
|
15
|
+
bitmap,
|
|
16
|
+
isDesktopWeb,
|
|
17
|
+
panel,
|
|
18
|
+
scene,
|
|
19
|
+
colors,
|
|
20
|
+
drawings,
|
|
21
|
+
selectedDrawingId,
|
|
22
|
+
drawColor,
|
|
23
|
+
selectedLevelId,
|
|
24
|
+
activeDrag,
|
|
25
|
+
toolDirection,
|
|
26
|
+
projectedLevels,
|
|
27
|
+
}: Pick<CompositePanelSurfaceProps, "panel" | "scene" | "colors" | "drawings" | "selectedDrawingId" | "drawColor" | "selectedLevelId"> & {
|
|
28
|
+
bitmap: ReturnType<typeof useCompositePanelBitmap>;
|
|
29
|
+
isDesktopWeb: boolean;
|
|
30
|
+
activeDrag: ChartToolDrag | null;
|
|
31
|
+
toolDirection: "up" | "down" | undefined;
|
|
32
|
+
projectedLevels: readonly ProjectedLevel[];
|
|
33
|
+
}) {
|
|
34
|
+
const panelDrawings = useMemo(
|
|
35
|
+
() => drawings.filter((drawing) => drawing.panelId === panel.id),
|
|
36
|
+
[drawings, panel.id],
|
|
37
|
+
);
|
|
38
|
+
// Drawings project through the time scale alone: the crosshair moving,
|
|
39
|
+
// here or on a linked chart, must not repaint the plot, which the terminal's
|
|
40
|
+
// kitty surface would upload whole again.
|
|
41
|
+
const drawingProjection = useMemo(() => ({ timeScale: scene.timeScale }), [scene.timeScale]);
|
|
42
|
+
const bitmapLayers = useMemo(() => {
|
|
43
|
+
if (!bitmap) return null;
|
|
44
|
+
// The desktop composites overlays as vectors, so the plot raster stays put
|
|
45
|
+
// while a tool drags. Copying and reblending it per frame is what made the
|
|
46
|
+
// ruler feel heavy.
|
|
47
|
+
if (isDesktopWeb) return [bitmap];
|
|
48
|
+
const base = !activeDrag && panelDrawings.length === 0 ? bitmap : drawChartToolOverlay(
|
|
49
|
+
bitmap,
|
|
50
|
+
activeDrag,
|
|
51
|
+
{
|
|
52
|
+
positive: themeColors.positive,
|
|
53
|
+
negative: colors.negative,
|
|
54
|
+
zoom: colors.crosshair,
|
|
55
|
+
draw: drawColor,
|
|
56
|
+
},
|
|
57
|
+
toolDirection ?? "up",
|
|
58
|
+
{ scene: drawingProjection, panel, items: panelDrawings, selectedId: selectedDrawingId },
|
|
59
|
+
);
|
|
60
|
+
return [projectedLevels.length > 0 ? paintLevels(base, projectedLevels, selectedLevelId) : base];
|
|
61
|
+
}, [
|
|
62
|
+
activeDrag,
|
|
63
|
+
bitmap,
|
|
64
|
+
colors.crosshair,
|
|
65
|
+
colors.negative,
|
|
66
|
+
drawColor,
|
|
67
|
+
drawingProjection,
|
|
68
|
+
isDesktopWeb,
|
|
69
|
+
panel,
|
|
70
|
+
panelDrawings,
|
|
71
|
+
projectedLevels,
|
|
72
|
+
selectedDrawingId,
|
|
73
|
+
selectedLevelId,
|
|
74
|
+
toolDirection,
|
|
75
|
+
]);
|
|
76
|
+
const vectors = useMemo<ChartSurfaceProps["vectors"]>(() => {
|
|
77
|
+
if (!isDesktopWeb) return null;
|
|
78
|
+
const shapes = [...levelVectors(projectedLevels, selectedLevelId), ...buildChartToolVectors({
|
|
79
|
+
scene,
|
|
80
|
+
panel,
|
|
81
|
+
drawings: panelDrawings,
|
|
82
|
+
selectedId: selectedDrawingId,
|
|
83
|
+
drag: activeDrag,
|
|
84
|
+
colors: {
|
|
85
|
+
positive: themeColors.positive,
|
|
86
|
+
negative: colors.negative,
|
|
87
|
+
zoom: colors.crosshair,
|
|
88
|
+
draw: drawColor,
|
|
89
|
+
},
|
|
90
|
+
direction: toolDirection ?? "up",
|
|
91
|
+
})];
|
|
92
|
+
return shapes.length > 0 ? shapes : null;
|
|
93
|
+
}, [
|
|
94
|
+
activeDrag,
|
|
95
|
+
colors.crosshair,
|
|
96
|
+
colors.negative,
|
|
97
|
+
drawColor,
|
|
98
|
+
isDesktopWeb,
|
|
99
|
+
panel,
|
|
100
|
+
panelDrawings,
|
|
101
|
+
projectedLevels,
|
|
102
|
+
scene,
|
|
103
|
+
selectedDrawingId,
|
|
104
|
+
selectedLevelId,
|
|
105
|
+
toolDirection,
|
|
106
|
+
]);
|
|
107
|
+
return { bitmapLayers, vectors };
|
|
108
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef } from "react";
|
|
2
|
+
import { PAN_COAST_START_SPEED, PAN_COAST_STOP_SPEED, panCoastStep } from "../panel-gestures";
|
|
3
|
+
import type { CompositePanelSurfaceProps } from "./props";
|
|
4
|
+
import { webFrame } from "./pointer";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A pan let go while still moving keeps going and slows down, on the
|
|
8
|
+
* desktop and web only: in a terminal every frame would upload the plot again.
|
|
9
|
+
*/
|
|
10
|
+
export function usePanCoast({
|
|
11
|
+
isDesktopWeb,
|
|
12
|
+
plotWidth,
|
|
13
|
+
onPanViewport,
|
|
14
|
+
}: Pick<CompositePanelSurfaceProps, "onPanViewport"> & {
|
|
15
|
+
isDesktopWeb: boolean;
|
|
16
|
+
/** The plot's measured width in cells, which the speed is read against. */
|
|
17
|
+
plotWidth: number;
|
|
18
|
+
}) {
|
|
19
|
+
const coastFrameRef = useRef<number | null>(null);
|
|
20
|
+
const coastPanRef = useRef(onPanViewport);
|
|
21
|
+
coastPanRef.current = onPanViewport;
|
|
22
|
+
const stopCoast = useCallback(() => {
|
|
23
|
+
if (coastFrameRef.current === null) return;
|
|
24
|
+
webFrame.cancelAnimationFrame?.(coastFrameRef.current);
|
|
25
|
+
coastFrameRef.current = null;
|
|
26
|
+
globalThis.removeEventListener?.("pointerdown", stopCoast, true);
|
|
27
|
+
globalThis.removeEventListener?.("keydown", stopCoast, true);
|
|
28
|
+
}, []);
|
|
29
|
+
const startCoast = useCallback((velocity: number, positionsPerCell: number) => {
|
|
30
|
+
stopCoast();
|
|
31
|
+
const requestFrame = webFrame.requestAnimationFrame;
|
|
32
|
+
const widthCells = Math.max(plotWidth, 1);
|
|
33
|
+
const plotWidthsPerSecond = (speed: number) => Math.abs(speed) * 1000 / widthCells;
|
|
34
|
+
if (!isDesktopWeb || !requestFrame || plotWidthsPerSecond(velocity) < PAN_COAST_START_SPEED) return;
|
|
35
|
+
if (globalThis.matchMedia?.("(prefers-reduced-motion: reduce)").matches) return;
|
|
36
|
+
let speed = velocity;
|
|
37
|
+
let last = performance.now();
|
|
38
|
+
const step = () => {
|
|
39
|
+
const now = performance.now();
|
|
40
|
+
const next = panCoastStep(speed, now - last);
|
|
41
|
+
last = now;
|
|
42
|
+
speed = next.velocity;
|
|
43
|
+
coastPanRef.current(next.cells * positionsPerCell);
|
|
44
|
+
if (plotWidthsPerSecond(speed) < PAN_COAST_STOP_SPEED) stopCoast();
|
|
45
|
+
else coastFrameRef.current = requestFrame(step);
|
|
46
|
+
};
|
|
47
|
+
coastFrameRef.current = requestFrame(step);
|
|
48
|
+
// Any press or key anywhere, a range button or a reset, takes the view back from the coast.
|
|
49
|
+
globalThis.addEventListener?.("pointerdown", stopCoast, true);
|
|
50
|
+
globalThis.addEventListener?.("keydown", stopCoast, true);
|
|
51
|
+
}, [isDesktopWeb, plotWidth, stopCoast]);
|
|
52
|
+
useEffect(() => stopCoast, [stopCoast]);
|
|
53
|
+
return { stopCoast, startCoast };
|
|
54
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { useMemo, type ComponentProps, type RefObject } from "react";
|
|
2
|
+
import { ChartSurface, Text, useUiCapabilities, type BoxRenderable } from "../../../../ui";
|
|
3
|
+
import { useCursorScene, useValueStore, type CompositeCursorSource, type ValueStore } from "../cursor-source";
|
|
4
|
+
import type { CompositeAxisValueFormatter } from "../format";
|
|
5
|
+
import { writeLevelText, type ProjectedLevel } from "../levels";
|
|
6
|
+
import type { useCompositePanelBitmap } from "../panel-bitmap";
|
|
7
|
+
import { cursorAxisLabel, resolvePanelCrosshair, resolveSeriesAxisTags, resolveSeriesCursorYRatio } from "../panel-labels";
|
|
8
|
+
import { PriceAxisLabels } from "../price-axis-labels";
|
|
9
|
+
import { renderCompositePanelText } from "../text-renderer";
|
|
10
|
+
import type { CompositeAxisSide, CompositePanelScene } from "../types";
|
|
11
|
+
import type { KeyboardToolDrag } from "../use-composite-chart-keyboard";
|
|
12
|
+
import type { LevelGrab } from "./panel-levels";
|
|
13
|
+
|
|
14
|
+
export const DESKTOP_PLOT_STYLE = { flex: "1 1 0px", width: "auto", minWidth: 0, height: "auto", alignSelf: "stretch" } as const;
|
|
15
|
+
/** Over a level that drags, or while one does: it moves up and down only. */
|
|
16
|
+
const DESKTOP_PLOT_LEVEL_STYLE = { ...DESKTOP_PLOT_STYLE, cursor: "ns-resize" } as const;
|
|
17
|
+
export const DESKTOP_PLOT_PICK_STYLE = { ...DESKTOP_PLOT_STYLE, cursor: "crosshair" } as const;
|
|
18
|
+
|
|
19
|
+
/** What a panel's crosshair is read from: everything else in the panel holds still while it moves. */
|
|
20
|
+
export interface PanelCursorView {
|
|
21
|
+
panel: CompositePanelScene;
|
|
22
|
+
cursor: CompositeCursorSource;
|
|
23
|
+
pointerY: ValueStore<number | null>;
|
|
24
|
+
keyboardDrag: KeyboardToolDrag | null;
|
|
25
|
+
sharedCursor: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The crosshair over a panel, read by the parts that draw it, so a pointer move renders only them. */
|
|
29
|
+
export function usePanelCursor({ panel, cursor, pointerY, keyboardDrag, sharedCursor }: PanelCursorView) {
|
|
30
|
+
const scene = useCursorScene(cursor);
|
|
31
|
+
const cursorYRatio = useValueStore(pointerY);
|
|
32
|
+
// A keyboard placement's end sits at the level the user put it, so it
|
|
33
|
+
// draws a level line where otherwise only the pointer would.
|
|
34
|
+
const heldCursorYRatio = keyboardDrag ? keyboardDrag.drag.endYRatio : sharedCursor ? null : cursorYRatio;
|
|
35
|
+
const seriesCursorYRatio = useMemo(
|
|
36
|
+
() => resolveSeriesCursorYRatio(panel, scene),
|
|
37
|
+
[panel, scene],
|
|
38
|
+
);
|
|
39
|
+
return {
|
|
40
|
+
scene,
|
|
41
|
+
cursorYRatio,
|
|
42
|
+
activeCursorYRatio: scene.cursorXRatio === null ? null : heldCursorYRatio ?? seriesCursorYRatio,
|
|
43
|
+
// The chart under the pointer draws both lines: the level line follows the
|
|
44
|
+
// pointer, and its value sits on the axes, beside each series' own tag on
|
|
45
|
+
// the desktop and the web. A keyboard or shared cursor knows its column
|
|
46
|
+
// only, so a chart following another chart's cursor draws the time line
|
|
47
|
+
// alone. A tool placed from the keyboard is the exception: its end is a level.
|
|
48
|
+
pointerCursorYRatio: scene.cursorXRatio === null ? null : heldCursorYRatio,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The plot, with the crosshair over it and, in a terminal without graphics, the text that draws both. */
|
|
53
|
+
export function PanelPlotSurface({
|
|
54
|
+
view,
|
|
55
|
+
plotRef,
|
|
56
|
+
bitmap,
|
|
57
|
+
crosshairColor,
|
|
58
|
+
textColor,
|
|
59
|
+
textFallback,
|
|
60
|
+
plotWidth,
|
|
61
|
+
projectedLevels,
|
|
62
|
+
selectedLevelId,
|
|
63
|
+
levelGrab,
|
|
64
|
+
...surface
|
|
65
|
+
}: Omit<ComponentProps<typeof ChartSurface>, "ref" | "crosshair" | "children"> & {
|
|
66
|
+
view: PanelCursorView;
|
|
67
|
+
plotRef: RefObject<BoxRenderable | null>;
|
|
68
|
+
bitmap: ReturnType<typeof useCompositePanelBitmap>;
|
|
69
|
+
crosshairColor: string;
|
|
70
|
+
textColor: string;
|
|
71
|
+
textFallback: boolean;
|
|
72
|
+
plotWidth: number;
|
|
73
|
+
projectedLevels: readonly ProjectedLevel[];
|
|
74
|
+
selectedLevelId: string | null;
|
|
75
|
+
levelGrab: LevelGrab;
|
|
76
|
+
}) {
|
|
77
|
+
const { panel } = view;
|
|
78
|
+
const { scene, cursorYRatio, pointerCursorYRatio } = usePanelCursor(view);
|
|
79
|
+
const crosshair = useMemo(
|
|
80
|
+
() => resolvePanelCrosshair(panel, bitmap, scene, pointerCursorYRatio, crosshairColor),
|
|
81
|
+
[bitmap, crosshairColor, panel, pointerCursorYRatio, scene],
|
|
82
|
+
);
|
|
83
|
+
const textLines = useMemo(
|
|
84
|
+
() => textFallback
|
|
85
|
+
? writeLevelText(
|
|
86
|
+
renderCompositePanelText(panel, plotWidth, scene.cursorXRatio, pointerCursorYRatio),
|
|
87
|
+
projectedLevels,
|
|
88
|
+
selectedLevelId,
|
|
89
|
+
)
|
|
90
|
+
: [],
|
|
91
|
+
[panel, plotWidth, pointerCursorYRatio, projectedLevels, scene.cursorXRatio, selectedLevelId, textFallback],
|
|
92
|
+
);
|
|
93
|
+
const grab = typeof levelGrab === "function" ? cursorYRatio !== null && levelGrab(cursorYRatio) : levelGrab;
|
|
94
|
+
return (
|
|
95
|
+
<ChartSurface
|
|
96
|
+
ref={plotRef}
|
|
97
|
+
{...surface}
|
|
98
|
+
style={grab && surface.style ? DESKTOP_PLOT_LEVEL_STYLE : surface.style}
|
|
99
|
+
cursor={grab && surface.cursor ? "ns-resize" : surface.cursor}
|
|
100
|
+
crosshair={crosshair}
|
|
101
|
+
>
|
|
102
|
+
{textLines.map((line, index) => <Text key={index} fg={textColor}>{line}</Text>)}
|
|
103
|
+
</ChartSurface>
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** A value axis, with the cursor's value on it and, on the desktop, each series' value at the cursor. */
|
|
108
|
+
export function PanelValueAxis({
|
|
109
|
+
view,
|
|
110
|
+
formatAxisValue,
|
|
111
|
+
side,
|
|
112
|
+
...axis
|
|
113
|
+
}: Omit<ComponentProps<typeof PriceAxisLabels>, "cursorRow" | "cursorPixelY" | "cursorLabel" | "seriesTags" | "side"> & {
|
|
114
|
+
view: PanelCursorView;
|
|
115
|
+
formatAxisValue?: CompositeAxisValueFormatter;
|
|
116
|
+
side: CompositeAxisSide;
|
|
117
|
+
}) {
|
|
118
|
+
const { panel } = view;
|
|
119
|
+
const isDesktopWeb = useUiCapabilities().nativePaneChrome === true;
|
|
120
|
+
const { cellHeightPx = 18 } = useUiCapabilities();
|
|
121
|
+
const { scene, activeCursorYRatio, pointerCursorYRatio } = usePanelCursor(view);
|
|
122
|
+
// The pointer's value on the axes follows the level line it belongs to.
|
|
123
|
+
const axisCursorYRatio = isDesktopWeb ? pointerCursorYRatio : activeCursorYRatio;
|
|
124
|
+
const seriesTags = useMemo(
|
|
125
|
+
() => isDesktopWeb ? resolveSeriesAxisTags(panel, scene, formatAxisValue).filter((tag) => tag.side === side) : undefined,
|
|
126
|
+
[formatAxisValue, isDesktopWeb, panel, scene, side],
|
|
127
|
+
);
|
|
128
|
+
return (
|
|
129
|
+
<PriceAxisLabels
|
|
130
|
+
{...axis}
|
|
131
|
+
side={side}
|
|
132
|
+
cursorRow={activeCursorYRatio === null ? null : Math.round(activeCursorYRatio * Math.max(panel.height - 1, 0))}
|
|
133
|
+
cursorPixelY={activeCursorYRatio === null ? null : activeCursorYRatio * Math.max(panel.height * cellHeightPx - 1, 0)}
|
|
134
|
+
cursorLabel={cursorAxisLabel(panel, side, axisCursorYRatio, formatAxisValue)}
|
|
135
|
+
seriesTags={seriesTags}
|
|
136
|
+
/>
|
|
137
|
+
);
|
|
138
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { useMemo, useRef, useState } from "react";
|
|
2
|
+
import { hitTestLevel, levelGrabRatio, projectLevels, type ProjectedLevel } from "../levels";
|
|
3
|
+
import type { CompositePanelSurfaceProps } from "./props";
|
|
4
|
+
|
|
5
|
+
/** Over a level a drag moves: always while one moves, never, or at the pointer's height. */
|
|
6
|
+
export type LevelGrab = boolean | ((cursorYRatio: number) => boolean);
|
|
7
|
+
|
|
8
|
+
/** A level being dragged, where it is now: it is saved once, when the pointer lets go. */
|
|
9
|
+
export interface LevelDraft {
|
|
10
|
+
id: string;
|
|
11
|
+
value: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const NO_LEVELS: readonly ProjectedLevel[] = [];
|
|
15
|
+
|
|
16
|
+
/** The chart's levels on this panel: where each draws, the one being dragged, and where a drag would grab one. */
|
|
17
|
+
export function usePanelLevels({
|
|
18
|
+
panel,
|
|
19
|
+
levels,
|
|
20
|
+
armedTool,
|
|
21
|
+
onPickTime,
|
|
22
|
+
onPickPrice,
|
|
23
|
+
}: Pick<CompositePanelSurfaceProps, "panel" | "levels" | "armedTool" | "onPickTime" | "onPickPrice">) {
|
|
24
|
+
// A dragged level moves here and is saved once, when the pointer lets go.
|
|
25
|
+
const [levelDraft, setLevelDraft] = useState<LevelDraft | null>(null);
|
|
26
|
+
const levelDraftRef = useRef(levelDraft);
|
|
27
|
+
levelDraftRef.current = levelDraft;
|
|
28
|
+
const levelDomain = useMemo(() => {
|
|
29
|
+
const series = levels ? panel.series.find((entry) => entry.source.id === levels.seriesId) : undefined;
|
|
30
|
+
return series ? panel.axes[series.source.axis] ?? null : null;
|
|
31
|
+
}, [levels, panel]);
|
|
32
|
+
const projectedLevels = useMemo(
|
|
33
|
+
() => levels && levelDomain ? projectLevels(levels.items, levelDomain, levelDraft) : NO_LEVELS,
|
|
34
|
+
[levelDomain, levelDraft, levels],
|
|
35
|
+
);
|
|
36
|
+
// Over a line a plain drag moves, and while one moves, the pointer says it
|
|
37
|
+
// goes up and down. Where the pointer is, the plot reads (see `PanelPlotSurface`).
|
|
38
|
+
const levelGrab = useMemo<LevelGrab>(() => {
|
|
39
|
+
if (levelDraft) return true;
|
|
40
|
+
if (!levels?.onEdit || onPickTime || onPickPrice || (armedTool && armedTool !== "level")) return false;
|
|
41
|
+
return (cursorYRatio) => {
|
|
42
|
+
const hit = hitTestLevel(projectedLevels, cursorYRatio, levelGrabRatio(panel.height));
|
|
43
|
+
return !!hit?.editable && !!hit.draggable;
|
|
44
|
+
};
|
|
45
|
+
}, [armedTool, levelDraft, levels?.onEdit, onPickPrice, onPickTime, panel.height, projectedLevels]);
|
|
46
|
+
return { levelDraftRef, setLevelDraft, levelDomain, projectedLevels, levelGrab };
|
|
47
|
+
}
|