mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-02-04 07:13:25 +08:00
Handle more edge cases, and lint fixes
This commit is contained in:
parent
304f133227
commit
c063b92cc9
@ -232,17 +232,16 @@ export const addSingleLink = function (_start: string, _end: string, type: any,
|
||||
edge.stroke = type.stroke;
|
||||
edge.length = type.length > 10 ? 10 : type.length;
|
||||
}
|
||||
if (id) {
|
||||
|
||||
if (id && !edges.some((e) => e.id === id)) {
|
||||
edge.id = id;
|
||||
edge.isUserDefinedId = true;
|
||||
} else {
|
||||
const existingLinks = edges.filter((e) => e.start === edge.start && e.end === edge.end);
|
||||
if (existingLinks.length === 0) {
|
||||
edge.id = getEdgeId(edge.start, edge.end, { counter: 0, prefix: 'L' });
|
||||
//edge.id = `${edge.start}-${edge.end}-${edge.length}`;
|
||||
} else {
|
||||
edge.id = getEdgeId(edge.start, edge.end, { counter: existingLinks.length + 1, prefix: 'L' });
|
||||
//edge.id = `${edge.start}-${edge.end}-${existingLinks.length + 1}`;
|
||||
}
|
||||
}
|
||||
|
||||
@ -280,11 +279,16 @@ export const addLink = function (_start: string[], _end: string[], linkData: unk
|
||||
|
||||
// for a group syntax like A e1@--> B & C, only the first edge should have an the userDefined id
|
||||
// the rest of the edges should have auto generated ids
|
||||
let isEdgeConsumed = false;
|
||||
for (const start of _start) {
|
||||
for (const end of _end) {
|
||||
addSingleLink(start, end, linkData, !isEdgeConsumed ? id : undefined);
|
||||
isEdgeConsumed = true;
|
||||
//use the id only for last node in _start and and first node in _end
|
||||
const isLastStart = start === _start[_start.length - 1];
|
||||
const isFirstEnd = end === _end[0];
|
||||
if (isLastStart && isFirstEnd) {
|
||||
addSingleLink(start, end, linkData, id);
|
||||
} else {
|
||||
addSingleLink(start, end, linkData, undefined);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -343,6 +343,41 @@ describe('when parsing directions', function () {
|
||||
expect(data4Layout.nodes[9].label).toEqual('@for@ AS@');
|
||||
expect(data4Layout.nodes[10].label).toEqual('@for@ AS@');
|
||||
});
|
||||
|
||||
it('should handle unique edge creation with using @ and &', function () {
|
||||
const res = flow.parser.parse(`flowchart TD
|
||||
A & B e1@--> C & D
|
||||
A1 e2@--> C1 & D1
|
||||
`);
|
||||
|
||||
const data4Layout = flow.parser.yy.getData();
|
||||
expect(data4Layout.nodes.length).toBe(7);
|
||||
expect(data4Layout.edges.length).toBe(6);
|
||||
expect(data4Layout.edges[0].id).toEqual('L_A_C_0');
|
||||
expect(data4Layout.edges[1].id).toEqual('L_A_D_0');
|
||||
expect(data4Layout.edges[2].id).toEqual('e1');
|
||||
expect(data4Layout.edges[3].id).toEqual('L_B_D_0');
|
||||
expect(data4Layout.edges[4].id).toEqual('e2');
|
||||
expect(data4Layout.edges[5].id).toEqual('L_A1_D1_0');
|
||||
});
|
||||
|
||||
it('should handle redefine same edge ids again', function () {
|
||||
const res = flow.parser.parse(`flowchart TD
|
||||
A & B e1@--> C & D
|
||||
A1 e1@--> C1 & D1
|
||||
`);
|
||||
|
||||
const data4Layout = flow.parser.yy.getData();
|
||||
expect(data4Layout.nodes.length).toBe(7);
|
||||
expect(data4Layout.edges.length).toBe(6);
|
||||
expect(data4Layout.edges[0].id).toEqual('L_A_C_0');
|
||||
expect(data4Layout.edges[1].id).toEqual('L_A_D_0');
|
||||
expect(data4Layout.edges[2].id).toEqual('e1');
|
||||
expect(data4Layout.edges[3].id).toEqual('L_B_D_0');
|
||||
expect(data4Layout.edges[4].id).toEqual('L_A1_C1_0');
|
||||
expect(data4Layout.edges[5].id).toEqual('L_A1_D1_0');
|
||||
});
|
||||
|
||||
it.skip('should be possible to use @ syntax to add labels with trail spaces', function () {
|
||||
const res = flow.parser.parse(
|
||||
`flowchart TB
|
||||
|
Loading…
x
Reference in New Issue
Block a user