Skip to content

Commit b3f4ad7

Browse files
committed
Prototype multiple elements on the same line
When a note or signal is indented with '=' it is shown on the same height as the latest note or signal without indentation. This is just a PoC implementation of bramp#130. The formatting is mucked up and the logic is a bit hard to read. If we decide this is a useful feature we probably want to make 'lines' (collections of notes/signals that are on the same line) a first-class citizen.
1 parent ece3df8 commit b3f4ad7

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

src/diagram.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -59,24 +59,28 @@
5959
this.index = index;
6060
};
6161

62-
Diagram.Signal = function(actorA, signaltype, actorB, message) {
62+
Diagram.Signal = function(actorA, signaltype, actorB, message, indent) {
6363
this.type = "Signal";
6464
this.actorA = actorA;
6565
this.actorB = actorB;
6666
this.linetype = signaltype & 3;
6767
this.arrowtype = (signaltype >> 2) & 3;
6868
this.message = message;
69+
if (indent) this.offset = 0;
70+
else this.offset = 1;
6971
};
7072

7173
Diagram.Signal.prototype.isSelf = function() {
7274
return this.actorA.index == this.actorB.index;
7375
};
7476

75-
Diagram.Note = function(actor, placement, message) {
77+
Diagram.Note = function(actor, placement, message, indent) {
7678
this.type = "Note";
7779
this.actor = actor;
7880
this.placement = placement;
7981
this.message = message;
82+
if (indent) this.offset = 0;
83+
else this.offset = 1;
8084

8185
if (this.hasManyActors() && actor[0] == actor[1]) {
8286
throw new Error("Note should be over two different actors");
@@ -158,6 +162,3 @@
158162
delete diagram.parseError;
159163
return diagram;
160164
};
161-
162-
163-

src/grammar.jison

+10-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"note" return 'note';
2424
"title" return 'title';
2525
"," return ',';
26+
"=" return 'INDENT';
2627
[^\->:,\r\n]+ return 'ACTOR';
2728
"--" return 'DOTLINE';
2829
"-" return 'LINE';
@@ -60,8 +61,8 @@ statement
6061
;
6162

6263
note_statement
63-
: 'note' placement actor message { $$ = new Diagram.Note($3, $2, $4); }
64-
| 'note' 'over' actor_pair message { $$ = new Diagram.Note($3, Diagram.PLACEMENT.OVER, $4); }
64+
: indent 'note' placement actor message { $$ = new Diagram.Note($4, $3, $5, $1); }
65+
| indent 'note' 'over' actor_pair message { $$ = new Diagram.Note($4, Diagram.PLACEMENT.OVER, $5, $1); }
6566
;
6667

6768
actor_pair
@@ -75,10 +76,15 @@ placement
7576
;
7677

7778
signal
78-
: actor signaltype actor message
79-
{ $$ = new Diagram.Signal($1, $2, $3, $4); }
79+
: indent actor signaltype actor message
80+
{ $$ = new Diagram.Signal($2, $3, $4, $5, $1); }
8081
;
8182

83+
indent
84+
: /* empty */ { $$ = false }
85+
| 'INDENT' { $$ = true }
86+
;
87+
8288
actor
8389
: ACTOR { $$ = yy.parser.yy.getActor(Diagram.unescape($1)); }
8490
;

src/sequence-diagram.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@
230230

231231
this.draw_title();
232232
this.draw_actors(y);
233-
this.draw_signals(y + this._actors_height);
233+
this.draw_signals(y, this._actors_height);
234234

235235
this._paper.setFinish();
236236
},
@@ -293,6 +293,7 @@
293293
}
294294
}
295295

296+
var currentLineHeight = 0;
296297
_.each(signals, function(s) {
297298
var a, b; // Indexes of the left and right actors involved
298299

@@ -354,8 +355,14 @@
354355
}
355356

356357
actor_ensure_distance(a, b, s.width + extra_width);
357-
this._signals_height += s.height;
358+
359+
currentLineHeight = Math.max(currentLineHeight, s.height);
360+
if (s.offset == 1) {
361+
this._signals_height += currentLineHeight;
362+
currentLineHeight = 0;
363+
}
358364
}, this);
365+
this._signals_height += currentLineHeight;
359366

360367
// Re-jig the positions
361368
var actors_x = 0;
@@ -416,9 +423,16 @@
416423
this.draw_text_box(actor, actor.name, ACTOR_MARGIN, ACTOR_PADDING, this._font);
417424
},
418425

419-
draw_signals : function (offsetY) {
426+
draw_signals : function (offsetY, currentLineHeight) {
420427
var y = offsetY;
421428
_.each(this.diagram.signals, function(s) {
429+
if (s.offset == 1) {
430+
y += currentLineHeight;
431+
currentLineHeight = s.height;
432+
} else {
433+
currentLineHeight = Math.max(currentLineHeight, s.height);
434+
}
435+
422436
if (s.type == "Signal") {
423437
if (s.isSelf()) {
424438
this.draw_self_signal(s, y);
@@ -430,7 +444,6 @@
430444
this.draw_note(s, y);
431445
}
432446

433-
y += s.height;
434447
}, this);
435448
},
436449

@@ -637,4 +650,3 @@
637650
drawing.draw(container);
638651

639652
}; // end of drawSVG
640-

0 commit comments

Comments
 (0)