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
fix: topics addition and deletion
thecodrr22100d167cb6
diff --git a/packages/core/__tests__/notes.test.js b/packages/core/__tests__/notes.test.jsindex 647149e6f..bae88224e 100644--- a/packages/core/__tests__/notes.test.js+++ b/packages/core/__tests__/notes.test.js@@ -223,7 +223,8 @@ test("add note to topic", () =>noteTest().then(async ({ db, id }) => {let notebookId = await db.notebooks.add({ title: "Hello" });let topics = db.notebooks.notebook(notebookId).topics;- let topic = await topics.add("Home");+ await topics.add("Home");+ let topic = topics.topic("Home");await topic.add(id);expect(topic.all.length).toBe(1);let note = db.notes.note(id);@@ -234,7 +235,8 @@ test("duplicate note to topic should not be added", () =>noteTest().then(async ({ db, id }) => {let notebookId = await db.notebooks.add({ title: "Hello" });let topics = db.notebooks.notebook(notebookId).topics;- let topic = await topics.add("Home");+ await topics.add("Home");+ let topic = topics.topic("Home");await topic.add(id);expect(topic.all.length).toBe(1);}));@@ -243,7 +245,8 @@ test("move note", () =>noteTest().then(async ({ db, id }) => {let notebookId = await db.notebooks.add({ title: "Hello" });let topics = db.notebooks.notebook(notebookId).topics;- let topic = await topics.add("Home");+ await topics.add("Home");+ let topic = topics.topic("Home");await topic.add(id);setTimeout(async () => {let notebookId2 = await db.notebooks.add({ title: "Hello2" });@@ -258,7 +261,8 @@ test("moving note to same notebook and topic should do nothing", () =>noteTest().then(async ({ db, id }) => {let notebookId = await db.notebooks.add({ title: "Hello" });let topics = db.notebooks.notebook(notebookId).topics;- let topic = await topics.add("Home");+ await topics.add("Home");+ let topic = topics.topic("Home");await topic.add(id);await db.notes.move({ id: notebookId, topic: "Home" }, id);let note = db.notes.note(id);diff --git a/packages/core/__tests__/topics.test.js b/packages/core/__tests__/topics.test.jsindex 78d918f17..2e347c5bb 100644--- a/packages/core/__tests__/topics.test.js+++ b/packages/core/__tests__/topics.test.js@@ -1,4 +1,4 @@-import { notebookTest, StorageInterface } from "./utils";+import { notebookTest, StorageInterface, TEST_NOTE } from "./utils";beforeEach(() => StorageInterface.clear());@@ -8,9 +8,9 @@ test("get empty topic", () =>expect(topic.all.length).toBe(0);}));-test("getting invalid topic should throw", () =>+test("getting invalid topic should return undefined", () =>notebookTest().then(({ db, id }) => {- expect(() => db.notebooks.topics(id).get("invalid")).toThrow();+ expect(db.notebooks.notebook(id).topics.topic("invalid")).toBeUndefined();}));test("add topic to notebook", () =>@@ -21,6 +21,16 @@ test("add topic to notebook", () =>expect(topics.all.findIndex(v => v.title === "Home")).toBeGreaterThan(-1);}));+test("update topic", () =>+ notebookTest().then(async ({ db, id }) => {+ let topics = db.notebooks.notebook(id).topics;+ await topics.add("Home");+ let topic = topics.topic("Home");+ let noteId = await db.notes.add(TEST_NOTE);+ await topic.add(noteId);+ expect(topics.all.find(v => v.title === "Home").notes.length).toBe(1);+ }));+test("duplicate topic to notebook should not be added", () =>notebookTest().then(async ({ db, id }) => {let topics = db.notebooks.notebook(id).topics;@@ -33,7 +43,8 @@ test("duplicate topic to notebook should not be added", () =>test("get topic", () =>notebookTest().then(async ({ db, id }) => {let topics = db.notebooks.notebook(id).topics;- let topic = await topics.add("Home");+ await topics.add("Home");+ let topic = topics.topic("Home");let noteId = await db.notes.add({ content: { text: "Hello", delta: [] } });await topic.add(noteId);expect(topic.all[0].content.text).toBe("Hello");diff --git a/packages/core/collections/notebooks.js b/packages/core/collections/notebooks.jsindex bc4d88868..c955d18cd 100644--- a/packages/core/collections/notebooks.js+++ b/packages/core/collections/notebooks.js@@ -38,35 +38,6 @@ export default class Notebooks {...oldNotebook,...notebookArg};- /* if (notebookArg.topics) {- notebook.topics = [...notebook.topics, ...notebookArg.topics];- } */- if (oldNotebook && oldNotebook.topics) {- notebook.topics = [...notebook.topics, ...oldNotebook.topics];- }-- let topics =- notebook.topics.filter((v, i) => notebook.topics.indexOf(v) === i) || [];- if (!oldNotebook) {- topics[0] = makeTopic("General", id);- }-- for (let i = 0; i < topics.length; i++) {- let topic = topics[i];-- let isEmpty =- !topic || (typeof topic === "string" && topic.trim().length <= 0);-- if (isEmpty) {- topics.splice(i, 1);- i--;- continue;- }-- if (typeof topic === "string") {- topics[i] = makeTopic(topic, id);- }- }notebook = {id,@@ -77,11 +48,18 @@ export default class Notebooks {dateEdited: Date.now(),pinned: !!notebook.pinned,favorite: !!notebook.favorite,- topics,+ topics: notebook.topics || [],totalNotes: 0};+ if (!oldNotebook) {+ notebook.topics.splice(0, 0, "General");+ }await this.collection.addItem(notebook);++ //if (!oldNotebook) {+ await this.notebook(notebook.id).topics.add(...notebook.topics);+ //}return notebook.id;}@@ -114,15 +92,3 @@ export default class Notebooks {);}}--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.jsindex a85273288..f2f1711d9 100644--- a/packages/core/collections/topics.js+++ b/packages/core/collections/topics.js@@ -19,12 +19,20 @@ export default class Topics {return this.all.findIndex(v => v.title === (topic.title || topic)) > -1;}- async add(topic) {- await this.notebooks.add({- id: this.notebookId,- topics: [topic]- });- return this.topic(topic);+ async add(...topics) {+ let notebook = this.notebooks.notebook(this.notebookId);+ let uniqueTopics = [...notebook.data.topics, ...topics];+ uniqueTopics = uniqueTopics.filter(+ (v, i) =>+ v &&+ (v.title || v).trim().length > 0 &&+ uniqueTopics.findIndex(t => (v.title || v) === (t.title || t)) === i+ );+ notebook.data.topics = [];+ for (let topic of uniqueTopics) {+ notebook.data.topics.push(makeTopic(topic, this.notebookId));+ }+ await this.notebooks.collection.addItem(notebook.data);}get all() {@@ -51,9 +59,18 @@ export default class Topics {allTopics.splice(i, 1);}}- await this.notebooks.add({- id: this.notebookId,- topics: allTopics- });+ await this.notebooks.add({ id: this.notebookId, topics: allTopics });}}++function makeTopic(topic, notebookId) {+ if (typeof topic !== "string") return topic;+ return {+ type: "topic",+ notebookId,+ title: topic,+ dateCreated: Date.now(),+ totalNotes: 0,+ notes: []+ };+}diff --git a/packages/core/collections/trash.js b/packages/core/collections/trash.jsindex 93cbb2b4e..39fe98f4a 100644--- a/packages/core/collections/trash.js+++ b/packages/core/collections/trash.js@@ -78,8 +78,10 @@ export default class Trash {await this.notebooks.add(item);let notebook = this.notebooks.notebook(item.id);for (let topic of topics) {- let t = await notebook.topics.add(topic.title);- await t.add(...topic.notes);+ await notebook.topics.add(topic.title || topic);+ let t = notebook.topics.topic(topic.title || topic);+ if (!t) continue;+ if (topic.notes) await t.add(...topic.notes);}}await this.collection.removeItem(id);diff --git a/packages/core/models/topic.js b/packages/core/models/topic.jsindex c63729d1a..8498c5235 100644--- a/packages/core/models/topic.js+++ b/packages/core/models/topic.js@@ -64,9 +64,10 @@ export default class Topic {return await this.save();}- save() {+ async save() {if (this.transactionOpen) return this;- return this.topics.add(this.topic);+ await this.topics.add(this.topic);+ return this;}get all() {