You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.8 KiB
52 lines
1.8 KiB
const { loadNotesInDirectory, saveNoteInDirectory } = require("./notes");
|
|
const { loadTags, updateTagsForNote } = require("./tags");
|
|
const { defaultSettings } = require("./settings");
|
|
|
|
function newNoteBackend(initSettings) {
|
|
let settings = { ...defaultSettings, ...initSettings };
|
|
|
|
async function onNoteCommand({ type, payload }) {
|
|
console.log(`got command: ${type} -n ${JSON.stringify(payload)}`);
|
|
const { directory } = settings;
|
|
switch (type) {
|
|
case "getNotes":
|
|
const files = await loadNotesInDirectory(directory);
|
|
const loadedTags = await loadTags(directory);
|
|
return {
|
|
type: "getNotes",
|
|
payload: {
|
|
notes: files.map(f => ({
|
|
...f,
|
|
tags: loadedTags[f.title]
|
|
}))
|
|
}
|
|
};
|
|
case "saveNote":
|
|
const { title, content, tags } = payload;
|
|
const result = await saveNoteInDirectory(settings, {
|
|
title,
|
|
content,
|
|
tags
|
|
});
|
|
await updateTagsForNote(directory, { title, tags });
|
|
return { type: "saveNote", payload: { ...result, tags } };
|
|
case "getSettings":
|
|
return { type: "getSettings", payload: { settings } };
|
|
case "saveSettings":
|
|
default:
|
|
return { type: "noop" };
|
|
}
|
|
}
|
|
|
|
function updateSettings({ directory }) {
|
|
settings.directory = directory;
|
|
}
|
|
|
|
return {
|
|
onNoteCommand,
|
|
updateSettings
|
|
};
|
|
}
|
|
|
|
module.exports = { createBackend: newNoteBackend };
|