diff --git a/packages/datalib/src/ChangeSet.ts b/packages/datalib/src/ChangeSet.ts index ea91fc0df..652c7c2c7 100644 --- a/packages/datalib/src/ChangeSet.ts +++ b/packages/datalib/src/ChangeSet.ts @@ -194,7 +194,9 @@ export function changeSetToSql(changeSet: ChangeSet): Command[] { } export function revertChangeSetRowChanges(changeSet: ChangeSet, definition: ChangeSetRowDefinition): ChangeSet { + console.log('definition', definition) const [field, item] = findExistingChangeSetItem(changeSet, definition); + console.log('field, item', field, item) if (item) return { ...changeSet, diff --git a/packages/datalib/src/GridDisplay.ts b/packages/datalib/src/GridDisplay.ts index d8621d40e..fcc805307 100644 --- a/packages/datalib/src/GridDisplay.ts +++ b/packages/datalib/src/GridDisplay.ts @@ -371,21 +371,19 @@ export abstract class GridDisplay { if (!this.baseTable) return null; if (this.baseTable.pureName != col.pureName || this.baseTable.schemaName != col.schemaName) return null; return { - pureName: col.pureName, - schemaName: col.schemaName, + ...this.getChangeSetRow(row, insertedRowIndex), uniqueName: uniqueName, columnName: col.columnName, - insertedRowIndex, - condition: insertedRowIndex == null ? this.getChangeSetCondition(row) : null, }; } - getChangeSetRow(row): ChangeSetRowDefinition { + getChangeSetRow(row, insertedRowIndex): ChangeSetRowDefinition { if (!this.baseTable) return null; return { pureName: this.baseTable.pureName, schemaName: this.baseTable.schemaName, - condition: this.getChangeSetCondition(row), + insertedRowIndex, + condition: insertedRowIndex == null ? this.getChangeSetCondition(row) : null, }; } } diff --git a/packages/web/src/datagrid/DataGridCore.js b/packages/web/src/datagrid/DataGridCore.js index 7277986ef..f8f69a9b1 100644 --- a/packages/web/src/datagrid/DataGridCore.js +++ b/packages/web/src/datagrid/DataGridCore.js @@ -339,16 +339,36 @@ export default function DataGridCore(props) { } } - function revertRowChanges() { - if (loadedRows && currentCell && loadedRows[currentCell[0]]) { - setChangeSet(revertChangeSetRowChanges(changeSet, display.getChangeSetRow(loadedRows[currentCell[0]]))); + function getSelectedRowDefinitions() { + const res = []; + if (!loadedAndInsertedRows) return res; + const rowIndexes = _.uniq((selectedCells || []).map(x => x[0])); + for (const index of rowIndexes) { + if (loadedAndInsertedRows[index] && _.isNumber(index)) { + const insertedRowIndex = + firstVisibleRowScrollIndex + index >= loadedRows.length + ? firstVisibleRowScrollIndex + index - loadedRows.length + : null; + res.push(display.getChangeSetRow(loadedAndInsertedRows[index], insertedRowIndex)); + } } + return res; + } + + function revertRowChanges() { + const updatedChangeSet = getSelectedRowDefinitions().reduce( + (chs, row) => revertChangeSetRowChanges(chs, row), + changeSet + ); + setChangeSet(updatedChangeSet); } function deleteCurrentRow() { - if (loadedRows && currentCell && loadedRows[currentCell[0]]) { - setChangeSet(deleteChangeSetRows(changeSet, display.getChangeSetRow(loadedRows[currentCell[0]]))); - } + const updatedChangeSet = getSelectedRowDefinitions().reduce( + (chs, row) => deleteChangeSetRows(chs, row), + changeSet + ); + setChangeSet(updatedChangeSet); } function handleGridWheel(event) { diff --git a/packages/web/src/datagrid/DataGridRow.js b/packages/web/src/datagrid/DataGridRow.js index 2f204cd35..51177dfcd 100644 --- a/packages/web/src/datagrid/DataGridRow.js +++ b/packages/web/src/datagrid/DataGridRow.js @@ -109,7 +109,7 @@ export default function DataGridRow({ insertedRowIndex, }) { // console.log('RENDER ROW', rowIndex); - const rowDefinition = display.getChangeSetRow(row); + const rowDefinition = display.getChangeSetRow(row, insertedRowIndex); const [matchedField, matchedChangeSetItem] = findExistingChangeSetItem(changeSet, rowDefinition); const rowUpdated = matchedChangeSetItem ? { ...row, ...matchedChangeSetItem.fields } : row; const hintFieldsAllowed = visibleRealColumns