-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathDiffFileTreeItem.vue
181 lines (164 loc) · 5.4 KB
/
DiffFileTreeItem.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<script lang="ts" setup>
import {SvgIcon, type SvgName} from '../svg.ts';
import {diffTreeStore} from '../modules/stores.ts';
import {computed, nextTick, ref, watch} from 'vue';
import type {Item, DirItem, File, FileStatus} from '../utils/filetree.ts';
// ------------------------------------------------------------
// Component Props
// ------------------------------------------------------------
const props = defineProps<{
item: Item,
setViewed?:(val: boolean) => void,
}>();
// ------------------------------------------------------------
// Reactive State & Refs
// ------------------------------------------------------------
const store = diffTreeStore();
const count = ref(0);
let pendingUpdate = 0;
let pendingTimer: Promise<void> | undefined;
// ------------------------------------------------------------
// Batch Update Mechanism
// ------------------------------------------------------------
/**
* Handles batched updates to count value
* Prevents multiple updates within the same tick
*/
const setCount = (isViewed: boolean) => {
pendingUpdate += (isViewed ? 1 : -1);
if (pendingTimer === undefined) {
pendingTimer = nextTick(() => {
count.value = Math.max(0, count.value + pendingUpdate);
pendingUpdate = 0;
pendingTimer = undefined;
});
}
};
// ------------------------------------------------------------
// Computed Properties
// ------------------------------------------------------------
/**
* Determines viewed state based on item type:
* - Files: Directly use IsViewed property
* - Directories: Compare children count with viewed count
*/
const isViewed = computed(() => {
return props.item.isFile ? props.item.file.IsViewed : (props.item as DirItem).children.length === count.value;
});
// ------------------------------------------------------------
// Watchers & Side Effects
// ------------------------------------------------------------
/**
* Propagate viewed state changes to parent component
* Using post flush to ensure DOM updates are complete
*/
watch(
() => isViewed.value,
(newVal) => {
if (props.setViewed) {
props.setViewed(newVal);
}
},
{immediate: true, flush: 'post'},
);
// ------------------------------------------------------------
// Collapse Behavior Documentation
// ------------------------------------------------------------
/**
* Collapse behavior rules:
* - Viewed folders start collapsed initially
* - Manual expand/collapse takes precedence over automatic behavior
* - State persists after manual interaction
*/
const collapsed = ref(isViewed.value);
function getIconForDiffStatus(pType: FileStatus) {
const diffTypes: Record<FileStatus, { name: SvgName, classes: Array<string> }> = {
'added': {name: 'octicon-diff-added', classes: ['text', 'green']},
'modified': {name: 'octicon-diff-modified', classes: ['text', 'yellow']},
'deleted': {name: 'octicon-diff-removed', classes: ['text', 'red']},
'renamed': {name: 'octicon-diff-renamed', classes: ['text', 'teal']},
'copied': {name: 'octicon-diff-renamed', classes: ['text', 'green']},
'typechange': {name: 'octicon-diff-modified', classes: ['text', 'green']}, // there is no octicon for copied, so renamed should be ok
};
return diffTypes[pType];
}
function fileIcon(file: File) {
if (file.IsSubmodule) {
return 'octicon-file-submodule';
}
return 'octicon-file';
}
</script>
<template>
<!--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"-->
<a
v-if="item.isFile" class="item-file"
:class="{ 'selected': store.selectedItem === '#diff-' + item.file.NameHash, 'viewed': isViewed }"
:title="item.name" :href="'#diff-' + item.file.NameHash"
>
<!-- file -->
<SvgIcon :name="fileIcon(item.file)"/>
<span class="gt-ellipsis tw-flex-1">{{ item.name }}</span>
<SvgIcon
:name="getIconForDiffStatus(item.file.Status).name"
:class="getIconForDiffStatus(item.file.Status).classes"
/>
</a>
<template v-else-if="item.isFile === false">
<div class="item-directory" :class="{ 'viewed': isViewed }" :title="item.name" @click.stop="collapsed = !collapsed">
<!-- directory -->
<SvgIcon :name="collapsed ? 'octicon-chevron-right' : 'octicon-chevron-down'"/>
<SvgIcon
class="text primary"
:name="collapsed ? 'octicon-file-directory-fill' : 'octicon-file-directory-open-fill'"
/>
<span class="gt-ellipsis">{{ item.name }}</span>
</div>
<div v-show="!collapsed" class="sub-items">
<DiffFileTreeItem v-for="childItem in item.children" :key="childItem.name" :item="childItem" :set-viewed="setCount"/>
</div>
</template>
</template>
<style scoped>
a,
a:hover {
text-decoration: none;
color: var(--color-text);
}
.sub-items {
display: flex;
flex-direction: column;
gap: 1px;
margin-left: 13px;
border-left: 1px solid var(--color-secondary);
}
.sub-items .item-file {
padding-left: 18px;
}
.item-file.selected {
color: var(--color-text);
background: var(--color-active);
border-radius: 4px;
}
.item-file.viewed,
.item-directory.viewed {
color: var(--color-text-light-3);
}
.item-directory {
user-select: none;
}
.item-file,
.item-directory {
display: flex;
align-items: center;
gap: 0.25em;
padding: 6px;
}
.item-file:hover,
.item-directory:hover {
color: var(--color-text);
background: var(--color-hover);
border-radius: 4px;
cursor: pointer;
}
</style>