fix: noteId not being removed from color or deleting

This commit is contained in:
thecodrr
2021-01-01 15:03:51 +05:00
parent bf29447630
commit 2208270009
5 changed files with 10 additions and 49 deletions

View File

@@ -111,8 +111,6 @@ describe.each([
verifyIndex(data, db, "notebooks", "notebooks"); verifyIndex(data, db, "notebooks", "notebooks");
verifyIndex(data, db, "delta", "content"); verifyIndex(data, db, "delta", "content");
verifyIndex(data, db, "content", "content"); verifyIndex(data, db, "content", "content");
verifyIndex(data, db, "tags", "tags");
verifyIndex(data, db, "colors", "colors");
verifyIndex(data, db, "trash", "trash"); verifyIndex(data, db, "trash", "trash");
}); });
}); });

View File

@@ -223,8 +223,8 @@ export default class Notes extends Collection {
async _delete(moveToTrash = true, ...ids) { async _delete(moveToTrash = true, ...ids) {
for (let id of ids) { for (let id of ids) {
let item = this.note(id); let item = this.note(id);
const itemData = qclone(item.data);
if (!item) continue; if (!item) continue;
const itemData = qclone(item.data);
if (item.notebooks) { if (item.notebooks) {
for (let notebook of item.notebooks) { for (let notebook of item.notebooks) {
for (let topic of notebook.topics) { for (let topic of notebook.topics) {
@@ -238,8 +238,8 @@ export default class Notes extends Collection {
for (let tag of item.tags) { for (let tag of item.tags) {
await this._db.tags.remove(tag, id); await this._db.tags.remove(tag, id);
} }
if (item.color) { if (item.data.color) {
await this._db.colors.remove(item.color, id); await this._db.colors.remove(item.data.color, id);
} }
await this._collection.removeItem(id); await this._collection.removeItem(id);
if (moveToTrash) await this._db.trash.add(itemData); if (moveToTrash) await this._db.trash.add(itemData);

View File

@@ -10,37 +10,9 @@ export default class Tags extends Collection {
return tagItem; return tagItem;
} }
async merge(tag) {
if (!tag) return;
if (tag.deleted) {
await this._collection.addItem(tag);
return;
}
const oldTag = this.all.find(
(t) => t.id === tag.id || t.title === tag.title
);
if (!oldTag) return await this._collection.addItem(tag);
const deletedIds = set.union(oldTag.deletedIds, tag.deletedIds);
const noteIds = set.difference(
set.union(oldTag.noteIds, tag.noteIds),
deletedIds
);
const dateEdited =
tag.dateEdited > oldTag.dateEdited ? tag.dateEdited : oldTag.dateEdited;
tag = {
...oldTag,
noteIds,
dateEdited,
deletedIds,
};
await this._collection.addItem(tag);
}
async add(tagId, noteId) { async add(tagId, noteId) {
if (!tagId || !noteId) return; if (!tagId || !noteId) new Error("tagId and noteId cannot be falsy.");
let tag = this.all.find((t) => t.id === tagId || t.title === tagId) || { let tag = this.all.find((t) => t.id === tagId || t.title === tagId) || {
title: tagId, title: tagId,
}; };
@@ -50,13 +22,11 @@ export default class Tags extends Collection {
if (notes.find((id) => id === noteId)) return id; if (notes.find((id) => id === noteId)) return id;
let deletedIds = tag.deletedIds || [];
tag = { tag = {
type: "tag", type: "tag",
id, id,
title: tag.title, title: tag.title,
noteIds: [...notes, noteId], noteIds: [...notes, noteId],
deletedIds,
}; };
await this._collection.addItem(tag); await this._collection.addItem(tag);
@@ -72,14 +42,14 @@ export default class Tags extends Collection {
} }
async remove(tagTitle, noteId) { async remove(tagTitle, noteId) {
if (!tagTitle || !noteId) return; if (!tagTitle || !noteId) new Error("tagTitle and noteId cannot be falsy.");
let tag = this.all.find((t) => t.title === tagTitle); let tag = this.all.find((t) => t.title === tagTitle);
if (!tag) return; if (!tag) throw new Error(`No tag with title "${tagTitle}" found.`);
tag = qclone(tag); tag = qclone(tag);
const noteIndex = tag.noteIds.indexOf(noteId); const noteIndex = tag.noteIds.indexOf(noteId);
if (noteIndex <= -1) return; if (noteIndex <= -1)
throw new Error(`No note of id "${noteId}" exists in this tag.`);
tag.noteIds.splice(noteIndex, 1); tag.noteIds.splice(noteIndex, 1);
tag.deletedIds.push(noteId);
if (tag.noteIds.length > 0) await this._collection.addItem(tag); if (tag.noteIds.length > 0) await this._collection.addItem(tag);
else await this._collection.removeItem(tag.id); else await this._collection.removeItem(tag.id);
} }

View File

@@ -137,14 +137,6 @@ export default class Backup {
index: data["notebooks"], index: data["notebooks"],
dbCollection: this._db.notebooks, dbCollection: this._db.notebooks,
}, },
{
index: data["tags"],
dbCollection: this._db.tags,
},
{
index: data["colors"],
dbCollection: this._db.colors,
},
{ {
index: data["trash"], index: data["trash"],
dbCollection: this._db.trash, dbCollection: this._db.trash,

View File

@@ -94,6 +94,7 @@ export default class Note {
} }
async uncolor() { async uncolor() {
if (!this._note.color) return;
await this._db.colors.remove(this._note.color, this._note.id); await this._db.colors.remove(this._note.color, this._note.id);
await this._db.notes._collection.addItem({ await this._db.notes._collection.addItem({
...this._note, ...this._note,