Skip to content

Commit

Permalink
copy and paste feature. onSelectedCell fcn provides the index to save…
Browse files Browse the repository at this point in the history
… cell data into jotai
  • Loading branch information
gibsonliketheguitar committed Jan 7, 2022
1 parent 6f4c74f commit c9e7c9f
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 13 deletions.
10 changes: 10 additions & 0 deletions src/assets/icons/Paste.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import SvgIcon, { SvgIconProps } from "@mui/material/SvgIcon";
import { mdiContentPaste } from "@mdi/js";

export default function Paste(props: SvgIconProps) {
return (
<SvgIcon {...props}>
<path d={mdiContentPaste} />
</SvgIcon>
);
}
10 changes: 10 additions & 0 deletions src/assets/icons/PasteFromClipboard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import SvgIcon, { SvgIconProps } from "@mui/material/SvgIcon";
import { mdiClipboardArrowDownOutline } from "@mdi/js";

export default function PasteFromClipboard(props: SvgIconProps) {
return (
<SvgIcon {...props}>
<path d={mdiClipboardArrowDownOutline} />
</SvgIcon>
);
}
2 changes: 1 addition & 1 deletion src/components/SideDrawer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export default function SideDrawer() {

const [cell, setCell] = useState<SelectedCell>(null);
const [open, setOpen] = useState(false);
if (sideDrawerRef) sideDrawerRef.current = { cell, setCell, open, setOpen };

if (sideDrawerRef) sideDrawerRef.current = { cell, setCell, open, setOpen };
const handleNavigate = (direction: "up" | "down") => () => {
if (!tableState?.rows) return;

Expand Down
61 changes: 52 additions & 9 deletions src/components/Table/CellMenu/index.tsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,87 @@
import React from "react";
import { PopoverProps } from "@mui/material";
import CopyCells from "@src/assets/icons/CopyCells";
import Paste from "@src/assets/icons/Paste";
import { useProjectContext } from "@src/contexts/ProjectContext";
import { MenuContents } from "./MenuContent";
import _find from "lodash/find";
import { atom, useAtom } from "jotai";
import PasteFromClipboard from "@src/assets/icons/PasteFromClipboard";

export const cellMenuDataAtom = atom("");

export type SelectedCell = {
rowIndex: number;
colIndex: number;
};

export type CellMenuRef = {
selectedCell: SelectedCell;
setSelectedCell: React.Dispatch<React.SetStateAction<SelectedCell | null>>;
anchorEl: HTMLElement | null;
setAnchorEl: React.Dispatch<
React.SetStateAction<PopoverProps["anchorEl"] | null>
>;
};

export default function CellMenu() {
const { cellMenuRef }: any = useProjectContext();
const { cellMenuRef, tableState, updateCell }: any = useProjectContext();
const [anchorEl, setAnchorEl] = React.useState<any | null>(null);
const [selectedCell, setSelectedCell] = React.useState<any | null>();
const [cellMenuData, setCellMenuData] = useAtom(cellMenuDataAtom);
const open = Boolean(anchorEl);

if (cellMenuRef)
cellMenuRef.current = {
anchorEl,
setAnchorEl,
selectedCell,
setSelectedCell,
} as {};

const handleClose = () => setAnchorEl(null);
const handleCopy = () => {
const selected: any = anchorEl?.innerHTML;
const copy = navigator.clipboard.writeText(selected);
const onSuccess = () => {
console.log("Copied");
var promise = navigator.clipboard.readText();
promise.then((text) => console.log(text));
};
const cols = _find(tableState.columns, { index: selectedCell.colIndex });
const rows = tableState.rows[selectedCell.rowIndex];
const cell = rows[cols.key];
const formatData = typeof cell === "object" ? JSON.stringify(cell) : cell;
setCellMenuData(formatData);
const onFail = () => console.log("Fail to copy");
const onSuccess = () => console.log;

const copy = navigator.clipboard.writeText(formatData);
copy.then(onSuccess, onFail);

handleClose();
};
const handlePaste = () => {
const targetCol = _find(tableState.columns, {
index: selectedCell.colIndex,
});
const targetRow = tableState.rows[selectedCell.rowIndex];
if (cellMenuData) updateCell(targetRow.ref, targetCol.key, cellMenuData);
handleClose();
};

const handlePasteFromClipboard = () => {
const paste = navigator.clipboard.readText();
const targetCol = _find(tableState.columns, {
index: selectedCell.colIndex,
});
const targetRow = tableState.rows[selectedCell.rowIndex];
paste.then((clipText) =>
updateCell(targetRow.ref, targetCol.key, clipText)
);
};

const cellMenuAction = [
{ label: "Copy", icon: <CopyCells />, color: "", onClick: handleCopy },
{ label: "Copy", icon: <CopyCells />, onClick: handleCopy },
{ label: "Paste", icon: <Paste />, onClick: handlePaste },
{
label: "Paste from Clipboard",
icon: <PasteFromClipboard />,
onClick: handlePasteFromClipboard,
},
];

if (!cellMenuRef.current || !open) return <></>;
Expand Down
2 changes: 1 addition & 1 deletion src/components/Table/TableRow.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useProjectContext } from "@src/contexts/ProjectContext";
import { Fragment, useEffect } from "react";
import { Fragment } from "react";
import { Row, RowRendererProps } from "react-data-grid";

import OutOfOrderIndicator from "./OutOfOrderIndicator";
Expand Down
16 changes: 14 additions & 2 deletions src/components/Table/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,14 @@ const rowClass = (row: any) => (row._rowy_outOfOrder ? "out-of-order" : "");
//const SelectColumn = { ..._SelectColumn, width: 42, maxWidth: 42 };

export default function Table() {
const { tableState, tableActions, dataGridRef, sideDrawerRef, updateCell } =
useProjectContext();
const {
tableState,
tableActions,
dataGridRef,
cellMenuRef,
sideDrawerRef,
updateCell,
} = useProjectContext();
const { userDoc } = useAppContext();

const userDocHiddenFields =
Expand Down Expand Up @@ -246,6 +252,12 @@ export default function Table() {
});
}
}}
onSelectedCellChange={({ rowIdx, idx }) => {
cellMenuRef?.current?.setSelectedCell({
rowIndex: rowIdx,
colIndex: idx,
});
}}
/>
</DndProvider>
) : (
Expand Down

0 comments on commit c9e7c9f

Please # to comment.