#1033 Better handling of multiline notes

This commit is contained in:
Knut Sveidqvist 2019-10-27 17:16:29 +01:00
parent b113436055
commit 034a7c424d
2 changed files with 14 additions and 8 deletions

View File

@ -7,13 +7,14 @@ describe('State diagram', () => {
` `
stateDiagram stateDiagram
State1: The state with a note State1: The state with a note
note right of State1
Important information! You\ncan write
notes with multiple lines...
Here is another line...
And another line...
end note
`, `,
{} {}
); );
}); });
}); });
// note right of State1
// Important information! You\ncan write
// notes with multiple lines...
// Here is another line...
// end note

View File

@ -272,17 +272,22 @@ const _drawLongText = (_text, x, y, g) => {
let text = _text.replace(/\r\n/g, '<br/>'); let text = _text.replace(/\r\n/g, '<br/>');
text = text.replace(/\n/g, '<br/>'); text = text.replace(/\n/g, '<br/>');
const lines = text.split(/<br\/?>/gi); const lines = text.split(/<br\/?>/gi);
let tHeight = 1.25 * getConfig().state.noteMargin;
for (const line of lines) { for (const line of lines) {
const txt = line.trim(); const txt = line.trim();
if (txt.length > 0) { if (txt.length > 0) {
const span = textElem.append('tspan'); const span = textElem.append('tspan');
span.text(txt); span.text(txt);
const textBounds = span.node().getBBox(); if (tHeight === 0) {
textHeight += textBounds.height; const textBounds = span.node().getBBox();
tHeight += textBounds.height;
}
// console.warn('textBounds', textBounds);
textHeight += tHeight;
span.attr('x', x + getConfig().state.noteMargin); span.attr('x', x + getConfig().state.noteMargin);
span.attr('y', y + textHeight + 1.25 * getConfig().state.noteMargin); span.attr('y', y + textHeight + 1.25 * getConfig().state.noteMargin);
// textWidth = Math.max(textBounds.width, textWidth);
} }
} }
return { textWidth: textElem.node().getBBox().width, textHeight }; return { textWidth: textElem.node().getBBox().width, textHeight };