@chenchaolong/plugin-trade-compliance-workbench 1.0.133 → 1.0.134
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/remote-components/trade-compliance-workbench/app.js +7 -7
- package/dist/lib/remote-components/trade-compliance-workbench/editor-source.mjs +14 -3
- package/dist/lib/remote-components/trade-compliance-workbench/editor.js +19 -19
- package/dist/lib/remote-components/trade-compliance-workbench/preview-source.mjs +42 -13
- package/dist/lib/remote-components/trade-compliance-workbench/preview.js +168 -168
- package/dist/lib/remote-ui/app-source.d.ts.map +1 -1
- package/dist/lib/remote-ui/app-source.js +23 -34
- package/dist/lib/remote-ui/app-source.js.map +1 -1
- package/dist/lib/remote-ui/components/source-file-preview.d.ts +18 -3
- package/dist/lib/remote-ui/components/source-file-preview.d.ts.map +1 -1
- package/dist/lib/remote-ui/components/source-file-preview.js +34 -17
- package/dist/lib/remote-ui/components/source-file-preview.js.map +1 -1
- package/dist/lib/remote-ui/customer-contract-contracts.d.ts +8 -0
- package/dist/lib/remote-ui/customer-contract-contracts.d.ts.map +1 -1
- package/dist/lib/remote-ui/isolated-runtime.d.ts +36 -0
- package/dist/lib/remote-ui/isolated-runtime.d.ts.map +1 -0
- package/dist/lib/remote-ui/isolated-runtime.js +158 -0
- package/dist/lib/remote-ui/isolated-runtime.js.map +1 -0
- package/dist/lib/remote-ui/model.d.ts +1 -0
- package/dist/lib/remote-ui/model.d.ts.map +1 -1
- package/dist/lib/remote-ui/model.js +17 -0
- package/dist/lib/remote-ui/model.js.map +1 -1
- package/dist/lib/remote-ui/procurement-contracts.d.ts +8 -0
- package/dist/lib/remote-ui/procurement-contracts.d.ts.map +1 -1
- package/dist/lib/workbench-view.provider.d.ts +27 -7
- package/dist/lib/workbench-view.provider.d.ts.map +1 -1
- package/dist/lib/workbench-view.provider.js +20 -1
- package/dist/lib/workbench-view.provider.js.map +1 -1
- package/package.json +1 -1
|
@@ -3,6 +3,8 @@ import * as mammoth from 'mammoth'
|
|
|
3
3
|
import * as XLSX from 'xlsx'
|
|
4
4
|
|
|
5
5
|
const MAX_PDF_PAGES = 8
|
|
6
|
+
const MAX_PREVIEW_FILE_BYTES = 20 * 1024 * 1024
|
|
7
|
+
const MAX_RENDER_PIXELS = 6_000_000
|
|
6
8
|
|
|
7
9
|
let workerUrl = ''
|
|
8
10
|
function pdfWorkerUrl() {
|
|
@@ -11,40 +13,67 @@ function pdfWorkerUrl() {
|
|
|
11
13
|
}
|
|
12
14
|
GlobalWorkerOptions.workerSrc = pdfWorkerUrl()
|
|
13
15
|
window.TradeComplianceSourcePreview = {
|
|
14
|
-
async renderPdf(url) {
|
|
15
|
-
const
|
|
16
|
-
const
|
|
16
|
+
async renderPdf(url, { signal } = {}) {
|
|
17
|
+
const bytes = await readResponseBytes(url, signal, 'PDF')
|
|
18
|
+
const loadingTask = getDocument({ data: bytes })
|
|
19
|
+
const document = await loadingTask.promise
|
|
17
20
|
const pageCount = document.numPages
|
|
18
21
|
const pageNumbers = selectPages(pageCount, MAX_PDF_PAGES)
|
|
19
22
|
const pages = []
|
|
23
|
+
let completed = false
|
|
20
24
|
try {
|
|
21
25
|
for (const pageNumber of pageNumbers) {
|
|
26
|
+
throwIfAborted(signal)
|
|
22
27
|
const page = await document.getPage(pageNumber)
|
|
23
28
|
const base = page.getViewport({ scale: 1 })
|
|
24
|
-
const
|
|
29
|
+
const scale = Math.min(1.15, 900 / Math.max(1, base.width), Math.sqrt(MAX_RENDER_PIXELS / Math.max(1, base.width * base.height)))
|
|
30
|
+
const viewport = page.getViewport({ scale })
|
|
25
31
|
const canvas = window.document.createElement('canvas')
|
|
26
32
|
canvas.width = Math.ceil(viewport.width)
|
|
27
33
|
canvas.height = Math.ceil(viewport.height)
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
34
|
+
try {
|
|
35
|
+
await page.render({ canvas, canvasContext: canvas.getContext('2d'), viewport }).promise
|
|
36
|
+
throwIfAborted(signal)
|
|
37
|
+
pages.push(await canvasToObjectUrl(canvas))
|
|
38
|
+
} finally {
|
|
39
|
+
canvas.width = 0
|
|
40
|
+
canvas.height = 0
|
|
41
|
+
page.cleanup?.()
|
|
42
|
+
}
|
|
31
43
|
}
|
|
44
|
+
completed = true
|
|
32
45
|
return { pages, pageNumbers, pageCount }
|
|
33
46
|
} finally {
|
|
47
|
+
if (!completed) revokePageUrls(pages)
|
|
34
48
|
await document.destroy()
|
|
35
49
|
}
|
|
36
50
|
},
|
|
37
|
-
async readSpreadsheet(url) {
|
|
38
|
-
const
|
|
39
|
-
const workbook = XLSX.read(
|
|
51
|
+
async readSpreadsheet(url, { signal } = {}) {
|
|
52
|
+
const bytes = await readResponseBytes(url, signal, '表格')
|
|
53
|
+
const workbook = XLSX.read(bytes, { type: 'array', cellDates: true, raw: false, sheetRows: 200 })
|
|
54
|
+
throwIfAborted(signal)
|
|
40
55
|
return workbook.SheetNames.map(name => ({ name, rows: XLSX.utils.sheet_to_json(workbook.Sheets[name], { header: 1, defval: '', raw: false }).slice(0, 200).map(row => Array.isArray(row) ? row.slice(0, 30).map(cell => String(cell ?? '')) : []) })).filter(sheet => sheet.rows.some(row => row.some(Boolean)))
|
|
41
56
|
},
|
|
42
|
-
async convertDocx(url) {
|
|
43
|
-
const
|
|
44
|
-
const result = await mammoth.convertToHtml({ arrayBuffer:
|
|
57
|
+
async convertDocx(url, { signal } = {}) {
|
|
58
|
+
const bytes = await readResponseBytes(url, signal, 'Word')
|
|
59
|
+
const result = await mammoth.convertToHtml({ arrayBuffer: bytes })
|
|
60
|
+
throwIfAborted(signal)
|
|
61
|
+
return result.value.replace(/<script[\s\S]*?<\/script>/gi, '').replace(/<style[\s\S]*?<\/style>/gi, '').replace(/\son\w+=(?:"[^"]*"|'[^']*')/gi, '')
|
|
45
62
|
}
|
|
46
63
|
}
|
|
47
64
|
function selectPages(pageCount, maxPages) { if (pageCount <= maxPages) return Array.from({ length: pageCount }, (_, index) => index + 1); return [...Array.from({ length: maxPages - 2 }, (_, index) => index + 1), pageCount - 1, pageCount] }
|
|
65
|
+
async function readResponseBytes(url, signal, label) {
|
|
66
|
+
throwIfAborted(signal)
|
|
67
|
+
const response = await fetch(url, { signal })
|
|
68
|
+
if (!response.ok) throw new Error(`${label}文件读取失败(${response.status})`)
|
|
69
|
+
const length = Number(response.headers.get('content-length'))
|
|
70
|
+
if (Number.isFinite(length) && length > MAX_PREVIEW_FILE_BYTES) throw new Error(`${label}文件超过在线预览限制(20 MB),请下载原文件查看`)
|
|
71
|
+
const bytes = new Uint8Array(await response.arrayBuffer())
|
|
72
|
+
if (bytes.byteLength > MAX_PREVIEW_FILE_BYTES) throw new Error(`${label}文件超过在线预览限制(20 MB),请下载原文件查看`)
|
|
73
|
+
return bytes
|
|
74
|
+
}
|
|
75
|
+
function throwIfAborted(signal) { if (signal?.aborted) throw new DOMException('预览已取消', 'AbortError') }
|
|
76
|
+
function revokePageUrls(pages) { for (const pageUrl of pages) if (pageUrl.startsWith('blob:')) URL.revokeObjectURL(pageUrl) }
|
|
48
77
|
async function canvasToObjectUrl(canvas) {
|
|
49
78
|
const blob = await new Promise((resolve, reject) => canvas.toBlob(value => value ? resolve(value) : reject(new Error('PDF 页面转码失败')), 'image/png'))
|
|
50
79
|
return URL.createObjectURL(blob)
|