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

147 lines
3.5 KiB
JavaScript
Raw Normal View History

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";
import { getContentFromData } from "../content-types";
2020-12-05 13:19:41 +05:00
import { CHECK_IDS, sendCheckUserStatusEvent } from "../common";
2021-07-12 13:48:48 +05:00
import { addItem, deleteItem } from "../utils/array";
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) {
this._note = note;
2020-04-16 02:14:53 +05:00
this._db = db;
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 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;
}
get dateEdited() {
return this._note.dateEdited;
}
/**
*
* @param {"html"|"md"|"txt"} format - Format to export into
* @param {string?} rawContent - Use this raw content instead of generating itself
*/
async export(to = "html", rawContent) {
2020-08-31 14:14:21 +05:00
const templateData = {
metadata: this.data,
title: this.title,
editedOn: this.dateEdited,
headline: this.headline,
2020-08-31 14:14:21 +05:00
createdOn: this.data.dateCreated,
};
const { data, type } = await this._db.content.raw(this._note.contentId);
let content = getContentFromData(type, data);
2020-12-05 13:19:41 +05:00
if (to !== "txt" && !(await sendCheckUserStatusEvent(CHECK_IDS.noteExport)))
return;
switch (to) {
case "html":
templateData.content = rawContent || content.toHTML();
2020-08-31 14:14:21 +05:00
return HTMLBuilder.buildHTML(templateData);
case "txt":
templateData.content = rawContent || content.toTXT();
2020-08-31 14:14:21 +05:00
return TextBuilder.buildText(templateData);
case "md":
templateData.content = rawContent || content.toMD();
2020-08-31 14:14:21 +05:00
return MarkdownBuilder.buildMarkdown(templateData);
default:
throw new Error("Export format not supported.");
}
}
2021-09-29 10:31:49 +05:00
async content(withAttachments = false) {
const content = await this._db.content.raw(
this._note.contentId,
withAttachments
);
return content.data;
2020-02-05 20:57:43 +05:00
}
async color(color) {
2020-12-05 13:19:41 +05:00
if (!(await sendCheckUserStatusEvent(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);
await this._db.notes._collection.addItem({
...this._note,
color: tag.title,
});
2020-02-06 22:35:53 +05:00
}
async uncolor() {
if (!this._note.color) return;
2021-07-12 10:32:35 +05:00
await this._db.colors.untag(this._note.color, this._note.id);
await this._db.notes._collection.addItem({
...this._note,
color: undefined,
});
2020-02-05 20:57:43 +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))
)
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))
await this._db.notes._collection.addItem(this._note);
2020-02-06 22:35:53 +05:00
}
2021-07-12 13:48:48 +05:00
async untag(tag) {
if (deleteItem(this._note.tags, tag)) {
await this._db.notes._collection.addItem(this._note);
} 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
}
_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() {
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
}
}