Skip to content

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

fix: add/remove tag from note

thecodrr061f6e060506
diff --git a/packages/core/__tests__/database.test.js b/packages/core/__tests__/database.test.js
index 3e32357c6..0a3fb3448 100644
--- a/packages/core/__tests__/database.test.js
+++ b/packages/core/__tests__/database.test.js
@@ -171,25 +171,21 @@ test("updating note with duplicate colors", () =>
expect(note.colors).toStrictEqual(["red", "blue"]);
}));
-test("updating note with new tags", () =>
- noteTest({
- ...TEST_NOTE,
- tags: ["hello", "world"]
- }).then(async ({ db, timestamp }) => {
- let updateTimestamp = await db.addNote({
- dateCreated: timestamp,
- tags: ["new", "tag", "goes", "here"]
- });
- expect(updateTimestamp).toBe(timestamp);
- let note = db.getNote(updateTimestamp);
- expect(note.tags).toStrictEqual([
- "hello",
- "world",
- "new",
- "tag",
- "goes",
- "here"
- ]);
+test("add tag to note", () =>
+ noteTest().then(async ({ db, timestamp }) => {
+ await db.addTag(timestamp, "hello");
+ expect(db.getNote(timestamp).tags[0]).toBe("hello");
+ expect(db.getTags()[0].title).toBe("hello");
+ }));
+
+test("remove tag from note", () =>
+ noteTest().then(async ({ db, timestamp }) => {
+ await db.addTag(timestamp, "hello");
+ expect(db.getNote(timestamp).tags[0]).toBe("hello");
+ expect(db.getTags()[0].title).toBe("hello");
+ await db.removeTag(timestamp, "hello");
+ expect(db.getNote(timestamp).tags.length).toBe(0);
+ expect(db.getTags().length).toBe(0);
}));
test("note with text longer than 150 characters should have ... in the headline", () =>
@@ -486,19 +482,11 @@ test("Operations on uninitialized database should throw", () => {
test("edit item with wrong id should throw", () =>
databaseTest().then(async db => {
- db.pinItem("notebook", 1242141).catch(err => {
+ db.pinNotebook(1242141).catch(err => {
expect(err.message).toContain("Wrong notebook id");
});
}));
-test("edit item with wrong type should throw", () => {
- databaseTest().then(async db => {
- db.pinItem("notebok", 1242141).catch(err => {
- expect(err.message).toContain("Invalid type");
- });
- });
-});
-
test("restoring an invalid item from trash should throw", () =>
databaseTest().then(async db => {
db.restoreItem(21412).catch(err =>
@@ -528,25 +516,25 @@ test("get grouped notes default (special)", () => groupedTest("", true));
test("pin note", () =>
noteTest().then(async ({ db, timestamp }) => {
- await db.pinItem("note", timestamp);
+ await db.pinNote(timestamp);
expect(db.getNote(timestamp).pinned).toBe(true);
}));
test("pin notebook", () =>
notebookTest().then(async ({ db, timestamp }) => {
- await db.pinItem("notebook", timestamp);
+ await db.pinNotebook(timestamp);
expect(db.getNotebook(timestamp).pinned).toBe(true);
}));
test("favorite note", () =>
noteTest().then(async ({ db, timestamp }) => {
- await db.favoriteItem("note", timestamp);
+ await db.favoriteNote(timestamp);
expect(db.getNote(timestamp).favorite).toBe(true);
}));
test("favorite notebook", () =>
notebookTest().then(async ({ db, timestamp }) => {
- await db.favoriteItem("notebook", timestamp);
+ await db.favoriteNotebook(timestamp);
expect(db.getNotebook(timestamp).favorite).toBe(true);
}));
diff --git a/packages/core/api/database.js b/packages/core/api/database.js
index e3f2e9575..cc8b3630d 100644
--- a/packages/core/api/database.js
+++ b/packages/core/api/database.js
@@ -149,10 +149,10 @@ class Database {
title: getNoteTitle(note),
content: getNoteContent(note),
pinned: !!note.pinned,
- tags: note.tags || [],
locked: !!note.locked,
notebook: note.notebook || {},
colors: note.colors || [],
+ tags: note.tags || [],
favorite: !!note.favorite,
headline: getNoteHeadline(note),
dateEditted: Date.now(),
@@ -161,13 +161,8 @@ class Database {
if (oldNote) {
note.colors = setManipulator.union(oldNote.colors, note.colors);
- await updateTags.call(
- this,
- setManipulator.complement(note.tags, oldNote.tags)
- );
- note.tags = setManipulator.union(oldNote.tags, note.tags);
} else {
- await updateTags.call(this, note.tags);
+ await addTags.call(this, note.tags);
}
this.notes[timestamp] = note;
@@ -175,12 +170,31 @@ class Database {
return timestamp;
}
- pinItem(type, id) {
- return editItem.call(this, type, id, "pinned");
+ async addTag(noteId, tag) {
+ if (!this.notes[noteId])
+ throw new Error("Couldn't add tag. This note doesn't exist.");
+ this.notes[noteId].tags.push(tag);
+ await addTags.call(this, [tag]);
+ await this.storage.write(KEYS.notes, this.notes);
}
- favoriteItem(type, id) {
- return editItem.call(this, type, id, "favorite");
+ async removeTag(noteId, tag) {
+ if (!this.notes[noteId])
+ throw new Error("Couldn't remove tag. This note doesn't exist.");
+ let tags = this.notes[noteId].tags;
+ if (tags.indexOf(tag) <= -1)
+ throw new Error("This note is not tagged by the specified tag.");
+ this.notes[noteId].tags.splice(tags.indexOf(tag), 1);
+ await removeTags.call(this, [tag]);
+ await this.storage.write(KEYS.notes, this.notes);
+ }
+
+ pinNote(id) {
+ return editItem.call(this, "note", id, "pinned");
+ }
+
+ favoriteNote(id) {
+ return editItem.call(this, "note", id, "favorite");
}
async deleteNotes(...noteIds) {
@@ -293,6 +307,14 @@ class Database {
return id;
}
+ pinNotebook(id) {
+ return editItem.call(this, "notebook", id, "pinned");
+ }
+
+ favoriteNotebook(id) {
+ return editItem.call(this, "notebook", id, "favorite");
+ }
+
addTopicToNotebook(notebookId, topic) {
return notebookTopicFn.call(this, notebookId, topic, notebook => {
if (notebook.topics.findIndex(t => t.title === topic) > -1)
@@ -598,7 +620,7 @@ function getNoteContent(note) {
};
}
-async function updateTags(tags) {
+async function addTags(tags) {
for (let tag of tags) {
if (!tag || tag.trim().length <= 0) continue;
let oldCount = this.tags[tag] ? this.tags[tag].count : 0;
@@ -607,5 +629,21 @@ async function updateTags(tags) {
count: oldCount + 1
};
}
- await this.storage.write(KEYS.tags, this[KEYS.tags]);
+ await this.storage.write(KEYS.tags, this.tags);
+}
+
+async function removeTags(tags) {
+ for (let tag of tags) {
+ if (!tag || tag.trim().length <= 0 || !this.tags[tag]) continue;
+ let oldCount = this.tags[tag].count;
+ if (oldCount <= 1) {
+ delete this.tags[tag];
+ } else {
+ this.tags[tag] = {
+ title: tag,
+ count: oldCount - 1
+ };
+ }
+ }
+ await this.storage.write(KEYS.tags, this.tags);
}