Skip to content
This repository was archived by the owner on Jul 13, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"develop": "concurrently npm:watch npm:storybook",
"website": "lerna run start --scope=@gitgraph/website --stream",
"deploy:website": "lerna run deploy --scope=@gitgraph/website --stream",
"lint": "eslint packages/*/src/**/*.{ts,tsx,js,jsx}",
"lint": "eslint packages/**/src/**/*.{ts,tsx}",
"lint:fix": "npm run lint -- --fix",
"pretest": "npm run build",
"test": "jest",
Expand Down
29 changes: 22 additions & 7 deletions packages/gitgraph-js/src/gitgraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ interface CommitYWithOffsets {

function createGitgraph(
graphContainer: HTMLElement,
options?: GitgraphOptions,
options?: GitgraphOptions & { responsive?: boolean },
) {
let commitsElements: {
[commitHash: string]: {
Expand All @@ -85,9 +85,16 @@ function createGitgraph(

// Create an `svg` context in which we'll render the graph.
const svg = createSvg();
adaptSvgOnUpdate();
adaptSvgOnUpdate(Boolean(options && options.responsive));
graphContainer.appendChild(svg);

if (options && options.responsive) {
graphContainer.setAttribute(
"style",
"display:inline-block; position: relative; width:100%; padding-bottom:100%; vertical-align:middle; overflow:hidden;",
);
}

// React on gitgraph updates to re-render the graph.
const gitgraph = new GitgraphCore(options);
gitgraph.subscribe((data) => {
Expand Down Expand Up @@ -123,15 +130,15 @@ function createGitgraph(
);
}

function adaptSvgOnUpdate(): void {
function adaptSvgOnUpdate(adaptToContainer: boolean): void {
const observer = new MutationObserver(() => {
if (shouldRecomputeOffsets) {
shouldRecomputeOffsets = false;
computeOffsets();
render(lastData);
} else {
positionCommitsElements();
adaptGraphDimensions();
adaptGraphDimensions(adaptToContainer);
}
});

Expand Down Expand Up @@ -221,7 +228,7 @@ function createGitgraph(
});
}

function adaptGraphDimensions(): void {
function adaptGraphDimensions(adaptToContainer: boolean): void {
const { height, width } = svg.getBBox();

// FIXME: In horizontal mode, we mimic @gitgraph/react behavior
Expand All @@ -245,8 +252,16 @@ function createGitgraph(
// Add `BRANCH_LABEL_PADDING_Y` so we don't crop branch label.
BRANCH_LABEL_PADDING_Y + TOOLTIP_PADDING + verticalCustomOffset;

svg.setAttribute("width", (width + widthOffset).toString());
svg.setAttribute("height", (height + heightOffset).toString());
if (adaptToContainer) {
svg.setAttribute("preserveAspectRatio", "xMinYMin meet");
svg.setAttribute(
"viewBox",
`0 0 ${width + widthOffset} ${height + heightOffset}`,
);
} else {
svg.setAttribute("width", (width + widthOffset).toString());
svg.setAttribute("height", (height + heightOffset).toString());
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/gitgraph-react/src/Gitgraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class Gitgraph extends React.Component<GitgraphProps, GitgraphState> {
<g ref={this.$commits}>
{this.state.commits.map((commit) => (
<Commit
key={commit.hashAbbrev}
key={commit.hash}
commits={this.state.commits}
commit={commit}
currentCommitOver={this.state.currentCommitOver}
Expand Down
17 changes: 17 additions & 0 deletions packages/stories/src/gitgraph-js/1-basic-usage.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -422,4 +422,21 @@ storiesOf("gitgraph-js/1. Basic usage", module)
develop.delete();
}}
</GraphContainer>
))
.add("responsive", () => (
<GraphContainer>
{(graphContainer) => {
const gitgraph = createGitgraph(graphContainer, {
generateCommitHash: createFixedHashGenerator(),
responsive: true,
});

const master = gitgraph.branch("master").commit("Initial commit");
const develop = gitgraph.branch("develop");
develop.commit("one");
master.commit("two");
develop.commit("three");
master.merge(develop);
}}
</GraphContainer>
));