Skip to content

Commit 10f9399

Browse files
Highlight selected file in the PR file tree (#23947) (#24126)
backport #23947 by @yusifeng before ![before](https://user-images.githubusercontent.com/36984894/230327904-6e712ca2-f777-4cad-99f3-53bc20008180.gif) after ![after](https://user-images.githubusercontent.com/36984894/230327966-6e5dd971-f0df-427a-a80b-6a9b6db6065d.gif) Co-authored-by: yusifeng <36984894+yusifeng@users.noreply.github.com>
1 parent 67a73dd commit 10f9399

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

web_src/js/components/DiffFileTree.vue

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
>
66
<!-- only render the tree if we're visible. in many cases this is something that doesn't change very often -->
77
<div class="ui list">
8-
<DiffFileTreeItem v-for="item in fileTree" :key="item.name" :item="item" />
8+
<DiffFileTreeItem v-for="item in fileTree" :key="item.name" :item="item" :selected-file="selectedFile"/>
99
</div>
1010
<div v-if="isIncomplete" id="diff-too-many-files-stats" class="gt-pt-2">
1111
<span class="gt-mr-2">{{ tooManyFilesMessage }}</span><a :class="['ui', 'basic', 'tiny', 'button', isLoadingNewData === true ? 'disabled' : '']" id="diff-show-more-files-stats" @click.stop="loadMoreData">{{ showMoreMessage }}</a>
@@ -25,7 +25,10 @@ export default {
2525
data: () => {
2626
const fileTreeIsVisible = localStorage.getItem(LOCAL_STORAGE_KEY) === 'true';
2727
pageData.diffFileInfo.fileTreeIsVisible = fileTreeIsVisible;
28-
return pageData.diffFileInfo;
28+
return {
29+
...pageData.diffFileInfo,
30+
selectedFile: ''
31+
};
2932
},
3033
computed: {
3134
fileTree() {
@@ -98,9 +101,16 @@ export default {
98101
pageData.diffFileInfo.files = this.files;
99102
100103
document.querySelector('.diff-toggle-file-tree-button').addEventListener('click', this.toggleVisibility);
104+
105+
this.hashChangeListener = () => {
106+
this.selectedFile = window.location.hash;
107+
};
108+
this.hashListener = window.addEventListener('hashchange', this.hashChangeListener);
109+
this.selectedFile = window.location.hash;
101110
},
102111
unmounted() {
103112
document.querySelector('.diff-toggle-file-tree-button').removeEventListener('click', this.toggleVisibility);
113+
window.removeEventListener('hashchange', this.hashChangeListener);
104114
},
105115
methods: {
106116
toggleVisibility() {

web_src/js/components/DiffFileTreeItem.vue

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<div v-show="show" class="tooltip" :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' : '', selectedFile === genCompleteFileHash(item.file?.NameHash) ? 'selected' : '']">
55
<!-- Files -->
66
<SvgIcon
77
v-if="item.isFile"
@@ -34,7 +34,7 @@
3434
<span class="gt-ellipsis">{{ item.name }}</span>
3535
</div>
3636
<div v-show="!collapsed">
37-
<DiffFileTreeItem v-for="childItem in item.children" :key="childItem.name" :item="childItem" class="list" />
37+
<DiffFileTreeItem v-for="childItem in item.children" :key="childItem.name" :item="childItem" class="list" :selected-file="selectedFile"/>
3838
</div>
3939
</div>
4040
</div>
@@ -54,6 +54,11 @@ export default {
5454
type: Boolean,
5555
required: false,
5656
default: true
57+
},
58+
selectedFile: {
59+
type: String,
60+
default: '',
61+
required: true
5762
}
5863
},
5964
data: () => ({
@@ -76,6 +81,9 @@ export default {
7681
};
7782
return diffTypes[pType];
7883
},
84+
genCompleteFileHash(hash) {
85+
return `#diff-${hash}`;
86+
}
7987
},
8088
};
8189
</script>
@@ -115,25 +123,25 @@ span.svg-icon.octicon-diff-renamed {
115123
padding-left: 18px !important;
116124
}
117125
118-
.item.filewrapper:hover {
126+
.item.filewrapper:hover, div.directory:hover {
119127
color: var(--color-text);
120128
background: var(--color-hover);
121129
border-radius: 4px;
122130
}
123131
132+
.item.filewrapper.selected {
133+
color: var(--color-text);
134+
background: var(--color-active);
135+
border-radius: 4px;
136+
}
137+
124138
div.directory {
125139
display: grid;
126140
grid-template-columns: 18px 20px auto;
127141
user-select: none;
128142
cursor: pointer;
129143
}
130144
131-
div.directory:hover {
132-
color: var(--color-text);
133-
background: var(--color-hover);
134-
border-radius: 4px;
135-
}
136-
137145
div.list {
138146
padding-bottom: 0 !important;
139147
padding-top: inherit !important;

0 commit comments

Comments
 (0)