|
| 1 | +import { DEFAULT_NODE_SPACING } from '@/utilities/constants'; |
| 2 | +import type { BaseNode } from '@/types/layout'; |
| 3 | + |
| 4 | +import { getNodeHeight, getNodeWidth } from './node-dimensions'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Get coordinates for a new node, positioning it in a grid pattern. |
| 8 | + * |
| 9 | + * This function takes maximum width and height of the existing nodes |
| 10 | + * and then finds place for a new one. |
| 11 | + * |
| 12 | + * The node is positioned such that it fits within the maximum width of the existing nodes, |
| 13 | + * and when the width is exceeded, it wraps to the next row. |
| 14 | + * |
| 15 | + * @param nodes A list of existing nodes, used to calculate the bounds of the diagram. |
| 16 | + * @param newNode A new node to add within the bounds of the diagram. |
| 17 | + */ |
| 18 | +export const getCoordinatesForNewNodeFromMaxDimensions = <N extends BaseNode>({ |
| 19 | + maxWidth, |
| 20 | + maxHeight, |
| 21 | + newNode, |
| 22 | +}: { |
| 23 | + newNode: N; |
| 24 | + maxWidth: number; |
| 25 | + maxHeight: number; |
| 26 | +}) => { |
| 27 | + const newNodeWidth = getNodeWidth(newNode); |
| 28 | + const newNodeHeight = getNodeHeight(newNode); |
| 29 | + |
| 30 | + let x = 0; |
| 31 | + let y = maxHeight + DEFAULT_NODE_SPACING; |
| 32 | + let rowHeight = 0; |
| 33 | + |
| 34 | + if (x + newNodeWidth + DEFAULT_NODE_SPACING > maxWidth) { |
| 35 | + x = 0; |
| 36 | + y += rowHeight + DEFAULT_NODE_SPACING; |
| 37 | + rowHeight = 0; |
| 38 | + } |
| 39 | + |
| 40 | + x += newNodeWidth + DEFAULT_NODE_SPACING; |
| 41 | + rowHeight = Math.max(rowHeight, newNodeHeight); |
| 42 | + |
| 43 | + return { x, y }; |
| 44 | +}; |
| 45 | + |
| 46 | +/** |
| 47 | + * Get coordinates for a new node, positioning it in a grid pattern. |
| 48 | + * |
| 49 | + * This function calculates the maximum width and height of the existing nodes |
| 50 | + * and then finds a place for the new node. |
| 51 | + * |
| 52 | + * The node is positioned such that it fits within the maximum width of the existing nodes, |
| 53 | + * and when the width is exceeded, it wraps to the next row. |
| 54 | + * |
| 55 | + * @param nodes A list of existing nodes, used to calculate the bounds of the diagram. |
| 56 | + * @param newNode A new node to add within the bounds of the diagram. |
| 57 | + */ |
| 58 | +export const getCoordinatesForNewNode = <N extends BaseNode>(nodes: N[], newNode: N): { x: number; y: number } => { |
| 59 | + if (nodes.length === 0) { |
| 60 | + return { x: DEFAULT_NODE_SPACING, y: DEFAULT_NODE_SPACING }; |
| 61 | + } |
| 62 | + |
| 63 | + const maxWidth = Math.max(...nodes.map(node => getNodeWidth(node))) + DEFAULT_NODE_SPACING; |
| 64 | + const maxHeight = Math.max(...nodes.map(node => getNodeHeight(node))) + DEFAULT_NODE_SPACING; |
| 65 | + |
| 66 | + return getCoordinatesForNewNodeFromMaxDimensions({ |
| 67 | + newNode, |
| 68 | + maxWidth, |
| 69 | + maxHeight, |
| 70 | + }); |
| 71 | +}; |
0 commit comments