You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gitgraph.js 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. // Although inspired by the https://github.com/bluef/gitgraph.js/blob/master/gitgraph.js
  2. // this has been completely rewritten with almost no remaining code
  3. // GitGraphCanvas is a canvas for drawing gitgraphs on to
  4. class GitGraphCanvas {
  5. constructor(canvas, widthUnits, heightUnits, config) {
  6. this.ctx = canvas.getContext('2d');
  7. const width = widthUnits * config.unitSize;
  8. this.height = heightUnits * config.unitSize;
  9. const ratio = window.devicePixelRatio || 1;
  10. canvas.width = width * ratio;
  11. canvas.height = this.height * ratio;
  12. canvas.style.width = `${width}px`;
  13. canvas.style.height = `${this.height}px`;
  14. this.ctx.lineWidth = config.lineWidth;
  15. this.ctx.lineJoin = 'round';
  16. this.ctx.lineCap = 'round';
  17. this.ctx.scale(ratio, ratio);
  18. this.config = config;
  19. }
  20. drawLine(moveX, moveY, lineX, lineY, color) {
  21. this.ctx.strokeStyle = color;
  22. this.ctx.beginPath();
  23. this.ctx.moveTo(moveX, moveY);
  24. this.ctx.lineTo(lineX, lineY);
  25. this.ctx.stroke();
  26. }
  27. drawLineRight(x, y, color) {
  28. this.drawLine(
  29. x - 0.5 * this.config.unitSize,
  30. y + this.config.unitSize / 2,
  31. x + 0.5 * this.config.unitSize,
  32. y + this.config.unitSize / 2,
  33. color
  34. );
  35. }
  36. drawLineUp(x, y, color) {
  37. this.drawLine(
  38. x,
  39. y + this.config.unitSize / 2,
  40. x,
  41. y - this.config.unitSize / 2,
  42. color
  43. );
  44. }
  45. drawNode(x, y, color) {
  46. this.ctx.strokeStyle = color;
  47. this.drawLineUp(x, y, color);
  48. this.ctx.beginPath();
  49. this.ctx.arc(x, y, this.config.nodeRadius, 0, Math.PI * 2, true);
  50. this.ctx.fillStyle = color;
  51. this.ctx.fill();
  52. }
  53. drawLineIn(x, y, color) {
  54. this.drawLine(
  55. x + 0.5 * this.config.unitSize,
  56. y + this.config.unitSize / 2,
  57. x - 0.5 * this.config.unitSize,
  58. y - this.config.unitSize / 2,
  59. color
  60. );
  61. }
  62. drawLineOut(x, y, color) {
  63. this.drawLine(
  64. x - 0.5 * this.config.unitSize,
  65. y + this.config.unitSize / 2,
  66. x + 0.5 * this.config.unitSize,
  67. y - this.config.unitSize / 2,
  68. color
  69. );
  70. }
  71. drawSymbol(symbol, columnNumber, rowNumber, color) {
  72. const y = this.height - this.config.unitSize * (rowNumber + 0.5);
  73. const x = this.config.unitSize * 0.5 * (columnNumber + 1);
  74. switch (symbol) {
  75. case '-':
  76. if (columnNumber % 2 === 1) {
  77. this.drawLineRight(x, y, color);
  78. }
  79. break;
  80. case '_':
  81. this.drawLineRight(x, y, color);
  82. break;
  83. case '*':
  84. this.drawNode(x, y, color);
  85. break;
  86. case '|':
  87. this.drawLineUp(x, y, color);
  88. break;
  89. case '/':
  90. this.drawLineOut(x, y, color);
  91. break;
  92. case '\\':
  93. this.drawLineIn(x, y, color);
  94. break;
  95. case '.':
  96. case ' ':
  97. break;
  98. default:
  99. console.error('Unknown symbol', symbol, color);
  100. }
  101. }
  102. }
  103. class GitGraph {
  104. constructor(canvas, rawRows, config) {
  105. this.rows = [];
  106. let maxWidth = 0;
  107. for (let i = 0; i < rawRows.length; i++) {
  108. const rowStr = rawRows[i];
  109. maxWidth = Math.max(rowStr.replace(/([_\s.-])/g, '').length, maxWidth);
  110. const rowArray = rowStr.split('');
  111. this.rows.unshift(rowArray);
  112. }
  113. this.currentFlows = [];
  114. this.previousFlows = [];
  115. this.gitGraphCanvas = new GitGraphCanvas(
  116. canvas,
  117. maxWidth,
  118. this.rows.length,
  119. config
  120. );
  121. }
  122. generateNewFlow(column) {
  123. let newId;
  124. do {
  125. newId = generateRandomColorString();
  126. } while (this.hasFlow(newId, column));
  127. return {id: newId, color: `#${newId}`};
  128. }
  129. hasFlow(id, column) {
  130. // We want to find the flow with the current ID
  131. // Possible flows are those in the currentFlows
  132. // Or flows in previousFlows[column-2:...]
  133. for (
  134. let idx = column - 2 < 0 ? 0 : column - 2;
  135. idx < this.previousFlows.length;
  136. idx++
  137. ) {
  138. if (this.previousFlows[idx] && this.previousFlows[idx].id === id) {
  139. return true;
  140. }
  141. }
  142. for (let idx = 0; idx < this.currentFlows.length; idx++) {
  143. if (this.currentFlows[idx] && this.currentFlows[idx].id === id) {
  144. return true;
  145. }
  146. }
  147. return false;
  148. }
  149. takePreviousFlow(column) {
  150. if (column < this.previousFlows.length && this.previousFlows[column]) {
  151. const flow = this.previousFlows[column];
  152. this.previousFlows[column] = null;
  153. return flow;
  154. }
  155. return this.generateNewFlow(column);
  156. }
  157. draw() {
  158. if (this.rows.length === 0) {
  159. return;
  160. }
  161. this.currentFlows = new Array(this.rows[0].length);
  162. // Generate flows for the first row - I do not believe that this can contain '_', '-', '.'
  163. for (let column = 0; column < this.rows[0].length; column++) {
  164. if (this.rows[0][column] === ' ') {
  165. continue;
  166. }
  167. this.currentFlows[column] = this.generateNewFlow(column);
  168. }
  169. // Draw the first row
  170. for (let column = 0; column < this.rows[0].length; column++) {
  171. const symbol = this.rows[0][column];
  172. const color = this.currentFlows[column] ? this.currentFlows[column].color : '';
  173. this.gitGraphCanvas.drawSymbol(symbol, column, 0, color);
  174. }
  175. for (let row = 1; row < this.rows.length; row++) {
  176. // Done previous row - step up the row
  177. const currentRow = this.rows[row];
  178. const previousRow = this.rows[row - 1];
  179. this.previousFlows = this.currentFlows;
  180. this.currentFlows = new Array(currentRow.length);
  181. // Set flows for this row
  182. for (let column = 0; column < currentRow.length; column++) {
  183. column = this.setFlowFor(column, currentRow, previousRow);
  184. }
  185. // Draw this row
  186. for (let column = 0; column < currentRow.length; column++) {
  187. const symbol = currentRow[column];
  188. const color = this.currentFlows[column] ? this.currentFlows[column].color : '';
  189. this.gitGraphCanvas.drawSymbol(symbol, column, row, color);
  190. }
  191. }
  192. }
  193. setFlowFor(column, currentRow, previousRow) {
  194. const symbol = currentRow[column];
  195. switch (symbol) {
  196. case '|':
  197. case '*':
  198. return this.setUpFlow(column, currentRow, previousRow);
  199. case '/':
  200. return this.setOutFlow(column, currentRow, previousRow);
  201. case '\\':
  202. return this.setInFlow(column, currentRow, previousRow);
  203. case '_':
  204. return this.setRightFlow(column, currentRow, previousRow);
  205. case '-':
  206. return this.setLeftFlow(column, currentRow, previousRow);
  207. case ' ':
  208. // In space no one can hear you flow ... (?)
  209. return column;
  210. default:
  211. // Unexpected so let's generate a new flow and wait for bug-reports
  212. this.currentFlows[column] = this.generateNewFlow(column);
  213. return column;
  214. }
  215. }
  216. // setUpFlow handles '|' or '*' - returns the last column that was set
  217. // generally we prefer to take the left most flow from the previous row
  218. setUpFlow(column, currentRow, previousRow) {
  219. // If ' |/' or ' |_'
  220. // '/|' '/|' -> Take the '|' flow directly beneath us
  221. if (
  222. column + 1 < currentRow.length &&
  223. (currentRow[column + 1] === '/' || currentRow[column + 1] === '_') &&
  224. column < previousRow.length &&
  225. (previousRow[column] === '|' || previousRow[column] === '*') &&
  226. previousRow[column - 1] === '/'
  227. ) {
  228. this.currentFlows[column] = this.takePreviousFlow(column);
  229. return column;
  230. }
  231. // If ' |/' or ' |_'
  232. // '/ ' '/ ' -> Take the '/' flow from the preceding column
  233. if (
  234. column + 1 < currentRow.length &&
  235. (currentRow[column + 1] === '/' || currentRow[column + 1] === '_') &&
  236. column - 1 < previousRow.length &&
  237. previousRow[column - 1] === '/'
  238. ) {
  239. this.currentFlows[column] = this.takePreviousFlow(column - 1);
  240. return column;
  241. }
  242. // If ' |'
  243. // '/' -> Take the '/' flow - (we always prefer the left-most flow)
  244. if (
  245. column > 0 &&
  246. column - 1 < previousRow.length &&
  247. previousRow[column - 1] === '/'
  248. ) {
  249. this.currentFlows[column] = this.takePreviousFlow(column - 1);
  250. return column;
  251. }
  252. // If '|' OR '|' take the '|' flow
  253. // '|' '*'
  254. if (
  255. column < previousRow.length &&
  256. (previousRow[column] === '|' || previousRow[column] === '*')
  257. ) {
  258. this.currentFlows[column] = this.takePreviousFlow(column);
  259. return column;
  260. }
  261. // If '| ' keep the '\' flow
  262. // ' \'
  263. if (column + 1 < previousRow.length && previousRow[column + 1] === '\\') {
  264. this.currentFlows[column] = this.takePreviousFlow(column + 1);
  265. return column;
  266. }
  267. // Otherwise just create a new flow - probably this is an error...
  268. this.currentFlows[column] = this.generateNewFlow(column);
  269. return column;
  270. }
  271. // setOutFlow handles '/' - returns the last column that was set
  272. // generally we prefer to take the left most flow from the previous row
  273. setOutFlow(column, currentRow, previousRow) {
  274. // If '_/' -> keep the '_' flow
  275. if (column > 0 && currentRow[column - 1] === '_') {
  276. this.currentFlows[column] = this.currentFlows[column - 1];
  277. return column;
  278. }
  279. // If '_|/' -> keep the '_' flow
  280. if (
  281. column > 1 &&
  282. (currentRow[column - 1] === '|' || currentRow[column - 1] === '*') &&
  283. currentRow[column - 2] === '_'
  284. ) {
  285. this.currentFlows[column] = this.currentFlows[column - 2];
  286. return column;
  287. }
  288. // If '|/'
  289. // '/' -> take the '/' flow (if it is still available)
  290. if (
  291. column > 1 &&
  292. currentRow[column - 1] === '|' &&
  293. column - 2 < previousRow.length &&
  294. previousRow[column - 2] === '/'
  295. ) {
  296. this.currentFlows[column] = this.takePreviousFlow(column - 2);
  297. return column;
  298. }
  299. // If ' /'
  300. // '/' -> take the '/' flow, but transform the symbol to '|' due to our spacing
  301. // This should only happen if there are 3 '/' - in a row so we don't need to be cleverer here
  302. if (
  303. column > 0 &&
  304. currentRow[column - 1] === ' ' &&
  305. column - 1 < previousRow.length &&
  306. previousRow[column - 1] === '/'
  307. ) {
  308. this.currentFlows[column] = this.takePreviousFlow(column - 1);
  309. currentRow[column] = '|';
  310. return column;
  311. }
  312. // If ' /'
  313. // '|' -> take the '|' flow
  314. if (
  315. column > 0 &&
  316. currentRow[column - 1] === ' ' &&
  317. column - 1 < previousRow.length &&
  318. (previousRow[column - 1] === '|' || previousRow[column - 1] === '*')
  319. ) {
  320. this.currentFlows[column] = this.takePreviousFlow(column - 1);
  321. return column;
  322. }
  323. // If '/' <- Not sure this ever happens... but take the '\' flow
  324. // '\'
  325. if (column < previousRow.length && previousRow[column] === '\\') {
  326. this.currentFlows[column] = this.takePreviousFlow(column);
  327. return column;
  328. }
  329. // Otherwise just generate a new flow and wait for bug-reports...
  330. this.currentFlows[column] = this.generateNewFlow(column);
  331. return column;
  332. }
  333. // setInFlow handles '\' - returns the last column that was set
  334. // generally we prefer to take the left most flow from the previous row
  335. setInFlow(column, currentRow, previousRow) {
  336. // If '\?'
  337. // '/?' -> take the '/' flow
  338. if (column < previousRow.length && previousRow[column] === '/') {
  339. this.currentFlows[column] = this.takePreviousFlow(column);
  340. return column;
  341. }
  342. // If '\?'
  343. // ' \' -> take the '\' flow and reassign to '|'
  344. // This should only happen if there are 3 '\' - in a row so we don't need to be cleverer here
  345. if (column + 1 < previousRow.length && previousRow[column + 1] === '\\') {
  346. this.currentFlows[column] = this.takePreviousFlow(column + 1);
  347. currentRow[column] = '|';
  348. return column;
  349. }
  350. // If '\?'
  351. // ' |' -> take the '|' flow
  352. if (
  353. column + 1 < previousRow.length &&
  354. (previousRow[column + 1] === '|' || previousRow[column + 1] === '*')
  355. ) {
  356. this.currentFlows[column] = this.takePreviousFlow(column + 1);
  357. return column;
  358. }
  359. // Otherwise just generate a new flow and wait for bug-reports if we're wrong...
  360. this.currentFlows[column] = this.generateNewFlow(column);
  361. return column;
  362. }
  363. // setRightFlow handles '_' - returns the last column that was set
  364. // generally we prefer to take the left most flow from the previous row
  365. setRightFlow(column, currentRow, previousRow) {
  366. // if '__' keep the '_' flow
  367. if (column > 0 && currentRow[column - 1] === '_') {
  368. this.currentFlows[column] = this.currentFlows[column - 1];
  369. return column;
  370. }
  371. // if '_|_' -> keep the '_' flow
  372. if (
  373. column > 1 &&
  374. currentRow[column - 1] === '|' &&
  375. currentRow[column - 2] === '_'
  376. ) {
  377. this.currentFlows[column] = this.currentFlows[column - 2];
  378. return column;
  379. }
  380. // if ' _' -> take the '/' flow
  381. // '/ '
  382. if (
  383. column > 0 &&
  384. column - 1 < previousRow.length &&
  385. previousRow[column - 1] === '/'
  386. ) {
  387. this.currentFlows[column] = this.takePreviousFlow(column - 1);
  388. return column;
  389. }
  390. // if ' |_'
  391. // '/? ' -> take the '/' flow (this may cause generation...)
  392. // we can do this because we know that git graph
  393. // doesn't create compact graphs like: ' |_'
  394. // '//'
  395. if (
  396. column > 1 &&
  397. column - 2 < previousRow.length &&
  398. previousRow[column - 2] === '/'
  399. ) {
  400. this.currentFlows[column] = this.takePreviousFlow(column - 2);
  401. return column;
  402. }
  403. // There really shouldn't be another way of doing this - generate and wait for bug-reports...
  404. this.currentFlows[column] = this.generateNewFlow(column);
  405. return column;
  406. }
  407. // setLeftFlow handles '----.' - returns the last column that was set
  408. // generally we prefer to take the left most flow from the previous row that terminates this left recursion
  409. setLeftFlow(column, currentRow, previousRow) {
  410. // This is: '----------.' or the like
  411. // ' \ \ /|\'
  412. // Find the end of the '-' or nearest '/|\' in the previousRow :
  413. let originalColumn = column;
  414. let flow;
  415. for (; column < currentRow.length && currentRow[column] === '-'; column++) {
  416. if (column > 0 && column - 1 < previousRow.length && previousRow[column - 1] === '/') {
  417. flow = this.takePreviousFlow(column - 1);
  418. break;
  419. } else if (column < previousRow.length && previousRow[column] === '|') {
  420. flow = this.takePreviousFlow(column);
  421. break;
  422. } else if (
  423. column + 1 < previousRow.length &&
  424. previousRow[column + 1] === '\\'
  425. ) {
  426. flow = this.takePreviousFlow(column + 1);
  427. break;
  428. }
  429. }
  430. // if we have a flow then we found a '/|\' in the previousRow
  431. if (flow) {
  432. for (; originalColumn < column + 1; originalColumn++) {
  433. this.currentFlows[originalColumn] = flow;
  434. }
  435. return column;
  436. }
  437. // If the symbol in the column is not a '.' then there's likely an error
  438. if (currentRow[column] !== '.') {
  439. // It really should end in a '.' but this one doesn't...
  440. // 1. Step back - we don't want to eat this column
  441. column--;
  442. // 2. Generate a new flow and await bug-reports...
  443. this.currentFlows[column] = this.generateNewFlow(column);
  444. // 3. Assign all of the '-' to the same flow.
  445. for (; originalColumn < column; originalColumn++) {
  446. this.currentFlows[originalColumn] = this.currentFlows[column];
  447. }
  448. return column;
  449. }
  450. // We have a terminal '.' eg. the current row looks like '----.'
  451. // the previous row should look like one of '/|\' eg. ' \'
  452. if (column > 0 && column - 1 < previousRow.length && previousRow[column - 1] === '/') {
  453. flow = this.takePreviousFlow(column - 1);
  454. } else if (column < previousRow.length && previousRow[column] === '|') {
  455. flow = this.takePreviousFlow(column);
  456. } else if (
  457. column + 1 < previousRow.length &&
  458. previousRow[column + 1] === '\\'
  459. ) {
  460. flow = this.takePreviousFlow(column + 1);
  461. } else {
  462. // Again unexpected so let's generate and wait the bug-report
  463. flow = this.generateNewFlow(column);
  464. }
  465. // Assign all of the rest of the ----. to this flow.
  466. for (; originalColumn < column + 1; originalColumn++) {
  467. this.currentFlows[originalColumn] = flow;
  468. }
  469. return column;
  470. }
  471. }
  472. function generateRandomColorString() {
  473. const chars = '0123456789ABCDEF';
  474. const stringLength = 6;
  475. let randomString = '',
  476. rnum,
  477. i;
  478. for (i = 0; i < stringLength; i++) {
  479. rnum = Math.floor(Math.random() * chars.length);
  480. randomString += chars.substring(rnum, rnum + 1);
  481. }
  482. return randomString;
  483. }
  484. export default async function initGitGraph() {
  485. const graphCanvas = document.getElementById('graph-canvas');
  486. if (!graphCanvas || !graphCanvas.getContext) return;
  487. // Grab the raw graphList
  488. const graphList = [];
  489. $('#graph-raw-list li span.node-relation').each(function () {
  490. graphList.push($(this).text());
  491. });
  492. // Define some drawing parameters
  493. const config = {
  494. unitSize: 20,
  495. lineWidth: 3,
  496. nodeRadius: 4
  497. };
  498. const gitGraph = new GitGraph(graphCanvas, graphList, config);
  499. gitGraph.draw();
  500. graphCanvas.closest('#git-graph-container').classList.add('in');
  501. }