Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

feat(ui): ui parts service support more generic situations (#2286)

Wenzhao Hu415d5b83b246
diff --git a/packages/docs-ui/src/controllers/doc-ui.controller.ts b/packages/docs-ui/src/controllers/doc-ui.controller.ts
index 7d5ba67109..a1f27ae10d 100644
--- a/packages/docs-ui/src/controllers/doc-ui.controller.ts
+++ b/packages/docs-ui/src/controllers/doc-ui.controller.ts
@@ -17,7 +17,7 @@
import type { DocumentDataModel } from '@univerjs/core';
import { Disposable, IUniverInstanceService, LifecycleStages, OnLifecycle, UniverInstanceType } from '@univerjs/core';
import type { IMenuItemFactory } from '@univerjs/ui';
-import { ComponentManager, DesktopUIPart, IEditorService, ILayoutService, IMenuService, IUIPartsService } from '@univerjs/ui';
+import { BuiltInUIPart, ComponentManager, IEditorService, ILayoutService, IMenuService, IUIPartsService } from '@univerjs/ui';
import { Inject, Injector } from '@wendellhu/redi';
import { connectInjector } from '@wendellhu/redi/react-bindings';
@@ -129,7 +129,7 @@ export class DocUIController extends Disposable {
const embedded = this._editorService.isEditor(firstDocUnitId);
if (!embedded) {
this.disposeWithMe(
- this._uiPartsService.registerComponent(DesktopUIPart.CONTENT, () => connectInjector(DocBackground, this._injector))
+ this._uiPartsService.registerComponent(BuiltInUIPart.CONTENT, () => connectInjector(DocBackground, this._injector))
);
}
}
diff --git a/packages/sheets-formula/src/controllers/formula-ui.controller.ts b/packages/sheets-formula/src/controllers/formula-ui.controller.ts
index 2b10714d09..3e662b41a4 100644
--- a/packages/sheets-formula/src/controllers/formula-ui.controller.ts
+++ b/packages/sheets-formula/src/controllers/formula-ui.controller.ts
@@ -16,7 +16,7 @@
import { Disposable, ICommandService, LifecycleStages, OnLifecycle, UniverInstanceType } from '@univerjs/core';
import type { MenuConfig } from '@univerjs/ui';
-import { ComponentManager, DesktopUIPart, IMenuService, IShortcutService, IUIPartsService } from '@univerjs/ui';
+import { BuiltInUIPart, ComponentManager, IMenuService, IShortcutService, IUIPartsService } from '@univerjs/ui';
import type { Ctor } from '@wendellhu/redi';
import { Inject, Injector } from '@wendellhu/redi';
import { connectInjector } from '@wendellhu/redi/react-bindings';
@@ -124,7 +124,7 @@ export class FormulaUIController extends Disposable {
private _registerComponents(): void {
this.disposeWithMe(
- this._uiPartsService.registerComponent(DesktopUIPart.CONTENT, () => connectInjector(RenderFormulaPromptContent, this._injector))
+ this._uiPartsService.registerComponent(BuiltInUIPart.CONTENT, () => connectInjector(RenderFormulaPromptContent, this._injector))
);
this._componentManager.register(MORE_FUNCTIONS_COMPONENT, MoreFunctions);
diff --git a/packages/sheets-ui/src/controllers/sheet-ui.controller.ts b/packages/sheets-ui/src/controllers/sheet-ui.controller.ts
index e9c8217fe9..7a8f70246f 100644
--- a/packages/sheets-ui/src/controllers/sheet-ui.controller.ts
+++ b/packages/sheets-ui/src/controllers/sheet-ui.controller.ts
@@ -24,7 +24,7 @@ import {
SetUnderlineCommand,
} from '@univerjs/sheets';
import type { IMenuItemFactory, MenuConfig } from '@univerjs/ui';
-import { ComponentManager, DesktopUIPart, ILayoutService, IMenuService, IShortcutService, IUIPartsService } from '@univerjs/ui';
+import { BuiltInUIPart, ComponentManager, ILayoutService, IMenuService, IShortcutService, IUIPartsService } from '@univerjs/ui';
import { Inject, Injector } from '@wendellhu/redi';
import { connectInjector } from '@wendellhu/redi/react-bindings';
@@ -485,9 +485,9 @@ export class SheetUIController extends Disposable {
const uiController = this._uiPartsService;
const injector = this._injector;
- this.disposeWithMe(uiController.registerComponent(DesktopUIPart.HEADER, () => connectInjector(RenderSheetHeader, injector)));
- this.disposeWithMe(uiController.registerComponent(DesktopUIPart.FOOTER, () => connectInjector(RenderSheetFooter, injector)));
- this.disposeWithMe(uiController.registerComponent(DesktopUIPart.CONTENT, () => connectInjector(RenderSheetContent, injector)));
+ this.disposeWithMe(uiController.registerComponent(BuiltInUIPart.HEADER, () => connectInjector(RenderSheetHeader, injector)));
+ this.disposeWithMe(uiController.registerComponent(BuiltInUIPart.FOOTER, () => connectInjector(RenderSheetFooter, injector)));
+ this.disposeWithMe(uiController.registerComponent(BuiltInUIPart.CONTENT, () => connectInjector(RenderSheetContent, injector)));
}
private _initFocusHandler(): void {
diff --git a/packages/slides-ui/src/controllers/slide-ui.controller.ts b/packages/slides-ui/src/controllers/slide-ui.controller.ts
index de40219753..41ebd29646 100644
--- a/packages/slides-ui/src/controllers/slide-ui.controller.ts
+++ b/packages/slides-ui/src/controllers/slide-ui.controller.ts
@@ -15,7 +15,7 @@
*/
import { Disposable, ICommandService, LifecycleStages, OnLifecycle } from '@univerjs/core';
-import { DesktopUIPart, IUIPartsService } from '@univerjs/ui';
+import { BuiltInUIPart, IUIPartsService } from '@univerjs/ui';
import { Inject, Injector } from '@wendellhu/redi';
import { connectInjector } from '@wendellhu/redi/react-bindings';
import { SlideSideBar } from '../views/slide-bar/SlideBar';
@@ -47,7 +47,7 @@ export class SlideUIController extends Disposable {
private _initUIComponents(): void {
this.disposeWithMe(
- this._uiPartsService.registerComponent(DesktopUIPart.LEFT_SIDEBAR, () => connectInjector(SlideSideBar, this._injector))
+ this._uiPartsService.registerComponent(BuiltInUIPart.LEFT_SIDEBAR, () => connectInjector(SlideSideBar, this._injector))
);
}
}
diff --git a/packages/ui/src/index.ts b/packages/ui/src/index.ts
index 1ccdf6c63f..deb3ed9d07 100644
--- a/packages/ui/src/index.ts
+++ b/packages/ui/src/index.ts
@@ -29,7 +29,7 @@ export {
} from './controllers/shared-shortcut.controller';
export { IUIController, type IWorkbenchOptions } from './controllers/ui/ui.controller';
export { DesktopUIController } from './controllers/ui/ui-desktop.controller';
-export { IUIPartsService, DesktopUIPart, DesktopUIPartsService } from './services/parts/parts.service';
+export { IUIPartsService, BuiltInUIPart, UIPartsService as BuiltInUIPartsService } from './services/parts/parts.service';
export { enUS, zhCN, ruRU } from './locale';
export { DesktopBeforeCloseService, IBeforeCloseService } from './services/before-close/before-close.service';
export { CopyCommand, CutCommand, PasteCommand } from './services/clipboard/clipboard.command';
diff --git a/packages/ui/src/services/confirm/desktop-confirm.service.ts b/packages/ui/src/services/confirm/desktop-confirm.service.ts
index 1ae927402e..de5b6f4814 100644
--- a/packages/ui/src/services/confirm/desktop-confirm.service.ts
+++ b/packages/ui/src/services/confirm/desktop-confirm.service.ts
@@ -20,7 +20,7 @@ import { Subject } from 'rxjs';
import { connectInjector } from '@wendellhu/redi/react-bindings';
import type { IConfirmPartMethodOptions } from '../../views/components/confirm-part/interface';
-import { DesktopUIPart, IUIPartsService } from '../parts/parts.service';
+import { BuiltInUIPart, IUIPartsService } from '../parts/parts.service';
import { ConfirmPart } from '../../views/components/confirm-part/ConfirmPart';
import type { IConfirmService } from './confirm.service';
@@ -83,7 +83,7 @@ export class DesktopConfirmService extends Disposable implements IConfirmService
protected _initUIPart(): void {
this.disposeWithMe(
- this._uiPartsService.registerComponent(DesktopUIPart.GLOBAL, () => connectInjector(ConfirmPart, this._injector))
+ this._uiPartsService.registerComponent(BuiltInUIPart.GLOBAL, () => connectInjector(ConfirmPart, this._injector))
);
}
}
diff --git a/packages/ui/src/services/dialog/desktop-dialog.service.ts b/packages/ui/src/services/dialog/desktop-dialog.service.ts
index ddc716d2e8..2ba74e5807 100644
--- a/packages/ui/src/services/dialog/desktop-dialog.service.ts
+++ b/packages/ui/src/services/dialog/desktop-dialog.service.ts
@@ -20,7 +20,7 @@ import { Subject } from 'rxjs';
import { connectInjector } from '@wendellhu/redi/react-bindings';
import type { IDialogPartMethodOptions } from '../../views/components/dialog-part/interface';
-import { DesktopUIPart, IUIPartsService } from '../parts/parts.service';
+import { BuiltInUIPart, IUIPartsService } from '../parts/parts.service';
import { DialogPart } from '../../views/components/dialog-part/DialogPart';
import type { IDialogService } from './dialog.service';
@@ -79,7 +79,7 @@ export class DesktopDialogService extends Disposable implements IDialogService {
protected _initUIPart(): void {
this.disposeWithMe(
- this._uiPartsService.registerComponent(DesktopUIPart.GLOBAL, () => connectInjector(DialogPart, this._injector))
+ this._uiPartsService.registerComponent(BuiltInUIPart.GLOBAL, () => connectInjector(DialogPart, this._injector))
);
}
}
diff --git a/packages/ui/src/services/notification/desktop-notification.service.ts b/packages/ui/src/services/notification/desktop-notification.service.ts
index 7d96db0eb8..4a9741909b 100644
--- a/packages/ui/src/services/notification/desktop-notification.service.ts
+++ b/packages/ui/src/services/notification/desktop-notification.service.ts
@@ -20,7 +20,7 @@ import { type IDisposable, Inject, Injector } from '@wendellhu/redi';
import { connectInjector } from '@wendellhu/redi/react-bindings';
import type { INotificationOptions } from '../../components/notification/Notification';
import { notification, Notification } from '../../components/notification/Notification';
-import { DesktopUIPart, IUIPartsService } from '../parts/parts.service';
+import { BuiltInUIPart, IUIPartsService } from '../parts/parts.service';
import type { INotificationService } from './notification.service';
export class DesktopNotificationService extends Disposable implements INotificationService {
@@ -40,6 +40,6 @@ export class DesktopNotificationService extends Disposable implements INotificat
}
protected _initUIPart(): void {
- this.disposeWithMe(this._uiPartsService.registerComponent(DesktopUIPart.GLOBAL, () => connectInjector(Notification, this._injector)));
+ this.disposeWithMe(this._uiPartsService.registerComponent(BuiltInUIPart.GLOBAL, () => connectInjector(Notification, this._injector)));
}
}
diff --git a/packages/ui/src/services/parts/parts.service.ts b/packages/ui/src/services/parts/parts.service.ts
index aea0a88bef..e693fc3fcd 100644
--- a/packages/ui/src/services/parts/parts.service.ts
+++ b/packages/ui/src/services/parts/parts.service.ts
@@ -17,10 +17,14 @@
import type { IDisposable } from '@wendellhu/redi';
import { createIdentifier } from '@wendellhu/redi';
import { type Observable, Subject } from 'rxjs';
-import { toDisposable } from '@univerjs/core';
+import { Disposable, toDisposable } from '@univerjs/core';
+
import type { ComponentType } from '../../common/component-manager';
-export enum DesktopUIPart {
+type ComponentRenderer = () => ComponentType;
+type ComponentPartKey = BuiltInUIPart | string;
+
+export enum BuiltInUIPart {
GLOBAL = 'global',
HEADER = 'header',
HEADER_MENU = 'header-menu',
@@ -30,39 +34,43 @@ export enum DesktopUIPart {
}
export interface IUIPartsService {
- componentRegistered$: Observable<void>;
+ componentRegistered$: Observable<ComponentPartKey>;
- registerComponent(part: DesktopUIPart, component: () => ComponentType): IDisposable;
- getComponents(part: DesktopUIPart): Set<() => ComponentType>;
+ registerComponent(part: ComponentPartKey, component: () => ComponentType): IDisposable;
+ getComponents(part: ComponentPartKey): Set<ComponentRenderer>;
}
export const IUIPartsService = createIdentifier<IUIPartsService>('ui.parts.service');
-export class DesktopUIPartsService implements IUIPartsService {
- private _componentsByPart: Map<DesktopUIPart, Set<() => ComponentType>> = new Map();
+export class UIPartsService extends Disposable implements IUIPartsService {
+ private _componentsByPart: Map<ComponentPartKey, Set<ComponentRenderer>> = new Map();
- private readonly _componentRegistered$ = new Subject<void>();
+ private readonly _componentRegistered$ = new Subject<ComponentPartKey>();
readonly componentRegistered$ = this._componentRegistered$.asObservable();
- registerComponent(part: DesktopUIPart, component: () => React.ComponentType): IDisposable {
+ override dispose(): void {
+ super.dispose();
+
+ this._componentRegistered$.complete();
+ }
+
+ registerComponent(part: ComponentPartKey, component: () => React.ComponentType): IDisposable {
const components = (
this._componentsByPart.get(part)
|| this._componentsByPart.set(part, new Set()).get(part)!
).add(component);
- this._componentRegistered$.next();
+ this._componentRegistered$.next(part);
return toDisposable(() => {
components.delete(component);
if (components.size === 0) {
this._componentsByPart.delete(part);
}
-
- this._componentRegistered$.complete();
});
}
- getComponents(part: DesktopUIPart): Set<() => ComponentType> {
+ getComponents(part: ComponentPartKey): Set<ComponentRenderer> {
return new Set([...(this._componentsByPart.get(part) || new Set())]);
}
}
diff --git a/packages/ui/src/ui-plugin.ts b/packages/ui/src/ui-plugin.ts
index 76774bdb8f..0622083bb8 100644
--- a/packages/ui/src/ui-plugin.ts
+++ b/packages/ui/src/ui-plugin.ts
@@ -54,7 +54,7 @@ import { IZenZoneService } from './services/zen-zone/zen-zone.service';
import { EditorService, IEditorService } from './services/editor/editor.service';
import { IRangeSelectorService, RangeSelectorService } from './services/range-selector/range-selector.service';
import { IProgressService, ProgressService } from './services/progress/progress.service';
-import { DesktopUIPartsService, IUIPartsService } from './services/parts/parts.service';
+import { IUIPartsService, UIPartsService } from './services/parts/parts.service';
const PLUGIN_NAME = 'ui';
@@ -100,7 +100,7 @@ export class UniverUIPlugin extends Plugin {
// services
[ShortcutPanelService],
- [IUIPartsService, { useClass: DesktopUIPartsService }],
+ [IUIPartsService, { useClass: UIPartsService }],
[ILayoutService, { useClass: DesktopLayoutService }],
[IShortcutService, { useClass: DesktopShortcutService }],
[IPlatformService, { useClass: DesktopPlatformService }],
diff --git a/packages/ui/src/views/App.tsx b/packages/ui/src/views/App.tsx
index 7ce059d75a..1a867b08a8 100644
--- a/packages/ui/src/views/App.tsx
+++ b/packages/ui/src/views/App.tsx
@@ -21,7 +21,7 @@ import { useDependency } from '@wendellhu/redi/react-bindings';
import React, { useEffect, useMemo, useRef, useState } from 'react';
import type { IWorkbenchOptions } from '../controllers/ui/ui.controller';
import { IMessageService } from '../services/message/message.service';
-import { DesktopUIPart, IUIPartsService } from '../services/parts/parts.service';
+import { BuiltInUIPart, IUIPartsService } from '../services/parts/parts.service';
import styles from './app.module.less';
import { ComponentContainer } from './components/ComponentContainer';
import { Toolbar } from './components/doc-bars/Toolbar';
@@ -67,12 +67,12 @@ export function App(props: IUniverAppProps) {
}, []);
const { headerComponents, contentComponents, footerComponents, headerMenuComponents, leftSidebarComponents, globalComponents } = useMemo(() => ({
- headerComponents: uiPartsService.getComponents(DesktopUIPart.HEADER),
- contentComponents: uiPartsService.getComponents(DesktopUIPart.CONTENT),
- footerComponents: uiPartsService.getComponents(DesktopUIPart.FOOTER),
- headerMenuComponents: uiPartsService.getComponents(DesktopUIPart.HEADER_MENU),
- leftSidebarComponents: uiPartsService.getComponents(DesktopUIPart.LEFT_SIDEBAR),
- globalComponents: uiPartsService.getComponents(DesktopUIPart.GLOBAL),
+ headerComponents: uiPartsService.getComponents(BuiltInUIPart.HEADER),
+ contentComponents: uiPartsService.getComponents(BuiltInUIPart.CONTENT),
+ footerComponents: uiPartsService.getComponents(BuiltInUIPart.FOOTER),
+ headerMenuComponents: uiPartsService.getComponents(BuiltInUIPart.HEADER_MENU),
+ leftSidebarComponents: uiPartsService.getComponents(BuiltInUIPart.LEFT_SIDEBAR),
+ globalComponents: uiPartsService.getComponents(BuiltInUIPart.GLOBAL),
// eslint-disable-next-line react-hooks/exhaustive-deps
}), [updateTrigger]);