Skip to content

Commit

Permalink
refactor: 消息总线
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Oct 23, 2024
1 parent 6050bf6 commit f760dba
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 67 deletions.
1 change: 1 addition & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"lodash-es": "^4.17.21",
"luxon": "^3.5.0",
"marked": "^14.1.2",
"mitt": "^3.0.1",
"pinia": "^2.2.4",
"pinia-plugin-persistedstate": "^4.1.1",
"remove": "^0.1.5",
Expand Down
8 changes: 8 additions & 0 deletions web/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions web/src/utils/common/naiveTools.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useThemeStore } from '@/store'
import mitt from 'mitt'
import * as NaiveUI from 'naive-ui'

export async function setupNaiveDiscreteApi() {
Expand All @@ -16,4 +17,5 @@ export async function setupNaiveDiscreteApi() {
window.$notification = notification
window.$message = message
window.$dialog = dialog
window.$bus = mitt()
}
35 changes: 0 additions & 35 deletions web/src/utils/event/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion web/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export * from './auth'
export * from './common'
export * from './event'
export * from './file'
export * from './http'
export * from './storage'
3 changes: 1 addition & 2 deletions web/src/views/file/CompressModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { NButton, NInput } from 'naive-ui'
import api from '@/api/panel/file'
import { generateRandomString, getBase } from '@/utils'
import EventBus from '@/utils/event'
const show = defineModel<boolean>('show', { type: Boolean, required: true })
const path = defineModel<string>('path', { type: String, required: true })
Expand Down Expand Up @@ -43,7 +42,7 @@ const handleArchive = async () => {
})
message?.destroy()
loading.value = false
EventBus.emit('file:refresh')
window.$bus.emit('file:refresh')
}
onMounted(() => {
Expand Down
15 changes: 7 additions & 8 deletions web/src/views/file/ListTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type { RowData } from 'naive-ui/es/data-table/src/interface'
import file from '@/api/panel/file'
import TheIcon from '@/components/custom/TheIcon.vue'
import EventBus from '@/utils/event'
import { checkName, checkPath, getExt, getFilename, getIconByExt, isCompress } from '@/utils/file'
import EditModal from '@/views/file/EditModal.vue'
import type { Marked } from '@/views/file/types'
Expand Down Expand Up @@ -233,7 +232,7 @@ const columns: DataTableColumns<RowData> = [
onPositiveClick: () => {
file.delete(row.full).then(() => {
window.$message.success('删除成功')
EventBus.emit('file:refresh')
window.$bus.emit('file:refresh')
})
},
onNegativeClick: () => {}
Expand Down Expand Up @@ -389,7 +388,7 @@ const handleRename = () => {
file.move(source, target).then(() => {
window.$message.success('重命名成功')
renameModal.value = false
EventBus.emit('file:refresh')
window.$bus.emit('file:refresh')
})
}
Expand All @@ -411,7 +410,7 @@ const handleUnCompress = () => {
message?.destroy()
window.$message.success('解压成功')
unCompressModal.value = false
EventBus.emit('file:refresh')
window.$bus.emit('file:refresh')
})
.catch(() => {
message?.destroy()
Expand Down Expand Up @@ -476,7 +475,7 @@ const handleSelect = (key: string) => {
case 'delete':
file.delete(selectedRow.value.full).then(() => {
window.$message.success('删除成功')
EventBus.emit('file:refresh')
window.$bus.emit('file:refresh')
})
break
}
Expand Down Expand Up @@ -517,15 +516,15 @@ onMounted(() => {
path,
() => {
handlePageChange(1)
EventBus.emit('push-history', path.value)
window.$bus.emit('push-history', path.value)
},
{ immediate: true }
)
EventBus.on('file:refresh', handleRefresh)
window.$bus.on('file:refresh', handleRefresh)
})
onUnmounted(() => {
EventBus.off('file:refresh', handleRefresh)
window.$bus.off('file:refresh', handleRefresh)
})
</script>

Expand Down
14 changes: 7 additions & 7 deletions web/src/views/file/PathInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import type { InputInst } from 'naive-ui'
import { onUnmounted } from 'vue'
import EventBus from '@/utils/event'
import { checkPath } from '@/utils/file'
import SearchModal from '@/views/file/SearchModal.vue'
const path = defineModel<string>('path', { type: String, required: true })
const isInput = ref(false)
Expand All @@ -13,7 +13,7 @@ const input = ref('www')
const history: string[] = []
let current = -1
const searchModal = ref(false)
const searchShow = ref(false)
const search = ref({
keyword: '',
sub: false
Expand All @@ -38,7 +38,7 @@ const handleBlur = () => {
}
const handleRefresh = () => {
EventBus.emit('file:refresh')
window.$bus.emit('file:refresh')
}
const handleUp = () => {
Expand Down Expand Up @@ -90,7 +90,7 @@ const handlePushHistory = (path: string) => {
}
const handleSearch = () => {
searchModal.value = true
searchShow.value = true
}
watch(
Expand All @@ -102,11 +102,11 @@ watch(
)
onMounted(() => {
EventBus.on('push-history', handlePushHistory)
window.$bus.on('push-history', handlePushHistory)
})
onUnmounted(() => {
EventBus.off('push-history', handlePushHistory)
window.$bus.off('push-history', handlePushHistory)
})
</script>

Expand Down Expand Up @@ -158,7 +158,7 @@ onUnmounted(() => {
</n-input-group>
</n-flex>
<search-modal
v-model:show="searchModal"
v-model:show="searchShow"
v-model:path="path"
v-model:keyword="search.keyword"
v-model:sub="search.sub"
Expand Down
3 changes: 1 addition & 2 deletions web/src/views/file/PermissionModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import { NButton, NInput } from 'naive-ui'
import file from '@/api/panel/file'
import EventBus from '@/utils/event'
const show = defineModel<boolean>('show', { type: Boolean, required: true })
const selected = defineModel<string[]>('selected', { type: Array, required: true })
Expand All @@ -29,7 +28,7 @@ const handlePermission = async () => {
window.$message.error(`修改 ${path} 失败`)
})
}
EventBus.emit('file:refresh')
window.$bus.emit('file:refresh')
}
const calculateOctal = (permissions: string[]) => {
Expand Down
3 changes: 1 addition & 2 deletions web/src/views/file/SearchModal.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script setup lang="ts">
import file from '@/api/panel/file'
import EventBus from '@/utils/event'
import { NButton, NPopconfirm, NSpace, NTag } from 'naive-ui'
import type { DataTableColumns } from 'naive-ui'
Expand Down Expand Up @@ -75,7 +74,7 @@ const columns: DataTableColumns<RowData> = [
onPositiveClick: () => {
file.delete(row.full).then(() => {
window.$message.success('删除成功')
EventBus.emit('file:refresh')
window.$bus.emit('file:refresh')
})
},
onNegativeClick: () => {}
Expand Down
13 changes: 5 additions & 8 deletions web/src/views/file/ToolBar.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
<script setup lang="ts">
import { NButton, NSpace } from 'naive-ui'
import file from '@/api/panel/file'
import EventBus from '@/utils/event'
import { checkName, lastDirectory } from '@/utils/file'
import UploadModal from '@/views/file/UploadModal.vue'
import type { Marked } from '@/views/file/types'
Expand Down Expand Up @@ -41,7 +38,7 @@ const handleCreate = () => {
file.create(fullPath, createModel.value.dir).then(() => {
create.value = false
window.$message.success('创建成功')
EventBus.emit('file:refresh')
window.$bus.emit('file:refresh')
})
}
Expand All @@ -56,7 +53,7 @@ const handleDownload = () => {
.then(() => {
download.value = false
window.$message.success('下载任务创建成功')
EventBus.emit('file:refresh')
window.$bus.emit('file:refresh')
})
}
Expand Down Expand Up @@ -97,12 +94,12 @@ const handlePaste = async () => {
if (type === 'copy') {
await file.copy(source, target).then(() => {
window.$message.success(`复制 ${source} 到 ${target} 成功`)
EventBus.emit('file:refresh')
window.$bus.emit('file:refresh')
})
} else {
await file.move(source, target).then(() => {
window.$message.success(`移动 ${source} 到 ${target} 成功`)
EventBus.emit('file:refresh')
window.$bus.emit('file:refresh')
})
}
}
Expand All @@ -119,7 +116,7 @@ const bulkDelete = () => {
for (const path of selected.value) {
file.delete(path).then(() => {
window.$message.success(`删除 ${path} 成功`)
EventBus.emit('file:refresh')
window.$bus.emit('file:refresh')
})
}
}
Expand Down
3 changes: 1 addition & 2 deletions web/src/views/file/UploadModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import type { UploadCustomRequestOptions } from 'naive-ui'
import api from '@/api/panel/file'
import EventBus from '@/utils/event'
const show = defineModel<boolean>('show', { type: Boolean, required: true })
const path = defineModel<string>('path', { type: String, required: true })
Expand All @@ -15,7 +14,7 @@ const uploadRequest = ({ file, onFinish, onError, onProgress }: UploadCustomRequ
.upload(`${path.value}/${file.name}`, formData, onProgress)
.then(() => {
window.$message.success(`上传 ${file.name} 成功`)
EventBus.emit('file:refresh')
window.$bus.emit('file:refresh')
onFinish()
})
.catch(() => {
Expand Down
1 change: 1 addition & 0 deletions web/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ interface Window {
$dialog: import('naive-ui').DialogProviderInst
$message: import('naive-ui').MessageProviderInst
$notification: import('naive-ui').NotificationProviderInst
$bus: import('mitt').Emitter
}

0 comments on commit f760dba

Please sign in to comment.