Cleanup search algo

Former-commit-id: 85dd3833e19e696f2eda8c832e7d49aad96ed2f2 [formerly 5bd2b727539c810464f09fcf273c26d5d610ba55 [formerly ae611f364bb5faff1e729db60deda09f855added]]
Former-commit-id: dfb3c98bd95eeb435e55756051a5f27fb05766de
Former-commit-id: c46bade21134e7ca9c5ad1aed58ca630e14bbca6
This commit is contained in:
Jaifroid 2021-07-11 14:46:42 +01:00
parent 89491f2aad
commit 61e79b52a9
3 changed files with 21 additions and 17 deletions

View File

@ -173,7 +173,7 @@ function intercept(event) {
// DEV: Electron uses the file:// protocol and hacks it to work with SW, but it has CORS issues when using the Fetch API to fetch local files,
// so we must bypass it here if we're fetching a local file
if (/^file:/i.test(event.request.url) && ! (regexpZIMUrlWithNamespace.test(event.request.url) && /\.zim\w{0,2}\//i.test(event.request.url))) return;
console.debug('[SW] Service Worker ' + (event.request.method === "GET" ? 'intercepted ' : 'noted ') + event.request.url, event.request.method);
// console.debug('[SW] Service Worker ' + (event.request.method === "GET" ? 'intercepted ' : 'noted ') + event.request.url, event.request.method);
if (event.request.method !== "GET") return;
event.respondWith(
fromCache(event.request).then(function (response) {

View File

@ -267,16 +267,17 @@ define(['jquery', 'zimArchiveLoader', 'uiUtil', 'util', 'cache', 'images', 'sett
});
// Hide the search results if user moves out of prefix field
document.getElementById('prefix').addEventListener('blur', function () {
if (!searchArticlesFocused) {
appstate.search.state = 'cancelled';
}
// We need to wait one tick for the activeElement to receive focus
setTimeout(function () {
if (!(/^articleList/.test(document.activeElement.id) || /^list-group/.test(document.activeElement.className))) {
document.getElementById('scrollbox').style.height = 0;
document.getElementById('articleListWithHeader').style.display = 'none';
}
}, 1);
if (!searchArticlesFocused) {
appstate.search.state = 'cancelled';
}
// We need to wait one tick for the activeElement to receive focus
setTimeout(function () {
if (!(/^articleList/.test(document.activeElement.id) || /^list-group/.test(document.activeElement.className))) {
document.getElementById('scrollbox').style.height = 0;
document.getElementById('articleListWithHeader').style.display = 'none';
appstate.tempPrefix = '';
}
}, 1);
});
//Add keyboard shortcuts
@ -2749,6 +2750,8 @@ define(['jquery', 'zimArchiveLoader', 'uiUtil', 'util', 'cache', 'images', 'sett
}
window.timeoutKeyUpPrefix = window.setTimeout(function () {
var prefix = document.getElementById('prefix').value;
// console.debug(appstate.tempPrefix);
// console.debug(appstate.search.prefix);
if (prefix === appstate.tempPrefix) return;
if (prefix && prefix.length > 0 && prefix !== appstate.search.prefix) {
appstate.tempPrefix = prefix;

View File

@ -183,17 +183,18 @@ define(['zimfile', 'zimDirEntry', 'util', 'utf8'],
// NB duplicates are removed before processing search array
var startArray = [];
// Regex below breaks the string into the pattern: group 1: alphanumericsearch; group 2: regex beginning with .* or .+, or contained in (?:regex)
var isPrefixRegExp = search.prefix.match(/^((?:[^(.]|\((?!\?:)|\.(?![*+]))+)(\(\?:.+\)|\.[*+].+)$/);
var isPrefixRegExp = search.prefix.match(/^((?:[^(.]|\((?!\?:)|\.(?![*+]))+)(\(\?:.*\)|\.[*+].*)$/);
search.rgxPrefix = null;
var prefix = search.prefix;
if (isPrefixRegExp) {
// User has initiated a regular expression search - note the only regexp special character allowed in the alphanumeric part is \s
search.prefix = isPrefixRegExp[1].replace(/\\s/g, ' ');
prefix = isPrefixRegExp[1].replace(/\\s/g, ' ');
search.rgxPrefix = new RegExp(isPrefixRegExp[1] + isPrefixRegExp[2], 'i');
}
// Ensure a search is done on the string exactly as typed
startArray.push(search.prefix);
startArray.push(prefix);
// Normalize any spacing and make string all lowercase
var prefix = search.prefix.replace(/\s+/g, ' ').toLocaleLowerCase();
prefix = prefix.replace(/\s+/g, ' ').toLocaleLowerCase();
// Add lowercase string with initial uppercase (this is a very common pattern)
startArray.push(prefix.replace(/^./, function (m) {
return m.toLocaleUpperCase();
@ -301,11 +302,11 @@ define(['zimfile', 'zimDirEntry', 'util', 'utf8'],
});
}, true).then(function(firstIndex) {
var dirEntries = [];
console.debug('Scanning for "' + prefix + '"...');
var addDirEntries = function(index, lastTitle) {
if (search.status === 'cancelled' || index >= firstIndex + searchWindow || index >= articleCount
|| lastTitle && !~lastTitle.indexOf(prefix) || search.found >= search.resultSize) {
console.debug('Sacnned ' + (index - firstIndex) + ' titles.' );
var cnt = index - firstIndex;
if (cnt) console.debug('Sacnned ' + cnt + ' titles for "' + prefix + '"');
return {
'dirEntries': dirEntries,
'nextStart': index