From 4a1eb5512746e5dd7a178ad426c14cd5dcf0e536 Mon Sep 17 00:00:00 2001 From: Knut Sveidqvist Date: Wed, 23 Oct 2019 19:22:36 +0200 Subject: [PATCH] #1022 Fix for long names for state diagrams --- .../rendering/stateDiagram.spec.js | 24 +++++++++++++++++++ src/diagrams/state/parser/stateDiagram.jison | 12 +++++++++- src/diagrams/state/shapes.js | 8 +++++-- src/diagrams/state/stateDb.js | 9 ++++++- 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/cypress/integration/rendering/stateDiagram.spec.js b/cypress/integration/rendering/stateDiagram.spec.js index 2c6bdb1e53..cba7bb63cb 100644 --- a/cypress/integration/rendering/stateDiagram.spec.js +++ b/cypress/integration/rendering/stateDiagram.spec.js @@ -13,6 +13,30 @@ describe('State diagram', () => { ); cy.get('svg'); }); + it('should render a long descriptions instead of id when available', () => { + imgSnapshotTest( + ` + stateDiagram + + [*] --> S1 + state "Some long name" as S1 + `, + { logLevel: 0 } + ); + cy.get('svg'); + }); + it('should render a long descriptions with additional descriptions', () => { + imgSnapshotTest( + ` + stateDiagram + + [*] --> S1 + state "Some long name" as S1: The description + `, + { logLevel: 0 } + ); + cy.get('svg'); + }); it('should render a single state with short descr', () => { imgSnapshotTest( ` diff --git a/src/diagrams/state/parser/stateDiagram.jison b/src/diagrams/state/parser/stateDiagram.jison index 09b151ea49..a5ad58cf90 100644 --- a/src/diagrams/state/parser/stateDiagram.jison +++ b/src/diagrams/state/parser/stateDiagram.jison @@ -133,7 +133,17 @@ statement /* console.warn('Adding document for state without id ', $1);*/ $$={ stmt: 'state', id: $1, type: 'default', description: '', doc: $3 } } - | STATE_DESCR AS ID { $$={stmt: 'state', id: $3, type: 'default', description: $1.trim()};} + | STATE_DESCR AS ID { + var id=$3; + var description = $1.trim(); + if($3.match(':')){ + var parts = $3.split(':'); + id=parts[0]; + description = [description, parts[1]]; + } + $$={stmt: 'state', id: id, type: 'default', description: description}; + + } | STATE_DESCR AS ID STRUCT_START document STRUCT_STOP { //console.warn('Adding document for state with id ', $3, $4); yy.addDocument($3); diff --git a/src/diagrams/state/shapes.js b/src/diagrams/state/shapes.js index ad387efbc8..ce77de8619 100644 --- a/src/diagrams/state/shapes.js +++ b/src/diagrams/state/shapes.js @@ -76,7 +76,7 @@ export const drawDescrState = (g, stateDef) => { .attr('y', getConfig().state.textHeight + 1.5 * getConfig().state.padding) .attr('font-size', getConfig().state.fontSize) .attr('class', 'state-title') - .text(stateDef.id); + .text(stateDef.descriptions[0]); const titleBox = title.node().getBBox(); const titleHeight = titleBox.height; @@ -94,8 +94,12 @@ export const drawDescrState = (g, stateDef) => { .attr('class', 'state-description'); let isFirst = true; + let isSecond = true; stateDef.descriptions.forEach(function(descr) { - addTspan(description, descr, isFirst); + if (!isFirst) { + addTspan(description, descr, isSecond); + isSecond = false; + } isFirst = false; }); diff --git a/src/diagrams/state/stateDb.js b/src/diagrams/state/stateDb.js index 11fc72c14e..44d6fe6ea7 100644 --- a/src/diagrams/state/stateDb.js +++ b/src/diagrams/state/stateDb.js @@ -64,7 +64,14 @@ export const addState = function(id, type, doc, descr, note) { currentDocument.states[id].type = type; } } - if (descr) addDescription(id, descr.trim()); + if (descr) { + if (typeof descr === 'string') addDescription(id, descr.trim()); + + if (typeof descr === 'object') { + descr.forEach(des => addDescription(id, des.trim())); + } + } + if (note) currentDocument.states[id].note = note; };