Skip to content

Commit

Permalink
feat: support URI based paths
Browse files Browse the repository at this point in the history
See: #12
  • Loading branch information
alpha0010 committed Jan 28, 2022
1 parent 37e55a6 commit 71fa74f
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 7 deletions.
12 changes: 11 additions & 1 deletion src/PdfUtil.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { NativeModules } from 'react-native';
import { asPath } from './Util';

export type PageDim = { height: number; width: number };

Expand All @@ -14,7 +15,16 @@ type PdfUtilType = {
getPageSizes(source: string): Promise<PageDim[]>;
};

const PdfUtilNative: PdfUtilType = NativeModules.RNPdfUtil;

/**
* Utility pdf actions.
*/
export const PdfUtil: PdfUtilType = NativeModules.RNPdfUtil;
export const PdfUtil: PdfUtilType = {
getPageCount(source: string) {
return PdfUtilNative.getPageCount(asPath(source));
},
getPageSizes(source: string) {
return PdfUtilNative.getPageSizes(asPath(source));
},
};
5 changes: 3 additions & 2 deletions src/PdfView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
requireNativeComponent,
ViewStyle,
} from 'react-native';
import { asPath } from './Util';

export type ErrorEvent = { message: string };

Expand Down Expand Up @@ -104,13 +105,13 @@ export function PdfView(props: PdfViewProps) {

return (
<PdfViewNative
annotation={props.annotation}
annotation={asPath(props.annotation)}
onLayout={onLayout}
onPdfError={onPdfError}
onPdfLoadComplete={onPdfLoadComplete}
page={props.page}
resizeMode={props.resizeMode}
source={props.source}
source={asPath(props.source)}
style={props.style}
/>
);
Expand Down
11 changes: 11 additions & 0 deletions src/Util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Unifies usage between absolute and URI style paths.
*/
export function asPath(str: string): string;
export function asPath(str?: string): string | undefined;
export function asPath(str?: string) {
if (str == null || !str.startsWith('file://')) {
return str;
}
return str.substring(7);
}
17 changes: 13 additions & 4 deletions src/__tests__/Pdf.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ test('pdf viewer load error', async () => {
expect(onLoadComplete).not.toHaveBeenCalled();
});

async function getRenderedViewer() {
async function getRenderedViewer(source: string) {
MockPdfUtil.getPageSizes.mockImplementationOnce(() =>
Promise.resolve([
{ height: 400, width: 300 },
Expand All @@ -58,14 +58,23 @@ async function getRenderedViewer() {
const onLoadComplete = jest.fn();
const loadCompletePromise = promisify(onLoadComplete);
const result = render(
<Pdf onLoadComplete={onLoadComplete} source="test.pdf" />
<Pdf onLoadComplete={onLoadComplete} source={source} />
);
await act(async () => await loadCompletePromise);
return result;
}

test('viewer renders pages', async () => {
const { getByTestId } = await getRenderedViewer();
const { getByTestId } = await getRenderedViewer('test.pdf');
const flatList = getByTestId('pdfFlatList');

const { toJSON } = render(flatList.props.renderItem({ index: 0 }));

expect(toJSON()).toMatchSnapshot();
});

test('viewer renders pages URI source', async () => {
const { getByTestId } = await getRenderedViewer('file:///path/to/test.pdf');
const flatList = getByTestId('pdfFlatList');

const { toJSON } = render(flatList.props.renderItem({ index: 0 }));
Expand All @@ -74,7 +83,7 @@ test('viewer renders pages', async () => {
});

test('viewer computes layout', async () => {
const { getByTestId } = await getRenderedViewer();
const { getByTestId } = await getRenderedViewer('test.pdf');
const flatList = getByTestId('pdfFlatList');
const pageDimsData = [
{ height: 400, width: 300 },
Expand Down
37 changes: 37 additions & 0 deletions src/__tests__/__snapshots__/Pdf.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,40 @@ exports[`viewer renders pages 1`] = `
</View>
</View>
`;

exports[`viewer renders pages URI source 1`] = `
<View
style={
Array [
Object {
"alignItems": "center",
},
Object {
"maxHeight": undefined,
},
]
}
>
<View>
<RNPdfView
onPdfError={[Function]}
onPdfLoadComplete={[Function]}
page={0}
source="/path/to/test.pdf"
style={
Object {
"backgroundColor": "#fff",
"elevation": 4,
"shadowColor": "#000",
"shadowOffset": Object {
"height": 2,
"width": 0,
},
"shadowOpacity": 0.23,
"shadowRadius": 2.62,
}
}
/>
</View>
</View>
`;

0 comments on commit 71fa74f

Please sign in to comment.