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: optimize component registration (#5245)
白熱1dfdf1b13aaf
diff --git a/packages/action-recorder/src/controllers/action-recorder.controller.tsx b/packages/action-recorder/src/controllers/action-recorder.controller.tsxindex 3b7f014b25..522c970dc0 100644--- a/packages/action-recorder/src/controllers/action-recorder.controller.tsx+++ b/packages/action-recorder/src/controllers/action-recorder.controller.tsx@@ -115,7 +115,9 @@ export class ActionRecorderController extends Disposable {private _initUI(): void {this._uiPartsSrv.registerComponent(BuiltInUIPart.GLOBAL, () => connectInjector(RecorderPanel, this._injector));- this._componentManager.register('RecordSingle', RecordSingle);+ this.disposeWithMe(+ this._componentManager.register('RecordSingle', RecordSingle)+ );this._menuManagerService.mergeMenu(menuSchema);}diff --git a/packages/core/src/facade/f-univer.ts b/packages/core/src/facade/f-univer.tsindex c1f9fa0aa5..632fbef3fb 100644--- a/packages/core/src/facade/f-univer.ts+++ b/packages/core/src/facade/f-univer.ts@@ -14,10 +14,10 @@* limitations under the License.*/-import type { CommandListener, DocumentDataModel, IDisposable, IDocumentData, IExecutionOptions, IParagraphStyle, ITextDecoration, ITextStyle, LifecycleStages } from '@univerjs/core';+import type { CommandListener, DocumentDataModel, IDisposable, IDocumentData, IExecutionOptions, ILanguagePack, IParagraphStyle, ITextDecoration, ITextStyle, LifecycleStages, LocaleType } from '@univerjs/core';import type { Subscription } from 'rxjs';import type { ICommandEvent, IEventParamConfig } from './f-event';-import { CanceledError, ColorBuilder, Disposable, ICommandService, Inject, Injector, IUniverInstanceService, LifecycleService, ParagraphStyleBuilder, ParagraphStyleValue, RedoCommand, RichTextBuilder, RichTextValue, TextDecorationBuilder, TextStyleBuilder, TextStyleValue, ThemeService, toDisposable, UndoCommand, Univer, UniverInstanceType } from '@univerjs/core';+import { CanceledError, ColorBuilder, Disposable, ICommandService, Inject, Injector, IUniverInstanceService, LifecycleService, LocaleService, ParagraphStyleBuilder, ParagraphStyleValue, RedoCommand, RichTextBuilder, RichTextValue, TextDecorationBuilder, TextStyleBuilder, TextStyleValue, ThemeService, toDisposable, UndoCommand, Univer, UniverInstanceType } from '@univerjs/core';import { FBlob } from './f-blob';import { FDoc } from './f-doc';import { FEnum } from './f-enum';@@ -331,6 +331,36 @@ export class FUniver extends Disposable {themeService.setDarkMode(isDarkMode);}+ /**+ * Load locales for the given locale.+ * @description This method is utilized to load locales, which can be either built-in or custom-defined.+ * @param {string} locale - A unique locale identifier.+ * @param {ILanguagePack} locales - The locales object containing the translations.+ * @example+ * ```ts+ * univerAPI.loadLocales('esES', {+ * 'Hello World': 'Hola Mundo',+ * });+ * ```+ */+ loadLocales(locale: string, locales: ILanguagePack): void {+ const localeService = this._injector.get(LocaleService);+ localeService.load({ [locale]: locales });+ }++ /**+ * Set the current locale.+ * @param {string} locale - A unique locale identifier.+ * @example+ * ```ts+ * univerAPI.setLocale('esES');+ * ```+ */+ setLocale(locale: string): void {+ const localeService = this._injector.get(LocaleService);+ localeService.setLocale(locale as LocaleType);+ }+/*** Register a callback that will be triggered before invoking a command.* @deprecated use `univerAPI.addEvent(univerAPI.Event.BeforeCommandExecute, (event) => {})` instead.diff --git a/packages/docs-hyper-link-ui/src/controllers/ui.controller.ts b/packages/docs-hyper-link-ui/src/controllers/ui.controller.tsindex de1441e4c9..d5ba971598 100644--- a/packages/docs-hyper-link-ui/src/controllers/ui.controller.ts+++ b/packages/docs-hyper-link-ui/src/controllers/ui.controller.ts@@ -52,7 +52,9 @@ export class DocHyperLinkUIController extends Disposable {[DocLinkPopup, DocLinkPopup.componentKey],[LinkSingle, DOC_LINK_ICON],] as const).forEach(([comp, key]) => {- this._componentManager.register(key, comp);+ this.disposeWithMe(+ this._componentManager.register(key, comp)+ );});}diff --git a/packages/docs-thread-comment-ui/src/controllers/doc-thread-comment-ui.controller.ts b/packages/docs-thread-comment-ui/src/controllers/doc-thread-comment-ui.controller.tsindex 8df6949d8e..53e58da281 100644--- a/packages/docs-thread-comment-ui/src/controllers/doc-thread-comment-ui.controller.ts+++ b/packages/docs-thread-comment-ui/src/controllers/doc-thread-comment-ui.controller.ts@@ -52,10 +52,13 @@ export class DocThreadCommentUIController extends Disposable {}private _initComponents() {- [DocThreadCommentPanel].forEach((comp) => {- this.disposeWithMe(this._componentManager.register(comp.componentKey, comp));+ ([+ [DocThreadCommentPanel.componentKey, DocThreadCommentPanel],+ ['CommentSingle', CommentSingle],+ ] as [string, React.FC][]).forEach(([id, component]) => {+ this.disposeWithMe(+ this._componentManager.register(id, component)+ );});-- this.disposeWithMe(this._componentManager.register('CommentSingle', CommentSingle));}}diff --git a/packages/find-replace/src/controllers/find-replace.controller.ts b/packages/find-replace/src/controllers/find-replace.controller.tsindex a035ebb077..2a24bb0160 100644--- a/packages/find-replace/src/controllers/find-replace.controller.ts+++ b/packages/find-replace/src/controllers/find-replace.controller.ts@@ -110,8 +110,14 @@ export class FindReplaceController extends RxDisposable {}private _initUI(): void {- this.disposeWithMe(this._componentManager.register('FindReplaceDialog', FindReplaceDialog));- this.disposeWithMe(this._componentManager.register('SearchIcon', SearchSingle));+ ([+ ['FindReplaceDialog', FindReplaceDialog],+ ['SearchIcon', SearchSingle],+ ] as [string, React.FC][]).forEach(([id, component]) => {+ this.disposeWithMe(+ this._componentManager.register(id, component)+ );+ });this._menuManagerService.mergeMenu(menuSchema);diff --git a/packages/sheets-conditional-formatting-ui/src/controllers/cf.panel.controller.ts b/packages/sheets-conditional-formatting-ui/src/controllers/cf.panel.controller.tsindex b62b8b3004..346f1d0f06 100644--- a/packages/sheets-conditional-formatting-ui/src/controllers/cf.panel.controller.ts+++ b/packages/sheets-conditional-formatting-ui/src/controllers/cf.panel.controller.ts@@ -68,6 +68,8 @@ export class ConditionalFormattingPanelController extends Disposable {}private _initPanel() {- this._componentManager.register(CF_PANEL_KEY, ConditionFormattingPanel);+ this.disposeWithMe(+ this._componentManager.register(CF_PANEL_KEY, ConditionFormattingPanel)+ );}}diff --git a/packages/sheets-filter-ui/src/controllers/sheets-filter-ui-desktop.controller.ts b/packages/sheets-filter-ui/src/controllers/sheets-filter-ui-desktop.controller.tsindex 5a5bd5e406..bcd0e96478 100644--- a/packages/sheets-filter-ui/src/controllers/sheets-filter-ui-desktop.controller.ts+++ b/packages/sheets-filter-ui/src/controllers/sheets-filter-ui-desktop.controller.ts@@ -94,8 +94,15 @@ export class SheetsFilterUIDesktopController extends SheetsFilterUIMobileControl}private _initUI(): void {- this.disposeWithMe(this._componentManager.register(FILTER_PANEL_POPUP_KEY, FilterPanel));- this.disposeWithMe(this._componentManager.register('FilterSingle', FilterSingle));+ ([+ [FILTER_PANEL_POPUP_KEY, FilterPanel],+ ['FilterSingle', FilterSingle],+ ] as [string, React.FC][]).forEach(([id, component]) => {+ this.disposeWithMe(+ this._componentManager.register(id, component)+ );+ });+this.disposeWithMe(this._contextService.subscribeContextValue$(FILTER_PANEL_OPENED_KEY).pipe(distinctUntilChanged()).subscribe((open) => {diff --git a/packages/sheets-note-ui/src/controllers/sheets-note-ui.controller.ts b/packages/sheets-note-ui/src/controllers/sheets-note-ui.controller.tsindex 44a8d42547..9abfe25b40 100644--- a/packages/sheets-note-ui/src/controllers/sheets-note-ui.controller.ts+++ b/packages/sheets-note-ui/src/controllers/sheets-note-ui.controller.ts@@ -36,10 +36,16 @@ export class SheetsNoteUIController extends Disposable {}private _initComponents() {- this.disposeWithMe(this._componentManager.register(SHEET_NOTE_COMPONENT, SheetsNote));- this.disposeWithMe(this._componentManager.register('AddNoteSingle', AddNoteSingle));- this.disposeWithMe(this._componentManager.register('DeleteNoteSingle', DeleteNoteSingle));- this.disposeWithMe(this._componentManager.register('HideNoteSingle', HideNoteSingle));+ ([+ [SHEET_NOTE_COMPONENT, SheetsNote],+ ['AddNoteSingle', AddNoteSingle],+ ['DeleteNoteSingle', DeleteNoteSingle],+ ['HideNoteSingle', HideNoteSingle],+ ] as [string, React.FC][]).forEach(([id, component]) => {+ this.disposeWithMe(+ this._componentManager.register(id, component)+ );+ });}private _initMenu() {diff --git a/packages/sheets-numfmt-ui/src/controllers/numfmt.controller.ts b/packages/sheets-numfmt-ui/src/controllers/numfmt.controller.tsindex d19231a991..dfb13abc59 100644--- a/packages/sheets-numfmt-ui/src/controllers/numfmt.controller.ts+++ b/packages/sheets-numfmt-ui/src/controllers/numfmt.controller.ts@@ -187,7 +187,9 @@ export class SheetNumfmtUIController extends Disposable {}private _initPanel() {- this._componentManager.register(SHEET_NUMFMT_PANEL, SheetNumfmtPanel);+ this.disposeWithMe(+ this._componentManager.register(SHEET_NUMFMT_PANEL, SheetNumfmtPanel)+ );}// eslint-disable-next-line max-lines-per-functiondiff --git a/packages/sheets-numfmt-ui/src/controllers/numfmt.menu.controller.ts b/packages/sheets-numfmt-ui/src/controllers/numfmt.menu.controller.tsindex 126b346f21..74bf707974 100644--- a/packages/sheets-numfmt-ui/src/controllers/numfmt.menu.controller.ts+++ b/packages/sheets-numfmt-ui/src/controllers/numfmt.menu.controller.ts@@ -33,7 +33,13 @@ export class NumfmtMenuController extends Disposable {private _initMenu() {this._menuManagerService.mergeMenu(menuSchema);- this.disposeWithMe((this._componentManager.register(MORE_NUMFMT_TYPE_KEY, MoreNumfmtType)));- this.disposeWithMe((this._componentManager.register(OPTIONS_KEY, Options)));+ ([+ [MORE_NUMFMT_TYPE_KEY, MoreNumfmtType],+ [OPTIONS_KEY, Options],+ ] as [string, React.FC][]).forEach(([id, component]) => {+ this.disposeWithMe(+ this._componentManager.register(id, component)+ );+ });}}diff --git a/packages/sheets-sort-ui/src/controllers/sheets-sort-ui.controller.ts b/packages/sheets-sort-ui/src/controllers/sheets-sort-ui.controller.tsindex 37c3afa680..f35f28dc98 100644--- a/packages/sheets-sort-ui/src/controllers/sheets-sort-ui.controller.ts+++ b/packages/sheets-sort-ui/src/controllers/sheets-sort-ui.controller.ts@@ -76,13 +76,22 @@ export class SheetsSortUIController extends RxDisposable {}private _initUI(): void {- this.disposeWithMe(this._componentManager.register('CustomSortPanel', CustomSortPanel));- this.disposeWithMe(this._uiPartsService.registerComponent(SheetsUIPart.FILTER_PANEL_EMBED_POINT, () => connectInjector(EmbedSortBtn, this._injector)));- this.disposeWithMe(this._componentManager.register(SHEETS_SORT_ASC_ICON, AscendingSingle));- this.disposeWithMe(this._componentManager.register(SHEETS_SORT_ASC_EXT_ICON, ExpandAscendingSingle));- this.disposeWithMe(this._componentManager.register(SHEETS_SORT_DESC_ICON, DescendingSingle));- this.disposeWithMe(this._componentManager.register(SHEETS_SORT_DESC_EXT_ICON, ExpandDescendingSingle));- this.disposeWithMe(this._componentManager.register(SHEETS_SORT_CUSTOM_ICON, CustomSortSingle));+ this.disposeWithMe(+ this._uiPartsService.registerComponent(SheetsUIPart.FILTER_PANEL_EMBED_POINT, () => connectInjector(EmbedSortBtn, this._injector))+ );++ ([+ ['CustomSortPanel', CustomSortPanel],+ [SHEETS_SORT_ASC_ICON, AscendingSingle],+ [SHEETS_SORT_ASC_EXT_ICON, ExpandAscendingSingle],+ [SHEETS_SORT_DESC_ICON, DescendingSingle],+ [SHEETS_SORT_DESC_EXT_ICON, ExpandDescendingSingle],+ [SHEETS_SORT_CUSTOM_ICON, CustomSortSingle],+ ] as [string, React.FC][]).forEach(([id, component]) => {+ this.disposeWithMe(+ this._componentManager.register(id, component)+ );+ });// this controller is also responsible for toggling the CustomSortDialogthis._sheetsSortUIService.customSortState$.pipe(takeUntil(this.dispose$)).subscribe((newState) => {diff --git a/packages/sheets-thread-comment-ui/src/controllers/sheets-thread-comment.controller.ts b/packages/sheets-thread-comment-ui/src/controllers/sheets-thread-comment.controller.tsindex a4d4c5ca43..f3b85920eb 100644--- a/packages/sheets-thread-comment-ui/src/controllers/sheets-thread-comment.controller.ts+++ b/packages/sheets-thread-comment-ui/src/controllers/sheets-thread-comment.controller.ts@@ -50,7 +50,9 @@ export class SheetsThreadCommentController extends Disposable {[THREAD_COMMENT_PANEL, SheetsThreadCommentPanel],[COMMENT_SINGLE_ICON, CommentSingle],] as const).forEach(([key, comp]) => {- this._componentManager.register(key, comp);+ this.disposeWithMe(+ this._componentManager.register(key, comp)+ );});}}diff --git a/packages/sheets-ui/src/controllers/cell-alert.controller.ts b/packages/sheets-ui/src/controllers/cell-alert.controller.tsindex 9704d37d93..86a314fa15 100644--- a/packages/sheets-ui/src/controllers/cell-alert.controller.ts+++ b/packages/sheets-ui/src/controllers/cell-alert.controller.ts@@ -33,6 +33,8 @@ export class CellAlertRenderController extends Disposable implements IRenderModu}private _initComponent() {- this._componentManager.register(CELL_ALERT_KEY, CellAlert);+ this.disposeWithMe(+ this._componentManager.register(CELL_ALERT_KEY, CellAlert)+ );}}diff --git a/packages/sheets-ui/src/plugin.ts b/packages/sheets-ui/src/plugin.tsindex be79a39e67..2fe0883816 100644--- a/packages/sheets-ui/src/plugin.ts+++ b/packages/sheets-ui/src/plugin.ts@@ -124,12 +124,15 @@ export class UniverSheetsUIPlugin extends Plugin {if (rest.protectedRangeUserSelector) {customComponents.add(UNIVER_SHEET_PERMISSION_USER_PART);- this._componentManager.register(- UNIVER_SHEET_PERMISSION_USER_PART,- rest.protectedRangeUserSelector.component,- {- framework: rest.protectedRangeUserSelector.framework,- }++ this.disposeWithMe(+ this._componentManager.register(+ UNIVER_SHEET_PERMISSION_USER_PART,+ rest.protectedRangeUserSelector.component,+ {+ framework: rest.protectedRangeUserSelector.framework,+ }+ ));}diff --git a/packages/ui/src/controllers/ui/ui-desktop.controller.tsx b/packages/ui/src/controllers/ui/ui-desktop.controller.tsxindex 142040a90c..c07d70891e 100644--- a/packages/ui/src/controllers/ui/ui-desktop.controller.tsx+++ b/packages/ui/src/controllers/ui/ui-desktop.controller.tsx@@ -63,42 +63,18 @@ export class DesktopUIController extends SingleUnitUIController {}private _registerComponents() {- this.disposeWithMe(- this._componentManager.register(- COMMON_LABEL_COMPONENT,- CommonLabel- )- );- this.disposeWithMe(- this._componentManager.register(- HEADING_ITEM_COMPONENT,- HeadingItem- )- );- this.disposeWithMe(- this._componentManager.register(- FONT_FAMILY_COMPONENT,- FontFamily- )- );- this.disposeWithMe(- this._componentManager.register(- FONT_FAMILY_ITEM_COMPONENT,- FontFamilyItem- )- );- this.disposeWithMe(- this._componentManager.register(- FONT_SIZE_COMPONENT,- FontSize- )- );- this.disposeWithMe(- this._componentManager.register(- COLOR_PICKER_COMPONENT,- ColorPicker- )- );+ ([+ [COMMON_LABEL_COMPONENT, CommonLabel],+ [HEADING_ITEM_COMPONENT, HeadingItem],+ [FONT_FAMILY_COMPONENT, FontFamily],+ [FONT_FAMILY_ITEM_COMPONENT, FontFamilyItem],+ [FONT_SIZE_COMPONENT, FontSize],+ [COLOR_PICKER_COMPONENT, ColorPicker],+ ] as [string, React.FC][]).forEach(([id, component]) => {+ this.disposeWithMe(+ this._componentManager.register(id, component)+ );+ });}override bootstrap(callback: (contentElement: HTMLElement, containerElement: HTMLElement) => void): IDisposable {diff --git a/packages/ui/src/services/global-zone/desktop-global-zone.service.ts b/packages/ui/src/services/global-zone/desktop-global-zone.service.tsindex e6c010f2d4..deade5b144 100644--- a/packages/ui/src/services/global-zone/desktop-global-zone.service.ts+++ b/packages/ui/src/services/global-zone/desktop-global-zone.service.ts@@ -14,13 +14,12 @@* limitations under the License.*/-import { toDisposable } from '@univerjs/core';-import { type IDisposable, Inject } from '@univerjs/core';+import type { IDisposable } from '@univerjs/core';import type { ForwardRefExoticComponent } from 'react';+import type { IGlobalZoneService } from './global-zone.service';+import { Inject, toDisposable } from '@univerjs/core';import { Subject } from 'rxjs';-import { ComponentManager } from '../../common/component-manager';-import type { IGlobalZoneService } from './global-zone.service';export class DesktopGlobalZoneService implements IGlobalZoneService {readonly visible$ = new Subject<boolean>();diff --git a/packages/ui/src/services/zen-zone/desktop-zen-zone.service.ts b/packages/ui/src/services/zen-zone/desktop-zen-zone.service.tsindex 3f45bf5579..9f4a6b80c9 100644--- a/packages/ui/src/services/zen-zone/desktop-zen-zone.service.ts+++ b/packages/ui/src/services/zen-zone/desktop-zen-zone.service.ts@@ -14,11 +14,11 @@* limitations under the License.*/+import type { IDisposable } from '@univerjs/core';import type { ComponentType } from '../../common/component-manager';import type { IZenZoneService } from './zen-zone.service';-import { toDisposable } from '@univerjs/core';-import { type IDisposable, Inject } from '@univerjs/core';+import { Inject, toDisposable } from '@univerjs/core';import { BehaviorSubject, ReplaySubject } from 'rxjs';import { ComponentManager } from '../../common/component-manager';