diff --git a/packages/datalib/src/ChangeSet.ts b/packages/datalib/src/ChangeSet.ts index a46f5935..22adaedf 100644 --- a/packages/datalib/src/ChangeSet.ts +++ b/packages/datalib/src/ChangeSet.ts @@ -337,6 +337,25 @@ export function revertChangeSetRowChanges(changeSet: ChangeSet, definition: Chan return changeSet; } +function consolidateInsertIndexes(changeSet: ChangeSet, name: NamedObjectInfo): ChangeSet { + const indexes = changeSet.inserts + .filter(x => x.pureName == name.pureName && x.schemaName == name.schemaName) + .map(x => x.insertedRowIndex); + + indexes.sort((a, b) => a - b); + if (indexes[indexes.length - 1] != indexes.length - 1) { + return { + ...changeSet, + inserts: changeSet.inserts.map(x => ({ + ...x, + insertedRowIndex: indexes.indexOf(x.insertedRowIndex), + })), + }; + } + + return changeSet; +} + export function deleteChangeSetRows(changeSet: ChangeSet, definition: ChangeSetRowDefinition): ChangeSet { let [fieldName, existingItem] = findExistingChangeSetItem(changeSet, definition); if (fieldName == 'updates') { @@ -344,7 +363,7 @@ export function deleteChangeSetRows(changeSet: ChangeSet, definition: ChangeSetR [fieldName, existingItem] = findExistingChangeSetItem(changeSet, definition); } if (fieldName == 'inserts') { - return revertChangeSetRowChanges(changeSet, definition); + return consolidateInsertIndexes(revertChangeSetRowChanges(changeSet, definition), definition); } else { if (existingItem && fieldName == 'deletes') return changeSet; return { @@ -390,11 +409,7 @@ export function changeSetInsertNewRow(changeSet: ChangeSet, name?: NamedObjectIn }; } -export function changeSetInsertDocuments( - changeSet: ChangeSet, - documents: any[], - name?: NamedObjectInfo -): ChangeSet { +export function changeSetInsertDocuments(changeSet: ChangeSet, documents: any[], name?: NamedObjectInfo): ChangeSet { const insertedRows = getChangeSetInsertedRows(changeSet, name); return { ...changeSet, diff --git a/packages/web/src/datagrid/DataGridCore.svelte b/packages/web/src/datagrid/DataGridCore.svelte index 5c0e119f..bd3cf4b0 100644 --- a/packages/web/src/datagrid/DataGridCore.svelte +++ b/packages/web/src/datagrid/DataGridCore.svelte @@ -391,7 +391,7 @@ export function deleteSelectedRows() { grider.beginUpdate(); - for (const index of getSelectedRowIndexes()) { + for (const index of _.sortBy(getSelectedRowIndexes(), x => -x)) { if (_.isNumber(index)) grider.deleteRow(index); } grider.endUpdate(); @@ -750,7 +750,7 @@ export function generateSqlFromData() { const columnIndexes = _.uniq(selectedCells.map(x => x[1])); - columnIndexes.sort(); + columnIndexes.sort((a, b) => a - b); showModal(GenerateSqlFromDataModal, { rows: getSelectedRowData(), diff --git a/packages/web/src/datagrid/SeriesSizes.ts b/packages/web/src/datagrid/SeriesSizes.ts index 483d5c50..ec62a740 100644 --- a/packages/web/src/datagrid/SeriesSizes.ts +++ b/packages/web/src/datagrid/SeriesSizes.ts @@ -68,7 +68,7 @@ export class SeriesSizes { // _.map(this.intKeys(_.keys(this.sizeOverridesByModelIndex)), (x) => this.modelToReal(x) - this.frozenCount), x => x >= 0 ); - this.scrollIndexes.sort(); + this.scrollIndexes.sort((a, b) => a - b); let lastScrollIndex: number = -1; let lastEndPosition: number = 0; this.scrollIndexes.forEach(scrollIndex => {