@ysgroup/ui 0.1.42 → 0.1.43
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/package.json +7 -4
- package/src/components/YsDocViewer.vue +218 -0
- package/src/components/YsFilePreview.vue +215 -158
- package/src/components/YsMediaPlayer.vue +720 -0
- package/src/components/YsPdfViewer.vue +752 -0
- package/src/components/YsRowActionMenu.vue +1 -3
- package/src/components/YsSheetViewer.vue +461 -0
- package/src/components/YsViewerIcon.vue +53 -0
- package/src/components/YsViewerState.vue +66 -0
- package/src/filePreview.ts +143 -8
- package/src/index.ts +12 -0
- package/src/preview/docx.ts +27 -0
- package/src/preview/media.ts +97 -0
- package/src/preview/pdfAssets.ts +381 -0
- package/src/preview/pdfLayout.ts +125 -0
- package/src/preview/pdfjs.ts +38 -0
- package/src/preview/sheetGrid.ts +205 -0
- package/src/preview/viewer.css +255 -0
- package/src/vTip.ts +93 -0
- package/src/vendor/docx-preview.LICENSE.txt +206 -0
- package/src/vendor/docx-preview.d.mts +25 -0
- package/src/vendor/docx-preview.mjs +42 -0
package/package.json
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ysgroup/ui",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.43",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"types": "src/index.ts",
|
|
7
7
|
"dependencies": {
|
|
8
|
+
"@e965/xlsx": "0.20.3",
|
|
8
9
|
"@element-plus/icons-vue": "^2.3.1",
|
|
9
|
-
"@ysgroup/contracts": "0.1.8"
|
|
10
|
+
"@ysgroup/contracts": "0.1.8",
|
|
11
|
+
"pdfjs-dist": "6.5.438"
|
|
10
12
|
},
|
|
11
13
|
"peerDependencies": {
|
|
12
|
-
"@ysgroup/theme": ">=0.2.
|
|
14
|
+
"@ysgroup/theme": ">=0.2.6",
|
|
13
15
|
"element-plus": ">=2.9",
|
|
14
16
|
"vue": ">=3.5"
|
|
15
17
|
},
|
|
@@ -22,6 +24,7 @@
|
|
|
22
24
|
"access": "public"
|
|
23
25
|
},
|
|
24
26
|
"devDependencies": {
|
|
25
|
-
"@ysgroup/theme": "^0.2.
|
|
27
|
+
"@ysgroup/theme": "^0.2.6",
|
|
28
|
+
"docx-preview": "^0.4.1"
|
|
26
29
|
}
|
|
27
30
|
}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
<!-- YsDocViewer —— Word(.docx)只读预览,docx-preview 高保真渲染:段落、标题、表格、图片、列表、页眉页脚、脚注,按分页显示。
|
|
2
|
+
纸张与 PDF 阅读器同一套:亮色浅灰舞台 + 白纸阴影,暗色深色舞台 + 白纸略降亮度,可切夜间护眼;
|
|
3
|
+
默认适合宽度(窄屏自动缩小,宽屏不放大),可放大缩小。文档里的链接只保留 http(s)/mailto,新标签页打开。
|
|
4
|
+
docx-preview(含 jszip,约 60 KB gzip)在第一次打开 Word 时才加载。旧版 .doc 不支持。 -->
|
|
5
|
+
<script setup lang="ts">
|
|
6
|
+
import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from "vue";
|
|
7
|
+
import YsViewerIcon from "./YsViewerIcon.vue";
|
|
8
|
+
import YsViewerState from "./YsViewerState.vue";
|
|
9
|
+
import "../preview/viewer.css";
|
|
10
|
+
import { formatZoom, zoomIn as nextZoomIn, zoomOut as nextZoomOut } from "../preview/pdfLayout";
|
|
11
|
+
import { fitDocZoom, sanitizeLinks } from "../preview/docx";
|
|
12
|
+
|
|
13
|
+
const props = withDefaults(
|
|
14
|
+
defineProps<{
|
|
15
|
+
/** docx 内容:Blob、ArrayBuffer 或字节。 */
|
|
16
|
+
source: Blob | ArrayBuffer | Uint8Array | null;
|
|
17
|
+
/** 是否显示工具条上的全屏按钮。 */
|
|
18
|
+
fullscreenButton?: boolean;
|
|
19
|
+
}>(),
|
|
20
|
+
{ fullscreenButton: true },
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
const emit = defineEmits<{
|
|
24
|
+
loaded: [pages: number];
|
|
25
|
+
error: [message: string];
|
|
26
|
+
}>();
|
|
27
|
+
|
|
28
|
+
const root = ref<HTMLElement | null>(null);
|
|
29
|
+
const scroller = ref<HTMLElement | null>(null);
|
|
30
|
+
const body = ref<HTMLElement | null>(null);
|
|
31
|
+
const styles = ref<HTMLElement | null>(null);
|
|
32
|
+
const status = ref<"loading" | "ready" | "error">("loading");
|
|
33
|
+
const errorText = ref("");
|
|
34
|
+
const pages = ref(0);
|
|
35
|
+
const zoomMode = ref<"fit" | number>("fit");
|
|
36
|
+
const naturalWidth = ref(0);
|
|
37
|
+
const boxWidth = ref(0);
|
|
38
|
+
const night = ref(false);
|
|
39
|
+
const isFullscreen = ref(false);
|
|
40
|
+
let token = 0;
|
|
41
|
+
let resizeObserver: ResizeObserver | null = null;
|
|
42
|
+
|
|
43
|
+
const zoom = computed(() => (typeof zoomMode.value === "number" ? zoomMode.value : fitDocZoom(naturalWidth.value, boxWidth.value)));
|
|
44
|
+
|
|
45
|
+
function describe(reason: unknown): string {
|
|
46
|
+
const message = reason instanceof Error ? reason.message : String(reason ?? "");
|
|
47
|
+
// 先判断组件加载失败(动态导入、网络),再判断文件本身不是 docx。
|
|
48
|
+
if (/dynamically imported module|Importing a module script failed|does not provide an export|Loading chunk|fetch/i.test(message))
|
|
49
|
+
return "Word 预览组件加载失败,请检查网络后重试。";
|
|
50
|
+
if (/zip|central directory|end of data|signature/i.test(message)) return "文件不是有效的 Word(.docx)文档。旧版 .doc 请下载后用 Word / WPS 打开。";
|
|
51
|
+
return "Word 文档解析失败,请下载后查看。";
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function render() {
|
|
55
|
+
const mine = ++token;
|
|
56
|
+
status.value = "loading";
|
|
57
|
+
errorText.value = "";
|
|
58
|
+
pages.value = 0;
|
|
59
|
+
naturalWidth.value = 0;
|
|
60
|
+
zoomMode.value = "fit";
|
|
61
|
+
body.value?.replaceChildren();
|
|
62
|
+
styles.value?.replaceChildren();
|
|
63
|
+
if (!props.source) return;
|
|
64
|
+
try {
|
|
65
|
+
// 预打包的纯 ESM(docx-preview + jszip),见 scripts/bundle-docx-preview.mjs。
|
|
66
|
+
const { renderAsync } = await import("../vendor/docx-preview.mjs");
|
|
67
|
+
if (mine !== token || !body.value || !styles.value) return;
|
|
68
|
+
const data = props.source instanceof Blob ? props.source : new Blob([props.source as BlobPart]);
|
|
69
|
+
await renderAsync(data, body.value, styles.value, {
|
|
70
|
+
className: "docx",
|
|
71
|
+
inWrapper: true,
|
|
72
|
+
breakPages: true,
|
|
73
|
+
ignoreLastRenderedPageBreak: false,
|
|
74
|
+
renderHeaders: true,
|
|
75
|
+
renderFooters: true,
|
|
76
|
+
renderFootnotes: true,
|
|
77
|
+
renderEndnotes: true,
|
|
78
|
+
renderComments: false,
|
|
79
|
+
renderChanges: false,
|
|
80
|
+
useBase64URL: true,
|
|
81
|
+
experimental: true,
|
|
82
|
+
});
|
|
83
|
+
if (mine !== token) return;
|
|
84
|
+
sanitizeLinks(body.value);
|
|
85
|
+
status.value = "ready";
|
|
86
|
+
// 显示出来(v-show)后才量得到页面宽度。
|
|
87
|
+
await nextTick();
|
|
88
|
+
if (mine !== token || !body.value) return;
|
|
89
|
+
const sections = body.value.querySelectorAll<HTMLElement>(".docx-wrapper > section.docx");
|
|
90
|
+
pages.value = sections.length;
|
|
91
|
+
naturalWidth.value = Math.max(0, ...Array.from(sections, (s) => s.offsetWidth));
|
|
92
|
+
measure();
|
|
93
|
+
emit("loaded", pages.value);
|
|
94
|
+
} catch (reason) {
|
|
95
|
+
if (mine !== token) return;
|
|
96
|
+
body.value?.replaceChildren();
|
|
97
|
+
status.value = "error";
|
|
98
|
+
errorText.value = describe(reason);
|
|
99
|
+
emit("error", errorText.value);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function measure() {
|
|
104
|
+
if (scroller.value) boxWidth.value = scroller.value.clientWidth;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function zoomStep(direction: 1 | -1) {
|
|
108
|
+
zoomMode.value = direction > 0 ? nextZoomIn(zoom.value) : nextZoomOut(zoom.value);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function onWheel(event: WheelEvent) {
|
|
112
|
+
if (!event.ctrlKey && !event.metaKey) return;
|
|
113
|
+
event.preventDefault();
|
|
114
|
+
zoomStep(event.deltaY < 0 ? 1 : -1);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async function toggleFullscreen() {
|
|
118
|
+
if (document.fullscreenElement) await document.exitFullscreen().catch(() => undefined);
|
|
119
|
+
else await root.value?.requestFullscreen?.().catch(() => undefined);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function onFullscreenChange() {
|
|
123
|
+
isFullscreen.value = document.fullscreenElement === root.value;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
onMounted(() => {
|
|
127
|
+
resizeObserver = new ResizeObserver(() => measure());
|
|
128
|
+
if (scroller.value) resizeObserver.observe(scroller.value);
|
|
129
|
+
document.addEventListener("fullscreenchange", onFullscreenChange);
|
|
130
|
+
void render();
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
watch(
|
|
134
|
+
() => props.source,
|
|
135
|
+
() => void render(),
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
onBeforeUnmount(() => {
|
|
139
|
+
token += 1;
|
|
140
|
+
resizeObserver?.disconnect();
|
|
141
|
+
document.removeEventListener("fullscreenchange", onFullscreenChange);
|
|
142
|
+
});
|
|
143
|
+
</script>
|
|
144
|
+
|
|
145
|
+
<template>
|
|
146
|
+
<div ref="root" class="ys-viewer ys-doc" :class="{ 'is-night': night }">
|
|
147
|
+
<div v-if="status === 'ready'" class="ys-viewer__bar" role="toolbar" aria-label="Word 工具条">
|
|
148
|
+
<span class="ys-viewer__pager">共 {{ pages }} 页</span>
|
|
149
|
+
<span class="ys-viewer__spacer" />
|
|
150
|
+
<button type="button" class="ys-viewer__btn" title="缩小" aria-label="缩小" @click="zoomStep(-1)"><YsViewerIcon name="minus" /></button>
|
|
151
|
+
<button type="button" class="ys-viewer__btn ys-doc__zoom" title="适合宽度" aria-label="适合宽度" @click="zoomMode = 'fit'">{{ formatZoom(zoom) }}</button>
|
|
152
|
+
<button type="button" class="ys-viewer__btn" title="放大" aria-label="放大" @click="zoomStep(1)"><YsViewerIcon name="plus" /></button>
|
|
153
|
+
<span class="ys-viewer__sep" />
|
|
154
|
+
<button type="button" class="ys-viewer__btn" :class="{ 'is-active': night }" :aria-pressed="night" title="夜间护眼" aria-label="夜间护眼" @click="night = !night">
|
|
155
|
+
<YsViewerIcon name="moon" />
|
|
156
|
+
</button>
|
|
157
|
+
<button v-if="fullscreenButton" type="button" class="ys-viewer__btn" :title="isFullscreen ? '退出全屏' : '全屏'" :aria-label="isFullscreen ? '退出全屏' : '全屏'" @click="toggleFullscreen">
|
|
158
|
+
<YsViewerIcon :name="isFullscreen ? 'minimize' : 'maximize'" />
|
|
159
|
+
</button>
|
|
160
|
+
<slot name="toolbar" />
|
|
161
|
+
</div>
|
|
162
|
+
<YsViewerState v-if="status === 'loading'" title="正在打开 Word 文档…" />
|
|
163
|
+
<YsViewerState v-else-if="status === 'error'" type="error" title="无法显示此文档" :detail="errorText">
|
|
164
|
+
<template v-if="$slots['error-actions']" #actions><slot name="error-actions" /></template>
|
|
165
|
+
</YsViewerState>
|
|
166
|
+
<div v-show="status === 'ready'" ref="scroller" class="ys-doc__scroller" tabindex="0" aria-label="Word 文档" @wheel="onWheel">
|
|
167
|
+
<div ref="styles" class="ys-doc__styles" />
|
|
168
|
+
<div ref="body" class="ys-doc__body" :style="{ zoom }" />
|
|
169
|
+
</div>
|
|
170
|
+
</div>
|
|
171
|
+
</template>
|
|
172
|
+
|
|
173
|
+
<style scoped>
|
|
174
|
+
.ys-doc__scroller {
|
|
175
|
+
flex: 1;
|
|
176
|
+
min-height: 0;
|
|
177
|
+
overflow: auto;
|
|
178
|
+
overscroll-behavior: contain;
|
|
179
|
+
}
|
|
180
|
+
.ys-doc__scroller:focus {
|
|
181
|
+
outline: none;
|
|
182
|
+
}
|
|
183
|
+
.ys-doc__styles {
|
|
184
|
+
display: none;
|
|
185
|
+
}
|
|
186
|
+
.ys-doc__zoom {
|
|
187
|
+
min-width: 52px;
|
|
188
|
+
font-weight: 600;
|
|
189
|
+
}
|
|
190
|
+
/* docx-preview 生成的节点不带 scoped 标记,用 :deep。特异度高于它自带的 .docx-wrapper 样式。 */
|
|
191
|
+
.ys-doc__body :deep(.docx-wrapper) {
|
|
192
|
+
display: flex;
|
|
193
|
+
flex-flow: column;
|
|
194
|
+
align-items: center;
|
|
195
|
+
width: max-content;
|
|
196
|
+
min-width: 100%;
|
|
197
|
+
box-sizing: border-box;
|
|
198
|
+
padding: 24px;
|
|
199
|
+
background: transparent;
|
|
200
|
+
}
|
|
201
|
+
.ys-doc__body :deep(.docx-wrapper > section.docx) {
|
|
202
|
+
flex: none;
|
|
203
|
+
margin: 0 0 24px;
|
|
204
|
+
background: var(--ys-color-paper);
|
|
205
|
+
box-shadow: var(--ys-shadow-paper);
|
|
206
|
+
filter: var(--ys-paper-filter);
|
|
207
|
+
}
|
|
208
|
+
.ys-doc__body :deep(.docx-wrapper > section.docx:last-child) {
|
|
209
|
+
margin-bottom: 0;
|
|
210
|
+
}
|
|
211
|
+
.ys-doc.is-night .ys-doc__body :deep(.docx-wrapper > section.docx) {
|
|
212
|
+
box-shadow: 0 0 0 1px var(--ys-color-border);
|
|
213
|
+
filter: var(--ys-paper-filter-night);
|
|
214
|
+
}
|
|
215
|
+
.ys-doc__body :deep(a[href]) {
|
|
216
|
+
cursor: pointer;
|
|
217
|
+
}
|
|
218
|
+
</style>
|