2024-11-03 03:09:00 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) Contributors to the Suwayomi project
|
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { useHotkeys as useHotKeysHook, useHotkeysContext } from 'react-hotkeys-hook';
|
|
|
|
|
import { useEffect } from 'react';
|
2024-12-08 22:28:00 +01:00
|
|
|
import { useTheme } from '@mui/material/styles';
|
2024-11-03 03:09:00 +01:00
|
|
|
import { HOTKEY_SCOPES } from '@/modules/hotkeys/Hotkeys.constants.ts';
|
|
|
|
|
import { ReaderService } from '@/modules/reader/services/ReaderService.ts';
|
2024-12-27 00:30:19 +01:00
|
|
|
import { IReaderSettings, ReaderHotkey } from '@/modules/reader/types/Reader.types.ts';
|
2024-11-03 03:09:00 +01:00
|
|
|
import { useReaderOverlayContext } from '@/modules/reader/contexts/ReaderOverlayContext.tsx';
|
|
|
|
|
import { getNextRotationValue } from '@/modules/core/utils/ValueRotationButton.utils.ts';
|
|
|
|
|
import {
|
2024-12-27 00:30:19 +01:00
|
|
|
CONTINUOUS_READING_MODE_TO_SCROLL_DIRECTION,
|
2024-11-03 03:09:00 +01:00
|
|
|
READER_PAGE_SCALE_MODE_VALUES,
|
|
|
|
|
ReaderScrollAmount,
|
2024-12-17 16:27:20 +01:00
|
|
|
READING_DIRECTION_VALUES,
|
2024-12-17 16:23:09 +01:00
|
|
|
READING_MODE_VALUES,
|
2024-11-03 03:09:00 +01:00
|
|
|
} from '@/modules/reader/constants/ReaderSettings.constants.tsx';
|
|
|
|
|
import { useReaderStateMangaContext } from '@/modules/reader/contexts/state/ReaderStateMangaContext.tsx';
|
|
|
|
|
import { HotkeyScope } from '@/modules/hotkeys/Hotkeys.types.ts';
|
|
|
|
|
import { ReaderControls } from '@/modules/reader/services/ReaderControls.ts';
|
2024-12-27 00:30:19 +01:00
|
|
|
import { ScrollOffset } from '@/modules/core/Core.types.ts';
|
2024-12-11 13:59:50 +01:00
|
|
|
import { getOptionForDirection } from '@/modules/theme/services/ThemeCreator.ts';
|
2024-12-26 02:47:37 +01:00
|
|
|
import { FALLBACK_MANGA } from '@/modules/manga/Manga.constants.ts';
|
2024-12-27 00:30:19 +01:00
|
|
|
import { useReaderAutoScrollContext } from '@/modules/reader/contexts/ReaderAutoScrollContext.tsx';
|
2024-11-03 03:09:00 +01:00
|
|
|
|
|
|
|
|
const useHotkeys = (...args: Parameters<typeof useHotKeysHook>): ReturnType<typeof useHotKeysHook> => {
|
|
|
|
|
const [keys, callback, options, dependencies] = args;
|
|
|
|
|
return useHotKeysHook(keys, callback, { ...options, ...HOTKEY_SCOPES.reader }, dependencies);
|
|
|
|
|
};
|
|
|
|
|
|
2024-12-11 20:48:05 +01:00
|
|
|
const updateSettingCycleThrough = <Setting extends keyof IReaderSettings>(
|
|
|
|
|
updateSetting: ReturnType<typeof ReaderService.useCreateUpdateSetting>,
|
|
|
|
|
deleteSetting: ReturnType<typeof ReaderService.useCreateDeleteSetting>,
|
|
|
|
|
setting: Setting,
|
|
|
|
|
value: IReaderSettings[Setting],
|
|
|
|
|
values: IReaderSettings[Setting][],
|
|
|
|
|
isDefault: boolean,
|
|
|
|
|
isDefaultable: boolean,
|
|
|
|
|
) => {
|
|
|
|
|
if (isDefault) {
|
|
|
|
|
updateSetting(setting, values[0]);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const nextValue = getNextRotationValue(values.indexOf(value), values, isDefaultable);
|
|
|
|
|
|
|
|
|
|
const isDefaultNextValue = nextValue === undefined;
|
|
|
|
|
if (isDefaultNextValue) {
|
|
|
|
|
deleteSetting(setting);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateSetting(setting, nextValue);
|
|
|
|
|
};
|
|
|
|
|
|
2024-11-03 03:09:00 +01:00
|
|
|
export const ReaderHotkeys = ({
|
|
|
|
|
scrollElementRef,
|
|
|
|
|
}: {
|
|
|
|
|
scrollElementRef: React.MutableRefObject<HTMLElement | null>;
|
|
|
|
|
}) => {
|
2024-12-08 22:28:00 +01:00
|
|
|
const { direction: themeDirection } = useTheme();
|
2024-12-09 13:58:17 +01:00
|
|
|
const readerThemeDirection = ReaderService.useGetThemeDirection();
|
2024-11-03 03:09:00 +01:00
|
|
|
const { enableScope, disableScope } = useHotkeysContext();
|
|
|
|
|
const { manga } = useReaderStateMangaContext();
|
2024-12-11 21:04:44 +01:00
|
|
|
const { isVisible, setIsVisible: setIsOverlayVisible } = useReaderOverlayContext();
|
2024-12-16 15:22:21 +01:00
|
|
|
const { hotkeys, pageScaleMode, shouldStretchPage, shouldOffsetDoubleSpreads, readingMode, readingDirection } =
|
|
|
|
|
ReaderService.useSettings();
|
2024-12-27 00:30:19 +01:00
|
|
|
const automaticScrolling = useReaderAutoScrollContext();
|
2024-11-03 03:09:00 +01:00
|
|
|
|
|
|
|
|
const openChapter = ReaderControls.useOpenChapter();
|
|
|
|
|
const openPage = ReaderControls.useOpenPage();
|
|
|
|
|
|
2024-12-26 02:47:37 +01:00
|
|
|
const updateSetting = ReaderService.useCreateUpdateSetting(manga ?? FALLBACK_MANGA);
|
|
|
|
|
const deleteSetting = ReaderService.useCreateDeleteSetting(manga ?? FALLBACK_MANGA);
|
2024-11-03 03:09:00 +01:00
|
|
|
|
|
|
|
|
useHotkeys(hotkeys[ReaderHotkey.PREVIOUS_PAGE], () => openPage('previous'), [openPage]);
|
|
|
|
|
useHotkeys(hotkeys[ReaderHotkey.NEXT_PAGE], () => openPage('next'), [openPage]);
|
|
|
|
|
useHotkeys(
|
|
|
|
|
hotkeys[ReaderHotkey.SCROLL_BACKWARD],
|
|
|
|
|
() =>
|
|
|
|
|
scrollElementRef.current &&
|
|
|
|
|
ReaderControls.scroll(
|
|
|
|
|
ScrollOffset.BACKWARD,
|
|
|
|
|
CONTINUOUS_READING_MODE_TO_SCROLL_DIRECTION[readingMode.value],
|
2024-12-08 22:28:00 +01:00
|
|
|
readingMode.value,
|
2024-11-03 03:09:00 +01:00
|
|
|
readingDirection.value,
|
2024-12-08 22:28:00 +01:00
|
|
|
themeDirection,
|
2024-11-03 03:09:00 +01:00
|
|
|
scrollElementRef.current,
|
2024-12-03 18:00:46 +01:00
|
|
|
openChapter,
|
2024-12-11 21:04:44 +01:00
|
|
|
setIsOverlayVisible,
|
2024-11-03 03:09:00 +01:00
|
|
|
ReaderScrollAmount.SMALL,
|
|
|
|
|
),
|
|
|
|
|
{ preventDefault: true },
|
2024-12-08 22:28:00 +01:00
|
|
|
[readingMode.value, readingDirection.value, themeDirection, openChapter],
|
2024-11-03 03:09:00 +01:00
|
|
|
);
|
|
|
|
|
useHotkeys(
|
|
|
|
|
hotkeys[ReaderHotkey.SCROLL_FORWARD],
|
|
|
|
|
() =>
|
|
|
|
|
scrollElementRef.current &&
|
|
|
|
|
ReaderControls.scroll(
|
|
|
|
|
ScrollOffset.FORWARD,
|
|
|
|
|
CONTINUOUS_READING_MODE_TO_SCROLL_DIRECTION[readingMode.value],
|
2024-12-08 22:28:00 +01:00
|
|
|
readingMode.value,
|
2024-11-03 03:09:00 +01:00
|
|
|
readingDirection.value,
|
2024-12-08 22:28:00 +01:00
|
|
|
themeDirection,
|
2024-11-03 03:09:00 +01:00
|
|
|
scrollElementRef.current,
|
2024-12-03 18:00:46 +01:00
|
|
|
openChapter,
|
2024-12-11 21:04:44 +01:00
|
|
|
setIsOverlayVisible,
|
2024-11-03 03:09:00 +01:00
|
|
|
ReaderScrollAmount.SMALL,
|
|
|
|
|
),
|
|
|
|
|
{ preventDefault: true },
|
2024-12-08 22:28:00 +01:00
|
|
|
[readingMode.value, readingDirection.value, themeDirection, openChapter],
|
2024-11-03 03:09:00 +01:00
|
|
|
);
|
2024-12-09 13:58:17 +01:00
|
|
|
useHotkeys(
|
|
|
|
|
hotkeys[ReaderHotkey.PREVIOUS_CHAPTER],
|
|
|
|
|
() => openChapter(getOptionForDirection('previous', 'next', readerThemeDirection)),
|
|
|
|
|
[openChapter, readerThemeDirection],
|
|
|
|
|
);
|
|
|
|
|
useHotkeys(
|
|
|
|
|
hotkeys[ReaderHotkey.NEXT_CHAPTER],
|
|
|
|
|
() => openChapter(getOptionForDirection('next', 'previous', readerThemeDirection)),
|
|
|
|
|
[openChapter, readerThemeDirection],
|
|
|
|
|
);
|
2024-12-11 21:04:44 +01:00
|
|
|
useHotkeys(hotkeys[ReaderHotkey.TOGGLE_MENU], () => setIsOverlayVisible(!isVisible), [isVisible]);
|
2024-11-03 03:09:00 +01:00
|
|
|
useHotkeys(
|
|
|
|
|
hotkeys[ReaderHotkey.CYCLE_SCALE_TYPE],
|
|
|
|
|
() => {
|
2024-12-11 20:48:05 +01:00
|
|
|
updateSettingCycleThrough(
|
|
|
|
|
updateSetting,
|
|
|
|
|
deleteSetting,
|
|
|
|
|
'pageScaleMode',
|
|
|
|
|
pageScaleMode.value,
|
2024-11-03 03:09:00 +01:00
|
|
|
READER_PAGE_SCALE_MODE_VALUES,
|
2024-12-11 20:48:05 +01:00
|
|
|
pageScaleMode.isDefault,
|
2024-11-03 03:09:00 +01:00
|
|
|
true,
|
|
|
|
|
);
|
|
|
|
|
},
|
2024-12-17 16:23:09 +01:00
|
|
|
[updateSetting, deleteSetting, pageScaleMode.value, pageScaleMode.isDefault],
|
2024-11-03 03:09:00 +01:00
|
|
|
);
|
|
|
|
|
useHotkeys(
|
|
|
|
|
hotkeys[ReaderHotkey.STRETCH_IMAGE],
|
|
|
|
|
() => updateSetting('shouldStretchPage', !shouldStretchPage.value),
|
|
|
|
|
[updateSetting, shouldStretchPage.value],
|
|
|
|
|
);
|
|
|
|
|
useHotkeys(
|
|
|
|
|
hotkeys[ReaderHotkey.OFFSET_SPREAD_PAGES],
|
|
|
|
|
() => updateSetting('shouldOffsetDoubleSpreads', !shouldOffsetDoubleSpreads.value),
|
|
|
|
|
[updateSetting, shouldOffsetDoubleSpreads.value],
|
|
|
|
|
);
|
2024-12-17 16:23:09 +01:00
|
|
|
useHotkeys(
|
|
|
|
|
hotkeys[ReaderHotkey.CYCLE_READING_MODE],
|
|
|
|
|
() => {
|
|
|
|
|
updateSettingCycleThrough(
|
|
|
|
|
updateSetting,
|
|
|
|
|
deleteSetting,
|
|
|
|
|
'readingMode',
|
|
|
|
|
readingMode.value,
|
|
|
|
|
READING_MODE_VALUES,
|
|
|
|
|
readingMode.isDefault,
|
|
|
|
|
true,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
[updateSetting, deleteSetting, readingMode.value, readingMode.isDefault],
|
|
|
|
|
);
|
2024-12-17 16:27:20 +01:00
|
|
|
useHotkeys(
|
|
|
|
|
hotkeys[ReaderHotkey.CYCLE_READING_DIRECTION],
|
|
|
|
|
() => {
|
|
|
|
|
updateSettingCycleThrough(
|
|
|
|
|
updateSetting,
|
|
|
|
|
deleteSetting,
|
|
|
|
|
'readingDirection',
|
|
|
|
|
readingDirection.value,
|
|
|
|
|
READING_DIRECTION_VALUES,
|
|
|
|
|
readingDirection.isDefault,
|
|
|
|
|
true,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
[updateSetting, deleteSetting, readingDirection.value, readingDirection.isDefault],
|
|
|
|
|
);
|
2024-12-27 00:30:19 +01:00
|
|
|
useHotkeys(hotkeys[ReaderHotkey.TOGGLE_AUTO_SCROLL], automaticScrolling.toggleActive, { preventDefault: true }, [
|
|
|
|
|
updateSetting,
|
|
|
|
|
automaticScrolling.toggleActive,
|
|
|
|
|
]);
|
2024-11-03 03:09:00 +01:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
enableScope(HotkeyScope.READER);
|
|
|
|
|
|
|
|
|
|
return () => disableScope(HotkeyScope.READER);
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
};
|