@malloy-publisher/sdk 0.2.6 → 0.3.0

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.
Files changed (100) hide show
  1. package/README.md +172 -204
  2. package/dist/ServerProvider-CGnLjzag.cjs.js +1 -0
  3. package/dist/{ServerProvider-ksC5Su_x.es.js → ServerProvider-DSK9rfSY.es.js} +245 -245
  4. package/dist/client/api.d.ts +13 -1
  5. package/dist/client/index.cjs.js +1 -1
  6. package/dist/client/index.es.js +1 -1
  7. package/dist/components/ApiErrorDisplay.d.ts +1 -2
  8. package/dist/components/Dashboard/Dashboard.d.ts +4 -2
  9. package/dist/components/Dashboard/promoteMeasureRow.d.ts +36 -0
  10. package/dist/components/DocumentStorage/BrowserDocumentStorage.d.ts +14 -0
  11. package/dist/components/DocumentStorage/DocumentStorage.d.ts +58 -0
  12. package/dist/components/DocumentStorage/DocumentStorageProvider.d.ts +18 -0
  13. package/dist/components/DocumentStorage/index.d.ts +3 -0
  14. package/dist/components/Model/index.d.ts +0 -1
  15. package/dist/components/RenderedResult/RenderedResult.d.ts +12 -0
  16. package/dist/components/RenderedResult/ResultContainer.d.ts +7 -1
  17. package/dist/components/RenderedResult/index.d.ts +1 -0
  18. package/dist/components/RenderedResult/resultSizing.d.ts +183 -0
  19. package/dist/components/drill/index.d.ts +1 -0
  20. package/dist/components/drill/useDrillSelf.d.ts +46 -0
  21. package/dist/components/given/filterValue.d.ts +63 -0
  22. package/dist/components/given/index.d.ts +1 -0
  23. package/dist/components/given/useGivenUrlParams.d.ts +39 -0
  24. package/dist/components/index.d.ts +2 -3
  25. package/dist/components/styles.d.ts +3 -3
  26. package/dist/core-8NVBCLZh.cjs.js +148 -0
  27. package/dist/{core-xdZbLgaF.es.js → core-B8oMW4sm.es.js} +3540 -2335
  28. package/dist/hooks/index.d.ts +1 -1
  29. package/dist/hooks/useSuggestOptions.d.ts +28 -12
  30. package/dist/index.cjs.js +281 -1
  31. package/dist/index.es.js +45803 -79
  32. package/dist/sdk.css +1 -1
  33. package/dist/styles.css +0 -1
  34. package/package.json +2 -6
  35. package/src/components/ApiErrorDisplay.tsx +1 -1
  36. package/src/components/Connections/EditConnectionDialog.tsx +96 -86
  37. package/src/components/Dashboard/Dashboard.tsx +60 -95
  38. package/src/components/Dashboard/DashboardTile.tsx +25 -2
  39. package/src/components/Dashboard/promoteMeasureRow.spec.ts +137 -0
  40. package/src/components/Dashboard/promoteMeasureRow.ts +129 -0
  41. package/src/components/DocumentStorage/BrowserDocumentStorage.spec.ts +81 -0
  42. package/src/components/DocumentStorage/BrowserDocumentStorage.ts +105 -0
  43. package/src/components/DocumentStorage/DocumentStorage.ts +73 -0
  44. package/src/components/DocumentStorage/DocumentStorageProvider.tsx +46 -0
  45. package/src/components/DocumentStorage/index.ts +15 -0
  46. package/src/components/Environment/Connections.tsx +15 -15
  47. package/src/components/Model/ModelCell.tsx +2 -1
  48. package/src/components/Model/index.ts +0 -1
  49. package/src/components/Notebook/Notebook.tsx +10 -76
  50. package/src/components/Notebook/NotebookCell.tsx +2 -8
  51. package/src/components/RenderedResult/RenderedResult.tsx +86 -76
  52. package/src/components/RenderedResult/ResultContainer.tsx +21 -24
  53. package/src/components/RenderedResult/index.ts +15 -0
  54. package/src/components/RenderedResult/resultSizing.spec.ts +249 -0
  55. package/src/components/RenderedResult/resultSizing.ts +308 -0
  56. package/src/components/ResultsDialog.tsx +2 -1
  57. package/src/components/drill/index.ts +5 -0
  58. package/src/components/drill/useDrillSelf.spec.tsx +64 -0
  59. package/src/components/drill/useDrillSelf.ts +110 -0
  60. package/src/components/given/GivenInput.spec.tsx +196 -2
  61. package/src/components/given/GivenInput.tsx +300 -25
  62. package/src/components/given/filterValue.spec.ts +112 -0
  63. package/src/components/given/filterValue.ts +188 -0
  64. package/src/components/given/index.ts +4 -0
  65. package/src/components/given/useGivenUrlParams.spec.tsx +74 -0
  66. package/src/components/given/useGivenUrlParams.ts +87 -0
  67. package/src/components/index.ts +5 -3
  68. package/src/hooks/index.ts +0 -1
  69. package/src/hooks/useGivensState.spec.ts +158 -0
  70. package/src/hooks/useGivensState.ts +114 -23
  71. package/src/hooks/useSuggestOptions.dom.spec.ts +60 -1
  72. package/src/hooks/useSuggestOptions.ts +94 -51
  73. package/vite.config.ts +0 -1
  74. package/dist/ServerProvider-xXW1HJOJ.cjs.js +0 -1
  75. package/dist/components/AnalyzePackageButton.d.ts +0 -6
  76. package/dist/components/Workbook/BrowserWorkbookStorage.d.ts +0 -9
  77. package/dist/components/Workbook/EditableMalloyCell.d.ts +0 -11
  78. package/dist/components/Workbook/ModelPicker.d.ts +0 -11
  79. package/dist/components/Workbook/MutableCell.d.ts +0 -17
  80. package/dist/components/Workbook/Workbook.d.ts +0 -7
  81. package/dist/components/Workbook/WorkbookList.d.ts +0 -7
  82. package/dist/components/Workbook/WorkbookManager.d.ts +0 -84
  83. package/dist/components/Workbook/WorkbookStorage.d.ts +0 -17
  84. package/dist/components/Workbook/WorkbookStorageProvider.d.ts +0 -12
  85. package/dist/components/Workbook/index.d.ts +0 -7
  86. package/dist/core-RvrkY1Ss.cjs.js +0 -148
  87. package/dist/index-B3scIDRd.cjs.js +0 -356
  88. package/dist/index-Bda7HuZc.es.js +0 -66409
  89. package/dist/markdown-editor.css +0 -341
  90. package/src/components/AnalyzePackageButton.tsx +0 -319
  91. package/src/components/Workbook/BrowserWorkbookStorage.ts +0 -64
  92. package/src/components/Workbook/EditableMalloyCell.tsx +0 -49
  93. package/src/components/Workbook/ModelPicker.tsx +0 -134
  94. package/src/components/Workbook/MutableCell.tsx +0 -458
  95. package/src/components/Workbook/Workbook.tsx +0 -501
  96. package/src/components/Workbook/WorkbookList.tsx +0 -110
  97. package/src/components/Workbook/WorkbookManager.ts +0 -214
  98. package/src/components/Workbook/WorkbookStorage.ts +0 -35
  99. package/src/components/Workbook/WorkbookStorageProvider.tsx +0 -40
  100. package/src/components/Workbook/index.ts +0 -10
