在 Astro 中集成
请先阅读安装和基本使用。下面的示例使用 Astro 挂载 Univer Pdfs,容器需要指定明确的高度。
使用普通的 <script> 元素在浏览器中直接初始化编辑器。首次加载时执行一次初始化,之后在 astro:page-load 时再次执行;如果使用了 ClientRouter,则在 astro:before-swap 之前销毁实例。请勿将编辑器代码放在 Astro frontmatter 中。详见 Astro 导航事件。
集成步骤
- 在
mount()/astro:page-load中创建 Univer 实例。 - 在
astro:before-swap中销毁该实例。
插件配置
UniverUIPlugin
containerstring | HTMLElement编辑器挂载的 DOM 元素或元素 ID。
UniverLicensePlugin
licensestring客户端许可证文件的内容。
示例代码
Astro
<div id="univer-pdfs" style="height: 600px"></div><script> import { UniverLicensePlugin } from '@univerjs-pro/license' import { UniverPdfsPlugin } from '@univerjs-pro/pdfs' import { UniverPdfsUIPlugin } from '@univerjs-pro/pdfs-ui' import PdfsUIzhCN from '@univerjs-pro/pdfs-ui/locale/zh-CN' import { LocaleType, mergeLocales, Univer } from '@univerjs/core' import { FUniver } from '@univerjs/core/facade' import DesignzhCN from '@univerjs/design/locale/zh-CN' import { UniverUIPlugin } from '@univerjs/ui' import UIzhCN from '@univerjs/ui/locale/zh-CN' import '@univerjs/design/lib/index.css' import '@univerjs/ui/lib/index.css' import '@univerjs-pro/pdfs-ui/lib/index.css' import '@univerjs-pro/pdfs/facade' let univerInstance: Univer | undefined function mount() { const container = document.getElementById('univer-pdfs') if (!container || univerInstance) return const univer = new Univer({ locale: LocaleType.ZH_CN, locales: { [LocaleType.ZH_CN]: mergeLocales( DesignzhCN, UIzhCN, PdfsUIzhCN, ), }, }) univer.registerPlugin(UniverLicensePlugin, { license: 'your license.txt', }) univer.registerPlugin(UniverUIPlugin, { container }) univer.registerPlugin(UniverPdfsPlugin) univer.registerPlugin(UniverPdfsUIPlugin) const univerAPI = FUniver.newAPI(univer) univerAPI.createPdf({ name: 'Untitled PDF' }) univerInstance = univer } function dispose() { univerInstance?.dispose() univerInstance = undefined } mount() document.addEventListener('astro:page-load', mount) document.addEventListener('astro:before-swap', dispose)</script>你觉得这篇文档如何?