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 | 49x 5665x 547x 547x 2511x 12269x 2511x 2501x 2501x 547x 547x 547x 1094x 1869x 1869x 1869x 1869x 1869x 1869x 1869x 1719x 1719x 1719x 1719x 547x 547x 547x 547x 5665x 49x 31817x 31817x 15132x 15132x 15132x 15132x 16685x 16685x 16685x 16685x 31817x 1704x 31817x 49x 7887x 7887x 9198x 9198x 9198x 9198x 9198x 22067x 12783x 82364x 1651x 9284x 8651x 55403x 17151x 633x 315x 2025x 1035x 318x 2166x 1110x 9198x 19073x 19073x 9198x 12869x 6941x 12869x 12869x 26512x 19073x 12869x 12869x 12869x 12869x 12869x 12869x 11879x 990x 9198x 12744x 6261x 6261x 6483x 6483x 9198x 9198x 9198x 9198x 9198x 12744x 12744x 12744x 12744x 9198x 66x 66x 9132x 9132x 59x 9198x 9198x 9198x 12869x 7887x | import { Edge, Handle, Node, Position } from "@xyflow/react";
import { memo } from "react";
import {
CoreRecycledataListApiResponse,
GetConnectionsList,
GraphicObjectRead,
ObjectTypeEnum,
SimulationObjectRead,
useGraphicsgroupingsConnectionsRetrieveQuery,
} from "@/api/apiStore.gen";
import { useCurrentGroupId } from "@/hooks/flowsheetObjects";
import {
useObjectRecycleData,
useSimObjectConnectedToRecycle,
} from "@/hooks/recycleData";
import { isStream } from "../../../../lib/isStream";
interface RenderOperationProps {
currentGroupObjects: SimulationObjectRead[];
graphicsObjects: GraphicObjectRead[] | undefined;
connectionGraphics: GetConnectionsList | undefined;
recycleConnections: CoreRecycledataListApiResponse;
objectsGraphicObjectsMap: Map<number, GraphicObjectRead>;
}
export const useRenderOperations = (props: RenderOperationProps) => {
const { currentGroupObjects, graphicsObjects } = props;
function renderNodes(): Node[] {
const nodes: Node[] = [];
currentGroupObjects.map((unitop) => {
const graphicObject = graphicsObjects?.find(
(obj) => obj.simulationObject.id === unitop.id,
);
if (!graphicObject || !graphicObject.visible) return;
const newNode: Node = {
id: unitop.id.toString(),
type: "simulationObjectGraphic",
position: { x: +(graphicObject.x || 0), y: +(graphicObject.y || 0) },
width: graphicObject.width,
height: graphicObject.height,
data: {
simulationObject: unitop,
graphicObject: graphicObject,
label: unitop.componentName,
},
};
nodes.push(newNode);
});
return nodes;
}
function renderEdges(): Edge[] {
const { connectionGraphics, recycleConnections, objectsGraphicObjectsMap } =
props;
const mapConnectionsToEdges = (
connectionGraphics:
| GetConnectionsList
| CoreRecycledataListApiResponse
| undefined,
isRecycle: boolean,
) => {
connectionGraphics?.map((conn) => {
const streamId = isRecycle ? conn.tearObject : conn.stream;
const unitOpId = isRecycle ? conn.simulationObject : conn.unitOp;
const unitopGraphic = objectsGraphicObjectsMap.get(unitOpId);
const streamGraphic = objectsGraphicObjectsMap.get(streamId);
const unitOpType = unitopGraphic?.simulationObject.objectType;
const streamType = streamGraphic?.simulationObject.objectType;
if (!unitopGraphic || !streamGraphic) return null; // couldn't find the graphic object, it is probably still being fetched
const type = isRecycle ? "inlet" : conn.direction;
const isEnergy = streamType === ObjectTypeEnum.EnergyStream;
const newEdge: Edge = {
id:
type === "inlet"
? `edge-${streamId}-${unitOpId}`
: `edge-${unitOpId}-${streamId}`,
source: type === "inlet" ? streamId.toString() : unitOpId.toString(),
target: type === "outlet" ? streamId.toString() : unitOpId.toString(),
sourceHandle:
type === "inlet"
? `${streamType}-${streamId}-${unitOpId}`
: `${unitOpType}-${unitOpId}-${streamId}`,
targetHandle:
type === "outlet"
? `${streamType}-${unitOpId}-${streamId}`
: `${unitOpType}-${streamId}-${unitOpId}`,
type: "step",
style: {
stroke: "#000000",
strokeDasharray: isRecycle ? "5,5" : null,
},
data: { isEnergyStream: isEnergy },
};
edges.push(newEdge);
});
};
const edges: Edge[] = [];
mapConnectionsToEdges(connectionGraphics, false);
mapConnectionsToEdges(recycleConnections, true);
return edges;
}
return { renderNodes: renderNodes, renderEdges: renderEdges };
};
interface DynamicHandlesProps {
simulationObj: SimulationObjectRead;
graphicObj: GraphicObjectRead;
}
const getHandlePositionRelativeToRotationAndFlip = (
type: string,
rotation: number,
flipped: boolean,
): Position => {
let handleOrientation = Position.Left;
if (type === "target") {
if (rotation === 0) handleOrientation = Position.Left;
if (rotation === 90) handleOrientation = Position.Top;
if (rotation === 180) handleOrientation = Position.Right;
if (rotation === 270) handleOrientation = Position.Bottom;
} else {
if (rotation === 0) handleOrientation = Position.Right;
if (rotation === 90) handleOrientation = Position.Bottom;
if (rotation === 180) handleOrientation = Position.Left;
if (rotation === 270) handleOrientation = Position.Top;
}
if (flipped) {
handleOrientation === Position.Left
? (handleOrientation = Position.Right)
: handleOrientation === Position.Right
? (handleOrientation = Position.Left)
: handleOrientation === Position.Top
? (handleOrientation = Position.Bottom)
: handleOrientation === Position.Bottom
? (handleOrientation = Position.Top)
: console.log("Orientation not found!");
}
return handleOrientation;
};
export const useDynamicHandles = (props: DynamicHandlesProps) => {
const { simulationObj, graphicObj } = props;
const DynamicHandles = memo(() => {
const groupId = useCurrentGroupId();
const connQuery = useGraphicsgroupingsConnectionsRetrieveQuery({
group: groupId,
});
const getRecycleConnection = useSimObjectConnectedToRecycle();
const getRecycleData = useObjectRecycleData();
type ConnectionHandle = {
id: string;
type: "source" | "target";
position: Position;
};
const getConnectedObjects = () => {
if (
simulationObj.objectType === ObjectTypeEnum.Stream ||
simulationObj.objectType === ObjectTypeEnum.EnergyStream
) {
return connQuery.data
?.filter((obj) => obj.stream === simulationObj.id)
.sort((a, b) => a.port - b.port);
} else {
if (graphicObj.rotation === 0) {
return connQuery.data
?.filter((obj) => obj.unitOp === simulationObj.id)
.sort((a, b) => a.port - b.port);
} else if (graphicObj.rotation === 90 || graphicObj.rotation === 180) {
return connQuery.data
?.filter((obj) => obj.unitOp === simulationObj.id)
.sort((a, b) => b.port - a.port);
} else {
return connQuery.data
?.filter((obj) => obj.unitOp === simulationObj.id)
.sort((b, a) => b.port - a.port);
}
}
};
const mapPortKeyToPosition = (direction: string): Position => {
const type = direction === "inlet" ? "target" : "source";
return getHandlePositionRelativeToRotationAndFlip(
type,
graphicObj.rotation || 0,
graphicObj.flipped || false,
);
};
const getHandlePlacement = (
handles: ConnectionHandle[],
handle: ConnectionHandle,
index: number,
) => {
const sameSideIndex = handles
.slice(0, index)
.filter((h) => h.position === handle.position).length;
const connObjects = getConnectedObjects();
const handlesOnSide = connObjects?.filter((obj) => {
if (obj.unitOp !== simulationObj.id) return false;
return mapPortKeyToPosition(obj.direction) === handle.position;
});
const handleCount = handlesOnSide?.length || 1;
const nodeHeight = graphicObj.height!;
const nodeWidth = graphicObj.width!;
const offsetY =
handle.position === Position.Left || handle.position === Position.Right
? ((sameSideIndex + 0.5) / handleCount) * nodeHeight
: 0;
const offsetX =
handle.position === Position.Top || handle.position === Position.Bottom
? ((sameSideIndex + 0.5) / handleCount) * nodeWidth
: 0;
if (
handle.position === Position.Left ||
handle.position === Position.Right
) {
return {
top: offsetY,
transform:
handle.position === Position.Left
? "translate(50%, -50%)"
: "translate(-50%, -50%)",
};
} else {
return {
left: offsetX,
transform:
handle.position === Position.Top
? "translate(-50%, 50%)"
: "translate(-50%, -50%)",
};
}
};
const mapDirectionToHandle = (
direction: string,
): { type: "target" | "source"; position: Position } => {
if (isStream(simulationObj)) {
const handleType = direction === "inlet" ? "source" : "target";
return {
type: handleType,
position: getHandlePositionRelativeToRotationAndFlip(
handleType,
graphicObj.rotation || 0,
graphicObj.flipped || false,
),
};
} else {
const handleType = direction === "inlet" ? "target" : "source";
return {
type: handleType,
position: getHandlePositionRelativeToRotationAndFlip(
handleType,
graphicObj.rotation || 0,
graphicObj.flipped || false,
),
};
}
};
const getHandles = (): ConnectionHandle[] => {
const handles: ConnectionHandle[] = [];
const connObjects = getConnectedObjects();
const objType = simulationObj.objectType;
connObjects?.forEach((obj) => {
const { type, position } = mapDirectionToHandle(obj.direction);
const firstObjId = obj.direction === "inlet" ? obj.stream : obj.unitOp;
const secondObjId = obj.direction === "inlet" ? obj.unitOp : obj.stream;
handles.push({
id: `${objType}-${firstObjId}-${secondObjId}`,
type,
position,
});
});
// If object is recycle, else (it's a stream) get the connection to recycle
if (simulationObj.objectType === ObjectTypeEnum.Recycle) {
const recycleData = getRecycleData(+simulationObj.id!);
handles.push({
id: `${objType}-${recycleData?.tearObject}-${simulationObj.id}`,
type: "target",
position: Position.Top,
});
} else {
const recycle = getRecycleConnection(+simulationObj.id!);
if (recycle) {
handles.push({
id: `${objType}-${simulationObj.id}-${recycle.simulationObject}`,
type: "source",
position: Position.Bottom,
});
}
}
return handles;
};
const handles = getHandles();
return (
<>
{handles.map((handle, index) => (
<Handle
key={handle.id}
id={handle.id}
type={handle.type}
position={handle.position}
isConnectable={false}
style={{
...getHandlePlacement(handles, handle, index),
opacity: 0,
}}
/>
))}
</>
);
});
return { DynamicHandles: DynamicHandles };
};
|