Compare commits

...

20 Commits

Author SHA1 Message Date
ashishj
ef3787c6fc #2028 Getting swimlanes to work again after merge from develop 2023-10-25 20:51:31 +02:00
ashishj
a64fd61eb9 #2028 Merge from develop 2023-10-25 20:10:55 +02:00
Knut Sveidqvist
ddc006e49b #2028 Updated layout algorithm 2023-06-08 19:19:47 +02:00
Knut Sveidqvist
fe3e471880 Merge branch 'develop' into 2028_swimlanes 2023-06-08 17:58:30 +02:00
ashishj
b12a18763c #2028 added lanes and header 2023-05-31 20:31:31 +02:00
Knut Sveidqvist
0a31ebdcd1 Update of layout algorithm for widths 2023-05-31 20:17:26 +02:00
ashishj
6fe35ef2d7 #2028 updated with first case 2023-05-31 18:41:43 +02:00
Knut Sveidqvist
e7e8db6016 Update of layout algorithm 2023-05-24 20:51:27 +02:00
Knut Sveidqvist
7f1a664ffc Merge branch 'develop' into 2028_swimlanes 2023-05-24 20:04:21 +02:00
ashishj
65a590ee39 #2028 updated with first case 2023-05-24 19:57:47 +02:00
Knut Sveidqvist
e2c2a9064f Merge branch 'develop' into 2028_swimlanes 2023-05-24 19:52:00 +02:00
Knut Sveidqvist
606385cbf0 WIP 2023-05-15 08:51:07 +02:00
ashishj
2c9da722d6 #2028 basic render 2023-05-10 20:09:39 +02:00
Knut Sveidqvist
e506893a0e #2028 returning node coordinates from layout 2023-05-10 18:25:22 +02:00
Knut Sveidqvist
48283eefd2 Fixing import 2023-05-10 17:49:45 +02:00
Knut Sveidqvist
acb8722c9c Adding ranking function and test 2023-05-07 17:33:06 +02:00
Knut Sveidqvist
49f852f615 Merge branch '2028_swimlanes' of github.com:mermaid-js/mermaid into 2028_swimlanes 2023-05-07 08:06:09 +02:00
Knut Sveidqvist
42b1fafb12 Example diagram 2023-05-07 08:06:05 +02:00
ashishj
9f77aa2e94 #2028 iter2 2023-05-03 20:00:39 +02:00
ashishj
34dbb6ba5e #2028 Initial example 2023-05-03 18:15:28 +02:00
18 changed files with 5039 additions and 5363 deletions

View File

