Skip to content

Commit

Permalink
update getContent in PDFReader and type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
jackiequach committed Jun 26, 2024
1 parent 7f37fb6 commit 0386500
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 45 deletions.
11 changes: 3 additions & 8 deletions src/HtmlReader/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import React from 'react';
import {
ColorMode,
ReaderReturn,
ReaderArguments,
FontFamily,
HtmlNavigator,
} from '../types';
import { ColorMode, ReaderReturn, FontFamily, HtmlNavigator } from '../types';
import LoadingSkeleton from '../ui/LoadingSkeleton';
import {
DEFAULT_HEIGHT,
Expand All @@ -28,8 +22,9 @@ import { useUpdateScroll } from './useUpdateScroll';
import useUpdateCSS from './useUpdateCSS';
import useIframeLinkClick from './useIframeLinkClick';
import useUpdateLocalStorage from '../utils/localstorage';
import { HtmlReaderArguments } from './types';

export default function useHtmlReader(args: ReaderArguments): ReaderReturn {
export default function useHtmlReader(args: HtmlReaderArguments): ReaderReturn {
const {
webpubManifestUrl,
manifest,
Expand Down
4 changes: 2 additions & 2 deletions src/HtmlReader/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
getLocalStorageLocation,
getLocalStorageSettings,
} from '../utils/localstorage';
import { ReaderArguments } from '../types';
import {
getCurrentIndex,
linkToLocator,
Expand All @@ -21,6 +20,7 @@ import {
RenderingIframeState,
ResourceFetchErrorState,
InactiveState,
HtmlReaderArguments,
} from './types';
import { getLocationQuery } from './useLocationQuery';
import { DEFAULT_SETTINGS } from '../constants';
Expand All @@ -30,7 +30,7 @@ import { DEFAULT_SETTINGS } from '../constants';
* without passing them in to every `dispatch` call.
*/
export default function makeHtmlReducer(
args: ReaderArguments
args: HtmlReaderArguments
): (state: HtmlState, action: HtmlAction) => HtmlState {
/**
* If there are no args, it's an inactive hook, or you are pre-first render.
Expand Down
9 changes: 7 additions & 2 deletions src/HtmlReader/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Locator } from '../Readium/Locator';
import {
ActiveReaderArguments,
ColorMode,
FontFamily,
ReaderArguments,
InactiveReaderArguments,
ReaderSettings,
ReaderState,
} from '../types';
Expand Down Expand Up @@ -86,8 +87,12 @@ export type ReadyState = ReaderState & {
settings: ReaderSettings;
};

export type HtmlReaderArguments =
| ActiveReaderArguments<string>
| InactiveReaderArguments;

export type HtmlAction =
| { type: 'ARGS_CHANGED'; args: ReaderArguments }
| { type: 'ARGS_CHANGED'; args: HtmlReaderArguments }
| { type: 'IFRAME_LOADED' }
| { type: 'NAV_PREVIOUS_RESOURCE' }
| { type: 'NAV_NEXT_RESOURCE' }
Expand Down
16 changes: 5 additions & 11 deletions src/PdfReader/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Document, PageProps, pdfjs } from 'react-pdf';
import * as React from 'react';
import { ReaderArguments, ReaderReturn } from '../types';
import { ReaderReturn } from '../types';
import { Flex } from '@chakra-ui/react';
import useMeasure from './useMeasure';
import ChakraPage from './ChakraPage';
Expand All @@ -13,13 +13,9 @@ import {
DEFAULT_SHOULD_GROW_WHEN_SCROLLING,
} from '../constants';
import LoadingSkeleton from '../ui/LoadingSkeleton';
import {
getContentDefault,
getResourceUrl,
loadResource,
SCALE_STEP,
} from './lib';
import { getContentDefault, getResourceUrl, SCALE_STEP } from './lib';
import { makePdfReducer } from './reducer';
import { PdfReaderArguments } from './types';

/**
* The PDF reader
Expand All @@ -30,7 +26,7 @@ import { makePdfReducer } from './reducer';
* @param args T
* @returns
*/
export default function usePdfReader(args: ReaderArguments): ReaderReturn {
export default function usePdfReader(args: PdfReaderArguments): ReaderReturn {
// use a passed in src for the pdf worker
if (args?.pdfWorkerSrc) {
pdfjs.GlobalWorkerOptions.workerSrc = args.pdfWorkerSrc;
Expand Down Expand Up @@ -115,9 +111,7 @@ export default function usePdfReader(args: ReaderArguments): ReaderReturn {
);

const fetchResource = async () => {
const resourceUrl = await getContent(currentResource);

loadResource(resourceUrl, proxyUrl).then((data) => {
getContent(currentResource, proxyUrl).then((data) => {
dispatch({
type: 'RESOURCE_FETCH_SUCCESS',
resource: { data },
Expand Down
7 changes: 5 additions & 2 deletions src/PdfReader/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ export const loadResource = async (
/**
* Fetches a resource url as text
*/
export async function getContentDefault(url: string): Promise<string> {
return url;
export async function getContentDefault(
resourceUrl: string,
proxyUrl?: string
): Promise<Uint8Array> {
return loadResource(resourceUrl, proxyUrl);
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/PdfReader/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { DEFAULT_SETTINGS } from '../constants';
import { ReaderArguments } from '../types';
import {
getIndexFromHref,
getStartPageFromHref,
getPageNumberFromHref,
} from './lib';
import { PdfState, PdfReaderAction } from './types';
import { PdfState, PdfReaderAction, PdfReaderArguments } from './types';

export function makePdfReducer(
args: ReaderArguments
args: PdfReaderArguments
): (state: PdfState, action: PdfReaderAction) => PdfState {
/**
* If there are no args, it's an inactive hook, or you are pre-first render.
Expand Down
13 changes: 11 additions & 2 deletions src/PdfReader/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { ReaderArguments, ReaderSettings, ReaderState } from '../types';
import {
ActiveReaderArguments,
InactiveReaderArguments,
ReaderSettings,
ReaderState,
} from '../types';

export type InternalState = {
resourceIndex: number;
Expand All @@ -24,10 +29,14 @@ export type ActiveState = ReaderState &

export type PdfState = InactiveState | ActiveState;

export type PdfReaderArguments =
| ActiveReaderArguments<Uint8Array>
| InactiveReaderArguments;

export type PdfReaderAction =
| {
type: 'ARGS_CHANGED';
args: ReaderArguments;
args: PdfReaderArguments;
}
| { type: 'GO_FORWARD' }
| { type: 'GO_BACKWARD' }
Expand Down
3 changes: 2 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import useWebReader from './useWebReader';
* The main React component export.
*/

export type WebReaderProps = UseWebReaderArguments & ReaderManagerArguments;
export type WebReaderProps = UseWebReaderArguments<string | Uint8Array> &
ReaderManagerArguments;

export const WebReaderWithoutBoundary: FC<WebReaderProps> = ({
webpubManifestUrl,
Expand Down
15 changes: 9 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,19 @@ export type ActiveReader = PDFActiveReader | HTMLActiveReader;
export type ReaderReturn = InactiveReader | LoadingReader | ActiveReader;

// should fetch and decrypt a resource
export type GetContent = (href: string) => Promise<string>;
export type GetContent<T extends string | Uint8Array> = (
href: string,
proxyUrl?: string
) => Promise<T>;

export type ReaderManagerArguments = {
headerLeft?: JSX.Element; // Top-left header section
};

export type UseWebReaderArguments = {
export type UseWebReaderArguments<T extends string | Uint8Array> = {
webpubManifestUrl: string;
proxyUrl?: string;
getContent?: GetContent;
getContent?: GetContent<T>;
pdfWorkerSrc?: string;
injectablesReflowable?: Injectable[];
injectablesFixed?: Injectable[];
Expand Down Expand Up @@ -137,12 +140,12 @@ export type UseWebReaderArguments = {
persistSettings?: boolean;
};

export type ActiveReaderArguments = UseWebReaderArguments & {
export type ActiveReaderArguments<
T extends string | Uint8Array
> = UseWebReaderArguments<T> & {
manifest: WebpubManifest;
};

export type InactiveReaderArguments = undefined;

export type ReaderArguments = ActiveReaderArguments | InactiveReaderArguments;

export type GetColor = (light: string, dark: string, sepia: string) => string;
9 changes: 6 additions & 3 deletions src/useWebReader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
LoadingReader,
PDFActiveReader,
WebpubManifest,
GetContent,
} from './types';
import {
EpubConformsTo,
Expand Down Expand Up @@ -42,7 +43,7 @@ function getReaderType(conformsTo: ConformsTo | null | undefined) {
* for that type.
*/
export default function useWebReader(
args: UseWebReaderArguments
args: UseWebReaderArguments<string | Uint8Array>
): HTMLActiveReader | PDFActiveReader | LoadingReader {
const {
webpubManifestUrl,
Expand All @@ -67,6 +68,8 @@ export default function useWebReader(
const readerType = getReaderType(
manifest ? manifest.metadata.conformsTo : null
);
const getContentHtml = getContent as GetContent<string> | undefined;
const getContentPdf = getContent as GetContent<Uint8Array> | undefined;
/**
* Our HTML reader and PDf Reader. Note that we cannot conditionally
* call a React hook, so we must _always_ call the hook, but allow for the
Expand All @@ -78,7 +81,7 @@ export default function useWebReader(
? {
webpubManifestUrl,
manifest,
getContent,
getContent: getContentHtml,
injectablesReflowable,
injectablesFixed,
height,
Expand All @@ -94,7 +97,7 @@ export default function useWebReader(
? {
webpubManifestUrl,
manifest,
getContent,
getContent: getContentPdf,
proxyUrl,
pdfWorkerSrc,
height,
Expand Down
10 changes: 5 additions & 5 deletions src/utils/localstorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {
LOCAL_STORAGE_LOCATIONS_KEY,
LOCAL_STORAGE_SETTINGS_KEY,
} from '../constants';
import { HtmlState } from '../HtmlReader/types';
import { HtmlReaderArguments, HtmlState } from '../HtmlReader/types';
import { Locator } from '../Readium/Locator';
import { ReaderArguments, ReaderSettings } from '../types';
import { ReaderSettings } from '../types';

export type LSLocation = {
location: Locator;
Expand All @@ -22,7 +22,7 @@ export type LSLocationsRecord = Record<string, LSLocation>;
*/
export function getLocalStorageLocation(
identifier: string,
args: ReaderArguments
args: HtmlReaderArguments
): LSLocation | undefined {
if (!args?.persistLastLocation) return undefined;
const item = localStorage.getItem(LOCAL_STORAGE_LOCATIONS_KEY);
Expand All @@ -35,7 +35,7 @@ export function getLocalStorageLocation(
}

export function getLocalStorageSettings(
args: ReaderArguments
args: HtmlReaderArguments
): ReaderSettings | undefined {
if (!args?.persistSettings) return undefined;
const item = localStorage.getItem(LOCAL_STORAGE_SETTINGS_KEY);
Expand All @@ -49,7 +49,7 @@ export function getLocalStorageSettings(
export default function useUpdateLocalStorage(
identifier: string | null,
state: HtmlState,
args: ReaderArguments
args: HtmlReaderArguments
): void {
/**
* Keep location up to date as the state changes
Expand Down

0 comments on commit 0386500

Please sign in to comment.