在 Astro 中整合
請先閱讀安裝與基本使用。以下範例透過 Astro 將 Univer Slides 掛載到具有明確高度的容器中。
使用一般的 <script>,在瀏覽器中直接初始化編輯器。首次載入時執行初始化,使用 ClientRouter 時透過 astro:page-load 重新掛載,在 astro:before-swap 銷毀舊執行個體。不要在 Astro frontmatter 匯入編輯器。詳見 Astro 導航事件。
整合步驟
- 在
mount()/astro:page-load中初始化 Univer - 在
astro:before-swap中銷毀 Univer
範例程式碼
Astro
<div id="univer-slides" style="height: 600px"></div><script> import { UniverLicensePlugin } from '@univerjs-pro/license' import { UniverSlidesPlugin } from '@univerjs-pro/slides' import { UniverSlidesUIPlugin } from '@univerjs-pro/slides-ui' import SlidesUIzhTW from '@univerjs-pro/slides-ui/locale/zh-TW' import SlideszhTW from '@univerjs-pro/slides/locale/zh-TW' import { LocaleType, mergeLocales, Univer, UniverInstanceType } from '@univerjs/core' import DesignzhTW from '@univerjs/design/locale/zh-TW' import { UniverDocsPlugin } from '@univerjs/docs' import { UniverDocsUIPlugin } from '@univerjs/docs-ui' import DocsUIzhTW from '@univerjs/docs-ui/locale/zh-TW' import { UniverDrawingPlugin } from '@univerjs/drawing' import { UniverRenderEnginePlugin } from '@univerjs/engine-render' 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/docs-ui/lib/index.css' import '@univerjs-pro/slides-ui/lib/index.css' let univerInstance: Univer | undefined function mount() { const container = document.getElementById('univer-slides') if (!container || univerInstance) return const univer = new Univer({ locale: LocaleType.ZH_TW, locales: { [LocaleType.ZH_TW]: mergeLocales( DesignzhTW, UIzhTW, DocsUIzhTW, SlideszhTW, SlidesUIzhTW, ), }, }) univer.registerPlugin(UniverLicensePlugin, { license: 'your license.txt', }) univer.registerPlugin(UniverRenderEnginePlugin) univer.registerPlugin(UniverUIPlugin, { container, }) univer.registerPlugin(UniverDocsPlugin) univer.registerPlugin(UniverDocsUIPlugin) univer.registerPlugin(UniverDrawingPlugin) univer.registerPlugin(UniverSlidesPlugin) univer.registerPlugin(UniverSlidesUIPlugin) univer.createUnit(UniverInstanceType.UNIVER_SLIDE, {}) univerInstance = univer } function dispose() { univerInstance?.dispose() univerInstance = undefined } mount() document.addEventListener('astro:page-load', mount) document.addEventListener('astro:before-swap', dispose)</script>你覺得這篇文檔如何?