From a3d9ac0833493f886f0e817e0509f26aa80fe728 Mon Sep 17 00:00:00 2001 From: Shigma Date: Thu, 30 May 2024 17:02:23 +0800 Subject: [PATCH] fix(explorer): support filter object --- plugins/explorer/client/file-picker.vue | 21 ++++++++++++++------- plugins/explorer/client/index.vue | 6 +++--- plugins/explorer/client/upload.vue | 4 ++-- plugins/explorer/package.json | 3 ++- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/plugins/explorer/client/file-picker.vue b/plugins/explorer/client/file-picker.vue index b1dfa891..831bbfd8 100644 --- a/plugins/explorer/client/file-picker.vue +++ b/plugins/explorer/client/file-picker.vue @@ -73,10 +73,12 @@ const allowDir = computed(() => options.value.filters.includes('directory')) const allowFile = computed(() => options.value.filters.some(x => x !== 'directory')) const hint = computed(() => { - if (allowDir.value) { - return options.value.filters.length === 1 ? '选择目录' : '选择目录或文件' - } else { + if (!allowDir.value) { return '选择文件' + } else if (allowFile.value) { + return '选择目录或文件' + } else { + return '选择目录' } }) @@ -89,11 +91,16 @@ const entries = computed(() => { return children.filter((entry) => { if (entry.type === 'directory') return true if (entry.type === 'file' || entry.type === 'symlink') { - if (filters.includes('file')) return true + const index = entry.name.lastIndexOf('.') + const ext = index === -1 ? '' : entry.name.slice(index) return filters.some((filter) => { - const index = entry.name.lastIndexOf('.') - const ext = index === -1 ? '' : entry.name.slice(index) - return filter === ext + if (filter === 'directory') return false + if (filter === 'file') return true + if (typeof filter === 'string') { + return filter === ext + } else { + return filter.extensions.includes(ext) + } }) } }) diff --git a/plugins/explorer/client/index.vue b/plugins/explorer/client/index.vue index 4730acf0..7ef831a2 100644 --- a/plugins/explorer/client/index.vue +++ b/plugins/explorer/client/index.vue @@ -82,7 +82,7 @@ import { ref, computed, watch, onActivated, nextTick } from 'vue' import { useRoute, useRouter } from 'vue-router' import { useElementSize } from '@vueuse/core' -import { base64ToArrayBuffer, send, store, useColorMode, useContext, useMenu } from '@koishijs/client' +import { Binary, send, store, useColorMode, useContext, useMenu } from '@koishijs/client' import { Entry } from '@koishijs/plugin-explorer' import { files, TreeEntry, uploading, vFocus } from './store' import { model } from './editor' @@ -323,7 +323,7 @@ watch(() => files[active.value], async (entry) => { if (mime) { entry.oldValue = entry.newValue = `data:${mime};base64,${base64}` } else { - entry.oldValue = entry.newValue = new TextDecoder().decode(base64ToArrayBuffer(base64)) + entry.oldValue = entry.newValue = new TextDecoder().decode(Binary.fromBase64(base64)) } } model.setValue(entry.newValue) @@ -374,7 +374,7 @@ onActivated(async () => { async function downloadFile(filename: string) { const { base64 } = await send('explorer/read', filename) - const blob = new Blob([base64ToArrayBuffer(base64)]) + const blob = new Blob([Binary.fromBase64(base64)]) const url = URL.createObjectURL(blob) const a = document.createElement("a") a.href = url diff --git a/plugins/explorer/client/upload.vue b/plugins/explorer/client/upload.vue index 1d4ca96f..d165831c 100644 --- a/plugins/explorer/client/upload.vue +++ b/plugins/explorer/client/upload.vue @@ -13,7 +13,7 @@ import { computed } from 'vue' import { useEventListener } from '@vueuse/core' -import { arrayBufferToBase64, send } from '@koishijs/client' +import { Binary, send } from '@koishijs/client' import { uploading } from './store' const showUploading = computed({ @@ -29,7 +29,7 @@ function handleDataTransfer(event: Event, transfer: DataTransfer) { const file = item.getAsFile() const reader = new FileReader() reader.addEventListener('load', function () { - send('explorer/write', prefix + file.name, arrayBufferToBase64(reader.result as ArrayBuffer), true) + send('explorer/write', prefix + file.name, Binary.toBase64(reader.result as ArrayBuffer), true) }, false) reader.readAsArrayBuffer(file) } diff --git a/plugins/explorer/package.json b/plugins/explorer/package.json index 234ca8e5..255fbea2 100644 --- a/plugins/explorer/package.json +++ b/plugins/explorer/package.json @@ -71,7 +71,8 @@ "@koishijs/console": "^5.28.0", "anymatch": "^3.1.3", "chardet": "^2.0.0", - "chokidar": "^3.5.3", + "chokidar": "^3.6.0", + "file-type": "^16.5.4", "throttle-debounce": "^3.0.1" } }