Skip to content

Commit aa4d1d9

Browse files
authored
Diff improvements (#23553)
- Avoid flash of wrong tree toggle icon on page load by setting icon based on sync state - Avoid "pop-in" of tree on page load by leaving space based on sync state - Use the same border/box-shadow combo used on comment `:target` also for file `:target`. - Refactor `DiffFileTree.vue` to use `toggleElem` instead of hardcoded class name. - Left-align inline comment boxes and make them fit the same amount of markup content on a line as GitHub. - Fix height of `diff-file-list` Fixes: #23593 <img width="1250" alt="Screenshot 2023-03-18 at 00 52 04" src="https://user-images.githubusercontent.com/115237/226071392-6789a644-aead-4756-a77e-aba3642150a0.png"> <img width="1246" alt="Screenshot 2023-03-18 at 00 59 43" src="https://user-images.githubusercontent.com/115237/226071443-8bcba924-458b-48bd-b2f0-0de59cb180ac.png"> <img width="1250" alt="Screenshot 2023-03-18 at 01 27 14" src="https://user-images.githubusercontent.com/115237/226073121-ccb99f9a-d3ac-40b7-9589-43580c4a01c9.png"> <img width="1231" alt="Screenshot 2023-03-19 at 21 44 16" src="https://user-images.githubusercontent.com/115237/226207951-81bcae1b-6b41-4e39-83a7-0f37951df6be.png"> (Yes I'm aware the border-radius in bottom corners is suboptimal, but this would be notorously hard to fix without relying on `overflow: hidden`).
1 parent 35cb786 commit aa4d1d9

File tree

7 files changed

+81
-40
lines changed

7 files changed

+81
-40
lines changed

Diff for: options/locale/locale_en-US.ini

+2
Original file line numberDiff line numberDiff line change
@@ -2280,6 +2280,8 @@ diff.image.side_by_side = Side by Side
22802280
diff.image.swipe = Swipe
22812281
diff.image.overlay = Overlay
22822282
diff.has_escaped = This line has hidden Unicode characters
2283+
diff.show_file_tree = Show file tree
2284+
diff.hide_file_tree = Hide file tree
22832285

22842286
releases.desc = Track project versions and downloads.
22852287
release.releases = Releases

Diff for: templates/repo/diff/box.tmpl

+14-4
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,18 @@
1515
<div>
1616
<div class="diff-detail-box diff-box sticky gt-df gt-sb gt-ac gt-fw">
1717
<div class="gt-df gt-ac gt-fw">
18-
<a class="diff-toggle-file-tree-button muted gt-df gt-ac">
18+
<button class="diff-toggle-file-tree-button gt-df gt-ac" data-show-text="{{.locale.Tr "repo.diff.show_file_tree"}}" data-hide-text="{{.locale.Tr "repo.diff.hide_file_tree"}}">
1919
{{/* the icon meaning is reversed here, "octicon-sidebar-collapse" means show the file tree */}}
20-
{{svg "octicon-sidebar-collapse" 20 "icon hide"}}
21-
{{svg "octicon-sidebar-expand" 20 "icon"}}
22-
</a>
20+
{{svg "octicon-sidebar-collapse" 20 "icon gt-hidden"}}
21+
{{svg "octicon-sidebar-expand" 20 "icon gt-hidden"}}
22+
</button>
23+
<script>
24+
const diffTreeVisible = localStorage?.getItem('diff_file_tree_visible') === 'true';
25+
const diffTreeBtn = document.querySelector('.diff-toggle-file-tree-button');
26+
const diffTreeIcon = `.octicon-sidebar-${diffTreeVisible ? 'expand' : 'collapse'}`;
27+
diffTreeBtn.querySelector(diffTreeIcon).classList.remove('gt-hidden');
28+
diffTreeBtn.setAttribute('data-tooltip-content', diffTreeBtn.getAttribute(diffTreeVisible ? 'data-hide-text' : 'data-show-text'));
29+
</script>
2330
<div class="diff-detail-stats gt-df gt-ac gt-ml-3">
2431
{{svg "octicon-diff" 16 "gt-mr-2"}}{{.locale.Tr "repo.diff.stats_desc" .Diff.NumFiles .Diff.TotalAddition .Diff.TotalDeletion | Str2html}}
2532
</div>
@@ -68,6 +75,9 @@
6875
<div id="diff-file-list"></div>
6976
<div id="diff-container">
7077
<div id="diff-file-tree" class="gt-hidden"></div>
78+
<script>
79+
if (diffTreeVisible) document.getElementById('diff-file-tree').classList.remove('gt-hidden');
80+
</script>
7181
<div id="diff-file-boxes" class="sixteen wide column">
7282
{{range $i, $file := .Diff.Files}}
7383
{{/*notice: the index of Diff.Files should not be used for element ID, because the index will be restarted from 0 when doing load-more for PRs with a lot of files*/}}

Diff for: web_src/css/base.css

+4
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ table {
238238
border-collapse: collapse;
239239
}
240240

241+
button {
242+
cursor: pointer;
243+
}
244+
241245
details summary {
242246
cursor: pointer;
243247
}

Diff for: web_src/css/repository.css

+37-6
Original file line numberDiff line numberDiff line change
@@ -1616,14 +1616,12 @@
16161616
padding: 7px 0;
16171617
background: var(--color-body);
16181618
line-height: 30px;
1619-
height: 47px; /* match .ui.attached.header.diff-file-header.sticky-2nd-row */
16201619
}
16211620

