handreyrc commented on code in PR #3252: URL: https://github.com/apache/incubator-kie-tools/pull/3252#discussion_r2616007987
########## packages/serverless-workflow-editor/src/diagram/Diagram.tsx: ########## @@ -0,0 +1,793 @@ +/* + * 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 { useOnViewportChange, Viewport } from "reactflow"; +import * as React from "react"; +import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react"; +import { Button, ButtonVariant } from "@patternfly/react-core/dist/js/components/Button"; +import { + EmptyState, + EmptyStateIcon, + EmptyStateBody, + EmptyStateHeader, +} from "@patternfly/react-core/dist/js/components/EmptyState"; +import { Label } from "@patternfly/react-core/dist/js/components/Label"; +import { Popover } from "@patternfly/react-core/dist/js/components/Popover"; +import { Title } from "@patternfly/react-core/dist/js/components/Title"; +import { Bullseye } from "@patternfly/react-core/dist/js/layouts/Bullseye"; +import { MousePointerIcon } from "@patternfly/react-icons/dist/js/icons/mouse-pointer-icon"; +import { TimesIcon } from "@patternfly/react-icons/dist/js/icons/times-icon"; +import { VirtualMachineIcon } from "@patternfly/react-icons/dist/js/icons/virtual-machine-icon"; +import { useSwfEditor } from "../SwfEditorContext"; +import { AutolayoutButton } from "../autolayout/AutolayoutButton"; +import { addEdge } from "../mutations/addEdge"; +import { deleteEdge } from "../mutations/deleteEdge"; +import { OverlaysPanel } from "../overlaysPanel/OverlaysPanel"; +import { SnapGrid, State } from "../store/Store"; +import { useSwfEditorStore, useSwfEditorStoreApi } from "../store/StoreContext"; +import { DiagramContainerContextProvider } from "./DiagramContainerContext"; +import { ConnectionLine } from "./connections/ConnectionLine"; +import { PositionalNodeHandleId } from "./connections/PositionalNodeHandles"; +import { containment, EdgeType, getDefaultEdgeTypeBetween, NodeType } from "./connections/graphStructure"; +import { checkIsValidConnection } from "./connections/isValidConnection"; +import { EdgeMarkers } from "./edges/EdgeMarkers"; +import { EDGE_TYPES } from "./edges/SwfEdgeTypes"; +import { + TransitionEdge, + SwfDiagramEdgeData, + ErrorTransitionEdge, + EventConditionTransitionEdge, + DefaultConditionTransitionEdge, + CompensationTransitionEdge, + DataConditionTransitionEdge, +} from "./edges/SwfEdges"; +import { buildHierarchy } from "./graph/graph"; +import { getContainmentRelationship, getSwfBoundsCenterPoint, getHandlePosition } from "./maths/SwfMaths"; +import { DEFAULT_NODE_SIZES, MIN_NODE_SIZES } from "./nodes/SwfDefaultSizes"; +import { NODE_TYPES } from "./nodes/SwfNodeTypes"; +import { + OperationState, + SwitchState, + SleepState, + SwfDiagramNodeData, + ParallelState, + InjectState, + ForEachState, + CallbackState, + EventState, + UnknownNode, +} from "./nodes/SwfNodes"; +import { DiagramCommands } from "./DiagramCommands"; +import { getAutoLayoutedInfo } from "../autolayout/autoLayoutInfo"; +import { useSettings } from "../settings/SwfEditorSettingsContext"; +import { Flex } from "@patternfly/react-core/dist/js/layouts/Flex"; +import { applyAutoLayoutToSwf } from "../mutations/applyAutoLayoutToSwf"; + +const isFirefox = typeof (window as any).InstallTrigger !== "undefined"; // See https://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browsers + +const PAN_ON_DRAG = [1, 2]; + +const FIT_VIEW_OPTIONS: RF.FitViewOptions = { maxZoom: 1, minZoom: 0.1, duration: 400 }; + +export const DEFAULT_VIEWPORT = { x: 100, y: 100, zoom: 1 }; + +const DELETE_NODE_KEY_CODES = ["Backspace", "Delete"]; + +const AREA_ABOVE_OVERLAYS_PANEL = 120; + +const nodeTypes: Record<NodeType, any> = { + [NODE_TYPES.callbackState]: CallbackState, + [NODE_TYPES.eventState]: EventState, + [NODE_TYPES.foreachState]: ForEachState, + [NODE_TYPES.injectState]: InjectState, + [NODE_TYPES.operationState]: OperationState, + [NODE_TYPES.parallelState]: ParallelState, + [NODE_TYPES.sleepState]: SleepState, + [NODE_TYPES.switchState]: SwitchState, + [NODE_TYPES.unknown]: UnknownNode, +}; + +const edgeTypes: Record<EdgeType, any> = { + [EDGE_TYPES.compensationTransition]: CompensationTransitionEdge, + [EDGE_TYPES.dataConditionTransition]: DataConditionTransitionEdge, + [EDGE_TYPES.defaultConditionTransition]: DefaultConditionTransitionEdge, + [EDGE_TYPES.errorTransition]: ErrorTransitionEdge, + [EDGE_TYPES.eventConditionTransition]: EventConditionTransitionEdge, + [EDGE_TYPES.transition]: TransitionEdge, +}; + +const MIME_TYPE_FOR_SWF_EDITOR_NODE = "kie-swf-editor--node"; + +export type DiagramRef = { + getReactFlowInstance: () => RF.ReactFlowInstance | undefined; +}; + +export const Diagram = React.forwardRef<DiagramRef, { container: React.RefObject<HTMLElement> }>( + ({ container }, ref) => { + // Contexts + + const swfEditorStoreApi = useSwfEditorStoreApi(); + const snapGrid = useSwfEditorStore((s) => s.diagram.snapGrid); + const thisSwf = useSwfEditorStore((s) => s.swf); + const settings = useSettings(); + const { swfModelBeforeEditingRef } = useSwfEditor(); + + // State + + const [reactFlowInstance, setReactFlowInstance] = useState< + RF.ReactFlowInstance<SwfDiagramNodeData, SwfDiagramEdgeData> | undefined + >(undefined); + + const viewport = useSwfEditorStore((s) => s.diagram.viewport); + + // Refs + + React.useImperativeHandle( + ref, + () => ({ + getReactFlowInstance: () => { + return reactFlowInstance; + }, + }), + [reactFlowInstance] + ); + + const nodeIdBeingDraggedRef = useRef<string | null>(null); + + // Memos + + const rfSnapGrid = useMemo<[number, number]>( + () => (snapGrid.isEnabled ? [snapGrid.x, snapGrid.y] : [1, 1]), + [snapGrid.isEnabled, snapGrid.x, snapGrid.y] + ); + + // Callbacks + + const getFirstNodeFittingBounds = useCallback( + ( + nodeIdToIgnore: string, + bounds: RF.Rect, + minSizes: (args: { snapGrid: SnapGrid }) => RF.Dimensions, + snapGrid: SnapGrid + ) => + reactFlowInstance + ?.getNodes() + .reverse() // Respect the nodes z-index. + .find( + (node) => + node.id !== nodeIdToIgnore && // don't ever use the node being dragged + getContainmentRelationship({ + bounds: bounds!, + container: { ...DEFAULT_NODE_SIZES[node.type as NodeType]({ snapGrid: snapGrid }), ...{ x: 0, y: 0 } }, + snapGrid, + containerMinSizes: DEFAULT_NODE_SIZES[node.type as NodeType], + boundsMinSizes: minSizes, + }).isInside + ), + [reactFlowInstance] + ); + + const ongoingConnection = useSwfEditorStore((s) => s.diagram.ongoingConnection); + useEffect(() => { + const edgeUpdaterSource = document.querySelectorAll( + ".react-flow__edgeupdater-source, .react-flow__edgeupdater-target" + ); + if (ongoingConnection) { + edgeUpdaterSource.forEach((e) => e.classList.add("hidden")); + } else { + edgeUpdaterSource.forEach((e) => e.classList.remove("hidden")); + } + }, [ongoingConnection]); Review Comment: Looking at the highlighted code above, connection rules can become pretty specific. I agree that maybe we could abstract it and then take specific connection rules as an input. Some time ago I asked about that. By that time the answer was no. The editors were being developed independently of each other to a certain extent. -- 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]
