All files / src/pages/flowsheet-page/flowsheet/Canvas/Nodes HelperFunctions.tsx

91.48% Statements 43/47
49.05% Branches 26/53
93.75% Functions 15/16
95% Lines 38/40

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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417      45x 45x 45x 45x 45x 45x 45x 45x 45x           2259x 2159x 2159x 1580x       2259x 1674x 1615x       9893x                   9893x                           9893x                                                     5798x 5798x                                                             59x   472x       59x                                                                             257x                                                           2259x 2259x                                                                   257x 257x   54x                                                   194x   194x                                                                                                                 514x                                                                               59x   59x           59x                                                                                   3901x 3901x                            
import React from "react";
import { GenericObjectGraphicProps } from "@/hooks/graphicTypes.ts";
 
const lightGrayColour = "#aaaaaa";
const tealColour = "#26967f";
const blueColour = "#1077c4";
const darkGrayColour = "#404040";
const lightFill = "#E9EDF0";
const lightHoverFill = "#f2f3f5";
const yellowColour = "#eab308";
const whiteColour = "#FFFFFF";
const blackColour = "#000000";
 
export function getStrokeColor(
  props: GenericObjectGraphicProps,
  highlightStroke: boolean,
) {
  if (props.disabled) return lightGrayColour;
  Iif (highlightStroke) return tealColour;
  if (props.isSelected) return blueColour;
  return darkGrayColour;
}
 
export function getFillColor(props: GenericObjectGraphicProps) {
  if (props.isSelected) return lightFill;
  if (props.isHovered) return lightHoverFill;
  return whiteColour;
}
 
export function strokeColor(props: GenericObjectGraphicProps) {
  return props.disabled
    ? lightGrayColour
    : props.highlight
      ? tealColour
      : props.isSelected
        ? blueColour
        : darkGrayColour;
}
 
export function fillColor(props: GenericObjectGraphicProps) {
  return props.isSelected || props.isHovered ? lightFill : whiteColour;
}
 
export function Circle({
  size,
  fill,
  stroke,
  strokeWidth = 2,
}: {
  size: number;
  fill: string;
  stroke: string;
  strokeWidth?: number;
}) {
  return (
    <div
      style={{
        position: "absolute",
        inset: 0,
        width: size,
        height: size,
        borderRadius: 9999,
        background: fill,
        border: `${strokeWidth}px solid ${stroke}`,
        boxSizing: "border-box",
      }}
    />
  );
}
 
export function FeedIcon({
  src,
  size,
  rotation = 0,
  flipped = false,
}: {
  src: string;
  size: number;
  rotation?: number;
  flipped?: boolean;
}) {
  const scale = flipped ? "scale(-1, -1)" : "scale(1, 1)";
  return (
    <img
      src={src}
      alt=""
      draggable={false}
      style={{
        position: "absolute",
        left: "50%",
        top: "50%",
        width: size,
        height: size,
        transform: `translate(-50%, -50%) rotate(${rotation}deg) ${scale}`,
        transformOrigin: "center",
        pointerEvents: "none",
        userSelect: "none",
      }}
    />
  );
}
 
export function Line({
  points,
  fill,
  stroke,
  strokeWidth = 2,
}: {
  points: number[];
  fill: string;
  stroke: string;
  strokeWidth?: number;
}) {
  const pointsStr = points
    .reduce((acc, val, index) => {
      return index % 2 === 0 ? acc + `${val},` : acc + `${val} `;
    }, "")
    .trim();
 
  return (
    <svg
      style={{
        position: "absolute",
        inset: 0,
        width: "100%",
        height: "100%",
        pointerEvents: "none",
        userSelect: "none",
      }}
    >
      <polygon
        points={pointsStr}
        fill={fill}
        stroke={stroke}
        strokeWidth={strokeWidth}
      />
    </svg>
  );
}
export function Box({
  width,
  height,
  fill,
  stroke,
  strokeWidth = 2,
  radius = 2,
  onDoubleClick,
  children,
}: {
  width: number;
  height: number;
  fill: string;
  stroke: string;
  strokeWidth?: number;
  radius?: number;
  onDoubleClick?: () => void;
  children?: React.ReactNode;
}) {
  return (
    <div
      onDoubleClick={onDoubleClick}
      style={{
        width,
        height,
        background: fill,
        border: `${strokeWidth}px solid ${stroke}`,
        borderRadius: radius,
        boxSizing: "border-box",
        position: "relative",
        display: "flex",
        alignItems: "center",
        justifyContent: "center",
        userSelect: "none",
      }}
    >
      {children}
    </div>
  );
}
export function NodeLabel({
  text,
  width,
  y,
}: {
  text: string;
  width: number;
  y: number;
}) {
  const darkGrayColour = "#404040";
  return (
    <div
      style={{
        position: "absolute",
        left: 0,
        top: y,
        width,
        textAlign: "center",
        fontSize: 16,
        fontFamily: "Inter",
        color: darkGrayColour,
        whiteSpace: "nowrap",
        overflow: "hidden",
        textOverflow: "ellipsis",
        pointerEvents: "none",
        userSelect: "none",
        letterSpacing: -0.2,
        fontWeight: "500",
      }}
    >
      {text}
    </div>
  );
}
 
