|
| 1 | +import { type Node } from "@xyflow/react"; |
| 2 | + |
| 3 | +import { isDefinedNode, type NodeType } from "../types"; |
| 4 | + |
| 5 | +type ZIndexDefinition = { |
| 6 | + min: number; |
| 7 | + max: number; |
| 8 | + default: number; |
| 9 | +}; |
| 10 | + |
| 11 | +export const Z_INDEX_RANGES: Record<NodeType, ZIndexDefinition> = { |
| 12 | + task: { |
| 13 | + min: -100, |
| 14 | + max: 100, |
| 15 | + default: 0, |
| 16 | + }, |
| 17 | + input: { |
| 18 | + min: -100, |
| 19 | + max: 100, |
| 20 | + default: 0, |
| 21 | + }, |
| 22 | + output: { |
| 23 | + min: -100, |
| 24 | + max: 100, |
| 25 | + default: 0, |
| 26 | + }, |
| 27 | + ghost: { |
| 28 | + min: 100, |
| 29 | + max: 100, |
| 30 | + default: 100, |
| 31 | + }, |
| 32 | +}; |
| 33 | + |
| 34 | +const getNodeZIndexProp = ( |
| 35 | + node: Node, |
| 36 | + prop: keyof ZIndexDefinition, |
| 37 | +): number => { |
| 38 | + if (!isDefinedNode(node)) { |
| 39 | + return 0; |
| 40 | + } |
| 41 | + |
| 42 | + return Z_INDEX_RANGES[node.type][prop]; |
| 43 | +}; |
| 44 | + |
| 45 | +const getAllNodeZIndices = (nodes: Node[]): number[] => { |
| 46 | + return nodes |
| 47 | + .map((node) => node.zIndex ?? getNodeZIndexProp(node, "default")) |
| 48 | + .sort((a, b) => a - b); |
| 49 | +}; |
| 50 | + |
| 51 | +export const bringToFront = (node: Node, allNodes: Node[]): number => { |
| 52 | + const allZIndices = getAllNodeZIndices(allNodes); |
| 53 | + const maxZ = Math.max(...allZIndices, getNodeZIndexProp(node, "default")); |
| 54 | + const nodeMax = getNodeZIndexProp(node, "max"); |
| 55 | + return Math.min(maxZ + 1, nodeMax); |
| 56 | +}; |
| 57 | + |
| 58 | +export const sendToBack = (node: Node, allNodes: Node[]): number => { |
| 59 | + const allZIndices = getAllNodeZIndices(allNodes); |
| 60 | + const minZ = Math.min(...allZIndices, getNodeZIndexProp(node, "default")); |
| 61 | + const nodeMin = getNodeZIndexProp(node, "min"); |
| 62 | + return Math.max(minZ - 1, nodeMin); |
| 63 | +}; |
| 64 | + |
| 65 | +export const moveForward = (node: Node, allNodes: Node[]): number => { |
| 66 | + const currentZ = node.zIndex ?? getNodeZIndexProp(node, "default"); |
| 67 | + const allZIndices = getAllNodeZIndices(allNodes); |
| 68 | + const nodeMax = getNodeZIndexProp(node, "max"); |
| 69 | + |
| 70 | + const higherIndices = allZIndices.filter((z) => z > currentZ); |
| 71 | + if (higherIndices.length === 0) { |
| 72 | + return currentZ; |
| 73 | + } |
| 74 | + |
| 75 | + const nextZ = Math.min(...higherIndices); |
| 76 | + return Math.min(nextZ === currentZ + 1 ? nextZ + 1 : nextZ, nodeMax); |
| 77 | +}; |
| 78 | + |
| 79 | +export const moveBackward = (node: Node, allNodes: Node[]): number => { |
| 80 | + const currentZ = node.zIndex ?? getNodeZIndexProp(node, "default"); |
| 81 | + const allZIndices = getAllNodeZIndices(allNodes); |
| 82 | + const nodeMin = getNodeZIndexProp(node, "min"); |
| 83 | + |
| 84 | + const lowerIndices = allZIndices.filter((z) => z < currentZ); |
| 85 | + if (lowerIndices.length === 0) { |
| 86 | + return currentZ; |
| 87 | + } |
| 88 | + |
| 89 | + const prevZ = Math.max(...lowerIndices); |
| 90 | + return Math.max(prevZ === currentZ - 1 ? prevZ - 1 : prevZ, nodeMin); |
| 91 | +}; |
0 commit comments