feat: 重构 TemplateTree 组件的 v-model 绑定,添加 Snackbar 以显示错误消息

This commit is contained in:
zhanggy committed 2025-10-28 11:18:06 +08:00
1 parent 87ce5cb24f
commit 6431049d05
2 files changed
+42 -22

No files matched your search

+9 -12
View File
@@ -152,7 +152,6 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { useVModel } from '@vueuse/core'
interface TreeItem { interface TreeItem {
id: string id: string
@@ -166,8 +165,6 @@ interface Props {
height?: number height?: number
readOnly?: boolean readOnly?: boolean
emptyText?: string emptyText?: string
expanded?: string[]
showContextMenuVar?: boolean
contextMenuX?: number contextMenuX?: number
contextMenuY?: number contextMenuY?: number
currentContextItem?: TreeItem | null currentContextItem?: TreeItem | null
@@ -177,27 +174,27 @@ const props = withDefaults(defineProps<Props>(), {
height: 600, height: 600,
readOnly: false, readOnly: false,
emptyText: '暂无文件或目录', emptyText: '暂无文件或目录',
expanded: () => [],
showContextMenuVar: false,
contextMenuX: 0, contextMenuX: 0,
contextMenuY: 0, contextMenuY: 0,
currentContextItem: null currentContextItem: null
}) })
const localExpanded = defineModel<string[]>('expanded', {
default: () => []
})
const localShowContextMenuVar = defineModel<boolean>('showContextMenuVar', {
default: false
})
const emit = defineEmits([ const emit = defineEmits([
'showContextMenu', 'showContextMenu',
'addNewItemToCurrentDirectory', 'addNewItemToCurrentDirectory',
'editFile', 'editFile',
'deleteItem', 'deleteItem',
'addDirectory', 'addDirectory',
'addFile', 'addFile'
'update:showContextMenuVar',
'update:expanded'
]) ])
// Use useVModel to create two-way binding for props
const localShowContextMenuVar = useVModel(props, 'showContextMenuVar', emit)
const localExpanded = useVModel(props, 'expanded', emit)
</script> </script>
<style scoped> <style scoped>
+33 -10
View File
@@ -141,8 +141,8 @@
<TemplateTree <TemplateTree
:items="treeItems" :items="treeItems"
:height="treeHeight" :height="treeHeight"
:expanded="expandedNodes" v-model:expanded="expandedNodes"
:show-context-menu-var="showContextMenuVar" v-model:showContextMenuVar="showContextMenuVar"
:context-menu-x="contextMenuX" :context-menu-x="contextMenuX"
:context-menu-y="contextMenuY" :context-menu-y="contextMenuY"
:current-context-item="currentContextItem" :current-context-item="currentContextItem"
@@ -154,8 +154,6 @@
@delete-item="deleteItem" @delete-item="deleteItem"
@add-directory="addDirectory" @add-directory="addDirectory"
@add-file="addFile" @add-file="addFile"
@update:showContextMenuVar="handleContextMenuUpdate"
@update:expanded="expandedNodes = $event"
/> />
</v-col> </v-col>
</v-row> </v-row>
@@ -270,6 +268,24 @@
</v-card-text> </v-card-text>
</v-card> </v-card>
</v-dialog> </v-dialog>
<!-- Snackbar for error messages -->
<v-snackbar
v-model="snackbarVisible"
:color="snackbarColor"
:timeout="2000"
location="top"
>
{{ snackbarMessage }}
<template #actions>
<v-btn
icon
@click="snackbarVisible = false"
>
<v-icon>mdi-close</v-icon>
</v-btn>
</template>
</v-snackbar>
</v-container> </v-container>
</template> </template>
@@ -310,10 +326,10 @@ const contextMenuX = ref(0)
const contextMenuY = ref(0) const contextMenuY = ref(0)
const currentContextItem = ref<TreeItem | null>(null) const currentContextItem = ref<TreeItem | null>(null)
// Handler for context menu visibility updates from TemplateTree component // Snackbar variables
const handleContextMenuUpdate = (value: boolean) => { const snackbarVisible = ref(false)
showContextMenuVar.value = value const snackbarMessage = ref('')
} const snackbarColor = ref('error') // Can be 'error', 'success', 'warning', etc.
// New item state // New item state
const newDirectoryPath = ref('') const newDirectoryPath = ref('')
@@ -470,7 +486,7 @@ const confirmAddDirectory = () => {
(d) => d.path === newDirectoryPath.value (d) => d.path === newDirectoryPath.value
) )
if (exists) { if (exists) {
alert('该目录路径已存在!') showSnackbar('该目录路径已存在!')
return return
} }
@@ -489,7 +505,7 @@ const confirmAddFile = () => {
// Check for duplicates // Check for duplicates
const exists = files.value.some((f) => f.path === newFilePath.value) const exists = files.value.some((f) => f.path === newFilePath.value)
if (exists) { if (exists) {
alert('该文件路径已存在!') showSnackbar('该文件路径已存在!')
return return
} }
@@ -563,6 +579,13 @@ const deleteItem = (item: TreeItem) => {
} }
} }
// Show snackbar message
const showSnackbar = (message: string, color: string = 'error') => {
snackbarMessage.value = message
snackbarColor.value = color
snackbarVisible.value = true
}
// Auto focus methods // Auto focus methods
const autoFocusDirectoryInput = async () => { const autoFocusDirectoryInput = async () => {
await nextTick() await nextTick()