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: implement colors
thecodrre1e081fc607f
diff --git a/packages/core/__tests__/notes.test.js b/packages/core/__tests__/notes.test.jsindex e0683bf12..d20097488 100644--- a/packages/core/__tests__/notes.test.js+++ b/packages/core/__tests__/notes.test.js@@ -122,6 +122,25 @@ test("remove tag from note", () =>expect(db.notes.tags.length).toBe(0);}));+test("color a note", () =>+ noteTest().then(async ({ db, id }) => {+ let note = db.notes.note(id);+ await note.color("red");+ expect(note.colors[0]).toBe("red");+ expect(db.notes.colors[0].title).toBe("red");+ }));++test("uncolor a note", () =>+ noteTest().then(async ({ db, id }) => {+ let note = db.notes.note(id);+ await note.color("red");+ expect(note.colors.length).toBe(1);+ expect(db.notes.colors.length).toBe(1);+ await note.uncolor("red");+ expect(note.colors.length).toBe(0);+ expect(db.notes.colors.length).toBe(0);+ }));+test("note with text longer than 150 characters should have ... in the headline", () =>noteTest({content: {diff --git a/packages/core/collections/notes.js b/packages/core/collections/notes.jsindex 3ec5ee64f..e9fb18512 100644--- a/packages/core/collections/notes.js+++ b/packages/core/collections/notes.js@@ -21,7 +21,8 @@ export default class Notes {constructor(context) {this.collection = new CachedCollection(context, "notes");this.deltaStorage = new Storage(context);- this.tagsCollection = new Tags(context);+ this.tagsCollection = new Tags(context, "tags");+ this.colorsCollection = new Tags(context, "colors");}/**@@ -34,6 +35,7 @@ export default class Notes {this.notebooks = notebooks;this.trash = trash;await this.tagsCollection.init();+ await this.colorsCollection.init();}async add(noteArg) {@@ -69,9 +71,11 @@ export default class Notes {dateCreated: note.dateCreated || Date.now()};- if (oldNote) {- // note.colors = setManipulator.union(oldNote.colors, note.colors);- } else {+ if (!oldNote) {+ for (let color of note.colors) {+ await this.colorsCollection.add(color);+ }+for (let tag of note.tags) {await this.tagsCollection.add(tag);}@@ -186,6 +190,10 @@ export default class Notes {return this.tagsCollection.all();}+ get colors() {+ return this.colorsCollection.all();+ }+async move(to, ...noteIds) {if (!to) throw new Error("The destination notebook cannot be undefined.");if (!to.id || !to.topic)diff --git a/packages/core/collections/tags.js b/packages/core/collections/tags.jsindex 97e53f3d8..5a47926a4 100644--- a/packages/core/collections/tags.js+++ b/packages/core/collections/tags.js@@ -1,8 +1,8 @@import CachedCollection from "../database/cached-collection";export default class Tags {- constructor(context) {- this.collection = new CachedCollection(context, "tags");+ constructor(context, name) {+ this.collection = new CachedCollection(context, name);}init() {diff --git a/packages/core/models/note.js b/packages/core/models/note.jsindex 8306f36c5..6e2df2fd4 100644--- a/packages/core/models/note.js+++ b/packages/core/models/note.js@@ -27,6 +27,10 @@ export default class Note {return this.note.tags;}+ get colors() {+ return this.note.colors;+ }+get id() {return this.note.id;}@@ -43,20 +47,18 @@ export default class Note {return this.notes.deltaStorage.read(this.note.id + "_delta");}- async tag(tag) {- if (this.note.tags.indexOf(tag) > -1)- throw new Error("Cannot add a duplicate tag.");- this.note.tags.push(tag);- await this.notes.tagsCollection.add(tag);- await this.notes.collection.addItem(this.note);+ color(color) {+ return addTag.call(this, color, "colorsCollection", "colors");+ }+ uncolor(color) {+ return removeTag.call(this, color, "colorsCollection", "colors");}- async untag(tag) {- if (this.note.tags.indexOf(tag) <= -1)- throw new Error("This note is not tagged by the specified tag.");- this.note.tags.splice(this.note.tags.indexOf(tag), 1);- await this.notes.tagsCollection.remove(tag);- await this.notes.collection.addItem(this.note);+ tag(tag) {+ return addTag.call(this, tag, "tagsCollection", "tags");+ }+ untag(tag) {+ return removeTag.call(this, tag, "tagsCollection", "tags");}async save() {@@ -99,3 +101,19 @@ export default class Note {return { ...this.note, content: JSON.parse(decrypted) };}}++async function addTag(tag, collection, array) {+ if (this.note[array].indexOf(tag) > -1)+ throw new Error("Cannot add a duplicate tag.");+ this.note[array].push(tag);+ await this.notes[collection].add(tag);+ await this.notes.collection.addItem(this.note);+}++async function removeTag(tag, collection, array) {+ if (this.note[array].indexOf(tag) <= -1)+ throw new Error("This note is not tagged by the specified tag.");+ this.note[array].splice(this.note[array].indexOf(tag), 1);+ await this.notes[collection].remove(tag);+ await this.notes.collection.addItem(this.note);+}