mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-01-14 06:43:25 +08:00
#945 Rendering from diagram data
This commit is contained in:
parent
fad76ad534
commit
13baa43081
@ -70,31 +70,11 @@
|
||||
|
||||
"stateDiagram"\s+ { console.log('Got state diagram', yytext,'#');return 'SD'; }
|
||||
"hide empty description" { console.log('HIDE_EMPTY', yytext,'#');return 'HIDE_EMPTY'; }
|
||||
// "participant" { this.begin('ID'); return 'participant'; }
|
||||
// <ID>[^\->:\n,;]+?(?=((?!\n)\s)+"as"(?!\n)\s|[#\n;]|$) { yytext = yytext.trim(); this.begin('ALIAS'); return 'ACTOR'; }
|
||||
// <ALIAS>"as" { this.popState(); this.popState(); this.begin('LINE'); return 'AS'; }
|
||||
// <ALIAS>(?:) { this.popState(); this.popState(); return 'NL'; }
|
||||
// "<<fork>>" { this.begin('LINE'); return 'else'; }
|
||||
// "<<join>>" { this.begin('LINE'); return 'par'; }
|
||||
// "and" { this.begin('LINE'); return 'and'; }
|
||||
// <LINE>[^#\n;]* { this.popState(); return 'restOfLine'; }
|
||||
// "end" return 'end';
|
||||
<INITIAL,struct>"[*]" { console.log('EDGE_STATE=',yytext); return 'EDGE_STATE';}
|
||||
<INITIAL,struct>[^:\n\s\-\{]+ { console.log('=>ID=',yytext); return 'ID';}
|
||||
<INITIAL,struct>\s*":"[^\+\->:\n,;]+ { yytext = yytext.trim(); console.log('Descr = ', yytext); return 'DESCR'; }
|
||||
// "over" return 'over';
|
||||
// "note" return 'note';
|
||||
// "activate" { this.begin('ID'); return 'activate'; }
|
||||
// "deactivate" { this.begin('ID'); return 'deactivate'; }
|
||||
// "title" return 'title';
|
||||
// "stateDiagram" return 'SD';
|
||||
// "," return ',';
|
||||
// ";" return 'NL';
|
||||
// [^\+\->:\n,;]+ { yytext = yytext.trim(); return 'ACTOR'; }
|
||||
<INITIAL,struct>"-->" return '-->';
|
||||
<struct>"--" return 'CONCURRENT';
|
||||
// "--" return '--';
|
||||
// ":"[^#\n;]+ return 'TXT';
|
||||
<<EOF>> return 'NL';
|
||||
. return 'INVALID';
|
||||
|
||||
@ -125,7 +105,7 @@ line
|
||||
|
||||
statement
|
||||
: idStatement DESCR
|
||||
| idStatement '-->' idStatement
|
||||
| idStatement '-->' idStatement {yy.addRelation($1, $3);}
|
||||
| idStatement '-->' idStatement DESCR
|
||||
| HIDE_EMPTY
|
||||
| scale WIDTH
|
||||
@ -141,8 +121,8 @@ statement
|
||||
;
|
||||
|
||||
idStatement
|
||||
: ID
|
||||
| EDGE_STATE
|
||||
: ID {$$=$1;}
|
||||
| EDGE_STATE {$$=$1;}
|
||||
;
|
||||
|
||||
notePosition
|
||||
|
@ -3,6 +3,9 @@ import { logger } from '../../logger';
|
||||
let relations = [];
|
||||
let states = {};
|
||||
|
||||
let startCnt = 0;
|
||||
let endCnt = 0;
|
||||
|
||||
/**
|
||||
* Function called by parser when a node definition has been found.
|
||||
* @param id
|
||||
@ -10,11 +13,12 @@ let states = {};
|
||||
* @param type
|
||||
* @param style
|
||||
*/
|
||||
export const addState = function(id) {
|
||||
export const addState = function(id, type) {
|
||||
if (typeof states[id] === 'undefined') {
|
||||
states[id] = {
|
||||
id: id,
|
||||
descriptions: []
|
||||
descriptions: [],
|
||||
type
|
||||
};
|
||||
}
|
||||
};
|
||||
@ -27,19 +31,35 @@ export const clear = function() {
|
||||
export const getState = function(id) {
|
||||
return states[id];
|
||||
};
|
||||
export const getstates = function() {
|
||||
export const getStates = function() {
|
||||
return states;
|
||||
};
|
||||
|
||||
export const getRelations = function() {
|
||||
// const relations1 = [{ id1: 'start1', id2: 'state1' }, { id1: 'state1', id2: 'exit1' }];
|
||||
// return relations;
|
||||
return relations;
|
||||
};
|
||||
|
||||
export const addRelation = function(relation) {
|
||||
logger.debug('Adding relation: ' + JSON.stringify(relation));
|
||||
addState(relation.id1);
|
||||
addState(relation.id2);
|
||||
relations.push(relation);
|
||||
export const addRelation = function(_id1, _id2) {
|
||||
let id1 = _id1;
|
||||
let id2 = _id2;
|
||||
let type1 = 'default';
|
||||
let type2 = 'default';
|
||||
if (_id1 === '[*]') {
|
||||
startCnt++;
|
||||
id1 = 'start' + startCnt;
|
||||
type1 = 'start';
|
||||
}
|
||||
if (_id2 === '[*]') {
|
||||
endCnt++;
|
||||
id2 = 'end' + startCnt;
|
||||
type2 = 'end';
|
||||
}
|
||||
console.log(id1, id2);
|
||||
addState(id1, type1);
|
||||
addState(id2, type2);
|
||||
relations.push({ id1, id2 });
|
||||
};
|
||||
|
||||
export const addMember = function(className, member) {
|
||||
@ -83,7 +103,7 @@ export default {
|
||||
addState,
|
||||
clear,
|
||||
getState,
|
||||
getstates,
|
||||
getStates,
|
||||
getRelations,
|
||||
addRelation,
|
||||
addMember,
|
||||
|
@ -8,6 +8,36 @@ describe('state diagram, ', function() {
|
||||
parser.yy = stateDb;
|
||||
});
|
||||
|
||||
fit('super simple', function() {
|
||||
const str = `
|
||||
stateDiagram
|
||||
[*] --> State1
|
||||
State1 --> [*]
|
||||
`;
|
||||
|
||||
parser.parse(str);
|
||||
expect(stateDb.getRelations()).toEqual([
|
||||
{ id1: 'start1', id2: 'State1' },
|
||||
{ id1: 'State1', id2: 'end1' }
|
||||
]);
|
||||
expect(stateDb.getStates()).toEqual({
|
||||
State1: {
|
||||
id: 'State1',
|
||||
type: 'default',
|
||||
descriptions: []
|
||||
},
|
||||
end1: {
|
||||
id: 'end1',
|
||||
type: 'end',
|
||||
descriptions: []
|
||||
},
|
||||
start1: {
|
||||
id: 'start1',
|
||||
type: 'start',
|
||||
descriptions: []
|
||||
}
|
||||
});
|
||||
});
|
||||
it('simple', function() {
|
||||
const str = `stateDiagram\n
|
||||
State1 : this is another string
|
||||
|
@ -406,21 +406,7 @@ export const draw = function(text, id) {
|
||||
return {};
|
||||
});
|
||||
|
||||
// const states = stateDb.getStates();
|
||||
const states = {
|
||||
start1: {
|
||||
id: 'start1',
|
||||
type: 'start'
|
||||
},
|
||||
state1: {
|
||||
id: 'state1',
|
||||
type: 'default'
|
||||
},
|
||||
exit: {
|
||||
id: 'exit1',
|
||||
type: 'end'
|
||||
}
|
||||
};
|
||||
const states = stateDb.getStates();
|
||||
const keys = Object.keys(states);
|
||||
total = keys.length;
|
||||
for (let i = 0; i < keys.length; i++) {
|
||||
@ -433,8 +419,7 @@ export const draw = function(text, id) {
|
||||
logger.info('Org height: ' + node.height);
|
||||
}
|
||||
|
||||
// const relations = stateDb.getRelations();
|
||||
const relations = [{ id1: 'start1', id2: 'state1' }, { id1: 'state1', id2: 'exit1' }];
|
||||
const relations = stateDb.getRelations();
|
||||
relations.forEach(function(relation) {
|
||||
logger.info(
|
||||
'tjoho' + getGraphId(relation.id1) + getGraphId(relation.id2) + JSON.stringify(relation)
|
||||
|
Loading…
x
Reference in New Issue
Block a user