SpreadsheetPreview.tsx
1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"use client";
import { useEffect, useRef } from "react";
import type { SheetData } from "x-data-spreadsheet";
// 只读电子表格预览:动态加载 x-data-spreadsheet 并渲染。从 app/page.tsx 抽出。
export const SpreadsheetPreview = ({ sheets }: { sheets: SheetData[] }) => {
const containerRef = useRef<HTMLDivElement | null>(null);
useEffect(() => {
const container = containerRef.current;
if (!container) return undefined;
container.innerHTML = "";
let cancelled = false;
import("x-data-spreadsheet/dist/xspreadsheet.js")
.then(() => {
if (cancelled || !window.x_spreadsheet) return;
const instance = window.x_spreadsheet(container, {
mode: "read",
showToolbar: false,
showContextmenu: false,
showBottomBar: true,
view: {
height: () => container.clientHeight,
width: () => container.clientWidth,
},
});
instance.loadData(sheets);
})
.catch((e) => {
console.error("加载 x-data-spreadsheet 失败", e);
});
return () => {
cancelled = true;
container.innerHTML = "";
};
}, [sheets]);
return (
<div
ref={containerRef}
className="wiki-spreadsheet-readonly w-full h-full overflow-hidden"
onWheel={(e) => e.stopPropagation()}
/>
);
};
export default SpreadsheetPreview;