Merge pull request #1872 from Technologicat/fragment-search-chats

Fragment search (a.k.a. swoop) for Manage chat files
This commit is contained in:
Cohee 2024-03-01 21:30:41 +02:00 committed by GitHub
commit 81bf6cb399
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -6200,7 +6200,29 @@ export async function displayPastChats() {
const fileName = chat['file_name'];
const chatContent = rawChats[fileName];
return chatContent && Object.values(chatContent).some(message => message?.mes?.toLowerCase()?.includes(searchQuery.toLowerCase()));
// // Uncomment this to return to old behavior (classical full-substring search).
// return chatContent && Object.values(chatContent).some(message => message?.mes?.toLowerCase()?.includes(searchQuery.toLowerCase()));
// Fragment search a.k.a. swoop (as in `helm-swoop` in the Helm package of Emacs).
// Split a `query` {string} into its fragments {string[]}.
function makeQueryFragments(query) {
let fragments = query.trim().split(/\s+/).map( function (str) { return str.trim(); } );
fragments = [...new Set(fragments)]; // uniques only
// fragments = fragments.filter( function(str) { return str.length >= 3; } ); // Helm does this, but perhaps better if we don't.
fragments = fragments.map( function (str) { return str.toLowerCase(); } );
return fragments;
}
// Check whether `text` {string} includes all of the `fragments` {string[]}.
function matchFragments(fragments, text) {
if (!text) {
return false;
}
return fragments.every(function (item, idx, arr) { return text.includes(item); });
}
const fragments = makeQueryFragments(searchQuery);
// At least one chat message must match *all* the fragments.
// Currently, this doesn't match if the fragment matches are distributed across several chat messages.
return chatContent && Object.values(chatContent).some(message => matchFragments(fragments, message?.mes?.toLowerCase()));
});
console.debug(filteredData);
@ -6249,6 +6271,14 @@ export async function displayPastChats() {
const searchQuery = $(this).val();
debouncedDisplay(searchQuery);
});
// UX convenience: Focus the search field when the Manage Chat Files view opens.
setTimeout(function () {
const textSearchElement = $('#select_chat_search');
textSearchElement.click();
textSearchElement.focus();
textSearchElement.select(); // select content (if any) for easy erasing
}, 200);
}
function selectRightMenuWithAnimation(selectedMenuId) {