mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-02 07:06:43 +02:00
Merge pull request #1259 from mermaid-js/bug/1257_autonumbering
#1257 Adding the autonumber keyword + tests and docs
This commit is contained in:
@@ -149,5 +149,38 @@ context('Sequence diagram', () => {
|
|||||||
{}
|
{}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
it('should render autonumber when configured with such', () => {
|
||||||
|
imgSnapshotTest(
|
||||||
|
`
|
||||||
|
sequenceDiagram
|
||||||
|
Alice->>John: Hello John, how are you?
|
||||||
|
loop Healthcheck
|
||||||
|
John->>John: Fight against hypochondria
|
||||||
|
end
|
||||||
|
Note right of John: Rational thoughts!
|
||||||
|
John-->>Alice: Great!
|
||||||
|
John->>Bob: How about you?
|
||||||
|
Bob-->>John: Jolly good!
|
||||||
|
`,
|
||||||
|
{sequence: { actorMargin: 50, showSequenceNumbers: true }}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
it('should render autonumber when autonumber keyword is used', () => {
|
||||||
|
imgSnapshotTest(
|
||||||
|
`
|
||||||
|
sequenceDiagram
|
||||||
|
autonumber
|
||||||
|
Alice->>John: Hello John, how are you?
|
||||||
|
loop Healthcheck
|
||||||
|
John->>John: Fight against hypochondria
|
||||||
|
end
|
||||||
|
Note right of John: Rational thoughts!
|
||||||
|
John-->>Alice: Great!
|
||||||
|
John->>Bob: How about you?
|
||||||
|
Bob-->>John: Jolly good!
|
||||||
|
`,
|
||||||
|
{}
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@@ -20,10 +20,15 @@
|
|||||||
<h1>info below</h1>
|
<h1>info below</h1>
|
||||||
<div style="display: flex;width: 100%; height: 100%">
|
<div style="display: flex;width: 100%; height: 100%">
|
||||||
<div class="mermaid" style="width: 100%; height: 100%">
|
<div class="mermaid" style="width: 100%; height: 100%">
|
||||||
stateDiagram
|
sequenceDiagram
|
||||||
|
Alice->>John: Hello John, how are you?
|
||||||
NotFound --> NotFound: Status
|
loop Healthcheck
|
||||||
NotFound --> NotFound: Stop
|
John->>John: Fight against hypochondria
|
||||||
|
end
|
||||||
|
Note right of John: Rational thoughts!
|
||||||
|
John-->>Alice: Great!
|
||||||
|
John->>Bob: How about you?
|
||||||
|
Bob-->>John: Jolly good!
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -36,7 +41,7 @@
|
|||||||
logLevel: 0,
|
logLevel: 0,
|
||||||
flowchart: { curve: 'linear', "htmlLabels": false },
|
flowchart: { curve: 'linear', "htmlLabels": false },
|
||||||
// gantt: { axisFormat: '%m/%d/%Y' },
|
// gantt: { axisFormat: '%m/%d/%Y' },
|
||||||
sequence: { actorMargin: 50 },
|
sequence: { actorMargin: 50, showSequenceNumbers: true },
|
||||||
// sequenceDiagram: { actorMargin: 300 } // deprecated
|
// sequenceDiagram: { actorMargin: 300 } // deprecated
|
||||||
fontFamily: '"Noto Sans SC", sans-serif'
|
fontFamily: '"Noto Sans SC", sans-serif'
|
||||||
});
|
});
|
||||||
|
@@ -282,6 +282,43 @@ sequenceDiagram
|
|||||||
John-->>Alice: Great!
|
John-->>Alice: Great!
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## sequenceNumbers
|
||||||
|
|
||||||
|
It is possiebl to get a sequence number attached to each arrow in a sequence diagram. This can be configured when adding mermaid to the website as shown below:
|
||||||
|
```
|
||||||
|
<script>
|
||||||
|
mermaid.initialize({
|
||||||
|
sequence: { showSequenceNumbers: true },
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
```
|
||||||
|
|
||||||
|
It can also be be turned on via the diagram code as in the diagram:
|
||||||
|
```
|
||||||
|
sequenceDiagram
|
||||||
|
autonumber
|
||||||
|
Alice->>John: Hello John, how are you?
|
||||||
|
loop Healthcheck
|
||||||
|
John->>John: Fight against hypochondria
|
||||||
|
end
|
||||||
|
Note right of John: Rational thoughts!
|
||||||
|
John-->>Alice: Great!
|
||||||
|
John->>Bob: How about you?
|
||||||
|
Bob-->>John: Jolly good!
|
||||||
|
```
|
||||||
|
```mermaid
|
||||||
|
sequenceDiagram
|
||||||
|
autonumber
|
||||||
|
Alice->>John: Hello John, how are you?
|
||||||
|
loop Healthcheck
|
||||||
|
John->>John: Fight against hypochondria
|
||||||
|
end
|
||||||
|
Note right of John: Rational thoughts!
|
||||||
|
John-->>Alice: Great!
|
||||||
|
John->>Bob: How about you?
|
||||||
|
Bob-->>John: Jolly good!
|
||||||
|
```
|
||||||
|
|
||||||
## Styling
|
## Styling
|
||||||
|
|
||||||
Styling of a sequence diagram is done by defining a number of css classes. During rendering these classes are extracted from the file located at src/themes/sequence.scss
|
Styling of a sequence diagram is done by defining a number of css classes. During rendering these classes are extracted from the file located at src/themes/sequence.scss
|
||||||
|
@@ -47,6 +47,7 @@
|
|||||||
"deactivate" { this.begin('ID'); return 'deactivate'; }
|
"deactivate" { this.begin('ID'); return 'deactivate'; }
|
||||||
"title" return 'title';
|
"title" return 'title';
|
||||||
"sequenceDiagram" return 'SD';
|
"sequenceDiagram" return 'SD';
|
||||||
|
"autonumber" return 'autonumber';
|
||||||
"," return ',';
|
"," return ',';
|
||||||
";" return 'NL';
|
";" return 'NL';
|
||||||
[^\+\->:\n,;]+ { yytext = yytext.trim(); return 'ACTOR'; }
|
[^\+\->:\n,;]+ { yytext = yytext.trim(); return 'ACTOR'; }
|
||||||
@@ -91,6 +92,7 @@ statement
|
|||||||
: 'participant' actor 'AS' restOfLine 'NL' {$2.description=$4; $$=$2;}
|
: 'participant' actor 'AS' restOfLine 'NL' {$2.description=$4; $$=$2;}
|
||||||
| 'participant' actor 'NL' {$$=$2;}
|
| 'participant' actor 'NL' {$$=$2;}
|
||||||
| signal 'NL'
|
| signal 'NL'
|
||||||
|
| autonumber {yy.enableSequenceNumbers()}
|
||||||
| 'activate' actor 'NL' {$$={type: 'activeStart', signalType: yy.LINETYPE.ACTIVE_START, actor: $2};}
|
| 'activate' actor 'NL' {$$={type: 'activeStart', signalType: yy.LINETYPE.ACTIVE_START, actor: $2};}
|
||||||
| 'deactivate' actor 'NL' {$$={type: 'activeEnd', signalType: yy.LINETYPE.ACTIVE_END, actor: $2};}
|
| 'deactivate' actor 'NL' {$$={type: 'activeEnd', signalType: yy.LINETYPE.ACTIVE_END, actor: $2};}
|
||||||
| note_statement 'NL'
|
| note_statement 'NL'
|
||||||
|
@@ -4,7 +4,7 @@ let actors = {};
|
|||||||
let messages = [];
|
let messages = [];
|
||||||
const notes = [];
|
const notes = [];
|
||||||
let title = '';
|
let title = '';
|
||||||
|
let sequenceNumbersEnabled = false;
|
||||||
export const addActor = function(id, name, description) {
|
export const addActor = function(id, name, description) {
|
||||||
// Don't allow description nulling
|
// Don't allow description nulling
|
||||||
const old = actors[id];
|
const old = actors[id];
|
||||||
@@ -80,6 +80,10 @@ export const getActorKeys = function() {
|
|||||||
export const getTitle = function() {
|
export const getTitle = function() {
|
||||||
return title;
|
return title;
|
||||||
};
|
};
|
||||||
|
export const enableSequenceNumbers = function() {
|
||||||
|
sequenceNumbersEnabled = true;
|
||||||
|
};
|
||||||
|
export const showSequenceNumbers = () => sequenceNumbersEnabled;
|
||||||
|
|
||||||
export const clear = function() {
|
export const clear = function() {
|
||||||
actors = {};
|
actors = {};
|
||||||
@@ -210,6 +214,8 @@ export default {
|
|||||||
addActor,
|
addActor,
|
||||||
addMessage,
|
addMessage,
|
||||||
addSignal,
|
addSignal,
|
||||||
|
enableSequenceNumbers,
|
||||||
|
showSequenceNumbers,
|
||||||
getMessages,
|
getMessages,
|
||||||
getActors,
|
getActors,
|
||||||
getActor,
|
getActor,
|
||||||
|
@@ -33,6 +33,27 @@ describe('when parsing a sequenceDiagram', function() {
|
|||||||
expect(messages[0].from).toBe('Alice');
|
expect(messages[0].from).toBe('Alice');
|
||||||
expect(messages[2].from).toBe('Bob');
|
expect(messages[2].from).toBe('Bob');
|
||||||
});
|
});
|
||||||
|
it('it should not show sequence numbers per default', function() {
|
||||||
|
const str =
|
||||||
|
'sequenceDiagram\n' +
|
||||||
|
'Alice->Bob:Hello Bob, how are you?\n' +
|
||||||
|
'Note right of Bob: Bob thinks\n' +
|
||||||
|
'Bob-->Alice: I am good thanks!';
|
||||||
|
|
||||||
|
parser.parse(str);
|
||||||
|
expect(parser.yy.showSequenceNumbers()).toBe(false);
|
||||||
|
});
|
||||||
|
it('it should show sequence numbers when autonumber is enabled', function() {
|
||||||
|
const str =
|
||||||
|
'sequenceDiagram\n' +
|
||||||
|
'autonumber\n' +
|
||||||
|
'Alice->Bob:Hello Bob, how are you?\n' +
|
||||||
|
'Note right of Bob: Bob thinks\n' +
|
||||||
|
'Bob-->Alice: I am good thanks!';
|
||||||
|
|
||||||
|
parser.parse(str);
|
||||||
|
expect(parser.yy.showSequenceNumbers()).toBe(true);
|
||||||
|
});
|
||||||
it('it should handle a sequenceDiagram definition with a title', function() {
|
it('it should handle a sequenceDiagram definition with a title', function() {
|
||||||
const str =
|
const str =
|
||||||
'sequenceDiagram\n' +
|
'sequenceDiagram\n' +
|
||||||
|
@@ -342,7 +342,7 @@ const drawMessage = function(elem, startx, stopx, verticalPos, msg, sequenceInde
|
|||||||
}
|
}
|
||||||
|
|
||||||
// add node number
|
// add node number
|
||||||
if (conf.showSequenceNumbers) {
|
if (sequenceDb.showSequenceNumbers() || conf.showSequenceNumbers) {
|
||||||
line.attr('marker-start', 'url(' + url + '#sequencenumber)');
|
line.attr('marker-start', 'url(' + url + '#sequencenumber)');
|
||||||
g.append('text')
|
g.append('text')
|
||||||
.attr('x', startx)
|
.attr('x', startx)
|
||||||
|
Reference in New Issue
Block a user