Skip to content

Commit 8a18710

Browse files
authored
Fix row indexing when header disabled (#25)
1 parent 3a7bffe commit 8a18710

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/extension.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,11 @@ class CsvEditorProvider implements vscode.CustomTextEditorProvider {
276276
data.forEach((row, r) => {
277277
tableHtml += `<tr>${
278278
addSerialIndex
279-
? `<td tabindex="0" style="min-width: 4ch; max-width: 4ch; border: 1px solid #555; color: #888;" data-row="${r + 1}" data-col="-1">${r + 1}</td>`
279+
? `<td tabindex="0" style="min-width: 4ch; max-width: 4ch; border: 1px solid #555; color: #888;" data-row="${r}" data-col="-1">${r + 1}</td>`
280280
: ''
281281
}`;
282282
row.forEach((cell, i) => {
283-
tableHtml += `<td tabindex="0" style="min-width: ${Math.min(columnWidths[i] || 0, 100)}ch; max-width: 100ch; border: 1px solid #555; color: ${columnColors[i]}; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;" data-row="${r + 1}" data-col="${i}">${cell}</td>`;
283+
tableHtml += `<td tabindex="0" style="min-width: ${Math.min(columnWidths[i] || 0, 100)}ch; max-width: 100ch; border: 1px solid #555; color: ${columnColors[i]}; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;" data-row="${r}" data-col="${i}">${cell}</td>`;
284284
});
285285
tableHtml += `</tr>`;
286286
});
@@ -364,6 +364,7 @@ class CsvEditorProvider implements vscode.CustomTextEditorProvider {
364364
let startCell = null, endCell = null, selectionMode = "cell";
365365
let editingCell = null, originalCellValue = "";
366366
const table = document.querySelector('table');
367+
const hasHeader = document.querySelector('thead') !== null;
367368
const getCellCoords = cell => ({ row: parseInt(cell.getAttribute('data-row')), col: parseInt(cell.getAttribute('data-col')) });
368369
const clearSelection = () => { currentSelection.forEach(c => c.classList.remove('selected')); currentSelection = []; };
369370
table.addEventListener('mousedown', e => {
@@ -424,7 +425,7 @@ class CsvEditorProvider implements vscode.CustomTextEditorProvider {
424425
const minCol = Math.min(start.col, end.col), maxCol = Math.max(start.col, end.col);
425426
for(let r = minRow; r <= maxRow; r++){
426427
for(let c = minCol; c <= maxCol; c++){
427-
const selector = (r === 0 ? 'th' : 'td') + '[data-row="'+r+'"][data-col="'+c+'"]';
428+
const selector = (hasHeader && r === 0 ? 'th' : 'td') + '[data-row="'+r+'"][data-col="'+c+'"]';
428429
const selCell = table.querySelector(selector);
429430
if(selCell){ selCell.classList.add('selected'); currentSelection.push(selCell); }
430431
}
@@ -588,7 +589,7 @@ class CsvEditorProvider implements vscode.CustomTextEditorProvider {
588589
for(let r = minRow; r <= maxRow; r++){
589590
let rowVals = [];
590591
for(let c = minCol; c <= maxCol; c++){
591-
const selector = (r === 0 ? 'th' : 'td') + '[data-row="'+r+'"][data-col="'+c+'"]';
592+
const selector = (hasHeader && r === 0 ? 'th' : 'td') + '[data-row="'+r+'"][data-col="'+c+'"]';
592593
const cell = table.querySelector(selector);
593594
rowVals.push(cell ? cell.innerText : '');
594595
}

0 commit comments

Comments
 (0)