Skip to content

Commit

Permalink
#5237 Clean and fix for issue with edges to the diamond shape
Browse files Browse the repository at this point in the history
  • Loading branch information
knsv committed Aug 21, 2024
1 parent 10c2534 commit 9134e1c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 62 deletions.
24 changes: 15 additions & 9 deletions cypress/platform/knsv2.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,20 @@
---
config:
look: handDrawn
flowchart:
htmlLabels: false
layout: elk
elk:
nodePlacementStrategy: BRANDES_KOEPF
---
flowchart
A[I am a long text, where do I go??? handdrawn - false]
flowchart TB
%% A --> C
B -- sdf sdf --> C{"Evaluate"}
C --> n4["salkdfjh akljd lkasdjf lkashj lksjadf klasdh"]
C --> F


</pre
>
<pre id="diagram" class="mermaid">
<pre id="diagram" class="mermaid2">
---
config:
look: handdrawn
Expand All @@ -106,7 +112,7 @@
>
</div>
<div class="flex">
<pre id="diagram" class="mermaid">
<pre id="diagram" class="mermaid2">
---
config:
flowchart:
Expand All @@ -116,7 +122,7 @@
A[I am a long text, where do I go??? classic - false]
</pre
>
<pre id="diagram" class="mermaid">
<pre id="diagram" class="mermaid2">
---
config:
flowchart:
Expand Down Expand Up @@ -159,8 +165,8 @@

<script type="module">
import mermaid from './mermaid.esm.mjs';
// import layouts from './mermaid-layout-elk.esm.mjs';
// mermaid.registerLayoutLoaders(layouts);
import layouts from './mermaid-layout-elk.esm.mjs';
mermaid.registerLayoutLoaders(layouts);
mermaid.parseError = function (err, hash) {
console.error('Mermaid error: ', err);
};
Expand Down
66 changes: 14 additions & 52 deletions packages/mermaid-layout-elk/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export const render = async (
{ algorithm }: RenderOptions
) => {
const nodeDb: Record<string, any> = {};

Check warning on line 25 in packages/mermaid-layout-elk/src/render.ts

View workflow job for this annotation

GitHub Actions / autofix

Unexpected any. Specify a different type
const portPos: Record<string, any> = {};
const clusterDb: Record<string, any> = {};

Check warning on line 26 in packages/mermaid-layout-elk/src/render.ts

View workflow job for this annotation

GitHub Actions / autofix

Unexpected any. Specify a different type

const addVertex = async (nodeEl: any, graph: { children: any[] }, nodeArr: any, node: any) => {

Check warning on line 28 in packages/mermaid-layout-elk/src/render.ts

View workflow job for this annotation

GitHub Actions / autofix

Unexpected any. Specify a different type

Check warning on line 28 in packages/mermaid-layout-elk/src/render.ts

View workflow job for this annotation

GitHub Actions / autofix

Unexpected any. Specify a different type

Check warning on line 28 in packages/mermaid-layout-elk/src/render.ts

View workflow job for this annotation

GitHub Actions / autofix

Unexpected any. Specify a different type

Check warning on line 28 in packages/mermaid-layout-elk/src/render.ts

View workflow job for this annotation

GitHub Actions / autofix

Unexpected any. Specify a different type
Expand Down Expand Up @@ -169,42 +168,6 @@ export const render = async (
);
};

const getNextPort = (node: string | number, edgeDirection: string, graphDirection: any) => {
log.info('getNextPort abc88', { node, edgeDirection, graphDirection });
if (!portPos[node]) {
switch (graphDirection) {
case 'TB':
case 'TD':
portPos[node] = {
inPosition: 'north',
outPosition: 'south',
};
break;
case 'BT':
portPos[node] = {
inPosition: 'south',
outPosition: 'north',
};
break;
case 'RL':
portPos[node] = {
inPosition: 'east',
outPosition: 'west',
};
break;
case 'LR':
portPos[node] = {
inPosition: 'west',
outPosition: 'east',
};
break;
}
}
const result = edgeDirection === 'in' ? portPos[node].inPosition : portPos[node].outPosition;

return result;
};

const addSubGraphs = (nodeArr: any[]): TreeData => {
const parentLookupDb: TreeData = { parentById: {}, childrenById: {} };
const subgraphs = nodeArr.filter((node: { isGroup: any }) => node.isGroup);
Expand All @@ -229,9 +192,9 @@ export const render = async (
return parentLookupDb;
};

const getEdgeStartEndPoint = (edge: any, dir: any) => {
let source: any = edge.start;
let target: any = edge.end;
const getEdgeStartEndPoint = (edge: any) => {
const source: any = edge.start;
const target: any = edge.end;

// Save the original source and target
const sourceId = source;
Expand All @@ -244,14 +207,6 @@ export const render = async (
return { source, target };
}

if (startNode.shape === 'diamond') {
source = `${source}-${getNextPort(source, 'out', dir)}`;
}

if (endNode.shape === 'diamond') {
target = `${target}-${getNextPort(target, 'in', dir)}`;
}

// Add the edge to the graph
return { source, target, sourceId, targetId };
};
Expand Down Expand Up @@ -980,10 +935,17 @@ export const render = async (
});
}
if (endNode.shape === 'diamond') {
edge.points.push({
x: endNode.x + endNode.width / 2 + offset.x,
y: endNode.y + endNode.height / 2 + offset.y,
});
const x = endNode.x + endNode.width / 2 + offset.x;
// Add a point at the center of the diamond
if (
Math.abs(edge.points[edge.points.length - 1].y - endNode.y - offset.y) > 0.001 ||
Math.abs(edge.points[edge.points.length - 1].x - x) > 0.001
) {
edge.points.push({
x: endNode.x + endNode.width / 2 + offset.x,
y: endNode.y + endNode.height / 2 + offset.y,
});
}
}

edge.points = cutPathAtIntersect(
Expand Down
21 changes: 21 additions & 0 deletions packages/mermaid/src/rendering-util/rendering-elements/edges.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,27 @@ export const insertEdge = function (elem, edge, clusterDb, diagramType, startNod
.attr('style', edgeStyles ? edgeStyles.reduce((acc, style) => acc + ';' + style, '') : '');
}

// DEBUG code, DO NOT REMOVE
// adds a red circle at each edge coordinate
// cornerPoints.forEach((point) => {
// elem
// .append('circle')
// .style('stroke', 'blue')
// .style('fill', 'blue')
// .attr('r', 3)
// .attr('cx', point.x)
// .attr('cy', point.y);
// });
// lineData.forEach((point) => {
// elem
// .append('circle')
// .style('stroke', 'blue')
// .style('fill', 'blue')
// .attr('r', 3)
// .attr('cx', point.x)
// .attr('cy', point.y);
// });

let url = '';
if (getConfig().flowchart.arrowMarkerAbsolute || getConfig().state.arrowMarkerAbsolute) {
url =
Expand Down
2 changes: 1 addition & 1 deletion packages/mermaid/src/schemas/config.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ properties:
- NETWORK_SIMPLEX
- LINEAR_SEGMENTS
- BRANDES_KOEPF
default: SIMPLE
default: BRANDES_KOEPF
darkMode:
type: boolean
default: false
Expand Down

0 comments on commit 9134e1c

Please sign in to comment.