Skip to content

Commit

Permalink
Merge pull request #294 from victorsoares96/feat/able-to-change-chara…
Browse files Browse the repository at this point in the history
…cters-per-location

feat: able to change characters per location
  • Loading branch information
victorsoares96 authored Dec 9, 2024
2 parents f514ba6 + 0dee474 commit f414b5b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export default function App() {
| `fullsize` | `boolean` | |
| `waitForLocationsReady` | `boolean` | only render book after locations generated |
| `keepScrollOffsetOnLocationChange` | `boolean` | Prevents scroll top when change location. Works with `scrolled-doc` flow.
| `charactersPerLocation` | `number` | Default is 1600 |

## Hooks

Expand Down
19 changes: 12 additions & 7 deletions src/Reader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Platform } from 'react-native';
import { LoadingFile } from './utils/LoadingFile';
import type { ReaderProps } from './types';
import { View } from './View';
import { useInjectWebVieWVariables } from './hooks/useInjectWebviewVariables';
import { useInjectWebViewVariables } from './hooks/useInjectWebviewVariables';
import { ReaderContext, defaultTheme as initialTheme } from './context';
import { isURL } from './utils/isURL';
import { getSourceType } from './utils/getSourceType';
Expand Down Expand Up @@ -31,6 +31,7 @@ export function Reader({
snap,
spread,
fullsize,
charactersPerLocation,
...rest
}: ReaderProps) {
const {
Expand All @@ -46,7 +47,7 @@ export function Reader({
const allowPopups = onPressExternalLink ? true : rest.allowPopups || false;

const { setIsLoading, isLoading } = useContext(ReaderContext);
const { injectWebVieWVariables } = useInjectWebVieWVariables();
const { injectWebViewVariables } = useInjectWebViewVariables();
const [template, setTemplate] = useState<string | null>(null);
const [templateUrl, setTemplateUrl] = useState<string | null>(null);
const [allowedUris, setAllowedUris] = useState<string | null>(null);
Expand Down Expand Up @@ -86,7 +87,7 @@ export function Reader({
}
if (sourceType === SourceType.BASE64) {
setTemplate(
injectWebVieWVariables({
injectWebViewVariables({
jszip: jszipFileUri,
epubjs: epubjsFileUri,
type: SourceType.BASE64,
Expand All @@ -101,13 +102,14 @@ export function Reader({
snap,
spread,
fullsize,
charactersPerLocation,
})
);

setIsLoading(false);
} else {
setTemplate(
injectWebVieWVariables({
injectWebViewVariables({
jszip: jszipFileUri,
epubjs: epubjsFileUri,
type: SourceType.BINARY,
Expand All @@ -122,6 +124,7 @@ export function Reader({
snap,
spread,
fullsize,
charactersPerLocation,
})
);

Expand All @@ -138,7 +141,7 @@ export function Reader({

if (sourceType === SourceType.OPF || sourceType === SourceType.EPUB) {
setTemplate(
injectWebVieWVariables({
injectWebViewVariables({
jszip: jszipFileUri,
epubjs: epubjsFileUri,
type: sourceType,
Expand All @@ -153,6 +156,7 @@ export function Reader({
snap,
spread,
fullsize,
charactersPerLocation,
})
);

Expand All @@ -165,7 +169,7 @@ export function Reader({
setAllowedUris(`${bookFileUri},${jszipFileUri},${epubjsFileUri}`);

setTemplate(
injectWebVieWVariables({
injectWebViewVariables({
jszip: jszipFileUri,
epubjs: epubjsFileUri,
type: sourceType,
Expand All @@ -180,6 +184,7 @@ export function Reader({
snap,
spread,
fullsize,
charactersPerLocation,
})
);

Expand All @@ -197,7 +202,7 @@ export function Reader({
downloadFile,
enableSelection,
initialLocations,
injectWebVieWVariables,
injectWebViewVariables,
setIsLoading,
src,
// ! Causing unknown loop
Expand Down
14 changes: 10 additions & 4 deletions src/hooks/useInjectWebviewVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type { Flow, Manager, Spread, Theme, ePubCfi } from '../types';
import template from '../template';
import type { SourceType } from '../utils/enums/source-type.enum';

export function useInjectWebVieWVariables() {
const injectWebVieWVariables = useCallback(
export function useInjectWebViewVariables() {
const injectWebViewVariables = useCallback(
({
jszip,
epubjs,
Expand All @@ -20,6 +20,7 @@ export function useInjectWebVieWVariables() {
snap,
spread,
fullsize,
charactersPerLocation = 1600,
}: {
jszip: string;
epubjs: string;
Expand All @@ -35,6 +36,7 @@ export function useInjectWebVieWVariables() {
snap?: boolean;
spread?: Spread;
fullsize?: boolean;
charactersPerLocation?: number;
}) => {
return template
.replace(
Expand Down Expand Up @@ -71,9 +73,13 @@ export function useInjectWebVieWVariables() {
/spread: undefined/,
`spread: ${spread ? JSON.stringify(spread) : undefined}`
)
.replace(/fullsize: undefined/, `fullsize: ${fullsize ?? undefined}`);
.replace(/fullsize: undefined/, `fullsize: ${fullsize ?? undefined}`)
.replace(
/book\.locations\.generate\(1600\)/,
`book.locations.generate(${charactersPerLocation})`
);
},
[]
);
return { injectWebVieWVariables };
return { injectWebViewVariables };
}
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,4 +467,9 @@ export interface ReaderProps {
* Default is false
*/
keepScrollOffsetOnLocationChange?: boolean;

/**
* Default is 1600
*/
charactersPerLocation?: number;
}

0 comments on commit f414b5b

Please sign in to comment.