viewkitjs 0.30.1 → 0.32.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +143 -0
- package/MIGRATION.md +40 -6
- package/README.md +1 -1
- package/fields/dist/query/index.d.ts +2 -0
- package/fields/dist/query/index.js +2 -0
- package/fields/dist/query/list-request.d.ts +148 -0
- package/fields/dist/query/list-request.js +125 -0
- package/fields/dist/query/saved-view.d.ts +15 -0
- package/fields/dist/query/saved-view.js +11 -0
- package/fields/dist/query/settings.d.ts +9 -0
- package/fields/dist/query/settings.js +9 -0
- package/fields/docs/reference/query.md +66 -0
- package/package.json +8 -2
- package/postgres/README.md +1 -0
- package/postgres/dist/handler.d.ts +53 -0
- package/postgres/dist/handler.js +211 -0
- package/postgres/dist/ids.d.ts +1 -1
- package/postgres/dist/ids.js +19 -1
- package/postgres/dist/index.d.ts +1 -0
- package/postgres/dist/index.js +1 -0
- package/postgres/dist/limits.d.ts +3 -1
- package/postgres/dist/limits.js +1 -1
- package/postgres/dist/list-fields.d.ts +10 -0
- package/postgres/dist/list-fields.js +36 -0
- package/postgres/dist/readable.d.ts +4 -0
- package/postgres/dist/readable.js +4 -0
- package/postgres/dist/refusals.d.ts +9 -2
- package/postgres/dist/refusals.js +6 -0
- package/postgres/docs/guides/field-map.md +2 -2
- package/postgres/docs/guides/list-handler.md +99 -0
- package/postgres/docs/index.md +1 -0
- package/postgres/docs/reference/postgres.md +11 -3
- package/views/README.md +2 -2
- package/views/dist/atoms/RowMenu.d.ts +7 -0
- package/views/dist/atoms/RowMenu.js +18 -16
- package/views/dist/atoms/carried.d.ts +5 -0
- package/views/dist/atoms/carried.js +7 -0
- package/views/dist/atoms/types.d.ts +3 -0
- package/views/dist/contract.d.ts +26 -6
- package/views/dist/contract.js +25 -0
- package/views/dist/controls/alert-dialog/alert-dialog.js +2 -1
- package/views/dist/controls/atoms.d.ts +2 -0
- package/views/dist/controls/atoms.js +2 -0
- package/views/dist/controls/avatar/avatar.d.ts +10 -4
- package/views/dist/controls/avatar/avatar.js +5 -3
- package/views/dist/controls/avatar/tokens.d.ts +104 -0
- package/views/dist/controls/avatar/tokens.js +26 -0
- package/views/dist/controls/avatar.css +46 -15
- package/views/dist/controls/choice/tokens.d.ts +0 -4
- package/views/dist/controls/choice/tokens.js +0 -1
- package/views/dist/controls/choice.css +3 -3
- package/views/dist/controls/controls.d.ts +6 -0
- package/views/dist/controls/controls.js +10 -2
- package/views/dist/controls/customize/map-customize-body.js +1 -1
- package/views/dist/controls/field/field.d.ts +19 -4
- package/views/dist/controls/field/field.js +63 -5
- package/views/dist/controls/field/tokens.d.ts +4 -0
- package/views/dist/controls/field/tokens.js +1 -0
- package/views/dist/controls/field.css +41 -16
- package/views/dist/controls/glyphs/glyphs.d.ts +10 -0
- package/views/dist/controls/glyphs/glyphs.js +173 -0
- package/views/dist/controls/index.d.ts +4 -2
- package/views/dist/controls/index.js +3 -1
- package/views/dist/controls/input/input.d.ts +12 -1
- package/views/dist/controls/input/input.js +10 -6
- package/views/dist/controls/input/save-mark.d.ts +8 -0
- package/views/dist/controls/input/save-mark.js +26 -0
- package/views/dist/controls/input/tokens.d.ts +20 -0
- package/views/dist/controls/input/tokens.js +5 -0
- package/views/dist/controls/input.css +49 -3
- package/views/dist/controls/internal/icons.js +10 -0
- package/views/dist/controls/internal/keep-focus.d.ts +2 -0
- package/views/dist/controls/internal/keep-focus.js +17 -0
- package/views/dist/controls/internal/watch-frame.d.ts +2 -0
- package/views/dist/controls/internal/watch-frame.js +33 -0
- package/views/dist/controls/markdown/map-markdown.d.ts +4 -0
- package/views/dist/controls/markdown/map-markdown.js +5 -0
- package/views/dist/controls/menu/map-menu.js +2 -2
- package/views/dist/controls/menu-list/menu-list.d.ts +3 -0
- package/views/dist/controls/menu-list/menu-list.js +5 -4
- package/views/dist/controls/modal/modal.js +5 -1
- package/views/dist/controls/pane/pane.js +2 -1
- package/views/dist/controls/phone-header/header-bar.d.ts +5 -1
- package/views/dist/controls/phone-header/header-bar.js +2 -2
- package/views/dist/controls/phone-header.css +11 -1
- package/views/dist/controls/picker/map-picker.js +3 -0
- package/views/dist/controls/picker/picker.d.ts +14 -0
- package/views/dist/controls/picker/picker.js +30 -10
- package/views/dist/controls/picker.css +2 -0
- package/views/dist/controls/popover/popover.js +2 -1
- package/views/dist/controls/radio/radio.d.ts +12 -3
- package/views/dist/controls/radio/radio.js +25 -5
- package/views/dist/controls/radio/tokens.d.ts +84 -0
- package/views/dist/controls/radio/tokens.js +21 -0
- package/views/dist/controls/radio.css +130 -3
- package/views/dist/controls/select/select.d.ts +12 -2
- package/views/dist/controls/select/select.js +15 -7
- package/views/dist/controls/select/tokens.d.ts +8 -0
- package/views/dist/controls/select/tokens.js +2 -0
- package/views/dist/controls/select.css +17 -0
- package/views/dist/controls/setting-row/looks.d.ts +10 -0
- package/views/dist/controls/setting-row/looks.js +22 -0
- package/views/dist/controls/setting-row/setting-row.d.ts +100 -0
- package/views/dist/controls/setting-row/setting-row.js +133 -0
- package/views/dist/controls/setting-row/tokens.d.ts +156 -0
- package/views/dist/controls/setting-row/tokens.js +41 -0
- package/views/dist/controls/setting-row.css +215 -0
- package/views/dist/controls/sheet/bottom-sheet.d.ts +10 -2
- package/views/dist/controls/sheet/bottom-sheet.js +6 -3
- package/views/dist/controls/sheet.css +4 -1
- package/views/dist/controls/sort-editor/sort-editor.js +3 -2
- package/views/dist/controls/sort-editor.css +8 -2
- package/views/dist/controls/switch-row/switch-row.d.ts +1 -15
- package/views/dist/controls/switch-row/switch-row.js +1 -9
- package/views/dist/controls/switch-row/tokens.d.ts +0 -12
- package/views/dist/controls/switch-row/tokens.js +0 -3
- package/views/dist/controls/switch-row.css +3 -7
- package/views/dist/controls/switch.css +17 -7
- package/views/dist/controls/tag/tag.d.ts +3 -1
- package/views/dist/controls/tag/tag.js +2 -2
- package/views/dist/controls/tag/tokens.d.ts +4 -0
- package/views/dist/controls/tag/tokens.js +1 -0
- package/views/dist/controls/tag-field/tag-field.d.ts +5 -1
- package/views/dist/controls/tag-field/tag-field.js +11 -5
- package/views/dist/controls/tag-field/tokens.d.ts +16 -0
- package/views/dist/controls/tag-field/tokens.js +4 -0
- package/views/dist/controls/tag-field.css +19 -0
- package/views/dist/controls/tag.css +2 -0
- package/views/dist/controls/tokens.js +2 -0
- package/views/dist/controls/toolbar/toolbar.d.ts +15 -3
- package/views/dist/controls/toolbar/toolbar.js +22 -15
- package/views/dist/controls/toolbar-button/atom-toolbar-button.d.ts +14 -0
- package/views/dist/controls/toolbar-button/atom-toolbar-button.js +51 -0
- package/views/dist/controls/toolbar-button/toolbar-button.d.ts +1 -1
- package/views/dist/controls/toolbar-button/toolbar-button.js +2 -3
- package/views/dist/controls/toolbar-button.css +4 -0
- package/views/dist/controls/toolbar.css +5 -0
- package/views/dist/controls/views-list/map-view-menu.d.ts +3 -0
- package/views/dist/controls/views-list/map-view-menu.js +25 -0
- package/views/dist/controls/views-list/map-views-list-body.d.ts +28 -1
- package/views/dist/controls/views-list/map-views-list-body.js +39 -21
- package/views/dist/controls/views-list/rules.d.ts +2 -0
- package/views/dist/controls/views-list/view-menu.d.ts +7 -0
- package/views/dist/controls/views-list/view-menu.js +28 -24
- package/views/dist/controls/views-list/words.d.ts +2 -0
- package/views/dist/controls/views-list/words.js +1 -1
- package/views/dist/editors/EditorPanel.js +2 -1
- package/views/dist/editors/edit-session.js +2 -5
- package/views/dist/field-row/FieldRow.d.ts +4 -2
- package/views/dist/field-row/FieldRow.js +67 -20
- package/views/dist/field-row/FieldRowLane.d.ts +7 -2
- package/views/dist/field-row/FieldRowLane.js +4 -3
- package/views/dist/field-row/editors/RowEditor.d.ts +12 -0
- package/views/dist/field-row/editors/RowEditor.js +31 -1
- package/views/dist/field-row/editors/RowValueListEditor.d.ts +5 -1
- package/views/dist/field-row/editors/RowValueListEditor.js +4 -1
- package/views/dist/field-row/field-row-button.d.ts +5 -0
- package/views/dist/field-row/field-row-button.js +2 -0
- package/views/dist/fields/binding.d.ts +3 -2
- package/views/dist/fields/binding.js +38 -5
- package/views/dist/grid/chrome/GridColumnMenu.d.ts +4 -2
- package/views/dist/grid/chrome/GridColumnMenu.js +3 -2
- package/views/dist/grid/chrome/GridGroupMenu.d.ts +4 -1
- package/views/dist/grid/chrome/GridGroupMenu.js +4 -3
- package/views/dist/grid/chrome/GridHeader.d.ts +4 -1
- package/views/dist/grid/chrome/GridHeader.js +2 -2
- package/views/dist/grid/chrome/GridRow.js +6 -5
- package/views/dist/grid/chrome/GridRowCells.js +6 -9
- package/views/dist/grid/chrome/GridStructure.d.ts +4 -1
- package/views/dist/grid/chrome/GridStructure.js +4 -4
- package/views/dist/grid/chrome/GridTable.js +5 -2
- package/views/dist/grid/chrome/grid-column-menu-items.js +1 -1
- package/views/dist/grid/chrome/grid-row-menu-items.d.ts +27 -3
- package/views/dist/grid/chrome/grid-row-menu-items.js +92 -6
- package/views/dist/grid/chrome/grid-row-props.d.ts +7 -2
- package/views/dist/grid/chrome/grid-rows-menu.js +34 -6
- package/views/dist/grid/chrome/grid-table-props.d.ts +38 -7
- package/views/dist/grid/chrome/useGridBatchApi.d.ts +3 -1
- package/views/dist/grid/chrome/useGridBatchApi.js +4 -2
- package/views/dist/grid/chrome/useGridKeyboard.d.ts +4 -2
- package/views/dist/grid/chrome/useGridKeyboard.js +15 -9
- package/views/dist/grid/chrome/useGridPendingReconciliation.js +1 -2
- package/views/dist/grid/chrome/useGridTableInteractionModel.d.ts +1 -1
- package/views/dist/grid/chrome/useGridTableInteractionModel.js +11 -5
- package/views/dist/grid/core/commit-model.d.ts +20 -2
- package/views/dist/grid/core/editing-controller.d.ts +18 -0
- package/views/dist/grid/core/editing-controller.js +188 -42
- package/views/dist/grid/core/editing-model.d.ts +3 -0
- package/views/dist/grid/core/editing-model.js +3 -0
- package/views/dist/grid/core/save-path.d.ts +24 -0
- package/views/dist/grid/core/save-path.js +122 -0
- package/views/dist/grid/core/spreadsheet-operations.d.ts +3 -0
- package/views/dist/grid/core/spreadsheet-operations.js +23 -0
- package/views/dist/grid/index.d.ts +3 -2
- package/views/dist/grid/index.js +1 -0
- package/views/dist/internal/field-rows.d.ts +26 -0
- package/views/dist/internal/field-rows.js +8 -0
- package/views/dist/internal/page-close.d.ts +7 -0
- package/views/dist/internal/page-close.js +43 -0
- package/views/dist/list/browser-views-store.d.ts +10 -0
- package/views/dist/list/browser-views-store.js +182 -0
- package/views/dist/list/index.d.ts +23 -0
- package/views/dist/list/index.js +8 -0
- package/views/dist/list/menu.d.ts +31 -0
- package/views/dist/list/menu.js +76 -0
- package/views/dist/list/parts.d.ts +6 -0
- package/views/dist/list/parts.js +59 -0
- package/views/dist/list/registry.d.ts +11 -0
- package/views/dist/list/registry.js +31 -0
- package/views/dist/list/root.d.ts +40 -0
- package/views/dist/list/root.js +128 -0
- package/views/dist/list/slot.d.ts +14 -0
- package/views/dist/list/slot.js +22 -0
- package/views/dist/list/source.d.ts +11 -0
- package/views/dist/list/source.js +31 -0
- package/views/dist/list/store.d.ts +87 -0
- package/views/dist/list/store.js +115 -0
- package/views/dist/list/toolbar.d.ts +6 -0
- package/views/dist/list/toolbar.js +359 -0
- package/views/dist/list/views-axis.d.ts +5 -0
- package/views/dist/list/views-axis.js +96 -0
- package/views/dist/list/views-store.d.ts +41 -0
- package/views/dist/list/views-store.js +1 -0
- package/views/dist/list/views.d.ts +67 -0
- package/views/dist/list/views.js +400 -0
- package/views/dist/looks/index.d.ts +1 -0
- package/views/dist/looks/index.js +1 -0
- package/views/dist/looks/pieces.js +2 -0
- package/views/dist/model/preferences.js +1 -1
- package/views/dist/model/prefs-buckets.js +1 -1
- package/views/dist/panels/index.d.ts +3 -1
- package/views/dist/panels/index.js +1 -0
- package/views/dist/panels/react/customize-body.js +1 -0
- package/views/dist/panels/react/toolbar/ReadyToolbar.d.ts +41 -4
- package/views/dist/panels/react/toolbar/ReadyToolbar.js +6 -326
- package/views/dist/panels/react/toolbar/Toolbar.d.ts +9 -1
- package/views/dist/panels/react/toolbar/Toolbar.js +34 -23
- package/views/dist/panels/react/toolbar/ToolbarButton.js +4 -1
- package/views/dist/panels/react/toolbar/ToolbarNew.js +1 -1
- package/views/dist/panels/react/toolbar/ToolbarQuickFilters.d.ts +2 -2
- package/views/dist/panels/react/toolbar/ToolbarQuickFilters.js +3 -1
- package/views/dist/panels/react/toolbar/ToolbarSearch.js +1 -1
- package/views/dist/panels/react/toolbar/context.d.ts +10 -2
- package/views/dist/panels/react/toolbar/context.js +3 -1
- package/views/dist/panels/react/views-row/FoldingViewRow.d.ts +1 -1
- package/views/dist/panels/react/views-row/FoldingViewRow.js +15 -4
- package/views/dist/panels/react/views-row/PillGroup.d.ts +5 -2
- package/views/dist/panels/react/views-row/PillGroup.js +16 -7
- package/views/dist/panels/react/views-row/ViewPicker.js +2 -12
- package/views/dist/panels/react/views-row/ViewPickerRow.d.ts +4 -2
- package/views/dist/panels/react/views-row/ViewPickerRow.js +9 -5
- package/views/dist/panels/react.d.ts +2 -2
- package/views/dist/panels/toolbar/edit-view.d.ts +7 -1
- package/views/dist/panels/toolbar/edit-view.js +14 -0
- package/views/dist/panels/toolbar/model.d.ts +5 -0
- package/views/dist/panels/toolbar/model.js +7 -3
- package/views/dist/panels/view-state.d.ts +5 -3
- package/views/dist/panels/views-row/model.d.ts +19 -2
- package/views/dist/panels/views-row/model.js +14 -0
- package/views/dist/provider/ViewsProvider.d.ts +16 -7
- package/views/dist/provider/ViewsProvider.js +23 -5
- package/views/dist/provider/components.d.ts +43 -2
- package/views/dist/provider/context.d.ts +2 -1
- package/views/dist/provider/context.js +1 -1
- package/views/dist/provider/ports.d.ts +19 -12
- package/views/dist/provider/ports.js +69 -1
- package/views/dist/react.d.ts +1 -1
- package/views/dist/styles.css +583 -61
- package/views/dist/styles.unlayered.css +583 -61
- package/views/docs/contract.md +42 -31
- package/views/docs/controls/avatar.md +26 -0
- package/views/docs/controls/choice.md +0 -1
- package/views/docs/controls/field.md +3 -0
- package/views/docs/controls/input.md +9 -0
- package/views/docs/controls/radio.md +33 -0
- package/views/docs/controls/select.md +6 -0
- package/views/docs/controls/setting-row.md +44 -0
- package/views/docs/controls/switch-row.md +0 -3
- package/views/docs/controls/tag-field.md +4 -0
- package/views/docs/controls/tag.md +1 -0
- package/views/docs/getting-started.md +2 -2
- package/views/docs/guides/atoms.md +2 -2
- package/views/docs/guides/columns.md +1 -1
- package/views/docs/guides/ports.md +1 -1
- package/views/docs/guides/the-grid.md +34 -7
- package/views/docs/guides/the-provider.md +4 -2
- package/views/docs/index.md +1 -0
- package/views/docs/reference/controls.md +54 -27
- package/views/docs/reference/editors-session.md +1 -1
- package/views/docs/reference/field-row.md +9 -1
- package/views/docs/reference/grid.md +14 -8
- package/views/docs/reference/list.md +227 -0
- package/views/docs/reference/looks.md +3 -0
- package/views/docs/reference/panels-react.md +17 -12
- package/views/docs/reference/panels.md +8 -5
- package/views/docs/reference/react.md +11 -9
- package/views/docs/reference/views.md +2 -2
- package/views/skills/views/SKILL.md +9 -6
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import type { FieldSettlement } from "viewkitjs/fields";
|
|
2
2
|
/** `reload: false`: the commit's own reload is skipped because its caller reloads once after a second
|
|
3
|
-
* write — a board drop under manual order saves the order next, and that reload shows both.
|
|
3
|
+
* write — a board drop under manual order saves the order next, and that reload shows both.
|
|
4
|
+
* `closing`: the page is going (`pagehide`): send it so it outlives the page (`fetch(…, { keepalive: true })`). */
|
|
4
5
|
export type GridCommitContext = {
|
|
5
6
|
kind: "commit";
|
|
6
7
|
reload?: false;
|
|
8
|
+
closing?: true;
|
|
7
9
|
} | {
|
|
8
10
|
kind: "restore";
|
|
9
11
|
expected: unknown;
|
|
12
|
+
closing?: true;
|
|
10
13
|
};
|
|
11
14
|
export type GridCommitOutcome = {
|
|
12
15
|
status: "confirmed";
|
|
@@ -22,7 +25,8 @@ export type GridCommitOutcome = {
|
|
|
22
25
|
* as null): the value as the row now reads it in this column. Present, even
|
|
23
26
|
* as null, the cell shows it in place of what was typed, undo and redo
|
|
24
27
|
* restore from it (unless `history` says otherwise), and the rows that carry
|
|
25
|
-
* it next clear the cell without a word.
|
|
28
|
+
* it next clear the cell without a word. On an undo or a redo the cell shows it
|
|
29
|
+
* until the rows carry it; the history is left as it was. */
|
|
26
30
|
stored?: unknown;
|
|
27
31
|
} | {
|
|
28
32
|
status: "unconfirmed";
|
|
@@ -31,6 +35,20 @@ export type GridCommitOutcome = {
|
|
|
31
35
|
status: "failed";
|
|
32
36
|
error?: unknown;
|
|
33
37
|
};
|
|
38
|
+
/** One record's change, as an app's save receives it: the record's id, its row as the grid holds it, the values to
|
|
39
|
+
* write (one key or more per cell, merged), and whether the page is going (send it so it outlives the page). */
|
|
40
|
+
export type ListChange<Row> = {
|
|
41
|
+
id: string;
|
|
42
|
+
row: Row;
|
|
43
|
+
values: Record<string, unknown>;
|
|
44
|
+
closing?: boolean;
|
|
45
|
+
};
|
|
46
|
+
/** The one function an app gives to save a change to a record: an edit, a paste, a fill, an undo or a redo. It
|
|
47
|
+
* resolves once the record holds the values, with what was kept where the server stored something else; it rejects
|
|
48
|
+
* to refuse the change. */
|
|
49
|
+
export type ListSave<Row> = (change: ListChange<Row>) => Promise<void | {
|
|
50
|
+
stored?: Partial<Row>;
|
|
51
|
+
}>;
|
|
34
52
|
/** One write the app refused, as the editing core reports it: the columns of the cells that failed, in order, how many,
|
|
35
53
|
* and the first failure's error (or the call's rejection). */
|
|
36
54
|
export type GridCommitRefusal = {
|
|
@@ -68,6 +68,10 @@ export declare class GridEditingController<Row> {
|
|
|
68
68
|
* as success: a caller awaiting this should confirm, not report a failure.
|
|
69
69
|
*/
|
|
70
70
|
commit(cells: GridCommitCell<Row>[]): Promise<boolean>;
|
|
71
|
+
/** The rows were read again, and these cells now show their overlay's value (or have moved past it). A press's
|
|
72
|
+
* overlay not yet sent is held: rows that show what it puts back before its write went out may be rows the edit it
|
|
73
|
+
* takes back hasn't reached yet. It is cleared when its restore is confirmed, if the latest rows still showed it:
|
|
74
|
+
* every pass says again what the rows show, the press sent or not. */
|
|
71
75
|
reconcile(matchedKeys: readonly string[]): void;
|
|
72
76
|
undo(resolveRow: ResolveRow<Row>, onApply?: (pos: GridCellPos) => void): boolean;
|
|
73
77
|
redo(resolveRow: ResolveRow<Row>, onApply?: (pos: GridCellPos) => void): boolean;
|
|
@@ -136,7 +140,21 @@ export declare class GridEditingController<Row> {
|
|
|
136
140
|
private performBatch;
|
|
137
141
|
private inflightFor;
|
|
138
142
|
private enqueueApply;
|
|
143
|
+
/** Takes a press's overlay off these cells where it still stands, putting back what it covered; a later press that
|
|
144
|
+
* covered it covers what it covered instead. */
|
|
145
|
+
private liftPress;
|
|
146
|
+
/** What a waiting press covered follows the overlay it covered: gone with a refused write, the kept value with a
|
|
147
|
+
* stored one, marked with a `requested` one. */
|
|
148
|
+
private reviseCovered;
|
|
149
|
+
/** Is this cell's overlay a press's own, not yet sent? */
|
|
150
|
+
private unsentPressOn;
|
|
139
151
|
private applyPin;
|
|
152
|
+
/** Decides what a press writes and hands it to the app, synchronously up to the call: after the wait for the edit
|
|
153
|
+
* it takes back, or inside the page's close. Null when it sends nothing. */
|
|
154
|
+
private sendPin;
|
|
155
|
+
/** The page is going: a press still waiting for the edit it takes back is sent now, inside the event, or never. Its
|
|
156
|
+
* answer settles it as usual if the page stays (the back-forward cache). */
|
|
157
|
+
private sendAtClose;
|
|
140
158
|
/** A press that applied hands what it put back to the first edit written over each of its cells since: that edit
|
|
141
159
|
* was typed over what the press took back, so its own undo has to put back what the press put back. */
|
|
142
160
|
private carryPutBack;
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { gridCellKey, } from "./commit-model.js";
|
|
2
|
-
import { applyGridPending, carryGridPutBack, createGridHistory, gridEditValue, gridOverlayValue, gridHistoryValue, gridKeptValue, groupGridPendingByRow, groupGridPendingSettlement, keepGridPending, markGridPendingSettlement, planGridPendingAssignments, pressGridHistory, pruneGridEntry, pruneGridHistoryEntry, reconcileGridPending, recordGridHistory, revertGridHistoryPress, rollbackGridPending, } from "./editing-model.js";
|
|
2
|
+
import { applyGridPending, carryGridPutBack, createGridHistory, gridEditValue, gridOverlayValue, gridHistoryValue, gridKeptValue, gridRestoreShown, groupGridPendingByRow, groupGridPendingSettlement, keepGridPending, markGridPendingSettlement, planGridPendingAssignments, pressGridHistory, pruneGridEntry, pruneGridHistoryEntry, reconcileGridPending, recordGridHistory, revertGridHistoryPress, rollbackGridPending, } from "./editing-model.js";
|
|
3
3
|
import { setGridWriteStates } from "./write-states.js";
|
|
4
4
|
import { refusalWords } from "./refusal-sentence.js";
|
|
5
|
+
import { isPageClosing, onPageClose } from "../../internal/page-close.js";
|
|
5
6
|
import { noteGridEdited } from "./undo-owner.js";
|
|
7
|
+
import { markGridKeptValue } from "./value-equality.js";
|
|
6
8
|
const confirmed = (outcome) => outcome ?? { status: "confirmed" };
|
|
7
9
|
export class GridEditingController {
|
|
8
10
|
callbacks;
|
|
@@ -95,9 +97,16 @@ export class GridEditingController {
|
|
|
95
97
|
});
|
|
96
98
|
}
|
|
97
99
|
reconcile(matchedKeys) {
|
|
98
|
-
|
|
100
|
+
for (const pin of this.pins)
|
|
101
|
+
pin.heldBack.clear();
|
|
102
|
+
const settled = matchedKeys.filter((key) => {
|
|
103
|
+
const pin = this.unsentPressOn(key);
|
|
104
|
+
pin?.heldBack.add(key);
|
|
105
|
+
return pin == null;
|
|
106
|
+
});
|
|
107
|
+
if (settled.length === 0)
|
|
99
108
|
return;
|
|
100
|
-
const next = reconcileGridPending(this.pending,
|
|
109
|
+
const next = reconcileGridPending(this.pending, settled);
|
|
101
110
|
if (next.size === this.pending.size)
|
|
102
111
|
return;
|
|
103
112
|
this.publishPending(next);
|
|
@@ -152,11 +161,14 @@ export class GridEditingController {
|
|
|
152
161
|
const owned = assignments.filter((assignment) => requested.has(assignment.key));
|
|
153
162
|
if (owned.length > 0)
|
|
154
163
|
next = markGridPendingSettlement(next, owned, "requested");
|
|
164
|
+
this.reviseCovered(kept.map((each) => each.assignment), { stored: storedByKey });
|
|
165
|
+
this.reviseCovered(owned, { settlement: "requested" });
|
|
155
166
|
if (next !== this.pending)
|
|
156
167
|
this.publishPending(next);
|
|
157
168
|
}
|
|
158
169
|
hasInflight(rowId, columnId) {
|
|
159
|
-
|
|
170
|
+
const key = gridCellKey(rowId, columnId);
|
|
171
|
+
return (this.inflight.get(key)?.size ?? 0) > 0 || this.unsentPressOn(key) != null;
|
|
160
172
|
}
|
|
161
173
|
sent(rowId, columnId) {
|
|
162
174
|
const newest = this.lastSent.get(gridCellKey(rowId, columnId))?.writes.at(-1);
|
|
@@ -348,27 +360,34 @@ export class GridEditingController {
|
|
|
348
360
|
}
|
|
349
361
|
let commit;
|
|
350
362
|
try {
|
|
363
|
+
const closing = isPageClosing();
|
|
351
364
|
commit = Promise.resolve(context === "restore"
|
|
352
|
-
? onCellCommit(row, columnId, value, { kind: "restore", expected: baseline })
|
|
353
|
-
:
|
|
365
|
+
? onCellCommit(row, columnId, value, { kind: "restore", expected: baseline, ...(closing ? { closing } : {}) })
|
|
366
|
+
: closing
|
|
367
|
+
? onCellCommit(row, columnId, value, { kind: "commit", closing })
|
|
368
|
+
: onCellCommit(row, columnId, value));
|
|
354
369
|
}
|
|
355
370
|
catch (error) {
|
|
356
371
|
commit = Promise.reject(error);
|
|
357
372
|
}
|
|
373
|
+
const refuse = (result) => {
|
|
374
|
+
if (sentToken)
|
|
375
|
+
this.dropSent(key, sentToken);
|
|
376
|
+
if (assignments.length > 0) {
|
|
377
|
+
this.publishPending(rollbackGridPending(this.pending, assignments));
|
|
378
|
+
this.reviseCovered(assignments, "drop");
|
|
379
|
+
}
|
|
380
|
+
return result;
|
|
381
|
+
};
|
|
358
382
|
const promise = commit.then((outcome) => {
|
|
359
383
|
const result = confirmed(outcome);
|
|
384
|
+
if (result.status === "failed")
|
|
385
|
+
return refuse(result);
|
|
360
386
|
if (sentToken)
|
|
361
387
|
this.settleSent(key, sentToken, result, value);
|
|
362
388
|
this.settle([key], [result], assignments);
|
|
363
389
|
return result;
|
|
364
|
-
}, (error) => {
|
|
365
|
-
if (sentToken)
|
|
366
|
-
this.dropSent(key, sentToken);
|
|
367
|
-
if (assignments.length > 0) {
|
|
368
|
-
this.publishPending(rollbackGridPending(this.pending, assignments));
|
|
369
|
-
}
|
|
370
|
-
return { status: "failed", error };
|
|
371
|
-
});
|
|
390
|
+
}, (error) => refuse({ status: "failed", error }));
|
|
372
391
|
this.registerInflight([key], promise);
|
|
373
392
|
this.trackWrite([{ rowId, columnId }], promise.then((result) => [result]));
|
|
374
393
|
return promise;
|
|
@@ -396,6 +415,7 @@ export class GridEditingController {
|
|
|
396
415
|
}
|
|
397
416
|
let commit;
|
|
398
417
|
try {
|
|
418
|
+
const closing = isPageClosing();
|
|
399
419
|
commit = Promise.resolve(onCellsCommit(cells.map(({ row, cell }) => ({
|
|
400
420
|
row,
|
|
401
421
|
columnId: cell.columnId,
|
|
@@ -405,9 +425,12 @@ export class GridEditingController {
|
|
|
405
425
|
context: {
|
|
406
426
|
kind: "restore",
|
|
407
427
|
expected: gridHistoryValue(cell, side === "next" ? "prev" : "next"),
|
|
428
|
+
...(closing ? { closing } : {}),
|
|
408
429
|
},
|
|
409
430
|
}
|
|
410
|
-
:
|
|
431
|
+
: closing
|
|
432
|
+
? { context: { kind: "commit", closing } }
|
|
433
|
+
: {}),
|
|
411
434
|
}))));
|
|
412
435
|
}
|
|
413
436
|
catch (error) {
|
|
@@ -420,7 +443,9 @@ export class GridEditingController {
|
|
|
420
443
|
.filter((_, index) => results[index]?.status === "failed")
|
|
421
444
|
.map(({ cell }) => gridCellKey(cell.rowId, cell.columnId)));
|
|
422
445
|
if (failedKeys.size > 0) {
|
|
423
|
-
|
|
446
|
+
const failed = assignments.filter((assignment) => failedKeys.has(assignment.key));
|
|
447
|
+
this.publishPending(rollbackGridPending(this.pending, failed));
|
|
448
|
+
this.reviseCovered(failed, "drop");
|
|
424
449
|
}
|
|
425
450
|
const settled = cells.map((_, index) => results[index] ?? { status: "failed" });
|
|
426
451
|
if (sentTokens) {
|
|
@@ -440,7 +465,9 @@ export class GridEditingController {
|
|
|
440
465
|
.filter((_, index) => carried[index]?.status === "failed")
|
|
441
466
|
.map(({ cell }) => gridCellKey(cell.rowId, cell.columnId)));
|
|
442
467
|
if (failedKeys.size > 0) {
|
|
443
|
-
|
|
468
|
+
const failed = assignments.filter((assignment) => failedKeys.has(assignment.key));
|
|
469
|
+
this.publishPending(rollbackGridPending(this.pending, failed));
|
|
470
|
+
this.reviseCovered(failed, "drop");
|
|
444
471
|
}
|
|
445
472
|
return carried;
|
|
446
473
|
}
|
|
@@ -448,6 +475,7 @@ export class GridEditingController {
|
|
|
448
475
|
keys.forEach((key, index) => this.dropSent(key, sentTokens[index] ?? null));
|
|
449
476
|
if (assignments.length > 0) {
|
|
450
477
|
this.publishPending(rollbackGridPending(this.pending, assignments));
|
|
478
|
+
this.reviseCovered(assignments, "drop");
|
|
451
479
|
}
|
|
452
480
|
return cells.map(() => ({ status: "failed", error }));
|
|
453
481
|
});
|
|
@@ -470,8 +498,22 @@ export class GridEditingController {
|
|
|
470
498
|
if (!entry)
|
|
471
499
|
return false;
|
|
472
500
|
this.history = pressed.state;
|
|
473
|
-
const pin = {
|
|
501
|
+
const pin = {
|
|
502
|
+
kind,
|
|
503
|
+
entry,
|
|
504
|
+
since: this.nextHistoryId,
|
|
505
|
+
written: new Map(),
|
|
506
|
+
notes: [],
|
|
507
|
+
laid: [],
|
|
508
|
+
covered: new Map(),
|
|
509
|
+
sent: false,
|
|
510
|
+
heldBack: new Set(),
|
|
511
|
+
revealFailed: false,
|
|
512
|
+
closeOff: () => { },
|
|
513
|
+
};
|
|
474
514
|
this.pins.add(pin);
|
|
515
|
+
pin.closeOff = onPageClose(() => this.sendAtClose(pin, resolveRow));
|
|
516
|
+
const revision = this.nextPendingRevision++;
|
|
475
517
|
const side = kind === "undo" ? "prev" : "next";
|
|
476
518
|
const other = kind === "undo" ? "next" : "prev";
|
|
477
519
|
for (const cell of entry.edits) {
|
|
@@ -488,53 +530,124 @@ export class GridEditingController {
|
|
|
488
530
|
const write = this.notePutBack(cell, putBack, gridKeptValue(cell, other), entry.id, dropped);
|
|
489
531
|
if (write)
|
|
490
532
|
pin.notes.push({ key, write, from: carried?.from ?? null });
|
|
533
|
+
if (cell.overlay) {
|
|
534
|
+
pin.laid.push({
|
|
535
|
+
key,
|
|
536
|
+
value: carried && !cell.shown ? carried.value : gridRestoreShown(cell, side),
|
|
537
|
+
baseline: gridRestoreShown(cell, other),
|
|
538
|
+
revision,
|
|
539
|
+
});
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
if (pin.laid.length > 0) {
|
|
543
|
+
for (const { key } of pin.laid)
|
|
544
|
+
pin.covered.set(key, this.pending.get(key));
|
|
545
|
+
this.publishPending(applyGridPending(this.pending, pin.laid));
|
|
546
|
+
}
|
|
547
|
+
const first = entry.edits[0];
|
|
548
|
+
try {
|
|
549
|
+
if (first)
|
|
550
|
+
onApply?.({ rowId: first.rowId, columnId: first.columnId });
|
|
551
|
+
}
|
|
552
|
+
catch {
|
|
553
|
+
pin.revealFailed = true;
|
|
554
|
+
this.liftPress(pin, pin.laid.map((laid) => laid.key));
|
|
491
555
|
}
|
|
492
|
-
this.applyChain = this.applyChain.then(() => this.applyPin(pin, resolveRow
|
|
556
|
+
this.applyChain = this.applyChain.then(() => this.applyPin(pin, resolveRow));
|
|
493
557
|
return true;
|
|
494
558
|
}
|
|
495
|
-
|
|
496
|
-
let
|
|
559
|
+
liftPress(pin, keys) {
|
|
560
|
+
let next = null;
|
|
561
|
+
for (const key of keys) {
|
|
562
|
+
const laid = pin.laid.find((each) => each.key === key);
|
|
563
|
+
if (!laid)
|
|
564
|
+
continue;
|
|
565
|
+
const under = pin.covered.get(key);
|
|
566
|
+
for (const other of this.pins) {
|
|
567
|
+
if (other !== pin && other.covered.get(key)?.revision === laid.revision)
|
|
568
|
+
other.covered.set(key, under);
|
|
569
|
+
}
|
|
570
|
+
const now = next ?? this.pending;
|
|
571
|
+
if (now.get(key)?.revision !== laid.revision)
|
|
572
|
+
continue;
|
|
573
|
+
next ??= new Map(this.pending);
|
|
574
|
+
if (under)
|
|
575
|
+
next.set(key, under);
|
|
576
|
+
else
|
|
577
|
+
next.delete(key);
|
|
578
|
+
}
|
|
579
|
+
if (next)
|
|
580
|
+
this.publishPending(next);
|
|
581
|
+
}
|
|
582
|
+
reviseCovered(assignments, change) {
|
|
583
|
+
if (this.pins.size === 0 || assignments.length === 0)
|
|
584
|
+
return;
|
|
585
|
+
for (const pin of this.pins) {
|
|
586
|
+
for (const assignment of assignments) {
|
|
587
|
+
const held = pin.covered.get(assignment.key);
|
|
588
|
+
if (!held || held.revision !== assignment.revision)
|
|
589
|
+
continue;
|
|
590
|
+
if (change === "drop")
|
|
591
|
+
pin.covered.set(assignment.key, undefined);
|
|
592
|
+
else if ("stored" in change) {
|
|
593
|
+
if (!change.stored.has(assignment.key))
|
|
594
|
+
continue;
|
|
595
|
+
const value = change.stored.get(assignment.key);
|
|
596
|
+
markGridKeptValue(value);
|
|
597
|
+
pin.covered.set(assignment.key, { ...held, value });
|
|
598
|
+
}
|
|
599
|
+
else
|
|
600
|
+
pin.covered.set(assignment.key, { ...held, settlement: change.settlement });
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
unsentPressOn(key) {
|
|
605
|
+
const revision = this.pending.get(key)?.revision;
|
|
606
|
+
if (revision === undefined)
|
|
607
|
+
return null;
|
|
608
|
+
for (const pin of this.pins) {
|
|
609
|
+
if (!pin.sent && pin.laid.some((each) => each.key === key && each.revision === revision))
|
|
610
|
+
return pin;
|
|
611
|
+
}
|
|
612
|
+
return null;
|
|
613
|
+
}
|
|
614
|
+
async applyPin(pin, resolveRow) {
|
|
615
|
+
const kept = new Set();
|
|
497
616
|
try {
|
|
498
617
|
if (pin.entry) {
|
|
499
618
|
const inflight = this.inflightFor(pin.entry);
|
|
500
619
|
if (inflight.length > 0)
|
|
501
620
|
await Promise.allSettled(inflight);
|
|
502
621
|
}
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
const first = entry.edits[0];
|
|
507
|
-
onApply?.({ rowId: first.rowId, columnId: first.columnId });
|
|
508
|
-
const edits = entry.edits.filter((cell) => !(pin.written.get(gridCellKey(cell.rowId, cell.columnId))?.size ?? 0));
|
|
509
|
-
if (edits.length === 0) {
|
|
510
|
-
this.carryPutBack(pin, entry);
|
|
622
|
+
pin.sending ??= this.sendPin(pin, resolveRow);
|
|
623
|
+
const sending = await pin.sending;
|
|
624
|
+
if (!sending)
|
|
511
625
|
return;
|
|
512
|
-
}
|
|
513
|
-
const cells = edits.map((cell) => ({
|
|
514
|
-
row: resolveRow(cell.rowId) ?? cell.rowSnapshot,
|
|
515
|
-
cell,
|
|
516
|
-
}));
|
|
517
|
-
const overwritten = cells.map(({ cell }) => {
|
|
518
|
-
const key = gridCellKey(cell.rowId, cell.columnId);
|
|
519
|
-
const held = this.pending.get(key);
|
|
520
|
-
return held ? { key, value: held.value, revision: held.revision } : null;
|
|
521
|
-
});
|
|
522
|
-
sent = true;
|
|
523
|
-
const results = await this.performBatch(cells, pin.kind === "undo" ? "prev" : "next", "restore");
|
|
626
|
+
const { entry, cells, overwritten, results } = sending;
|
|
524
627
|
this.reportRefusals(cells, results);
|
|
628
|
+
cells.forEach(({ cell }, index) => {
|
|
629
|
+
if (results[index]?.status === "confirmed")
|
|
630
|
+
kept.add(gridCellKey(cell.rowId, cell.columnId));
|
|
631
|
+
});
|
|
632
|
+
this.settle(cells.map(({ cell }) => gridCellKey(cell.rowId, cell.columnId)), results, pin.laid.filter((laid) => kept.has(laid.key)));
|
|
525
633
|
const cleared = overwritten.filter((held, index) => held != null && results[index]?.status === "confirmed");
|
|
526
634
|
if (cleared.length > 0)
|
|
527
635
|
this.publishPending(rollbackGridPending(this.pending, cleared));
|
|
636
|
+
const landed = pin.laid.filter((laid) => kept.has(laid.key) && pin.heldBack.has(laid.key) && this.pending.get(laid.key)?.revision === laid.revision);
|
|
637
|
+
if (landed.length > 0)
|
|
638
|
+
this.publishPending(rollbackGridPending(this.pending, landed));
|
|
528
639
|
if (results.every((result) => result.status === "confirmed"))
|
|
529
640
|
this.carryPutBack(pin, entry);
|
|
530
641
|
else
|
|
531
642
|
this.refusePin(pin, entry);
|
|
532
643
|
}
|
|
533
644
|
catch {
|
|
534
|
-
if (!sent && pin.entry)
|
|
645
|
+
if (!pin.sent && pin.entry)
|
|
535
646
|
this.refusePin(pin, pin.entry);
|
|
536
647
|
}
|
|
537
648
|
finally {
|
|
649
|
+
pin.closeOff();
|
|
650
|
+
this.liftPress(pin, pin.laid.filter((laid) => !kept.has(laid.key)).map((laid) => laid.key));
|
|
538
651
|
for (const { key, write } of pin.notes) {
|
|
539
652
|
this.openNotes.delete(write.token);
|
|
540
653
|
this.dropSent(key, write.token);
|
|
@@ -543,6 +656,39 @@ export class GridEditingController {
|
|
|
543
656
|
this.settleCarriedNotes();
|
|
544
657
|
}
|
|
545
658
|
}
|
|
659
|
+
async sendPin(pin, resolveRow) {
|
|
660
|
+
const entry = pin.entry;
|
|
661
|
+
if (!entry || entry.edits.length === 0)
|
|
662
|
+
return null;
|
|
663
|
+
if (pin.revealFailed) {
|
|
664
|
+
this.refusePin(pin, entry);
|
|
665
|
+
return null;
|
|
666
|
+
}
|
|
667
|
+
const edits = entry.edits.filter((cell) => !(pin.written.get(gridCellKey(cell.rowId, cell.columnId))?.size ?? 0));
|
|
668
|
+
if (edits.length === 0) {
|
|
669
|
+
this.carryPutBack(pin, entry);
|
|
670
|
+
return null;
|
|
671
|
+
}
|
|
672
|
+
const cells = edits.map((cell) => ({
|
|
673
|
+
row: resolveRow(cell.rowId) ?? cell.rowSnapshot,
|
|
674
|
+
cell,
|
|
675
|
+
}));
|
|
676
|
+
const pressed = new Set([...this.pins].flatMap((each) => each.laid.map((laid) => laid.revision)));
|
|
677
|
+
const overwritten = cells.map(({ cell }) => {
|
|
678
|
+
const key = gridCellKey(cell.rowId, cell.columnId);
|
|
679
|
+
const held = this.pending.get(key);
|
|
680
|
+
return held && !pressed.has(held.revision) ? { key, value: held.value, revision: held.revision } : null;
|
|
681
|
+
});
|
|
682
|
+
pin.sent = true;
|
|
683
|
+
const results = await this.performBatch(cells, pin.kind === "undo" ? "prev" : "next", "restore");
|
|
684
|
+
return { entry, cells, overwritten, results };
|
|
685
|
+
}
|
|
686
|
+
sendAtClose(pin, resolveRow) {
|
|
687
|
+
if (pin.sending)
|
|
688
|
+
return;
|
|
689
|
+
pin.sending = this.sendPin(pin, resolveRow);
|
|
690
|
+
pin.sending.catch(() => { });
|
|
691
|
+
}
|
|
546
692
|
carryPutBack(pin, entry) {
|
|
547
693
|
const side = pin.kind === "undo" ? "prev" : "next";
|
|
548
694
|
for (const cell of entry.edits) {
|
|
@@ -59,6 +59,9 @@ export declare function gridOverlayValue<Row>(cell: GridEditCell<Row>, side: "ne
|
|
|
59
59
|
* with no `historyPrev`), else what was typed. An app's own `history` snapshots set both sides and are not editor
|
|
60
60
|
* values; what the app stored beside them is kept as `storedNext`. */
|
|
61
61
|
export declare function gridKeptValue<Row>(cell: GridEditCell<Row>, side: "next" | "prev"): unknown;
|
|
62
|
+
/** What an undo (`prev`) or a redo (`next`) draws over its cell from its press until the rows show it: a figure's number
|
|
63
|
+
* (`shown`), else the editor value the side holds as the rows read it (`gridKeptValue`). */
|
|
64
|
+
export declare function gridRestoreShown<Row>(cell: GridEditCell<Row>, side: "next" | "prev"): unknown;
|
|
62
65
|
/** An edit written over a cell while ⌘Z or ⌘⇧Z waited carries what that press put back, as what its own undo puts
|
|
63
66
|
* back: its `prev`, and its `historyPrev` only where the app answered it with its own snapshots. */
|
|
64
67
|
export declare function carryGridPutBack<Row>(cell: GridEditCell<Row>, prev: unknown, historyPrev: unknown): GridEditCell<Row>;
|
|
@@ -67,6 +67,9 @@ export function gridKeptValue(cell, side) {
|
|
|
67
67
|
return cell.historyNext;
|
|
68
68
|
return gridEditValue(cell, side);
|
|
69
69
|
}
|
|
70
|
+
export function gridRestoreShown(cell, side) {
|
|
71
|
+
return cell.shown ? cell.shown[side] : gridKeptValue(cell, side);
|
|
72
|
+
}
|
|
70
73
|
export function carryGridPutBack(cell, prev, historyPrev) {
|
|
71
74
|
return "historyPrev" in cell ? { ...cell, prev, historyPrev } : { ...cell, prev };
|
|
72
75
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { GridCellCommitFn, GridCellsCommitFn, GridCommitContext, ListSave } from "./commit-model.js";
|
|
2
|
+
export type GridSaveOptions<Row> = {
|
|
3
|
+
/** The record's id, which `save` is sent as `id`. */
|
|
4
|
+
getRowId: (row: Row) => string;
|
|
5
|
+
/** One cell's change as the values `save` is sent; throw to refuse it (its message is said through the toast).
|
|
6
|
+
* Default: `{ [columnId]: value }` (fields bound by their key). */
|
|
7
|
+
values?: (row: Row, columnId: string, value: unknown, context: GridCommitContext) => Record<string, unknown>;
|
|
8
|
+
/** A column's value off a row, to read what `stored` kept. Default: `row[columnId]`. */
|
|
9
|
+
read?: (row: Row, columnId: string) => unknown;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* The grid's two commit functions over one save: every write (an edit, a paste, a fill, an undo, a redo) is one call
|
|
13
|
+
* per record, its cells' values merged, and a record has one call in flight at a time, the next waiting for it to
|
|
14
|
+
* settle. When the page goes, every change still waiting is sent at once, marked `closing`, and leaves its record's
|
|
15
|
+
* queue: its own answer settles it; a call already out is not sent again. A cell whose `getRowId` or `values` throws
|
|
16
|
+
* is refused alone; a save that rejects refuses that record's cells with its error. Neither function ever rejects.
|
|
17
|
+
*
|
|
18
|
+
* The queues live in what one call returns: make it once per grid (at module scope, in a `useState` initializer, or a
|
|
19
|
+
* `useMemo` on a stable `save`), never in a render, or each render starts with empty queues.
|
|
20
|
+
*/
|
|
21
|
+
export declare function gridSave<Row>(save: ListSave<Row>, options: GridSaveOptions<Row>): {
|
|
22
|
+
onCellCommit: GridCellCommitFn<Row>;
|
|
23
|
+
onCellsCommit: GridCellsCommitFn<Row>;
|
|
24
|
+
};
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { isPageClosing, onPageClose } from "../../internal/page-close.js";
|
|
2
|
+
function keptOutcome(stored, keys, kept) {
|
|
3
|
+
if (!stored || !keys.some((key) => key in stored))
|
|
4
|
+
return { status: "confirmed" };
|
|
5
|
+
try {
|
|
6
|
+
return { status: "confirmed", stored: kept() };
|
|
7
|
+
}
|
|
8
|
+
catch {
|
|
9
|
+
return { status: "confirmed" };
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
const bindByKey = (_row, columnId, value) => ({ [columnId]: value });
|
|
13
|
+
const readByKey = (row, columnId) => row[columnId];
|
|
14
|
+
export function gridSave(save, options) {
|
|
15
|
+
const { getRowId } = options;
|
|
16
|
+
const values = options.values ?? bindByKey;
|
|
17
|
+
const read = options.read ?? readByKey;
|
|
18
|
+
const records = new Map();
|
|
19
|
+
let waitingCount = 0;
|
|
20
|
+
let closeOff = null;
|
|
21
|
+
const send = (change, settle) => {
|
|
22
|
+
const record = records.get(change.id) ?? { inflight: 0, waiting: [] };
|
|
23
|
+
records.set(change.id, record);
|
|
24
|
+
record.inflight += 1;
|
|
25
|
+
let answer;
|
|
26
|
+
try {
|
|
27
|
+
answer = Promise.resolve(save(change));
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
answer = Promise.reject(error);
|
|
31
|
+
}
|
|
32
|
+
void answer
|
|
33
|
+
.then((result) => settle({ ok: true, ...(result?.stored ? { stored: result.stored } : {}) }), (error) => settle({ ok: false, error }))
|
|
34
|
+
.finally(() => {
|
|
35
|
+
record.inflight -= 1;
|
|
36
|
+
if (record.inflight > 0)
|
|
37
|
+
return;
|
|
38
|
+
const next = record.waiting.shift();
|
|
39
|
+
if (next) {
|
|
40
|
+
leftWaiting(1);
|
|
41
|
+
send(next.change, next.settle);
|
|
42
|
+
}
|
|
43
|
+
else
|
|
44
|
+
records.delete(change.id);
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
const leftWaiting = (count) => {
|
|
48
|
+
waitingCount -= count;
|
|
49
|
+
if (waitingCount > 0)
|
|
50
|
+
return;
|
|
51
|
+
closeOff?.();
|
|
52
|
+
closeOff = null;
|
|
53
|
+
};
|
|
54
|
+
const sendAll = () => {
|
|
55
|
+
for (const record of records.values()) {
|
|
56
|
+
const waiting = record.waiting.splice(0);
|
|
57
|
+
leftWaiting(waiting.length);
|
|
58
|
+
for (const each of waiting)
|
|
59
|
+
send({ ...each.change, closing: true }, each.settle);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
const admit = (change) => new Promise((settle) => {
|
|
63
|
+
const record = records.get(change.id);
|
|
64
|
+
const closing = isPageClosing();
|
|
65
|
+
if (closing && closeOff)
|
|
66
|
+
sendAll();
|
|
67
|
+
if (!record || record.inflight === 0 || closing) {
|
|
68
|
+
send(closing ? { ...change, closing: true } : change, settle);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
record.waiting.push({ change, settle });
|
|
72
|
+
waitingCount += 1;
|
|
73
|
+
closeOff ??= onPageClose(sendAll);
|
|
74
|
+
});
|
|
75
|
+
const onCellsCommit = async (edits) => {
|
|
76
|
+
const outcomes = new Array(edits.length);
|
|
77
|
+
const groups = new Map();
|
|
78
|
+
edits.forEach((edit, index) => {
|
|
79
|
+
let id;
|
|
80
|
+
let cellValues;
|
|
81
|
+
let keys;
|
|
82
|
+
try {
|
|
83
|
+
id = getRowId(edit.row);
|
|
84
|
+
cellValues = values(edit.row, edit.columnId, edit.value, edit.context ?? { kind: "commit" });
|
|
85
|
+
keys = Object.keys(cellValues);
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
outcomes[index] = { status: "failed", error };
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const group = groups.get(id) ?? { row: edit.row, cells: [], values: {}, closing: false };
|
|
92
|
+
groups.set(id, group);
|
|
93
|
+
if (edit.context?.closing)
|
|
94
|
+
group.closing = true;
|
|
95
|
+
Object.assign(group.values, cellValues);
|
|
96
|
+
group.cells.push({ index, keys });
|
|
97
|
+
});
|
|
98
|
+
await Promise.all([...groups].map(async ([id, group]) => {
|
|
99
|
+
if (group.cells.length === 0)
|
|
100
|
+
return;
|
|
101
|
+
const answer = await admit({
|
|
102
|
+
id,
|
|
103
|
+
row: group.row,
|
|
104
|
+
values: group.values,
|
|
105
|
+
...(group.closing ? { closing: true } : {}),
|
|
106
|
+
});
|
|
107
|
+
for (const { index, keys } of group.cells) {
|
|
108
|
+
const edit = edits[index];
|
|
109
|
+
if (!answer.ok)
|
|
110
|
+
outcomes[index] = { status: "failed", error: answer.error };
|
|
111
|
+
else
|
|
112
|
+
outcomes[index] = keptOutcome(answer.stored, keys, () => read({ ...group.row, ...group.values, ...answer.stored }, edit.columnId));
|
|
113
|
+
}
|
|
114
|
+
}));
|
|
115
|
+
return outcomes;
|
|
116
|
+
};
|
|
117
|
+
const onCellCommit = async (row, columnId, value, context) => {
|
|
118
|
+
const [outcome] = await onCellsCommit([{ row, columnId, value, ...(context ? { context } : {}) }]);
|
|
119
|
+
return outcome;
|
|
120
|
+
};
|
|
121
|
+
return { onCellCommit, onCellsCommit };
|
|
122
|
+
}
|
|
@@ -46,6 +46,9 @@ export type GridSpreadsheetModel<Row> = {
|
|
|
46
46
|
/** The list's settings (the grid's `listConfig`): a column they lock takes no write from a
|
|
47
47
|
* clear, a paste or a fill, and is refused in the sentence an edit of one cell says. */
|
|
48
48
|
listConfig?: ListSettingsConfig;
|
|
49
|
+
/** The grid's `rowWritable`: a row it refuses takes no write from a clear, a paste or a fill, each of its cells
|
|
50
|
+
* refused as "Cell is read-only". Absent: every row writes as its columns say. */
|
|
51
|
+
rowWritable?: (row: Row) => boolean;
|
|
49
52
|
};
|
|
50
53
|
type GridPlannedEdit<Row> = GridEditCell<Row> & {
|
|
51
54
|
row: Row;
|