16221621
@media (max-width: 991px) {
16231622
.repository .diff-detail-box {
16241623
flex-direction: column;
16251624
align-items: flex-start;
1626-
height: 77px; /* match .ui.attached.header.diff-file-header.sticky-2nd-row */
16271625
}
16281626
}
16291627

@@ -1679,6 +1677,13 @@
16791677
}
16801678
}
16811679

1680+
.diff-detail-actions {
1681+
/* prevent font-size from increasing element height so that .diff-detail-box comes
1682+
out with height of 47px (one line) and 77px (two lines), which is important for
1683+
position: sticky */
1684+
height: 33px;
1685+
}
1686+
16821687
.repository .diff-detail-box .diff-detail-actions > * {
16831688
margin-right: 0;
16841689
}
@@ -1853,10 +1858,24 @@
18531858
padding-bottom: 5px;
18541859
}
18551860

1861+
.diff-file-box {
1862+
border: 1px solid transparent;
1863+
border-radius: var(--border-radius);
1864+
}
1865+
1866+
/* TODO: this can potentially be made "global" by removing the class prefix */
1867+
.diff-file-box .ui.attached.header,
1868+
.diff-file-box .ui.attached.table {
1869+
margin: 0; /* remove fomantic negative margins */;
1870+
width: initial; /* remove fomantic over 100% width */;
1871+
max-width: initial; /* remove fomantic over 100% width */;
1872+
}
1873+
18561874
.repository .diff-stats {
18571875
clear: both;
18581876
margin-bottom: 5px;
1859-
max-height: 400px;
1877+
max-height: 200px;
1878+
height: fit-content;
18601879
overflow: auto;
18611880
padding-left: 0;
18621881
}
@@ -2652,7 +2671,8 @@
26522671
filter: drop-shadow(-3px 0 0 var(--color-primary-alpha-30)) !important;
26532672
}
26542673

