@bicharts/chart-host 0.5.49 → 0.5.51

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.
@@ -6,7 +6,7 @@ import {
6
6
  } from "./chunk-A2GMXZP7.mjs";
7
7
 
8
8
  // src/contract.ts
9
- var HOST_CONTRACT_VERSION = "1.5.0";
9
+ var HOST_CONTRACT_VERSION = "1.6.0";
10
10
  var GEO_POINT_PRECISIONS = ["latlon", "city", "zip3", "state", "country"];
11
11
  var MARK_CLASS = "d3-mark";
12
12
  var LEGEND_MARK_CLASS = "d3-legend-mark";
@@ -1197,6 +1197,23 @@ function blankRenderFlag(lang, cause) {
1197
1197
  }
1198
1198
 
1199
1199
  // src/host.ts
1200
+ function sessionViewStateProvider(container) {
1201
+ const holder = container;
1202
+ const existing = holder[CONTAINER_SLOT_UI_STATE];
1203
+ const store = existing && typeof existing === "object" ? existing : holder[CONTAINER_SLOT_UI_STATE] = {};
1204
+ return {
1205
+ load: () => store,
1206
+ save: (next) => {
1207
+ if (!next || typeof next !== "object") return;
1208
+ for (const k2 of Object.keys(store)) delete store[k2];
1209
+ Object.assign(store, next);
1210
+ }
1211
+ };
1212
+ }
1213
+ function noopViewStateProvider() {
1214
+ return { load: () => ({}), save: () => {
1215
+ } };
1216
+ }
1200
1217
  var parseRowIdxs = (s) => (s || "").split(",").map((x2) => parseInt(x2, 10)).filter((n) => Number.isFinite(n));
