From 4e960ece6d5e88e394137796f044c9342e12b39a Mon Sep 17 00:00:00 2001 From: Maksim Kostromin Date: Thu, 3 Dec 2026 11:11:16 -0500 Subject: [PATCH] feat(viewer): chart window keeps its charts' zoom between opens, refresh gives the standard view --- src/viewer/app/tv-chart.test.ts | 2 ++ src/viewer/app/tv-chart.ts | 13 +++++---- src/viewer/ui/Fundamentals.tsx | 17 ++++++++--- src/viewer/ui/Zoom.test.tsx | 51 +++++++++++++++++++++++++++++++++ src/viewer/ui/Zoom.tsx | 24 ++++++++++++---- 5 files changed, 93 insertions(+), 14 deletions(-) create mode 100644 src/viewer/ui/Zoom.test.tsx diff --git a/src/viewer/app/tv-chart.test.ts b/src/viewer/app/tv-chart.test.ts index aea5839..879226b 100644 --- a/src/viewer/app/tv-chart.test.ts +++ b/src/viewer/app/tv-chart.test.ts @@ -38,6 +38,8 @@ test('the widget follows the interval, theme and language, and never lets the sy expect(c.hide_volume).toBe(true); // no range: TradingView's own view of the interval, the last candle near the price scale (a range left ~10 empty candles) expect('range' in c).toBe(false); + // no zoom kept by TradingView across charts: every draw and ↻ is the standard view + expect(c.disabled_features).toEqual(['use_localstorage_for_settings']); expect(tvConfig('AAPL', 'D', false, 'en', 'tv-2', '#368727').theme).toBe('light'); }); diff --git a/src/viewer/app/tv-chart.ts b/src/viewer/app/tv-chart.ts index c11dfcf..45b84b8 100644 --- a/src/viewer/app/tv-chart.ts +++ b/src/viewer/app/tv-chart.ts @@ -64,11 +64,13 @@ export function candleOverrides(c: CandleColors): Record -import { useEffect, useRef, useState } from 'react'; +import { useEffect, useId, useRef, useState } from 'react'; import type { Position } from '../../shared/model'; import { fundamentalsFrame, fundamentalsLater, fundamentalsTicker, fundamentalsUrl, reopens } from '../app/fundamentals'; import { lang, t } from '../app/i18n'; import { prefs, setPrefs } from '../app/prefs'; +import { createStore } from '../../shared/store'; import { shownPicked } from '../app/multi'; import { asSplit, paneDir } from '../app/split'; import { CHART_HEAD_STEPS, chartHeadLook, fitStep } from '../app/head-fit'; @@ -37,6 +38,10 @@ export const hasFundamentals = (p: Position): boolean => !!fundamentalsFrame(p, // the words of the intervals, said in the app's language when drawn const WORDS: Record string> = { D: () => t('Daily'), W: () => t('Weekly'), M: () => t('Monthly') }; +// the one window kept drawn while closed (ui/Zoom.tsx keep), the last one opened: its charts keep their zoom and position +// for the next open until ↻; opening another symbol's window lets the old one go, so only one set of frames stays alive +const keptWindow = createStore(''); + // set once a link reopened the window kept from before the reload, so the other links of that symbol do not open theirs let reopened = false; @@ -48,10 +53,13 @@ let reopened = false; export function FundamentalsLink({ p, className, children }: { p: Position; className: string; children: React.ReactNode }) { const [open, setOpenState] = useState(false); // the open window is kept in the prefs, so a reload opens it again (owner 2026-12-03) - const setOpen = (on: boolean): void => { setOpenState(on); setPrefs({ fundamentalsOpen: on ? p.symbol : '' }); }; + const me = useId(); + const kept = useStore(keptWindow) === me; + const setOpen = (on: boolean): void => { setOpenState(on); if (on) keptWindow.set(me); setPrefs({ fundamentalsOpen: on ? p.symbol : '' }); }; useEffect(() => { if (!reopens(prefs.get().fundamentalsOpen, p.symbol, reopened)) return; reopened = true; + keptWindow.set(me); setOpenState(true); }, [p.symbol]); const [drawn, setDrawn] = useState(0); @@ -64,7 +72,8 @@ export function FundamentalsLink({ p, className, children }: { p: Position; clas // controls is measured in an invisible copy, the ticker as drawn, the head is as wide as the charts or the page under it const [looksBox, looks] = useWidths(); const [headingBox, headingWidth] = useWidth(); - const dark = open && typeof document !== 'undefined' && document.documentElement.classList.contains('dark'); + // the theme of the page, read on every draw also while closed: a kept window must not redraw its charts on close + const dark = typeof document !== 'undefined' && document.documentElement.classList.contains('dark'); const frame = fundamentalsFrame(p, dark, language); const page = fundamentalsUrl(p); const ticker = fundamentalsTicker(p); @@ -121,7 +130,7 @@ export function FundamentalsLink({ p, className, children }: { p: Position; clas return ( <> - setOpen(false)} tools={tools}> + setOpen(false)} tools={tools}> {chart // one chart per interval: side by side, or one under another when the window is too narrow for that ?
diff --git a/src/viewer/ui/Zoom.test.tsx b/src/viewer/ui/Zoom.test.tsx new file mode 100644 index 0000000..ee2f116 --- /dev/null +++ b/src/viewer/ui/Zoom.test.tsx @@ -0,0 +1,51 @@ +/// +/// +/// +// A kept window stays drawn but hidden when closed, so its frames keep their state (the TradingView charts' zoom); a +// plain one goes away; neither is drawn before its first open +import { afterAll, beforeAll, expect, test } from 'bun:test'; +import { GlobalRegistrator } from '@happy-dom/global-registrator'; + +// no animations here: the window opens and closes at once (Zoom's path for a browser without Element.animate); put back +// afterwards, the other test files share the same classes +let animate: unknown; +beforeAll(() => { + GlobalRegistrator.register({ url: 'http://localhost:1237/' }); + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; + const proto = Element.prototype as unknown as { animate?: unknown }; + animate = proto.animate; + proto.animate = undefined; +}); +afterAll(() => { + (Element.prototype as unknown as { animate?: unknown }).animate = animate; + GlobalRegistrator.unregister(); +}); + +test('closed, a kept window stays hidden with its content, a plain one is gone', async () => { + const { act } = await import('react'); + const { createRoot } = await import('react-dom/client'); + const { Zoom } = await import('./Zoom'); + document.body.innerHTML = '
'; + const root = createRoot(document.getElementById('root')!); + const draw = (open: boolean, keep: boolean) => act(async () => root.render( undefined}>