Skip to content

Commit 0e2741c

Browse files
authored
fix(graph): fixed canvas 2d graph alignment when showHeaders is false (#117) (#119)
Resolves the bug raised in #117.
2 parents 59e345f + da9e1aa commit 0e2741c

12 files changed

Lines changed: 89 additions & 16 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ This strategy can be used by rendering the `<GitLog.GraphHTMLGrid />` subcompone
117117

118118
### Canvas 2D
119119

120+
> [!WARNING]
121+
> This Canvas2D variant of the graph is in-complete and may have rendering bugs. It's also not as feature-rich as the HTML variant due to canvas API limitations and unfinished work.
122+
120123
This implementation uses a standard HTML `canvas` element with a `2d` rendering context.
121124

122125
This strategy can be used by rendering the `<GitLog.GraphCanvas2D />` subcomponent under the `<GitLog />`.

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/library/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,10 @@ The following properties are injected into the functions `props` argument:
337337
338338
### GraphCanvas2D
339339
340+
> [!WARNING]
341+
> This Canvas2D variant of the graph is in-complete and may have rendering bugs. It's also not as feature-rich as the HTML variant due to canvas API limitations and unfinished work.
342+
343+
340344
| Property | Type | Description |
341345
|-------------------|---------------------------------------|----------------------------------------------------------------------------------------------------------------|
342346
| `nodeTheme` | [`NodeTheme`](#nodetheme) | Theme applied to commit node elements in the graph. |

packages/library/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"author": "Thomas Plumpton",
44
"repository": "https://github.com/TomPlum/react-git-log",
55
"description": "A flexible, themable, React component for visualising Git commit history, branch and tag metadata.",
6-
"version": "3.5.0",
6+
"version": "3.5.1",
77
"license": "Apache-2.0",
88
"type": "module",
99
"main": "dist/react-git-log.js",

packages/library/src/__snapshots__/GitLog.spec.tsx.snap

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3691,6 +3691,7 @@ exports[`GitLog > Canvas2D Graph > should render correctly and match the snapsho
36913691
>
36923692
<canvas
36933693
class="canvas"
3694+
data-testid="graph-2d-canvas"
36943695
height="18960"
36953696
style="width: 168px; height: 18960px;"
36963697
width="168"
@@ -22732,7 +22733,7 @@ exports[`GitLog > HTML Grid Graph > should render correctly and match the snapsh
2273222733
>
2273322734
<div
2273422735
class="graph"
22735-
style="grid-template-columns: repeat(7, 1fr); grid-template-rows: repeat(474, 40px);"
22736+
style="grid-template-columns: repeat(7, 1fr); grid-template-rows: repeat(474, 40px); margin-top: 12px;"
2273622737
>
2273722738
<button
2273822739
class="column"
@@ -85368,7 +85369,7 @@ exports[`GitLog > HTML Grid Graph > should render correctly and match the snapsh
8536885369
>
8536985370
<div
8537085371
class="graph"
85371-
style="grid-template-columns: repeat(7, 1fr); grid-template-rows: repeat(474, 40px);"
85372+
style="grid-template-columns: repeat(7, 1fr); grid-template-rows: repeat(474, 40px); margin-top: 12px;"
8537285373
>
8537385374
<button
8537485375
class="column"
@@ -144982,7 +144983,7 @@ exports[`GitLog > HTML Grid Graph > should render correctly and match the snapsh
144982144983
>
144983144984
<div
144984144985
class="graph"
144985-
style="grid-template-columns: repeat(7, 1fr); grid-template-rows: repeat(30, 40px);"
144986+
style="grid-template-columns: repeat(7, 1fr); grid-template-rows: repeat(30, 40px); margin-top: 12px;"
144986144987
>
144987144988
<button
144988144989
class="column"
@@ -152750,7 +152751,7 @@ exports[`GitLog > HTML Grid Graph > should render correctly and match the snapsh
152750152751
>
152751152752
<div
152752152753
class="graph"
152753-
style="grid-template-columns: repeat(7, 1fr); grid-template-rows: repeat(473, 40px);"
152754+
style="grid-template-columns: repeat(7, 1fr); grid-template-rows: repeat(473, 40px); margin-top: 12px;"
152754152755
>
152755152756
<button
152756152757
class="column"
@@ -211463,7 +211464,7 @@ exports[`GitLog > HTML Grid Graph > should render correctly and match the snapsh
211463211464
>
211464211465
<div
211465211466
class="graph"
211466-
style="grid-template-columns: repeat(1, 1fr); grid-template-rows: repeat(10, 40px);"
211467+
style="grid-template-columns: repeat(1, 1fr); grid-template-rows: repeat(10, 40px); margin-top: 12px;"
211467211468
>
211468211469
<button
211469211470
class="column"
@@ -215695,7 +215696,7 @@ exports[`GitLog > HTML Grid Graph > should render correctly and match the snapsh
215695215696
>
215696215697
<div
215697215698
class="graph"
215698-
style="grid-template-columns: repeat(7, 1fr); grid-template-rows: repeat(474, 40px);"
215699+
style="grid-template-columns: repeat(7, 1fr); grid-template-rows: repeat(474, 40px); margin-top: 12px;"
215699215700
>
215700215701
<button
215701215702
class="column"

packages/library/src/__snapshots__/GitLogPaged.spec.tsx.snap

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ exports[`GitLogPaged > should render correctly and match the snapshot the pagina
276276
>
277277
<canvas
278278
class="canvas"
279+
data-testid="graph-2d-canvas"
279280
height="1600"
280281
style="width: 120px; height: 1600px;"
281282
width="120"
@@ -1763,7 +1764,7 @@ exports[`GitLogPaged > should render correctly and match the snapshot the pagina
17631764
>
17641765
<div
17651766
class="graph"
1766-
style="grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(20, 40px);"
1767+
style="grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(20, 40px); margin-top: 12px;"
17671768
>
17681769
<button
17691770
class="column"
@@ -3912,7 +3913,7 @@ exports[`GitLogPaged > should render correctly and match the snapshot the pagina
39123913
>
39133914
<div
39143915
class="graph"
3915-
style="grid-template-columns: repeat(5, 1fr); grid-template-rows: repeat(20, 40px);"
3916+
style="grid-template-columns: repeat(5, 1fr); grid-template-rows: repeat(20, 40px); margin-top: 12px;"
39163917
>
39173918
<button
39183919
class="column"
@@ -6854,7 +6855,7 @@ exports[`GitLogPaged > should render correctly and match the snapshot the pagina
68546855
>
68556856
<div
68566857
class="graph"
6857-
style="grid-template-columns: repeat(5, 1fr); grid-template-rows: repeat(40, 40px);"
6858+
style="grid-template-columns: repeat(5, 1fr); grid-template-rows: repeat(40, 40px); margin-top: 12px;"
68586859
>
68596860
<button
68606861
class="column"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* The number of pixels of margin at the
3+
* top of the graph element that offsets
4+
* it enough to line-up correctly with
5+
* adjacent elements (such as tags or a table).
6+
*/
7+
export const GRAPH_MARGIN_TOP = 12

packages/library/src/modules/Graph/core/GraphCore.module.scss

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
}
44

55
.graph {
6-
margin-top: 12px;
76
height: 100%;
87
display: grid;
98
gap: 0;

packages/library/src/modules/Graph/strategies/Canvas/Canvas2DGraph.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import {
99
} from 'modules/Graph/strategies/Canvas'
1010
import { useSelectCommit } from 'hooks/useSelectCommit'
1111
import styles from './Canvas2DGraph.module.scss'
12+
import { GRAPH_MARGIN_TOP } from 'modules/Graph/constants'
1213

1314
export const Canvas2DGraph = () => {
1415
const { selectCommitHandler } = useSelectCommit()
15-
const { isServerSidePaginated } = useGitContext()
1616
const { graphWidth, visibleCommits, nodeSize, nodeTheme, orientation } = useGraphContext()
1717

1818
const {
@@ -31,7 +31,9 @@ export const Canvas2DGraph = () => {
3131
indexCommit,
3232
isIndexVisible,
3333
selectedCommit,
34-
previewedCommit
34+
previewedCommit,
35+
isServerSidePaginated,
36+
showHeaders
3537
} = useGitContext()
3638

3739
const getNodeColours = useCallback<GetCanvasRendererColoursFunction>((columnIndex: number) => {
@@ -168,6 +170,10 @@ export const Canvas2DGraph = () => {
168170
width={canvasWidth}
169171
height={canvasHeight}
170172
className={styles.canvas}
173+
data-testid='graph-2d-canvas'
174+
style={{
175+
marginTop: showHeaders ? undefined : GRAPH_MARGIN_TOP
176+
}}
171177
/>
172178
)
173179
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { render, screen } from '@testing-library/react'
2+
import { Canvas2DGraph } from 'modules/Graph/strategies/Canvas/Canvas2DGraph'
3+
import * as gitContext from 'context/GitContext/useGitContext'
4+
import { commit, gitContextBag, graphContextBag, graphData } from 'test/stubs'
5+
import { describe, it, expect, beforeEach } from 'vitest'
6+
import * as graphContext from 'modules/Graph/context'
7+
8+
describe('CanvasGraph2D', () => {
9+
beforeEach(() => {
10+
vi.spyOn(graphContext, 'useGraphContext').mockReturnValue(graphContextBag())
11+
})
12+
13+
it('should apply margin-top to the canvas element when showHeaders is false', () => {
14+
const selectedCommit = commit({ hash: 'selected' })
15+
16+
vi.spyOn(gitContext, 'useGitContext').mockReturnValue(
17+
gitContextBag({
18+
showHeaders: false,
19+
selectedCommit,
20+
graphData: graphData({
21+
hashToCommit: new Map([[selectedCommit.hash, selectedCommit]]),
22+
positions: new Map([[selectedCommit.hash, [0, 0]]])
23+
})
24+
})
25+
)
26+
27+
render(<Canvas2DGraph />)
28+
29+
expect(screen.getByTestId('graph-2d-canvas')).toHaveStyle('margin-top: 12px')
30+
})
31+
32+
it('should NOT apply margin-top to the canvas element when showHeaders is true', () => {
33+
const selectedCommit = commit({ hash: 'selected' })
34+
35+
vi.spyOn(gitContext, 'useGitContext').mockReturnValue(
36+
gitContextBag({
37+
showHeaders: true,
38+
selectedCommit,
39+
graphData: graphData({
40+
hashToCommit: new Map([[selectedCommit.hash, selectedCommit]]),
41+
positions: new Map([[selectedCommit.hash, [0, 0]]])
42+
})
43+
})
44+
)
45+
46+
render(<Canvas2DGraph />)
47+
48+
expect(screen.getByTestId('graph-2d-canvas')).toHaveStyle('margin-top: undefined')
49+
})
50+
})

0 commit comments

Comments
 (0)