import { type HandleRule, ruleResult } from "./HandleRuleLogic.ts"; import useFlowStore from "./VisProgStores.tsx"; /** * this specifies what types of nodes can make a connection to a handle that uses this rule */ export function allowOnlyConnectionsFromType(nodeTypes: string[]) : HandleRule { return ((_, {source}) => { const sourceType = useFlowStore.getState().nodes.find(node => node.id === source.id)!.type!; return nodeTypes.find(type => sourceType === type) ? ruleResult.satisfied : ruleResult.notSatisfied(`the target doesn't allow connections from nodes with type: ${sourceType}`); }) } /** * similar to allowOnlyConnectionsFromType, * this is a more specific variant that allows you to restrict connections to specific handles on each nodeType */ // export function allowOnlyConnectionsFromHandle(handles: {nodeType: string, handleId: string}[]) : HandleRule { return ((_, {source}) => { const sourceNode = useFlowStore.getState().nodes.find(node => node.id === source.id)!; return handles.find(handle => sourceNode.type === handle.nodeType && source.handleId === handle.handleId) ? ruleResult.satisfied : ruleResult.notSatisfied(`the target doesn't allow connections from nodes with type: ${sourceNode.type}`); }) } /** * This rule prevents a node from making a connection between its own handles */ export const noSelfConnections : HandleRule = (connection, _) => { return connection.source !== connection.target ? ruleResult.satisfied : ruleResult.notSatisfied("nodes are not allowed to connect to themselves"); }