From 31ff5555d83710d3d99a17d8df4ff83731e01d83 Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Tue, 17 Nov 2020 00:00:35 +0100 Subject: [PATCH] Fix search --- editor/src/editor.js | 16 ++++++++++++++-- editor/src/index.js | 10 +++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/editor/src/editor.js b/editor/src/editor.js index a876eff..1c327c4 100644 --- a/editor/src/editor.js +++ b/editor/src/editor.js @@ -132,9 +132,21 @@ function showSearchResultsExtended(element, template, searchTool, query, input, let $ul = el('ul', _.map(results, (hit, i) => { - const li = el('li', [document.createTextNode(hit.item.label || hit.item.title)]) + let div = el('div', []); + div.innerHTML = hit.text + const fragment = hit.text ? [div] : [] + + let children = [ + document.createTextNode(hit.title || hit.item.label || hit.item.title), + ...(fragment) + ]; + if (hit.ref) { + children = hit.ref ? [el('a', children)] : children; + children[0].setAttribute('href', '/edit/' + hit.ref) + } + const li = el('li', children) if (selectedPos === i) li.classList.add('selected') - li.dataset['new_page'] = hit.item.title + li.dataset['new_page'] = hit.title || hit.item.title return li }) ) diff --git a/editor/src/index.js b/editor/src/index.js index 79dd043..3b100a3 100644 --- a/editor/src/index.js +++ b/editor/src/index.js @@ -43,7 +43,7 @@ $(document).on('keydown', '#search-input', function (event) { if (event.key === 'Enter') { _.defer(() => { let $a = $('#autocomplete li.selected > a'); - $a[0].click() + if ($a.length) $a[0].click() }) return true; } @@ -54,12 +54,16 @@ $(document).on('keydown', '#search-input', function (event) { if (event.key === 'ArrowDown') { let x = $ac.find('li.selected').removeClass('selected').next('li').addClass('selected') - x[0].scrollIntoView({behavior: "smooth", block: "nearest"}) + if (x.length) { + x[0].scrollIntoView({behavior: "smooth", block: "nearest"}) + } return false } if (event.key === 'ArrowUp') { let x = $ac.find('li.selected').removeClass('selected').prev('li').addClass('selected') - x[0].scrollIntoView({behavior: "smooth", block: "nearest"}) + if (x.length) { + x[0].scrollIntoView({behavior: "smooth", block: "nearest"}) + } return false }