Skip to content

Commit

Permalink
feat(Edge): Use Decorator for adding a + icon
Browse files Browse the repository at this point in the history
  • Loading branch information
lordrip committed Oct 17, 2024
1 parent 96bbdcb commit ec7394c
Show file tree
Hide file tree
Showing 6 changed files with 697 additions and 160 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createVisualizationNode } from '../../../models/visualization';
import { CamelRouteVisualEntity, createVisualizationNode } from '../../../models/visualization';
import { BaseVisualCamelEntity } from '../../../models/visualization/base-visual-entity';
import { FlowService } from './flow.service';

Expand Down Expand Up @@ -86,18 +86,7 @@ describe('FlowService', () => {
});

it('should return a group node for a multiple nodes VisualizationNode with a group', () => {
const routeNode = createVisualizationNode('route', {
entity: { getId: () => 'myId' } as BaseVisualCamelEntity,
isGroup: true,
});

const fromNode = createVisualizationNode('timer', {
path: 'from',
icon: undefined,
processorName: 'from',
componentName: 'timer',
});
routeNode.addChild(fromNode);
const routeNode = new CamelRouteVisualEntity({ from: { uri: 'timer:clock', steps: [] } }).toVizNode();

const { nodes, edges } = FlowService.getFlowDiagram(routeNode);

Expand Down

This file was deleted.

62 changes: 33 additions & 29 deletions packages/ui/src/components/Visualization/Custom/Edge/EdgeEnd.tsx
Original file line number Diff line number Diff line change
@@ -1,53 +1,57 @@
import { DefaultEdge, Edge, EdgeModel, EdgeTerminalType, observer } from '@patternfly/react-topology';
import { Button, Tooltip } from '@patternfly/react-core';
import { PlusIcon } from '@patternfly/react-icons';
import './CustomEdge.scss';
import { addNode } from '../ContextMenu/ItemAddStep';
import { CatalogModalContext, EntitiesContext } from '../../../../providers';
import { useContext } from 'react';
import {
Decorator,
DefaultEdge,
EdgeModel,
EdgeTerminalType,
GraphElement,
isEdge,
observer,
} from '@patternfly/react-topology';
import { FunctionComponent, useCallback, useContext } from 'react';
import { IVisualizationNode } from '../../../../models';
import { CatalogModalContext, EntitiesContext } from '../../../../providers';
import { addNode } from '../ContextMenu/ItemAddStep';
import { LayoutType } from '../../Canvas';

interface EdgeEndProps {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
element: Edge<EdgeModel, any>;
type DefaultEdgeProps = Parameters<typeof DefaultEdge>[0];
interface EdgeEndProps extends DefaultEdgeProps {
/** We're not providing Data to edges */
element: GraphElement<EdgeModel, unknown>;
}

const EdgeEnd: React.FC<EdgeEndProps> = observer(({ element, ...rest }) => {
export const EdgeEndWithButton: FunctionComponent<EdgeEndProps> = observer(({ element, ...rest }) => {
if (!isEdge(element)) {
throw new Error('EdgeEndWithButton must be used only on Edge elements');

Check warning on line 25 in packages/ui/src/components/Visualization/Custom/Edge/EdgeEnd.tsx

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/components/Visualization/Custom/Edge/EdgeEnd.tsx#L25

Added line #L25 was not covered by tests
}
const entitiesContext = useContext(EntitiesContext);
const catalogModalContext = useContext(CatalogModalContext);
const vizNode: IVisualizationNode = element.getSource().getData()?.vizNode;
const isHorizontal = element.getGraph().getLayout() === 'DagreHorizontal';
const isHorizontal = element.getGraph().getLayout() === LayoutType.DagreHorizontal;
const endPoint = element.getEndPoint();
let x, y;

const onAdd = useCallback(() => {
addNode(catalogModalContext, entitiesContext, vizNode);

Check warning on line 34 in packages/ui/src/components/Visualization/Custom/Edge/EdgeEnd.tsx

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/components/Visualization/Custom/Edge/EdgeEnd.tsx#L34

Added line #L34 was not covered by tests
}, [catalogModalContext, entitiesContext, vizNode]);

let x = endPoint.x;
let y = endPoint.y;
if (isHorizontal) {
x = endPoint.x;
y = endPoint.y - 12;
x += 12;

Check warning on line 40 in packages/ui/src/components/Visualization/Custom/Edge/EdgeEnd.tsx

View check run for this annotation

Codecov / codecov/patch

packages/ui/src/components/Visualization/Custom/Edge/EdgeEnd.tsx#L40

Added line #L40 was not covered by tests
} else {
x = endPoint.x - 12;
y = endPoint.y;
y += 4;
}
const onAdd = () => {
addNode(catalogModalContext, entitiesContext, vizNode);
};

return (
<DefaultEdge
element={element}
startTerminalType={EdgeTerminalType.none}
endTerminalType={EdgeTerminalType.none}
className="custom-edge"
{...rest}
>
<g data-testid={`custom-edge__${element?.getId()}`}>
<foreignObject x={x} y={y} width="24" height="24" className="custom-edge">
<Tooltip content="Append">
<Button variant="plain" className="custom-edge__end" onClick={onAdd}>
<PlusIcon size={40} />
</Button>
</Tooltip>
</foreignObject>
<Decorator showBackground radius={12} x={x} y={y} icon={<PlusIcon />} onClick={onAdd} />
</g>
</DefaultEdge>
);
});

export const EdgeEndWithButton: typeof DefaultEdge = EdgeEnd as typeof DefaultEdge;
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { BaseEdge, getTopCollapsedParent, Point } from '@patternfly/react-topology';
import { LayoutType } from '../Canvas';

export class NoBendpointsEdge extends BaseEdge {
getBendpoints(): Point[] {
return [];
}

getStartPoint(): Point {
if (this.getTarget() === this.getSource()) {
const parent = getTopCollapsedParent(this.getSource());
const isHorizontal = this.getGraph().getLayout() === 'DagreHorizontal';
const isHorizontal = this.getGraph().getLayout() === LayoutType.DagreHorizontal;
const parentPos = parent.getPosition();
const parentSize = parent.getDimensions();
let x, y;
Expand All @@ -29,14 +31,15 @@ export class NoBendpointsEdge extends BaseEdge {
}
}
return new Point(x, y);
} else {
return super.getStartPoint();
}

return super.getStartPoint();
}

getEndPoint(): Point {
if (this.getTarget() === this.getSource()) {
const parent = getTopCollapsedParent(this.getSource());
const isHorizontal = this.getGraph().getLayout() === 'DagreHorizontal';
const isHorizontal = this.getGraph().getLayout() === LayoutType.DagreHorizontal;
const parentPos = parent.getPosition();
const parentSize = parent.getDimensions();
let x, y;
Expand All @@ -58,8 +61,8 @@ export class NoBendpointsEdge extends BaseEdge {
}
}
return new Point(x, y);
} else {
return super.getEndPoint();
}

return super.getEndPoint();
}
}
21 changes: 21 additions & 0 deletions packages/ui/src/tests/__snapshots__/nodes-edges.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4922,12 +4922,33 @@ exports[`Nodes and Edges should generate edges for steps with branches 2`] = `
"target": "choice-1234",
"type": "edge",
},
{
"edgeStyle": "dashed",
"id": "setHeader-1234-end",
"source": "setHeader-1234",
"target": "setHeader-1234",
"type": "edge-end",
},
{
"edgeStyle": "dashed",
"id": "log-1234-end",
"source": "log-1234",
"target": "log-1234",
"type": "edge-end",
},
{
"edgeStyle": "solid",
"id": "choice-1234-to-sql-1234",
"source": "choice-1234",
"target": "sql-1234",
"type": "edge",
},
{
"edgeStyle": "dashed",
"id": "sql-1234-end",
"source": "sql-1234",
"target": "sql-1234",
"type": "edge-end",
},
]
`;

0 comments on commit ec7394c

Please sign in to comment.