excalidraw/src/components/ContextMenu.tsx

133 lines
3.5 KiB
TypeScript
Raw Normal View History

2020-01-07 07:50:59 +05:00
import React from "react";
import { render, unmountComponentAtNode } from "react-dom";
import clsx from "clsx";
import { Popover } from "./Popover";
import { t } from "../i18n";
2020-01-07 07:50:59 +05:00
2020-04-10 18:09:29 -04:00
import "./ContextMenu.scss";
import {
getShortcutFromShortcutName,
ShortcutName,
} from "../actions/shortcuts";
import { Action } from "../actions/types";
import { ActionManager } from "../actions/manager";
import { AppState } from "../types";
2020-01-07 07:50:59 +05:00
export type ContextMenuOption = "separator" | Action;
2020-01-07 07:50:59 +05:00
type ContextMenuProps = {
2020-01-07 07:50:59 +05:00
options: ContextMenuOption[];
onCloseRequest?(): void;
top: number;
left: number;
actionManager: ActionManager;
appState: Readonly<AppState>;
2020-01-07 07:50:59 +05:00
};
const ContextMenu = ({
options,
onCloseRequest,
top,
left,
actionManager,
appState,
}: ContextMenuProps) => {
const isDarkTheme = !!document
.querySelector(".excalidraw")
?.classList.contains("Appearance_dark");
return (
<div
className={clsx("excalidraw", {
"Appearance_dark Appearance_dark-background-none": isDarkTheme,
})}
>
<Popover
onCloseRequest={onCloseRequest}
top={top}
left={left}
fitInViewport={true}
>
<ul
className="context-menu"
onContextMenu={(event) => event.preventDefault()}
>
{options.map((option, idx) => {
if (option === "separator") {
return <hr key={idx} className="context-menu-option-separator" />;
}
const actionName = option.name;
const label = option.contextItemLabel
? t(option.contextItemLabel)
: "";
return (
<li key={idx} data-testid={actionName} onClick={onCloseRequest}>
<button
className={clsx("context-menu-option", {
dangerous: actionName === "deleteSelectedElements",
checkmark: option.checked?.(appState),
})}
onClick={() => actionManager.executeAction(option)}
>
<div className="context-menu-option__label">{label}</div>
<kbd className="context-menu-option__shortcut">
{actionName
? getShortcutFromShortcutName(actionName as ShortcutName)
: ""}
</kbd>
</button>
</li>
);
})}
</ul>
</Popover>
</div>
);
};
2020-01-07 07:50:59 +05:00
let contextMenuNode: HTMLDivElement;
const getContextMenuNode = (): HTMLDivElement => {
2020-01-07 07:50:59 +05:00
if (contextMenuNode) {
return contextMenuNode;
}
const div = document.createElement("div");
document.body.appendChild(div);
return (contextMenuNode = div);
};
2020-01-07 07:50:59 +05:00
type ContextMenuParams = {
options: (ContextMenuOption | false | null | undefined)[];
top: ContextMenuProps["top"];
left: ContextMenuProps["left"];
actionManager: ContextMenuProps["actionManager"];
appState: Readonly<AppState>;
2020-01-07 07:50:59 +05:00
};
const handleClose = () => {
2020-01-07 07:50:59 +05:00
unmountComponentAtNode(getContextMenuNode());
};
2020-01-07 07:50:59 +05:00
export default {
push(params: ContextMenuParams) {
const options = Array.of<ContextMenuOption>();
params.options.forEach((option) => {
2020-01-07 07:50:59 +05:00
if (option) {
options.push(option);
}
});
if (options.length) {
render(
<ContextMenu
top={params.top}
left={params.left}
options={options}
onCloseRequest={handleClose}
actionManager={params.actionManager}
appState={params.appState}
2020-01-07 07:50:59 +05:00
/>,
2020-01-24 12:04:54 +02:00
getContextMenuNode(),
2020-01-07 07:50:59 +05:00
);
}
2020-01-24 12:04:54 +02:00
},
2020-01-07 07:50:59 +05:00
};