自定义组件
Web SDK 提供了多种方式来集成自定义组件,使你能够扩展和定制 Web SDK 的功能。本指南将介绍几种常用的方法。
Univer 的渲染函数不接受组件作为参数。需要先通过 Facade API 注册组件,Univer 才能在各个挂载点找到它。
如果还没有 Facade API 实例,请参阅 Facade API。
注册自定义组件
univerAPI.registerComponent(componentKey, CustomComponent, options)univerAPI.registerComponent 接受三个参数:
componentKey:组件的唯一标识,用于在 Univer 内引用该组件。CustomComponent:组件本身,可以是 React 组件、Vue 组件或 Web Component。options:可选配置,例如组件依赖的框架。
React 组件
React 组件不需要额外配置,只需确保它是一个有效的 React 组件:
function ReactComponent(props: Record<string, any>) { return <div>Hello Univer!</div>}univerAPI.registerComponent('MyReactComponent', ReactComponent)Vue 组件
Vue 3.x
注册 Vue 组件前,需要先安装并注册 @univerjs/ui-adapter-vue3 包中的 UniverVue3AdapterPlugin:
pnpm add @univerjs/ui-adapter-vue3npm install @univerjs/ui-adapter-vue3yarn add @univerjs/ui-adapter-vue3bun add @univerjs/ui-adapter-vue3import { UniverVue3AdapterPlugin } from '@univerjs/ui-adapter-vue3'univer.registerPlugin(UniverVue3AdapterPlugin)注册 Vue 组件时,将 framework 选项设置为 'vue3':
const Vue3Component = defineComponent({ setup(props) { return () => <div>Hello Univer!</div> },})univerAPI.registerComponent('MyVue3Component', Vue3Component, { framework: 'vue3',})Vue 2.x
Vue 2.x 已 EOL(End of Life),不再维护,因此 Univer 不提供 Vue 2.x 的 UI 适配器插件。可以通过自定义插件实现,以下示例即是一个 Vue 2.x 适配器:
import { DependentOn, Inject, Injector, Plugin } from '@univerjs/core'import { ComponentManager, UniverUIPlugin } from '@univerjs/ui'import Vue from 'vue'/** * The plugin that allows Univer to use Vue 2 components as UI components. */@DependentOn(UniverUIPlugin)export class UniverVue2AdapterPlugin extends Plugin { static override pluginName = 'UNIVER_UI_VUE2_ADAPTER_PLUGIN' constructor( private readonly _config = {}, @Inject(Injector) protected readonly _injector: Injector, @Inject(ComponentManager) protected readonly _componentManager: ComponentManager, ) { super() } override onStarting(): void { const { createElement, useEffect, useRef } = this._componentManager.reactUtils this._componentManager.setHandler('vue2', (component: any) => { return (props: Record<string, any>) => createElement(VueComponentWrapper, { component, props: Object.keys(props).reduce<Record<string, any>>((acc, key) => { if (key !== 'key') { acc[key] = props[key] } return acc }, {}), reactUtils: { createElement, useEffect, useRef }, }) }) }}export function VueComponentWrapper(options: { component: any props: Record<string, any> reactUtils: typeof ComponentManager.prototype.reactUtils}) { const { component, props, reactUtils } = options const { createElement, useEffect, useRef } = reactUtils const domRef = useRef<HTMLDivElement>(null) useEffect(() => { if (!domRef.current) return const Constructor = Vue.extend(component) const instance = new Constructor({ data: props, }) instance.$mount() domRef.current.appendChild(instance.$el) return () => { instance.$destroy() } }, [props]) return createElement('div', { ref: domRef })}然后注册该插件:
import { UniverVue2AdapterPlugin } from './ui-adapter-vue2'univer.registerPlugin(UniverUIPlugin)univer.registerPlugin(UniverVue2AdapterPlugin)注册 Vue 2.x 组件时,将 framework 选项设置为 'vue2':
const Vue2Component = Vue.component('MyVue2Component', { template: '<div>Hello, Univer!</div>',})univerAPI.registerComponent('MyVue2Component', Vue2Component, { framework: 'vue2',})Web Components
Web Component 必须符合 Web Components 标准。请安装并注册 @univerjs/ui-adapter-web-component 包中的 UniverWebComponentAdapterPlugin:
pnpm add @univerjs/ui-adapter-web-componentnpm install @univerjs/ui-adapter-web-componentyarn add @univerjs/ui-adapter-web-componentbun add @univerjs/ui-adapter-web-componentimport { UniverWebComponentAdapterPlugin } from '@univerjs/ui-adapter-web-component'univer.registerPlugin(UniverWebComponentAdapterPlugin)注册 Web Component 时,将 framework 选项设置为 'web-component':
class WebComponent extends HTMLElement { constructor() { super() const shadow = this.attachShadow({ mode: 'open' }) const div = document.createElement('div') div.textContent = 'Hello Univer!' shadow.appendChild(div) }}univerAPI.registerComponent('my-web-component', WebComponent, { framework: 'web-component',})使用自定义组件
以下方法可以将自定义组件挂载到 Univer 自身的界面中。
注意事项
- 调用这些方法前,请确保 Univer 已完成渲染。2. 组件需要先注册再使用。3. 调用
dispose()清理并移除添加的组件,否则会造成内存泄漏。
添加自定义菜单项
顶部菜单栏(Ribbon)和右键菜单(Context Menu)都可以放置自定义组件。这些菜单项应由你的插件提供,使用前注册对应组件,并在插件销毁时一并释放注册。
替换内置组件
警告
替换内置组件可能导致部分功能无法正常工作。请先阅读拟替换组件的源码和文档,再谨慎操作。
univerAPI.registerComponent 默认注册覆盖组件(override: true)。无论插件在调用之前还是之后注册内置默认组件,覆盖组件都优先。每个 componentKey 同时只能有一个覆盖组件,因此在注册新的覆盖组件前,应先释放上一个注册。对返回的注册对象调用 dispose(),会恢复已注册的内置默认组件。传入 { override: false } 则注册默认组件。
覆盖组件应在对应视图首次打开前注册。修改注册不会重新渲染已挂载的视图。
例如,以下代码替换内置的 ColorPicker 组件:
// 此代码仅可在 UI 层面替换内置 ColorPicker 组件,无法替代其功能实现univerAPI.registerComponent('UI_COLOR_PICKER_COMPONENT', () => <input type="color" />)作为内容组件添加到……
在侧边栏中使用
univerAPI.openSidebar 会在 Univer 界面中打开侧边栏,并在其中渲染自定义组件。
// 你应该在合适的时机(比如渲染完成)注册组件univerAPI.registerComponent('MyCustomSidebarComponent', () => <div>Hello Univer!</div>)const sidebar = univerAPI.openSidebar({ header: { title: 'My Sidebar' }, children: { label: 'MyCustomSidebarComponent' }, onClose: () => { console.log('close') }, width: 360,})// 稍后关闭侧边栏sidebar.dispose()在对话框中使用
univerAPI.openDialog 会打开一个渲染自定义组件的对话框。
// 你应该在合适的时机(比如渲染完成)注册组件univerAPI.registerComponent('MyCustomDialogComponent', () => <div>Hello Univer!</div>)const dialog = univerAPI.openDialog({ id: 'unique-dialog-id', // 对话框的唯一标识符 draggable: true, width: 300, title: { title: 'My Dialog' }, children: { label: 'MyCustomDialogComponent', }, destroyOnClose: true, preservePositionOnDestroy: true, onClose: () => {},})// 稍后关闭对话框dialog.dispose()你觉得这篇文档如何?