Skip to content

Commit

Permalink
fix(explorer): support filter object
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 30, 2024
1 parent b82f45b commit a3d9ac0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
21 changes: 14 additions & 7 deletions plugins/explorer/client/file-picker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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 '选择目录'
}
})

Expand All @@ -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)
}
})
}
})
Expand Down
6 changes: 3 additions & 3 deletions plugins/explorer/client/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions plugins/explorer/client/upload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion plugins/explorer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

0 comments on commit a3d9ac0

Please sign in to comment.