UniverSheet.vue
3.65 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<template>
<div ref="container" class="univer-container"></div>
</template>
<script setup lang="ts">
import { onMounted, onBeforeUnmount, ref, shallowRef } from "vue";
import { Univer, UniverInstanceType, LocaleType, Tools, IUniverInstanceService } from "@univerjs/core";
import { defaultTheme } from "@univerjs/design";
import { UniverRenderEnginePlugin } from "@univerjs/engine-render";
import { UniverFormulaEnginePlugin } from "@univerjs/engine-formula";
import { UniverUIPlugin } from "@univerjs/ui";
import { UniverDocsPlugin } from "@univerjs/docs";
import { UniverDocsUIPlugin } from "@univerjs/docs-ui";
import { UniverSheetsPlugin } from "@univerjs/sheets";
import { UniverSheetsFormulaPlugin } from "@univerjs/sheets-formula";
import { UniverSheetsUIPlugin } from "@univerjs/sheets-ui";
// Import Locales
import zhCNDesign from "@univerjs/design/lib/locale/zh-CN";
import zhCNUI from "@univerjs/ui/lib/locale/zh-CN";
import zhCNDocsUI from "@univerjs/docs-ui/lib/locale/zh-CN";
import zhCNSheetsUI from "@univerjs/sheets-ui/lib/locale/zh-CN";
// Import CSS
import "@univerjs/design/lib/index.css";
import "@univerjs/ui/lib/index.css";
import "@univerjs/sheets-ui/lib/index.css";
import "@univerjs/docs-ui/lib/index.css";
// import "@univerjs/sheets-xlsx/lib/index.css"; // CSS not required or found
const props = defineProps<{
// options?: any;
}>();
const container = ref<HTMLElement | null>(null);
const univerRef = shallowRef<Univer | null>(null);
const workbook = shallowRef<any | null>(null);
const initUniver = () => {
if (!container.value) return;
const univer = new Univer({
theme: defaultTheme,
locale: LocaleType.ZH_CN,
locales: {
[LocaleType.ZH_CN]: Tools.deepMerge(
zhCNDesign,
zhCNUI,
zhCNDocsUI,
zhCNSheetsUI
),
},
});
univer.registerPlugin(UniverRenderEnginePlugin);
univer.registerPlugin(UniverFormulaEnginePlugin);
univer.registerPlugin(UniverUIPlugin, {
container: container.value,
header: true,
footer: true,
});
univer.registerPlugin(UniverDocsPlugin, {
hasScroll: false,
});
univer.registerPlugin(UniverDocsUIPlugin);
univer.registerPlugin(UniverSheetsPlugin);
univer.registerPlugin(UniverSheetsFormulaPlugin);
univer.registerPlugin(UniverSheetsUIPlugin);
// Create empty workbook initially to avoid blank screen
workbook.value = univer.createUnit(UniverInstanceType.UNIVER_SHEET, {});
univerRef.value = univer;
};
const createWorkbook = (data: any) => {
const univer = univerRef.value;
if (!univer) return;
const injector = (univer as any).__getInjector();
const instanceService = injector.get(IUniverInstanceService);
// Dispose existing workbook if any
if (workbook.value) {
try {
const unitId = workbook.value.getUnitId();
instanceService.disposeUnit(unitId);
} catch (e) {
console.warn("Dispose unit failed:", e);
}
}
// Create new workbook with data
const newWorkbook = univer.createUnit(UniverInstanceType.UNIVER_SHEET, data);
workbook.value = newWorkbook;
// 显式设置为当前活跃单位,确保 UI 切换
if (newWorkbook) {
const unitId = newWorkbook.getUnitId();
instanceService.setCurrentUnitForType(unitId);
instanceService.focusUnit(unitId);
}
};
onMounted(() => {
initUniver();
});
onBeforeUnmount(() => {
if (univerRef.value) {
univerRef.value.dispose();
univerRef.value = null;
}
});
defineExpose({
getUniver: () => univerRef.value,
getWorkbook: () => workbook.value,
createWorkbook,
});
</script>
<style scoped>
.univer-container {
width: 100%;
height: 100%;
overflow: hidden;
position: relative;
}
:deep(.univer-app-layout) {
height: 100%;
}
</style>