diff --git a/src/components/port/CustomPortModel.ts b/src/components/port/CustomPortModel.ts index 13cc2de7..b6c9cf5f 100644 --- a/src/components/port/CustomPortModel.ts +++ b/src/components/port/CustomPortModel.ts @@ -73,15 +73,22 @@ export class CustomPortModel extends DefaultPortModel { let thisNode = this.getNode(); let thisNodeModelType = thisNode.getOptions()["extras"]["type"]; let thisName = port.getName(); - + let thisPortType = thisName.split('-')[1]; if (this.isParameterNode(thisNodeModelType) == true){ // if the port you are trying to link ready has other links console.log("port name: ", thisName); console.log("parameter port: ", port.getNode().getInPorts()); if (Object.keys(port.getLinks()).length > 0){ + // When port is 'string', 'list' and 'dict' type, just return + switch (thisPortType){ + case "string": + case "list": + case "dict": + return; + } port.getNode().getOptions().extras["borderColor"]="red"; - port.getNode().getOptions().extras["tip"]="Port has other link"; + port.getNode().getOptions().extras["tip"]=`Port doesn't allow multi-link of ${thisPortType} type`; port.getNode().setSelected(true); return false; } diff --git a/src/components/xircuitBodyWidget.tsx b/src/components/xircuitBodyWidget.tsx index 849dd3fb..667b8df5 100644 --- a/src/components/xircuitBodyWidget.tsx +++ b/src/components/xircuitBodyWidget.tsx @@ -182,6 +182,7 @@ export const BodyWidget: FC = ({ const xircuitLogger = new Log(app); const contextRef = useRef(context); const notInitialRender = useRef(false); + const needAppend = useRef(""); const onChange = useCallback( (): void => { @@ -468,6 +469,9 @@ export const BodyWidget: FC = ({ let bindingName = 'c_' + ++j; let currentNodeModel = getNodeModelById(nodeModels, targetNodeId); let allPort = currentNodeModel.getPorts(); + // Reset appending values + needAppend.current = ""; + for (let port in allPort) { let portIn = allPort[port].getOptions().alignment == 'left'; @@ -496,34 +500,53 @@ export const BodyWidget: FC = ({ //Get the id of the node of the connected link let linkSourceNodeId = allPort[port]["links"][portLink]["sourcePort"]["parent"]["options"]["id"]; + let equalSign = ' = '; + let sourcePortLabelStructure; + + // When port is 'string', 'list' and 'dict' type + // append values if there's multiple link connected + if (port.includes('string') || + port.includes('list') || + port.includes('dict') + ) { + if (needAppend.current == label) { + switch (sourceNodeType) { + case "dict": + equalSign = ' |= ' + break; + default: + equalSign = ' += ' + break; + } + } + needAppend.current = label; + } if (port.startsWith("parameter")) { if (sourceNodeName.startsWith("Literal")) { - - if (sourceNodeType == 'string') { - pythonCode += ' ' + bindingName + '.' + label + '.value = ' + "'" + sourcePortLabel + "'\n"; - } - - else if (sourceNodeType == 'list') { - pythonCode += ' ' + bindingName + '.' + label + '.value = ' + "[" + sourcePortLabel + "]" + "\n"; - } - - else if (sourceNodeType == 'tuple') { - pythonCode += ' ' + bindingName + '.' + label + '.value = ' + "(" + sourcePortLabel + ")" + "\n"; - } - - else if (sourceNodeType == 'dict') { - pythonCode += ' ' + bindingName + '.' + label + '.value = ' + "{" + sourcePortLabel + "}" + "\n"; - } - - else { - pythonCode += ' ' + bindingName + '.' + label + '.value = ' + sourcePortLabel + "\n"; + switch (sourceNodeType) { + case "string": + sourcePortLabelStructure = "'" + sourcePortLabel + "'"; + break; + case "list": + sourcePortLabelStructure = "[" + sourcePortLabel + "]"; + break; + case "tuple": + sourcePortLabelStructure = "(" + sourcePortLabel + ")"; + break; + case "dict": + sourcePortLabelStructure = "{" + sourcePortLabel + "}"; + break; + default: + sourcePortLabelStructure = sourcePortLabel; + break; } + pythonCode += ' ' + bindingName + '.' + label + '.value' + equalSign + sourcePortLabelStructure + "\n"; + } else if (linkSourceNodeId == sourceNodeId && !sourceNodeName.startsWith("Hyperparameter")) { // Make sure the node id match between connected link and source node // Skip Hyperparameter Components - } else if (linkSourceNodeId == sourceNodeId && !sourceNodeName.startsWith("Hyperparameter")) { - pythonCode += ' ' + bindingName + '.' + label + ' = ' + preBindingName + '.' + sourcePortLabel + '\n'; + pythonCode += ' ' + bindingName + '.' + label + equalSign + preBindingName + '.' + sourcePortLabel + '\n'; } else { sourcePortLabel = sourcePortLabel.replace(/\s+/g, "_"); sourcePortLabel = sourcePortLabel.toLowerCase(); @@ -531,11 +554,11 @@ export const BodyWidget: FC = ({ let paramName = sourceNodeName[sourceNodeName.length - 1]; paramName = paramName.replace(/\s+/g, "_"); paramName = paramName.toLowerCase(); - pythonCode += ' ' + bindingName + '.' + label + '.value = args.' + paramName + '\n'; + pythonCode += ' ' + bindingName + '.' + label + '.value' + equalSign + 'args.' + paramName + '\n'; } } else { - pythonCode += ' ' + bindingName + '.' + label + ' = ' + preBindingName + '.' + sourcePortLabel + '\n'; + pythonCode += ' ' + bindingName + '.' + label + equalSign + preBindingName + '.' + sourcePortLabel + '\n'; } } }