@malloy-publisher/sdk 0.9.0 → 0.9.2
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/{Dashboard-H1kJlDFo.es.js → DashboardTile-BFcxdv7R.es.js} +15918 -17760
- package/dist/DashboardTile-sg8LjUk8.cjs.js +137 -0
- package/dist/RenderedResult-B48wAjM8.cjs.js +141 -0
- package/dist/{RenderedResult-BRgIgxWh.es.js → RenderedResult-CBLHr32C.es.js} +193 -183
- package/dist/{ServerProvider-Boxrv6Ym.es.js → ServerProvider-DCV8BUWD.es.js} +529 -521
- package/dist/ServerProvider-HRqT3w1C.cjs.js +1 -0
- package/dist/builder/index.cjs.js +146 -58
- package/dist/builder/index.es.js +9076 -6479
- package/dist/builder-entry.d.ts +4 -0
- package/dist/client/api.d.ts +82 -48
- 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 +84 -82
- package/dist/components/Dashboard/Dashboard.d.ts +8 -15
- package/dist/components/Dashboard/DashboardGrid.d.ts +23 -6
- package/dist/components/Dashboard/DashboardTile.d.ts +10 -1
- package/dist/components/Dashboard/DashboardView.d.ts +36 -0
- package/dist/components/Dashboard/TileCard.d.ts +15 -4
- package/dist/components/Dashboard/TileFilterTag.d.ts +12 -0
- package/dist/components/Dashboard/index.d.ts +3 -0
- package/dist/components/Dashboard/telemetry.d.ts +15 -3
- package/dist/components/DashboardBuilder/AddTileDialog.d.ts +8 -1
- package/dist/components/DashboardBuilder/BuilderToolbar.d.ts +16 -5
- package/dist/components/DashboardBuilder/ChartPicker.d.ts +29 -0
- package/dist/components/DashboardBuilder/DashboardBuilder.d.ts +48 -21
- package/dist/components/DashboardBuilder/DashboardEditor.d.ts +17 -4
- package/dist/components/DashboardBuilder/DiffDialog.d.ts +2 -10
- package/dist/components/DashboardBuilder/FilterDialog.d.ts +2 -2
- package/dist/components/DashboardBuilder/FilterStrip.d.ts +3 -1
- package/dist/components/DashboardBuilder/InlineMarkdown.d.ts +18 -0
- package/dist/components/DashboardBuilder/InlineText.d.ts +19 -0
- package/dist/components/DashboardBuilder/NotebookEditor.d.ts +15 -0
- package/dist/components/DashboardBuilder/SaveNotice.d.ts +27 -0
- package/dist/components/DashboardBuilder/SettingsPopover.d.ts +19 -7
- package/dist/components/DashboardBuilder/SourceViewPicker.d.ts +9 -0
- package/dist/components/DashboardBuilder/TextTileBody.d.ts +7 -0
- package/dist/components/DashboardBuilder/TileFrame.d.ts +14 -12
- package/dist/components/DashboardBuilder/TileMenu.d.ts +11 -3
- package/dist/components/DashboardBuilder/annotationText.d.ts +4 -0
- package/dist/components/DashboardBuilder/catalog.d.ts +4 -0
- package/dist/components/DashboardBuilder/changedTile.d.ts +7 -0
- package/dist/components/DashboardBuilder/chartLine.d.ts +33 -0
- package/dist/components/DashboardBuilder/controls.d.ts +2 -2
- package/dist/components/DashboardBuilder/document.d.ts +42 -26
- package/dist/components/DashboardBuilder/documentSession.d.ts +100 -0
- package/dist/components/DashboardBuilder/legacyNotebook.d.ts +73 -0
- package/dist/components/DashboardBuilder/loadMalloy.d.ts +15 -0
- package/dist/components/DashboardBuilder/malloyText.d.ts +45 -5
- package/dist/components/DashboardBuilder/malloyTree.d.ts +119 -0
- package/dist/components/DashboardBuilder/newDashboard.d.ts +3 -1
- package/dist/components/DashboardBuilder/openDraft.d.ts +10 -0
- package/dist/components/DashboardBuilder/preview.d.ts +24 -4
- package/dist/components/DashboardBuilder/readDocument.d.ts +4 -1
- package/dist/components/DashboardBuilder/readForEditor.d.ts +16 -0
- package/dist/components/DashboardBuilder/sortable.d.ts +14 -0
- package/dist/components/DashboardBuilder/spliceDocument.d.ts +13 -13
- package/dist/components/DashboardBuilder/spliceResult.d.ts +11 -0
- package/dist/components/DashboardBuilder/tagParse.d.ts +24 -0
- package/dist/components/DashboardBuilder/telemetry.d.ts +43 -0
- package/dist/components/DashboardBuilder/testing/fixtures.d.ts +3 -1
- package/dist/components/DashboardBuilder/testing/inline.d.ts +4 -0
- package/dist/components/DashboardBuilder/useBuilderSession.d.ts +89 -0
- package/dist/components/DashboardBuilder/useBuilderShortcuts.d.ts +9 -6
- package/dist/components/DashboardBuilder/useDashboardEditor.d.ts +17 -55
- package/dist/components/DashboardBuilder/useDocumentEditor.d.ts +105 -0
- package/dist/components/DashboardBuilder/useExitGuard.d.ts +19 -0
- package/dist/components/DashboardBuilder/useFilterForm.d.ts +3 -3
- package/dist/components/DocumentCreate/canRetry.d.ts +6 -0
- package/dist/components/DocumentCreate/createDocument.d.ts +58 -0
- package/dist/components/DocumentCreate/documentPath.d.ts +10 -0
- package/dist/components/DocumentCreate/events.d.ts +10 -0
- package/dist/components/DocumentCreate/exportedSources.d.ts +7 -0
- package/dist/components/DocumentCreate/guards.d.ts +12 -0
- package/dist/components/DocumentCreate/index.d.ts +6 -0
- package/dist/components/DocumentCreate/newNotebook.d.ts +9 -0
- package/dist/components/DocumentCreate/useDocumentChoices.d.ts +32 -0
- package/dist/components/DocumentStorage/DocumentStorage.d.ts +2 -1
- package/dist/components/NarrowEditGate.d.ts +8 -0
- package/dist/components/Notebook/Notebook.d.ts +1 -1
- package/dist/components/Package/NewDocumentDialog.d.ts +33 -0
- package/dist/components/Package/Package.d.ts +1 -0
- package/dist/components/Package/documentLocation.d.ts +28 -0
- package/dist/components/Package/index.d.ts +4 -0
- package/dist/components/Package/useDocumentLocation.d.ts +12 -0
- package/dist/components/PackageSection.d.ts +2 -2
- package/dist/components/RenderedResult/RenderedResult.d.ts +2 -0
- package/dist/components/RenderedResult/ResultContainer.d.ts +8 -1
- package/dist/components/RenderedResult/ResultPanel.d.ts +3 -1
- package/dist/components/RenderedResult/resultSizing.d.ts +9 -1
- package/dist/components/ServerProvider.d.ts +2 -1
- package/dist/components/UnsavedChangesDialog.d.ts +9 -0
- package/dist/components/given/GivensPanel.d.ts +2 -2
- package/dist/components/index.d.ts +2 -0
- package/dist/hooks/index.d.ts +1 -0
- package/dist/hooks/useNarrowScreen.d.ts +4 -0
- package/dist/hooks/useQueryResult.d.ts +2 -0
- package/dist/hooks/useSettled.d.ts +14 -0
- package/dist/index.cjs.js +23 -24
- package/dist/index.es.js +9714 -7072
- package/dist/text/index.cjs.js +1 -0
- package/dist/text/index.d.ts +2 -0
- package/dist/text/index.es.js +6 -0
- package/dist/text-entry-BJwltZiy.cjs.js +7 -0
- package/dist/text-entry-CbbBcZl8.es.js +196 -0
- package/dist/text-entry.d.ts +2 -0
- package/dist/utils/clock.d.ts +2 -0
- package/package.json +10 -1
- package/src/builder-entry.ts +35 -0
- package/src/components/Dashboard/Dashboard.tsx +27 -278
- package/src/components/Dashboard/DashboardGrid.tsx +68 -7
- package/src/components/Dashboard/DashboardTile.spec.tsx +24 -0
- package/src/components/Dashboard/DashboardTile.tsx +31 -4
- package/src/components/Dashboard/DashboardView.spec.tsx +212 -0
- package/src/components/Dashboard/DashboardView.tsx +352 -0
- package/src/components/Dashboard/RowsDialog.tsx +1 -1
- package/src/components/Dashboard/TileCard.tsx +44 -10
- package/src/components/Dashboard/TileFilterTag.spec.tsx +73 -0
- package/src/components/Dashboard/TileFilterTag.tsx +78 -0
- package/src/components/Dashboard/defaultTileSpan.spec.ts +32 -0
- package/src/components/Dashboard/index.ts +3 -0
- package/src/components/Dashboard/telemetry.ts +16 -5
- package/src/components/DashboardBuilder/AddTileDialog.spec.tsx +88 -0
- package/src/components/DashboardBuilder/AddTileDialog.tsx +137 -111
- package/src/components/DashboardBuilder/BuilderToolbar.tsx +85 -27
- package/src/components/DashboardBuilder/ChartPicker.render.spec.tsx +25 -0
- package/src/components/DashboardBuilder/ChartPicker.spec.ts +77 -0
- package/src/components/DashboardBuilder/ChartPicker.tsx +153 -0
- package/src/components/DashboardBuilder/DashboardBuilder.charts.spec.tsx +205 -0
- package/src/components/DashboardBuilder/DashboardBuilder.exit.spec.tsx +192 -0
- package/src/components/DashboardBuilder/DashboardBuilder.inline.spec.tsx +329 -0
- package/src/components/DashboardBuilder/DashboardBuilder.notebook.spec.tsx +363 -0
- package/src/components/DashboardBuilder/DashboardBuilder.notice.spec.tsx +194 -0
- package/src/components/DashboardBuilder/DashboardBuilder.openDraft.spec.tsx +157 -0
- package/src/components/DashboardBuilder/DashboardBuilder.sources.spec.tsx +124 -0
- package/src/components/DashboardBuilder/DashboardBuilder.spec.tsx +103 -78
- package/src/components/DashboardBuilder/DashboardBuilder.textTiles.spec.tsx +177 -0
- package/src/components/DashboardBuilder/DashboardBuilder.tsx +610 -315
- package/src/components/DashboardBuilder/DashboardEditor.authoritative.spec.tsx +58 -8
- package/src/components/DashboardBuilder/DashboardEditor.emptyStart.spec.tsx +191 -0
- package/src/components/DashboardBuilder/DashboardEditor.filterTag.spec.tsx +162 -0
- package/src/components/DashboardBuilder/DashboardEditor.notebook.spec.tsx +316 -0
- package/src/components/DashboardBuilder/DashboardEditor.package.spec.tsx +56 -10
- package/src/components/DashboardBuilder/DashboardEditor.resourceUri.spec.tsx +5 -22
- package/src/components/DashboardBuilder/DashboardEditor.spec.tsx +96 -14
- package/src/components/DashboardBuilder/DashboardEditor.tsx +482 -332
- package/src/components/DashboardBuilder/DiffDialog.spec.tsx +25 -0
- package/src/components/DashboardBuilder/DiffDialog.tsx +5 -24
- package/src/components/DashboardBuilder/DrillDialog.tsx +1 -1
- package/src/components/DashboardBuilder/FilterDialog.tsx +10 -3
- package/src/components/DashboardBuilder/FilterStrip.tsx +26 -4
- package/src/components/DashboardBuilder/InlineMarkdown.tsx +184 -0
- package/src/components/DashboardBuilder/InlineText.tsx +106 -0
- package/src/components/DashboardBuilder/NotebookEditor.tsx +44 -0
- package/src/components/DashboardBuilder/SaveNotice.tsx +113 -0
- package/src/components/DashboardBuilder/SettingsPopover.spec.tsx +87 -0
- package/src/components/DashboardBuilder/SettingsPopover.tsx +225 -67
- package/src/components/DashboardBuilder/SourceViewPicker.tsx +122 -0
- package/src/components/DashboardBuilder/TextTileBody.tsx +28 -0
- package/src/components/DashboardBuilder/TileFrame.tsx +89 -115
- package/src/components/DashboardBuilder/TileMenu.tsx +168 -90
- package/src/components/DashboardBuilder/annotationText.spec.ts +33 -0
- package/src/components/DashboardBuilder/annotationText.ts +17 -0
- package/src/components/DashboardBuilder/artifactBlock.spec.ts +248 -0
- package/src/components/DashboardBuilder/artifactTagParity.spec.ts +41 -0
- package/src/components/DashboardBuilder/catalog.aggregate.spec.ts +49 -0
- package/src/components/DashboardBuilder/catalog.spec.ts +2 -0
- package/src/components/DashboardBuilder/catalog.ts +61 -21
- package/src/components/DashboardBuilder/changedTile.spec.ts +46 -0
- package/src/components/DashboardBuilder/changedTile.ts +28 -0
- package/src/components/DashboardBuilder/chartLine.spec.ts +180 -0
- package/src/components/DashboardBuilder/chartLine.ts +121 -0
- package/src/components/DashboardBuilder/controls.spec.ts +21 -10
- package/src/components/DashboardBuilder/controls.ts +22 -8
- package/src/components/DashboardBuilder/document.ts +53 -5
- package/src/components/DashboardBuilder/documentSession.spec.ts +209 -0
- package/src/components/DashboardBuilder/documentSession.ts +257 -0
- package/src/components/DashboardBuilder/emptyStart.spec.ts +131 -0
- package/src/components/DashboardBuilder/invariants.spec.ts +6 -2
- package/src/components/DashboardBuilder/legacyNotebook.chart.spec.ts +77 -0
- package/src/components/DashboardBuilder/legacyNotebook.convert.spec.ts +630 -0
- package/src/components/DashboardBuilder/legacyNotebook.read.spec.ts +338 -0
- package/src/components/DashboardBuilder/legacyNotebook.ts +1117 -0
- package/src/components/DashboardBuilder/loadMalloy.spec.ts +80 -0
- package/src/components/DashboardBuilder/loadMalloy.ts +39 -0
- package/src/components/DashboardBuilder/malloyText.spec.ts +48 -0
- package/src/components/DashboardBuilder/malloyText.ts +181 -27
- package/src/components/DashboardBuilder/malloyTree.ts +155 -7
- package/src/components/DashboardBuilder/newDashboard.spec.ts +46 -2
- package/src/components/DashboardBuilder/newDashboard.ts +30 -3
- package/src/components/DashboardBuilder/openDraft.ts +52 -0
- package/src/components/DashboardBuilder/preview.shapes.spec.ts +75 -0
- package/src/components/DashboardBuilder/preview.spec.ts +159 -12
- package/src/components/DashboardBuilder/preview.ts +83 -9
- package/src/components/DashboardBuilder/readDocument.spec.ts +139 -27
- package/src/components/DashboardBuilder/readDocument.ts +130 -44
- package/src/components/DashboardBuilder/readForEditor.spec.ts +80 -0
- package/src/components/DashboardBuilder/readForEditor.ts +47 -0
- package/src/components/DashboardBuilder/scannerShapes.spec.ts +14 -13
- package/src/components/DashboardBuilder/sortable.spec.ts +65 -0
- package/src/components/DashboardBuilder/sortable.tsx +2 -12
- package/src/components/DashboardBuilder/spliceDocument.chart.spec.ts +340 -0
- package/src/components/DashboardBuilder/spliceDocument.imports.spec.ts +167 -0
- package/src/components/DashboardBuilder/spliceDocument.spec.ts +229 -94
- package/src/components/DashboardBuilder/spliceDocument.ts +762 -130
- package/src/components/DashboardBuilder/spliceResult.ts +15 -0
- package/src/components/DashboardBuilder/statementExtent.spec.ts +33 -30
- package/src/components/DashboardBuilder/tagParse.spec.ts +149 -0
- package/src/components/DashboardBuilder/tagParse.ts +103 -0
- package/src/components/DashboardBuilder/telemetry.ts +47 -0
- package/src/components/DashboardBuilder/testing/artifactTagParity.json +197 -0
- package/src/components/DashboardBuilder/testing/fixtures.ts +16 -1
- package/src/components/DashboardBuilder/testing/inline.ts +25 -0
- package/src/components/DashboardBuilder/textTiles.spec.ts +718 -0
- package/src/components/DashboardBuilder/useBuilderSession.spec.ts +335 -0
- package/src/components/DashboardBuilder/useBuilderSession.ts +292 -0
- package/src/components/DashboardBuilder/useBuilderShortcuts.overlay.spec.tsx +65 -0
- package/src/components/DashboardBuilder/useBuilderShortcuts.spec.tsx +60 -0
- package/src/components/DashboardBuilder/useBuilderShortcuts.ts +36 -10
- package/src/components/DashboardBuilder/useDashboardEditor.spec.ts +16 -13
- package/src/components/DashboardBuilder/useDashboardEditor.ts +54 -181
- package/src/components/DashboardBuilder/useDocumentEditor.spec.ts +565 -0
- package/src/components/DashboardBuilder/useDocumentEditor.ts +399 -0
- package/src/components/DashboardBuilder/useExitGuard.spec.ts +159 -0
- package/src/components/DashboardBuilder/useExitGuard.ts +89 -0
- package/src/components/DashboardBuilder/useFilterForm.existing.spec.tsx +41 -0
- package/src/components/DashboardBuilder/useFilterForm.spec.tsx +24 -2
- package/src/components/DashboardBuilder/useFilterForm.ts +41 -13
- package/src/components/DashboardBuilder/useTileReorder.spec.tsx +28 -5
- package/src/components/DashboardBuilder/useTileReorder.ts +5 -7
- package/src/components/DocumentCreate/canRetry.spec.ts +32 -0
- package/src/components/DocumentCreate/canRetry.ts +19 -0
- package/src/components/DocumentCreate/createDocument.spec.ts +310 -0
- package/src/components/DocumentCreate/createDocument.ts +159 -0
- package/src/components/DocumentCreate/documentPath.ts +40 -0
- package/src/components/DocumentCreate/events.ts +15 -0
- package/src/components/DocumentCreate/exportedSources.ts +25 -0
- package/src/components/DocumentCreate/guards.ts +45 -0
- package/src/components/DocumentCreate/index.ts +27 -0
- package/src/components/DocumentCreate/newNotebook.spec.ts +156 -0
- package/src/components/DocumentCreate/newNotebook.ts +44 -0
- package/src/components/DocumentCreate/useDocumentChoices.spec.tsx +138 -0
- package/src/components/DocumentCreate/useDocumentChoices.ts +113 -0
- package/src/components/DocumentStorage/DocumentStorage.ts +2 -1
- package/src/components/Model/ModelExplorer.spec.tsx +2 -2
- package/src/components/Model/SourcesExplorer.tsx +2 -4
- package/src/components/NarrowEditGate.spec.tsx +73 -0
- package/src/components/NarrowEditGate.tsx +29 -0
- package/src/components/Notebook/Notebook.layout.spec.tsx +94 -0
- package/src/components/Notebook/Notebook.tsx +66 -27
- package/src/components/Package/NewDocumentDialog.spec.tsx +749 -0
- package/src/components/Package/NewDocumentDialog.tsx +401 -0
- package/src/components/Package/Package.artifacts.spec.tsx +190 -0
- package/src/components/Package/Package.create.spec.tsx +380 -0
- package/src/components/Package/Package.notebooks.spec.tsx +18 -0
- package/src/components/Package/Package.tsx +366 -171
- package/src/components/Package/documentLocation.spec.ts +81 -0
- package/src/components/Package/documentLocation.ts +55 -0
- package/src/components/Package/index.ts +13 -0
- package/src/components/Package/useDocumentLocation.ts +59 -0
- package/src/components/PackageSection.tsx +2 -2
- package/src/components/RenderedResult/RenderedResult.tsx +18 -1
- package/src/components/RenderedResult/ResultContainer.tsx +44 -11
- package/src/components/RenderedResult/ResultPanel.tsx +4 -0
- package/src/components/RenderedResult/resultSizing.spec.ts +68 -0
- package/src/components/RenderedResult/resultSizing.ts +14 -3
- package/src/components/ServerProvider.tsx +2 -1
- package/src/components/UnsavedChangesDialog.spec.tsx +70 -0
- package/src/components/UnsavedChangesDialog.tsx +49 -0
- package/src/components/given/GivensPanel.spec.tsx +6 -6
- package/src/components/given/GivensPanel.tsx +3 -3
- package/src/components/index.ts +2 -0
- package/src/hooks/index.ts +3 -0
- package/src/hooks/useNarrowScreen.ts +21 -0
- package/src/hooks/useQueryResult.ts +16 -0
- package/src/hooks/useSettled.spec.ts +41 -0
- package/src/hooks/useSettled.ts +27 -0
- package/src/mainEntry.spec.ts +145 -0
- package/src/text-entry.ts +12 -0
- package/src/textEntry.spec.ts +118 -0
- package/src/utils/clock.spec.ts +12 -0
- package/src/utils/clock.ts +6 -0
- package/vite.config.ts +4 -0
- package/dist/Dashboard-ChyylkTD.cjs.js +0 -136
- package/dist/RenderedResult-AJPMUkAD.cjs.js +0 -135
- package/dist/ServerProvider-CXrOKD6g.cjs.js +0 -1
- package/dist/components/DashboardBuilder/useTileResize.d.ts +0 -29
- package/dist/components/Package/NewDashboardDialog.d.ts +0 -18
- package/src/components/DashboardBuilder/useTileResize.spec.tsx +0 -121
- package/src/components/DashboardBuilder/useTileResize.ts +0 -110
- package/src/components/Package/NewDashboardDialog.spec.tsx +0 -129
- package/src/components/Package/NewDashboardDialog.tsx +0 -265
|
@@ -0,0 +1,335 @@
|
|
|
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 {
|
|
7
|
+
useBuilderSession,
|
|
8
|
+
type BuilderSessionOptions,
|
|
9
|
+
type SessionEditor,
|
|
10
|
+
} from "./useBuilderSession";
|
|
11
|
+
import type { SaveOutcome } from "./useDocumentEditor";
|
|
12
|
+
|
|
13
|
+
type Doc = { n: number };
|
|
14
|
+
|
|
15
|
+
const makeEditor = (
|
|
16
|
+
over: Partial<SessionEditor<Doc>> = {},
|
|
17
|
+
): SessionEditor<Doc> => ({
|
|
18
|
+
document: { n: 1 },
|
|
19
|
+
saved: { n: 0 },
|
|
20
|
+
dirty: true,
|
|
21
|
+
structural: false,
|
|
22
|
+
canUndo: true,
|
|
23
|
+
canRedo: false,
|
|
24
|
+
undo: mock(() => {}),
|
|
25
|
+
redo: mock(() => {}),
|
|
26
|
+
save: mock(async (): Promise<SaveOutcome> => ({ ok: true })),
|
|
27
|
+
canUndoSave: false,
|
|
28
|
+
undoSave: mock(async (): Promise<SaveOutcome> => ({ ok: true })),
|
|
29
|
+
...over,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const mount = (
|
|
33
|
+
over: Partial<BuilderSessionOptions<Doc>> = {},
|
|
34
|
+
editor: SessionEditor<Doc> = makeEditor(),
|
|
35
|
+
) => {
|
|
36
|
+
const saved = mock((_info: unknown) => {});
|
|
37
|
+
const refused = mock((_reason: string) => {});
|
|
38
|
+
const undone = mock((_info: unknown) => {});
|
|
39
|
+
const undoRefused = mock((_reason: string) => {});
|
|
40
|
+
const onSave = mock(async (_source: string) => {});
|
|
41
|
+
const shortcuts = { escape: () => {} };
|
|
42
|
+
const view = renderHook(
|
|
43
|
+
(props: { editor: SessionEditor<Doc> }) =>
|
|
44
|
+
useBuilderSession<Doc>({
|
|
45
|
+
editor: props.editor,
|
|
46
|
+
onSave,
|
|
47
|
+
shortcuts,
|
|
48
|
+
unit: { name: "tile", count: (document) => document.n },
|
|
49
|
+
report: { size: 3, saved, refused, undone, undoRefused },
|
|
50
|
+
...over,
|
|
51
|
+
}),
|
|
52
|
+
{ initialProps: { editor } },
|
|
53
|
+
);
|
|
54
|
+
return { ...view, editor, saved, refused, undone, undoRefused, onSave };
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const press = (key: string) =>
|
|
58
|
+
window.dispatchEvent(
|
|
59
|
+
new KeyboardEvent("keydown", {
|
|
60
|
+
key,
|
|
61
|
+
metaKey: true,
|
|
62
|
+
ctrlKey: true,
|
|
63
|
+
bubbles: true,
|
|
64
|
+
}),
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
describe("useBuilderSession save", () => {
|
|
68
|
+
it("writes at once and reports the size and structure it saved", async () => {
|
|
69
|
+
const { result, saved, editor } = mount(
|
|
70
|
+
{},
|
|
71
|
+
makeEditor({ structural: false }),
|
|
72
|
+
);
|
|
73
|
+
await act(async () => {
|
|
74
|
+
await result.current.save();
|
|
75
|
+
});
|
|
76
|
+
expect(editor.save).toHaveBeenCalledTimes(1);
|
|
77
|
+
expect(saved).toHaveBeenCalledTimes(1);
|
|
78
|
+
const info = saved.mock.calls[0][0] as {
|
|
79
|
+
size: number;
|
|
80
|
+
structural: boolean;
|
|
81
|
+
durationMs: number;
|
|
82
|
+
};
|
|
83
|
+
expect(info.size).toBe(3);
|
|
84
|
+
expect(info.structural).toBe(false);
|
|
85
|
+
expect(typeof info.durationMs).toBe("number");
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("reports a refusal and not a save", async () => {
|
|
89
|
+
const editor = makeEditor({
|
|
90
|
+
save: mock(
|
|
91
|
+
async (): Promise<SaveOutcome> => ({ ok: false, reason: "nope" }),
|
|
92
|
+
),
|
|
93
|
+
});
|
|
94
|
+
const { result, saved, refused } = mount({}, editor);
|
|
95
|
+
await act(async () => {
|
|
96
|
+
await result.current.save();
|
|
97
|
+
});
|
|
98
|
+
expect(refused).toHaveBeenCalledWith("nope");
|
|
99
|
+
expect(saved).not.toHaveBeenCalled();
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("does nothing when clean, without a writer, or while saving", async () => {
|
|
103
|
+
const clean = mount({}, makeEditor({ dirty: false }));
|
|
104
|
+
await act(async () => {
|
|
105
|
+
await clean.result.current.save();
|
|
106
|
+
});
|
|
107
|
+
expect(clean.editor.save).not.toHaveBeenCalled();
|
|
108
|
+
|
|
109
|
+
const none = mount({ onSave: undefined });
|
|
110
|
+
await act(async () => {
|
|
111
|
+
await none.result.current.save();
|
|
112
|
+
});
|
|
113
|
+
expect(none.editor.save).not.toHaveBeenCalled();
|
|
114
|
+
expect(none.result.current.toolbarProps.onSave).toBeUndefined();
|
|
115
|
+
|
|
116
|
+
let release = () => {};
|
|
117
|
+
const slow = makeEditor({
|
|
118
|
+
save: mock(
|
|
119
|
+
() =>
|
|
120
|
+
new Promise<SaveOutcome>((resolve) => {
|
|
121
|
+
release = () => resolve({ ok: true });
|
|
122
|
+
}),
|
|
123
|
+
),
|
|
124
|
+
});
|
|
125
|
+
const busy = mount({}, slow);
|
|
126
|
+
let first: Promise<void> | void;
|
|
127
|
+
act(() => {
|
|
128
|
+
first = busy.result.current.save();
|
|
129
|
+
});
|
|
130
|
+
expect(busy.result.current.saving).toBe(true);
|
|
131
|
+
await act(async () => {
|
|
132
|
+
await busy.result.current.save();
|
|
133
|
+
});
|
|
134
|
+
expect(slow.save).toHaveBeenCalledTimes(1);
|
|
135
|
+
await act(async () => {
|
|
136
|
+
release();
|
|
137
|
+
await first;
|
|
138
|
+
});
|
|
139
|
+
expect(busy.result.current.saving).toBe(false);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("runs every entry point through prepare", async () => {
|
|
143
|
+
const prepare = mock((run: () => Promise<void> | void) => run());
|
|
144
|
+
const { result, editor } = mount({ prepare });
|
|
145
|
+
await act(async () => {
|
|
146
|
+
await result.current.save();
|
|
147
|
+
});
|
|
148
|
+
expect(prepare).toHaveBeenCalledTimes(1);
|
|
149
|
+
expect(editor.save).toHaveBeenCalledTimes(1);
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
describe("useBuilderSession reports", () => {
|
|
154
|
+
it("reports dirty from the editor or the extra state, and clean on unmount", () => {
|
|
155
|
+
const onDirtyChange = mock((_dirty: boolean) => {});
|
|
156
|
+
const clean = makeEditor({ dirty: false });
|
|
157
|
+
const view = mount({ onDirtyChange, extraDirty: true }, clean);
|
|
158
|
+
expect(onDirtyChange).toHaveBeenLastCalledWith(true);
|
|
159
|
+
expect(view.result.current.toolbarProps.dirty).toBe(true);
|
|
160
|
+
view.unmount();
|
|
161
|
+
expect(onDirtyChange).toHaveBeenLastCalledWith(false);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("reports the document on mount and when it changes", () => {
|
|
165
|
+
const onChange = mock((_doc: Doc) => {});
|
|
166
|
+
const view = mount({ onChange });
|
|
167
|
+
expect(onChange).toHaveBeenLastCalledWith({ n: 1 });
|
|
168
|
+
view.rerender({ editor: makeEditor({ document: { n: 2 } }) });
|
|
169
|
+
expect(onChange).toHaveBeenLastCalledWith({ n: 2 });
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
describe("useBuilderSession exit", () => {
|
|
174
|
+
it("offers Done only when the host can leave, and leaves at once when clean", () => {
|
|
175
|
+
expect(mount().result.current.toolbarProps.onExit).toBeUndefined();
|
|
176
|
+
const onExit = mock(() => {});
|
|
177
|
+
const { result } = mount({ onExit }, makeEditor({ dirty: false }));
|
|
178
|
+
act(() => result.current.toolbarProps.onExit?.());
|
|
179
|
+
expect(onExit).toHaveBeenCalledTimes(1);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("asks before leaving unsaved edits", () => {
|
|
183
|
+
const onExit = mock(() => {});
|
|
184
|
+
const { result } = mount({ onExit });
|
|
185
|
+
act(() => result.current.toolbarProps.onExit?.());
|
|
186
|
+
expect(onExit).not.toHaveBeenCalled();
|
|
187
|
+
expect(result.current.exitGuard.dialog.open).toBe(true);
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
describe("useBuilderSession undo save", () => {
|
|
192
|
+
const LAST = {
|
|
193
|
+
before: "a",
|
|
194
|
+
after: "a,b",
|
|
195
|
+
structural: true,
|
|
196
|
+
clearsHistory: false,
|
|
197
|
+
};
|
|
198
|
+
const offering = (over: Partial<SessionEditor<Doc>> = {}) =>
|
|
199
|
+
makeEditor({ dirty: false, canUndoSave: true, lastSave: LAST, ...over });
|
|
200
|
+
|
|
201
|
+
it("undoes through the editor and reports it like a save", async () => {
|
|
202
|
+
const { result, editor, undone, saved } = mount({}, offering());
|
|
203
|
+
expect(result.current.canUndoSave).toBe(true);
|
|
204
|
+
expect(result.current.lastSave).toEqual(LAST);
|
|
205
|
+
await act(async () => {
|
|
206
|
+
await result.current.undoSave();
|
|
207
|
+
});
|
|
208
|
+
expect(editor.undoSave).toHaveBeenCalledTimes(1);
|
|
209
|
+
expect(saved).not.toHaveBeenCalled();
|
|
210
|
+
const info = undone.mock.calls[0][0] as {
|
|
211
|
+
size: number;
|
|
212
|
+
structural: boolean;
|
|
213
|
+
durationMs: number;
|
|
214
|
+
};
|
|
215
|
+
expect(info.size).toBe(3);
|
|
216
|
+
expect(info.structural).toBe(true);
|
|
217
|
+
expect(typeof info.durationMs).toBe("number");
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it("reports a refused undo with its reason", async () => {
|
|
221
|
+
const editor = offering({
|
|
222
|
+
undoSave: mock(
|
|
223
|
+
async (): Promise<SaveOutcome> => ({
|
|
224
|
+
ok: false,
|
|
225
|
+
reason: "changed since",
|
|
226
|
+
}),
|
|
227
|
+
),
|
|
228
|
+
});
|
|
229
|
+
const { result, undone, undoRefused } = mount({}, editor);
|
|
230
|
+
await act(async () => {
|
|
231
|
+
await result.current.undoSave();
|
|
232
|
+
});
|
|
233
|
+
expect(undone).not.toHaveBeenCalled();
|
|
234
|
+
expect(undoRefused).toHaveBeenCalledWith("changed since");
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it("is saving while the undo writes, so Save and a second Undo do nothing", async () => {
|
|
238
|
+
let finish!: (outcome: SaveOutcome) => void;
|
|
239
|
+
const editor = offering({
|
|
240
|
+
dirty: true,
|
|
241
|
+
undoSave: mock(
|
|
242
|
+
() => new Promise<SaveOutcome>((resolve) => (finish = resolve)),
|
|
243
|
+
),
|
|
244
|
+
});
|
|
245
|
+
const { result } = mount({}, editor);
|
|
246
|
+
let undoing!: Promise<void> | void;
|
|
247
|
+
act(() => {
|
|
248
|
+
undoing = result.current.undoSave();
|
|
249
|
+
void result.current.undoSave();
|
|
250
|
+
});
|
|
251
|
+
expect(result.current.saving).toBe(true);
|
|
252
|
+
expect(result.current.canUndoSave).toBe(false);
|
|
253
|
+
await act(async () => {
|
|
254
|
+
await result.current.save();
|
|
255
|
+
});
|
|
256
|
+
expect(editor.save).not.toHaveBeenCalled();
|
|
257
|
+
await act(async () => {
|
|
258
|
+
finish({ ok: true });
|
|
259
|
+
await undoing;
|
|
260
|
+
});
|
|
261
|
+
expect(editor.undoSave).toHaveBeenCalledTimes(1);
|
|
262
|
+
expect(result.current.saving).toBe(false);
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it("does nothing without a writer or an offer", async () => {
|
|
266
|
+
const none = mount({}, makeEditor());
|
|
267
|
+
await act(async () => {
|
|
268
|
+
await none.result.current.undoSave();
|
|
269
|
+
});
|
|
270
|
+
expect(none.editor.undoSave).not.toHaveBeenCalled();
|
|
271
|
+
const readOnly = mount({ onSave: undefined }, offering());
|
|
272
|
+
expect(readOnly.result.current.canUndoSave).toBe(false);
|
|
273
|
+
await act(async () => {
|
|
274
|
+
await readOnly.result.current.undoSave();
|
|
275
|
+
});
|
|
276
|
+
expect(readOnly.editor.undoSave).not.toHaveBeenCalled();
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
it("pauses the keyboard while the change is being viewed, and closes the viewer with the offer", () => {
|
|
280
|
+
const { result, editor, rerender } = mount({}, offering());
|
|
281
|
+
act(() => result.current.notice.onView(true));
|
|
282
|
+
expect(result.current.notice.viewing).toBe(true);
|
|
283
|
+
press("z");
|
|
284
|
+
expect(editor.undo).not.toHaveBeenCalled();
|
|
285
|
+
act(() => result.current.notice.onView(false));
|
|
286
|
+
press("z");
|
|
287
|
+
expect(editor.undo).toHaveBeenCalledTimes(1);
|
|
288
|
+
act(() => result.current.notice.onView(true));
|
|
289
|
+
rerender({ editor: makeEditor() });
|
|
290
|
+
expect(result.current.notice.viewing).toBe(false);
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
it("counts what the save moved, from the saved document to the one written", async () => {
|
|
294
|
+
const { result } = mount(
|
|
295
|
+
{},
|
|
296
|
+
makeEditor({ document: { n: 3 }, saved: { n: 1 } }),
|
|
297
|
+
);
|
|
298
|
+
await act(async () => {
|
|
299
|
+
await result.current.save();
|
|
300
|
+
});
|
|
301
|
+
expect(result.current.notice.moved).toEqual({ before: 1, after: 3 });
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
it("says the save was undone until the next edit", async () => {
|
|
305
|
+
const document = { n: 1 };
|
|
306
|
+
const view = mount({}, offering({ document }));
|
|
307
|
+
await act(async () => {
|
|
308
|
+
await view.result.current.undoSave();
|
|
309
|
+
});
|
|
310
|
+
view.rerender({ editor: makeEditor({ document }) });
|
|
311
|
+
expect(view.result.current.notice.undone).toBe(true);
|
|
312
|
+
view.rerender({ editor: makeEditor({ document: { n: 2 } }) });
|
|
313
|
+
expect(view.result.current.notice.undone).toBe(false);
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
it("tells the host whether a save can still be undone, and that it cannot once unmounted", () => {
|
|
317
|
+
const onSaveNoticeChange = mock((_showing: boolean) => {});
|
|
318
|
+
const view = mount({ onSaveNoticeChange }, makeEditor());
|
|
319
|
+
expect(onSaveNoticeChange).toHaveBeenLastCalledWith(false);
|
|
320
|
+
view.rerender({ editor: offering() });
|
|
321
|
+
expect(onSaveNoticeChange).toHaveBeenLastCalledWith(true);
|
|
322
|
+
view.unmount();
|
|
323
|
+
expect(onSaveNoticeChange).toHaveBeenLastCalledWith(false);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it("withdraws the notice when the writer goes away while it stands", () => {
|
|
327
|
+
const onSaveNoticeChange = mock((_showing: boolean) => {});
|
|
328
|
+
const over: Partial<BuilderSessionOptions<Doc>> = { onSaveNoticeChange };
|
|
329
|
+
const view = mount(over, offering());
|
|
330
|
+
expect(onSaveNoticeChange).toHaveBeenLastCalledWith(true);
|
|
331
|
+
over.onSave = undefined;
|
|
332
|
+
view.rerender({ editor: offering() });
|
|
333
|
+
expect(onSaveNoticeChange).toHaveBeenLastCalledWith(false);
|
|
334
|
+
});
|
|
335
|
+
});
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
useCallback,
|
|
6
|
+
useEffect,
|
|
7
|
+
useLayoutEffect,
|
|
8
|
+
useMemo,
|
|
9
|
+
useRef,
|
|
10
|
+
useState,
|
|
11
|
+
} from "react";
|
|
12
|
+
import { now } from "../../utils/clock";
|
|
13
|
+
import type { BuilderToolbarProps } from "./BuilderToolbar";
|
|
14
|
+
import {
|
|
15
|
+
useBuilderShortcuts,
|
|
16
|
+
type BuilderShortcutHandlers,
|
|
17
|
+
} from "./useBuilderShortcuts";
|
|
18
|
+
import type { LastSave, SaveHandler, SaveOutcome } from "./useDocumentEditor";
|
|
19
|
+
import { useExitGuard } from "./useExitGuard";
|
|
20
|
+
import type { SaveNoticeProps } from "./SaveNotice";
|
|
21
|
+
|
|
22
|
+
/** What a builder's session reads of its document editor. */
|
|
23
|
+
export interface SessionEditor<D, L extends LastSave = LastSave> {
|
|
24
|
+
document: D;
|
|
25
|
+
/** The document as of the last save (or open). */
|
|
26
|
+
saved: D;
|
|
27
|
+
dirty: boolean;
|
|
28
|
+
/** Unsaved edits made by a person, when that differs from `dirty` (an open that is unsaved before any edit); absent means `dirty`. */
|
|
29
|
+
edited?: boolean;
|
|
30
|
+
structural: boolean;
|
|
31
|
+
/** Whether the document opened unsaved (a conversion); the save that clears it reports so. */
|
|
32
|
+
pendingOpen?: boolean;
|
|
33
|
+
canUndo: boolean;
|
|
34
|
+
canRedo: boolean;
|
|
35
|
+
undo: () => void;
|
|
36
|
+
redo: () => void;
|
|
37
|
+
save: () => Promise<SaveOutcome>;
|
|
38
|
+
canUndoSave: boolean;
|
|
39
|
+
lastSave?: L;
|
|
40
|
+
undoSave: () => Promise<SaveOutcome>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** What a builder says about a save, in its own event's words. */
|
|
44
|
+
export interface SessionReport {
|
|
45
|
+
/** How many tiles or cells the document has, as the event counts them. */
|
|
46
|
+
size: number;
|
|
47
|
+
saved: (info: SessionSaveInfo) => void;
|
|
48
|
+
refused: (reason: string) => void;
|
|
49
|
+
/** An Undo save that wrote the file back. */
|
|
50
|
+
undone: (info: SessionSaveInfo) => void;
|
|
51
|
+
undoRefused: (reason: string) => void;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface SessionSaveInfo {
|
|
55
|
+
size: number;
|
|
56
|
+
structural: boolean;
|
|
57
|
+
/** The save wrote a document that opened unsaved. */
|
|
58
|
+
fromOpen: boolean;
|
|
59
|
+
durationMs: number;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface BuilderSessionOptions<D, L extends LastSave = LastSave> {
|
|
63
|
+
editor: SessionEditor<D, L>;
|
|
64
|
+
onSave?: SaveHandler<D>;
|
|
65
|
+
onExit?: () => void;
|
|
66
|
+
onDirtyChange?: (dirty: boolean) => void;
|
|
67
|
+
/** Whether the save notice is showing, on every change and on mount. */
|
|
68
|
+
onSaveNoticeChange?: (showing: boolean) => void;
|
|
69
|
+
onChange?: (document: D) => void;
|
|
70
|
+
/** Unsaved state the editor does not hold, such as an open text draft. */
|
|
71
|
+
extraDirty?: boolean;
|
|
72
|
+
report: SessionReport;
|
|
73
|
+
/** What the notice counts and calls them: "tile" or "cell". */
|
|
74
|
+
unit: { name: string; count: (document: D) => number };
|
|
75
|
+
/** Runs before every save entry point, handing it the save to run once the document is ready. */
|
|
76
|
+
prepare?: (run: () => Promise<void> | void) => Promise<void> | void;
|
|
77
|
+
/** The builder's own keys, less undo, redo and save, which the session owns. Memoized by the caller. */
|
|
78
|
+
shortcuts: Pick<BuilderShortcutHandlers, "escape" | "nudge">;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* What the dashboard and notebook builders share around their document editor:
|
|
83
|
+
* saving (with its event and timing), the dirty and change reports, the exit
|
|
84
|
+
* guard, the keyboard, and the props their toolbar takes.
|
|
85
|
+
*/
|
|
86
|
+
export function useBuilderSession<
|
|
87
|
+
D,
|
|
88
|
+
L extends LastSave & { removedComments?: string[] } = LastSave,
|
|
89
|
+
>({
|
|
90
|
+
editor,
|
|
91
|
+
onSave,
|
|
92
|
+
onExit,
|
|
93
|
+
onDirtyChange,
|
|
94
|
+
onSaveNoticeChange,
|
|
95
|
+
onChange,
|
|
96
|
+
extraDirty = false,
|
|
97
|
+
report,
|
|
98
|
+
unit,
|
|
99
|
+
prepare,
|
|
100
|
+
shortcuts,
|
|
101
|
+
}: BuilderSessionOptions<D, L>) {
|
|
102
|
+
const [saving, setSaving] = useState(false);
|
|
103
|
+
const [viewing, setViewing] = useState(false);
|
|
104
|
+
const [undone, setUndone] = useState(false);
|
|
105
|
+
const [moved, setMoved] = useState<
|
|
106
|
+
{ before: number; after: number } | undefined
|
|
107
|
+
>(undefined);
|
|
108
|
+
const saveButton = useRef<HTMLButtonElement>(null);
|
|
109
|
+
const reportRef = useRef(report);
|
|
110
|
+
reportRef.current = report;
|
|
111
|
+
const dirty = editor.dirty || extraDirty;
|
|
112
|
+
// Work to lose: a conversion nobody has touched is rebuilt by opening the file again.
|
|
113
|
+
const hasEdits = (editor.edited ?? editor.dirty) || extraDirty;
|
|
114
|
+
|
|
115
|
+
useEffect(() => {
|
|
116
|
+
onChange?.(editor.document);
|
|
117
|
+
}, [editor.document, onChange]);
|
|
118
|
+
// Also on mount, so a host that remounted the builder on new text is told the slate is clean rather than carrying the previous mount's answer.
|
|
119
|
+
// A layout effect, so a host's leave guard sees the flag in the same task as the edit.
|
|
120
|
+
useLayoutEffect(() => {
|
|
121
|
+
onDirtyChange?.(hasEdits);
|
|
122
|
+
}, [hasEdits, onDirtyChange]);
|
|
123
|
+
// A host guarding navigation on this must not be left holding a stale "dirty" once the builder is gone.
|
|
124
|
+
const onDirtyChangeRef = useRef(onDirtyChange);
|
|
125
|
+
onDirtyChangeRef.current = onDirtyChange;
|
|
126
|
+
useEffect(() => () => onDirtyChangeRef.current?.(false), []);
|
|
127
|
+
// The offer, not `canUndoSave`, which also drops while a write is in flight; a host holding a newer version back must keep holding then. Without a writer nothing can be undone, so the offer is withdrawn.
|
|
128
|
+
const undoOffered = editor.lastSave !== undefined && !!onSave;
|
|
129
|
+
useEffect(() => {
|
|
130
|
+
onSaveNoticeChange?.(undoOffered);
|
|
131
|
+
}, [undoOffered, onSaveNoticeChange]);
|
|
132
|
+
const onSaveNoticeChangeRef = useRef(onSaveNoticeChange);
|
|
133
|
+
onSaveNoticeChangeRef.current = onSaveNoticeChange;
|
|
134
|
+
useEffect(() => () => onSaveNoticeChangeRef.current?.(false), []);
|
|
135
|
+
|
|
136
|
+
// The viewer shows the offer's change, so it closes when the offer is withdrawn.
|
|
137
|
+
useEffect(() => {
|
|
138
|
+
if (!undoOffered) setViewing(false);
|
|
139
|
+
}, [undoOffered]);
|
|
140
|
+
const unitRef = useRef(unit);
|
|
141
|
+
unitRef.current = unit;
|
|
142
|
+
// The line saying the undo happened goes with the next edit or save; leaving it up over new work would say something stale.
|
|
143
|
+
useEffect(() => {
|
|
144
|
+
setUndone(false);
|
|
145
|
+
}, [editor.document]);
|
|
146
|
+
|
|
147
|
+
const commitSave = useCallback(() => {
|
|
148
|
+
setSaving(true);
|
|
149
|
+
setUndone(false);
|
|
150
|
+
const started = now();
|
|
151
|
+
const { structural } = editor;
|
|
152
|
+
const fromOpen = editor.pendingOpen ?? false;
|
|
153
|
+
const size = reportRef.current.size;
|
|
154
|
+
const sizes = {
|
|
155
|
+
before: unitRef.current.count(editor.saved),
|
|
156
|
+
after: unitRef.current.count(editor.document),
|
|
157
|
+
};
|
|
158
|
+
return editor
|
|
159
|
+
.save()
|
|
160
|
+
.then((outcome) => {
|
|
161
|
+
if (outcome.ok === true) {
|
|
162
|
+
setMoved(sizes);
|
|
163
|
+
reportRef.current.saved({
|
|
164
|
+
size,
|
|
165
|
+
structural,
|
|
166
|
+
fromOpen,
|
|
167
|
+
durationMs: now() - started,
|
|
168
|
+
});
|
|
169
|
+
} else reportRef.current.refused(outcome.reason);
|
|
170
|
+
})
|
|
171
|
+
.finally(() => setSaving(false));
|
|
172
|
+
}, [editor]);
|
|
173
|
+
|
|
174
|
+
// A ref as well as `saving`, so a second click before the re-render does not write twice.
|
|
175
|
+
const undoingRef = useRef(false);
|
|
176
|
+
const focusSave = useRef(false);
|
|
177
|
+
// The notice that held focus is gone, so the key that follows is the next save; Save is enabled again once the write settles.
|
|
178
|
+
useEffect(() => {
|
|
179
|
+
if (!focusSave.current || saving) return;
|
|
180
|
+
focusSave.current = false;
|
|
181
|
+
saveButton.current?.focus();
|
|
182
|
+
}, [undone, saving]);
|
|
183
|
+
const undoSave = useCallback((): Promise<void> | void => {
|
|
184
|
+
if (!onSave || saving || undoingRef.current || !editor.canUndoSave)
|
|
185
|
+
return;
|
|
186
|
+
undoingRef.current = true;
|
|
187
|
+
setSaving(true);
|
|
188
|
+
const started = now();
|
|
189
|
+
const structural = editor.lastSave?.structural ?? false;
|
|
190
|
+
const size = reportRef.current.size;
|
|
191
|
+
return editor
|
|
192
|
+
.undoSave()
|
|
193
|
+
.then((outcome) => {
|
|
194
|
+
if (outcome.ok === true) {
|
|
195
|
+
focusSave.current = true;
|
|
196
|
+
setUndone(true);
|
|
197
|
+
reportRef.current.undone({
|
|
198
|
+
size,
|
|
199
|
+
structural,
|
|
200
|
+
fromOpen: false,
|
|
201
|
+
durationMs: now() - started,
|
|
202
|
+
});
|
|
203
|
+
} else reportRef.current.undoRefused(outcome.reason);
|
|
204
|
+
})
|
|
205
|
+
.finally(() => {
|
|
206
|
+
undoingRef.current = false;
|
|
207
|
+
setSaving(false);
|
|
208
|
+
});
|
|
209
|
+
}, [onSave, saving, editor]);
|
|
210
|
+
|
|
211
|
+
const guardedSave = useCallback((): Promise<void> | void => {
|
|
212
|
+
if (!onSave || !editor.dirty || saving) return;
|
|
213
|
+
return commitSave();
|
|
214
|
+
}, [onSave, editor, saving, commitSave]);
|
|
215
|
+
const guardedRef = useRef(guardedSave);
|
|
216
|
+
guardedRef.current = guardedSave;
|
|
217
|
+
const save = useCallback((): Promise<void> | void => {
|
|
218
|
+
const run = () => guardedRef.current();
|
|
219
|
+
return prepare ? prepare(run) : run();
|
|
220
|
+
}, [prepare]);
|
|
221
|
+
|
|
222
|
+
const exitGuard = useExitGuard({
|
|
223
|
+
dirty: hasEdits,
|
|
224
|
+
saving,
|
|
225
|
+
canSave: !!onSave,
|
|
226
|
+
save,
|
|
227
|
+
onExit: () => onExit?.(),
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
useBuilderShortcuts(
|
|
231
|
+
// One handlers object per change of what they read, so the key listener is not torn down and re-bound on every render.
|
|
232
|
+
useMemo(
|
|
233
|
+
() => ({
|
|
234
|
+
...shortcuts,
|
|
235
|
+
undo: editor.undo,
|
|
236
|
+
redo: editor.redo,
|
|
237
|
+
...(onSave ? { save } : {}),
|
|
238
|
+
paused: exitGuard.dialog.open || viewing,
|
|
239
|
+
}),
|
|
240
|
+
[shortcuts, editor, onSave, save, exitGuard.dialog.open, viewing],
|
|
241
|
+
),
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
const canUndoSave = !!onSave && !saving && editor.canUndoSave;
|
|
245
|
+
const lastSave = editor.lastSave;
|
|
246
|
+
|
|
247
|
+
const notice: SaveNoticeProps = {
|
|
248
|
+
...(lastSave ? { lastSave } : {}),
|
|
249
|
+
unit: unit.name,
|
|
250
|
+
moved,
|
|
251
|
+
canUndoSave,
|
|
252
|
+
undone: undone && lastSave === undefined,
|
|
253
|
+
viewing,
|
|
254
|
+
onView: setViewing,
|
|
255
|
+
onUndoSave: undoSave,
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
const toolbarProps: Pick<
|
|
259
|
+
BuilderToolbarProps,
|
|
260
|
+
| "canUndo"
|
|
261
|
+
| "canRedo"
|
|
262
|
+
| "onUndo"
|
|
263
|
+
| "onRedo"
|
|
264
|
+
| "dirty"
|
|
265
|
+
| "saving"
|
|
266
|
+
| "onSave"
|
|
267
|
+
| "onExit"
|
|
268
|
+
| "saveButton"
|
|
269
|
+
> = {
|
|
270
|
+
canUndo: editor.canUndo,
|
|
271
|
+
canRedo: editor.canRedo,
|
|
272
|
+
onUndo: editor.undo,
|
|
273
|
+
onRedo: editor.redo,
|
|
274
|
+
dirty,
|
|
275
|
+
saving,
|
|
276
|
+
saveButton,
|
|
277
|
+
...(onSave ? { onSave: save } : {}),
|
|
278
|
+
...(onExit ? { onExit: exitGuard.requestExit } : {}),
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
return {
|
|
282
|
+
saving,
|
|
283
|
+
save,
|
|
284
|
+
exitGuard,
|
|
285
|
+
/** Write the file back as it was before the last save, and put the edits back unsaved. */
|
|
286
|
+
undoSave,
|
|
287
|
+
canUndoSave,
|
|
288
|
+
lastSave,
|
|
289
|
+
notice,
|
|
290
|
+
toolbarProps,
|
|
291
|
+
};
|
|
292
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// Copyright (c) Credible Data Inc.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
|
|
5
|
+
import { afterEach, describe, expect, it, mock } from "bun:test";
|
|
6
|
+
import { isMac, useBuilderShortcuts } from "./useBuilderShortcuts";
|
|
7
|
+
|
|
8
|
+
const mod = isMac ? { metaKey: true } : { ctrlKey: true };
|
|
9
|
+
|
|
10
|
+
function Harness({ onSave }: { onSave: () => void }) {
|
|
11
|
+
useBuilderShortcuts({
|
|
12
|
+
undo: () => {},
|
|
13
|
+
redo: () => {},
|
|
14
|
+
save: onSave,
|
|
15
|
+
escape: () => {},
|
|
16
|
+
});
|
|
17
|
+
return <input aria-label="Field" />;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
afterEach(() => {
|
|
21
|
+
cleanup();
|
|
22
|
+
// The hosts are attached by hand, and a leaked role=dialog would show up in other specs.
|
|
23
|
+
document.body.replaceChildren();
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
/** Presses the save chord on `tag` placed inside `host`, which is attached to the page. */
|
|
27
|
+
const pressIn = (host: HTMLElement, tag: "input" | "button") => {
|
|
28
|
+
const onSave = mock(() => {});
|
|
29
|
+
render(<Harness onSave={onSave} />);
|
|
30
|
+
const inner = document.createElement(tag);
|
|
31
|
+
host.appendChild(inner);
|
|
32
|
+
document.body.appendChild(host);
|
|
33
|
+
inner.focus();
|
|
34
|
+
fireEvent.keyDown(inner, { key: "s", ...mod });
|
|
35
|
+
return onSave;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
describe("useBuilderShortcuts: save inside a dialog or popover", () => {
|
|
39
|
+
it("does not save from a field inside a dialog, whose draft commits on close", () => {
|
|
40
|
+
const host = document.createElement("div");
|
|
41
|
+
host.setAttribute("role", "dialog");
|
|
42
|
+
expect(pressIn(host, "input")).not.toHaveBeenCalled();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("does not save from a field inside a popover", () => {
|
|
46
|
+
const host = document.createElement("div");
|
|
47
|
+
host.className = "MuiPopover-root";
|
|
48
|
+
expect(pressIn(host, "input")).not.toHaveBeenCalled();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("does not save from outside a text field while a dialog holds focus", () => {
|
|
52
|
+
const host = document.createElement("div");
|
|
53
|
+
host.className = "MuiDialog-root";
|
|
54
|
+
expect(pressIn(host, "button")).not.toHaveBeenCalled();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("still saves from a plain inline field", () => {
|
|
58
|
+
const onSave = mock(() => {});
|
|
59
|
+
render(<Harness onSave={onSave} />);
|
|
60
|
+
const field = screen.getByLabelText("Field");
|
|
61
|
+
field.focus();
|
|
62
|
+
fireEvent.keyDown(field, { key: "s", ...mod });
|
|
63
|
+
expect(onSave).toHaveBeenCalledTimes(1);
|
|
64
|
+
});
|
|
65
|
+
});
|