1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00

Fix slider rounding

This commit is contained in:
Dmitry Lyzo 2023-09-25 23:37:45 +03:00
parent 2946ed4e35
commit 9e8c7d788a
2 changed files with 25 additions and 1 deletions

View file

@ -6,6 +6,7 @@ import './emby-slider.scss';
import 'webcomponents.js/webcomponents-lite'; import 'webcomponents.js/webcomponents-lite';
import '../emby-input/emby-input'; import '../emby-input/emby-input';
import globalize from '../../scripts/globalize'; import globalize from '../../scripts/globalize';
import { decimalCount } from '../../utils/number';
const EmbySliderPrototype = Object.create(HTMLInputElement.prototype); const EmbySliderPrototype = Object.create(HTMLInputElement.prototype);
@ -75,13 +76,23 @@ function mapClientToFraction(range, clientX) {
function mapFractionToValue(range, fraction) { function mapFractionToValue(range, fraction) {
let value = (range.max - range.min) * fraction; let value = (range.max - range.min) * fraction;
let decimals = null;
// Snap to step // Snap to step
if (range.step !== 'any') { if (range.step !== 'any') {
const step = normalizeSliderStep(range); const step = normalizeSliderStep(range);
decimals = decimalCount(step);
value = Math.round(value / step) * step; value = Math.round(value / step) * step;
} }
value += parseFloat(range.min); const min = parseFloat(range.min);
value += min;
if (decimals != null) {
decimals = Math.max(decimals, decimalCount(min));
value = parseFloat(value.toFixed(decimals));
}
return Math.min(Math.max(value, range.min), range.max); return Math.min(Math.max(value, range.min), range.max);
} }

View file

@ -32,3 +32,16 @@ export function toPercent(value: number | null | undefined, locale: string): str
return `${Math.round(value * 100)}%`; return `${Math.round(value * 100)}%`;
} }
/**
* Gets decimal count of a Number.
* @param {number} value Number.
* @returns {number} Decimal count of a Number.
*/
export function decimalCount(value: number): number {
if (Number.isInteger(value)) return 0;
const arr = value.toString().split('.');
return arr[1].length;
}