excalidraw/src/components/ContextMenu.tsx

145 lines
4.1 KiB
TypeScript
Raw Normal View History

import { createRoot, Root } from "react-dom/client";
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";
import { NonDeletedExcalidrawElement } from "../element/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>;
elements: readonly NonDeletedExcalidrawElement[];
2020-01-07 07:50:59 +05:00
};
const ContextMenu = ({
options,
onCloseRequest,
top,
left,
actionManager,
appState,
elements,
}: ContextMenuProps) => {
return (
2021-04-05 17:26:37 +02:00
<Popover
onCloseRequest={onCloseRequest}
top={top}
left={left}
fitInViewport={true}
offsetLeft={appState.offsetLeft}
offsetTop={appState.offsetTop}
viewportWidth={appState.width}
viewportHeight={appState.height}
>
2021-04-05 17:26:37 +02:00
<ul
className="context-menu"
onContextMenu={(event) => event.preventDefault()}
>
2021-04-05 17:26:37 +02:00
{options.map((option, idx) => {
if (option === "separator") {
return <hr key={idx} className="context-menu-option-separator" />;
}
2021-04-05 17:26:37 +02:00
const actionName = option.name;
let label = "";
if (option.contextItemLabel) {
if (typeof option.contextItemLabel === "function") {
label = t(option.contextItemLabel(elements, appState));
} else {
label = t(option.contextItemLabel);
}
}
2021-04-05 17:26:37 +02:00
return (
<li key={idx} data-testid={actionName} onClick={onCloseRequest}>
<button
className={clsx("context-menu-option", {
dangerous: actionName === "deleteSelectedElements",
checkmark: option.checked?.(appState),
})}
2022-03-28 14:46:40 +02:00
onClick={() =>
actionManager.executeAction(option, "contextMenu")
}
2021-04-05 17:26:37 +02:00
>
<div className="context-menu-option__label">{label}</div>
<kbd className="context-menu-option__shortcut">
{actionName
? getShortcutFromShortcutName(actionName as ShortcutName)
: ""}
</kbd>
</button>
</li>
);
})}
</ul>
</Popover>
);
};
2020-01-07 07:50:59 +05:00
const contextMenuRoots = new WeakMap<HTMLElement, Root>();
2021-04-05 17:26:37 +02:00
const getContextMenuRoot = (container: HTMLElement): Root => {
let contextMenuRoot = contextMenuRoots.get(container);
if (contextMenuRoot) {
return contextMenuRoot;
2020-01-07 07:50:59 +05:00
}
contextMenuRoot = createRoot(
container.querySelector(".excalidraw-contextMenuContainer")!,
);
contextMenuRoots.set(container, contextMenuRoot);
return contextMenuRoot;
2020-01-07 07:50:59 +05:00
};
2021-04-05 17:26:37 +02:00
const handleClose = (container: HTMLElement) => {
const contextMenuRoot = contextMenuRoots.get(container);
if (contextMenuRoot) {
contextMenuRoot.unmount();
contextMenuRoots.delete(container);
2021-04-05 17:26:37 +02:00
}
};
2020-01-07 07:50:59 +05:00
export default {
push(params: {
options: (ContextMenuOption | false | null | undefined)[];
top: ContextMenuProps["top"];
left: ContextMenuProps["left"];
actionManager: ContextMenuProps["actionManager"];
appState: Readonly<AppState>;
container: HTMLElement;
elements: readonly NonDeletedExcalidrawElement[];
}) {
2020-01-07 07:50:59 +05:00
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) {
getContextMenuRoot(params.container).render(
2020-01-07 07:50:59 +05:00
<ContextMenu
top={params.top}
left={params.left}
options={options}
2021-04-05 17:26:37 +02:00
onCloseRequest={() => handleClose(params.container)}
actionManager={params.actionManager}
appState={params.appState}
elements={params.elements}
2020-01-07 07:50:59 +05:00
/>,
);
}
2020-01-24 12:04:54 +02:00
},
2020-01-07 07:50:59 +05:00
};