File size: 6,979 Bytes
4e1096a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | import clsx from 'clsx';
import React, { useEffect, useState } from 'react';
import { Insets } from '@/types/misc';
import { useEnv } from '@/context/EnvContext';
import { useReaderStore } from '@/store/readerStore';
import { useTranslation } from '@/hooks/useTranslation';
import { useBookDataStore } from '@/store/bookDataStore';
import { formatNumber, formatProgress } from '@/utils/progress';
import { saveViewSettings } from '@/helpers/settings';
import { SIZE_PER_LOC, SIZE_PER_TIME_UNIT } from '@/services/constants';
interface PageInfoProps {
bookKey: string;
horizontalGap: number;
contentInsets: Insets;
gridInsets: Insets;
}
const ProgressInfoView: React.FC<PageInfoProps> = ({
bookKey,
horizontalGap,
contentInsets,
gridInsets,
}) => {
const _ = useTranslation();
const { envConfig, appService } = useEnv();
const { getBookData } = useBookDataStore();
const { getProgress, getViewSettings, getView } = useReaderStore();
const view = getView(bookKey);
const bookData = getBookData(bookKey);
const viewSettings = getViewSettings(bookKey)!;
const progress = getProgress(bookKey);
const { section, pageinfo } = progress || {};
const showDoubleBorder = viewSettings.vertical && viewSettings.doubleBorder;
const isScrolled = viewSettings.scrolled;
const isVertical = viewSettings.vertical;
const isEink = viewSettings.isEink;
const { progressStyle: readingProgressStyle } = viewSettings;
const template =
readingProgressStyle === 'fraction'
? isVertical
? '{current} · {total}'
: '{current} / {total}'
: '{percent}%';
const lang = localStorage?.getItem('i18nextLng') || '';
const localize = isVertical && lang.toLowerCase().startsWith('zh');
const pageInfo = bookData?.isFixedLayout ? section : pageinfo;
const progressInfo = formatProgress(pageInfo?.current, pageInfo?.total, template, localize, lang);
const { page = 0, pages = 0 } = view?.renderer || {};
const current = page;
const total = pages;
const pagesLeft = Math.max(total - current - 1, 0);
const timeLeftStr =
total - 1 > current
? _('{{time}} min left in chapter', {
time: formatNumber(
Math.round((pagesLeft * SIZE_PER_LOC) / SIZE_PER_TIME_UNIT),
localize,
lang,
),
})
: '';
const pagesLeftStr =
total - 1 > current
? localize
? _('{{number}} pages left in chapter', {
number: formatNumber(pagesLeft, localize, lang),
})
: _('{{count}} pages left in chapter', {
count: pagesLeft,
})
: '';
const [progressInfoMode, setProgressInfoMode] = useState(viewSettings.progressInfoMode);
const cycleProgressInfoModes = () => {
if (!viewSettings.tapToToggleFooter) return;
const hasRemainingInfo = viewSettings.showRemainingTime || viewSettings.showRemainingPages;
const hasProgressInfo = viewSettings.showProgressInfo;
const modeSequence: (typeof progressInfoMode)[] = ['all', 'remaining', 'progress', 'none'];
const currentIndex = modeSequence.indexOf(progressInfoMode);
for (let i = 1; i <= modeSequence.length; i++) {
const nextIndex = (currentIndex + i) % modeSequence.length;
const nextMode = modeSequence[nextIndex]!;
const currentRenders = {
remaining:
progressInfoMode === 'all' || progressInfoMode === 'remaining' ? hasRemainingInfo : false,
progress:
progressInfoMode === 'all' || progressInfoMode === 'progress' ? hasProgressInfo : false,
};
const nextRenders = {
remaining: nextMode === 'all' || nextMode === 'remaining' ? hasRemainingInfo : false,
progress: nextMode === 'all' || nextMode === 'progress' ? hasProgressInfo : false,
};
const isDifferent =
currentRenders.remaining !== nextRenders.remaining ||
currentRenders.progress !== nextRenders.progress;
if (isDifferent) {
setProgressInfoMode(nextMode);
return;
}
}
const nextIndex = (currentIndex + 1) % modeSequence.length;
setProgressInfoMode(modeSequence[nextIndex]!);
};
useEffect(() => {
saveViewSettings(envConfig, bookKey, 'progressInfoMode', progressInfoMode);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [progressInfoMode]);
const isMobile = appService?.isMobile || window.innerWidth < 640;
return (
<div
role='presentation'
className={clsx(
'progressinfo absolute bottom-0 flex items-center justify-between font-sans',
isEink ? 'text-sm font-normal' : 'text-neutral-content text-xs font-extralight',
isVertical ? 'writing-vertical-rl' : 'w-full',
isScrolled && !isVertical && 'bg-base-100',
isMobile ? 'pointer-events-auto' : 'pointer-events-none',
)}
onClick={() => cycleProgressInfoModes()}
aria-label={[
progress
? _('On {{current}} of {{total}} page', {
current: current + 1,
total: total,
})
: '',
timeLeftStr,
pagesLeftStr,
]
.filter(Boolean)
.join(', ')}
style={
isVertical
? {
bottom: `${(contentInsets.bottom - gridInsets.bottom) * 1.5}px`,
left: showDoubleBorder
? `calc(${contentInsets.left}px)`
: `calc(${Math.max(0, contentInsets.left - 32)}px)`,
width: showDoubleBorder ? '32px' : `${contentInsets.left}px`,
height: `calc(100% - ${((contentInsets.top + contentInsets.bottom) / 2) * 3}px)`,
}
: {
paddingInlineStart: `calc(${horizontalGap / 2}% + ${contentInsets.left / 2}px)`,
paddingInlineEnd: `calc(${horizontalGap / 2}% + ${contentInsets.right / 2}px)`,
paddingBottom: appService?.hasSafeAreaInset ? `${gridInsets.bottom * 0.33}px` : 0,
}
}
>
<div
aria-hidden='true'
className={clsx(
'flex items-center justify-between',
isVertical ? 'h-full' : 'h-[52px] w-full',
)}
>
{(progressInfoMode === 'all' || progressInfoMode === 'remaining') && (
<>
{viewSettings.showRemainingTime ? (
<span className='time-left-label text-start'>{timeLeftStr}</span>
) : viewSettings.showRemainingPages ? (
<span className='pages-left-label text-start'>{pagesLeftStr}</span>
) : null}
</>
)}
{(progressInfoMode === 'all' || progressInfoMode === 'progress') && (
<>
{viewSettings.showProgressInfo && (
<span
className={clsx('progress-info-label text-end', isVertical ? 'mt-auto' : 'ms-auto')}
>
{progressInfo}
</span>
)}
</>
)}
</div>
</div>
);
};
export default ProgressInfoView;
|