@malloy-publisher/sdk 0.9.2 → 0.9.4
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/dist/{RenderedResult-CBLHr32C.es.js → RenderedResult-DwB0-9Yx.es.js} +118 -119
- package/dist/{RenderedResult-B48wAjM8.cjs.js → RenderedResult-hKjFEqaN.cjs.js} +2 -2
- package/dist/{ServerProvider-DCV8BUWD.es.js → ServerProvider-CcSrGaRN.es.js} +1281 -1142
- package/dist/ServerProvider-Gbf1YPOP.cjs.js +1 -0
- package/dist/builder/index.cjs.js +112 -115
- package/dist/builder/index.es.js +11766 -11502
- package/dist/builder-entry.d.ts +1 -1
- package/dist/client/api.d.ts +170 -9
- package/dist/client/index.cjs.js +1 -1
- package/dist/client/index.es.js +93 -89
- package/dist/client-entry.d.ts +2 -0
- package/dist/components/ApiErrorDisplay.d.ts +11 -0
- package/dist/components/AppDialog.d.ts +6 -6
- package/dist/components/Dashboard/DashboardGrid.d.ts +18 -3
- package/dist/components/Dashboard/DashboardTile.d.ts +12 -2
- package/dist/components/Dashboard/DashboardView.d.ts +38 -3
- package/dist/components/Dashboard/TileCard.d.ts +16 -2
- package/dist/components/Dashboard/dropValueUrlTags.d.ts +6 -0
- package/dist/components/Dashboard/index.d.ts +3 -2
- package/dist/components/Dashboard/telemetry.d.ts +2 -1
- package/dist/components/Dashboard/textSource.d.ts +19 -0
- package/dist/components/DashboardBuilder/AddTileDialog.d.ts +7 -8
- package/dist/components/DashboardBuilder/BuilderDialogs.d.ts +25 -0
- package/dist/components/DashboardBuilder/BuilderGrid.d.ts +31 -0
- package/dist/components/DashboardBuilder/BuilderHeader.d.ts +17 -0
- package/dist/components/DashboardBuilder/BuilderToolbar.d.ts +14 -17
- package/dist/components/DashboardBuilder/ChartPicker.d.ts +3 -1
- package/dist/components/DashboardBuilder/DashboardBuilder.d.ts +15 -7
- package/dist/components/DashboardBuilder/DashboardEditor.d.ts +28 -2
- package/dist/components/DashboardBuilder/EditorSurface.d.ts +33 -0
- package/dist/components/DashboardBuilder/FilterStrip.d.ts +6 -4
- package/dist/components/DashboardBuilder/InlineText.d.ts +1 -1
- package/dist/components/DashboardBuilder/NotebookEditor.d.ts +1 -1
- package/dist/components/DashboardBuilder/TextTileBody.d.ts +4 -1
- package/dist/components/DashboardBuilder/TileFrame.d.ts +43 -6
- package/dist/components/DashboardBuilder/TileMenu.d.ts +6 -8
- package/dist/components/DashboardBuilder/builderReport.d.ts +13 -0
- package/dist/components/DashboardBuilder/catalog.d.ts +0 -18
- package/dist/components/DashboardBuilder/documentSession.d.ts +2 -2
- package/dist/components/DashboardBuilder/imports.d.ts +19 -0
- package/dist/components/DashboardBuilder/inlineEdit.d.ts +18 -0
- package/dist/components/DashboardBuilder/readDocument.d.ts +3 -1
- package/dist/components/DashboardBuilder/readForEditor.d.ts +1 -1
- package/dist/components/DashboardBuilder/sortable.d.ts +2 -0
- package/dist/components/DashboardBuilder/spliceDocument.d.ts +2 -0
- package/dist/components/DashboardBuilder/testing/resize.d.ts +7 -0
- package/dist/components/DashboardBuilder/tileDisplayTitle.d.ts +7 -0
- package/dist/components/DashboardBuilder/useBuilderSelection.d.ts +53 -0
- package/dist/components/DashboardBuilder/useBuilderSession.d.ts +4 -7
- package/dist/components/DashboardBuilder/useControlBindings.d.ts +30 -0
- package/dist/components/DashboardBuilder/useConversionConfirm.d.ts +20 -0
- package/dist/components/DashboardBuilder/useDashboardEditor.d.ts +2 -0
- package/dist/components/DashboardBuilder/useDraft.d.ts +3 -1
- package/dist/components/DashboardBuilder/useHostCopy.d.ts +26 -0
- package/dist/components/DashboardBuilder/useOpenDraftCommit.d.ts +17 -0
- package/dist/components/DashboardBuilder/useOpenedDocument.d.ts +71 -0
- package/dist/components/DashboardBuilder/useSaveChannel.d.ts +50 -0
- package/dist/components/DashboardBuilder/useTileEditing.d.ts +33 -0
- package/dist/components/DashboardBuilder/useTileResize.d.ts +32 -0
- package/dist/components/Loading.d.ts +13 -0
- package/dist/components/Notebook/NotebookCell.d.ts +1 -5
- package/dist/components/Package/Package.d.ts +16 -1
- package/dist/components/Package/index.d.ts +1 -1
- package/dist/components/Prose.d.ts +1 -1
- package/dist/components/RenderedResult/loadRenderer.d.ts +7 -0
- package/dist/components/given/paramCodec.d.ts +8 -1
- package/dist/components/index.d.ts +1 -2
- package/dist/components/styles.d.ts +4 -4
- package/dist/hooks/givenValue.d.ts +6 -0
- package/dist/hooks/index.d.ts +1 -0
- package/dist/hooks/useCompiledDocument.d.ts +23 -0
- package/dist/hooks/useDocumentControls.d.ts +6 -1
- package/dist/hooks/useNearViewport.d.ts +16 -0
- package/dist/hooks/useQueryWithApiError.d.ts +7 -2
- package/dist/hooks/useSuggestOptions.d.ts +9 -2
- package/dist/index.cjs.js +14 -14
- package/dist/index.es.js +9760 -10363
- package/dist/text/index.es.js +1 -1
- package/dist/{text-entry-CbbBcZl8.es.js → text-entry-CA0LZ1xL.es.js} +8 -8
- package/dist/text-entry.d.ts +1 -1
- package/dist/theme/accent.d.ts +29 -0
- package/dist/theme/buildVegaThemeOverride.d.ts +3 -3
- package/dist/theme/index.d.ts +3 -0
- package/dist/theme/keys.d.ts +1 -1
- package/dist/theme/motion.d.ts +31 -0
- package/dist/theme/readChartAnnotations.d.ts +1 -1
- package/dist/theme/resolveTheme.d.ts +4 -4
- package/dist/theme/types.d.ts +33 -5
- package/dist/{DashboardTile-BFcxdv7R.es.js → useCompiledDocument-BU6d8FPp.es.js} +6978 -6289
- package/dist/useCompiledDocument-pOg2W-Yt.cjs.js +142 -0
- package/package.json +11 -11
- package/src/builder-entry.ts +1 -0
- package/src/client-entry.ts +8 -0
- package/src/clientEntry.spec.ts +17 -0
- package/src/components/ApiErrorDisplay.tsx +37 -18
- package/src/components/AppDialog.tsx +8 -7
- package/src/components/Dashboard/Dashboard.spec.tsx +3 -32
- package/src/components/Dashboard/Dashboard.tsx +2 -2
- package/src/components/Dashboard/DashboardGrid.tsx +23 -5
- package/src/components/Dashboard/DashboardTile.spec.tsx +97 -2
- package/src/components/Dashboard/DashboardTile.tsx +95 -31
- package/src/components/Dashboard/DashboardView.spec.tsx +156 -0
- package/src/components/Dashboard/DashboardView.tsx +165 -89
- package/src/components/Dashboard/RowsDialog.tsx +3 -2
- package/src/components/Dashboard/TileCard.tsx +17 -2
- package/src/components/Dashboard/dropValueUrlTags.spec.ts +177 -0
- package/src/components/Dashboard/dropValueUrlTags.ts +108 -0
- package/src/components/Dashboard/index.ts +11 -2
- package/src/components/Dashboard/telemetry.ts +2 -1
- package/src/components/Dashboard/textSource.spec.ts +36 -0
- package/src/components/Dashboard/textSource.ts +39 -0
- package/src/components/DashboardBuilder/AddTileDialog.tsx +16 -56
- package/src/components/DashboardBuilder/BuilderDialogs.tsx +162 -0
- package/src/components/DashboardBuilder/BuilderGrid.tsx +245 -0
- package/src/components/DashboardBuilder/BuilderHeader.tsx +107 -0
- package/src/components/DashboardBuilder/BuilderToolbar.tsx +86 -113
- package/src/components/DashboardBuilder/ChartPicker.tsx +4 -1
- package/src/components/DashboardBuilder/DashboardBuilder.charts.spec.tsx +5 -5
- package/src/components/DashboardBuilder/DashboardBuilder.exit.spec.tsx +74 -83
- package/src/components/DashboardBuilder/DashboardBuilder.inline.spec.tsx +62 -18
- package/src/components/DashboardBuilder/DashboardBuilder.notebook.spec.tsx +55 -108
- package/src/components/DashboardBuilder/DashboardBuilder.openDraft.spec.tsx +10 -62
- package/src/components/DashboardBuilder/DashboardBuilder.sources.spec.tsx +50 -57
- package/src/components/DashboardBuilder/DashboardBuilder.spec.tsx +53 -40
- package/src/components/DashboardBuilder/DashboardBuilder.textTiles.spec.tsx +10 -8
- package/src/components/DashboardBuilder/DashboardBuilder.tsx +227 -704
- package/src/components/DashboardBuilder/DashboardEditor.authoritative.spec.tsx +32 -52
- package/src/components/DashboardBuilder/DashboardEditor.emptyStart.spec.tsx +1 -50
- package/src/components/DashboardBuilder/DashboardEditor.notebook.spec.tsx +12 -18
- package/src/components/DashboardBuilder/DashboardEditor.package.spec.tsx +4 -24
- package/src/components/DashboardBuilder/DashboardEditor.resourceUri.spec.tsx +4 -4
- package/src/components/DashboardBuilder/DashboardEditor.spec.tsx +8 -13
- package/src/components/DashboardBuilder/DashboardEditor.textSource.spec.tsx +317 -0
- package/src/components/DashboardBuilder/DashboardEditor.tsx +142 -820
- package/src/components/DashboardBuilder/DrillDialog.tsx +2 -2
- package/src/components/DashboardBuilder/EditorSurface.tsx +471 -0
- package/src/components/DashboardBuilder/FilterDialog.tsx +13 -8
- package/src/components/DashboardBuilder/FilterStrip.tsx +117 -79
- package/src/components/DashboardBuilder/InlineMarkdown.tsx +7 -9
- package/src/components/DashboardBuilder/InlineText.tsx +23 -17
- package/src/components/DashboardBuilder/NotebookEditor.tsx +3 -3
- package/src/components/DashboardBuilder/TextTileBody.tsx +5 -2
- package/src/components/DashboardBuilder/TileFrame.tsx +255 -90
- package/src/components/DashboardBuilder/TileMenu.tsx +41 -91
- package/src/components/DashboardBuilder/artifactBlock.spec.ts +1 -1
- package/src/components/DashboardBuilder/artifactTagParity.spec.ts +9 -4
- package/src/components/DashboardBuilder/builderReport.ts +74 -0
- package/src/components/DashboardBuilder/catalog.spec.ts +0 -44
- package/src/components/DashboardBuilder/catalog.ts +0 -36
- package/src/components/DashboardBuilder/controls.spec.ts +1 -1
- package/src/components/DashboardBuilder/controls.ts +1 -1
- package/src/components/DashboardBuilder/documentSession.ts +3 -2
- package/src/components/DashboardBuilder/imports.spec.ts +61 -0
- package/src/components/DashboardBuilder/imports.ts +93 -0
- package/src/components/DashboardBuilder/inlineEdit.tsx +78 -0
- package/src/components/DashboardBuilder/legacyNotebook.ts +1 -1
- package/src/components/DashboardBuilder/malloyTree.ts +5 -1
- package/src/components/DashboardBuilder/newDashboard.ts +1 -1
- package/src/components/DashboardBuilder/preview.ts +1 -1
- package/src/components/DashboardBuilder/readDocument.spec.ts +36 -0
- package/src/components/DashboardBuilder/readDocument.ts +10 -4
- package/src/components/DashboardBuilder/readForEditor.ts +7 -2
- package/src/components/DashboardBuilder/sortable.spec.ts +25 -1
- package/src/components/DashboardBuilder/sortable.tsx +11 -0
- package/src/components/DashboardBuilder/spliceDocument.ts +8 -3
- package/src/components/DashboardBuilder/testing/artifactTagParity.json +62 -6
- package/src/components/DashboardBuilder/testing/documentKindRule.json +40 -0
- package/src/components/DashboardBuilder/testing/inline.ts +1 -1
- package/src/components/DashboardBuilder/testing/resize.ts +48 -0
- package/src/components/DashboardBuilder/testing/urlRenderTags.json +62 -0
- package/src/components/DashboardBuilder/textTiles.spec.ts +43 -1
- package/src/components/DashboardBuilder/tileDisplayTitle.ts +13 -0
- package/src/components/DashboardBuilder/useBuilderSelection.ts +121 -0
- package/src/components/DashboardBuilder/useBuilderSession.spec.ts +0 -85
- package/src/components/DashboardBuilder/useBuilderSession.ts +6 -43
- package/src/components/DashboardBuilder/useBuilderShortcuts.overlay.spec.tsx +32 -0
- package/src/components/DashboardBuilder/useBuilderShortcuts.ts +11 -1
- package/src/components/DashboardBuilder/useControlBindings.ts +152 -0
- package/src/components/DashboardBuilder/useConversionConfirm.ts +55 -0
- package/src/components/DashboardBuilder/useDashboardEditor.ts +5 -2
- package/src/components/DashboardBuilder/useDraft.ts +15 -2
- package/src/components/DashboardBuilder/useHostCopy.ts +104 -0
- package/src/components/DashboardBuilder/useOpenDraftCommit.ts +47 -0
- package/src/components/DashboardBuilder/useOpenedDocument.ts +314 -0
- package/src/components/DashboardBuilder/useSaveChannel.ts +275 -0
- package/src/components/DashboardBuilder/useTileEditing.ts +141 -0
- package/src/components/DashboardBuilder/useTileResize.spec.tsx +146 -0
- package/src/components/DashboardBuilder/useTileResize.ts +133 -0
- package/src/components/DataAppViewer/DataAppViewer.tsx +7 -4
- package/src/components/DocumentCreate/guards.ts +1 -1
- package/src/components/Environment/Environment.tsx +0 -10
- package/src/components/Loading.tsx +14 -0
- package/src/components/Model/Model.tsx +16 -9
- package/src/components/Model/ModelExplorer.tsx +2 -2
- package/src/components/Notebook/Notebook.tsx +6 -2
- package/src/components/Notebook/NotebookCell.tsx +90 -215
- package/src/components/Package/Package.artifacts.spec.tsx +8 -12
- package/src/components/Package/Package.create.spec.tsx +22 -1
- package/src/components/Package/Package.tsx +51 -27
- package/src/components/Package/index.ts +1 -1
- package/src/components/Prose.spec.tsx +37 -0
- package/src/components/Prose.tsx +35 -5
- package/src/components/RenderedResult/RenderedResult.tsx +5 -7
- package/src/components/RenderedResult/ResultContainer.tsx +7 -2
- package/src/components/RenderedResult/ResultPanel.tsx +20 -5
- package/src/components/RenderedResult/loadRenderer.ts +42 -0
- package/src/components/given/GivenInput.spec.tsx +5 -5
- package/src/components/given/GivenInput.tsx +46 -38
- package/src/components/given/GivensPanel.tsx +2 -8
- package/src/components/given/paramCodec.ts +27 -1
- package/src/components/index.ts +1 -2
- package/src/components/styles.ts +4 -5
- package/src/hooks/givenValue.ts +9 -0
- package/src/hooks/index.ts +4 -0
- package/src/hooks/useCompiledDocument.spec.tsx +86 -0
- package/src/hooks/useCompiledDocument.ts +59 -0
- package/src/hooks/useDimensionalFilterRangeData.ts +3 -1
- package/src/hooks/useDocumentControls.ts +13 -1
- package/src/hooks/useNearViewport.spec.tsx +150 -0
- package/src/hooks/useNearViewport.ts +102 -0
- package/src/hooks/useQueryResult.ts +7 -1
- package/src/hooks/useQueryWithApiError.ts +10 -3
- package/src/hooks/useRawQueryData.ts +3 -1
- package/src/hooks/useSuggestOptions.dom.spec.ts +40 -1
- package/src/hooks/useSuggestOptions.ts +42 -10
- package/src/text-entry.ts +1 -1
- package/src/textEntry.spec.ts +2 -8
- package/src/theme/accent.spec.ts +116 -0
- package/src/theme/accent.ts +148 -0
- package/src/theme/buildTableCssVars.spec.ts +24 -0
- package/src/theme/buildTableCssVars.ts +1 -2
- package/src/theme/buildVegaThemeOverride.spec.ts +38 -2
- package/src/theme/buildVegaThemeOverride.ts +5 -5
- package/src/theme/defaults.ts +34 -0
- package/src/theme/index.ts +9 -0
- package/src/theme/keys.ts +6 -0
- package/src/theme/motion.ts +44 -0
- package/src/theme/readChartAnnotations.spec.ts +18 -0
- package/src/theme/readChartAnnotations.ts +1 -1
- package/src/theme/resolveTheme.spec.ts +73 -0
- package/src/theme/resolveTheme.ts +61 -20
- package/src/theme/types.ts +30 -5
- package/src/utils/queryClient.ts +8 -0
- package/dist/DashboardTile-sg8LjUk8.cjs.js +0 -137
- package/dist/ServerProvider-HRqT3w1C.cjs.js +0 -1
- package/dist/components/Dashboard/DashboardBar.d.ts +0 -21
- package/dist/components/Dashboard/ExploreDialog.d.ts +0 -20
- package/dist/components/DashboardBuilder/DiffDialog.d.ts +0 -7
- package/dist/components/DashboardBuilder/SaveNotice.d.ts +0 -27
- package/dist/components/DashboardBuilder/SettingsPopover.d.ts +0 -32
- package/dist/components/DashboardBuilder/diff.d.ts +0 -35
- package/src/components/Dashboard/DashboardBar.tsx +0 -88
- package/src/components/Dashboard/ExploreDialog.tsx +0 -89
- package/src/components/DashboardBuilder/DashboardBuilder.notice.spec.tsx +0 -194
- package/src/components/DashboardBuilder/DiffDialog.spec.tsx +0 -25
- package/src/components/DashboardBuilder/DiffDialog.tsx +0 -103
- package/src/components/DashboardBuilder/SaveNotice.tsx +0 -113
- package/src/components/DashboardBuilder/SettingsPopover.spec.tsx +0 -87
- package/src/components/DashboardBuilder/SettingsPopover.tsx +0 -326
- package/src/components/DashboardBuilder/diff.spec.ts +0 -44
- package/src/components/DashboardBuilder/diff.ts +0 -93
- /package/dist/{components/DashboardBuilder → utils}/malloyText.d.ts +0 -0
- /package/src/{components/DashboardBuilder → utils}/malloyText.spec.ts +0 -0
- /package/src/{components/DashboardBuilder → utils}/malloyText.ts +0 -0
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import type { QueryClient } from "@tanstack/react-query";
|
|
5
|
+
import {
|
|
6
|
+
useCallback,
|
|
7
|
+
useState,
|
|
8
|
+
type Dispatch,
|
|
9
|
+
type MutableRefObject,
|
|
10
|
+
type SetStateAction,
|
|
11
|
+
} from "react";
|
|
12
|
+
import { modelResultsKey } from "../../hooks/useQueryResult";
|
|
13
|
+
import { encodeResourceUri } from "../../utils/formatting";
|
|
14
|
+
import {
|
|
15
|
+
isDocumentNotFound,
|
|
16
|
+
type DocumentStorage,
|
|
17
|
+
type Workspace,
|
|
18
|
+
} from "../DocumentStorage";
|
|
19
|
+
import { locatorFor } from "../DocumentCreate/documentPath";
|
|
20
|
+
import type { useServer } from "../ServerProvider";
|
|
21
|
+
import type { DocumentKind } from "./document";
|
|
22
|
+
import {
|
|
23
|
+
expectedHashFor,
|
|
24
|
+
saveTarget,
|
|
25
|
+
storageErrorMessage,
|
|
26
|
+
writePackageFile,
|
|
27
|
+
} from "./documentSession";
|
|
28
|
+
import type { OpenedDocument } from "./useOpenedDocument";
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Where Save writes, and the write itself: into the host's store, or into the
|
|
32
|
+
* package, and what each write does to the editor's idea of which text is
|
|
33
|
+
* open, which was seen, and what the next save is spliced against.
|
|
34
|
+
*/
|
|
35
|
+
export function useSaveChannel({
|
|
36
|
+
storage,
|
|
37
|
+
workspace,
|
|
38
|
+
kind,
|
|
39
|
+
notebook,
|
|
40
|
+
environmentName,
|
|
41
|
+
packageName,
|
|
42
|
+
modelPath,
|
|
43
|
+
versionId,
|
|
44
|
+
apiClients,
|
|
45
|
+
queryClient,
|
|
46
|
+
mutable,
|
|
47
|
+
textHeld,
|
|
48
|
+
readFailure,
|
|
49
|
+
authoritative,
|
|
50
|
+
resume,
|
|
51
|
+
setResume,
|
|
52
|
+
setOpened,
|
|
53
|
+
setDraft,
|
|
54
|
+
setOffered,
|
|
55
|
+
setSeen,
|
|
56
|
+
setWrote,
|
|
57
|
+
savedHashRef,
|
|
58
|
+
packageBaseRef,
|
|
59
|
+
fetchedAtRef,
|
|
60
|
+
}: {
|
|
61
|
+
storage: DocumentStorage | undefined;
|
|
62
|
+
workspace: Workspace | undefined;
|
|
63
|
+
kind: DocumentKind;
|
|
64
|
+
notebook: boolean;
|
|
65
|
+
environmentName: string;
|
|
66
|
+
packageName: string;
|
|
67
|
+
modelPath: string;
|
|
68
|
+
versionId: string | undefined;
|
|
69
|
+
apiClients: ReturnType<typeof useServer>["apiClients"];
|
|
70
|
+
queryClient: QueryClient;
|
|
71
|
+
/** Whether the server takes writes; unknown while `/status` loads. */
|
|
72
|
+
mutable: boolean | undefined;
|
|
73
|
+
/** A document held as text is saved only to the host's record, never into the package. */
|
|
74
|
+
textHeld: boolean;
|
|
75
|
+
readFailure: string | undefined;
|
|
76
|
+
authoritative: boolean;
|
|
77
|
+
resume: boolean | undefined;
|
|
78
|
+
setResume: Dispatch<SetStateAction<boolean | undefined>>;
|
|
79
|
+
setOpened: Dispatch<SetStateAction<OpenedDocument | undefined>>;
|
|
80
|
+
setDraft: Dispatch<SetStateAction<string | undefined>>;
|
|
81
|
+
setOffered: Dispatch<SetStateAction<boolean>>;
|
|
82
|
+
setSeen: Dispatch<SetStateAction<string | undefined>>;
|
|
83
|
+
setWrote: Dispatch<
|
|
84
|
+
SetStateAction<{ text: string; onFetch: number } | undefined>
|
|
85
|
+
>;
|
|
86
|
+
savedHashRef: MutableRefObject<string | undefined>;
|
|
87
|
+
packageBaseRef: MutableRefObject<string | undefined>;
|
|
88
|
+
fetchedAtRef: MutableRefObject<number>;
|
|
89
|
+
}) {
|
|
90
|
+
const locator =
|
|
91
|
+
workspace === undefined
|
|
92
|
+
? undefined
|
|
93
|
+
: locatorFor(
|
|
94
|
+
kind,
|
|
95
|
+
workspace.name,
|
|
96
|
+
environmentName,
|
|
97
|
+
packageName,
|
|
98
|
+
modelPath,
|
|
99
|
+
);
|
|
100
|
+
// Into the host's store: the record when the host says so, and otherwise a
|
|
101
|
+
// copy kept beside the package.
|
|
102
|
+
const saveToStorage = useCallback(
|
|
103
|
+
async (source: string) => {
|
|
104
|
+
if (!storage || !locator)
|
|
105
|
+
throw new Error(
|
|
106
|
+
"This host keeps no documents, so there is nowhere to save.",
|
|
107
|
+
);
|
|
108
|
+
await storage.saveDocument(locator, source);
|
|
109
|
+
// The builder keeps its history and its text; what it holds is now
|
|
110
|
+
// this, not the text it was opened with.
|
|
111
|
+
setOpened((previous) => previous && { ...previous, source });
|
|
112
|
+
setDraft(source);
|
|
113
|
+
// Whenever the copy IS the channel — the record, or a copy the reader
|
|
114
|
+
// resumed — the write moved that channel, and its own text is not an
|
|
115
|
+
// incoming version. A copy written while the package is what is open
|
|
116
|
+
// leaves the package where it was, so nothing there has moved.
|
|
117
|
+
if (authoritative || resume === true) setSeen(source);
|
|
118
|
+
// Saving without choosing is choosing the package file.
|
|
119
|
+
if (!authoritative) setResume((chosen) => chosen ?? false);
|
|
120
|
+
},
|
|
121
|
+
[
|
|
122
|
+
storage,
|
|
123
|
+
locator,
|
|
124
|
+
authoritative,
|
|
125
|
+
resume,
|
|
126
|
+
setOpened,
|
|
127
|
+
setDraft,
|
|
128
|
+
setSeen,
|
|
129
|
+
setResume,
|
|
130
|
+
],
|
|
131
|
+
);
|
|
132
|
+
// A supersede that did not happen, left where a reader can see it: the copy
|
|
133
|
+
// is still there and will be offered again on the next visit.
|
|
134
|
+
const [supersedeFailure, setSupersedeFailure] = useState<string | undefined>(
|
|
135
|
+
undefined,
|
|
136
|
+
);
|
|
137
|
+
// Into the package itself, when the server takes writes: compile-checked,
|
|
138
|
+
// written atomically and reloaded there, refused if the file changed since
|
|
139
|
+
// it was opened. A copy of the same file kept beside it is superseded by it.
|
|
140
|
+
const saveToPackage = useCallback(
|
|
141
|
+
async (source: string) => {
|
|
142
|
+
// The hash of the package file this editor opened against, never of
|
|
143
|
+
// the latest fetch. A version held back while the reader keeps
|
|
144
|
+
// editing moves the fetch past that file, and a hash taken from it
|
|
145
|
+
// would match, be accepted, and overwrite the change the reader was
|
|
146
|
+
// just told about.
|
|
147
|
+
const expectedHash = await expectedHashFor(
|
|
148
|
+
savedHashRef.current,
|
|
149
|
+
packageBaseRef.current,
|
|
150
|
+
);
|
|
151
|
+
if (expectedHash === undefined)
|
|
152
|
+
throw new Error("The package file is still loading; try again.");
|
|
153
|
+
const modelKey = [
|
|
154
|
+
"dashboard-editor-model",
|
|
155
|
+
environmentName,
|
|
156
|
+
packageName,
|
|
157
|
+
modelPath,
|
|
158
|
+
versionId,
|
|
159
|
+
notebook,
|
|
160
|
+
];
|
|
161
|
+
await writePackageFile({
|
|
162
|
+
apiClients,
|
|
163
|
+
queryClient,
|
|
164
|
+
environmentName,
|
|
165
|
+
packageName,
|
|
166
|
+
modelPath,
|
|
167
|
+
source,
|
|
168
|
+
expectedHash,
|
|
169
|
+
// A refused write usually means the file moved: fetching it offers the reader that version instead of re-saving against a base the server keeps rejecting.
|
|
170
|
+
invalidateOnError: [modelKey],
|
|
171
|
+
afterWrite: async (contentHash) => {
|
|
172
|
+
setOpened((previous) => previous && { ...previous, source });
|
|
173
|
+
setWrote({ text: source, onFetch: fetchedAtRef.current });
|
|
174
|
+
setSeen(source);
|
|
175
|
+
savedHashRef.current = contentHash;
|
|
176
|
+
setSupersedeFailure(undefined);
|
|
177
|
+
if (storage && locator) {
|
|
178
|
+
try {
|
|
179
|
+
await storage.deleteDocument(locator);
|
|
180
|
+
setDraft(undefined);
|
|
181
|
+
setOffered(false);
|
|
182
|
+
} catch (error) {
|
|
183
|
+
// Only absence means the copy is gone; any other rejection leaves it there, so the state must keep saying so.
|
|
184
|
+
if (isDocumentNotFound(error)) {
|
|
185
|
+
setDraft(undefined);
|
|
186
|
+
setOffered(false);
|
|
187
|
+
} else setSupersedeFailure(storageErrorMessage(error));
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
// The package is what is open now even if the copy survived the supersede; reading the channel off a stale copy would put the builder back on the text this save replaced.
|
|
191
|
+
setResume(false);
|
|
192
|
+
},
|
|
193
|
+
invalidate: [
|
|
194
|
+
{ queryKey: modelKey, wait: true },
|
|
195
|
+
// A cached result is keyed on the query text, not the file, so a changed chart would otherwise be drawn from the old rows.
|
|
196
|
+
{
|
|
197
|
+
queryKey: modelResultsKey({
|
|
198
|
+
environmentName,
|
|
199
|
+
packageName,
|
|
200
|
+
versionId,
|
|
201
|
+
modelPath,
|
|
202
|
+
}),
|
|
203
|
+
},
|
|
204
|
+
{ queryKey: ["dashboard-editor-manifest"] },
|
|
205
|
+
{ queryKey: ["dashboards"] },
|
|
206
|
+
{ queryKey: ["dashboard"] },
|
|
207
|
+
// The notebook viewer keys its read on the resource URI.
|
|
208
|
+
...(notebook
|
|
209
|
+
? [
|
|
210
|
+
{
|
|
211
|
+
queryKey: [
|
|
212
|
+
encodeResourceUri({
|
|
213
|
+
environmentName,
|
|
214
|
+
packageName,
|
|
215
|
+
modelPath,
|
|
216
|
+
}),
|
|
217
|
+
],
|
|
218
|
+
},
|
|
219
|
+
]
|
|
220
|
+
: []),
|
|
221
|
+
],
|
|
222
|
+
});
|
|
223
|
+
},
|
|
224
|
+
[
|
|
225
|
+
apiClients,
|
|
226
|
+
environmentName,
|
|
227
|
+
packageName,
|
|
228
|
+
modelPath,
|
|
229
|
+
versionId,
|
|
230
|
+
notebook,
|
|
231
|
+
storage,
|
|
232
|
+
locator,
|
|
233
|
+
queryClient,
|
|
234
|
+
savedHashRef,
|
|
235
|
+
packageBaseRef,
|
|
236
|
+
fetchedAtRef,
|
|
237
|
+
setOpened,
|
|
238
|
+
setWrote,
|
|
239
|
+
setSeen,
|
|
240
|
+
setDraft,
|
|
241
|
+
setOffered,
|
|
242
|
+
setResume,
|
|
243
|
+
],
|
|
244
|
+
);
|
|
245
|
+
const canWriteWorkspace = workspace?.writeable === true;
|
|
246
|
+
// Unknown while `/status` loads, and a package write needs a yes.
|
|
247
|
+
const takesWrites = mutable === true && !textHeld;
|
|
248
|
+
const { savesTo, pinnedPackageSave, writer } = saveTarget({
|
|
249
|
+
authoritative,
|
|
250
|
+
mutable: takesWrites,
|
|
251
|
+
...(versionId !== undefined ? { versionId } : {}),
|
|
252
|
+
// A notebook reads back only the record, so a copy anywhere else would be a write nobody sees.
|
|
253
|
+
canStore:
|
|
254
|
+
!!storage &&
|
|
255
|
+
!!locator &&
|
|
256
|
+
canWriteWorkspace &&
|
|
257
|
+
(!notebook || authoritative),
|
|
258
|
+
readFailed: readFailure !== undefined,
|
|
259
|
+
});
|
|
260
|
+
const save =
|
|
261
|
+
writer === "storage"
|
|
262
|
+
? saveToStorage
|
|
263
|
+
: writer === "package"
|
|
264
|
+
? saveToPackage
|
|
265
|
+
: undefined;
|
|
266
|
+
|
|
267
|
+
return {
|
|
268
|
+
save,
|
|
269
|
+
savesTo,
|
|
270
|
+
writer,
|
|
271
|
+
pinnedPackageSave,
|
|
272
|
+
takesWrites,
|
|
273
|
+
supersedeFailure,
|
|
274
|
+
};
|
|
275
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { useState } from "react";
|
|
5
|
+
import type { NewTile } from "./AddTileDialog";
|
|
6
|
+
import {
|
|
7
|
+
isQueryTile,
|
|
8
|
+
isTextTile,
|
|
9
|
+
tileKey,
|
|
10
|
+
type DashboardTile,
|
|
11
|
+
} from "./document";
|
|
12
|
+
import { withSource } from "./imports";
|
|
13
|
+
import type { DashboardEditor } from "./useDashboardEditor";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Adding, changing and removing tiles, with the add-tile picker and a tile's
|
|
17
|
+
* menu that they are reached from.
|
|
18
|
+
*/
|
|
19
|
+
export function useTileEditing({
|
|
20
|
+
editor,
|
|
21
|
+
notebook,
|
|
22
|
+
columns,
|
|
23
|
+
modelPath,
|
|
24
|
+
selectTile,
|
|
25
|
+
deselectTile,
|
|
26
|
+
}: {
|
|
27
|
+
editor: DashboardEditor;
|
|
28
|
+
notebook: boolean;
|
|
29
|
+
columns: number;
|
|
30
|
+
/** The document's file within the package, which a tile's import is relative to. */
|
|
31
|
+
modelPath: string | undefined;
|
|
32
|
+
selectTile: (index: number) => void;
|
|
33
|
+
deselectTile: () => void;
|
|
34
|
+
}) {
|
|
35
|
+
// A tile's menu, anchored to the button that opened it.
|
|
36
|
+
const [menu, setMenu] = useState<
|
|
37
|
+
{ anchor: HTMLElement; index: number } | undefined
|
|
38
|
+
>(undefined);
|
|
39
|
+
// The add-tile picker.
|
|
40
|
+
const [addingTile, setAddingTile] = useState(false);
|
|
41
|
+
// Where the next added tile lands; undefined appends.
|
|
42
|
+
const [insertAt, setInsertAt] = useState<number | undefined>(undefined);
|
|
43
|
+
const openAdd = (at?: number) => {
|
|
44
|
+
setInsertAt(at);
|
|
45
|
+
setAddingTile(true);
|
|
46
|
+
};
|
|
47
|
+
const closeAdd = () => setAddingTile(false);
|
|
48
|
+
|
|
49
|
+
/** A tile from the picker: on the extension of its source, or a new one. */
|
|
50
|
+
const addTile = (tile: NewTile) => {
|
|
51
|
+
setAddingTile(false);
|
|
52
|
+
editor.update((draft) => {
|
|
53
|
+
// A source the file cannot see yet comes in by name, with the tile.
|
|
54
|
+
if (tile.modelPath)
|
|
55
|
+
draft.imports = withSource(
|
|
56
|
+
draft,
|
|
57
|
+
tile.base,
|
|
58
|
+
tile.modelPath,
|
|
59
|
+
...(modelPath ? [modelPath] : []),
|
|
60
|
+
);
|
|
61
|
+
let extension = draft.sources.find((s) => s.base === tile.base);
|
|
62
|
+
if (!extension) {
|
|
63
|
+
// A name of the file's own: the base's, suffixed, since an
|
|
64
|
+
// extension cannot share its base's name.
|
|
65
|
+
const taken = new Set(draft.sources.map((s) => s.name));
|
|
66
|
+
let name = `${tile.base}_tiles`;
|
|
67
|
+
for (let n = 2; taken.has(name); n++)
|
|
68
|
+
name = `${tile.base}_tiles_${n}`;
|
|
69
|
+
extension = { name, base: tile.base };
|
|
70
|
+
draft.sources.push(extension);
|
|
71
|
+
}
|
|
72
|
+
// The view's name in the extension: the base view's, suffixed,
|
|
73
|
+
// because an extension inherits its base's views and cannot redeclare
|
|
74
|
+
// one under the same name; then kept distinct from its siblings.
|
|
75
|
+
const used = new Set(
|
|
76
|
+
draft.tiles
|
|
77
|
+
.filter(isQueryTile)
|
|
78
|
+
.filter((t) => t.source === extension!.name)
|
|
79
|
+
.map((t) => t.name),
|
|
80
|
+
);
|
|
81
|
+
let name = `${tile.view}_tile`;
|
|
82
|
+
for (let n = 2; used.has(name); n++) name = `${tile.view}_tile_${n}`;
|
|
83
|
+
draft.tiles.splice(insertAt ?? draft.tiles.length, 0, {
|
|
84
|
+
name,
|
|
85
|
+
source: extension.name,
|
|
86
|
+
declaration: { kind: "reference", from: tile.view },
|
|
87
|
+
...(notebook ? {} : { colspan: tile.colspan }),
|
|
88
|
+
...(tile.label ? { label: tile.label } : {}),
|
|
89
|
+
...(tile.chart ? { chart: tile.chart } : {}),
|
|
90
|
+
...(tile.chartCarried ? { chartCarried: tile.chartCarried } : {}),
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
selectTile(insertAt ?? editor.document.tiles.length);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/** An empty text tile at the end, named for the first free `text_N`. */
|
|
97
|
+
const addText = () => {
|
|
98
|
+
setAddingTile(false);
|
|
99
|
+
editor.update((draft) => {
|
|
100
|
+
const taken = new Set(
|
|
101
|
+
draft.tiles.filter(isTextTile).map((t) => t.name),
|
|
102
|
+
);
|
|
103
|
+
let n = 1;
|
|
104
|
+
while (taken.has(`text_${n}`)) n++;
|
|
105
|
+
draft.tiles.splice(insertAt ?? draft.tiles.length, 0, {
|
|
106
|
+
kind: "text",
|
|
107
|
+
name: `text_${n}`,
|
|
108
|
+
markdown: "",
|
|
109
|
+
...(notebook ? {} : { colspan: columns }),
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
selectTile(insertAt ?? editor.document.tiles.length);
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/** Change one tile where it stands, found by key so a preview order cannot misdirect it. */
|
|
116
|
+
const editTile = (key: string, change: (tile: DashboardTile) => void) =>
|
|
117
|
+
editor.update((draft) => {
|
|
118
|
+
const tile = draft.tiles.find((each) => tileKey(each) === key);
|
|
119
|
+
if (tile) change(tile);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const removeTile = (index: number) => {
|
|
123
|
+
setMenu(undefined);
|
|
124
|
+
deselectTile();
|
|
125
|
+
editor.update((draft) => {
|
|
126
|
+
draft.tiles.splice(index, 1);
|
|
127
|
+
});
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
menu,
|
|
132
|
+
setMenu,
|
|
133
|
+
addingTile,
|
|
134
|
+
openAdd,
|
|
135
|
+
closeAdd,
|
|
136
|
+
addTile,
|
|
137
|
+
addText,
|
|
138
|
+
editTile,
|
|
139
|
+
removeTile,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { act, renderHook } from "@testing-library/react";
|
|
5
|
+
import { describe, expect, it, mock } from "bun:test";
|
|
6
|
+
import type { PointerEvent } from "react";
|
|
7
|
+
import { GRID_GAP_PX } from "../Dashboard/DashboardGrid";
|
|
8
|
+
import type { DashboardTile } from "./document";
|
|
9
|
+
import { useTileResize } from "./useTileResize";
|
|
10
|
+
|
|
11
|
+
const COLUMNS = 12;
|
|
12
|
+
const GRID_WIDTH = 1200 + GRID_GAP_PX * (COLUMNS - 1);
|
|
13
|
+
const TRACK = 100;
|
|
14
|
+
|
|
15
|
+
const tiles: DashboardTile[] = [
|
|
16
|
+
{
|
|
17
|
+
name: "a",
|
|
18
|
+
source: "s",
|
|
19
|
+
declaration: { kind: "reference", from: "x" },
|
|
20
|
+
colspan: 6,
|
|
21
|
+
},
|
|
22
|
+
{ name: "b", source: "s", declaration: { kind: "inherited" } },
|
|
23
|
+
];
|
|
24
|
+
|
|
25
|
+
/** A pointer event on a handle whose tile starts at `left`, with the grid measured. */
|
|
26
|
+
const pointer = (clientX: number, left = 0) => {
|
|
27
|
+
const handle = document.createElement("div");
|
|
28
|
+
const tile = document.createElement("div");
|
|
29
|
+
tile.appendChild(handle);
|
|
30
|
+
tile.getBoundingClientRect = () =>
|
|
31
|
+
({ left, width: 600, top: 0, height: 100 }) as DOMRect;
|
|
32
|
+
let captured = false;
|
|
33
|
+
Object.assign(handle, {
|
|
34
|
+
setPointerCapture: () => {
|
|
35
|
+
captured = true;
|
|
36
|
+
},
|
|
37
|
+
hasPointerCapture: () => captured,
|
|
38
|
+
releasePointerCapture: () => {
|
|
39
|
+
captured = false;
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
return {
|
|
43
|
+
clientX,
|
|
44
|
+
pointerId: 1,
|
|
45
|
+
currentTarget: handle,
|
|
46
|
+
stopPropagation: mock(() => {}),
|
|
47
|
+
preventDefault: mock(() => {}),
|
|
48
|
+
} as unknown as PointerEvent<HTMLDivElement>;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const mount = () => {
|
|
52
|
+
const commit = mock((_index: number, _span: number) => {});
|
|
53
|
+
const onStart = mock((_index: number) => {});
|
|
54
|
+
const grid = document.createElement("div");
|
|
55
|
+
grid.getBoundingClientRect = () =>
|
|
56
|
+
({ left: 0, width: GRID_WIDTH, top: 0, height: 400 }) as DOMRect;
|
|
57
|
+
const gridBox = { current: grid as HTMLDivElement | null };
|
|
58
|
+
const view = renderHook(() =>
|
|
59
|
+
useTileResize({ tiles, columns: COLUMNS, gridBox, onStart, commit }),
|
|
60
|
+
);
|
|
61
|
+
return { view, commit, onStart };
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
describe("useTileResize", () => {
|
|
65
|
+
it("previews the width in whole columns as the edge moves, and writes it once on release", () => {
|
|
66
|
+
const { view, commit, onStart } = mount();
|
|
67
|
+
act(() => view.result.current.startResize(pointer(600), 0));
|
|
68
|
+
expect(onStart).toHaveBeenCalledWith(0);
|
|
69
|
+
expect(view.result.current.resize).toMatchObject({
|
|
70
|
+
index: 0,
|
|
71
|
+
span: 6,
|
|
72
|
+
track: TRACK,
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// Eight tracks and seven gutters: the edge sits at exactly eight columns.
|
|
76
|
+
act(() =>
|
|
77
|
+
view.result.current.onResize(pointer(8 * TRACK + 7 * GRID_GAP_PX)),
|
|
78
|
+
);
|
|
79
|
+
expect(view.result.current.resize?.span).toBe(8);
|
|
80
|
+
// Part way into the ninth column still rounds to eight; nothing is
|
|
81
|
+
// written yet.
|
|
82
|
+
act(() =>
|
|
83
|
+
view.result.current.onResize(
|
|
84
|
+
pointer(8 * TRACK + 7 * GRID_GAP_PX + TRACK / 3),
|
|
85
|
+
),
|
|
86
|
+
);
|
|
87
|
+
expect(view.result.current.resize?.span).toBe(8);
|
|
88
|
+
expect(commit).not.toHaveBeenCalled();
|
|
89
|
+
|
|
90
|
+
act(() => view.result.current.endResize(pointer(0)));
|
|
91
|
+
expect(commit).toHaveBeenCalledTimes(1);
|
|
92
|
+
expect(commit).toHaveBeenCalledWith(0, 8);
|
|
93
|
+
expect(view.result.current.resize).toBeUndefined();
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("clamps the preview to the grid, one column to full width", () => {
|
|
97
|
+
const { view } = mount();
|
|
98
|
+
act(() => view.result.current.startResize(pointer(600), 0));
|
|
99
|
+
act(() => view.result.current.onResize(pointer(-500)));
|
|
100
|
+
expect(view.result.current.resize?.span).toBe(1);
|
|
101
|
+
act(() => view.result.current.onResize(pointer(GRID_WIDTH * 3)));
|
|
102
|
+
expect(view.result.current.resize?.span).toBe(COLUMNS);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("writes nothing for a press that does not change the width", () => {
|
|
106
|
+
const { view, commit } = mount();
|
|
107
|
+
act(() => view.result.current.startResize(pointer(600), 1));
|
|
108
|
+
act(() => view.result.current.endResize(pointer(600)));
|
|
109
|
+
expect(commit).not.toHaveBeenCalled();
|
|
110
|
+
expect(view.result.current.resize).toBeUndefined();
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("puts the width back when the gesture is cancelled", () => {
|
|
114
|
+
const { view, commit } = mount();
|
|
115
|
+
act(() => view.result.current.startResize(pointer(600), 0));
|
|
116
|
+
act(() =>
|
|
117
|
+
view.result.current.onResize(pointer(8 * TRACK + 7 * GRID_GAP_PX)),
|
|
118
|
+
);
|
|
119
|
+
expect(view.result.current.resize?.span).toBe(8);
|
|
120
|
+
act(() =>
|
|
121
|
+
view.result.current.endResize({
|
|
122
|
+
...pointer(0),
|
|
123
|
+
type: "pointercancel",
|
|
124
|
+
} as PointerEvent<HTMLDivElement>),
|
|
125
|
+
);
|
|
126
|
+
expect(commit).not.toHaveBeenCalled();
|
|
127
|
+
expect(view.result.current.resize).toBeUndefined();
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it("does nothing when the grid has not been measured", () => {
|
|
131
|
+
const commit = mock(() => {});
|
|
132
|
+
const view = renderHook(() =>
|
|
133
|
+
useTileResize({
|
|
134
|
+
tiles,
|
|
135
|
+
columns: COLUMNS,
|
|
136
|
+
gridBox: { current: null },
|
|
137
|
+
onStart: () => {},
|
|
138
|
+
commit,
|
|
139
|
+
}),
|
|
140
|
+
);
|
|
141
|
+
act(() => view.result.current.startResize(pointer(600), 0));
|
|
142
|
+
expect(view.result.current.resize).toBeUndefined();
|
|
143
|
+
act(() => view.result.current.endResize(pointer(0)));
|
|
144
|
+
expect(commit).not.toHaveBeenCalled();
|
|
145
|
+
});
|
|
146
|
+
});
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { useEffect, useRef, useState, type RefObject } from "react";
|
|
5
|
+
import { GRID_GAP_PX } from "../Dashboard/DashboardGrid";
|
|
6
|
+
import type { DashboardTile } from "./document";
|
|
7
|
+
|
|
8
|
+
/** A resize in flight: the tile, the width it WOULD be, and the geometry. */
|
|
9
|
+
export interface ResizeState {
|
|
10
|
+
index: number;
|
|
11
|
+
span: number;
|
|
12
|
+
/** The width the drag started from, so a release that changed nothing writes nothing. */
|
|
13
|
+
from: number;
|
|
14
|
+
left: number;
|
|
15
|
+
track: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* While a RESIZE is live, the page has to stop behaving like a document:
|
|
20
|
+
* dragging an edge across a dashboard otherwise selects the text it crosses and
|
|
21
|
+
* leaves the cursor as whatever it was over. (A MOVE gets the same from the
|
|
22
|
+
* library's own plugins.)
|
|
23
|
+
*/
|
|
24
|
+
const dragChrome = (on: boolean) => {
|
|
25
|
+
const body = window.document.body;
|
|
26
|
+
body.style.userSelect = on ? "none" : "";
|
|
27
|
+
body.style.cursor = on ? "grabbing" : "";
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Dragging a tile's right edge to set its `colspan`. The width is previewed
|
|
32
|
+
* by the caller from `resize.span` and written ONCE, on release: writing on
|
|
33
|
+
* every pointer move would put a dozen documents in the history for one
|
|
34
|
+
* gesture, and undo would walk back through the drag a column at a time.
|
|
35
|
+
*/
|
|
36
|
+
export function useTileResize({
|
|
37
|
+
tiles,
|
|
38
|
+
columns,
|
|
39
|
+
gridBox,
|
|
40
|
+
onStart,
|
|
41
|
+
commit,
|
|
42
|
+
}: {
|
|
43
|
+
tiles: readonly DashboardTile[];
|
|
44
|
+
columns: number;
|
|
45
|
+
/** Wraps the grid, so its content box IS the grid's: what a dragged edge is measured against to work out a column count. */
|
|
46
|
+
gridBox: RefObject<HTMLDivElement | null>;
|
|
47
|
+
/** The tile a resize begins on, which the caller selects. */
|
|
48
|
+
onStart: (index: number) => void;
|
|
49
|
+
/** The width to write when the drag ends. */
|
|
50
|
+
commit: (index: number, span: number) => void;
|
|
51
|
+
}) {
|
|
52
|
+
const [resize, setResizeState] = useState<ResizeState | undefined>(
|
|
53
|
+
undefined,
|
|
54
|
+
);
|
|
55
|
+
// The gesture as the handlers last left it. A release can arrive before the
|
|
56
|
+
// render for the last move does, and reading state there would write the
|
|
57
|
+
// width one column behind where the pointer let go.
|
|
58
|
+
const live = useRef<ResizeState | undefined>(undefined);
|
|
59
|
+
const setResize = (next: ResizeState | undefined) => {
|
|
60
|
+
live.current = next;
|
|
61
|
+
setResizeState(next);
|
|
62
|
+
};
|
|
63
|
+
// A builder unmounted mid-drag (a route change, say) must not leave the
|
|
64
|
+
// whole page unselectable with a grabbing cursor.
|
|
65
|
+
useEffect(() => () => dragChrome(false), []);
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Width is the ONLY thing a resize can change.
|
|
69
|
+
*
|
|
70
|
+
* The format lays tiles out as a flow — `# colspan` for width, `# break` to
|
|
71
|
+
* start a row — with no row index, no column position and no height. So a
|
|
72
|
+
* right edge maps onto `colspan` and persists; a bottom edge has nothing to
|
|
73
|
+
* be written as, and a left edge would mean placing the tile, which the grid
|
|
74
|
+
* cannot express either. Offering those handles would be offering a drag the
|
|
75
|
+
* writer then refuses, which is worse than not offering it.
|
|
76
|
+
*/
|
|
77
|
+
const startResize = (
|
|
78
|
+
event: React.PointerEvent<HTMLDivElement>,
|
|
79
|
+
index: number,
|
|
80
|
+
) => {
|
|
81
|
+
const grid = gridBox.current?.getBoundingClientRect();
|
|
82
|
+
const tile = event.currentTarget.parentElement?.getBoundingClientRect();
|
|
83
|
+
if (!grid || !tile) return;
|
|
84
|
+
// Stops the tile's own click selecting a second time on release.
|
|
85
|
+
event.stopPropagation();
|
|
86
|
+
event.preventDefault();
|
|
87
|
+
event.currentTarget.setPointerCapture(event.pointerId);
|
|
88
|
+
dragChrome(true);
|
|
89
|
+
onStart(index);
|
|
90
|
+
const from = tiles[index]?.colspan ?? 1;
|
|
91
|
+
setResize({
|
|
92
|
+
index,
|
|
93
|
+
span: from,
|
|
94
|
+
from,
|
|
95
|
+
left: tile.left,
|
|
96
|
+
// One column track: the row's width less every gutter in it.
|
|
97
|
+
track: (grid.width - GRID_GAP_PX * (columns - 1)) / columns,
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const onResize = (event: React.PointerEvent<HTMLDivElement>) => {
|
|
102
|
+
const resize = live.current;
|
|
103
|
+
if (!resize) return;
|
|
104
|
+
const width = event.clientX - resize.left;
|
|
105
|
+
// A tile of N tracks is N tracks plus the N-1 gutters between them, so
|
|
106
|
+
// adding one gutter back makes the division land on whole columns.
|
|
107
|
+
const span = Math.round(
|
|
108
|
+
(width + GRID_GAP_PX) / (resize.track + GRID_GAP_PX),
|
|
109
|
+
);
|
|
110
|
+
const clamped = Math.min(Math.max(span, 1), columns);
|
|
111
|
+
if (clamped !== resize.span) setResize({ ...resize, span: clamped });
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const endResize = (event: React.PointerEvent<HTMLDivElement>) => {
|
|
115
|
+
if (event.currentTarget.hasPointerCapture(event.pointerId))
|
|
116
|
+
event.currentTarget.releasePointerCapture(event.pointerId);
|
|
117
|
+
dragChrome(false);
|
|
118
|
+
const resize = live.current;
|
|
119
|
+
if (!resize) return;
|
|
120
|
+
const { index, span, from } = resize;
|
|
121
|
+
setResize(undefined);
|
|
122
|
+
// A cancelled gesture (the browser took the pointer) puts the width back;
|
|
123
|
+
// a press that moved nothing writes nothing, or a tile with no
|
|
124
|
+
// `# colspan` would gain an explicit `colspan=1` from a click.
|
|
125
|
+
if (event.type === "pointercancel" || span === from) return;
|
|
126
|
+
// ONE history entry for the whole drag. Writing on every pointer move
|
|
127
|
+
// would put a dozen documents in the stack for one gesture, and undo
|
|
128
|
+
// would walk back through the drag a column at a time.
|
|
129
|
+
commit(index, span);
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
return { resize, startResize, onResize, endResize };
|
|
133
|
+
}
|