Files
notesnook/packages/core/api/lookup.js

67 lines
1.5 KiB
JavaScript
Raw Normal View History

2021-02-20 02:11:32 +05:00
import { search } from "fast-fuzzy";
import { qclone } from "qclone";
2020-11-16 15:54:16 +05:00
import { getContentFromData } from "../content-types";
2020-03-09 12:39:49 +05:00
export default class Lookup {
2020-03-19 12:38:33 +05:00
/**
*
* @param {import('./index').default} db
2020-03-19 12:38:33 +05:00
*/
constructor(db) {
this._db = db;
}
async notes(notes, query) {
2021-02-20 02:11:32 +05:00
notes = qclone(notes);
2021-02-12 10:15:37 +05:00
const contents = await this._db.content.multi(
2021-02-20 02:11:32 +05:00
notes.map((note) => note.contentId || "")
);
2021-02-20 02:11:32 +05:00
const candidates = notes.map((note) => {
note.content = "";
if (!note.locked) {
2021-02-12 10:15:37 +05:00
let content = contents.find((content) => content.id === note.contentId);
2021-02-20 02:11:32 +05:00
if (!content) return note;
content = getContentFromData(content.type, content.data);
2021-02-20 02:11:32 +05:00
note.content = content.toHTML();
}
2021-02-20 02:11:32 +05:00
return note;
});
return search(query, candidates, {
keySelector: (item) => [item.title, item.content],
ignoreCase: false,
});
2020-03-09 12:39:49 +05:00
}
notebooks(array, query) {
2021-02-20 02:11:32 +05:00
return search(query, array, {
keySelector: (item) => {
return [
item.title,
item.description || "",
...item.topics.map((t) => t.title),
];
},
ignoreCase: true,
});
2020-03-09 12:39:49 +05:00
}
topics(array, query) {
return this._byTitle(array, query);
}
tags(array, query) {
return this._byTitle(array, query);
}
trash(array, query) {
return this._byTitle(array, query);
}
_byTitle(array, query) {
2021-02-20 02:11:32 +05:00
return search(query, array, {
keySelector: (item) => item.title || "",
ignoreCase: true,
});
2020-03-09 12:39:49 +05:00
}
}