Cybrkyd's Git Repositories

IndexedDB-notes - commit: 6667b30

commit 6667b30d643b676119217caa8e2028d544b93c99dd581016a9a609ec545d9eef
author cybrkyd <git@cybrkyd.com> 2026-05-26 10:21:08 +0100
committer cybrkyd <git@cybrkyd.com> 2026-05-26 10:21:08 +0100

Commit Message

Rename function

- Added rename function for existing notes
- 3rd line to sidebar per note, allowing more room for renames
- name: null for graceful backwards compatibility of existing notes

📊 Diffstat

idb-notes.html 60
1 files changed, 51 insertions(+), 9 deletions(-)

Diff

diff --git a/idb-notes.html b/idb-notes.html
index 95438b3..b516e04 100644
--- a/idb-notes.html
+++ b/idb-notes.html
@@ -257,14 +257,20 @@ function saveDocument(text) {
const store = tx.objectStore("documents");
if (currentDocId !== null) {
- const doc = {
- id: currentDocId,
- content: text,
- created: new Date().toLocaleString()
+ const getReq = store.get(currentDocId);
+ getReq.onsuccess = function() {
+ const existing = getReq.result;
+ const doc = {
+ id: currentDocId,
+ name: existing ? existing.name : null,
+ content: text,
+ created: new Date().toLocaleString()
+ };
+ store.put(doc);
};
- store.put(doc);
} else {
const doc = {
+ name: null,
content: text,
created: new Date().toLocaleString()
};
@@ -295,10 +301,14 @@ function loadDocuments() {
const line1 = document.createElement("div");
line1.className = "doc-line1";
- line1.textContent = `Note #${doc.id} - ${doc.created}`;
+ line1.textContent = doc.name ? doc.name : `Note #${doc.id}`;
const line2 = document.createElement("div");
- line2.className = "doc-line2";
+ line2.className = "doc-line1";
+ line2.textContent = doc.created;
+
+ const line3 = document.createElement("div");
+ line3.className = "doc-line2";
const loadBtn = document.createElement("button");
loadBtn.textContent = "Load";
@@ -308,6 +318,10 @@ function loadDocuments() {
deleteBtn.textContent = "Delete";
deleteBtn.className = "small-link-btn";
+ const renameBtn = document.createElement("button");
+ renameBtn.textContent = "Rename";
+ renameBtn.className = "small-link-btn";
+
loadBtn.onclick = function() {
document.getElementById("editor").value = doc.content;
currentDocId = doc.id;
@@ -319,17 +333,45 @@ function loadDocuments() {
deleteDocument(doc.id);
};
- line2.appendChild(loadBtn);
- line2.appendChild(deleteBtn);
+ renameBtn.onclick = function() {
+ renameDocument(doc.id, doc.name ? doc.name : `Note #${doc.id}`);
+ };
+
+ line3.appendChild(loadBtn);
+ line3.appendChild(deleteBtn);
+ line3.appendChild(renameBtn);
div.appendChild(line1);
div.appendChild(line2);
+ div.appendChild(line3);
list.appendChild(div);
});
};
}
+ function renameDocument(id, currentName) {
+ const newName = prompt("Enter a new name for this note:", currentName);
+ if (newName === null) return;
+ const trimmed = newName.trim();
+ if (!trimmed) { alert("Name cannot be empty"); return; }
+
+ const tx = db.transaction("documents", "readwrite");
+ const store = tx.objectStore("documents");
+ const getReq = store.get(id);
+
+ getReq.onsuccess = function() {
+ const doc = getReq.result;
+ if (!doc) return;
+ doc.name = trimmed;
+ store.put(doc);
+ };
+
+ tx.oncomplete = function() {
+ loadDocuments();
+ };
+ }
+
function deleteDocument(id) {
if (!confirm("Delete this document?")) return;