Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

feat(examples): add webcomponent demo (#5201)

白熱095f5132ec46
diff --git a/examples/esbuild.config.ts b/examples/esbuild.config.ts
index 2e1709e7fb..2186802d6f 100644
--- a/examples/esbuild.config.ts
+++ b/examples/esbuild.config.ts
@@ -214,6 +214,9 @@ const entryPoints = [
// sheets-uniscript
'./src/sheets-uniscript/main.ts',
+ // sheets-webcomponent
+ './src/sheets-webcomponent/main.tsx',
+
// docs
'./src/docs/main.ts',
diff --git a/examples/package.json b/examples/package.json
index 57020ec08f..b842ac971a 100644
--- a/examples/package.json
+++ b/examples/package.json
@@ -17,6 +17,7 @@
"lint:types": "tsc --noEmit"
},
"dependencies": {
+ "@lit/react": "^1.0.7",
"@univerjs/action-recorder": "workspace:*",
"@univerjs/core": "workspace:*",
"@univerjs/data-validation": "workspace:*",
@@ -81,6 +82,7 @@
"@univerjs/uniscript": "workspace:*",
"@univerjs/uniui": "workspace:*",
"@univerjs/watermark": "workspace:*",
+ "lit": "^3.3.0",
"monaco-editor": "0.52.2",
"react": "^19.1.0",
"react-dom": "^19.1.0",
diff --git a/examples/src/main.tsx b/examples/src/main.tsx
index 9cb52ef56f..9a51f9ef65 100644
--- a/examples/src/main.tsx
+++ b/examples/src/main.tsx
@@ -52,6 +52,9 @@ function Examples() {
}, {
title: '🗂️ Sheets Multi Instance',
href: './sheets-multi/',
+ }, {
+ title: '🗂️ Sheets With Webcomponent',
+ href: './sheets-webcomponent/',
}, {
title: '🏡 Sheets Multi Units',
href: './sheets-multi-units/',
diff --git a/examples/src/sheets-webcomponent/main.tsx b/examples/src/sheets-webcomponent/main.tsx
new file mode 100644
index 0000000000..3c73aba55e
--- /dev/null
+++ b/examples/src/sheets-webcomponent/main.tsx
@@ -0,0 +1,94 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { createComponent } from '@lit/react';
+import { LocaleType, LogLevel, Univer, UniverInstanceType } from '@univerjs/core';
+import { render } from '@univerjs/design';
+import { UniverDocsPlugin } from '@univerjs/docs';
+import { UniverDocsUIPlugin } from '@univerjs/docs-ui';
+import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula';
+import { UniverRenderEnginePlugin } from '@univerjs/engine-render';
+import { DEFAULT_WORKBOOK_DATA_DEMO } from '@univerjs/mockdata';
+import { UniverSheetsPlugin } from '@univerjs/sheets';
+import { UniverSheetsFormulaPlugin } from '@univerjs/sheets-formula';
+import { UniverSheetsFormulaUIPlugin } from '@univerjs/sheets-formula-ui';
+import { UniverSheetsNumfmtPlugin } from '@univerjs/sheets-numfmt';
+import { UniverSheetsNumfmtUIPlugin } from '@univerjs/sheets-numfmt-ui';
+import { UniverSheetsUIPlugin } from '@univerjs/sheets-ui';
+import { UniverUIPlugin } from '@univerjs/ui';
+import { html, LitElement } from 'lit';
+import { customElement } from 'lit/decorators.js';
+import React from 'react';
+import { enUS, faIR, frFR, ruRU, zhCN } from '../locales';
+import '../global.css';
+
+// 创建 Lit Element
+@customElement('my-univer')
+class MyWebComponent extends LitElement {
+ override firstUpdated() {
+ const container = this.renderRoot.querySelector('#containerId') as HTMLDivElement;
+
+ const univer = new Univer({
+ locale: LocaleType.ZH_CN,
+ locales: {
+ [LocaleType.ZH_CN]: zhCN,
+ [LocaleType.EN_US]: enUS,
+ [LocaleType.FR_FR]: frFR,
+ [LocaleType.RU_RU]: ruRU,
+ [LocaleType.FA_IR]: faIR,
+ },
+ logLevel: LogLevel.VERBOSE,
+ });
+
+ univer.registerPlugin(UniverFormulaEnginePlugin);
+ univer.registerPlugin(UniverRenderEnginePlugin);
+ univer.registerPlugin(UniverUIPlugin, {
+ container,
+ });
+ univer.registerPlugin(UniverDocsPlugin);
+ univer.registerPlugin(UniverDocsUIPlugin);
+
+ // sheets plugin
+ univer.registerPlugin(UniverSheetsPlugin);
+ univer.registerPlugin(UniverSheetsUIPlugin);
+ univer.registerPlugin(UniverSheetsFormulaUIPlugin);
+ // sheet feature plugins
+ univer.registerPlugin(UniverSheetsNumfmtPlugin);
+ univer.registerPlugin(UniverSheetsNumfmtUIPlugin);
+ univer.registerPlugin(UniverSheetsFormulaPlugin);
+
+ univer.createUnit(UniverInstanceType.UNIVER_SHEET, DEFAULT_WORKBOOK_DATA_DEMO);
+ }
+
+ override render() {
+ return html`
+ <link rel="stylesheet" href="./main.css">
+ <div style="height: 100%;" id="containerId" />
+ `;
+ }
+}
+
+// 将 Web Component 包装为 React 组件
+const App = createComponent({
+ tagName: 'my-univer',
+ elementClass: MyWebComponent,
+ react: React,
+ events: {
+ onMyEvent: 'my-event', // 事件映射
+ },
+});
+
+render(<App />, document.getElementById('app')!);
diff --git a/examples/src/sheets-webcomponent/univer.tsx b/examples/src/sheets-webcomponent/univer.tsx
new file mode 100644
index 0000000000..1b6d4bd64e
--- /dev/null
+++ b/examples/src/sheets-webcomponent/univer.tsx
@@ -0,0 +1,74 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { LocaleType, LogLevel, Univer, UniverInstanceType } from '@univerjs/core';
+import { UniverDocsPlugin } from '@univerjs/docs';
+import { UniverDocsUIPlugin } from '@univerjs/docs-ui';
+import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula';
+import { UniverRenderEnginePlugin } from '@univerjs/engine-render';
+import { DEFAULT_WORKBOOK_DATA_DEMO } from '@univerjs/mockdata';
+import { UniverSheetsPlugin } from '@univerjs/sheets';
+import { UniverSheetsFormulaPlugin } from '@univerjs/sheets-formula';
+import { UniverSheetsFormulaUIPlugin } from '@univerjs/sheets-formula-ui';
+import { UniverSheetsNumfmtPlugin } from '@univerjs/sheets-numfmt';
+import { UniverSheetsNumfmtUIPlugin } from '@univerjs/sheets-numfmt-ui';
+import { UniverSheetsUIPlugin } from '@univerjs/sheets-ui';
+import { UniverUIPlugin } from '@univerjs/ui';
+import { useEffect, useRef } from 'react';
+import { enUS, faIR, frFR, ruRU, zhCN } from '../locales';
+
+import '../global.css';
+
+export function UniverComponent() {
+ const containerRef = useRef<HTMLDivElement>(null!);
+
+ useEffect(() => {
+ const univer = new Univer({
+ locale: LocaleType.ZH_CN,
+ locales: {
+ [LocaleType.ZH_CN]: zhCN,
+ [LocaleType.EN_US]: enUS,
+ [LocaleType.FR_FR]: frFR,
+ [LocaleType.RU_RU]: ruRU,
+ [LocaleType.FA_IR]: faIR,
+ },
+ logLevel: LogLevel.VERBOSE,
+ });
+
+ univer.registerPlugin(UniverFormulaEnginePlugin);
+ univer.registerPlugin(UniverRenderEnginePlugin);
+ univer.registerPlugin(UniverUIPlugin, {
+ container: containerRef.current,
+ });
+ univer.registerPlugin(UniverDocsPlugin);
+ univer.registerPlugin(UniverDocsUIPlugin);
+
+ // sheets plugin
+ univer.registerPlugin(UniverSheetsPlugin);
+ univer.registerPlugin(UniverSheetsUIPlugin);
+ univer.registerPlugin(UniverSheetsFormulaUIPlugin);
+ // sheet feature plugins
+ univer.registerPlugin(UniverSheetsNumfmtPlugin);
+ univer.registerPlugin(UniverSheetsNumfmtUIPlugin);
+ univer.registerPlugin(UniverSheetsFormulaPlugin);
+
+ univer.createUnit(UniverInstanceType.UNIVER_SHEET, DEFAULT_WORKBOOK_DATA_DEMO);
+ }, []);
+
+ return (
+ <div ref={containerRef} className="univer-h-full" />
+ );
+};
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 535cb15571..be6bd6f18b 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -283,6 +283,9 @@ importers:
examples:
dependencies:
+ '@lit/react':
+ specifier: ^1.0.7
+ version: 1.0.7(@types/[email protected])
'@univerjs/action-recorder':
specifier: workspace:*
version: link:../packages/action-recorder
@@ -475,6 +478,9 @@ importers:
'@univerjs/watermark':
specifier: workspace:*
version: link:../packages/watermark
+ lit:
+ specifier: ^3.3.0
+ version: 3.3.0
monaco-editor:
specifier: 0.52.2
version: 0.52.2
@@ -4362,6 +4368,17 @@ packages:
'@js-sdsl/[email protected]':
resolution: {integrity: sha512-iUKgm52T8HOE/makSxjqoWhe95ZJA1/G1sYsGev2JDKUSS14KAgg1LHb+Ba+IPow0xflbnSkOsZcO08C7w1gYw==}
+ '@lit-labs/[email protected]':
+ resolution: {integrity: sha512-nQIWonJ6eFAvUUrSlwyHDm/aE8PBDu5kRpL0vHMg6K8fK3Diq1xdPjTnsJSwxABhaZ+5eBi1btQB5ShUTKo4nQ==}
+
+ resolution: {integrity: sha512-cencnwwLXQKiKxjfFzSgZRngcWJzUDZi/04E0fSaF86wZgchMdvTyu+lE36DrUfvuus3bH8+xLPrhM1cTjwpzw==}
+ peerDependencies:
+ '@types/react': ^19.1.3
+
+ resolution: {integrity: sha512-L2qyoZSQClcBmq0qajBVbhYEcG6iK0XfLn66ifLe/RfC0/ihpc+pl0Wdn8bJ8o+hj38cG0fGXRgSS20MuXn7qA==}
+
'@mdx-js/[email protected]':
resolution: {integrity: sha512-QjHtSaoameoalGnKDT3FoIl4+9RwyTmo9ZJGBdLOks/YOiWHoRDI3PUwEzOE7kEmGcV3AFcp9K6dYu9rEuKLAQ==}
peerDependencies:
@@ -5564,6 +5581,9 @@ packages:
resolution: {integrity: sha512-72dv/zdhuyXWVHUXG2VTPEQdOG+oen95/DNFx2aMFFaY6LoITI6PwEqf5x31JF49kp2w9hvUzkNfTGBIeg61LQ==}
+ '@types/[email protected]':
+ resolution: {integrity: sha512-ScaPdn1dQczgbl0QFTeTOmVHFULt394XJgOQNoyVhZ6r2vLnMLJfBPd53SB52T/3G36VI1/g2MZaX0cwDuXsfw==}
+
resolution: {integrity: sha512-ko/gIFJRv177XgZsZcBwnqJN5x/Gien8qNOn0D5bQU/zAzVf9Zt3BlcUiLqhV9y4ARk0GbT3tnUiPNgnTXzc/Q==}
@@ -8593,6 +8613,15 @@ packages:
resolution: {integrity: sha512-LWzX2KsqcB1wqQ4AHgYb4RsDXauQiqhjLk+6hjbaeHG4zpjjVAB6wC/gz6X0l+Du1cN3pUB5ZlrvTbhGSNnUQQ==}
engines: {node: '>=18.0.0'}
+ resolution: {integrity: sha512-MGrXJVAI5x+Bfth/pU9Kst1iWID6GHDLEzFEnyULB/sFiRLgkd8NPK/PeeXxktA3T6EIIaq8U3KcbTU5XFcP2Q==}
+
+ resolution: {integrity: sha512-RHoswrFAxY2d8Cf2mm4OZ1DgzCoBKUKSPvA1fhtSELxUERq2aQQ2h05pO9j81gS1o7RIRJ+CePLogfyahwmynw==}
+
+ resolution: {integrity: sha512-DGVsqsOIHBww2DqnuZzW7QsuCdahp50ojuDaBPC7jUDRpYoH0z7kHBBYZewRzer75FwtrkmkKk7iOAwSaWdBmw==}
+
resolution: {integrity: sha512-3R/1M+yS3j5ou80Me59j7F9IMs4PXs3VqRrm0TU3AbKPxlmpoY1TNscJV/oGJXo8qCatFGTfDbY6W6ipGOYXfg==}
engines: {node: '>=6.11.5'}
@@ -12252,6 +12281,16 @@ snapshots:
'@js-sdsl/[email protected]': {}
+ '@lit-labs/[email protected]': {}
+
+ dependencies:
+ '@types/react': 19.1.3
+
+ dependencies:
+ '@lit-labs/ssr-dom-shim': 1.3.0
+
dependencies:
'@types/mdx': 2.0.13
@@ -13537,6 +13576,8 @@ snapshots:
dependencies:
'@types/node': 22.15.17
+ '@types/[email protected]': {}
+
'@types/[email protected]': {}
'@types/[email protected]': {}
@@ -16938,6 +16979,22 @@ snapshots:
rfdc: 1.4.1
wrap-ansi: 9.0.0
+ dependencies:
+ '@lit-labs/ssr-dom-shim': 1.3.0
+ '@lit/reactive-element': 2.1.0
+ lit-html: 3.3.0
+
+ dependencies:
+ '@types/trusted-types': 2.0.7
+
+ dependencies:
+ '@lit/reactive-element': 2.1.0
+ lit-element: 4.2.0
+ lit-html: 3.3.0
+