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

236 lines
5.7 KiB
JavaScript
Raw Normal View History

import CachedCollection from "../database/cached-collection";
2020-02-03 12:03:07 +05:00
import fuzzysearch from "fuzzysearch";
import { groupBy } from "../utils";
import sort from "fast-sort";
2020-02-03 12:03:07 +05:00
import {
getWeekGroupFromTimestamp,
months,
getLastWeekTimestamp
} from "../utils/date";
import Storage from "../database/storage";
2020-02-05 00:17:12 +05:00
import Notebooks from "./notebooks";
2020-02-05 20:57:43 +05:00
import Note from "../models/note";
2020-02-06 16:46:23 +05:00
import Trash from "./trash";
2020-02-03 12:03:07 +05:00
var tfun = require("transfun/transfun.js").tfun;
if (!tfun) {
tfun = global.tfun;
}
export default class Notes {
constructor(context) {
this._collection = new CachedCollection(context, "notes");
this._deltaStorage = new Storage(context);
2020-02-03 12:03:07 +05:00
}
2020-02-05 00:17:12 +05:00
/**
*
* @param {Notebooks} notebooks
2020-02-06 16:46:23 +05:00
* @param {Trash} trash
2020-02-05 00:17:12 +05:00
*/
2020-02-21 22:06:13 +05:00
async init(notebooks, trash, tags, colors) {
await this._collection.init();
this._notebooks = notebooks;
this._trash = trash;
this._tagsCollection = tags;
this._colorsCollection = colors;
2020-02-03 12:03:07 +05:00
}
async add(noteArg) {
if (!noteArg) return;
let id = noteArg.id || Date.now().toString() + "_note";
let oldNote = this._collection.getItem(id);
2020-02-03 12:03:07 +05:00
let note = {
...oldNote,
...noteArg
};
if (isNoteEmpty(note)) {
if (oldNote) await this.delete(id);
return;
}
2020-03-08 11:33:55 +05:00
if (note.content.delta && note.content.delta.ops) {
await this._deltaStorage.write(id + "_delta", note.content.delta);
2020-02-20 11:28:16 +05:00
}
2020-02-03 12:03:07 +05:00
note = {
id,
type: "note",
title: getNoteTitle(note),
2020-03-08 11:33:55 +05:00
content: getNoteContent(note),
2020-02-03 12:03:07 +05:00
pinned: !!note.pinned,
locked: !!note.locked,
notebook: note.notebook || {},
colors: note.colors || [],
tags: note.tags || [],
favorite: !!note.favorite,
headline: getNoteHeadline(note),
dateCreated: note.dateCreated
2020-02-03 12:03:07 +05:00
};
2020-02-06 22:35:53 +05:00
if (!oldNote) {
for (let color of note.colors) {
await this._colorsCollection.add(color);
2020-02-06 22:35:53 +05:00
}
2020-02-03 12:03:07 +05:00
for (let tag of note.tags) {
await this._tagsCollection.add(tag);
2020-02-03 12:03:07 +05:00
}
}
await this._collection.addItem(note);
return note.id;
}
2020-02-06 22:46:57 +05:00
/**
*
* @param {string} id The id of note
* @returns {Note} The note of the given id
*/
2020-02-05 20:57:43 +05:00
note(id) {
if (!id) return;
let note = id.type ? id : this._collection.getItem(id);
if (!note) return;
2020-02-05 20:57:43 +05:00
return new Note(this, note);
2020-02-03 12:03:07 +05:00
}
get all() {
return this._collection.getAllItems();
2020-02-03 12:03:07 +05:00
}
get pinned() {
return tfun.filter(".pinned === true")(this.all);
}
get favorites() {
return tfun.filter(".favorite === true")(this.all);
}
tagged(tag) {
return tfun.filter(`.tags.includes('${tag}')`)(this.all);
}
colored(color) {
return tfun.filter(`.colors.includes('${color}')`)(this.all);
}
group(by, special = false) {
let notes = !special
? tfun.filter(".pinned === false")(this.all)
: this.all;
2020-02-27 16:43:47 +05:00
notes = sort(notes).desc(t => t.dateCreated);
2020-02-03 12:03:07 +05:00
switch (by) {
case "abc":
2020-02-27 16:43:47 +05:00
return groupBy(notes, note => note.title[0].toUpperCase(), special);
2020-02-03 12:03:07 +05:00
case "month":
return groupBy(
notes,
note => months[new Date(note.dateCreated).getMonth()],
special
);
case "week":
return groupBy(
notes,
note => getWeekGroupFromTimestamp(note.dateCreated),
special
);
case "year":
return groupBy(
notes,
note => new Date(note.dateCreated).getFullYear().toString(),
special
);
default:
let timestamps = {
recent: getLastWeekTimestamp(7),
lastWeek: getLastWeekTimestamp(7) - 604800000 //seven day timestamp value
};
return groupBy(
notes,
note =>
note.dateCreated >= timestamps.recent
? "Recent"
: note.dateCreated >= timestamps.lastWeek
? "Last week"
: "Older",
special
);
}
}
async delete(...ids) {
for (let id of ids) {
2020-02-05 20:57:43 +05:00
let item = this.note(id);
if (!item) continue;
if (item.notebook && item.notebook.id && item.notebook.topic) {
await this._collection.transaction(() =>
this._notebooks
2020-02-05 20:57:43 +05:00
.notebook(item.notebook.id)
.topics.topic(item.notebook.topic)
.delete(id)
);
2020-02-03 12:03:07 +05:00
}
for (let tag of item.tags) {
await this._tagsCollection.remove(tag);
2020-02-03 12:03:07 +05:00
}
for (let color of item.colors) {
await this._colorsCollection.remove(color);
}
await this._collection.removeItem(id);
await this._trash.add(item.data);
2020-02-03 12:03:07 +05:00
}
}
2020-02-05 00:17:12 +05:00
async move(to, ...noteIds) {
if (!to) throw new Error("The destination notebook cannot be undefined.");
if (!to.id || !to.topic)
throw new Error(
"The destination notebook must contain notebookId and topic."
);
let topic = this._notebooks.notebook(to.id).topics.topic(to.topic);
2020-02-05 00:17:12 +05:00
if (!topic) throw new Error("No such topic exists.");
2020-03-02 10:29:29 +05:00
await topic.add(...noteIds);
2020-02-05 00:17:12 +05:00
}
2020-02-03 12:03:07 +05:00
}
function isNoteEmpty(note) {
2020-03-09 09:45:54 +05:00
if (!note.content) return true;
const {
title,
content: { delta, text },
locked
} = note;
const isTitleEmpty = !title || !title.trim().length;
const isTextEmpty = !text || !text.trim().length;
const isDeltaEmpty = !delta || !delta.ops;
return !locked && isTitleEmpty && isTextEmpty && isDeltaEmpty;
2020-02-03 12:03:07 +05:00
}
function getNoteHeadline(note) {
if (note.locked) return "";
return (
note.content.text.substring(0, 150) +
(note.content.text.length > 150 ? "..." : "")
);
}
function getNoteTitle(note) {
if (note.title && note.title.length > 0) return note.title.trim();
return note.content.text
.split(" ")
.slice(0, 3)
.join(" ")
.trim();
}
2020-03-08 11:33:55 +05:00
function getNoteContent(note) {
2020-02-03 12:03:07 +05:00
if (note.locked) {
return note.content;
}
2020-02-03 12:03:07 +05:00
return {
2020-03-08 11:33:55 +05:00
text: note.content.text.trim()
2020-02-03 12:03:07 +05:00
};
}