From 79be45284ccaf1ebf5984e463368925a9a91b408 Mon Sep 17 00:00:00 2001 From: Ovidijus Parsiunas Date: Tue, 20 Feb 2024 20:15:14 +0000 Subject: [PATCH] updating the CSV paste logic to not add extra rows if displayAddNewRow is set to false --- .../utils/paste/CSV/overwriteCellsViaCSVOnPaste.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/component/src/utils/paste/CSV/overwriteCellsViaCSVOnPaste.ts b/component/src/utils/paste/CSV/overwriteCellsViaCSVOnPaste.ts index f6ec28df..977ad823 100644 --- a/component/src/utils/paste/CSV/overwriteCellsViaCSVOnPaste.ts +++ b/component/src/utils/paste/CSV/overwriteCellsViaCSVOnPaste.ts @@ -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; process(); @@ -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)); }