Skip to content

Commit

Permalink
mermaid-js#2032 Adding new statement to add choice shape in state dia…
Browse files Browse the repository at this point in the history
…grams
  • Loading branch information
knsv committed May 1, 2021
1 parent b20f866 commit 6dc7112
Show file tree
Hide file tree
Showing 11 changed files with 353 additions and 189 deletions.
18 changes: 18 additions & 0 deletions cypress/integration/rendering/stateDiagram-v2.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,24 @@ describe('State diagram', () => {
}
);
});
it('v2 it should be possibel to use a choice', () => {
imgSnapshotTest(
`
stateDiagram-v2
[*] --> Off
Off --> On
state MyChoice [[choice]]
On --> MyChoice
MyChoice --> Washing
MyChoice --> Drying
Washing --> Finished
Finished --> [*]
`,
{
logLevel: 0,
}
);
});
it('v2 Simplest composite state', () => {
imgSnapshotTest(
`
Expand Down
45 changes: 5 additions & 40 deletions cypress/platform/knsv.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,51 +56,16 @@
linkStyle 0,1 color:orange, stroke: orange;
</div>
<div class="mermaid" style="width: 100%; height: 20%;">
%% The width of composite states does not grow with the title
stateDiagram-v2
[*] --> Off
Off --> On
On --> Select
Select --> Washing
state MyChoice [[choice]]
On --> MyChoice
MyChoice --> Washing
MyChoice --> Drying
Washing --> Finished
Finished --> [*]
state Select
{
[*] --> Prg1
Prg1 --> [*]
[*] --> Prg2
Prg2 --> [*]
[*] --> Prg3
Prg3 --> [*]
}

state Washing {
state Using_Prg1 {
[*] --> P1Step1
P1Step1 --> P1Step2
P1Step2 --> P1Step3
P1Step3 --> [*]
}
state Using_Prg2 {
[*] --> P2Step1
P2Step1 --> P2Step2
P2Step2 --> P2Step3
P2Step3 --> [*]
}

state Using_Prg3 {
[*] --> Step1
Step1 --> Step2
Step2 --> [*]
}
[*] --> Using_Prg1
[*] --> Using_Prg2
[*] --> Using_Prg3
Using_Prg1 --> [*]
Using_Prg2 --> [*]
Using_Prg3 --> [*]
}


</div>
<div class="mermaid2" style="width: 100%; height: 20%;">
stateDiagram-v2
Expand Down
210 changes: 138 additions & 72 deletions dist/mermaid.core.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/mermaid.core.js.map

Large diffs are not rendered by default.

210 changes: 138 additions & 72 deletions dist/mermaid.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/mermaid.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/mermaid.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/mermaid.min.js.map

Large diffs are not rendered by default.

39 changes: 39 additions & 0 deletions src/dagre-wrapper/nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,44 @@ const question = (parent, node) => {
return shapeSvg;
};

const choice = (parent, node) => {
const shapeSvg = parent
.insert('g')
.attr('class', 'node default')
.attr('id', node.domId || node.id);

const s = 28;
const points = [
{ x: 0, y: s / 2 },
{ x: s / 2, y: 0 },
{ x: 0, y: -s / 2 },
{ x: -s / 2, y: 0 }
];

const choice = shapeSvg.insert('polygon', ':first-child').attr(
'points',
points
.map(function(d) {
return d.x + ',' + d.y;
})
.join(' ')
);
// center the circle around its coordinate
choice
.attr('class', 'state-start')
.attr('r', 7)
.attr('width', 28)
.attr('height', 28);
node.width = 28;
node.height = 28;

node.intersect = function(point) {
return intersect.circle(node, 14, point);
};

return shapeSvg;
};

const hexagon = (parent, node) => {
const { shapeSvg, bbox } = labelHelper(parent, node, undefined, true);

Expand Down Expand Up @@ -836,6 +874,7 @@ const shapes = {
question,
rect,
rectWithTitle,
choice,
circle,
stadium,
hexagon,
Expand Down
5 changes: 5 additions & 0 deletions src/diagrams/state/parser/stateDiagram.jison
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@
<INITIAL,struct>"state"\s+ { console.log('Starting STATE');this.pushState('STATE'); }
<STATE>.*"<<fork>>" {this.popState();yytext=yytext.slice(0,-8).trim(); /*console.warn('Fork Fork: ',yytext);*/return 'FORK';}
<STATE>.*"<<join>>" {this.popState();yytext=yytext.slice(0,-8).trim();/*console.warn('Fork Join: ',yytext);*/return 'JOIN';}
<STATE>.*"<<choice>>" {this.popState();yytext=yytext.slice(0,-10).trim();/*console.warn('Fork Join: ',yytext);*/return 'CHOICE';}
<STATE>.*"[[fork]]" {this.popState();yytext=yytext.slice(0,-8).trim();/*console.warn('Fork Fork: ',yytext);*/return 'FORK';}
<STATE>.*"[[join]]" {this.popState();yytext=yytext.slice(0,-8).trim();/*console.warn('Fork Join: ',yytext);*/return 'JOIN';}
<STATE>.*"[[choice]]" {this.popState();yytext=yytext.slice(0,-10).trim();/*console.warn('Fork Join: ',yytext);*/return 'CHOICE';}
<STATE>["] { console.log('Starting STATE_STRING');this.begin("STATE_STRING");}
<STATE>\s*"as"\s+ {this.popState();this.pushState('STATE_ID');return "AS";}
<STATE_ID>[^\n\{]* {this.popState();/* console.log('STATE_ID', yytext);*/return "ID";}
Expand Down Expand Up @@ -168,6 +170,9 @@ statement
| JOIN {
$$={ stmt: 'state', id: $1, type: 'join' }
}
| CHOICE {
$$={ stmt: 'state', id: $1, type: 'choice' }
}
| CONCURRENT {
$$={ stmt: 'state', id: yy.getDividerId(), type: 'divider' }
}
Expand Down
5 changes: 5 additions & 0 deletions src/diagrams/state/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ g.stateGroup line {
stroke: ${options.nodeBorder};
stroke-width: 1px;
}
.node polygon {
fill: ${options.mainBkg};
stroke: ${options.nodeBorder};
stroke-width: 1px;
}
#statediagram-barbEnd {
fill: ${options.lineColor};
}
Expand Down

0 comments on commit 6dc7112

Please sign in to comment.