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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
chore(demo): collaboration demo
Dushusir5f7b98db9e2f
diff --git a/examples/univer-doc-ts/src/main.tsx b/examples/univer-doc-ts/src/main.tsxindex e70ff1b9d9..fa3aef187c 100644--- a/examples/univer-doc-ts/src/main.tsx+++ b/examples/univer-doc-ts/src/main.tsx@@ -3,12 +3,12 @@ import { RenderEngine } from '@univer/base-render';import { UniverComponentSheet } from '@univer/style-universheet';import { DocPlugin } from '@univer/base-docs';import { BaseComponentPlugin } from '@univer/base-component';-import { DEFAULT_DOCUMENT_DATA } from '@univer/common-plugin-data';+import { DEFAULT_DOCUMENT_DATA_EN } from '@univer/common-plugin-data';const docPluginConfig = {container: 'univerdoc',};-const univerDoc = UniverDoc.newInstance(DEFAULT_DOCUMENT_DATA);+const univerDoc = UniverDoc.newInstance(DEFAULT_DOCUMENT_DATA_EN);univerDoc.installPlugin(new RenderEngine());univerDoc.installPlugin(new UniverComponentSheet());diff --git a/examples/univer-preact-ts/src/main.tsx b/examples/univer-preact-ts/src/main.tsxindex c09403e38d..16e38bf344 100644--- a/examples/univer-preact-ts/src/main.tsx+++ b/examples/univer-preact-ts/src/main.tsx@@ -1,4 +1,4 @@-import { DEFAULT_WORKBOOK_DATA, DEFAULT_WORKBOOK_DATA_DOWN } from '@univer/common-plugin-data';+import { DEFAULT_WORKBOOK_DATA, DEFAULT_WORKBOOK_DATA_DEMO3, DEFAULT_WORKBOOK_DATA_DOWN } from '@univer/common-plugin-data';import { IWorkbookConfig, IWorksheetConfig, SheetTypes, Tools } from '@univer/core';import { univerSheetCustom } from '.';@@ -36,33 +36,34 @@ let columnCount = 8;if (window.innerWidth < 1366) {columnCount = 5;}-const defaultWorkbookData = Tools.deepClone(DEFAULT_WORKBOOK_DATA);-defaultWorkbookData.id = 'workbook-01';-defaultWorkbookData.styles = null;-defaultWorkbookData.namedRanges = null;-defaultWorkbookData.sheets = {- 'sheet-01': {- type: 0,- id: 'sheet-01',- name: 'sheet1',- columnCount,- status: 1,- cellData: {- '0': {- '0': {- m: '',- v: '',- },- },- },- },-};+const defaultWorkbookData = Tools.deepClone(DEFAULT_WORKBOOK_DATA_DEMO3);+// defaultWorkbookData.id = 'workbook-01';+// defaultWorkbookData.styles = null;+// defaultWorkbookData.namedRanges = null;+// defaultWorkbookData.sheets = {+// 'sheet-01': {+// type: 0,+// id: 'sheet-01',+// name: 'sheet1',+// columnCount,+// status: 1,+// cellData: {+// '0': {+// '0': {+// m: '',+// v: '',+// },+// },+// },+// },+// };univerSheetCustom({coreConfig: defaultWorkbookData,baseSheetsConfig: sheetConfig,collaborationConfig: {- url: 'ws://luckysheet.lashuju.com/ws',+ url: 'ws://localhost:8448/ws',+ // url: 'ws://luckysheet.lashuju.com/ws',},});diff --git a/examples/univer-preact-ts/vite.config.ts b/examples/univer-preact-ts/vite.config.tsindex 164a5360bf..f1ca7078dd 100644--- a/examples/univer-preact-ts/vite.config.ts+++ b/examples/univer-preact-ts/vite.config.ts@@ -16,6 +16,7 @@ export default defineConfig({fileName: 'univer-preact-ts',},outDir: './lib',+ sourcemap: true,},define: {pkgJson: { name, version },diff --git a/examples/univer-sheet-ts/src/main.tsx b/examples/univer-sheet-ts/src/main.tsxindex c220cdb155..f8018eb5e1 100644--- a/examples/univer-sheet-ts/src/main.tsx+++ b/examples/univer-sheet-ts/src/main.tsx@@ -46,7 +46,7 @@ const uiDefaultConfigDemo = {// const univerSheetDemo = UniverSheet.newInstance(DEFAULT_WORKBOOK_DATA_DEMO1);// const univerSheetDemo = UniverSheet.newInstance(DEFAULT_WORKBOOK_DATA_DEMO2);// const univerSheetDemo = UniverSheet.newInstance(DEFAULT_WORKBOOK_DATA_DEMO3);-const univerSheetDemo = UniverSheet.newInstance(DEFAULT_WORKBOOK_DATA_DEMO);+const univerSheetDemo = UniverSheet.newInstance(DEFAULT_WORKBOOK_DATA_DEMO3);univerSheetDemo.installPlugin(new RenderEngine());univerSheetDemo.installPlugin(new UniverComponentSheet());univerSheetDemo.installPlugin(new SheetPlugin(uiDefaultConfigDemo));diff --git a/packages/common-plugin-collaboration/src/CollaborationPlugin.tsx b/packages/common-plugin-collaboration/src/CollaborationPlugin.tsxindex 20e11efe12..29e0683583 100644--- a/packages/common-plugin-collaboration/src/CollaborationPlugin.tsx+++ b/packages/common-plugin-collaboration/src/CollaborationPlugin.tsx@@ -48,4 +48,8 @@ export class CollaborationPlugin extends Plugin {getConfig() {return this.config;}++ getCollaborationController() {+ return this._collaborationController;+ }}diff --git a/packages/common-plugin-collaboration/src/Controller/CollaborationController.ts b/packages/common-plugin-collaboration/src/Controller/CollaborationController.tsindex c96912aebb..371bc98836 100644--- a/packages/common-plugin-collaboration/src/Controller/CollaborationController.ts+++ b/packages/common-plugin-collaboration/src/Controller/CollaborationController.ts@@ -10,6 +10,8 @@ export class CollaborationController {socket: IOSocket;+ previousMessage: string;+constructor(plugin: CollaborationPlugin) {this._plugin = plugin;this._initialize();@@ -85,8 +87,10 @@ export class CollaborationController {type: 'workbookData',config,};- console.log('config===', message);- this.socket.send(JSON.stringify(message));++ const stringMessage = JSON.stringify(message);+ this.previousMessage = stringMessage;+ this.socket.send(stringMessage);}, 100);} catch (error) {console.info(error);@@ -98,14 +102,17 @@ export class CollaborationController {onMessage(data) {const json = JSON.parse(data);const content = json.content;- if (content === HEART_BEAT_MESSAGE) {+ if (content === HEART_BEAT_MESSAGE || this.previousMessage === content) {return;}+ this.previousMessage = content;+try {const message = JSON.parse(content);if (message && message.type === 'workbookData') {+ console.log('refresh');this.refresh(message.config);}} catch (error) {@@ -128,6 +135,10 @@ export class CollaborationController {console.log('onDestroy====', data);}+ close() {+ this.socket.close();+ }+refresh(config) {// console.log('onMessage===config====', config);const context = this._plugin.getContext() as SheetContext;diff --git a/packages/core/src/Shared/IOSocket.ts b/packages/core/src/Shared/IOSocket.tsindex 1052aa3966..e3dbe5e623 100644--- a/packages/core/src/Shared/IOSocket.ts+++ b/packages/core/src/Shared/IOSocket.ts@@ -215,4 +215,8 @@ export class IOSocket {on(type: IOSocketListenType, listen: Function): void {this._listens.set(type, listen);}++ close(): void {+ this._socket && this._socket.close();+ }}