2655-
.code-comment:target {
2674+
.code-comment:target,
2675+
.diff-file-box:target {
26562676
border-color: var(--color-primary) !important;
26572677
border-radius: var(--border-radius) !important;
26582678
box-shadow: 0 0 0 3px var(--color-primary-alpha-30) !important;
@@ -3226,17 +3246,28 @@ td.blob-excerpt {
32263246
}
32273247

32283248
#diff-file-tree {
3229-
width: 20%;
3249+
flex: 0 0 20%;
32303250
max-width: 380px;
32313251
line-height: inherit;
32323252
position: sticky;
32333253
padding-top: 0;
32343254
top: 47px;
3235-
max-height: calc(100vh - 50px);
3255+
max-height: calc(100vh - 47px);
32363256
height: 100%;
32373257
overflow-y: auto;
32383258
}
32393259

3260+
.diff-toggle-file-tree-button {
3261+
background: none;
3262+
border: none;
3263+
user-select: none;
3264+
color: inherit;
3265+
}
3266+
3267+
.diff-toggle-file-tree-button:hover {
3268+
color: var(--color-primary);
3269+
}
3270+
32403271
@media (max-width: 991px) {
32413272
#diff-file-tree {
32423273
display: none !important;

Diff for: web_src/css/review.css

+1-10
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@
6767
.comment-code-cloud {
6868
padding: 0.5rem 1rem !important;
6969
position: relative;
70-
margin: 0 auto;
71-
max-width: 1000px;
70+
max-width: 820px;
7271
}
7372

7473
@media (max-width: 767px) {
@@ -308,11 +307,3 @@ a.blob-excerpt:hover {
308307
width: 72px;
309308
height: 10px;
310309
}
311-
312-
.diff-file-box {
313-
border-radius: 0.285rem; /* Just like ui.top.attached.header */
314-
}
315-
316-
.diff-file-box:target {
317-
box-shadow: 0 0 0 3px var(--color-accent);
318-
}

Diff for: web_src/js/components/DiffFileTree.vue

+11-10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<script>
1717
import DiffFileTreeItem from './DiffFileTreeItem.vue';
1818
import {doLoadMoreFiles} from '../features/repo-diff.js';
19+
import {toggleElem} from '../utils/dom.js';
1920
2021
const {pageData} = window.config;
2122
const LOCAL_STORAGE_KEY = 'diff_file_tree_visible';
@@ -92,8 +93,6 @@ export default {
9293
}
9394
},
9495
mounted() {
95-
// ensure correct buttons when we are mounted to the dom
96-
this.adjustToggleButton(this.fileTreeIsVisible);
9796
// replace the pageData.diffFileInfo.files with our watched data so we get updates
9897
pageData.diffFileInfo.files = this.files;
9998
@@ -109,15 +108,17 @@ export default {
109108
updateVisibility(visible) {
110109
this.fileTreeIsVisible = visible;
111110
localStorage.setItem(LOCAL_STORAGE_KEY, this.fileTreeIsVisible);
112-
this.adjustToggleButton(this.fileTreeIsVisible);
111+
this.updateState(this.fileTreeIsVisible);
113112
},
114-
adjustToggleButton(visible) {
115-
const [toShow, toHide] = document.querySelectorAll('.diff-toggle-file-tree-button .icon');
116-
toShow.classList.toggle('gt-hidden', visible); // hide the toShow icon if the tree is visible
117-
toHide.classList.toggle('gt-hidden', !visible); // similarly
118-
119-
const diffTree = document.getElementById('diff-file-tree');
120-
diffTree.classList.toggle('gt-hidden', !visible);
113+
updateState(visible) {
114+
const btn = document.querySelector('.diff-toggle-file-tree-button');
115+
const [toShow, toHide] = btn.querySelectorAll('.icon');
116+
const tree = document.getElementById('diff-file-tree');
117+
const newTooltip = btn.getAttribute(visible ? 'data-hide-text' : 'data-show-text');
118+
btn.setAttribute('data-tooltip-content', newTooltip);
119+
toggleElem(tree, visible);
120+
toggleElem(toShow, !visible);
121+
toggleElem(toHide, visible);
121122
},
122123
loadMoreData() {
123124
this.isLoadingNewData = true;

Diff for: web_src/js/components/DiffFileTreeItem.vue

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<div v-show="show" :title="item.name">
33
<!--title instead of tooltip above as the tooltip needs too much work with the current methods, i.e. not being loaded or staying open for "too long"-->
4-
<div class="item" :class="item.isFile ? 'filewrapper gt-p-1' : ''">
4+
<div class="item" :class="item.isFile ? 'filewrapper gt-p-1 gt-ac' : ''">
55
<!-- Files -->
66
<SvgIcon
77
v-if="item.isFile"
@@ -10,7 +10,7 @@
1010
/>
1111
<a
1212
v-if="item.isFile"
13-
class="file gt-ellipsis muted"
13+
class="file gt-ellipsis"
1414
:href="item.isFile ? '#diff-' + item.file.NameHash : ''"
1515
>{{ item.name }}</a>
1616
<SvgIcon
@@ -20,7 +20,7 @@
2020
/>
2121

2222
<!-- Directories -->
23-
<div v-if="!item.isFile" class="directory gt-p-1" @click.stop="handleClick(item.isFile)">
23+
<div v-if="!item.isFile" class="directory gt-p-1 gt-ac" @click.stop="handleClick(item.isFile)">
2424
<SvgIcon
2525
class="svg-icon"
2626
:name="collapsed ? 'octicon-chevron-right' : 'octicon-chevron-down'"
@@ -79,31 +79,31 @@ export default {
7979
</script>
8080

8181
<style scoped>
82-
span.svg-icon.status {
82+
.svg-icon.status {
8383
float: right;
8484
}
8585
86-
span.svg-icon.file {
86+
.svg-icon.file {
8787
color: var(--color-secondary-dark-7);
8888
}
8989
90-
span.svg-icon.directory {
90+
.svg-icon.directory {
9191
color: var(--color-primary);
9292
}
9393
94-
span.svg-icon.octicon-diff-modified {
94+
.svg-icon.octicon-diff-modified {
9595
color: var(--color-yellow);
9696
}
9797
98-
span.svg-icon.octicon-diff-added {
98+
.svg-icon.octicon-diff-added {
9999
color: var(--color-green);
100100
}
101101
102-
span.svg-icon.octicon-diff-removed {
102+
.svg-icon.octicon-diff-removed {
103103
color: var(--color-red);
104104
}
105105
106-
span.svg-icon.octicon-diff-renamed {
106+
.svg-icon.octicon-diff-renamed {
107107
color: var(--color-teal);
108108
}
109109
@@ -139,9 +139,11 @@ div.list {
139139
140140
a {
141141
text-decoration: none;
142+
color: var(--color-text);
142143
}
143144
144145
a:hover {
145146
text-decoration: none;
147+
color: var(--color-text);
146148
}
147149
</style>

0 commit comments

Comments
 (0)