Skip to content

Commit 5cd8f76

Browse files
ziluvatarbramp
authored andcommitted
fix alignment for longer multi line message for arrow signals
1 parent 0b252fa commit 5cd8f76

File tree

4 files changed

+35
-22
lines changed

4 files changed

+35
-22
lines changed

Diff for: src/theme-raphael.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ if (typeof Raphael != 'undefined') {
127127
* x,y (int) x,y top left point of the text, or the center of the text (depending on align param)
128128
* text (string) text to print
129129
* font (Object)
130-
* align (string) ALIGN_LEFT or ALIGN_CENTER
130+
* align (string) ALIGN_LEFT, ALIGN_CENTER, ALIGN_HORIZONTAL_CENTER or ALIGN_VERTICAL_CENTER
131131
*/
132132
drawText: function(x, y, text, font, align) {
133133
text = this.cleanText(text);
@@ -137,8 +137,10 @@ if (typeof Raphael != 'undefined') {
137137
var paper = this.paper_;
138138
var bb = this.textBBox(text, font);
139139

140-
if (align == ALIGN_CENTER) {
140+
if (align == ALIGN_CENTER || align == ALIGN_HORIZONTAL_CENTER) {
141141
x = x - bb.width / 2;
142+
}
143+
if (align == ALIGN_CENTER || align == ALIGN_VERTICAL_CENTER) {
142144
y = y - bb.height / 2;
143145
}
144146

Diff for: src/theme-snap.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -199,14 +199,16 @@ if (typeof Snap != 'undefined') {
199199
* x,y (int) x,y top left point of the text, or the center of the text (depending on align param)
200200
* text (string) text to print
201201
* font (Object)
202-
* align (string) ALIGN_LEFT or ALIGN_CENTER
202+
* align (string) ALIGN_LEFT, ALIGN_CENTER, ALIGN_HORIZONTAL_CENTER or ALIGN_VERTICAL_CENTER
203203
*/
204204
drawText: function(x, y, text, font, align) {
205205
var t = this.createText(text, font);
206206
var bb = t.getBBox();
207207

208-
if (align == ALIGN_CENTER) {
208+
if (align == ALIGN_CENTER || align == ALIGN_HORIZONTAL_CENTER) {
209209
x = x - bb.width / 2;
210+
}
211+
if (align == ALIGN_CENTER || align == ALIGN_VERTICAL_CENTER) {
210212
y = y - bb.height / 2;
211213
}
212214

Diff for: src/theme.js

+24-16
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ var ARROWTYPE = Diagram.ARROWTYPE;
4040

4141
var ALIGN_LEFT = 0;
4242
var ALIGN_CENTER = 1;
43+
var ALIGN_HORIZONTAL_CENTER = 2;
44+
var ALIGN_VERTICAL_CENTER = 3;
4345

4446
function AssertException(message) { this.message = message; }
4547
AssertException.prototype.toString = function() {
@@ -378,38 +380,44 @@ _.extend(BaseTheme.prototype, {
378380
},
379381

380382
drawSelfSignal: function(signal, offsetY) {
381-
assert(signal.isSelf(), 'signal must be a self signal');
383+
assert(signal.isSelf(), 'signal must be a self signal');
382384

383-
var textBB = signal.textBB;
384-
var aX = getCenterX(signal.actorA);
385+
var textBB = signal.textBB;
386+
var aX = getCenterX(signal.actorA);
385387

386-
var x = aX + SELF_SIGNAL_WIDTH + SIGNAL_PADDING;
387-
var y = offsetY + SIGNAL_PADDING + signal.height / 2 + textBB.y;
388+
var y1 = offsetY + SIGNAL_MARGIN + SIGNAL_PADDING;
389+
var y2 = y1 + signal.height - 2 * SIGNAL_MARGIN - SIGNAL_PADDING;
388390

389-
this.drawText(x, y, signal.message, this.font_, ALIGN_LEFT);
391+
// Draw three lines, the last one with a arrow
392+
this.drawLine(aX, y1, aX + SELF_SIGNAL_WIDTH, y1, signal.linetype);
393+
this.drawLine(aX + SELF_SIGNAL_WIDTH, y1, aX + SELF_SIGNAL_WIDTH, y2, signal.linetype);
394+
this.drawLine(aX + SELF_SIGNAL_WIDTH, y2, aX, y2, signal.linetype, signal.arrowtype);
390395

391-
var y1 = offsetY + SIGNAL_MARGIN + SIGNAL_PADDING;
392-
var y2 = y1 + signal.height - 2 * SIGNAL_MARGIN - SIGNAL_PADDING;
396+
// Draw text
397+
var x = aX + SELF_SIGNAL_WIDTH + SIGNAL_PADDING;
398+
var arrowHeight = (y2 - y1);
399+
var emptyVerticalSpace = arrowHeight - textBB.height;
400+
var topPadding = emptyVerticalSpace / 2;
401+
var y = y1 + topPadding;
393402

394-
// Draw three lines, the last one with a arrow
395-
this.drawLine(aX, y1, aX + SELF_SIGNAL_WIDTH, y1, signal.linetype);
396-
this.drawLine(aX + SELF_SIGNAL_WIDTH, y1, aX + SELF_SIGNAL_WIDTH, y2, signal.linetype);
397-
this.drawLine(aX + SELF_SIGNAL_WIDTH, y2, aX, y2, signal.linetype, signal.arrowtype);
398-
},
403+
this.drawText(x, y, signal.message, this.font_, ALIGN_LEFT);
404+
},
399405

400406
drawSignal: function(signal, offsetY) {
401407
var aX = getCenterX(signal.actorA);
402408
var bX = getCenterX(signal.actorB);
403409

404410
// Mid point between actors
405411
var x = (bX - aX) / 2 + aX;
406-
var y = offsetY + SIGNAL_MARGIN + 2 * SIGNAL_PADDING;
412+
var y = offsetY + SIGNAL_MARGIN + SIGNAL_PADDING;
407413

408414
// Draw the text in the middle of the signal
409-
this.drawText(x, y, signal.message, this.font_, ALIGN_CENTER);
415+
this.drawText(x, y, signal.message, this.font_, ALIGN_HORIZONTAL_CENTER);
410416

411417
// Draw the line along the bottom of the signal
412-
y = offsetY + signal.height - SIGNAL_MARGIN - SIGNAL_PADDING;
418+
// Padding above, between message and line
419+
// Margin below the line, between line and next signal
420+
y = offsetY + signal.height - SIGNAL_PADDING;
413421
this.drawLine(aX, y, bX, y, signal.linetype, signal.arrowtype);
414422
},
415423

Diff for: test/gallery.html

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@
3535
B->C:
3636
C->C:
3737
A->B: Normal line
38+
A->B: Normal line\nwith\nmany\nlines
3839
B-->C: Dashed line
3940
C->>D: Open arrow
4041
D-->>A: Dashed open arrow
4142
D->D: Self
42-
D->D: Many\nLines
43+
D->D: Self\nMany\nLines
4344
</textarea>
4445
<textarea class="language" rows="10" cols="35">
4546
# Example of a comment.
@@ -94,7 +95,7 @@
9495
});
9596
table.append(row);
9697

97-
$('textarea.language').each(function(i){
98+
$('textarea.language').each(function(i){
9899
var textarea = $(this);
99100
var row = $("<tr>");
100101
var td = $("<td>");

0 commit comments

Comments
 (0)