save commands

This commit is contained in:
Jan Prochazka 2021-03-06 18:50:58 +01:00
parent 423644e9d9
commit 49f22e1a3b
3 changed files with 60 additions and 3 deletions

View File

@ -347,5 +347,6 @@ export function changeSetInsertNewRow(changeSet: ChangeSet, name?: NamedObjectIn
}
export function changeSetContainsChanges(changeSet: ChangeSet) {
if (!changeSet) return false;
return changeSet.deletes.length > 0 || changeSet.updates.length > 0 || changeSet.inserts.length > 0;
}

View File

@ -1,6 +1,8 @@
<script lang="ts" context="module">
const currentDataGrid = writable(null);
const currentDataGridChangeSet = memberStore(currentDataGrid, grid => grid?.getChangeSetStore() || nullStore);
registerCommand({
id: 'dataGrid.refresh',
category: 'Data grid',
@ -8,7 +10,7 @@
keyText: 'F5',
toolbar: true,
icon: 'icon reload',
enabledStore: derived([currentDataGrid], ([grid]) => grid != null),
enabledStore: derived(currentDataGrid, grid => grid != null),
onClick: () => get(currentDataGrid).refresh(),
});
@ -19,10 +21,27 @@
keyText: 'Ctrl+S',
toolbar: true,
icon: 'icon save',
enabledStore: derived([currentDataGrid], ([grid]) => grid?.getGrider()?.allowSave),
enabledStore: derived(currentDataGridChangeSet, (changeSet: any) => changeSetContainsChanges(changeSet?.value)),
onClick: () => get(currentDataGrid).save(),
});
registerCommand({
id: 'dataGrid.revertRowChanges',
category: 'Data grid',
name: 'Revert row changes',
keyText: 'Ctrl+R',
enabledStore: derived(currentDataGridChangeSet, (changeSet: any) => changeSetContainsChanges(changeSet?.value)),
onClick: () => get(currentDataGrid).revertRowChanges(),
});
registerCommand({
id: 'dataGrid.revertAllChanges',
category: 'Data grid',
name: 'Revert all changes',
enabledStore: derived(currentDataGridChangeSet, (changeSet: any) => changeSetContainsChanges(changeSet?.value)),
onClick: () => get(currentDataGrid).revertAllChanges(),
});
function getRowCountInfo(selectedCells, grider, realColumnUniqueNames, selectedRowData, allRowCount) {
if (selectedCells.length > 1 && selectedCells.every(x => _.isNumber(x[0]) && _.isNumber(x[1]))) {
let sum = _.sumBy(selectedCells, cell => {
@ -49,7 +68,7 @@
</script>
<script lang="ts">
import { GridDisplay } from 'dbgate-datalib';
import { changeSetContainsChanges, GridDisplay } from 'dbgate-datalib';
import { get_current_component } from 'svelte/internal';
import _ from 'lodash';
import { writable, get, derived } from 'svelte/store';
@ -76,6 +95,7 @@
import createReducer from '../utility/createReducer';
import keycodes from '../utility/keycodes';
import { nullStore } from '../stores';
import memberStore from '../utility/memberStore';
export let loadNextData = undefined;
export let grider = undefined;
@ -91,6 +111,7 @@
export let isLoadedAll;
export let loadedTime;
export let changeSetStore;
const wheelRowCount = 5;
const instance = get_current_component();
@ -124,6 +145,22 @@
return grider;
}
export function getChangeSetStore() {
return changeSetStore;
}
export function revertRowChanges() {
grider.beginUpdate();
for (const index of getSelectedRowIndexes()) {
if (_.isNumber(index)) grider.revertRowChanges(index);
}
grider.endUpdate();
}
export function revertAllChanges() {
grider.revertAllChanges();
}
$: autofillMarkerCell =
selectedCells && selectedCells.length > 0 && _.uniq(selectedCells.map(x => x[0])).length == 1
? [_.max(selectedCells.map(x => x[0])), _.max(selectedCells.map(x => x[1]))]

View File

@ -0,0 +1,19 @@
import { writable } from 'svelte/store';
export default function memberStore(store, extractStore) {
let res;
let unsubscribeSub = null;
store.subscribe(value => {
const subStore = extractStore(value);
if (unsubscribeSub) unsubscribeSub();
unsubscribeSub = subStore.subscribe(subValue => {
if (res) {
console.log('subValue', subValue);
res.set(subValue);
} else {
res = writable(subValue);
}
});
});
return res;
}