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

58 lines
1.3 KiB
JavaScript
Raw Normal View History

2021-02-22 10:48:03 +05:00
import * as fuzzysort from "fuzzysort";
2021-02-20 02:11:32 +05:00
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;
});
2021-02-22 10:48:03 +05:00
return fzs(query, candidates, ["title", "content"]);
2020-03-09 12:39:49 +05:00
}
notebooks(array, query) {
2021-02-22 10:48:03 +05:00
return fzs(query, array, ["title", "description"]);
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-22 10:48:03 +05:00
return fzs(query, array, ["title"]);
2020-03-09 12:39:49 +05:00
}
}
2021-02-22 10:48:03 +05:00
function fzs(query, array, fields) {
return fuzzysort
.go(query, array, { allowTypo: true, keys: fields })
.map((result) => result.obj);
}