Refactor IndexedDb boilerplate.

This commit is contained in:
Chris McCormick 2026-01-05 15:24:03 +08:00
parent 2e6cd66f56
commit c3d93998d1
2 changed files with 12 additions and 29 deletions

View file

@ -17,12 +17,12 @@
- [ ] Consolidate menu creation (card menus and burger menu)
- [ ] Extract line parsing logic - separate parsing from state management
- [ ] Reduce IndexedDB boilerplate with generic DB operation wrapper
- [ ] Make createAddForm more generic to handle both columns and cards
- [ ] Extract and consolidate indent detection functions
## Done
- [x] Reduce IndexedDB boilerplate with generic DB operation wrapper
- [x] Simplify drop indicator logic
- [x] Extract file operations into higher-level abstraction
- [x] Consolidate modal logic into generic modal handler

View file

@ -1077,48 +1077,31 @@ chmod 755 kbtd
};
});
async function getFileHandle(path) {
async function dbOperation(mode, fn) {
const db = await dbPromise;
const tx = db.transaction(STORE_NAME, mode);
const store = tx.objectStore(STORE_NAME);
return new Promise((resolve, reject) => {
const tx = db.transaction(STORE_NAME, 'readonly');
const store = tx.objectStore(STORE_NAME);
const request = store.get(path);
const request = fn(store);
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
}
async function getFileHandle(path) {
return dbOperation('readonly', store => store.get(path));
}
async function storeFileHandle(path, handle) {
const db = await dbPromise;
return new Promise((resolve, reject) => {
const tx = db.transaction(STORE_NAME, 'readwrite');
const store = tx.objectStore(STORE_NAME);
const request = store.put(handle, path);
request.onsuccess = () => resolve();
request.onerror = () => reject(request.error);
});
return dbOperation('readwrite', store => store.put(handle, path));
}
async function removeFileHandle(path) {
const db = await dbPromise;
return new Promise((resolve, reject) => {
const tx = db.transaction(STORE_NAME, 'readwrite');
const store = tx.objectStore(STORE_NAME);
const request = store.delete(path);
request.onsuccess = () => resolve();
request.onerror = () => reject(request.error);
});
return dbOperation('readwrite', store => store.delete(path));
}
async function getAllStoredProjects() {
const db = await dbPromise;
return new Promise((resolve, reject) => {
const tx = db.transaction(STORE_NAME, 'readonly');
const store = tx.objectStore(STORE_NAME);
const request = store.getAllKeys();
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error);
});
return dbOperation('readonly', store => store.getAllKeys());
}
async function validateFileHandle(handle) {