From c3d93998d15d3eb26f0b0253d43725bd7d05fb86 Mon Sep 17 00:00:00 2001 From: Chris McCormick Date: Mon, 5 Jan 2026 15:24:03 +0800 Subject: [PATCH] Refactor IndexedDb boilerplate. --- TODO.md | 2 +- index.html | 39 +++++++++++---------------------------- 2 files changed, 12 insertions(+), 29 deletions(-) diff --git a/TODO.md b/TODO.md index 8233c7e..d1deba8 100644 --- a/TODO.md +++ b/TODO.md @@ -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 diff --git a/index.html b/index.html index ae4cb6a..f07452e 100644 --- a/index.html +++ b/index.html @@ -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) {