Skip to content
This repository was archived by the owner on Jul 13, 2024. It is now read-only.

Commit 3f42ee1

Browse files
authored
Merge pull request #286 from nicoespeon/custom-render-branch-labels
Customize how we render branches labels
2 parents aa0a4f3 + 75aabe4 commit 3f42ee1

File tree

7 files changed

+102
-11
lines changed

7 files changed

+102
-11
lines changed

packages/gitgraph-core/src/branch.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { TemplateOptions, BranchStyle } from "./template";
55

66
export {
77
BranchCommitDefaultOptions,
8+
BranchRenderOptions,
89
BranchOptions,
910
DELETED_BRANCH_NAME,
1011
createDeletedBranch,
@@ -17,7 +18,11 @@ interface BranchCommitDefaultOptions<TNode> extends CommitRenderOptions<TNode> {
1718
style?: TemplateOptions["commit"];
1819
}
1920

20-
interface BranchOptions<TNode = SVGElement> {
21+
interface BranchRenderOptions<TNode> {
22+
renderLabel?: (branch: Branch<TNode>) => TNode;
23+
}
24+
25+
interface BranchOptions<TNode = SVGElement> extends BranchRenderOptions<TNode> {
2126
/**
2227
* Gitgraph constructor
2328
*/
@@ -52,6 +57,7 @@ class Branch<TNode = SVGElement> {
5257
public computedColor?: BranchStyle["color"];
5358
public parentCommitHash: BranchOptions["parentCommitHash"];
5459
public commitDefaultOptions: BranchCommitDefaultOptions<TNode>;
60+
public renderLabel: BranchOptions<TNode>["renderLabel"];
5561

5662
private gitgraph: GitgraphCore<TNode>;
5763
private onGraphUpdate: () => void;
@@ -63,6 +69,7 @@ class Branch<TNode = SVGElement> {
6369
this.parentCommitHash = options.parentCommitHash;
6470
this.commitDefaultOptions = options.commitDefaultOptions || { style: {} };
6571
this.onGraphUpdate = options.onGraphUpdate;
72+
this.renderLabel = options.renderLabel;
6673
}
6774

6875
/**

packages/gitgraph-core/src/user-api/gitgraph-user-api.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Commit, CommitRenderOptions, CommitOptions } from "../commit";
33
import {
44
Branch,
55
BranchCommitDefaultOptions,
6+
BranchRenderOptions,
67
DELETED_BRANCH_NAME,
78
} from "../branch";
89
import { GitgraphCore } from "../gitgraph";
@@ -31,7 +32,7 @@ interface GitgraphTagOptions {
3132
style?: TemplateOptions["tag"];
3233
}
3334

34-
interface GitgraphBranchOptions<TNode> {
35+
interface GitgraphBranchOptions<TNode> extends BranchRenderOptions<TNode> {
3536
/**
3637
* Branch name
3738
*/

packages/gitgraph-js/src/gitgraph.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -429,24 +429,28 @@ function createGitgraph(
429429
// To do so, we'd need to reposition each of them appropriately.
430430
if (commit.branchToDisplay !== branch.name) return null;
431431

432-
let branchLabel;
432+
const branchLabel = branch.renderLabel
433+
? branch.renderLabel(branch)
434+
: createBranchLabel(branch, commit);
435+
436+
let branchLabelContainer;
433437
if (gitgraph.isVertical) {
434-
branchLabel = createG({
435-
children: [createBranchLabel(branch, commit)],
438+
branchLabelContainer = createG({
439+
children: [branchLabel],
436440
});
437441
} else {
438442
const commitDotSize = commit.style.dot.size * 2;
439443
const horizontalMarginTop = 10;
440444

441-
branchLabel = createG({
445+
branchLabelContainer = createG({
442446
translate: { x: commit.x, y: commitDotSize + horizontalMarginTop },
443-
children: [createBranchLabel(branch, commit)],
447+
children: [branchLabel],
444448
});
445449
}
446450

447-
setBranchLabelRef(commit, branchLabel);
451+
setBranchLabelRef(commit, branchLabelContainer);
448452

449-
return branchLabel;
453+
return branchLabelContainer;
450454
});
451455
}
452456

packages/gitgraph-react/src/Gitgraph.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,11 @@ class Gitgraph extends React.Component<GitgraphProps, GitgraphState> {
318318
if (commit.branchToDisplay !== branch.name) return null;
319319

320320
const ref = this.createBranchLabelRef(commit);
321-
const branchLabel = <BranchLabel branch={branch} commit={commit} />;
321+
const branchLabel = branch.renderLabel ? (
322+
branch.renderLabel(branch)
323+
) : (
324+
<BranchLabel branch={branch} commit={commit} />
325+
);
322326

323327
if (this.gitgraph.isVertical) {
324328
return (

packages/stories/src/gitgraph-js/6-custom-renders.stories.tsx

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { storiesOf } from "@storybook/react";
33
import {
44
createGitgraph,
55
CommitOptions,
6+
BranchOptions,
67
Mode,
78
Orientation,
89
TemplateName,
@@ -239,4 +240,39 @@ storiesOf("gitgraph-js/6. Custom renders", module)
239240
});
240241
}}
241242
</GraphContainer>
242-
));
243+
))
244+
.add("with render branch label", () => {
245+
const renderLabel: BranchOptions["renderLabel"] = (branch) => {
246+
return createText({
247+
content: `🎷 ${branch.name}`,
248+
fill: branch.style.label.color,
249+
font: branch.style.label.font,
250+
translate: {
251+
x: 0,
252+
y: 20,
253+
},
254+
rotate: -15,
255+
});
256+
};
257+
258+
return (
259+
<GraphContainer>
260+
{(graphContainer) => {
261+
const gitgraph = createGitgraph(graphContainer, {
262+
generateCommitHash: createFixedHashGenerator(),
263+
});
264+
265+
gitgraph
266+
.branch({ name: "master", renderLabel })
267+
.commit("Initial commit")
268+
.commit("Another commit")
269+
.commit("Do something crazy");
270+
271+
gitgraph
272+
.branch({ name: "dev", renderLabel })
273+
.commit("Oh my god")
274+
.commit("Last commit of the branch");
275+
}}
276+
</GraphContainer>
277+
);
278+
});

packages/stories/src/gitgraph-react/6-custom-renders.stories.tsx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { storiesOf } from "@storybook/react";
33
import {
44
Gitgraph,
55
CommitOptions,
6+
BranchOptions,
67
Mode,
78
Orientation,
89
templateExtend,
@@ -244,4 +245,37 @@ storiesOf("gitgraph-react/6. Custom renders", module)
244245
}}
245246
</Gitgraph>
246247
);
248+
})
249+
.add("with render branch label", () => {
250+
const renderLabel: BranchOptions["renderLabel"] = (branch) => {
251+
return (
252+
<text
253+
alignmentBaseline="middle"
254+
dominantBaseline="middle"
255+
fill={branch.style.label.color}
256+
style={{ font: branch.style.label.font }}
257+
y={20}
258+
transform={"rotate(-15)"}
259+
>
260+
🎷 {branch.name}
261+
</text>
262+
);
263+
};
264+
265+
return (
266+
<Gitgraph options={{ generateCommitHash: createFixedHashGenerator() }}>
267+
{(gitgraph) => {
268+
gitgraph
269+
.branch({ name: "master", renderLabel })
270+
.commit("Initial commit")
271+
.commit("Another commit")
272+
.commit("Do something crazy");
273+
274+
gitgraph
275+
.branch({ name: "dev", renderLabel })
276+
.commit("Oh my god")
277+
.commit("Last commit of the branch");
278+
}}
279+
</Gitgraph>
280+
);
247281
});

packages/stories/src/helpers.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ interface TextOptions {
130130
x: number;
131131
y: number;
132132
};
133+
rotate?: number;
133134
onClick?: () => void;
134135
}
135136

@@ -156,6 +157,10 @@ function createText(options: TextOptions): SVGTextElement {
156157
text.setAttribute("y", options.translate.y.toString());
157158
}
158159

160+
if (options.rotate) {
161+
text.setAttribute("transform", `rotate(${options.rotate})`);
162+
}
163+
159164
if (options.onClick) {
160165
text.addEventListener("click", options.onClick);
161166
}

0 commit comments

Comments
 (0)