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

96 lines
2.0 KiB
JavaScript
Raw Normal View History

2020-02-05 20:57:43 +05:00
import Notes from "../collections/notes";
import { qclone } from "qclone";
2020-02-05 20:57:43 +05:00
export default class Note {
/**
*
* @param {Notes} notes
* @param {Object} note
*/
constructor(notes, note) {
this._note = note;
this._notes = notes;
2020-02-05 20:57:43 +05:00
}
get data() {
return this._note;
2020-02-05 20:57:43 +05:00
}
get headline() {
return this._note.headline;
2020-02-05 20:57:43 +05:00
}
get title() {
return this._note.title;
2020-02-05 20:57:43 +05:00
}
get tags() {
return this._note.tags;
2020-02-05 20:57:43 +05:00
}
2020-02-06 22:35:53 +05:00
get colors() {
return this._note.colors;
2020-02-06 22:35:53 +05:00
}
2020-02-05 20:57:43 +05:00
get id() {
return this._note.id;
2020-02-05 20:57:43 +05:00
}
get notebook() {
return this._note.notebook;
2020-02-05 20:57:43 +05:00
}
2020-03-19 11:30:05 +05:00
delta() {
return this._notes._deltaCollection.get(this._note.content.delta);
2020-02-05 20:57:43 +05:00
}
2020-03-19 11:30:05 +05:00
text() {
return this._notes._textCollection.get(this._note.content.text);
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");
2020-02-06 22:35:53 +05:00
}
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");
2020-02-06 22:35:53 +05:00
}
untag(tag) {
return removeTag.call(this, tag, "_tagsCollection", "tags");
2020-02-05 20:57:43 +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() {
return this._toggle("favorite");
2020-02-05 20:57:43 +05:00
}
pin() {
return this._toggle("pinned");
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)
2020-02-06 22:35:53 +05:00
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.addItem(note);
2020-02-06 22:35:53 +05:00
}
async function removeTag(tag, collection, array) {
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.");
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.addItem(note);
2020-02-06 22:35:53 +05:00
}