forked from nicoespeon/gitgraph.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit.ts
More file actions
309 lines (281 loc) · 7.14 KB
/
commit.ts
File metadata and controls
309 lines (281 loc) · 7.14 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import { CommitStyle, TagStyle } from "./template";
import { Branch } from "./branch";
import { Refs } from "./refs";
import { Tag } from "./tag";
import { GitgraphTagOptions } from "./user-api/gitgraph-user-api";
export { CommitRenderOptions, CommitOptions, Commit };
interface CommitRenderOptions<TNode> {
renderDot?: (commit: Commit<TNode>) => TNode;
renderMessage?: (commit: Commit<TNode>) => TNode;
renderTooltip?: (commit: Commit<TNode>) => TNode;
}
interface CommitOptions<TNode> extends CommitRenderOptions<TNode> {
author: string;
subject: string;
style: CommitStyle;
body?: string;
hash?: string;
parents?: string[];
dotText?: string;
onClick?: (commit: Commit<TNode>) => void;
onMessageClick?: (commit: Commit<TNode>) => void;
onMouseOver?: (commit: Commit<TNode>) => void;
onMouseOut?: (commit: Commit<TNode>) => void;
}
/**
* Generate a random hash.
*
* @return hex string with 40 chars
*/
const getRandomHash = () =>
(
Math.random().toString(16).substring(3) +
Math.random().toString(16).substring(3) +
Math.random().toString(16).substring(3) +
Math.random().toString(16).substring(3)
).substring(0, 40);
class Commit<TNode = SVGElement> {
/**
* Ref names
*/
public refs: Array<Branch["name"] | "HEAD"> = [];
/**
* Commit x position
*/
public x = 0;
/**
* Commit y position
*/
public y = 0;
/**
* Commit hash
*/
public hash: string;
/**
* Abbreviated commit hash
*/
public hashAbbrev: string;
/**
* Parent hashes
*/
public parents: Array<Commit<TNode>["hash"]>;
/**
* Abbreviated parent hashed
*/
public parentsAbbrev: Array<Commit<TNode>["hashAbbrev"]>;
/**
* Author
*/
public author: {
/**
* Author name
*/
name: string;
/**
* Author email
*/
email: string;
/**
* Author date
*/
timestamp: number;
};
/**
* Committer
*/
public committer: {
/**
* Commiter name
*/
name: string;
/**
* Commiter email
*/
email: string;
/**
* Commiter date
*/
timestamp: number;
};
/**
* Subject
*/
public subject: string;
/**
* Body
*/
public body: string;
/**
* Message
*/
public get message() {
let message = "";
if (this.style.message.displayHash) {
message += `${this.hashAbbrev} `;
}
message += this.subject;
if (this.style.message.displayAuthor) {
message += ` - ${this.author.name} <${this.author.email}>`;
}
return message;
}
/**
* Style
*/
public style: CommitStyle;
/**
* Text inside commit dot
*/
public dotText?: string;
/**
* List of branches attached
*/
public branches?: Array<Branch["name"]>;
/**
* Branch that should be rendered
*/
public get branchToDisplay(): Branch["name"] {
return this.branches ? this.branches[0] : "";
}
/**
* List of tags attached
*/
public tags?: Array<Tag<TNode>>;
/**
* Callback to execute on click.
*/
public onClick: () => void;
/**
* Callback to execute on click on the commit message.
*/
public onMessageClick: () => void;
/**
* Callback to execute on mouse over.
*/
public onMouseOver: () => void;
/**
* Callback to execute on mouse out.
*/
public onMouseOut: () => void;
/**
* Custom dot render
*/
public renderDot?: (commit: Commit<TNode>) => TNode;
/**
* Custom message render
*/
public renderMessage?: (commit: Commit<TNode>) => TNode;
/**
* Custom tooltip render
*/
public renderTooltip?: (commit: Commit<TNode>) => TNode;
constructor(options: CommitOptions<TNode>) {
// Set author & committer
let name, email;
try {
[, name, email] = options.author.match(/(.*) <(.*)>/) as RegExpExecArray;
} catch (e) {
[name, email] = [options.author, ""];
}
this.author = { name, email, timestamp: Date.now() };
this.committer = { name, email, timestamp: Date.now() };
// Set commit message
this.subject = options.subject;
this.body = options.body || "";
// Set commit hash
this.hash = options.hash || getRandomHash();
this.hashAbbrev = this.hash.substring(0, 7);
// Set parent hash
this.parents = options.parents ? options.parents : [];
this.parentsAbbrev = this.parents.map((commit) => commit.substring(0, 7));
// Set style
this.style = {
...options.style,
message: { ...options.style.message },
dot: { ...options.style.dot },
};
this.dotText = options.dotText;
// Set callbacks
this.onClick = () => (options.onClick ? options.onClick(this) : undefined);
this.onMessageClick = () =>
options.onMessageClick ? options.onMessageClick(this) : undefined;
this.onMouseOver = () =>
options.onMouseOver ? options.onMouseOver(this) : undefined;
this.onMouseOut = () =>
options.onMouseOut ? options.onMouseOut(this) : undefined;
// Set custom renders
this.renderDot = options.renderDot;
this.renderMessage = options.renderMessage;
this.renderTooltip = options.renderTooltip;
}
public setRefs(refs: Refs): this {
this.refs = refs.getNames(this.hash);
return this;
}
public setTags(
tags: Refs,
getTagStyle: (name: Tag<TNode>["name"]) => Partial<TagStyle>,
getTagRender: (
name: Tag<TNode>["name"],
) => GitgraphTagOptions<TNode>["render"],
): this {
this.tags = tags
.getNames(this.hash)
.map(
(name) =>
new Tag(name, getTagStyle(name), getTagRender(name), this.style),
);
return this;
}
public setBranches(branches: Array<Branch["name"]>): this {
this.branches = branches;
return this;
}
public setPosition({ x, y }: { x: number; y: number }): this {
this.x = x;
this.y = y;
return this;
}
public withDefaultColor(color: string): Commit<TNode> {
const newStyle = {
...this.style,
dot: { ...this.style.dot },
message: { ...this.style.message },
};
if (!newStyle.color) newStyle.color = color;
if (!newStyle.dot.color) newStyle.dot.color = color;
if (!newStyle.message.color) newStyle.message.color = color;
const commit = this.cloneCommit();
commit.style = newStyle;
return commit;
}
/**
* Ideally, we want Commit to be a [Value Object](https://martinfowler.com/bliki/ValueObject.html).
* We started with a mutable class. So we'll refactor that little by little.
* This private function is a helper to create a new Commit from existing one.
*/
private cloneCommit() {
const commit = new Commit({
author: `${this.author.name} <${this.author.email}>`,
subject: this.subject,
style: this.style,
body: this.body,
hash: this.hash,
parents: this.parents,
dotText: this.dotText,
onClick: this.onClick,
onMessageClick: this.onMessageClick,
onMouseOver: this.onMouseOver,
onMouseOut: this.onMouseOut,
renderDot: this.renderDot,
renderMessage: this.renderMessage,
renderTooltip: this.renderTooltip,
});
commit.refs = this.refs;
commit.branches = this.branches;
commit.tags = this.tags;
commit.x = this.x;
commit.y = this.y;
return commit;
}
}