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

72.83% Statements 59/81
50.45% Branches 56/111
42.3% Functions 11/26
75.51% Lines 37/49

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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248                                                        76x                                                                                                               76x                       51x                                   36x   56x 36x     74x   72x 72x 72x 72x 324x 36x 74x   76x   19x 19x 38x     18x     18x 18x   1x 163x   36x 36x             36x 74x       36x   105x 105x   105x     105x                   280x                                                                     5x                                 2x                           396x 144x      
import { Node } from "@xyflow/react";
import React from "react";
import { isStreamType } from "../../../../lib/isStream";
import { ContextMenuObject } from "./ContextMenuObject";
 
export interface Coordinates {
  x: number;
  y: number;
}
 
interface ContextMenuProps {
  nodes: Node[];
  position: Coordinates;
  onClick: () => void;
  handleAutoSortCanvas: () => void | Promise<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;
  handleTrackStream: (node: Node) => 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",
    },
    autoSort: {
      label: "Auto-sort canvas",
      ariaLabel: "Auto-sort canvas 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",
    },
  },
  stream: {
    track: {
      label: "Track stream",
      ariaLabel: "Track stream action",
    },
  },
};
 
const paneClickedContextInfo = {
  paste: {
    label: "Paste",
    shortcut: "CTRL + V",
    ariaLabel: "Paste object action",
  },
  autoSort: {
    label: "Auto-sort canvas",
    ariaLabel: "Auto-sort canvas action",
  },
};
 
export function ContextMenu({
  nodes,
  position,
  onClick,
  handleAutoSortCanvas,
  canMutate,
  handleCopyObject,
  handlePasteObject,
  handleDeleteObject,
  handleGroupObjects,
  handleUngroupObject,
  handleRotateObjectLeft,
  handleRotateObjectRight,
  handleFlipObjectHorizontal,
  handleFlipObjectVertical,
  handleTrackStream,
}: ContextMenuProps) {
  Iif (!position) return;
  const runAutoSort = () => {
    void handleAutoSortCanvas();
  };
  const generalActions = {
    delete: handleDeleteObject,
    copy: handleCopyObject,
    paste: () => handlePasteObject(position),
    autoSort: runAutoSort,
    rotateLeft: () => handleRotateObjectLeft("left"),
    rotateRight: () => handleRotateObjectRight("right"),
    flipHorizontal: () => handleFlipObjectHorizontal("horizontal"),
    flipVertical: () => handleFlipObjectVertical("vertical"),
  } as const;
  const paneActions = {
    paste: () => handlePasteObject(position),
    autoSort: runAutoSort,
  } as const;
  let groupCondition, itemClicked;
  const selectedNode = nodes.length === 1 ? nodes[0] : undefined;
  const selectedObjectType = selectedNode?.data.simulationObject.objectType;
  const canTrackStream = isStreamType(selectedObjectType);
 
  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;
              const shouldShowSeparator =
                !isLastGroup &&
                ((contextGroup === "general" &&
                  (canTrackStream || canMutate)) ||
                  (contextGroup === "stream" && canTrackStream && canMutate));
 
              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={
                            generalActions[
                              key as keyof typeof generalActions
                            ] ?? (() => {})
                          }
                          label={subGroupData.label}
                          shortcut={
                            subGroupData.shortcut
                              ? subGroupData.shortcut
                              : undefined
                          }
                          ariaLabel={subGroupData.ariaLabel}
                        ></ContextMenuObject>
                      );
                    })}
                  {contextGroup === "stream" && canTrackStream && (
                    <ContextMenuObject
                      key="track"
                      onClick={() =>
                        selectedNode && handleTrackStream(selectedNode)
                      }
                      label={nodeClickedContextInfo.stream.track.label}
                      ariaLabel={nodeClickedContextInfo.stream.track.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>
                  )}
                  {shouldShowSeparator && (
                    <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={
                  paneActions[key as keyof typeof paneActions] ?? (() => {})
                }
                label={subGroupData.label}
                shortcut={
                  subGroupData.shortcut ? subGroupData.shortcut : undefined
                }
                ariaLabel={subGroupData.ariaLabel}
              ></ContextMenuObject>
            );
          })}
    </div>
  );
}