Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

feat: add items pin/unpin

thecodrrf04088d8242f
diff --git a/packages/core/api/index.js b/packages/core/api/index.js
index 310968d38..01c1d507e 100644
--- a/packages/core/api/index.js
+++ b/packages/core/api/index.js
@@ -55,6 +55,7 @@ class Database {
this.conflicts = new Conflicts(this);
this.lookup = new Lookup(this);
this.backup = new Backup(this);
+ this.settings = new Settings(this);
// collections
/** @type {Notes} */
@@ -70,6 +71,7 @@ class Database {
/** @type {Trash} */
this.trash = await Trash.new(this, "trash");
+ await this.settings.init();
await this.user.sync();
}
diff --git a/packages/core/api/settings.js b/packages/core/api/settings.js
new file mode 100644
index 000000000..0c869bab8
--- /dev/null
+++ b/packages/core/api/settings.js
@@ -0,0 +1,43 @@
+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) {
+ if (type !== "notebook" || type !== "topic" || type !== "tag")
+ throw new Error("This item cannot be pinned.");
+ this._settings.pins.push({ id, data });
+ await this._db.context.write("settings", this._settings);
+ }
+
+ async unpin(id) {
+ const index = this._settings.pins.findIndex((i) => i.id === id);
+ 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
+ .notebook(pin.data.id)
+ .topics.topic(pin.data.topic)._topic;
+ } else if (pin.type === "tag") {
+ return this._db.tags.tag(pin.data.id);
+ }
+ });
+ }
+}