|
| 1 | +import * as React from 'react'; |
| 2 | +import { |
| 3 | + generateGraphData, |
| 4 | + ICommit, |
| 5 | + INode, |
| 6 | + IRoute |
| 7 | +} from '../generateGraphData'; |
| 8 | +import { SVGPathData } from '../svgPathData'; |
| 9 | + |
| 10 | +const COLOURS = [ |
| 11 | + '#e11d21', |
| 12 | + '#fbca04', |
| 13 | + '#009800', |
| 14 | + '#006b75', |
| 15 | + '#207de5', |
| 16 | + '#0052cc', |
| 17 | + '#5319e7', |
| 18 | + '#f7c6c7', |
| 19 | + '#fad8c7', |
| 20 | + '#fef2c0', |
| 21 | + '#bfe5bf', |
| 22 | + '#c7def8', |
| 23 | + '#bfdadc', |
| 24 | + '#bfd4f2', |
| 25 | + '#d4c5f9', |
| 26 | + '#cccccc', |
| 27 | + '#84b6eb', |
| 28 | + '#e6e6e6', |
| 29 | + '#cc317c' |
| 30 | +]; |
| 31 | + |
| 32 | +const DEFAULT_BRANCH_GAP = 10; |
| 33 | +const DEFAULT_RADIUS = 3; |
| 34 | +const DEFAULT_LINE_WIDTH = 2; |
| 35 | + |
| 36 | +const getColour = function (branch: number) { |
| 37 | + const n = COLOURS.length; |
| 38 | + return COLOURS[branch % n]; |
| 39 | +}; |
| 40 | + |
| 41 | +const branchCount = (commitNodes: INode[]): number => { |
| 42 | + let maxBranch = -1; |
| 43 | + |
| 44 | + commitNodes.forEach(node => { |
| 45 | + maxBranch = node.routes.reduce((max, route) => { |
| 46 | + return Math.max(max, route.from, route.to); |
| 47 | + }, maxBranch); |
| 48 | + }); |
| 49 | + |
| 50 | + return maxBranch + 1; |
| 51 | +}; |
| 52 | + |
| 53 | +export interface IGitCommitGraphProps { |
| 54 | + /** |
| 55 | + * A list of commits with its own hash and its parents' hashes. |
| 56 | + */ |
| 57 | + commits: ICommit[]; |
| 58 | + /** |
| 59 | + * Callback to inquire the height of a specific SinglePastCommitInfo component. |
| 60 | + */ |
| 61 | + getNodeHeight: (sha: string) => number; |
| 62 | + /** |
| 63 | + * Radius of the commit dot. |
| 64 | + */ |
| 65 | + dotRadius?: number; |
| 66 | + /** |
| 67 | + * Width of the lines connecting the commit dots. |
| 68 | + */ |
| 69 | + lineWidth?: number; |
| 70 | +} |
| 71 | + |
| 72 | +export class GitCommitGraph extends React.Component<IGitCommitGraphProps> { |
| 73 | + constructor(props: IGitCommitGraphProps) { |
| 74 | + super(props); |
| 75 | + this._graphData = []; |
| 76 | + this._x_step = DEFAULT_BRANCH_GAP; |
| 77 | + this._dotRadius = this.props.dotRadius || DEFAULT_RADIUS; |
| 78 | + this._lineWidth = this.props.lineWidth || DEFAULT_LINE_WIDTH; |
| 79 | + } |
| 80 | + |
| 81 | + getGraphData(): INode[] { |
| 82 | + this._graphData = generateGraphData( |
| 83 | + this.props.commits, |
| 84 | + this.props.getNodeHeight |
| 85 | + ); |
| 86 | + return this._graphData; |
| 87 | + } |
| 88 | + |
| 89 | + getBranchCount(): number { |
| 90 | + return branchCount(this.getGraphData()); |
| 91 | + } |
| 92 | + |
| 93 | + getWidth(): number { |
| 94 | + return (this.getBranchCount() + 0.5) * this._x_step; |
| 95 | + } |
| 96 | + |
| 97 | + getHeight(): number { |
| 98 | + return ( |
| 99 | + this._graphData[this._graphData.length - 1].yOffset + |
| 100 | + this.props.getNodeHeight( |
| 101 | + this.props.commits[this.props.commits.length - 1].sha |
| 102 | + ) |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + renderRouteNode(svgPathDataAttribute: string, branch: number): JSX.Element { |
| 107 | + const colour = getColour(branch); |
| 108 | + const style = { |
| 109 | + stroke: colour, |
| 110 | + 'stroke-width': this._lineWidth, |
| 111 | + fill: 'none' |
| 112 | + }; |
| 113 | + |
| 114 | + const classes = `commits-graph-branch-${branch}`; |
| 115 | + |
| 116 | + return ( |
| 117 | + <path d={svgPathDataAttribute} style={style} className={classes}></path> |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + renderRoute(yOffset: number, route: IRoute, height: number): JSX.Element { |
| 122 | + const { from, to, branch } = route; |
| 123 | + const x_step = this._x_step; |
| 124 | + |
| 125 | + const svgPath = new SVGPathData(); |
| 126 | + |
| 127 | + const from_x = (from + 1) * x_step; |
| 128 | + const from_y = yOffset; |
| 129 | + const to_x = (to + 1) * x_step; |
| 130 | + const to_y = yOffset + height; |
| 131 | + |
| 132 | + svgPath.moveTo(from_x, from_y); |
| 133 | + if (from_x === to_x) { |
| 134 | + svgPath.lineTo(to_x, to_y); |
| 135 | + } else { |
| 136 | + svgPath.bezierCurveTo( |
| 137 | + from_x - x_step / 4, |
| 138 | + from_y + height / 2, |
| 139 | + to_x + x_step / 4, |
| 140 | + to_y - height / 2, |
| 141 | + to_x, |
| 142 | + to_y |
| 143 | + ); |
| 144 | + } |
| 145 | + |
| 146 | + return this.renderRouteNode(svgPath.toString(), branch); |
| 147 | + } |
| 148 | + |
| 149 | + renderCommitNode( |
| 150 | + x: number, |
| 151 | + y: number, |
| 152 | + sha: string, |
| 153 | + dot_branch: number |
| 154 | + ): JSX.Element { |
| 155 | + const radius = this._dotRadius; |
| 156 | + |
| 157 | + const colour = getColour(dot_branch); |
| 158 | + const strokeWidth = 1; |
| 159 | + const style = { |
| 160 | + stroke: colour, |
| 161 | + 'stroke-width': strokeWidth, |
| 162 | + fill: colour |
| 163 | + }; |
| 164 | + |
| 165 | + const classes = `commits-graph-branch-${dot_branch}`; |
| 166 | + |
| 167 | + return ( |
| 168 | + <circle |
| 169 | + cx={x} |
| 170 | + cy={y} |
| 171 | + r={radius} |
| 172 | + style={style} |
| 173 | + data-sha={sha} |
| 174 | + className={classes} |
| 175 | + > |
| 176 | + <title>{sha.slice(0, 7)}</title> |
| 177 | + </circle> |
| 178 | + ); |
| 179 | + } |
| 180 | + |
| 181 | + renderCommit(commit: INode): [JSX.Element, JSX.Element[]] { |
| 182 | + const { sha, dot, routes, yOffset } = commit; |
| 183 | + const { lateralOffset, branch } = dot; |
| 184 | + |
| 185 | + // draw dot |
| 186 | + const x = (lateralOffset + 1) * this._x_step; |
| 187 | + const y = yOffset; |
| 188 | + |
| 189 | + const commitNode = this.renderCommitNode(x, y, sha, branch); |
| 190 | + |
| 191 | + const routeNodes = routes.map(route => |
| 192 | + this.renderRoute( |
| 193 | + commit.yOffset, |
| 194 | + route, |
| 195 | + this.props.getNodeHeight(commit.sha) |
| 196 | + ) |
| 197 | + ); |
| 198 | + return [commitNode, routeNodes]; |
| 199 | + } |
| 200 | + |
| 201 | + render(): JSX.Element { |
| 202 | + // reset lookup table of commit node locations |
| 203 | + const allCommitNodes: JSX.Element[] = []; |
| 204 | + let allRouteNodes: JSX.Element[] = []; |
| 205 | + const commitNodes = this.getGraphData(); |
| 206 | + commitNodes.forEach(node => { |
| 207 | + const commit = node; |
| 208 | + const [commitNode, routeNodes] = this.renderCommit(commit); |
| 209 | + allCommitNodes.push(commitNode); |
| 210 | + allRouteNodes = allRouteNodes.concat(routeNodes); |
| 211 | + }); |
| 212 | + |
| 213 | + const children = [].concat(allRouteNodes, allCommitNodes); |
| 214 | + |
| 215 | + const height = this.getHeight(); |
| 216 | + const width = this.getWidth(); |
| 217 | + |
| 218 | + const style = { height, width, 'flex-shrink': 0 }; |
| 219 | + |
| 220 | + return ( |
| 221 | + <svg height={height} width={width} style={style}> |
| 222 | + {...children} |
| 223 | + </svg> |
| 224 | + ); |
| 225 | + } |
| 226 | + |
| 227 | + private _graphData: INode[]; |
| 228 | + private _x_step: number; |
| 229 | + private _dotRadius: number; |
| 230 | + private _lineWidth: number; |
| 231 | +} |
0 commit comments