|
| 1 | +import { ContentBlock } from "@/components/shared/ContextPanel/Blocks/ContentBlock"; |
| 2 | +import { StackingControls } from "@/components/shared/ReactFlow/FlowControls/StackingControls"; |
| 3 | +import { useComponentSpec } from "@/providers/ComponentSpecProvider"; |
| 4 | +import { ZINDEX_ANNOTATION } from "@/utils/annotations"; |
| 5 | +import { |
| 6 | + type ComponentSpec, |
| 7 | + type InputSpec, |
| 8 | + isInputSpec, |
| 9 | + isOutputSpec, |
| 10 | + type OutputSpec, |
| 11 | +} from "@/utils/componentSpec"; |
| 12 | +import { |
| 13 | + inputNameToNodeId, |
| 14 | + outputNameToNodeId, |
| 15 | +} from "@/utils/nodes/nodeIdUtils"; |
| 16 | +import { updateSubgraphSpec } from "@/utils/subgraphUtils"; |
| 17 | + |
| 18 | +interface IOZIndexEditorProps { |
| 19 | + ioSpec: InputSpec | OutputSpec; |
| 20 | +} |
| 21 | + |
| 22 | +export const IOZIndexEditor = ({ ioSpec }: IOZIndexEditorProps) => { |
| 23 | + const { |
| 24 | + componentSpec, |
| 25 | + currentSubgraphSpec, |
| 26 | + currentSubgraphPath, |
| 27 | + setComponentSpec, |
| 28 | + } = useComponentSpec(); |
| 29 | + |
| 30 | + const isInput = isInputSpec(ioSpec); |
| 31 | + |
| 32 | + const nodeId = isInput |
| 33 | + ? inputNameToNodeId(ioSpec.name) |
| 34 | + : outputNameToNodeId(ioSpec.name); |
| 35 | + |
| 36 | + const handleStackingControlChange = (newZIndex: number) => { |
| 37 | + const updatedSubgraphSpec = setZIndexInAnnotations( |
| 38 | + currentSubgraphSpec, |
| 39 | + ioSpec, |
| 40 | + newZIndex, |
| 41 | + ); |
| 42 | + |
| 43 | + const newRootSpec = updateSubgraphSpec( |
| 44 | + componentSpec, |
| 45 | + currentSubgraphPath, |
| 46 | + updatedSubgraphSpec, |
| 47 | + ); |
| 48 | + |
| 49 | + setComponentSpec(newRootSpec); |
| 50 | + }; |
| 51 | + |
| 52 | + return ( |
| 53 | + <ContentBlock className="border rounded-lg p-2 bg-background w-fit mx-auto"> |
| 54 | + <StackingControls |
| 55 | + nodeId={nodeId} |
| 56 | + onChange={handleStackingControlChange} |
| 57 | + /> |
| 58 | + </ContentBlock> |
| 59 | + ); |
| 60 | +}; |
| 61 | + |
| 62 | +function setZIndexInAnnotations( |
| 63 | + componentSpec: ComponentSpec, |
| 64 | + ioSpec: InputSpec | OutputSpec, |
| 65 | + zIndex: number, |
| 66 | +): ComponentSpec { |
| 67 | + const newComponentSpec = { ...componentSpec }; |
| 68 | + |
| 69 | + const isInput = isInputSpec(ioSpec); |
| 70 | + const isOutput = isOutputSpec(ioSpec); |
| 71 | + |
| 72 | + if (isInput) { |
| 73 | + const inputs = [...(newComponentSpec.inputs || [])]; |
| 74 | + const inputIndex = inputs.findIndex((input) => input.name === ioSpec.name); |
| 75 | + |
| 76 | + if (inputIndex >= 0) { |
| 77 | + const annotations = inputs[inputIndex].annotations || {}; |
| 78 | + |
| 79 | + const updatedAnnotations = { |
| 80 | + ...annotations, |
| 81 | + [ZINDEX_ANNOTATION]: `${zIndex}`, |
| 82 | + }; |
| 83 | + |
| 84 | + inputs[inputIndex] = { |
| 85 | + ...inputs[inputIndex], |
| 86 | + annotations: updatedAnnotations, |
| 87 | + }; |
| 88 | + |
| 89 | + newComponentSpec.inputs = inputs; |
| 90 | + } |
| 91 | + } else if (isOutput) { |
| 92 | + const outputs = [...(newComponentSpec.outputs || [])]; |
| 93 | + const outputIndex = outputs.findIndex( |
| 94 | + (output) => output.name === ioSpec.name, |
| 95 | + ); |
| 96 | + |
| 97 | + if (outputIndex >= 0) { |
| 98 | + const annotations = outputs[outputIndex].annotations || {}; |
| 99 | + |
| 100 | + const updatedAnnotations = { |
| 101 | + ...annotations, |
| 102 | + [ZINDEX_ANNOTATION]: `${zIndex}`, |
| 103 | + }; |
| 104 | + |
| 105 | + outputs[outputIndex] = { |
| 106 | + ...outputs[outputIndex], |
| 107 | + annotations: updatedAnnotations, |
| 108 | + }; |
| 109 | + |
| 110 | + newComponentSpec.outputs = outputs; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return newComponentSpec; |
| 115 | +} |
0 commit comments