feat: pdfjsWorkerSrc 修改使用方式

This commit is contained in:
allenve 2024-09-24 20:01:12 +08:00
parent 625d36c82a
commit 236d3b0873
8 changed files with 45 additions and 60 deletions

View File

@ -870,37 +870,3 @@ Toast 提示弹出位置,默认为`'top-center'`。
#### loadTinymcePlugin #### loadTinymcePlugin
可以用来加载 tinymce 插件,每次渲染 tinymce 的时候执行,可以用来加载 tinymce 插件。 可以用来加载 tinymce 插件,每次渲染 tinymce 的时候执行,可以用来加载 tinymce 插件。
#### pdfjsWorkerSrc
渲染 pdf 时,需要加载 worker.js可以使用 CDN 地址,也可以使用本地文件。
```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

@ -5,8 +5,15 @@ import Portal from 'react-overlays/Portal';
import {normalizeLink} from 'amis-core'; import {normalizeLink} from 'amis-core';
import {withRouter} from 'react-router-dom'; import {withRouter} from 'react-router-dom';
import copy from 'copy-to-clipboard'; import copy from 'copy-to-clipboard';
import {qsparse, parseQuery, attachmentAdpator} from 'amis-core'; import {
qsparse,
parseQuery,
attachmentAdpator,
supportsMjs,
setGlobalOptions
} from 'amis-core';
import isPlainObject from 'lodash/isPlainObject'; import isPlainObject from 'lodash/isPlainObject';
import {pdfUrlLoad} from '../loadPdfjsWorker';
function loadEditor() { function loadEditor() {
return new Promise(resolve => return new Promise(resolve =>
@ -16,6 +23,10 @@ function loadEditor() {
const viewMode = localStorage.getItem('amis-viewMode') || 'pc'; const viewMode = localStorage.getItem('amis-viewMode') || 'pc';
setGlobalOptions({
pdfjsWorkerSrc: supportsMjs() ? pdfUrlLoad() : ''
});
/** /**
* *
* @param {*} schema schema配置 * @param {*} schema schema配置
@ -247,11 +258,6 @@ export default function (schema, schemaProps, showCode, envOverrides) {
}); });
}, },
// testid // testid
// enableTestid: true,
// pdfjsWorkerSrc: new URL(
// 'pdfjs-dist/build/pdf.worker.min.mjs',
// import.meta.url
// ).toString(),
...envOverrides ...envOverrides
}; };

View File

@ -23,11 +23,15 @@ import 'amis-ui/lib/themes/antd';
import 'amis-ui/lib/themes/dark'; import 'amis-ui/lib/themes/dark';
import 'history'; import 'history';
import {attachmentAdpator} from 'amis-core'; import {attachmentAdpator, setGlobalOptions} from 'amis-core';
import {pdfUrlLoad} from './loadPdfjsWorker'; import {pdfUrlLoad} from './loadPdfjsWorker';
import type {ToastLevel, ToastConf} from 'amis-ui/lib/components/Toast'; import type {ToastLevel, ToastConf} from 'amis-ui/lib/components/Toast';
setGlobalOptions({
pdfjsWorkerSrc: supportsMjs() ? pdfUrlLoad() : ''
});
export function embed( export function embed(
container: string | HTMLElement, container: string | HTMLElement,
schema: any, schema: any,
@ -254,7 +258,6 @@ export function embed(
richTextToken: '', richTextToken: '',
affixOffsetBottom: 0, affixOffsetBottom: 0,
customStyleClassPrefix: '.amis-scope', customStyleClassPrefix: '.amis-scope',
pdfjsWorkerSrc: supportsMjs() ? pdfUrlLoad() : '',
...env ...env
}; };

View File

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

View File

@ -610,3 +610,19 @@ export function getRendererByName(name: string) {
} }
export {RendererEnv}; export {RendererEnv};
export interface IGlobalOptions {
pdfjsWorkerSrc: string;
}
const GlobalOptions: IGlobalOptions = {
pdfjsWorkerSrc: ''
};
export function setGlobalOptions(options: Partial<IGlobalOptions>) {
Object.assign(GlobalOptions, options);
}
export function getGlobalOptions() {
return GlobalOptions;
}

View File

@ -22,7 +22,9 @@ import {
stores, stores,
defaultOptions, defaultOptions,
addSchemaFilter, addSchemaFilter,
extendDefaultEnv extendDefaultEnv,
getGlobalOptions,
setGlobalOptions
} from './factory'; } from './factory';
import type { import type {
RenderOptions, RenderOptions,
@ -220,7 +222,9 @@ export {
CustomStyle, CustomStyle,
enableDebug, enableDebug,
disableDebug, disableDebug,
envOverwrite envOverwrite,
getGlobalOptions,
setGlobalOptions
}; };
export function render( export function render(

View File

@ -5,14 +5,14 @@
*/ */
import React from 'react'; import React from 'react';
import {themeable, ThemeProps} from 'amis-core'; import {themeable, ThemeProps, getGlobalOptions} 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';
pdfjs.GlobalWorkerOptions.workerSrc = getGlobalOptions().pdfjsWorkerSrc;
export interface PdfViewerProps extends ThemeProps { export interface PdfViewerProps extends ThemeProps {
pdfjsWorkerSrc: string;
file?: ArrayBuffer; file?: ArrayBuffer;
width?: number; width?: number;
height?: number; height?: number;
@ -21,7 +21,6 @@ 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);
@ -30,10 +29,6 @@ const PdfViewer: React.FC<PdfViewerProps> = props => {
const [total, setTotal] = React.useState(1); const [total, setTotal] = React.useState(1);
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);

View File

@ -13,7 +13,8 @@ import {
Renderer, Renderer,
RendererProps, RendererProps,
resolveVariableAndFilter, resolveVariableAndFilter,
ScopedContext ScopedContext,
getGlobalOptions
} from 'amis-core'; } from 'amis-core';
import {BaseSchema} from '../Schema'; import {BaseSchema} from '../Schema';
@ -244,7 +245,7 @@ export default class PdfViewer extends React.Component<
<div> <div>
<p> <p>
[PdfViewer]: pdfjsWorkerSrc is required, Please set the [PdfViewer]: pdfjsWorkerSrc is required, Please set the
`pdfjsWorkerSrc` in env. `pdfjsWorkerSrc` in setGlobalOptions function.
</p> </p>
</div> </div>
); );
@ -258,7 +259,7 @@ export default class PdfViewer extends React.Component<
height, height,
background background
} = this.props; } = this.props;
const pdfjs = this.props.env.pdfjsWorkerSrc; const pdfjs = getGlobalOptions().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);
@ -271,7 +272,6 @@ export default class PdfViewer extends React.Component<
<Suspense fallback={<div>...</div>}> <Suspense fallback={<div>...</div>}>
{inited && !error ? ( {inited && !error ? (
<PdfView <PdfView
pdfjsWorkerSrc={pdfjs}
file={this.file} file={this.file}
loading={loading} loading={loading}
className={className} className={className}