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 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 | 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 17x 17x 17x 17x 17x 17x 16x 16x 16x 16x 16x 16x 16x 17x 50x 50x 50x 38x 38x 36x 36x 38x 35x 35x 35x 35x 35x 35x 38x 50x 50x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 17x 50x 50x 50x 50x 16x 16x 16x 16x 16x 16x 16x 17x 50x 50x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 97x 97x 97x 97x 97x 16x 16x 16x 16x 16x 16x 16x 16x 16x 46x 46x 46x 46x 34x 2x 34x 32x 30x 30x 30x 46x 46x 46x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 50x 16x 16x 50x 35x 33x 33x 33x 33x 33x 33x 33x 16x 16x 16x 50x 19x 3x 3x 3x 3x 50x 16x 50x 50x 50x 50x 35x 35x 35x 35x 35x 2x 35x 35x 35x 33x 33x 33x 33x 33x 33x 33x 33x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 35x 16x 19x 19x 16x 16x 35x 31x 31x 34x 34x 31x 31x 31x 33x 33x 33x 33x 31x 31x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 31x 31x 19x 97x 35x 35x 35x 35x 35x 50x 50x 50x 50x 50x 15x 50x 1x 1x 1x 1x 1x 50x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 49x 50x 35x 35x 35x 35x 35x 35x 97x 16x 16x 16x 16x 16x 16x 16x 35x 35x 35x 35x 35x 35x 36x 16x 3x 19x 19x 19x 19x 19x 36x 20x 20x 20x 20x 20x 31x 20x 20x 20x 20x 31x 31x 34x 31x 33x 33x 33x 33x 33x 33x 33x 33x 33x 33x 31x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 20x 31x 31x 31x 31x 31x 31x 31x 31x 20x 36x 36x 36x 50x 36x 35x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 50x 50x 50x 50x 50x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 106x 106x 106x 106x 106x 105x 105x 105x 105x 105x 105x 105x 105x 105x 106x 107x 107x 107x 107x 107x 106x 106x 106x 106x 106x 106x 106x 106x 106x 105x 105x 106x 106x 106x 106x 106x 106x 106x 106x 97x 106x 97x 108x 97x 108x 108x 108x 106x 106x 106x 106x 106x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 96x 96x 96x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 111x 111x 111x 111x 111x 111x 111x 97x 102x 102x 102x 102x 102x 102x 102x 102x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x 97x | import {
Node,
FlowPosition,
NodeUI,
StickyNote
} from '../store/flow-definition';
import { snapToGrid } from './utils';
const VERTICAL_GAP = 80;
const HORIZONTAL_GAP = 60;
const STICKY_GAP = 20;
const MAX_WIDTH = 1200;
interface NodeSize {
width: number;
height: number;
}
/**
* Calculates a layered layout for a flow, placing the start node at the
* upper-left and arranging the flow downward. Sibling nodes at splits
* share the same horizontal plane.
*/
export function calculateLayeredLayout(
nodes: Node[],
nodeUIs: Record<string, NodeUI>,
startNodeUuid: string,
getNodeSize: (uuid: string) => NodeSize
): Record<string, FlowPosition> {
if (nodes.length === 0) return {};
const nodeSet = new Set(nodes.map((n) => n.uuid));
// Build deduplicated adjacency lists
const children = new Map<string, string[]>();
const parents = new Map<string, string[]>();
for (const node of nodes) {
const seen = new Set<string>();
const childUuids: string[] = [];
for (const exit of node.exits) {
if (
exit.destination_uuid &&
nodeSet.has(exit.destination_uuid) &&
!seen.has(exit.destination_uuid)
) {
seen.add(exit.destination_uuid);
childUuids.push(exit.destination_uuid);
const p = parents.get(exit.destination_uuid) || [];
p.push(node.uuid);
parents.set(exit.destination_uuid, p);
}
}
children.set(node.uuid, childUuids);
}
// Find back-edges via DFS so we can ignore cycles during layering
const backEdges = findBackEdges(startNodeUuid, children);
// Assign layers using longest-path on the DAG (ignoring back-edges)
const layers = assignLayers(
startNodeUuid,
nodes,
children,
parents,
backEdges
);
// Group nodes by layer
const layerGroups = new Map<number, string[]>();
for (const [uuid, layer] of layers) {
const group = layerGroups.get(layer) || [];
group.push(uuid);
layerGroups.set(layer, group);
}
// Order nodes within each layer using barycenter heuristic
const sortedLayers = Array.from(layerGroups.keys()).sort((a, b) => a - b);
orderNodesInLayers(sortedLayers, layerGroups, parents, layers);
// Gather sizes
const sizes = new Map<string, NodeSize>();
for (const node of nodes) {
sizes.set(node.uuid, getNodeSize(node.uuid));
}
// Compute positions
return computePositions(
sortedLayers,
layerGroups,
sizes,
parents,
layers,
startNodeUuid
);
}
/**
* Finds back-edges (cycle-forming edges) via DFS from the start node.
* Returns a set of "parentUuid->childUuid" strings representing edges to ignore.
*/
function findBackEdges(
startNodeUuid: string,
children: Map<string, string[]>
): Set<string> {
const backEdges = new Set<string>();
const visiting = new Set<string>(); // currently on the DFS stack
const visited = new Set<string>(); // fully processed
function dfs(node: string): void {
visiting.add(node);
visited.add(node);
for (const child of children.get(node) || []) {
if (visiting.has(child)) {
backEdges.add(`${node}->${child}`);
} else if (!visited.has(child)) {
dfs(child);
}
}
visiting.delete(node);
}
dfs(startNodeUuid);
return backEdges;
}
/**
* Assigns layers using topological processing order on the DAG
* (back-edges removed). Each node's layer = max(parent layers) + 1,
* giving the longest-path assignment so merge nodes sit below all parents.
*/
function assignLayers(
startNodeUuid: string,
nodes: Node[],
children: Map<string, string[]>,
parents: Map<string, string[]>,
backEdges: Set<string>
): Map<string, number> {
const layers = new Map<string, number>();
layers.set(startNodeUuid, 0);
// Build forward in-degree (ignoring back-edges) for topological processing
const inDegree = new Map<string, number>();
for (const node of nodes) {
inDegree.set(node.uuid, 0);
}
for (const [parent, childList] of children) {
for (const child of childList) {
if (!backEdges.has(`${parent}->${child}`)) {
inDegree.set(child, (inDegree.get(child) || 0) + 1);
}
}
}
// Process nodes in topological order (Kahn's algorithm)
// Start with nodes that have no forward in-edges
const queue: string[] = [];
for (const [uuid, deg] of inDegree) {
if (deg === 0) {
queue.push(uuid);
if (!layers.has(uuid)) {
layers.set(uuid, 0);
}
}
}
while (queue.length > 0) {
const current = queue.shift()!;
const currentLayer = layers.get(current)!;
for (const child of children.get(current) || []) {
if (backEdges.has(`${current}->${child}`)) continue;
// Longest path: child layer = max of all parent layers + 1
const newLayer = currentLayer + 1;
if (!layers.has(child) || newLayer > layers.get(child)!) {
layers.set(child, newLayer);
}
// Decrement in-degree; enqueue when all forward parents processed
const remaining = inDegree.get(child)! - 1;
inDegree.set(child, remaining);
if (remaining === 0) {
queue.push(child);
}
}
}
// Handle unreachable nodes (not reachable from start)
const unreachable = nodes.filter((n) => !layers.has(n.uuid));
if (unreachable.length > 0) {
const maxLayer = Math.max(...Array.from(layers.values()), -1);
let unreachableLayer = maxLayer + 2;
for (const node of unreachable) {
layers.set(node.uuid, unreachableLayer);
unreachableLayer++;
}
}
return layers;
}
/**
* Orders nodes within each layer using a barycenter heuristic:
* each node is positioned based on the average index of its parents
* in layers above.
*/
function orderNodesInLayers(
sortedLayers: number[],
layerGroups: Map<number, string[]>,
parents: Map<string, string[]>,
layers: Map<string, number>
): void {
const indexInLayer = new Map<string, number>();
for (const layer of sortedLayers) {
const group = layerGroups.get(layer)!;
if (layer === sortedLayers[0]) {
group.forEach((uuid, idx) => indexInLayer.set(uuid, idx));
continue;
}
const barycenters: { uuid: string; value: number }[] = group.map((uuid) => {
// Only consider parents that are in layers above this one
const nodeParents = (parents.get(uuid) || []).filter((p) => {
const pl = layers.get(p);
return pl !== undefined && pl < layer;
});
if (nodeParents.length === 0) {
return { uuid, value: Infinity };
}
const sum = nodeParents.reduce((acc, p) => {
return acc + (indexInLayer.get(p) ?? 0);
}, 0);
return { uuid, value: sum / nodeParents.length };
});
barycenters.sort((a, b) => a.value - b.value);
const sorted = barycenters.map((b) => b.uuid);
layerGroups.set(layer, sorted);
sorted.forEach((uuid, idx) => indexInLayer.set(uuid, idx));
}
}
/**
* Splits a layer's nodes into sub-rows that each fit within MAX_WIDTH.
*/
function splitIntoRows(
group: string[],
sizes: Map<string, NodeSize>
): string[][] {
const rows: string[][] = [];
let currentRow: string[] = [];
let currentWidth = 0;
for (const uuid of group) {
const nodeWidth = sizes.get(uuid)?.width || 200;
const additionalWidth =
currentRow.length > 0 ? HORIZONTAL_GAP + nodeWidth : nodeWidth;
if (currentRow.length > 0 && currentWidth + additionalWidth > MAX_WIDTH) {
rows.push(currentRow);
currentRow = [uuid];
currentWidth = nodeWidth;
} else {
currentRow.push(uuid);
currentWidth += additionalWidth;
}
}
if (currentRow.length > 0) {
rows.push(currentRow);
}
return rows;
}
/**
* Computes pixel positions for each node. Each node's ideal X is centered
* under its parent(s), with overlap resolution to prevent collisions.
* Layers that exceed MAX_WIDTH are split into multiple rows.
*/
function computePositions(
sortedLayers: number[],
layerGroups: Map<number, string[]>,
sizes: Map<string, NodeSize>,
parents: Map<string, string[]>,
layers: Map<string, number>,
startNodeUuid: string
): Record<string, FlowPosition> {
const positions: Record<string, FlowPosition> = {};
let currentTop = 0;
for (const layer of sortedLayers) {
const group = layerGroups.get(layer)!;
const subRows = splitIntoRows(group, sizes);
for (const subRow of subRows) {
const top = snapToGrid(currentTop);
if (layer === sortedLayers[0]) {
// First layer: start node at top, others nudged down two grid squares
let x = 0;
for (const uuid of subRow) {
const nodeTop = uuid === startNodeUuid ? top : top + 40;
positions[uuid] = { left: snapToGrid(x), top: nodeTop };
x += (sizes.get(uuid)?.width || 200) + HORIZONTAL_GAP;
}
} else {
// Compute total width of this sub-row
let totalWidth = 0;
for (const uuid of subRow) {
totalWidth += sizes.get(uuid)?.width || 200;
}
totalWidth += HORIZONTAL_GAP * (subRow.length - 1);
// Find the center point to place this sub-row under: midpoint of
// the span of all parent centers for nodes in this sub-row
const parentCenters: number[] = [];
for (const uuid of subRow) {
const nodeParents = (parents.get(uuid) || []).filter((p) => {
const pl = layers.get(p);
return pl !== undefined && pl < layer;
});
for (const pUuid of nodeParents) {
const parentPos = positions[pUuid];
if (parentPos) {
const parentWidth = sizes.get(pUuid)?.width || 200;
parentCenters.push(parentPos.left + parentWidth / 2);
}
}
}
// Center the sub-row under the parent span, anchored left if not enough room
let rowLeft: number;
if (parentCenters.length > 0) {
const spanCenter =
(Math.min(...parentCenters) + Math.max(...parentCenters)) / 2;
rowLeft = Math.max(0, spanCenter - totalWidth / 2);
} else {
rowLeft = 0;
}
// Place nodes left-to-right starting from rowLeft
let x = rowLeft;
for (const uuid of subRow) {
const nodeWidth = sizes.get(uuid)?.width || 200;
positions[uuid] = { left: snapToGrid(x), top };
x = snapToGrid(x) + nodeWidth + HORIZONTAL_GAP;
}
}
// Advance past this sub-row
const maxHeight = Math.max(
...subRow.map((uuid) => sizes.get(uuid)?.height || 100)
);
currentTop = top + maxHeight + VERTICAL_GAP;
}
}
// Shift everything so the start node is at (0, 0)
const startPos = positions[startNodeUuid];
if (startPos) {
const offsetX = startPos.left;
const offsetY = startPos.top;
for (const uuid of Object.keys(positions)) {
positions[uuid] = {
left: snapToGrid(Math.max(0, positions[uuid].left - offsetX)),
top: snapToGrid(Math.max(0, positions[uuid].top - offsetY))
};
}
}
return positions;
}
interface StickySize {
width: number;
height: number;
}
/**
* Places sticky notes next to the node they were closest to before reflow.
* If a sticky was to the left of the start node, it is placed to the right instead.
*/
export function placeStickyNotes(
stickies: Record<string, StickyNote>,
oldNodePositions: Record<string, FlowPosition>,
newNodePositions: Record<string, FlowPosition>,
nodeSizes: Map<string, NodeSize>,
stickySizes: Map<string, StickySize>,
startNodeUuid: string
): Record<string, FlowPosition> {
const stickyPositions: Record<string, FlowPosition> = {};
const nodeUuids = Object.keys(newNodePositions);
if (nodeUuids.length === 0) return stickyPositions;
// For each sticky, find the closest node based on pre-reflow positions
const stickyToNode = new Map<string, string>();
const nodeStickies = new Map<string, { uuid: string; wasLeft: boolean }[]>();
for (const [stickyUuid, sticky] of Object.entries(stickies)) {
if (!sticky.position) continue;
const sx = sticky.position.left;
const sy = sticky.position.top;
let closestNode = nodeUuids[0];
let closestDist = Infinity;
for (const nodeUuid of nodeUuids) {
const np = oldNodePositions[nodeUuid];
if (!np) continue;
const dx = sx - np.left;
const dy = sy - np.top;
const dist = dx * dx + dy * dy;
if (dist < closestDist) {
closestDist = dist;
closestNode = nodeUuid;
}
}
stickyToNode.set(stickyUuid, closestNode);
// Was the sticky to the left of the node?
const nodePos = oldNodePositions[closestNode];
const wasLeft = nodePos ? sx < nodePos.left : false;
const list = nodeStickies.get(closestNode) || [];
list.push({ uuid: stickyUuid, wasLeft });
nodeStickies.set(closestNode, list);
}
// Place stickies next to their associated nodes
// Collect all placed rectangles (nodes + stickies) for collision avoidance
const placed: { left: number; top: number; width: number; height: number }[] =
[];
// Add all nodes to placed rectangles
for (const nodeUuid of nodeUuids) {
const pos = newNodePositions[nodeUuid];
const size = nodeSizes.get(nodeUuid) || { width: 200, height: 100 };
placed.push({
left: pos.left,
top: pos.top,
width: size.width,
height: size.height
});
}
for (const [nodeUuid, stickyList] of nodeStickies) {
const nodePos = newNodePositions[nodeUuid];
if (!nodePos) continue;
const nodeSize = nodeSizes.get(nodeUuid) || { width: 200, height: 100 };
for (const { uuid: stickyUuid, wasLeft } of stickyList) {
const stickySize = stickySizes.get(stickyUuid) || {
width: 182,
height: 100
};
// Determine placement side: right of node if it's the start node and sticky
// was to the left, otherwise prefer the side it was on originally
const placeRight = (nodeUuid === startNodeUuid && wasLeft) || !wasLeft;
let candidateLeft: number;
if (placeRight) {
candidateLeft = nodePos.left + nodeSize.width + STICKY_GAP;
} else {
candidateLeft = nodePos.left - stickySize.width - STICKY_GAP;
}
let candidateTop = nodePos.top;
// Snap and clamp
candidateLeft = snapToGrid(Math.max(0, candidateLeft));
candidateTop = snapToGrid(Math.max(0, candidateTop));
// Nudge down if colliding with any placed rectangle
let maxAttempts = 50;
while (
maxAttempts-- > 0 &&
collidesWithAny(
candidateLeft,
candidateTop,
stickySize.width,
stickySize.height,
placed
)
) {
candidateTop = snapToGrid(candidateTop + STICKY_GAP);
}
stickyPositions[stickyUuid] = {
left: candidateLeft,
top: candidateTop
};
// Add this sticky to placed rectangles
placed.push({
left: candidateLeft,
top: candidateTop,
width: stickySize.width,
height: stickySize.height
});
}
}
return stickyPositions;
}
function collidesWithAny(
left: number,
top: number,
width: number,
height: number,
placed: { left: number; top: number; width: number; height: number }[]
): boolean {
for (const r of placed) {
if (
left < r.left + r.width + STICKY_GAP &&
left + width + STICKY_GAP > r.left &&
top < r.top + r.height + STICKY_GAP &&
top + height + STICKY_GAP > r.top
) {
return true;
}
}
return false;
}
|