Skip to content

Commit 745f604

Browse files
authored
Merge pull request #5 from axifive/master
Fix line break bug, aka 'Cannot read property 'color' of undefined' (#4)
2 parents 9b492e8 + 521a01c commit 745f604

File tree

1 file changed

+43
-26
lines changed

1 file changed

+43
-26
lines changed

gitgraph.js

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,20 @@ var gitGraph = function (canvas, rawGraphList, config) {
125125
!(row[i - 2] && row[i] === "_" && row[i - 2] === "|")) {}
126126

127127
return i;
128-
}
128+
};
129+
130+
var findLineBreak = function (row) {
131+
if (!row) {
132+
return -1
133+
}
134+
135+
var i = row.length;
136+
137+
while (i-- &&
138+
!(row[i - 1] && row[i - 2] && row[i] === " " && row[i - 1] === "|" && row[i - 2] === "_")) {}
139+
140+
return i;
141+
};
129142

130143
var genNewFlow = function () {
131144
var newId;
@@ -137,21 +150,21 @@ var gitGraph = function (canvas, rawGraphList, config) {
137150
return {id:newId, color:"#" + newId};
138151
};
139152

140-
//draw method
141-
var drawLineRight = function (x, y, color) {
153+
//Draw methods
154+
var drawLine = function (moveX, moveY, lineX, lineY, color) {
142155
ctx.strokeStyle = color;
143156
ctx.beginPath();
144-
ctx.moveTo(x, y + config.unitSize / 2);
145-
ctx.lineTo(x + config.unitSize, y + config.unitSize / 2);
157+
ctx.moveTo(moveX, moveY);
158+
ctx.lineTo(lineX, lineY);
146159
ctx.stroke();
147160
};
148161

162+
var drawLineRight = function (x, y, color) {
163+
drawLine(x, y + config.unitSize / 2, x + config.unitSize, y + config.unitSize / 2, color);
164+
};
165+
149166
var drawLineUp = function (x, y, color) {
150-
ctx.strokeStyle = color;
151-
ctx.beginPath();
152-
ctx.moveTo(x, y + config.unitSize / 2);
153-
ctx.lineTo(x, y - config.unitSize / 2);
154-
ctx.stroke();
167+
drawLine(x, y + config.unitSize / 2, x, y - config.unitSize / 2, color);
155168
};
156169

157170
var drawNode = function (x, y, color) {
@@ -165,37 +178,28 @@ var gitGraph = function (canvas, rawGraphList, config) {
165178
};
166179

167180
var drawLineIn = function (x, y, color) {
168-
ctx.strokeStyle = color;
169-
170-
ctx.beginPath();
171-
ctx.moveTo(x + config.unitSize, y + config.unitSize / 2);
172-
ctx.lineTo(x, y - config.unitSize / 2);
173-
ctx.stroke();
181+
drawLine(x + config.unitSize, y + config.unitSize / 2, x, y - config.unitSize / 2, color);
174182
};
175183

176184
var drawLineOut = function (x, y, color) {
177-
ctx.strokeStyle = color;
178-
ctx.beginPath();
179-
ctx.moveTo(x, y + config.unitSize / 2);
180-
ctx.lineTo(x + config.unitSize, y - config.unitSize / 2);
181-
ctx.stroke();
185+
drawLine(x, y + config.unitSize / 2, x + config.unitSize, y - config.unitSize / 2, color);
182186
};
183187

184188
var draw = function (graphList) {
185-
var colomn, colomnIndex, prevColomn, condenseIndex;
189+
var colomn, colomnIndex, prevColomn, condenseIndex, breakIndex = -1;
186190
var x, y;
187191
var color;
188-
var nodePos, outPos;
192+
var nodePos;
189193
var tempFlow;
190194
var prevRowLength = 0;
191195
var flowSwapPos = -1;
192196
var lastLinePos;
193-
var i, k, l;
197+
var i, l;
194198
var condenseCurrentLength, condensePrevLength = 0, condenseNextLength = 0;
195199

196200
var inlineIntersect = false;
197201

198-
//initiate for first row
202+
//initiate color array for first row
199203
for (i = 0, l = graphList[0].length; i < l; i++) {
200204
if (graphList[0][i] !== "_" && graphList[0][i] !== " ") {
201205
flows.push(genNewFlow());
@@ -274,13 +278,26 @@ var gitGraph = function (canvas, rawGraphList, config) {
274278
colomnIndex = 0; //reset index
275279
condenseIndex = 0;
276280
condensePrevLength = 0;
281+
breakIndex = -1; //reset break index
277282
while (colomnIndex < currentRow.length) {
278283
colomn = currentRow[colomnIndex];
279284

280285
if (colomn !== " " && colomn !== "_") {
281286
++condensePrevLength;
282287
}
283288

289+
//check and fix line break in next row
290+
if (colomn === "/" && currentRow[colomnIndex - 1] && currentRow[colomnIndex - 1] === "|") {
291+
if ((breakIndex = findLineBreak(nextRow)) !== -1) {
292+
nextRow.splice(breakIndex, 1);
293+
}
294+
}
295+
//if line break found replace all '/' with '|' after breakIndex in previous row
296+
if (breakIndex !== - 1 && colomn === "/" && colomnIndex > breakIndex) {
297+
currentRow[colomnIndex] = "|";
298+
colomn = "|";
299+
}
300+
284301
if (colomn === " " &&
285302
currentRow[colomnIndex + 1] &&
286303
currentRow[colomnIndex + 1] === "_" &&
@@ -293,7 +310,7 @@ var gitGraph = function (canvas, rawGraphList, config) {
293310
colomn = "/";
294311
}
295312

296-
//create new flow only when no intersetc happened
313+
//create new flow only when no intersect happened
297314
if (flowSwapPos === -1 &&
298315
colomn === "/" &&
299316
currentRow[colomnIndex - 1] &&

0 commit comments

Comments
 (0)