@ftshare-lab/dsh-kline 0.0.0-stage → 0.6.11
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/CHANGELOG.md +611 -0
- package/LICENSE +21 -0
- package/PROVENANCE.md +13 -0
- package/README.en.md +171 -0
- package/README.md +170 -2
- package/chart_service.py +687 -0
- package/config/basic-symbols.json +570 -0
- package/config/dsh-kline.patch.yml +4 -0
- package/config/dsh-tool-catalog.json +3875 -0
- package/cordis.patch.yml +33 -0
- package/core/__init__.py +0 -0
- package/core/adjustment.py +16 -0
- package/core/calc.py +691 -0
- package/core/rows.py +228 -0
- package/core/ta_engine.py +99 -0
- package/docs/anonymous-market-support.md +33 -0
- package/docs/automatic-drawing.md +24 -0
- package/docs/chart-functions.md +136 -0
- package/docs/drawing-examples.en.md +13 -0
- package/docs/drawing-examples.md +13 -0
- package/docs/embedding.md +77 -0
- package/docs/fixtures/price-pattern-real-v1.json +1 -0
- package/docs/free-foreign-daily.md +44 -0
- package/docs/guide.en.md +232 -0
- package/docs/guide.md +248 -0
- package/docs/images/abc-drawing-v0.5.0.jpg +0 -0
- package/docs/images/channel-drawing-v0.5.0.jpg +0 -0
- package/docs/images/drawing-workbench-v0.5.0.jpg +0 -0
- package/docs/images/dsh-kline-tab-workspace.png +0 -0
- package/docs/images/dsh-market.png +0 -0
- package/docs/images/kline-news.png +0 -0
- package/docs/images/kline-range-stats.png +0 -0
- package/docs/images/kline-support.png +0 -0
- package/docs/images/main-chart-comparison-v0.6.9.png +0 -0
- package/docs/images/market-overview-v0.6.9.png +0 -0
- package/docs/images/natural-language-abc-wolong-2026-10-27.png +0 -0
- package/docs/images/natural-language-descending-channel-wolong-2026-10-27.png +0 -0
- package/docs/images/platform-breakout-msft-v0.6.9.png +0 -0
- package/docs/npm-release.md +33 -0
- package/docs/price-pattern-rules-v1.md +40 -0
- package/docs/price-pattern-samples-v1.md +42 -0
- package/docs/provider-adaptation.md +73 -0
- package/docs/release-acceptance-v0.4.0.md +46 -0
- package/docs/release-acceptance-v0.4.1.md +43 -0
- package/docs/release-acceptance-v0.5.0.md +40 -0
- package/docs/release-acceptance-v0.6.0.md +33 -0
- package/docs/release-acceptance-v0.6.1.md +19 -0
- package/docs/release-acceptance-v0.6.10.md +21 -0
- package/docs/release-acceptance-v0.6.11.md +21 -0
- package/docs/release-acceptance-v0.6.2.md +31 -0
- package/docs/release-acceptance-v0.6.3.md +25 -0
- package/docs/release-acceptance-v0.6.4.md +24 -0
- package/docs/release-acceptance-v0.6.5.md +21 -0
- package/docs/release-acceptance-v0.6.6.md +21 -0
- package/docs/release-acceptance-v0.6.7.md +18 -0
- package/docs/release-acceptance-v0.6.8.md +18 -0
- package/docs/release-acceptance-v0.6.9.md +98 -0
- package/docs/release-notes-v0.5.0.md +23 -0
- package/docs/release-notes-v0.6.0.md +11 -0
- package/docs/release-notes-v0.6.1.md +20 -0
- package/docs/release-notes-v0.6.10.md +11 -0
- package/docs/release-notes-v0.6.11.md +16 -0
- package/docs/release-notes-v0.6.2.md +14 -0
- package/docs/release-notes-v0.6.3.md +26 -0
- package/docs/release-notes-v0.6.4.md +12 -0
- package/docs/release-notes-v0.6.5.md +13 -0
- package/docs/release-notes-v0.6.6.md +13 -0
- package/docs/release-notes-v0.6.7.md +17 -0
- package/docs/release-notes-v0.6.8.md +10 -0
- package/docs/release-notes-v0.6.9.md +16 -0
- package/lib/candle-sequence.js +15 -0
- package/lib/chart-adjustment.js +7 -0
- package/lib/chart-annotations.js +33 -0
- package/lib/chart-applications.js +171 -0
- package/lib/chart-context.js +164 -0
- package/lib/chart-observation.js +251 -0
- package/lib/chart-service-status.js +82 -0
- package/lib/chart-source-status.js +39 -0
- package/lib/chart-sources.js +354 -0
- package/lib/client/BetterSidebarTab.js +70 -0
- package/lib/client/ClassicShell.js +254 -0
- package/lib/client/KlineContent.js +155 -0
- package/lib/client/PanelHost.js +46 -0
- package/lib/client/chart-apply.js +73 -0
- package/lib/client/chart-context.js +233 -0
- package/lib/client/chart-handoff.js +34 -0
- package/lib/client/chart-session-resolver.js +74 -0
- package/lib/client/client-context.js +1 -0
- package/lib/client/conversation.js +88 -0
- package/lib/client/default-tab.js +83 -0
- package/lib/client/embed.js +50 -0
- package/lib/client/generated-view.js +12983 -0
- package/lib/client/horizontal-lines.js +19 -0
- package/lib/client/host-options.js +41 -0
- package/lib/client/host-panels.js +111 -0
- package/lib/client/index.js +77 -0
- package/lib/client/mode-preferences.js +48 -0
- package/lib/client/native-sidebar-contract.js +45 -0
- package/lib/client/native-sidebar-register.js +133 -0
- package/lib/client/native-sidebar-services.js +1 -0
- package/lib/client/native-sidebar.js +98 -0
- package/lib/client/sidebar-integration.js +93 -0
- package/lib/client/ui-onboarding.js +48 -0
- package/lib/client/version.js +14 -0
- package/lib/client.js +17581 -0
- package/lib/client.js.map +1 -0
- package/lib/drawing-feedback.js +284 -0
- package/lib/fibonacci.js +18 -0
- package/lib/ftshare-settings-bridge.js +159 -0
- package/lib/index.js +279 -0
- package/lib/labeled-polyline.js +17 -0
- package/lib/pattern-groups.js +91 -0
- package/lib/price-bands.js +55 -0
- package/lib/price-series.js +47 -0
- package/lib/price-sticks.js +32 -0
- package/lib/types/candle-sequence.d.ts +2 -0
- package/lib/types/chart-adjustment.d.ts +2 -0
- package/lib/types/chart-annotations.d.ts +4 -0
- package/lib/types/chart-applications.d.ts +26 -0
- package/lib/types/chart-context.d.ts +14 -0
- package/lib/types/chart-observation.d.ts +8 -0
- package/lib/types/chart-service-status.d.ts +22 -0
- package/lib/types/chart-source-status.d.ts +47 -0
- package/lib/types/chart-sources.d.ts +25 -0
- package/lib/types/client/BetterSidebarTab.d.ts +12 -0
- package/lib/types/client/ClassicShell.d.ts +4 -0
- package/lib/types/client/KlineContent.d.ts +37 -0
- package/lib/types/client/PanelHost.d.ts +25 -0
- package/lib/types/client/chart-apply.d.ts +13 -0
- package/lib/types/client/chart-context.d.ts +23 -0
- package/lib/types/client/chart-handoff.d.ts +5 -0
- package/lib/types/client/chart-session-resolver.d.ts +2 -0
- package/lib/types/client/client-context.d.ts +16 -0
- package/lib/types/client/conversation.d.ts +43 -0
- package/lib/types/client/default-tab.d.ts +3 -0
- package/lib/types/client/embed.d.ts +37 -0
- package/lib/types/client/generated-view.d.ts +2 -0
- package/lib/types/client/horizontal-lines.d.ts +1 -0
- package/lib/types/client/host-options.d.ts +38 -0
- package/lib/types/client/host-panels.d.ts +55 -0
- package/lib/types/client/index.d.ts +4 -0
- package/lib/types/client/mode-preferences.d.ts +25 -0
- package/lib/types/client/native-sidebar-contract.d.ts +52 -0
- package/lib/types/client/native-sidebar-register.d.ts +46 -0
- package/lib/types/client/native-sidebar-services.d.ts +45 -0
- package/lib/types/client/native-sidebar.d.ts +47 -0
- package/lib/types/client/sidebar-integration.d.ts +12 -0
- package/lib/types/client/ui-onboarding.d.ts +7 -0
- package/lib/types/client/version.d.ts +1 -0
- package/lib/types/drawing-feedback.d.ts +5 -0
- package/lib/types/fibonacci.d.ts +5 -0
- package/lib/types/ftshare-settings-bridge.d.ts +50 -0
- package/lib/types/index.d.ts +26 -0
- package/lib/types/labeled-polyline.d.ts +11 -0
- package/lib/types/pattern-groups.d.ts +2 -0
- package/lib/types/price-bands.d.ts +3 -0
- package/lib/types/price-series.d.ts +5 -0
- package/lib/types/price-sticks.d.ts +3 -0
- package/package.json +155 -4
- package/requirements.txt +6 -0
- package/screenshots.json +8 -0
- package/scripts/bootstrap.sh +5 -0
- package/scripts/dsh-bootstrap.mjs +179 -0
- package/scripts/dsh-mcp-plugin.mjs +27 -0
- package/scripts/run-dsh-kline.mjs +658 -0
- package/scripts/run-dsh-kline.sh +5 -0
- package/server.py +1755 -0
- package/tools/__init__.py +0 -0
- package/tools/annotations.py +119 -0
- package/tools/automatic_analysis.py +158 -0
- package/tools/baostock_source.py +176 -0
- package/tools/calc.py +39 -0
- package/tools/channel_candidates.py +260 -0
- package/tools/channel_preview.py +82 -0
- package/tools/channels.py +104 -0
- package/tools/chart_applications.py +204 -0
- package/tools/chart_context.py +186 -0
- package/tools/chart_edits.py +363 -0
- package/tools/chart_functions.py +451 -0
- package/tools/chart_indicators.py +65 -0
- package/tools/chart_measurements.py +84 -0
- package/tools/chart_source_status.py +32 -0
- package/tools/chart_sources.py +31 -0
- package/tools/closed_daily.py +68 -0
- package/tools/draw.py +410 -0
- package/tools/drawing_feedback.py +45 -0
- package/tools/drawing_interaction.py +117 -0
- package/tools/drawing_verification.py +141 -0
- package/tools/fetch.py +4680 -0
- package/tools/fibonacci.py +83 -0
- package/tools/foreign_daily.py +185 -0
- package/tools/free_sources.py +483 -0
- package/tools/lines.py +91 -0
- package/tools/nine_count.py +66 -0
- package/tools/pattern_groups.py +103 -0
- package/tools/pivots.py +60 -0
- package/tools/polylines.py +131 -0
- package/tools/price_bands.py +165 -0
- package/tools/price_patterns.py +311 -0
- package/tools/price_series.py +162 -0
- package/tools/price_sticks.py +114 -0
- package/tools/range_candidates.py +170 -0
- package/tools/recent_drawing_candidates.py +123 -0
- package/tools/rectangles.py +76 -0
- package/tools/regression_channels.py +87 -0
- package/tools/ta_functions.py +228 -0
- package/tools/trend_lines.py +106 -0
- package/tools/watchlist.py +149 -0
- package/view/ft-logo.jpg +0 -0
- package/view/kline.html +12436 -0
- package/view/vendor/klinecharts.min.js +7 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** Merge a drawing-only result into its owning view, preserving candles and toggles. */
|
|
2
|
+
const AI_DRAWING_COMMANDS = new Set(['DRAW_HORIZONTAL_LINE', 'DRAW_AGENT_TREND_LINE', 'DRAW_AGENT_CHANNEL', 'DRAW_AGENT_RECTANGLE', 'DRAW_AGENT_FIBONACCI', 'DRAW_AGENT_POLYLINE', 'DRAW_AGENT_ANNOTATION']);
|
|
3
|
+
export function mergeHorizontalLineSnapshot(payload, saved) {
|
|
4
|
+
if (payload?.drawing_guard || payload?.view_source_guard || !payload?.drawing_only || saved?.session !== payload.drawing_parent_session
|
|
5
|
+
|| saved?.payload?.symbol !== payload.symbol || saved?.payload?.interval !== payload.interval
|
|
6
|
+
|| !Array.isArray(saved?.payload?.chartCommands))
|
|
7
|
+
return undefined;
|
|
8
|
+
const { _live_chart_session, _live_application_pending, ...savedPayload } = saved.payload;
|
|
9
|
+
return {
|
|
10
|
+
...savedPayload,
|
|
11
|
+
drawing_only: true,
|
|
12
|
+
drawing_parent_session: payload.drawing_parent_session,
|
|
13
|
+
indicators_explicit: false,
|
|
14
|
+
chartCommands: [
|
|
15
|
+
...saved.payload.chartCommands.filter((command) => !AI_DRAWING_COMMANDS.has(command.type)),
|
|
16
|
+
...payload.chartCommands.filter((command) => AI_DRAWING_COMMANDS.has(command.type)),
|
|
17
|
+
],
|
|
18
|
+
};
|
|
19
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The optional host contract for the chrome the chart draws itself.
|
|
3
|
+
*
|
|
4
|
+
* A host client plugin provides the `klineHost` service; nothing else changes
|
|
5
|
+
* when it is absent, which is what keeps every upstream default intact. Only
|
|
6
|
+
* the members a host may switch are modelled here, and unknown values collapse
|
|
7
|
+
* to the default, so a newer host can never put the chart in a half-configured
|
|
8
|
+
* state.
|
|
9
|
+
*
|
|
10
|
+
* The store is deliberately framework-free: the router subscribes with
|
|
11
|
+
* `useSyncExternalStore`, and the onboarding classifier reads it once per open.
|
|
12
|
+
*/
|
|
13
|
+
let current;
|
|
14
|
+
const listeners = new Set();
|
|
15
|
+
/** The options the active host provided, if it provided any. */
|
|
16
|
+
export function readHostOptions() {
|
|
17
|
+
return current;
|
|
18
|
+
}
|
|
19
|
+
/** Subscribe to host option changes; the router repaints when they arrive. */
|
|
20
|
+
export function subscribeHostOptions(listener) {
|
|
21
|
+
listeners.add(listener);
|
|
22
|
+
return () => { listeners.delete(listener); };
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Take the options a host provided.
|
|
26
|
+
*
|
|
27
|
+
* Unknown or malformed values are dropped rather than trusted, so this stays a
|
|
28
|
+
* closed contract: only the switches this build understands ever take effect.
|
|
29
|
+
* @param value - the service value, or undefined when the host withdrew it.
|
|
30
|
+
*/
|
|
31
|
+
export function setHostOptions(value) {
|
|
32
|
+
const raw = (value ?? {});
|
|
33
|
+
const next = {};
|
|
34
|
+
if (raw.entry === 'tab')
|
|
35
|
+
next.entry = 'tab';
|
|
36
|
+
if (raw.onboarding === 'off')
|
|
37
|
+
next.onboarding = 'off';
|
|
38
|
+
current = Object.keys(next).length === 0 ? undefined : next;
|
|
39
|
+
for (const listener of [...listeners])
|
|
40
|
+
listener();
|
|
41
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The optional host contract for panel mounts.
|
|
3
|
+
*
|
|
4
|
+
* A host that already owns a bounded panel declares a slot of its own and asks
|
|
5
|
+
* the chart to fill it through the `klinePanels` service. The chart therefore
|
|
6
|
+
* never learns a host's slot names, and a host that does not ask sees the
|
|
7
|
+
* plugin exactly as it was before this contract existed.
|
|
8
|
+
*
|
|
9
|
+
* Kept free of React, like `native-sidebar-register.ts`, so the registration
|
|
10
|
+
* policy is testable with fake slot services.
|
|
11
|
+
*/
|
|
12
|
+
/** Coordinate caller lifetimes independently of the current slot-service lifetime. */
|
|
13
|
+
export function createPanelMountRegistry() {
|
|
14
|
+
const requests = new Map();
|
|
15
|
+
let current;
|
|
16
|
+
const disconnect = (connection) => {
|
|
17
|
+
if (current === connection)
|
|
18
|
+
current = undefined;
|
|
19
|
+
const disposers = [...connection.mounted.values()];
|
|
20
|
+
connection.mounted.clear();
|
|
21
|
+
const errors = [];
|
|
22
|
+
for (const dispose of disposers) {
|
|
23
|
+
try {
|
|
24
|
+
dispose();
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
errors.push(error);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (errors.length)
|
|
31
|
+
throw new AggregateError(errors, 'Could not release host chart panels');
|
|
32
|
+
};
|
|
33
|
+
return {
|
|
34
|
+
mount(seat, options) {
|
|
35
|
+
const chrome = options?.chrome ?? 'chart-search';
|
|
36
|
+
let request = requests.get(seat);
|
|
37
|
+
if (request && request.chrome !== chrome)
|
|
38
|
+
throw new Error(`Chart panel "${seat}" is already mounted with different chrome`);
|
|
39
|
+
if (!request) {
|
|
40
|
+
request = { chrome, users: 0 };
|
|
41
|
+
requests.set(seat, request);
|
|
42
|
+
try {
|
|
43
|
+
if (current)
|
|
44
|
+
current.mounted.set(seat, current.service.mount(seat, { chrome }));
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
requests.delete(seat);
|
|
48
|
+
throw error;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
const lease = request;
|
|
52
|
+
lease.users += 1;
|
|
53
|
+
let released = false;
|
|
54
|
+
return () => {
|
|
55
|
+
if (released)
|
|
56
|
+
return;
|
|
57
|
+
released = true;
|
|
58
|
+
if (--lease.users > 0 || requests.get(seat) !== lease)
|
|
59
|
+
return;
|
|
60
|
+
requests.delete(seat);
|
|
61
|
+
const dispose = current?.mounted.get(seat);
|
|
62
|
+
current?.mounted.delete(seat);
|
|
63
|
+
dispose?.();
|
|
64
|
+
};
|
|
65
|
+
},
|
|
66
|
+
connect(service) {
|
|
67
|
+
if (current)
|
|
68
|
+
disconnect(current);
|
|
69
|
+
const connection = { service, mounted: new Map() };
|
|
70
|
+
current = connection;
|
|
71
|
+
try {
|
|
72
|
+
for (const [seat, request] of requests) {
|
|
73
|
+
connection.mounted.set(seat, service.mount(seat, { chrome: request.chrome }));
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
catch (error) {
|
|
77
|
+
try {
|
|
78
|
+
disconnect(connection);
|
|
79
|
+
}
|
|
80
|
+
catch (cleanupError) {
|
|
81
|
+
throw new AggregateError([error, cleanupError], 'Could not attach host chart panels');
|
|
82
|
+
}
|
|
83
|
+
throw error;
|
|
84
|
+
}
|
|
85
|
+
return () => disconnect(connection);
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Turn slot services plus a body factory into the `klinePanels` service.
|
|
91
|
+
* @param host - the host services a mount registers into.
|
|
92
|
+
* @param body - the component body for one seat (one per mounted seat).
|
|
93
|
+
* @returns the service a host calls; nothing is registered until it calls.
|
|
94
|
+
*/
|
|
95
|
+
export function createPanelMounts(host, body) {
|
|
96
|
+
return {
|
|
97
|
+
mount(seat, options) {
|
|
98
|
+
// `slots.inject` returns the factory's disposer when the seat is already
|
|
99
|
+
// declared, and the injection's own disposer otherwise — the same shape
|
|
100
|
+
// the native tab registration relies on.
|
|
101
|
+
const dispose = host.slots.inject(seat, () => host.slots.register({ name: seat }, body(seat, options?.chrome)));
|
|
102
|
+
let released = false;
|
|
103
|
+
return () => {
|
|
104
|
+
if (released)
|
|
105
|
+
return;
|
|
106
|
+
released = true;
|
|
107
|
+
dispose?.();
|
|
108
|
+
};
|
|
109
|
+
},
|
|
110
|
+
};
|
|
111
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useSyncExternalStore } from 'react';
|
|
3
|
+
import { createRoot } from 'react-dom/client';
|
|
4
|
+
import { currentConversationId } from './conversation';
|
|
5
|
+
import { ClassicShell } from './ClassicShell';
|
|
6
|
+
import { BetterSidebarBridge } from './BetterSidebarTab';
|
|
7
|
+
import { NativeSidebarBridgeView, registerNativeSidebar } from './native-sidebar';
|
|
8
|
+
import { cleanClassicTabs, isSidebarUsable } from './sidebar-integration';
|
|
9
|
+
import { InterfaceContext, readDisplayMode, resolveDisplayMode, shouldDrawClassicLauncher } from './mode-preferences';
|
|
10
|
+
import { captureOnboarding } from './ui-onboarding';
|
|
11
|
+
import { readHostOptions, setHostOptions, subscribeHostOptions } from "./host-options.js";
|
|
12
|
+
import { registerPanelHosts } from './PanelHost';
|
|
13
|
+
export const inject = ['sessions'];
|
|
14
|
+
export function apply(ctx) {
|
|
15
|
+
let service;
|
|
16
|
+
const listeners = new Set();
|
|
17
|
+
const emit = () => listeners.forEach(listener => listener());
|
|
18
|
+
const subscribe = (listener) => { listeners.add(listener); return () => { listeners.delete(listener); }; };
|
|
19
|
+
const subscribeSessions = (listener) => ctx.sessions.list.subscribe(listener);
|
|
20
|
+
const preference = readDisplayMode();
|
|
21
|
+
captureOnboarding();
|
|
22
|
+
// Registered before the router paints, so the first frame already knows
|
|
23
|
+
// whether the host owns a right column this plugin can put a tab in.
|
|
24
|
+
const native = registerNativeSidebar(ctx, emit);
|
|
25
|
+
// A host client plugin can provide `klineHost` to switch the chrome it wants
|
|
26
|
+
// (its own chart entry, its own first-run experience). Without that service
|
|
27
|
+
// every default below stays exactly as upstream.
|
|
28
|
+
ctx.inject(['klineHost'], scoped => {
|
|
29
|
+
scoped.effect(() => {
|
|
30
|
+
setHostOptions(scoped.get('klineHost'));
|
|
31
|
+
return () => setHostOptions(undefined);
|
|
32
|
+
}, 'dsh-kline: host options');
|
|
33
|
+
});
|
|
34
|
+
function Router() {
|
|
35
|
+
const sidebar = useSyncExternalStore(subscribe, () => service);
|
|
36
|
+
const nativeTab = useSyncExternalStore(subscribe, () => native.available());
|
|
37
|
+
const host = useSyncExternalStore(subscribeHostOptions, readHostOptions);
|
|
38
|
+
const conversation = useSyncExternalStore(subscribeSessions, () => currentConversationId(ctx.sessions.list.getSnapshot()));
|
|
39
|
+
const available = nativeTab || isSidebarUsable(sidebar);
|
|
40
|
+
const mode = resolveDisplayMode(preference, available, host);
|
|
41
|
+
// A tab body lives in a Session, so before one exists the chart has no
|
|
42
|
+
// tab to live in — whichever column the host owns. The classic launcher
|
|
43
|
+
// covers that screen, which is what the old aria-label probe approximated
|
|
44
|
+
// and only ever got right in an English UI.
|
|
45
|
+
const drawClassicLauncher = shouldDrawClassicLauncher(host, mode, conversation !== undefined);
|
|
46
|
+
return _jsxs(InterfaceContext.Provider, { value: { preference, mode, available }, children: [nativeTab && mode === 'better-sidebar' && _jsx(NativeSidebarBridgeView, { bridge: native, sessions: ctx.sessions }), !nativeTab && sidebar && _jsx(BetterSidebarBridge, { service: sidebar, sessions: ctx.sessions }), nativeTab && sidebar && _jsx(RetireBetterSidebarTabs, { service: sidebar }), drawClassicLauncher && _jsx(ClassicShell, { sessions: ctx.sessions })] });
|
|
47
|
+
}
|
|
48
|
+
ctx.effect(() => {
|
|
49
|
+
const host = document.createElement('div');
|
|
50
|
+
host.dataset.dshKlineRouter = '';
|
|
51
|
+
document.body.appendChild(host);
|
|
52
|
+
const root = createRoot(host);
|
|
53
|
+
root.render(_jsx(Router, {}));
|
|
54
|
+
return () => { root.unmount(); host.remove(); listeners.clear(); };
|
|
55
|
+
}, 'dsh-kline: interface router');
|
|
56
|
+
// A separate dependency scope does not block the parent when absent.
|
|
57
|
+
ctx.inject(['betterSidebar'], scoped => {
|
|
58
|
+
scoped.effect(() => {
|
|
59
|
+
service = scoped.get('betterSidebar');
|
|
60
|
+
emit();
|
|
61
|
+
return () => { service = undefined; emit(); };
|
|
62
|
+
}, 'dsh-kline: optional sidebar');
|
|
63
|
+
});
|
|
64
|
+
// A host that owns bounded panels (a paper-trading rail, a canvas card) asks
|
|
65
|
+
// the chart to fill its own slot; hosts that never ask keep the plugin as is.
|
|
66
|
+
registerPanelHosts(ctx);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Remove the tabs this plugin saved into Better Sidebar's workbench once the
|
|
70
|
+
* native tab takes the same kind over. Better Sidebar draws a saved tab of an
|
|
71
|
+
* unregistered type as "plugin not loaded", so an upgrade would otherwise
|
|
72
|
+
* leave a broken-looking tab behind.
|
|
73
|
+
*/
|
|
74
|
+
function RetireBetterSidebarTabs({ service }) {
|
|
75
|
+
useEffect(() => cleanClassicTabs(service), [service]);
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { createContext } from 'react';
|
|
2
|
+
export const DISPLAY_MODE_KEY = 'dsh-kline:display-mode:v1';
|
|
3
|
+
export const AUTO_OPEN_KEY = 'dsh-kline:auto-open-after-analysis:v1';
|
|
4
|
+
export const UI_SEEN_KEY = 'dsh-kline:ui-change-seen:v0.2.0';
|
|
5
|
+
export const InterfaceContext = createContext({ preference: 'classic', mode: 'classic', available: false });
|
|
6
|
+
export function readDisplayMode(storage = globalThis.localStorage) {
|
|
7
|
+
try {
|
|
8
|
+
const value = storage.getItem(DISPLAY_MODE_KEY);
|
|
9
|
+
return value === 'better-sidebar' ? value : 'classic';
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
return 'classic';
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export function resolveDisplayMode(preference, available, host) {
|
|
16
|
+
// A host that owns the chart entry uses its column whenever it offers one,
|
|
17
|
+
// whatever the user's stored preference says: there is no floating launcher
|
|
18
|
+
// left to fall back to. Omitted host options keep the upstream rule below.
|
|
19
|
+
if (host?.entry === 'tab')
|
|
20
|
+
return available ? 'better-sidebar' : 'classic';
|
|
21
|
+
// A chart should be available before a new DSH conversation has created a
|
|
22
|
+
// workbench session. The native tab is therefore opt-in; the classic K
|
|
23
|
+
// launcher remains the dependable default.
|
|
24
|
+
return preference === 'better-sidebar' && available ? 'better-sidebar' : 'classic';
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Whether the floating classic launcher is drawn for this frame.
|
|
28
|
+
*
|
|
29
|
+
* Upstream keeps it for classic mode and for screens without a Session, where
|
|
30
|
+
* the tab body cannot live yet. A host that declared `entry: 'tab'` owns the
|
|
31
|
+
* chart entry instead, so it never draws the launcher.
|
|
32
|
+
* @param host - the host options, if the host provided any.
|
|
33
|
+
* @param mode - the interface mode this frame resolved to.
|
|
34
|
+
* @param hasConversation - whether a Session exists to host a tab body.
|
|
35
|
+
*/
|
|
36
|
+
export function shouldDrawClassicLauncher(host, mode, hasConversation) {
|
|
37
|
+
if (host?.entry === 'tab')
|
|
38
|
+
return false;
|
|
39
|
+
return mode === 'classic' || !hasConversation;
|
|
40
|
+
}
|
|
41
|
+
export function readAutoOpen(storage = globalThis.localStorage) {
|
|
42
|
+
try {
|
|
43
|
+
return storage.getItem(AUTO_OPEN_KEY) !== 'false';
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { KLINE_TAB_ID, klineTabTitle } from "./sidebar-integration.js";
|
|
2
|
+
/**
|
|
3
|
+
* The static half of the native right-Sidebar integration.
|
|
4
|
+
*
|
|
5
|
+
* Kept free of JSX so the tests can import it directly: the registration itself
|
|
6
|
+
* needs a host context, but what this plugin contributes — the tab kind, the
|
|
7
|
+
* band it registers in, the chip title and the guide entry — is plain data.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* Implementation identity of the tab type; also the key its body registers
|
|
11
|
+
* under. The host only requires it to be unique across every registration, and
|
|
12
|
+
* a package name is the natural value for a type shipped from another package.
|
|
13
|
+
*/
|
|
14
|
+
export const NATIVE_TAB_ID = '@ftshare-lab/dsh-kline';
|
|
15
|
+
/**
|
|
16
|
+
* The type definition this plugin contributes.
|
|
17
|
+
*
|
|
18
|
+
* A page type (no address `patterns`) is opened by kind, which is what the
|
|
19
|
+
* auto-open path does; the guide entry is how a person reaches the chart from
|
|
20
|
+
* the host's own add-tab page. Both keep the product label separate from the
|
|
21
|
+
* internal kind, exactly like the Better Sidebar title does.
|
|
22
|
+
*
|
|
23
|
+
* `keepMounted` matters for the chart: collapsing the right column or
|
|
24
|
+
* switching tabs must not tear the chart down, because the view keeps its
|
|
25
|
+
* own state (last instrument reference, zoom, drawings) that a remount loses.
|
|
26
|
+
*/
|
|
27
|
+
export function nativeTabDefinition() {
|
|
28
|
+
return {
|
|
29
|
+
id: NATIVE_TAB_ID,
|
|
30
|
+
kind: KLINE_TAB_ID,
|
|
31
|
+
keepMounted: true,
|
|
32
|
+
priority: 'extension',
|
|
33
|
+
title: () => klineTabTitle(),
|
|
34
|
+
guide: [{
|
|
35
|
+
id: NATIVE_TAB_ID,
|
|
36
|
+
order: 120,
|
|
37
|
+
title: () => klineTabTitle(),
|
|
38
|
+
description: nativeTabDescription,
|
|
39
|
+
}],
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/** One line under the guide capsule: what picking it opens, in the host language. */
|
|
43
|
+
export function nativeTabDescription(locale = globalThis.navigator?.language ?? '') {
|
|
44
|
+
return /^zh(?:-|$)/i.test(locale) ? '行情图表与关键点位分析' : 'Market chart and key-level analysis';
|
|
45
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { NATIVE_TAB_ID, nativeTabDefinition } from "./native-sidebar-contract.js";
|
|
2
|
+
import { KLINE_TAB_ID } from "./sidebar-integration.js";
|
|
3
|
+
/**
|
|
4
|
+
* The native right-Sidebar registration, without React and without a host
|
|
5
|
+
* context: a pure function over the three host services, so the bootstrap
|
|
6
|
+
* policy (both stages, their teardown order, the deferred body seat) is
|
|
7
|
+
* testable with fakes.
|
|
8
|
+
*
|
|
9
|
+
* A DSH host that owns its right column publishes `sidebarRightTabs` (stage
|
|
10
|
+
* one — what the tab type is) and a keyed `sidebar.right.pane.tab` seat
|
|
11
|
+
* (stage two — the body), navigated by `sidebarRight`. A type without a body
|
|
12
|
+
* makes the host draw "nothing can view this", so the two stages are one unit
|
|
13
|
+
* and any failure backs the type out again.
|
|
14
|
+
*/
|
|
15
|
+
/** The seat a tab type's body registers under. */
|
|
16
|
+
export const BODY_SEAT = 'sidebar.right.pane.tab';
|
|
17
|
+
/**
|
|
18
|
+
* Register the chart as a tab type of the host's right column.
|
|
19
|
+
* @param host - the resolved host services.
|
|
20
|
+
* @param options - the body, its props, and the availability callback.
|
|
21
|
+
* @returns the registration, live until `dispose()`.
|
|
22
|
+
*/
|
|
23
|
+
export function registerNativeTab(host, options) {
|
|
24
|
+
const onChange = options.onChange ?? (() => { });
|
|
25
|
+
let typeDispose;
|
|
26
|
+
let seatDispose;
|
|
27
|
+
let bodyDispose;
|
|
28
|
+
let available = false;
|
|
29
|
+
let disposed = false;
|
|
30
|
+
/** One teardown step: a failing disposer must not block the rest of the rollback. */
|
|
31
|
+
const runSafely = (dispose, what) => {
|
|
32
|
+
if (dispose === undefined)
|
|
33
|
+
return;
|
|
34
|
+
try {
|
|
35
|
+
dispose();
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
console.error(`[dsh-kline] native tab ${what} teardown failed:`, error);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
const settle = (next) => {
|
|
42
|
+
if (available === next)
|
|
43
|
+
return;
|
|
44
|
+
available = next;
|
|
45
|
+
try {
|
|
46
|
+
onChange();
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
console.error('[dsh-kline] native tab change handler failed:', error);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
const closeBody = () => {
|
|
53
|
+
const dispose = bodyDispose;
|
|
54
|
+
bodyDispose = undefined;
|
|
55
|
+
if (dispose !== undefined)
|
|
56
|
+
runSafely(dispose, 'body');
|
|
57
|
+
};
|
|
58
|
+
const release = () => {
|
|
59
|
+
if (disposed)
|
|
60
|
+
return;
|
|
61
|
+
disposed = true;
|
|
62
|
+
// The body goes first, so the type never outlives the thing that draws it.
|
|
63
|
+
closeBody();
|
|
64
|
+
runSafely(seatDispose, 'body seat');
|
|
65
|
+
seatDispose = undefined;
|
|
66
|
+
runSafely(typeDispose, 'type');
|
|
67
|
+
typeDispose = undefined;
|
|
68
|
+
settle(false);
|
|
69
|
+
};
|
|
70
|
+
const failed = () => ({ available: () => false, open: () => { }, dispose: () => { } });
|
|
71
|
+
try {
|
|
72
|
+
typeDispose = host.tabs.register(nativeTabDefinition());
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
console.error('[dsh-kline] native right sidebar tab type registration failed:', error);
|
|
76
|
+
return failed();
|
|
77
|
+
}
|
|
78
|
+
// Stage two may settle after this call: a host declares the seat when its
|
|
79
|
+
// sidebar mounts, which can be later than the plugin boot. A failure inside
|
|
80
|
+
// the factory therefore has to roll the type back itself, once the
|
|
81
|
+
// injection attempt has settled (calling our own seat disposer from inside
|
|
82
|
+
// its factory would re-enter the registry).
|
|
83
|
+
const mountBody = () => {
|
|
84
|
+
if (disposed)
|
|
85
|
+
return () => { };
|
|
86
|
+
let dispose;
|
|
87
|
+
try {
|
|
88
|
+
dispose = host.slots.register({
|
|
89
|
+
name: BODY_SEAT,
|
|
90
|
+
key: NATIVE_TAB_ID,
|
|
91
|
+
...options.bodyProps === undefined ? {} : { inject: options.bodyProps },
|
|
92
|
+
}, options.body);
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
console.error('[dsh-kline] native right sidebar tab body registration failed:', error);
|
|
96
|
+
queueMicrotask(release);
|
|
97
|
+
return () => { };
|
|
98
|
+
}
|
|
99
|
+
let closed = false;
|
|
100
|
+
bodyDispose = () => {
|
|
101
|
+
if (closed)
|
|
102
|
+
return;
|
|
103
|
+
closed = true;
|
|
104
|
+
runSafely(dispose, 'body');
|
|
105
|
+
};
|
|
106
|
+
settle(true);
|
|
107
|
+
return () => { closeBody(); settle(false); };
|
|
108
|
+
};
|
|
109
|
+
try {
|
|
110
|
+
seatDispose = host.slots.inject(BODY_SEAT, mountBody);
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
console.error('[dsh-kline] native right sidebar body seat failed:', error);
|
|
114
|
+
release();
|
|
115
|
+
return failed();
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
available: () => available && !disposed,
|
|
119
|
+
open: () => {
|
|
120
|
+
if (!available || disposed)
|
|
121
|
+
return;
|
|
122
|
+
try {
|
|
123
|
+
host.navigation.openTab(KLINE_TAB_ID);
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
// Opening is a navigation request: a host with no Session on screen
|
|
127
|
+
// refuses it, and the next analysis result or the guide entry retries.
|
|
128
|
+
console.warn('[dsh-kline] could not open the native chart tab:', error);
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
dispose: release,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useCallback, useMemo, useSyncExternalStore } from 'react';
|
|
3
|
+
import { useChartReference } from './chart-session-resolver';
|
|
4
|
+
import { KlineTabContent } from './BetterSidebarTab';
|
|
5
|
+
import { currentConversationId } from './conversation';
|
|
6
|
+
import { InterfaceContext, readAutoOpen, readDisplayMode, resolveDisplayMode } from './mode-preferences';
|
|
7
|
+
import { readHostOptions, subscribeHostOptions } from "./host-options.js";
|
|
8
|
+
import { registerNativeTab } from './native-sidebar-register';
|
|
9
|
+
/**
|
|
10
|
+
* The chart as a tab of the host's own right column.
|
|
11
|
+
*
|
|
12
|
+
* The registration policy lives in `native-sidebar-register.ts`; this module
|
|
13
|
+
* owns the two things that need React or the client context: the tab body and
|
|
14
|
+
* the auto-open trigger. The body is the same chart the Better Sidebar tab
|
|
15
|
+
* draws, mounted against the tab's own conversation.
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* One tab body: the chart of the tab's session.
|
|
19
|
+
*
|
|
20
|
+
* A registered tab can still be opened while the user chose Classic mode, or
|
|
21
|
+
* remain mounted while the host changes Sessions. Only the active conversation
|
|
22
|
+
* in Tab mode may publish a chart; otherwise show a harmless explanation.
|
|
23
|
+
* When that conversation owns the tab, visibility is passed through so a
|
|
24
|
+
* collapsed column does not discard the chart's viewport.
|
|
25
|
+
*/
|
|
26
|
+
export function NativeKlineTabBody({ sessionId, sessions, useTabInfo }) {
|
|
27
|
+
const visible = useTabInfo().tab.visible;
|
|
28
|
+
const preference = useMemo(readDisplayMode, []);
|
|
29
|
+
const host = useSyncExternalStore(subscribeHostOptions, readHostOptions);
|
|
30
|
+
const mode = resolveDisplayMode(preference, true, host);
|
|
31
|
+
const subscribe = useCallback((listener) => sessions.list.subscribe(listener), [sessions]);
|
|
32
|
+
const activeConversation = useSyncExternalStore(subscribe, () => currentConversationId(sessions.list.getSnapshot()));
|
|
33
|
+
return (_jsx(InterfaceContext.Provider, { value: { preference, mode, available: true }, children: _jsx("div", { "data-dsh-kline-tab": "", style: { width: '100%', height: '100%', minHeight: 0, overflow: 'hidden' }, children: mode !== 'better-sidebar'
|
|
34
|
+
? _jsx("div", { role: "status", style: { padding: 16 }, children: "\u5F53\u524D\u4F7F\u7528\u7ECF\u5178 K \u7EBF\u4FA7\u680F\uFF1B\u8BF7\u5728\u56FE\u8868\u53F3\u4E0A\u89D2\u300CUI\u300D\u5207\u6362\u4E3A Tab \u6A21\u5F0F\u3002 / Switch to Tab mode in the chart UI." })
|
|
35
|
+
: activeConversation !== sessionId
|
|
36
|
+
? _jsx("div", { role: "status", style: { padding: 16 }, children: "\u8BF7\u5148\u6253\u5F00\u6B64 Tab \u6240\u5C5E\u7684\u5BF9\u8BDD\u3002 / Open the conversation that owns this tab." })
|
|
37
|
+
: _jsx(KlineTabContent, { sessions: sessions, conversationId: sessionId, visible: visible }, sessionId) }) }));
|
|
38
|
+
}
|
|
39
|
+
/** Reveal the chart tab when an analysis result lands in the conversation on screen. */
|
|
40
|
+
function NativeAutoOpen({ sessions, conversationId, bridge }) {
|
|
41
|
+
const onNewChart = useCallback(() => {
|
|
42
|
+
if (readAutoOpen())
|
|
43
|
+
bridge.open();
|
|
44
|
+
}, [bridge]);
|
|
45
|
+
useChartReference(sessions, conversationId, onNewChart);
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Auto-open lives in the tab's own session, exactly like the Better Sidebar
|
|
50
|
+
* path; the router decides whether this path is the active interface.
|
|
51
|
+
*/
|
|
52
|
+
export function NativeSidebarBridgeView({ bridge, sessions }) {
|
|
53
|
+
const subscribe = useCallback((listener) => sessions.list.subscribe(listener), [sessions]);
|
|
54
|
+
const list = useSyncExternalStore(subscribe, () => sessions.list.getSnapshot());
|
|
55
|
+
const activeConversation = currentConversationId(list);
|
|
56
|
+
return activeConversation
|
|
57
|
+
? _jsx(NativeAutoOpen, { bridge: bridge, sessions: sessions, conversationId: activeConversation }, activeConversation)
|
|
58
|
+
: null;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Register the chart tab into the host's native right Sidebar.
|
|
62
|
+
*
|
|
63
|
+
* Every host service is optional: a host without `sidebarRightTabs`,
|
|
64
|
+
* `sidebarRight` or `slots` keeps `available() === false` and the plugin's
|
|
65
|
+
* existing interfaces untouched.
|
|
66
|
+
* @param ctx - the client context to register on.
|
|
67
|
+
* @param onChange - called whenever availability changes, so the router repaints.
|
|
68
|
+
* @returns the handle the router and the auto-open path share.
|
|
69
|
+
*/
|
|
70
|
+
export function registerNativeSidebar(ctx, onChange) {
|
|
71
|
+
let registration;
|
|
72
|
+
const bridge = {
|
|
73
|
+
available: () => registration?.available() ?? false,
|
|
74
|
+
open: () => registration?.open(),
|
|
75
|
+
};
|
|
76
|
+
ctx.inject(['sidebarRightTabs', 'sidebarRight', 'slots'], scope => {
|
|
77
|
+
scope.effect(() => {
|
|
78
|
+
const tabs = scope.get('sidebarRightTabs');
|
|
79
|
+
const navigation = scope.get('sidebarRight');
|
|
80
|
+
const slots = scope.get('slots');
|
|
81
|
+
if (tabs === undefined || navigation === undefined || slots === undefined)
|
|
82
|
+
return () => { };
|
|
83
|
+
const next = registerNativeTab({ tabs, navigation, slots }, {
|
|
84
|
+
body: NativeKlineTabBody,
|
|
85
|
+
bodyProps: (sessionId) => ({ sessions: ctx.sessions, sessionId }),
|
|
86
|
+
onChange,
|
|
87
|
+
});
|
|
88
|
+
registration = next;
|
|
89
|
+
onChange();
|
|
90
|
+
return () => {
|
|
91
|
+
registration = undefined;
|
|
92
|
+
next.dispose();
|
|
93
|
+
onChange();
|
|
94
|
+
};
|
|
95
|
+
}, 'dsh-kline: native right sidebar tab');
|
|
96
|
+
});
|
|
97
|
+
return bridge;
|
|
98
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
export const KLINE_TAB_ID = 'ftshare-kline:chart';
|
|
2
|
+
export const KLINE_TAB_TITLE_ZH = '非凸 K 线助手';
|
|
3
|
+
export const KLINE_TAB_TITLE_EN = 'FtAI K-Line';
|
|
4
|
+
// The tab belongs to the host interface. Keep its product label separate from
|
|
5
|
+
// the internal package id, and follow the host/browser language.
|
|
6
|
+
export function klineTabTitle(locale = globalThis.navigator?.language ?? '') {
|
|
7
|
+
return /^zh(?:-|$)/i.test(locale) ? KLINE_TAB_TITLE_ZH : KLINE_TAB_TITLE_EN;
|
|
8
|
+
}
|
|
9
|
+
export function migrateKlineTabTitle(service, tab, sessionId) {
|
|
10
|
+
if (tab.type !== KLINE_TAB_ID || !['K线分析 / K-line', 'K线分析', '非凸K线助手 / dsh_kline', KLINE_TAB_TITLE_ZH, KLINE_TAB_TITLE_EN].includes(tab.title ?? '')
|
|
11
|
+
|| !service.features?.includes('updateTab') || !service.features?.includes('stateSubscription')
|
|
12
|
+
|| typeof service.updateTab !== 'function' || typeof service.getSnapshot !== 'function')
|
|
13
|
+
return;
|
|
14
|
+
try {
|
|
15
|
+
// updateTab is current-session-only in the public API. Never mutate a
|
|
16
|
+
// foreign floating tab or a user-customized title while migrating labels.
|
|
17
|
+
if (sessionId && service.getSnapshot()?.sessionId === sessionId)
|
|
18
|
+
service.updateTab(tab.id, { title: klineTabTitle() });
|
|
19
|
+
}
|
|
20
|
+
catch { /* Older/disposed integrations keep their saved title harmlessly. */ }
|
|
21
|
+
}
|
|
22
|
+
export function isSidebarUsable(service) {
|
|
23
|
+
// Some DSH Desktop builds expose the stable public methods through a
|
|
24
|
+
// compatibility proxy but omit (or stale-cache) Better Sidebar's optional
|
|
25
|
+
// feature list. The tab itself can still be registered and opened, so use
|
|
26
|
+
// the callable public contract as the availability test. Optional helpers
|
|
27
|
+
// below remain individually feature-gated.
|
|
28
|
+
return Boolean(service && typeof service.registerTab === 'function' && typeof service.openTab === 'function');
|
|
29
|
+
}
|
|
30
|
+
// The framework persists open tabs across reloads. In Classic mode remove only
|
|
31
|
+
// our old tab shells, otherwise it displays a misleading "plugin unavailable"
|
|
32
|
+
// placeholder. Chart/workspace storage is separate and is never deleted here.
|
|
33
|
+
export function cleanClassicTabs(service) {
|
|
34
|
+
if (!service.features?.includes('stateSubscription') || typeof service.closeTab !== 'function'
|
|
35
|
+
|| typeof service.getSnapshot !== 'function' || typeof service.subscribeState !== 'function')
|
|
36
|
+
return () => { };
|
|
37
|
+
let stopped = false, pending = false;
|
|
38
|
+
const schedule = () => {
|
|
39
|
+
if (stopped || pending)
|
|
40
|
+
return;
|
|
41
|
+
pending = true;
|
|
42
|
+
queueMicrotask(() => {
|
|
43
|
+
pending = false;
|
|
44
|
+
if (stopped)
|
|
45
|
+
return;
|
|
46
|
+
try {
|
|
47
|
+
const snapshot = service.getSnapshot();
|
|
48
|
+
const { sessionId, state } = snapshot ?? {};
|
|
49
|
+
if (typeof sessionId !== 'string' || !sessionId || !state)
|
|
50
|
+
return;
|
|
51
|
+
const ids = new Set();
|
|
52
|
+
const record = (value) => typeof value === 'object' && value !== null;
|
|
53
|
+
const collect = (tab) => {
|
|
54
|
+
if (record(tab) && tab.type === KLINE_TAB_ID && typeof tab.id === 'string' && tab.id)
|
|
55
|
+
ids.add(tab.id);
|
|
56
|
+
};
|
|
57
|
+
// Layouts belong to another plugin and may contain older/incomplete
|
|
58
|
+
// nodes. Iterate defensively, including protection against cycles.
|
|
59
|
+
// Since Better Sidebar 0.19, DSH owns the right-sidebar layout.
|
|
60
|
+
// Our registered tab can only be in Better Sidebar's workbench.
|
|
61
|
+
const pendingNodes = [state.bottomSplits];
|
|
62
|
+
const seen = new Set();
|
|
63
|
+
while (pendingNodes.length) {
|
|
64
|
+
const node = pendingNodes.pop();
|
|
65
|
+
if (!record(node) || seen.has(node))
|
|
66
|
+
continue;
|
|
67
|
+
seen.add(node);
|
|
68
|
+
if (node.kind === 'split' && Array.isArray(node.children))
|
|
69
|
+
pendingNodes.push(...[...node.children].reverse());
|
|
70
|
+
else if (Array.isArray(node.tabs))
|
|
71
|
+
node.tabs.forEach(collect);
|
|
72
|
+
}
|
|
73
|
+
for (const id of ids) {
|
|
74
|
+
try {
|
|
75
|
+
service.closeTab(id, { sessionId });
|
|
76
|
+
}
|
|
77
|
+
catch { /* A stale tab must not block the remaining cleanup. */ }
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
catch { /* Optional layout cleanup must never break the classic shell. */ }
|
|
81
|
+
});
|
|
82
|
+
};
|
|
83
|
+
let unsubscribe;
|
|
84
|
+
try {
|
|
85
|
+
unsubscribe = service.subscribeState(schedule);
|
|
86
|
+
}
|
|
87
|
+
catch { /* Still attempt one initial cleanup. */ }
|
|
88
|
+
schedule();
|
|
89
|
+
return () => { stopped = true; try {
|
|
90
|
+
unsubscribe?.();
|
|
91
|
+
}
|
|
92
|
+
catch { /* Service may already have been disposed. */ } };
|
|
93
|
+
}
|