replace debounce with other performance improvements

- remove debounce from processHotkey
- replace dom-queries in conditions with vars
- replace some jQuery in conditions with vanilla JS
This commit is contained in:
LenAnderson 2024-03-26 12:09:26 -04:00
parent 92ec45af4b
commit e567aa2c31

View File

@ -884,9 +884,13 @@ export function initRossMods() {
} }
$(document).on('keydown', function (event) { $(document).on('keydown', function (event) {
processHotkeysDebounced(event.originalEvent); processHotkeys(event.originalEvent);
}); });
const processHotkeysDebounced = debounce(processHotkeys);
const hotkeyTargets = {
'send_textarea': sendTextArea,
'dialogue_popup_input': document.querySelector('#dialogue_popup_input'),
};
//Additional hotkeys CTRL+ENTER and CTRL+UPARROW //Additional hotkeys CTRL+ENTER and CTRL+UPARROW
/** /**
@ -894,17 +898,19 @@ export function initRossMods() {
*/ */
function processHotkeys(event) { function processHotkeys(event) {
//Enter to send when send_textarea in focus //Enter to send when send_textarea in focus
if ($(':focus').attr('id') === 'send_textarea') { if (document.activeElement == hotkeyTargets['send_textarea']) {
const sendOnEnter = shouldSendOnEnter(); const sendOnEnter = shouldSendOnEnter();
if (!event.shiftKey && !event.ctrlKey && !event.altKey && event.key == 'Enter' && sendOnEnter) { if (!event.shiftKey && !event.ctrlKey && !event.altKey && event.key == 'Enter' && sendOnEnter) {
event.preventDefault(); event.preventDefault();
sendTextareaMessage(); sendTextareaMessage();
return;
} }
} }
if ($(':focus').attr('id') === 'dialogue_popup_input' && !isMobile()) { if (document.activeElement == hotkeyTargets['dialogue_popup_input'] && !isMobile()) {
if (!event.shiftKey && !event.ctrlKey && event.key == 'Enter') { if (!event.shiftKey && !event.ctrlKey && event.key == 'Enter') {
event.preventDefault(); event.preventDefault();
$('#dialogue_popup_ok').trigger('click'); $('#dialogue_popup_ok').trigger('click');
return;
} }
} }
//ctrl+shift+up to scroll to context line //ctrl+shift+up to scroll to context line
@ -916,6 +922,7 @@ export function initRossMods() {
scrollTop: contextLine.offset().top - $('#chat').offset().top + $('#chat').scrollTop(), scrollTop: contextLine.offset().top - $('#chat').offset().top + $('#chat').scrollTop(),
}, 300); }, 300);
} else { toastr.warning('Context line not found, send a message first!'); } } else { toastr.warning('Context line not found, send a message first!'); }
return;
} }
//ctrl+shift+down to scroll to bottom of chat //ctrl+shift+down to scroll to bottom of chat
if (event.shiftKey && event.ctrlKey && event.key == 'ArrowDown') { if (event.shiftKey && event.ctrlKey && event.key == 'ArrowDown') {
@ -923,6 +930,7 @@ export function initRossMods() {
$('#chat').animate({ $('#chat').animate({
scrollTop: $('#chat').prop('scrollHeight'), scrollTop: $('#chat').prop('scrollHeight'),
}, 300); }, 300);
return;
} }
// Alt+Enter or AltGr+Enter to Continue // Alt+Enter or AltGr+Enter to Continue
@ -930,6 +938,7 @@ export function initRossMods() {
if (is_send_press == false) { if (is_send_press == false) {
console.debug('Continuing with Alt+Enter'); console.debug('Continuing with Alt+Enter');
$('#option_continue').trigger('click'); $('#option_continue').trigger('click');
return;
} }
} }
@ -939,6 +948,7 @@ export function initRossMods() {
if (editMesDone.length > 0) { if (editMesDone.length > 0) {
console.debug('Accepting edits with Ctrl+Enter'); console.debug('Accepting edits with Ctrl+Enter');
editMesDone.trigger('click'); editMesDone.trigger('click');
return;
} else if (is_send_press == false) { } else if (is_send_press == false) {
const skipConfirmKey = 'RegenerateWithCtrlEnter'; const skipConfirmKey = 'RegenerateWithCtrlEnter';
const skipConfirm = LoadLocalBool(skipConfirmKey); const skipConfirm = LoadLocalBool(skipConfirmKey);
@ -965,6 +975,7 @@ export function initRossMods() {
doRegenerate(); doRegenerate();
}); });
} }
return;
} else { } else {
console.debug('Ctrl+Enter ignored'); console.debug('Ctrl+Enter ignored');
} }
@ -973,7 +984,7 @@ export function initRossMods() {
// Helper function to check if nanogallery2's lightbox is active // Helper function to check if nanogallery2's lightbox is active
function isNanogallery2LightboxActive() { function isNanogallery2LightboxActive() {
// Check if the body has the 'nGY2On' class, adjust this based on actual behavior // Check if the body has the 'nGY2On' class, adjust this based on actual behavior
return $('body').hasClass('nGY2_body_scrollbar'); return document.body.classList.contains('nGY2_body_scrollbar');
} }
if (event.key == 'ArrowLeft') { //swipes left if (event.key == 'ArrowLeft') { //swipes left
@ -986,6 +997,7 @@ export function initRossMods() {
!isInputElementInFocus() !isInputElementInFocus()
) { ) {
$('.swipe_left:last').click(); $('.swipe_left:last').click();
return;
} }
} }
if (event.key == 'ArrowRight') { //swipes right if (event.key == 'ArrowRight') { //swipes right
@ -998,13 +1010,14 @@ export function initRossMods() {
!isInputElementInFocus() !isInputElementInFocus()
) { ) {
$('.swipe_right:last').click(); $('.swipe_right:last').click();
return;
} }
} }
if (event.ctrlKey && event.key == 'ArrowUp') { //edits last USER message if chatbar is empty and focused if (event.ctrlKey && event.key == 'ArrowUp') { //edits last USER message if chatbar is empty and focused
if ( if (
$('#send_textarea').val() === '' && hotkeyTargets['send_textarea'].value === '' &&
chatbarInFocus === true && chatbarInFocus === true &&
($('.swipe_right:last').css('display') === 'flex' || $('.last_mes').attr('is_system') === 'true') && ($('.swipe_right:last').css('display') === 'flex' || $('.last_mes').attr('is_system') === 'true') &&
$('#character_popup').css('display') === 'none' && $('#character_popup').css('display') === 'none' &&
@ -1015,6 +1028,7 @@ export function initRossMods() {
const editMes = lastIsUserMes.querySelector('.mes_block .mes_edit'); const editMes = lastIsUserMes.querySelector('.mes_block .mes_edit');
if (editMes !== null) { if (editMes !== null) {
$(editMes).trigger('click'); $(editMes).trigger('click');
return;
} }
} }
} }
@ -1022,7 +1036,7 @@ export function initRossMods() {
if (event.key == 'ArrowUp') { //edits last message if chatbar is empty and focused if (event.key == 'ArrowUp') { //edits last message if chatbar is empty and focused
console.log('got uparrow input'); console.log('got uparrow input');
if ( if (
$('#send_textarea').val() === '' && hotkeyTargets['send_textarea'].value === '' &&
chatbarInFocus === true && chatbarInFocus === true &&
//$('.swipe_right:last').css('display') === 'flex' && //$('.swipe_right:last').css('display') === 'flex' &&
$('.last_mes .mes_buttons').is(':visible') && $('.last_mes .mes_buttons').is(':visible') &&
@ -1033,6 +1047,7 @@ export function initRossMods() {
const editMes = lastMes.querySelector('.mes_block .mes_edit'); const editMes = lastMes.querySelector('.mes_block .mes_edit');
if (editMes !== null) { if (editMes !== null) {
$(editMes).click(); $(editMes).click();
return;
} }
} }
} }