Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix readerSettings ranges to match options-values #2504

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 54 additions & 52 deletions src/renderer/reader/components/ReaderSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import * as DockModalIcon from "readium-desktop/renderer/assets/icons/dockmodal-
import * as DoneIcon from "readium-desktop/renderer/assets/icons/done.svg";
import SVG, { ISVGProps } from "readium-desktop/renderer/common/components/SVG";
import { IPdfPlayerColumn, IPdfPlayerScale, IPdfPlayerView } from "../pdf/common/pdfReader.type";
import { IReaderSettingsProps } from "./options-values";
import optionsValues, { IReaderSettingsProps } from "./options-values";
import * as stylesSettings from "readium-desktop/renderer/assets/styles/components/settings.scss";
import * as stylesGlobal from "readium-desktop/renderer/assets/styles/global.scss";
import { useTranslator } from "readium-desktop/renderer/common/hooks/useTranslator";
Expand Down Expand Up @@ -390,63 +390,65 @@ interface ITable {

const Slider = ({ value, option, set }: { value: string, option: ITable, set: (a: Pick<ReaderConfig, "pageMargins" | "wordSpacing" | "letterSpacing" | "paraSpacing" | "lineHeight">) => void }) => {
const [currentSliderValue, setCurrentSliderValue] = React.useState(option.defaultValue);
const [currentIndex, setCurrentIndex] = React.useState(() => optionsValues[option.parameter].findIndex((el) => el === option.defaultValue));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would fail at compile time with TypeScript strictNullChecks (or strict) ... which is not yet activated in this project unfortunately.
I would recommend optionsValues[option.parameter] ==> (optionsValues[option.parameter] || []) and also .findIndex((el) => el === option.defaultValue) ==> .findIndex((el) => el === option.defaultValue) || 0


React.useEffect(() => {
setCurrentSliderValue(value.replace(/rem/g, ""));
}, [value]);
setCurrentSliderValue(value);
const newIndex = optionsValues[option.parameter].findIndex((el) => el === value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same remark as above regarding sanity-checking optionsValues[option.parameter] for undefined.

setCurrentIndex(newIndex);
}, [value, option.parameter]);

const updateValue = (index: number) => {
const newValue = optionsValues[option.parameter][index];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same sanity-checking remark.

setCurrentSliderValue(newValue);
setCurrentIndex(index);
set({ [option.parameter]: newValue } as Pick<ReaderConfig, "pageMargins" | "wordSpacing" | "letterSpacing" | "paraSpacing" | "lineHeight">);
};

const click = (direction: string) => {
const step = option.step;
let newStepValue: number;

if (direction === "out") {
newStepValue = Number(currentSliderValue.replace(/rem/g, "")) - step;
} else {
newStepValue = Number(currentSliderValue.replace(/rem/g, "")) + step;
}
const clampedValue = Math.min(Math.max(newStepValue, option.min), option.max);
const valueToString = clampedValue.toFixed(2);
setCurrentSliderValue(valueToString);
set({ [option.parameter]: valueToString + (option.rem ? "rem" : "") } as Pick<ReaderConfig, "pageMargins" | "wordSpacing" | "letterSpacing" | "paraSpacing" | "lineHeight">);
setCurrentIndex((prevIndex) => {
const newIndex = direction === "out" ? prevIndex - 1 : prevIndex + 1;
if (newIndex >= 0 && newIndex < optionsValues[option.parameter].length) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same sanity-check workaround here ... unless we can "guarantee" through the TypeScript type system that option.parameter is within bounds of the optionsValues array range.

updateValue(newIndex);
}
return prevIndex;
});
};

