forked from nicoespeon/gitgraph.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitgraph.tsx
More file actions
249 lines (219 loc) · 7.17 KB
/
Gitgraph.tsx
File metadata and controls
249 lines (219 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import * as React from "react";
import {
GitgraphCore,
GitgraphOptions,
GitgraphUserApi,
Commit as CommitCore,
MergeStyle,
Mode,
Orientation,
TemplateName,
templateExtend,
BranchesPaths,
Coordinate,
} from "@gitgraph/core";
import { BranchLabel } from "./BranchLabel";
import { Tooltip } from "./Tooltip";
import {
ReactSvgElement,
CommitOptions,
BranchOptions,
TagOptions,
MergeOptions,
Branch,
} from "./types";
import { Commit } from "./Commit";
import { BranchPath } from "./BranchPath";
export {
Gitgraph,
GitgraphProps,
GitgraphState,
TemplateName,
templateExtend,
MergeStyle,
Mode,
Orientation,
CommitOptions,
BranchOptions,
TagOptions,
MergeOptions,
Branch,
};
type GitgraphProps = GitgraphPropsWithChildren | GitgraphPropsWithGraph;
interface GitgraphPropsWithChildren {
options?: GitgraphOptions;
children: (gitgraph: GitgraphUserApi<ReactSvgElement>) => void;
}
interface GitgraphPropsWithGraph {
graph: GitgraphCore<ReactSvgElement>;
}
function isPropsWithGraph(
props: GitgraphProps,
): props is GitgraphPropsWithGraph {
return "graph" in props;
}
interface GitgraphState {
commits: Array<CommitCore<ReactSvgElement>>;
branchesPaths: BranchesPaths<ReactSvgElement>;
commitMessagesX: number;
// Store a map to replace commits y with the correct value,
// including the message offset. Allows custom, flexible message height.
// E.g. {20: 30} means for commit: y=20 -> y=30
// Offset should be computed when graph is rendered (componentDidUpdate).
commitYWithOffsets: { [key: number]: number };
shouldRecomputeOffsets: boolean;
currentCommitOver: CommitCore<ReactSvgElement> | null;
}
class Gitgraph extends React.Component<GitgraphProps, GitgraphState> {
public static defaultProps: Partial<GitgraphProps> = {
options: {},
};
private gitgraph: GitgraphCore<ReactSvgElement>;
private $graph = React.createRef<SVGSVGElement>();
private $commits = React.createRef<SVGGElement>();
private $tooltip: React.ReactElement<SVGGElement> | null = null;
constructor(props: GitgraphProps) {
super(props);
this.state = {
commits: [],
branchesPaths: new Map(),
commitMessagesX: 0,
commitYWithOffsets: {},
shouldRecomputeOffsets: true,
currentCommitOver: null,
};
this.gitgraph = isPropsWithGraph(props)
? props.graph
: new GitgraphCore<ReactSvgElement>(props.options);
this.gitgraph.subscribe((data) => {
const { commits, branchesPaths, commitMessagesX } = data;
this.setState({
commits,
branchesPaths,
commitMessagesX,
shouldRecomputeOffsets: true,
});
});
}
public render() {
return (
<svg ref={this.$graph}>
{/* Translate graph left => left-most branch label is not cropped (horizontal) */}
{/* Translate graph down => top-most commit tooltip is not cropped */}
<g transform={`translate(${BranchLabel.paddingX}, ${Tooltip.padding})`}>
{this.renderBranchesPaths()}
<g ref={this.$commits}>
{this.state.commits.map((commit) => (
<Commit
key={commit.hash}
commits={this.state.commits}
commit={commit}
currentCommitOver={this.state.currentCommitOver}
setCurrentCommitOver={this.setCurrentCommitOver.bind(this)}
gitgraph={this.gitgraph}
getWithCommitOffset={this.getWithCommitOffset.bind(this)}
setTooltip={this.setTooltip.bind(this)}
commitMessagesX={this.state.commitMessagesX}
/>
))}
</g>
{this.$tooltip}
</g>
</svg>
);
}
public componentDidMount() {
if (isPropsWithGraph(this.props)) return;
this.props.children(this.gitgraph.getUserApi());
}
public componentDidUpdate() {
if (this.$graph.current) {
const { height, width } = this.$graph.current.getBBox();
this.$graph.current.setAttribute(
"width",
// Add `Tooltip.padding` so we don't crop the tooltip text.
// Add `BranchLabel.paddingX` so we don't cut branch label.
(width + Tooltip.padding + BranchLabel.paddingX).toString(),
);
this.$graph.current.setAttribute(
"height",
// Add `Tooltip.padding` so we don't crop tooltip text
// Add `BranchLabel.paddingY` so we don't crop branch label.
(height + Tooltip.padding + BranchLabel.paddingY).toString(),
);
}
if (!this.state.shouldRecomputeOffsets) return;
if (!this.$commits.current) return;
const commits = Array.from(this.$commits.current.children);
this.setState({
commitYWithOffsets: this.computeOffsets(commits),
shouldRecomputeOffsets: false,
});
}
private setCurrentCommitOver(v: CommitCore<ReactSvgElement> | null) {
this.setState({ currentCommitOver: v });
}
private setTooltip(v: React.ReactElement<SVGGElement> | null) {
this.$tooltip = v;
}
private renderBranchesPaths() {
const offset = this.gitgraph.template.commit.dot.size;
const isBezier =
this.gitgraph.template.branch.mergeStyle === MergeStyle.Bezier;
return Array.from(this.state.branchesPaths).map(([branch, coordinates]) => (
<BranchPath
key={branch.name}
gitgraph={this.gitgraph}
branch={branch}
coordinates={coordinates}
getWithCommitOffset={this.getWithCommitOffset.bind(this)}
isBezier={isBezier}
offset={offset}
/>
));
}
private computeOffsets(
commits: Element[],
): GitgraphState["commitYWithOffsets"] {
let totalOffsetY = 0;
// In VerticalReverse orientation, commits are in the same order in the DOM.
const orientedCommits =
this.gitgraph.orientation === Orientation.VerticalReverse
? commits
: commits.reverse();
return orientedCommits.reduce<GitgraphState["commitYWithOffsets"]>(
(newOffsets, commit) => {
const commitY = parseInt(
commit.getAttribute("transform")!.split(",")[1].slice(0, -1),
10,
);
const firstForeignObject = commit.getElementsByTagName(
"foreignObject",
)[0];
const customHtmlMessage =
firstForeignObject && firstForeignObject.firstElementChild;
let messageHeight = 0;
if (customHtmlMessage) {
const height = customHtmlMessage.getBoundingClientRect().height;
const marginTopInPx =
window.getComputedStyle(customHtmlMessage).marginTop || "0px";
const marginTop = parseInt(marginTopInPx.replace("px", ""), 10);
messageHeight = height + marginTop;
}
// Force the height of the foreignObject (browser issue)
if (firstForeignObject) {
firstForeignObject.setAttribute("height", `${messageHeight}px`);
}
newOffsets[commitY] = commitY + totalOffsetY;
// Increment total offset after setting the offset
// => offset next commits accordingly.
totalOffsetY += messageHeight;
return newOffsets;
},
{},
);
}
private getWithCommitOffset({ x, y }: Coordinate): Coordinate {
return { x, y: this.state.commitYWithOffsets[y] || y };
}
}