2020-01-07 07:50:59 +05:00
|
|
|
import React from "react";
|
|
|
|
import { render, unmountComponentAtNode } from "react-dom";
|
2020-10-19 17:14:28 +03:00
|
|
|
import clsx from "clsx";
|
|
|
|
import { Popover } from "./Popover";
|
2020-01-07 07:50:59 +05:00
|
|
|
|
2020-04-10 18:09:29 -04:00
|
|
|
import "./ContextMenu.scss";
|
2020-12-12 23:03:58 +01:00
|
|
|
import {
|
|
|
|
getShortcutFromShortcutName,
|
|
|
|
ShortcutName,
|
|
|
|
} from "../actions/shortcuts";
|
2020-01-07 07:50:59 +05:00
|
|
|
|
|
|
|
type ContextMenuOption = {
|
2020-12-20 23:20:03 +01:00
|
|
|
checked?: boolean;
|
2020-12-12 23:03:58 +01:00
|
|
|
shortcutName: ShortcutName;
|
2020-01-07 07:50:59 +05:00
|
|
|
label: string;
|
|
|
|
action(): void;
|
|
|
|
};
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
options: ContextMenuOption[];
|
|
|
|
onCloseRequest?(): void;
|
|
|
|
top: number;
|
|
|
|
left: number;
|
|
|
|
};
|
|
|
|
|
2020-09-26 02:48:45 +05:30
|
|
|
const ContextMenu = ({ options, onCloseRequest, top, left }: Props) => {
|
|
|
|
const isDarkTheme = !!document
|
|
|
|
.querySelector(".excalidraw")
|
|
|
|
?.classList.contains("Appearance_dark");
|
|
|
|
return (
|
2020-10-19 17:14:28 +03:00
|
|
|
<div
|
|
|
|
className={clsx("excalidraw", {
|
|
|
|
"Appearance_dark Appearance_dark-background-none": isDarkTheme,
|
|
|
|
})}
|
|
|
|
>
|
2020-09-26 02:48:45 +05:30
|
|
|
<Popover
|
|
|
|
onCloseRequest={onCloseRequest}
|
|
|
|
top={top}
|
|
|
|
left={left}
|
|
|
|
fitInViewport={true}
|
|
|
|
>
|
|
|
|
<ul
|
|
|
|
className="context-menu"
|
|
|
|
onContextMenu={(event) => event.preventDefault()}
|
|
|
|
>
|
2020-12-20 23:20:03 +01:00
|
|
|
{options.map(({ action, checked, shortcutName, label }, idx) => (
|
2020-12-12 23:03:58 +01:00
|
|
|
<li data-testid={shortcutName} key={idx} onClick={onCloseRequest}>
|
2020-12-15 04:29:00 +05:30
|
|
|
<button
|
2020-12-20 23:20:03 +01:00
|
|
|
className={`context-menu-option
|
|
|
|
${shortcutName === "delete" ? "dangerous" : ""}
|
|
|
|
${checked ? "checkmark" : ""}`}
|
2020-12-15 04:29:00 +05:30
|
|
|
onClick={action}
|
|
|
|
>
|
2020-12-27 00:53:51 +05:30
|
|
|
<div className="context-menu-option__label">{label}</div>
|
2021-01-14 12:54:32 +05:30
|
|
|
<kbd className="context-menu-option__shortcut">
|
2020-12-12 23:03:58 +01:00
|
|
|
{shortcutName
|
|
|
|
? getShortcutFromShortcutName(shortcutName)
|
|
|
|
: ""}
|
2021-01-14 12:54:32 +05:30
|
|
|
</kbd>
|
2020-10-28 20:53:27 +01:00
|
|
|
</button>
|
2020-09-26 02:48:45 +05:30
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</Popover>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2020-01-07 07:50:59 +05:00
|
|
|
|
|
|
|
let contextMenuNode: HTMLDivElement;
|
2020-05-20 16:21:37 +03:00
|
|
|
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-05-20 16:21:37 +03:00
|
|
|
};
|
2020-01-07 07:50:59 +05:00
|
|
|
|
|
|
|
type ContextMenuParams = {
|
|
|
|
options: (ContextMenuOption | false | null | undefined)[];
|
|
|
|
top: number;
|
|
|
|
left: number;
|
|
|
|
};
|
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
const handleClose = () => {
|
2020-01-07 07:50:59 +05:00
|
|
|
unmountComponentAtNode(getContextMenuNode());
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-01-07 07:50:59 +05:00
|
|
|
|
|
|
|
export default {
|
|
|
|
push(params: ContextMenuParams) {
|
|
|
|
const options = Array.of<ContextMenuOption>();
|
2020-03-23 13:05:07 +02:00
|
|
|
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}
|
|
|
|
/>,
|
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
|
|
|
};
|