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) {
|
2020-02-22 21:53:56 +05:00
|
|
|
this._note = note;
|
|
|
|
|
this._notes = notes;
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get data() {
|
2020-02-22 21:53:56 +05:00
|
|
|
return this._note;
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get headline() {
|
2020-02-22 21:53:56 +05:00
|
|
|
return this._note.headline;
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get title() {
|
2020-02-22 21:53:56 +05:00
|
|
|
return this._note.title;
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get tags() {
|
2020-02-22 21:53:56 +05:00
|
|
|
return this._note.tags;
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
|
2020-02-06 22:35:53 +05:00
|
|
|
get colors() {
|
2020-02-22 21:53:56 +05:00
|
|
|
return this._note.colors;
|
2020-02-06 22:35:53 +05:00
|
|
|
}
|
|
|
|
|
|
2020-02-05 20:57:43 +05:00
|
|
|
get id() {
|
2020-02-22 21:53:56 +05:00
|
|
|
return this._note.id;
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get notebook() {
|
2020-02-22 21:53:56 +05:00
|
|
|
return this._note.notebook;
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get text() {
|
2020-02-22 21:53:56 +05:00
|
|
|
return this._note.content.text;
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delta() {
|
2020-02-22 21:53:56 +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) {
|
2020-02-22 21:53:56 +05:00
|
|
|
return addTag.call(this, color, "_colorsCollection", "colors");
|
2020-02-06 22:35:53 +05:00
|
|
|
}
|
|
|
|
|
uncolor(color) {
|
2020-02-22 21:53:56 +05:00
|
|
|
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) {
|
2020-02-22 21:53:56 +05:00
|
|
|
return addTag.call(this, tag, "_tagsCollection", "tags");
|
2020-02-06 22:35:53 +05:00
|
|
|
}
|
|
|
|
|
untag(tag) {
|
2020-02-22 21:53:56 +05:00
|
|
|
return removeTag.call(this, tag, "_tagsCollection", "tags");
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
|
2020-02-22 21:53:56 +05:00
|
|
|
_toggle(prop) {
|
|
|
|
|
return this._notes.add({ id: this._note.id, [prop]: !this._note[prop] });
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
favorite() {
|
2020-02-22 21:53:56 +05:00
|
|
|
return this._toggle("favorite");
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pin() {
|
2020-02-22 21:53:56 +05:00
|
|
|
return this._toggle("pinned");
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async lock(password) {
|
2020-02-07 06:32:39 +05:00
|
|
|
let delta = await this.delta();
|
|
|
|
|
if (delta) {
|
2020-02-22 21:53:56 +05:00
|
|
|
delta = await this._notes._collection.indexer.encrypt(
|
2020-02-07 06:32:39 +05:00
|
|
|
password,
|
|
|
|
|
JSON.stringify(delta)
|
|
|
|
|
);
|
2020-02-22 21:53:56 +05:00
|
|
|
await this._notes._deltaStorage.write(this._note.content.delta, delta);
|
2020-02-07 06:32:39 +05:00
|
|
|
}
|
2020-02-22 21:53:56 +05:00
|
|
|
const note = { ...this._note };
|
|
|
|
|
note.content = await this._notes._collection.indexer.encrypt(
|
2020-02-05 20:57:43 +05:00
|
|
|
password,
|
2020-02-22 21:53:56 +05:00
|
|
|
JSON.stringify(this._note.content)
|
2020-02-05 20:57:43 +05:00
|
|
|
);
|
2020-02-22 21:53:56 +05:00
|
|
|
note.locked = true;
|
|
|
|
|
return await this._notes._collection.addItem(note);
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async unlock(password, perm = false) {
|
2020-02-07 06:15:15 +05:00
|
|
|
let decrypted = JSON.parse(
|
2020-02-22 21:53:56 +05:00
|
|
|
await this._notes._collection.indexer.decrypt(
|
|
|
|
|
password,
|
|
|
|
|
this._note.content
|
|
|
|
|
)
|
2020-02-07 06:15:15 +05:00
|
|
|
);
|
|
|
|
|
let delta = JSON.parse(
|
2020-02-22 21:53:56 +05:00
|
|
|
await this._notes._collection.indexer.decrypt(
|
|
|
|
|
password,
|
|
|
|
|
await this.delta()
|
|
|
|
|
)
|
2020-02-05 20:57:43 +05:00
|
|
|
);
|
|
|
|
|
if (perm) {
|
2020-02-22 21:53:56 +05:00
|
|
|
const note = { ...this._note, locked: false, content: decrypted };
|
|
|
|
|
note.locked = false;
|
|
|
|
|
note.content = decrypted;
|
|
|
|
|
await this._notes._collection.addItem(note);
|
|
|
|
|
await this._notes._deltaStorage.write(note.content.delta, delta);
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
2020-02-07 06:15:15 +05:00
|
|
|
return {
|
2020-02-22 21:53:56 +05:00
|
|
|
...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) {
|
2020-02-22 21:53:56 +05:00
|
|
|
if (this._note[array].indexOf(tag) > -1)
|
2020-02-06 22:35:53 +05:00
|
|
|
throw new Error("Cannot add a duplicate tag.");
|
2020-02-24 23:24:43 +05:00
|
|
|
let arr = [...this._note[array], tag];
|
|
|
|
|
const note = { ...this._note, [array]: arr };
|
2020-02-22 21:53:56 +05:00
|
|
|
await this._notes[collection].add(tag);
|
2020-02-24 19:53:19 +05:00
|
|
|
await this._notes._collection.addItem(note);
|
2020-02-06 22:35:53 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function removeTag(tag, collection, array) {
|
2020-02-22 21:53:56 +05:00
|
|
|
if (this._note[array].indexOf(tag) <= -1)
|
2020-02-06 22:35:53 +05:00
|
|
|
throw new Error("This note is not tagged by the specified tag.");
|
2020-02-24 23:24:43 +05:00
|
|
|
let arr = [...this._note[array]];
|
|
|
|
|
arr.splice(arr.indexOf(tag), 1);
|
|
|
|
|
const note = { ...this._note, [array]: arr };
|
2020-02-22 21:53:56 +05:00
|
|
|
await this._notes[collection].remove(tag);
|
2020-02-24 19:53:19 +05:00
|
|
|
await this._notes._collection.addItem(note);
|
2020-02-06 22:35:53 +05:00
|
|
|
}
|