Thought cabinet

This commit is contained in:
2024-12-11 19:21:57 -07:00
parent 50acba5cdb
commit bb6662d7b1
41 changed files with 32477 additions and 2338 deletions

48
device/browser.js Normal file
View File

@@ -0,0 +1,48 @@
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()
}

68
device/nw.js Normal file
View File

@@ -0,0 +1,68 @@
const FS = require('fs/promises')
const Path = require('path')
const themePath = process.env.HOME ? Path.join(process.env.HOME, '.config/base16.css') : 'base16.css'
function handle(sPath) {
return {
name: Path.basename(sPath),
path: absolute(sPath)
}
}
function absolute(sPath) {
return Path.isAbsolute(sPath) ? sPath : Path.join(window.cwd, sPath)
}
export async function Init() {
window.cwd = nw.App.startPath
await loadTheme()
watchTheme()
}
async function watchTheme() {
let watcher = await FS.watch(themePath)
for await (const event of watcher) {
await loadTheme()
}
}
async function loadTheme() {
let f = await Open(handle(themePath))
loadStylesheet(f)
}
function loadStylesheet(fStylesheet) {
let l = document.createElement('link')
l.rel = "stylesheet"
l.href = URL.createObjectURL(fStylesheet)
document.head.appendChild(l)
}
export async function Entries() {
let pool = new Map()
for(let d of await FS.readdir(window.cwd, { withFileTypes: true })) {
if(d.isFile()) {
let h = handle(d.name)
pool.set(d.name, h)
}
}
return pool
}
export async function Create(sPath) {
return handle(sPath)
}
export async function Open(hFile) {
let b = await FS.readFile(hFile.path)
return new File([ b ], hFile.name)
}
export async function Write(hFile, sContent) {
return await FS.writeFile(hFile.path, sContent, { encoding: 'utf-8' })
}

46
device/store.js Normal file
View File

@@ -0,0 +1,46 @@
const app = window.app
const dbName = 'notes'
const objectStoreName = 'files'
export function transact() {
return window.db
.transaction(objectStoreName, "readwrite")
.objectStore(objectStoreName)
}
export function promisify(request) {
return new Promise((resolve, reject) => {
request.onsuccess = (event) => {
resolve(request.result)
}
request.onerror = (event) => {
reject(request.error)
}
})
}
export async function Init() {
const request = indexedDB.open(dbName, 2)
request.onupgradeneeded = (event) => {
const db = event.target.result
const objectStore = db.createObjectStore(objectStoreName)
objectStore.createIndex('value', 'value', { unique: false })
}
window.db = await promisify(request)
}
export function Get(key) {
return promisify(
transact().get(key)
)
}
export function Set(key, value) {
return promisify(
transact().put(value, key)
)
}