Skip to content

Commit

Permalink
add getContent to pdf reader
Browse files Browse the repository at this point in the history
  • Loading branch information
jackiequach committed Jun 14, 2024
1 parent ff0a1ed commit bd61a01
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/PdfReader/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
DEFAULT_SHOULD_GROW_WHEN_SCROLLING,
} from '../constants';
import LoadingSkeleton from '../ui/LoadingSkeleton';
import { getResourceUrl, loadResource, SCALE_STEP } from './lib';
import { fetchAsTxt, getResourceUrl, loadResource, SCALE_STEP } from './lib';
import { makePdfReducer } from './reducer';

/**
Expand All @@ -35,7 +35,7 @@ export default function usePdfReader(args: ReaderArguments): ReaderReturn {
webpubManifestUrl,
manifest,
proxyUrl,
getContent,
getContent = fetchAsTxt,
injectablesReflowable,
injectablesFixed,
height = DEFAULT_HEIGHT,
Expand Down Expand Up @@ -104,17 +104,25 @@ export default function usePdfReader(args: ReaderArguments): ReaderReturn {
throw new Error('Manifest has no Reading Order');
}

const resourceUrl = getResourceUrl(
const currentResource = getResourceUrl(
state.resourceIndex,
manifest.readingOrder
);
loadResource(resourceUrl, proxyUrl).then((data) => {
dispatch({
type: 'RESOURCE_FETCH_SUCCESS',
resource: { data },

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

loadResource(resourceUrl, proxyUrl).then((data) => {
dispatch({
type: 'RESOURCE_FETCH_SUCCESS',
resource: { data },
});
});
});
}, [state.resourceIndex, manifest, proxyUrl]);
};
if (manifest.readingOrder && manifest.readingOrder.length) {
fetchResource();
}
}, [state.resourceIndex, manifest, proxyUrl, getContent]);

/**
* calculate the height or width of the pdf page in paginated mode.
Expand Down
7 changes: 7 additions & 0 deletions src/PdfReader/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ export const loadResource = async (
return array;
};

/**
* Fetches a resource url as text
*/
export async function fetchAsTxt(url: string): Promise<string> {
return url;
}

/**
* Gets the index of the provided href in the readingOrder, or throws an error if one
* is not found.
Expand Down

0 comments on commit bd61a01

Please sign in to comment.