@malloy-publisher/sdk 0.3.0 → 0.3.1
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 +8 -0
- package/dist/DocumentStorageProvider-BeBD0uWc.cjs.js +134 -0
- package/dist/DocumentStorageProvider-tGvodI8f.es.js +24268 -0
- package/dist/RenderedResult-CPi6uQWq.cjs.js +115 -0
- package/dist/RenderedResult-mDE-4u8S.es.js +542 -0
- package/dist/ServerProvider-CH71GsoL.cjs.js +1 -0
- package/dist/{ServerProvider-DSK9rfSY.es.js → ServerProvider-X2Ma0HRs.es.js} +486 -521
- package/dist/builder/index.cjs.js +105 -0
- package/dist/builder/index.d.ts +2 -0
- package/dist/builder/index.es.js +9561 -0
- package/dist/builder-entry.d.ts +8 -0
- package/dist/client/index.cjs.js +1 -1
- package/dist/client/index.es.js +1 -1
- package/dist/components/Dashboard/Dashboard.d.ts +19 -14
- package/dist/components/Dashboard/DashboardGrid.d.ts +67 -0
- package/dist/components/Dashboard/DashboardTile.d.ts +6 -1
- package/dist/components/Dashboard/ExploreDialog.d.ts +18 -0
- package/dist/components/Dashboard/RowsDialog.d.ts +47 -0
- package/dist/components/Dashboard/TileCard.d.ts +44 -0
- package/dist/components/Dashboard/index.d.ts +2 -0
- package/dist/components/Dashboard/promoteMeasureRow.d.ts +7 -3
- package/dist/components/Dashboard/telemetry.d.ts +43 -0
- package/dist/components/DashboardBuilder/AddTileDialog.d.ts +35 -0
- package/dist/components/DashboardBuilder/BuilderToolbar.d.ts +29 -0
- package/dist/components/DashboardBuilder/DashboardBuilder.d.ts +112 -0
- package/dist/components/DashboardBuilder/DashboardEditor.d.ts +32 -0
- package/dist/components/DashboardBuilder/DiffDialog.d.ts +15 -0
- package/dist/components/DashboardBuilder/DrillDialog.d.ts +15 -0
- package/dist/components/DashboardBuilder/FilterDialog.d.ts +54 -0
- package/dist/components/DashboardBuilder/FilterStrip.d.ts +17 -0
- package/dist/components/DashboardBuilder/SettingsPopover.d.ts +20 -0
- package/dist/components/DashboardBuilder/TileFrame.d.ts +42 -0
- package/dist/components/DashboardBuilder/TileMenu.d.ts +24 -0
- package/dist/components/DashboardBuilder/catalog.d.ts +62 -0
- package/dist/components/DashboardBuilder/controls.d.ts +157 -0
- package/dist/components/DashboardBuilder/diff.d.ts +35 -0
- package/dist/components/DashboardBuilder/document.d.ts +218 -0
- package/dist/components/DashboardBuilder/layout.d.ts +71 -0
- package/dist/components/DashboardBuilder/malloyText.d.ts +45 -0
- package/dist/components/DashboardBuilder/preview.d.ts +60 -0
- package/dist/components/DashboardBuilder/readDocument.d.ts +106 -0
- package/dist/components/DashboardBuilder/sortable.d.ts +51 -0
- package/dist/components/DashboardBuilder/spliceDocument.d.ts +44 -0
- package/dist/components/DashboardBuilder/testing/fixtures.d.ts +10 -0
- package/dist/components/DashboardBuilder/useBuilderShortcuts.d.ts +22 -0
- package/dist/components/DashboardBuilder/useDashboardEditor.d.ts +63 -0
- package/dist/components/DashboardBuilder/useDraft.d.ts +13 -0
- package/dist/components/DashboardBuilder/useFilterForm.d.ts +65 -0
- package/dist/components/DashboardBuilder/useTileReorder.d.ts +22 -0
- package/dist/components/DashboardBuilder/useTileResize.d.ts +29 -0
- package/dist/components/DocumentStorage/DocumentStorageProvider.d.ts +7 -0
- package/dist/components/DocumentStorage/index.d.ts +1 -1
- package/dist/components/FloatingIconButton.d.ts +16 -0
- package/dist/components/Prose.d.ts +46 -0
- package/dist/components/RenderedResult/ResultPanel.d.ts +30 -0
- package/dist/components/RenderedResult/index.d.ts +1 -0
- package/dist/components/drill/index.d.ts +1 -1
- package/dist/components/drill/resolveDrill.d.ts +22 -0
- package/dist/components/drill/useDrill.d.ts +11 -2
- package/dist/components/index.d.ts +1 -0
- package/dist/components/styles.d.ts +0 -4
- package/dist/hooks/index.d.ts +2 -0
- package/dist/hooks/useDocumentControls.d.ts +70 -0
- package/dist/hooks/useQueryResult.d.ts +40 -0
- package/dist/index.cjs.js +23 -270
- package/dist/index.es.js +16977 -41293
- package/dist/utils/malloyLiteral.d.ts +11 -0
- package/package.json +24 -9
- package/src/builder-entry.ts +55 -0
- package/src/components/Dashboard/Dashboard.spec.tsx +7 -10
- package/src/components/Dashboard/Dashboard.tsx +165 -175
- package/src/components/Dashboard/DashboardGrid.tsx +124 -0
- package/src/components/Dashboard/DashboardTile.spec.tsx +30 -6
- package/src/components/Dashboard/DashboardTile.tsx +66 -123
- package/src/components/Dashboard/ExploreDialog.tsx +84 -0
- package/src/components/Dashboard/RowsDialog.spec.ts +42 -0
- package/src/components/Dashboard/RowsDialog.tsx +234 -0
- package/src/components/Dashboard/TileCard.tsx +120 -0
- package/src/components/Dashboard/index.ts +2 -0
- package/src/components/Dashboard/promoteMeasureRow.spec.ts +70 -0
- package/src/components/Dashboard/promoteMeasureRow.ts +33 -2
- package/src/components/Dashboard/telemetry.ts +44 -0
- package/src/components/Dashboard/tileGridColumn.spec.ts +1 -1
- package/src/components/DashboardBuilder/AddTileDialog.tsx +249 -0
- package/src/components/DashboardBuilder/BuilderToolbar.tsx +270 -0
- package/src/components/DashboardBuilder/DashboardBuilder.spec.tsx +858 -0
- package/src/components/DashboardBuilder/DashboardBuilder.tsx +653 -0
- package/src/components/DashboardBuilder/DashboardEditor.spec.tsx +211 -0
- package/src/components/DashboardBuilder/DashboardEditor.tsx +493 -0
- package/src/components/DashboardBuilder/DiffDialog.tsx +129 -0
- package/src/components/DashboardBuilder/DrillDialog.tsx +281 -0
- package/src/components/DashboardBuilder/FilterDialog.tsx +581 -0
- package/src/components/DashboardBuilder/FilterStrip.tsx +125 -0
- package/src/components/DashboardBuilder/SettingsPopover.tsx +168 -0
- package/src/components/DashboardBuilder/TileFrame.tsx +380 -0
- package/src/components/DashboardBuilder/TileMenu.tsx +188 -0
- package/src/components/DashboardBuilder/catalog.spec.ts +215 -0
- package/src/components/DashboardBuilder/catalog.ts +225 -0
- package/src/components/DashboardBuilder/controls.spec.ts +243 -0
- package/src/components/DashboardBuilder/controls.ts +398 -0
- package/src/components/DashboardBuilder/diff.spec.ts +44 -0
- package/src/components/DashboardBuilder/diff.ts +93 -0
- package/src/components/DashboardBuilder/document.ts +209 -0
- package/src/components/DashboardBuilder/keepRowStructure.spec.ts +84 -0
- package/src/components/DashboardBuilder/layout.ts +143 -0
- package/src/components/DashboardBuilder/malloyText.spec.ts +84 -0
- package/src/components/DashboardBuilder/malloyText.ts +151 -0
- package/src/components/DashboardBuilder/moveIntoGap.spec.ts +90 -0
- package/src/components/DashboardBuilder/preview.spec.ts +149 -0
- package/src/components/DashboardBuilder/preview.ts +172 -0
- package/src/components/DashboardBuilder/readDocument.spec.ts +375 -0
- package/src/components/DashboardBuilder/readDocument.ts +467 -0
- package/src/components/DashboardBuilder/sortable.tsx +151 -0
- package/src/components/DashboardBuilder/spliceDocument.spec.ts +1108 -0
- package/src/components/DashboardBuilder/spliceDocument.ts +1098 -0
- package/src/components/DashboardBuilder/testing/fixtures.ts +48 -0
- package/src/components/DashboardBuilder/useBuilderShortcuts.ts +88 -0
- package/src/components/DashboardBuilder/useDashboardEditor.spec.ts +255 -0
- package/src/components/DashboardBuilder/useDashboardEditor.ts +193 -0
- package/src/components/DashboardBuilder/useDraft.ts +43 -0
- package/src/components/DashboardBuilder/useFilterForm.spec.tsx +144 -0
- package/src/components/DashboardBuilder/useFilterForm.ts +352 -0
- package/src/components/DashboardBuilder/useTileReorder.spec.tsx +97 -0
- package/src/components/DashboardBuilder/useTileReorder.ts +115 -0
- package/src/components/DashboardBuilder/useTileResize.spec.tsx +121 -0
- package/src/components/DashboardBuilder/useTileResize.ts +110 -0
- package/src/components/DashboardBuilder/withGaps.spec.ts +96 -0
- package/src/components/DocumentStorage/DocumentStorageProvider.tsx +12 -0
- package/src/components/DocumentStorage/index.ts +1 -0
- package/src/components/Environment/About.tsx +4 -36
- package/src/components/FloatingIconButton.tsx +41 -0
- package/src/components/Model/Model.tsx +6 -23
- package/src/components/Model/ModelCell.tsx +33 -72
- package/src/components/Notebook/Notebook.tsx +25 -88
- package/src/components/Notebook/NotebookCell.tsx +35 -183
- package/src/components/Prose.spec.tsx +85 -0
- package/src/components/Prose.tsx +179 -0
- package/src/components/QueryResult/QueryResult.tsx +15 -43
- package/src/components/RenderedResult/ResultContainer.tsx +4 -12
- package/src/components/RenderedResult/ResultPanel.spec.tsx +49 -0
- package/src/components/RenderedResult/ResultPanel.tsx +73 -0
- package/src/components/RenderedResult/index.ts +1 -0
- package/src/components/drill/index.ts +1 -0
- package/src/components/drill/resolveDrill.ts +22 -1
- package/src/components/drill/useDrill.spec.tsx +94 -0
- package/src/components/drill/useDrill.tsx +52 -12
- package/src/components/given/GivensPanel.tsx +1 -1
- package/src/components/index.ts +6 -0
- package/src/components/styles.ts +0 -37
- package/src/hooks/index.ts +13 -0
- package/src/hooks/useDocumentControls.ts +170 -0
- package/src/hooks/useQueryResult.ts +82 -0
- package/src/utils/malloyLiteral.spec.ts +43 -0
- package/src/utils/malloyLiteral.ts +76 -0
- package/vite.config.ts +5 -0
- package/dist/ServerProvider-CGnLjzag.cjs.js +0 -1
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A line diff, for showing an author what a structural save will do to the
|
|
6
|
+
* file before it does it.
|
|
7
|
+
*
|
|
8
|
+
* Adding or removing a tile moves a declaration and the comment block above
|
|
9
|
+
* it, and the file cannot say whether that comment belonged to the tile, the
|
|
10
|
+
* row, or the page. The writer makes a defensible choice (a tile's `#` tags
|
|
11
|
+
* go with it; `//` comments stay), and this is what turns that choice from a
|
|
12
|
+
* silent one into one the author sees and approves.
|
|
13
|
+
*
|
|
14
|
+
* Longest-common-subsequence over lines: exact, and a dashboard file is a few
|
|
15
|
+
* hundred lines, so the quadratic table is small. Past `LIMIT` lines a side,
|
|
16
|
+
* the diff degrades to "everything changed" rather than stalling the page.
|
|
17
|
+
*/
|
|
18
|
+
export type DiffLine =
|
|
19
|
+
| { kind: "same"; text: string }
|
|
20
|
+
| { kind: "add"; text: string }
|
|
21
|
+
| { kind: "del"; text: string };
|
|
22
|
+
|
|
23
|
+
const LIMIT = 4000;
|
|
24
|
+
|
|
25
|
+
export function lineDiff(before: string, after: string): DiffLine[] {
|
|
26
|
+
const a = before.split("\n");
|
|
27
|
+
const b = after.split("\n");
|
|
28
|
+
if (a.length > LIMIT || b.length > LIMIT) {
|
|
29
|
+
return [
|
|
30
|
+
...a.map((text): DiffLine => ({ kind: "del", text })),
|
|
31
|
+
...b.map((text): DiffLine => ({ kind: "add", text })),
|
|
32
|
+
];
|
|
33
|
+
}
|
|
34
|
+
// lcs[i][j] = length of the LCS of a[i..] and b[j..].
|
|
35
|
+
const lcs: Uint32Array[] = Array.from(
|
|
36
|
+
{ length: a.length + 1 },
|
|
37
|
+
() => new Uint32Array(b.length + 1),
|
|
38
|
+
);
|
|
39
|
+
for (let i = a.length - 1; i >= 0; i--)
|
|
40
|
+
for (let j = b.length - 1; j >= 0; j--)
|
|
41
|
+
lcs[i][j] =
|
|
42
|
+
a[i] === b[j]
|
|
43
|
+
? lcs[i + 1][j + 1] + 1
|
|
44
|
+
: Math.max(lcs[i + 1][j], lcs[i][j + 1]);
|
|
45
|
+
const out: DiffLine[] = [];
|
|
46
|
+
let i = 0;
|
|
47
|
+
let j = 0;
|
|
48
|
+
while (i < a.length && j < b.length) {
|
|
49
|
+
if (a[i] === b[j]) {
|
|
50
|
+
out.push({ kind: "same", text: a[i] });
|
|
51
|
+
i++;
|
|
52
|
+
j++;
|
|
53
|
+
} else if (lcs[i + 1][j] >= lcs[i][j + 1]) {
|
|
54
|
+
out.push({ kind: "del", text: a[i++] });
|
|
55
|
+
} else {
|
|
56
|
+
out.push({ kind: "add", text: b[j++] });
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
while (i < a.length) out.push({ kind: "del", text: a[i++] });
|
|
60
|
+
while (j < b.length) out.push({ kind: "add", text: b[j++] });
|
|
61
|
+
return out;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The diff with unchanged stretches folded, keeping `context` lines around
|
|
66
|
+
* every change — what a review tool shows, and all a reader needs to judge a
|
|
67
|
+
* save. A fold is one entry saying how many lines it hides.
|
|
68
|
+
*/
|
|
69
|
+
export type DiffHunkLine = DiffLine | { kind: "fold"; count: number };
|
|
70
|
+
|
|
71
|
+
export function foldUnchanged(lines: DiffLine[], context = 3): DiffHunkLine[] {
|
|
72
|
+
const keep = new Array<boolean>(lines.length).fill(false);
|
|
73
|
+
lines.forEach((line, index) => {
|
|
74
|
+
if (line.kind === "same") return;
|
|
75
|
+
for (
|
|
76
|
+
let k = Math.max(0, index - context);
|
|
77
|
+
k <= Math.min(lines.length - 1, index + context);
|
|
78
|
+
k++
|
|
79
|
+
)
|
|
80
|
+
keep[k] = true;
|
|
81
|
+
});
|
|
82
|
+
const out: DiffHunkLine[] = [];
|
|
83
|
+
let folded = 0;
|
|
84
|
+
lines.forEach((line, index) => {
|
|
85
|
+
if (keep[index]) {
|
|
86
|
+
if (folded > 0) out.push({ kind: "fold", count: folded });
|
|
87
|
+
folded = 0;
|
|
88
|
+
out.push(line);
|
|
89
|
+
} else folded++;
|
|
90
|
+
});
|
|
91
|
+
if (folded > 0) out.push({ kind: "fold", count: folded });
|
|
92
|
+
return out;
|
|
93
|
+
}
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The dashboard builder's working representation of a `dashboards/*.malloy`
|
|
6
|
+
* file.
|
|
7
|
+
*
|
|
8
|
+
* This is an editing PROJECTION, not a complete representation of the file, and
|
|
9
|
+
* the distinction is load-bearing. The builder writes changes by SPLICING the
|
|
10
|
+
* source text — patching the ranges it owns and leaving every other byte alone —
|
|
11
|
+
* so anything this type does not model survives an edit untouched. A `where:` in
|
|
12
|
+
* a source extension, an inline computed dimension, a `#(doc)` tag, a reader's
|
|
13
|
+
* comments: none of them appear below, and none of them are lost.
|
|
14
|
+
*
|
|
15
|
+
* What that buys is the reason the projection exists. A writer that regenerated
|
|
16
|
+
* the whole file would have to model everything or destroy it, which is why the
|
|
17
|
+
* earlier design could only ever have edited files it wrote itself.
|
|
18
|
+
*
|
|
19
|
+
* The document is still what decides whether a file can be opened at all: the
|
|
20
|
+
* reader refuses a file it cannot fully turn into one of these, rather than
|
|
21
|
+
* opening it half-understood. See {@link readDashboardDocument}.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/** One `import` line. The file needs both forms, and for different reasons. */
|
|
25
|
+
export type DashboardImport =
|
|
26
|
+
/** `import "../givens.malloy"` — the whole file's declarations. */
|
|
27
|
+
| { kind: "all"; from: string }
|
|
28
|
+
/**
|
|
29
|
+
* `import { products, regions } from "../storefront.malloy"` — specific
|
|
30
|
+
* names. Not interchangeable with the bare form: a bare import is not
|
|
31
|
+
* transitive, so a dashboard whose control has `suggest { source=products }`
|
|
32
|
+
* must name `products` here or the package load reports an error for every
|
|
33
|
+
* such given.
|
|
34
|
+
*/
|
|
35
|
+
| { kind: "names"; names: string[]; from: string };
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A given this dashboard declares itself, rather than taking from the model.
|
|
39
|
+
*
|
|
40
|
+
* This is the CONVENTION for a dashboard the builder edits, not the exception:
|
|
41
|
+
* the builder adds and removes filters, and a filter it can add has to be a
|
|
42
|
+
* declaration in the one file it writes. A given imported from the model is
|
|
43
|
+
* still read and still bindable, but it is the model's, and the builder never
|
|
44
|
+
* edits imports or model files.
|
|
45
|
+
*
|
|
46
|
+
* Measured, and the reason the convention has to be this way round: a local
|
|
47
|
+
* given does NOT drive a model-level `where:` of the same name. Binding is per
|
|
48
|
+
* declaration, not per name, so a dashboard that declares `CATEGORY` and extends
|
|
49
|
+
* a source whose own `where:` reads the model's `CATEGORY` gets a control that
|
|
50
|
+
* moves nothing. The filtering therefore lives on the dashboard's tiles, as
|
|
51
|
+
* {@link DashboardTile.filters} — which is what the builder writes anyway.
|
|
52
|
+
*
|
|
53
|
+
* The fields are the declaration and its control contract, spelled as in the
|
|
54
|
+
* file. A tag this does not model survives an edit like any other unmodelled
|
|
55
|
+
* Malloy, because the writer patches rather than regenerates.
|
|
56
|
+
*/
|
|
57
|
+
export interface LocalGiven {
|
|
58
|
+
name: string;
|
|
59
|
+
/** The declared type, spelled as in the file: `filter<string>`, `date`, … */
|
|
60
|
+
type: string;
|
|
61
|
+
/** The default, spelled as in the file: `f'Jeans'`, `@2023-01-01`, … */
|
|
62
|
+
default: string;
|
|
63
|
+
label?: string;
|
|
64
|
+
/** `# description="…"`: helper text under the control. */
|
|
65
|
+
description?: string;
|
|
66
|
+
/** `control=select` / `multiselect`. */
|
|
67
|
+
control?: string;
|
|
68
|
+
/**
|
|
69
|
+
* `suggest { source=products dimension=category }`, or `{ query=… }`: where
|
|
70
|
+
* a picker gets its options. The source or query has to resolve in this file,
|
|
71
|
+
* and the builder does not add imports — so a given it creates suggests over
|
|
72
|
+
* a source the file already reads, or over nothing.
|
|
73
|
+
*/
|
|
74
|
+
suggest?: { source?: string; query?: string; dimension: string };
|
|
75
|
+
/** `range_min=` / `range_max=` on a `filter<number>`: a slider's bounds. */
|
|
76
|
+
rangeMin?: number;
|
|
77
|
+
rangeMax?: number;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** A `# drill` dimension the dashboard declares in its own source extension. */
|
|
81
|
+
export interface DashboardDrill {
|
|
82
|
+
/** The source this dimension is declared on. */
|
|
83
|
+
source: string;
|
|
84
|
+
/** `dimension: <name> is <expression>`. */
|
|
85
|
+
name: string;
|
|
86
|
+
expression: string;
|
|
87
|
+
/** Dashboard slugs, or the literal `self`. */
|
|
88
|
+
to: string[];
|
|
89
|
+
/** The given a click seeds. Absent means the dimension's own name. */
|
|
90
|
+
given?: string;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Where a tile's view is declared, and therefore what the builder may edit.
|
|
95
|
+
*
|
|
96
|
+
* Three forms, all of them found in dashboards that ship in this repository, and
|
|
97
|
+
* the distinction decides which affordances a tile gets:
|
|
98
|
+
*
|
|
99
|
+
* - `reference` — `view: revenue_trend is sales_by_month` in this file. Fully
|
|
100
|
+
* editable: the layout tags sit above that line and the builder owns them.
|
|
101
|
+
* - `inline` — `view: order_tile is { aggregate: order_count }` in this file.
|
|
102
|
+
* Its tags are editable for the same reason; its QUERY is not, because
|
|
103
|
+
* authoring a query body is a separate feature. The projection holds: the body
|
|
104
|
+
* is never rewritten, so it cannot be damaged.
|
|
105
|
+
* - `inherited` — not declared in this file at all, as in a dashboard whose only
|
|
106
|
+
* tile is `orders -> by_brand` against an imported source. Nothing about it is
|
|
107
|
+
* editable here, because its tags live on the model's own view and the builder
|
|
108
|
+
* does not write model files. Read and shown, not changed.
|
|
109
|
+
*/
|
|
110
|
+
export type TileDeclaration =
|
|
111
|
+
| { kind: "reference"; from: string }
|
|
112
|
+
| { kind: "inline" }
|
|
113
|
+
| { kind: "inherited" };
|
|
114
|
+
|
|
115
|
+
/** One tile: a view shown on the page, plus how it is presented. */
|
|
116
|
+
export interface DashboardTile {
|
|
117
|
+
/**
|
|
118
|
+
* The view name. For a tile declared in this file, assigned ONCE when the
|
|
119
|
+
* tile is added and never recomputed — deriving it from position would
|
|
120
|
+
* rename every later tile on a reorder, turning a pure-layout edit into a
|
|
121
|
+
* large diff.
|
|
122
|
+
*/
|
|
123
|
+
name: string;
|
|
124
|
+
/**
|
|
125
|
+
* The source it reads, by NAME — an index would break on reorder. May name a
|
|
126
|
+
* source this file declares in {@link DashboardDocument.sources}, or one it
|
|
127
|
+
* merely imports.
|
|
128
|
+
*/
|
|
129
|
+
source: string;
|
|
130
|
+
declaration: TileDeclaration;
|
|
131
|
+
/**
|
|
132
|
+
* Given bindings, emitted as a `+ { where: … }` refinement on the view.
|
|
133
|
+
*
|
|
134
|
+
* `op` is the comparison, and absent means `~`, which is how a `filter<…>`
|
|
135
|
+
* given binds. A plain `date` or `number` given is a VALUE, not a filter,
|
|
136
|
+
* and compares with `>=`, `<=` or `=` — measured: `created_at >= $SINCE`
|
|
137
|
+
* compiles and `created_at ~ $SINCE` does not, on a `date` given.
|
|
138
|
+
*/
|
|
139
|
+
filters?: Array<{ field: string; given: string; op?: string }>;
|
|
140
|
+
label?: string;
|
|
141
|
+
subtitle?: string;
|
|
142
|
+
colspan?: number;
|
|
143
|
+
break?: boolean;
|
|
144
|
+
borderless?: boolean;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* A source extension the dashboard DECLARES, holding tiles that read it.
|
|
149
|
+
*
|
|
150
|
+
* A LIST, not one source, because a composite dashboard exists precisely to
|
|
151
|
+
* combine queries no single Malloy result can span. Measured: a dashboard whose
|
|
152
|
+
* tiles come from two sources loads clean and scopes givens per source. A picker
|
|
153
|
+
* over a package catalog offers every source's views side by side, so the second
|
|
154
|
+
* tile a reader adds can already need this.
|
|
155
|
+
*
|
|
156
|
+
* May be EMPTY. A dashboard whose tiles all read imported sources declares no
|
|
157
|
+
* extension at all — `tiles=["orders -> by_brand"]` over an imported `orders` is
|
|
158
|
+
* a complete dashboard with nothing else in the file.
|
|
159
|
+
*/
|
|
160
|
+
export interface DashboardSource {
|
|
161
|
+
name: string;
|
|
162
|
+
/** What it extends: `source: <name> is <base> extend { … }`. */
|
|
163
|
+
base: string;
|
|
164
|
+
/** A `#(doc)` description on the source. */
|
|
165
|
+
doc?: string;
|
|
166
|
+
/**
|
|
167
|
+
* The dimensions this extension declares itself, in file order. These are
|
|
168
|
+
* the only fields a `# drill` can be authored on here: a drill is a tag on
|
|
169
|
+
* the dimension's declaration, and the model's dimensions are declared in
|
|
170
|
+
* the model.
|
|
171
|
+
*/
|
|
172
|
+
dimensions?: DashboardDimension[];
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** A tile's identity across a reorder: what the grid keys on, a drag names and the writer matches. */
|
|
176
|
+
export const tileKey = (tile: { source: string; name: string }) =>
|
|
177
|
+
`${tile.source}.${tile.name}`;
|
|
178
|
+
|
|
179
|
+
/** `dimension: <name> is <expression>` in the dashboard's own extension. */
|
|
180
|
+
export interface DashboardDimension {
|
|
181
|
+
name: string;
|
|
182
|
+
expression: string;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export interface DashboardDocument {
|
|
186
|
+
title: string;
|
|
187
|
+
/** The narrative header, as markdown. Emitted as `##"` lines. */
|
|
188
|
+
description?: string;
|
|
189
|
+
/** `# dashboard { columns=N }`. */
|
|
190
|
+
columns?: number;
|
|
191
|
+
/** `## artifact { autorun=false }`. Absent means autorun. */
|
|
192
|
+
autorun?: boolean;
|
|
193
|
+
imports: DashboardImport[];
|
|
194
|
+
sources: DashboardSource[];
|
|
195
|
+
localGivens?: LocalGiven[];
|
|
196
|
+
/** `## artifact { givens { … } }` — where the controls open. */
|
|
197
|
+
startingGivens?: Record<string, string>;
|
|
198
|
+
drills?: DashboardDrill[];
|
|
199
|
+
/**
|
|
200
|
+
* Tiles in layout order. The grid flows in declaration order, so this array
|
|
201
|
+
* IS the layout, and there is no separate ordering field to fall out of step
|
|
202
|
+
* with it.
|
|
203
|
+
*
|
|
204
|
+
* One array drives both halves of the file: the `## artifact { tiles=[…] }`
|
|
205
|
+
* list and the `view:` declarations inside each extension. They therefore
|
|
206
|
+
* cannot disagree, which they can and do in hand-written files.
|
|
207
|
+
*/
|
|
208
|
+
tiles: DashboardTile[];
|
|
209
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { describe, expect, it } from "bun:test";
|
|
5
|
+
import { keepRowStructure } from "./layout";
|
|
6
|
+
import type { DashboardTile } from "./document";
|
|
7
|
+
|
|
8
|
+
const tile = (
|
|
9
|
+
name: string,
|
|
10
|
+
colspan: number,
|
|
11
|
+
starts = false,
|
|
12
|
+
): DashboardTile => ({
|
|
13
|
+
name,
|
|
14
|
+
source: "a",
|
|
15
|
+
declaration: { kind: "reference", from: `${name}_view` },
|
|
16
|
+
colspan,
|
|
17
|
+
...(starts ? { break: true } : {}),
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
/** The break flags a row structure is made of, read back off a tile list. */
|
|
21
|
+
const rows = (tiles: DashboardTile[]) =>
|
|
22
|
+
tiles.map((each) => each.break ?? false);
|
|
23
|
+
const names = (tiles: DashboardTile[]) => tiles.map((each) => each.name);
|
|
24
|
+
|
|
25
|
+
describe("keepRowStructure", () => {
|
|
26
|
+
// Four half-width tiles on a twelve-column grid are a 2x2, and the whole
|
|
27
|
+
// point is that moving one leaves it a 2x2.
|
|
28
|
+
const quad = [
|
|
29
|
+
tile("one", 6),
|
|
30
|
+
tile("two", 6, true),
|
|
31
|
+
tile("three", 6),
|
|
32
|
+
tile("four", 6, true),
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
it("keeps the row starts where they were when a tile moves", () => {
|
|
36
|
+
const moved = [...quad];
|
|
37
|
+
const [taken] = moved.splice(1, 1);
|
|
38
|
+
moved.splice(2, 0, taken);
|
|
39
|
+
|
|
40
|
+
const settled = keepRowStructure(moved, rows(quad));
|
|
41
|
+
|
|
42
|
+
expect(names(settled)).toEqual(["one", "three", "two", "four"]);
|
|
43
|
+
// The pattern is unchanged, so the grid is still two rows of two: the
|
|
44
|
+
// tile that left did not carry its row start along with it.
|
|
45
|
+
expect(rows(settled)).toEqual(rows(quad));
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("gives the row start to whichever tile lands on it", () => {
|
|
49
|
+
const moved = [...quad];
|
|
50
|
+
const [taken] = moved.splice(0, 1);
|
|
51
|
+
moved.splice(1, 0, taken);
|
|
52
|
+
|
|
53
|
+
const settled = keepRowStructure(moved, rows(quad));
|
|
54
|
+
|
|
55
|
+
expect(names(settled)).toEqual(["two", "one", "three", "four"]);
|
|
56
|
+
// `two` carried the break and has given it up on moving off that slot;
|
|
57
|
+
// `one` has taken it on by arriving there.
|
|
58
|
+
expect(settled[0].break).toBeUndefined();
|
|
59
|
+
expect(settled[1].break).toBe(true);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("leaves widths with their tiles", () => {
|
|
63
|
+
const mixed = [tile("wide", 12), tile("narrow", 4, true)];
|
|
64
|
+
const moved = [mixed[1], mixed[0]];
|
|
65
|
+
|
|
66
|
+
const settled = keepRowStructure(moved, rows(mixed));
|
|
67
|
+
|
|
68
|
+
// A width is genuinely the tile's, so it travels; only the row start is
|
|
69
|
+
// positional.
|
|
70
|
+
expect(settled.map((each) => [each.name, each.colspan])).toEqual([
|
|
71
|
+
["narrow", 4],
|
|
72
|
+
["wide", 12],
|
|
73
|
+
]);
|
|
74
|
+
expect(rows(settled)).toEqual([false, true]);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("returns the same tile object when nothing about it changes", () => {
|
|
78
|
+
const settled = keepRowStructure([...quad], rows(quad));
|
|
79
|
+
// No needless copies: an untouched tile is the tile it was, which is what
|
|
80
|
+
// keeps the editor's own "did this change anything" check honest.
|
|
81
|
+
expect(settled[0]).toBe(quad[0]);
|
|
82
|
+
expect(settled[1]).toBe(quad[1]);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import type { GridTile } from "../Dashboard/DashboardGrid";
|
|
5
|
+
import { tileKey, type DashboardTile } from "./document";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* How the builder's grid is arranged: what a reorder does to the rows, and
|
|
9
|
+
* where the empty end of a row is. Pure, so the gestures that call it can be
|
|
10
|
+
* tested without a pointer.
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Reordering keeps the ROW STRUCTURE and moves only the tiles through it.
|
|
14
|
+
*
|
|
15
|
+
* `# break` starts a fresh row. It is written as a tag on a tile, so the
|
|
16
|
+
* obvious reading is that it belongs to the tile and should travel with it.
|
|
17
|
+
* That is wrong in practice: drag one of four half-width tiles and its `break`
|
|
18
|
+
* lands mid-row, forcing a new row and leaving the half beside it empty — four
|
|
19
|
+
* `colspan=6` tiles stop being a 2x2 the moment one moves.
|
|
20
|
+
*
|
|
21
|
+
* The tag is positional in MEANING even though it is stored per tile: it
|
|
22
|
+
* says "a row starts here", which is a fact about the grid, not about the
|
|
23
|
+
* view that happens to sit there. So a move re-applies the break pattern by
|
|
24
|
+
* POSITION, and the surrounding tiles close up behind the one that left.
|
|
25
|
+
* Widths still travel with their tiles, because a width really is the tile's.
|
|
26
|
+
*/
|
|
27
|
+
export const keepRowStructure = (
|
|
28
|
+
reordered: DashboardTile[],
|
|
29
|
+
pattern: readonly boolean[],
|
|
30
|
+
) =>
|
|
31
|
+
reordered.map((tile, index) => {
|
|
32
|
+
const starts = pattern[index] ?? false;
|
|
33
|
+
if (starts === (tile.break ?? false)) return tile;
|
|
34
|
+
const next = { ...tile };
|
|
35
|
+
if (starts) next.break = true;
|
|
36
|
+
else delete next.break;
|
|
37
|
+
return next;
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Moving a tile INTO A GAP — the empty end of a row — is the one move that
|
|
42
|
+
* must change the row structure, because the gap IS the row structure.
|
|
43
|
+
*
|
|
44
|
+
* A gap exists for one reason: the tile after the row's last tile carries a
|
|
45
|
+
* `# break`, so it starts a fresh row instead of filling the space. Dropping a
|
|
46
|
+
* tile into that space says, as plainly as a gesture can, "this belongs up
|
|
47
|
+
* here" — and {@link keepRowStructure} would refuse it: re-applying the break
|
|
48
|
+
* by position hands the break to the moved tile, which then starts its own row
|
|
49
|
+
* and leaves the gap exactly where it was. Measured on the storefront overview
|
|
50
|
+
* with the trend narrowed to half width: the map could not be dragged up
|
|
51
|
+
* beside it at all.
|
|
52
|
+
*
|
|
53
|
+
* So a gap drop lets the break TRAVEL instead: the moved tile arrives with
|
|
54
|
+
* none, so it flows into the row; the tile that started the next row keeps its
|
|
55
|
+
* break and still does. And the row the tile LEFT closes up — if it was that
|
|
56
|
+
* row's first tile, the tile after it starts the row now, which is what the
|
|
57
|
+
* positional rule would have done there too.
|
|
58
|
+
*
|
|
59
|
+
* `to` is where the tile lands in the array it is spliced back into, after
|
|
60
|
+
* `from` has been removed — the same convention the tile-target move uses.
|
|
61
|
+
*/
|
|
62
|
+
export const moveIntoGap = (
|
|
63
|
+
tiles: readonly DashboardTile[],
|
|
64
|
+
from: number,
|
|
65
|
+
to: number,
|
|
66
|
+
) => {
|
|
67
|
+
const next = [...tiles];
|
|
68
|
+
const [moved] = next.splice(from, 1);
|
|
69
|
+
if (!moved) return [...tiles];
|
|
70
|
+
// The tile that followed the one leaving, now at `from`, inherits a row
|
|
71
|
+
// start it did not have — the row must still begin somewhere.
|
|
72
|
+
const follower = next[from];
|
|
73
|
+
if (moved.break && follower && !follower.break)
|
|
74
|
+
next[from] = { ...follower, break: true };
|
|
75
|
+
const placed = { ...moved };
|
|
76
|
+
delete placed.break;
|
|
77
|
+
next.splice(to, 0, placed);
|
|
78
|
+
return next;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
/** One thing the grid lays out: a tile, or the empty end of a row. */
|
|
82
|
+
export type GridEntry = GridTile &
|
|
83
|
+
(
|
|
84
|
+
| {
|
|
85
|
+
kind: "tile";
|
|
86
|
+
tile: DashboardTile;
|
|
87
|
+
/** Its place among the TILES — what a sortable item is told. */
|
|
88
|
+
index: number;
|
|
89
|
+
}
|
|
90
|
+
| { kind: "gap"; after: string }
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
export const tileEntry = (tile: DashboardTile, index: number): GridEntry => ({
|
|
94
|
+
kind: "tile",
|
|
95
|
+
tile,
|
|
96
|
+
index,
|
|
97
|
+
colspan: tile.colspan,
|
|
98
|
+
break: tile.break,
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
/** A gap's id: its drop target's, and the grid's key for it. */
|
|
102
|
+
export const gapId = (after: string) => `gap.${after}`;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The grid's entries with a drop target in every gap.
|
|
106
|
+
*
|
|
107
|
+
* A flow grid has no element where a row runs out; the space is simply not
|
|
108
|
+
* painted, so there is nothing for a drag to be "over". This walks the tiles
|
|
109
|
+
* as the grid will place them — `# break` starts a row, and so does a tile too
|
|
110
|
+
* wide for what is left of the current one — and wherever a row ends short of
|
|
111
|
+
* the last column, lays a gap entry spanning exactly the columns left over. It
|
|
112
|
+
* fills space that was already empty, so adding it moves nothing; and it
|
|
113
|
+
* carries the key of the tile it follows, which is what a drop on it needs.
|
|
114
|
+
*/
|
|
115
|
+
export const withGaps = (
|
|
116
|
+
tiles: readonly DashboardTile[],
|
|
117
|
+
columns: number,
|
|
118
|
+
): GridEntry[] => {
|
|
119
|
+
const entries: GridEntry[] = [];
|
|
120
|
+
let used = 0;
|
|
121
|
+
tiles.forEach((tile, index) => {
|
|
122
|
+
const span = Math.min(tile.colspan ?? 1, columns);
|
|
123
|
+
const startsRow =
|
|
124
|
+
index === 0 || tile.break === true || used + span > columns;
|
|
125
|
+
if (startsRow && index > 0 && used < columns)
|
|
126
|
+
entries.push({
|
|
127
|
+
kind: "gap",
|
|
128
|
+
after: tileKey(tiles[index - 1]),
|
|
129
|
+
colspan: columns - used,
|
|
130
|
+
});
|
|
131
|
+
if (startsRow) used = 0;
|
|
132
|
+
entries.push(tileEntry(tile, index));
|
|
133
|
+
used += span;
|
|
134
|
+
});
|
|
135
|
+
const last = tiles.at(-1);
|
|
136
|
+
if (last && used < columns)
|
|
137
|
+
entries.push({
|
|
138
|
+
kind: "gap",
|
|
139
|
+
after: tileKey(last),
|
|
140
|
+
colspan: columns - used,
|
|
141
|
+
});
|
|
142
|
+
return entries;
|
|
143
|
+
};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { describe, expect, it } from "bun:test";
|
|
5
|
+
import {
|
|
6
|
+
artifactLine,
|
|
7
|
+
declarationLine,
|
|
8
|
+
declarationsUnder,
|
|
9
|
+
givenDeclarations,
|
|
10
|
+
tileSteps,
|
|
11
|
+
} from "./malloyText";
|
|
12
|
+
|
|
13
|
+
const LINES = `##! experimental.givens
|
|
14
|
+
## artifact { title="T" tiles=["a -> x"] }
|
|
15
|
+
import "../m.malloy"
|
|
16
|
+
|
|
17
|
+
given: CATEGORY :: filter<string> is f''
|
|
18
|
+
given:
|
|
19
|
+
SINCE :: date is @2023-01-01
|
|
20
|
+
MIN :: number is 0
|
|
21
|
+
|
|
22
|
+
source: a is one extend {
|
|
23
|
+
# drill { to=self }
|
|
24
|
+
dimension: cat is products.category
|
|
25
|
+
view: x is vx + { limit: 5, where: cat ~ $CATEGORY }
|
|
26
|
+
view: y is {
|
|
27
|
+
group_by: cat
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
source: b is two extend {
|
|
32
|
+
view: z is vz
|
|
33
|
+
}`.split("\n");
|
|
34
|
+
|
|
35
|
+
describe("the text a dashboard file is read as", () => {
|
|
36
|
+
it("finds each source's own declarations and where a body ends", () => {
|
|
37
|
+
expect([...declarationsUnder(LINES, "a", "view")]).toEqual([
|
|
38
|
+
["x", { line: 12, rest: "vx + { limit: 5, where: cat ~ $CATEGORY }" }],
|
|
39
|
+
["y", { line: 13, rest: "{" }],
|
|
40
|
+
]);
|
|
41
|
+
expect([...declarationsUnder(LINES, "a", "dimension")]).toEqual([
|
|
42
|
+
["cat", { line: 11, rest: "products.category" }],
|
|
43
|
+
]);
|
|
44
|
+
expect([...declarationsUnder(LINES, "b", "view").keys()]).toEqual(["z"]);
|
|
45
|
+
expect(declarationLine(LINES, "source", "b")).toBe(18);
|
|
46
|
+
expect(declarationLine(LINES, "view", "missing")).toBe(-1);
|
|
47
|
+
expect(artifactLine(LINES)).toBe(1);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("reads givens in both spellings, remembering a block's header", () => {
|
|
51
|
+
expect([...givenDeclarations(LINES)]).toEqual([
|
|
52
|
+
[
|
|
53
|
+
"CATEGORY",
|
|
54
|
+
{ line: 4, declaration: "CATEGORY :: filter<string> is f''" },
|
|
55
|
+
],
|
|
56
|
+
[
|
|
57
|
+
"SINCE",
|
|
58
|
+
{
|
|
59
|
+
line: 6,
|
|
60
|
+
blockHeader: 5,
|
|
61
|
+
declaration: "SINCE :: date is @2023-01-01",
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
[
|
|
65
|
+
"MIN",
|
|
66
|
+
{ line: 7, blockHeader: 5, declaration: "MIN :: number is 0" },
|
|
67
|
+
],
|
|
68
|
+
]);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("splits a tile expression into its steps", () => {
|
|
72
|
+
expect(tileSteps("orders -> by_brand")).toEqual({
|
|
73
|
+
source: "orders",
|
|
74
|
+
view: "by_brand",
|
|
75
|
+
});
|
|
76
|
+
expect(tileSteps("orders->by_brand + { limit: 2 }")).toEqual({
|
|
77
|
+
source: "orders",
|
|
78
|
+
view: "by_brand",
|
|
79
|
+
refinement: "+ { limit: 2 }",
|
|
80
|
+
});
|
|
81
|
+
expect(tileSteps("{ group_by: x } -> y -> z")).toBeUndefined();
|
|
82
|
+
expect(tileSteps(undefined)).toBeUndefined();
|
|
83
|
+
});
|
|
84
|
+
});
|