import { useEffect } from "react"; import { usePropsRef } from "../context/PropsContext"; import { selectionContext } from "../context/SelectionContext"; // This triggers the onCopy callback when the user copies using the keyboard export const useCopy = () => { const selectionRef = selectionContext.useRef(); const propsRef = usePropsRef(); useEffect(() => { document.addEventListener("copy", (e) => { const selection = selectionRef.current; if (!selection) { return; } let copyText = [...new Array(selection.height)] .map((_, y) => [...new Array(selection.width)] .map((_, x) => propsRef.current.columnProps[selection.start.x + x].onCopy( propsRef.current.rows[selection.start.y + y] ) ) .join("\t") ) .join("\n"); e.clipboardData?.setData("text/plain", copyText); e.preventDefault(); }); }, []); };