@macrostrat/data-sheet 3.1.0 → 4.0.0-dev2
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 +19 -0
- package/dist/actions/builtin.cjs +56 -0
- package/dist/actions/builtin.cjs.map +1 -0
- package/dist/actions/builtin.d.ts +15 -0
- package/dist/actions/builtin.js +56 -0
- package/dist/actions/builtin.js.map +1 -0
- package/dist/actions/clipboard.cjs +250 -0
- package/dist/actions/clipboard.cjs.map +1 -0
- package/dist/actions/clipboard.d.ts +42 -0
- package/dist/actions/clipboard.js +250 -0
- package/dist/actions/clipboard.js.map +1 -0
- package/dist/actions/filters.cjs +123 -0
- package/dist/actions/filters.cjs.map +1 -0
- package/dist/actions/filters.d.ts +6 -0
- package/dist/actions/filters.js +123 -0
- package/dist/actions/filters.js.map +1 -0
- package/dist/actions/index.d.ts +6 -0
- package/dist/actions/selection.cjs +194 -0
- package/dist/actions/selection.cjs.map +1 -0
- package/dist/actions/selection.d.ts +40 -0
- package/dist/actions/selection.js +192 -0
- package/dist/actions/selection.js.map +1 -0
- package/dist/actions/toolbar.cjs +171 -0
- package/dist/actions/toolbar.cjs.map +1 -0
- package/dist/actions/toolbar.d.ts +10 -0
- package/dist/actions/toolbar.js +171 -0
- package/dist/actions/toolbar.js.map +1 -0
- package/dist/actions/toolbar.module.sass.cjs +22 -0
- package/dist/actions/toolbar.module.sass.cjs.map +1 -0
- package/dist/actions/toolbar.module.sass.js +20 -0
- package/dist/actions/toolbar.module.sass.js.map +1 -0
- package/dist/actions/types.d.ts +143 -0
- package/dist/cell-renderer.cjs +44 -41
- package/dist/cell-renderer.cjs.map +1 -1
- package/dist/cell-renderer.d.ts +1 -6
- package/dist/cell-renderer.js +45 -42
- package/dist/cell-renderer.js.map +1 -1
- package/dist/components/actions.module.sass.cjs +3 -3
- package/dist/components/actions.module.sass.js +2 -2
- package/dist/components/colors.cjs +20 -16
- package/dist/components/colors.cjs.map +1 -1
- package/dist/components/colors.d.ts +3 -2
- package/dist/components/colors.js +20 -16
- package/dist/components/colors.js.map +1 -1
- package/dist/components/editor-popup.cjs +4 -3
- package/dist/components/editor-popup.cjs.map +1 -1
- package/dist/components/editor-popup.js +4 -3
- package/dist/components/editor-popup.js.map +1 -1
- package/dist/components/index.d.ts +1 -0
- package/dist/components/main.module.sass.cjs +12 -12
- package/dist/components/main.module.sass.js +11 -11
- package/dist/components/text-areas.cjs +23 -18
- package/dist/components/text-areas.cjs.map +1 -1
- package/dist/components/text-areas.d.ts +8 -4
- package/dist/components/text-areas.js +20 -13
- package/dist/components/text-areas.js.map +1 -1
- package/dist/components/view-info.cjs +18 -0
- package/dist/components/view-info.cjs.map +1 -0
- package/dist/components/view-info.d.ts +1 -0
- package/dist/components/view-info.js +18 -0
- package/dist/components/view-info.js.map +1 -0
- package/dist/core.cjs +184 -156
- package/dist/core.cjs.map +1 -1
- package/dist/core.d.ts +14 -1
- package/dist/core.js +187 -157
- package/dist/core.js.map +1 -1
- package/dist/data-sheet.css +181 -43
- package/dist/index.cjs +43 -13
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +43 -10
- package/dist/index.js.map +1 -1
- package/dist/main.module.sass.cjs +15 -14
- package/dist/main.module.sass.cjs.map +1 -1
- package/dist/main.module.sass.js +14 -13
- package/dist/main.module.sass.js.map +1 -1
- package/dist/notifications.cjs +19 -0
- package/dist/notifications.cjs.map +1 -0
- package/dist/notifications.d.ts +9 -0
- package/dist/notifications.js +19 -0
- package/dist/notifications.js.map +1 -0
- package/dist/postgrest-table/data-loaders.cjs +181 -104
- package/dist/postgrest-table/data-loaders.cjs.map +1 -1
- package/dist/postgrest-table/data-loaders.d.ts +52 -28
- package/dist/postgrest-table/data-loaders.js +181 -104
- package/dist/postgrest-table/data-loaders.js.map +1 -1
- package/dist/postgrest-table/index.cjs +205 -79
- package/dist/postgrest-table/index.cjs.map +1 -1
- package/dist/postgrest-table/index.d.ts +13 -16
- package/dist/postgrest-table/index.js +186 -58
- package/dist/postgrest-table/index.js.map +1 -1
- package/dist/postgrest-table/lazy-loader-table.cjs +18 -17
- package/dist/postgrest-table/lazy-loader-table.cjs.map +1 -1
- package/dist/postgrest-table/lazy-loader-table.d.ts +1 -1
- package/dist/postgrest-table/lazy-loader-table.js +17 -14
- package/dist/postgrest-table/lazy-loader-table.js.map +1 -1
- package/dist/postgrest-table/loading-utils.cjs +16 -0
- package/dist/postgrest-table/loading-utils.cjs.map +1 -0
- package/dist/postgrest-table/loading-utils.d.ts +13 -0
- package/dist/postgrest-table/loading-utils.js +16 -0
- package/dist/postgrest-table/loading-utils.js.map +1 -0
- package/dist/postgrest-table/main.module.sass.cjs +18 -13
- package/dist/postgrest-table/main.module.sass.cjs.map +1 -1
- package/dist/postgrest-table/main.module.sass.js +18 -13
- package/dist/postgrest-table/main.module.sass.js.map +1 -1
- package/dist/provider.cjs +58 -26
- package/dist/provider.cjs.map +1 -1
- package/dist/provider.js +57 -23
- package/dist/provider.js.map +1 -1
- package/dist/renderers/column-header-cell-postgrest.cjs +216 -0
- package/dist/renderers/column-header-cell-postgrest.cjs.map +1 -0
- package/dist/renderers/column-header-cell-postgrest.d.ts +13 -0
- package/dist/renderers/column-header-cell-postgrest.js +216 -0
- package/dist/renderers/column-header-cell-postgrest.js.map +1 -0
- package/dist/renderers/column-header-cell-postgrest.module.sass.cjs +23 -0
- package/dist/renderers/column-header-cell-postgrest.module.sass.cjs.map +1 -0
- package/dist/renderers/column-header-cell-postgrest.module.sass.js +22 -0
- package/dist/renderers/column-header-cell-postgrest.module.sass.js.map +1 -0
- package/dist/renderers/column-header-cell.cjs +103 -0
- package/dist/renderers/column-header-cell.cjs.map +1 -0
- package/dist/renderers/column-header-cell.d.ts +30 -0
- package/dist/renderers/column-header-cell.js +101 -0
- package/dist/renderers/column-header-cell.js.map +1 -0
- package/dist/renderers/index.d.ts +2 -0
- package/dist/table-updates/data-parameter.d.ts +14 -0
- package/dist/table-updates/filter.d.ts +16 -0
- package/dist/table-updates/index.d.ts +19 -0
- package/dist/types.cjs +9 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.ts +58 -4
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/column-spec.cjs +9 -1
- package/dist/utils/column-spec.cjs.map +1 -1
- package/dist/utils/column-spec.d.ts +21 -0
- package/dist/utils/column-spec.js +9 -1
- package/dist/utils/column-spec.js.map +1 -1
- package/dist/utils/key-handling.cjs +141 -52
- package/dist/utils/key-handling.cjs.map +1 -1
- package/dist/utils/key-handling.d.ts +2 -3
- package/dist/utils/key-handling.js +142 -53
- package/dist/utils/key-handling.js.map +1 -1
- package/dist/zustand-store.cjs +216 -51
- package/dist/zustand-store.cjs.map +1 -1
- package/dist/zustand-store.js +197 -32
- package/dist/zustand-store.js.map +1 -1
- package/package.json +5 -5
- package/src/actions/builtin.ts +65 -0
- package/src/actions/clipboard.ts +350 -0
- package/src/actions/filters.ts +150 -0
- package/src/actions/index.ts +6 -0
- package/src/actions/selection.ts +271 -0
- package/src/actions/toolbar.module.sass +30 -0
- package/src/actions/toolbar.ts +212 -0
- package/src/actions/types.ts +159 -0
- package/src/cell-renderer.ts +55 -51
- package/src/components/colors.ts +22 -19
- package/src/components/editor-popup.ts +3 -2
- package/src/components/index.ts +1 -0
- package/src/components/main.module.sass +1 -0
- package/src/components/text-areas.ts +34 -19
- package/src/components/view-info.ts +17 -0
- package/src/core.ts +243 -170
- package/src/index.ts +1 -0
- package/src/main.module.sass +14 -2
- package/src/notifications.ts +22 -0
- package/src/postgrest-table/data-loaders.ts +300 -201
- package/src/postgrest-table/index.ts +258 -84
- package/src/postgrest-table/lazy-loader-table.ts +18 -17
- package/src/postgrest-table/loading-utils.ts +49 -0
- package/src/postgrest-table/main.module.sass +16 -0
- package/src/provider.ts +63 -20
- package/src/renderers/column-header-cell-postgrest.module.sass +71 -0
- package/src/renderers/column-header-cell-postgrest.ts +282 -0
- package/src/renderers/column-header-cell.ts +388 -0
- package/src/renderers/index.ts +2 -0
- package/src/table-updates/data-parameter.ts +44 -0
- package/src/table-updates/filter.ts +124 -0
- package/src/table-updates/index.ts +239 -0
- package/src/types.ts +58 -3
- package/src/utils/column-spec.ts +47 -0
- package/src/utils/key-handling.ts +181 -72
- package/src/zustand-store.ts +241 -36
- package/dist/provider.d.ts +0 -15
|
@@ -1,43 +1,120 @@
|
|
|
1
|
-
import { storeAPIAtom, storeAtom } from "../provider.js";
|
|
1
|
+
import { storeAPIAtom, tableActionsAtom, storeAtom } from "../provider.js";
|
|
2
2
|
import { atom } from "jotai";
|
|
3
3
|
import { singleFocusedCell } from "../zustand-store.js";
|
|
4
|
-
|
|
4
|
+
import { toasterAtom } from "../notifications.js";
|
|
5
|
+
import { runActionWrapper } from "../actions/toolbar.js";
|
|
6
|
+
const isEditableAtom = atom((get) => {
|
|
7
|
+
const store = get(storeAtom);
|
|
8
|
+
return store?.editable ?? false;
|
|
9
|
+
});
|
|
10
|
+
atom((get) => {
|
|
11
|
+
const store = get(storeAtom);
|
|
12
|
+
const selection = store?.selection ?? [];
|
|
13
|
+
return selection.length > 0;
|
|
14
|
+
});
|
|
15
|
+
const tableHotkeysAtom = atom((get) => {
|
|
5
16
|
const store = get(storeAPIAtom);
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
17
|
+
const toaster = get(toasterAtom);
|
|
18
|
+
const editable = get(isEditableAtom);
|
|
19
|
+
const tableActions = get(tableActionsAtom);
|
|
20
|
+
if (store == null) return [];
|
|
21
|
+
const keyHandler = (actionRunner) => {
|
|
22
|
+
return (e) => {
|
|
23
|
+
const state = store.getState();
|
|
24
|
+
const setState = store.setState;
|
|
25
|
+
try {
|
|
26
|
+
const res = actionRunner(e, state, setState);
|
|
27
|
+
if (res instanceof Promise) {
|
|
28
|
+
res.then(() => {
|
|
29
|
+
}).catch((err) => {
|
|
30
|
+
console.error(err);
|
|
31
|
+
handleError(toaster, err);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
} catch (err) {
|
|
35
|
+
handleError(toaster, err);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
9
38
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
e.stopPropagation();
|
|
16
|
-
}
|
|
17
|
-
if (e.key === "Backspace" || e.key === "Delete") {
|
|
18
|
-
state.clearSelection();
|
|
19
|
-
e.preventDefault();
|
|
20
|
-
}
|
|
21
|
-
if (e.key in directionMap) {
|
|
22
|
-
const direction = directionMap[e.key];
|
|
23
|
-
state.moveFocusedCell(direction);
|
|
24
|
-
e.preventDefault();
|
|
39
|
+
function handleError(toaster2, err) {
|
|
40
|
+
toaster2.show({
|
|
41
|
+
message: `${err}`,
|
|
42
|
+
intent: "danger"
|
|
43
|
+
});
|
|
25
44
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
45
|
+
const directionCombos = ["up", "down", "left", "right"].map((dir) => {
|
|
46
|
+
return {
|
|
47
|
+
combo: dir,
|
|
48
|
+
group: "Navigation",
|
|
49
|
+
label: "Move focus " + dir,
|
|
50
|
+
preventDefault: true,
|
|
51
|
+
onKeyDown: keyHandler((e, state) => {
|
|
52
|
+
state.moveFocusedCell(dir);
|
|
53
|
+
})
|
|
54
|
+
};
|
|
55
|
+
});
|
|
56
|
+
const actionsCombos = tableActions.map((action) => {
|
|
57
|
+
if (action.hotkey == null) return null;
|
|
58
|
+
return {
|
|
59
|
+
combo: action.hotkey,
|
|
60
|
+
label: action.name,
|
|
61
|
+
group: action.group ?? "Actions",
|
|
62
|
+
preventDefault: true,
|
|
63
|
+
disabled: action.requiresEditable !== false && !editable,
|
|
64
|
+
onKeyDown(event) {
|
|
65
|
+
const state = store.getState();
|
|
66
|
+
const setState = store.setState;
|
|
67
|
+
runActionWrapper(action, state, setState, toaster);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
}).filter(Boolean);
|
|
71
|
+
return [
|
|
72
|
+
{
|
|
73
|
+
combo: "esc",
|
|
74
|
+
label: "Clear selection",
|
|
75
|
+
group: "Selection",
|
|
76
|
+
allowInInput: true,
|
|
77
|
+
onKeyDown: keyHandler((e, state) => {
|
|
78
|
+
if (state.selection.length == 0) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
state.setSelection([]);
|
|
82
|
+
e.preventDefault();
|
|
83
|
+
e.stopPropagation();
|
|
84
|
+
})
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
combo: "backspace",
|
|
88
|
+
label: "Clear selection",
|
|
89
|
+
group: "Selection",
|
|
90
|
+
allowInInput: true,
|
|
91
|
+
disabled: !editable,
|
|
92
|
+
onKeyDown: keyHandler((e, state) => {
|
|
93
|
+
state.clearSelection();
|
|
94
|
+
})
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
combo: "tab",
|
|
98
|
+
label: "Move focus right (or left with shift)",
|
|
99
|
+
group: "Navigation",
|
|
100
|
+
allowInInput: true,
|
|
101
|
+
onKeyDown: keyHandler((e, state) => {
|
|
102
|
+
state.moveFocusedCell(e.shiftKey ? "left" : "right");
|
|
103
|
+
})
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
combo: "enter",
|
|
107
|
+
label: "Move focus down",
|
|
108
|
+
group: "Navigation",
|
|
109
|
+
allowInInput: true,
|
|
110
|
+
onKeyDown: keyHandler((e, state) => {
|
|
111
|
+
state.moveFocusedCell("down");
|
|
112
|
+
})
|
|
113
|
+
},
|
|
114
|
+
...directionCombos,
|
|
115
|
+
...actionsCombos
|
|
116
|
+
];
|
|
117
|
+
});
|
|
41
118
|
const selectionAtom = atom((get) => {
|
|
42
119
|
const store = get(storeAtom);
|
|
43
120
|
return store.selection;
|
|
@@ -53,6 +130,7 @@ const editorKeyHandlerAtom = atom((get) => {
|
|
|
53
130
|
};
|
|
54
131
|
});
|
|
55
132
|
function editorKeyHandler(e, isSingleCellSelection) {
|
|
133
|
+
const target = e.target;
|
|
56
134
|
let shouldCaptureEventForInlineEditing = true;
|
|
57
135
|
if (e.key == "Enter") {
|
|
58
136
|
shouldCaptureEventForInlineEditing = e.shiftKey;
|
|
@@ -64,16 +142,38 @@ function editorKeyHandler(e, isSingleCellSelection) {
|
|
|
64
142
|
shouldCaptureEventForInlineEditing = isSingleCellSelection && !e.shiftKey;
|
|
65
143
|
}
|
|
66
144
|
if (!shouldCaptureEventForInlineEditing) {
|
|
67
|
-
|
|
145
|
+
target.blur();
|
|
68
146
|
e.preventDefault();
|
|
69
147
|
return;
|
|
70
148
|
}
|
|
71
|
-
const shouldPropagate =
|
|
149
|
+
const shouldPropagate = shouldPropagateKeystroke(e);
|
|
72
150
|
if (!shouldPropagate) {
|
|
73
151
|
e.stopPropagation();
|
|
74
152
|
} else {
|
|
75
|
-
e.
|
|
153
|
+
e.preventDefault();
|
|
154
|
+
target.blur();
|
|
155
|
+
target.parentNode?.dispatchEvent(new KeyboardEvent("keydown", e));
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
function shouldPropagateKeystroke(evt) {
|
|
159
|
+
if (evt.target.selectionStart == evt.target.selectionEnd) {
|
|
160
|
+
if (evt.key === "c" || evt.key === "v" || evt.key === "x") {
|
|
161
|
+
if (evt.ctrlKey || evt.metaKey) {
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (evt.key === "ArrowUp" || evt.key === "ArrowDown") {
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
const target = evt.target;
|
|
170
|
+
if ((evt.key === "ArrowLeft" || evt.key === "ArrowUp" || evt.key === "ArrowDown") && target.selectionStart === 0) {
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
if ((evt.key === "ArrowRight" || evt.key === "ArrowUp" || evt.key === "Tab" || evt.key === "Enter" || evt.key === "ArrowDown") && target.selectionStart === evt.target.value.length) {
|
|
174
|
+
return true;
|
|
76
175
|
}
|
|
176
|
+
return false;
|
|
77
177
|
}
|
|
78
178
|
function multiLineTextKeyHandler(evt) {
|
|
79
179
|
if (evt.key === "Enter") {
|
|
@@ -84,27 +184,16 @@ function multiLineTextKeyHandler(evt) {
|
|
|
84
184
|
evt.preventDefault();
|
|
85
185
|
return;
|
|
86
186
|
}
|
|
87
|
-
const shouldPropagate =
|
|
187
|
+
const shouldPropagate = shouldPropagateKeystroke(evt);
|
|
88
188
|
if (!shouldPropagate) {
|
|
89
189
|
evt.stopPropagation();
|
|
190
|
+
} else {
|
|
191
|
+
evt.target.blur();
|
|
90
192
|
}
|
|
91
193
|
}
|
|
92
|
-
function handleSpecialKeys(evt) {
|
|
93
|
-
const target = evt.target;
|
|
94
|
-
if ((evt.key === "ArrowLeft" || evt.key === "ArrowUp" || evt.key === "ArrowDown") && target.selectionStart === 0) {
|
|
95
|
-
target.blur();
|
|
96
|
-
return true;
|
|
97
|
-
}
|
|
98
|
-
if ((evt.key === "ArrowRight" || evt.key === "ArrowUp" || evt.key === "Tab" || evt.key === "Enter" || evt.key === "ArrowDown") && target.selectionStart === evt.target.value.length) {
|
|
99
|
-
target.blur();
|
|
100
|
-
return true;
|
|
101
|
-
}
|
|
102
|
-
return false;
|
|
103
|
-
}
|
|
104
194
|
export {
|
|
105
195
|
editorKeyHandlerAtom,
|
|
106
196
|
multiLineTextKeyHandler,
|
|
107
|
-
|
|
108
|
-
tableKeyHandlerAtom
|
|
197
|
+
tableHotkeysAtom
|
|
109
198
|
};
|
|
110
199
|
//# sourceMappingURL=key-handling.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"key-handling.js","sources":["../../src/utils/key-handling.ts"],"sourcesContent":["import type { KeyboardEvent } from \"react\";\nimport { storeAPIAtom, storeAtom } from \"../provider\";\nimport type { DataSheetStoreMain } from \"../types\";\nimport { atom } from \"jotai\";\nimport { singleFocusedCell } from \"../zustand-store.ts\";\n\nexport const tableKeyHandlerAtom = atom((get) => {\n const store = get(storeAPIAtom);\n return (e: KeyboardEvent) => {\n // Kind of a ridiculous hack to get the store from the atom.\n const state = store.getState();\n tableKeyHandler(e, state);\n };\n});\n\nexport function tableKeyHandler<T>(\n e: KeyboardEvent,\n state: DataSheetStoreMain<T>,\n) {\n // General key event\n if (e.key === \"Escape\") {\n // Clear selection on Escape\n state.setSelection([]);\n e.preventDefault();\n e.stopPropagation();\n }\n // Clear selection on Backspace or Delete\n if (e.key === \"Backspace\" || e.key === \"Delete\") {\n // Clear selection on Backspace or Delete\n state.clearSelection();\n e.preventDefault();\n }\n // Handle arrow keys for navigation\n if (e.key in directionMap) {\n // Handle arrow key navigation\n const direction = directionMap[e.key];\n state.moveFocusedCell(direction);\n // Prevent default scrolling behavior\n e.preventDefault();\n }\n if (e.key === \"Tab\") {\n state.moveFocusedCell(e.shiftKey ? \"left\" : \"right\");\n e.preventDefault();\n }\n if (e.key === \"Enter\") {\n state.moveFocusedCell(\"down\");\n e.preventDefault();\n }\n}\n\nconst directionMap = {\n ArrowUp: \"up\",\n ArrowDown: \"down\",\n ArrowLeft: \"left\",\n ArrowRight: \"right\",\n};\n\nconst selectionAtom = atom((get) => {\n const store = get(storeAtom);\n return store.selection;\n});\n\nconst isSingleCellSelectionAtom = atom((get) => {\n const selection = get(selectionAtom);\n return singleFocusedCell(selection) != null;\n});\n\nexport const editorKeyHandlerAtom = atom((get) => {\n return (e: KeyboardEvent) => {\n const isSingleCellSelection = get(isSingleCellSelectionAtom);\n editorKeyHandler(e, isSingleCellSelection);\n };\n});\n\nfunction editorKeyHandler(e: KeyboardEvent, isSingleCellSelection: boolean) {\n /** Get a key handler for inline cell editing */\n\n // if (e.key == \"Enter\" || e.key == \"Tab\") {\n // e.target.blur();\n // return;\n // }\n\n let shouldCaptureEventForInlineEditing = true;\n\n if (e.key == \"Enter\") {\n shouldCaptureEventForInlineEditing = e.shiftKey;\n }\n\n if (e.key == \"Escape\" || e.key == \"Tab\") {\n shouldCaptureEventForInlineEditing = false;\n }\n\n if (e.key == \"Backspace\" || e.key == \"Delete\") {\n /** Clear selection on Backspace or Delete on a multi-cell selection\n * (if shift is held, we allow deletion in a single cell)\n */\n shouldCaptureEventForInlineEditing = isSingleCellSelection && !e.shiftKey;\n }\n\n if (!shouldCaptureEventForInlineEditing) {\n e.target.blur();\n e.preventDefault();\n return;\n }\n\n const shouldPropagate = handleSpecialKeys(e);\n if (!shouldPropagate) {\n e.stopPropagation();\n } else {\n e.target.blur();\n //e.target.parentNode.dispatchEvent(new KeyboardEvent(\"keydown\", e));\n }\n}\n\nexport function multiLineTextKeyHandler(evt: InputEvent) {\n if (evt.key === \"Enter\") {\n //evt.preventDefault();\n evt.stopPropagation();\n return;\n }\n\n if (evt.key === \"Escape\") {\n evt.preventDefault();\n return;\n }\n\n const shouldPropagate = handleSpecialKeys(evt);\n if (!shouldPropagate) {\n evt.stopPropagation();\n }\n}\n\nfunction handleSpecialKeys(evt: KeyboardEvent): boolean {\n const target = evt.target;\n if (\n (evt.key === \"ArrowLeft\" ||\n evt.key === \"ArrowUp\" ||\n evt.key === \"ArrowDown\") &&\n target.selectionStart === 0\n ) {\n target.blur();\n return true;\n }\n\n if (\n (evt.key === \"ArrowRight\" ||\n evt.key === \"ArrowUp\" ||\n evt.key === \"Tab\" ||\n evt.key === \"Enter\" ||\n evt.key === \"ArrowDown\") &&\n target.selectionStart === evt.target.value.length\n ) {\n target.blur();\n return true;\n }\n\n return false;\n}\n"],"names":[],"mappings":";;;AAMO,MAAM,sBAAsB,KAAK,CAAC,QAAQ;AAC/C,QAAM,QAAQ,IAAI,YAAY;AAC9B,SAAO,CAAC,MAAqB;AAE3B,UAAM,QAAQ,MAAM,SAAA;AACpB,oBAAgB,GAAG,KAAK;AAAA,EAC1B;AACF,CAAC;AAEM,SAAS,gB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,OAAO,UAAU;AAI7C,yCAAqC,yBAAyB,CAAC,EAAE;AAAA,EACnE;AAEA,MAAI,CAAC,oCAAoC;AACvC,MAAE,OAAO,KAAA;AACT,MAAE,eAAA;AACF;AAAA,EACF;AAEA,QAAM,kBAAkB,kBAAkB,CAAC;AAC3C,MAAI,CAAC,iBAAiB;AACpB,MAAE,gBAAA;AAAA,EACJ,OAAO;AACL,MAAE,OAAO,KAAA;AAAA,EAEX;AACF;AAEO,SAAS,wBAAwB,KAAiB;AACvD,MAAI,IAAI,QAAQ,SAAS;AAEvB,QAAI,gBAAA;AACJ;AAAA,EACF;AAEA,MAAI,IAAI,QAAQ,UAAU;AACxB,QAAI,eAAA;AACJ;AAAA,EACF;AAEA,QAAM,kBAAkB,kBAAkB,GAAG;AAC7C,MAAI,CAAC,iBAAiB;AACpB,QAAI,gBAAA;AAAA,EACN;AACF;AAEA,SAAS,kBAAkB,KAA6B;AACtD,QAAM,SAAS,IAAI;AACnB,OACG,IAAI,QAAQ,eACX,IAAI,QAAQ,aACZ,IAAI,QAAQ,gBACd,OAAO,mBAAmB,GAC1B;AACA,WAAO,KAAA;AACP,WAAO;AAAA,EACT;AAEA,OACG,IAAI,QAAQ,gBACX,IAAI,QAAQ,aACZ,IAAI,QAAQ,SACZ,IAAI,QAAQ,WACZ,IAAI,QAAQ,gBACd,OAAO,mBAAmB,IAAI,OAAO,MAAM,QAC3C;AACA,WAAO,KAAA;AACP,WAAO;AAAA,EACT;AAEA,SAAO;AACT;"}
|
|
1
|
+
{"version":3,"file":"key-handling.js","sources":["../../src/utils/key-handling.ts"],"sourcesContent":["import type { KeyboardEvent } from \"react\";\nimport { storeAPIAtom, storeAtom, tableActionsAtom } from \"../provider\";\nimport type { DataSheetStoreMain } from \"../types\";\nimport { atom } from \"jotai\";\nimport { singleFocusedCell } from \"../zustand-store.ts\";\nimport { buildActionContext, runActionWrapper } from \"../actions\";\nimport { HotkeyConfig } from \"@blueprintjs/core\";\nimport { toasterAtom } from \"../notifications.ts\";\n\ntype HotkeyActionRunner = (\n e: KeyboardEvent,\n state: DataSheetStoreMain<any>,\n setState: (state: Partial<DataSheetStoreMain<any>>) => void,\n) => void | Promise<void>;\n\nconst isEditableAtom = atom((get) => {\n const store = get(storeAtom);\n return store?.editable ?? false;\n});\n\nconst hasSelectionAtom = atom((get) => {\n const store = get(storeAtom);\n const selection = store?.selection ?? [];\n return selection.length > 0;\n});\n\nexport const tableHotkeysAtom = atom<HotkeyConfig[]>((get) => {\n const store = get(storeAPIAtom);\n const toaster = get(toasterAtom);\n const editable = get(isEditableAtom);\n\n /** Set up hotkeys for table actions */\n const tableActions = get(tableActionsAtom);\n\n if (store == null) return [];\n const keyHandler = (\n actionRunner: HotkeyActionRunner,\n ): HotkeyConfig[\"onKeyDown\"] => {\n return (e) => {\n // Kind of a ridiculous hack to get the store from the atom.\n const state = store.getState();\n const setState = store.setState;\n try {\n const res = actionRunner(e, state, setState);\n if (res instanceof Promise) {\n res\n .then(() => {})\n .catch((err) => {\n console.error(err);\n handleError(toaster, err);\n });\n }\n } catch (err) {\n handleError(toaster, err);\n }\n };\n };\n\n function handleError(toaster, err) {\n toaster.show({\n message: `${err}`,\n intent: \"danger\",\n });\n }\n\n const directionCombos = [\"up\", \"down\", \"left\", \"right\"].map((dir) => {\n return {\n combo: dir,\n group: \"Navigation\",\n label: \"Move focus \" + dir,\n preventDefault: true,\n onKeyDown: keyHandler((e, state) => {\n state.moveFocusedCell(dir);\n }),\n };\n });\n\n /** TODO: merge a bit more with the actions config */\n\n const actionsCombos = tableActions\n .map((action) => {\n if (action.hotkey == null) return null;\n return {\n combo: action.hotkey,\n label: action.name,\n group: action.group ?? \"Actions\",\n preventDefault: true,\n disabled: action.requiresEditable !== false && !editable,\n onKeyDown(event: KeyboardEvent<any>): any {\n const state = store.getState();\n const setState = store.setState;\n runActionWrapper(action, state, setState, toaster);\n },\n } as any as HotkeyConfig;\n })\n .filter(Boolean) as HotkeyConfig[];\n\n return [\n {\n combo: \"esc\",\n label: \"Clear selection\",\n group: \"Selection\",\n allowInInput: true,\n onKeyDown: keyHandler((e, state) => {\n if (state.selection.length == 0) {\n // Focus goes back to parent\n return;\n }\n state.setSelection([]);\n e.preventDefault();\n e.stopPropagation();\n }),\n },\n {\n combo: \"backspace\",\n label: \"Clear selection\",\n group: \"Selection\",\n allowInInput: true,\n disabled: !editable,\n onKeyDown: keyHandler((e, state) => {\n state.clearSelection();\n }),\n },\n {\n combo: \"tab\",\n label: \"Move focus right (or left with shift)\",\n group: \"Navigation\",\n allowInInput: true,\n onKeyDown: keyHandler((e, state) => {\n state.moveFocusedCell(e.shiftKey ? \"left\" : \"right\");\n }),\n },\n {\n combo: \"enter\",\n label: \"Move focus down\",\n group: \"Navigation\",\n allowInInput: true,\n onKeyDown: keyHandler((e, state) => {\n state.moveFocusedCell(\"down\");\n }),\n },\n ...directionCombos,\n ...actionsCombos,\n ];\n});\n\nconst selectionAtom = atom((get) => {\n const store = get(storeAtom);\n return store.selection;\n});\n\nconst isSingleCellSelectionAtom = atom((get) => {\n const selection = get(selectionAtom);\n return singleFocusedCell(selection) != null;\n});\n\nexport const editorKeyHandlerAtom = atom((get) => {\n return (e: KeyboardEvent) => {\n const isSingleCellSelection = get(isSingleCellSelectionAtom);\n editorKeyHandler(e, isSingleCellSelection);\n };\n});\n\nfunction editorKeyHandler(\n e: KeyboardEvent<HTMLInputElement>,\n isSingleCellSelection: boolean,\n) {\n /** Get a key handler for inline cell editing */\n\n const target = e.target as HTMLInputElement;\n\n let shouldCaptureEventForInlineEditing = true;\n\n if (e.key == \"Enter\") {\n shouldCaptureEventForInlineEditing = e.shiftKey;\n }\n\n if (e.key == \"Escape\" || e.key == \"Tab\") {\n shouldCaptureEventForInlineEditing = false;\n }\n\n if (e.key == \"Backspace\" || e.key == \"Delete\") {\n /** Clear selection on Backspace or Delete on a multi-cell selection\n * (if shift is held, we allow deletion in a single cell)\n */\n shouldCaptureEventForInlineEditing = isSingleCellSelection && !e.shiftKey;\n }\n\n if (!shouldCaptureEventForInlineEditing) {\n target.blur();\n e.preventDefault();\n return;\n }\n\n const shouldPropagate = shouldPropagateKeystroke(e);\n if (!shouldPropagate) {\n e.stopPropagation();\n //e.preventDefault();\n } else {\n // we blur the editor and dispatch a keydown event on the parent to propagate the event to the table\n e.preventDefault();\n target.blur();\n //console.log(tableElement);\n //tableElement.focus();\n //target.parentNode?.dispatchEvent(e);\n target.parentNode?.dispatchEvent(new KeyboardEvent(\"keydown\", e));\n }\n}\n\nfunction shouldPropagateKeystroke(evt: KeyboardEvent): boolean {\n if (evt.target.selectionStart == evt.target.selectionEnd) {\n // We don't have anything selected, so we can propagate the event potentially\n // Propagate copy and paste events even if there is no selection, since they might want to copy/paste an entire row/column based on the focused cell.\n if (evt.key === \"c\" || evt.key === \"v\" || evt.key === \"x\") {\n if (evt.ctrlKey || evt.metaKey) {\n return true;\n }\n }\n }\n\n if (evt.key === \"ArrowUp\" || evt.key === \"ArrowDown\") {\n return true;\n }\n\n const target = evt.target;\n if (\n (evt.key === \"ArrowLeft\" ||\n evt.key === \"ArrowUp\" ||\n evt.key === \"ArrowDown\") &&\n target.selectionStart === 0\n ) {\n return true;\n }\n\n if (\n (evt.key === \"ArrowRight\" ||\n evt.key === \"ArrowUp\" ||\n evt.key === \"Tab\" ||\n evt.key === \"Enter\" ||\n evt.key === \"ArrowDown\") &&\n target.selectionStart === evt.target.value.length\n ) {\n return true;\n }\n\n return false;\n}\n\nexport function multiLineTextKeyHandler(evt: InputEvent) {\n if (evt.key === \"Enter\") {\n //evt.preventDefault();\n evt.stopPropagation();\n return;\n }\n\n if (evt.key === \"Escape\") {\n evt.preventDefault();\n return;\n }\n\n const shouldPropagate = shouldPropagateKeystroke(evt);\n if (!shouldPropagate) {\n evt.stopPropagation();\n } else {\n evt.target.blur();\n }\n}\n"],"names":["toaster"],"mappings":";;;;;AAeA,MAAM,iBAAiB,KAAK,CAAC,QAAQ;AACnC,QAAM,QAAQ,IAAI,SAAS;AAC3B,SA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AAAA,EACF;AAEA,MAAI,IAAI,QAAQ,aAAa,IAAI,QAAQ,aAAa;AACpD,WAAO;AAAA,EACT;AAEA,QAAM,SAAS,IAAI;AACnB,OACG,IAAI,QAAQ,eACX,IAAI,QAAQ,aACZ,IAAI,QAAQ,gBACd,OAAO,mBAAmB,GAC1B;AACA,WAAO;AAAA,EACT;AAEA,OACG,IAAI,QAAQ,gBACX,IAAI,QAAQ,aACZ,IAAI,QAAQ,SACZ,IAAI,QAAQ,WACZ,IAAI,QAAQ,gBACd,OAAO,mBAAmB,IAAI,OAAO,MAAM,QAC3C;AACA,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAEO,SAAS,wBAAwB,KAAiB;AACvD,MAAI,IAAI,QAAQ,SAAS;AAEvB,QAAI,gBAAA;AACJ;AAAA,EACF;AAEA,MAAI,IAAI,QAAQ,UAAU;AACxB,QAAI,eAAA;AACJ;AAAA,EACF;AAEA,QAAM,kBAAkB,yBAAyB,GAAG;AACpD,MAAI,CAAC,iBAAiB;AACpB,QAAI,gBAAA;AAAA,EACN,OAAO;AACL,QAAI,OAAO,KAAA;AAAA,EACb;AACF;"}
|
package/dist/zustand-store.cjs
CHANGED
|
@@ -1,28 +1,35 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const types = require("./types.cjs");
|
|
4
|
+
const table = require("@blueprintjs/table");
|
|
3
5
|
const index = require("./utils/index.cjs");
|
|
4
6
|
const update = require("immutability-helper");
|
|
7
|
+
const selection = require("./actions/selection.cjs");
|
|
5
8
|
const _interopDefault = (e) => e && e.__esModule ? e : { default: e };
|
|
6
9
|
const update__default = /* @__PURE__ */ _interopDefault(update);
|
|
7
10
|
function createZustandStore(set, get) {
|
|
8
11
|
return {
|
|
12
|
+
/** All the data in the table */
|
|
9
13
|
data: [],
|
|
14
|
+
updatedData: [],
|
|
15
|
+
rowStatus: [],
|
|
10
16
|
columnSpec: [],
|
|
11
17
|
defaultColumnWidth: 150,
|
|
12
18
|
editable: false,
|
|
13
|
-
deletedRows: /* @__PURE__ */ new Set(),
|
|
14
19
|
selection: [],
|
|
15
20
|
fillValueBaseCell: null,
|
|
16
|
-
updatedData: [],
|
|
17
21
|
focusedCell: null,
|
|
18
22
|
topLeftCell: null,
|
|
19
23
|
initialized: false,
|
|
20
24
|
tableRef: null,
|
|
21
|
-
visibleCellsRef: null,
|
|
22
25
|
// This is a placeholder
|
|
23
26
|
enableColumnReordering: false,
|
|
24
|
-
|
|
25
|
-
|
|
27
|
+
activeFilters: /* @__PURE__ */ new Map(),
|
|
28
|
+
columnSorts: [],
|
|
29
|
+
clipboardProxy: null,
|
|
30
|
+
filteredRowIndices: null,
|
|
31
|
+
setSelection(selection2) {
|
|
32
|
+
set(updateSelection(selection2));
|
|
26
33
|
},
|
|
27
34
|
columnWidthsIndex: /* @__PURE__ */ new Map(),
|
|
28
35
|
moveFocusedCell(direction) {
|
|
@@ -53,14 +60,17 @@ function createZustandStore(set, get) {
|
|
|
53
60
|
},
|
|
54
61
|
addRow(row = {}) {
|
|
55
62
|
set((state) => {
|
|
56
|
-
const { updatedData, data, selection } = state;
|
|
57
|
-
const lastRowIndex = getLastRowIndex(
|
|
58
|
-
const
|
|
59
|
-
$splice: [[lastRowIndex + 1, 0, row]]
|
|
60
|
-
};
|
|
61
|
-
console.log(spec);
|
|
63
|
+
const { updatedData, data, selection: selection2, rowStatus } = state;
|
|
64
|
+
const lastRowIndex = getLastRowIndex(selection2) ?? Math.max(data.length, updatedData.length) - 1;
|
|
65
|
+
const newIndex = lastRowIndex + 1;
|
|
62
66
|
return {
|
|
63
|
-
|
|
67
|
+
updateData: insertItemAtIndex(updatedData, newIndex, row),
|
|
68
|
+
data: insertItemAtIndex(data, newIndex, row),
|
|
69
|
+
rowStatus: insertItemAtIndex(
|
|
70
|
+
rowStatus,
|
|
71
|
+
newIndex,
|
|
72
|
+
types.TableElementStatus.ADDED
|
|
73
|
+
)
|
|
64
74
|
};
|
|
65
75
|
});
|
|
66
76
|
},
|
|
@@ -75,31 +85,47 @@ function createZustandStore(set, get) {
|
|
|
75
85
|
},
|
|
76
86
|
deleteSelectedRows() {
|
|
77
87
|
set((state) => {
|
|
78
|
-
const { selection, deletedRows } = state;
|
|
79
|
-
const rowIndices = getRowIndices(
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
|
|
88
|
+
const { selection: selection2, deletedRows } = state;
|
|
89
|
+
const rowIndices = getRowIndices(selection2);
|
|
90
|
+
const { addedRows } = state;
|
|
91
|
+
const rowsNewlyDeleted = new Set(rowIndices);
|
|
92
|
+
const rowsToRemoveFromDataArrays = new Set(
|
|
93
|
+
rowIndices.filter(
|
|
94
|
+
(d) => state.rowStatus[d] == types.TableElementStatus.ADDED
|
|
95
|
+
)
|
|
96
|
+
);
|
|
97
|
+
const rowsToMarkAsDeleted = rowsNewlyDeleted.difference(
|
|
98
|
+
rowsToRemoveFromDataArrays
|
|
99
|
+
);
|
|
100
|
+
let data = state.data;
|
|
101
|
+
let updatedData = state.updatedData;
|
|
102
|
+
let rowStatus = state.rowStatus;
|
|
103
|
+
if (rowsToMarkAsDeleted.size > 0) {
|
|
104
|
+
let spec = {};
|
|
105
|
+
for (const ix of rowsToMarkAsDeleted) {
|
|
106
|
+
spec[ix] = { $set: types.TableElementStatus.DELETED };
|
|
107
|
+
}
|
|
108
|
+
rowStatus = update__default.default(rowStatus, spec);
|
|
109
|
+
}
|
|
110
|
+
if (rowsToRemoveFromDataArrays.size > 0) {
|
|
111
|
+
const filterFunc = (_, i) => !rowsToRemoveFromDataArrays.has(i);
|
|
112
|
+
data = data.filter(filterFunc);
|
|
113
|
+
updatedData = updatedData.filter(filterFunc);
|
|
114
|
+
rowStatus = rowStatus.filter(filterFunc);
|
|
83
115
|
}
|
|
84
116
|
return {
|
|
85
|
-
deletedRows: newDeletedRows,
|
|
86
117
|
selection: [],
|
|
118
|
+
data,
|
|
119
|
+
updatedData,
|
|
120
|
+
rowStatus,
|
|
87
121
|
focusedCell: null,
|
|
88
122
|
topLeftCell: null,
|
|
89
123
|
fillValueBaseCell: null
|
|
90
124
|
};
|
|
91
125
|
});
|
|
92
126
|
},
|
|
93
|
-
resetChanges() {
|
|
94
|
-
set((state) => (
|
|
95
|
-
updatedData: [],
|
|
96
|
-
deletedRows: /* @__PURE__ */ new Set(),
|
|
97
|
-
columnWidths: /* @__PURE__ */ new Map(),
|
|
98
|
-
selection: [],
|
|
99
|
-
focusedCell: null,
|
|
100
|
-
topLeftCell: null,
|
|
101
|
-
fillValueBaseCell: null
|
|
102
|
-
}));
|
|
127
|
+
resetChanges(regions) {
|
|
128
|
+
set((state) => resetChangesForSelection(state, regions));
|
|
103
129
|
},
|
|
104
130
|
onColumnWidthChanged(columnIx, newWidth) {
|
|
105
131
|
set((state) => {
|
|
@@ -110,11 +136,6 @@ function createZustandStore(set, get) {
|
|
|
110
136
|
return { columnWidthsIndex: newColumnWidths };
|
|
111
137
|
});
|
|
112
138
|
},
|
|
113
|
-
setVisibleCells(visibleCells) {
|
|
114
|
-
const ref = get().visibleCellsRef;
|
|
115
|
-
if (ref == null) return;
|
|
116
|
-
ref.current = visibleCells;
|
|
117
|
-
},
|
|
118
139
|
onDragValue(event) {
|
|
119
140
|
set((state) => {
|
|
120
141
|
return { fillValueBaseCell: state.focusedCell };
|
|
@@ -136,19 +157,17 @@ function createZustandStore(set, get) {
|
|
|
136
157
|
return { updatedData: update__default.default(updatedData, spec) };
|
|
137
158
|
});
|
|
138
159
|
},
|
|
139
|
-
initialize(props) {
|
|
140
|
-
set({ ...props, initialized: true });
|
|
141
|
-
},
|
|
142
160
|
clearSelection() {
|
|
143
161
|
set((state) => {
|
|
144
|
-
const { selection, updatedData, columnSpec, data } = state;
|
|
162
|
+
const { selection: selection2, updatedData, columnSpec, data } = state;
|
|
145
163
|
let spec = {};
|
|
146
|
-
for (const region of
|
|
147
|
-
console.log("Clearing region", region);
|
|
164
|
+
for (const region of selection2) {
|
|
148
165
|
const { cols, rows } = region;
|
|
149
166
|
const rowRange = index.range(rows ?? [0, updatedData.length - 1]);
|
|
150
167
|
const colRange = index.range(cols ?? [0, columnSpec.length - 1]);
|
|
151
168
|
for (const row of rowRange) {
|
|
169
|
+
const rowIsDeleted = state.rowStatus[row] === types.TableElementStatus.DELETED;
|
|
170
|
+
if (rowIsDeleted) continue;
|
|
152
171
|
let vals = {};
|
|
153
172
|
for (const col of colRange) {
|
|
154
173
|
const key = columnSpec[col].key;
|
|
@@ -166,9 +185,9 @@ function createZustandStore(set, get) {
|
|
|
166
185
|
};
|
|
167
186
|
});
|
|
168
187
|
},
|
|
169
|
-
onSelection(
|
|
188
|
+
onSelection(selection2) {
|
|
170
189
|
set((state) => {
|
|
171
|
-
if (selectionEquals(
|
|
190
|
+
if (selectionEquals(selection2, state.selection) && singleFocusedCell(selection2) == null) {
|
|
172
191
|
return {
|
|
173
192
|
selection: [],
|
|
174
193
|
focusedCell: null,
|
|
@@ -176,19 +195,19 @@ function createZustandStore(set, get) {
|
|
|
176
195
|
fillValueBaseCell: null
|
|
177
196
|
};
|
|
178
197
|
}
|
|
179
|
-
let spec = updateSelection(
|
|
198
|
+
let spec = updateSelection(selection2);
|
|
180
199
|
if (state.fillValueBaseCell != null) {
|
|
181
|
-
spec.updatedData = fillValues(state,
|
|
200
|
+
spec.updatedData = fillValues(state, selection2);
|
|
182
201
|
}
|
|
183
202
|
return spec;
|
|
184
203
|
});
|
|
185
204
|
},
|
|
186
205
|
onSelectionEdited(value) {
|
|
187
206
|
set((state) => {
|
|
188
|
-
const { selection, updatedData, columnSpec, editable } = state;
|
|
207
|
+
const { selection: selection2, updatedData, columnSpec, editable } = state;
|
|
189
208
|
if (!editable) return {};
|
|
190
209
|
let spec = {};
|
|
191
|
-
for (const region of
|
|
210
|
+
for (const region of selection2) {
|
|
192
211
|
const { cols, rows } = region;
|
|
193
212
|
const rowRange = index.range(rows ?? [0, updatedData.length - 1]);
|
|
194
213
|
const colRange = index.range(cols ?? [0, columnSpec.length - 1]);
|
|
@@ -223,15 +242,94 @@ function createZustandStore(set, get) {
|
|
|
223
242
|
tableRef.current.scrollToRegion({
|
|
224
243
|
rows: [rowIndex, rowIndex]
|
|
225
244
|
});
|
|
245
|
+
},
|
|
246
|
+
setFilter(filterId, filter, filterState) {
|
|
247
|
+
set((state) => {
|
|
248
|
+
const newFilters = new Map(
|
|
249
|
+
state.activeFilters
|
|
250
|
+
);
|
|
251
|
+
newFilters.set(filterId, { filter, state: filterState });
|
|
252
|
+
return {
|
|
253
|
+
activeFilters: newFilters,
|
|
254
|
+
filteredRowIndices: selection.computeTransformedRowIndices(
|
|
255
|
+
state.data,
|
|
256
|
+
state.updatedData,
|
|
257
|
+
newFilters,
|
|
258
|
+
state.columnSorts
|
|
259
|
+
)
|
|
260
|
+
};
|
|
261
|
+
});
|
|
262
|
+
},
|
|
263
|
+
removeFilter(filterId) {
|
|
264
|
+
set((state) => {
|
|
265
|
+
const newFilters = new Map(
|
|
266
|
+
state.activeFilters
|
|
267
|
+
);
|
|
268
|
+
newFilters.delete(filterId);
|
|
269
|
+
return {
|
|
270
|
+
activeFilters: newFilters,
|
|
271
|
+
filteredRowIndices: selection.computeTransformedRowIndices(
|
|
272
|
+
state.data,
|
|
273
|
+
state.updatedData,
|
|
274
|
+
newFilters,
|
|
275
|
+
state.columnSorts
|
|
276
|
+
)
|
|
277
|
+
};
|
|
278
|
+
});
|
|
279
|
+
},
|
|
280
|
+
clearFilters() {
|
|
281
|
+
set((state) => ({
|
|
282
|
+
activeFilters: /* @__PURE__ */ new Map(),
|
|
283
|
+
filteredRowIndices: selection.computeTransformedRowIndices(
|
|
284
|
+
state.data,
|
|
285
|
+
state.updatedData,
|
|
286
|
+
/* @__PURE__ */ new Map(),
|
|
287
|
+
state.columnSorts
|
|
288
|
+
)
|
|
289
|
+
}));
|
|
290
|
+
},
|
|
291
|
+
setColumnSort(key, ascending) {
|
|
292
|
+
set((state) => {
|
|
293
|
+
let newSorts;
|
|
294
|
+
if (ascending == null) {
|
|
295
|
+
newSorts = state.columnSorts.filter((s) => s.key !== key);
|
|
296
|
+
} else {
|
|
297
|
+
const without = state.columnSorts.filter((s) => s.key !== key);
|
|
298
|
+
newSorts = [...without, { key, ascending }];
|
|
299
|
+
}
|
|
300
|
+
return {
|
|
301
|
+
columnSorts: newSorts,
|
|
302
|
+
filteredRowIndices: selection.computeTransformedRowIndices(
|
|
303
|
+
state.data,
|
|
304
|
+
state.updatedData,
|
|
305
|
+
state.activeFilters,
|
|
306
|
+
newSorts
|
|
307
|
+
)
|
|
308
|
+
};
|
|
309
|
+
});
|
|
310
|
+
},
|
|
311
|
+
clearColumnSorts() {
|
|
312
|
+
set((state) => ({
|
|
313
|
+
columnSorts: [],
|
|
314
|
+
filteredRowIndices: selection.computeTransformedRowIndices(
|
|
315
|
+
state.data,
|
|
316
|
+
state.updatedData,
|
|
317
|
+
state.activeFilters,
|
|
318
|
+
[]
|
|
319
|
+
)
|
|
320
|
+
}));
|
|
321
|
+
},
|
|
322
|
+
setClipboardProxy(proxy) {
|
|
323
|
+
set({ clipboardProxy: proxy });
|
|
226
324
|
}
|
|
227
325
|
};
|
|
228
326
|
}
|
|
229
|
-
function updateSelection(
|
|
230
|
-
const focusedCell = singleFocusedCell(
|
|
327
|
+
function updateSelection(selection2) {
|
|
328
|
+
const focusedCell = singleFocusedCell(selection2);
|
|
231
329
|
let spec = {
|
|
232
|
-
selection,
|
|
330
|
+
selection: selection2,
|
|
233
331
|
focusedCell,
|
|
234
|
-
topLeftCell: topLeftCell(
|
|
332
|
+
topLeftCell: topLeftCell(selection2)
|
|
235
333
|
};
|
|
236
334
|
if (focusedCell != null) {
|
|
237
335
|
spec.fillValueBaseCell = null;
|
|
@@ -292,9 +390,9 @@ function singleFocusedCell(sel) {
|
|
|
292
390
|
if (sel?.length !== 1) return null;
|
|
293
391
|
return topLeftCell(sel, true);
|
|
294
392
|
}
|
|
295
|
-
function fillValues(state,
|
|
393
|
+
function fillValues(state, selection2) {
|
|
296
394
|
const { updatedData, columnSpec, editable, fillValueBaseCell, data } = state;
|
|
297
|
-
let regions =
|
|
395
|
+
let regions = selection2.map((region) => {
|
|
298
396
|
const { cols, rows } = region;
|
|
299
397
|
const [col2] = cols;
|
|
300
398
|
return { cols: [col2, col2], rows };
|
|
@@ -313,6 +411,73 @@ function fillValues(state, selection) {
|
|
|
313
411
|
}
|
|
314
412
|
return update__default.default(updatedData, spec);
|
|
315
413
|
}
|
|
414
|
+
function resetChangesForSelection(state, selection$1) {
|
|
415
|
+
const emptySelection = {
|
|
416
|
+
selection: [],
|
|
417
|
+
focusedCell: null,
|
|
418
|
+
topLeftCell: null,
|
|
419
|
+
fillValueBaseCell: null
|
|
420
|
+
};
|
|
421
|
+
const { data } = state;
|
|
422
|
+
const cardinality = selection.getSelectionCardinality(selection$1 ?? []) ?? table.RegionCardinality.FULL_TABLE;
|
|
423
|
+
const rowStatus = [...state.rowStatus];
|
|
424
|
+
const updatedData = [...state.updatedData];
|
|
425
|
+
const addedRows = /* @__PURE__ */ new Set();
|
|
426
|
+
switch (cardinality) {
|
|
427
|
+
case table.RegionCardinality.FULL_TABLE:
|
|
428
|
+
for (let i = 0; i < rowStatus.length; i++) {
|
|
429
|
+
if (rowStatus[i] === types.TableElementStatus.ADDED) {
|
|
430
|
+
addedRows.add(i);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
return {
|
|
434
|
+
updatedData: [],
|
|
435
|
+
rowStatus: [],
|
|
436
|
+
data: data.filter((_, i) => !addedRows.has(i)),
|
|
437
|
+
...emptySelection
|
|
438
|
+
};
|
|
439
|
+
case table.RegionCardinality.FULL_COLUMNS:
|
|
440
|
+
case table.RegionCardinality.CELLS:
|
|
441
|
+
const columnKeys = selection.getSelectedColumnKeys(selection$1, state.columnSpec);
|
|
442
|
+
const spec = {};
|
|
443
|
+
const rowIndices = cardinality == table.RegionCardinality.FULL_COLUMNS ? Array.from({ length: updatedData.length }, (_, i) => i) : selection.getSelectedRowIndices(selection$1);
|
|
444
|
+
for (const row of rowIndices) {
|
|
445
|
+
if (updatedData[row] == null) continue;
|
|
446
|
+
spec[row] = { $unset: columnKeys };
|
|
447
|
+
}
|
|
448
|
+
return {
|
|
449
|
+
updatedData: update__default.default(updatedData, spec),
|
|
450
|
+
...emptySelection
|
|
451
|
+
};
|
|
452
|
+
case table.RegionCardinality.FULL_ROWS:
|
|
453
|
+
for (const row of selection.getSelectedRowIndices(selection$1)) {
|
|
454
|
+
updatedData[row] = void 0;
|
|
455
|
+
if (state.rowStatus[row] === types.TableElementStatus.DELETED) {
|
|
456
|
+
rowStatus[row] = void 0;
|
|
457
|
+
} else if (state.rowStatus[row] === types.TableElementStatus.ADDED) {
|
|
458
|
+
addedRows.add(row);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
const removeCandidateRows = (arr) => {
|
|
462
|
+
return arr.filter((d, i) => !addedRows.has(i));
|
|
463
|
+
};
|
|
464
|
+
return {
|
|
465
|
+
updatedData: removeCandidateRows(updatedData),
|
|
466
|
+
data: removeCandidateRows(state.data),
|
|
467
|
+
rowStatus: removeCandidateRows(rowStatus),
|
|
468
|
+
...emptySelection
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
function insertItemAtIndex(arr, index2, item) {
|
|
473
|
+
if (arr.length < index2) {
|
|
474
|
+
const newArr = [...arr];
|
|
475
|
+
newArr.length = index2;
|
|
476
|
+
newArr[index2] = item;
|
|
477
|
+
return newArr;
|
|
478
|
+
}
|
|
479
|
+
return [...arr.slice(0, index2), item, ...arr.slice(index2)];
|
|
480
|
+
}
|
|
316
481
|
exports.createZustandStore = createZustandStore;
|
|
317
482
|
exports.singleFocusedCell = singleFocusedCell;
|
|
318
483
|
exports.topLeftCell = topLeftCell;
|