From ea583e0ff55eff1d3d3d7ae36a67b8285baa5ac8 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Tue, 14 Nov 2023 21:43:08 +0200 Subject: [PATCH] Add fuzzy search to /bg command --- public/scripts/slash-commands.js | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/public/scripts/slash-commands.js b/public/scripts/slash-commands.js index 9fb31fbe9..33efad164 100644 --- a/public/scripts/slash-commands.js +++ b/public/scripts/slash-commands.js @@ -131,7 +131,7 @@ parser.addCommand('?', helpCommandCallback, ['help'], ' – get help on macros, parser.addCommand('name', setNameCallback, ['persona'], '(name) – sets user name and persona avatar (if set)', true, true); parser.addCommand('sync', syncCallback, [], ' – syncs user name in user-attributed messages in the current chat', true, true); parser.addCommand('lock', bindCallback, ['bind'], ' – locks/unlocks a persona (name and avatar) to the current chat', true, true); -parser.addCommand('bg', setBackgroundCallback, ['background'], '(filename) – sets a background according to filename, partial names allowed, will set the first one alphabetically if multiple files begin with the provided argument string', false, true); +parser.addCommand('bg', setBackgroundCallback, ['background'], '(filename) – sets a background according to filename, partial names allowed', false, true); parser.addCommand('sendas', sendMessageAs, [], ` – sends message as a specific character. Uses character avatar if it exists in the characters list. Example that will send "Hello, guys!" from "Chloe":
/sendas Chloe
Hello, guys!
`, true, true); parser.addCommand('sys', sendNarratorMessage, ['nar'], '(text) – sends message as a system narrator', false, true); parser.addCommand('sysname', setNarratorName, [], '(name) – sets a name for future system narrator messages in this chat (display only). Default: System. Leave empty to reset.', true, true); @@ -709,11 +709,23 @@ function setBackgroundCallback(_, bg) { if (!bg) { return; } - console.log('Set background to ' + bg); - const bgElement = $(`.bg_example[bgfile^="${bg.trim()}"`); - if (bgElement.length) { - bgElement.get(0).click(); + console.log('Set background to ' + bg); + + const bgElements = Array.from(document.querySelectorAll(`.bg_example`)).map((x) => ({ element: x, bgfile: x.getAttribute('bgfile') })); + + const fuse = new Fuse(bgElements, { keys: ['bgfile'] }); + const result = fuse.search(bg); + + if (!result.length) { + toastr.error(`No background found with name "${bg}"`); + return; + } + + const bgElement = result[0].item.element; + + if (bgElement instanceof HTMLElement) { + bgElement.click(); } }