From 388951a1c73063e2f6a94a7e2de5cf2f30b1892e Mon Sep 17 00:00:00 2001 From: Wenzhao Hu Date: Fri, 19 Apr 2024 13:53:40 +0800 Subject: [PATCH] refactor: extract plugin service (#1993) * refactor: extract plugin service * refactor: change how plugin name is provided * feat: change plugin names and types * feat: support register plugin with duplication detection * feat: find replace plugin register its dependency * fix: fix type error --- .../performance-monitor.controller.ts | 2 +- .../src/plugins/debugger/debugger-plugin.ts | 14 +- examples/src/sheets/main.ts | 5 - packages/core/src/common/unit.ts | 2 +- packages/core/src/index.ts | 7 +- packages/core/src/plugin/plugin.ts | 106 -------------- .../plugin}/plugin-holder.ts | 29 +++- .../src/services/plugin/plugin.service.ts | 137 ++++++++++++++++++ .../src/{common => services/plugin}/plugin.ts | 37 ++--- packages/core/src/univer.ts | 85 +++-------- packages/data-validation/src/plugin.ts | 7 +- packages/docs-ui/src/docs-ui-plugin.ts | 9 +- .../__tests__/create-command-test-bed.ts | 7 +- packages/docs/src/doc-plugin.ts | 7 +- .../__tests__/create-command-test-bed.ts | 7 +- .../__tests__/create-function-test-bed.ts | 7 +- .../__tests__/create-command-test-bed.ts | 7 +- packages/engine-formula/src/plugin.ts | 7 +- packages/engine-render/src/render-engine.ts | 7 +- .../src/apis/__tests__/create-test-bed.ts | 7 +- .../apis/docs/__tests__/create-test-bed.ts | 7 +- .../apis/sheets/__tests__/create-test-bed.ts | 7 +- packages/find-replace/src/plugin.ts | 7 +- packages/image/src/plugin.ts | 12 +- packages/rpc/src/plugin.ts | 12 +- .../src/plugin.ts | 8 +- .../src/plugin.ts | 9 +- .../src/services/__test__/test.util.ts | 7 +- packages/sheets-data-validation/src/plugin.ts | 7 +- packages/sheets-find-replace/src/plugin.ts | 18 ++- packages/sheets-formula/README-zh.md | 7 +- packages/sheets-formula/README.md | 7 +- .../__tests__/create-command-test-bed.ts | 7 +- .../__tests__/create-command-test-bed.ts | 7 +- .../sheets-formula/src/formula-ui-plugin.ts | 10 +- .../src/controllers/__tests__/test.util.ts | 7 +- packages/sheets-numfmt/src/numfmt-plugin.ts | 7 +- .../__tests__/create-command-test-bed.ts | 7 +- .../editor/__tests__/create-test-bed.ts | 7 +- .../menu/__tests__/create-menu-test-bed.ts | 11 +- .../clipboard/__tests__/clipboard-test-bed.ts | 7 +- .../__test__/create-service-test-bed.ts | 8 +- packages/sheets-ui/src/sheets-ui-plugin.ts | 8 +- packages/sheets-zen-editor/src/plugin.ts | 14 +- .../__tests__/create-command-test-bed.ts | 7 +- .../__tests__/create-command-test-bed.ts | 7 +- .../sheets/src/controllers/__tests__/util.ts | 7 +- .../sheets/src/services/__tests__/util.ts | 7 +- .../ref-range/__tests__/__testing__.ts | 7 +- .../__tests__/ref-range.service.spec.ts | 7 +- .../__tests__/create-core-test-bed.ts | 7 +- packages/sheets/src/sheets-plugin.ts | 9 +- packages/slides-ui/src/slides-ui-plugin.ts | 7 +- packages/slides/src/slides-plugin.ts | 11 +- packages/ui/src/ui-plugin.ts | 11 +- packages/uniscript/src/plugin.ts | 7 +- 56 files changed, 410 insertions(+), 388 deletions(-) delete mode 100644 packages/core/src/plugin/plugin.ts rename packages/core/src/{common => services/plugin}/plugin-holder.ts (73%) create mode 100644 packages/core/src/services/plugin/plugin.service.ts rename packages/core/src/{common => services/plugin}/plugin.ts (74%) diff --git a/examples/src/plugins/debugger/controllers/performance-monitor.controller.ts b/examples/src/plugins/debugger/controllers/performance-monitor.controller.ts index 85e0284186..3dc2eb3533 100644 --- a/examples/src/plugins/debugger/controllers/performance-monitor.controller.ts +++ b/examples/src/plugins/debugger/controllers/performance-monitor.controller.ts @@ -22,7 +22,7 @@ import { interval, takeUntil, throttle } from 'rxjs'; @OnLifecycle(LifecycleStages.Rendered, PerformanceMonitorController) export class PerformanceMonitorController extends RxDisposable { - private _documentType: UniverInstanceType = UniverInstanceType.UNKNOWN; + private _documentType: UniverInstanceType = UniverInstanceType.UNIVER; private _hasWatched = false; private _container!: HTMLDivElement; private _styleElement!: HTMLStyleElement; diff --git a/examples/src/plugins/debugger/debugger-plugin.ts b/examples/src/plugins/debugger/debugger-plugin.ts index 66a268503c..d5d1352bc2 100644 --- a/examples/src/plugins/debugger/debugger-plugin.ts +++ b/examples/src/plugins/debugger/debugger-plugin.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { LocaleService, Plugin, PluginType } from '@univerjs/core'; +import { Plugin, UniverInstanceType } from '@univerjs/core'; import type { Dependency } from '@wendellhu/redi'; import { Inject, Injector } from '@wendellhu/redi'; @@ -24,16 +24,16 @@ import { PerformanceMonitorController } from './controllers/performance-monitor. export interface IDebuggerPluginConfig {} export class DebuggerPlugin extends Plugin { - static override type = PluginType.Doc; + static override pluginName = 'debugger'; + static override type = UniverInstanceType.DOC; private _debuggerController!: DebuggerController; constructor( - config: IDebuggerPluginConfig, - @Inject(Injector) override readonly _injector: Injector, - @Inject(LocaleService) private readonly _localeService: LocaleService + _config: IDebuggerPluginConfig, + @Inject(Injector) override readonly _injector: Injector ) { - super('debugger'); + super(); this._initializeDependencies(_injector); } @@ -54,8 +54,6 @@ export class DebuggerPlugin extends Plugin { this.initialize(); } - override onDestroy(): void {} - getDebuggerController() { return this._debuggerController; } diff --git a/examples/src/sheets/main.ts b/examples/src/sheets/main.ts index b225d6d398..ec4cf001e4 100644 --- a/examples/src/sheets/main.ts +++ b/examples/src/sheets/main.ts @@ -20,7 +20,6 @@ import { UniverDocsPlugin } from '@univerjs/docs'; import { UniverDocsUIPlugin } from '@univerjs/docs-ui'; import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula'; import { UniverRenderEnginePlugin } from '@univerjs/engine-render'; -import { UniverFindReplacePlugin } from '@univerjs/find-replace'; import type { IUniverRPCMainThreadConfig } from '@univerjs/rpc'; import { UniverRPCMainThreadPlugin } from '@univerjs/rpc'; import { UniverSheetsPlugin } from '@univerjs/sheets'; @@ -81,10 +80,6 @@ univer.registerPlugin(UniverRPCMainThreadPlugin, { workerURL: './worker.js', } as IUniverRPCMainThreadConfig); -// find replace -univer.registerPlugin(UniverFindReplacePlugin); -// univer.registerPlugin(UniverSheetsFindPlugin); - // data validation univer.registerPlugin(UniverDataValidationPlugin); univer.registerPlugin(UniverSheetsDataValidationPlugin); diff --git a/packages/core/src/common/unit.ts b/packages/core/src/common/unit.ts index 22bfaec6a4..6362bb5937 100644 --- a/packages/core/src/common/unit.ts +++ b/packages/core/src/common/unit.ts @@ -20,7 +20,7 @@ import { Disposable } from '../shared'; * Type of built-in univer document instances. */ export enum UniverInstanceType { - UNKNOWN = 0, + UNIVER = 0, DOC = 1, SHEET = 2, SLIDE = 3, diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index bbfeeeed98..c069b8a3f0 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -19,7 +19,7 @@ import { installShims } from './common/shims'; export { type UnitType, UnitModel, UniverInstanceType } from './common/unit'; export { Registry, RegistryAsMap } from './common/registry'; export { Univer } from './univer'; -export { PluginHolder } from './common/plugin-holder'; +export { PluginHolder } from './services/plugin/plugin-holder'; export { shallowEqual, isRangesEqual, isUnitRangesEqual } from './common/equal'; export { dedupe, remove, rotate, groupBy } from './common/array'; export { @@ -47,7 +47,8 @@ export { getBodySlice, composeBody } from './docs/data-model/text-x/utils'; export { TextX } from './docs/data-model/text-x/text-x'; export { replaceInDocumentBody } from './docs/data-model/replacement'; export * from './observer'; -export { Plugin, PluginType } from './common/plugin'; +export { Plugin } from './services/plugin/plugin'; +export { PluginService } from './services/plugin/plugin.service'; export { type CommandListener, CommandService, @@ -115,7 +116,7 @@ export { fromCallback } from './shared/rxjs'; export type { IComposeInterceptors, IInterceptor, InterceptorHandler } from './common/interceptor'; export { composeInterceptors, createInterceptorKey, InterceptorManager } from './common/interceptor'; export { normalizeTextRuns } from './docs/data-model/apply-utils/common'; -export type { PluginCtor } from './common/plugin'; +export type { PluginCtor } from './services/plugin/plugin'; export { Range } from './sheets/range'; export { Styles } from './sheets/styles'; export { diff --git a/packages/core/src/plugin/plugin.ts b/packages/core/src/plugin/plugin.ts deleted file mode 100644 index 74f27aa547..0000000000 --- a/packages/core/src/plugin/plugin.ts +++ /dev/null @@ -1,106 +0,0 @@ -/** - * Copyright 2023-present DreamNum Inc. - * - * 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 type { Ctor, Injector } from '@wendellhu/redi'; - -export type PluginCtor = Ctor & { type: PluginType }; - -/** Plugin types for different kinds of business. */ -export enum PluginType { - Univer, - Doc, - Sheet, - Slide, -} - -/** - * Plug-in base class, all plug-ins must inherit from this base class. Provide basic methods. - */ -export abstract class Plugin { - static type: PluginType = PluginType.Univer; - - protected abstract _injector: Injector; - - private _name: string; - - protected constructor(name: string) { - this._name = name; - } - - onStarting(injector: Injector): void {} - - onReady(): void {} - - onRendered(): void {} - - onSteady(): void {} - - onDestroy(): void {} - - getPluginName(): string { - return this._name; - } -} - -interface IPluginRegistryItem { - plugin: PluginCtor; - options: any; -} - -/** - * Store plugin instances. - */ -export class PluginStore { - private readonly _plugins: Plugin[] = []; - - addPlugin(plugin: Plugin): void { - this._plugins.push(plugin); - } - - removePlugins(): Plugin[] { - const plugins = this._plugins.slice(); - this._plugins.length = 0; - return plugins; - } - - forEachPlugin(callback: (plugin: Plugin) => void): void { - this._plugins.forEach(callback); - } -} - -/** - * Store plugin registry items. - */ -export class PluginRegistry { - private readonly _pluginsRegisteredByBusiness = new Map(); - - registerPlugin(pluginCtor: PluginCtor, 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: pluginCtor, options }); - } - - getRegisterPlugins(type: PluginType): [IPluginRegistryItem] { - return this._pluginsRegisteredByBusiness.get(type) || ([] as unknown[] as [IPluginRegistryItem]); - } - - clearPluginsOfType(type: PluginType): void { - this._pluginsRegisteredByBusiness.delete(type); - } -} diff --git a/packages/core/src/common/plugin-holder.ts b/packages/core/src/services/plugin/plugin-holder.ts similarity index 73% rename from packages/core/src/common/plugin-holder.ts rename to packages/core/src/services/plugin/plugin-holder.ts index e23b972ebc..ef01e2e7f4 100644 --- a/packages/core/src/common/plugin-holder.ts +++ b/packages/core/src/services/plugin/plugin-holder.ts @@ -18,18 +18,21 @@ import { type Ctor, Inject, Injector } from '@wendellhu/redi'; -import { LifecycleStages } from '../services/lifecycle/lifecycle'; -import { LifecycleInitializerService, LifecycleService } from '../services/lifecycle/lifecycle.service'; -import { Disposable } from '../shared/lifecycle'; +import { LifecycleStages } from '../lifecycle/lifecycle'; +import { LifecycleInitializerService, LifecycleService } from '../lifecycle/lifecycle.service'; +import { Disposable } from '../../shared/lifecycle'; +import { ILogService } from '../log/log.service'; import { type Plugin, type PluginCtor, PluginRegistry, PluginStore } from './plugin'; export class PluginHolder extends Disposable { protected _started: boolean = false; - protected readonly _univerPluginStore = new PluginStore(); + protected readonly _pluginRegistered = new Set(); + protected readonly _pluginStore = new PluginStore(); protected readonly _pluginRegistry = new PluginRegistry(); constructor( + @ILogService protected readonly _logService: ILogService, @Inject(Injector) protected readonly _injector: Injector, @Inject(LifecycleService) protected readonly _lifecycleService: LifecycleService, @Inject(LifecycleInitializerService) protected readonly _lifecycleInitializerService: LifecycleInitializerService @@ -37,7 +40,21 @@ export class PluginHolder extends Disposable { super(); } - _registerPlugin>(pluginCtor: T, config?: ConstructorParameters[0]): void { + override dispose(): void { + super.dispose(); + + this._pluginStore.forEachPlugin((plugin) => plugin.dispose()); + this._pluginStore.removePlugins(); + this._pluginRegistry.removePlugins(); + } + + protected _registerPlugin>(pluginCtor: T, config?: ConstructorParameters[0]): void { + const { pluginName } = pluginCtor; + if (this._pluginRegistered.has(pluginName)) { + this._logService.warn('[PluginService]', `plugin ${pluginName} has already been registered. This registration will be ignored.`); + return; + } + this._pluginRegistry.registerPlugin(pluginCtor, config); } @@ -52,7 +69,7 @@ export class PluginHolder extends Disposable { if (!this._started) return; const plugins = this._pluginRegistry.getRegisterPlugins().map(({ plugin, options }) => this._initPlugin(plugin, options)); - this._pluginRegistry.clearPlugins(); + this._pluginRegistry.removePlugins(); this.disposeWithMe(this._lifecycleService.subscribeWithPrevious().subscribe((stage) => { this._pluginsRunLifecycle(plugins, stage); diff --git a/packages/core/src/services/plugin/plugin.service.ts b/packages/core/src/services/plugin/plugin.service.ts new file mode 100644 index 0000000000..e219030134 --- /dev/null +++ b/packages/core/src/services/plugin/plugin.service.ts @@ -0,0 +1,137 @@ +/** + * Copyright 2023-present DreamNum Inc. + * + * 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 { Inject, Injector } from '@wendellhu/redi'; +import { type UnitType, UniverInstanceType } from '../../common/unit'; +import { LifecycleInitializerService, LifecycleService } from '../lifecycle/lifecycle.service'; +import { LifecycleStages } from '../lifecycle/lifecycle'; +import { ILogService } from '../log/log.service'; +import { PluginHolder } from './plugin-holder'; +import type { Plugin, PluginCtor } from './plugin'; + +const INIT_LAZY_PLUGINS_TIMEOUT = 200; + +/** + * This service manages plugin registration. + */ +export class PluginService extends PluginHolder { + private _pluginHoldersForTypes = new Map(); + + constructor( + @ILogService _logService: ILogService, + @Inject(Injector) _injector: Injector, + @Inject(LifecycleService) _lifecycleService: LifecycleService, + @Inject(LifecycleInitializerService) _lifecycleInitializerService: LifecycleInitializerService + ) { + super(_logService, _injector, _lifecycleService, _lifecycleInitializerService); + } + + override dispose(): void { + this._clearFlushLazyPluginsTimer(); + + // Dispose all plugin holders including self. + super.dispose(); + for (const holder of this._pluginHoldersForTypes.values()) { + holder.dispose(); + } + } + + /** Register a plugin into univer. */ + registerPlugin>(plugin: T, config?: ConstructorParameters[0]): void { + this._assertPluginValid(plugin); + + const { type } = plugin; + if (type === UniverInstanceType.UNIVER) { + return this._registerPlugin(plugin, config); + } + + // If it's type is for specific document, we should run them at specific time. + const holder = this._ensurePluginHolderForType(type); + + // @ts-ignore + holder._registerPlugin(plugin, config); + } + + private _assertPluginValid(plugin: PluginCtor): void { + const { type, pluginName } = plugin; + + if (type === UniverInstanceType.UNRECOGNIZED) { + throw new Error(`[PluginService]: invalid plugin type for ${plugin}. Please assign a "type" to your plugin.`); + } + + if (pluginName === '') { + throw new Error(`[PluginService]: no plugin name for ${plugin}. Please assign a "pluginName" to your plugin.`); + } + } + + protected override _registerPlugin>(pluginCtor: T, options?: ConstructorParameters[0]): void { + const { pluginName } = pluginCtor; + if (this._pluginRegistered.has(pluginName)) { + this._logService.warn('[PluginService]', `plugin ${pluginName} has already been registered. This registration will be ignored.`); + return; + } + + this._pluginRegistered.add(pluginName); + + if (this._started) { + this._pluginRegistry.registerPlugin(pluginCtor, options); + + // If Univer has already started, we should manually call onStarting for the plugin. + // We do that in an asynchronous way, because user may lazy load several plugins at the same time. + return this._scheduleInitPluginAfterStarted(); + } else { + // For plugins at Univer level. Plugins would be initialized immediately so they can register dependencies. + const pluginInstance: Plugin = this._injector.createInstance(pluginCtor, options); + this._pluginStore.addPlugin(pluginInstance); + this._pluginsRunLifecycle([pluginInstance], LifecycleStages.Starting); + } + } + + _ensurePluginHolderForType(type: UnitType): PluginHolder { + if (!this._pluginHoldersForTypes.has(type)) { + const pluginHolder = this._injector.createInstance(PluginHolder); + this._pluginHoldersForTypes.set(type, pluginHolder); + return pluginHolder; + } + + return this._pluginHoldersForTypes.get(type)!; + } + + private _initLazyPluginsTimer?: number; + private _scheduleInitPluginAfterStarted() { + if (this._initLazyPluginsTimer === undefined) { + this._initLazyPluginsTimer = setTimeout( + () => this._flushLazyPlugins(), + INIT_LAZY_PLUGINS_TIMEOUT + ) as unknown as number; + } + } + + private _clearFlushLazyPluginsTimer() { + if (this._initLazyPluginsTimer) { + clearTimeout(this._initLazyPluginsTimer); + this._initLazyPluginsTimer = undefined; + } + } + + private _flushLazyPlugins() { + this._flush(); + + for (const [_, holder] of this._pluginHoldersForTypes) { + holder._flush(); + } + } +} diff --git a/packages/core/src/common/plugin.ts b/packages/core/src/services/plugin/plugin.ts similarity index 74% rename from packages/core/src/common/plugin.ts rename to packages/core/src/services/plugin/plugin.ts index c1bc2f4036..7d67a43f44 100644 --- a/packages/core/src/common/plugin.ts +++ b/packages/core/src/services/plugin/plugin.ts @@ -15,35 +15,21 @@ */ import type { Ctor, Injector } from '@wendellhu/redi'; +import { Disposable } from '../../shared'; +import { UniverInstanceType } from '../../common/unit'; -export type PluginCtor = Ctor & { type: PluginType }; - -/** - * Plugin types for different kinds of business. - * - * @deprecated use UnitType instead - */ -export enum PluginType { - Univer = 0, - Doc = 1, - Sheet = 2, - Slide = 3, -} +export type PluginCtor = Ctor & { type: UniverInstanceType; pluginName: string }; /** * Plug-in base class, all plug-ins must inherit from this base class. Provide basic methods. */ -export abstract class Plugin { - static type: PluginType = PluginType.Univer; +export abstract class Plugin extends Disposable { + static pluginName: string = ''; + static type: UniverInstanceType = UniverInstanceType.UNRECOGNIZED; protected abstract _injector: Injector; - private _name: string; - - protected constructor(name: string) { - this._name = name; - } - + // eslint-disable-next-line unused-imports/no-unused-vars onStarting(injector: Injector): void {} onReady(): void {} @@ -52,15 +38,18 @@ export abstract class Plugin { onSteady(): void {} - onDestroy(): void {} + getUniverInstanceType(): UniverInstanceType { + return (this.constructor as typeof Plugin).type; + } getPluginName(): string { - return this._name; + return (this.constructor as typeof Plugin).pluginName; } } interface IPluginRegistryItem { plugin: PluginCtor; + // eslint-disable-next-line ts/no-explicit-any options: any; } @@ -100,7 +89,7 @@ export class PluginRegistry { return this._pluginsRegistered.slice(); } - clearPlugins(): void { + removePlugins(): void { this._pluginsRegistered = []; } } diff --git a/packages/core/src/univer.ts b/packages/core/src/univer.ts index c88465dda7..1c4ac388d8 100644 --- a/packages/core/src/univer.ts +++ b/packages/core/src/univer.ts @@ -17,8 +17,6 @@ import { Injector } from '@wendellhu/redi'; import { DocumentDataModel } from './docs/data-model/document-data-model'; -import type { Plugin, PluginCtor } from './common/plugin'; -import { PluginType } from './common/plugin'; import { CommandService, ICommandService } from './services/command/command.service'; import { ConfigService, IConfigService } from './services/config/config.service'; import { ContextService, IContextService } from './services/context/context.service'; @@ -44,26 +42,25 @@ import { Workbook } from './sheets/workbook'; import { SlideDataModel } from './slides/slide-model'; import type { LocaleType } from './types/enum/locale-type'; import type { IDocumentData, ISlideData, IUniverData, IWorkbookData } from './types/interfaces'; -import { PluginHolder } from './common/plugin-holder'; import type { UnitModel, UnitType } from './common/unit'; import { UniverInstanceType } from './common/unit'; +import { PluginService } from './services/plugin/plugin.service'; +import type { Plugin, PluginCtor } from './services/plugin/plugin'; -const INIT_LAZY_PLUGINS_TIMEOUT = 200; - -export class Univer extends PluginHolder { +export class Univer { private _startedTypes = new Set(); - private _pluginHoldersForTypes = new Map(); + private _injector: Injector; private get _univerInstanceService(): IUniverInstanceService { return this._injector.get(IUniverInstanceService); } - constructor(univerData: Partial = {}) { - const injector = createUniverInjector(); - const lifecycleService = injector.get(LifecycleService); - const lifecycleInitializerService = injector.get(LifecycleInitializerService); + private get _pluginService(): PluginService { + return this._injector.get(PluginService); + } - super(injector, lifecycleService, lifecycleInitializerService); + constructor(univerData: Partial = {}) { + const injector = this._injector = createUniverInjector(); const { theme, locale, locales, logLevel } = univerData; @@ -79,10 +76,8 @@ export class Univer extends PluginHolder { return this._injector; } - override dispose(): void { + dispose(): void { this._injector.dispose(); - - super.dispose(); } setLocale(locale: LocaleType) { @@ -124,12 +119,12 @@ export class Univer extends PluginHolder { const univerInstanceService = injector.get(IUniverInstanceService) as UniverInstanceService; univerInstanceService.__setCreateHandler( (type: UnitType, data, ctor) => { - this._start(); + this._tryProgressToStart(); if (!this._startedTypes.has(type)) { this._startedTypes.add(type); - const pluginHolder = this._ensurePluginHolderForType(type); + const pluginHolder = this._pluginService._ensurePluginHolderForType(type); pluginHolder._start(); const model = injector.createInstance(ctor, data); @@ -147,6 +142,10 @@ export class Univer extends PluginHolder { ); } + private _tryProgressToStart(): void { + this._pluginService._start(); + } + private _tryProgressToReady(): void { const lifecycleService = this._injector.get(LifecycleService); if (lifecycleService.stage < LifecycleStages.Ready) { @@ -154,59 +153,10 @@ export class Univer extends PluginHolder { } } - private _ensurePluginHolderForType(type: UnitType): PluginHolder { - if (!this._pluginHoldersForTypes.has(type)) { - const pluginHolder = this._injector.createInstance(PluginHolder); - this._pluginHoldersForTypes.set(type, pluginHolder); - return pluginHolder; - } - - return this._pluginHoldersForTypes.get(type)!; - } /** Register a plugin into univer. */ registerPlugin>(plugin: T, config?: ConstructorParameters[0]): void { - const type = plugin.type; - if (type === PluginType.Univer) { - return this._registerPlugin(plugin, config); - } - - // If it's type is for specific document, we should run them at specific time. - const holder = this._ensurePluginHolderForType(type); - holder._registerPlugin(plugin, config); - } - - override _registerPlugin>(pluginCtor: T, options?: ConstructorParameters[0]): void { - if (this._started) { - this._pluginRegistry.registerPlugin(pluginCtor, options); - - // If Univer has already started, we should manually call onStarting for the plugin. - // We do that in an asynchronous way, because user may lazy load several plugins at the same time. - return this._scheduleInitPluginAfterStarted(); - } else { - // For plugins at Univer level. Plugins would be initialized immediately so they can register dependencies. - const pluginInstance: Plugin = this._injector.createInstance(pluginCtor, options); - this._univerPluginStore.addPlugin(pluginInstance); - this._pluginsRunLifecycle([pluginInstance], LifecycleStages.Starting); - } - } - - private _initLazyPluginsTimer?: number; - private _scheduleInitPluginAfterStarted() { - if (this._initLazyPluginsTimer === undefined) { - this._initLazyPluginsTimer = setTimeout( - () => this._flushLazyPlugins(), - INIT_LAZY_PLUGINS_TIMEOUT - ) as unknown as number; - } - } - - private _flushLazyPlugins() { - this._flush(); - - for (const [_, holder] of this._pluginHoldersForTypes) { - holder._flush(); - } + this._pluginService.registerPlugin(plugin, config); } } @@ -218,6 +168,7 @@ function createUniverInjector() { [LifecycleService], [LifecycleInitializerService], [UniverPermissionService], + [PluginService], [IUniverInstanceService, { useClass: UniverInstanceService }], [IPermissionService, { useClass: PermissionService }], [ILogService, { useClass: DesktopLogService, lazy: true }], diff --git a/packages/data-validation/src/plugin.ts b/packages/data-validation/src/plugin.ts index 429652ccbc..2a7b54aa4a 100644 --- a/packages/data-validation/src/plugin.ts +++ b/packages/data-validation/src/plugin.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { ICommandService, Plugin } from '@univerjs/core'; +import { ICommandService, Plugin, UniverInstanceType } from '@univerjs/core'; import type { Dependency } from '@wendellhu/redi'; import { Inject, Injector } from '@wendellhu/redi'; import { DataValidatorRegistryService } from './services/data-validator-registry.service'; @@ -28,12 +28,15 @@ import { DataValidationFormulaMarkDirty } from './commands/mutations/formula.mut const PLUGIN_NAME = 'data-validation'; export class UniverDataValidationPlugin extends Plugin { + static override pluginName = PLUGIN_NAME; + static override type = UniverInstanceType.UNIVER; + constructor( _config: unknown, @Inject(Injector) protected _injector: Injector, @ICommandService private _commandService: ICommandService ) { - super(PLUGIN_NAME); + super(); } override onStarting(injector: Injector): void { diff --git a/packages/docs-ui/src/docs-ui-plugin.ts b/packages/docs-ui/src/docs-ui-plugin.ts index 9d67c6d599..427f43b406 100644 --- a/packages/docs-ui/src/docs-ui-plugin.ts +++ b/packages/docs-ui/src/docs-ui-plugin.ts @@ -19,8 +19,8 @@ import { IUniverInstanceService, LocaleService, Plugin, - PluginType, Tools, + UniverInstanceType, } from '@univerjs/core'; import type { Dependency } from '@wendellhu/redi'; import { Inject, Injector } from '@wendellhu/redi'; @@ -58,7 +58,8 @@ import { TextSelectionController } from './controllers/text-selection.controller import { BackScrollController } from './controllers/back-scroll.controller'; export class UniverDocsUIPlugin extends Plugin { - static override type = PluginType.Doc; + static override pluginName = DOC_UI_PLUGIN_NAME; + static override type = UniverInstanceType.DOC; constructor( private readonly _config: IUniverDocsUIConfig, @@ -66,7 +67,7 @@ export class UniverDocsUIPlugin extends Plugin { @Inject(LocaleService) private readonly _localeService: LocaleService, @ILogService private _logService: ILogService ) { - super(DOC_UI_PLUGIN_NAME); + super(); this._localeService.load({ zhCN, @@ -82,8 +83,6 @@ export class UniverDocsUIPlugin extends Plugin { this._markDocAsFocused(); } - override onDestroy(): void {} - private _initializeCommands(): void { [ MoveCursorUpShortcut, diff --git a/packages/docs/src/commands/commands/__tests__/create-command-test-bed.ts b/packages/docs/src/commands/commands/__tests__/create-command-test-bed.ts index 6c213d969d..48901b5acd 100644 --- a/packages/docs/src/commands/commands/__tests__/create-command-test-bed.ts +++ b/packages/docs/src/commands/commands/__tests__/create-command-test-bed.ts @@ -21,8 +21,8 @@ import { IUniverInstanceService, LogLevel, Plugin, - PluginType, Univer, + UniverInstanceType, } from '@univerjs/core'; import type { Dependency } from '@wendellhu/redi'; import { Inject, Injector } from '@wendellhu/redi'; @@ -97,10 +97,11 @@ export function createCommandTestBed(workbookConfig?: IDocumentData, dependencie * This plugin hooks into Doc's DI system to expose API to test scripts */ class TestPlugin extends Plugin { - static override type = PluginType.Univer; + static override pluginName = 'test-plugin'; + static override type = UniverInstanceType.UNIVER; constructor(_config: undefined, @Inject(Injector) override readonly _injector: Injector) { - super('test-plugin'); + super(); } override onStarting(injector: Injector): void { diff --git a/packages/docs/src/doc-plugin.ts b/packages/docs/src/doc-plugin.ts index 051b15aee8..be07d9d855 100644 --- a/packages/docs/src/doc-plugin.ts +++ b/packages/docs/src/doc-plugin.ts @@ -18,7 +18,7 @@ import type { ICommand } from '@univerjs/core'; import { ICommandService, Plugin, - PluginType, + UniverInstanceType, } from '@univerjs/core'; import { ITextSelectionRenderManager, TextSelectionRenderManager } from '@univerjs/engine-render'; import type { Dependency } from '@wendellhu/redi'; @@ -70,7 +70,8 @@ const DEFAULT_DOCUMENT_PLUGIN_DATA = { const PLUGIN_NAME = 'docs'; export class UniverDocsPlugin extends Plugin { - static override type = PluginType.Doc; + static override pluginName = PLUGIN_NAME; + static override type = UniverInstanceType.DOC; private _config: IUniverDocsConfig; @@ -78,7 +79,7 @@ export class UniverDocsPlugin extends Plugin { config: Partial = {}, @Inject(Injector) override _injector: Injector ) { - super(PLUGIN_NAME); + super(); this._config = Object.assign(DEFAULT_DOCUMENT_PLUGIN_DATA, config); diff --git a/packages/engine-formula/src/engine/analysis/__tests__/create-command-test-bed.ts b/packages/engine-formula/src/engine/analysis/__tests__/create-command-test-bed.ts index 71107e6b42..fef83e5271 100644 --- a/packages/engine-formula/src/engine/analysis/__tests__/create-command-test-bed.ts +++ b/packages/engine-formula/src/engine/analysis/__tests__/create-command-test-bed.ts @@ -23,9 +23,9 @@ import { LogLevel, ObjectMatrix, Plugin, - PluginType, Univer, UniverInstanceType, + } from '@univerjs/core'; import type { Dependency } from '@wendellhu/redi'; import { Inject, Injector } from '@wendellhu/redi'; @@ -233,7 +233,8 @@ export function createCommandTestBed(workbookConfig?: IWorkbookData, dependencie * This plugin hooks into Sheet's DI system to expose API to test scripts */ class TestPlugin extends Plugin { - static override type = PluginType.Sheet; + static override pluginName = 'test-plugin'; + static override type = UniverInstanceType.SHEET; private _formulaDataModel: FormulaDataModel | null = null; @@ -241,7 +242,7 @@ export function createCommandTestBed(workbookConfig?: IWorkbookData, dependencie _config: undefined, @Inject(Injector) override readonly _injector: Injector ) { - super('test-plugin'); + super(); } override onStarting(injector: Injector): void { diff --git a/packages/engine-formula/src/functions/__tests__/create-function-test-bed.ts b/packages/engine-formula/src/functions/__tests__/create-function-test-bed.ts index 164f32e46a..c3ceada101 100644 --- a/packages/engine-formula/src/functions/__tests__/create-function-test-bed.ts +++ b/packages/engine-formula/src/functions/__tests__/create-function-test-bed.ts @@ -23,9 +23,9 @@ import { LogLevel, ObjectMatrix, Plugin, - PluginType, Univer, UniverInstanceType, + } from '@univerjs/core'; import type { Dependency } from '@wendellhu/redi'; import { Inject, Injector } from '@wendellhu/redi'; @@ -156,7 +156,8 @@ export function createFunctionTestBed(workbookConfig?: IWorkbookData, dependenci * This plugin hooks into Sheet's DI system to expose API to test scripts */ class TestPlugin extends Plugin { - static override type = PluginType.Sheet; + static override pluginName = 'test-plugin'; + static override type = UniverInstanceType.SHEET; private _formulaDataModel: FormulaDataModel | null = null; @@ -164,7 +165,7 @@ export function createFunctionTestBed(workbookConfig?: IWorkbookData, dependenci _config: undefined, @Inject(Injector) override readonly _injector: Injector ) { - super('test-plugin'); + super(); } override onStarting(injector: Injector): void { diff --git a/packages/engine-formula/src/models/__tests__/create-command-test-bed.ts b/packages/engine-formula/src/models/__tests__/create-command-test-bed.ts index a6a3a48e9b..af0078d158 100644 --- a/packages/engine-formula/src/models/__tests__/create-command-test-bed.ts +++ b/packages/engine-formula/src/models/__tests__/create-command-test-bed.ts @@ -15,7 +15,7 @@ */ import type { IWorkbookData } from '@univerjs/core'; -import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, PluginType, Univer } from '@univerjs/core'; +import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, Univer, UniverInstanceType } from '@univerjs/core'; import type { Dependency } from '@wendellhu/redi'; import { Inject, Injector } from '@wendellhu/redi'; @@ -59,13 +59,14 @@ export function createCommandTestBed(workbookConfig?: IWorkbookData, dependencie * This plugin hooks into Sheet's DI system to expose API to test scripts */ class TestPlugin extends Plugin { - static override type = PluginType.Sheet; + static override pluginName = 'test-plugin'; + static override type = UniverInstanceType.SHEET; constructor( _config: undefined, @Inject(Injector) override readonly _injector: Injector ) { - super('test-plugin'); + super(); } override onStarting(injector: Injector): void { diff --git a/packages/engine-formula/src/plugin.ts b/packages/engine-formula/src/plugin.ts index 5c20693b12..ff74a26a1f 100644 --- a/packages/engine-formula/src/plugin.ts +++ b/packages/engine-formula/src/plugin.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { Plugin } from '@univerjs/core'; +import { Plugin, UniverInstanceType } from '@univerjs/core'; import type { Ctor, Dependency } from '@wendellhu/redi'; import { Inject, Injector } from '@wendellhu/redi'; @@ -66,11 +66,14 @@ interface IUniverFormulaEngine { } export class UniverFormulaEnginePlugin extends Plugin { + static override type = UniverInstanceType.UNIVER; + static override pluginName = PLUGIN_NAME; + constructor( private _config: IUniverFormulaEngine, @Inject(Injector) protected override _injector: Injector ) { - super(PLUGIN_NAME); + super(); } override onStarting(): void { diff --git a/packages/engine-render/src/render-engine.ts b/packages/engine-render/src/render-engine.ts index 64b2b6a323..94d128b0ec 100644 --- a/packages/engine-render/src/render-engine.ts +++ b/packages/engine-render/src/render-engine.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { Plugin, PluginType } from '@univerjs/core'; +import { Plugin, UniverInstanceType } from '@univerjs/core'; import { createIdentifier, Inject, Injector } from '@wendellhu/redi'; import { Engine } from './engine'; @@ -28,13 +28,14 @@ export const IRenderingEngine = createIdentifier('univer.render-engine') const PLUGIN_NAME = 'render-engine'; export class UniverRenderEnginePlugin extends Plugin { - static override type = PluginType.Univer; + static override pluginName = PLUGIN_NAME; + static override type = UniverInstanceType.UNIVER; constructor( _config: undefined, @Inject(Injector) override readonly _injector: Injector ) { - super(PLUGIN_NAME); + super(); this._injector.add([IRenderingEngine, { useFactory: () => new Engine() }]); diff --git a/packages/facade/src/apis/__tests__/create-test-bed.ts b/packages/facade/src/apis/__tests__/create-test-bed.ts index 6c99ef4584..42f12e24d1 100644 --- a/packages/facade/src/apis/__tests__/create-test-bed.ts +++ b/packages/facade/src/apis/__tests__/create-test-bed.ts @@ -22,9 +22,9 @@ import { LocaleType, LogLevel, Plugin, - PluginType, ThemeService, Univer, + UniverInstanceType, } from '@univerjs/core'; import { FunctionService, IFunctionService } from '@univerjs/engine-formula'; import { ISocketService, WebSocketService } from '@univerjs/network'; @@ -86,13 +86,14 @@ export function createTestBed(workbookConfig?: IWorkbookData, dependencies?: Dep const injector = univer.__getInjector(); class TestPlugin extends Plugin { - static override type = PluginType.Sheet; + static override pluginName = 'test-plugin'; + static override type = UniverInstanceType.SHEET; constructor( _config: undefined, @Inject(Injector) override readonly _injector: Injector ) { - super('test-plugin'); + super(); } override onStarting(injector: Injector): void { diff --git a/packages/facade/src/apis/docs/__tests__/create-test-bed.ts b/packages/facade/src/apis/docs/__tests__/create-test-bed.ts index 6ceb107ea1..b587d7f57c 100644 --- a/packages/facade/src/apis/docs/__tests__/create-test-bed.ts +++ b/packages/facade/src/apis/docs/__tests__/create-test-bed.ts @@ -21,8 +21,8 @@ import { LocaleService, LogLevel, Plugin, - PluginType, Univer, + UniverInstanceType, } from '@univerjs/core'; import { enUS, @@ -67,13 +67,14 @@ export function createTestBed(documentConfig?: IDocumentData, dependencies?: Dep const injector = univer.__getInjector(); class TestPlugin extends Plugin { - static override type = PluginType.Univer; + static override pluginName = 'test-plugin'; + static override type = UniverInstanceType.UNIVER; constructor( _config: undefined, @Inject(Injector) override readonly _injector: Injector ) { - super('test-plugin'); + super(); this._injector = _injector; } diff --git a/packages/facade/src/apis/sheets/__tests__/create-test-bed.ts b/packages/facade/src/apis/sheets/__tests__/create-test-bed.ts index 0e28c6c877..03251b7d2c 100644 --- a/packages/facade/src/apis/sheets/__tests__/create-test-bed.ts +++ b/packages/facade/src/apis/sheets/__tests__/create-test-bed.ts @@ -22,8 +22,8 @@ import { LocaleType, LogLevel, Plugin, - PluginType, Univer, + UniverInstanceType, } from '@univerjs/core'; import { FunctionService, IFunctionService } from '@univerjs/engine-formula'; import { ISocketService, WebSocketService } from '@univerjs/network'; @@ -73,13 +73,14 @@ export function createTestBed(workbookConfig?: IWorkbookData, dependencies?: Dep const injector = univer.__getInjector(); class TestPlugin extends Plugin { - static override type = PluginType.Sheet; + static override pluginName = 'test-plugin'; + static override type = UniverInstanceType.SHEET; constructor( _config: undefined, @Inject(Injector) override readonly _injector: Injector ) { - super('test-plugin'); + super(); this._injector = _injector; } diff --git a/packages/find-replace/src/plugin.ts b/packages/find-replace/src/plugin.ts index 5a7329a86c..63d9544ce7 100644 --- a/packages/find-replace/src/plugin.ts +++ b/packages/find-replace/src/plugin.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { LocaleService, Plugin } from '@univerjs/core'; +import { LocaleService, Plugin, UniverInstanceType } from '@univerjs/core'; import { type Dependency, Inject, type Injector } from '@wendellhu/redi'; import { FindReplaceController } from './controllers/find-replace.controller'; @@ -24,11 +24,14 @@ import { FindReplaceService, IFindReplaceService } from './services/find-replace const PLUGIN_NAME = 'FIND_REPLACE'; export class UniverFindReplacePlugin extends Plugin { + static override pluginName = PLUGIN_NAME; + static override type = UniverInstanceType.UNIVER; + constructor( protected readonly _injector: Injector, @Inject(LocaleService) private readonly _localeService: LocaleService ) { - super(PLUGIN_NAME); + super(); this._localeService.load({ zhCN, diff --git a/packages/image/src/plugin.ts b/packages/image/src/plugin.ts index 54d716075e..fe8c57dcf2 100644 --- a/packages/image/src/plugin.ts +++ b/packages/image/src/plugin.ts @@ -14,9 +14,8 @@ * limitations under the License. */ -import { DEFAULT_DOCUMENT_SUB_COMPONENT_ID, LocaleService, Plugin } from '@univerjs/core'; +import { DEFAULT_DOCUMENT_SUB_COMPONENT_ID, Plugin } from '@univerjs/core'; import type { Dependency, Injector } from '@wendellhu/redi'; -import { Inject } from '@wendellhu/redi'; import { ImageLoadController } from './controllers/image.load.controller'; import { ImageModel } from './models/image-model'; @@ -26,12 +25,13 @@ import { IImageRenderService, ImageRenderService } from './services/image-render const PLUGIN_NAME = 'IMAGE_PLUGIN'; export class UniverImagePlugin extends Plugin { + static override pluginName = PLUGIN_NAME; + constructor( - config: undefined, - protected _injector: Injector, - @Inject(LocaleService) private readonly _localeService: LocaleService + _config: undefined, + protected _injector: Injector ) { - super(PLUGIN_NAME); + super(); } override onStarting(_injector: Injector): void { diff --git a/packages/rpc/src/plugin.ts b/packages/rpc/src/plugin.ts index c009e6984c..2d8eae72c3 100644 --- a/packages/rpc/src/plugin.ts +++ b/packages/rpc/src/plugin.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { Plugin, PluginType } from '@univerjs/core'; +import { Plugin, UniverInstanceType } from '@univerjs/core'; import type { Dependency } from '@wendellhu/redi'; import { Inject, Injector } from '@wendellhu/redi'; @@ -41,13 +41,14 @@ export interface IUniverRPCMainThreadConfig { * is also responsible for booting up the Web Worker instance of Univer. */ export class UniverRPCMainThreadPlugin extends Plugin { - static override type = PluginType.Univer; + static override pluginName = 'UNIVER_RPC_MAIN_THREAD_PLUGIN'; + static override type = UniverInstanceType.UNIVER; constructor( private readonly _config: IUniverRPCMainThreadConfig, @Inject(Injector) protected readonly _injector: Injector ) { - super('UNIVER_RPC_MAIN_THREAD_PLUGIN'); + super(); } override async onStarting(injector: Injector): Promise { @@ -78,13 +79,14 @@ export interface IUniverRPCWorkerThreadPluginConfig {} * This plugin is used to register the RPC services on the worker thread. */ export class UniverRPCWorkerThreadPlugin extends Plugin { - static override type = PluginType.Univer; + static override pluginName = 'UNIVER_RPC_WORKER_THREAD_PLUGIN'; + static override type = UniverInstanceType.UNIVER; constructor( private readonly _config: IUniverRPCWorkerThreadPluginConfig, @Inject(Injector) protected readonly _injector: Injector ) { - super('UNIVER_RPC_WORKER_THREAD_PLUGIN'); + super(); } override onStarting(injector: Injector): void { diff --git a/packages/sheets-conditional-formatting-ui/src/plugin.ts b/packages/sheets-conditional-formatting-ui/src/plugin.ts index 0589557770..a891551c71 100644 --- a/packages/sheets-conditional-formatting-ui/src/plugin.ts +++ b/packages/sheets-conditional-formatting-ui/src/plugin.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { ICommandService, Plugin, PluginType } from '@univerjs/core'; +import { ICommandService, Plugin, UniverInstanceType } from '@univerjs/core'; import { Inject, Injector } from '@wendellhu/redi'; import { SHEET_CONDITIONAL_FORMATTING_PLUGIN, SheetsConditionalFormattingPlugin } from '@univerjs/sheets-conditional-formatting'; import { AddAverageCfCommand } from './commands/commands/add-average-cf.command'; @@ -44,7 +44,9 @@ import { ConditionalFormattingEditorController } from './controllers/cf.editor.c import { ConditionalFormattingClearController } from './controllers/cf.clear.controller'; export class UniverSheetsConditionalFormattingUIPlugin extends Plugin { - static override type = PluginType.Sheet; + static override pluginName = SHEET_CONDITIONAL_FORMATTING_PLUGIN; + static override type = UniverInstanceType.SHEET; + static commandList = [ AddAverageCfCommand, AddColorScaleConditionalRuleCommand, @@ -69,7 +71,7 @@ export class UniverSheetsConditionalFormattingUIPlugin extends Plugin { @Inject(Injector) override readonly _injector: Injector, @Inject(ICommandService) private _commandService: ICommandService ) { - super(SHEET_CONDITIONAL_FORMATTING_PLUGIN); + super(); this._initCommand(); SheetsConditionalFormattingPlugin.dependencyList.forEach((dependency) => { this._injector.add(dependency); diff --git a/packages/sheets-conditional-formatting/src/plugin.ts b/packages/sheets-conditional-formatting/src/plugin.ts index 79ee636681..4130091495 100644 --- a/packages/sheets-conditional-formatting/src/plugin.ts +++ b/packages/sheets-conditional-formatting/src/plugin.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { ICommandService, Plugin, PluginType } from '@univerjs/core'; +import { ICommandService, Plugin, UniverInstanceType } from '@univerjs/core'; import type { Dependency } from '@wendellhu/redi'; import { Inject, Injector } from '@wendellhu/redi'; import { SHEET_CONDITIONAL_FORMATTING_PLUGIN } from './base/const'; @@ -30,15 +30,18 @@ import { ConditionalFormattingFormulaService } from './services/conditional-form import { ConditionalFormattingFormulaMarkDirty } from './commands/mutations/formula-mark-dirty.mutation'; export class SheetsConditionalFormattingPlugin extends Plugin { - static override type = PluginType.Sheet; + static override pluginName = SHEET_CONDITIONAL_FORMATTING_PLUGIN; + static override type = UniverInstanceType.SHEET; + static readonly dependencyList: Dependency[] = [[ConditionalFormattingService], [ConditionalFormattingFormulaService], [ConditionalFormattingRuleModel], [ConditionalFormattingViewModel]]; static readonly mutationList = [AddConditionalRuleMutation, DeleteConditionalRuleMutation, SetConditionalRuleMutation, MoveConditionalRuleMutation, ConditionalFormattingFormulaMarkDirty]; + constructor( _config: unknown, @Inject(Injector) override readonly _injector: Injector, @Inject(ICommandService) private _commandService: ICommandService ) { - super(SHEET_CONDITIONAL_FORMATTING_PLUGIN); + super(); this._initCommand(); } diff --git a/packages/sheets-conditional-formatting/src/services/__test__/test.util.ts b/packages/sheets-conditional-formatting/src/services/__test__/test.util.ts index b18e4ebdaa..287cf1ec2a 100644 --- a/packages/sheets-conditional-formatting/src/services/__test__/test.util.ts +++ b/packages/sheets-conditional-formatting/src/services/__test__/test.util.ts @@ -15,7 +15,7 @@ */ import type { IWorkbookData } from '@univerjs/core'; -import { ICommandService, IUniverInstanceService, LocaleType, Plugin, PluginType, Univer } from '@univerjs/core'; +import { ICommandService, IUniverInstanceService, LocaleType, Plugin, Univer, UniverInstanceType } from '@univerjs/core'; import { SheetInterceptorService, } from '@univerjs/sheets'; @@ -60,13 +60,14 @@ export const createTestBed = (dependencies?: Dependency[]) => { const get = injector.get.bind(injector); class TestPlugin extends Plugin { - static override type = PluginType.Sheet; + static override pluginName = 'test-plugin'; + static override type = UniverInstanceType.SHEET; constructor( _config: undefined, @Inject(Injector) override readonly _injector: Injector ) { - super('test-plugin'); + super(); } override onStarting(injector: Injector): void { diff --git a/packages/sheets-data-validation/src/plugin.ts b/packages/sheets-data-validation/src/plugin.ts index 796736280f..ad5ffc4d3a 100644 --- a/packages/sheets-data-validation/src/plugin.ts +++ b/packages/sheets-data-validation/src/plugin.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { ICommandService, LocaleService, Plugin } from '@univerjs/core'; +import { ICommandService, LocaleService, Plugin, UniverInstanceType } from '@univerjs/core'; import { type Dependency, Inject, Injector } from '@wendellhu/redi'; import { DataValidationRenderController } from './controllers/dv-render.controller'; import { DataValidationController } from './controllers/dv.controller'; @@ -37,13 +37,16 @@ import { DataValidationRejectInputController } from './controllers/dv-reject-inp import { DataValidationPanelService } from './services/data-validation-panel.service'; export class UniverSheetsDataValidationPlugin extends Plugin { + static override pluginName = DATA_VALIDATION_PLUGIN_NAME; + static override type = UniverInstanceType.SHEET; + constructor( _config: unknown, @Inject(Injector) protected _injector: Injector, @ICommandService private readonly _commandService: ICommandService, @Inject(LocaleService) private readonly _localeService: LocaleService ) { - super(DATA_VALIDATION_PLUGIN_NAME); + super(); } override onStarting(injector: Injector) { diff --git a/packages/sheets-find-replace/src/plugin.ts b/packages/sheets-find-replace/src/plugin.ts index 3b3c4cd83a..96bbc9a8c3 100644 --- a/packages/sheets-find-replace/src/plugin.ts +++ b/packages/sheets-find-replace/src/plugin.ts @@ -14,24 +14,26 @@ * limitations under the License. */ -import { Plugin, PluginType } from '@univerjs/core'; +import { Plugin, PluginService, UniverInstanceType } from '@univerjs/core'; import type { Dependency } from '@wendellhu/redi'; import { Inject, Injector } from '@wendellhu/redi'; - +import { UniverFindReplacePlugin } from '@univerjs/find-replace'; import { SheetsFindReplaceController } from './controllers/sheet-find-replace.controller'; -export interface IFindPluginConfig {} - const NAME = 'UNIVER_SHEETS_FIND_REPLACE_PLUGIN'; export class UniverSheetsFindReplacePlugin extends Plugin { - static override type = PluginType.Sheet; + static override pluginName = NAME; + static override type = UniverInstanceType.SHEET; constructor( - _config: Partial, - @Inject(Injector) protected readonly _injector: Injector + _config: unknown, + @Inject(Injector) protected readonly _injector: Injector, + @Inject(PluginService) private readonly _pluginService: PluginService ) { - super(NAME); + super(); + + this._pluginService.registerPlugin(UniverFindReplacePlugin); } override onStarting(injector: Injector): void { diff --git a/packages/sheets-formula/README-zh.md b/packages/sheets-formula/README-zh.md index 60e62932bf..a87e58d75e 100644 --- a/packages/sheets-formula/README-zh.md +++ b/packages/sheets-formula/README-zh.md @@ -617,7 +617,7 @@ Uniscript 底层使用了 `@univerjs/facade`,你也可以直接在项目中使 首先 `plugin.ts` 中就不需要注册 `CustomFunctionController` 了,同级目录新建一个 `custom-function-plugin.ts`,专门用于注册 `CustomFunctionController`。 ```ts -import { Plugin, PluginType } from '@univerjs/core'; +import { Plugin, UniverInstanceType } from '@univerjs/core'; import type { Dependency } from '@wendellhu/redi'; import { Inject, Injector } from '@wendellhu/redi'; @@ -625,10 +625,11 @@ import { FORMULA_UI_PLUGIN_NAME } from './common/plugin-name'; import { CustomFunctionController } from './controllers/custom-function.controller'; export class UniverSheetsCustomFunctionPlugin extends Plugin { - static override type = PluginType.Sheet; + static override pluginName = FORMULA_UI_PLUGIN_NAME + static override type = UniverInstanceType.SHEET; constructor(@Inject(Injector) override readonly _injector: Injector) { - super(FORMULA_UI_PLUGIN_NAME); + super(); } initialize(): void { diff --git a/packages/sheets-formula/README.md b/packages/sheets-formula/README.md index 179ec20291..1b76ae4ea3 100644 --- a/packages/sheets-formula/README.md +++ b/packages/sheets-formula/README.md @@ -617,7 +617,7 @@ In addition to registering through the `UniverFormulaEnginePlugin` configuration First of all, there is no need to register `CustomFunctionController` in `plugin.ts`. Create a new `custom-function-plugin.ts` in the same directory, specifically for registering `CustomFunctionController`. ```ts -import { Plugin, PluginType } from '@univerjs/core'; +import { Plugin, UniverInstanceType } from '@univerjs/core'; import type { Dependency } from '@wendellhu/redi'; import { Inject, Injector } from '@wendellhu/redi'; @@ -625,10 +625,11 @@ import { FORMULA_UI_PLUGIN_NAME } from './common/plugin-name'; import { CustomFunctionController } from './controllers/custom-function.controller'; export class UniverSheetsCustomFunctionPlugin extends Plugin { - static override type = PluginType.Sheet; + static override pluginName = FORMULA_UI_PLUGIN_NAME + static override type = UniverInstanceType.SHEET; constructor(@Inject(Injector) override readonly _injector: Injector) { - super(FORMULA_UI_PLUGIN_NAME); + super(); } initialize(): void { diff --git a/packages/sheets-formula/src/commands/operations/__tests__/create-command-test-bed.ts b/packages/sheets-formula/src/commands/operations/__tests__/create-command-test-bed.ts index 011e30b827..d68525b5f3 100644 --- a/packages/sheets-formula/src/commands/operations/__tests__/create-command-test-bed.ts +++ b/packages/sheets-formula/src/commands/operations/__tests__/create-command-test-bed.ts @@ -15,7 +15,7 @@ */ import type { IWorkbookData } from '@univerjs/core'; -import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, PluginType, Univer } from '@univerjs/core'; +import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, Univer, UniverInstanceType } from '@univerjs/core'; import { LexerTreeBuilder } from '@univerjs/engine-formula'; import { SelectionManagerService } from '@univerjs/sheets'; import type { Dependency } from '@wendellhu/redi'; @@ -54,13 +54,14 @@ export function createCommandTestBed(workbookConfig?: IWorkbookData, dependencie * This plugin hooks into Sheet's DI system to expose API to test scripts */ class TestPlugin extends Plugin { - static override type = PluginType.Sheet; + static override pluginName = 'test-plugin'; + static override type = UniverInstanceType.SHEET; constructor( _config: undefined, @Inject(Injector) override readonly _injector: Injector ) { - super('test-plugin'); + super(); this._injector = _injector; } diff --git a/packages/sheets-formula/src/controllers/__tests__/create-command-test-bed.ts b/packages/sheets-formula/src/controllers/__tests__/create-command-test-bed.ts index 6361216b4f..a5cbeab5dc 100644 --- a/packages/sheets-formula/src/controllers/__tests__/create-command-test-bed.ts +++ b/packages/sheets-formula/src/controllers/__tests__/create-command-test-bed.ts @@ -15,7 +15,7 @@ */ import type { IWorkbookData, Workbook } from '@univerjs/core'; -import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, PluginType, Univer } from '@univerjs/core'; +import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, Univer, UniverInstanceType } from '@univerjs/core'; import { CalculateFormulaService, DefinedNamesService, FormulaCurrentConfigService, FormulaDataModel, FormulaRuntimeService, IDefinedNamesService, IFormulaCurrentConfigService, IFormulaRuntimeService, LexerTreeBuilder } from '@univerjs/engine-formula'; import { SelectionManagerService, SheetInterceptorService } from '@univerjs/sheets'; import type { Dependency } from '@wendellhu/redi'; @@ -62,7 +62,8 @@ export function createCommandTestBed(workbookConfig?: IWorkbookData, dependencie const get = injector.get.bind(injector); class TestPlugin extends Plugin { - static override type = PluginType.Sheet; + static override pluginName = 'test-plugin'; + static override type = UniverInstanceType.SHEET; private _formulaDataModel: FormulaDataModel | null = null; @@ -70,7 +71,7 @@ export function createCommandTestBed(workbookConfig?: IWorkbookData, dependencie _config: undefined, @Inject(Injector) override readonly _injector: Injector ) { - super('test-plugin'); + super(); } override onStarting(injector: Injector): void { diff --git a/packages/sheets-formula/src/formula-ui-plugin.ts b/packages/sheets-formula/src/formula-ui-plugin.ts index 4569a82497..1d75f7de1a 100644 --- a/packages/sheets-formula/src/formula-ui-plugin.ts +++ b/packages/sheets-formula/src/formula-ui-plugin.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { IUniverInstanceService, LocaleService, Plugin, PluginType } from '@univerjs/core'; +import { LocaleService, Plugin, UniverInstanceType } from '@univerjs/core'; import type { BaseFunction, IFunctionInfo, IFunctionNames } from '@univerjs/engine-formula'; import type { Ctor, Dependency } from '@wendellhu/redi'; import { Inject, Injector } from '@wendellhu/redi'; @@ -50,15 +50,15 @@ interface IFormulaUIConfig { function: Array<[Ctor, IFunctionNames]>; } export class UniverSheetsFormulaPlugin extends Plugin { - static override type = PluginType.Sheet; + static override pluginName = FORMULA_UI_PLUGIN_NAME; + static override type = UniverInstanceType.SHEET; constructor( private _config: Partial, @Inject(Injector) override readonly _injector: Injector, - @Inject(LocaleService) private readonly _localeService: LocaleService, - @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService + @Inject(LocaleService) private readonly _localeService: LocaleService ) { - super(FORMULA_UI_PLUGIN_NAME); + super(); } initialize(): void { diff --git a/packages/sheets-numfmt/src/controllers/__tests__/test.util.ts b/packages/sheets-numfmt/src/controllers/__tests__/test.util.ts index 03478ff7c9..cedb1bb142 100644 --- a/packages/sheets-numfmt/src/controllers/__tests__/test.util.ts +++ b/packages/sheets-numfmt/src/controllers/__tests__/test.util.ts @@ -15,7 +15,7 @@ */ import type { IWorkbookData } from '@univerjs/core'; -import { ICommandService, IUniverInstanceService, LocaleType, Plugin, PluginType, Univer } from '@univerjs/core'; +import { ICommandService, IUniverInstanceService, LocaleType, Plugin, Univer, UniverInstanceType } from '@univerjs/core'; import { LexerTreeBuilder } from '@univerjs/engine-formula'; import { INumfmtService, @@ -53,13 +53,14 @@ export const createTestBed = (dependencies?: Dependency[]) => { const get = injector.get.bind(injector); class TestPlugin extends Plugin { - static override type = PluginType.Sheet; + static override pluginName = 'test-plugin'; + static override type = UniverInstanceType.SHEET; constructor( _config: undefined, @Inject(Injector) override readonly _injector: Injector ) { - super('test-plugin'); + super(); } override onStarting(injector: Injector): void { diff --git a/packages/sheets-numfmt/src/numfmt-plugin.ts b/packages/sheets-numfmt/src/numfmt-plugin.ts index fc660097d9..111878669a 100644 --- a/packages/sheets-numfmt/src/numfmt-plugin.ts +++ b/packages/sheets-numfmt/src/numfmt-plugin.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { Plugin, PluginType } from '@univerjs/core'; +import { Plugin, UniverInstanceType } from '@univerjs/core'; import { Inject, Injector } from '@wendellhu/redi'; import { SHEET_NUMFMT_PLUGIN } from './base/const/PLUGIN_NAME'; @@ -31,13 +31,14 @@ import { INumfmtController } from './controllers/type'; import { UserHabitController } from './controllers/user-habit.controller'; export class UniverSheetsNumfmtPlugin extends Plugin { - static override type = PluginType.Sheet; + static override pluginName = SHEET_NUMFMT_PLUGIN; + static override type = UniverInstanceType.SHEET; constructor( _config: unknown, @Inject(Injector) override readonly _injector: Injector ) { - super(SHEET_NUMFMT_PLUGIN); + super(); } override onStarting(): void { diff --git a/packages/sheets-ui/src/commands/commands/__tests__/create-command-test-bed.ts b/packages/sheets-ui/src/commands/commands/__tests__/create-command-test-bed.ts index 78381d1501..ae22a5b427 100644 --- a/packages/sheets-ui/src/commands/commands/__tests__/create-command-test-bed.ts +++ b/packages/sheets-ui/src/commands/commands/__tests__/create-command-test-bed.ts @@ -23,8 +23,8 @@ import { LocaleType, LogLevel, Plugin, - PluginType, Univer, + UniverInstanceType, } from '@univerjs/core'; import { BorderStyleManagerService, SelectionManagerService, SheetInterceptorService } from '@univerjs/sheets'; import type { Dependency } from '@wendellhu/redi'; @@ -81,13 +81,14 @@ export function createCommandTestBed(workbookConfig?: IWorkbookData, dependencie const injector = univer.__getInjector(); class TestPlugin extends Plugin { - static override type = PluginType.Sheet; + static override pluginName = 'test-plugin'; + static override type = UniverInstanceType.SHEET; constructor( _config: undefined, @Inject(Injector) override readonly _injector: Injector ) { - super('test-plugin'); + super(); this._injector = _injector; } diff --git a/packages/sheets-ui/src/controllers/editor/__tests__/create-test-bed.ts b/packages/sheets-ui/src/controllers/editor/__tests__/create-test-bed.ts index 927514cd86..d5f93906e1 100644 --- a/packages/sheets-ui/src/controllers/editor/__tests__/create-test-bed.ts +++ b/packages/sheets-ui/src/controllers/editor/__tests__/create-test-bed.ts @@ -15,7 +15,7 @@ */ import type { IWorkbookData, Workbook } from '@univerjs/core'; -import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, PluginType, Univer } from '@univerjs/core'; +import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, Univer, UniverInstanceType } from '@univerjs/core'; import { SelectionManagerService, SheetInterceptorService } from '@univerjs/sheets'; import type { Dependency } from '@wendellhu/redi'; import { Inject, Injector } from '@wendellhu/redi'; @@ -48,13 +48,14 @@ export function createTestBed(workbookConfig?: IWorkbookData, dependencies?: Dep const injector = univer.__getInjector(); class TestPlugin extends Plugin { - static override type = PluginType.Sheet; + static override pluginName = 'test-plugin'; + static override type = UniverInstanceType.SHEET; constructor( _config: undefined, @Inject(Injector) override readonly _injector: Injector ) { - super('test-plugin'); + super(); this._injector = _injector; } diff --git a/packages/sheets-ui/src/controllers/menu/__tests__/create-menu-test-bed.ts b/packages/sheets-ui/src/controllers/menu/__tests__/create-menu-test-bed.ts index a00e5181c7..84f821688f 100644 --- a/packages/sheets-ui/src/controllers/menu/__tests__/create-menu-test-bed.ts +++ b/packages/sheets-ui/src/controllers/menu/__tests__/create-menu-test-bed.ts @@ -15,7 +15,7 @@ */ import type { IWorkbookData } from '@univerjs/core'; -import { LocaleType, Plugin, PluginType, Univer } from '@univerjs/core'; +import { LocaleType, Plugin, Univer, UniverInstanceType } from '@univerjs/core'; import { SelectionManagerService, SheetInterceptorService, SheetPermissionService } from '@univerjs/sheets'; import { DesktopMenuService, DesktopShortcutService, IMenuService, IShortcutService } from '@univerjs/ui'; import { Inject, Injector } from '@wendellhu/redi'; @@ -50,12 +50,13 @@ export function createMenuTestBed() { * This plugin hooks into Sheet's DI system to expose API to test scripts */ class TestPlugin extends Plugin { + static override pluginName = 'test-plugin'; protected override _injector: Injector; - static override type = PluginType.Sheet; + static override type = UniverInstanceType.SHEET; constructor(_config: unknown, @Inject(Injector) _injector: Injector) { - super('test-plugin'); + super(); this._injector = _injector; get = this._injector.get.bind(this._injector); @@ -68,10 +69,6 @@ export function createMenuTestBed() { injector.add([SheetPermissionService]); injector.add([SheetInterceptorService]); } - - override onDestroy(): void { - get = null; - } } univer.registerPlugin(TestPlugin); diff --git a/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-test-bed.ts b/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-test-bed.ts index 04468cbef8..f117fb042f 100644 --- a/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-test-bed.ts +++ b/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-test-bed.ts @@ -15,7 +15,7 @@ */ import type { IWorkbookData } from '@univerjs/core'; -import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, PluginType, Univer } from '@univerjs/core'; +import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, Univer, UniverInstanceType } from '@univerjs/core'; import { IRenderManagerService, RenderManagerService } from '@univerjs/engine-render'; import { SelectionManagerService, SheetInterceptorService } from '@univerjs/sheets'; import { @@ -520,13 +520,14 @@ export function clipboardTestBed(workbookConfig?: IWorkbookData, dependencies?: * This plugin hooks into Sheet's DI system to expose API to test scripts */ class TestPlugin extends Plugin { - static override type = PluginType.Sheet; + static override pluginName = 'test-plugin'; + static override type = UniverInstanceType.SHEET; constructor( _config: undefined, @Inject(Injector) override readonly _injector: Injector ) { - super('test-plugin'); + super(); } override onStarting(injector: Injector): void { diff --git a/packages/sheets-ui/src/services/selection/__test__/create-service-test-bed.ts b/packages/sheets-ui/src/services/selection/__test__/create-service-test-bed.ts index e64956a247..13d7d064e2 100644 --- a/packages/sheets-ui/src/services/selection/__test__/create-service-test-bed.ts +++ b/packages/sheets-ui/src/services/selection/__test__/create-service-test-bed.ts @@ -21,8 +21,8 @@ import { IUniverInstanceService, LogLevel, Plugin, - PluginType, Univer, + UniverInstanceType, } from '@univerjs/core'; import type { Dependency } from '@wendellhu/redi'; import { Inject, Injector } from '@wendellhu/redi'; @@ -109,14 +109,14 @@ export function createCommandTestBed(workbookConfig?: IWorkbookData, dependencie const injector = univer.__getInjector(); class TestPlugin extends Plugin { - static override type = PluginType.Sheet; + static override pluginName = 'test-plugin'; + static override type = UniverInstanceType.SHEET; constructor( _config: undefined, @Inject(Injector) override readonly _injector: Injector ) { - super('test-plugin'); - + super(); this._injector = _injector; } diff --git a/packages/sheets-ui/src/sheets-ui-plugin.ts b/packages/sheets-ui/src/sheets-ui-plugin.ts index d131048585..e240f9f8e8 100644 --- a/packages/sheets-ui/src/sheets-ui-plugin.ts +++ b/packages/sheets-ui/src/sheets-ui-plugin.ts @@ -15,12 +15,11 @@ */ import type { Workbook } from '@univerjs/core'; -import { IUniverInstanceService, LocaleService, Plugin, PluginType, UniverInstanceType } from '@univerjs/core'; +import { IUniverInstanceService, LocaleService, Plugin, UniverInstanceType } from '@univerjs/core'; import type { Dependency } from '@wendellhu/redi'; import { Inject, Injector } from '@wendellhu/redi'; import { filter } from 'rxjs/operators'; -import { SHEET_UI_PLUGIN_NAME } from './basics'; import { ActiveWorksheetController } from './controllers/active-worksheet/active-worksheet.controller'; import { AutoFillController } from './controllers/auto-fill.controller'; import { AutoHeightController } from './controllers/auto-height.controller'; @@ -71,7 +70,8 @@ import { CellCustomRenderController } from './controllers/cell-custom-render.con import { SheetCanvasPopManagerService } from './services/canvas-pop-manager.service'; export class UniverSheetsUIPlugin extends Plugin { - static override type = PluginType.Sheet; + static override pluginName = 'SHEET_UI_PLUGIN_NAME'; + static override type = UniverInstanceType.SHEET; constructor( config: undefined, @@ -79,7 +79,7 @@ export class UniverSheetsUIPlugin extends Plugin { @Inject(LocaleService) private readonly _localeService: LocaleService, @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService ) { - super(SHEET_UI_PLUGIN_NAME); + super(); this._localeService.load({ zhCN, diff --git a/packages/sheets-zen-editor/src/plugin.ts b/packages/sheets-zen-editor/src/plugin.ts index 9622196f68..3b7f158bca 100644 --- a/packages/sheets-zen-editor/src/plugin.ts +++ b/packages/sheets-zen-editor/src/plugin.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { LocaleService, Plugin, PluginType } from '@univerjs/core'; +import { LocaleService, Plugin, UniverInstanceType } from '@univerjs/core'; import type { Dependency } from '@wendellhu/redi'; import { Inject, Injector } from '@wendellhu/redi'; @@ -25,14 +25,16 @@ import { zhCN } from './locale'; export interface IUniverSheetsZenEditorPluginConfig {} export class UniverSheetsZenEditorPlugin extends Plugin { - static override type = PluginType.Doc; + static override pluginName = 'zen-editor'; + static override type = UniverInstanceType.DOC; constructor( - config: IUniverSheetsZenEditorPluginConfig, + _config: IUniverSheetsZenEditorPluginConfig, @Inject(Injector) override readonly _injector: Injector, @Inject(LocaleService) private readonly _localeService: LocaleService ) { - super('zen-editor'); + super(); + this._initialize(); this._initializeDependencies(this._injector); } @@ -52,8 +54,4 @@ export class UniverSheetsZenEditorPlugin extends Plugin { dependencies.forEach((dependency) => injector.add(dependency)); } - - override onRendered(): void {} - - override onDestroy(): void {} } diff --git a/packages/sheets/src/commands/commands/__tests__/create-command-test-bed.ts b/packages/sheets/src/commands/commands/__tests__/create-command-test-bed.ts index c9493adf5a..ce4a1fcd09 100644 --- a/packages/sheets/src/commands/commands/__tests__/create-command-test-bed.ts +++ b/packages/sheets/src/commands/commands/__tests__/create-command-test-bed.ts @@ -23,9 +23,9 @@ import { LocaleType, LogLevel, Plugin, - PluginType, Tools, Univer, + UniverInstanceType, } from '@univerjs/core'; import type { Dependency } from '@wendellhu/redi'; import { Inject, Injector } from '@wendellhu/redi'; @@ -81,13 +81,14 @@ export function createCommandTestBed(workbookConfig?: IWorkbookData, dependencie const injector = univer.__getInjector(); class TestPlugin extends Plugin { - static override type = PluginType.Sheet; + static override pluginName = 'test-plugin'; + static override type = UniverInstanceType.SHEET; constructor( _config: undefined, @Inject(Injector) override readonly _injector: Injector ) { - super('test-plugin'); + super(); this._injector = _injector; } diff --git a/packages/sheets/src/commands/mutations/__tests__/create-command-test-bed.ts b/packages/sheets/src/commands/mutations/__tests__/create-command-test-bed.ts index e54d0dd695..b5238cbf8b 100644 --- a/packages/sheets/src/commands/mutations/__tests__/create-command-test-bed.ts +++ b/packages/sheets/src/commands/mutations/__tests__/create-command-test-bed.ts @@ -21,8 +21,8 @@ import { IUniverInstanceService, LogLevel, Plugin, - PluginType, Univer, + UniverInstanceType, } from '@univerjs/core'; import type { Dependency } from '@wendellhu/redi'; import { Inject, Injector } from '@wendellhu/redi'; @@ -109,13 +109,14 @@ export function createCommandTestBed(workbookConfig?: IWorkbookData, dependencie const injector = univer.__getInjector(); class TestPlugin extends Plugin { - static override type = PluginType.Sheet; + static override pluginName = 'test-plugin'; + static override type = UniverInstanceType.SHEET; constructor( _config: undefined, @Inject(Injector) override readonly _injector: Injector ) { - super('test-plugin'); + super(); this._injector = _injector; } diff --git a/packages/sheets/src/controllers/__tests__/util.ts b/packages/sheets/src/controllers/__tests__/util.ts index 9bc5d1e0dc..028dea3d0a 100644 --- a/packages/sheets/src/controllers/__tests__/util.ts +++ b/packages/sheets/src/controllers/__tests__/util.ts @@ -15,7 +15,7 @@ */ import type { IWorkbookData } from '@univerjs/core'; -import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, PluginType, Univer } from '@univerjs/core'; +import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, Univer, UniverInstanceType } from '@univerjs/core'; import type { Dependency } from '@wendellhu/redi'; import { Inject, Injector } from '@wendellhu/redi'; @@ -52,13 +52,14 @@ export function createTestBase(workbookConfig?: IWorkbookData, dependencies?: De * This plugin hooks into Sheet's DI system to expose API to test scripts */ class TestPlugin extends Plugin { - static override type = PluginType.Sheet; + static override pluginName = 'test-plugin'; + static override type = UniverInstanceType.SHEET; constructor( _config: undefined, @Inject(Injector) override readonly _injector: Injector ) { - super('test-plugin'); + super(); } override onStarting(injector: Injector): void { diff --git a/packages/sheets/src/services/__tests__/util.ts b/packages/sheets/src/services/__tests__/util.ts index a35a8d36cb..31df6c8fe4 100644 --- a/packages/sheets/src/services/__tests__/util.ts +++ b/packages/sheets/src/services/__tests__/util.ts @@ -15,7 +15,7 @@ */ import type { IWorkbookData } from '@univerjs/core'; -import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, PluginType, Univer } from '@univerjs/core'; +import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, Univer, UniverInstanceType } from '@univerjs/core'; import type { Dependency } from '@wendellhu/redi'; import { Inject, Injector } from '@wendellhu/redi'; @@ -66,13 +66,14 @@ export function createTestBase(workbookConfig?: IWorkbookData, dependencies?: De * This plugin hooks into Sheet's DI system to expose API to test scripts */ class TestPlugin extends Plugin { - static override type = PluginType.Sheet; + static override pluginName = 'test-plugin'; + static override type = UniverInstanceType.SHEET; constructor( _config: undefined, @Inject(Injector) override readonly _injector: Injector ) { - super('test-plugin'); + super(); } override onStarting(injector: Injector): void { diff --git a/packages/sheets/src/services/ref-range/__tests__/__testing__.ts b/packages/sheets/src/services/ref-range/__tests__/__testing__.ts index a35a8d36cb..31df6c8fe4 100644 --- a/packages/sheets/src/services/ref-range/__tests__/__testing__.ts +++ b/packages/sheets/src/services/ref-range/__tests__/__testing__.ts @@ -15,7 +15,7 @@ */ import type { IWorkbookData } from '@univerjs/core'; -import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, PluginType, Univer } from '@univerjs/core'; +import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, Univer, UniverInstanceType } from '@univerjs/core'; import type { Dependency } from '@wendellhu/redi'; import { Inject, Injector } from '@wendellhu/redi'; @@ -66,13 +66,14 @@ export function createTestBase(workbookConfig?: IWorkbookData, dependencies?: De * This plugin hooks into Sheet's DI system to expose API to test scripts */ class TestPlugin extends Plugin { - static override type = PluginType.Sheet; + static override pluginName = 'test-plugin'; + static override type = UniverInstanceType.SHEET; constructor( _config: undefined, @Inject(Injector) override readonly _injector: Injector ) { - super('test-plugin'); + super(); } override onStarting(injector: Injector): void { diff --git a/packages/sheets/src/services/ref-range/__tests__/ref-range.service.spec.ts b/packages/sheets/src/services/ref-range/__tests__/ref-range.service.spec.ts index f67b70cdc9..8744061ef2 100644 --- a/packages/sheets/src/services/ref-range/__tests__/ref-range.service.spec.ts +++ b/packages/sheets/src/services/ref-range/__tests__/ref-range.service.spec.ts @@ -16,7 +16,7 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import type { ICommand, IRange, IWorkbookData, Nullable } from '@univerjs/core'; -import { ICommandService, ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, PluginType, Univer } from '@univerjs/core'; +import { ICommandService, ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, Univer, UniverInstanceType } from '@univerjs/core'; import type { Dependency } from '@wendellhu/redi'; import { Inject, Injector } from '@wendellhu/redi'; @@ -61,13 +61,14 @@ export function createRefRangeTestBed() { const get = injector.get.bind(injector); class TestPlugin extends Plugin { - static override type = PluginType.Sheet; + static override pluginName = 'test-plugin'; + static override type = UniverInstanceType.SHEET; constructor( _config: undefined, @Inject(Injector) override readonly _injector: Injector ) { - super('test-plugin'); + super(); } } diff --git a/packages/sheets/src/services/sheet-interceptor/__tests__/create-core-test-bed.ts b/packages/sheets/src/services/sheet-interceptor/__tests__/create-core-test-bed.ts index f6eae94c0a..6da8913ec1 100644 --- a/packages/sheets/src/services/sheet-interceptor/__tests__/create-core-test-bed.ts +++ b/packages/sheets/src/services/sheet-interceptor/__tests__/create-core-test-bed.ts @@ -15,7 +15,7 @@ */ import type { IWorkbookData } from '@univerjs/core'; -import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, PluginType, Univer } from '@univerjs/core'; +import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, Univer, UniverInstanceType } from '@univerjs/core'; import type { Dependency } from '@wendellhu/redi'; import { Inject, Injector } from '@wendellhu/redi'; @@ -49,13 +49,14 @@ export function createCoreTestBed(workbookConfig?: IWorkbookData, dependencies?: const get = injector.get.bind(injector); class TestPlugin extends Plugin { - static override type = PluginType.Sheet; + static override pluginName = 'test-plugin'; + static override type = UniverInstanceType.SHEET; constructor( _config: undefined, @Inject(Injector) override readonly _injector: Injector ) { - super('test-spy-plugin'); + super(); } override onStarting(injector: Injector): void { diff --git a/packages/sheets/src/sheets-plugin.ts b/packages/sheets/src/sheets-plugin.ts index fbae4be929..8d22654c27 100644 --- a/packages/sheets/src/sheets-plugin.ts +++ b/packages/sheets/src/sheets-plugin.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { ICommandService, LocaleService, Plugin, PluginType } from '@univerjs/core'; +import { ICommandService, LocaleService, Plugin, UniverInstanceType } from '@univerjs/core'; import type { Dependency } from '@wendellhu/redi'; import { Inject, Injector } from '@wendellhu/redi'; @@ -32,7 +32,7 @@ import { SelectionManagerService } from './services/selection-manager.service'; import { SheetInterceptorService } from './services/sheet-interceptor/sheet-interceptor.service'; import { DefinedNameDataController } from './controllers/defined-name-data.controller'; -const PLUGIN_NAME = 'sheet'; +const PLUGIN_NAME = 'sheets'; export interface IUniverSheetsConfig { notExecuteFormula?: boolean; @@ -42,7 +42,8 @@ export interface IUniverSheetsConfig { * The main sheet base, construct the sheet container and layout, mount the rendering engine */ export class UniverSheetsPlugin extends Plugin { - static override type = PluginType.Sheet; + static override pluginName = PLUGIN_NAME; + static override type = UniverInstanceType.SHEET; constructor( private _config: IUniverSheetsConfig, @@ -50,7 +51,7 @@ export class UniverSheetsPlugin extends Plugin { @Inject(LocaleService) private readonly _localeService: LocaleService, @Inject(Injector) override readonly _injector: Injector ) { - super(PLUGIN_NAME); + super(); this._initializeDependencies(_injector); } diff --git a/packages/slides-ui/src/slides-ui-plugin.ts b/packages/slides-ui/src/slides-ui-plugin.ts index eb8b2a8c89..ddfd94b2af 100644 --- a/packages/slides-ui/src/slides-ui-plugin.ts +++ b/packages/slides-ui/src/slides-ui-plugin.ts @@ -15,7 +15,7 @@ */ import type { SlideDataModel } from '@univerjs/core'; -import { IUniverInstanceService, LocaleService, Plugin, PluginType, UniverInstanceType } from '@univerjs/core'; +import { IUniverInstanceService, LocaleService, Plugin, UniverInstanceType } from '@univerjs/core'; import type { Dependency } from '@wendellhu/redi'; import { Inject, Injector } from '@wendellhu/redi'; @@ -25,7 +25,8 @@ import { zhCN } from './locale'; export const SLIDE_UI_PLUGIN_NAME = 'slides-ui'; export class UniverSlidesUIPlugin extends Plugin { - static override type = PluginType.Slide; + static override pluginName = SLIDE_UI_PLUGIN_NAME; + static override type = UniverInstanceType.SLIDE; constructor( _config: unknown, @@ -33,7 +34,7 @@ export class UniverSlidesUIPlugin extends Plugin { @Inject(LocaleService) private readonly _localeService: LocaleService, @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService ) { - super(SLIDE_UI_PLUGIN_NAME); + super(); } override onStarting(injector: Injector): void { diff --git a/packages/slides/src/slides-plugin.ts b/packages/slides/src/slides-plugin.ts index af56f3022e..2fa95cb95e 100644 --- a/packages/slides/src/slides-plugin.ts +++ b/packages/slides/src/slides-plugin.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { IUniverInstanceService, LocaleService, Plugin, PluginType } from '@univerjs/core'; +import { IUniverInstanceService, LocaleService, Plugin, UniverInstanceType } from '@univerjs/core'; import type { Engine } from '@univerjs/engine-render'; import { IRenderingEngine } from '@univerjs/engine-render'; import type { Dependency } from '@wendellhu/redi'; @@ -30,7 +30,8 @@ const DEFAULT_SLIDE_PLUGIN_DATA = {}; const PLUGIN_NAME = 'slides'; export class UniverSlidesPlugin extends Plugin { - static override type = PluginType.Slide; + static override pluginName = PLUGIN_NAME; + static override type = UniverInstanceType.SLIDE; private _config: IUniverSlidesConfig; @@ -44,7 +45,7 @@ export class UniverSlidesPlugin extends Plugin { @Inject(LocaleService) private readonly _localeService: LocaleService, @Inject(Injector) override readonly _injector: Injector ) { - super(PLUGIN_NAME); + super(); this._config = Object.assign(DEFAULT_SLIDE_PLUGIN_DATA, config); this._initializeDependencies(this._injector); @@ -69,10 +70,6 @@ export class UniverSlidesPlugin extends Plugin { this.initialize(); } - override onDestroy(): void { - super.onDestroy(); - } - getCanvasEngine() { return this._canvasEngine; } diff --git a/packages/ui/src/ui-plugin.ts b/packages/ui/src/ui-plugin.ts index 76bfc9f4f1..afaf5908f7 100644 --- a/packages/ui/src/ui-plugin.ts +++ b/packages/ui/src/ui-plugin.ts @@ -15,7 +15,7 @@ */ import type { IContextService } from '@univerjs/core'; -import { IConfigService, ILocalStorageService, LocaleService, Plugin, PluginType } from '@univerjs/core'; +import { IConfigService, ILocalStorageService, LocaleService, Plugin, UniverInstanceType } from '@univerjs/core'; import type { Dependency } from '@wendellhu/redi'; import { Inject, Injector } from '@wendellhu/redi'; @@ -68,7 +68,8 @@ export const DISABLE_AUTO_FOCUS_KEY = 'DISABLE_AUTO_FOCUS'; * UI plugin provides basic interaction with users. Including workbench (menus, UI parts, notifications etc.), copy paste, shortcut. */ export class UniverUIPlugin extends Plugin { - static override type = PluginType.Univer; + static override pluginName = PLUGIN_NAME; + static override type = UniverInstanceType.UNIVER; constructor( private _config: Partial = {}, @@ -76,11 +77,9 @@ export class UniverUIPlugin extends Plugin { @Inject(Injector) protected readonly _injector: Injector, @Inject(LocaleService) private readonly _localeService: LocaleService ) { - super(PLUGIN_NAME); + super(); - this._localeService.load({ - zhCN, - }); + this._localeService.load({ zhCN }); if (this._config.disableAutoFocus) { this._contextService.setContextValue(DISABLE_AUTO_FOCUS_KEY, true); diff --git a/packages/uniscript/src/plugin.ts b/packages/uniscript/src/plugin.ts index 3a02d374e7..5d2ec5622d 100644 --- a/packages/uniscript/src/plugin.ts +++ b/packages/uniscript/src/plugin.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { LocaleService, Plugin, PluginType } from '@univerjs/core'; +import { LocaleService, Plugin, UniverInstanceType } from '@univerjs/core'; import type { Dependency } from '@wendellhu/redi'; import { Inject, Injector } from '@wendellhu/redi'; @@ -30,14 +30,15 @@ const PLUGIN_NAME = 'uniscript'; export interface IUniscriptConfig extends IScriptEditorServiceConfig {} export class UniverUniscriptPlugin extends Plugin { - static override type = PluginType.Univer; + static override pluginName = PLUGIN_NAME; + static override type = UniverInstanceType.UNIVER; constructor( private readonly _config: IUniscriptConfig, @Inject(Injector) protected override _injector: Injector, @Inject(LocaleService) private readonly _localeService: LocaleService ) { - super(PLUGIN_NAME); + super(); } override onStarting(injector: Injector): void {