|
|
@@ -2,7 +2,7 @@
|
|
|
<div class="container">
|
|
|
<div class="modal-height">
|
|
|
<div class="file-box">
|
|
|
- <p class="file-name" v-show="!renderLoading">{{ file.filename }}</p>
|
|
|
+ <!-- <p class="file-name" v-show="!renderLoading">{{ file.filename }}</p> -->
|
|
|
<div class="view-wrapper">
|
|
|
<div class="preview-wrapper" ref="wrapper" v-if="isAbleView">
|
|
|
<div class="preview-inner" ref="preview-inner">
|
|
|
@@ -41,6 +41,7 @@
|
|
|
>
|
|
|
‹
|
|
|
</button>
|
|
|
+
|
|
|
<img class="viewfile-image-preview-img" :src="imageSrcList[previewIndex]" alt="" />
|
|
|
<button
|
|
|
v-if="imageSrcList.length > 1"
|
|
|
@@ -52,7 +53,12 @@
|
|
|
</button>
|
|
|
</div>
|
|
|
</div>
|
|
|
- <div v-else class="output" v-show="!renderLoading" ref="output"></div>
|
|
|
+ <div
|
|
|
+ v-else
|
|
|
+ class="output"
|
|
|
+ :style="{ visibility: renderLoading ? 'hidden' : 'visible' }"
|
|
|
+ ref="output"
|
|
|
+ ></div>
|
|
|
</div>
|
|
|
</div>
|
|
|
<view-other-component
|
|
|
@@ -67,6 +73,7 @@
|
|
|
|
|
|
<script setup>
|
|
|
import { ref, computed, watch, nextTick, onBeforeUnmount, onMounted } from "vue"
|
|
|
+import { debounce } from "lodash-es"
|
|
|
import { readDataURL, render } from "./util"
|
|
|
import ViewOtherComponent from "./vendors/other/index.vue"
|
|
|
|
|
|
@@ -164,12 +171,54 @@ function onKeydown(e) {
|
|
|
if (e.key === "ArrowLeft") prevImage()
|
|
|
}
|
|
|
|
|
|
+const scaleDocx = () => {
|
|
|
+ const docxWrapper = output.value?.querySelector(".docx-wrapper")
|
|
|
+ if (!docxWrapper) return
|
|
|
+
|
|
|
+ // 重置样式以便重新计算
|
|
|
+ docxWrapper.style.transform = ""
|
|
|
+ docxWrapper.style.transformOrigin = ""
|
|
|
+
|
|
|
+ const sections = docxWrapper.querySelectorAll("section.docx")
|
|
|
+ if (!sections.length) return
|
|
|
+
|
|
|
+ // 获取内容宽度(基于第一页)
|
|
|
+ const sectionWidth = sections[0].offsetWidth
|
|
|
+ const contentWidth = sectionWidth + 40 // 加上两侧边距冗余
|
|
|
+ const containerWidth = output.value.clientWidth
|
|
|
+
|
|
|
+ // 锁定容器宽度为内容宽度,确保缩放基准正确
|
|
|
+ docxWrapper.style.width = `${contentWidth}px`
|
|
|
+
|
|
|
+ // 仅在内容宽度超出容器时进行缩放
|
|
|
+ if (contentWidth > containerWidth) {
|
|
|
+ const scale = (containerWidth - 10) / contentWidth // 留出 10px 间隙
|
|
|
+ docxWrapper.style.transformOrigin = "top left"
|
|
|
+ docxWrapper.style.transform = `scale(${scale})`
|
|
|
+ docxWrapper.style.marginLeft = "5px" // 居中微调
|
|
|
+ docxWrapper.style.height = "auto" // 确保高度自适应
|
|
|
+ // 缩放后,由于transform不改变流体高度,容器底部可能会有大量空白
|
|
|
+ // 可以通过设置父容器高度或者 marginBottom 来修复,但稍微复杂,先保证显示正常
|
|
|
+ } else {
|
|
|
+ docxWrapper.style.margin = "0 auto" // 桌面端居中
|
|
|
+ docxWrapper.style.marginLeft = ""
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+const handleResize = debounce(() => {
|
|
|
+ if (props.file?.type === "docx" || props.file?.type === "doc") {
|
|
|
+ scaleDocx()
|
|
|
+ }
|
|
|
+}, 200)
|
|
|
+
|
|
|
onMounted(() => {
|
|
|
window.addEventListener("keydown", onKeydown)
|
|
|
+ window.addEventListener("resize", handleResize)
|
|
|
})
|
|
|
|
|
|
onBeforeUnmount(() => {
|
|
|
window.removeEventListener("keydown", onKeydown)
|
|
|
+ window.removeEventListener("resize", handleResize)
|
|
|
})
|
|
|
|
|
|
/**
|
|
|
@@ -182,6 +231,8 @@ const renderResult = (buffer, extend) => {
|
|
|
}
|
|
|
output.value.innerHTML = ""
|
|
|
const node = document.createElement("div")
|
|
|
+
|
|
|
+
|
|
|
const child = output.value.appendChild(node)
|
|
|
return new Promise((resolve, reject) =>
|
|
|
render(buffer, extend, child).then(resolve).catch(reject)
|
|
|
@@ -215,6 +266,12 @@ watch(
|
|
|
previewIndex.value = 0
|
|
|
renderResult(newFile.fileBuffer, newFile.type).finally(() => {
|
|
|
renderLoading.value = false
|
|
|
+ if (newFile.type === "docx" || newFile.type === "doc") {
|
|
|
+ nextTick(() => {
|
|
|
+ // 延迟一下等待DOM渲染完成
|
|
|
+ setTimeout(scaleDocx, 100)
|
|
|
+ })
|
|
|
+ }
|
|
|
})
|
|
|
})
|
|
|
} catch (error) {
|
|
|
@@ -309,6 +366,7 @@ watch(
|
|
|
.viewfile-image-preview-next {
|
|
|
right: 16px;
|
|
|
}
|
|
|
+
|
|
|
</style>
|
|
|
<style lang="less" scoped>
|
|
|
@import url("./index.less");
|
|
|
@@ -322,7 +380,7 @@ watch(
|
|
|
height: 100%;
|
|
|
.file-box {
|
|
|
flex: 1;
|
|
|
- padding: 15px 40px;
|
|
|
+ // padding: 15px 40px;
|
|
|
overflow: scroll;
|
|
|
text-align: center;
|
|
|
|