Skip to content

Commit 0ad2409

Browse files
committed
* dtable: add copyHeader option to selectable plugin and implement header copying in copySelections function
1 parent 4d1c965 commit 0ad2409

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

lib/dtable/src/plugins/datagrid/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ export const datagridPlugin: DTablePlugin<DTableDatagridTypes, DTableDatagridDep
299299
cellValueSplitter: '\t',
300300
cellValueGetter,
301301
hotkeys: {},
302+
copyHeader: false,
302303
autoExpandGrid: true,
303304
},
304305
options(options) {

lib/dtable/src/plugins/selectable/index.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export interface DTableSelectableTypes {
2828
beforeSelectCells: (this: DTableSelectable, cells: DTableCellPos[]) => void | DTableCellPos[];
2929
ignoreDeselectOn: string;
3030
markSelectRange: boolean;
31+
copyHeader?: boolean;
3132
selectableHotkeys?: {
3233
selectAll?: boolean | string;
3334
copy?: boolean | string;
@@ -487,6 +488,7 @@ function copySelections(this: DTableSelectable) {
487488
minRowIndex = Math.min(pos.row, minRowIndex);
488489
});
489490
const data: unknown[][] = [];
491+
const selectedColIndexes = new Set<number>();
490492
selectedCells.forEach((pos) => {
491493
const value = this.getCellDraftValue ? this.getCellDraftValue(pos.row, pos.col) : this.getCellValue(pos.row, pos.col);
492494
let rowData = data[pos.row - minRowIndex];
@@ -495,7 +497,20 @@ function copySelections(this: DTableSelectable) {
495497
data[pos.row - minRowIndex] = rowData;
496498
}
497499
rowData[pos.col - minColIndex] = value;
500+
selectedColIndexes.add(pos.col);
498501
});
502+
if (this.options.copyHeader) {
503+
const headerRow: unknown[] = [];
504+
selectedColIndexes.forEach((colIndex) => {
505+
const colInfo = this.getColInfo(colIndex);
506+
if (!colInfo) {
507+
return;
508+
}
509+
const value = this.getCellDraftValue ? this.getCellDraftValue(-1, colInfo) : this.getCellValue(-1, colInfo);
510+
headerRow[colIndex - minColIndex] = (value === colInfo.name) ? '' : value;
511+
});
512+
data.unshift(headerRow);
513+
}
499514
const plainText = trimDataGrid(data).map(x => x.join('\t')).join('\n');
500515
navigator.clipboard.writeText(plainText);
501516
return true;
@@ -550,7 +565,11 @@ const hotkeyHandlers: Record<string, (this: DTableSelectable, event: KeyboardEve
550565

551566
const selectablePlugin: DTablePlugin<DTableSelectableTypes, [DTableHotkeyTypes, DTableMousemoveTypes, DTableAutoscrollTypes, DTableDraftTypes]> = {
552567
name: 'selectable',
553-
defaultOptions: {selectable: true, markSelectRange: true},
568+
defaultOptions: {
569+
selectable: true,
570+
copyHeader: true,
571+
markSelectRange: true,
572+
},
554573
when: options => !!options.selectable,
555574
plugins: [mousemove, hotkey],
556575
state() {

0 commit comments

Comments
 (0)