Skip to content

Commit c77ffde

Browse files
authored
Merge pull request #749 from streamich/pdf
feat: 🎸 add simple PDF viewer
2 parents 15b1086 + 7314775 commit c77ffde

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

src/ReactEmbed.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,19 @@ export interface Blocks {
2727

2828
const defaultBlocks: Blocks = {
2929
figma: React.lazy(() => import('./blocks/figma')),
30+
gfycat: React.lazy(() => import('./blocks/gfycat')),
3031
gist: React.lazy(() => import('./blocks/gist')),
3132
gmaps: React.lazy(() => import('./blocks/gmaps')),
3233
imgur: React.lazy(() => import('./blocks/imgur')),
3334
instagram: React.lazy(() => import('./blocks/instagram')),
3435
jsfiddle: React.lazy(() => import('./blocks/jsfiddle')),
35-
simplePlayer: React.lazy(() => import('./blocks/react-simple-player')),
36+
pdf: React.lazy(() => import('./blocks/pdf')),
3637
reactPlayer: React.lazy(() => import('./blocks/react-player')),
3738
replit: React.lazy(() => import('./blocks/replit')),
39+
simplePlayer: React.lazy(() => import('./blocks/react-simple-player')),
3840
soundcloud: React.lazy(() => import('./blocks/soundcloud')),
3941
tweet: React.lazy(() => import('./blocks/tweet')),
4042
youtube: React.lazy(() => import('./blocks/youtube')),
41-
gfycat: React.lazy(() => import('./blocks/gfycat')),
4243
};
4344

4445
export type ReactEmbedRouterResult = undefined | [undefined | React.ComponentType<BlockProps>, EmbedBlockId];

src/__story__/pdf.story.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as React from 'react';
2+
import {storiesOf} from '@storybook/react';
3+
import Embed from '..';
4+
import {Box} from './Box';
5+
6+
const pdf = 'http://www.africau.edu/images/default/sample.pdf';
7+
8+
storiesOf('PDF', module)
9+
.add('A pdf file', () => {
10+
return <Embed url={pdf} />;
11+
})
12+
.add('Fitted', () => {
13+
return (
14+
<Box>
15+
<Embed url={pdf} />
16+
</Box>
17+
);
18+
});

src/blocks/pdf/canPlay.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const MATCH_PDF_FILE = /\.(pdf)($|\?)/i;
2+
const canPlay = (url: string): boolean => MATCH_PDF_FILE.test(url);
3+
4+
export default canPlay;

src/blocks/pdf/index.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as React from 'react';
2+
import {BlockProps} from '../..';
3+
4+
const Pdf: React.FC<BlockProps> = ({url, isDark, width, renderWrap}) => {
5+
return renderWrap(
6+
<embed src={url} type="application/pdf" style={{width: '100%', minHeight: 600}} className="react-embed--pdf" />
7+
);
8+
};
9+
10+
export default Pdf;

src/routeToBlock.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {Blocks, ReactEmbedRouter, ParsedUrl} from '.';
22
import canPlaySimplePlayer from './blocks/react-simple-player/canPlay';
33
import canPlay from './blocks/react-player/canPlay';
4+
import canPlayPdf from './blocks/pdf/canPlay';
45

56
const routeTwitter: ReactEmbedRouter = (blocks, {pathname}) => {
67
const steps = pathname.split('/');
@@ -89,7 +90,9 @@ const routeToBlock: ReactEmbedRouter = (blocks: Blocks, parsed: ParsedUrl) => {
8990
case 'gfycat.com':
9091
return routeGfycat(blocks, parsed);
9192
default:
92-
if (canPlaySimplePlayer(url)) {
93+
if (canPlayPdf(url)) {
94+
return [blocks.pdf, ''];
95+
} else if (canPlaySimplePlayer(url)) {
9396
return [blocks.simplePlayer, ''];
9497
} else if (canPlay(url)) {
9598
return [blocks.reactPlayer, ''];

0 commit comments

Comments
 (0)