@@ -17,6 +17,14 @@ import {
17
17
  type DashboardCardGeometry,
18
18
  } from "../../theme/buildTableCssVars";
19
19
  import { buildVegaThemeOverride } from "../../theme/buildVegaThemeOverride";
20
+ import {
21
+ contentNode,
22
+ contentNodeDepth,
23
+ measureContentHeight,
24
+ remeasuresAfterReady,
25
+ resultSizing,
26
+ type ResultSizing,
27
+ } from "./resultSizing";
20
28
  import { readChartAnnotations } from "../../theme/readChartAnnotations";
21
29
  import { resolveTheme } from "../../theme/resolveTheme";
22
30
  import { usePublisherTheme } from "../../theme/ThemeContext";
@@ -41,11 +49,17 @@ type MalloyRenderElement = HTMLElement & Record<string, unknown>;
41
49
  */
42
50
  interface MalloyVizHandle extends DrillMetadataSource {
43
51
  // Narrowed from `DrillMetadataSource`, which only declares the part the
44
- // drill affordance reads. `getRootField().renderAs()` is what decides
45
- // whether one measurement is enough; see `sizesToContent`.
52
+ // drill affordance reads. `getRootField().renderAs()` is what every sizing
53
+ // decision keys off; see `./resultSizing`.
46
54
  getMetadata: () =>
47
55
  | (NonNullable<ReturnType<DrillMetadataSource["getMetadata"]>> & {
48
- getRootField: () => { renderAs: () => string };
56
+ getRootField: () => { renderAs: () => string; key: string };
57
+ // The root's render plugin, whose `sizingStrategy` is the renderer's
58
+ // own answer to whether the result fills its box; see
59
+ // `./resultSizing`. A table or a `# dashboard` grid has none.
60
+ getPluginsForField: (
61
+ key: string,
62
+ ) => Array<{ sizingStrategy?: string }> | undefined;
49
63
  })
50
64
  | null
51
65
  | undefined;
@@ -74,7 +88,18 @@ interface RenderedResultProps {
74
88
  result: string;
75
89
  height?: number;
76
90
  isFillElement?: (boolean) => void;
91
+ /**
92
+ * The measured content height. Fires only for a content-sized root; a
93
+ * container-sized one has no height of its own to report. See
94
+ * {@link resultSizing}.
95
+ */
77
96
  onSizeChange?: (height: number) => void;
97
+ /**
98
+ * Which sizing rule the rendered root follows, as soon as the renderer's
99
+ * metadata says. Lets a caller stop treating its cap as a height for a
100
+ * result that will never report one.
101
+ */
102
+ onSizing?: (sizing: ResultSizing) => void;
78
103
  /**
79
104
  * Makes `# drill` cells clickable, and makes them look it. From `useDrill`
80
105
  * on the host surface, which decides where a click may go; omitted where
@@ -328,48 +353,12 @@ function injectRendererOverrides(): void {
328
353
  document.head.appendChild(style);
329
354
  }
330
355
 
331
- /**
332
- * Whether the renderer's top-level output sizes itself to its CONTENT rather
333
- * than to the box it was handed, which decides whether measuring it once is
334
- * enough.
335
- *
336
- * It is not, for a table. The renderer signals `onReady` as soon as it knows it
337
- * can paint, and for a table that is immediately — a chart waits for a non-zero
338
- * parent size, a table does not — so the single measurement taken there lands
339
- * before the table's virtualized grid has laid out, and reads a height the
340
- * table never keeps.
341
- *
342
- * Re-measuring a table TERMINATES: `.malloy-table.root` is
343
- * `height: fit-content; max-height: 100%` in the renderer's own CSS and the
344
- * wrappers between it and the container add no padding, so shrinking the
345
- * container to the table's content height is the exact point at which the cap
346
- * stops binding and the next measurement reports the same number.
347
- *
348
- * A root that FILLS its container stays on the one-shot path deliberately: a
349
- * chart draws itself inset from the box it is given, so feeding its height back
350
- * as the new container height would ratchet the container down by that inset
351
- * every pass and never converge.
352
- *
353
- * A table is the only root that needs this, and the reason is the virtualized
354
- * grid rather than the sizing strategy it shares with a `# size=` chart: that
355
- * chart takes its dimensions from a lookup synchronously, so its first
356
- * measurement is already right. Measured, for every size value and spelling.
357
- */
358
- function sizesToContent(viz: MalloyVizHandle): boolean {
359
- try {
360
- return viz.getMetadata()?.getRootField().renderAs() === "table";
361
- } catch {
362
- // Metadata unavailable: measure once, which is the behavior every
363
- // non-table root gets anyway.
364
- return false;
365
- }
366
- }
367
-
368
356
  function RenderedResultInner({
369
357
  result,
370
358
  height: inputHeight,
371
359
  drill,
372
360
  onSizeChange,
361
+ onSizing,
373
362
  }: RenderedResultProps) {
374
363
  const ref = useRef<HTMLDivElement>(null);
375
364
  // The renderer binds its click handler at construction, so a changing
@@ -469,38 +458,29 @@ function RenderedResultInner({
469
458
 
470
459
  hasMeasuredRef.current = false;
471
460
 
472
- // Measure the rendered chart's natural height off `root` (the stage that
473
- // wraps the renderer output) and report it up. Same grandchild/dashboard
474
- // HACK as before, just anchored on the stage wrapper.
461
+ // What the renderer says it is rendering, read once from its metadata
462
+ // after `setResult`. Every height decision below keys off this rather
463
+ // than off the DOM the renderer went on to build; see `./resultSizing`.
464
+ let renderAs: string | undefined;
465
+ let strategy: string | undefined;
466
+
467
+ // Measure the rendered result's content height off `root` (the stage
468
+ // that wraps the renderer output) and report it up.
475
469
  const measureRenderedSize = (root: HTMLElement) => {
476
470
  if (cancelled || !root.firstElementChild) return;
477
- const child = root.firstElementChild as HTMLElement;
478
- const grandchild = child.firstElementChild as HTMLElement;
479
- if (!grandchild) return;
480
- // A table reports a height of its own, so it keeps being measured
481
- // even after the first one lands. Everything else measures once.
482
- const contentSized = viz !== undefined && sizesToContent(viz);
483
- if (hasMeasuredRef.current && !contentSized) return;
484
- const greatgrandchild = grandchild.firstElementChild as HTMLElement;
485
- // `scrollHeight` is the CONTENT height, so this assumes the box adds
486
- // no chrome of its own. True today: `.malloy-table.root` has no
487
- // border, and a horizontal scrollbar costs no layout where scrollbars
488
- // overlay. A wide table clipped by a few pixels on a platform that
489
- // draws classic scrollbars would be this assumption breaking, and the
490
- // fix is `+ (offsetHeight - clientHeight)`.
491
- let renderedHeight =
492
- grandchild.scrollHeight || grandchild.offsetHeight || 0;
493
-
494
- // HACK - malloy dashboards height are determined by the greatgrandchild.
495
- if (
496
- greatgrandchild &&
497
- grandchild.classList.contains("malloy-dashboard")
498
- ) {
499
- renderedHeight =
500
- greatgrandchild.scrollHeight ||
501
- greatgrandchild.offsetHeight ||
502
- 0;
503
- }
471
+ // A chart fills the box it is handed, so measuring one reports back
472
+ // the height we just gave it. Nothing to learn, and reporting it is
473
+ // what used to ratchet an uncapped chart to its first-paint height.
474
+ if (resultSizing(renderAs, strategy) === "container") return;
475
+ // A content-sized root keeps being measured after the first height
476
+ // lands, because the first one races the renderer's layout.
477
+ const remeasures = remeasuresAfterReady(renderAs, strategy);
478
+ if (hasMeasuredRef.current && !remeasures) return;
479
+
480
+ const renderedHeight = measureContentHeight(
481
+ root,
482
+ contentNodeDepth(renderAs),
483
+ );
504
484
 
505
485
  if (renderedHeight > 0) {
506
486
  hasMeasuredRef.current = true;
@@ -509,14 +489,16 @@ function RenderedResultInner({
509
489
  onSizeChange(renderedHeight);
510
490
  }
511
491
  }
512
- // Watch the table from here on. Attached after the first measurement
513
- // rather than at render time because the node does not exist until
514
- // the renderer builds it, and this is the callback that first sees it.
515
- if (contentSized && observedNode !== grandchild) {
516
- observedNode = grandchild;
492
+ // Watch that same node from here on. Attached after the first
493
+ // measurement rather than at render time because the node does not
494
+ // exist until the renderer builds it, and this is the callback that
495
+ // first sees it.
496
+ const measured = contentNode(root, contentNodeDepth(renderAs));
497
+ if (remeasures && measured && observedNode !== measured) {
498
+ observedNode = measured;
517
499
  sizeObserver?.disconnect();
518
500
  sizeObserver = new ResizeObserver(() => measureRenderedSize(root));
519
- sizeObserver.observe(grandchild);
501
+ sizeObserver.observe(measured);
520
502
  }
521
503
  };
522
504
 
@@ -626,6 +608,33 @@ function RenderedResultInner({
626
608
  // The renderer accepts a Malloy Result; we don't import that type
627
609
  // in the SDK to avoid pinning to the malloy core types here.
628
610
  viz.setResult(parsed);
611
+ // Read the root's render type before painting, so the first
612
+ // measurement already knows whether it is worth taking. Metadata is
613
+ // complete as soon as the result is set; the DOM is not.
614
+ try {
615
+ const metadata = viz.getMetadata();
616
+ const root = metadata?.getRootField();
617
+ renderAs = root?.renderAs();
618
+ // The plugin's own `sizingStrategy`, which decides this outright
619
+ // where there is one. Read from the PLUGIN; the field entry's
620
+ // `renderProperties.sizingStrategy` reads "fit" for every root.
621
+ strategy = root
622
+ ? metadata?.getPluginsForField(root.key)?.[0]?.sizingStrategy
623
+ : undefined;
624
+ } catch {
625
+ // Metadata unavailable: fall through to the content-sized
626
+ // default, which is what every root got before this existed.
627
+ renderAs = undefined;
628
+ strategy = undefined;
629
+ }
630
+ onSizing?.(resultSizing(renderAs, strategy));
631
+ // Published on the stage because every sizing decision keys off it
632
+ // and nothing else in the DOM says what it was. A panel at an
633
+ // unexpected height is otherwise a guessing game about which rule
634
+ // applied, and the rule is chosen from a vocabulary — plugin names
635
+ // — that the rendered markup does not spell out anywhere.
636
+ stage.dataset.malloyRenderAs = renderAs ?? "unknown";
637
+ stage.dataset.malloySizing = resultSizing(renderAs, strategy);
629
638
  viz.render(stage);
630
639
 
631
640
  // Mark the cells a `# drill` makes clickable, so they read as
@@ -876,6 +885,7 @@ function RenderedResultInner({
876
885
  handleDrillClick,
877
886
  drillable,
878
887
  onSizeChange,
888
+ onSizing,
879
889
  baseTheme,
880
890
  layers,
881
891
  mode,
@@ -9,12 +9,19 @@ import { LogMessage } from "../../client";
9
9
  import type { DrillBinding } from "../drill/useDrill";
10
10
  import { Loading } from "../Loading";
11
11
  import { summarizeRenderLogs } from "./renderLogs";
12
+ import { resolveResultHeight, type ResultSizing } from "./resultSizing";
12
13
 
13
14
  const RenderedResult = lazy(() => import("../RenderedResult/RenderedResult"));
14
15
 
15
16
  interface ResultContainerProps {
16
17
  result: string | undefined;
17
- maxHeight: number;
18
+ /**
19
+ * Cap on the panel's height. A CAP, not a height request: a result shorter
20
+ * than this paints at its own height. Leave it out for no cap, which is what
21
+ * the single-query dashboard form wants — a result that IS the page should
22
+ * not be clipped to tidy it.
23
+ */
24
+ maxHeight?: number;
18
25
  // if Results are larger than this size, show a warning and a button to proceed
19
26
  // this is to prevent performance issues with large results.
20
27
  // the default is 0, which means no warning will be shown.
@@ -28,24 +35,6 @@ interface ResultContainerProps {
28
35
  drill?: DrillBinding;
29
36
  }
30
37
 
31
- /**
32
- * Height to paint at before the result has been measured.
33
- *
34
- * `maxHeight` is a CAP, but it was also the first paint's height, because the
35
- * measured height starts out equal to it. That was harmless while every caller
36
- * passed something viewport-sized (400 to 800). A caller that means "no cap"
37
- * passes a number that is not a height anyone wants to see: the dashboard
38
- * viewer's whole-page form passes 20000, so the panel painted twenty thousand
39
- * pixels tall until the measurement landed, and a result the renderer sizes to
40
- * its CONTAINER (a top-level chart, as opposed to a `# dashboard` grid, which
41
- * reports its own height) measured that back and kept it.
42
- *
43
- * Bounded here rather than by lowering the cap, so "no cap" stays expressible.
44
- * Above the cap it does nothing, which is every caller that passes a real
45
- * height, so this changes nothing for them.
46
- */
47
- const INITIAL_RENDER_HEIGHT = 2000;
48
-
49
38
  // ResultContainer is a component that renders a result, with a toggle button to expand/collapse the result.
50
39
  // For fill-elements, the result is rendered at minHeight, and the toggle button is shown to scale up to maxHeight.
51
40
  // For non-fill-elements, the result is rendered at explicitHeight, with a small window (minHeight) that can be expanded to maxHeight.
@@ -58,8 +47,12 @@ export default function ResultContainer({
58
47
  drill,
59
48
  }: ResultContainerProps) {
60
49
  const containerRef = useRef<HTMLDivElement>(null);
61
- const [measuredHeight, setMeasuredHeight] = useState(
62
- Math.min(maxHeight, INITIAL_RENDER_HEIGHT),
50
+ // Both start unknown and are filled in by the render: `sizing` as soon as
51
+ // the renderer's metadata is read, `contentHeight` only if the root has one
52
+ // to report. `resolveResultHeight` owns what to paint at each stage.
53
+ const [sizing, setSizing] = useState<ResultSizing | undefined>(undefined);
54
+ const [contentHeight, setContentHeight] = useState<number | undefined>(
55
+ undefined,
63
56
  );
64
57
  const [userAcknowledged, setUserAcknowledged] = useState(false);
65
58
  const renderLogSummary = summarizeRenderLogs(renderLogs);
@@ -104,8 +97,11 @@ export default function ResultContainer({
104
97
  }
105
98
 
106
99
  const loading = <Loading text="Loading..." centered={true} size={32} />;
107
- // Fixed height for content - no resizing
108
- const renderedHeight = Math.min(maxHeight, measuredHeight);
100
+ const renderedHeight = resolveResultHeight({
101
+ sizing,
102
+ contentHeight,
103
+ maxHeight,
104
+ });
109
105
 
110
106
  return (
111
107
  <Box
@@ -123,7 +119,8 @@ export default function ResultContainer({
123
119
  <RenderedResult
124
120
  result={result}
125
121
  height={renderedHeight}
126
- onSizeChange={setMeasuredHeight}
122
+ onSizeChange={setContentHeight}
123
+ onSizing={setSizing}
127
124
  drill={drill}
128
125
  />
129
126
  </Suspense>
@@ -3,3 +3,18 @@
3
3
 
4
4
  export { default as RenderedResult } from "./RenderedResult";
5
5
  export { default as ResultContainer } from "./ResultContainer";
6
+ export {
7
+ contentNodeDepth,
8
+ INITIAL_RENDER_HEIGHT,
9
+ initialResultHeight,
10
+ measureContentHeight,
11
+ MODEL_CELL_MAX_HEIGHT,
12
+ NOTEBOOK_CELL_MAX_HEIGHT,
13
+ remeasuresAfterReady,
14
+ resolveResultHeight,
15
+ RESULTS_DIALOG_MAX_HEIGHT,
16
+ resultSizing,
17
+ TILE_MAX_HEIGHT,
18
+ UNCAPPED_CONTAINER_HEIGHT,
19
+ type ResultSizing,
20
+ } from "./resultSizing";
@@ -0,0 +1,249 @@
1
+ // Copyright (c) Credible Data Inc.
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import { describe, expect, it } from "bun:test";
5
+ import {
6
+ contentNode,
7
+ contentNodeDepth,
8
+ INITIAL_RENDER_HEIGHT,
9
+ initialResultHeight,
10
+ measureContentHeight,
11
+ remeasuresAfterReady,
12
+ resolveResultHeight,
13
+ resultSizing,
14
+ UNCAPPED_CONTAINER_HEIGHT,
15
+ } from "./resultSizing";
16
+
17
+ describe("resultSizing", () => {
18
+ // `renderAs()` reports the PLUGIN's name, and the renderer's default
19
+ // registry names the bar and line plugins "bar" and "line" — the tag
20
+ // spellings "bar_chart" and "line_chart" never come back from it. Getting
21
+ // this wrong is invisible: the chart is simply measured, and a `# line_chart`
22
+ // tile quietly shrinks to the inset height it drew itself at.
23
+ it.each(["bar", "line", "chart", "scatter_chart"])(
24
+ "calls %s container-sized",
25
+ (renderAs) => {
26
+ expect(resultSizing(renderAs)).toBe("container");
27
+ },
28
+ );
29
+
30
+ it.each([
31
+ "table",
32
+ "dashboard",
33
+ "list",
34
+ "big_value",
35
+ "cell",
36
+ "link",
37
+ "image",
38
+ // Measured: a `# shape_map` reports 365 in a 400px tile and 365 in a
39
+ // 700px cell. Its height is its aspect ratio, not its box, so it shrinks
40
+ // to content like a table rather than filling like a bar chart, which
41
+ // reports its box minus an 8px inset at both of those sizes.
42
+ "shape_map",
43
+ "segment_map",
44
+ ])("calls %s content-sized", (renderAs) => {
45
+ expect(resultSizing(renderAs)).toBe("content");
46
+ });
47
+
48
+ // A render plugin returns its own name here, and a renderer newer than this
49
+ // SDK can return one we have never heard of. Content-sized is what every
50
+ // root got before this classification existed, so an unknown name keeps the
51
+ // old behavior rather than silently losing its height.
52
+ it("treats an unknown root as content-sized", () => {
53
+ expect(resultSizing("vega_lite_plugin")).toBe("content");
54
+ expect(resultSizing(undefined)).toBe("content");
55
+ });
56
+ });
57
+
58
+ // The renderer's own answer, and the one that decides where both are present.
59
+ // Measured against the bundled examples: `bar:fill`, `line:fill`,
60
+ // `shape_map:fixed`, and no plugin at all for a table or a `# dashboard` grid.
61
+ describe("resultSizing from the plugin's declared strategy", () => {
62
+ it("takes fill as container-sized and fixed as content-sized", () => {
63
+ expect(resultSizing("anything", "fill")).toBe("container");
64
+ expect(resultSizing("anything", "fixed")).toBe("content");
65
+ });
66
+
67
+ // The point of reading it: a host's own plugin says which it is instead of
68
+ // falling into the unknown-name default, and gets re-measured accordingly.
69
+ it("places a plugin this SDK has never heard of", () => {
70
+ expect(resultSizing("acme_gauge", "fixed")).toBe("content");
71
+ expect(remeasuresAfterReady("acme_gauge", "fixed")).toBe(true);
72
+ expect(resultSizing("acme_gauge", "fill")).toBe("container");
73
+ expect(remeasuresAfterReady("acme_gauge", "fill")).toBe(false);
74
+ });
75
+
76
+ // A `# dashboard` grid and a table have no plugin, so the names still carry
77
+ // them, and every name that DOES have one agrees with it today.
78
+ it("falls back to the name when no plugin claims the root", () => {
79
+ expect(resultSizing("table", undefined)).toBe("content");
80
+ expect(resultSizing("dashboard", undefined)).toBe("content");
81
+ expect(resultSizing("bar", undefined)).toBe("container");
82
+ expect(resultSizing("shape_map", undefined)).toBe("content");
83
+ });
84
+
85
+ it("still measures an unplaceable root exactly once", () => {
86
+ expect(resultSizing("acme_gauge", undefined)).toBe("content");
87
+ expect(remeasuresAfterReady("acme_gauge", undefined)).toBe(false);
88
+ });
89
+ });
90
+
91
+ describe("remeasuresAfterReady", () => {
92
+ // The first measurement races the renderer's layout: a table signals ready
93
+ // before its virtualized grid has laid out, and a `# big_value` row reports
94
+ // the box it was handed rather than its own 136px.
95
+ it.each(["table", "big_value", "dashboard", "list", "shape_map"])(
96
+ "keeps watching %s",
97
+ (renderAs) => {
98
+ expect(remeasuresAfterReady(renderAs)).toBe(true);
99
+ },
100
+ );
101
+
102
+ // A host's own plugin could be a fill-the-box viz wearing a name we do not
103
+ // know, and one measurement is what it got before any of this existed.
104
+ it.each(["vega_lite_plugin", undefined])(
105
+ "measures the unrecognised %s once",
106
+ (renderAs) => {
107
+ expect(resultSizing(renderAs)).toBe("content");
108
+ expect(remeasuresAfterReady(renderAs)).toBe(false);
109
+ },
110
+ );
111
+
112
+ // The one root re-measuring is not safe for, and the reason it used to be
113
+ // tables only: a chart insets itself inside its box, so feeding its height
114
+ // back would ratchet the container down every pass. It is never measured now.
115
+ it.each(["bar", "line", "chart", "scatter_chart"])(
116
+ "never measures %s at all",
117
+ (renderAs) => {
118
+ expect(remeasuresAfterReady(renderAs)).toBe(false);
119
+ },
120
+ );
121
+ });
122
+
123
+ describe("contentNodeDepth", () => {
124
+ it("reads a dashboard grid one level deeper than every other root", () => {
125
+ expect(contentNodeDepth("dashboard")).toBe(3);
126
+ expect(contentNodeDepth("table")).toBe(2);
127
+ expect(contentNodeDepth(undefined)).toBe(2);
128
+ });
129
+ });
130
+
131
+ /** A chain of nested divs, each with the scrollHeight it is given. */
132
+ function stageWithHeights(...heights: number[]): HTMLElement {
133
+ const stage = document.createElement("div");
134
+ let parent: HTMLElement = stage;
135
+ for (const height of heights) {
136
+ const node = document.createElement("div");
137
+ Object.defineProperty(node, "scrollHeight", {
138
+ value: height,
139
+ configurable: true,
140
+ });
141
+ parent.appendChild(node);
142
+ parent = node;
143
+ }
144
+ return stage;
145
+ }
146
+
147
+ describe("contentNode", () => {
148
+ // The node measured and the node watched for changes have to be the same one,
149
+ // or a result that settles later settles at a height nothing reported.
150
+ it("is the node measureContentHeight reads", () => {
151
+ const stage = stageWithHeights(400, 137, 120);
152
+ expect(contentNode(stage, 2)?.scrollHeight).toBe(
153
+ measureContentHeight(stage, 2),
154
+ );
155
+ });
156
+
157
+ it("is null before the renderer has built anything", () => {
158
+ expect(contentNode(document.createElement("div"), 2)).toBeNull();
159
+ });
160
+
161
+ it("is null while the renderer has not built that deep", () => {
162
+ expect(contentNode(stageWithHeights(400, 400), 3)).toBeNull();
163
+ });
164
+ });
165
+
166
+ describe("measureContentHeight", () => {
167
+ // Depth 1 is the renderer's outer wrapper, which is box-sized: measuring it
168
+ // would floor every result at the height the container already has.
169
+ it("reads the root render node at depth 2", () => {
170
+ expect(measureContentHeight(stageWithHeights(400, 137, 120), 2)).toBe(
171
+ 137,
172
+ );
173
+ });
174
+
175
+ it("reads a dashboard grid at depth 3", () => {
176
+ expect(measureContentHeight(stageWithHeights(400, 400, 227), 3)).toBe(
177
+ 227,
178
+ );
179
+ });
180
+
181
+ // The shallow nodes are the renderer's box-sized wrappers, so answering with
182
+ // one reports back the height the panel already has. That reads as a
183
+ // successful measurement and stops the retry, which is how a `# big_value`
184
+ // tile got stuck at its 400px cap with its own content measuring 136.
185
+ it("reports nothing while the DOM is shallower than the content node", () => {
186
+ expect(measureContentHeight(stageWithHeights(400, 400), 3)).toBe(0);
187
+ });
188
+
189
+ it("reports nothing when the renderer has not built anything yet", () => {
190
+ expect(measureContentHeight(document.createElement("div"), 2)).toBe(0);
191
+ });
192
+ });
193
+
194
+ describe("initialResultHeight", () => {
195
+ it("paints an uncapped result at the seed height", () => {
196
+ expect(initialResultHeight(undefined)).toBe(INITIAL_RENDER_HEIGHT);
197
+ });
198
+
199
+ it("never paints taller than the cap", () => {
200
+ expect(initialResultHeight(400)).toBe(400);
201
+ });
202
+ });
203
+
204
+ describe("resolveResultHeight", () => {
205
+ const at = (
206
+ sizing: "content" | "container" | undefined,
207
+ contentHeight: number | undefined,
208
+ maxHeight: number | undefined,
209
+ ) => resolveResultHeight({ sizing, contentHeight, maxHeight });
210
+
211
+ it("paints at the seed before anything is known", () => {
212
+ expect(at(undefined, undefined, 400)).toBe(400);
213
+ expect(at(undefined, undefined, undefined)).toBe(INITIAL_RENDER_HEIGHT);
214
+ });
215
+
216
+ // The whole point of the classification: a chart fills its box, so the cap
217
+ // IS its height and it is never measured. This is what the 20000px "no cap"
218
+ // sentinel used to turn into a 1992px bar chart.
219
+ it("gives a container-sized root its cap, and a default with no cap", () => {
220
+ expect(at("container", undefined, 400)).toBe(400);
221
+ expect(at("container", undefined, undefined)).toBe(
222
+ UNCAPPED_CONTAINER_HEIGHT,
223
+ );
224
+ });
225
+
226
+ // Even if one arrived: a chart's measurement only ever reports back the
227
+ // height it was handed.
228
+ it("ignores a measurement from a container-sized root", () => {
229
+ expect(at("container", 1992, undefined)).toBe(UNCAPPED_CONTAINER_HEIGHT);
230
+ });
231
+
232
+ it("shrinks a content-sized root to its content", () => {
233
+ expect(at("content", 137, 400)).toBe(137);
234
+ });
235
+
236
+ it("caps a content-sized root taller than the cap", () => {
237
+ expect(at("content", 4000, 700)).toBe(700);
238
+ });
239
+
240
+ // No cap means no cap: the single-query dashboard form lets its one result
241
+ // set the page height and scrolls.
242
+ it("lets an uncapped content-sized root use its full height", () => {
243
+ expect(at("content", 4000, undefined)).toBe(4000);
244
+ });
245
+
246
+ it("stays at the seed while a content-sized root measures zero", () => {
247
+ expect(at("content", 0, 400)).toBe(400);
248
+ });
249
+ });