From df8f2a477a3200a8d31409bd482fec97aa79549b Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Fri, 10 Oct 2025 21:42:44 +0300 Subject: [PATCH] feat: prevent scrolling of containers when using focused number inputs (#4629) * feat: prevent scrolling of containers when using focused number inputs #4607 * feat: enhance number input wheel behavior with slider sync and Firefox support * refactor: optimize number input wheel handler with native DOM methods and type checks * Refactor: Move into dom-handlers folder * refactor: use optional chaining for slider element selection * Simplify file paths * Unlock scroll to edit in misc controls * refactor: throttle wheel event handler updates * refactor: Extract value update into a function * fix: NaN-aware value clamping * fix: Add sanity checks for input value calculations --------- Co-authored-by: Wolfsblvt --- public/index.html | 8 ++--- public/script.js | 2 ++ public/scripts/dom-handlers.js | 66 ++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 public/scripts/dom-handlers.js diff --git a/public/index.html b/public/index.html index b34b87d22..6f0a2b71b 100644 --- a/public/index.html +++ b/public/index.html @@ -253,7 +253,7 @@
-
+
Response (tokens)
@@ -284,7 +284,7 @@
-
+
Context (tokens)
@@ -648,7 +648,7 @@ Max Response Length (tokens)
- +
@@ -4214,7 +4214,7 @@ Token Padding
- +
diff --git a/public/script.js b/public/script.js index 1b2d835d6..400c5c34f 100644 --- a/public/script.js +++ b/public/script.js @@ -270,6 +270,7 @@ import { getSystemMessageByType, initSystemMessages, SAFETY_CHAT, sendSystemMess import { event_types, eventSource } from './scripts/events.js'; import { initAccessibility } from './scripts/a11y.js'; import { applyStreamFadeIn } from './scripts/util/stream-fadein.js'; +import { initDomHandlers } from './scripts/dom-handlers.js'; // API OBJECT FOR EXTERNAL WIRING globalThis.SillyTavern = { @@ -640,6 +641,7 @@ async function firstLoadInit() { showLoader(); registerPromptManagerMigration(); + initDomHandlers(); initStandaloneMode(); initLibraryShims(); addShowdownPatch(showdown); diff --git a/public/scripts/dom-handlers.js b/public/scripts/dom-handlers.js new file mode 100644 index 000000000..c68fd8826 --- /dev/null +++ b/public/scripts/dom-handlers.js @@ -0,0 +1,66 @@ +import { throttle } from './utils.js'; + +export function initDomHandlers() { + handleInputWheel(); +} + +/** + * Trap mouse wheel inside of focused number inputs to prevent scrolling their containers. + * Instead of firing wheel events, manually update both slider and input values. + * This also makes wheel work inside Firefox. + */ +function handleInputWheel() { + const minInterval = 25; // ms + + /** + * Update input and slider values based on wheel delta + * @param {HTMLInputElement} input The number input element + * @param {HTMLInputElement|null} slider The associated range input element, if any + * @param {number} deltaY The wheel deltaY value + */ + function updateValue(input, slider, deltaY) { + const currentValue = parseFloat(input.value); + const step = parseFloat(input.step); + const min = parseFloat(input.min); + const max = parseFloat(input.max); + + // Sanity checks before trying to calculate new value + if (isNaN(currentValue) || isNaN(step) || step <= 0 || deltaY === 0) return; + + // Calculate new value based on wheel movement delta (negative = up, positive = down) + let newValue = currentValue + (deltaY > 0 ? -step : step); + // Ensure it's a multiple of step + newValue = Math.round(newValue / step) * step; + // Ensure it's within the min and max range (NaN-aware) + newValue = !isNaN(min) ? Math.max(newValue, min) : newValue; + newValue = !isNaN(max) ? Math.min(newValue, max) : newValue; + // Simple fix for floating point precision issues + newValue = Math.round(newValue * 1e10) / 1e10; + + // Update both input and slider values + input.value = newValue.toString(); + if (slider) slider.value = newValue.toString(); + // Trigger input event (just ONE) to update any listeners + const inputEvent = new Event('input', { bubbles: true }); + input.dispatchEvent(inputEvent); + } + + const updateValueThrottled = throttle(updateValue, minInterval); + + document.addEventListener('wheel', (e) => { + // Try to carefully narrow down if we even need to fire this handler + const input = document.activeElement instanceof HTMLInputElement ? document.activeElement : null; + if (input && input.type === 'number' && input.hasAttribute('step')) { + const parent = input.closest('.range-block-range-and-counter') ?? input.closest('div') ?? input.parentElement; + const slider = /** @type {HTMLInputElement} */ (parent?.querySelector('input[type="range"]')); + + // Stop propagation for either target + if (e.target === input || (slider && e.target === slider)) { + e.stopPropagation(); + e.preventDefault(); + + updateValueThrottled(input, slider, e.deltaY); + } + } + }, { passive: false }); +}