2021-12-20 09:29:25 +05:00
|
|
|
import { Fzf } from "fzf";
|
2020-03-09 12:39:49 +05:00
|
|
|
|
|
|
|
|
export default class Lookup {
|
2020-03-19 12:38:33 +05:00
|
|
|
/**
|
|
|
|
|
*
|
2020-04-12 11:04:30 +05:00
|
|
|
* @param {import('./index').default} db
|
2020-03-19 12:38:33 +05:00
|
|
|
*/
|
|
|
|
|
constructor(db) {
|
|
|
|
|
this._db = db;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-19 10:42:40 +05:00
|
|
|
async notes(notes, query) {
|
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 || "")
|
2020-11-19 10:42:40 +05:00
|
|
|
);
|
2021-12-20 09:29:25 +05:00
|
|
|
|
|
|
|
|
const items = [];
|
|
|
|
|
for (let i = 0; i < notes.length; ++i) {
|
|
|
|
|
const note = notes[i];
|
|
|
|
|
const item = { note, text: note.title };
|
|
|
|
|
items.push(item);
|
|
|
|
|
if (
|
|
|
|
|
!note.locked &&
|
|
|
|
|
!!note.contentId &&
|
|
|
|
|
contents.hasOwnProperty(note.contentId)
|
|
|
|
|
)
|
|
|
|
|
item.text += " " + contents[note.contentId]["data"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new Fzf(items, {
|
|
|
|
|
selector: (v) => v.text,
|
|
|
|
|
normalize: false,
|
|
|
|
|
})
|
|
|
|
|
.find(query)
|
|
|
|
|
.map((v) => v.item.note);
|
2020-03-09 12:39:49 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notebooks(array, query) {
|
2021-12-20 09:29:25 +05:00
|
|
|
return new Fzf(array, {
|
|
|
|
|
selector: (n) => `${n.title} ${n.description}`,
|
|
|
|
|
})
|
|
|
|
|
.find(query)
|
|
|
|
|
.map((v) => v.item);
|
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-12-20 09:29:25 +05:00
|
|
|
return new Fzf(array, {
|
|
|
|
|
selector: (n) => n.title,
|
|
|
|
|
})
|
|
|
|
|
.find(query)
|
|
|
|
|
.map((v) => v.item);
|
2020-03-09 12:39:49 +05:00
|
|
|
}
|
|
|
|
|
}
|