@malloy-publisher/sdk 0.0.382-dev → 0.2.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.
- package/README.md +115 -95
- package/TREE_SHAKING.md +16 -11
- package/bunfig.toml +7 -0
- package/dist/ServerProvider-CTopDUWW.cjs.js +1 -0
- package/dist/ServerProvider-DUFPY6Rv.es.js +4072 -0
- package/dist/client/api.d.ts +3718 -1507
- package/dist/client/configuration.d.ts +1 -1
- package/dist/client/index.cjs.js +1 -1
- package/dist/client/index.d.ts +1 -1
- package/dist/client/index.es.js +85 -59
- package/dist/components/Connections/EditConnectionDialog.d.ts +2 -1
- package/dist/components/Connections/common.d.ts +8 -2
- package/dist/components/Dashboard/Dashboard.d.ts +49 -0
- package/dist/components/Dashboard/DashboardTile.d.ts +48 -0
- package/dist/components/Dashboard/index.d.ts +2 -0
- package/dist/components/DataAppViewer/DataAppViewer.d.ts +23 -0
- package/dist/components/DataAppViewer/index.d.ts +1 -0
- package/dist/components/Environment/Environment.d.ts +6 -0
- package/dist/components/Environment/index.d.ts +2 -0
- package/dist/components/Home/AddEnvironmentDialog.d.ts +1 -0
- package/dist/components/Home/DeleteEnvironmentDialog.d.ts +5 -0
- package/dist/components/Home/EditEnvironmentDialog.d.ts +7 -0
- package/dist/components/Home/Home.d.ts +2 -2
- package/dist/components/Materializations/CreateEnvMaterializationDialog.d.ts +14 -0
- package/dist/components/Materializations/CreateMaterializationDialog.d.ts +10 -0
- package/dist/components/Materializations/DeleteMaterializationDialog.d.ts +7 -0
- package/dist/components/Materializations/EnvironmentMaterializations.d.ts +7 -0
- package/dist/components/Materializations/ManifestView.d.ts +13 -0
- package/dist/components/Materializations/MaterializationDetailDialog.d.ts +9 -0
- package/dist/components/Materializations/MaterializationRunsList.d.ts +15 -0
- package/dist/components/Materializations/Materializations.d.ts +7 -0
- package/dist/components/Materializations/ScheduleCard.d.ts +20 -0
- package/dist/components/Materializations/SectionLabel.d.ts +10 -0
- package/dist/components/Materializations/SetScheduleDialog.d.ts +11 -0
- package/dist/components/Materializations/TriggerChip.d.ts +10 -0
- package/dist/components/Materializations/cron.d.ts +23 -0
- package/dist/components/Materializations/index.d.ts +2 -0
- package/dist/components/Materializations/utils.d.ts +46 -0
- package/dist/components/Notebook/Notebook.d.ts +47 -4
- package/dist/components/Notebook/NotebookCell.d.ts +37 -1
- package/dist/components/Notebook/runPlan.d.ts +29 -0
- package/dist/components/Notebook/types.d.ts +10 -0
- package/dist/components/Package/ContentTypeIcon.d.ts +31 -0
- package/dist/components/Package/Package.d.ts +1 -4
- package/dist/components/RenderedResult/RenderedResult.d.ts +7 -1
- package/dist/components/RenderedResult/ResultContainer.d.ts +5 -1
- package/dist/components/RenderedResult/renderLogs.d.ts +6 -0
- package/dist/components/ResultsDialog.d.ts +6 -1
- package/dist/components/ServerProvider.d.ts +28 -4
- package/dist/components/click_helper.d.ts +10 -1
- package/dist/components/drill/index.d.ts +3 -0
- package/dist/components/drill/markDrillableCells.d.ts +55 -0
- package/dist/components/drill/resolveDrill.d.ts +139 -0
- package/dist/components/drill/useDrill.d.ts +63 -0
- package/dist/components/given/GivenInput.d.ts +52 -0
- package/dist/components/given/GivensPanel.d.ts +47 -0
- package/dist/components/given/filterValue.d.ts +97 -0
- package/dist/components/given/index.d.ts +2 -0
- package/dist/components/given/paramCodec.d.ts +60 -0
- package/dist/components/given/utils.d.ts +17 -0
- package/dist/components/highlighter.d.ts +6 -1
- package/dist/components/index.d.ts +7 -2
- package/dist/components/styles.d.ts +47 -0
- package/dist/constants/docLinks.d.ts +12 -0
- package/dist/{core-YNpOLuB1.cjs.js → core-hLahnv0W.cjs.js} +1 -1
- package/dist/{core-CjeTkq8O.es.js → core-lb28vQkR.es.js} +1 -1
- package/dist/github-dark-BKL9xNgN.cjs.js +1 -0
- package/dist/github-dark-DenFmJkN.es.js +4 -0
- package/dist/hooks/givenValue.d.ts +10 -0
- package/dist/hooks/index.d.ts +4 -0
- package/dist/hooks/useDimensionFiltersFromSpec.d.ts +3 -3
- package/dist/hooks/useDimensionFiltersQuery.d.ts +3 -3
- package/dist/hooks/useDimensionalFilterRangeData.d.ts +4 -4
- package/dist/hooks/useGivensState.d.ts +64 -0
- package/dist/hooks/useModelGivens.d.ts +10 -0
- package/dist/hooks/useSuggestOptions.d.ts +78 -0
- package/dist/index-DvEanVgc.cjs.js +339 -0
- package/dist/{index-DRDu9kIV.es.js → index-txw04ZbN.es.js} +40531 -27964
- package/dist/index.cjs.js +1 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.es.js +79 -48
- package/dist/malloy-explorer.css +12 -1
- package/dist/sdk.css +1 -1
- package/dist/theme/ThemeContext.d.ts +45 -0
- package/dist/theme/buildMalloyExplicitTheme.d.ts +34 -0
- package/dist/theme/buildTableCssVars.d.ts +25 -0
- package/dist/theme/buildVegaThemeOverride.d.ts +16 -0
- package/dist/theme/defaults.d.ts +7 -0
- package/dist/theme/index.d.ts +8 -0
- package/dist/theme/keys.d.ts +20 -0
- package/dist/theme/readChartAnnotations.d.ts +23 -0
- package/dist/theme/resolveTheme.d.ts +22 -0
- package/dist/theme/types.d.ts +70 -0
- package/dist/utils/dataAppEmbed.d.ts +44 -0
- package/dist/utils/formatting.d.ts +1 -1
- package/dist/utils/malloyString.d.ts +7 -0
- package/dist/utils/parsing.d.ts +2 -2
- package/dist/utils/queryClient.d.ts +5 -0
- package/package.json +19 -8
- package/src/client-entry.ts +3 -0
- package/src/components/AnalyzePackageButton.tsx +9 -3
- package/src/components/ApiErrorDisplay.tsx +3 -0
- package/src/components/Connections/AddConnectionDialog.tsx +30 -2
- package/src/components/Connections/DeleteConnectionDialog.tsx +14 -3
- package/src/components/Connections/EditConnectionDialog.tsx +33 -4
- package/src/components/Connections/common.ts +64 -1
- package/src/components/DOMElement/DOMElement.tsx +3 -0
- package/src/components/DOMElement/index.ts +3 -0
- package/src/components/Dashboard/Dashboard.tsx +398 -0
- package/src/components/Dashboard/DashboardTile.tsx +153 -0
- package/src/components/Dashboard/index.ts +5 -0
- package/src/components/DataAppViewer/DataAppViewer.tsx +214 -0
- package/src/components/DataAppViewer/index.ts +4 -0
- package/src/components/{Project → Environment}/About.tsx +11 -4
- package/src/components/Environment/AddPackageDialog.tsx +202 -0
- package/src/components/{Project → Environment}/ConnectionExplorer.tsx +17 -14
- package/src/components/Environment/Connections.tsx +391 -0
- package/src/components/{Project → Environment}/DeletePackageDialog.tsx +8 -4
- package/src/components/{Project → Environment}/EditPackageDialog.tsx +21 -13
- package/src/components/Environment/Environment.tsx +89 -0
- package/src/components/Environment/Packages.tsx +206 -0
- package/src/components/Environment/index.ts +5 -0
- package/src/components/Home/{AddProjectDialog.tsx → AddEnvironmentDialog.tsx} +25 -19
- package/src/components/Home/{DeleteProjectDialog.tsx → DeleteEnvironmentDialog.tsx} +20 -15
- package/src/components/Home/{EditProjectDialog.tsx → EditEnvironmentDialog.tsx} +35 -28
- package/src/components/Home/Home.tsx +386 -408
- package/src/components/Home/index.ts +3 -0
- package/src/components/Loading.tsx +3 -0
- package/src/components/Materializations/CreateEnvMaterializationDialog.tsx +179 -0
- package/src/components/Materializations/CreateMaterializationDialog.tsx +112 -0
- package/src/components/Materializations/DeleteMaterializationDialog.tsx +109 -0
- package/src/components/Materializations/EnvironmentMaterializations.tsx +267 -0
- package/src/components/Materializations/ManifestView.tsx +87 -0
- package/src/components/Materializations/MaterializationDetailDialog.tsx +259 -0
- package/src/components/Materializations/MaterializationRunsList.tsx +256 -0
- package/src/components/Materializations/Materializations.tsx +355 -0
- package/src/components/Materializations/ScheduleCard.tsx +231 -0
- package/src/components/Materializations/SectionLabel.tsx +30 -0
- package/src/components/Materializations/SetScheduleDialog.tsx +162 -0
- package/src/components/Materializations/TriggerChip.tsx +44 -0
- package/src/components/Materializations/cron.ts +124 -0
- package/src/components/Materializations/index.ts +5 -0
- package/src/components/Materializations/utils.ts +145 -0
- package/src/components/Model/Model.tsx +44 -2
- package/src/components/Model/ModelCell.tsx +14 -5
- package/src/components/Model/ModelExplorer.tsx +3 -0
- package/src/components/Model/ModelExplorerDialog.tsx +4 -1
- package/src/components/Model/SourcesExplorer.tsx +9 -6
- package/src/components/Model/index.ts +3 -0
- package/src/components/Model/useModelData.ts +6 -3
- package/src/components/Notebook/Notebook.tsx +475 -339
- package/src/components/Notebook/NotebookCell.tsx +264 -25
- package/src/components/Notebook/index.ts +3 -0
- package/src/components/Notebook/runPlan.spec.ts +155 -0
- package/src/components/Notebook/runPlan.ts +67 -0
- package/src/components/Notebook/types.ts +14 -0
- package/src/components/Package/ContentTypeIcon.spec.ts +91 -0
- package/src/components/Package/ContentTypeIcon.tsx +177 -0
- package/src/components/Package/FileTreeView.tsx +3 -0
- package/src/components/Package/Models.tsx +7 -4
- package/src/components/Package/Package.tsx +666 -59
- package/src/components/Package/index.ts +3 -0
- package/src/components/QueryResult/QueryResult.tsx +17 -8
- package/src/components/QueryResult/index.ts +3 -0
- package/src/components/RenderedResult/RenderedResult.tsx +685 -67
- package/src/components/RenderedResult/ResultContainer.tsx +71 -2
- package/src/components/RenderedResult/index.ts +3 -0
- package/src/components/RenderedResult/renderLogs.spec.ts +58 -0
- package/src/components/RenderedResult/renderLogs.ts +43 -0
- package/src/components/ResultsDialog.tsx +13 -1
- package/src/components/ServerProvider.tsx +175 -51
- package/src/components/Workbook/BrowserWorkbookStorage.ts +3 -0
- package/src/components/Workbook/EditableMalloyCell.tsx +3 -0
- package/src/components/Workbook/ModelPicker.tsx +7 -4
- package/src/components/Workbook/MutableCell.tsx +10 -5
- package/src/components/Workbook/Workbook.tsx +8 -5
- package/src/components/Workbook/WorkbookList.tsx +3 -0
- package/src/components/Workbook/WorkbookManager.ts +3 -0
- package/src/components/Workbook/WorkbookStorage.ts +3 -0
- package/src/components/Workbook/WorkbookStorageProvider.tsx +3 -0
- package/src/components/Workbook/index.ts +3 -0
- package/src/components/click_helper.ts +17 -1
- package/src/components/drill/index.ts +28 -0
- package/src/components/drill/markDrillableCells.dom.spec.ts +313 -0
- package/src/components/drill/markDrillableCells.spec.ts +109 -0
- package/src/components/drill/markDrillableCells.ts +314 -0
- package/src/components/drill/resolveDrill.spec.ts +449 -0
- package/src/components/drill/resolveDrill.ts +375 -0
- package/src/components/drill/useDrill.tsx +203 -0
- package/src/components/filter/DimensionFilter.tsx +3 -0
- package/src/components/filter/index.ts +3 -0
- package/src/components/filter/utils.ts +16 -5
- package/src/components/given/GivenInput.spec.tsx +689 -0
- package/src/components/given/GivenInput.tsx +801 -0
- package/src/components/given/GivensPanel.spec.tsx +175 -0
- package/src/components/given/GivensPanel.tsx +195 -0
- package/src/components/given/filterValue.spec.ts +364 -0
- package/src/components/given/filterValue.ts +207 -0
- package/src/components/given/index.ts +9 -0
- package/src/components/given/paramCodec.spec.ts +857 -0
- package/src/components/given/paramCodec.ts +483 -0
- package/src/components/given/utils.spec.ts +60 -0
- package/src/components/given/utils.ts +79 -0
- package/src/components/highlighter.ts +22 -5
- package/src/components/index.ts +10 -2
- package/src/components/styles.ts +82 -33
- package/src/constants/docLinks.spec.ts +54 -0
- package/src/constants/docLinks.ts +46 -0
- package/src/hooks/givenValue.ts +13 -0
- package/src/hooks/index.ts +13 -0
- package/src/hooks/useDimensionFilters.ts +4 -7
- package/src/hooks/useDimensionFiltersFromSpec.ts +8 -5
- package/src/hooks/useDimensionFiltersQuery.ts +10 -13
- package/src/hooks/useDimensionalFilterRangeData.ts +11 -14
- package/src/hooks/useGivensState.spec.ts +449 -0
- package/src/hooks/useGivensState.ts +340 -0
- package/src/hooks/useModelGivens.ts +29 -0
- package/src/hooks/useQueryWithApiError.ts +3 -0
- package/src/hooks/useRawQueryData.ts +6 -3
- package/src/hooks/useSuggestOptions.spec.ts +112 -0
- package/src/hooks/useSuggestOptions.ts +263 -0
- package/src/index.ts +10 -1
- package/src/react-app-env.d.ts +3 -0
- package/src/theme/ThemeContext.tsx +86 -0
- package/src/theme/buildMalloyExplicitTheme.ts +61 -0
- package/src/theme/buildTableCssVars.spec.ts +67 -0
- package/src/theme/buildTableCssVars.ts +67 -0
- package/src/theme/buildVegaThemeOverride.spec.ts +56 -0
- package/src/theme/buildVegaThemeOverride.ts +47 -0
- package/src/theme/defaults.ts +78 -0
- package/src/theme/index.ts +11 -0
- package/src/theme/keys.ts +32 -0
- package/src/theme/readChartAnnotations.spec.ts +58 -0
- package/src/theme/readChartAnnotations.ts +69 -0
- package/src/theme/resolveTheme.spec.ts +160 -0
- package/src/theme/resolveTheme.ts +121 -0
- package/src/theme/types.ts +79 -0
- package/src/utils/dataAppEmbed.spec.ts +63 -0
- package/src/utils/dataAppEmbed.ts +84 -0
- package/src/utils/formatting.spec.ts +25 -21
- package/src/utils/formatting.ts +11 -7
- package/src/utils/malloyString.spec.ts +24 -0
- package/src/utils/malloyString.ts +12 -0
- package/src/utils/parsing.spec.ts +26 -19
- package/src/utils/parsing.ts +11 -6
- package/src/utils/queryClient.ts +17 -0
- package/test/README.md +141 -0
- package/test/setup.ts +29 -0
- package/tsconfig.json +12 -1
- package/tsconfig.spec.json +22 -0
- package/vite.config.ts +3 -0
- package/dist/ServerProvider-lOQXDlqB.cjs.js +0 -1
- package/dist/ServerProvider-on-8FH5Z.es.js +0 -3060
- package/dist/components/Home/AddProjectDialog.d.ts +0 -1
- package/dist/components/Home/DeleteProjectDialog.d.ts +0 -5
- package/dist/components/Home/EditProjectDialog.d.ts +0 -7
- package/dist/components/Package/Config.d.ts +0 -5
- package/dist/components/Package/Databases.d.ts +0 -5
- package/dist/components/Package/Notebooks.d.ts +0 -6
- package/dist/components/Project/Project.d.ts +0 -6
- package/dist/components/Project/index.d.ts +0 -2
- package/dist/index-BOLBP6_i.cjs.js +0 -233
- package/src/components/Package/Config.tsx +0 -97
- package/src/components/Package/Connections.tsx +0 -373
- package/src/components/Package/Databases.tsx +0 -228
- package/src/components/Package/Notebooks.tsx +0 -77
- package/src/components/Project/AddPackageDialog.tsx +0 -159
- package/src/components/Project/Packages.tsx +0 -191
- package/src/components/Project/Project.tsx +0 -52
- package/src/components/Project/index.ts +0 -2
- /package/dist/components/{Project → Environment}/About.d.ts +0 -0
- /package/dist/components/{Project → Environment}/AddPackageDialog.d.ts +0 -0
- /package/dist/components/{Project → Environment}/ConnectionExplorer.d.ts +0 -0
- /package/dist/components/{Package → Environment}/Connections.d.ts +0 -0
- /package/dist/components/{Project → Environment}/DeletePackageDialog.d.ts +0 -0
- /package/dist/components/{Project → Environment}/EditPackageDialog.d.ts +0 -0
- /package/dist/components/{Project → Environment}/Packages.d.ts +0 -0
|
@@ -1,7 +1,49 @@
|
|
|
1
|
-
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { Box } from "@mui/material";
|
|
5
|
+
import React, {
|
|
6
|
+
Suspense,
|
|
7
|
+
useCallback,
|
|
8
|
+
useEffect,
|
|
9
|
+
useLayoutEffect,
|
|
10
|
+
useRef,
|
|
11
|
+
} from "react";
|
|
12
|
+
import { buildMalloyExplicitTheme } from "../../theme/buildMalloyExplicitTheme";
|
|
13
|
+
import { buildTableCssVars } from "../../theme/buildTableCssVars";
|
|
14
|
+
import { buildVegaThemeOverride } from "../../theme/buildVegaThemeOverride";
|
|
15
|
+
import { readChartAnnotations } from "../../theme/readChartAnnotations";
|
|
16
|
+
import { resolveTheme } from "../../theme/resolveTheme";
|
|
17
|
+
import { usePublisherTheme } from "../../theme/ThemeContext";
|
|
18
|
+
import type { ResolvedTheme } from "../../theme/types";
|
|
19
|
+
import {
|
|
20
|
+
DRILL_CELL_CLASS,
|
|
21
|
+
moveDrillStop,
|
|
22
|
+
drillableFieldNames,
|
|
23
|
+
markDrillableCells,
|
|
24
|
+
type DrillMetadataSource,
|
|
25
|
+
} from "../drill/markDrillableCells";
|
|
26
|
+
import type { DrillClickPayload } from "../drill/resolveDrill";
|
|
27
|
+
import type { DrillBinding } from "../drill/useDrill";
|
|
2
28
|
|
|
3
29
|
type MalloyRenderElement = HTMLElement & Record<string, unknown>;
|
|
4
30
|
|
|
31
|
+
/**
|
|
32
|
+
* Loose alias for the {@link MalloyViz} instance returned by createViz().
|
|
33
|
+
* Avoids pinning the SDK to a specific class import; we only call methods
|
|
34
|
+
* declared on the renderer's public type. Keep in sync with
|
|
35
|
+
* `@malloydata/render` if more methods are needed.
|
|
36
|
+
*/
|
|
37
|
+
interface MalloyVizHandle extends DrillMetadataSource {
|
|
38
|
+
setResult: (result: unknown) => void;
|
|
39
|
+
render: (element: HTMLElement) => void;
|
|
40
|
+
remove: () => void;
|
|
41
|
+
// Fires once the chart has painted (or immediately if it already has). We
|
|
42
|
+
// use it to swap a freshly-rendered chart in only when it's ready, so the
|
|
43
|
+
// previously-painted chart never blanks out mid-render.
|
|
44
|
+
onReady: (callback: () => void) => void;
|
|
45
|
+
}
|
|
46
|
+
|
|
5
47
|
declare global {
|
|
6
48
|
// eslint-disable-next-line @typescript-eslint/no-namespace
|
|
7
49
|
namespace JSX {
|
|
@@ -19,61 +61,331 @@ interface RenderedResultProps {
|
|
|
19
61
|
height?: number;
|
|
20
62
|
isFillElement?: (boolean) => void;
|
|
21
63
|
onSizeChange?: (height: number) => void;
|
|
22
|
-
|
|
64
|
+
/**
|
|
65
|
+
* Makes `# drill` cells clickable, and makes them look it. From `useDrill`
|
|
66
|
+
* on the host surface, which decides where a click may go; omitted where
|
|
67
|
+
* nothing can act on one, and then the result renders inert.
|
|
68
|
+
*/
|
|
69
|
+
drill?: DrillBinding;
|
|
23
70
|
}
|
|
24
71
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
72
|
+
const createRenderer = async (
|
|
73
|
+
theme: ResolvedTheme,
|
|
74
|
+
onClick?: DrillBinding["onClick"],
|
|
75
|
+
): Promise<MalloyVizHandle> => {
|
|
28
76
|
if (typeof window === "undefined") {
|
|
29
77
|
throw new Error("MalloyRenderer can only be used in browser environment");
|
|
30
78
|
}
|
|
31
79
|
|
|
32
80
|
const { MalloyRenderer } = await import("@malloydata/render");
|
|
33
81
|
const renderer = new MalloyRenderer({
|
|
34
|
-
onClick
|
|
82
|
+
onClick,
|
|
83
|
+
vegaConfigOverride: buildVegaThemeOverride(theme),
|
|
84
|
+
// Pass the explicit theme so table chrome and dashboard tiles
|
|
85
|
+
// pick up the operator's colours directly. Without this the
|
|
86
|
+
// renderer's own inline style writes `--malloy-render--*` values
|
|
87
|
+
// sourced from its built-in defaults and shadows whatever we set
|
|
88
|
+
// on the outer wrapper.
|
|
89
|
+
theme: buildMalloyExplicitTheme(theme),
|
|
35
90
|
onError: (error) => {
|
|
36
91
|
console.error("Error rendering visualization:", typeof error, error);
|
|
37
92
|
},
|
|
38
93
|
});
|
|
39
|
-
return renderer.createViz();
|
|
94
|
+
return renderer.createViz() as MalloyVizHandle;
|
|
40
95
|
};
|
|
41
96
|
|
|
42
|
-
//
|
|
97
|
+
// Warm the renderer chunk as soon as this module loads so the first chart
|
|
98
|
+
// paint doesn't have to wait on the dynamic import resolving (the async
|
|
99
|
+
// import is what widened the clear-then-repaint gap into a visible flicker).
|
|
100
|
+
if (typeof window !== "undefined") {
|
|
101
|
+
void import("@malloydata/render");
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Pull a per-chart Theme override out of a parsed Malloy result by reading
|
|
106
|
+
* `# theme.*` annotations on the model and the query. Returns `undefined`
|
|
107
|
+
* if no theme annotations are present, the shape is unexpected, or the
|
|
108
|
+
* annotation parser is unavailable at runtime.
|
|
109
|
+
*
|
|
110
|
+
* Three failure modes are kept distinct so a malformed annotation doesn't
|
|
111
|
+
* masquerade as a missing dependency:
|
|
112
|
+
*
|
|
113
|
+
* 1. No `theme.*` annotations in the result — return undefined silently.
|
|
114
|
+
* 2. `@malloydata/malloy-tag` not resolvable at runtime — return undefined
|
|
115
|
+
* silently. SDK consumers may bundle without it; the chart still renders.
|
|
116
|
+
* 3. Parser threw on a malformed annotation — return undefined AND log a
|
|
117
|
+
* `console.warn` with the offending lines so authors see the typo.
|
|
118
|
+
*/
|
|
119
|
+
async function extractChartThemeOverride(parsed: unknown) {
|
|
120
|
+
if (!parsed || typeof parsed !== "object") return undefined;
|
|
121
|
+
const r = parsed as {
|
|
122
|
+
annotations?: Array<{ value?: string }>;
|
|
123
|
+
model_annotations?: Array<{ value?: string }>;
|
|
124
|
+
source_annotations?: Array<{ value?: string }>;
|
|
125
|
+
};
|
|
126
|
+
const lines = [
|
|
127
|
+
...(Array.isArray(r.model_annotations) ? r.model_annotations : []),
|
|
128
|
+
...(Array.isArray(r.source_annotations) ? r.source_annotations : []),
|
|
129
|
+
...(Array.isArray(r.annotations) ? r.annotations : []),
|
|
130
|
+
]
|
|
131
|
+
.map((a) => (typeof a?.value === "string" ? a.value : undefined))
|
|
132
|
+
.filter((s): s is string => Boolean(s));
|
|
133
|
+
if (lines.length === 0) return undefined;
|
|
134
|
+
|
|
135
|
+
let parseAnnotation: typeof import("@malloydata/malloy-tag").parseAnnotation;
|
|
136
|
+
try {
|
|
137
|
+
({ parseAnnotation } = await import("@malloydata/malloy-tag"));
|
|
138
|
+
} catch {
|
|
139
|
+
// Missing peer dep is an acceptable fallback. Charts render with the
|
|
140
|
+
// shell theme only.
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
try {
|
|
145
|
+
const { tag } = parseAnnotation(lines);
|
|
146
|
+
return readChartAnnotations(tag);
|
|
147
|
+
} catch (error) {
|
|
148
|
+
console.warn(
|
|
149
|
+
"Failed to parse # theme.* annotations; chart will render with the shell theme.",
|
|
150
|
+
{ error, lines },
|
|
151
|
+
);
|
|
152
|
+
return undefined;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function applyTableCssVars(element: HTMLElement, theme: ResolvedTheme): void {
|
|
157
|
+
const vars = buildTableCssVars(theme);
|
|
158
|
+
for (const [key, value] of Object.entries(vars)) {
|
|
159
|
+
element.style.setProperty(key, value);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* CSS overrides for the renderer's own hardcoded colours.
|
|
165
|
+
*
|
|
166
|
+
* These are appended as a `<style>` in `document.head` by
|
|
167
|
+
* `injectRendererOverrides` below, which is the mechanism actually in use and
|
|
168
|
+
* is described where it happens. An earlier version of this block said the
|
|
169
|
+
* renderer puts its DOM in a Shadow Root and that we register through
|
|
170
|
+
* `MalloyViz.addStylesheet()`. Neither is true of this code: nothing here calls
|
|
171
|
+
* that API, and were the boundary real a `document.head` stylesheet could not
|
|
172
|
+
* reach `.malloy-dashboard` at all, which is precisely what these rules do and
|
|
173
|
+
* what the theming tests check. Selectors are written at higher specificity
|
|
174
|
+
* than the renderer's own so they win regardless of stylesheet order.
|
|
175
|
+
*/
|
|
176
|
+
const PUBLISHER_RENDERER_OVERRIDES_CSS = `
|
|
177
|
+
/* dashboard.css hardcodes background: #f7f9fc on .malloy-dashboard
|
|
178
|
+
and .dashboard-row-header, which would paint light grey in dark
|
|
179
|
+
mode and also bleed an operator-picked palette.background across
|
|
180
|
+
the panel chrome in light. Both surfaces use
|
|
181
|
+
--malloy-render--background here, which buildMalloyExplicitTheme
|
|
182
|
+
wires to dashboardRoot: a mode-keyed neutral that stays decoupled
|
|
183
|
+
from palette.background (the chart canvas) on purpose. Selectors
|
|
184
|
+
are duplicated at higher specificity to beat the renderer s own
|
|
185
|
+
scoped rules. */
|
|
186
|
+
.malloy-render .malloy-dashboard,
|
|
187
|
+
.malloy-render.malloy-render .malloy-dashboard,
|
|
188
|
+
div.malloy-render .malloy-dashboard {
|
|
189
|
+
/* --publisher-dashboard-root is set on the outer Publisher wrapper
|
|
190
|
+
(see buildTableCssVars) and stays decoupled from
|
|
191
|
+
--malloy-render--background, which gets shadowed inside the
|
|
192
|
+
renderer when a theme.background annotation is present. The
|
|
193
|
+
dedicated var means the panel between tiles always paints the
|
|
194
|
+
operator neutral dashboardRoot regardless of annotations. */
|
|
195
|
+
background: var(--publisher-dashboard-root) !important;
|
|
196
|
+
background-color: var(--publisher-dashboard-root) !important;
|
|
197
|
+
color: var(--malloy-render--table-body-color) !important;
|
|
198
|
+
}
|
|
199
|
+
.malloy-render .malloy-dashboard .dashboard-row,
|
|
200
|
+
.malloy-render .malloy-dashboard .dashboard-row-body,
|
|
201
|
+
.malloy-render .malloy-dashboard .dashboard-row-header,
|
|
202
|
+
.malloy-render.malloy-render .malloy-dashboard .dashboard-row-header,
|
|
203
|
+
div.malloy-render .malloy-dashboard .dashboard-row-header {
|
|
204
|
+
/* Belt + suspenders: dashboard.css hardcodes .dashboard-row-header
|
|
205
|
+
to #f7f9fc, and row / row-body have no explicit background but
|
|
206
|
+
get caught in any annotation-driven inline writes. Force all of
|
|
207
|
+
them to the neutral panel colour. */
|
|
208
|
+
background: var(--publisher-dashboard-root) !important;
|
|
209
|
+
background-color: var(--publisher-dashboard-root) !important;
|
|
210
|
+
}
|
|
211
|
+
.malloy-render .malloy-dashboard .dashboard-item {
|
|
212
|
+
/* Tile padding around each chart / table. Uses our custom
|
|
213
|
+
--malloy-render--tile-background so the operator can theme the
|
|
214
|
+
tile separately from the table header row (which paints from
|
|
215
|
+
--malloy-render--table-pinned-background below). */
|
|
216
|
+
background: var(--malloy-render--tile-background) !important;
|
|
217
|
+
color: var(--malloy-render--table-body-color) !important;
|
|
218
|
+
box-shadow: none !important;
|
|
219
|
+
border: var(--malloy-render--table-border) !important;
|
|
220
|
+
}
|
|
221
|
+
.malloy-render .malloy-dashboard .dashboard-row-header-separator {
|
|
222
|
+
background: var(--malloy-render--table-border) !important;
|
|
223
|
+
}
|
|
224
|
+
.malloy-render .malloy-table .th.column-cell {
|
|
225
|
+
/* Non-pinned tables have no header background in the renderer's
|
|
226
|
+
own CSS (only pinned scrolled tables paint the pinned-header
|
|
227
|
+
with this var). Force the same value here so every table has a
|
|
228
|
+
visible header band reflecting the operator's choice. */
|
|
229
|
+
background: var(--malloy-render--table-pinned-background) !important;
|
|
230
|
+
}
|
|
231
|
+
.malloy-render .dashboard-item-title,
|
|
232
|
+
.malloy-render .dashboard-dimension-name {
|
|
233
|
+
color: var(--malloy-render--label-color) !important;
|
|
234
|
+
}
|
|
235
|
+
.malloy-render .dashboard-item-value,
|
|
236
|
+
.malloy-render .dashboard-item-value-measure,
|
|
237
|
+
.malloy-render .dashboard-dimension-value {
|
|
238
|
+
color: var(--malloy-render--value-color) !important;
|
|
239
|
+
}
|
|
240
|
+
.malloy-render .malloy-table,
|
|
241
|
+
.malloy-render .malloy-list {
|
|
242
|
+
color: var(--malloy-render--table-body-color) !important;
|
|
243
|
+
}
|
|
244
|
+
.malloy-render .column-cell.th,
|
|
245
|
+
.malloy-render .cell-content.header {
|
|
246
|
+
color: var(--malloy-render--table-header-color) !important;
|
|
247
|
+
}
|
|
248
|
+
/* A cell whose column declares a "# drill" this surface can honor, marked
|
|
249
|
+
by markDrillableCells. Ordinary text with a pointer cursor until hovered,
|
|
250
|
+
then it reads as a link: the same affordance Malloyyo gives the same tag,
|
|
251
|
+
and the only thing that tells a reader a cell navigates. Kept off the
|
|
252
|
+
resting state deliberately: a column painted blue competes with the data.
|
|
253
|
+
(No backticks in here: this is inside a template literal.) */
|
|
254
|
+
.malloy-render .${DRILL_CELL_CLASS} {
|
|
255
|
+
cursor: pointer;
|
|
256
|
+
}
|
|
257
|
+
.malloy-render .${DRILL_CELL_CLASS}:hover > .cell-content {
|
|
258
|
+
color: var(--publisher-drill-link) !important;
|
|
259
|
+
text-decoration: underline;
|
|
260
|
+
}
|
|
261
|
+
/* Keyboard focus reads the same as hover, plus a ring: a hover colour is not
|
|
262
|
+
something a keyboard user can produce, and a focus ring is not something a
|
|
263
|
+
mouse user sees. focus-visible rather than focus, so a click does not leave a
|
|
264
|
+
ring behind it. (No backticks in here: this is inside a template literal.) */
|
|
265
|
+
.malloy-render .${DRILL_CELL_CLASS}:focus-visible {
|
|
266
|
+
outline: 2px solid var(--publisher-drill-link);
|
|
267
|
+
outline-offset: -2px;
|
|
268
|
+
}
|
|
269
|
+
.malloy-render .${DRILL_CELL_CLASS}:focus-visible > .cell-content {
|
|
270
|
+
color: var(--publisher-drill-link) !important;
|
|
271
|
+
text-decoration: underline;
|
|
272
|
+
}
|
|
273
|
+
`;
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Append the overrides as a `<style>` in `document.head`. The renderer
|
|
277
|
+
* adds its own styles the same way (`MalloyViz.addStylesheet` just calls
|
|
278
|
+
* `document.head.appendChild(<style>)`), so this puts our rules in the
|
|
279
|
+
* same cascade. Our selectors are written at higher specificity than the
|
|
280
|
+
* renderer's nested `.malloy-dashboard .dashboard-item`, with !important,
|
|
281
|
+
* so they win whether they land before or after the renderer's stylesheet.
|
|
282
|
+
*
|
|
283
|
+
* Idempotent: the style element is keyed by id, so re-mounts skip
|
|
284
|
+
* re-injection.
|
|
285
|
+
*/
|
|
286
|
+
const PUBLISHER_RENDERER_OVERRIDE_ID = "publisher-malloy-renderer-overrides";
|
|
287
|
+
function injectRendererOverrides(): void {
|
|
288
|
+
if (typeof document === "undefined") return;
|
|
289
|
+
if (document.getElementById(PUBLISHER_RENDERER_OVERRIDE_ID)) return;
|
|
290
|
+
const style = document.createElement("style");
|
|
291
|
+
style.id = PUBLISHER_RENDERER_OVERRIDE_ID;
|
|
292
|
+
style.textContent = PUBLISHER_RENDERER_OVERRIDES_CSS;
|
|
293
|
+
document.head.appendChild(style);
|
|
294
|
+
}
|
|
295
|
+
|
|
43
296
|
function RenderedResultInner({
|
|
44
297
|
result,
|
|
45
298
|
height: inputHeight,
|
|
46
|
-
|
|
299
|
+
drill,
|
|
47
300
|
onSizeChange,
|
|
48
301
|
}: RenderedResultProps) {
|
|
49
302
|
const ref = useRef<HTMLDivElement>(null);
|
|
303
|
+
// The renderer binds its click handler at construction, so a changing
|
|
304
|
+
// `drill` identity would otherwise re-run the render effect and rebuild the
|
|
305
|
+
// chart: undoing the no-flicker swap below every time the parent
|
|
306
|
+
// re-renders. Read the latest binding through a ref instead, and keep
|
|
307
|
+
// `drill` out of the effect's dependencies, so callers can pass an inline
|
|
308
|
+
// one safely.
|
|
309
|
+
const drillRef = useRef(drill);
|
|
310
|
+
drillRef.current = drill;
|
|
311
|
+
const handleDrillClick = useCallback((payload: DrillClickPayload) => {
|
|
312
|
+
drillRef.current?.onClick(payload);
|
|
313
|
+
}, []);
|
|
314
|
+
// Whether any drill is wired at all, which is what the effect reacts to: a
|
|
315
|
+
// surface that gains or loses the capability has to (re)mark its cells, but
|
|
316
|
+
// a new inline binding with the same capability must not rebuild anything.
|
|
317
|
+
//
|
|
318
|
+
// KNOWN LIMITATION, and the narrower alternative is worse. `canDrill` decides
|
|
319
|
+
// WHICH cells get marked and is read once per render pass, so a predicate
|
|
320
|
+
// that changes while the result stays put leaves the previous pass's marks in
|
|
321
|
+
// place. Depending on the predicate's identity instead was tried and reverted:
|
|
322
|
+
// `canDrill` chains back to the notebook's parameters, so every control edit
|
|
323
|
+
// tore down and rebuilt every chart, which is the flicker this component was
|
|
324
|
+
// fixed for. The stale case needs a host whose predicate resolves after first
|
|
325
|
+
// paint against an unchanged result; it cannot happen in the notebook, where
|
|
326
|
+
// anything that changes the declared givens also rebuilds the cells. Fixing
|
|
327
|
+
// it properly means re-marking without re-rendering, which is a change to the
|
|
328
|
+
// marking pass rather than to this dependency list.
|
|
329
|
+
const drillable = drill !== undefined;
|
|
50
330
|
const hasMeasuredRef = useRef(false);
|
|
331
|
+
// The chart currently painted into the container, held across renders. A
|
|
332
|
+
// new render paints into a fresh offscreen stage and only swaps in (and
|
|
333
|
+
// disposes this one) once it's ready, so the container never blanks between
|
|
334
|
+
// charts. That up-front clear + cleanup dispose was the flicker.
|
|
335
|
+
const liveRef = useRef<{ viz: MalloyVizHandle; node: HTMLElement } | null>(
|
|
336
|
+
null,
|
|
337
|
+
);
|
|
338
|
+
// Bumped on every render-effect run and on unmount. A slow async render that
|
|
339
|
+
// resolves after a newer one has started (or after unmount) checks this and
|
|
340
|
+
// bails, so overlapping renders can't leave two charts or leak a viz.
|
|
341
|
+
const renderGenRef = useRef(0);
|
|
342
|
+
const { theme: baseTheme, layers, mode } = usePublisherTheme();
|
|
343
|
+
|
|
344
|
+
// Dispose the last live viz on unmount only. Deliberately NOT done in the
|
|
345
|
+
// render effect's cleanup: a re-run must keep the old chart until the new
|
|
346
|
+
// one has painted, and the new render disposes it during the swap.
|
|
347
|
+
useEffect(() => {
|
|
348
|
+
return () => {
|
|
349
|
+
renderGenRef.current += 1;
|
|
350
|
+
liveRef.current?.viz.remove();
|
|
351
|
+
liveRef.current = null;
|
|
352
|
+
};
|
|
353
|
+
}, []);
|
|
51
354
|
|
|
52
|
-
// Render the visualization once the component mounts
|
|
53
355
|
useLayoutEffect(() => {
|
|
54
356
|
if (!ref.current || !result) return;
|
|
55
357
|
|
|
56
|
-
|
|
57
|
-
const element = ref.current;
|
|
358
|
+
injectRendererOverrides();
|
|
58
359
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
//
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
360
|
+
const element = ref.current;
|
|
361
|
+
const myGen = (renderGenRef.current += 1);
|
|
362
|
+
let cancelled = false;
|
|
363
|
+
const isCurrent = () => !cancelled && myGen === renderGenRef.current;
|
|
364
|
+
// Created inside the async body; hoisted so cleanup can tear them down
|
|
365
|
+
// if this render is superseded before it swaps itself into `liveRef`.
|
|
366
|
+
let stage: HTMLDivElement | undefined;
|
|
367
|
+
let viz: MalloyVizHandle | undefined;
|
|
368
|
+
let drillObserver: MutationObserver | null = null;
|
|
369
|
+
let drillFrame = 0;
|
|
370
|
+
let drillKeydown: ((event: KeyboardEvent) => void) | null = null;
|
|
371
|
+
let drillKeyup: ((event: KeyboardEvent) => void) | null = null;
|
|
372
|
+
let drillFocusIn: ((event: FocusEvent) => void) | null = null;
|
|
68
373
|
let observer: MutationObserver | null = null;
|
|
69
374
|
let measureTimeout: NodeJS.Timeout | null = null;
|
|
375
|
+
// Safety net so a render that never signals ready (an async renderer
|
|
376
|
+
// error that only reaches onError) can't leave a previous chart showing
|
|
377
|
+
// stale data forever; see the setTimeout below.
|
|
378
|
+
let readyFallback: ReturnType<typeof setTimeout> | null = null;
|
|
70
379
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
380
|
+
hasMeasuredRef.current = false;
|
|
381
|
+
|
|
382
|
+
// Measure the rendered chart's natural height off `root` (the stage that
|
|
383
|
+
// wraps the renderer output) and report it up. Same grandchild/dashboard
|
|
384
|
+
// HACK as before, just anchored on the stage wrapper.
|
|
385
|
+
const measureRenderedSize = (root: HTMLElement) => {
|
|
386
|
+
if (hasMeasuredRef.current || cancelled || !root.firstElementChild)
|
|
74
387
|
return;
|
|
75
|
-
|
|
76
|
-
const child = element.firstElementChild as HTMLElement;
|
|
388
|
+
const child = root.firstElementChild as HTMLElement;
|
|
77
389
|
const grandchild = child.firstElementChild as HTMLElement;
|
|
78
390
|
if (!grandchild) return;
|
|
79
391
|
const greatgrandchild = grandchild.firstElementChild as HTMLElement;
|
|
@@ -99,45 +411,353 @@ function RenderedResultInner({
|
|
|
99
411
|
}
|
|
100
412
|
};
|
|
101
413
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
observer.observe(element, {
|
|
118
|
-
childList: true,
|
|
119
|
-
subtree: true,
|
|
120
|
-
attributes: true,
|
|
121
|
-
});
|
|
414
|
+
(async () => {
|
|
415
|
+
let parsed: unknown;
|
|
416
|
+
try {
|
|
417
|
+
parsed = JSON.parse(result);
|
|
418
|
+
} catch (error) {
|
|
419
|
+
console.error("Error parsing visualization result:", error);
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
const perChart = await extractChartThemeOverride(parsed);
|
|
424
|
+
const effectiveTheme = perChart
|
|
425
|
+
? resolveTheme([...layers, perChart], mode)
|
|
426
|
+
: baseTheme;
|
|
122
427
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
observer?.disconnect();
|
|
129
|
-
}
|
|
130
|
-
})
|
|
131
|
-
.catch((error) => {
|
|
428
|
+
if (!isCurrent()) return;
|
|
429
|
+
|
|
430
|
+
try {
|
|
431
|
+
viz = await createRenderer(effectiveTheme, handleDrillClick);
|
|
432
|
+
} catch (error) {
|
|
132
433
|
console.error("Failed to create renderer:", error);
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
if (!isCurrent()) {
|
|
437
|
+
// Superseded during the dynamic import / construction.
|
|
438
|
+
viz.remove();
|
|
439
|
+
viz = undefined;
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// Render into a fresh stage appended to the container. While a
|
|
444
|
+
// previous chart is still on screen, keep the stage laid out (so the
|
|
445
|
+
// fill chart actually measures a size and `onReady` fires) but hidden
|
|
446
|
+
// and overlaid; reveal it and drop the old chart only once painted.
|
|
447
|
+
const previous = liveRef.current;
|
|
448
|
+
stage = document.createElement("div");
|
|
449
|
+
stage.style.width = "100%";
|
|
450
|
+
stage.style.height = "100%";
|
|
451
|
+
// Theme the chart's table/dashboard chrome on the stage itself, not on
|
|
452
|
+
// the shared container: during a swap the outgoing chart is still a
|
|
453
|
+
// child of the container, so writing the new per-chart CSS vars there
|
|
454
|
+
// would repaint the old chart's chrome to the new theme before it is
|
|
455
|
+
// swapped out. Scoping the vars to this stage keeps each chart stable.
|
|
456
|
+
applyTableCssVars(stage, effectiveTheme);
|
|
457
|
+
if (previous) {
|
|
458
|
+
element.style.position = "relative";
|
|
459
|
+
stage.style.position = "absolute";
|
|
460
|
+
stage.style.inset = "0";
|
|
461
|
+
stage.style.visibility = "hidden";
|
|
462
|
+
}
|
|
463
|
+
element.appendChild(stage);
|
|
464
|
+
|
|
465
|
+
// Fallback measurement if `onReady` never fires: settle on DOM
|
|
466
|
+
// mutations, then measure once.
|
|
467
|
+
const stageNode = stage;
|
|
468
|
+
observer = new MutationObserver(() => {
|
|
469
|
+
if (measureTimeout) clearTimeout(measureTimeout);
|
|
470
|
+
measureTimeout = setTimeout(() => {
|
|
471
|
+
measureRenderedSize(stageNode);
|
|
472
|
+
observer?.disconnect();
|
|
473
|
+
}, 100);
|
|
474
|
+
});
|
|
475
|
+
observer.observe(stage, {
|
|
476
|
+
childList: true,
|
|
477
|
+
subtree: true,
|
|
478
|
+
attributes: true,
|
|
133
479
|
});
|
|
134
480
|
|
|
481
|
+
const activeViz = viz;
|
|
482
|
+
let promoted = false;
|
|
483
|
+
const promote = () => {
|
|
484
|
+
if (promoted || !isCurrent()) return;
|
|
485
|
+
promoted = true;
|
|
486
|
+
if (readyFallback) {
|
|
487
|
+
clearTimeout(readyFallback);
|
|
488
|
+
readyFallback = null;
|
|
489
|
+
}
|
|
490
|
+
// The new chart has painted; drop the outgoing one now.
|
|
491
|
+
if (previous) {
|
|
492
|
+
previous.viz.remove();
|
|
493
|
+
if (previous.node.parentNode === element) {
|
|
494
|
+
element.removeChild(previous.node);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
stageNode.style.position = "";
|
|
498
|
+
stageNode.style.inset = "";
|
|
499
|
+
stageNode.style.visibility = "";
|
|
500
|
+
element.style.position = "";
|
|
501
|
+
liveRef.current = { viz: activeViz, node: stageNode };
|
|
502
|
+
measureRenderedSize(stageNode);
|
|
503
|
+
};
|
|
504
|
+
|
|
505
|
+
try {
|
|
506
|
+
// The renderer accepts a Malloy Result; we don't import that type
|
|
507
|
+
// in the SDK to avoid pinning to the malloy core types here.
|
|
508
|
+
viz.setResult(parsed);
|
|
509
|
+
viz.render(stage);
|
|
510
|
+
|
|
511
|
+
// Mark the cells a `# drill` makes clickable, so they read as
|
|
512
|
+
// links. The names come from the result's field metadata, which is
|
|
513
|
+
// complete as soon as it is set, but the DOM is not: a
|
|
514
|
+
// `# dashboard` result builds its cards over later frames, so a
|
|
515
|
+
// one-shot pass right after render() would miss every table that
|
|
516
|
+
// appears after it. Hence the observer, which re-marks (batched to
|
|
517
|
+
// a frame) as cards arrive.
|
|
518
|
+
const binding = drillRef.current;
|
|
519
|
+
// Isolated from the render above. The chart has already rendered by
|
|
520
|
+
// this point, and a throw in here would reach the same catch and
|
|
521
|
+
// replace a working result with an error message: the affordance
|
|
522
|
+
// would be taking the chart down with it. `drillableFieldNames`
|
|
523
|
+
// already guards its own metadata read on the same principle.
|
|
524
|
+
try {
|
|
525
|
+
if (binding) {
|
|
526
|
+
const names = drillableFieldNames(viz, binding.canDrill);
|
|
527
|
+
// Guarded INSIDE, not just at the first call. The try
|
|
528
|
+
// below wraps this function's definition, not the frames the
|
|
529
|
+
// observer schedules it on, so a throw on a re-mark escaped
|
|
530
|
+
// to the window and the isolation only ever covered the
|
|
531
|
+
// synchronous pass.
|
|
532
|
+
const mark = () => {
|
|
533
|
+
try {
|
|
534
|
+
markDrillableCells(stageNode, names);
|
|
535
|
+
} catch (markError) {
|
|
536
|
+
console.warn("Drill affordance skipped:", markError);
|
|
537
|
+
}
|
|
538
|
+
};
|
|
539
|
+
mark();
|
|
540
|
+
// Enter and Space fire the drill for a focused cell. Delegated
|
|
541
|
+
// on the stage rather than bound per cell, because the
|
|
542
|
+
// renderer rebuilds cells as a dashboard's cards arrive and a
|
|
543
|
+
// per-cell listener would be lost with them; this one outlives
|
|
544
|
+
// every re-mark.
|
|
545
|
+
//
|
|
546
|
+
// It synthesises a CLICK rather than calling the drill handler
|
|
547
|
+
// directly, so the payload comes from the renderer's own click
|
|
548
|
+
// path. Building one here would mean re-deriving which field
|
|
549
|
+
// and value a cell represents, which is exactly the mapping
|
|
550
|
+
// this module has no access to and the reason the affordance
|
|
551
|
+
// is matched on rendered text in the first place.
|
|
552
|
+
//
|
|
553
|
+
// The click goes to the cell's INNER `.cell-content`, not to
|
|
554
|
+
// the cell. Measured, because dispatching on the cell looked
|
|
555
|
+
// right and silently did nothing: a real mouse click lands on
|
|
556
|
+
// `.cell-content` and the renderer reads the event target to
|
|
557
|
+
// identify the field, so a click on the outer cell reaches the
|
|
558
|
+
// handler with a target it does not recognise and is dropped
|
|
559
|
+
// without a sound. Falls back to the cell for a shape that has
|
|
560
|
+
// no inner content node.
|
|
561
|
+
//
|
|
562
|
+
// Enter fires on keydown and Space on keyUP, which is the
|
|
563
|
+
// native button rule and is load-bearing here rather than
|
|
564
|
+
// pedantry. Firing Space on keydown opened the drill menu and
|
|
565
|
+
// then the SAME keypress's keyup landed on the now-focused
|
|
566
|
+
// first menu item and chose it, so a Space drill navigated
|
|
567
|
+
// straight past the menu it had just opened. Measured: the
|
|
568
|
+
// menu was visible while the key was held and gone, with the
|
|
569
|
+
// URL already changed, once it was released.
|
|
570
|
+
//
|
|
571
|
+
// Dispatched with the cell's own coordinates rather than
|
|
572
|
+
// through `HTMLElement.click()`, which reports `clientX/Y` as
|
|
573
|
+
// 0. A drill offering two destinations anchors its menu on
|
|
574
|
+
// those numbers (`useDrill` holds no ref into the renderer's
|
|
575
|
+
// DOM), so a keyboard-opened menu appeared in the corner of
|
|
576
|
+
// the viewport instead of beside the cell it came from.
|
|
577
|
+
const fire = (cell: Element) => {
|
|
578
|
+
const content =
|
|
579
|
+
cell.querySelector<HTMLElement>(".cell-content");
|
|
580
|
+
const target = content ?? (cell as HTMLElement);
|
|
581
|
+
const rect = target.getBoundingClientRect();
|
|
582
|
+
target.dispatchEvent(
|
|
583
|
+
new MouseEvent("click", {
|
|
584
|
+
bubbles: true,
|
|
585
|
+
cancelable: true,
|
|
586
|
+
view: target.ownerDocument.defaultView,
|
|
587
|
+
clientX: Math.round(rect.left + rect.width / 2),
|
|
588
|
+
clientY: Math.round(rect.bottom),
|
|
589
|
+
}),
|
|
590
|
+
);
|
|
591
|
+
};
|
|
592
|
+
const drillCell = (event: KeyboardEvent) =>
|
|
593
|
+
(
|
|
594
|
+
event.target as HTMLElement | null
|
|
595
|
+
)?.closest?.<HTMLElement>(`.${DRILL_CELL_CLASS}`) ?? null;
|
|
596
|
+
// Move the single tab stop within a drillable column.
|
|
597
|
+
// `markDrillableCells` gives a column one stop rather than one
|
|
598
|
+
// per cell, so these keys are how a reader reaches the other
|
|
599
|
+
// rows; without them the affordance would only ever fire on
|
|
600
|
+
// whichever row held the stop. Clamped rather than wrapping,
|
|
601
|
+
// which is what a table's rows read as.
|
|
602
|
+
const rove = (
|
|
603
|
+
cell: HTMLElement,
|
|
604
|
+
to: number | "first" | "last",
|
|
605
|
+
) => {
|
|
606
|
+
// The move lives in `markDrillableCells` beside the column
|
|
607
|
+
// rule, because it has to hold the same invariant the
|
|
608
|
+
// marking does: exactly one stop per column. Doing it here
|
|
609
|
+
// by clearing the focused cell assumed that cell HELD the
|
|
610
|
+
// stop, which a click-focused `tabindex="-1"` cell does not.
|
|
611
|
+
moveDrillStop(cell, to)?.focus();
|
|
612
|
+
};
|
|
613
|
+
drillKeydown = (event: KeyboardEvent) => {
|
|
614
|
+
const cell = drillCell(event) as HTMLElement | null;
|
|
615
|
+
if (!cell) return;
|
|
616
|
+
const roveKey =
|
|
617
|
+
event.key === "ArrowDown"
|
|
618
|
+
? 1
|
|
619
|
+
: event.key === "ArrowUp"
|
|
620
|
+
? -1
|
|
621
|
+
: event.key === "Home"
|
|
622
|
+
? ("first" as const)
|
|
623
|
+
: event.key === "End"
|
|
624
|
+
? ("last" as const)
|
|
625
|
+
: undefined;
|
|
626
|
+
if (roveKey !== undefined) {
|
|
627
|
+
// Prevented whether or not the move happened, so the
|
|
628
|
+
// first and last rows do not scroll the page out from
|
|
629
|
+
// under a reader who is still inside the column.
|
|
630
|
+
event.preventDefault();
|
|
631
|
+
rove(cell, roveKey);
|
|
632
|
+
return;
|
|
633
|
+
}
|
|
634
|
+
if (event.key === "Enter") {
|
|
635
|
+
// Otherwise Enter submits an enclosing form on an
|
|
636
|
+
// embedding host.
|
|
637
|
+
event.preventDefault();
|
|
638
|
+
fire(cell);
|
|
639
|
+
} else if (event.key === " ") {
|
|
640
|
+
// Held Space scrolls the page; the activation itself
|
|
641
|
+
// waits for keyup.
|
|
642
|
+
event.preventDefault();
|
|
643
|
+
}
|
|
644
|
+
};
|
|
645
|
+
drillKeyup = (event: KeyboardEvent) => {
|
|
646
|
+
if (event.key !== " ") return;
|
|
647
|
+
const cell = drillCell(event);
|
|
648
|
+
if (!cell) return;
|
|
649
|
+
event.preventDefault();
|
|
650
|
+
fire(cell);
|
|
651
|
+
};
|
|
652
|
+
// Focus and the tab stop have to agree however focus got
|
|
653
|
+
// there. The arrow handler moves the stop, but a
|
|
654
|
+
// `tabindex="-1"` cell is still CLICK-focusable, so without
|
|
655
|
+
// this a reader who clicked row 5, tabbed away and tabbed back
|
|
656
|
+
// landed on row 0. That is the same asymmetry the arrow path
|
|
657
|
+
// already fixes, for the one way of arriving that does not go
|
|
658
|
+
// through it. `moveDrillStop(cell, 0)` resolves to the cell
|
|
659
|
+
// itself, so it just makes that cell the column's stop.
|
|
660
|
+
drillFocusIn = (event: FocusEvent) => {
|
|
661
|
+
const cell = (
|
|
662
|
+
event.target as HTMLElement | null
|
|
663
|
+
)?.closest?.<HTMLElement>(`.${DRILL_CELL_CLASS}`);
|
|
664
|
+
if (cell) moveDrillStop(cell, 0);
|
|
665
|
+
};
|
|
666
|
+
stageNode.addEventListener("keydown", drillKeydown);
|
|
667
|
+
stageNode.addEventListener("keyup", drillKeyup);
|
|
668
|
+
stageNode.addEventListener("focusin", drillFocusIn);
|
|
669
|
+
if (
|
|
670
|
+
names.size > 0 &&
|
|
671
|
+
typeof MutationObserver !== "undefined"
|
|
672
|
+
) {
|
|
673
|
+
drillObserver = new MutationObserver(() => {
|
|
674
|
+
cancelAnimationFrame(drillFrame);
|
|
675
|
+
drillFrame = requestAnimationFrame(mark);
|
|
676
|
+
});
|
|
677
|
+
// childList only: marking writes classes, and observing
|
|
678
|
+
// attributes would have it retrigger itself every frame.
|
|
679
|
+
drillObserver.observe(stageNode, {
|
|
680
|
+
childList: true,
|
|
681
|
+
subtree: true,
|
|
682
|
+
});
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
} catch (drillError) {
|
|
686
|
+
// No affordance, and the result stands. Clicks still resolve,
|
|
687
|
+
// because the renderer routes those itself.
|
|
688
|
+
console.warn("Drill affordance skipped:", drillError);
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
viz.onReady(promote);
|
|
692
|
+
// If onReady never fires (an async render error that only reaches
|
|
693
|
+
// the renderer's onError), force the swap after a bounded wait so
|
|
694
|
+
// the outcome becomes visible instead of the previous chart
|
|
695
|
+
// lingering with stale data indefinitely.
|
|
696
|
+
readyFallback = setTimeout(promote, 10000);
|
|
697
|
+
} catch (error) {
|
|
698
|
+
console.error("Error rendering visualization:", error);
|
|
699
|
+
observer?.disconnect();
|
|
700
|
+
drillObserver?.disconnect();
|
|
701
|
+
viz.remove();
|
|
702
|
+
viz = undefined;
|
|
703
|
+
if (stageNode.parentNode === element) {
|
|
704
|
+
element.removeChild(stageNode);
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
})();
|
|
708
|
+
|
|
135
709
|
return () => {
|
|
136
|
-
|
|
710
|
+
cancelled = true;
|
|
137
711
|
observer?.disconnect();
|
|
138
712
|
if (measureTimeout) clearTimeout(measureTimeout);
|
|
713
|
+
if (readyFallback) clearTimeout(readyFallback);
|
|
714
|
+
// If this render built a stage but never swapped it into `liveRef`
|
|
715
|
+
// (superseded, or unmounted mid-render), tear it down so it can't
|
|
716
|
+
// leak a viz or leave an orphan node behind.
|
|
717
|
+
if (stage && liveRef.current?.node !== stage) {
|
|
718
|
+
// The keyboard listeners go with the stage, and ONLY with the
|
|
719
|
+
// stage. Removing them unconditionally tied their lifetime to the
|
|
720
|
+
// effect RUN instead, while the stage's is tied to `liveRef`: a
|
|
721
|
+
// re-run tore them off the chart that is still on screen, which
|
|
722
|
+
// keeps its `tabindex` and `role="button"` and so still says Enter
|
|
723
|
+
// and Space do something. Usually that window closes when the new
|
|
724
|
+
// render promotes, but a render that never gets that far (a parse
|
|
725
|
+
// failure, a renderer that throws) leaves the visible chart
|
|
726
|
+
// permanently keyboard-dead while still advertising otherwise.
|
|
727
|
+
// Left on a promoted stage they are collected with the node when
|
|
728
|
+
// `promote` removes it, so nothing leaks by keeping them.
|
|
729
|
+
if (drillKeydown)
|
|
730
|
+
stage.removeEventListener("keydown", drillKeydown);
|
|
731
|
+
if (drillKeyup) stage.removeEventListener("keyup", drillKeyup);
|
|
732
|
+
if (drillFocusIn)
|
|
733
|
+
stage.removeEventListener("focusin", drillFocusIn);
|
|
734
|
+
// The re-marker is scoped the same way, and for the same reason.
|
|
735
|
+
// Disconnecting it unconditionally tied it to the effect RUN: a
|
|
736
|
+
// re-run while a `# dashboard` was still building its cards stopped
|
|
737
|
+
// the LIVE stage being re-marked, so cards arriving after that point
|
|
738
|
+
// never became drillable. Like the listeners, it is collected with
|
|
739
|
+
// the node once a promoted stage is dropped.
|
|
740
|
+
drillObserver?.disconnect();
|
|
741
|
+
cancelAnimationFrame(drillFrame);
|
|
742
|
+
viz?.remove();
|
|
743
|
+
if (stage.parentNode === element) {
|
|
744
|
+
element.removeChild(stage);
|
|
745
|
+
}
|
|
746
|
+
// This render may have set the container position:relative for its
|
|
747
|
+
// overlay stage but never promoted; reset it so no stray relative
|
|
748
|
+
// lingers on the container.
|
|
749
|
+
element.style.position = "";
|
|
750
|
+
}
|
|
139
751
|
};
|
|
140
|
-
}, [
|
|
752
|
+
}, [
|
|
753
|
+
result,
|
|
754
|
+
handleDrillClick,
|
|
755
|
+
drillable,
|
|
756
|
+
onSizeChange,
|
|
757
|
+
baseTheme,
|
|
758
|
+
layers,
|
|
759
|
+
mode,
|
|
760
|
+
]);
|
|
141
761
|
|
|
142
762
|
// Malloy renderer requires explicit pixel height to render visualizations
|
|
143
763
|
return (
|
|
@@ -151,41 +771,39 @@ function RenderedResultInner({
|
|
|
151
771
|
);
|
|
152
772
|
}
|
|
153
773
|
|
|
154
|
-
// Main component with error boundary and fallback
|
|
155
774
|
export default function RenderedResult(props: RenderedResultProps) {
|
|
156
|
-
// Show loading state if we're in server-side rendering
|
|
157
775
|
if (typeof window === "undefined") {
|
|
158
776
|
return (
|
|
159
|
-
<
|
|
160
|
-
|
|
777
|
+
<Box
|
|
778
|
+
sx={{
|
|
161
779
|
width: "100%",
|
|
162
780
|
height: props.height ? `${props.height}px` : "100%",
|
|
163
781
|
display: "flex",
|
|
164
782
|
alignItems: "center",
|
|
165
783
|
justifyContent: "center",
|
|
166
|
-
color: "
|
|
784
|
+
color: "text.secondary",
|
|
167
785
|
}}
|
|
168
786
|
>
|
|
169
787
|
Loading...
|
|
170
|
-
</
|
|
788
|
+
</Box>
|
|
171
789
|
);
|
|
172
790
|
}
|
|
173
791
|
|
|
174
792
|
return (
|
|
175
793
|
<Suspense
|
|
176
794
|
fallback={
|
|
177
|
-
<
|
|
178
|
-
|
|
795
|
+
<Box
|
|
796
|
+
sx={{
|
|
179
797
|
width: "100%",
|
|
180
798
|
height: props.height ? `${props.height}px` : "100%",
|
|
181
799
|
display: "flex",
|
|
182
800
|
alignItems: "center",
|
|
183
801
|
justifyContent: "center",
|
|
184
|
-
color: "
|
|
802
|
+
color: "text.secondary",
|
|
185
803
|
}}
|
|
186
804
|
>
|
|
187
805
|
Loading visualization...
|
|
188
|
-
</
|
|
806
|
+
</Box>
|
|
189
807
|
}
|
|
190
808
|
>
|
|
191
809
|
<RenderedResultInner {...props} />
|