diff --git a/webapp/src/components/DAGSvgs/DAGBoard.tsx b/webapp/src/components/DAGSvgs/DAGBoard.tsx index 8659f13..790a77f 100644 --- a/webapp/src/components/DAGSvgs/DAGBoard.tsx +++ b/webapp/src/components/DAGSvgs/DAGBoard.tsx @@ -136,7 +136,7 @@ export const DAGBoard: FC = props => { const [[x0, y0], [x1, y1]] = it.returnValue as BBox; return [x + ox, y + oy]; -; + +const makeOffsetMaintainer = (startPoint: Point) => { + let offset: Point = [0, 0]; + let i = 0; + return { + setI(newI: number) { + i = newI; + }, + resetY() { + offset[1] = 0; + }, + update(p: Point, innerStartLoc: Point) { + offset[0] = Math.max(offset[0], p[0] - i - 0.5 - startPoint[0]) + offset[1] = Math.max(offset[1], p[1] - innerStartLoc[1] + 1) + }, + get x() { + return offset[0] + }, + get y() { + return offset[1] + } + } +} function* inner( nodeLoc: Point, @@ -34,28 +57,45 @@ function* inner( } } -const makeOffsetMaintainer = (startPoint: Point) => { - let offset: Point = [0, 0]; - let i = 0; - return { - setI(newI: number) { - i = newI; - }, - resetY() { - offset[1] = 0; - }, - update(p: Point, innerStartLoc: Point) { - offset[0] = Math.max(offset[0], p[0] - i - 0.5 - startPoint[0]) - offset[1] = Math.max(offset[1], p[1] - innerStartLoc[1] + 1) - }, - get x() { - return offset[0] - }, - get y() { - return offset[1] +function* dealWithChoose( + node: ChooseAction, + nodeLoc: Point, + makeUpdater: (j: number | null) => Updater, + offset: ReturnType, + keyPrefix: string, +) { + offset.resetY() + for (let j = 0; j < node.action_data.choose.length; j++) { + const innerStartLoc = offsetPoint(nodeLoc, [1, j + 1 + offset.y]); + for (let elm of inner( + nodeLoc, + innerStartLoc , + makeUpdater(j), + `${keyPrefix}.${j}`, + node.action_data.choose[j].sequence + )) { + yield elm + if (elm.type === 'node') { + offset.update(elm.loc, innerStartLoc) + } + } + } + const innerStartLoc = offsetPoint(nodeLoc, [1, node.action_data.choose.length + 1 + offset.y]); + for (let elm of inner( + nodeLoc, + innerStartLoc, + makeUpdater(null), + `${keyPrefix}-d`, + node.action_data.default, + )) { + yield elm + if (elm.type === 'node') { + offset.update(elm.loc, innerStartLoc) } } } + + export function* computeNodesEdgesPos( startPoint: Point, sequence: AutomationSequenceNode[], @@ -64,7 +104,7 @@ export function* computeNodesEdgesPos( ): Generator { const rootUpdater = makeUpdater(sequence, onChange); const offset = makeOffsetMaintainer(startPoint) - + let lastNodeLoc: Point | null = null; for (let i = 0; i < sequence.length; i++) { offset.setI(i) const node = sequence[i]; @@ -77,48 +117,26 @@ export function* computeNodesEdgesPos( onRemove: () => rootUpdater.removeNode(i), onAdd: i === sequence.length - 1 ? rootUpdater.addNode : undefined, } - if (i > 0) { + if (lastNodeLoc) { yield { type: 'edge', key: `${keyPrefix}(${i-1}->${i})`, - p1: offsetPoint(startPoint, [i - 1, 0]), - p2: offsetPoint(nodeLoc, [0, 0]), + p1: lastNodeLoc, + p2: nodeLoc, direction: '1->2', } } + lastNodeLoc = [...nodeLoc]; if ( (node.$smType === 'action') && (node.action === 'choose') ) { - offset.resetY() - - for (let j = 0; j < node.action_data.choose.length; j++) { - const innerStartLoc = offsetPoint(nodeLoc, [1, j + 1 + offset.y]); - for (let elm of inner( - nodeLoc, - innerStartLoc , - rootUpdater.makeChildUpdaterForChooseAction(i, j), - `${keyPrefix}.${i}.${j}`, - node.action_data.choose[j].sequence - )) { - yield elm - if (elm.type === 'node') { - offset.update(elm.loc, innerStartLoc) - } - } - } - const innerStartLoc = offsetPoint(nodeLoc, [1, node.action_data.choose.length + 1 + offset.y]); - for (let elm of inner( + yield* dealWithChoose( + node, nodeLoc, - innerStartLoc, - rootUpdater.makeChildUpdaterForChooseAction(i, null), - `${keyPrefix}${i}-d`, - node.action_data.default, - )) { - yield elm - if (elm.type === 'node') { - offset.update(elm.loc, innerStartLoc) - } - } + j => rootUpdater.makeChildUpdaterForChooseAction(i, j), + offset, + `${keyPrefix}.${i}`, + ) } } }