@@ -57,360 +57,41 @@
</style> </style>
</head> </head>
<body> <body>
<pre style="background:black;color:white">
swimlane LR
subgraph "`one`"
start -- l1 --> cat --> rat
end
subgraph "`two`"
monkey -- l2 --> dog --> done2
end
subgraph "`three`"
cow --> horse --> done3
cow --> sheep --> done3
end
cat --> monkey
cow --> dog
</pre>
<pre id="diagram" class="mermaid"> <pre id="diagram" class="mermaid">
flowchart TB swimlane LR
C & D & E & F & G & H & I & J & K & L & M & N & O & P & Q & R & S & T & U & V & W & X & Y & Z & A1 & A2 & A3 & A4 & A5 & A6 & A7 & A8
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------->
C & D & E & F & G & H & I & J & K & L & M & N & O & P & Q & R & S & T & U & V & W & X & Y & Z & A1 & A2 & A3 & A4 & A5 & A6 & A7 & A8
</pre>
<pre id="diagram" class="mermaid2">
flowchart TB
A & A & A & A & A & A & A & A ---> C & D & E & F & G & H & I & J & K & L & M & N & O & P & Q & R & S & T & U & V & W & X & Y & Z
</pre>
<pre id="diagram" class="mermaid2">
flowchart TB
A1 & A2 & A3 & A4 & A5 & A6 & A7 & A8 --> C & D & E & F & G & H & I & J & K & L & M & N & O & P & Q & R & S & T & U & V & W & X & Y & Z
</pre>
<pre id="diagram" class="mermaid2">
flowchart
Node1:::class1 --> Node2:::class2
Node1:::class1 --> Node3:::class2
Node3 --> Node4((I am a circle)):::larger
classDef class1 fill:lightblue
classDef class2 fill:pink
classDef larger font-size:30px,fill:yellow
</pre
>
<pre id="diagram" class="mermaid2">
stateDiagram-v2
[*] --> Still
Still --> [*]
Still --> Moving
Moving --> Still
Moving --> Crash
Crash --> [*] </pre
>
<pre id="diagram" class="mermaid2">
flowchart RL
subgraph "`one`" subgraph "`one`"
a1 -- l1 --> a2 start -- l1 --> cat --> rat
a1 -- l2 --> a2 end
end subgraph "`two`"
</pre> monkey -- l2 --> dog --> done2
<pre id="diagram" class="mermaid2"> end
flowchart RL subgraph "`three`"
subgraph "`one`" cow --> horse --> done3
a1 -- l1 --> a2 cow --> sheep --> done3
a1 -- l2 --> a2 end
end cat --> monkey
</pre> cow --> dog
<pre id="diagram" class="mermaid2"> sheep --> dog
flowchart
id["`A root with a long text that wraps to keep the node size in check. A root with a long text that wraps to keep the node size in check`"]</pre
>
<pre id="diagram" class="mermaid2">
flowchart LR
A[A text that needs to be wrapped wraps to another line]
B[A text that needs to be<br/>wrapped wraps to another line]
C["`A text that needs to be wrapped to another line`"]</pre>
<pre id="diagram" class="mermaid2">
flowchart LR
C["`A text
that needs
to be wrapped
in another
way`"]
</pre
>
<pre id="diagram" class="mermaid2">
classDiagram-v2
note "I love this diagram!\nDo you love it?"
</pre>
<pre id="diagram" class="mermaid2">
stateDiagram-v2
State1: The state with a note with minus - and plus + in it
note left of State1
Important information! You can write
notes with . and in them.
end note </pre
>
<pre id="diagram" class="mermaid2">
mindmap
root
Child3(A node with an icon and with a long text that wraps to keep the node size in check)
</pre
>
<pre id="diagram" class="mermaid2">
%%{init: {"theme": "forest"} }%%
mindmap
id1[**Start2**<br/>end]
id2[**Start2**<br />end]
%% Another comment
id3[**Start2**<br>end] %% Comment
id4[**Start2**<br >end<br >the very end]
</pre>
<pre id="diagram" class="mermaid2">
mindmap
id1["`**Start2**
second line 😎 with long text that is wrapping to the next line`"]
id2["`Child **with bold** text`"]
id3["`Children of which some
is using *italic type of* text`"]
id4[Child]
id5["`Child
Row
and another
`"]
</pre>
<pre id="diagram" class="mermaid2">
mindmap
id1("`**Root**`"]
id2["`A formatted text... with **bold** and *italics*`"]
id3[Regular labels works as usual]
id4["`Emojis and unicode works too: 🤓
शान्तिः سلام 和平 `"]
</pre> </pre>
<pre id="diagram" class="mermaid2">
%%{init: {"flowchart": {"defaultRenderer": "elk"}} }%%
flowchart TB
%% I could not figure out how to use double quotes in labels in Mermaid
subgraph ibm[IBM Espresso CPU]
core0[IBM PowerPC Broadway Core 0]
core1[IBM PowerPC Broadway Core 1]
core2[IBM PowerPC Broadway Core 2]
rom[16 KB ROM]
core0 --- core2
rom --> core2
end
subgraph amd["`**AMD** Latte GPU`"]
mem[Memory & I/O Bridge]
dram[DRAM Controller]
edram[32 MB EDRAM MEM1]
rom[512 B SEEPROM]
sata[SATA IF]
exi[EXI]
subgraph gx[GX]
sram[3 MB 1T-SRAM]
end
radeon[AMD Radeon R7xx GX2]
mem --- gx
mem --- radeon
rom --- mem
mem --- sata
mem --- exi
dram --- sata
dram --- exi
end
ddr3[2 GB DDR3 RAM MEM2]
mem --- ddr3
dram --- ddr3
edram --- ddr3
core1 --- mem
exi --- rtc
rtc{{rtc}}
</pre
>
<pre id="diagram" class="mermaid2">
%%{init: {"flowchart": {"defaultRenderer": "elk", "htmlLabels": false}} }%%
flowchart TB
%% I could not figure out how to use double quotes in labels in Mermaid
subgraph ibm[IBM Espresso CPU]
core0[IBM PowerPC Broadway Core 0]
core1[IBM PowerPC Broadway Core 1]
core2[IBM PowerPC Broadway Core 2]
rom[16 KB ROM]
core0 --- core2
rom --> core2
end
subgraph amd["`**AMD** Latte GPU`"]
mem[Memory & I/O Bridge]
dram[DRAM Controller]
edram[32 MB EDRAM MEM1]
rom[512 B SEEPROM]
sata[SATA IF]
exi[EXI]
subgraph gx[GX]
sram[3 MB 1T-SRAM]
end
radeon[AMD Radeon R7xx GX2]
mem --- gx
mem --- radeon
rom --- mem
mem --- sata
mem --- exi
dram --- sata
dram --- exi
end
ddr3[2 GB DDR3 RAM MEM2]
mem --- ddr3
dram --- ddr3
edram --- ddr3
core1 --- mem
exi --- rtc
rtc{{rtc}}
</pre
>
<br />
<pre id="diagram" class="mermaid2">
flowchart TB
%% I could not figure out how to use double quotes in labels in Mermaid
subgraph ibm[IBM Espresso CPU]
core0[IBM PowerPC Broadway Core 0]
core1[IBM PowerPC Broadway Core 1]
core2[IBM PowerPC Broadway Core 2]
rom[16 KB ROM]
core0 --- core2
rom --> core2
end
subgraph amd[AMD Latte GPU]
mem[Memory & I/O Bridge]
dram[DRAM Controller]
edram[32 MB EDRAM MEM1]
rom[512 B SEEPROM]
sata[SATA IF]
exi[EXI]
subgraph gx[GX]
sram[3 MB 1T-SRAM]
end
radeon[AMD Radeon R7xx GX2]
mem --- gx
mem --- radeon
rom --- mem
mem --- sata
mem --- exi
dram --- sata
dram --- exi
end
ddr3[2 GB DDR3 RAM MEM2]
mem --- ddr3
dram --- ddr3
edram --- ddr3
core1 --- mem
exi --- rtc
rtc{{rtc}}
</pre
>
<br />
&nbsp;
<pre id="diagram" class="mermaid2">
flowchart LR
B1 --be be--x B2
B1 --bo bo--o B3
subgraph Ugge
B2
B3
subgraph inner
B4
B5
end
subgraph inner2
subgraph deeper
C4
C5
end
C6
end
B4 --> C4
B3 -- X --> B4
B2 --> inner
C4 --> C5
end
subgraph outer
B6
end
B6 --> B5
</pre
>
<pre id="diagram" class="mermaid2">
sequenceDiagram
Customer->>+Stripe: Makes a payment request
Stripe->>+Bank: Forwards the payment request to the bank
Bank->>+Customer: Asks for authorization
Customer->>+Bank: Provides authorization
Bank->>+Stripe: Sends a response with payment details
Stripe->>+Merchant: Sends a notification of payment receipt
Merchant->>+Stripe: Confirms the payment
Stripe->>+Customer: Sends a confirmation of payment
Customer->>+Merchant: Receives goods or services
</pre
>
<pre id="diagram" class="mermaid2">
mindmap
root((mindmap))
Origins
Long history
::icon(fa fa-book)
Popularisation
British popular psychology author Tony Buzan
Research
On effectiveness<br/>and features
On Automatic creation
Uses
Creative techniques
Strategic planning
Argument mapping
Tools
Pen and paper
Mermaid
</pre>
<br />
<pre id="diagram" class="mermaid2">
example-diagram
</pre>
monkey dog --> cat monkey --> cow -->
<!-- <div id="cy"></div> --> <!-- <div id="cy"></div> -->
<!-- <script src="http://localhost:9000/packages/mermaid-mindmap/dist/mermaid-mindmap-detector.js"></script> --> <!-- <script src="http://localhost:9000/packages/mermaid-mindmap/dist/mermaid-mindmap-detector.js"></script> -->
<!-- <script src="./mermaid-example-diagram-detector.js"></script> --> <!-- <script src="./mermaid-example-diagram-detector.js"></script> -->
@@ -425,32 +106,21 @@ mindmap
mermaid.parseError = function (err, hash) { mermaid.parseError = function (err, hash) {
// console.error('Mermaid error: ', err); // console.error('Mermaid error: ', err);
}; };
// mermaid.initialize({
// // theme: 'forest',
// startOnLoad: true,
// logLevel: 0,
// flowchart: {
// // defaultRenderer: 'elk',
// useMaxWidth: false,
// // htmlLabels: false,
// htmlLabels: true,
// },
// // htmlLabels: false,
// gantt: {
// useMaxWidth: false,
// },
// useMaxWidth: false,
// });
mermaid.initialize({ mermaid.initialize({
flowchart: { titleTopMargin: 10 }, // theme: 'forest',
fontFamily: 'courier', startOnLoad: true,
sequence: {
actorFontFamily: 'courier',
noteFontFamily: 'courier',
messageFontFamily: 'courier',
},
fontSize: 16,
logLevel: 0, logLevel: 0,
flowchart: {
// defaultRenderer: 'elk',
useMaxWidth: false,
// htmlLabels: false,
htmlLabels: true,
},
// htmlLabels: false,
gantt: {
useMaxWidth: false,
},
useMaxWidth: false,
}); });
function callback() { function callback() {
alert('It worked'); alert('It worked');

View File

@@ -14,7 +14,7 @@ import { insertCluster, clear as clearClusters } from './clusters.js';
import { insertEdgeLabel, positionEdgeLabel, insertEdge, clear as clearEdges } from './edges.js'; import { insertEdgeLabel, positionEdgeLabel, insertEdge, clear as clearEdges } from './edges.js';
import { log } from '../logger.js'; import { log } from '../logger.js';
const recursiveRender = async (_elem, graph, diagramtype, id, parentCluster) => { const recursiveRender = async (_elem, graph, diagramtype, parentCluster) => {
log.info('Graph in recursive render: XXX', graphlibJson.write(graph), parentCluster); log.info('Graph in recursive render: XXX', graphlibJson.write(graph), parentCluster);
const dir = graph.graph().rankdir; const dir = graph.graph().rankdir;
log.trace('Dir in recursive render - dir:', dir); log.trace('Dir in recursive render - dir:', dir);
@@ -52,7 +52,7 @@ const recursiveRender = async (_elem, graph, diagramtype, id, parentCluster) =>
if (node && node.clusterNode) { if (node && node.clusterNode) {
// const children = graph.children(v); // const children = graph.children(v);
log.info('Cluster identified', v, node.width, graph.node(v)); log.info('Cluster identified', v, node.width, graph.node(v));
const o = await recursiveRender(nodes, node.graph, diagramtype, id, graph.node(v)); const o = await recursiveRender(nodes, node.graph, diagramtype, graph.node(v));
const newEl = o.elem; const newEl = o.elem;
updateNodeBounds(node, newEl); updateNodeBounds(node, newEl);
node.diff = o.diff || 0; node.diff = o.diff || 0;
@@ -134,7 +134,9 @@ const recursiveRender = async (_elem, graph, diagramtype, id, parentCluster) =>
const edge = graph.edge(e); const edge = graph.edge(e);
log.info('Edge ' + e.v + ' -> ' + e.w + ': ' + JSON.stringify(edge), edge); log.info('Edge ' + e.v + ' -> ' + e.w + ': ' + JSON.stringify(edge), edge);
const paths = insertEdge(edgePaths, e, edge, clusterDb, diagramtype, graph, id);
const paths = insertEdge(edgePaths, e, edge, clusterDb, diagramtype, graph);
positionEdgeLabel(edge, paths); positionEdgeLabel(edge, paths);
}); });
@@ -148,6 +150,7 @@ const recursiveRender = async (_elem, graph, diagramtype, id, parentCluster) =>
return { elem, diff }; return { elem, diff };
}; };
export const render = async (elem, graph, markers, diagramtype, id) => { export const render = async (elem, graph, markers, diagramtype, id) => {
insertMarkers(elem, markers, diagramtype, id); insertMarkers(elem, markers, diagramtype, id);
clearNodes(); clearNodes();
@@ -155,11 +158,11 @@ export const render = async (elem, graph, markers, diagramtype, id) => {
clearClusters(); clearClusters();
clearGraphlib(); clearGraphlib();
log.warn('Graph at first:', JSON.stringify(graphlibJson.write(graph))); log.warn('Graph at first:', graphlibJson.write(graph));
adjustClustersAndEdges(graph); adjustClustersAndEdges(graph);
log.warn('Graph after:', JSON.stringify(graphlibJson.write(graph))); log.warn('Graph after:', graphlibJson.write(graph));
// log.warn('Graph ever after:', graphlibJson.write(graph.node('A').graph)); // log.warn('Graph ever after:', graphlibJson.write(graph.node('A').graph));
await recursiveRender(elem, graph, diagramtype, id); await recursiveRender(elem, graph, diagramtype);
}; };
// const shapeDefinitions = {}; // const shapeDefinitions = {};

View File

@@ -16,6 +16,7 @@ import state from '../diagrams/state/stateDetector.js';
import stateV2 from '../diagrams/state/stateDetector-V2.js'; import stateV2 from '../diagrams/state/stateDetector-V2.js';
import journey from '../diagrams/user-journey/journeyDetector.js'; import journey from '../diagrams/user-journey/journeyDetector.js';
import errorDiagram from '../diagrams/error/errorDiagram.js'; import errorDiagram from '../diagrams/error/errorDiagram.js';
import swimlane from '../diagrams/flowchart/swimlane/detector.js';
import flowchartElk from '../diagrams/flowchart/elk/detector.js'; import flowchartElk from '../diagrams/flowchart/elk/detector.js';
import timeline from '../diagrams/timeline/detector.js'; import timeline from '../diagrams/timeline/detector.js';
import mindmap from '../diagrams/mindmap/detector.js'; import mindmap from '../diagrams/mindmap/detector.js';
@@ -76,6 +77,7 @@ export const addDiagrams = () => {
pie, pie,
requirement, requirement,
sequence, sequence,
swimlane,
flowchartElk, flowchartElk,
flowchartV2, flowchartV2,
flowchart, flowchart,

View File

@@ -52,6 +52,7 @@ export const registerDiagram = (
throw new Error(`Diagram ${id} already registered.`); throw new Error(`Diagram ${id} already registered.`);
} }
diagrams[id] = diagram; diagrams[id] = diagram;
if (detector) { if (detector) {
addDetector(id, detector); addDetector(id, detector);
} }

View File

@@ -50,6 +50,23 @@ export const lookUpDomId = function (id) {
return id; return id;
}; };
/**
* Function to lookup domId from id in the graph definition.
*
* @param id
* @param domId
* @public
*/
export const lookUpId = function (domId) {
const veritceKeys = Object.keys(vertices);
for (const veritceKey of veritceKeys) {
if (vertices[veritceKey].domId === domId) {
return vertices[veritceKey].id;
}
}
return domId;
};
/** /**
* Function called by parser when a node definition has been found * Function called by parser when a node definition has been found
* *
@@ -782,6 +799,7 @@ export default {
setAccDescription, setAccDescription,
addVertex, addVertex,
lookUpDomId, lookUpDomId,
lookUpId,
addLink, addLink,
updateLinkInterpolate, updateLinkInterpolate,
updateLink, updateLink,

View File

@@ -81,6 +81,7 @@ that id.
<click>[\s\n] this.popState(); <click>[\s\n] this.popState();
<click>[^\s\n]* return 'CLICK'; <click>[^\s\n]* return 'CLICK';
"swimlane" {if(yy.lex.firstGraph()){this.begin("dir");} return 'GRAPH';}
"flowchart-elk" {if(yy.lex.firstGraph()){this.begin("dir");} return 'GRAPH';} "flowchart-elk" {if(yy.lex.firstGraph()){this.begin("dir");} return 'GRAPH';}
"graph" {if(yy.lex.firstGraph()){this.begin("dir");} return 'GRAPH';} "graph" {if(yy.lex.firstGraph()){this.begin("dir");} return 'GRAPH';}
"flowchart" {if(yy.lex.firstGraph()){this.begin("dir");} return 'GRAPH';} "flowchart" {if(yy.lex.firstGraph()){this.begin("dir");} return 'GRAPH';}

View File

@@ -0,0 +1,55 @@
import plugin from './detector.js';
import { describe, it } from 'vitest';
const { detector } = plugin;
describe('swimlane detector', () => {
it('should fail for dagre-d3', () => {
expect(
detector('swimlane', {
flowchart: {
defaultRenderer: 'dagre-d3',
},
})
).toBe(false);
});
it('should fail for dagre-wrapper', () => {
expect(
detector('flowchart', {
flowchart: {
defaultRenderer: 'dagre-wrapper',
},
})
).toBe(false);
});
it('should succeed for elk', () => {
expect(
detector('flowchart', {
flowchart: {
defaultRenderer: 'elk',
},
})
).toBe(true);
expect(
detector('graph', {
flowchart: {
defaultRenderer: 'elk',
},
})
).toBe(true);
});
it('should detect swimlane', () => {
expect(detector('swimlane')).toBe(true);
});
it('should not detect class with defaultRenderer set to elk', () => {
expect(
detector('class', {
flowchart: {
defaultRenderer: 'elk',
},
})
).toBe(false);
});
});

View File

@@ -0,0 +1,29 @@
import type {
ExternalDiagramDefinition,
DiagramDetector,
DiagramLoader,
} from '../../../diagram-api/types.js';
const id = 'swimlane';
const detector: DiagramDetector = (txt, config): boolean => {
if (txt.match(/^\s*swimlane/)) {
return true;
}
return false;
};
const loader: DiagramLoader = async () => {
const { diagram } = await import('./swimlane-definition.js');
return { id, diagram };
};
const plugin: ExternalDiagramDefinition = {
id,
detector,
loader,
};
export default plugin;

View File

@@ -0,0 +1,40 @@
import { findCommonAncestor, TreeData } from './render-utils.js';
describe('when rendering a flowchart using elk ', () => {
let lookupDb: TreeData;
beforeEach(() => {
lookupDb = {
parentById: {
B4: 'inner',
B5: 'inner',
C4: 'inner2',
C5: 'inner2',
B2: 'Ugge',
B3: 'Ugge',
inner: 'Ugge',
inner2: 'Ugge',
B6: 'outer',
},
childrenById: {
inner: ['B4', 'B5'],
inner2: ['C4', 'C5'],
Ugge: ['B2', 'B3', 'inner', 'inner2'],
outer: ['B6'],
},
};
});
it('to find parent of siblings in a subgraph', () => {
expect(findCommonAncestor('B4', 'B5', lookupDb)).toBe('inner');
});
it('to find an uncle', () => {
expect(findCommonAncestor('B4', 'B2', lookupDb)).toBe('Ugge');
});
it('to find a cousin', () => {
expect(findCommonAncestor('B4', 'C4', lookupDb)).toBe('Ugge');
});
it('to find a grandparent', () => {
expect(findCommonAncestor('B4', 'B6', lookupDb)).toBe('root');
});
it('to find ancestor of siblings in the root', () => {
expect(findCommonAncestor('B1', 'outer', lookupDb)).toBe('root');
});
});

View File

@@ -0,0 +1,25 @@
export interface TreeData {
parentById: Record<string, string>;
childrenById: Record<string, string[]>;
}
export const findCommonAncestor = (id1: string, id2: string, treeData: TreeData) => {
const { parentById } = treeData;
const visited = new Set();
let currentId = id1;
while (currentId) {
visited.add(currentId);
if (currentId === id2) {
return currentId;
}
currentId = parentById[currentId];
}
currentId = id2;
while (currentId) {
if (visited.has(currentId)) {
return currentId;
}
currentId = parentById[currentId];
}
return 'root';
};

View File

@@ -0,0 +1,395 @@
import * as graphlib from 'dagre-d3-es/src/graphlib/index.js';
import { select, curveLinear, selectAll } from 'd3';
import { getConfig } from '../../../config.js';
import utils from '../../../utils.js';
import { addHtmlLabel } from 'dagre-d3-es/src/dagre-js/label/add-html-label.js';
import { log } from '../../../logger.js';
import common, { evaluate } from '../../common/common.js';
import { interpolateToCurve, getStylesFromArray } from '../../../utils.js';
const conf = {};
export const setConf = function (cnf) {
const keys = Object.keys(cnf);
for (const key of keys) {
conf[key] = cnf[key];
}
};
/**
* Add edges to graph based on parsed graph definition
*
* @param {object} edges The edges to add to the graph
* @param {object} g The graph object
* @param diagObj
*/
export const addEdges = function (edges, g, diagObj,svg) {
log.info('abc78 edges = ', edges);
let cnt = 0;
let linkIdCnt = {};
let defaultStyle;
let defaultLabelStyle;
if (edges.defaultStyle !== undefined) {
const defaultStyles = getStylesFromArray(edges.defaultStyle);
defaultStyle = defaultStyles.style;
defaultLabelStyle = defaultStyles.labelStyle;
}
edges.forEach(function (edge) {
cnt++;
// Identify Link
var linkIdBase = 'L-' + edge.start + '-' + edge.end;
// count the links from+to the same node to give unique id
if (linkIdCnt[linkIdBase] === undefined) {
linkIdCnt[linkIdBase] = 0;
log.info('abc78 new entry', linkIdBase, linkIdCnt[linkIdBase]);
} else {
linkIdCnt[linkIdBase]++;
log.info('abc78 new entry', linkIdBase, linkIdCnt[linkIdBase]);
}
let linkId = linkIdBase + '-' + linkIdCnt[linkIdBase];
log.info('abc78 new link id to be used is', linkIdBase, linkId, linkIdCnt[linkIdBase]);
var linkNameStart = 'LS-' + edge.start;
var linkNameEnd = 'LE-' + edge.end;
const edgeData = { style: '', labelStyle: '' };
edgeData.minlen = edge.length || 1;
//edgeData.id = 'id' + cnt;
// Set link type for rendering
if (edge.type === 'arrow_open') {
edgeData.arrowhead = 'none';
} else {
edgeData.arrowhead = 'normal';
}
// Check of arrow types, placed here in order not to break old rendering
edgeData.arrowTypeStart = 'arrow_open';
edgeData.arrowTypeEnd = 'arrow_open';
/* eslint-disable no-fallthrough */
switch (edge.type) {
case 'double_arrow_cross':
edgeData.arrowTypeStart = 'arrow_cross';
case 'arrow_cross':
edgeData.arrowTypeEnd = 'arrow_cross';
break;
case 'double_arrow_point':
edgeData.arrowTypeStart = 'arrow_point';
case 'arrow_point':
edgeData.arrowTypeEnd = 'arrow_point';
break;
case 'double_arrow_circle':
edgeData.arrowTypeStart = 'arrow_circle';
case 'arrow_circle':
edgeData.arrowTypeEnd = 'arrow_circle';
break;
}
let style = '';
let labelStyle = '';
switch (edge.stroke) {
case 'normal':
style = 'fill:none;';
if (defaultStyle !== undefined) {
style = defaultStyle;
}
if (defaultLabelStyle !== undefined) {
labelStyle = defaultLabelStyle;
}
edgeData.thickness = 'normal';
edgeData.pattern = 'solid';
break;
case 'dotted':
edgeData.thickness = 'normal';
edgeData.pattern = 'dotted';
edgeData.style = 'fill:none;stroke-width:2px;stroke-dasharray:3;';
break;
case 'thick':
edgeData.thickness = 'thick';
edgeData.pattern = 'solid';
edgeData.style = 'stroke-width: 3.5px;fill:none;';
break;
case 'invisible':
edgeData.thickness = 'invisible';
edgeData.pattern = 'solid';
edgeData.style = 'stroke-width: 0;fill:none;';
break;
}
if (edge.style !== undefined) {
const styles = getStylesFromArray(edge.style);
style = styles.style;
labelStyle = styles.labelStyle;
}
edgeData.style = edgeData.style += style;
edgeData.labelStyle = edgeData.labelStyle += labelStyle;
if (edge.interpolate !== undefined) {
edgeData.curve = interpolateToCurve(edge.interpolate, curveLinear);
} else if (edges.defaultInterpolate !== undefined) {
edgeData.curve = interpolateToCurve(edges.defaultInterpolate, curveLinear);
} else {
edgeData.curve = interpolateToCurve(conf.curve, curveLinear);
}
if (edge.text === undefined) {
if (edge.style !== undefined) {
edgeData.arrowheadStyle = 'fill: #333';
}
} else {
edgeData.arrowheadStyle = 'fill: #333';
edgeData.labelpos = 'c';
}
edgeData.labelType = edge.labelType;
edgeData.label = edge.text.replace(common.lineBreakRegex, '\n');
if (edge.style === undefined) {
edgeData.style = edgeData.style || 'stroke: #333; stroke-width: 1.5px;fill:none;';
}
edgeData.labelStyle = edgeData.labelStyle.replace('color:', 'fill:');
edgeData.id = linkId;
edgeData.classes = 'flowchart-link ' + linkNameStart + ' ' + linkNameEnd;
// Add the edge to the graph
g.setEdge(edge.start, edge.end, edgeData, cnt);
});
};
/**
* Function that adds the vertices found during parsing to the graph to be rendered.
*
* @param vert Object containing the vertices.
* @param g The graph that is to be drawn.
* @param svgId
* @param root
* @param doc
* @param diagObj
*/
export const addVertices = function (vert, g, svgId, root, doc, diagObj) {
const svg = root.select(`[id="${svgId}"]`);
const keys = Object.keys(vert);
// Iterate through each item in the vertex object (containing all the vertices found) in the graph definition
keys.forEach(function (id) {
const vertex = vert[id];
/**
* Variable for storing the classes for the vertex
*
* @type {string}
*/
let classStr = 'default';
if (vertex.classes.length > 0) {
classStr = vertex.classes.join(' ');
}
classStr = classStr + ' flowchart-label';
const styles = getStylesFromArray(vertex.styles);
// Use vertex id as text in the box if no text is provided by the graph definition
let vertexText = vertex.text !== undefined ? vertex.text : vertex.id;
// We create a SVG label, either by delegating to addHtmlLabel or manually
let vertexNode;
log.info('vertex', vertex, vertex.labelType);
if (vertex.labelType === 'markdown') {
log.info('vertex', vertex, vertex.labelType);
} else {
if (evaluate(getConfig().flowchart.htmlLabels) && svg.html) {
// TODO: addHtmlLabel accepts a labelStyle. Do we possibly have that?
const node = {
label: vertexText.replace(
/fa[blrs]?:fa-[\w-]+/g,
(s) => `<i class='${s.replace(':', ' ')}'></i>`
),
};
vertexNode = addHtmlLabel(svg, node).node();
vertexNode.parentNode.removeChild(vertexNode);
} else {
const svgLabel = doc.createElementNS('http://www.w3.org/2000/svg', 'text');
svgLabel.setAttribute('style', styles.labelStyle.replace('color:', 'fill:'));
const rows = vertexText.split(common.lineBreakRegex);
for (const row of rows) {
const tspan = doc.createElementNS('http://www.w3.org/2000/svg', 'tspan');
tspan.setAttributeNS('http://www.w3.org/XML/1998/namespace', 'xml:space', 'preserve');
tspan.setAttribute('dy', '1em');
tspan.setAttribute('x', '1');
tspan.textContent = row;
svgLabel.appendChild(tspan);
}
vertexNode = svgLabel;
}
}
let radious = 0;
let _shape = '';
// Set the shape based parameters
switch (vertex.type) {
case 'round':
radious = 5;
_shape = 'rect';
break;
case 'square':
_shape = 'rect';
break;
case 'diamond':
_shape = 'question';
break;
case 'hexagon':
_shape = 'hexagon';
break;
case 'odd':
_shape = 'rect_left_inv_arrow';
break;
case 'lean_right':
_shape = 'lean_right';
break;
case 'lean_left':
_shape = 'lean_left';
break;
case 'trapezoid':
_shape = 'trapezoid';
break;
case 'inv_trapezoid':
_shape = 'inv_trapezoid';
break;
case 'odd_right':
_shape = 'rect_left_inv_arrow';
break;
case 'circle':
_shape = 'circle';
break;
case 'ellipse':
_shape = 'ellipse';
break;
case 'stadium':
_shape = 'stadium';
break;
case 'subroutine':
_shape = 'subroutine';
break;
case 'cylinder':
_shape = 'cylinder';
break;
case 'group':
_shape = 'rect';
break;
case 'doublecircle':
_shape = 'doublecircle';
break;
default:
_shape = 'rect';
}
// Add the node
g.setNode(vertex.id, {
labelStyle: styles.labelStyle,
shape: _shape,
labelText: vertexText,
labelType: vertex.labelType,
rx: radious,
ry: radious,
class: classStr,
style: styles.style,
id: vertex.id,
link: vertex.link,
linkTarget: vertex.linkTarget,
tooltip: diagObj.db.getTooltip(vertex.id) || '',
domId: diagObj.db.lookUpDomId(vertex.id),
haveCallback: vertex.haveCallback,
width: vertex.type === 'group' ? 500 : undefined,
dir: vertex.dir,
type: vertex.type,
props: vertex.props,
padding: getConfig().flowchart.padding,
});
log.info('setNode', {
labelStyle: styles.labelStyle,
labelType: vertex.labelType,
shape: _shape,
labelText: vertexText,
rx: radious,
ry: radious,
class: classStr,
style: styles.style,
id: vertex.id,
domId: diagObj.db.lookUpDomId(vertex.id),
width: vertex.type === 'group' ? 500 : undefined,
type: vertex.type,
dir: vertex.dir,
props: vertex.props,
padding: getConfig().flowchart.padding,
});
});
};
/**
*
* @param diagObj
* @param id
* @param root
* @param doc
*/
function setupGraph(diagObj, id, root, doc) {
const { securityLevel, flowchart: conf } = getConfig();
const nodeSpacing = conf.nodeSpacing || 50;
const rankSpacing = conf.rankSpacing || 50;
// Fetch the default direction, use TD if none was found
let dir = diagObj.db.getDirection();
if (dir === undefined) {
dir = 'TD';
}
// Create the input mermaid.graph
const g = new graphlib.Graph({
multigraph: true,
compound: true,
})
.setGraph({
rankdir: dir,
nodesep: nodeSpacing,
ranksep: rankSpacing,
marginx: 0,
marginy: 0,
})
.setDefaultEdgeLabel(function () {
return {};
});
let subG;
const subGraphs = diagObj.db.getSubGraphs();
// Fetch the vertices/nodes and edges/links from the parsed graph definition
const vert = diagObj.db.getVertices();
const edges = diagObj.db.getEdges();
log.info('Edges', edges);
let i = 0;
// for (i = subGraphs.length - 1; i >= 0; i--) {
// // for (let i = 0; i < subGraphs.length; i++) {
// subG = subGraphs[i];
// selectAll('cluster').append('text');
// for (let j = 0; j < subG.nodes.length; j++) {
// log.info('Setting up subgraphs', subG.nodes[j], subG.id);
// g.setParent(subG.nodes[j], subG.id);
// }
// }
addVertices(vert, g, id, root, doc, diagObj);
addEdges(edges, g, diagObj);
return g;
}
export default setupGraph;

View File

@@ -0,0 +1,143 @@
/** Returns the styles given options */
export interface FlowChartStyleOptions {
arrowheadColor: string;
border2: string;
clusterBkg: string;
clusterBorder: string;
edgeLabelBackground: string;
fontFamily: string;
lineColor: string;
mainBkg: string;
nodeBorder: string;
nodeTextColor: string;
tertiaryColor: string;
textColor: string;
titleColor: string;
[key: string]: string;
}
const genSections = (options: FlowChartStyleOptions) => {
let sections = '';
for (let i = 0; i < 5; i++) {
sections += `
.subgraph-lvl-${i} {
fill: ${options[`surface${i}`]};
stroke: ${options[`surfacePeer${i}`]};
}
`;
}
return sections;
};
const getStyles = (options: FlowChartStyleOptions) =>
`.label {
font-family: ${options.fontFamily};
color: ${options.nodeTextColor || options.textColor};
}
.cluster-label text {
fill: ${options.titleColor};
}
.cluster-label span {
color: ${options.titleColor};
}
.label text,span {
fill: ${options.nodeTextColor || options.textColor};
color: ${options.nodeTextColor || options.textColor};
}
.node rect,
.node circle,
.node ellipse,
.node polygon,
.node path {
fill: ${options.mainBkg};
stroke: ${options.nodeBorder};
stroke-width: 1px;
}
.node .label {
text-align: center;
}
.node.clickable {
cursor: pointer;
}
.arrowheadPath {
fill: ${options.arrowheadColor};
}
.edgePath .path {
stroke: ${options.lineColor};
stroke-width: 2.0px;
}
.flowchart-link {
stroke: ${options.lineColor};
fill: none;
}
.edgeLabel {
background-color: ${options.edgeLabelBackground};
rect {
opacity: 0.85;
background-color: ${options.edgeLabelBackground};
fill: ${options.edgeLabelBackground};
}
text-align: center;
}
.cluster rect {
fill: ${options.clusterBkg};
stroke: ${options.clusterBorder};
stroke-width: 1px;
}
.cluster text {
fill: ${options.titleColor};
}
.cluster span {
color: ${options.titleColor};
}
/* .cluster div {
color: ${options.titleColor};
} */
div.mermaidTooltip {
position: absolute;
text-align: center;
max-width: 200px;
padding: 2px;
font-family: ${options.fontFamily};
font-size: 12px;
background: ${options.tertiaryColor};
border: 1px solid ${options.border2};
border-radius: 2px;
pointer-events: none;
z-index: 100;
}
.flowchartTitleText {
text-anchor: middle;
font-size: 18px;
fill: ${options.textColor};
}
.subgraph {
stroke-width:2;
rx:3;
}
// .subgraph-lvl-1 {
// fill:#ccc;
// // stroke:black;
// }
.flowchart-label text {
text-anchor: middle;
}
${genSections(options)}
`;
export default getStyles;

View File

@@ -0,0 +1,13 @@
// @ts-ignore: JISON typing missing
import parser from '../parser/flow.jison';
import * as db from '../flowDb.js';
import renderer from './swimlaneRenderer.js';
import styles from './styles.js';
export const diagram = {
db,
renderer,
parser,
styles,
};

View File

@@ -0,0 +1,220 @@
import { log } from '../../../logger.js';
import flowDb from '../flowDb.js';
export const getSubgraphLookupTable = function (diagObj) {
const subGraphs = diagObj.db.getSubGraphs();
const subgraphDb = {};
log.info('Subgraphs - ', subGraphs);
for (let i = subGraphs.length - 1; i >= 0; i--) {
const subG = subGraphs[i];
log.info('Subgraph - ', subG);
for (let j = 0; j < subG.nodes.length; j++) {
log.info('Setting up subgraphs', subG.nodes[j], subG.id);
subgraphDb[flowDb.lookUpId(subG.nodes[j])] = subG.id;
}
}
return subgraphDb;
};
/**
*
* @param graph
* @param subgraphLookupTable
*/
export function assignRanks(graph, subgraphLookupTable) {
let visited = new Set();
const lock = new Map();
const ranks = new Map();
let cnt = 0;
let changesDetected = true;
/**
*
* @param nodeId
* @param currentRank
*/
function dfs(nodeId, currentRank) {
if (visited.has(nodeId)) {
return;
}
visited.add(nodeId);
const existingRank = ranks.get(nodeId) || 0;
// console.log('APA444 DFS Base case for', nodeId, 'to', Math.max(existingRank, currentRank));
if (lock.get(nodeId) !== 1) {
ranks.set(nodeId, Math.max(existingRank, currentRank));
} else {
console.log(
'APA444 ',
nodeId,
'was locked to ',
existingRank,
'so not changing it',
ranks.get(nodeId)
);
}
const currentRankAdjusted = ranks.get(nodeId) || currentRank;
graph.successors(nodeId).forEach((targetId) => {
if (subgraphLookupTable[targetId] !== subgraphLookupTable[nodeId]) {
dfs(targetId, currentRankAdjusted);
} else {
// In same line, easy increase
dfs(targetId, currentRankAdjusted + 1);
}
});
}
/**
*
*/
function adjustSuccessors() {
console.log('APA444 Adjusting successors');
graph.nodes().forEach((nodeId) => {
console.log('APA444 Going through nodes', nodeId);
// if (graph.predecessors(nodeId).length === 0) {
console.log('APA444 has no predecessors', nodeId);
graph.successors(nodeId).forEach((successorNodeId) => {
console.log('APA444 has checking successor', successorNodeId);
if (subgraphLookupTable[successorNodeId] !== subgraphLookupTable[nodeId]) {
const newRank = ranks.get(successorNodeId);
ranks.set(nodeId, newRank);
console.log('APA444 POST-process case for', nodeId, 'to', newRank);
lock.set(nodeId, 1);
changesDetected = true;
// setRankFromTopNodes();
// Adjust ranks of successors in the same subgraph
graph.successors(nodeId).forEach((sameSubGraphSuccessorNodeId) => {
if (subgraphLookupTable[sameSubGraphSuccessorNodeId] === subgraphLookupTable[nodeId]) {
console.log(
'APA444 Adjusting rank of',
sameSubGraphSuccessorNodeId,
'to',
newRank + 1
);
ranks.set(sameSubGraphSuccessorNodeId, newRank + 1);
lock.set(sameSubGraphSuccessorNodeId, 1);
changesDetected = true;
// dfs(sameSubGraphSuccessorNodeId, newRank + 1);
// setRankFromTopNodes();
}
});
} else {
console.log('APA444 Node', nodeId, ' and ', successorNodeId, ' is in the same lane');
}
});
// }
});
}
/**
*
*/
function setRankFromTopNodes() {
visited = new Set();
graph.nodes().forEach((nodeId) => {
if (graph.predecessors(nodeId).length === 0) {
dfs(nodeId, 0);
}
});
adjustSuccessors();
}
while (changesDetected && cnt < 10) {
setRankFromTopNodes();
cnt++;
}
// Post-process the ranks
return ranks;
}
/**
*
* @param graph
* @param subgraphLÖookupTable
* @param ranks
* @param subgraphLookupTable
*/
export function assignAffinities(graph, ranks, subgraphLookupTable) {
const affinities = new Map();
const swimlaneRankAffinities = new Map();
const swimlaneMaxAffinity = new Map();
graph.nodes().forEach((nodeId) => {
const swimlane = subgraphLookupTable[nodeId];
const rank = ranks.get(nodeId);
const key = swimlane + ':' + rank;
let currentAffinity = swimlaneRankAffinities.get(key);
if (currentAffinity === undefined) {
currentAffinity = -1;
}
const newAffinity = currentAffinity + 1;
swimlaneRankAffinities.set(key, newAffinity);
affinities.set(nodeId, newAffinity);
let currentMaxAffinity = swimlaneMaxAffinity.get(swimlane);
if (currentMaxAffinity === undefined) {
swimlaneMaxAffinity.set(swimlane, 0);
currentMaxAffinity = 0;
}
if (newAffinity > currentMaxAffinity) {
swimlaneMaxAffinity.set(swimlane, newAffinity);
}
});
// console.log('APA444 affinities', swimlaneRankAffinities);
return { affinities, swimlaneMaxAffinity };
//return affinities;
}
/**
*
* @param graph
* @param diagObj
*/
export function swimlaneLayout(graph, diagObj) {
const subgraphLookupTable = getSubgraphLookupTable(diagObj);
const ranks = assignRanks(graph, subgraphLookupTable);
const { affinities, swimlaneMaxAffinity } = assignAffinities(graph, ranks, subgraphLookupTable);
// const affinities = assignAffinities(graph, ranks, subgraphLookupTable);
const subGraphs = diagObj.db.getSubGraphs();
const lanes = [];
const laneDb = {};
let xPos = 0;
for (const subG of subGraphs) {
const maxAffinity = swimlaneMaxAffinity.get(subG.id);
const lane = {
title: subG.title,
x: xPos,
width: 200 + maxAffinity * 150,
};
xPos += lane.width;
lanes.push(lane);
laneDb[subG.id] = lane;
}
const rankWidth = [];
// Basic layout, calculate the node positions based on rank
graph.nodes().forEach((nodeId) => {
const rank = ranks.get(nodeId);
if (!rankWidth[rank]) {
const laneId = subgraphLookupTable[nodeId];
const lane = laneDb[laneId];
const n = graph.node(nodeId);
console.log('Node', nodeId, n);
const affinity = affinities.get(nodeId);
console.log('APA444', nodeId, 'rank', rank, 'affinity', affinity);
graph.setNode(nodeId, { y: rank * 200 + 50, x: lane.x + 150 * affinity + 100 });
// lane.width = Math.max(lane.width, lane.x + 150*affinity + lane.width / 4);
}
});
return { graph, lanes };
}

View File

@@ -0,0 +1,129 @@
import flowDb from '../flowDb.js';
import { cleanupComments } from '../../../diagram-api/comments.js';
import setupGraph from './setup-graph.js';
import { swimlaneLayout, assignRanks, getSubgraphLookupTable } from './swimlane-layout.js';
import { getDiagramFromText } from '../../../Diagram.js';
import { addDiagrams } from '../../../diagram-api/diagram-orchestration.ts';
import jsdom from 'jsdom';
const { JSDOM } = jsdom;
addDiagrams();
describe('When doing a assigning ranks specific for swim lanes ', () => {
let root;
let doc;
beforeEach(function () {
const dom = new JSDOM(`<!DOCTYPE html><body><pre id="swimmer">My First JSDOM!</pre></body>`);
root = select(dom.window.document.getElementById('swimmer'));
root.html = () => {
' return <div>hello</div>';
};
doc = dom.window.document;
});
describe('Layout: ', () => {
// it('should rank the nodes:', async () => {
// const diagram = await getDiagramFromText(`swimlane LR
// subgraph "\`one\`"
// start --> cat --> rat
// end`);
// const g = setupGraph(diagram, 'swimmer', root, doc);
// const subgraphLookupTable = getSubgraphLookupTable(diagram);
// const ranks = assignRanks(g, subgraphLookupTable);
// expect(ranks.get('start')).toEqual(0);
// expect(ranks.get('cat')).toEqual(1);
// expect(ranks.get('rat')).toEqual(2);
// });
it('should rank the nodes:', async () => {
const diagram = await getDiagramFromText(`swimlane LR
subgraph "\`one\`"
start --> cat --> rat
end
subgraph "\`two\`"
monkey --> dog --> done
end
cat --> monkey`);
const g = setupGraph(diagram, 'swimmer', root, doc);
const subgraphLookupTable = getSubgraphLookupTable(diagram);
const ranks = assignRanks(g, subgraphLookupTable);
expect(ranks.get('start')).toEqual(0);
expect(ranks.get('cat')).toEqual(1);
expect(ranks.get('rat')).toEqual(2);
expect(ranks.get('monkey')).toEqual(1);
expect(ranks.get('dog')).toEqual(2);
expect(ranks.get('done')).toEqual(3);
});
});
describe('Layout: ', () => {
it('should rank the nodes:', async () => {
const diagram = await getDiagramFromText(`swimlane LR
subgraph "\`one\`"
start --> cat --> rat
end`);
const g = setupGraph(diagram, 'swimmer', root, doc);
const subgraphLookupTable = getSubgraphLookupTable(diagram);
const { graph, lanes } = swimlaneLayout(g, diagram);
expect(lanes.length).toBe(1);
const start = graph.node('start');
const cat = graph.node('cat');
const rat = graph.node('rat');
expect(start.y).toBe(50);
expect(cat.y).toBe(250);
expect(rat.y).toBe(450);
expect(rat.x).toBe(100);
});
it('should rank the nodes:', async () => {
const diagram = await getDiagramFromText(`swimlane LR
subgraph "\`one\`"
start --> cat --> rat
end
subgraph "\`two\`"
monkey --> dog --> done
end
cat --> monkey`);
const g = setupGraph(diagram, 'swimmer', root, doc);
const subgraphLookupTable = getSubgraphLookupTable(diagram);
const { graph, lanes } = swimlaneLayout(g, diagram);
expect(lanes.length).toBe(2);
const start = graph.node('start');
const cat = graph.node('cat');
const rat = graph.node('rat');
const monkey = graph.node('monkey');
const dog = graph.node('dog');
const done = graph.node('done');
expect(start.y).toBe(50);
expect(cat.y).toBe(250);
expect(rat.y).toBe(450);
expect(rat.x).toBe(100);
expect(monkey.y).toBe(250);
expect(dog.y).toBe(450);
expect(done.y).toBe(650);
expect(monkey.x).toBe(300);
});
it.only('should rank the nodes:', async () => {
const diagram = await getDiagramFromText(`swimlane LR
subgraph "\`one\`"
start --> cat --> rat & hat
end
`);
const g = setupGraph(diagram, 'swimmer', root, doc);
const subgraphLookupTable = getSubgraphLookupTable(diagram);
const { graph, lanes } = swimlaneLayout(g, diagram);
expect(lanes.length).toBe(1);
const start = graph.node('start');
const cat = graph.node('cat');
const rat = graph.node('rat');
const hat = graph.node('rat');
expect(start.y).toBe(50);
expect(cat.y).toBe(250);
expect(rat.y).toBe(450);
expect(rat.x).toBe(300);
expect(hat.y).toBe(450);
expect(hat.x).toBe(100);
});
});
});

