feat: added rule based connection validation and connection limits to the editor
This commit is contained in:
committed by
Björn Otgaar
parent
6d1c17e77b
commit
9e7c192804
45
src/pages/VisProgPage/visualProgrammingUI/HandleRules.ts
Normal file
45
src/pages/VisProgPage/visualProgrammingUI/HandleRules.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
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");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user