Files
notesnook/packages/core/api/settings.js

45 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-11-25 15:04:39 +05:00
class Settings {
/**
*
* @param {import("./index").default} db
*/
constructor(db) {
this._db = db;
this._settings = { pins: [] };
}
async init() {
this._settings =
(await this._db.context.read("settings")) || this._settings;
}
async pin(type, data) {
2020-11-25 20:49:29 +05:00
if (type !== "notebook" && type !== "topic" && type !== "tag")
2020-11-25 15:04:39 +05:00
throw new Error("This item cannot be pinned.");
2020-11-25 20:49:29 +05:00
this._settings.pins.push({ type, data });
2020-11-25 15:04:39 +05:00
await this._db.context.write("settings", this._settings);
}
async unpin(id) {
2020-11-25 20:49:29 +05:00
const index = this._settings.pins.findIndex((i) => i.data.id === id);
2020-11-25 15:04:39 +05:00
if (index <= -1) return;
this._settings.pins.splice(index, 1);
await this._db.context.write("settings", this._settings);
}
get pins() {
return this._settings.pins.map((pin) => {
if (pin.type === "notebook") {
return this._db.notebooks.notebook(pin.data.id).data;
} else if (pin.type === "topic") {
return this._db.notebooks
2020-11-25 20:49:29 +05:00
.notebook(pin.data.notebookId)
2020-11-25 15:04:39 +05:00
.topics.topic(pin.data.topic)._topic;
} else if (pin.type === "tag") {
return this._db.tags.tag(pin.data.id);
}
});
}
}
export default Settings;