Files
notesnook/packages/core/collections/notebooks.js

112 lines
2.9 KiB
JavaScript
Raw Normal View History

2020-04-15 23:45:56 +05:00
import Collection from "./collection";
2020-02-04 18:27:32 +05:00
import fuzzysearch from "fuzzysearch";
2020-02-05 20:57:43 +05:00
import Notebook from "../models/notebook";
2020-03-02 15:40:20 +05:00
import sort from "fast-sort";
2020-03-19 11:30:05 +05:00
import getId from "../utils/id";
2020-12-05 13:19:41 +05:00
import { CHECK_IDS, sendCheckUserStatusEvent } from "../common";
2021-01-11 12:23:12 +05:00
import { qclone } from "qclone";
2020-02-04 18:27:32 +05:00
var tfun = require("transfun/transfun.js").tfun;
if (!tfun) {
tfun = global.tfun;
}
2020-04-15 23:45:56 +05:00
export default class Notebooks extends Collection {
2020-02-04 18:27:32 +05:00
async add(notebookArg) {
if (!notebookArg) throw new Error("Notebook cannot be undefined or null.");
2020-04-09 16:36:57 +05:00
if (notebookArg.remote) {
return await this._collection.addItem(notebookArg);
}
2020-02-04 18:27:32 +05:00
//TODO reliably and efficiently check for duplicates.
2020-03-18 14:06:20 +05:00
const id = notebookArg.id || getId();
let oldNotebook = this._collection.getItem(id);
2020-02-04 18:27:32 +05:00
if (
!oldNotebook &&
this.all.length >= 3 &&
2020-12-05 13:19:41 +05:00
!(await sendCheckUserStatusEvent(CHECK_IDS.notebookAdd))
)
return;
2020-02-04 18:27:32 +05:00
if (!oldNotebook && !notebookArg.title)
throw new Error("Notebook must contain at least a title.");
let notebook = {
...oldNotebook,
2020-04-09 16:36:57 +05:00
...notebookArg,
2020-02-04 18:27:32 +05:00
};
notebook = {
id,
type: "notebook",
title: notebook.title,
description: notebook.description,
dateCreated: notebook.dateCreated,
dateEdited: notebook.dateEdited,
2020-02-04 18:27:32 +05:00
pinned: !!notebook.pinned,
2020-02-06 18:47:42 +05:00
topics: notebook.topics || [],
totalNotes: notebook.totalNotes || 0,
2020-02-04 18:27:32 +05:00
};
if (
!oldNotebook &&
notebook.topics.findIndex((topic) => topic.title === "General") <= -1
) {
2020-02-06 18:47:42 +05:00
notebook.topics.splice(0, 0, "General");
}
2020-02-04 18:27:32 +05:00
await this._collection.addItem(notebook);
2020-02-06 18:47:42 +05:00
if (!oldNotebook) {
await this.notebook(notebook).topics.add(...notebook.topics);
}
2020-04-09 16:36:57 +05:00
return id;
2020-02-04 18:27:32 +05:00
}
2020-03-23 15:06:12 +05:00
get raw() {
return this._collection.getRaw();
}
2020-02-04 18:27:32 +05:00
get all() {
return sort(this._collection.getItems()).desc((t) => t.pinned);
2020-02-04 18:27:32 +05:00
}
get pinned() {
return tfun.filter(".pinned === true")(this.all);
}
2021-02-16 16:56:06 +05:00
get deleted() {
return tfun.filter(".dateDeleted > 0")(this.raw);
}
2020-02-06 22:46:57 +05:00
/**
*
* @param {string} id The id of the notebook
* @returns {Notebook} The notebook of the given id
*/
2020-02-05 20:57:43 +05:00
notebook(id) {
let notebook = id.type ? id : this._collection.getItem(id);
if (!notebook || notebook.deleted) return;
2020-04-16 02:14:53 +05:00
return new Notebook(notebook, this._db);
2020-02-04 18:27:32 +05:00
}
async delete(...ids) {
for (let id of ids) {
2020-02-05 20:57:43 +05:00
let notebook = this.notebook(id);
if (!notebook) continue;
2021-01-11 12:23:12 +05:00
const notebookData = qclone(notebook.data);
await notebook.topics.delete(...notebook.data.topics);
2020-03-23 13:22:28 +05:00
await this._collection.removeItem(id);
await this._db.settings.unpin(id);
2021-01-11 12:23:12 +05:00
await this._db.trash.add(notebookData);
}
}
2020-02-04 18:27:32 +05:00
filter(query) {
if (!query) return [];
2020-04-09 16:36:57 +05:00
let queryFn = (v) => fuzzysearch(query, v.title + " " + v.description);
2020-02-11 16:28:28 +05:00
if (query instanceof Function) queryFn = query;
return tfun.filter(queryFn)(this.all);
2020-02-04 18:27:32 +05:00
}
}