Merge branch 'right-click-menu' into righ-click-v2

This commit is contained in:
Gibson Han
2022-02-02 22:18:03 +07:00
11 changed files with 156 additions and 121 deletions

18
src/atoms/ContextMenu.ts Normal file
View File

@@ -0,0 +1,18 @@
import { atomWithReset } from "jotai/utils";
export type SelectedCell = {
rowIndex: number;
colIndex: number;
};
export interface IContextMenuAtom {
selectedCell: SelectedCell | null;
anchorEl: HTMLElement | null;
}
const INIT_VALUE = {
selectedCell: null,
anchorEl: null,
};
export const contextMenuAtom = atomWithReset<IContextMenuAtom>(INIT_VALUE);

View File

@@ -1,11 +1,12 @@
import { Menu } from "@mui/material";
import MenuRow, { IMenuRow } from "./MenuRow";
import { Menu, MenuList } from "@mui/material";
import { default as MenuItem } from "./MenuItem";
import { IContextMenuItem } from "./MenuItem";
interface IMenuContents {
anchorEl: HTMLElement;
open: boolean;
handleClose: () => void;
items: IMenuRow[];
items: IContextMenuItem[];
}
export function MenuContents({
@@ -31,13 +32,21 @@ export function MenuContents({
vertical: "top",
horizontal: "left",
}}
sx={{ "& .MuiMenu-paper": { backgroundColor: "background.default" } }}
sx={{
"& .MuiMenu-paper": {
backgroundColor: "background.default",
width: 200,
maxWidth: "100%",
},
}}
MenuListProps={{ disablePadding: true }}
onContextMenu={handleContext}
>
{items.map((item, indx: number) => (
<MenuRow key={indx} {...item} />
))}
<MenuList dense>
{items.map((item, indx: number) => (
<MenuItem key={indx} {...item} />
))}
</MenuList>
</Menu>
);
}

View File

@@ -0,0 +1,34 @@
import {
ListItemIcon,
ListItemText,
MenuItem,
Typography,
} from "@mui/material";
export interface IContextMenuItem {
onClick: () => void;
icon: JSX.Element;
label: string;
disabled?: boolean;
hotkeyLabel?: string;
}
export default function ContextMenuItem({
onClick,
icon,
label,
disabled,
hotkeyLabel,
}: IContextMenuItem) {
return (
<MenuItem disabled={disabled} onClick={onClick}>
<ListItemIcon>{icon} </ListItemIcon>
<ListItemText> {label} </ListItemText>
{hotkeyLabel && (
<Typography variant="body2" color="text.secondary">
{hotkeyLabel}
</Typography>
)}
</MenuItem>
);
}

View File

@@ -1,17 +0,0 @@
import { ListItemIcon, ListItemText, MenuItem } from "@mui/material";
export interface IMenuRow {
onClick: () => void;
icon: JSX.Element;
label: string;
disabled?: boolean;
}
export default function MenuRow({ onClick, icon, label, disabled }: IMenuRow) {
return (
<MenuItem disabled={disabled} onClick={onClick}>
<ListItemIcon>{icon} </ListItemIcon>
<ListItemText> {label} </ListItemText>
</MenuItem>
);
}

View File

