Skip to content

Commit 0aca217

Browse files
Make changes to allow for decimal values for sequence numbers, added corresponding unit tests, and updated docs.
1 parent 717d3b3 commit 0aca217

File tree

6 files changed

+117
-3
lines changed

6 files changed

+117
-3
lines changed

cypress/integration/rendering/sequencediagram.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,18 @@ describe('Sequence diagram', () => {
242242
`
243243
);
244244
});
245+
it('should render a sequence diagram with sequence numbers that are decimals and increase by a decimal value', () => {
246+
imgSnapshotTest(
247+
`
248+
sequenceDiagram
249+
autonumber 10.1 .01
250+
Alice->Bob: Hello Bob, how are you?
251+
Bob-->Alice: I am good thanks!
252+
Alice->Bob: That is good to hear!
253+
Bob->Alice: See you later!
254+
`
255+
);
256+
});
245257
describe('font settings', () => {
246258
it('should render different note fonts when configured', () => {
247259
imgSnapshotTest(

docs/syntax/sequenceDiagram.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,16 @@ sequenceDiagram
895895
Bob-->>John: Jolly good!
896896
```
897897

898+
### Start and Increment values (v\<MERMAID_RELEASE_VERSION>+)
899+
900+
It is possible to specify a starting value and an increment value for automatic numbering. Both the starting value and increment value can include decimals up to the hundredths place.
901+
902+
Use the following syntax in your diagram definition:
903+
904+
```
905+
autonumber <start> <increment>
906+
```
907+
898908
## Actor Menus
899909

900910
Actors can have popup-menus containing individualized links to external pages. For example, if an actor represented a web service, useful links might include a link to the service health dashboard, repo containing the code for the service, or a wiki page describing the service.

packages/mermaid/src/diagrams/sequence/parser/sequenceDiagram.jison

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<INITIAL,ID,ALIAS,LINE>\#[^\n]* /* skip comments */
2828
\%%(?!\{)[^\n]* /* skip comments */
2929
[^\}]\%\%[^\n]* /* skip comments */
30-
[0-9]+(?=[ \n]+) return 'NUM';
30+
([0-9]+(\.[0-9]{1,2})?|\.[0-9]{1,2})(?=[ \n]+) return 'NUM';
3131
<ID>\@\{ { this.begin('CONFIG'); return 'CONFIG_START'; }
3232
<CONFIG>[^\}]+ { return 'CONFIG_CONTENT'; }
3333
<CONFIG>\} { this.popState(); this.popState(); return 'CONFIG_END'; }

packages/mermaid/src/diagrams/sequence/sequenceDiagram.spec.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,44 @@ Bob-->Alice: I am good thanks!`;
479479
expect(diagram.db.showSequenceNumbers()).toBe(true);
480480
});
481481

482+
it('should allow sequence numbers to have decimals up to the hundredths place', async () => {
483+
const str = `
484+
sequenceDiagram
485+
autonumber 10.1 .01
486+
Alice->Bob:Hello Bob, how are you?
487+
Note right of Bob: Bob thinks
488+
Bob-->Alice: I am good thanks!
489+
`;
490+
491+
let error = false;
492+
try {
493+
const diagram = await Diagram.fromText(str);
494+
await diagram.renderer.draw(str, 'tst', '1.2.3', diagram); // needs to be rendered for the correct value of visibility auto numbers
495+
} catch (e) {
496+
error = true;
497+
}
498+
expect(error).toBe(false);
499+
});
500+
501+
it('should not allow sequence numbers to have decimals to the thousandths place or greater', async () => {
502+
const str = `
503+
sequenceDiagram
504+
autonumber 10.001
505+
Alice->Bob:Hello Bob, how are you?
506+
Note right of Bob: Bob thinks
507+
Bob-->Alice: I am good thanks!
508+
`;
509+
510+
let error = false;
511+
try {
512+
const diagram = await Diagram.fromText(str);
513+
await diagram.renderer.draw(str, 'tst', '1.2.3', diagram); // needs to be rendered for the correct value of visibility auto numbers
514+
} catch (e) {
515+
error = true;
516+
}
517+
expect(error).toBe(true);
518+
});
519+
482520
it('should handle a sequenceDiagram definition with a title:', async () => {
483521
const diagram = await Diagram.fromText(`
484522
sequenceDiagram
@@ -2211,6 +2249,40 @@ end`;
22112249
expect(bounds.stopx).toBe(conf.width * 2 + conf.actorMargin);
22122250
expect(bounds.stopy).toBe(models.lastLoop().stopy);
22132251
});
2252+
2253+
it('should increment the sequence number with a decimal in the hundredths place', async () => {
2254+
const str = `
2255+
sequenceDiagram
2256+
autonumber 10.01 .01
2257+
Alice->Bob:Hello Bob, how are you?
2258+
Bob-->Alice: I am good thanks!
2259+
Alice-->Bob: Have a good day!
2260+
`;
2261+
2262+
const diagram = await Diagram.fromText(str);
2263+
await diagram.renderer.draw(str, 'tst', '1.2.3', diagram); // needs to be rendered for the correct value of visibility auto numbers
2264+
expect(diagram.db.showSequenceNumbers()).toBe(true);
2265+
expect(diagram.db.getMessages()[1].msgModel.sequenceIndex).toBe(10.01);
2266+
expect(diagram.db.getMessages()[2].msgModel.sequenceIndex).toBe(10.02);
2267+
expect(diagram.db.getMessages()[3].msgModel.sequenceIndex).toBe(10.03);
2268+
});
2269+
2270+
it('should increment the sequence number with a decimal in the tenths place', async () => {
2271+
const str = `
2272+
sequenceDiagram
2273+
autonumber 10.1 .1
2274+
Alice->Bob:Hello Bob, how are you?
2275+
Bob-->Alice: I am good thanks!
2276+
Alice-->Bob: Have a good day!
2277+
`;
2278+
2279+
const diagram = await Diagram.fromText(str);
2280+
await diagram.renderer.draw(str, 'tst', '1.2.3', diagram); // needs to be rendered for the correct value of visibility auto numbers
2281+
expect(diagram.db.showSequenceNumbers()).toBe(true);
2282+
expect(diagram.db.getMessages()[1].msgModel.sequenceIndex).toBe(10.1);
2283+
expect(diagram.db.getMessages()[2].msgModel.sequenceIndex).toBe(10.2);
2284+
expect(diagram.db.getMessages()[3].msgModel.sequenceIndex).toBe(10.3);
2285+
});
22142286
});
22152287

22162288
describe('when rendering a sequenceDiagram with actor mirror activated', () => {

packages/mermaid/src/diagrams/sequence/sequenceRenderer.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,15 @@ const drawMessage = async function (diagram, msgModel, lineStartY: number, diagO
602602
x = 3.5;
603603
}
604604

605+
let fontSize = '12px';
606+
const sequenceIndexLength = sequenceIndex.toString().length;
607+
608+
if (sequenceIndexLength > 5) {
609+
fontSize = '7px';
610+
} else if (sequenceIndexLength > 3) {
611+
fontSize = '9px';
612+
}
613+
605614
diagram
606615
.append('line')
607616
.attr('x1', startx)
@@ -617,7 +626,7 @@ const drawMessage = async function (diagram, msgModel, lineStartY: number, diagO
617626
.attr('x', startx)
618627
.attr('y', lineStartY + 4)
619628
.attr('font-family', 'sans-serif')
620-
.attr('font-size', '12px')
629+
.attr('font-size', fontSize)
621630
.attr('text-anchor', 'middle')
622631
.attr('class', 'sequenceNumber')
623632
.text(sequenceIndex)
@@ -1221,7 +1230,8 @@ export const draw = async function (_text: string, id: string, _version: string,
12211230
diagObj.db.LINETYPE.BIDIRECTIONAL_DOTTED,
12221231
].includes(msg.type)
12231232
) {
1224-
sequenceIndex = sequenceIndex + sequenceIndexStep;
1233+
// hitting a floating point number error, so round to 2 decimal places
1234+
sequenceIndex = Math.round((sequenceIndex + sequenceIndexStep) * 100) / 100;
12251235
}
12261236
index++;
12271237
}

packages/mermaid/src/docs/syntax/sequenceDiagram.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,16 @@ sequenceDiagram
607607
Bob-->>John: Jolly good!
608608
```
609609

610+
### Start and Increment values (v<MERMAID_RELEASE_VERSION>+)
611+
612+
It is possible to specify a starting value and an increment value for automatic numbering. Both the starting value and increment value can include decimals up to the hundredths place.
613+
614+
Use the following syntax in your diagram definition:
615+
616+
```
617+
autonumber <start> <increment>
618+
```
619+
610620
## Actor Menus
611621

612622
Actors can have popup-menus containing individualized links to external pages. For example, if an actor represented a web service, useful links might include a link to the service health dashboard, repo containing the code for the service, or a wiki page describing the service.

0 commit comments

Comments
 (0)