diff --git a/src/routes/api/autocomplete.js b/src/routes/api/autocomplete.js index 039758cca..dbf328fe8 100644 --- a/src/routes/api/autocomplete.js +++ b/src/routes/api/autocomplete.js @@ -9,7 +9,7 @@ async function getAutocomplete(req) { return results.map(res => { return { - value: res.title + '(' + res.path + ')', + value: res.title + ' (' + res.path + ')', title: res.title } }); diff --git a/src/services/autocomplete.js b/src/services/autocomplete.js index 0175c74e0..43f0820ca 100644 --- a/src/services/autocomplete.js +++ b/src/services/autocomplete.js @@ -42,30 +42,49 @@ function getResults(query) { } } + results.sort((a, b) => a.title < b.title ? -1 : 1); + return results; } function search(noteIds, tokens, path, results) { - if (!noteIds) { + if (!noteIds || noteIds.length === 0) { + return; + } + + if (tokens.length === 0) { + let curNoteId = noteIds[0]; + + while (curNoteId !== 'root') { + path.push(curNoteId); + + const parents = childToParent[curNoteId]; + + if (!parents || parents.length === 0) { + return; + } + + curNoteId = parents[0]; + } + + path.reverse(); + + const noteTitle = getNoteTitle(path); + + results.push({ + title: noteTitle, + path: path.join('/') + }); + return; } for (const noteId of noteIds) { + if (results.length >= 200) { + return; + } + if (noteId === 'root') { - if (tokens.length === 0) { - const reversedPath = path.slice(); - reversedPath.reverse(); - - const noteTitle = getNoteTitle(reversedPath); - - console.log(noteTitle); - - results.push({ - title: noteTitle, - path: reversedPath.join('/') - }); - } - continue; }