Skip to content

Commit 6f3b0c3

Browse files
committed
better export but predicates didn\'t work
1 parent 88c76bc commit 6f3b0c3

5 files changed

Lines changed: 114 additions & 42 deletions

File tree

otoroshi/javascript/src/extensions/workflows/WorkflowsDesigner.js

Lines changed: 94 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ function createNode(id, existingNodes, child, addInformationsToNode) {
156156

157157
const buildGraph = (workflows, addInformationsToNode, targetId, handleId) => {
158158

159-
160159
if (workflows.length === 0) {
161160
return { edges: [], nodes: [] }
162161
}
@@ -331,7 +330,7 @@ const buildGraph = (workflows, addInformationsToNode, targetId, handleId) => {
331330
// sub path of switch group
332331

333332
const predicate = buildGraph([{
334-
kind: 'predicate'
333+
kind: 'predicate',
335334
}], addInformationsToNode, me, 'node')
336335
const predicateNode = predicate.nodes[0]
337336

@@ -573,61 +572,119 @@ function WorkflowsDesigner(props) {
573572
}, []);
574573

575574
const graphToJson = () => {
576-
const nodesWithConnections = nodes
577-
.filter(node => node.id !== 'returned-node')
578-
.reduce((acc, node) => {
579-
const connections = edges
580-
.filter(edge => edge.source === node.id)
581-
return [
582-
...acc,
583-
{
584-
node,
585-
connections: connections.map(connection => ({
586-
node: nodes.find(n => n.id === connection.target),
587-
handle: connection.sourceHandle.replace(`-${connection.source}`, '')
588-
}))
589-
}
590-
]
591-
}, [])
575+
const start = {
576+
kind: 'workflow',
577+
steps: [],
578+
returned: nodes.find(node => node.id === 'returned-node').data.workflow.returned
579+
}
592580

593-
const [first, ...rest] = nodesWithConnections
594-
return nodeToJson(first, rest, nodesWithConnections)
581+
const startOutput = edges.find(edge => edge.source === 'start')
582+
const firstNode = nodes.find(node => node.id === startOutput.target)
583+
584+
const graph = nodeToJson(firstNode, start)
585+
586+
return graph
595587
}
596588

597-
const nodeToJson = (current, rest, all) => {
598-
const { node, connections } = current
589+
const emptyWorkflow = ({
590+
kind: 'workflow',
591+
steps: [],
592+
})
593+
594+
const nodeToJson = (node, currentWorkflow, disableRecursion) => {
595+
const connections = edges.filter(edge => edge.source === node.id)
599596

600597
const { kind } = node.data
601598

602-
if (kind === 'workflow') {
603-
const steps = connections.filter(conn => conn.handle !== 'output').map(conn => all.find(n => n.node.id === conn.node.id))
599+
let subflow = undefined
604600

601+
if (node.id.endsWith('returned-node')) {
605602
return {
606-
kind: 'workflow',
607-
steps: steps.map(step => nodeToJson(step, rest, all)),
608-
returned: connections.find(conn => conn.handle === 'output')?.node.data.workflow.returned
603+
...currentWorkflow,
604+
returned: node.data.workflow
609605
}
610-
} else if (kind === "if") {
611-
return {
606+
}
607+
608+
if (kind === "if") {
609+
// CHECK THIS
610+
subflow = {
612611
kind: 'if',
613612
predicate: connections.find(conn => conn.handle === 'predicate')?.node.data.workflow,
614613
then: connections.find(conn => conn.handle === 'then')?.node.data.workflow,
615614
else: connections.find(conn => conn.handle === 'else')?.node.data.workflow
616615
}
617-
} else if (kind === 'flatmap') {
618-
619616
} else if (kind === 'foreach') {
620-
return {
621-
...node.data.workflow,
622-
node: connections.length > 0 ? nodeToJson(connections[0], rest, all) : undefined
617+
const foreachFlow = node.data.workflow
618+
const foreachLoop = connections.find(conn => conn.sourceHandle.startsWith('ForEachLoop'))
619+
620+
if (foreachLoop) {
621+
const node = nodeToJson(nodes.find(n => n.id === foreachLoop.target), emptyWorkflow)
622+
subflow = {
623+
...foreachFlow,
624+
node
625+
}
626+
} else {
627+
subflow = foreachFlow
628+
}
629+
} else if (kind === 'map' || kind === 'flatmap' || kind === 'foreach') {
630+
const flow = node.data.workflow
631+
const nodeLoop = connections.find(conn => conn.sourceHandle.startsWith('Item'))
632+
633+
if (nodeLoop) {
634+
const node = nodeToJson(nodes.find(n => n.id === nodeLoop.target), emptyWorkflow)
635+
subflow = {
636+
...flow,
637+
node
638+
}
639+
} else {
640+
subflow = flow
641+
}
642+
} else if (kind === 'filter') {
643+
const targets = edges.filter(edge => edge.target === node.id)
644+
const predicateFlow = node.data.workflow
645+
const predicate = targets.find(conn => conn.targetHandle.startsWith('predicate'))
646+
647+
if (predicate) {
648+
subflow = {
649+
...predicateFlow,
650+
predicate: nodeToJson(nodes.find(n => n.id === predicate.source), undefined, true)
651+
}
652+
} else {
653+
subflow = predicateFlow
623654
}
624-
} else if (kind === 'map') {
655+
} else if (kind === 'parallel' || kind === 'switch') {
656+
const paths = connections.map(conn => nodes.find(n => n.id === conn.target))
625657

626-
} else if (kind === 'switch') {
658+
subflow = paths.reduce((acc, path) => {
659+
return {
660+
...acc,
661+
paths: [...acc.paths, nodeToJson(path, emptyWorkflow)]
662+
}
663+
}, {
664+
...node.data.workflow,
665+
paths: []
666+
})
627667

628668
} else {
629-
return node.data.workflow
669+
subflow = node.data.workflow
630670
}
671+
672+
let outputWorkflow = subflow
673+
674+
if (currentWorkflow && currentWorkflow.kind === 'workflow') {
675+
outputWorkflow = {
676+
...currentWorkflow,
677+
steps: [...currentWorkflow.steps, subflow]
678+
}
679+
}
680+
681+
const output = connections.find(conn => conn.sourceHandle.startsWith('output'))
682+
683+
if (output && !disableRecursion)
684+
return nodeToJson(nodes.find(n => n.id === output.target), outputWorkflow)
685+
686+
console.log(outputWorkflow, currentWorkflow)
687+
return outputWorkflow
631688
}
632689

633690
const handleSave = () => {

otoroshi/javascript/src/extensions/workflows/nodes/CallNode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Row } from '../../../components/Row';
55

66
export function CallNode(_workflow) {
77
return {
8-
label: <i className='fas fa-play' />,
8+
label: <i className='fas fa-code' />,
99
name: 'Call',
1010
kind: 'call',
1111
description: 'Execute a function with args',
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import React from 'react'
2+
import { ValueToCheck } from '../operators/ValueToCheck'
23

34
export const PredicateNode = (_workflow) => {
45
return {
56
label: <i className='fas fa-filter' />,
6-
kind: 'PredicateNode',
7+
kind: 'predicate',
78
name: 'Predicate',
89
type: 'simple',
910
workflow: _workflow,
10-
sources: ['output']
11+
sources: ['output'],
12+
targets: ['PredicateOperator'],
13+
schema: {
14+
value: ValueToCheck('Predicate', false)
15+
},
16+
flow: ['value']
1117
}
1218
}

otoroshi/javascript/src/extensions/workflows/nodes/ValueNode.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react'
2+
import { ValueToCheck } from '../operators/ValueToCheck'
23

34
export const ValueNode = (_workflow) => ({
45
label: <i className='fas fa-cube' />,
@@ -7,6 +8,10 @@ export const ValueNode = (_workflow) => ({
78
workflow: _workflow,
89
kind: 'value',
910
sources: ['output'],
11+
schema: {
12+
value: ValueToCheck('Value', false)
13+
},
14+
flow: ['value']
1015
// {
1116
// "kind": "workflow",
1217
// "steps": [ <Node> ],

otoroshi/javascript/src/style/components/_workflow.scss

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@
138138
justify-content: flex-start;
139139
gap: .5rem;
140140
background-color: var(--bg-color_level2);
141-
border-top-left-radius: .75rem;
142-
border-top-right-radius: .75rem;
141+
border-top-left-radius: .5rem;
142+
border-top-right-radius: .5rem;
143143
}
144144

145145
// .source::before {
@@ -267,6 +267,10 @@
267267
justify-content: center;
268268
}
269269

270+
.react-flow__node-group .node-one-output {
271+
background-color: var(--bg-color_level1);
272+
}
273+
270274
.node-group-label {
271275
border: none !important;
272276
background: var(--bg-color_level2) !important;

0 commit comments

Comments
 (0)