カスタムコンポーネント
Web SDK は、カスタムコンポーネントを統合するための複数の方法を提供しており、その機能を拡張およびカスタマイズできます。このガイドでは、いくつかの一般的な方法を紹介します。
Univer 関数をレンダーする引数としてコンポーネントを受け入れません。コンポーネントを登録します。 Facade API, と Univer 各取り付けポイントで見つけることができます。
まだFacade APIインスタンスがない場合は、Facade APIを参照してください。
カスタムコンポーネントの登録
univerAPI.registerComponent(componentKey, CustomComponent, options)univerAPI.registerComponentは3つの変数を受け入れます:
componentKey: コンポーネントのユニークな識別子で、Univer内で参照するために使用されます。CustomComponent:Reactコンポーネント、Vue コンポーネント、または Web コンポーネントとしてコンポーネント自体。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(生命の端)であり、もはや維持されないので、UniverはUIのアダプターのプラグインを提供していません。カスタムプラグインとして1つを実装できます。以下の例はVueの2.xのアダプターをビルドします。
import { DependentOn, Inject, Injector, Plugin } from '@univerjs/core'import { ComponentManager, UniverUIPlugin } from '@univerjs/ui'import Vue from 'vue'/** * Univer が Vue 2 コンポーネントを UI コンポーネントとして使用できるようにするプラグイン。 */@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 コンポーネントは Web コンポーネント規格に準拠する必要があります。 @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 コンポーネントを登録するとき、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の独自のUI内のカスタムコンポーネントをマウントする以下の方法。
注意事項
- これらのメソッドのいずれかを呼び出す前に、Univerがレンダリングを完了していることを確認してください。 2。 コンポーネントを登録して使用する前に。 3。
dispose()を呼び出して、追加のコンポーネントをクリーンアップして削除します。 そうでなければ、メモリリークを引き起こします。
カスタムメニューアイテムの追加
トップメニューバー(リブボン)と右クリックメニュー(コンテキストメニュー)は、カスタムコンポーネントの両方が含まれていることができます。 これらのメニュー項目を独自のプラグインから提供し、使用前にコンポーネントを登録し、プラグインと一緒に登録を解除します。
組み込みコンポーネントの置き換え
インフォメーション
組み込みコンポーネントを交換すると、正しく機能を停止する機能が生じる場合があります。 置換するコンポーネントのソースコードとドキュメントを読んで、注意を払ってください。
univerAPI.registerComponent は、デフォルトでオーバーライド (override: true) を登録します。オーバーライドは、プラグインがデフォルトで、または呼び出し後に登録するかどうかに関係なく、組み込みコンポーネントに優先されます。1つのオーバーライドのみが、componentKey ごとに有効になっているため、別の登録前に以前の登録を解除します。返された登録で dispose() を呼び出すと、組み込みのデフォルトが復元されます。1 が登録されている場合は、ビルドインのデフォルトを復元します。{ override: false } を渡すと、代わりにデフォルトを登録します。
対応するビューが初めて開く前にオーバーライドを登録します。登録を変更しても、既にマウントされているビューは再レンダーしません。
例えば、次のコードは組み込みのColorPickerコンポーネントを置き換えます。
// Following code only replaces the UI of the built-in ColorPicker component, not its functionalityuniverAPI.registerComponent('UI_COLOR_PICKER_COMPONENT', () => <input type="color" />)コンテンツコンポーネントとしての使用
サイドバーで
univerAPI.openSidebarはUniverインターフェイスのサイドバーを開き、内部にカスタムコンポーネントをレンダリングします。
// 適切なタイミング(例: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は、カスタムコンポーネントをレンダリングするダイアログを開きます。
// 適切なタイミング(例:Univer が読み込まれた後)でコンポーネントを登録する必要があります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()このガイドはいかがですか?