mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-02 07:06:43 +02:00
refactor: handle StateDB .extract()
properly
Fix the StateDB properly for stateDiagram-v1 vs stateDiagram-v2.
This commit is contained in:

committed by
saurabhg772244

parent
548256507d
commit
cfe710f42b
@@ -9,7 +9,7 @@ setConfig({
|
|||||||
describe('state parser can parse...', () => {
|
describe('state parser can parse...', () => {
|
||||||
let stateDb;
|
let stateDb;
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
stateDb = new StateDB();
|
stateDb = new StateDB(2);
|
||||||
stateDiagram.parser.yy = stateDb;
|
stateDiagram.parser.yy = stateDb;
|
||||||
stateDiagram.parser.yy.clear();
|
stateDiagram.parser.yy.clear();
|
||||||
});
|
});
|
||||||
|
@@ -9,7 +9,7 @@ setConfig({
|
|||||||
describe('ClassDefs and classes when parsing a State diagram', () => {
|
describe('ClassDefs and classes when parsing a State diagram', () => {
|
||||||
let stateDb;
|
let stateDb;
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
stateDb = new StateDB();
|
stateDb = new StateDB(2);
|
||||||
stateDiagram.parser.yy = stateDb;
|
stateDiagram.parser.yy = stateDb;
|
||||||
stateDiagram.parser.yy.clear();
|
stateDiagram.parser.yy.clear();
|
||||||
});
|
});
|
||||||
@@ -135,7 +135,6 @@ describe('ClassDefs and classes when parsing a State diagram', () => {
|
|||||||
diagram += '[*]:::exampleStyleClass --> b\n';
|
diagram += '[*]:::exampleStyleClass --> b\n';
|
||||||
|
|
||||||
stateDiagram.parser.parse(diagram);
|
stateDiagram.parser.parse(diagram);
|
||||||
stateDiagram.parser.yy.extract(stateDiagram.parser.yy.getRootDocV2());
|
|
||||||
|
|
||||||
const states = stateDiagram.parser.yy.getStates();
|
const states = stateDiagram.parser.yy.getStates();
|
||||||
const classes = stateDiagram.parser.yy.getClasses();
|
const classes = stateDiagram.parser.yy.getClasses();
|
||||||
|
@@ -58,9 +58,14 @@ const newDoc = () => {
|
|||||||
const clone = (o) => JSON.parse(JSON.stringify(o));
|
const clone = (o) => JSON.parse(JSON.stringify(o));
|
||||||
|
|
||||||
export class StateDB {
|
export class StateDB {
|
||||||
constructor() {
|
/**
|
||||||
|
* @param {1 | 2} version - v1 renderer or v2 renderer.
|
||||||
|
*/
|
||||||
|
constructor(version) {
|
||||||
this.clear();
|
this.clear();
|
||||||
|
|
||||||
|
this.version = version;
|
||||||
|
|
||||||
// Needed for JISON since it only supports direct properties
|
// Needed for JISON since it only supports direct properties
|
||||||
this.setRootDoc = this.setRootDoc.bind(this);
|
this.setRootDoc = this.setRootDoc.bind(this);
|
||||||
this.getDividerId = this.getDividerId.bind(this);
|
this.getDividerId = this.getDividerId.bind(this);
|
||||||
@@ -68,6 +73,12 @@ export class StateDB {
|
|||||||
this.trimColon = this.trimColon.bind(this);
|
this.trimColon = this.trimColon.bind(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
* @type {1 | 2}
|
||||||
|
*/
|
||||||
|
version;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
* @type {Array}
|
* @type {Array}
|
||||||
@@ -130,7 +141,11 @@ export class StateDB {
|
|||||||
log.info('Setting root doc', o);
|
log.info('Setting root doc', o);
|
||||||
// rootDoc = { id: 'root', doc: o };
|
// rootDoc = { id: 'root', doc: o };
|
||||||
this.rootDoc = o;
|
this.rootDoc = o;
|
||||||
this.extract(o);
|
if (this.version === 1) {
|
||||||
|
this.extract(o);
|
||||||
|
} else {
|
||||||
|
this.extract(this.getRootDocV2());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getRootDoc() {
|
getRootDoc() {
|
||||||
@@ -190,6 +205,10 @@ export class StateDB {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
getRootDocV2() {
|
getRootDocV2() {
|
||||||
this.docTranslator({ id: 'root' }, { id: 'root', doc: this.rootDoc }, true);
|
this.docTranslator({ id: 'root' }, { id: 'root', doc: this.rootDoc }, true);
|
||||||
return { id: 'root', doc: this.rootDoc };
|
return { id: 'root', doc: this.rootDoc };
|
||||||
|
@@ -3,7 +3,7 @@ import { StateDB } from './stateDb.js';
|
|||||||
describe('State Diagram stateDb', () => {
|
describe('State Diagram stateDb', () => {
|
||||||
let stateDb;
|
let stateDb;
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
stateDb = new StateDB();
|
stateDb = new StateDB(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('addStyleClass', () => {
|
describe('addStyleClass', () => {
|
||||||
@@ -23,7 +23,7 @@ describe('State Diagram stateDb', () => {
|
|||||||
describe('addDescription to a state', () => {
|
describe('addDescription to a state', () => {
|
||||||
let stateDb;
|
let stateDb;
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
stateDb = new StateDB();
|
stateDb = new StateDB(1);
|
||||||
stateDb.addState('state1');
|
stateDb.addState('state1');
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -79,7 +79,7 @@ describe('State Diagram stateDb', () => {
|
|||||||
describe('state db class', () => {
|
describe('state db class', () => {
|
||||||
let stateDb;
|
let stateDb;
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
stateDb = new StateDB();
|
stateDb = new StateDB(1);
|
||||||
});
|
});
|
||||||
// This is to ensure that functions used in state JISON are exposed as function from StateDb
|
// This is to ensure that functions used in state JISON are exposed as function from StateDb
|
||||||
it('should have functions used in flow JISON as own property', () => {
|
it('should have functions used in flow JISON as own property', () => {
|
||||||
|
@@ -6,7 +6,7 @@ describe('state diagram V2, ', function () {
|
|||||||
describe('when parsing an info graph it', function () {
|
describe('when parsing an info graph it', function () {
|
||||||
let stateDb;
|
let stateDb;
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
stateDb = new StateDB();
|
stateDb = new StateDB(2);
|
||||||
parser.yy = stateDb;
|
parser.yy = stateDb;
|
||||||
stateDiagram.parser.yy = stateDb;
|
stateDiagram.parser.yy = stateDb;
|
||||||
stateDiagram.parser.yy.clear();
|
stateDiagram.parser.yy.clear();
|
||||||
|
@@ -8,7 +8,7 @@ import renderer from './stateRenderer-v3-unified.js';
|
|||||||
export const diagram: DiagramDefinition = {
|
export const diagram: DiagramDefinition = {
|
||||||
parser,
|
parser,
|
||||||
get db() {
|
get db() {
|
||||||
return new StateDB();
|
return new StateDB(2);
|
||||||
},
|
},
|
||||||
renderer,
|
renderer,
|
||||||
styles,
|
styles,
|
||||||
|
@@ -5,7 +5,7 @@ describe('state diagram, ', function () {
|
|||||||
describe('when parsing an info graph it', function () {
|
describe('when parsing an info graph it', function () {
|
||||||
let stateDb;
|
let stateDb;
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
stateDb = new StateDB();
|
stateDb = new StateDB(1);
|
||||||
parser.yy = stateDb;
|
parser.yy = stateDb;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@@ -8,7 +8,7 @@ import renderer from './stateRenderer.js';
|
|||||||
export const diagram: DiagramDefinition = {
|
export const diagram: DiagramDefinition = {
|
||||||
parser,
|
parser,
|
||||||
get db() {
|
get db() {
|
||||||
return new StateDB();
|
return new StateDB(1);
|
||||||
},
|
},
|
||||||
renderer,
|
renderer,
|
||||||
styles,
|
styles,
|
||||||
|
@@ -136,7 +136,6 @@ const renderDoc = (doc, diagram, parentId, altBkg, root, domDocument, diagObj) =
|
|||||||
return {};
|
return {};
|
||||||
});
|
});
|
||||||
|
|
||||||
diagObj.db.extract(doc);
|
|
||||||
const states = diagObj.db.getStates();
|
const states = diagObj.db.getStates();
|
||||||
const relations = diagObj.db.getRelations();
|
const relations = diagObj.db.getRelations();
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user