handreyrc commented on code in PR #3252: URL: https://github.com/apache/incubator-kie-tools/pull/3252#discussion_r2621335782
########## packages/serverless-workflow-editor/src/store/computed/computeDiagramData.ts: ########## @@ -0,0 +1,524 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import * as RF from "reactflow"; +import { snapShapeDimensions, snapShapePosition } from "../../diagram/SnapGrid"; +import { EdgeType, NodeType } from "../../diagram/connections/graphStructure"; +import { SwfDiagramEdgeData } from "../../diagram/edges/SwfEdges"; +import { + SwfEdge, + SwfEdgeTypes, + SwfAdjacencyList, + EdgeVisitor, + NodeVisitor, + getAdjMatrix, + traverse, +} from "../../diagram/graph/graph"; +import { getNodeTypeFromSwfObject } from "../../diagram/maths/SwfMaths"; +import { DEFAULT_NODE_SIZES, MIN_NODE_SIZES } from "../../diagram/nodes/SwfDefaultSizes"; +import { ___NASTY_HACK_FOR_SAFARI_to_force_redrawing_svgs_and_avoid_repaint_glitches } from "../../diagram/nodes/SwfNodeSvgs"; +import { SwfDiagramNodeData, NodeSwfObjects } from "../../diagram/nodes/SwfNodes"; +import { TypeOrReturnType } from "../ComputedStateCache"; +import { Computed, State } from "../Store"; +import { Specification } from "@serverlessworkflow/sdk-typescript"; +import { Unpacked } from "../../tsExt/tsExt"; + +export const NODE_LAYERS = { + NODES: 1000, // We need a difference > 1000 here, since ReactFlow will add 1000 to the z-index when a node is selected. +}; + +interface EdgeArgs { + id: string; + swfObject: SwfDiagramEdgeData["swfObject"]; + type: EdgeType; + source: string; + target: string; +} + +type AckEdge = (args: EdgeArgs) => RF.Edge<SwfDiagramEdgeData>; + +type AckNode = (swfObject: NodeSwfObjects, index: number) => RF.Node<SwfDiagramNodeData> | undefined; + +export function computeDiagramData( + diagram: State["diagram"], + definitions: State["swf"]["model"], + indexedSwf: TypeOrReturnType<Computed["indexedSwf"]>, + nodeIds: Array<string>, + nodesPosition: Array<RF.XYPosition>, + edgeIds: Array<string>, + edgeWaypoints: Array<RF.XYPosition[]>, + requiresLayout?: boolean +) { + // console.time("nodes"); + ___NASTY_HACK_FOR_SAFARI_to_force_redrawing_svgs_and_avoid_repaint_glitches.flag = + !___NASTY_HACK_FOR_SAFARI_to_force_redrawing_svgs_and_avoid_repaint_glitches.flag; + + const selectedNodesById = new Map<string, RF.Node<SwfDiagramNodeData>>(); + const selectedEdgesById = new Map<string, RF.Edge<SwfDiagramEdgeData>>(); + const selectedNodeTypes = new Set<NodeType>(); + const nodesById = new Map<string, RF.Node<SwfDiagramNodeData>>(); + const edgesById = new Map<string, RF.Edge<SwfDiagramEdgeData>>(); + const edges: RF.Edge<SwfDiagramEdgeData>[] = []; + const swfEdges: SwfEdge[] = []; + const swfAdjacencyList: SwfAdjacencyList = new Map(); + + if (requiresLayout && (!nodeIds || nodeIds.length == 0)) { + return { + swfEdges, + swfAdjacencyList, + nodes: [] as RF.Node<SwfDiagramNodeData>[], + edges, + edgesById, + nodesById, + selectedNodeTypes, + selectedNodesById, + selectedEdgesById, + }; + } + + const { selectedNodes, draggingNodes, selectedEdges } = { + selectedNodes: new Set(diagram._selectedNodes), + draggingNodes: new Set(diagram.draggingNodes), + selectedEdges: new Set(diagram._selectedEdges), + }; + + // console.time("edges"); + const ackEdge: AckEdge = ({ id, type, swfObject, source, target }) => { + const data = { + swfObject, + swfEdge: id ? indexedSwf.swfEdgesBySwfRef.get(id) : undefined, + swfSource: indexedSwf.swfNodesByHref.get(source), + swfTarget: indexedSwf.swfNodesByHref.get(target), + }; + + const edge: RF.Edge<SwfDiagramEdgeData> = { + data, + id, + type, + source, + target, + selected: selectedEdges.has(id), + }; + + edgesById.set(edge.id, edge); + if (edge.selected) { + selectedEdgesById.set(edge.id, edge); + } + + edges.push(edge); + + swfEdges.push({ id, sourceId: source, targetId: target, swfObject }); + + const targetAdjancyList = swfAdjacencyList.get(target); + if (!targetAdjancyList) { + swfAdjacencyList.set(target, { dependencies: new Set([source]) }); + } else { + targetAdjancyList.dependencies.add(source); + } + + return edge; + }; + + // trasitions + ackTransitionEdges(definitions.states, ackEdge); + + // console.timeEnd("edges"); + + // nodes + const ackNode: AckNode = (swfObject, index) => { + const type = getNodeTypeFromSwfObject(swfObject); + if (!type) { + return undefined; + } + + const nodeName = swfObject!.name!; + + const data: SwfDiagramNodeData = { + swfObject, + index, + parentRfNode: undefined, + }; + + // if there is no position calculated for the node in the state go for x:0 y:0 + const i = nodeIds.indexOf(nodeName); + const position = nodeIds.indexOf(nodeName) === -1 ? { x: 0, y: 0 } : nodesPosition[i]; Review Comment: Fixed! -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
