feat: pdfWokerJs 修改为env引入

This commit is contained in:
yupeng12 2024-07-05 10:58:02 +08:00
parent f2fb6135a6
commit 900cb29c07
5 changed files with 84 additions and 20 deletions

View File

@ -870,3 +870,37 @@ Toast 提示弹出位置,默认为`'top-center'`。
#### loadTinymcePlugin #### loadTinymcePlugin
可以用来加载 tinymce 插件,每次渲染 tinymce 的时候执行,可以用来加载 tinymce 插件。 可以用来加载 tinymce 插件,每次渲染 tinymce 的时候执行,可以用来加载 tinymce 插件。
#### pdfjsWorkerSrc
渲染 pdf 时,需要加载 worker.js可以使用 CND 地址,也可以使用本地文件。
```javascript
import {pdfjs} from 'react-pdf';
// 本地文件
const pdfJsUrl = new URL(
'pdfjs-dist/build/pdf.worker.min.mjs',
import.meta.url
).toString();
// CDN 地址
const pdfJsUrl1 = `//unpkg.com/pdfjs-dist@${pdfjs.version}/build/pdf.worker.min.mjs`;
let amisScoped = amis.embed(
'#root',
{
type: 'page',
body: {
type: 'pdf-viewer',
id: 'pdf-viewer',
src: '/examples/static/simple.pdf',
width: 500
}
},
{},
{
pdfjsWorkerSrc: pdfJsUrl // pdfJsUrl1
}
);
```

View File

@ -248,6 +248,10 @@ export default function (schema, schemaProps, showCode, envOverrides) {
}, },
// testid // testid
// enableTestid: true, // enableTestid: true,
pdfjsWorkerSrc: new URL(
'pdfjs-dist/build/pdf.worker.min.mjs',
import.meta.url
).toString(),
...envOverrides ...envOverrides
}; };

View File

@ -140,6 +140,11 @@ export interface RendererEnv {
*/ */
enableTestid?: boolean; enableTestid?: boolean;
/**
* pdfjs worker pdf
*/
pdfjsWorkerSrc?: string;
/** /**
* URL * URL
*/ */

View File

@ -7,14 +7,12 @@
import React from 'react'; import React from 'react';
import {themeable, ThemeProps} from 'amis-core'; import {themeable, ThemeProps} from 'amis-core';
import {Document, Page, pdfjs} from 'react-pdf'; import {Document, Page, pdfjs} from 'react-pdf';
import {Icon} from './icons'; import {Icon} from './icons';
import Input from './Input'; import Input from './Input';
import Spinner from './Spinner'; import Spinner from './Spinner';
import * as pdfJSWorkerURL from 'pdfjs-dist/build/pdf.worker.min';
pdfjs.GlobalWorkerOptions.workerSrc = pdfJSWorkerURL;
export interface PdfViewerProps extends ThemeProps { export interface PdfViewerProps extends ThemeProps {
pdfjsWorkerSrc: string;
file?: ArrayBuffer; file?: ArrayBuffer;
width?: number; width?: number;
height?: number; height?: number;
@ -23,15 +21,19 @@ export interface PdfViewerProps extends ThemeProps {
} }
const PdfViewer: React.FC<PdfViewerProps> = props => { const PdfViewer: React.FC<PdfViewerProps> = props => {
const pdfjsWorkerSrc = props.pdfjsWorkerSrc;
const {classnames: cx, className, loading, width = 300} = props; const {classnames: cx, className, loading, width = 300} = props;
const [file, setFile] = React.useState(props.file); const [file, setFile] = React.useState(props.file);
const [loaded, setLoaded] = React.useState(false); const [loaded, setLoaded] = React.useState(false);
const [page, setPage] = React.useState(1); const [page, setPage] = React.useState(1);
const [scale, setScale] = React.useState(1); const [scale, setScale] = React.useState(1);
const [total, setTotal] = React.useState(1); const [total, setTotal] = React.useState(1);
const wrapper = React.useRef<HTMLDivElement>(null);
const inputRef = React.useRef<HTMLInputElement>(); const inputRef = React.useRef<HTMLInputElement>();
React.useEffect(() => {
pdfjsWorkerSrc && (pdfjs.GlobalWorkerOptions.workerSrc = pdfjsWorkerSrc);
}, [props.pdfjsWorkerSrc]);
React.useEffect(() => { React.useEffect(() => {
if (props.file instanceof ArrayBuffer && props.file.byteLength > 0) { if (props.file instanceof ArrayBuffer && props.file.byteLength > 0) {
setFile(props.file); setFile(props.file);
@ -112,7 +114,7 @@ const PdfViewer: React.FC<PdfViewerProps> = props => {
} }
return ( return (
<div className={cx(className, 'PdfViewer')} ref={wrapper}> <div className={cx(className, 'PdfViewer')}>
{!file || loading ? ( {!file || loading ? (
renderLoading() renderLoading()
) : ( ) : (
@ -121,6 +123,7 @@ const PdfViewer: React.FC<PdfViewerProps> = props => {
<Document <Document
file={file} file={file}
onLoadSuccess={handleLoadSuccess} onLoadSuccess={handleLoadSuccess}
onLoadError={err => console.log(err)}
loading={renderLoading()} loading={renderLoading()}
> >
<Page <Page

View File

@ -238,34 +238,52 @@ export default class PdfViewer extends React.Component<
return null; return null;
} }
@autobind
renderTip() {
return (
<div>
<p>
[PdfViewer]: pdfjsWorkerSrc is required, Please set the
`pdfjsWorkerSrc` in env.
</p>
</div>
);
}
render() { render() {
const { const {
className, className,
classnames: cx, classnames: cx,
translate: __, translate: __,
height, height,
background, background
src
} = this.props; } = this.props;
const pdfjs = this.props.env.pdfjsWorkerSrc;
const {loading, inited, error} = this.state; const {loading, inited, error} = this.state;
const width = Math.max(this.props.width || this.state.width, 300); const width = Math.max(this.props.width || this.state.width, 300);
return ( return (
<div ref={this.wrapper}> <div ref={this.wrapper}>
{this.renderEmpty()} {this.renderEmpty()}
<Suspense fallback={<div>...</div>}> {!pdfjs ? (
{inited && !error ? ( this.renderTip()
<PdfView ) : (
file={this.file} <Suspense fallback={<div>...</div>}>
loading={loading} {inited && !error ? (
className={className} <PdfView
classnames={cx} pdfjsWorkerSrc={pdfjs}
width={width} file={this.file}
height={height} loading={loading}
background={background} className={className}
/> classnames={cx}
) : null} width={width}
</Suspense> height={height}
background={background}
/>
) : null}
</Suspense>
)}
{this.renderError()} {this.renderError()}
</div> </div>
); );