Use "coerceIn" util function

This commit is contained in:
schroda
2024-12-29 23:00:12 +01:00
parent 530ae1578f
commit 8eae025d8f
3 changed files with 9 additions and 8 deletions

View File

@@ -25,6 +25,7 @@ import Slider from '@mui/material/Slider';
import DialogContentText from '@mui/material/DialogContentText'; import DialogContentText from '@mui/material/DialogContentText';
import InfoIcon from '@mui/icons-material/Info'; import InfoIcon from '@mui/icons-material/Info';
import { SxProps, Theme } from '@mui/material/styles'; import { SxProps, Theme } from '@mui/material/styles';
import { coerceIn } from '@/lib/HelperFunctions.ts';
type BaseProps = { type BaseProps = {
settingTitle: string; settingTitle: string;
@@ -170,10 +171,8 @@ export const NumberSetting = ({
type="number" type="number"
onChange={(e) => { onChange={(e) => {
const newValue = Number(e.target.value); const newValue = Number(e.target.value);
const newValueMinLimit = Math.min(newValue, maxValue ?? newValue); const newValueCoerced = coerceIn(newValue, minValue ?? newValue, maxValue ?? newValue);
const newValueMaxLimit = Math.max(newValueMinLimit, minValue ?? newValueMinLimit); updateValue(newValueCoerced, false);
updateValue(newValueMaxLimit, false);
}} }}
slotProps={{ slotProps={{
input: { input: {

View File

@@ -14,6 +14,7 @@
import { MutableRefObject, useEffect, useRef, useState } from 'react'; import { MutableRefObject, useEffect, useRef, useState } from 'react';
import { ScrollDirection } from '@/modules/core/Core.types.ts'; import { ScrollDirection } from '@/modules/core/Core.types.ts';
import { coerceIn } from '@/lib/HelperFunctions.ts';
type Positions = [OldestPos: number, SecondOldestPos: number, LatestPos: number]; type Positions = [OldestPos: number, SecondOldestPos: number, LatestPos: number];
type ClickTimes = [OldestTime: number, SecondOldestTime: number, LatestTime: number]; type ClickTimes = [OldestTime: number, SecondOldestTime: number, LatestTime: number];
@@ -80,7 +81,7 @@ export const useMouseDragScroll = (
return Math.abs(v0[X]); return Math.abs(v0[X]);
})(); })();
const unitVector = [v0[X] / a0V, v0[Y] / a0V]; const unitVector = [v0[X] / a0V, v0[Y] / a0V];
const a0VCoerced = Math.min(12, Math.max(-12, 1.2 * a0V)); const a0VCoerced = coerceIn(1.2 * a0V, -12, 12);
const t = (Date.now() - previousClickTime.current[LATEST]) / 1000; const t = (Date.now() - previousClickTime.current[LATEST]) / 1000;
const v = const v =
@@ -109,8 +110,8 @@ export const useMouseDragScroll = (
element.scrollHeight - element.clientHeight, element.scrollHeight - element.clientHeight,
]; ];
const newScrollPos = [ const newScrollPos = [
Math.min(maxScrollPos[X], Math.max(isRTL ? -maxScrollPos[X] : 0, scrollAtT0.current[X] - delta[X])), coerceIn(scrollAtT0.current[X] - delta[X], isRTL ? -maxScrollPos[X] : 0, maxScrollPos[X]),
Math.min(maxScrollPos[Y], Math.max(0, scrollAtT0.current[Y] - delta[Y])), coerceIn(scrollAtT0.current[Y] - delta[Y], 0, maxScrollPos[Y]),
]; ];
const isScrollXPossible = newScrollPos[X] !== 0 || newScrollPos[X] !== maxScrollPos[X]; const isScrollXPossible = newScrollPos[X] !== 0 || newScrollPos[X] !== maxScrollPos[X];

View File

@@ -25,6 +25,7 @@ import {
} from '@/modules/reader/utils/ReaderPager.utils.tsx'; } from '@/modules/reader/utils/ReaderPager.utils.tsx';
import { ReaderStatePages } from '@/modules/reader/types/ReaderProgressBar.types.ts'; import { ReaderStatePages } from '@/modules/reader/types/ReaderProgressBar.types.ts';
import { ReaderControls } from '@/modules/reader/services/ReaderControls.ts'; import { ReaderControls } from '@/modules/reader/services/ReaderControls.ts';
import { coerceIn } from '@/lib/HelperFunctions.ts';
export const getInitialReaderPageIndex = ( export const getInitialReaderPageIndex = (
resumeMode: ReaderResumeMode, resumeMode: ReaderResumeMode,
@@ -39,7 +40,7 @@ export const getInitialReaderPageIndex = (
return lastPageIndex; return lastPageIndex;
} }
return Math.max(0, Math.min(lastPageIndex, lastReadPageIndex)); return coerceIn(lastReadPageIndex, 0, lastPageIndex);
}; };
export const getReaderChapterFromCache = (id: ChapterIdInfo['id']): TChapterReader | null => export const getReaderChapterFromCache = (id: ChapterIdInfo['id']): TChapterReader | null =>