From 120989b73c8be6b07489fb62714e063779c07075 Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Sun, 1 Nov 2020 18:44:31 +0100 Subject: [PATCH] Improve search movements --- editor/src/index.js | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/editor/src/index.js b/editor/src/index.js index 1d2dbf7..79dd043 100644 --- a/editor/src/index.js +++ b/editor/src/index.js @@ -31,26 +31,39 @@ $(document).on('keydown', '.keyboard-list', function (event) { }) $(document).on('keydown', '#search-input', function (event) { - let $ac = $('#autocomplete'); - let isVisible = $(':visible', $ac) - if (event.key === 'ArrowDown' && isVisible) { - $ac.focus() - $ac.find('li:first-child').addClass('selected') - return false - } + let $ac = $('#autocomplete:visible'); if (event.key === 'Escape') { $(this).val(''); - $ac.fadeOut(); - return false; - } - return true -}) -$(document).on('popup:selected', '#autocomplete', function (event, linkName, resultType, element) { - if (resultType === 'search-result') { - element.find('a')[0].click() - return false; + if ($ac.length) { + $ac.fadeOut(); + return false; + } } + if (event.key === 'Enter') { + _.defer(() => { + let $a = $('#autocomplete li.selected > a'); + $a[0].click() + }) + return true; + } + + if (!$ac.length) { + return true; + } + + if (event.key === 'ArrowDown') { + let x = $ac.find('li.selected').removeClass('selected').next('li').addClass('selected') + 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"}) + return false + } + + return true }) document.querySelectorAll(".page-loader")