hiss/device/browser.js
2025-02-04 14:21:03 -07:00

49 lines
1.1 KiB
JavaScript

import Store from 'store'
const noWorkingDirWarning = "Please open a directory to allow file saving."
export let root
export let store
export async function Start() {
store = await Store.Open('device', 'handles')
root = await store.Get('directory')
}
export async function cd() {
root = await window.showDirectoryPicker()
await store.Set('directory', root)
}
export async function Entries(handle = root) {
let pool = new Map()
for await (let [ , e ] of handle.entries()) {
pool.set(e.name, e)
}
return pool
}
export async function Create(sPath) {
return root.getFileHandle(sPath, { create: true })
}
export async function Open(hFile) {
if(!root) {
return new Blob([ noWorkingDirWarning ], { type: "text/plain" })
}
return hFile.getFile()
}
export async function Write(hFile, sContent) {
if(!root) {
throw new Error("Unable to save file: no working directory")
}
let s = await hFile.createWritable()
.catch(console.error)
await s.write(sContent)
await s.close()
}