From a6632e7b556a779099e8ad605a957a851c668eab Mon Sep 17 00:00:00 2001 From: "DESKTOP-JKIE562\\PC" <1013397054@qq.com> Date: Tue, 1 Dec 2026 08:07:05 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E7=BF=BB=E8=AF=91=E9=A2=84=E8=A7=88):?= =?UTF-8?q?=E6=89=93=E5=BC=80=E8=81=8A=E5=A4=A9=E9=99=84=E4=BB=B6=E9=A2=84?= =?UTF-8?q?=E8=A7=88=E6=8A=BD=E5=B1=89=E6=88=96=E4=BA=A7=E7=89=A9=E9=A2=84?= =?UTF-8?q?=E8=A7=88=20=E2=86=92=20xlsx=20=E7=BD=91=E6=A0=BC=E4=B8=8A?= =?UTF-8?q?=E5=BA=94=E5=87=BA=E7=8E=B0=E5=B5=8C=E5=85=A5=E5=9B=BE=E7=89=87?= =?UTF-8?q?=EF=BC=88=E6=8C=89=E9=94=9A=E7=82=B9=E5=8D=95=E5=85=83=E6=A0=BC?= =?UTF-8?q?=E5=AE=9A=E4=BD=8D=EF=BC=89=20=E8=8B=A5=E4=BB=8D=E6=97=A0?= =?UTF-8?q?=E5=9B=BE=EF=BC=9A=E6=8C=89=20F12=20=E6=89=93=E5=BC=80=E6=B5=8F?= =?UTF-8?q?=E8=A7=88=E5=99=A8=E6=8E=A7=E5=88=B6=E5=8F=B0=EF=BC=8C=E6=9F=A5?= =?UTF-8?q?=E6=89=BE=20[DocumentPreview]=20extract=20embedded=20images=20f?= =?UTF-8?q?ailed=20=E5=91=8A=E8=AD=A6=E5=8F=91=E6=88=91=E2=80=94=E2=80=94?= =?UTF-8?q?=E7=8E=B0=E5=9C=A8=E5=A4=B1=E8=B4=A5=E4=B8=8D=E5=86=8D=E9=9D=99?= =?UTF-8?q?=E9=BB=98=EF=BC=8C=E5=8F=AF=E7=9B=B4=E6=8E=A5=E5=AE=9A=E4=BD=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/components/Input-field.vue | 8 +- frontend/src/components/document-preview.vue | 13 ++ frontend/src/utils/xlsxDrawings.ts | 211 +++++++++++++++++++ 3 files changed, 229 insertions(+), 3 deletions(-) create mode 100644 frontend/src/utils/xlsxDrawings.ts diff --git a/frontend/src/components/Input-field.vue b/frontend/src/components/Input-field.vue index 20feaf3..bed058c 100644 --- a/frontend/src/components/Input-field.vue +++ b/frontend/src/components/Input-field.vue @@ -95,8 +95,10 @@ const inputPlaceholder = computed(() => { if ( selectedAgentId.value === SIP_TRANSLATOR_AGENT_ID && uploadedAttachments.value.length > 0 && - uploadedAttachments.value.every(item => item.documentId && item.status !== 'failed') + uploadedAttachments.value.every(item => item.status !== 'failed') ) { + // A first conversation has no session yet, so local files carry no + // documentId until send — readiness is "not failed", not "uploaded". return '翻译文件已就绪,直接发送即可开始翻译'; } return t('input.placeholder'); @@ -115,9 +117,9 @@ watch( sipAutoFilled.value = false; return; } - const ready = items.every(a => a.documentId && a.status !== 'failed'); + const ready = items.every(a => a.status !== 'failed'); if (ready && !sipAutoFilled.value && !query.value.trim()) { - query.value = '翻译'; + query.value = '翻译文件已就绪,直接发送即可开始翻译'; sipAutoFilled.value = true; } }, diff --git a/frontend/src/components/document-preview.vue b/frontend/src/components/document-preview.vue index 3ce9c1c..422f814 100644 --- a/frontend/src/components/document-preview.vue +++ b/frontend/src/components/document-preview.vue @@ -12,6 +12,7 @@ import { useI18n } from 'vue-i18n'; import { sanitizeHTML, sanitizeMarkdownHTML, hydrateProtectedFileImages } from '@/utils/security'; import { preparePptxPreview, isCompletePptxRender } from '@/utils/pptxPreview'; import { findMarkdownSourceRange } from '@/utils/markdownSourceLocate'; +import { extractExcelImages, mountExcelPreviewImages } from '@/utils/xlsxDrawings'; import { renderDocumentPreviewMarkdown } from '@/utils/documentPreviewMarkdown'; import { buildHtmlPreview } from '@/utils/htmlPreview'; import { openMermaidFullscreen } from '@/utils/mermaidViewer'; @@ -373,6 +374,12 @@ function decodeCSVBlob(arrayBuffer: ArrayBuffer): string { async function renderExcel(blob: Blob, fileType?: string) { const XLSX = await import('xlsx'); const arrayBuffer = await blob.arrayBuffer(); + // Embedded photos live outside the cell model; keep the bytes so the + // drawings overlay can mount them after the grid renders. + const drawings = await extractExcelImages(arrayBuffer).catch(e => { + console.warn('[DocumentPreview] extract embedded images failed:', e); + return []; + }); let workbook; const lowerType = fileType?.toLowerCase(); @@ -399,6 +406,12 @@ async function renderExcel(blob: Blob, fileType?: string) { html += ``; }); excelHtml.value = sanitizeHTML(html); + + await nextTick(); + const container = previewContent.value as HTMLElement | null; + if (container && drawings.length > 0) { + mountExcelPreviewImages(container, drawings); + } } async function renderText(blob: Blob, fileType: string) { diff --git a/frontend/src/utils/xlsxDrawings.ts b/frontend/src/utils/xlsxDrawings.ts new file mode 100644 index 0000000..0fa3898 --- /dev/null +++ b/frontend/src/utils/xlsxDrawings.ts @@ -0,0 +1,211 @@ +// xlsxDrawings — extracts embedded images and their cell anchors from an +// .xlsx package so the spreadsheet preview can overlay them on the rendered +// sheet grid. SheetJS only models cells; drawings (xl/drawings/*) live +// outside its data model, which is why a plain sheet_to_html preview loses +// every embedded photo. +import JSZip from 'jszip'; + +// EMU (English Metric Unit) per CSS pixel: 914400 EMU / inch at 96 DPI. +const EMU_PER_PX = 9525; + +export interface ExcelCellPoint { + col: number; + colOffPx: number; + row: number; + rowOffPx: number; +} + +export interface ExcelDrawingImage { + /** zero-based sheet index, matching workbook.SheetNames order */ + sheetIndex: number; + from: ExcelCellPoint; + /** twoCellAnchor end corner; null when the anchor carries an explicit size */ + to: ExcelCellPoint | null; + /** explicit size from (oneCellAnchor), in preview pixels */ + extPx: { width: number; height: number } | null; + /** data URL of the embedded image */ + src: string; +} + +function attr(tag: string, name: string): string { + const m = tag.match(new RegExp(`${name}="([^"]*)"`)); + return m ? m[1] : ''; +} + +function blockNum(block: string, tag: string): number { + const m = block.match(new RegExp(`<${tag}>(-?\\d+)`)); + return m ? Number(m[1]) : 0; +} + +function parseCellPoint(block: string): ExcelCellPoint { + return { + col: blockNum(block, 'xdr:col'), + colOffPx: Math.round(blockNum(block, 'xdr:colOff') / EMU_PER_PX), + row: blockNum(block, 'xdr:row'), + rowOffPx: Math.round(blockNum(block, 'xdr:rowOff') / EMU_PER_PX), + }; +} + +// normalizeRelPath resolves a rels Target such as "../media/image5.png" +// against the part's own directory ("xl/drawings/") into a zip path. +function normalizeRelPath(baseDir: string, target: string): string { + const parts = `${baseDir}/${target}`.split('/'); + const out: string[] = []; + for (const part of parts) { + if (part === '' || part === '.') continue; + if (part === '..') out.pop(); + else out.push(part); + } + return out.join('/'); +} + +function relsMap(relsXml: string): Map { + const map = new Map(); + for (const tag of relsXml.match(/]*>/g) ?? []) { + const id = attr(tag, 'Id'); + const target = attr(tag, 'Target'); + if (id && target) map.set(id, target); + } + return map; +} + +async function mediaDataUrl(zip: JSZip, mediaPath: string): Promise { + const file = zip.file(mediaPath); + if (!file) return ''; + const b64 = await file.async('base64'); + const ext = (mediaPath.split('.').pop() || 'png').toLowerCase(); + const mime = ext === 'jpg' ? 'image/jpeg' : `image/${ext}`; + return `data:${mime};base64,${b64}`; +} + +/** + * extractExcelImages reads every embedded picture of an .xlsx package with + * its cell anchor, converted to preview pixel geometry. Best-effort: anchors + * that reference unreadable media are skipped. + */ +export async function extractExcelImages(buf: ArrayBuffer): Promise { + const zip = await JSZip.loadAsync(buf); + + // Sheet order in workbook.xml matches SheetJS's workbook.SheetNames order. + const workbookXml = (await zip.file('xl/workbook.xml')?.async('string')) ?? ''; + const sheetRids = (workbookXml.match(/]*>/g) ?? []).map(tag => attr(tag, 'r:id')); + + const wbRelsXml = (await zip.file('xl/_rels/workbook.xml.rels')?.async('string')) ?? ''; + const wbRels = relsMap(wbRelsXml); + + const mediaCache = new Map(); + const resolveMedia = async (baseDir: string, relsXml: string, rid: string): Promise<{ src: string } | null> => { + const mediaTarget = relsMap(relsXml).get(rid); + if (!mediaTarget) return null; + const mediaPath = normalizeRelPath(baseDir, mediaTarget); + let src = mediaCache.get(mediaPath); + if (src === undefined) { + src = await mediaDataUrl(zip, mediaPath); + mediaCache.set(mediaPath, src ?? ''); + } + return src ? { src } : null; + }; + + const result: ExcelDrawingImage[] = []; + for (let sheetIndex = 0; sheetIndex < sheetRids.length; sheetIndex++) { + const sheetTarget = wbRels.get(sheetRids[sheetIndex]); + if (!sheetTarget) continue; + const sheetPath = normalizeRelPath('xl', sheetTarget); + const sheetXml = (await zip.file(sheetPath)?.async('string')) ?? ''; + const drawingTag = sheetXml.match(/]*\br:id="([^"]+)"/); + if (!drawingTag) continue; + + const sheetBaseDir = sheetPath.replace(/[^/]+$/, ''); + const sheetRelsXml = + (await zip.file(normalizeRelPath(sheetBaseDir, `_rels/${sheetPath.split('/').pop()}.rels`))?.async('string')) ?? ''; + const drawingTarget = relsMap(sheetRelsXml).get(drawingTag[1]); + if (!drawingTarget) continue; + const drawingPath = normalizeRelPath(sheetBaseDir, drawingTarget); + const drawingXml = (await zip.file(drawingPath)?.async('string')) ?? ''; + if (!drawingXml) continue; + + const drawingBaseDir = drawingPath.replace(/[^/]+$/, ''); + const drawingRelsXml = + (await zip.file(normalizeRelPath(drawingBaseDir, `_rels/${drawingPath.split('/').pop()}.rels`))?.async('string')) ?? ''; + + const anchorRe = //g; + for (const anchor of drawingXml.match(anchorRe) ?? []) { + const blip = anchor.match(/]*\br:embed="([^"]+)"/); + if (!blip) continue; + const media = await resolveMedia(drawingBaseDir, drawingRelsXml, blip[1]); + if (!media) continue; + + const fromMatch = anchor.match(/[\s\S]*?<\/xdr:from>/); + if (!fromMatch) continue; + const toMatch = anchor.match(/[\s\S]*?<\/xdr:to>/); + const extMatch = anchor.match(/ { + let x = anchor.colOffPx; + let y = anchor.rowOffPx; + const rowEl = table.rows[Math.min(anchor.row, table.rows.length - 1)]; + if (rowEl) { + y += rowEl.getBoundingClientRect().top - tableRect.top; + const cellEl = rowEl.cells[Math.min(anchor.col, rowEl.cells.length - 1)]; + if (cellEl) { + x += cellEl.getBoundingClientRect().left - tableRect.left; + } + } + return { x, y }; + }; + + const from = point(img.from); + let width = img.extPx?.width ?? 160; + let height = img.extPx?.height ?? 120; + if (img.to) { + const to = point(img.to); + width = Math.max(to.x - from.x, 2); + height = Math.max(to.y - from.y, 2); + } + + const el = document.createElement('img'); + el.className = 'excel-preview-image'; + el.src = img.src; + el.alt = ''; + el.style.position = 'absolute'; + el.style.left = `${Math.round(from.x)}px`; + el.style.top = `${Math.round(from.y)}px`; + el.style.width = `${Math.round(width)}px`; + el.style.height = `${Math.round(height)}px`; + el.style.objectFit = 'fill'; + el.style.zIndex = '5'; + parent.appendChild(el); + } +}