超連結
超連結從文件指向外部網頁、電子郵件地址或其他內容。
外掛模式
安裝
Shell
pnpm add @univerjs/docs-hyper-link @univerjs/docs-hyper-link-uiShell
npm install @univerjs/docs-hyper-link @univerjs/docs-hyper-link-uiShell
yarn add @univerjs/docs-hyper-link @univerjs/docs-hyper-link-uiShell
bun add @univerjs/docs-hyper-link @univerjs/docs-hyper-link-ui使用
TypeScript
import { LocaleType, mergeLocales, Univer } from '@univerjs/core'import { UniverDocsHyperLinkPlugin } from '@univerjs/docs-hyper-link'import { UniverDocsHyperLinkUIPlugin } from '@univerjs/docs-hyper-link-ui'import DocsHyperLinkUIZhTW from '@univerjs/docs-hyper-link-ui/locale/zh-TW'import '@univerjs/docs-hyper-link-ui/lib/index.css'const univer = new Univer({ locale: LocaleType.ZH_TW, locales: { [LocaleType.ZH_TW]: mergeLocales( DocsHyperLinkUIZhTW, ), },})univer.registerPlugin(UniverDocsHyperLinkPlugin)univer.registerPlugin(UniverDocsHyperLinkUIPlugin)行動裝置支援
先配置行動版編輯器。在本頁外掛模式的配置中,將下面的桌面版註冊替換為同一套件匯出的行動版外掛,並且要在開啟文件之前完成替換。模型外掛、樣式、語言套件和 Facade 匯入保持不變,兩個版本不要同時註冊。
| 套件 | 桌面版註冊 | 行動版註冊 |
|---|---|---|
@univerjs/docs-hyper-link-ui | UniverDocsHyperLinkUIPlugin | UniverDocsHyperLinkMobileUIPlugin |
預設模式
安裝
Shell
pnpm add @univerjs/preset-docs-hyper-linkShell
npm install @univerjs/preset-docs-hyper-linkShell
yarn add @univerjs/preset-docs-hyper-linkShell
bun add @univerjs/preset-docs-hyper-link使用
TypeScript
import { UniverDocsCorePreset } from '@univerjs/preset-docs-core'import UniverPresetDocsCoreZhTW from '@univerjs/preset-docs-core/locales/zh-TW'import { UniverDocsHyperLinkPreset } from '@univerjs/preset-docs-hyper-link'import UniverPresetDocsHyperLinkZhTW from '@univerjs/preset-docs-hyper-link/locales/zh-TW'import { createUniver, LocaleType, mergeLocales } from '@univerjs/presets'import '@univerjs/preset-docs-core/lib/index.css'import '@univerjs/preset-docs-hyper-link/lib/index.css'const { univerAPI } = createUniver({ locale: LocaleType.ZH_TW, locales: { [LocaleType.ZH_TW]: mergeLocales( UniverPresetDocsCoreZhTW, UniverPresetDocsHyperLinkZhTW, ), }, presets: [ UniverDocsCorePreset(), UniverDocsHyperLinkPreset(), ],})使用 Facade API 建立及編輯連結
link(text, url) 追加連結文字並記錄其覆蓋的範圍,因此無須手動計算字元位置。之後要修改連結目標時,傳入 getLinks() 返回的範圍 ID。
TypeScript
const richText = univerAPI .newRichText() .text('Read ') .link('Univer Office SDK documentation', 'https://docs.univer.ai') .text(' for details.')const [link] = richText.getLinks()if (!link) throw new Error('Link was not created')richText.updateLink(link.rangeId, 'https://docs.univer.ai/guides')removeLink(link.rangeId) 移除連結目標,同時保留可見文字。
你覺得這篇文檔如何?