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.

add: add univer init

jerryfebf5dc17e4a
diff --git a/packages/core/src/Basics/DocContext.ts b/packages/core/src/Basics/DocContext.ts
index e14858eb88..bce1fb1a1e 100644
--- a/packages/core/src/Basics/DocContext.ts
+++ b/packages/core/src/Basics/DocContext.ts
@@ -1,4 +1,4 @@
-import { IDocumentData } from '../Interfaces/IDocumentData';
+import { IDocumentData } from '../Interfaces';
import { ContextBase } from './ContextBase';
import { Document } from '../Docs/Domain/Document';
@@ -15,4 +15,8 @@ export class DocContext extends ContextBase {
}
protected _setObserver(): void {}
+
+ getDocument(): Document {
+ return this._document;
+ }
}
diff --git a/packages/core/src/Basics/Univer.ts b/packages/core/src/Basics/Univer.ts
new file mode 100644
index 0000000000..202652a9ff
--- /dev/null
+++ b/packages/core/src/Basics/Univer.ts
@@ -0,0 +1,42 @@
+import { UniverSheet } from './UniverSheet';
+import { UniverDoc } from './UniverDoc';
+import { UniverSlide } from './UniverSlide';
+import { Nullable } from '../Shared';
+
+export class Univer {
+ private _univerSheets: UniverSheet[];
+
+ private _univerDocs: UniverDoc[];
+
+ private _univerSlides: UniverSlide[];
+
+ constructor() {
+ this._univerSlides = [];
+ this._univerDocs = [];
+ this._univerSlides = [];
+ }
+
+ addUniverSheet(univerSheet: UniverSheet): void {
+ this._univerSheets.push(univerSheet);
+ }
+
+ addUniverDoc(univerDoc: UniverDoc): void {
+ this._univerDocs.push(univerDoc);
+ }
+
+ addUniverSlide(univerSlide: UniverSlide): void {
+ this._univerSlides.push(univerSlide);
+ }
+
+ getUniverSheetInstance(id: string): Nullable<UniverSheet> {
+ return this._univerSheets.find((sheet) => sheet.getUnitId() === id);
+ }
+
+ getUniverDocInstance(id: string): Nullable<UniverDoc> {
+ return this._univerDocs.find((doc) => doc.getUnitId() === id);
+ }
+
+ getUniverSlideInstance(id: string): Nullable<UniverSheet> {
+ return null;
+ }
+}
diff --git a/packages/core/src/Basics/UniverDoc.ts b/packages/core/src/Basics/UniverDoc.ts
index a7bf490018..770ced77f7 100644
--- a/packages/core/src/Basics/UniverDoc.ts
+++ b/packages/core/src/Basics/UniverDoc.ts
@@ -50,6 +50,13 @@ export class UniverDoc {
return this._context;
}
+ /**
+ * get unit id
+ */
+ getUnitId(): string {
+ return this._context.getDocument().getUnitId();
+ }
+
/**
* install plugin
*
diff --git a/packages/core/src/Basics/UniverSheet.ts b/packages/core/src/Basics/UniverSheet.ts
index 9a274e3330..ac03ab5be2 100644
--- a/packages/core/src/Basics/UniverSheet.ts
+++ b/packages/core/src/Basics/UniverSheet.ts
@@ -1,10 +1,9 @@
-import { Workbook } from '../Sheets/Domain';
+import { Workbook, ColorBuilder } from '../Sheets/Domain';
import { IWorkbookConfig } from '../Interfaces';
import { BasePlugin, Plugin } from '../Plugin';
import { IOHttp, IOHttpConfig, Logger } from '../Shared';
import { SheetContext } from './SheetContext';
import { VersionCode, VersionEnv } from './Version';
-import { ColorBuilder } from '../Sheets/Domain/ColorBuilder';
/**
* Externally provided UniverSheet root instance
@@ -78,6 +77,13 @@ export class UniverSheet {
return this._context;
}
+ /**
+ * get unit id
+ */
+ getUnitId(): string {
+ return this.getWorkBook().getUnitId();
+ }
+
/**
* install plugin
*
diff --git a/packages/core/src/Docs/Domain/Document.ts b/packages/core/src/Docs/Domain/Document.ts
index e66e5aeff1..514852b574 100644
--- a/packages/core/src/Docs/Domain/Document.ts
+++ b/packages/core/src/Docs/Domain/Document.ts
@@ -1,3 +1,4 @@
+import { nanoid } from 'nanoid';
import { DocContext } from '../../Basics';
import { Command } from '../../Command';
import { IDocumentData } from '../../Interfaces';
@@ -8,9 +9,16 @@ export class Document {
private _context: DocContext;
+ private _unitId: string;
+
constructor(config: Partial<IDocumentData>, context: DocContext) {
this._context = context;
this._config = { ...DEFAULT_DOC, ...config };
+ this._unitId = this._config.documentId ?? nanoid(6);
+ }
+
+ getUnitId(): string {
+ return this._unitId;
}
insertText(text: string): Document {