View File

@@ -0,0 +1,596 @@
import * as graphlib from 'dagre-d3-es/src/graphlib/index.js';
import { select, curveLinear, selectAll } from 'd3';
import { swimlaneLayout } from './swimlane-layout.js';
import insertMarkers from '../../../dagre-wrapper/markers.js';
import { insertNode } from '../../../dagre-wrapper/nodes.js';
import flowDb from '../flowDb.js';
import { getConfig } from '../../../config.js';
import {getStylesFromArray} from '../../../utils.js';
import setupGraph, { addEdges, addVertices } from './setup-graph.js';
import { render } from '../../../dagre-wrapper/index.js';
import { log } from '../../../logger.js';
import { setupGraphViewbox } from '../../../setupGraphViewbox.js';
import common, { evaluate } from '../../common/common.js';
import { addHtmlLabel } from 'dagre-d3-es/src/dagre-js/label/add-html-label.js';
import { insertEdge,positionEdgeLabel } from '../../../dagre-wrapper/edges.js';
import {
clear as clearGraphlib,
clusterDb,
adjustClustersAndEdges,
findNonClusterChild,
sortNodesByHierarchy,
} from '../../../dagre-wrapper/mermaid-graphlib.js';
const conf = {};
export const setConf = function (cnf) {
const keys = Object.keys(cnf);
for (const key of keys) {
conf[key] = cnf[key];
}
};
/**
*
* @param element
* @param graph
* @param layout
* @param vert
* @param elem
* @param g
* @param id
* @param conf
*/
async function swimlaneRender(layout,vert, elem,g, id, conf) {
let renderedNodes = [];
// draw nodes from layout.graph to element
const nodes = layout.graph.nodes();
// lanes are the swimlanes
const lanes = layout.lanes;
const nodesElements = elem.insert('g').attr('class', 'nodes');
// for each node, draw a rect, with a child text inside as label
for (const node of nodes) {
const nodeFromLayout = layout.graph.node(node);
const vertex = vert[node];
//Initialise the node
/**
* Variable for storing the classes for the vertex
*
* @type {string}
*/
let classStr = 'default';
if (vertex.classes.length > 0) {
classStr = vertex.classes.join(' ');
}
classStr = classStr + ' swimlane-label';
const styles = getStylesFromArray(vertex.styles);
// Use vertex id as text in the box if no text is provided by the graph definition
let vertexText = vertex.text !== undefined ? vertex.text : vertex.id;
// We create a SVG label, either by delegating to addHtmlLabel or manually
let vertexNode;
log.info('vertex', vertex, vertex.labelType);
if (vertex.labelType === 'markdown') {
log.info('vertex', vertex, vertex.labelType);
} else {
if (evaluate(getConfig().flowchart.htmlLabels)) {
// TODO: addHtmlLabel accepts a labelStyle. Do we possibly have that?
const node = {
label: vertexText.replace(
/fa[blrs]?:fa-[\w-]+/g,
(s) => `<i class='${s.replace(':', ' ')}'></i>`
),
};
vertexNode = addHtmlLabel(elem, node).node();
vertexNode.parentNode.removeChild(vertexNode);
} else {
const svgLabel = doc.createElementNS('http://www.w3.org/2000/svg', 'text');
svgLabel.setAttribute('style', styles.labelStyle.replace('color:', 'fill:'));
const rows = vertexText.split(common.lineBreakRegex);
for (const row of rows) {
const tspan = doc.createElementNS('http://www.w3.org/2000/svg', 'tspan');
tspan.setAttributeNS('http://www.w3.org/XML/1998/namespace', 'xml:space', 'preserve');
tspan.setAttribute('dy', '1em');
tspan.setAttribute('x', '1');
tspan.textContent = row;
svgLabel.appendChild(tspan);
}
vertexNode = svgLabel;
}
}
let radious = 0;
let _shape = '';
// Set the shape based parameters
switch (vertex.type) {
case 'round':
radious = 5;
_shape = 'rect';
break;
case 'square':
_shape = 'rect';
break;
case 'diamond':
_shape = 'question';
break;
case 'hexagon':
_shape = 'hexagon';
break;
case 'odd':
_shape = 'rect_left_inv_arrow';
break;
case 'lean_right':
_shape = 'lean_right';
break;
case 'lean_left':
_shape = 'lean_left';
break;
case 'trapezoid':
_shape = 'trapezoid';
break;
case 'inv_trapezoid':
_shape = 'inv_trapezoid';
break;
case 'odd_right':
_shape = 'rect_left_inv_arrow';
break;
case 'circle':
_shape = 'circle';
break;
case 'ellipse':
_shape = 'ellipse';
break;
case 'stadium':
_shape = 'stadium';
break;
case 'subroutine':
_shape = 'subroutine';
break;
case 'cylinder':
_shape = 'cylinder';
break;
case 'group':
_shape = 'rect';
break;
case 'doublecircle':
_shape = 'doublecircle';
break;
default:
_shape = 'rect';
}
// Add the node
let nodeObj ={
labelStyle: styles.labelStyle,
shape: _shape,
labelText: vertexText,
labelType: vertex.labelType,
rx: radious,
ry: radious,
class: classStr,
style: styles.style,
id: vertex.id,
link: vertex.link,
linkTarget: vertex.linkTarget,
// tooltip: diagObj.db.getTooltip(vertex.id) || '',
// domId: diagObj.db.lookUpDomId(vertex.id),
haveCallback: vertex.haveCallback,
width: vertex.type === 'group' ? 500 : undefined,
dir: vertex.dir,
type: vertex.type,
props: vertex.props,
padding: getConfig().flowchart.padding,
x: nodeFromLayout.x,
y: nodeFromLayout.y,
};
let boundingBox;
let nodeEl;
// Add the element to the DOM
nodeEl = await insertNode(nodesElements, nodeObj, vertex.dir);
boundingBox = nodeEl.node().getBBox();
nodeEl.attr('transform', `translate(${nodeObj.x}, ${nodeObj.y / 2})`);
// add to rendered nodes
renderedNodes.push({id: vertex.id, nodeObj: nodeObj, boundingBox: boundingBox});
}
return renderedNodes;
}
/**
* Returns the all the styles from classDef statements in the graph definition.
*
* @param text
* @param diagObj
* @returns {object} ClassDef styles
*/
// export const getClasses = function (text, diagObj) {
// log.info('Extracting classes');
// diagObj.db.clear();
// try {
// // Parse the graph definition
// diagObj.parse(text);
// return diagObj.db.getClasses();
// } catch (e) {
// return;
// }
// };
/**
* Returns the all the styles from classDef statements in the graph definition.
*
* @param text
* @param diagObj
* @returns {Record<string, import('../../../diagram-api/types.js').DiagramStyleClassDef>} ClassDef styles
*/
export const getClasses = function (text, diagObj) {
log.info('Extracting classes');
return diagObj.db.getClasses();
};
/**
* Draws a flowchart in the tag with id: id based on the graph definition in text.
*
* @param text
* @param id
*/
export const draw = async function (text, id, _version, diagObj) {
log.info('Drawing flowchart');
diagObj.db.clear();
flowDb.setGen('gen-2');
// Parse the graph definition
diagObj.parser.parse(text);
const { securityLevel, flowchart: conf } = getConfig();
// Handle root and document for when rendering in sandbox mode
let sandboxElement;
if (securityLevel === 'sandbox') {
sandboxElement = select('#i' + id);
}
const root =
securityLevel === 'sandbox'
? select(sandboxElement.nodes()[0].contentDocument.body)
: select('body');
const doc = securityLevel === 'sandbox' ? sandboxElement.nodes()[0].contentDocument : document;
// create g as a graphlib graph using setupGraph from setup-graph.js
const g = setupGraph(diagObj, id, root, doc);
let subG;
const subGraphs = diagObj.db.getSubGraphs();
log.info('Subgraphs - ', subGraphs);
for (let i = subGraphs.length - 1; i >= 0; i--) {
subG = subGraphs[i];
log.info('Subgraph - ', subG);
diagObj.db.addVertex(
subG.id,
{ text: subG.title, type: subG.labelType },
'group',
undefined,
subG.classes,
subG.dir
);
}
// Fetch the vertices/nodes and edges/links from the parsed graph definition
const vert = diagObj.db.getVertices();
const edges = diagObj.db.getEdges();
log.info('Edges', edges);
const svg = root.select('#' + id);
svg.append('g');
// Run the renderer. This is what draws the final graph.
// const element = root.select('#' + id + ' g');
console.log('diagObj',diagObj);
console.log('subGraphs', diagObj.db.getSubGraphs());
const layout = swimlaneLayout(g, diagObj);
console.log('custom layout',layout);
// insert markers
// Define the supported markers for the diagram
const markers = ['point', 'circle', 'cross'];
insertMarkers(svg, markers, 'flowchart', id);
// draw lanes as vertical lines
const lanesElements = svg.insert('g').attr('class', 'lanes');
let laneCount = 0;
for (const lane of layout.lanes) {
laneCount++;
//draw lane header as rectangle with lane title centered in it
const laneHeader = document.createElementNS("http://www.w3.org/2000/svg", "rect");
// Set attributes for the rectangle
laneHeader.setAttribute("x",lane.x); // x-coordinate of the top-left corner
laneHeader.setAttribute("y", -50); // y-coordinate of the top-left corner
laneHeader.setAttribute("width", lane.width); // width of the rectangle
laneHeader.setAttribute("height", "50"); // height of the rectangle
if(laneCount % 2 == 0){
//set light blue color for even lanes
laneHeader.setAttribute("fill", "blue"); // fill color of the rectangle
}else{
//set white color odd lanes
laneHeader.setAttribute("fill", "grey"); // fill color of the rectangle
}
laneHeader.setAttribute("stroke", "black"); // color of the stroke/border
laneHeader.setAttribute("stroke-width", "2"); // width of the stroke/border
// Append the rectangle to the SVG element
lanesElements.node().appendChild(laneHeader);
//draw lane title
const laneTitle = document.createElementNS("http://www.w3.org/2000/svg", "text");
// Set attributes for the rectangle
laneTitle.setAttribute("x",lane.x + lane.width/2); // x-coordinate of the top-left corner
laneTitle.setAttribute("y", -50 + 50/2); // y-coordinate of the top-left corner
laneTitle.setAttribute("width", lane.width); // width of the rectangle
laneTitle.setAttribute("height", "50"); // height of the rectangle
laneTitle.setAttribute("fill", "white"); // fill color of the rectangle
laneTitle.setAttribute("stroke-width", "1"); // width of the stroke/border
laneTitle.setAttribute("text-anchor", "middle"); // width of the stroke/border
laneTitle.setAttribute("alignment-baseline", "middle"); // width of the stroke/border
laneTitle.setAttribute("font-size", "20"); // width of the stroke/border
laneTitle.textContent = lane.title;
// Append the rectangle to the SVG element
lanesElements.node().appendChild(laneTitle);
//draw lane
// Create a <rect> element
const rectangle = document.createElementNS("http://www.w3.org/2000/svg", "rect");
// Set attributes for the rectangle
rectangle.setAttribute("x",lane.x); // x-coordinate of the top-left corner
rectangle.setAttribute("y", 0); // y-coordinate of the top-left corner
rectangle.setAttribute("width", lane.width); // width of the rectangle
rectangle.setAttribute("height", "500"); // height of the rectangle
if(laneCount % 2 == 0){
//set light blue color for even lanes
rectangle.setAttribute("fill", "lightblue"); // fill color of the rectangle
}else{
//set white color odd lanes
rectangle.setAttribute("fill", "#ffffff"); // fill color of the rectangle
}
rectangle.setAttribute("stroke", "black"); // color of the stroke/border
rectangle.setAttribute("stroke-width", "2"); // width of the stroke/border
// Append the rectangle to the SVG element
lanesElements.node().appendChild(rectangle);
}
// append lanesElements to elem
svg.node().appendChild(lanesElements.node());
// add lane headers
const laneHeaders = svg.insert('g').attr('class', 'laneHeaders');
let drawnEdges =[];
//get edge markers
let renderedNodes = await swimlaneRender(layout,vert, svg,g,id, conf);
let renderedEdgePaths= [];
addEdges(edges, g, diagObj,svg);
g.edges().forEach(function (e) {
const edge = g.edge(e);
log.info('Edge ' + e.v + ' -> ' + e.w + ': ' + JSON.stringify(edge), edge);
const edgePaths = svg.insert('g').attr('class', 'edgePaths');
//get start node x, y coordinates
let sourceNode = {x:layout.graph.node(e.v).x, y:layout.graph.node(e.v).y/2, id: e.v};
//get end node x, y coordinates=
const targetNode = {x:layout.graph.node(e.w).x, y:layout.graph.node(e.w).y/2, id: e.w};
//create edge points based on start and end node
edge.points = getEdgePoints(sourceNode, targetNode, drawnEdges, renderedNodes,renderedEdgePaths);
// add to drawn edges
drawnEdges.push(edge);
const paths = insertEdge(edgePaths, e, edge, clusterDb, 'flowchart', g);
//positionEdgeLabel(edge, paths);
});
// utils.insertTitle(svg, 'flowchartTitleText', conf.titleTopMargin, diagObj.db.getDiagramTitle());
setupGraphViewbox(g, svg, conf.diagramPadding, conf.useMaxWidth);
};
// function to find edge path points based on start and end node
/**
*
* @param startNode
* @param endNode
* @param drawnEdges
* @param renderedNodes
*/
function getEdgePoints(startNode, endNode, drawnEdges, renderedNodes) {
let potentialEdgePaths = [];
for(let i=1;i<=3;i++){
const points = [];
// add start point
points.push({ x: startNode.x, y: startNode.y })
// Point in the middle, if both nodes do not have same x or y
if (startNode.x !== endNode.x && startNode.y !== endNode.y && i!=1) {
if(i==2){
points.push({ x: startNode.x, y: endNode.y });
}else{
points.push({ x: endNode.x, y: startNode.y });
}
}
// add end point
points.push({ x: endNode.x, y: endNode.y });
//print points
console.log('points before intersection', points);
// get start and end node objects from array of rendered nodes
const startNodeObj = renderedNodes.find(node => node.id === startNode.id);
const endNodeObj = renderedNodes.find(node => node.id === endNode.id);
console.log(" intersection startNodeObj", startNodeObj);
console.log(" intersection endNodeObj", endNodeObj);
startNodeObj.nodeObj.x = startNode.x;
startNodeObj.nodeObj.y = startNode.y;
// the first point should be the intersection of the start node and the edge
let startInsection = startNodeObj.nodeObj.intersect(points[1]);
points[0] = startInsection;
//log intersection
console.log('start intersection', startInsection);
endNodeObj.nodeObj.x = endNode.x;
endNodeObj.nodeObj.y = endNode.y;
// the last point should be the intersection of the end node and the edge
let endInsection = endNodeObj.nodeObj.intersect(points[points.length - 2]);
points[points.length - 1] = endInsection;
//log intersection
console.log('end intersection', endInsection);
//push points to potential edge paths
potentialEdgePaths.push({points: points});
}
// Create a new list of renderedNodes without the start and end node
const filteredRenderedNodes = renderedNodes.filter(node => node.id !== startNode.id && node.id !== endNode.id);
//Rank the potential edge path
const rankedEdgePaths = rankEdgePaths(potentialEdgePaths, filteredRenderedNodes);
if(startNode.id==='sheep' && endNode.id === 'dog'){
console.log('sheep--> dog rankedEdgePaths', rankedEdgePaths);
}
return rankedEdgePaths[0].edgePath.points;
}
// Function to check if a point is inside a nodes bounding box
/**
*
* @param point
* @param nodes
*/
function isPointInsideNode(point, nodes) {
let isInside = false;
for (const node of nodes) {
if (
point.x >= node.nodeObj.x &&
point.x <= node.nodeObj.x + node.boundingBox.width &&
point.y >= node.nodeObj.y &&
point.y <= node.nodeObj.y + node.boundingBox.height
) {
isInside = true;
}
}
return isInside;
}
// Ranks edgePaths (points) based on the number of intersections with nodes
/**
*
* @param edgePaths
* @param nodes
*/
function rankEdgePaths(edgePaths, nodes) {
let rankedEdgePaths = [];
for (const edgePath of edgePaths) {
let rank = 10 + edgePath.points.length;
for (const point of edgePath.points) {
if (isPointInsideNode(point, nodes)) {
// remove edge path
}
}
rankedEdgePaths.push({ rank: rank, edgePath: edgePath });
}
//sort on the basis of rank, highest rank first
rankedEdgePaths.sort((a, b) => (a.rank < b.rank ? 1 : -1));
return rankedEdgePaths;
}
/**
* Function to find if edge path is intersecting with any other edge path
* @param edgePath
* @param renderedEdgePaths
* @returns {boolean}
*/
function isEdgePathIntersecting(edgePath, renderedEdgePaths) {
let isIntersecting = false;
for (const renderedEdgePath of renderedEdgePaths) {
// check if line drawn from start point of edge path to start point of rendered edge path is intersecting with any other edge path
if (
common.isLineIntersecting(
edgePath.points[0],
renderedEdgePath.points[0],
edgePath.points[1],
renderedEdgePath.points[1]
)
) {
isIntersecting = true;
}
}
return isIntersecting;
}
export default {
setConf,
addVertices,
addEdges,
getClasses,
draw,
};

