All files / src/pages/flowsheet-page/flowsheet/Canvas ContextMenu.tsx

88.88% Statements 24/27
88.09% Branches 37/42
84.61% Functions 11/13
92.3% Lines 24/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211                                                  62x                                                                                           62x                                             49x       48x 89x   48x 48x   1x     49x 49x           49x                 96x 96x               336x 336x             1x       4x   3x     3x         2x                                         5x                                 1x 1x       1x                          
import { Node } from "@xyflow/react";
import React from "react";
import { ContextMenuObject } from "./ContextMenuObject";
 
export interface Coordinates {
  x: number;
  y: number;
}
 
interface ContextMenuProps {
  nodes: Node[];
  position: Coordinates;
  onClick: () => void;
  canMutate: boolean;
  handleCopyObject: () => void;
  handlePasteObject: (position: Coordinates) => void;
  handleDeleteObject: () => void;
  handleGroupObjects: () => void;
  handleUngroupObject: () => void;
  handleRotateObjectLeft: (direction: string) => void;
  handleRotateObjectRight: (direction: string) => void;
  handleFlipObjectHorizontal: (direction: string) => void;
  handleFlipObjectVertical: (direction: string) => void;
}
 
const nodeClickedContextInfo = {
  general: {
    delete: {
      label: "Delete",
      shortcut: "Backspace / Delete",
      ariaLabel: "Delete object action",
    },
    copy: {
      label: "Copy",
      shortcut: "CTRL + C",
      ariaLabel: "Copy object action",
    },
    paste: {
      label: "Paste",
      shortcut: "CTRL + V",
      ariaLabel: "Paste object action",
    },
    rotateLeft: {
      label: "Rotate Left",
      ariaLabel: "Rotate object left action",
    },
    rotateRight: {
      label: "Rotate Right",
      ariaLabel: "Rotate object right action",
    },
    flipHorizontal: {
      label: "Flip Horizontally",
      ariaLabel: "Flip object horizontally action",
    },
    flipVertical: {
      label: "Flip Vertically",
      ariaLabel: "Flip object vertically action",
    },
  },
  group: {
    create: {
      label: "Pack",
      ariaLabel: "Merge elements action",
    },
    unpack: {
      label: "Unpack",
      ariaLabel: "Unpack merged element action",
    },
  },
};
 
const paneClickedContextInfo = {
  paste: {
    label: "Paste",
    shortcut: "CTRL + V",
    ariaLabel: "Paste object action",
  },
};
 
export function ContextMenu({
  nodes,
  position,
  onClick,
  canMutate,
  handleCopyObject,
  handlePasteObject,
  handleDeleteObject,
  handleGroupObjects,
  handleUngroupObject,
  handleRotateObjectLeft,
  handleRotateObjectRight,
  handleFlipObjectHorizontal,
  handleFlipObjectVertical,
}: ContextMenuProps) {
  Iif (!position) return;
  let groupCondition, itemClicked;
 
  if (nodes.length > 0) {
    const groupNode = nodes.find(
      (node) => node.data.simulationObject.objectType === "group",
    );
    groupCondition = groupNode ? "unpack" : "create";
    itemClicked = "nodeClicked";
  } else {
    itemClicked = "paneClicked";
  }
 
  const hasPaneActions = itemClicked === "paneClicked" && canMutate;
  const hasNodeActions = itemClicked === "nodeClicked";
 
  if (!hasPaneActions && !hasNodeActions) {
    return null;
  }
 
  return (
    <div
      style={{ top: position.y, left: position.x, position: "absolute" }}
      className={`z-100 w-64 min-w-[8rem] overflow-hidden rounded-md border bg-popover p-1 text-popover-foreground shadow-md`}
      onClick={onClick}
    >
      {itemClicked === "nodeClicked"
        ? Object.entries(nodeClickedContextInfo).map(
            ([contextGroup, contextGroupData], index, array) => {
              const isLastGroup = index === array.length - 1;
              return (
                <React.Fragment key={index}>
                  {contextGroup === "general" &&
                    Object.keys(contextGroupData).map((key) => {
                      if (!canMutate && key !== "copy") {
                        return null;
                      }
                      const subGroupData =
                        nodeClickedContextInfo[contextGroup][key];
                      return (
                        <ContextMenuObject
                          key={key}
                          onClick={
                            key === "delete"
                              ? handleDeleteObject
                              : key === "paste"
                                ? () => handlePasteObject(position)
                                : key === "copy"
                                  ? handleCopyObject
                                  : key === "rotateLeft"
                                    ? () => handleRotateObjectLeft("left")
                                    : key === "rotateRight"
                                      ? () => handleRotateObjectRight("right")
                                      : key === "flipHorizontal"
                                        ? () =>
                                            handleFlipObjectHorizontal(
                                              "horizontal",
                                            )
                                        : key === "flipVertical"
                                          ? () =>
                                              handleFlipObjectVertical(
                                                "vertical",
                                              )
                                          : () => {}
                          }
                          label={subGroupData.label}
                          shortcut={
                            subGroupData.shortcut
                              ? subGroupData.shortcut
                              : undefined
                          }
                          ariaLabel={subGroupData.ariaLabel}
                        ></ContextMenuObject>
                      );
                    })}
                  {contextGroup === "group" && canMutate && (
                    <ContextMenuObject
                      key={groupCondition}
                      onClick={
                        groupCondition === "unpack"
                          ? handleUngroupObject
                          : () => handleGroupObjects()
                      }
                      label={contextGroupData[groupCondition].label}
                      shortcut={contextGroupData[groupCondition].shortcut}
                      ariaLabel={contextGroupData[groupCondition].ariaLabel}
                      customClassName={""}
                    ></ContextMenuObject>
                  )}
                  {!isLastGroup && (
                    <div className="-mx-1 my-1 h-px bg-border" />
                  )}
                </React.Fragment>
              );
            },
          )
        : canMutate &&
          Object.keys(paneClickedContextInfo).map((key) => {
            const subGroupData = paneClickedContextInfo[key];
            return (
              <ContextMenuObject
                key={key}
                onClick={
                  key === "paste" ? () => handlePasteObject(position) : () => {}
                }
                label={subGroupData.label}
                shortcut={
                  subGroupData.shortcut ? subGroupData.shortcut : undefined
                }
                ariaLabel={subGroupData.ariaLabel}
              ></ContextMenuObject>
            );
          })}
    </div>
  );
}