A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
feat: allow note to be in multiple notebooks and topics
thecodrr97d396a01e1c
diff --git a/packages/core/__tests__/notebooks.test.js b/packages/core/__tests__/notebooks.test.jsindex dc5b40914..8322461b9 100644--- a/packages/core/__tests__/notebooks.test.js+++ b/packages/core/__tests__/notebooks.test.js@@ -2,7 +2,7 @@ import {StorageInterface,notebookTest,TEST_NOTEBOOK,- TEST_NOTE+ TEST_NOTE,} from "./utils";beforeEach(async () => {@@ -26,7 +26,7 @@ test("search all notebooks", () =>notebookTest({...TEST_NOTEBOOK,title: "I will be searched.",- description: "searched description"+ description: "searched description",}).then(({ db }) => {let filtered = db.notebooks.filter("searhed");expect(filtered.length).toBeGreaterThan(0);@@ -54,20 +54,17 @@ test("unpin a notebook", () =>test("delete a notebook", () =>notebookTest().then(async ({ db, id }) => {let noteId = await db.notes.add(TEST_NOTE);- await db.notebooks- .notebook(id)- .topics.topic("General")- .add(noteId);- expect(- db.notebooks- .notebook(id)- .topics.topic("General")- .has(noteId)- ).toBe(true);+ await db.notebooks.notebook(id).topics.topic("General").add(noteId);+ expect(db.notebooks.notebook(id).topics.topic("General").has(noteId)).toBe(+ true+ );let note = db.notes.note(noteId);- expect(note.notebook.id).toBe(id);++ expect(note.notebooks.some((n) => n.id === id)).toBe(true);+await db.notebooks.delete(id);expect(db.notebooks.notebook(id).data.deleted).toBe(true);note = db.notes.note(noteId);- expect(note.notebook.id).toBeUndefined();++ expect(note.notebooks.length).toBe(0);}));diff --git a/packages/core/__tests__/notes.test.js b/packages/core/__tests__/notes.test.jsindex 0437dffe4..966df1a73 100644--- a/packages/core/__tests__/notes.test.js+++ b/packages/core/__tests__/notes.test.js@@ -165,7 +165,7 @@ test("add note to topic", () =>expect(topic.totalNotes).toBe(1);expect(db.notebooks.notebook(notebookId).data.totalNotes).toBe(1);let note = db.notes.note(id);- expect(note.notebook.id).toBe(notebookId);+ expect(note.notebooks.some((n) => n.id === notebookId)).toBe(true);}));test("duplicate note to topic should not be added", () =>@@ -190,7 +190,7 @@ test("move note", () =>await db.notebooks.notebook(notebookId2).topics.add("Home2");await db.notes.move({ id: notebookId2, topic: "Home2" }, id);let note = db.notes.note(id);- expect(note.notebook.id).toBe(notebookId2);+ expect(note.notebooks.some((n) => n.id === notebookId2)).toBe(true);}));test("moving note to same notebook and topic should do nothing", () =>@@ -202,7 +202,7 @@ test("moving note to same notebook and topic should do nothing", () =>await topic.add(id);await db.notes.move({ id: notebookId, topic: "Home" }, id);let note = db.notes.note(id);- expect(note.notebook.id).toBe(notebookId);+ expect(note.notebooks.some((n) => n.id === notebookId)).toBe(true);}));test("export note to html", () =>diff --git a/packages/core/__tests__/trash.test.js b/packages/core/__tests__/trash.test.jsindex a5c1d1d2d..112fd2e37 100644--- a/packages/core/__tests__/trash.test.js+++ b/packages/core/__tests__/trash.test.js@@ -42,8 +42,8 @@ test("restore a deleted note that was in a notebook", () =>const notebook = db.notebooks.notebook(nbId);expect(notebook.topics.topic("General").has(id)).toBe(true);- expect(note.notebook.id).toBe(nbId);- expect(note.notebook.topic).toBeDefined();+ expect(note.notebooks.some((n) => n.id === nbId)).toBe(true);+expect(notebook.topics.has("General")).toBeDefined();}));@@ -92,7 +92,7 @@ test("delete a notebook", () =>await db.notebooks.notebook(id).topics.topic("General").add(noteId);await db.notebooks.delete(id);expect(db.notebooks.notebook(id).data.deleted).toBe(true);- expect(db.notes.note(noteId).notebook).toStrictEqual({});+ expect(db.notes.note(noteId).notebook).toBeUndefined();}));test("restore a deleted notebook", () =>@@ -104,9 +104,9 @@ test("restore a deleted notebook", () =>let notebook = db.notebooks.notebook(id);expect(notebook).toBeDefined();let note = db.notes.note(noteId);- expect(note.notebook.id).toBe(id);- expect(note.notebook.topic).toBeDefined();- expect(notebook.topics.topic(note.notebook.topic)).toBeDefined();+ const index = note.notebooks.findIndex((n) => n.id === id);+ expect(note.notebooks[index]).toBeDefined();+ expect(notebook.topics.topic(note.notebooks[index].topic)).toBeDefined();}));test("restore a notebook that has deleted notes", () =>diff --git a/packages/core/collections/notes.js b/packages/core/collections/notes.jsindex 55e58537f..a84a93fa6 100644--- a/packages/core/collections/notes.js+++ b/packages/core/collections/notes.js@@ -66,7 +66,7 @@ export default class Notes extends Collection {headline: note.headline,pinned: !!note.pinned,locked: !!note.locked,- notebook: note.notebook || undefined,+ notebooks: note.notebooks || undefined,colors: note.colors || [],tags: note.tags || [],favorite: !!note.favorite,diff --git a/packages/core/database/backup.js b/packages/core/database/backup.jsindex 35a35235c..8ac0107cd 100644--- a/packages/core/database/backup.js+++ b/packages/core/database/backup.js@@ -177,10 +177,12 @@ const migrations = {if (await migrations.handleDeleted(db, "notes", item)) return;const contentId = item.content.delta;+ const notebook = item.notebook;delete item.content;+ delete item.notebook;item.contentId = contentId;item.remote = true;- if (!item.notebook.id) item.notebook = undefined;+ if (notebook) item.notebooks = [notebook];await db.notes.add(item);},delta: async function (db, item) {diff --git a/packages/core/models/note.js b/packages/core/models/note.jsindex bec7a1d62..89a582e98 100644--- a/packages/core/models/note.js+++ b/packages/core/models/note.js@@ -39,8 +39,8 @@ export default class Note {return this._note.id;}- get notebook() {- return this._note.notebook;+ get notebooks() {+ return this._note.notebooks;}get dateEdited() {diff --git a/packages/core/models/topic.js b/packages/core/models/topic.jsindex efe796c7e..c35b68614 100644--- a/packages/core/models/topic.js+++ b/packages/core/models/topic.js@@ -25,20 +25,19 @@ export default class Topic {let note = this._db.notes.note(noteId);if (this.has(noteId) || !note || note.data.deleted) continue;topic.notes.push(noteId);- if (note.notebook && note.notebook.id && note.notebook.topic) {- if (- note.notebook.id === this._notebookId &&- note.notebook.topic === topic.id++ const array = note.notebooks || [];+ if (+ array.some(+ (item) => item.id === this._notebookId && item.topic === topic.id)- return this;- await this._db.notebooks- .notebook(note.notebook.id)- .topics.topic(note.notebook.topic)- .delete(note.id);- }+ )+ return this;+ array.push({ id: this._notebookId, topic: topic.id });+await this._db.notes.add({id: noteId,- notebook: { id: this._notebookId, topic: topic.id },+ notebooks: array,});topic.totalNotes++;}@@ -48,12 +47,20 @@ export default class Topic {async delete(...noteIds) {const topic = qclone(this._topic);for (let noteId of noteIds) {- if (!this.has(noteId)) return this;+ let note = this._db.notes.note(noteId);+ if (!this.has(noteId) || !note || note.data.deleted) return this;let index = topic.notes.findIndex((n) => n === noteId);topic.notes.splice(index, 1);++ const array = note.notebooks || [];+ index = array.findIndex(+ (n) => n.id === this._notebookId && n.topic === topic.id+ );+ array.splice(index, 1);+await this._db.notes.add({id: noteId,- notebook: {},+ notebooks: array,});topic.totalNotes--;}