Skip to content

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

feat: implement notebooks and topics

thecodrrf59acd2e57e2
diff --git a/packages/core/__tests__/notebooks.test.js b/packages/core/__tests__/notebooks.test.js
new file mode 100644
index 000000000..4008192a9
--- /dev/null
+++ b/packages/core/__tests__/notebooks.test.js
@@ -0,0 +1,107 @@
+import DB from "../api";
+import StorageInterface from "../__mocks__/storage.mock";
+
+const TEST_NOTEBOOK = {
+ title: "Test Notebook",
+ description: "Test Description",
+ topics: ["hello", "hello", " "]
+};
+const TEST_NOTEBOOK2 = {
+ title: "Test Notebook 2",
+ description: "Test Description 2",
+ topics: ["Home2"]
+};
+
+function databaseTest() {
+ let db = new DB(StorageInterface);
+ return db.init().then(() => db);
+}
+
+const notebookTest = (notebook = TEST_NOTEBOOK) =>
+ databaseTest().then(async db => {
+ let id = await db.notebooks.add(notebook);
+ return { db, id };
+ });
+
+beforeEach(async () => {
+ StorageInterface.clear();
+});
+
+test("add a notebook", () =>
+ notebookTest().then(({ db, id }) => {
+ expect(id).toBeDefined();
+ let notebook = db.notebooks.get(id);
+ expect(notebook).toBeDefined();
+ expect(notebook.title).toBe(TEST_NOTEBOOK.title);
+ }));
+
+test("get all notebooks", () =>
+ notebookTest().then(({ db }) => {
+ expect(db.notebooks.all.length).toBeGreaterThan(0);
+ }));
+
+test("search all notebooks", () =>
+ notebookTest({
+ ...TEST_NOTEBOOK,
+ title: "I will be searched.",
+ description: "searched description"
+ }).then(({ db }) => {
+ let filtered = db.notebooks.filter("searhed");
+ expect(filtered.length).toBeGreaterThan(0);
+ }));
+
+test("pin a notebook", () =>
+ notebookTest().then(async ({ db, id }) => {
+ await db.notebooks.pin(id);
+ let notebook = db.notebooks.get(id);
+ expect(notebook.pinned).toBe(true);
+ }));
+
+test("unpin a notebook", () =>
+ notebookTest().then(async ({ db, id }) => {
+ await db.notebooks.unpin(id);
+ let notebook = db.notebooks.get(id);
+ expect(notebook.pinned).toBe(false);
+ }));
+
+test("favorite a notebook", () =>
+ notebookTest().then(async ({ db, id }) => {
+ await db.notebooks.favorite(id);
+ let notebook = db.notebooks.get(id);
+ expect(notebook.favorite).toBe(true);
+ }));
+
+test("unfavorite a notebook", () =>
+ notebookTest().then(async ({ db, id }) => {
+ await db.notebooks.unfavorite(id);
+ let notebook = db.notebooks.get(id);
+ expect(notebook.favorite).toBe(false);
+ }));
+
+test("get empty topic", () =>
+ notebookTest().then(({ db, id }) => {
+ let notes = db.notebooks.topics(id).get("General");
+ expect(notes.length).toBe(0);
+ }));
+
+test("getting invalid topic should throw", () =>
+ notebookTest().then(({ db, id }) => {
+ expect(() => db.notebooks.topics(id).get("invalid")).toThrow();
+ }));
+
+test("add topic to notebook", () =>
+ notebookTest().then(async ({ db, id }) => {
+ let topics = db.notebooks.topics(id);
+ await topics.add("Home");
+ expect(topics.all.length).toBeGreaterThan(1);
+ expect(topics.all.findIndex(v => v.title === "Home")).toBeGreaterThan(-1);
+ }));
+
+test("duplicate topic to notebook should not be added", () =>
+ notebookTest().then(async ({ db, id }) => {
+ let topics = db.notebooks.topics(id);
+ await topics.add("Home");
+ let len = topics.all.length;
+ await topics.add("Home");
+ expect(topics.all.length).toBe(len);
+ }));
diff --git a/packages/core/api/index.js b/packages/core/api/index.js
index 9fe5bfaba..565f25720 100644
--- a/packages/core/api/index.js
+++ b/packages/core/api/index.js
@@ -1,4 +1,5 @@
import Notes from "../collections/notes";
+import Notebooks from "../collections/notebooks";
class DB {
constructor(context) {
@@ -6,6 +7,7 @@ class DB {
}
async init() {
this.notes = new Notes(this.context);
+ this.notebooks = new Notebooks(this.context);
}
}
diff --git a/packages/core/collections/notebooks.js b/packages/core/collections/notebooks.js
new file mode 100644
index 000000000..6ce217f03
--- /dev/null
+++ b/packages/core/collections/notebooks.js
@@ -0,0 +1,120 @@
+import CachedCollection from "../database/cached-collection";
+import fuzzysearch from "fuzzysearch";
+import Topics from "./topics";
+var tfun = require("transfun/transfun.js").tfun;
+if (!tfun) {
+ tfun = global.tfun;
+}
+export default class Notebooks {
+ constructor(context) {
+ this.context = context;
+ this.collection = new CachedCollection(context, "notebooks");
+ }
+
+ async add(notebookArg) {
+ if (!notebookArg) throw new Error("Notebook cannot be undefined or null.");
+ //TODO reliably and efficiently check for duplicates.
+ const id = notebookArg.id || Date.now().toString() + "_notebook";
+ let oldNotebook = this.collection.getItem(id);
+
+ if (!oldNotebook && !notebookArg.title)
+ throw new Error("Notebook must contain at least a title.");
+
+ let notebook = {
+ ...oldNotebook,
+ ...notebookArg
+ };
+ if (oldNotebook && oldNotebook.topics) {
+ notebook.topics = [...oldNotebook.topics];
+ }
+ if (notebookArg.topics) {
+ notebook.topics = [...notebook.topics, ...notebookArg.topics];
+ }
+
+ let topics = notebook.topics || [];
+ if (!oldNotebook) {
+ topics[0] = makeTopic("General", id);
+ }
+
+ for (let i = 0; i < topics.length; i++) {
+ let topic = topics[i];
+ let isDuplicate =
+ topics.findIndex(t => t.title === (topic || topic.title)) > -1; //check for duplicate
+
+ let isEmpty =
+ !topic || (typeof topic === "string" && topic.trim().length <= 0);
+
+ if (isEmpty || isDuplicate) {
+ topics.splice(i, 1);
+ i--;
+ continue;
+ }
+
+ if (typeof topic === "string") {
+ topics[i] = makeTopic(topic, id);
+ }
+ }
+
+ notebook = {
+ id,
+ type: "notebook",
+ title: notebook.title,
+ description: notebook.description,
+ dateCreated: notebook.dateCreated || Date.now(),
+ dateEdited: Date.now(),
+ pinned: !!notebook.pinned,
+ favorite: !!notebook.favorite,
+ topics,
+ totalNotes: 0
+ };
+
+ await this.collection.addItem(notebook);
+ return notebook.id;
+ }
+
+ get all() {
+ return this.collection.getAllItems();
+ }
+
+ get(id) {
+ return this.collection.getItem(id);
+ }
+
+ delete(...ids) {}
+
+ filter(query) {
+ if (!query) return [];
+ return tfun.filter(v => fuzzysearch(query, v.title + " " + v.description))(
+ this.all
+ );
+ }
+
+ topics(id) {
+ return new Topics(this, id);
+ }
+
+ pin(id) {
+ return this.add({ id, pinned: true });
+ }
+ unpin(id) {
+ return this.add({ id, pinned: false });
+ }
+ favorite(id) {
+ return this.add({ id, favorite: true });
+ }
+ unfavorite(id) {
+ return this.add({ id, favorite: false });
+ }
+}
+
+function makeTopic(topic, notebookId) {
+ return {
+ type: "topic",
+ notebookId,
+ title: topic,
+ id: Date.now(),
+ dateCreated: Date.now(),
+ totalNotes: 0,
+ notes: []
+ };
+}
diff --git a/packages/core/collections/topics.js b/packages/core/collections/topics.js
new file mode 100644
index 000000000..a5083b096
--- /dev/null
+++ b/packages/core/collections/topics.js
@@ -0,0 +1,51 @@
+import Notebooks from "./notebooks";
+import Notes from "./notes";
+
+export default class Topics {
+ /**
+ *
+ * @param {Notebooks} notebooks
+ * @param {string} notebookId
+ */
+ constructor(notebooks, notebookId) {
+ this.notebooks = notebooks;
+ this.notebookId = notebookId;
+ this.notes = new Notes(this.notebooks.context);
+ }
+
+ add(topic) {
+ return this.notebooks.add({
+ id: this.notebookId,
+ topics: [topic]
+ });
+ }
+
+ get all() {
+ return this.notebooks.get(this.notebookId).topics;
+ }
+
+ get(topic) {
+ let notebook = this.notebooks.get(this.notebookId);
+ if (typeof topic === "string") {
+ topic = notebook.topics.find(v => v.title === topic);
+ }
+ if (!topic)
+ throw new Error("topics.get: Topic cannot be undefined or null.");
+ if (!topic.notes)
+ throw new Error("topics.get: Topic must contain an array of note ids.");
+ return topic.notes.map(note => this.notes.get(note));
+ }
+
+ async delete(...topics) {
+ let notebook = this.notebooks.get(this.notebookId);
+ for (let topic of topics) {
+ let index = notebook.topics.findIndex(t => (t.title = topic.title));
+ if (index <= -1) continue;
+ notebook.topics.splice(index, 1);
+ }
+ await this.notebooks.add({
+ id: notebook.id,
+ topics: notebook.topics
+ });
+ }
+}