Initial commit

This commit is contained in:
meswork
2026-06-01 17:55:16 +08:00
commit 9cfb8d16f0
33 changed files with 27762 additions and 0 deletions

57
src/utils/filePicker.js Normal file
View File

@@ -0,0 +1,57 @@
export function isAbortError(error) {
return error && error.name === 'AbortError'
}
export function isSavePickerSupported() {
return typeof window !== 'undefined' && typeof window.showSaveFilePicker === 'function'
}
export function isOpenPickerSupported() {
return typeof window !== 'undefined' && typeof window.showOpenFilePicker === 'function'
}
function downloadBlob(blob, filename) {
const url = URL.createObjectURL(blob)
const link = document.createElement('a')
link.href = url
link.download = filename
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
URL.revokeObjectURL(url)
}
export async function saveBlob(blob, filename, types) {
if (isSavePickerSupported()) {
const handle = await window.showSaveFilePicker({
suggestedName: filename,
types
})
const writable = await handle.createWritable()
await writable.write(blob)
await writable.close()
return { method: 'picker' }
}
downloadBlob(blob, filename)
return { method: 'download' }
}
export async function openJsonFile() {
if (!isOpenPickerSupported()) return null
const handles = await window.showOpenFilePicker({
multiple: false,
types: [
{
description: 'JSON 配置文件',
accept: {
'application/json': ['.json']
}
}
]
})
if (!handles.length) return null
return handles[0].getFile()
}