Files
notesnook/packages/core/models/note.js

134 lines
2.9 KiB
JavaScript
Raw Normal View History

2020-02-05 20:57:43 +05:00
import Notes from "../collections/notes";
export default class Note {
/**
*
* @param {Notes} notes
* @param {Object} note
*/
constructor(notes, note) {
this.note = note;
this.notes = notes;
}
get data() {
return this.note;
}
get headline() {
return this.note.headline;
}
get title() {
return this.note.title;
}
get tags() {
return this.note.tags;
}
2020-02-06 22:35:53 +05:00
get colors() {
return this.note.colors;
}
2020-02-05 20:57:43 +05:00
get id() {
return this.note.id;
}
get notebook() {
return this.note.notebook;
}
get text() {
return this.note.content.text;
}
delta() {
2020-02-07 06:32:39 +05:00
return this.notes.deltaStorage.read(this.note.id + "_delta");
2020-02-05 20:57:43 +05:00
}
2020-02-06 22:35:53 +05:00
color(color) {
return addTag.call(this, color, "colorsCollection", "colors");
}
uncolor(color) {
return removeTag.call(this, color, "colorsCollection", "colors");
2020-02-05 20:57:43 +05:00
}
2020-02-06 22:35:53 +05:00
tag(tag) {
return addTag.call(this, tag, "tagsCollection", "tags");
}
untag(tag) {
return removeTag.call(this, tag, "tagsCollection", "tags");
2020-02-05 20:57:43 +05:00
}
async save() {
await this.notes.add(this.note);
return this;
}
toggle(prop) {
this.note[prop] = !this.note[prop];
return this.save();
}
favorite() {
return this.toggle("favorite");
}
pin() {
return this.toggle("pinned");
}
async lock(password) {
2020-02-07 06:32:39 +05:00
let delta = await this.delta();
if (delta) {
delta = await this.notes.collection.indexer.encrypt(
password,
JSON.stringify(delta)
);
await this.notes.deltaStorage.write(this.note.content.delta, delta);
}
2020-02-05 20:57:43 +05:00
this.note.content = await this.notes.collection.indexer.encrypt(
password,
JSON.stringify(this.note.content)
);
this.note.locked = true;
return await this.notes.collection.addItem(this.note);
}
async unlock(password, perm = false) {
2020-02-07 06:15:15 +05:00
let decrypted = JSON.parse(
await this.notes.collection.indexer.decrypt(password, this.note.content)
);
let delta = JSON.parse(
await this.notes.collection.indexer.decrypt(password, await this.delta())
2020-02-05 20:57:43 +05:00
);
if (perm) {
this.note.locked = false;
2020-02-07 06:15:15 +05:00
this.note.content = decrypted;
2020-02-05 20:57:43 +05:00
await this.notes.collection.addItem(this.note);
2020-02-07 06:15:15 +05:00
await this.notes.deltaStorage.write(this.note.content.delta, delta);
2020-02-05 20:57:43 +05:00
}
2020-02-07 06:15:15 +05:00
return {
...this.note,
2020-02-07 06:32:39 +05:00
content: { ...decrypted, delta }
2020-02-07 06:15:15 +05:00
};
2020-02-05 20:57:43 +05:00
}
}
2020-02-06 22:35:53 +05:00
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);
}