在 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 PdfsUIzhTW from '@univerjs-pro/pdfs-ui/locale/zh-TW' import { LocaleType, mergeLocales, Univer } from '@univerjs/core' import { FUniver } from '@univerjs/core/facade' import DesignzhTW from '@univerjs/design/locale/zh-TW' import { UniverUIPlugin } from '@univerjs/ui' import UIzhTW from '@univerjs/ui/locale/zh-TW' 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_TW, locales: { [LocaleType.ZH_TW]: mergeLocales( DesignzhTW, UIzhTW, PdfsUIzhTW, ), }, }) 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>你覺得這篇文檔如何?