return (
<section className={stylesSettings.section} key={option.title}>
<div className={stylesSettings.spacing_heading}>
<h4>{option.title}</h4>
<p>
{
currentSliderValue === "0" ? "auto" :
currentSliderValue.includes("rem") ?
currentSliderValue :
currentSliderValue + (option.rem ? "rem" : "")
}</p>
{currentSliderValue === "0" ? "auto" : currentSliderValue}
</p>
</div>
<div className={stylesSettings.size_range}>
<button onClick={() => {
const newValue = "0";
setCurrentSliderValue(newValue);
set({ [option.parameter]: newValue } as Pick<ReaderConfig, "pageMargins" | "wordSpacing" | "letterSpacing" | "paraSpacing" | "lineHeight">);
}
} className={stylesSettings.reset_button} title="default value"><SVG ariaHidden svg={ResetIcon} /></button>
<button onClick={() => click("out")} className={stylesSettings.scale_button}><SVG ariaHidden svg={MinusIcon} /></button>
<button
onClick={() => updateValue(0)}
className={stylesSettings.reset_button}
title="default value"
>
<SVG ariaHidden svg={ResetIcon} />
</button>
<button onClick={() => click("out")} className={stylesSettings.scale_button}>
<SVG ariaHidden svg={MinusIcon} />
</button>
<input
id={option.title}
type="range"
aria-labelledby={option.ariaLabel}
min={option.min}
max={option.max}
min="0"
max={optionsValues[option.parameter].length - 1}
step={option.step}
value={currentIndex}
aria-valuemin={option.ariaValuemin}
value={currentSliderValue}
onChange={(e) => {
const newValue = e.target.value;
setCurrentSliderValue(newValue);
set({ [option.parameter]: newValue + (option.rem ? "rem" : "") } as Pick<ReaderConfig, "pageMargins" | "wordSpacing" | "letterSpacing" | "paraSpacing" | "lineHeight">);
}}
onChange={(e) => updateValue(parseInt(e.target.value, 10))}
className={currentSliderValue === "0" ? stylesSettings.range_inactive : ""}
/>
<button onClick={() => click("in")} className={stylesSettings.scale_button}><SVG ariaHidden svg={PlusIcon} /></button>
<button onClick={() => click("in")} className={stylesSettings.scale_button}>
<SVG ariaHidden svg={PlusIcon} />
</button>
</div>
</section>
);
Expand All @@ -464,9 +466,9 @@ const ReadingSpacing = () => {
{
title: `${__("reader.settings.margin")}`,
ariaLabel: "label_pageMargins",
min: 0.5,
max: 2,
step: 0.25,
min: 0,
max: optionsValues.pageMargins.length - 1,
step: 1,
ariaValuemin: 0,
defaultValue: pageMargins,
parameter: "pageMargins",
Expand All @@ -476,9 +478,9 @@ const ReadingSpacing = () => {
{
title: `${__("reader.settings.wordSpacing")}`,
ariaLabel: "label_wordSpacing",
min: 0.05,
max: 1,
step: 0.05,
min: 0,
max: optionsValues.wordSpacing.length - 1,
step: 1,
ariaValuemin: 0,
defaultValue: wordSpacing,
parameter: "wordSpacing",
Expand All @@ -488,9 +490,9 @@ const ReadingSpacing = () => {
{
title: `${__("reader.settings.letterSpacing")}`,
ariaLabel: "label_letterSpacing",
min: 0.05,
max: 0.5,
step: 0.05,
min: 0,
max: optionsValues.letterSpacing.length - 1,
step: 1,
ariaValuemin: 0,
defaultValue: letterSpacing,
parameter: "letterSpacing",
Expand All @@ -500,9 +502,9 @@ const ReadingSpacing = () => {
{
title: `${__("reader.settings.paraSpacing")}`,
ariaLabel: "label_paraSpacing",
min: 0.5,
max: 3,
step: 0.5,
min: 0,
max: optionsValues.paraSpacing.length - 1,
step: 1,
ariaValuemin: 0,
defaultValue: paraSpacing,
parameter: "paraSpacing",
Expand All @@ -512,9 +514,9 @@ const ReadingSpacing = () => {
{
title: `${__("reader.settings.lineSpacing")}`,
ariaLabel: "label_lineHeight",
min: 0.5,
max: 3,
step: 0.5,
min: 0,
max: optionsValues.lineHeight.length - 1,
step: 1,
ariaValuemin: 0,
defaultValue: lineHeight,
parameter: "lineHeight",
Expand Down