1201
1218
  var D3_PLUGIN_PACKAGES = {
1202
1219
  sankey: "d3-sankey",
@@ -1280,16 +1297,22 @@ function createChartHost(container, config) {
1280
1297
  }
1281
1298
  let raw = { ...config.options ?? {} };
1282
1299
  if (typeof raw.setUiState !== "function") {
1283
- const holder = container;
1284
- const existing = holder[CONTAINER_SLOT_UI_STATE];
1285
- const store = existing && typeof existing === "object" ? existing : holder[CONTAINER_SLOT_UI_STATE] = {};
1286
- if (raw.uiState && typeof raw.uiState === "object" && Object.keys(store).length === 0)
1287
- Object.assign(store, raw.uiState);
1288
- raw.uiState = store;
1300
+ const provider = config.viewState ?? sessionViewStateProvider(container);
1301
+ let bag;
1302
+ try {
1303
+ bag = provider.load() ?? {};
1304
+ } catch {
1305
+ bag = {};
1306
+ }
1307
+ if (raw.uiState && typeof raw.uiState === "object" && Object.keys(bag).length === 0)
1308
+ Object.assign(bag, raw.uiState);
1309
+ raw.uiState = bag;
1289
1310
  raw.setUiState = (s) => {
1290
1311
  if (!s || typeof s !== "object") return;
1291
- for (const k2 of Object.keys(store)) delete store[k2];
1292
- Object.assign(store, s);
1312
+ try {
1313
+ provider.save(s);
1314
+ } catch {
1315
+ }
1293
1316
  };
1294
1317
  }
1295
1318
  let resolved = resolveOptions(raw);
@@ -1587,6 +1610,8 @@ export {
1587
1610
  censusMarks,
1588
1611
  isBlankRender,
1589
1612
  blankRenderFlag,
1613
+ sessionViewStateProvider,
1614
+ noopViewStateProvider,
1590
1615
  requiredD3Plugins,
1591
1616
  explainRenderFailure,
1592
1617
  stripEsmExports,
package/dist/index.mjs CHANGED
@@ -48,13 +48,15 @@ import {
48
48
  geoFromCache,
49
49
  isBlankRender,
50
50
  loadGeo,
51
+ noopViewStateProvider,
51
52
  periodTickSuppressesFeedback,
52
53
  registerGeo,
53
54
  registerGeoAsset,
54
55
  requiredD3Plugins,
55
56
  resolveOptions,
57
+ sessionViewStateProvider,
56
58
  stripEsmExports
57
- } from "./chunk-OSJNZVRA.mjs";
59
+ } from "./chunk-B4RZSFAC.mjs";
58
60
  import "./chunk-A2GMXZP7.mjs";
59
61
 
60
62
  // src/trivial.ts
@@ -923,6 +925,7 @@ export {
923
925
  loadGeo,
924
926
  newQualifyGroupState,
925
927
  newQualifyRefusalGroupState,
928
+ noopViewStateProvider,
926
929
  normaliseAggregation,
927
930
  orderRefusalsForDisplay,
928
931
  periodTickSuppressesFeedback,
@@ -940,6 +943,7 @@ export {
940
943
  registerGeoAsset,
941
944
  requiredD3Plugins,
942
945
  resolveOptions,
946
+ sessionViewStateProvider,
943
947
  shouldOpenChooserOnGenerate,
944
948
  shouldOpenInlineChooserOnGenerate,
945
949
  shouldReview,
package/dist/react.mjs CHANGED
@@ -4,7 +4,7 @@ import {
4
4
  createChartHost,
5
5
  geoFromCache,
6
6
  loadGeo
7
- } from "./chunk-OSJNZVRA.mjs";
7
+ } from "./chunk-B4RZSFAC.mjs";
8
8
  import "./chunk-A2GMXZP7.mjs";
9
9
 
10
10
  // src/react.tsx
@@ -49,6 +49,7 @@ function BicChart(props) {
49
49
  options,
50
50
  d3,
51
51
  geoKind,
52
+ viewState,
52
53
  id,
53
54
  filteredBy,
54
55
  respondsWith,
@@ -89,7 +90,7 @@ function BicChart(props) {
89
90
  useLayoutEffect(() => {
90
91
  const el = ref.current;
91
92
  if (!el || !code && !renderFn || !data) return;
92
- const host = createChartHost(el, { code, renderFn, data, options, d3, geoKind });
93
+ const host = createChartHost(el, { code, renderFn, data, options, d3, geoKind, viewState });
93
94
  hostRef.current = host;
94
95
  const off = host.selection.onChange((payloadIdxs, source) => {
95
96
  if (source === "host") return;
@@ -1,4 +1,4 @@
1
- export declare const HOST_CONTRACT_VERSION = "1.5.0";
1
+ export declare const HOST_CONTRACT_VERSION = "1.6.0";
2
2
  export type GeoPointPrecision = "latlon" | "city" | "zip3" | "state" | "country";
3
3
  /** Every valid tier, ordered most precise → coarsest. Runtime form of GeoPointPrecision. */
4
4
  export declare const GEO_POINT_PRECISIONS: readonly GeoPointPrecision[];
@@ -16,6 +16,32 @@ export declare const CONTAINER_SLOT_ANIM_STOP = "__llmAnimStop";
16
16
  export declare const CONTAINER_SLOT_XF_CLEAR = "__llmXfClear";
17
17
  export declare const CONTAINER_SLOT_INITIAL_XF_MARK = "__llmInitialXfMark";
18
18
  export declare const CONTAINER_SLOT_UI_STATE = "__lchUiState";
19
+ /**
20
+ * WHERE A CHART'S RESTING VIEW-STATE LIVES between draws (contract 1.6.0).
21
+ *
22
+ * Generated code never sees this. It keeps reading `options.uiState` and calling
23
+ * `options.setUiState(bag)`; `createChartHost` is what binds that pair to a provider. The seam
24
+ * exists because the answer genuinely differs per host: a Power BI visual persists into the
25
+ * report file so every viewer lands on the author's view, an Excel add-in persists into the
26
+ * workbook that travels with the sheet, a React page has only the session, and a static preview
27
+ * has nowhere at all. Before this, the first of those was a raw function pair passed by one
28
+ * caller and every other host got a session store hard-coded in the host runtime.
29
+ *
30
+ * IMPLEMENTORS: `load()` must never throw and returns `{}` when nothing is stored — a chart must
31
+ * never be blocked from drawing by a storage failure. `save()` takes the WHOLE new bag (REPLACE
32
+ * semantics, matching setUiState) and MAY DEBOUNCE; a drag emits hundreds of repaints and no
33
+ * host's persistence layer is a frame buffer.
34
+ *
35
+ * READERS: whatever comes back is UNTRUSTED. It may have been written by a previous generation of
36
+ * the chart, by a different chart on the same element, or by a person editing a file. Validate and
37
+ * clamp every value you read; a key you do not recognise is not an error, it is someone else's.
38
+ */
39
+ export interface ViewStateProvider {
40
+ /** The stored bag, or {} when there is none. Never throws. */
41
+ load(): Record<string, unknown>;
42
+ /** Replace the stored bag. May debounce internally. */
43
+ save(next: Record<string, unknown>): void;
44
+ }
19
45
  export declare const HOST_CONTAINER_CLASS = "bic-chart-host";
20
46
  export declare const SELECTION_ACTIVE_CLASS = "lch-has-selection";
21
47
  export declare const MARK_SELECTED_CLASS = "lch-mark-selected";
@@ -1,7 +1,29 @@
1
- import { type RenderOptions } from "./contract";
1
+ import { type RenderOptions, type ViewStateProvider } from "./contract";
2
2
  import { type ResolveOptionsInput } from "./defaults";
3
3
  import { type MarkCensus } from "./blankRender";
4
4
  export type RenderFn = (container: HTMLElement, data: any, options: RenderOptions) => void;
5
+ /**
6
+ * THE SESSION PROVIDER — what every host got hard-coded before contract 1.6.0, now named.
7
+ *
8
+ * The bag is parked on the CONTAINER ELEMENT, which outlives the host object in every host that
9
+ * destroys and re-creates itself on the same node — the Excel add-in does exactly that on every
10
+ * resize and every cell edit, so a chart's resting knob used to reset whenever the pane moved.
11
+ * It dies with the element, which is precisely the scope "session" means here.
12
+ *
13
+ * `load()` returns the LIVE store, and `save()` mutates it in place rather than replacing the
14
+ * object, so a caller holding the bag from before a save still sees the current values. That is
15
+ * the 1.5.0 behaviour preserved exactly.
16
+ */
17
+ export declare function sessionViewStateProvider(container: HTMLElement): ViewStateProvider;
18
+ /**
19
+ * NOWHERE TO PUT IT, said out loud. For a host with no session to speak of — a server-side
20
+ * render, a static preview, a thumbnail capture — where remembering would be wrong rather than
21
+ * merely absent: a thumbnail must be the same picture every time it is taken.
22
+ *
23
+ * Deliberately not the same as omitting a provider. Omitting one gets the session store and a
24
+ * chart that remembers within the page; choosing this one says the forgetting is the point.
25
+ */
26
+ export declare function noopViewStateProvider(): ViewStateProvider;
5
27
  export interface ChartHostConfig {
6
28
  data: {
7
29
  columns: any[];
@@ -28,6 +50,10 @@ export interface ChartHostConfig {
28
50
  renderFn?: RenderFn;
29
51
  /** Partial raw options; resolveOptions applies the shared defaults/clamps. */
30
52
  options?: ResolveOptionsInput;
53
+ /** WHERE this host keeps a chart's resting view-state (contract 1.6.0). Omit for the
54
+ * session store parked on the container — the behaviour every host had before 1.6.0.
55
+ * A raw `options.setUiState` still wins over this, so an existing caller is untouched. */
56
+ viewState?: ViewStateProvider;
31
57
  /** D3 v7 instance handed to the compiled code. Default: (globalThis as any).d3. */
32
58
  d3?: any;
33
59
  /** Attach geometry for this geoKind as options.geo (choropleths / basemaps). Resolved
@@ -1,11 +1,11 @@
1
- export { HOST_CONTRACT_VERSION, GEO_POINT_PRECISIONS, MARK_CLASS, LEGEND_MARK_CLASS, AXIS_FILTER_CLASS, ROW_IDX_ATTR, XFILTER_REFRESH_EVENT, CONTAINER_SLOT_ANIM_STOP, CONTAINER_SLOT_XF_CLEAR, CONTAINER_SLOT_INITIAL_XF_MARK, CONTAINER_SLOT_UI_STATE, HOST_CONTAINER_CLASS, SELECTION_ACTIVE_CLASS, MARK_SELECTED_CLASS, ACTIVE_TICK_CLASS, DIM_OPACITY_VAR, DIM_OPACITY_DEFAULT, chartOwnsTimeline, periodTickSuppressesFeedback, ANIM_PLAY_SPEED_DEFAULT, ANIM_PLAY_SPEED_MIN, ANIM_PLAY_SPEED_MAX, ANIM_LOOP_DELAY_DEFAULT, ANIM_LOOP_DELAY_MIN, ANIM_MAX_IDEAL_FRAMES_DEFAULT, ANIM_MAX_IDEAL_FRAMES_MIN, ANIM_MAX_IDEAL_FRAMES_MAX, COLOR_SCALE_SELF_CLAMP_PCT_DEFAULT, COLOR_SCALE_SELF_CLAMP_PCT_MIN, COLOR_SCALE_SELF_CLAMP_PCT_MAX, FLIP_MODE_DEFAULT, type GeoPointPrecision, type GeoMapKind, type TimelineStyle, type FlipMode, type ColorScaleScope, type RenderOptions, } from "./contract";
1
+ export { HOST_CONTRACT_VERSION, GEO_POINT_PRECISIONS, MARK_CLASS, LEGEND_MARK_CLASS, AXIS_FILTER_CLASS, ROW_IDX_ATTR, XFILTER_REFRESH_EVENT, CONTAINER_SLOT_ANIM_STOP, CONTAINER_SLOT_XF_CLEAR, CONTAINER_SLOT_INITIAL_XF_MARK, CONTAINER_SLOT_UI_STATE, HOST_CONTAINER_CLASS, SELECTION_ACTIVE_CLASS, MARK_SELECTED_CLASS, ACTIVE_TICK_CLASS, DIM_OPACITY_VAR, DIM_OPACITY_DEFAULT, chartOwnsTimeline, periodTickSuppressesFeedback, ANIM_PLAY_SPEED_DEFAULT, ANIM_PLAY_SPEED_MIN, ANIM_PLAY_SPEED_MAX, ANIM_LOOP_DELAY_DEFAULT, ANIM_LOOP_DELAY_MIN, ANIM_MAX_IDEAL_FRAMES_DEFAULT, ANIM_MAX_IDEAL_FRAMES_MIN, ANIM_MAX_IDEAL_FRAMES_MAX, COLOR_SCALE_SELF_CLAMP_PCT_DEFAULT, COLOR_SCALE_SELF_CLAMP_PCT_MIN, COLOR_SCALE_SELF_CLAMP_PCT_MAX, FLIP_MODE_DEFAULT, type GeoPointPrecision, type GeoMapKind, type TimelineStyle, type FlipMode, type ColorScaleScope, type RenderOptions, type ViewStateProvider, } from "./contract";
2
2
  export { resolveOptions, type ResolveOptionsInput } from "./defaults";
3
3
  export { loadGeo, geoFromCache, registerGeo, registerGeoAsset, geoAssetFor, clearGeoCache, type GeoAssetName, } from "./geoLazy";
4
4
  /** REPLACE the bundled city gazetteer with another packed table. Rarely needed — see the
5
5
  * note above registerCityTable in shape-core's geoPoint.ts before using it. */
6
6
  export declare function registerCityTable(packed: string): void;
7
7
  export { buildRenderPayload, type RenderPayload, type GeoPointBinding } from "./payload";
8
- export { createChartHost, compileRenderFn, stripEsmExports, requiredD3Plugins, explainRenderFailure, type ChartHost, type ChartHostConfig, type RenderFn } from "./host";
8
+ export { createChartHost, compileRenderFn, stripEsmExports, requiredD3Plugins, explainRenderFailure, sessionViewStateProvider, noopViewStateProvider, type ChartHost, type ChartHostConfig, type RenderFn } from "./host";
9
9
  export { createMarkResolver, type MarkResolver, type MarkResolverEnv } from "./selection";
10
10
  export { planTrivialChart, compileTrivialSource, type TrivialPlan, type TrivialShapeKind } from "./trivial";
11
11
  export { captureSvgSnapshot, svgToDataUrl, svgNaturalSize, rasterizeSvgToPngDataUrl, type SnapshotOptions } from "./snapshot";
@@ -15,6 +15,16 @@ export interface BicChartProps {
15
15
  d3: any;
16
16
  /** Attach the bundled geometry for this kind as options.geo (choropleth or basemap). */
17
17
  geoKind?: string;
18
+ /**
19
+ * WHERE THIS APP KEEPS THE CHART'S RESTING VIEW-STATE - a sort, an expanded row, a 3D
20
+ * camera. Omit for the session store parked on the container element, which is the right
21
+ * answer for most pages: the chart remembers while it is mounted and forgets on reload.
22
+ *
23
+ * Supply one to outlive the page. A React app is the host that most often has somewhere
24
+ * better to put it - localStorage, a URL query, a user profile on the server - and no
25
+ * default could pick between them, which is why this is a prop rather than a flag.
26
+ */
27
+ viewState?: ChartHostConfig["viewState"];
18
28
  /** Identity within a <BicChartGroup> — the key other charts filter by. */
19
29
  id?: string;
20
30
  /** Take this group member's selection as a filter on THIS chart's rows. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bicharts/chart-host",
3
- "version": "0.5.49",
3
+ "version": "0.5.51",
4
4
  "description": "Run a BIC-generated D3 chart in any web host: compiles the generated render() function, applies the shared option defaults, resolves mark clicks (through tooltip overlays), owns the selection affordance, and translates row indices between cross-filtered charts. The same contract the BIC Power BI visual implements, minus Power BI. React bindings at @bicharts/chart-host/react.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",