mirror of
https://gitee.com/baidu/amis.git
synced 2024-11-29 18:48:45 +08:00
feat: pdfWokerJs 修改为env引入
This commit is contained in:
parent
f2fb6135a6
commit
900cb29c07
@ -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
|
||||||
|
}
|
||||||
|
);
|
||||||
|
```
|
||||||
|
@ -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
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -140,6 +140,11 @@ export interface RendererEnv {
|
|||||||
*/
|
*/
|
||||||
enableTestid?: boolean;
|
enableTestid?: boolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* pdfjs worker 地址,用于渲染 pdf
|
||||||
|
*/
|
||||||
|
pdfjsWorkerSrc?: string;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 替换文本,用于实现 URL 替换、语言替换等
|
* 替换文本,用于实现 URL 替换、语言替换等
|
||||||
*/
|
*/
|
||||||
|
@ -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
|
||||||
|
@ -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>
|
||||||
);
|
);
|
||||||
|
Loading…
Reference in New Issue
Block a user