2018-04-18 12:26:42 +08:00
|
|
|
const sql = require('./sql');
|
|
|
|
const sqlInit = require('./sql_init');
|
2018-04-19 11:11:30 +08:00
|
|
|
const syncTableService = require('./sync_table');
|
|
|
|
const repository = require('./repository');
|
2018-04-18 12:26:42 +08:00
|
|
|
|
|
|
|
let noteTitles;
|
|
|
|
let noteIds;
|
|
|
|
const childToParent = {};
|
2018-04-19 12:13:55 +08:00
|
|
|
const hideInAutocomplete = {};
|
2018-04-18 12:26:42 +08:00
|
|
|
|
|
|
|
async function load() {
|
|
|
|
noteTitles = await sql.getMap(`SELECT noteId, LOWER(title) FROM notes WHERE isDeleted = 0 AND isProtected = 0`);
|
|
|
|
noteIds = Object.keys(noteTitles);
|
|
|
|
|
|
|
|
const relations = await sql.getRows(`SELECT noteId, parentNoteId FROM branches WHERE isDeleted = 0`);
|
|
|
|
|
|
|
|
for (const rel of relations) {
|
|
|
|
childToParent[rel.noteId] = childToParent[rel.noteId] || [];
|
|
|
|
childToParent[rel.noteId].push(rel.parentNoteId);
|
|
|
|
}
|
2018-04-19 12:13:55 +08:00
|
|
|
|
|
|
|
const hiddenLabels = await sql.getColumn(`SELECT noteId FROM labels WHERE isDeleted = 0 AND name = 'hideInAutocomplete'`);
|
|
|
|
|
|
|
|
for (const noteId of hiddenLabels) {
|
|
|
|
hideInAutocomplete[noteId] = true;
|
|
|
|
}
|
2018-04-18 12:26:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function getResults(query) {
|
|
|
|
if (!noteTitles || query.length <= 2) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const tokens = query.toLowerCase().split(" ");
|
|
|
|
const results = [];
|
|
|
|
|
|
|
|
for (const noteId in noteTitles) {
|
2018-04-19 12:13:55 +08:00
|
|
|
if (hideInAutocomplete[noteId]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-04-18 12:26:42 +08:00
|
|
|
const title = noteTitles[noteId];
|
|
|
|
const foundTokens = [];
|
|
|
|
|
|
|
|
for (const token of tokens) {
|
|
|
|
if (title.includes(token)) {
|
|
|
|
foundTokens.push(token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (foundTokens.length > 0) {
|
|
|
|
const remainingTokens = tokens.filter(token => !foundTokens.includes(token));
|
|
|
|
|
|
|
|
search(childToParent[noteId], remainingTokens, [noteId], results);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 08:56:23 +08:00
|
|
|
results.sort((a, b) => a.title < b.title ? -1 : 1);
|
|
|
|
|
2018-04-18 12:26:42 +08:00
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
function search(noteIds, tokens, path, results) {
|
2018-04-19 08:56:23 +08:00
|
|
|
if (!noteIds || noteIds.length === 0) {
|
2018-04-18 12:26:42 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-19 08:56:23 +08:00
|
|
|
if (tokens.length === 0) {
|
2018-04-19 12:13:55 +08:00
|
|
|
const retPath = getSomePath(noteIds, path);
|
2018-04-18 12:26:42 +08:00
|
|
|
|
2018-04-19 12:13:55 +08:00
|
|
|
if (retPath) {
|
|
|
|
const noteTitle = getNoteTitle(retPath);
|
2018-04-18 12:26:42 +08:00
|
|
|
|
2018-04-19 12:13:55 +08:00
|
|
|
results.push({
|
|
|
|
title: noteTitle,
|
|
|
|
path: retPath.join('/')
|
|
|
|
});
|
2018-04-19 08:56:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const noteId of noteIds) {
|
|
|
|
if (results.length >= 200) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-19 12:13:55 +08:00
|
|
|
if (noteId === 'root' || hideInAutocomplete[noteId]) {
|
2018-04-18 12:26:42 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const title = noteTitles[noteId];
|
|
|
|
const foundTokens = [];
|
|
|
|
|
|
|
|
for (const token of tokens) {
|
|
|
|
if (title.includes(token)) {
|
|
|
|
foundTokens.push(token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (foundTokens.length > 0) {
|
|
|
|
const remainingTokens = tokens.filter(token => !foundTokens.includes(token));
|
|
|
|
|
|
|
|
search(childToParent[noteId], remainingTokens, path.concat([noteId]), results);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
search(childToParent[noteId], tokens, path.concat([noteId]), results);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getNoteTitle(path) {
|
|
|
|
const titles = path.map(noteId => noteTitles[noteId]);
|
|
|
|
|
|
|
|
return titles.join(' / ');
|
|
|
|
}
|
|
|
|
|
2018-04-19 12:13:55 +08:00
|
|
|
function getSomePath(noteIds, path) {
|
|
|
|
for (const noteId of noteIds) {
|
|
|
|
if (noteId === 'root') {
|
|
|
|
path.reverse();
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hideInAutocomplete[noteId]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const parents = childToParent[noteId];
|
|
|
|
|
|
|
|
if (!parents || parents.length === 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const retPath = getSomePath(parents, path.concat([noteId]));
|
|
|
|
|
|
|
|
if (retPath) {
|
|
|
|
return retPath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-19 11:11:30 +08:00
|
|
|
syncTableService.addListener(async (entityName, entityId) => {
|
|
|
|
if (entityName === 'notes') {
|
|
|
|
const note = await repository.getNote(entityId);
|
|
|
|
|
|
|
|
if (note.isDeleted) {
|
|
|
|
delete noteTitles[note.noteId];
|
|
|
|
delete childToParent[note.noteId];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
noteTitles[note.noteId] = note.title;
|
|
|
|
}
|
|
|
|
}
|
2018-04-19 12:13:55 +08:00
|
|
|
else if (entityName === 'labels') {
|
|
|
|
const label = await repository.getLabel(entityId);
|
|
|
|
|
|
|
|
if (label.name === 'hideInAutocomplete') {
|
|
|
|
// we're not using label object directly, since there might be other non-deleted hideInAutocomplete label
|
|
|
|
const hideLabel = await repository.getEntity(`SELECT * FROM labels WHERE isDeleted = 0
|
|
|
|
AND name = 'hideInAutocomplete' AND noteId = ?`, [label.noteId]);
|
|
|
|
|
|
|
|
if (hideLabel) {
|
|
|
|
hideInAutocomplete[label.noteId] = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
delete hideInAutocomplete[label.noteId];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-19 11:11:30 +08:00
|
|
|
});
|
|
|
|
|
2018-04-18 12:26:42 +08:00
|
|
|
sqlInit.dbReady.then(load);
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getResults
|
|
|
|
};
|