View File

@@ -0,0 +1,69 @@
swimlane
lane "Customer"
Start(Start) --> place-order[Place Order]
update-order[Update Order] --> place-order
end
lane "Sales Team"
completness[Check order for Completeness]
complete{Order complete?} --Yes--> forward[Forward order to Warehouse]
complete{Order complete?} --no--> request-changes[Request Changes]
end
lane "Inventory"
in-stock{Item in Stock?} --> ship[Ship Items] --> complete(Order Complete)
end
lane "CSD"
refund
cancelled
end
place-order -->completeness
request-changes --> order-update
forward --> in-stock --> refund
in-stock --> refund[Manage Refund] --> cancelled(Order cancelled)
request --> update-order
elk.direction:DOWN
cycleBreaking.strategy: INTERACTIVE
layering.strategy: INTERACTIVE
node n1 {
layout [position: 0, 0]
label "n1"
}
node n2 {
layout [position: 0, 50]
label "n2"
}
node n3 {
layout [position: 0, 100]
label "n3"
}
node n4 {
layout [position: 50, 0]
label "n4"
}
node n5 {
layout [position: 50, 50]
label "n5"
}
node n6 {
layout [position: 50, 100]
label "n6"
}
node n7 {
layout [position: 50, 100]
label "n7"
}
edge n1 -> n2
edge n2 -> n3
edge n2 -> n4
edge n4 -> n5
edge n5 -> n6
edge n5 -> n7

8237
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff