import { render, unmountComponentAtNode } from "react-dom"; import clsx from "clsx"; import { Popover } from "./Popover"; import { t } from "../i18n"; 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"; export type ContextMenuOption = "separator" | Action; type ContextMenuProps = { options: ContextMenuOption[]; onCloseRequest?(): void; top: number; left: number; actionManager: ActionManager; appState: Readonly; elements: readonly NonDeletedExcalidrawElement[]; }; const ContextMenu = ({ options, onCloseRequest, top, left, actionManager, appState, elements, }: ContextMenuProps) => { return ( ); }; const contextMenuNodeByContainer = new WeakMap(); const getContextMenuNode = (container: HTMLElement): HTMLDivElement => { let contextMenuNode = contextMenuNodeByContainer.get(container); if (contextMenuNode) { return contextMenuNode; } contextMenuNode = document.createElement("div"); container .querySelector(".excalidraw-contextMenuContainer")! .appendChild(contextMenuNode); contextMenuNodeByContainer.set(container, contextMenuNode); return contextMenuNode; }; type ContextMenuParams = { options: (ContextMenuOption | false | null | undefined)[]; top: ContextMenuProps["top"]; left: ContextMenuProps["left"]; actionManager: ContextMenuProps["actionManager"]; appState: Readonly; container: HTMLElement; elements: readonly NonDeletedExcalidrawElement[]; }; const handleClose = (container: HTMLElement) => { const contextMenuNode = contextMenuNodeByContainer.get(container); if (contextMenuNode) { unmountComponentAtNode(contextMenuNode); contextMenuNode.remove(); contextMenuNodeByContainer.delete(container); } }; export default { push(params: ContextMenuParams) { const options = Array.of(); params.options.forEach((option) => { if (option) { options.push(option); } }); if (options.length) { render( handleClose(params.container)} actionManager={params.actionManager} appState={params.appState} elements={params.elements} />, getContextMenuNode(params.container), ); } }, };