From 00c33395207412a2463fed926dd3f1d688b762e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Takahashi?= Date: Thu, 10 Oct 2019 18:25:41 -0300 Subject: [PATCH] Add Create React App example with TypeScript and basic usage --- examples/create-react-app/.env | 1 + examples/create-react-app/.gitignore | 24 +++++++ examples/create-react-app/README.md | 68 ++++++++++++++++++ examples/create-react-app/package.json | 38 ++++++++++ examples/create-react-app/public/favicon.ico | Bin 0 -> 22382 bytes .../create-react-app/public/helloworld.pdf | Bin 0 -> 678 bytes examples/create-react-app/public/index.html | 43 +++++++++++ .../create-react-app/public/manifest.json | 15 ++++ examples/create-react-app/public/robots.txt | 2 + examples/create-react-app/src/app.tsx | 12 ++++ examples/create-react-app/src/index.css | 11 +++ examples/create-react-app/src/index.tsx | 7 ++ examples/create-react-app/src/pdf.tsx | 62 ++++++++++++++++ .../create-react-app/src/react-app-env.d.ts | 1 + examples/create-react-app/tsconfig.json | 25 +++++++ 15 files changed, 309 insertions(+) create mode 100644 examples/create-react-app/.env create mode 100644 examples/create-react-app/.gitignore create mode 100644 examples/create-react-app/README.md create mode 100644 examples/create-react-app/package.json create mode 100644 examples/create-react-app/public/favicon.ico create mode 100644 examples/create-react-app/public/helloworld.pdf create mode 100644 examples/create-react-app/public/index.html create mode 100644 examples/create-react-app/public/manifest.json create mode 100644 examples/create-react-app/public/robots.txt create mode 100644 examples/create-react-app/src/app.tsx create mode 100644 examples/create-react-app/src/index.css create mode 100644 examples/create-react-app/src/index.tsx create mode 100644 examples/create-react-app/src/pdf.tsx create mode 100644 examples/create-react-app/src/react-app-env.d.ts create mode 100644 examples/create-react-app/tsconfig.json diff --git a/examples/create-react-app/.env b/examples/create-react-app/.env new file mode 100644 index 0000000000000..7d910f1484c5e --- /dev/null +++ b/examples/create-react-app/.env @@ -0,0 +1 @@ +SKIP_PREFLIGHT_CHECK=true \ No newline at end of file diff --git a/examples/create-react-app/.gitignore b/examples/create-react-app/.gitignore new file mode 100644 index 0000000000000..7d3cec2ef6bd6 --- /dev/null +++ b/examples/create-react-app/.gitignore @@ -0,0 +1,24 @@ +# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. + +# dependencies +/node_modules +/.pnp +.pnp.js + +# testing +/coverage + +# production +/build + +# misc +.DS_Store +.env.local +.env.development.local +.env.test.local +.env.production.local + +npm-debug.log* +yarn-debug.log* +yarn-error.log* +yarn.lock diff --git a/examples/create-react-app/README.md b/examples/create-react-app/README.md new file mode 100644 index 0000000000000..89b278ae3edb6 --- /dev/null +++ b/examples/create-react-app/README.md @@ -0,0 +1,68 @@ +This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). + +## Available Scripts + +In the project directory, you can run: + +### `yarn start` + +Runs the app in the development mode.
+Open [http://localhost:3000](http://localhost:3000) to view it in the browser. + +The page will reload if you make edits.
+You will also see any lint errors in the console. + +### `yarn test` + +Launches the test runner in the interactive watch mode.
+See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. + +### `yarn build` + +Builds the app for production to the `build` folder.
+It correctly bundles React in production mode and optimizes the build for the best performance. + +The build is minified and the filenames include the hashes.
+Your app is ready to be deployed! + +See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. + +### `yarn eject` + +**Note: this is a one-way operation. Once you `eject`, you can’t go back!** + +If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. + +Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. + +You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. + +## Learn More + +You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). + +To learn React, check out the [React documentation](https://reactjs.org/). + +### Code Splitting + +This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting + +### Analyzing the Bundle Size + +This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size + +### Making a Progressive Web App + +This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app + +### Advanced Configuration + +This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration + +### Deployment + +This section has moved here: https://facebook.github.io/create-react-app/docs/deployment + +### `yarn build` fails to minify + +This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify diff --git a/examples/create-react-app/package.json b/examples/create-react-app/package.json new file mode 100644 index 0000000000000..00a037c277e37 --- /dev/null +++ b/examples/create-react-app/package.json @@ -0,0 +1,38 @@ +{ + "name": "create-react-app", + "version": "0.1.0", + "private": true, + "dependencies": { + "pdfjs-dist": "^2.2.228", + "react": "^16.10.2", + "react-dom": "^16.10.2", + "react-scripts": "3.2.0" + }, + "devDependencies": { + "@types/jest": "^24.0.18", + "@types/node": "^12.7.12", + "@types/react": "^16.9.5", + "@types/react-dom": "^16.9.1", + "eslint-config-react-app": "^5.0.2", + "eslint-plugin-react-hooks": "^2.1.2", + "typescript": "^3.6.3" + }, + "scripts": { + "start": "react-scripts start", + "build": "react-scripts build", + "test": "react-scripts test", + "eject": "react-scripts eject" + }, + "browserslist": { + "production": [ + ">0.2%", + "not dead", + "not op_mini all" + ], + "development": [ + "last 1 chrome version", + "last 1 firefox version", + "last 1 safari version" + ] + } +} diff --git a/examples/create-react-app/public/favicon.ico b/examples/create-react-app/public/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..c2c86b859eaa20639adf92ff979c2be8d580433e GIT binary patch literal 22382 zcmeI4_m@>g631uH?hiA>Aq+VTNjPWS;EE`MGJt>?K?MV56hy>`7*J7EOc)R|k_>>b z$<4tHZVqnp59+N?_4~fpbLZao?tAP3ciB0o&waPMLU(m_RdsdWX>0pJ+ZWq9JKOBp z-M02iZEdsK+SiSaLvu*!^O)cRqLua3I8RFCx>l&hKB3cjSJ6zFeF@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_W=L@JAozeZ|l@fegFUf literal 0 HcmV?d00001 diff --git a/examples/create-react-app/public/helloworld.pdf b/examples/create-react-app/public/helloworld.pdf new file mode 100644 index 0000000000000000000000000000000000000000..d98b4e1dbef68af5863b06f7dac29f7d11a0ca2d GIT binary patch literal 678 zcmZuvO>e?5488kTc!x@xG)7t|9TMWemnLn~5bJH?5NKdUB|%BhV84ELDXe0P)GB%L zdw#YPOh=PO_>cgED9|i6hyg0??Gbk-*B*L3Ky0SRox$H4kO~spx+0j zGvWd;z?(X&3}w!%%5pg{2mGKYfsr_UbG{pzz4jRL_BqcSg|t{3=XG_4{!>30vShn* zL@G1o%-$}^Wa)*FiJ|-Iz{e2VWeQ>=QVrFQtY}YC4r9Kq=jZtA{H)6P-}eh&Hi=tR z)-jMyl{uSfzE!RZ?6PKsNRr6kyUi(jMx0&L+RdNf16cT~4z;py$ zKp0`Vg4-um6y_ekj4iU8J4`n)PI)91>pYx;RVxtK0dZN_iQ`s`E4*MKjfNNMUo7eV y!NP=waTV7R^oFjn$C{qEGB1>^3VhGM6`A2#8AG!_ciVYsed)E`jUWidZ<8MpEU1bA literal 0 HcmV?d00001 diff --git a/examples/create-react-app/public/index.html b/examples/create-react-app/public/index.html new file mode 100644 index 0000000000000..330f617fe1f12 --- /dev/null +++ b/examples/create-react-app/public/index.html @@ -0,0 +1,43 @@ + + + + + + + + + + + + + PDF.js rendering example with Create React App + + + +
+ + + diff --git a/examples/create-react-app/public/manifest.json b/examples/create-react-app/public/manifest.json new file mode 100644 index 0000000000000..e383893e05926 --- /dev/null +++ b/examples/create-react-app/public/manifest.json @@ -0,0 +1,15 @@ +{ + "short_name": "PDf.js React App", + "name": "PDF.js rendering example with Create React App", + "icons": [ + { + "src": "favicon.ico", + "sizes": "64x64 32x32 24x24 16x16", + "type": "image/x-icon" + } + ], + "start_url": ".", + "display": "standalone", + "theme_color": "#000000", + "background_color": "#ffffff" +} diff --git a/examples/create-react-app/public/robots.txt b/examples/create-react-app/public/robots.txt new file mode 100644 index 0000000000000..01b0f9a10733b --- /dev/null +++ b/examples/create-react-app/public/robots.txt @@ -0,0 +1,2 @@ +# https://www.robotstxt.org/robotstxt.html +User-agent: * diff --git a/examples/create-react-app/src/app.tsx b/examples/create-react-app/src/app.tsx new file mode 100644 index 0000000000000..ebdc72157b7e4 --- /dev/null +++ b/examples/create-react-app/src/app.tsx @@ -0,0 +1,12 @@ +import React from 'react'; + +import Pdf from './pdf'; + +export default function App () { + return ( +
+

PDF.js rendering example with Create React App

+ +
+ ) +} \ No newline at end of file diff --git a/examples/create-react-app/src/index.css b/examples/create-react-app/src/index.css new file mode 100644 index 0000000000000..5b8a80ddd5451 --- /dev/null +++ b/examples/create-react-app/src/index.css @@ -0,0 +1,11 @@ +html { + text-align: center; +} + +canvas { + border: 1px dashed; + display: flex; + justify-content: center; + align-items: center; + margin: auto; +} diff --git a/examples/create-react-app/src/index.tsx b/examples/create-react-app/src/index.tsx new file mode 100644 index 0000000000000..8429ea8d28578 --- /dev/null +++ b/examples/create-react-app/src/index.tsx @@ -0,0 +1,7 @@ +import React from 'react'; +import ReactDOM from 'react-dom'; +import App from './app'; + +import './index.css'; + +ReactDOM.render(, document.getElementById('root')); diff --git a/examples/create-react-app/src/pdf.tsx b/examples/create-react-app/src/pdf.tsx new file mode 100644 index 0000000000000..1c09ba6b777c0 --- /dev/null +++ b/examples/create-react-app/src/pdf.tsx @@ -0,0 +1,62 @@ +import React, { useRef, useEffect } from 'react'; +import PropTypes from 'prop-types'; + +import pdfjs from 'pdfjs-dist'; +import pdfjsWorker from 'pdfjs-dist/build/pdf.worker.entry'; + +pdfjs.GlobalWorkerOptions.workerSrc = pdfjsWorker; + +const PdfComponent = ({ src }) => { + const canvasRef = useRef(null) + + useEffect(() => { + const fetchPdf = async () => { + const loadingTask = pdfjs.getDocument(src); + + const pdf = await loadingTask.promise; + + const firstPageNumber = 1; + + const page = await pdf.getPage(firstPageNumber); + + const scale = 1.5; + const viewport = page.getViewport({scale: scale}); + + // Prepare canvas using PDF page dimensions + const canvas = canvasRef.current; + + const context = canvas.getContext('2d'); + canvas.height = viewport.height; + canvas.width = viewport.width; + + // Render PDF page into canvas context + const renderContext = { + canvasContext: context, + viewport: viewport + }; + const renderTask = page.render(renderContext); + + await renderTask.promise; + }; + + fetchPdf(); + }, [src]); + + return ( + + ); +} + +PdfComponent.propTypes = { + src: PropTypes.string +}; + +PdfComponent.defaultProps = { + src: `${process.env.PUBLIC_URL}/helloworld.pdf` +}; + +export default PdfComponent; diff --git a/examples/create-react-app/src/react-app-env.d.ts b/examples/create-react-app/src/react-app-env.d.ts new file mode 100644 index 0000000000000..6431bc5fc6b2c --- /dev/null +++ b/examples/create-react-app/src/react-app-env.d.ts @@ -0,0 +1 @@ +/// diff --git a/examples/create-react-app/tsconfig.json b/examples/create-react-app/tsconfig.json new file mode 100644 index 0000000000000..171592f7eedf5 --- /dev/null +++ b/examples/create-react-app/tsconfig.json @@ -0,0 +1,25 @@ +{ + "compilerOptions": { + "target": "es5", + "lib": [ + "dom", + "dom.iterable", + "esnext" + ], + "allowJs": true, + "skipLibCheck": true, + "esModuleInterop": true, + "allowSyntheticDefaultImports": true, + "strict": false, + "forceConsistentCasingInFileNames": true, + "module": "esnext", + "moduleResolution": "node", + "resolveJsonModule": true, + "isolatedModules": true, + "noEmit": true, + "jsx": "react" + }, + "include": [ + "src" + ] +}