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

237 lines
6.0 KiB
JavaScript
Raw Normal View History

/*
This file is part of the Notesnook project (https://notesnook.com/)
2023-01-16 13:44:52 +05:00
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-08-30 16:13:11 +05:00
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";
import { CHECK_IDS, checkIsUserPremium } from "../common";
2021-07-12 13:48:48 +05:00
import { addItem, deleteItem } from "../utils/array";
2023-01-16 13:44:52 +05:00
import { formatDate } from "../utils/date";
2023-04-25 11:07:50 +05:00
import qclone from "qclone";
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
* @param {any} note
2020-02-05 20:57:43 +05:00
*/
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;
}
2021-12-29 09:34:20 +05:00
get dateModified() {
return this._note.dateModified;
}
/**
*
* @param {"html"|"md"|"txt"} format - Format to export into
* @param {string} [contentItem=undefined]
* @param {boolean} [template=true]
* @param {string} [rawHTML=undefined] rawHTML - Use this raw content instead of generating itself
* @returns {Promise<string | false | undefined>}
*/
async export(to = "html", contentItem, template = true, rawHTML) {
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,
2022-12-05 16:52:44 +05:00
editedOn: formatDate(this.dateEdited),
headline: this.headline,
2022-12-05 16:52:44 +05:00
createdOn: formatDate(this.data.dateCreated),
tags: this.tags.join(", ")
2020-08-31 14:14:21 +05:00
};
contentItem = contentItem ||
(await this._db.content.raw(this._note.contentId)) || {
type: "tiptap",
data: "<p></p>"
};
let content;
if (to !== "txt") {
const { data, type } = await this._db.content.downloadMedia(
`export-${this.id}`,
contentItem,
false
);
content = getContentFromData(type, data);
} else {
content = getContentFromData(contentItem.type, contentItem.data);
}
switch (to) {
case "html":
templateData.content = rawHTML || content.toHTML();
return template
? await HTMLBuilder.buildHTML(templateData)
: templateData.content;
case "txt":
templateData.content = rawHTML || content.toTXT();
return template
? TextBuilder.buildText(templateData)
: templateData.content;
case "md":
templateData.content = rawHTML || content.toMD();
return template
? MarkdownBuilder.buildMarkdown(templateData)
: templateData.content;
default:
throw new Error("Export format not supported.");
}
}
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);
const duplicateId = await this._db.notes.add({
2023-04-25 11:07:50 +05:00
...qclone(this._note),
2022-03-11 11:48:23 +05:00
id: undefined,
2023-04-25 11:12:34 +05:00
content: content
? {
type: content.type,
data: content.data
}
: undefined,
2022-03-11 11:48:23 +05:00
readonly: false,
favorite: false,
pinned: false,
contentId: null,
title: this._note.title + " (Copy)",
dateEdited: null,
dateCreated: null,
dateModified: null
2022-03-11 11:48:23 +05:00
});
for (const notebook of this._db.relations.to(this._note, "notebook")) {
await this._db.relations.add(notebook, { id: duplicateId, type: "note" });
}
return duplicateId;
2022-03-11 11:48:23 +05:00
}
async color(color) {
if (!(await checkIsUserPremium(CHECK_IDS.noteColor))) return;
if (this._note.color)
await this._db.colors.untag(this._note.color, this._note.id);
await this._db.notes.add({
id: this.id,
color: this._db.colors.sanitize(color)
2020-12-31 14:32:51 +05:00
});
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.add({
id: this.id,
color: undefined
});
2020-02-05 20:57:43 +05:00
}
async tag(tag) {
if (
!this._db.tags.tag(tag) &&
this._db.tags.all.length >= 5 &&
!(await checkIsUserPremium(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.add(this._note);
2020-02-06 22:35:53 +05:00
}
2021-07-12 13:48:48 +05:00
async untag(tag) {
const tagItem = this._db.tags.tag(tag);
if (tagItem && deleteItem(this._note.tags, tagItem.title)) {
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
}
_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
}
localOnly() {
return this._toggle("localOnly");
}
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
}
readonly() {
return this._toggle("readonly");
}
synced() {
return !this.data.contentId || this._db.content.exists(this.data.contentId);
}
2020-02-05 20:57:43 +05:00
}