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";
|
2021-10-26 23:05:47 +05:00
|
|
|
import { CHECK_IDS, checkIsUserPremium } from "../common";
|
2021-07-12 13:48:48 +05:00
|
|
|
import { addItem, deleteItem } from "../utils/array";
|
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
|
|
|
}
|
|
|
|
|
|
2021-07-06 12:13:55 +05:00
|
|
|
get deleted() {
|
|
|
|
|
return this._note.deleted;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-19 15:14:29 +05:00
|
|
|
get dateEdited() {
|
|
|
|
|
return this._note.dateEdited;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-29 09:34:20 +05:00
|
|
|
get dateModified() {
|
|
|
|
|
return this._note.dateModified;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-31 10:05:34 +05:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {"html"|"md"|"txt"} format - Format to export into
|
2021-02-20 12:32:41 +05:00
|
|
|
* @param {string?} rawContent - Use this raw content instead of generating itself
|
2020-08-31 10:05:34 +05:00
|
|
|
*/
|
2021-02-20 12:32:41 +05:00
|
|
|
async export(to = "html", rawContent) {
|
2021-10-26 23:05:47 +05:00
|
|
|
if (to !== "txt" && !(await checkIsUserPremium(CHECK_IDS.noteExport)))
|
2021-10-22 11:52:56 +05:00
|
|
|
return false;
|
|
|
|
|
|
2020-08-31 14:14:21 +05:00
|
|
|
const templateData = {
|
|
|
|
|
metadata: this.data,
|
|
|
|
|
title: this.title,
|
|
|
|
|
editedOn: this.dateEdited,
|
2021-04-28 10:35:32 +05:00
|
|
|
headline: this.headline,
|
2020-08-31 14:14:21 +05:00
|
|
|
createdOn: this.data.dateCreated,
|
|
|
|
|
};
|
2021-10-22 11:52:56 +05:00
|
|
|
const contentItem = await this._db.content.raw(this._note.contentId);
|
|
|
|
|
if (!contentItem) return false;
|
|
|
|
|
const { data, type } = await this._db.content.downloadMedia(
|
|
|
|
|
`export-${this.id}`,
|
|
|
|
|
contentItem,
|
|
|
|
|
false
|
|
|
|
|
);
|
2020-11-04 10:17:37 +05:00
|
|
|
let content = getContentFromData(type, data);
|
2020-11-11 15:43:09 +05:00
|
|
|
|
2020-08-31 10:03:56 +05:00
|
|
|
switch (to) {
|
|
|
|
|
case "html":
|
2021-02-20 12:32:41 +05:00
|
|
|
templateData.content = rawContent || content.toHTML();
|
2020-08-31 14:14:21 +05:00
|
|
|
return HTMLBuilder.buildHTML(templateData);
|
2020-08-31 10:03:56 +05:00
|
|
|
case "txt":
|
2021-02-20 12:32:41 +05:00
|
|
|
templateData.content = rawContent || content.toTXT();
|
2020-08-31 14:14:21 +05:00
|
|
|
return TextBuilder.buildText(templateData);
|
2020-08-31 10:03:56 +05:00
|
|
|
case "md":
|
2021-02-20 12:32:41 +05:00
|
|
|
templateData.content = rawContent || 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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-22 11:52:56 +05:00
|
|
|
async content() {
|
|
|
|
|
const content = await this._db.content.raw(this._note.contentId);
|
2022-03-30 15:52:48 +05:00
|
|
|
return content ? content.data : null;
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|
|
|
|
|
|
2022-03-11 11:48:23 +05:00
|
|
|
async duplicate() {
|
|
|
|
|
const content = await this._db.content.raw(this._note.contentId);
|
|
|
|
|
return await this._db.notes.add({
|
|
|
|
|
...this._note,
|
|
|
|
|
id: undefined,
|
|
|
|
|
content: {
|
|
|
|
|
type: content.type,
|
|
|
|
|
data: content.data,
|
|
|
|
|
},
|
|
|
|
|
readonly: false,
|
|
|
|
|
favorite: false,
|
|
|
|
|
pinned: false,
|
|
|
|
|
contentId: null,
|
|
|
|
|
title: this._note.title + " (Copy)",
|
|
|
|
|
dateEdited: null,
|
|
|
|
|
dateCreated: null,
|
|
|
|
|
dateModified: null,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 15:43:09 +05:00
|
|
|
async color(color) {
|
2021-10-26 23:05:47 +05:00
|
|
|
if (!(await checkIsUserPremium(CHECK_IDS.noteColor))) return;
|
2020-12-07 12:25:22 +05:00
|
|
|
await this.uncolor();
|
2020-12-31 14:32:51 +05:00
|
|
|
let tag = await this._db.colors.add(color, this._note.id);
|
2022-02-19 13:23:53 +05:00
|
|
|
await this._db.notes.add({
|
|
|
|
|
id: this.id,
|
2020-12-31 14:32:51 +05:00
|
|
|
color: tag.title,
|
|
|
|
|
});
|
2020-02-06 22:35:53 +05:00
|
|
|
}
|
2020-11-11 15:43:09 +05:00
|
|
|
|
2020-12-06 22:14:33 +05:00
|
|
|
async uncolor() {
|
2021-01-01 15:03:51 +05:00
|
|
|
if (!this._note.color) return;
|
2021-07-12 10:32:35 +05:00
|
|
|
await this._db.colors.untag(this._note.color, this._note.id);
|
2022-02-19 13:23:53 +05:00
|
|
|
await this._db.notes.add({
|
|
|
|
|
id: this.id,
|
2020-12-06 22:14:33 +05:00
|
|
|
color: undefined,
|
|
|
|
|
});
|
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 &&
|
2021-10-26 23:05:47 +05:00
|
|
|
!(await checkIsUserPremium(CHECK_IDS.noteTag))
|
2020-11-11 15:43:09 +05:00
|
|
|
)
|
|
|
|
|
return;
|
2021-07-12 13:48:48 +05:00
|
|
|
|
|
|
|
|
let tagItem = await this._db.tags.add(tag, this._note.id);
|
|
|
|
|
if (addItem(this._note.tags, tagItem.title))
|
2022-02-19 13:23:53 +05:00
|
|
|
await this._db.notes.add(this._note);
|
2020-02-06 22:35:53 +05:00
|
|
|
}
|
2020-11-11 15:43:09 +05:00
|
|
|
|
2021-07-12 13:48:48 +05:00
|
|
|
async untag(tag) {
|
|
|
|
|
if (deleteItem(this._note.tags, tag)) {
|
2022-02-19 13:23:53 +05:00
|
|
|
await this._db.notes.add(this._note);
|
2021-07-12 13:48:48 +05:00
|
|
|
} else console.error("This note is not tagged by the specified tag.", tag);
|
|
|
|
|
await this._db.tags.untag(tag, this._note.id);
|
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
|
|
|
}
|
|
|
|
|
|
2022-03-11 12:40:42 +05:00
|
|
|
localOnly() {
|
|
|
|
|
return this._toggle("localOnly");
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
2022-02-15 10:24:53 +05:00
|
|
|
|
|
|
|
|
readonly() {
|
2022-02-19 13:23:53 +05:00
|
|
|
return this._toggle("readonly");
|
2022-02-15 10:24:53 +05:00
|
|
|
}
|
2020-02-05 20:57:43 +05:00
|
|
|
}
|