Przeglądaj źródła

fix: 修复图片渲染和恢复要素高亮

1. 图片渲染改进:
   - 移除图片说明文字(image-caption)
   - 保持图片原始尺寸,不强制居中
   - 简化图片容器样式

2. 恢复要素(实体)高亮:
   - 优先使用 blocks 的 markedHtml(包含实体高亮标记)
   - 仅当 blocks 无内容时才回退到 paragraphs(带格式但无实体标记)
   - 跳过 type=page 的根节点
何文松 4 tygodni temu
rodzic
commit
d599ee6f5b
2 zmienionych plików z 23 dodań i 21 usunięć
  1. 23 21
      frontend/vue-demo/src/views/Editor.vue
  2. BIN
      test/.~test.docx

+ 23 - 21
frontend/vue-demo/src/views/Editor.vue

@@ -477,44 +477,46 @@ function renderStructuredDocument(structuredDoc) {
   const images = structuredDoc.images || []
   const paragraphs = structuredDoc.paragraphs || []
   
-  // 优先使用 paragraphs(包含格式信息),如果存在的话
-  const hasParagraphsWithRuns = paragraphs.some(p => p.runs && p.runs.length > 0)
-  
   // 将所有元素合并
   const allElements = []
   
-  // 添加 blocks(带实体标记)或 paragraphs(带格式)
-  if (hasParagraphsWithRuns) {
-    // 使用 paragraphs 渲染(保留格式)
-    paragraphs.forEach(para => {
-      allElements.push({
-        type: 'paragraph',
-        index: para.index,
-        html: renderParagraphWithRuns(para)
-      })
-    })
-  } else {
+  // 检查 blocks 是否有有效内容(markedHtml 不为空)
+  const hasBlocksWithContent = blocks.some(b => b.markedHtml || b.html || b.plainText)
+  
+  // 优先使用 blocks(带实体高亮标记),如果有内容的话
+  if (hasBlocksWithContent) {
     // 使用 blocks 渲染(带实体标记)
     blocks.forEach(block => {
+      if (block.type === 'page') return // 跳过根节点
       allElements.push({
         type: 'block',
         index: block.index,
         html: block.markedHtml || block.html || block.plainText || ''
       })
     })
+  } else if (paragraphs.length > 0) {
+    // 回退到 paragraphs 渲染(保留格式,但无实体标记)
+    paragraphs.forEach(para => {
+      allElements.push({
+        type: 'paragraph',
+        index: para.index,
+        html: renderParagraphWithRuns(para)
+      })
+    })
   }
   
-  // 添加图片
+  // 添加图片(保持原始尺寸,不显示说明文字)
   images.forEach(img => {
+    // 图片样式:保持原始尺寸,不强制居中
+    const imgStyle = img.width && img.height 
+      ? `width:${img.width}px; height:${img.height}px;`
+      : 'max-width: 100%; height: auto;'
+    
     allElements.push({
       type: 'image',
       index: img.index,
-      html: `<div class="doc-image" style="text-align: center; margin: 16px 0;">
-        <img src="${img.url}" alt="${img.alt || '图片'}" 
-             style="max-width: 100%; height: auto;"
-             ${img.width ? `width="${img.width}"` : ''}
-             ${img.height ? `height="${img.height}"` : ''} />
-        ${img.alt ? `<p class="image-caption" style="color: #666; font-size: 12px; margin-top: 8px;">${img.alt}</p>` : ''}
+      html: `<div class="doc-image" style="margin: 8px 0;">
+        <img src="${img.url}" alt="${img.alt || '图片'}" style="${imgStyle}" />
       </div>`
     })
   })

BIN
test/.~test.docx