@@ -1,56 +1,28 @@
import React from "react";
import _find from "lodash/find";
import { PopoverProps } from "@mui/material";
import { getFieldProp } from "@src/components/fields";
import { useProjectContext } from "@src/contexts/ProjectContext";
import useContextMenuAtom from "@src/hooks/useContextMenuAtom";
import { MenuContents } from "./MenuContent";
export type SelectedCell = {
rowIndex: number;
colIndex: number;
};
export type ContextMenuRef = {
selectedCell: SelectedCell;
setSelectedCell: React.Dispatch<React.SetStateAction<SelectedCell | null>>;
anchorEl: HTMLElement | null;
setAnchorEl: React.Dispatch<
React.SetStateAction<PopoverProps["anchorEl"] | null>
>;
};
export default function ContextMenu() {
const { contextMenuRef, tableState }: any = useProjectContext();
const [anchorEl, setAnchorEl] = React.useState<any | null>(null);
const [selectedCell, setSelectedCell] = React.useState<any | null>();
const open = Boolean(anchorEl);
const handleClose = () => setAnchorEl(null);
if (contextMenuRef)
contextMenuRef.current = {
anchorEl,
setAnchorEl,
selectedCell,
setSelectedCell,
} as {};
const { tableState }: any = useProjectContext();
const { contextMenu, resetContextMenu } = useContextMenuAtom();
const { anchorEl, selectedCell } = contextMenu;
const columns = tableState?.columns;
const selectedColIndex = selectedCell?.colIndex;
const selectedCol = _find(tableState?.columns, { index: selectedColIndex });
const getActions =
const selectedCol = _find(columns, { index: selectedColIndex });
const configActions =
getFieldProp("contextMenuActions", selectedCol?.type) ||
function empty() {};
const actions = getActions() || [];
const actions = configActions(selectedCell, resetContextMenu) || [];
const hasNoActions = Boolean(actions.length === 0);
if (!contextMenuRef.current || hasNoActions) return null;
if (!contextMenu || hasNoActions) return <></>;
return (
<MenuContents
anchorEl={anchorEl}
open={open}
handleClose={handleClose}
anchorEl={anchorEl as HTMLElement}
open={Boolean(contextMenu.anchorEl)}
handleClose={resetContextMenu}
items={actions}
/>
);

View File

@@ -1,25 +1,26 @@
import { useProjectContext } from "@src/contexts/ProjectContext";
import useContextMenuAtom from "@src/hooks/useContextMenuAtom";
import { Fragment } from "react";
import { Row, RowRendererProps } from "react-data-grid";
import OutOfOrderIndicator from "./OutOfOrderIndicator";
export default function TableRow(props: RowRendererProps<any>) {
const { contextMenuRef }: any = useProjectContext();
const handleContextMenu = (
e: React.MouseEvent<HTMLDivElement, MouseEvent>
) => {
const { setContextMenu } = useContextMenuAtom();
function handleContextMenu(e: React.MouseEvent<HTMLDivElement, MouseEvent>) {
e.preventDefault();
if (contextMenuRef?.current) contextMenuRef?.current?.setAnchorEl(e.target);
};
if (setContextMenu)
setContextMenu((prev) => ({
...prev,
anchorEl: e?.target as HTMLElement,
}));
}
if (props.row._rowy_outOfOrder)
return (
<Fragment key={props.row.id}>
<OutOfOrderIndicator top={props.top} height={props.height} />
<Row {...props} onContextMenu={handleContextMenu} />
<Row onContextMenu={(e) => handleContextMenu(e)} {...props} />
</Fragment>
);
return <Row {...props} onContextMenu={handleContextMenu} />;
return <Row onContextMenu={(e) => handleContextMenu(e)} {...props} />;
}

View File

@@ -32,6 +32,7 @@ import { formatSubTableName } from "@src/utils/fns";
import { useAppContext } from "@src/contexts/AppContext";
import { useProjectContext } from "@src/contexts/ProjectContext";
import useWindowSize from "@src/hooks/useWindowSize";
import useContextMenuAtom from "@src/hooks/useContextMenuAtom";
export type TableColumn = Column<any> & {
isNew?: boolean;
@@ -49,11 +50,11 @@ export default function Table() {
tableState,
tableActions,
dataGridRef,
contextMenuRef,
sideDrawerRef,
updateCell,
} = useProjectContext();
const { userDoc, userClaims } = useAppContext();
const { setContextMenu } = useContextMenuAtom();
const userDocHiddenFields =
userDoc.state.doc?.tables?.[formatSubTableName(tableState?.config.id)]
@@ -265,10 +266,13 @@ export default function Table() {
}
}}
onSelectedCellChange={({ rowIdx, idx }) => {
contextMenuRef?.current?.setSelectedCell({
rowIndex: rowIdx,
colIndex: idx,
});
setContextMenu((prev) => ({
...prev,
selectedCell: {
rowIndex: rowIdx,
colIndex: idx,
},
}));
}}
/>
</DndProvider>

View File

