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
feat: add noteIds arrays to tags & colors collection
thecodrr0d2cb3e245b5
diff --git a/packages/core/__tests__/notes.test.js b/packages/core/__tests__/notes.test.jsindex 514d4fdc9..bf73d85d4 100644--- a/packages/core/__tests__/notes.test.js+++ b/packages/core/__tests__/notes.test.js@@ -95,50 +95,6 @@ test("updating empty note should delete it", () =>expect(note).toBeUndefined();}));-test("add tag to note", () =>- noteTest().then(async ({ db, id }) => {- let note = db.notes.note(id);- await note.tag("hello");- note = db.notes.note(id);- expect(note.tags[0]).toBe("hello");- expect(db.tags.all[0].title).toBe("hello");- }));--test("remove tag from note", () =>- noteTest().then(async ({ db, id }) => {- let note = db.notes.note(id);- await note.tag("hello");- note = db.notes.note(id);- expect(note.tags[0]).toBe("hello");- expect(db.tags.all[0].title).toBe("hello");- await note.untag("hello");- note = db.notes.note(id);- expect(note.tags.length).toBe(0);- expect(db.tags.all.length).toBe(0);- }));--test("color a note", () =>- noteTest().then(async ({ db, id }) => {- let note = db.notes.note(id);- await note.color("red");- note = db.notes.note(id);- expect(note.colors[0]).toBe("red");- expect(db.colors.all[0].title).toBe("red");- }));--test("uncolor a note", () =>- noteTest().then(async ({ db, id }) => {- let note = db.notes.note(id);- await note.color("red");- note = db.notes.note(id);- expect(note.colors[0]).toBe("red");- expect(db.colors.all[0].title).toBe("red");- await note.uncolor("red");- note = db.notes.note(id);- expect(note.colors.length).toBe(0);- expect(db.colors.all.length).toBe(0);- }));-test("note with text longer than 150 characters should have ... in the headline", () =>noteTest({content: {@@ -150,27 +106,6 @@ test("note with text longer than 150 characters should have ... in the headline"expect(note.headline.includes("...")).toBe(true);}));-test("get tags", () =>- noteTest({- ...TEST_NOTE,- tags: ["new", "tag", "goes", "here"]- }).then(async ({ db }) => {- expect(db.tags.all.length).toBeGreaterThan(0);- }));--test("get notes in tag", () =>- noteTest({- ...TEST_NOTE,- tags: ["new", "tag", "goes", "here"]- }).then(async ({ db }) => {- expect(db.notes.tagged("tag")[0].tags).toStrictEqual([- "new",- "tag",- "goes",- "here"- ]);- }));-test("get favorite notes", () =>noteTest({favorite: true,diff --git a/packages/core/__tests__/tags.test.js b/packages/core/__tests__/tags.test.jsnew file mode 100644index 000000000..d02554990--- /dev/null+++ b/packages/core/__tests__/tags.test.js@@ -0,0 +1,60 @@+import { noteTest, TEST_NOTE, StorageInterface } from "./utils";++beforeEach(async () => {+ StorageInterface.clear();+});++describe.each([+ ["tag", "untag", "tags", "tagged", ["hello", "tag"]],+ ["color", "uncolor", "colors", "colored", ["red", "blue"]]+])("%s", (action, unaction, collection, filter, values) => {+ test(`${action} a note`, () =>+ noteTest().then(async ({ db, id }) => {+ let note = db.notes.note(id);+ await note[action](values[0]);+ note = db.notes.note(id);+ expect(note[collection][0]).toBe(values[0]);+ expect(db[collection].all[0].title).toBe(values[0]);+ expect(db[collection].all[0].noteIds.length).toBe(1);+ }));++ test(`${action} 2 notes`, () =>+ noteTest().then(async ({ db, id }) => {+ const id2 = await db.notes.add(TEST_NOTE);+ let note = db.notes.note(id);+ await note[action](values[0]);+ note = db.notes.note(id2);+ await note[action](values[0]);+ expect(db[collection].all[0].title).toBe(values[0]);+ expect(db[collection].all[0].noteIds.length).toBe(2);+ }));++ test(`${unaction} from note`, () =>+ noteTest().then(async ({ db, id }) => {+ let note = db.notes.note(id);+ await note[action](values[0]);+ note = db.notes.note(id);+ expect(note[collection][0]).toBe(values[0]);+ expect(db[collection].all[0].title).toBe(values[0]);+ await note[unaction](values[0]);+ note = db.notes.note(id);+ expect(note[collection].length).toBe(0);+ expect(db[collection].all.length).toBe(0);+ }));++ test(`get ${collection}`, () =>+ noteTest({+ ...TEST_NOTE,+ [collection]: values+ }).then(async ({ db }) => {+ expect(db[collection].all.length).toBeGreaterThan(0);+ }));++ test(`get notes in ${action}`, () =>+ noteTest({+ ...TEST_NOTE,+ [collection]: values+ }).then(async ({ db }) => {+ expect(db.notes[filter](values[0])[0][collection]).toStrictEqual(values);+ }));+});diff --git a/packages/core/collections/notes.js b/packages/core/collections/notes.jsindex 6729e0bdf..9d2f68c0a 100644--- a/packages/core/collections/notes.js+++ b/packages/core/collections/notes.js@@ -110,11 +110,11 @@ export default class Notes {if (!oldNote) {for (let color of note.colors) {- await this._colorsCollection.add(color);+ await this._colorsCollection.add(color, id);}for (let tag of note.tags) {- await this._tagsCollection.add(tag);+ await this._tagsCollection.add(tag, id);}}@@ -147,11 +147,15 @@ export default class Notes {}tagged(tag) {- return tfun.filter(`.tags.includes('${tag}')`)(this.all);+ return this._tagsCollection+ .get(tag)+ .map(id => this._collection.getItem(id));}colored(color) {- return tfun.filter(`.colors.includes('${color}')`)(this.all);+ return this._colorsCollection+ .get(color)+ .map(id => this._collection.getItem(id));}group(by, special = false) {@@ -211,10 +215,10 @@ export default class Notes {);}for (let tag of item.tags) {- await this._tagsCollection.remove(tag);+ await this._tagsCollection.remove(tag, id);}for (let color of item.colors) {- await this._colorsCollection.remove(color);+ await this._colorsCollection.remove(color, id);}await this._collection.removeIndex(id);await this._trash.add(item.data);diff --git a/packages/core/collections/tags.js b/packages/core/collections/tags.jsindex 8cb07edae..f15e31e43 100644--- a/packages/core/collections/tags.js+++ b/packages/core/collections/tags.js@@ -1,5 +1,5 @@import CachedCollection from "../database/cached-collection";-+import getId from "../utils/id";export default class Tags {constructor(context, name) {this._collection = new CachedCollection(context, name);@@ -9,16 +9,25 @@ export default class Tags {return this._collection.init();}- async add(id) {- if (!id || id.trim().length <= 0) return;- let tag = this._collection.exists(id)- ? { ...(await this._collection.getItem(id)) }- : {- id,- title: id,- count: 0- };- tag.count++;+ get(tag) {+ const tagItem = this.all.find(t => t.title === tag);+ if (!tagItem) return [];+ return tagItem.noteIds;+ }++ async add(tagTitle, noteId) {+ if (!tagTitle || !noteId) return;+ const oldTag = this.all.find(t => t.title === tagTitle) || {};++ let tag = { ...oldTag, title: tagTitle };+ let id = tag.id || getId();+ let notes = tag.noteIds || [];+ tag = {+ id,+ title: tag.title,+ noteIds: [...notes, noteId]+ };+await this._collection.addItem(tag);}@@ -26,16 +35,14 @@ export default class Tags {return this._collection.getAllItems();}- async remove(id) {- if (!id || id.trim().length <= 0) return;- let tag = this._collection.getItem(id);+ async remove(tagTitle, noteId) {+ if (!tagTitle || !noteId) return;+ const tag = this.all.find(t => t.title === tagTitle);if (!tag) return;- tag = { ...tag };- tag.count--;- if (tag.count === 0) {- await this._collection.removeItem(id);- } else {- await this._collection.addItem(tag);- }+ const noteIndex = tag.noteIds.indexOf(noteId);+ if (noteIndex <= -1) return;+ tag.noteIds.splice(noteIndex, 1);+ if (tag.noteIds.length > 0) await this._collection.addItem(tag);+ await this._collection.removeItem(tag.id);}}diff --git a/packages/core/models/note.js b/packages/core/models/note.jsindex 12759de9d..e1dbf1306 100644--- a/packages/core/models/note.js+++ b/packages/core/models/note.js@@ -84,7 +84,7 @@ async function addTag(tag, collection, array) {throw new Error("Cannot add a duplicate tag.");let arr = [...this._note[array], tag];const note = { ...this._note, [array]: arr };- await this._notes[collection].add(tag);+ await this._notes[collection].add(tag, note.id);await this._notes._collection.addItem(note);}@@ -94,6 +94,6 @@ async function removeTag(tag, collection, array) {let arr = [...this._note[array]];arr.splice(arr.indexOf(tag), 1);const note = { ...this._note, [array]: arr };- await this._notes[collection].remove(tag);+ await this._notes[collection].remove(tag, note.id);await this._notes._collection.addItem(note);}