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 | 41x 1x 7x 7x 9x 11x 10x 10x | import SuperJSON from "superjson";
import {
AddCommand,
MoveCommand,
DeleteCommand,
ConnectCommand,
DisconnectCommand,
MergeStreamsCommand,
} from "@/types/commands";
import { SimulationObjectRead } from "@/api/apiStore.gen";
/**
* Create an add command for undo/redo
*/
export function createAddCommand({
objectId,
graphicObjectId,
objectType,
simulationObject,
position,
groupId,
}: {
objectId: number;
graphicObjectId: number;
objectType: string;
simulationObject: SimulationObjectRead;
position: { x: number; y: number };
groupId?: number;
}): AddCommand {
return {
type: "ADD",
timestamp: Date.now(),
objectId,
graphicObjectId,
objectType,
serializedState: SuperJSON.stringify(simulationObject),
position,
groupId,
};
}
/**
* Create a move command for undo/redo
*/
export function createMoveCommand({
objectId,
graphicObjectId,
oldPosition,
newPosition,
groupObjects,
}: {
objectId: number;
graphicObjectId: number;
oldPosition: { x: number; y: number };
newPosition: { x: number; y: number };
groupObjects?: Array<{
objectId: number;
graphicObjectId: number;
oldPosition: { x: number; y: number };
newPosition: { x: number; y: number };
}>;
}): MoveCommand {
return {
type: "MOVE",
timestamp: Date.now(),
objectId,
graphicObjectId,
oldPosition,
newPosition,
groupObjects,
};
}
/**
* Create a delete command for undo/redo
*/
export function createDeleteCommand({
objects,
connectionData,
}: {
objects: Array<{
id: number;
objectType: string;
fullObjectData: unknown; // The complete object data to be serialized
}>;
connectionData?: Array<{
portId: number;
streamId: number | null;
serializedConnection: string;
}>;
}): DeleteCommand {
return {
type: "DELETE",
timestamp: Date.now(),
objects: objects.map((obj) => ({
id: obj.id,
objectType: obj.objectType,
serializedState: SuperJSON.stringify(obj.fullObjectData),
})),
connectionData,
};
}
/**
* Create a connect command for undo/redo
*/
export function createConnectCommand({
connectionId,
portId1,
objectId1,
portId2,
objectId2,
streamId,
connectionData,
overwrittenConnections,
}: {
connectionId?: string | number;
portId1: number;
objectId1: number;
portId2: number;
objectId2: number;
streamId?: number;
connectionData?: unknown; // Full connection object data
overwrittenConnections?: Array<{
portId: number;
oldStreamId: number | null;
oldConnectionData: unknown;
}>;
}): ConnectCommand {
return {
type: "CONNECT",
timestamp: Date.now(),
connectionId,
portId1,
objectId1,
portId2,
objectId2,
streamId,
serializedConnection: connectionData
? SuperJSON.stringify(connectionData)
: undefined,
overwrittenConnections: overwrittenConnections?.map((conn) => ({
portId: conn.portId,
oldStreamId: conn.oldStreamId,
serializedOldConnection: SuperJSON.stringify(conn.oldConnectionData),
})),
};
}
/**
* Create a disconnect command for undo/redo
*/
export function createDisconnectCommand({
connectionId,
portId1,
objectId1,
portId2,
objectId2,
streamId,
connectionData,
wasSplitOperation,
originalStreamId,
newStreamIds,
portStreamMapping,
}: {
connectionId?: string | number;
portId1: number;
objectId1: number;
portId2: number;
objectId2: number;
streamId?: number;
connectionData: unknown; // Full connection object data before disconnection
wasSplitOperation?: boolean;
originalStreamId?: number;
newStreamIds?: number[];
portStreamMapping?: { [portId: number]: number };
}): DisconnectCommand {
return {
type: "DISCONNECT",
timestamp: Date.now(),
connectionId,
portId1,
objectId1,
portId2,
objectId2,
streamId,
serializedConnection: SuperJSON.stringify(connectionData),
wasSplitOperation,
originalStreamId,
newStreamIds,
portStreamMapping,
};
}
/**
* Create a merge streams command for undo/redo - REDESIGNED for proper restoration
* This captures complete state information needed to recreate deleted streams during undo
*/
export function createMergeStreamsCommand({
stream1,
stream2,
stream1PortConnections,
stream2PortConnections,
stream1GraphicPositions,
stream2GraphicPositions,
stream1TypeWasFeed,
mergeType = "simple",
stream1WasDeleted = false,
stream2WasDeleted = false,
}: {
stream1: SimulationObjectRead;
stream2: SimulationObjectRead;
stream1PortConnections: Array<{
portId: number;
unitOpId: number;
direction: string;
key: string;
}>;
stream2PortConnections: Array<{
portId: number;
unitOpId: number;
direction: string;
key: string;
}>;
stream1GraphicPositions: Array<{
x: number;
y: number;
width: number;
height: number;
groupId: number;
}>;
stream2GraphicPositions: Array<{
x: number;
y: number;
width: number;
height: number;
groupId: number;
}>;
stream1TypeWasFeed: boolean;
mergeType?: "simple" | "intermediate" | "parallel" | "decision_node";
stream1WasDeleted?: boolean;
stream2WasDeleted?: boolean;
}): MergeStreamsCommand {
return {
type: "MERGE_STREAMS",
timestamp: Date.now(),
// Original stream information
stream1Id: stream1.id,
stream1Data: SuperJSON.stringify(stream1),
stream2Id: stream2.id,
stream2Data: SuperJSON.stringify(stream2),
// Port connection information (critical for restoration)
stream1PortConnections: SuperJSON.stringify(stream1PortConnections),
stream2PortConnections: SuperJSON.stringify(stream2PortConnections),
// Result information (will be updated after merge operation)
resultingStreamId: stream1.id, // Initially assume stream1 survives, will be updated
deletedStreamId: stream2.id, // Initially assume stream2 gets deleted, will be updated
// Merge metadata
stream1TypeWasFeed,
mergeType,
// Graphic positioning
stream1GraphicPositions: SuperJSON.stringify(stream1GraphicPositions),
stream2GraphicPositions: SuperJSON.stringify(stream2GraphicPositions),
// NEW deletion flags
stream1WasDeleted,
stream2WasDeleted,
};
}
/**
* Helper to safely serialize an object for command storage
*/
export function safeSerialize(obj: unknown): string {
try {
return SuperJSON.stringify(obj);
} catch (error) {
console.error("Failed to serialize object for undo/redo:", error);
// Fallback to JSON if SuperJSON fails
try {
return JSON.stringify(obj);
} catch (jsonError) {
console.error(
"Failed to JSON serialize object for undo/redo:",
jsonError,
);
return "{}";
}
}
}
/**
* Helper to safely deserialize an object from command storage
*/
export function safeDeserialize(serializedData: string): unknown {
try {
return SuperJSON.parse(serializedData);
} catch (error) {
console.error("Failed to deserialize object from undo/redo:", error);
// Fallback to JSON if SuperJSON fails
try {
return JSON.parse(serializedData);
} catch (jsonError) {
console.error("Failed to JSON parse object from undo/redo:", jsonError);
return {};
}
}
}
|