@@ -5,56 +5,56 @@ import CopyCells from "@src/assets/icons/CopyCells";
import Paste from "@mui/icons-material/ContentPaste";
import { useProjectContext } from "@src/contexts/ProjectContext";
import { useSnackbar } from "notistack";
import { SelectedCell } from "@src/atoms/ContextMenu";
export default function BasicContextMenuActions() {
const { contextMenuRef, tableState, deleteCell, updateCell } =
useProjectContext();
export interface IContextMenuActions {
label: string;
icon: React.ReactNode;
onClick: () => void;
}
export default function BasicContextMenuActions(
selectedCell: SelectedCell,
reset: () => void | Promise<void>
): IContextMenuActions[] {
const { tableState, deleteCell, updateCell } = useProjectContext();
const { enqueueSnackbar } = useSnackbar();
const columns = tableState?.columns;
const rows = tableState?.rows;
const selectedRowIndex = contextMenuRef?.current?.selectedCell
.rowIndex as number;
const selectedColIndex = contextMenuRef?.current?.selectedCell?.colIndex;
const selectedRowIndex = selectedCell.rowIndex as number;
const selectedColIndex = selectedCell?.colIndex;
const selectedCol = _find(columns, { index: selectedColIndex });
const selectedRow = rows?.[selectedRowIndex];
const cell = selectedRow?.[selectedCol.key];
const handleClose = () => {
contextMenuRef?.current?.setSelectedCell(null);
contextMenuRef?.current?.setAnchorEl(null);
};
const handleCopy = () => {
const cell = selectedRow?.[selectedCol.key];
// const onFail = () => console.log("Fail to copy");
// const onSuccess = () => console.log("Save to clipboard successful");
// const copy =
navigator.clipboard.writeText(JSON.stringify(cell));
// copy.then(onSuccess, onFail);
const handleClose = async () => await reset?.();
const handleCopy = async () => {
try {
await navigator.clipboard.writeText(JSON.stringify(cell));
enqueueSnackbar("Copied to clipboard", { variant: "success" });
} catch (error) {
enqueueSnackbar("Failed to copy", { variant: "error" });
}
handleClose();
};
const handleCut = () => {
const cell = selectedRow?.[selectedCol.key];
const notUndefined = Boolean(typeof cell !== "undefined");
if (deleteCell && notUndefined)
deleteCell(selectedRow?.ref, selectedCol?.key);
handleClose();
};
const handlePaste = () => {
// console.log("home", rows);
const paste = navigator.clipboard.readText();
paste.then(async (clipText) => {
try {
const paste = await JSON.parse(clipText);
updateCell?.(selectedRow?.ref, selectedCol.key, paste);
} catch (error) {
//TODO check the coding style guide about error message
//Add breadcrumb handler her
// console.log(error);
}
});
const handlePaste = async () => {
try {
const text = await navigator.clipboard.readText();
const paste = await JSON.parse(text);
updateCell?.(selectedRow?.ref, selectedCol.key, paste);
} catch (error) {
enqueueSnackbar(`${error}`, { variant: "error" });
}
handleClose();
};
@@ -64,10 +64,11 @@ export default function BasicContextMenuActions() {
// return typeof cell === "undefined" ? true : false;
// };
const cellMenuAction = [
const contextMenuActions = [
{ label: "Cut", icon: <Cut />, onClick: handleCut },
{ label: "Copy", icon: <CopyCells />, onClick: handleCopy },
{ label: "Paste", icon: <Paste />, onClick: handlePaste },
];
return cellMenuAction;
return contextMenuActions;
}

View File

@@ -1,8 +1,9 @@
import { FieldType } from "@src/constants/fields";
import { FormatterProps, EditorProps } from "react-data-grid";
import { Control, UseFormReturn } from "react-hook-form";
import { PopoverProps } from "@mui/material";
import { SelectedCell } from "@src/atoms/ContextMenu";
import { IContextMenuActions } from "./_BasicCell/BasicCellContextMenuActions";
export { FieldType };
@@ -17,7 +18,10 @@ export interface IFieldConfig {
icon?: React.ReactNode;
description?: string;
setupGuideLink?: string;
contextMenuActions?: () => void;
contextMenuActions?: (
selectedCell: SelectedCell,
reset: () => void | Promise<void>
) => IContextMenuActions[];
TableCell: React.ComponentType<FormatterProps<any>>;
TableEditor: React.ComponentType<EditorProps<any, any>>;
SideDrawerField: React.ComponentType<ISideDrawerFieldProps>;

View File

@@ -14,7 +14,6 @@ import useSettings from "@src/hooks/useSettings";
import { useAppContext } from "./AppContext";
import { SideDrawerRef } from "@src/components/SideDrawer";
import { ColumnMenuRef } from "@src/components/Table/ColumnMenu";
import { ContextMenuRef } from "@src/components/Table/ContextMenu";
import { ImportWizardRef } from "@src/components/Wizards/ImportWizard";
import { rowyRun, IRowyRunRequestProps } from "@src/utils/rowyRun";
@@ -105,8 +104,6 @@ export interface IProjectContext {
dataGridRef: React.RefObject<DataGridHandle>;
// A ref to the side drawer state. Prevents unnecessary re-renders
sideDrawerRef: React.MutableRefObject<SideDrawerRef | undefined>;
//A ref to the cell menu. Prevents unnecessary re-render
contextMenuRef: React.MutableRefObject<ContextMenuRef | undefined>;
// A ref to the column menu. Prevents unnecessary re-renders
columnMenuRef: React.MutableRefObject<ColumnMenuRef | undefined>;
// A ref ot the import wizard. Prevents unnecessary re-renders
@@ -401,7 +398,6 @@ export const ProjectContextProvider: React.FC = ({ children }) => {
// A ref to the data grid. Contains data grid functions
const dataGridRef = useRef<DataGridHandle>(null);
const sideDrawerRef = useRef<SideDrawerRef>();
const contextMenuRef = useRef<ContextMenuRef>();
const columnMenuRef = useRef<ColumnMenuRef>();
const importWizardRef = useRef<ImportWizardRef>();
@@ -422,7 +418,6 @@ export const ProjectContextProvider: React.FC = ({ children }) => {
table,
dataGridRef,
sideDrawerRef,
contextMenuRef,
columnMenuRef,
importWizardRef,
rowyRun: _rowyRun,

View File

@@ -0,0 +1,14 @@
import { useAtom } from "jotai";
import { useResetAtom } from "jotai/utils";
import { contextMenuAtom } from "@src/atoms/ContextMenu";
export default function useContextMenuAtom() {
const [contextMenu, setContextMenu] = useAtom(contextMenuAtom);
const resetContextMenu = useResetAtom(contextMenuAtom);
return {
contextMenu,
setContextMenu,
resetContextMenu,
};
}