Skip to content

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

refactor: cleanup tags related code

thecodrrcc93e4f1c925
diff --git a/packages/core/collections/tags.js b/packages/core/collections/tags.js
index 884ae0a0e..33fa612de 100644
--- a/packages/core/collections/tags.js
+++ b/packages/core/collections/tags.js
@@ -1,7 +1,7 @@
import Collection from "./collection";
import { qclone } from "qclone";
import { makeId } from "../utils/id";
-import { deleteItem, deleteItems } from "../utils/array";
+import { deleteItem, deleteItems, hasItem } from "../utils/array";
import setManipulator from "../utils/set";
export default class Tags extends Collection {
@@ -11,14 +11,9 @@ export default class Tags extends Collection {
}
async add(tagId, ...noteIds) {
- if (!tagId) {
- console.error("tagId cannot be undefined.");
- return;
- }
-
- if (typeof tagId === "object") {
- tagId = tagId.id;
- }
+ if (!tagId) return console.error("tagId cannot be undefined.");
+ if (typeof tagId === "object")
+ throw new Error("tagId cannot be an object: " + JSON.stringify(tagId));
let tag = this.tag(tagId) || {
title: tagId,
@@ -63,11 +58,13 @@ export default class Tags extends Collection {
console.error(`No tag found. Tag id:`, tagId);
return;
}
+
for (let noteId of tag.noteIds) {
const note = this._db.notes.note(noteId);
if (!note) continue;
- await note.untag(tagId);
+ if (hasItem(note.tags, tag.title)) await note.untag(tag.title);
}
+
await this._db.settings.unpin(tagId);
await this._collection.deleteItem(tagId);
}
diff --git a/packages/core/models/note.js b/packages/core/models/note.js
index f7ba24c1c..277dfcb19 100644
--- a/packages/core/models/note.js
+++ b/packages/core/models/note.js
@@ -3,6 +3,7 @@ import HTMLBuilder from "../utils/templates/html/builder";
import TextBuilder from "../utils/templates/text/builder";
import { getContentFromData } from "../content-types";
import { CHECK_IDS, sendCheckUserStatusEvent } from "../common";
+import { addItem, deleteItem } from "../utils/array";
export default class Note {
/**
@@ -114,11 +115,17 @@ export default class Note {
!(await sendCheckUserStatusEvent(CHECK_IDS.noteTag))
)
return;
- return await addTag.call(this, tag);
+
+ let tagItem = await this._db.tags.add(tag, this._note.id);
+ if (addItem(this._note.tags, tagItem.title))
+ await this._db.notes._collection.addItem(this._note);
}
- untag(tag) {
- return removeTag.call(this, tag);
+ async untag(tag) {
+ if (deleteItem(this._note.tags, tag)) {
+ await this._db.notes._collection.addItem(this._note);
+ } else console.error("This note is not tagged by the specified tag.", tag);
+ await this._db.tags.untag(tag, this._note.id);
}
_toggle(prop) {
@@ -133,24 +140,3 @@ export default class Note {
return this._toggle("pinned");
}
}
-
-async function addTag(tag) {
- if (this._note.tags.indexOf(tag) > -1)
- return console.error("Cannot add a duplicate tag.");
-
- let tagItem = await this._db.tags.add(tag, this._note.id);
- let arr = [...this._note.tags, tagItem.title];
- const note = { ...this._note, tags: arr };
- await this._db.notes._collection.addItem(note);
-}
-
-async function removeTag(tag) {
- if (this._note.tags.indexOf(tag) <= -1)
- return console.error("This note is not tagged by the specified tag.");
-
- let arr = [...this._note.tags];
- arr.splice(arr.indexOf(tag), 1);
- const note = { ...this._note, tags: arr };
- await this._db.tags.untag(tag, note.id);
- await this._db.notes._collection.addItem(note);
-}
diff --git a/packages/core/utils/array.js b/packages/core/utils/array.js
index 79d8165b5..26a0528f2 100644
--- a/packages/core/utils/array.js
+++ b/packages/core/utils/array.js
@@ -2,6 +2,13 @@ export function findItemAndDelete(array, predicate) {
return deleteAtIndex(array, array.findIndex(predicate));
}
+export function addItem(array, item) {
+ const index = array.indexOf(item);
+ if (index > -1) return false;
+ array.push(item);
+ return true;
+}
+
export function deleteItem(array, item) {
return deleteAtIndex(array, array.indexOf(item));
}
@@ -16,6 +23,10 @@ export function findById(array, id) {
return array.find((item) => item.id === id);
}
+export function hasItem(array, item) {
+ return array.indexOf(item) > -1;
+}
+
function deleteAtIndex(array, index) {
if (index === -1) return false;
array.splice(index, 1);