CompanyFileNode.vue
2.7 KB
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
<template>
<div>
<div
class="file-node"
:class="{ selected: node.type === 'file' && selected === node.path, directory: node.type === 'directory' }"
:style="{ paddingLeft: depth * 16 + 8 + 'px' }"
@click="handleClick"
>
<span class="node-icon">{{ node.type === 'directory' ? (expanded ? '📂' : '📁') : fileIcon(node.name) }}</span>
<span class="node-name">{{ node.name }}</span>
<span v-if="node.type === 'directory'" class="node-count">{{ node.fileCount }}</span>
<span v-if="node.type === 'file' && node.size" class="node-size">{{ formatSize(node.size) }}</span>
</div>
<template v-if="node.type === 'directory' && expanded">
<CompanyFileNode
v-for="child in node.children"
:key="child.path"
:node="child"
:depth="depth + 1"
:selected="selected"
@select="$emit('select', $event)"
/>
</template>
</div>
</template>
<script setup>
import { ref } from 'vue'
const props = defineProps({
node: { type: Object, required: true },
depth: { type: Number, default: 0 },
selected: { type: String, default: null },
})
const emit = defineEmits(['select'])
const expanded = ref(props.depth < 1) // 自动展开第一层
function handleClick() {
if (props.node.type === 'directory') {
expanded.value = !expanded.value
} else {
emit('select', props.node.path)
}
}
function fileIcon(name) {
if (!name) return '📄'
const ext = name.split('.').pop()?.toLowerCase()
const icons = {
js: '🟨', ts: '🔷', vue: '💚', json: '📋', md: '📝',
py: '🐍', sh: '⚙️', css: '🎨', html: '🌐', log: '📜',
txt: '📄', yaml: '📐', yml: '📐', toml: '📐',
}
return icons[ext] || '📄'
}
function formatSize(bytes) {
if (bytes < 1024) return `${bytes}B`
if (bytes < 1048576) return `${(bytes / 1024).toFixed(0)}K`
return `${(bytes / 1048576).toFixed(1)}M`
}
</script>
<style scoped>
.file-node {
display: flex; align-items: center; gap: 6px;
padding: 4px 8px; cursor: pointer; border-radius: 5px;
font-size: 12px; color: var(--text-secondary); transition: all 0.1s;
user-select: none;
}
.file-node:hover { background: var(--bg-surface-hover); }
.file-node.selected { background: var(--accent-bg); color: var(--accent-light); }
.file-node.directory { font-weight: 600; color: var(--text-primary); }
.node-icon { font-size: 13px; flex-shrink: 0; width: 18px; text-align: center; }
.node-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.node-count {
font-size: 9px; padding: 1px 5px; border-radius: 8px;
background: var(--accent-bg); color: var(--accent); font-weight: 600;
}
.node-size { font-size: 9px; color: var(--text-faint); }
</style>