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
refactor: add DI system into core
Wenzhao Hu57fd0faed13e
diff --git a/examples/univer-sheet-ts/src/main.tsx b/examples/univer-sheet-ts/src/main.tsxindex 86735865e1..9cf37d744b 100644--- a/examples/univer-sheet-ts/src/main.tsx+++ b/examples/univer-sheet-ts/src/main.tsx@@ -1,4 +1,4 @@-import { UniverSheet, Univer, LocaleType } from '@univerjs/core';+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';@@ -18,9 +18,7 @@ const univer = new Univer({});// base-render-univer.install(new RenderEngine());--+univer.registerPlugin(RenderEngine);// register pluginsuniver.registerPlugin(SheetPlugin);@@ -30,21 +28,19 @@ univer.addUniverSheet(universheet);// base-sheet// ui-plugin-sheets-univer.install(- new SheetUIPlugin({- container: 'universheet',- layout: {- sheetContainerConfig: {- infoBar: true,- formulaBar: true,- toolbar: true,- sheetBar: true,- countBar: true,- rightMenu: true,- },+univer.registerPlugin(SheetUIPlugin, {+ container: 'universheet',+ layout: {+ sheetContainerConfig: {+ infoBar: true,+ formulaBar: true,+ toolbar: true,+ sheetBar: true,+ countBar: true,+ rightMenu: true,},- })-);+ },+});FormulaPlugin.create(DEFAULT_FORMULA_DATA_DEMO).installTo(universheet);FindPlugin.create().installTo(universheet);@@ -59,4 +55,4 @@ declare global {univer?: any;}}-window.univer = univer;\ No newline at end of file+window.univer = univer;diff --git a/packages/base-render/package.json b/packages/base-render/package.jsonindex 7db00e104a..3fc3b0596e 100644--- a/packages/base-render/package.json+++ b/packages/base-render/package.json@@ -62,6 +62,7 @@"vite-plugin-external": "1.2.3"},"dependencies": {- "@univerjs/core": "workspace:^0.0.1"+ "@univerjs/core": "workspace:^0.0.1",+ "@wendellhu/redi": "^0.8.0"}-}+}\ No newline at end of filediff --git a/packages/base-render/src/RenderEngine.ts b/packages/base-render/src/RenderEngine.tsindex 331790fdb3..80d5a1874f 100644--- a/packages/base-render/src/RenderEngine.ts+++ b/packages/base-render/src/RenderEngine.ts@@ -1,12 +1,20 @@-import { Plugin, PLUGIN_NAMES, Univer } from '@univerjs/core';+import { Injector, Optional, createIdentifier } from '@wendellhu/redi';+import { Plugin, PluginType, PLUGIN_NAMES, Univer } from '@univerjs/core';import { Engine } from './Engine';+export const IRenderingEngine = createIdentifier<Engine>('univer.render-engine');+export class RenderEngine extends Plugin {+ static override type = PluginType.Univer;+private _engine: Engine;- constructor() {+ constructor(@Optional(Injector) private readonly _injector?: Injector) {super(PLUGIN_NAMES.BASE_RENDER);+this._engine = new Engine();++ this._injector?.add(IRenderingEngine, { useFactory: () => this._engine });}static create() {diff --git a/packages/base-sheets/src/Controller/EditTooltipsController.ts b/packages/base-sheets/src/Controller/EditTooltipsController.tsindex 5fcda13f3d..17ef8812f8 100644--- a/packages/base-sheets/src/Controller/EditTooltipsController.ts+++ b/packages/base-sheets/src/Controller/EditTooltipsController.ts@@ -1,8 +1,8 @@import { CommandManager, SetWorkSheetActivateAction, SheetContext } from '@univerjs/core';-import { Engine, Layer } from '@univerjs/base-render';+import { Engine, IRenderingEngine, Layer } from '@univerjs/base-render';import { EditTooltips, EditTooltipsProps } from '../View/Views';-import { IRenderingEngine, ISheetContext } from '../Services/tokens';+import { ISheetContext } from '../Services/tokens';import { CANVAS_VIEW_KEY } from '../View';export class EditTooltipsController {diff --git a/packages/base-sheets/src/Services/tokens.ts b/packages/base-sheets/src/Services/tokens.tsindex cce6083d4e..d557fccbd8 100644--- a/packages/base-sheets/src/Services/tokens.ts+++ b/packages/base-sheets/src/Services/tokens.ts@@ -1,8 +1,6 @@import { createIdentifier } from '@wendellhu/redi';import { Context, SheetContext } from '@univerjs/core';-import { Engine } from '@univerjs/base-render';-import { SheetPlugin } from '../SheetPlugin';import { SelectionManager } from '../Controller';import { CanvasView } from '../View';@@ -11,12 +9,10 @@ export const IGlobalContext = createIdentifier<Context>('univer.global.context')export const ISheetContext = createIdentifier<SheetContext>('univer.sheet.context');-export const IRenderingEngine = createIdentifier<Engine>('univer.render-engine');-/** @deprecated temp solution */export const ISheetPlugin = createIdentifier<SheetPlugin>('deprecated.univer.sheet.plugin');/** @deprecated temporary solution, we should inject SelectionManager directly */export const ISelectionManager = createIdentifier<SelectionManager>('deprecated.univer.sheet.selection-manager');-export const ICanvasView = createIdentifier<CanvasView>('univer.sheet.canvas-view');\ No newline at end of file+export const ICanvasView = createIdentifier<CanvasView>('univer.sheet.canvas-view');diff --git a/packages/base-sheets/src/SheetPlugin.ts b/packages/base-sheets/src/SheetPlugin.tsindex c519c98efd..9fc14cf2e4 100644--- a/packages/base-sheets/src/SheetPlugin.ts+++ b/packages/base-sheets/src/SheetPlugin.ts@@ -1,6 +1,6 @@-import { Engine, RenderEngine } from '@univerjs/base-render';+import { Engine, IRenderingEngine } from '@univerjs/base-render';import { SheetContext, Plugin, PLUGIN_NAMES, DEFAULT_SELECTION, UniverSheet, UIObserver, PluginType } from '@univerjs/core';-import { Dependency, Inject, Injector, Optional } from '@wendellhu/redi';+import { Dependency, Inject, Injector } from '@wendellhu/redi';import { SheetPluginObserve, uninstall } from './Basics/Observer';import { CANVAS_VIEW_KEY } from './View/BaseView';@@ -20,7 +20,7 @@ import { DEFAULT_SPREADSHEET_PLUGIN_DATA, install, ISheetPluginConfig } from './import { FormulaBarController } from './Controller/FormulaBarController';import { NamedRangeActionExtensionFactory } from './Basics/Register/NamedRangeActionExtension';import { en, zh } from './Locale';-import { IGlobalContext, IRenderingEngine, ISelectionManager, ISheetContext, ISheetPlugin } from './Services/tokens';+import { IGlobalContext, ISelectionManager, ISheetContext, ISheetPlugin } from './Services/tokens';import { DragLineController } from './Controller/Selection/DragLineController';import { ColumnTitleController } from './Controller/Selection/ColumnTitleController';import { RowTitleController } from './Controller/Selection/RowTitleController';@@ -43,7 +43,6 @@ export class SheetPlugin extends Plugin<SheetPluginObserve, SheetContext> {private _toolbarController: ToolbarController;-private _editTooltipsController: EditTooltipsController;private _formulaBarController: FormulaBarController;@@ -127,8 +126,7 @@ export class SheetPlugin extends Plugin<SheetPluginObserve, SheetContext> {}initCanvasView() {- const engine = this.getGlobalContext().getPluginManager().getRequirePluginByName<RenderEngine>(PLUGIN_NAMES.BASE_RENDER).getEngine();- this._canvasEngine = engine;+ this._canvasEngine = this.sheetInjector.get(IRenderingEngine);}override onMounted(ctx: SheetContext): void {@@ -272,10 +270,9 @@ export class SheetPlugin extends Plugin<SheetPluginObserve, SheetContext> {private initializeDependencies(sheetInjector: Injector) {const self = this;- const dependencies: Dependency[] = ([+ const dependencies: Dependency[] = [[IGlobalContext, { useFactory: () => this.getGlobalContext() }],[ISheetContext, { useFactory: () => this.getContext() }],- [IRenderingEngine, { useFactory: () => this.getGlobalContext().getPluginManager().getRequirePluginByName<RenderEngine>(PLUGIN_NAMES.BASE_RENDER).getEngine() }],[ISheetPlugin, { useValue: self }],// Rendering Module@@ -308,9 +305,9 @@ export class SheetPlugin extends Plugin<SheetPluginObserve, SheetContext> {// RulerManager[ColumnRulerManager],// #endregion Controllers- ]);+ ];- dependencies.forEach(d => {+ dependencies.forEach((d) => {if (d.length === 1) {sheetInjector.add(d[0]);} else {@@ -318,4 +315,4 @@ export class SheetPlugin extends Plugin<SheetPluginObserve, SheetContext> {}});}-}\ No newline at end of file+}diff --git a/packages/base-sheets/src/View/CanvasView.ts b/packages/base-sheets/src/View/CanvasView.tsindex e1a9db6550..bac2843268 100644--- a/packages/base-sheets/src/View/CanvasView.ts+++ b/packages/base-sheets/src/View/CanvasView.ts@@ -1,11 +1,11 @@import { Inject, Injector } from '@wendellhu/redi';import { EventState, SheetContext, Worksheet } from '@univerjs/core';-import { Engine, EVENT_TYPE, IScrollObserverParam, IWheelEvent, Layer, Scene, ScrollBar, Viewport } from '@univerjs/base-render';+import { Engine, EVENT_TYPE, IRenderingEngine, IScrollObserverParam, IWheelEvent, Layer, Scene, ScrollBar, Viewport } from '@univerjs/base-render';import { BaseView, CANVAS_VIEW_KEY, CanvasViewRegistry } from './BaseView';import { SheetView } from './Views/SheetView';import './Views';-import { IRenderingEngine, ISheetContext } from '../Services/tokens';+import { ISheetContext } from '../Services/tokens';// workbookexport class CanvasView {@@ -157,4 +157,4 @@ export class CanvasView {this._views.push(viewFactory.create(scene, this._injector));});}-}\ No newline at end of file+}diff --git a/packages/core/src/Basics/Univer.ts b/packages/core/src/Basics/Univer.tsindex 0c79c4e673..697a38d0c9 100644--- a/packages/core/src/Basics/Univer.ts+++ b/packages/core/src/Basics/Univer.ts@@ -5,7 +5,7 @@ import { UniverDoc } from './UniverDoc';import { UniverSlide } from './UniverSlide';import { Nullable } from '../Shared';import { Context } from './Context';-import { Plugin, PluginType } from '../Plugin';+import { Plugin, PluginCtor, PluginType } from '../Plugin';import { IUniverData, IWorkbookConfig } from '../Types/Interfaces';import { UniverObserverImpl } from './UniverObserverImpl';@@ -17,13 +17,13 @@ interface IPluginRegistryItem {class PluginRegistry {private readonly pluginsRegisteredByBusiness = new Map<PluginType, [IPluginRegistryItem]>();- registerPlugin(plugin: typeof Plugin, options: any) {- const type = plugin.type;+ registerPlugin(pluginCtor: PluginCtor<any>, options: any) {+ const type = pluginCtor.type;if (!this.pluginsRegisteredByBusiness.has(type)) {this.pluginsRegisteredByBusiness.set(type, [] as unknown[] as [IPluginRegistryItem]);}- this.pluginsRegisteredByBusiness.get(type)!.push({ plugin, options });+ this.pluginsRegisteredByBusiness.get(type)!.push({ plugin: pluginCtor, options });}getRegisterPlugins(type: PluginType): [IPluginRegistryItem] {@@ -79,10 +79,11 @@ export class Univer {}/** Register a plugin into univer. */- registerPlugin<T>(plugin: Ctor<T>, configs?: any): void {+ registerPlugin<T extends Ctor<Plugin> & { type: PluginType }>(plugin: T, configs?: any): void {// TODO: type of `configs` could be optimized here using typescript infer+if (plugin.type === PluginType.Univer) {- this.registerUniverPlugin(plugin);+ this.registerUniverPlugin(plugin, configs);} else if (plugin.type === PluginType.Sheet) {this.registerSheetPlugin(plugin, configs);} else {@@ -102,14 +103,6 @@ export class Univer {return sheet;}- private initializePluginsForSheet(sheet: UniverSheet): void {- const plugins = this._univerPluginRegistry.getRegisterPlugins(PluginType.Sheet);- plugins.forEach(p => {- const pluginInstance: Plugin = this._univerInjector.createInstance(p.plugin as unknown as Ctor<any>, p.options);- sheet.installPlugin(pluginInstance);- });- }-addUniverSheet(univerSheet: UniverSheet): void {univerSheet.context.onUniver(this);this._univerSheets.push(univerSheet);@@ -196,19 +189,33 @@ export class Univer {return new Injector();}- private registerUniverPlugin<T>(plugin: Ctor<T>, options?: any): void {+ private registerUniverPlugin<T extends Plugin>(plugin: PluginCtor<T>, options?: any): void {// For plugins at Univer level. Plugins would be initialized immediately so they can register dependencies.const pluginInstance: Plugin = this._univerInjector.createInstance(plugin as unknown as Ctor<any>, options);- pluginInstance.onCreate(this._context); // TODO: remove this later++ // TODO: remove these two lines later+ pluginInstance.onCreate(this._context);+ this._context.getPluginManager().install(pluginInstance);+this._univerPluginStore.addPlugin(pluginInstance);}- private registerSheetPlugin(plugin: typeof Plugin, options?: any) {+ private registerSheetPlugin<T extends Plugin>(pluginCtor: PluginCtor<T>, options?: any) {// Add plugins to the plugin registration. And for each initialized UniverSheet, instantiate these dependencies immediately.if (this._univerSheets.length) {- // TODO: add plugin to these already instantiated UniverSheet instances.+ this._univerSheets.forEach((sheet) => {+ sheet.addPlugin(pluginCtor, options);+ });} else {- this._univerPluginRegistry.registerPlugin(plugin, options);+ this._univerPluginRegistry.registerPlugin(pluginCtor, options);}}-}\ No newline at end of file++ private initializePluginsForSheet(sheet: UniverSheet): void {+ const plugins = this._univerPluginRegistry.getRegisterPlugins(PluginType.Sheet);+ plugins.forEach((p) => {+ const pluginInstance: Plugin = this._univerInjector.createInstance(p.plugin as unknown as Ctor<any>, p.options);+ sheet.installPlugin(pluginInstance);+ });+ }+}diff --git a/packages/core/src/Basics/UniverSheet.ts b/packages/core/src/Basics/UniverSheet.tsindex 07c004f663..a6eb0bd6c1 100644--- a/packages/core/src/Basics/UniverSheet.ts+++ b/packages/core/src/Basics/UniverSheet.ts@@ -1,7 +1,7 @@import { Ctor, Injector, Optional, Disposable } from '@wendellhu/redi';import { Workbook, ColorBuilder } from '../Sheets/Domain';import { IWorkbookConfig } from '../Types/Interfaces';-import { BasePlugin, Plugin } from '../Plugin';+import { BasePlugin, Plugin, PluginCtor } from '../Plugin';import { IOHttp, IOHttpConfig, Logger } from '../Shared';import { SheetContext } from './SheetContext';import { VersionCode, VersionEnv } from './Version';@@ -134,9 +134,12 @@ export class UniverSheet implements Disposable {* Add a plugin into UniverSheet. UniverSheet should add dependencies exposed from this plugin to its DI system.* @param plugin constructor of the plugin class*/- addPlugin(plugin: typeof Plugin, options: any): void {+ addPlugin<T extends Plugin>(plugin: PluginCtor<T>, options: any): void {const pluginInstance: Plugin = this._sheetInjector.createInstance(plugin as unknown as Ctor<any>, options);- pluginInstance.onCreate(this._context); // TODO: remove context passed in here++ // TODO: remove context passed in here+ pluginInstance.onCreate(this._context);+this._pluginStore.addPlugin(pluginInstance);}diff --git a/packages/core/src/Plugin/Plugin.ts b/packages/core/src/Plugin/Plugin.tsindex 8f749acb5f..706930a2b8 100644--- a/packages/core/src/Plugin/Plugin.ts+++ b/packages/core/src/Plugin/Plugin.ts@@ -4,6 +4,8 @@ import { Observable } from '../Observer';import { Locale, Nullable, PropsFrom } from '../Shared';import { Univer } from '../Basics';+export type PluginCtor<T> = Ctor<T> & { type: PluginType };+/** Plugin types for different kinds of business. */export enum PluginType {Univer,diff --git a/packages/sheets-plugin-numfmt/src/NumfmtPlugin.ts b/packages/sheets-plugin-numfmt/src/NumfmtPlugin.tsindex 15583825c1..b4cb65bc6c 100644--- a/packages/sheets-plugin-numfmt/src/NumfmtPlugin.ts+++ b/packages/sheets-plugin-numfmt/src/NumfmtPlugin.ts@@ -1,5 +1,5 @@-import { RenderEngine } from '@univerjs/base-render';-import { IGlobalContext, IRenderingEngine, ISheetContext } from '@univerjs/base-sheets';+import { IRenderingEngine, RenderEngine } from '@univerjs/base-render';+import { IGlobalContext, ISheetContext } from '@univerjs/base-sheets';import { IRangeData, ObjectMatrixPrimitiveType, Plugin, PLUGIN_NAMES, SheetContext } from '@univerjs/core';import { Injector } from '@wendellhu/redi';import { NUMFMT_PLUGIN_NAME } from './Basics/Const/PLUGIN_NAME';@@ -72,4 +72,4 @@ export class NumfmtPlugin extends Plugin<NumfmtPluginObserve, SheetContext> {[NumfmtModalController],]);}-}\ No newline at end of file+}diff --git a/packages/ui-plugin-sheets/package.json b/packages/ui-plugin-sheets/package.jsonindex ddd9b17cf7..26b7493be0 100644--- a/packages/ui-plugin-sheets/package.json+++ b/packages/ui-plugin-sheets/package.json@@ -62,6 +62,7 @@"@univerjs/base-sheets": "workspace:^0.0.1","@univerjs/base-ui": "workspace:^0.0.1","@univerjs/core": "workspace:^0.0.1",+ "@wendellhu/redi": "^0.8.0","css-vars-ponyfill": "^2.4.6","preact": "^10.12.1"}diff --git a/packages/ui-plugin-sheets/src/SheetUIPlugin.ts b/packages/ui-plugin-sheets/src/SheetUIPlugin.tsindex ca084de1b0..26606dc767 100644--- a/packages/ui-plugin-sheets/src/SheetUIPlugin.ts+++ b/packages/ui-plugin-sheets/src/SheetUIPlugin.ts@@ -1,6 +1,7 @@-import { Plugin, Tools, PLUGIN_NAMES, Context, Univer } from '@univerjs/core';+import { Inject, Injector } from '@wendellhu/redi';+import { Plugin, Tools, Context, Univer, PluginType } from '@univerjs/core';import { ComponentManager, getRefElement, RegisterManager, KeyboardManager, SlotComponent, ZIndexManager } from '@univerjs/base-ui';-import { RenderEngine } from '@univerjs/base-render';+import { IRenderingEngine } from '@univerjs/base-render';import { DefaultSheetUIConfig, installObserver, ISheetUIPluginConfig, SheetUIPluginObserve, SHEET_UI_PLUGIN_NAME } from './Basics';import { AppUIController } from './Controller/AppUIController';import { Fx } from './View/FormulaBar';@@ -9,6 +10,8 @@ import { IToolbarItemProps } from './Controller';import { zh, en } from './Locale';export class SheetUIPlugin extends Plugin<SheetUIPluginObserve, Context> {+ static override type = PluginType.Univer;+private _appUIController: AppUIController;private _keyboardManager: KeyboardManager;@@ -21,13 +24,14 @@ export class SheetUIPlugin extends Plugin<SheetUIPluginObserve, Context> {private _componentManager: ComponentManager;- constructor(config?: ISheetUIPluginConfig) {+ constructor(config: ISheetUIPluginConfig, @Inject(Injector) private readonly _injector: Injector) {super(SHEET_UI_PLUGIN_NAME);+this._config = Tools.deepMerge({}, DefaultSheetUIConfig, config);}static create(config?: ISheetUIPluginConfig) {- return new SheetUIPlugin(config);+ return new SheetUIPlugin(config || {}, new Injector());}installTo(univerInstance: Univer) {@@ -93,7 +97,7 @@ export class SheetUIPlugin extends Plugin<SheetUIPluginObserve, Context> {}initRender() {- const engine = this.getPluginByName<RenderEngine>(PLUGIN_NAMES.BASE_RENDER)?.getEngine()!;+ const engine = this._injector.get(IRenderingEngine);let container = getRefElement(this._appUIController.getSheetContainerController().getContentRef());// mount canvas to DOM container