Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Directives, sequenceDiagram refactor, config normalization, many tweaks #1523

Merged
merged 38 commits into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
038d70a
Diagram showing sequence diagram with long message text
knsv Jun 19, 2020
feb9ffa
Diagram showing sequence diagram with cut long message text
knsv Jun 19, 2020
1118c33
Fixed incorrect positioning and bounds for actors and notes with wrap…
chrismoran-bkt Jun 19, 2020
710b88e
Merge branch 'feature/1483_long_messages' of https://github.com/merma…
chrismoran-bkt Jun 19, 2020
5646518
Tweaked the PR for long messages
chrismoran-bkt Jun 19, 2020
1825439
Merge branch 'mermaid-js-develop' into develop
chrismoran-bkt Jun 19, 2020
809ba2b
Fixed: width of notes left of and right of
chrismoran-bkt Jun 19, 2020
280c89a
Merge pull request #1484 from chrismoran-bkt/develop
knsv Jun 19, 2020
ee109c2
noteModel generated during calculation of loop bounds
cmmoran Jun 20, 2020
f95b5d0
Merge pull request #1489 from cmmoran/develop
cmmoran Jun 21, 2020
7d9bf83
addition: added bounds models for bounds checking in unit tests.
cmmoran Jun 22, 2020
217bd1f
Fixed test cases for sequence diagrams
cmmoran Jun 26, 2020
d3647e2
Merge pull request #1501 from cmmoran/develop
cmmoran Jun 26, 2020
6915634
Missed `activationWidth` new configuration option for sequence diagrams
cmmoran Jun 26, 2020
06dad78
Merge pull request #1502 from cmmoran/develop
cmmoran Jun 26, 2020
fcd2126
Getting closer
cmmoran Jun 28, 2020
fed142d
Merge branch 'mermaid-js-develop' into develop
cmmoran Jun 30, 2020
083d0c9
Merge branch 'develop' into feature/1483_long_messages
cmmoran Jun 30, 2020
6a3aaf3
Merge pull request #1510 from cmmoran/feature/1483_long_messages
cmmoran Jun 30, 2020
d42b95b
messageFont, noteFont, and actorFont getConfig() calls were not speci…
cmmoran Jun 30, 2020
3365fa1
Merge pull request #1511 from cmmoran/feature/1483_long_messages
cmmoran Jun 30, 2020
5b37536
math is hard sometimes?
cmmoran Jun 30, 2020
b441475
Merge pull request #1512 from cmmoran/feature/1483_long_messages
cmmoran Jun 30, 2020
6c6f683
conf.messageFont() would have a different cacheKey based on the cache…
cmmoran Jun 30, 2020
ecb4671
Merge pull request #1513 from cmmoran/feature/1483_long_messages
cmmoran Jun 30, 2020
174b098
loop/opt/alt/etc titles should always wrap
cmmoran Jun 30, 2020
cbb69bd
() => is not the same as function() {} smh
cmmoran Jul 1, 2020
eb3f62f
Merge pull request #1517 from cmmoran/feature/1483_long_messages
cmmoran Jul 1, 2020
fda1d7c
Merge remote-tracking branch 'upstream/feature/1483_long_messages' in…
cmmoran Jul 1, 2020
2ffb298
One small refactor to fix activation starty
cmmoran Jul 1, 2020
9952f77
Merge pull request #1518 from cmmoran/feature/1483_long_messages
cmmoran Jul 1, 2020
ee425d2
Merge pull request #1520 from mermaid-js/develop
cmmoran Jul 1, 2020
ca86ab7
added hasBreaks and splitBreaks to common.js
cmmoran Jul 1, 2020
1cc2e74
Merge pull request #1521 from cmmoran/feature/1483_long_messages
cmmoran Jul 1, 2020
01a2d90
sequence numbers for dark mode should probably be black since the cir…
cmmoran Jul 1, 2020
35a4709
Merge pull request #1522 from cmmoran/feature/1483_long_messages
cmmoran Jul 1, 2020
b3a465d
Merge branch 'develop' into feature/1483_long_messages
knsv Jul 2, 2020
c655cfa
Merge branch 'develop' into feature/1483_long_messages
knsv Jul 2, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,9 @@ const config = {
wrap: false,
/**
* This sets the auto-wrap padding for the diagram (sides only)
* **Default value 15.
* **Default value 10.
*/
wrapPadding: 15,
wrapPadding: 10,
/**
* This sets the width of the loop-box (loop, alt, opt, par)
* **Default value 50.
Expand All @@ -306,24 +306,27 @@ const config = {
*/
labelBoxHeight: 20,
messageFont: () => {
const c = getConfig();
return {
fontFamily: config.messageFontFamily,
fontSize: config.messageFontSize,
fontWeight: config.messageFontWeight
fontFamily: c.messageFontFamily,
fontSize: c.messageFontSize,
fontWeight: c.messageFontWeight
};
},
noteFont: () => {
const c = getConfig();
return {
fontFamily: config.noteFontFamily,
fontSize: config.noteFontSize,
fontWeight: config.noteFontWeight
fontFamily: c.noteFontFamily,
fontSize: c.noteFontSize,
fontWeight: c.noteFontWeight
};
},
actorFont: () => {
const c = getConfig();
return {
fontFamily: config.actorFontFamily,
fontSize: config.actorFontSize,
fontWeight: config.actorFontWeight
fontFamily: c.actorFontFamily,
fontSize: c.actorFontSize,
fontWeight: c.actorFontWeight
};
}
},
Expand Down
135 changes: 65 additions & 70 deletions src/diagrams/sequence/sequenceRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export const bounds = {
},
newActivation: function(message, diagram, actors) {
const actorRect = actors[message.from.actor];
const stackedSize = actorActivations(message.from.actor).length;
const stackedSize = actorActivations(message.from.actor).length || 0;
const x = actorRect.x + actorRect.width / 2 + ((stackedSize - 1) * conf.activationWidth) / 2;
this.activations.push({
startx: x,
Expand Down Expand Up @@ -248,13 +248,16 @@ const drawNote = function(elem, noteModel) {
* @param msgModel - the model containing fields describing a message
*/
const drawMessage = function(g, msgModel) {
bounds.bumpVerticalPos(conf.messageMargin);
msgModel.height += conf.messageMargin;
msgModel.starty = bounds.getVerticalPos();
const { startx, stopx, starty: verticalPos, message, type, sequenceIndex, wrap } = msgModel;
const { startx, stopx, starty, message, type, sequenceIndex, wrap } = msgModel;
const lines = message.split(common.lineBreakRegex).length;
let textDims = utils.calculateTextDimensions(message, conf);
const lineHeight = textDims.height / lines;
msgModel.height += lineHeight;

bounds.bumpVerticalPos(lineHeight);
const textObj = svgDraw.getTextObj();
textObj.x = startx;
textObj.y = verticalPos;
textObj.y = starty;
textObj.width = stopx - startx;
textObj.class = 'messageText';
textObj.dy = '1em';
Expand All @@ -268,18 +271,11 @@ const drawMessage = function(g, msgModel) {
textObj.tspan = false;
textObj.wrap = wrap;

let textElem = drawText(g, textObj);
const lineHeight = (textElem[0]._groups || textElem[0])[0][0].getBBox().height;
textElem.forEach(te => te.attr('y', verticalPos - 7 - lineHeight / 2));

const lines = message.split(common.lineBreakRegex).length - 1;
drawText(g, textObj);

let totalOffset = Math.round(lineHeight + lines * lineHeight);
let totalOffset = textDims.height;

let textWidth = Math.max.apply(
null,
textElem.map(te => (te._groups || te)[0][0].getBBox().width)
);
let textWidth = textDims.width;

let line;
if (startx === stopx) {
Expand All @@ -288,7 +284,8 @@ const drawMessage = function(g, msgModel) {
.append('path')
.attr(
'd',
`M ${startx},${verticalPos + totalOffset} H ${startx + conf.width / 2} V ${verticalPos +
`M ${startx},${bounds.getVerticalPos() + totalOffset} H ${startx +
Math.max(conf.width / 2, textWidth / 2)} V ${bounds.getVerticalPos() +
25 +
totalOffset} H ${startx}`
);
Expand All @@ -302,49 +299,43 @@ const drawMessage = function(g, msgModel) {
'M ' +
startx +
',' +
(verticalPos + totalOffset) +
(bounds.getVerticalPos() + totalOffset) +
' C ' +
(startx + 60) +
',' +
(verticalPos - 10 + totalOffset) +
(bounds.getVerticalPos() - 10 + totalOffset) +
' ' +
(startx + 60) +
',' +
(verticalPos + 30 + totalOffset) +
(bounds.getVerticalPos() + 30 + totalOffset) +
' ' +
startx +
',' +
(verticalPos + 20 + totalOffset)
(bounds.getVerticalPos() + 20 + totalOffset)
);
}

bounds.bumpVerticalPos(30);
msgModel.height += 30;
const dx = Math.max(textWidth / 2, 100);
totalOffset += 30;
const dx = Math.max(textWidth / 2, conf.width / 2);
bounds.insert(
startx - dx,
bounds.getVerticalPos() - 10 + totalOffset,
stopx + dx,
bounds.getVerticalPos() + 30 + totalOffset
);
bounds.bumpVerticalPos(10);
msgModel.height += 10;
} else {
totalOffset += conf.boxMargin;
line = g.append('line');
line.attr('x1', startx);
line.attr('y1', verticalPos + totalOffset);
line.attr('y1', bounds.getVerticalPos() + totalOffset);
line.attr('x2', stopx);
line.attr('y2', verticalPos + totalOffset);
bounds.bumpVerticalPos(10);
msgModel.height += 10;
line.attr('y2', bounds.getVerticalPos() + totalOffset);
bounds.insert(
startx,
bounds.getVerticalPos() - 10 + totalOffset,
stopx,
bounds.getVerticalPos() + totalOffset
);
msgModel.height += 10;
bounds.bumpVerticalPos(10);
}
// Make an SVG Container
// Draw the line
Expand Down Expand Up @@ -387,17 +378,18 @@ const drawMessage = function(g, msgModel) {
line.attr('marker-start', 'url(' + url + '#sequencenumber)');
g.append('text')
.attr('x', startx)
.attr('y', verticalPos + 4 + totalOffset)
.attr('y', bounds.getVerticalPos() + 4 + totalOffset)
.attr('font-family', 'sans-serif')
.attr('font-size', '12px')
.attr('text-anchor', 'middle')
.attr('textLength', '16px')
.attr('class', 'sequenceNumber')
.text(sequenceIndex);
}
bounds.bumpVerticalPos(totalOffset);
msgModel.height += totalOffset;
msgModel.stopy = msgModel.starty + msgModel.height;
bounds.insert(msgModel.fromBounds, msgModel.starty, msgModel.toBounds, msgModel.stopy);
logger.debug(`mm.h:${msgModel.height} vs c.h:${msgModel.stopy - msgModel.starty}`);
};

export const drawActors = function(diagram, actors, actorKeys, verticalPos) {
Expand Down Expand Up @@ -472,8 +464,11 @@ function adjustLoopHeightForWrap(loopWidths, msg, preMargin, postMargin, addLoop
msg.message = utils.wrapLabel(`[${msg.message}]`, loopWidth - 2 * conf.wrapPadding, textConf);
msg.width = loopWidth;

const textHeight = utils.calculateTextHeight(msg.message, textConf);
heightAdjust += textHeight;
// const lines = msg.message.split(common.lineBreakRegex).length;
const textDims = utils.calculateTextDimensions(msg.message, textConf);
const totalOffset = textDims.height - conf.labelBoxHeight;
heightAdjust = postMargin + totalOffset;
logger.debug(`${totalOffset} - ${msg.message}`);
}
addLoopFn(msg);
bounds.bumpVerticalPos(heightAdjust);
Expand All @@ -488,8 +483,9 @@ export const draw = function(text, id) {
parser.yy.clear();
parser.yy.setWrap(conf.wrap);
parser.parse(text + '\n');

bounds.init();
logger.debug(`C:${JSON.stringify(conf, null, 2)}`);

const diagram = select(`[id="${id}"]`);

// Fetch data from the parsing
Expand Down Expand Up @@ -553,7 +549,7 @@ export const draw = function(text, id) {
break;
case parser.yy.LINETYPE.LOOP_END:
loopModel = bounds.endLoop();
svgDraw.drawLoop(diagram, loopModel, 'loop', conf, bounds);
svgDraw.drawLoop(diagram, loopModel, 'loop', conf);
bounds.bumpVerticalPos(loopModel.stopy - bounds.getVerticalPos());
bounds.models.addLoop(loopModel);
break;
Expand Down Expand Up @@ -635,6 +631,7 @@ export const draw = function(text, id) {
try {
// lastMsg = msg
msgModel = msg.msgModel;
msgModel.starty = bounds.getVerticalPos();
msgModel.sequenceIndex = sequenceIndex;
drawMessage(diagram, msgModel);
bounds.models.addMessage(msgModel);
Expand Down Expand Up @@ -706,7 +703,7 @@ export const draw = function(text, id) {
' ' +
(height + extraVertForTitle)
);
logger.debug(`models: ${JSON.stringify(bounds.models, null, 2)}`);
logger.debug(`models:`, bounds.models);
};

/**
Expand Down Expand Up @@ -767,6 +764,11 @@ const getMaxMessageWidthPerActor = function(actors, messages) {
maxMessageWidthPerActor[msg.to] || 0,
messageWidth
);
} else if (isMessage && msg.from === actor.prevActor) {
maxMessageWidthPerActor[msg.from] = Math.max(
maxMessageWidthPerActor[msg.from] || 0,
messageWidth
);
} else if (msg.placement === parser.yy.PLACEMENT.RIGHTOF) {
maxMessageWidthPerActor[msg.from] = Math.max(
maxMessageWidthPerActor[msg.from] || 0,
Expand Down Expand Up @@ -861,7 +863,6 @@ const buildNoteModel = function(msg, actors) {
shouldWrap ? utils.wrapLabel(msg.message, conf.width, conf.noteFont()) : msg.message,
conf.noteFont()
);
logger.debug(`TD:[${textDimensions.width},${textDimensions.height}]`);
let noteModel = {
width: shouldWrap
? conf.width
Expand Down Expand Up @@ -892,14 +893,18 @@ const buildNoteModel = function(msg, actors) {
} else if (msg.to === msg.from) {
textDimensions = utils.calculateTextDimensions(
shouldWrap
? utils.wrapLabel(msg.message, Math.max(conf.width, actors[msg.to].width), conf.noteFont())
? utils.wrapLabel(
msg.message,
Math.max(conf.width, actors[msg.from].width),
conf.noteFont()
)
: msg.message,
conf.noteFont()
);
noteModel.width = shouldWrap
? Math.max(conf.width, actors[msg.to].width)
: Math.max(actors[msg.to].width, conf.width, textDimensions.width + 2 * conf.noteMargin);
noteModel.startx = startx + (actors[msg.to].width - noteModel.width) / 2;
? Math.max(conf.width, actors[msg.from].width)
: Math.max(actors[msg.from].width, conf.width, textDimensions.width + 2 * conf.noteMargin);
noteModel.startx = startx + (actors[msg.from].width - noteModel.width) / 2;
} else {
noteModel.width =
Math.abs(startx + actors[msg.from].width / 2 - (stopx + actors[msg.to].width / 2)) +
Expand All @@ -916,6 +921,9 @@ const buildNoteModel = function(msg, actors) {
conf.noteFont()
);
}
logger.debug(
`NM:[${noteModel.startx},${noteModel.stopx},${noteModel.starty},${noteModel.stopy}:${noteModel.width},${noteModel.height}=${msg.message}]`
);
return noteModel;
};

Expand Down Expand Up @@ -957,7 +965,7 @@ const buildMessageModel = function(msg, actors) {
if (msg.wrap && msg.message && !common.lineBreakRegex.test(msg.message)) {
msgModel.message = utils.wrapLabel(
msg.message,
Math.max(msgModel.width, conf.width),
Math.max(msgModel.width - 2 * conf.wrapPadding, conf.width),
conf.messageFont()
);
}
Expand Down Expand Up @@ -1028,44 +1036,31 @@ const calculateLoopBounds = function(messages, actors) {
if (isNote) {
noteModel = buildNoteModel(msg, actors);
msg.noteModel = noteModel;
let depth = 0;
stack.forEach(stk => {
current = stk;
current.from = Math.min(current.from, noteModel.startx);
current.to = Math.max(current.to, noteModel.startx + noteModel.width);
current.width =
Math.max(current.width, Math.abs(current.from - current.to)) -
50 -
conf.boxMargin * depth;
depth++;
Math.max(current.width, Math.abs(current.from - current.to)) - conf.labelBoxWidth;
});
} else {
msgModel = buildMessageModel(msg, actors);
msg.msgModel = msgModel;
if (msg.from && msg.to && stack.length > 0) {
let depth = 0;
if (msgModel.startx && msgModel.stopx && stack.length > 0) {
stack.forEach(stk => {
current = stk;
let from = actors[msg.from];
let to = actors[msg.to];
if (from.x === to.x) {
current.from = Math.min(current.from, from.x);
current.to = Math.max(current.to, to.x);
current.width = Math.max(current.width, from.width) - 50 - conf.boxMargin * depth;
} else {
if (from.x < to.x) {
current.from = Math.min(current.from, from.x);
current.to = Math.max(current.to, to.x);
} else {
current.from = Math.min(current.from, to.x);
current.to = Math.max(current.to, from.x);
}
if (msgModel.startx === msgModel.stopx) {
let from = actors[msg.from];
let to = actors[msg.to];
current.from = Math.min(from.x - from.width / 2, current.from);
current.to = Math.max(to.x + from.width / 2, current.to);
current.width =
Math.max(current.width, Math.abs(current.from - current.to)) -
50 -
conf.boxMargin * depth;
Math.max(current.width, Math.abs(current.to - current.from)) - conf.labelBoxWidth;
} else {
current.from = Math.min(msgModel.startx, current.from);
current.to = Math.max(msgModel.stopx, current.to);
current.width = Math.max(current.width, msgModel.width) - conf.labelBoxWidth;
}
depth++;
});
}
}
Expand Down
Loading