2024-09-26 17:02:44 +02: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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-09-28 03:24:16 +02:00
|
|
|
import { useCallback, useMemo } from 'react';
|
|
|
|
|
import { useNavigate } from 'react-router-dom';
|
|
|
|
|
import { Direction, useTheme } from '@mui/material/styles';
|
|
|
|
|
import { TChapterReader } from '@/modules/chapter/Chapter.types.ts';
|
|
|
|
|
import { Chapters } from '@/modules/chapter/services/Chapters.ts';
|
|
|
|
|
import { IReaderSettings, ReadingDirection } from '@/modules/reader/types/Reader.types.ts';
|
2024-09-26 17:02:44 +02:00
|
|
|
import { useReaderStateMangaContext } from '@/modules/reader/contexts/state/ReaderStateMangaContext.tsx';
|
|
|
|
|
import { getReaderSettingsFor, useDefaultReaderSettings } from '@/modules/reader/services/ReaderSettingsMetadata.ts';
|
|
|
|
|
|
2024-09-28 03:24:16 +02:00
|
|
|
const DIRECTION_TO_INVERTED: Record<Direction, Direction> = {
|
|
|
|
|
ltr: 'rtl',
|
|
|
|
|
rtl: 'ltr',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const DIRECTION_TO_READING_DIRECTION: Record<Direction, ReadingDirection> = {
|
|
|
|
|
ltr: ReadingDirection.LTR,
|
|
|
|
|
rtl: ReadingDirection.RTL,
|
|
|
|
|
};
|
|
|
|
|
|
2024-09-26 17:02:44 +02:00
|
|
|
export class ReaderService {
|
2024-09-28 03:24:16 +02:00
|
|
|
static useNavigateToChapter(chapter?: TChapterReader): () => void {
|
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
|
|
|
|
|
return useCallback(() => chapter && navigate(Chapters.getReaderUrl(chapter), { replace: true }), [chapter]);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-26 17:02:44 +02:00
|
|
|
static useSettings(): IReaderSettings {
|
|
|
|
|
const { manga } = useReaderStateMangaContext();
|
2024-09-28 03:24:16 +02:00
|
|
|
const defaultReaderSettings = useDefaultReaderSettings();
|
|
|
|
|
return useMemo(
|
|
|
|
|
() => getReaderSettingsFor(manga ?? { id: -1 }, defaultReaderSettings.settings),
|
|
|
|
|
[manga, defaultReaderSettings],
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static useGetThemeDirection(): Direction {
|
|
|
|
|
const { direction } = useTheme();
|
|
|
|
|
const { readingDirection } = ReaderService.useSettings();
|
2024-09-26 17:02:44 +02:00
|
|
|
|
2024-09-28 03:24:16 +02:00
|
|
|
return DIRECTION_TO_READING_DIRECTION[direction] === readingDirection
|
|
|
|
|
? direction
|
|
|
|
|
: DIRECTION_TO_INVERTED[direction];
|
2024-09-26 17:02:44 +02:00
|
|
|
}
|
|
|
|
|
}
|