Skip to content

Commit

Permalink
tweak metadata som emore
Browse files Browse the repository at this point in the history
  • Loading branch information
kjk committed Jul 5, 2024
1 parent 6601b32 commit 3bf1818
Showing 1 changed file with 59 additions and 56 deletions.
115 changes: 59 additions & 56 deletions src/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,65 @@ export function getMetadata() {
return metadata;
}

/**
* @returns {NoteMetadata[]}
*/
function getNotesMetadata() {
metadata.notes = metadata.notes || [];
return metadata.notes;
}

/**
* @returns {Promise<Metadata>}
*/
export async function loadNotesMetadata() {
console.log("loadNotesMetadata: started");
let dh = getStorageFS();
let s;
if (!dh) {
s = localStorage.getItem(kMetadataName);
} else {
try {
s = await fsReadTextFile(dh, kMetadataName);
} catch (e) {
// it's ok if doesn't exist
console.log("loadNotesMetadata: no metadata file", e);
s = "[]";
}
}
s = s || "[]";
metadata = JSON.parse(s);
console.log("loadNotesMetadata: finished", metadata);
return metadata;
}

/**
* @param {Metadata} m
*/
async function saveNotesMetadata(m) {
let s = JSON.stringify(m, null, 2);
let dh = getStorageFS();
if (dh) {
try {
await fsWriteTextFile(dh, kMetadataName, s);
} catch (e) {
console.log("fsWriteTextFile failed with:", e);
}
} else {
localStorage.setItem(kMetadataName, s);
}
metadata = m;
return m;
}

/**
* @param {string} noteName
* @param {boolean} createIfNotExists
* @returns {NoteMetadata}
*/
export function getMetadataForNote(noteName, createIfNotExists = false) {
// console.log("getMetadataForNote:", noteName);
let meta = getMetadata();
let notes = meta.notes || [];
let notes = getNotesMetadata();
for (let m of notes) {
if (m.name === noteName) {
return m;
Expand All @@ -49,7 +99,6 @@ export function getMetadataForNote(noteName, createIfNotExists = false) {
name: noteName,
};
notes.push(m);
metadata.notes = notes;
return m;
}

Expand Down Expand Up @@ -80,59 +129,15 @@ export async function updateMetadataForNote(
*/
export async function removeNoteFromMetadata(noteName) {
console.log("deleteMetadataForNote:", noteName);
let meta = getMetadata();
let notes = meta.notes;
let notes = getNotesMetadata();
let newNotes = [];
for (let m of notes) {
if (m.name !== noteName) {
newNotes.push(m);
}
}
meta.notes = newNotes;
await saveNotesMetadata(meta);
}

/**
* @returns {Promise<Metadata>}
*/
export async function loadNotesMetadata() {
console.log("loadNotesMetadata: started");
let dh = getStorageFS();
let s;
if (!dh) {
s = localStorage.getItem(kMetadataName);
} else {
try {
s = await fsReadTextFile(dh, kMetadataName);
} catch (e) {
// it's ok if doesn't exist
console.log("loadNotesMetadata: no metadata file", e);
s = "[]";
}
}
s = s || "[]";
metadata = JSON.parse(s);
console.log("loadNotesMetadata: finished", metadata);
return metadata;
}

/**
* @param {Metadata} m
*/
async function saveNotesMetadata(m) {
let s = JSON.stringify(m, null, 2);
let dh = getStorageFS();
if (dh) {
try {
await fsWriteTextFile(dh, kMetadataName, s);
} catch (e) {
console.log("fsWriteTextFile failed with:", e);
}
} else {
localStorage.setItem(kMetadataName, s);
}
metadata = m;
return m;
metadata.notes = newNotes;
await saveNotesMetadata(metadata);
}

/**
Expand All @@ -141,15 +146,14 @@ async function saveNotesMetadata(m) {
* @returns {Promise<Metadata>}
*/
export async function renameNoteInMetadata(oldName, newName) {
let meta = getMetadata();
let notes = meta.notes || [];
let notes = getNotesMetadata();
for (let o of notes) {
if (o.name === oldName) {
o.name = newName;
break;
}
}
let res = await saveNotesMetadata(meta);
let res = await saveNotesMetadata(metadata);
return res;
}

Expand All @@ -160,16 +164,15 @@ export async function renameNoteInMetadata(oldName, newName) {
*/
export async function reassignNoteShortcut(name, altShortcut) {
console.log("reassignNoteShortcut:", name, altShortcut);
let meta = getMetadata();
let notes = meta.notes || [];
let notes = getNotesMetadata();
for (let o of notes) {
if (o.altShortcut !== altShortcut) {
continue;
}
if (o.name === name) {
// same note: just remove shortcut
delete o.altShortcut;
let res = await saveNotesMetadata(meta);
let res = await saveNotesMetadata(metadata);
return res;
} else {
// a different note: remove shortcut and then assign to the new note
Expand Down

0 comments on commit 3bf1818

Please sign in to comment.