|
| 1 | +<script setup lang="ts"> |
| 2 | +import { computed, onMounted, onUnmounted, ref } from 'vue' |
| 3 | +import dayjs from 'dayjs' |
| 4 | +
|
| 5 | +import type { Commit } from '@/git' |
| 6 | +import { WEBVIEW_CHANNEL } from '@/constant' |
| 7 | +
|
| 8 | +const props = defineProps<{ |
| 9 | + commits: Commit[] |
| 10 | +}>() |
| 11 | +
|
| 12 | +// interface CommitNode { |
| 13 | +// commit: Commit |
| 14 | +// column: number |
| 15 | +// parents: { hash: string, fromColumn: number, toColumn: number }[] |
| 16 | +// color: string |
| 17 | +// branchName?: string |
| 18 | +// isHead?: boolean |
| 19 | +// isBranch?: boolean |
| 20 | +// isMerge?: boolean |
| 21 | +// } |
| 22 | +
|
| 23 | +const ITEMS_PER_PAGE = 45 |
| 24 | +const currentPage = ref(1) |
| 25 | +const observer = ref<IntersectionObserver | null>(null) |
| 26 | +const loadingTriggerRef = ref<HTMLElement | null>(null) |
| 27 | +
|
| 28 | +function handleCommitClick(commit: Commit & { date: string }) { |
| 29 | + try { |
| 30 | + if (window.vscode) { |
| 31 | + window.vscode.postMessage({ |
| 32 | + command: WEBVIEW_CHANNEL.SHOW_COMMIT_DETAILS, |
| 33 | + commitHash: commit.hash, |
| 34 | + }) |
| 35 | + } |
| 36 | + } |
| 37 | + catch (error) { |
| 38 | + console.error('Error sending commit details:', error) |
| 39 | + } |
| 40 | +} |
| 41 | +
|
| 42 | +// Calculate branch positions and paths |
| 43 | +// const COLUMN_WIDTH = 14 |
| 44 | +// const DOT_SIZE = 6 |
| 45 | +// const BRANCH_COLORS = [ |
| 46 | +// 'var(--vscode-charts-blue)', |
| 47 | +// 'var(--vscode-charts-red)', |
| 48 | +// 'var(--vscode-charts-yellow)', |
| 49 | +// 'var(--vscode-charts-orange)', |
| 50 | +// 'var(--vscode-charts-purple)', |
| 51 | +// 'var(--vscode-charts-green)', |
| 52 | +// ] |
| 53 | +
|
| 54 | +const graphData = computed(() => { |
| 55 | + return (props.commits || []).map(commit => ({ |
| 56 | + ...commit, |
| 57 | + date: dayjs(commit.date).format('YYYY-MM-DD HH:mm'), |
| 58 | + })) |
| 59 | +}) |
| 60 | +
|
| 61 | +const visibleCommits = computed(() => { |
| 62 | + const end = currentPage.value * ITEMS_PER_PAGE |
| 63 | + return graphData.value.slice(0, end) |
| 64 | +}) |
| 65 | +
|
| 66 | +onMounted(() => { |
| 67 | + observer.value = new IntersectionObserver((entries) => { |
| 68 | + const target = entries[0] |
| 69 | + if (target.isIntersecting && currentPage.value * ITEMS_PER_PAGE < graphData.value.length) { |
| 70 | + currentPage.value++ |
| 71 | + } |
| 72 | + }) |
| 73 | +
|
| 74 | + if (loadingTriggerRef.value) { |
| 75 | + observer.value.observe(loadingTriggerRef.value) |
| 76 | + } |
| 77 | +}) |
| 78 | +
|
| 79 | +onUnmounted(() => { |
| 80 | + if (observer.value) { |
| 81 | + observer.value.disconnect() |
| 82 | + } |
| 83 | +}) |
| 84 | +</script> |
| 85 | + |
| 86 | +<template> |
| 87 | + <div class="git-graph"> |
| 88 | + <table> |
| 89 | + <thead> |
| 90 | + <tr> |
| 91 | + <th class="hash-col"> |
| 92 | + CommitId |
| 93 | + </th> |
| 94 | + <th class="message-col"> |
| 95 | + Message |
| 96 | + </th> |
| 97 | + <th class="stats-col"> |
| 98 | + Changes |
| 99 | + </th> |
| 100 | + <th>Author</th> |
| 101 | + <th>Date</th> |
| 102 | + </tr> |
| 103 | + </thead> |
| 104 | + <tbody> |
| 105 | + <tr |
| 106 | + v-for="commit in visibleCommits" |
| 107 | + :key="commit.hash" |
| 108 | + class="commit-row" |
| 109 | + @click="handleCommitClick(commit)" |
| 110 | + > |
| 111 | + <td class="hash-col"> |
| 112 | + {{ commit.hash.substring(0, 7) }} |
| 113 | + </td> |
| 114 | + <td class="message-col"> |
| 115 | + {{ commit.message }} |
| 116 | + </td> |
| 117 | + <td class="stats-col"> |
| 118 | + <span v-if="commit.stats" class="commit-stats"> |
| 119 | + <span class="files">{{ commit.stats.files }} files</span> |
| 120 | + <span v-if="commit.stats.additions" class="additions">+{{ commit.stats.additions }}</span> |
| 121 | + <span v-if="commit.stats.deletions" class="deletions">-{{ commit.stats.deletions }}</span> |
| 122 | + </span> |
| 123 | + </td> |
| 124 | + <td class="author"> |
| 125 | + {{ commit.authorName }} |
| 126 | + </td> |
| 127 | + <td class="date"> |
| 128 | + {{ commit.date }} |
| 129 | + </td> |
| 130 | + </tr> |
| 131 | + <tr ref="loadingTriggerRef" class="loading-trigger"> |
| 132 | + <td colspan="5" class="loading-cell"> |
| 133 | + <div v-if="visibleCommits.length < graphData.length" class="loading-text"> |
| 134 | + Loading more commits... |
| 135 | + </div> |
| 136 | + </td> |
| 137 | + </tr> |
| 138 | + </tbody> |
| 139 | + </table> |
| 140 | + </div> |
| 141 | +</template> |
| 142 | + |
| 143 | +<style scoped> |
| 144 | +.git-graph { |
| 145 | + width: 100%; |
| 146 | + overflow: auto; |
| 147 | + font-family: var(--vscode-editor-font-family); |
| 148 | +} |
| 149 | +
|
| 150 | +table { |
| 151 | + width: 100%; |
| 152 | + border-collapse: collapse; |
| 153 | + font-size: var(--vscode-font-size); |
| 154 | + color: var(--vscode-foreground); |
| 155 | +} |
| 156 | +
|
| 157 | +th { |
| 158 | + position: sticky; |
| 159 | + top: 0; |
| 160 | + z-index: 1; |
| 161 | + text-align: left; |
| 162 | + border-bottom: 1px solid var(--vscode-panel-border); |
| 163 | + background-color: var(--vscode-sideBar-background); |
| 164 | +} |
| 165 | +
|
| 166 | +td { |
| 167 | + padding: 4px 8px; |
| 168 | + border-bottom: 1px solid var(--vscode-panel-border); |
| 169 | + vertical-align: middle; |
| 170 | +} |
| 171 | +
|
| 172 | +.graph-col { |
| 173 | + padding: 0; |
| 174 | +} |
| 175 | +
|
| 176 | +.hash-col { |
| 177 | + width: 80px; |
| 178 | + padding: 4px 8px; |
| 179 | + white-space: nowrap; |
| 180 | + cursor: pointer; |
| 181 | + color: var(--vscode-gitDecoration-addedResourceForeground); |
| 182 | +} |
| 183 | +
|
| 184 | +td.hash-col:hover { |
| 185 | + text-decoration: underline; |
| 186 | +} |
| 187 | +
|
| 188 | +.message-col { |
| 189 | + min-width: 200px; |
| 190 | + max-width: 400px; |
| 191 | + text-overflow: ellipsis; |
| 192 | + overflow: hidden; |
| 193 | + white-space: nowrap; |
| 194 | +} |
| 195 | +
|
| 196 | +.author-col { |
| 197 | + width: 120px; |
| 198 | +} |
| 199 | +
|
| 200 | +.date-col { |
| 201 | + width: 150px; |
| 202 | +} |
| 203 | +
|
| 204 | +.commit-stats { |
| 205 | + display: flex; |
| 206 | + gap: 8px; |
| 207 | + font-size: 12px; |
| 208 | +} |
| 209 | +
|
| 210 | +.additions { |
| 211 | + color: var(--vscode-gitDecoration-addedResourceForeground); |
| 212 | +} |
| 213 | +
|
| 214 | +.deletions { |
| 215 | + color: var(--vscode-gitDecoration-deletedResourceForeground); |
| 216 | +} |
| 217 | +
|
| 218 | +.author { |
| 219 | + white-space: nowrap; |
| 220 | +} |
| 221 | +
|
| 222 | +.date { |
| 223 | + white-space: nowrap; |
| 224 | + color: var(--vscode-descriptionForeground); |
| 225 | +} |
| 226 | +
|
| 227 | +tr:hover { |
| 228 | + background-color: var(--vscode-list-hoverBackground); |
| 229 | +} |
| 230 | +
|
| 231 | +.loading-cell { |
| 232 | + text-align: center; |
| 233 | + padding: 8px; |
| 234 | + color: var(--vscode-descriptionForeground); |
| 235 | +} |
| 236 | +
|
| 237 | +.loading-text { |
| 238 | + font-size: 12px; |
| 239 | +} |
| 240 | +
|
| 241 | +/* Ensure the graph lines remain visible when hovering */ |
| 242 | +tr:hover .commit-line { |
| 243 | + opacity: 0.8; |
| 244 | +} |
| 245 | +</style> |
0 commit comments