Skip to content

Commit

Permalink
updating the CSV paste logic to not add extra rows if displayAddNewRo…
Browse files Browse the repository at this point in the history
…w is set to false
  • Loading branch information
OvidijusParsiunas committed Feb 20, 2024
1 parent a786b3c commit 79be452
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion component/src/utils/paste/CSV/overwriteCellsViaCSVOnPaste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@ import {FocusedCellUtils} from '../../focusedElements/focusedCellUtils';
import {InsertMatrix} from '../../programmaticUpdates/insertMatrix';
import {ParseCSVClipboardText} from './parseCSVClipboardText';
import {FocusedCell} from '../../../types/focusedCell';
import {TableData} from '../../../types/tableData';
import {ActiveTable} from '../../../activeTable';

export class OverwriteCellsViaCSVOnPaste {
private static trimCSVRowsIfCantCreateNew(CSV: TableData, data: TableData, rowIndex: number) {
if (data.length < CSV.length + rowIndex) {
return CSV.slice(0, CSV.length - (CSV.length + rowIndex - data.length));
}
return CSV;
}

private static focusOriginalCellAfterProcess(at: ActiveTable, process: () => void) {
const {element, rowIndex, columnIndex} = at._focusedElements.cell as Required<FocusedCell>;
process();
Expand All @@ -15,7 +23,8 @@ export class OverwriteCellsViaCSVOnPaste {
public static overwrite(at: ActiveTable,
clipboardText: string, event: ClipboardEvent, rowIndex: number, columnIndex: number) {
event.preventDefault();
const CSV = ParseCSVClipboardText.parse(clipboardText);
let CSV = ParseCSVClipboardText.parse(clipboardText);
if (!at.displayAddNewRow) CSV = OverwriteCellsViaCSVOnPaste.trimCSVRowsIfCantCreateNew(CSV, at.data, rowIndex);
OverwriteCellsViaCSVOnPaste.focusOriginalCellAfterProcess(at,
InsertMatrix.insert.bind(this, at, CSV, rowIndex, columnIndex));
}
Expand Down

0 comments on commit 79be452

Please sign in to comment.