qwe/device/browser.js
2024-12-11 19:21:57 -07:00

48 lines
1.1 KiB
JavaScript

import * as store from './store.js'
const noWorkingDirWarning = "Please open a directory to allow file saving."
export async function Init() {
await store.Init()
window.cwd = await store.Get('directory')
}
export async function cd() {
window.cwd = await window.showDirectoryPicker()
await store.Set('directory', window.cwd)
}
export async function Entries() {
let pool = new Map()
for await (let [ , e ] of window.cwd.entries()) {
if(e instanceof FileSystemFileHandle) {
pool.set(e.name, e)
}
}
return pool
}
export async function Create(sPath) {
return window.cwd.getFileHandle(sPath, { create: true })
}
export async function Open(hFile) {
if(!window.cwd) {
return new Blob([ noWorkingDirWarning ], { type: "text/plain" })
}
return hFile.getFile()
}
export async function Write(hFile, sContent) {
if(!window.cwd) {
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()
}