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.

refactor: remove sheet context completely

Wenzhao Hue95956bf221e
diff --git a/.eslintrc.js b/.eslintrc.js
index 8ca17be573..b8d7052ea2 100644
--- a/.eslintrc.js
+++ b/.eslintrc.js
@@ -41,7 +41,7 @@ module.exports = {
ignore: ['\\.less$', '^@'],
},
],
- 'import/no-cycle': 2,
+ 'import/no-cycle': 'warn',
'no-param-reassign': 'off',
'no-bitwise': 'off',
'default-case': 'off',
@@ -125,4 +125,4 @@ module.exports = {
},
},
},
-};
+};
\ No newline at end of file
diff --git a/examples/univer-sheet-ts/src/main.tsx b/examples/univer-sheet-ts/src/main.tsx
index fc461659e9..86df045a12 100644
--- a/examples/univer-sheet-ts/src/main.tsx
+++ b/examples/univer-sheet-ts/src/main.tsx
@@ -1,57 +1,53 @@
-import { Univer, LocaleType } from '@univerjs/core';
-import { RenderEngine } from '@univerjs/base-render';
-import { SheetPlugin } from '@univerjs/base-sheets';
-import { SheetUIPlugin } from '@univerjs/ui-plugin-sheets';
-import { DEFAULT_WORKBOOK_DATA_DEMO } from '@univerjs/common-plugin-data';
-import { DEFAULT_FORMULA_DATA_DEMO, FormulaPlugin } from '@univerjs/sheets-plugin-formula';
-import { NumfmtPlugin } from '@univerjs/sheets-plugin-numfmt';
-import { FindPlugin } from '@univerjs/sheets-plugin-find';
-
-// TODO: @huwenzhao: change the following to new API
-
-// univer
-const univer = new Univer({
- locale: LocaleType.EN,
-});
-
-// create univer sheet instance
-const universheet = univer.createUniverSheet(DEFAULT_WORKBOOK_DATA_DEMO);
-
-// base-render
-univer.registerPlugin(RenderEngine);
-// register plugins
-univer.registerPlugin(SheetPlugin);
-// ui-plugin-sheets
-univer.registerPlugin(SheetUIPlugin, {
- container: 'universheet',
- layout: {
- sheetContainerConfig: {
- infoBar: true,
- formulaBar: true,
- toolbar: true,
- sheetBar: true,
- countBar: true,
- rightMenu: true,
- },
- },
-});
-univer.registerPlugin(NumfmtPlugin);
-
-univer.registerPlugin(FindPlugin);
-
-// base-sheet
-univer.registerPlugin(FormulaPlugin,DEFAULT_FORMULA_DATA_DEMO)
-// FindPlugin.create().installTo(universheet);
-// universheet.installPlugin(new OperationPlugin());
-// universheet.installPlugin(new ImportXlsxPlugin());
-// universheet.installPlugin(new OverGridImagePlugin());
-// universheet.installPlugin(new NumfmtPlugin());
-
-// use for console test
-declare global {
- interface Window {
- univer?: any;
- }
-}
-
-window.univer = univer;
+import { Univer, LocaleType } from '@univerjs/core';
+import { RenderEngine } from '@univerjs/base-render';
+import { SheetPlugin } from '@univerjs/base-sheets';
+import { SheetUIPlugin } from '@univerjs/ui-plugin-sheets';
+import { DEFAULT_WORKBOOK_DATA_DEMO } from '@univerjs/common-plugin-data';
+import { DEFAULT_FORMULA_DATA_DEMO, FormulaPlugin } from '@univerjs/sheets-plugin-formula';
+import { NumfmtPlugin } from '@univerjs/sheets-plugin-numfmt';
+import { FindPlugin } from '@univerjs/sheets-plugin-find';
+
+// TODO: @huwenzhao: change the following to new API
+
+// univer
+const univer = new Univer({
+ locale: LocaleType.EN,
+});
+
+// create univer sheet instance
+univer.createUniverSheet(DEFAULT_WORKBOOK_DATA_DEMO);
+
+// register plugins
+univer.registerPlugin(RenderEngine);
+univer.registerPlugin(SheetPlugin);
+univer.registerPlugin(SheetUIPlugin, {
+ container: 'universheet',
+ layout: {
+ sheetContainerConfig: {
+ infoBar: true,
+ formulaBar: true,
+ toolbar: true,
+ sheetBar: true,
+ countBar: true,
+ rightMenu: true,
+ },
+ },
+});
+univer.registerPlugin(NumfmtPlugin);
+univer.registerPlugin(FindPlugin);
+univer.registerPlugin(FormulaPlugin, DEFAULT_FORMULA_DATA_DEMO);
+
+// FindPlugin.create().installTo(universheet);
+// universheet.installPlugin(new OperationPlugin());
+// universheet.installPlugin(new ImportXlsxPlugin());
+// universheet.installPlugin(new OverGridImagePlugin());
+// universheet.installPlugin(new NumfmtPlugin());
+
+// use for console test
+declare global {
+ interface Window {
+ univer?: any;
+ }
+}
+
+window.univer = univer;
\ No newline at end of file
diff --git a/packages/base-render/src/RenderEngine.ts b/packages/base-render/src/RenderEngine.ts
index 06a3c6e109..042b61d5ff 100644
--- a/packages/base-render/src/RenderEngine.ts
+++ b/packages/base-render/src/RenderEngine.ts
@@ -9,7 +9,7 @@ export class RenderEngine extends Plugin {
private _engine: Engine;
- constructor(@Optional(Injector) private readonly _injector?: Injector) {
+ constructor(@Optional(Injector) override readonly _injector: Injector) {
super(PLUGIN_NAMES.BASE_RENDER);
this._engine = new Engine();
@@ -17,10 +17,6 @@ export class RenderEngine extends Plugin {
this._injector?.add([IRenderingEngine, { useFactory: () => this._engine }]);
}
- static create() {
- return new RenderEngine();
- }
-
installTo(univerInstance: Univer) {
univerInstance.install(this);
}
@@ -28,4 +24,4 @@ export class RenderEngine extends Plugin {
getEngine(): Engine {
return this._engine;
}
-}
+}
\ No newline at end of file
diff --git a/packages/core/src/Basics/SheetContext.ts b/packages/core/src/Basics/SheetContext.ts
deleted file mode 100644
index 228b4e242b..0000000000
--- a/packages/core/src/Basics/SheetContext.ts
+++ /dev/null
@@ -1,59 +0,0 @@
-import { Inject, Injector } from '@wendellhu/redi';
-
-import { Workbook } from '../Sheets/Domain';
-import { WorkBookObserverImpl } from './WorkBookObserverImpl';
-import { ContextBase } from './ContextBase';
-import { Observable } from '../Observer';
-import { GenName, PropsFrom } from '../Shared';
-import { Univer } from './Univer';
-import { WorkBookObserver } from './WorkBookObserver';
-import { IWorkbookConfig } from '../Types/Interfaces';
-
-/**
- * Core context, mount important instances, managers
- */
-export class SheetContext extends ContextBase {
- protected _workbook: Workbook;
-
- protected _genname: GenName;
-
- constructor(univerSheetData: Partial<IWorkbookConfig> = {}, @Inject(Injector) private readonly _sheetInjector: Injector) {
- super();
- this._workbook = this._sheetInjector.createInstance(Workbook, univerSheetData);
- }
-
- UNSAFE_setGenName(genName: GenName): void {
- this._genname = genName;
- }
-
- getWorkBook(): Workbook {
- return this._workbook;
- }
-
- getGenName(): GenName {
- return this._genname;
- }
-
- override onUniver(univer: Univer) {
- super.onUniver(univer);
-
- this._workbook.onUniver();
- }
-
- getContextObserver<Key extends keyof WorkBookObserver>(value: Key): Observable<PropsFrom<WorkBookObserver[Key]>> {
- return this.getObserverManager().requiredObserver(value, 'core');
- }
-
- TEMP_setObserver(): void {
- this._setObserver();
- }
-
- protected _setObserver(): void {
- const manager = this.getObserverManager();
- new WorkBookObserverImpl().install(manager);
- }
-
- protected override _initialize(): void {
- // EMPTY Context Initialize
- }
-}
\ No newline at end of file
diff --git a/packages/core/src/Basics/Univer.ts b/packages/core/src/Basics/Univer.ts
index f02627f5d5..14669ee252 100644
--- a/packages/core/src/Basics/Univer.ts
+++ b/packages/core/src/Basics/Univer.ts
@@ -1,12 +1,11 @@
import { Injector, Ctor } from '@wendellhu/redi';
-import { CommandManager } from 'src/Command';
+import { CommandManager, UndoManager } from 'src/Command';
import { LocaleService } from 'src/Service/Locale.service';
import { UniverSheet } from './UniverSheet';
import { UniverDoc } from './UniverDoc';
import { UniverSlide } from './UniverSlide';
import { Nullable } from '../Shared';
-import { Context } from './Context';
import { Plugin, PluginCtor, PluginRegistry, PluginStore, PluginType } from '../Plugin';
import { IUniverData, IWorkbookConfig } from '../Types/Interfaces';
import { UniverObserverImpl } from './UniverObserverImpl';
@@ -23,17 +22,9 @@ export class Univer {
private readonly _univerPluginRegistry = new PluginRegistry();
- /**
- * @deprecated
- */
- private _context: Context;
-
constructor(univerData: Partial<IUniverData> = {}) {
- this._context = new Context(univerData);
- this._setObserver();
- this._context.onUniver(this);
-
this._univerInjector = this.initializeDependencies();
+ this._setObserver();
// initialize localization info
const { locale } = univerData;
@@ -126,43 +117,12 @@ export class Univer {
return this._currentUniverService.getCurrentUniverSlideInstance();
}
- /**
- * @deprecated
- */
- getGlobalContext() {
- return this._context;
- }
-
- /**
- * install plugin
- *
- * @param plugin - install plugin
- */
- install(plugin: Plugin): void {
- this._context.getPluginManager().install(plugin);
- }
-
- /**
- * uninstall plugin
- *
- * @param name - plugin name
- */
- uninstall(name: string): void {
- this._context.getPluginManager().uninstall(name);
- }
-
protected _setObserver(): void {
- const manager = this._context.getObserverManager();
- new UniverObserverImpl().install(manager);
+ new UniverObserverImpl().install(this._univerInjector.get(ObserverManager));
}
private initializeDependencies(): Injector {
- return new Injector([
- [ObserverManager, { useFactory: () => this._context.getObserverManager() }],
- [ICurrentUniverService, { useClass: CurrentUniverService }],
- [CommandManager, { useFactory: () => this._context.getCommandManager() }],
- [LocaleService],
- ]);
+ return new Injector([[ObserverManager], [ICurrentUniverService, { useClass: CurrentUniverService }], [CommandManager], [LocaleService], [UndoManager]]);
}
private registerUniverPlugin<T extends Plugin>(plugin: PluginCtor<T>, options?: any): void {
@@ -170,8 +130,7 @@ export class Univer {
const pluginInstance: Plugin = this._univerInjector.createInstance(plugin as unknown as Ctor<any>, options);
// TODO: remove these two lines later
- pluginInstance.onCreate(this._context);
- this._context.getPluginManager().install(pluginInstance);
+ pluginInstance.onCreate();
this._univerPluginStore.addPlugin(pluginInstance);
}
diff --git a/packages/core/src/Basics/UniverDoc.ts b/packages/core/src/Basics/UniverDoc.ts
index fa237c5025..09f5070bc7 100644
--- a/packages/core/src/Basics/UniverDoc.ts
+++ b/packages/core/src/Basics/UniverDoc.ts
@@ -1,9 +1,9 @@
import { IDocumentData } from '../Types/Interfaces';
import { Plugin } from '../Plugin';
import { IOHttp, IOHttpConfig, Logger } from '../Shared';
-import { DocContext } from './DocContext';
import { VersionCode, VersionEnv } from './Version';
+
/**
* Externally provided UniverDoc root instance
*/
@@ -74,4 +74,4 @@ export class UniverDoc {
uninstallPlugin(name: string): void {
this._context.getPluginManager().uninstall(name);
}
-}
+}
\ No newline at end of file
diff --git a/packages/core/src/Basics/UniverSheet.ts b/packages/core/src/Basics/UniverSheet.ts
index 2d07cde251..6943622f90 100644
--- a/packages/core/src/Basics/UniverSheet.ts
+++ b/packages/core/src/Basics/UniverSheet.ts
@@ -57,9 +57,8 @@ export class UniverSheet implements Disposable {
addPlugin<T extends Plugin>(plugin: PluginCtor<T>, options: any): void {
const pluginInstance: Plugin = this._sheetInjector.createInstance(plugin as unknown as Ctor<any>, options);
- // TODO: remove context passed in here
- pluginInstance.onCreate({} as any);
- pluginInstance.onMounted({} as any);
+ pluginInstance.onCreate();
+ pluginInstance.onMounted();
this._pluginStore.addPlugin(pluginInstance);
}
@@ -71,4 +70,4 @@ export class UniverSheet implements Disposable {
private setObserver(): void {
new WorkBookObserverImpl().install(this._sheetInjector.get(ObserverManager));
}
-}
+}
\ No newline at end of file
diff --git a/packages/core/src/Basics/index.ts b/packages/core/src/Basics/index.ts
index fb7c7f9791..b6241209b5 100644
--- a/packages/core/src/Basics/index.ts
+++ b/packages/core/src/Basics/index.ts
@@ -1,10 +1,7 @@
// @index('./*.ts', f => `export * from '${f.path}'`)
-export * from './ContextBase';
-export * from './Context';
export * from './DocContext';
export * from './Environment';
export * from './Registry';
-export * from './SheetContext';
export * from './SlideContext';
export * from './UniverDoc';
export * from './UniverSheet';
@@ -15,4 +12,4 @@ export * from './WorkBookObserver';
export * from './WorkBookObserverImpl';
export * from './UniverObserver';
export * from './UniverObserverImpl';
-// @endindex
+// @endindex
\ No newline at end of file
diff --git a/packages/core/src/Command/CommandManager.ts b/packages/core/src/Command/CommandManager.ts
index 6023484646..0a51e580d9 100644
--- a/packages/core/src/Command/CommandManager.ts
+++ b/packages/core/src/Command/CommandManager.ts
@@ -7,6 +7,7 @@ import { CommandInjectorObservers } from './CommandInjectorObservers';
import { UndoManager } from './UndoManager';
import { ActionExtensionManager } from './ActionExtensionManager';
import { Command } from './Command';
+import { Inject } from '@wendellhu/redi';
/**
* Manage command
@@ -20,12 +21,9 @@ export class CommandManager {
private static _commandInjectorObservers: CommandInjectorObservers;
- private _undoManager: UndoManager;
-
private _actionExtensionManager: ActionExtensionManager;
- constructor(context: ContextBase) {
- this._undoManager = context.getUndoManager();
+ constructor(@Inject(UndoManager) private readonly _undoManager: UndoManager) {
this._actionExtensionManager = new ActionExtensionManager();
}
@@ -89,4 +87,4 @@ export class CommandManager {
}
}
-CommandManager.staticInitialize();
+CommandManager.staticInitialize();
\ No newline at end of file
diff --git a/packages/core/src/Plugin/Plugin.ts b/packages/core/src/Plugin/Plugin.ts
index f57192e1cb..22a8948d63 100644
--- a/packages/core/src/Plugin/Plugin.ts
+++ b/packages/core/src/Plugin/Plugin.ts
@@ -1,10 +1,7 @@
import { Ctor, Injector } from '@wendellhu/redi';
-import { ContextBase } from '../Basics/ContextBase';
-import { Context } from '../Basics/Context';
-import { Observable, Observer, ObserverManager } from '../Observer';
-import { Locale, Nullable, PropsFrom } from '../Shared';
-import { Univer } from '../Basics';
+import { Observable, ObserverManager } from '../Observer';
+import { Nullable } from '../Shared';
export type PluginCtor<T extends Plugin> = Ctor<T> & { type: PluginType };
@@ -20,49 +17,34 @@ export enum PluginType {
* Basics function of plugin
*/
export interface BasePlugin {
- context: ContextBase;
- getContext(): ContextBase;
- getGlobalContext(): Context;
- getLocale(): Locale;
getPluginName(): string;
/**
* Could register dependencies at this lifecycle stage.
*/
- onCreate(context: ContextBase): void;
+ onCreate(): void;
/**
* Could setup initialization works at this lifecycle stage.
*/
- onMounted(context: ContextBase): void;
+ onMounted(): void;
/**
* Could do some initialization works at this lifecycle stage.
*/
onDestroy(): void;
- deleteObserve(...names: string[]): void;
getPluginByName<T extends BasePlugin>(name: string): Nullable<T>;
- /**
- * save data
- */
- save(): object;
- /**
- * load data
- */
- load<T>(data: T): void;
}
/**
* Plug-in base class, all plug-ins must inherit from this base class. Provide basic methods.
*/
-export abstract class Plugin<Obs = any, O extends ContextBase = ContextBase> implements BasePlugin {
+export abstract class Plugin<Obs = any> implements BasePlugin {
static type: PluginType;
_injector: Injector;
- context: O;
-
private _name: string;
private _observeNames: Array<keyof Obs & string>;
@@ -72,24 +54,14 @@ export abstract class Plugin<Obs = any, O extends ContextBase = ContextBase> imp
this._observeNames = [];
}
- onCreate(context: O): void {
- this.context = context;
+ getPluginByName<T extends BasePlugin>(name: string): Nullable<T> {
+ throw new Error('Method not implemented.');
}
- load<T>(data: T): void {}
+ onCreate(): void {}
- save(): object {
- return Object();
- }
+ onMounted(): void {}
- /**
- * @deprecated
- */
- onMounted(context: O): void {}
-
- /**
- * @deprecated
- */
onDestroy(): void {
this.deleteObserve(...this._observeNames);
}
@@ -98,32 +70,8 @@ export abstract class Plugin<Obs = any, O extends ContextBase = ContextBase> imp
return this._name;
}
+ // TODO@huwenzhao: remove this in the future
/** @deprecated */
- getContext(): O {
- return this.context;
- }
-
- getGlobalContext(): Context {
- return this.context.getUniver().getGlobalContext();
- }
-
- getLocale(): Locale {
- return this.getGlobalContext().getLocale();
- }
-
- getUniver(): Univer {
- return this.context.getUniver();
- }
-
- getObserver<K extends keyof Obs & string>(name: K): Nullable<Observable<PropsFrom<Obs[K]>>> {
- const manager = this.context.getObserverManager();
- return manager.getObserver(name, this._name);
- }
-
- getPluginByName<T extends BasePlugin>(name: string): Nullable<T> {
- return this.context.getPluginManager().getPluginByName<T>(name);
- }
-
pushToObserve<K extends keyof Obs & string>(...names: K[]): void {
const manager = (this._injector as Injector).get(ObserverManager);
names.forEach((name) => {
@@ -134,12 +82,8 @@ export abstract class Plugin<Obs = any, O extends ContextBase = ContextBase> imp
});
}
- deleteObserve<K extends keyof Obs & string>(...names: K[]): void {
- const manager = this.context.getObserverManager();
- names.forEach((name) => {
- manager.removeObserver(name, this._name);
- });
- }
+ /** @deprecated */
+ deleteObserve<K extends keyof Obs & string>(...names: K[]): void {}
}
interface IPluginRegistryItem {
diff --git a/packages/ui-plugin-slides/src/Controller/AppUIController.ts b/packages/ui-plugin-slides/src/Controller/AppUIController.ts
index 33204ecebc..375888ec89 100644
--- a/packages/ui-plugin-slides/src/Controller/AppUIController.ts
+++ b/packages/ui-plugin-slides/src/Controller/AppUIController.ts
@@ -45,4 +45,4 @@ export class AppUIController {
getSlideContainerController() {
return this._slideContainerController;
}
-}
+}
\ No newline at end of file