2020-08-31 14:14:21 +05:00
|
|
|
import MarkdownBuilder from "../utils/templates/markdown/builder";
|
|
|
|
|
import HTMLBuilder from "../utils/templates/html/builder";
|
|
|
|
|
import TextBuilder from "../utils/templates/text/builder";
|
2020-11-04 10:17:37 +05:00
|
|
|
import { getContentFromData } from "../content-types";
|
2020-12-05 13:19:41 +05:00
|
|
|
import { CHECK_IDS, sendCheckUserStatusEvent } from "../common";
|
2020-08-31 10:03:56 +05:00
|
|
|
|
2020-02-05 20:57:43 +05:00
|
|
|
export default class Note {
|
|
|
|
|
/**
|
|
|
|
|
*
|
2020-04-16 02:14:53 +05:00
|
|
|
* @param {import('../api').default} db
|
2020-02-05 20:57:43 +05:00
|
|
|
* @param {Object} note
|
|
|
|
|
*/
|
2020-04-16 02:14:53 +05:00
|
|
|
constructor(note, db) {
|
2020-02-22 21:53:56 +05:00
|
|
|
this._note = note;
|
2020-04-16 02:14:53 +05:00
|
|
|
this._db = db;
|
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
|
|
|
}
|
|
|
|
|
|
2020-12-05 12:38:15 +05:00
|
|
|
get notebooks() {
|
|
|
|
|
return this._note.notebooks;
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
|
2020-03-19 15:14:29 +05:00
|
|
|
get dateEdited() {
|
|
|
|
|
return this._note.dateEdited;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-31 10:05:34 +05:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {"html"|"md"|"txt"} format - Format to export into
|
|
|
|
|
*/
|
2020-08-31 10:03:56 +05:00
|
|
|
async export(to = "html") {
|
2020-08-31 14:14:21 +05:00
|
|
|
const templateData = {
|
|
|
|
|
metadata: this.data,
|
|
|
|
|
title: this.title,
|
|
|
|
|
editedOn: this.dateEdited,
|
|
|
|
|
createdOn: this.data.dateCreated,
|
|
|
|
|
};
|
2020-11-04 10:17:37 +05:00
|
|
|
const { data, type } = await this._db.content.raw(this._note.contentId);
|
|
|
|
|
let content = getContentFromData(type, data);
|
2020-11-11 15:43:09 +05:00
|
|
|
|
2020-12-05 13:19:41 +05:00
|
|
|
if (to !== "txt" && !(await sendCheckUserStatusEvent(CHECK_IDS.noteExport)))
|
2020-11-11 15:43:09 +05:00
|
|
|
return;
|
|
|
|
|
|
2020-08-31 10:03:56 +05:00
|
|
|
switch (to) {
|
|
|
|
|
case "html":
|
2020-11-04 10:17:37 +05:00
|
|
|
templateData.content = content.toHTML();
|
2020-08-31 14:14:21 +05:00
|
|
|
return HTMLBuilder.buildHTML(templateData);
|
2020-08-31 10:03:56 +05:00
|
|
|
case "txt":
|
2020-11-16 15:54:16 +05:00
|
|
|
templateData.content = content.toTXT();
|
2020-08-31 14:14:21 +05:00
|
|
|
return TextBuilder.buildText(templateData);
|
2020-08-31 10:03:56 +05:00
|
|
|
case "md":
|
2020-11-04 10:17:37 +05:00
|
|
|
templateData.content = content.toMD();
|
2020-08-31 14:14:21 +05:00
|
|
|
return MarkdownBuilder.buildMarkdown(templateData);
|
2020-08-31 10:05:34 +05:00
|
|
|
default:
|
|
|
|
|
throw new Error("Export format not supported.");
|
2020-08-31 10:03:56 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-04 10:17:37 +05:00
|
|
|
content() {
|
|
|
|
|
return this._db.content.get(this._note.contentId);
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
|
2020-11-11 15:43:09 +05:00
|
|
|
async color(color) {
|
2020-12-05 13:19:41 +05:00
|
|
|
if (!(await sendCheckUserStatusEvent(CHECK_IDS.noteColor))) return;
|
2020-11-11 15:43:09 +05:00
|
|
|
return await addTag.call(this, color, "colors");
|
2020-02-06 22:35:53 +05:00
|
|
|
}
|
2020-11-11 15:43:09 +05:00
|
|
|
|
2020-02-06 22:35:53 +05:00
|
|
|
uncolor(color) {
|
2020-04-16 02:14:53 +05:00
|
|
|
return removeTag.call(this, color, "colors");
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
|
2020-11-11 15:43:09 +05:00
|
|
|
async tag(tag) {
|
|
|
|
|
if (
|
2020-11-11 20:45:53 +05:00
|
|
|
this._db.tags.all.length >= 5 &&
|
2020-12-05 13:19:41 +05:00
|
|
|
!(await sendCheckUserStatusEvent(CHECK_IDS.noteTag))
|
2020-11-11 15:43:09 +05:00
|
|
|
)
|
|
|
|
|
return;
|
|
|
|
|
return await addTag.call(this, tag, "tags");
|
2020-02-06 22:35:53 +05:00
|
|
|
}
|
2020-11-11 15:43:09 +05:00
|
|
|
|
2020-02-06 22:35:53 +05:00
|
|
|
untag(tag) {
|
2020-04-16 02:14:53 +05:00
|
|
|
return removeTag.call(this, tag, "tags");
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
|
2020-02-22 21:53:56 +05:00
|
|
|
_toggle(prop) {
|
2020-04-16 02:14:53 +05:00
|
|
|
return this._db.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
|
|
|
}
|
|
|
|
|
}
|
2020-02-06 22:35:53 +05:00
|
|
|
|
2020-04-16 02:14:53 +05:00
|
|
|
async function addTag(tag, 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-04-16 02:14:53 +05:00
|
|
|
await this._db[array].add(tag, note.id);
|
|
|
|
|
await this._db.notes._collection.addItem(note);
|
2020-02-06 22:35:53 +05:00
|
|
|
}
|
|
|
|
|
|
2020-04-16 02:14:53 +05:00
|
|
|
async function removeTag(tag, 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-04-16 02:14:53 +05:00
|
|
|
await this._db[array].remove(tag, note.id);
|
|
|
|
|
await this._db.notes._collection.addItem(note);
|
2020-02-06 22:35:53 +05:00
|
|
|
}
|