Skip to content

Commit bc3fd80

Browse files
committed
improve search of nested things
1 parent 0d05ad0 commit bc3fd80

File tree

1 file changed

+40
-15
lines changed

1 file changed

+40
-15
lines changed

templates/script.js

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -136,19 +136,35 @@ function clearSearch() {
136136
search('');
137137
}
138138

139-
function search(query) {
139+
function debounce(func, delay) {
140+
let timeout;
141+
return function() {
142+
const context = this;
143+
const args = arguments;
144+
const later = function() {
145+
timeout = null;
146+
func.apply(context, args);
147+
};
148+
clearTimeout(timeout);
149+
timeout = setTimeout(later, delay);
150+
};
151+
};
152+
153+
function searchActually(query) {
140154
searchQuery = query;
141155
if (!memberFunctionsList && selectedNavTab() == 'entities') {
142156
fetch(`${FLASH_OUTPUT_URL}/functions.json`)
143-
.then(res => res.json())
144-
.then(res => {
145-
memberFunctionsList = res;
146-
search(searchQuery);
147-
});
157+
.then(res => res.json())
158+
.then(res => {
159+
memberFunctionsList = res;
160+
searchActually(searchQuery);
161+
});
148162
}
149163
updateNav();
150164
}
151165

166+
const search = debounce(searchActually, 50);
167+
152168
function getFullName(node) {
153169
let parent = node;
154170
const result = [node.textContent.trim()];
@@ -233,11 +249,14 @@ function furryMatchMany(list, query, separator) {
233249
let score = 0;
234250
let someMatched = false;
235251
let i = 0;
252+
// hack: "::" -> ":"
253+
const queryParts = query.split(separator[0]).filter(x => x !== "");
254+
let queryIndex = 0;
236255
for (const item of list) {
237256
if (matched.length) {
238257
matched += `<span class="scope">${separator}</span>`;
239258
}
240-
const match = furryMatch(item, query);
259+
const match = furryMatch(item, queryParts[Math.min(queryIndex, queryParts.length - 1)]);
241260
if (match) {
242261
matched += match.matched;
243262
score += match.score;
@@ -246,12 +265,20 @@ function furryMatchMany(list, query, separator) {
246265
if (i !== list.length - 1) {
247266
score -= 5;
248267
}
268+
queryIndex++;
269+
if (queryIndex >= queryParts.length) {
270+
score -= 5;
271+
}
249272
}
250273
else {
251274
matched += item;
252275
}
253276
i++;
254277
}
278+
// theres still stuff that wasnt matched
279+
if (queryIndex < queryParts.length) {
280+
someMatched = false;
281+
}
255282
return someMatched ? { score, matched } : undefined;
256283
}
257284

@@ -302,21 +329,19 @@ function updateNav() {
302329
});
303330
if (selectedNavTab() == 'entities') {
304331
memberFunctionsList?.forEach(fun => {
305-
let f = fun.split('::');
306-
const name = f.pop();
307-
const match = furryMatchMany([name], searchQuery, '::');
332+
let funParts = fun.split('::');
333+
const name = funParts.at(-1);
334+
const match = furryMatchMany(funParts, searchQuery, '::');
308335
if (match) {
336+
funParts.pop();
309337
const node = document.createElement('a');
310-
const url = `${FLASH_OUTPUT_URL}/classes/${f.join('/')}#${name.replace(/\s+\([0-9]+\)/, '')}`;
338+
const url = `${FLASH_OUTPUT_URL}/classes/${funParts.join('/')}#${name.replace(/\s+\([0-9]+\)/, '')}`;
311339
node.setAttribute('href', url);
312340
node.addEventListener('click', e => {
313341
navigate(url);
314342
e.preventDefault();
315343
});
316-
f = f.map(a => `<span class="namespace">${a}</span>`);
317-
f.push(match.matched);
318-
node.innerHTML = feather.icons.code.toSvg({ 'class': 'icon class' }) +
319-
f.join('<span class="scope">::</span>');
344+
node.innerHTML = feather.icons.code.toSvg({ 'class': 'icon class' }) + match.matched;
320345
results.push([match.score, node]);
321346
}
322347
});

0 commit comments

Comments
 (0)