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

93.15% Statements 68/73
84.11% Branches 90/107
100% Functions 2/2
98.21% Lines 55/56

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                                          21500x       19218x               439x                                     5507x 5507x   5375x 5375x 5375x 5375x 5375x 5375x 5375x 5375x   5375x                                   34328x     34328x 34328x   34328x     34328x 53546x 53546x     24356x 24356x 24356x 24356x       13606x               105992x 13606x   78780x 13606x                   105992x 92386x         178x 178x 178x 178x 178x 598x       105x           493x 105x 178x 178x       178x           493x 598x           5507x         9794x 9794x         59357x 26315x 31822x       23380x               13057x                     101722x      
import { ObjectTypeEnum } from "@/api/apiStore.gen";
import {
  FLOWSHEET_PREVIEW_STROKE,
  type FlowsheetPreviewNode,
  type FlowsheetPreviewNodeLayout,
  type FlowsheetPreviewRenderEdge,
  getFlowsheetPreviewIconHref,
  getFlowsheetPreviewNodeKind,
} from "./flowsheetPreviewRendering.ts";
 
type FlowsheetPreviewNodeGlyphProps = {
  node: FlowsheetPreviewNode;
  layout: FlowsheetPreviewNodeLayout;
  strokeWidth?: number;
  iconScale?: number;
  opacity?: number | string;
  pointerEvents?: "none" | "auto";
  borderRadius?: number;
};
 
function roundSvgCoordinate(value: number) {
  return Math.round(value * 1000) / 1000;
}
 
function getNodeStyle(objectType: ObjectTypeEnum | string | undefined) {
  return {
    fill: "#ffffff",
    stroke: FLOWSHEET_PREVIEW_STROKE,
    radius: objectType === ObjectTypeEnum.Group ? 8 : 4,
  };
}
 
function renderEnergyIcon(x: number, y: number, size: number) {
  return (
    <path
      d="M13 2L3 14h9l-1 8 10-12h-9l1-8z"
      fill="#FFB300"
      pointerEvents="none"
      transform={`translate(${x} ${y}) scale(${size / 24})`}
    />
  );
}
 
function renderNodeIcon({
  node,
  layout,
  iconScale,
}: {
  node: FlowsheetPreviewNode;
  layout: FlowsheetPreviewNodeLayout;
  iconScale: number;
}) {
  const objectType = node.simulationObject.objectType;
  if (!objectType || objectType === ObjectTypeEnum.Header) return null;
 
  const iconWidth = roundSvgCoordinate(layout.width * iconScale);
  const iconHeight = roundSvgCoordinate(layout.height * iconScale);
  const iconX = roundSvgCoordinate(layout.width / 2 - iconWidth / 2);
  const iconY = roundSvgCoordinate(layout.height / 2 - iconHeight / 2);
  const rotation = node.rotation ?? 0;
  const flipped = !!(node.flipped ?? node.is_flipped);
  const scaleX = rotation === 0 || rotation === 180 ? (flipped ? -1 : 1) : 1;
  const scaleY = rotation === 90 || rotation === 270 ? (flipped ? -1 : 1) : 1;
 
  return (
    <g
      pointerEvents="none"
      transform={`translate(${layout.x + layout.width / 2} ${layout.y + layout.height / 2}) rotate(${rotation}) scale(${scaleX} ${scaleY}) translate(${-layout.width / 2} ${-layout.height / 2})`}
    >
      <image
        href={getFlowsheetPreviewIconHref(objectType)}
        x={iconX}
        y={iconY}
        width={iconWidth}
        height={iconHeight}
        preserveAspectRatio="xMidYMid meet"
        pointerEvents="none"
      />
    </g>
  );
}
 
export function FlowsheetPreviewNodeGlyph({
  node,
  layout,
  strokeWidth = 2,
  iconScale = 0.5,
  opacity,
  pointerEvents = "none",
  borderRadius,
}: FlowsheetPreviewNodeGlyphProps) {
  const objectType = node.simulationObject.objectType;
  const kind = getFlowsheetPreviewNodeKind(objectType);
  const style = getNodeStyle(objectType);
 
  if (kind === "stream" || kind === "decision") {
    const size = Math.min(layout.width, layout.height);
    const cx = layout.x + layout.width / 2;
    const cy = layout.y + layout.height / 2;
    const radius = Math.max(size / 2 - strokeWidth / 2, 1);
 
    return (
      <g opacity={opacity} pointerEvents={pointerEvents}>
        <circle
          cx={cx}
          cy={cy}
          r={radius}
          fill={style.fill}
          stroke={style.stroke}
          strokeWidth={strokeWidth}
          vectorEffect="non-scaling-stroke"
        />
        {objectType === ObjectTypeEnum.EnergyStream
          ? renderEnergyIcon(cx - size * 0.3, cy - size * 0.3, size * 0.6)
          : null}
        {kind === "decision" ? (
          <image
            href="/assets/UpdatedIcons/halfDecisionNode.svg"
            x={layout.x + layout.width / 2 - size * 0.35}
            y={layout.y + layout.height / 2 - size * 0.35}
            width={size * 0.7}
            height={size * 0.7}
            preserveAspectRatio="xMidYMid meet"
            pointerEvents="none"
          />
        ) : null}
      </g>
    );
  }
 
  if (kind === "recycle") {
    const points = [
      `${layout.x + layout.width / 2},${layout.y}`,
      `${layout.x + layout.width},${layout.y + layout.height / 2}`,
      `${layout.x + layout.width / 2},${layout.y + layout.height}`,
      `${layout.x},${layout.y + layout.height / 2}`,
    ].join(" ");
 
    return (
      <g opacity={opacity} pointerEvents={pointerEvents}>
        <polygon
          points={points}
          fill={FLOWSHEET_PREVIEW_STROKE}
          stroke={style.stroke}
          strokeWidth={strokeWidth}
          vectorEffect="non-scaling-stroke"
        />
        <text
          x={layout.x + layout.width / 2}
          y={layout.y + layout.height / 2}
          dominantBaseline="central"
          fill="#ffffff"
          fontFamily="Inter"
          fontSize={Math.max(8, Math.min(layout.width, layout.height) * 0.45)}
          fontWeight={700}
          pointerEvents="none"
          textAnchor="middle"
        >
          R
        </text>
      </g>
    );
  }
 
  return (
    <g opacity={opacity} pointerEvents={pointerEvents}>
      <rect
        x={layout.x}
        y={layout.y}
        width={layout.width}
        height={layout.height}
        rx={borderRadius ?? style.radius}
        ry={borderRadius ?? style.radius}
        fill={style.fill}
        stroke={style.stroke}
        strokeWidth={strokeWidth}
        vectorEffect="non-scaling-stroke"
      />
      {renderNodeIcon({ node, layout, iconScale })}
    </g>
  );
}
 
export function FlowsheetPreviewEdgeGlyph({
  edge,
  className,
}: {
  edge: FlowsheetPreviewRenderEdge;
  className?: string;
}) {
  return (
    <polyline
      className={className}
      points={edge.points}
      fill="none"
      stroke={edge.stroke}
      strokeDasharray={edge.strokeDasharray}
      strokeLinecap="round"
      strokeLinejoin="round"
      strokeOpacity={edge.opacity}
      strokeWidth={edge.strokeWidth}
      vectorEffect="non-scaling-stroke"
    />
  );
}