From 8b29fdd65beca1e9be4a0f756e56640b56b0495a Mon Sep 17 00:00:00 2001 From: yupeng12 Date: Fri, 5 Jul 2024 16:06:54 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20pdf=20worker=20js=20=E6=89=93=E5=8C=85?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/embed.tsx | 5 +++-- examples/loadPdfjsWorker.ts | 17 +++++++++++++++++ fis-conf.js | 3 ++- packages/amis-core/src/utils/helper.ts | 9 +++++++++ 4 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 examples/loadPdfjsWorker.ts diff --git a/examples/embed.tsx b/examples/embed.tsx index 827ade955..1afad7e27 100644 --- a/examples/embed.tsx +++ b/examples/embed.tsx @@ -4,7 +4,7 @@ import {createRoot} from 'react-dom/client'; import axios from 'axios'; import {match} from 'path-to-regexp'; import copy from 'copy-to-clipboard'; -import {normalizeLink} from 'amis-core'; +import {normalizeLink, supportsMjs} from 'amis-core'; import qs from 'qs'; import { @@ -20,6 +20,7 @@ import { import 'amis-ui/lib/locale/en-US'; import 'history'; import {attachmentAdpator} from 'amis-core'; +import {pdfUrlLoad} from './loadPdfjsWorker'; import type {ToastLevel, ToastConf} from 'amis-ui/lib/components/Toast'; @@ -249,7 +250,7 @@ export function embed( richTextToken: '', affixOffsetBottom: 0, customStyleClassPrefix: '.amis-scope', - pdfjsWorkerSrc: '/sdk/thirds/pdfjs-dist/build/pdf.worker.min.mjs', + pdfjsWorkerSrc: supportsMjs() ? pdfUrlLoad() : '', ...env }; diff --git a/examples/loadPdfjsWorker.ts b/examples/loadPdfjsWorker.ts new file mode 100644 index 000000000..7df37d744 --- /dev/null +++ b/examples/loadPdfjsWorker.ts @@ -0,0 +1,17 @@ +// 这是个特殊的方法,请看考 mod.js 里面的实现。 +export function pdfUrlLoad() { + // @ts-ignore + const pdfWorker = __uri('pdfjs-dist/build/pdf.worker.min.mjs'); + + return filterUrl(pdfWorker); +} + +function __uri(url: string) { + return url; +} + +// 用于发布 sdk 版本的时候替换,因为不确定 sdk 版本怎么部署,而 worker 地址路径不可知。 +// 所以会被 fis3 替换成取相对的代码。 +function filterUrl(url: string) { + return url; +} diff --git a/fis-conf.js b/fis-conf.js index 2a9d2acad..540d4a754 100644 --- a/fis-conf.js +++ b/fis-conf.js @@ -650,7 +650,8 @@ if (fis.project.currentMedia() === 'publish-sdk') { // 如果 sdk 和 worker 不是部署在一个地方,请通过指定 MonacoEnvironment.getWorkerUrl if ( file.subpath === '/node_modules/amis-ui/lib/components/Editor.js' || - file.subpath === '/examples/loadMonacoEditor.ts' + file.subpath === '/examples/loadMonacoEditor.ts' || + file.subpath === '/examples/loadPdfjsWorker.ts' ) { contents = contents.replace( /function\sfilterUrl\(url\)\s\{\s*return\s*url;/m, diff --git a/packages/amis-core/src/utils/helper.ts b/packages/amis-core/src/utils/helper.ts index 7ee2029d0..f026941cb 100644 --- a/packages/amis-core/src/utils/helper.ts +++ b/packages/amis-core/src/utils/helper.ts @@ -2329,3 +2329,12 @@ export class TestIdBuilder { return data ? filter(this.testId, data) : this.testId; } } + +export function supportsMjs() { + try { + new Function('import("")'); + return true; + } catch (e) { + return false; + } +}