Skip to content

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

feat: do not add duplicate notebook in note.notebooks

thecodrr4c48d4d5553e
diff --git a/packages/core/__tests__/trash.test.js b/packages/core/__tests__/trash.test.js
index 112fd2e37..562261ff3 100644
--- a/packages/core/__tests__/trash.test.js
+++ b/packages/core/__tests__/trash.test.js
@@ -106,7 +106,9 @@ test("restore a deleted notebook", () =>
let note = db.notes.note(noteId);
const index = note.notebooks.findIndex((n) => n.id === id);
expect(note.notebooks[index]).toBeDefined();
- expect(notebook.topics.topic(note.notebooks[index].topic)).toBeDefined();
+ expect(
+ notebook.topics.topic(note.notebooks[index].topics[0])
+ ).toBeDefined();
}));
test("restore a notebook that has deleted notes", () =>
diff --git a/packages/core/models/topic.js b/packages/core/models/topic.js
index c35b68614..855a34baf 100644
--- a/packages/core/models/topic.js
+++ b/packages/core/models/topic.js
@@ -27,13 +27,17 @@ export default class Topic {
topic.notes.push(noteId);
const array = note.notebooks || [];
- if (
- array.some(
- (item) => item.id === this._notebookId && item.topic === topic.id
- )
- )
- return this;
- array.push({ id: this._notebookId, topic: topic.id });
+ const notebookIndex = array.findIndex((nb) => nb.id === this._notebookId);
+ if (notebookIndex === -1) {
+ let notebook = {};
+ notebook.id = this._notebookId;
+ notebook.topics = [topic.id];
+ array.push(notebook);
+ } else {
+ const topicIndex = array[notebookIndex].topics.indexOf(topic.id);
+ if (topicIndex > -1) return;
+ array[notebookIndex].topics.push(topic.id);
+ }
await this._db.notes.add({
id: noteId,
@@ -49,14 +53,19 @@ export default class Topic {
for (let noteId of noteIds) {
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);
+ let index = topic.notes.indexOf(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);
+ const notebookIndex = array.findIndex((nb) => nb.id === this._notebookId);
+ if (notebookIndex === -1) return;
+
+ const topicIndex = array[notebookIndex].topics.indexOf(topic.id);
+ if (topicIndex === -1) return;
+
+ array[notebookIndex].topics.splice(topicIndex, 1);
+ if (array[notebookIndex].topics.length <= 0)
+ array.splice(notebookIndex, 1);
await this._db.notes.add({
id: noteId,