export function ResizeHandle({
  size = 12,
  onMouseDown,
  visible = true,
}: {
  size?: number;
  onMouseDown: (e: React.MouseEvent) => void;
  visible?: boolean;
}) {
  const blueColour = "#1077c4";
  if (!visible) return null;
 
  return (
    <div
      data-testid="resize-handle"
      aria-label="Resize Handle"
      onMouseDown={onMouseDown}
      style={{
        position: "absolute",
        right: 0,
        bottom: 0,
        width: size,
        height: size,
        background: blueColour,
        borderRadius: 3,
        cursor: "nwse-resize",
      }}
    />
  );
}
 
export function ZapIcon({
  sizePercent = 50,
  fill = yellowColour,
}: {
  sizePercent?: number;
  fill?: string;
}) {
  const zapPath = "M13 2L3 14h9l-1 8 10-12h-9l1-8z";
 
  return (
    <svg
      viewBox="0 0 24 24"
      draggable={false}
      style={{
        width: `${sizePercent}%`,
        height: `${sizePercent}%`,
        objectFit: "contain",
        pointerEvents: "none",
        userSelect: "none",
      }}
    >
      <path d={zapPath} fill={fill} />
    </svg>
  );
}
 
export function CenteredIcon({
  src,
  sizePercent = 50,
}: {
  src?: string;
  sizePercent?: number;
}) {
  Iif (!src) return null;
 
  return (
    <img
      src={src}
      alt=""
      draggable={false}
      style={{
        width: `${sizePercent}%`,
        height: `${sizePercent}%`,
        objectFit: "contain",
        pointerEvents: "none",
        userSelect: "none",
      }}
    />
  );
}
 
export function Overlay({
  left,
  top,
  right,
  bottom,
  children,
  style,
}: {
  left?: number;
  top?: number;
  right?: number;
  bottom?: number;
  children: React.ReactNode;
  style?: React.CSSProperties;
}) {
  return (
    <div
      style={{
        position: "absolute",
        left,
        top,
        right,
        bottom,
        ...style,
      }}
    >
      {children}
    </div>
  );
}
 
export function Text({
  listening = false,
  align = "center",
  verticalAlign = "middle",
  width,
  height,
  text,
  fontSize = 14,
  fontFamily = "Inter",
  fill = blackColour,
  fontStyle = "normal",
}: {
  listening?: boolean;
  align?: "left" | "center" | "right";
  verticalAlign?: "top" | "middle" | "bottom";
  width: number;
  height: number;
  text: string;
  fontSize?: number;
  fontFamily?: string;
  fill?: string;
  fontStyle?: string;
}) {
  const justifyContent =
    align === "left" ? "flex-start" : align === "right" ? "flex-end" : "center";
  const alignItems =
    verticalAlign === "top"
      ? "flex-start"
      : verticalAlign === "bottom"
        ? "flex-end"
        : "center";
 
  return (
    <div
      style={{
        position: "absolute",
        left: 0,
        top: 0,
        width,
        height,
        display: "flex",
        justifyContent,
        alignItems,
        fontSize,
        fontFamily,
        color: fill,
        fontStyle,
        pointerEvents: listening ? "auto" : "none",
        userSelect: listening ? "auto" : "none",
        padding: 4,
        boxSizing: "border-box",
      }}
    >
      <span
        style={{
          whiteSpace: "nowrap",
          overflow: "hidden",
          textOverflow: "ellipsis",
        }}
      >
        {text}
      </span>
    </div>
  );
}
export function InnerCircle({
  size,
  inset = 5,
  fill = blackColour,
}: {
  size: number;
  inset?: number;
  fill?: string;
}) {
  const inner = Math.max(0, size - inset * 2);
  return (
    <div
      style={{
        position: "absolute",
        left: inset,
        top: inset,
        width: inner,
        height: inner,
        borderRadius: 9999,
        background: fill,
      }}
    />
  );
}