Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | 8x 4x 3x 3x 2x 2x 2x 2x 4x 4x 4x 4x 8x 4x 2x 8x 4x 4x 4x 3x 3x 4x 3x 2x 2x 2x 2x 8x 1x 1x 1x 1x 1x 1x 2x 2x 4x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x | import { useEffect, useCallback } from 'react';
import type { CsvState, CsvAction } from './types';
import { getSelectionRange } from './useSelection';
interface UseCopyPasteProps {
state: CsvState;
sortedRows: string[][];
sortedToSourceMap: number[];
dispatch: (action: CsvAction) => void;
containerRef: React.RefObject<HTMLDivElement | null>;
}
export function useCopyPaste({ state, sortedRows, sortedToSourceMap, dispatch, containerRef }: UseCopyPasteProps) {
const handleCopy = useCallback((e: ClipboardEvent) => {
if (!state.selection) return;
const target = e.target as HTMLElement;
if (target.tagName === 'INPUT' || target.tagName === 'TEXTAREA') return;
e.preventDefault();
const { minRow, maxRow, minCol, maxCol } = getSelectionRange(state.selection);
const lines: string[] = [];
for (let r = minRow; r <= maxRow; r++) {
const row = sortedRows[r];
Iif (!row) continue;
const cells: string[] = [];
for (let c = minCol; c <= maxCol; c++) {
cells.push(row[c] || '');
}
lines.push(cells.join('\t'));
}
e.clipboardData?.setData('text/plain', lines.join('\n'));
}, [state.selection, sortedRows]);
const handlePaste = useCallback((e: ClipboardEvent) => {
const target = e.target as HTMLElement;
Iif (target.tagName === 'INPUT' || target.tagName === 'TEXTAREA') return;
if (!state.selection) return;
e.preventDefault();
const text = e.clipboardData?.getData('text/plain');
if (!text) return;
const pasteRows = text.split('\n').map(line => line.split('\t'));
const visualStartRow = Math.min(state.selection.start.row, state.selection.end.row);
const startCol = Math.min(state.selection.start.col, state.selection.end.col);
const startRow = sortedToSourceMap[visualStartRow];
dispatch({ type: 'PASTE_CELLS', startRow, startCol, data: pasteRows });
}, [state.selection, sortedToSourceMap, dispatch]);
const handleCut = useCallback((e: ClipboardEvent) => {
Iif (!state.selection) return;
const target = e.target as HTMLElement;
Iif (target.tagName === 'INPUT' || target.tagName === 'TEXTAREA') return;
handleCopy(e);
const { minRow, maxRow, minCol, maxCol } = getSelectionRange(state.selection);
for (let r = minRow; r <= maxRow; r++) {
const sourceRow = sortedToSourceMap[r];
for (let c = minCol; c <= maxCol; c++) {
dispatch({ type: 'SET_CELL', row: sourceRow, col: c, value: '' });
}
}
}, [state.selection, sortedToSourceMap, dispatch, handleCopy]);
useEffect(() => {
const el = containerRef.current;
Iif (!el) return;
el.addEventListener('copy', handleCopy);
el.addEventListener('paste', handlePaste);
el.addEventListener('cut', handleCut);
return () => {
el.removeEventListener('copy', handleCopy);
el.removeEventListener('paste', handlePaste);
el.removeEventListener('cut', handleCut);
};
}, [handleCopy, handlePaste, handleCut, containerRef]);
}
|