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
fix(ui): fix flickering on register ui parts (#2430)
Wenzhao Hu2d036a3bd501
diff --git a/packages/ui/src/services/parts/parts.service.ts b/packages/ui/src/services/parts/parts.service.tsindex ed564e0feb..53875d311f 100644--- a/packages/ui/src/services/parts/parts.service.ts+++ b/packages/ui/src/services/parts/parts.service.ts@@ -36,14 +36,14 @@ export enum BuiltInUIPart {export interface IUIPartsService {componentRegistered$: Observable<ComponentPartKey>;- registerComponent(part: ComponentPartKey, component: () => ComponentType): IDisposable;+ registerComponent(part: ComponentPartKey, componentFactory: () => ComponentType): IDisposable;getComponents(part: ComponentPartKey): Set<ComponentRenderer>;}export const IUIPartsService = createIdentifier<IUIPartsService>('ui.parts.service');export class UIPartsService extends Disposable implements IUIPartsService {- private _componentsByPart: Map<ComponentPartKey, Set<ComponentRenderer>> = new Map();+ private _componentsByPart: Map<ComponentPartKey, Set<ComponentType>> = new Map();private readonly _componentRegistered$ = new Subject<ComponentPartKey>();readonly componentRegistered$ = this._componentRegistered$.asObservable();@@ -54,16 +54,17 @@ export class UIPartsService extends Disposable implements IUIPartsService {this._componentRegistered$.complete();}- registerComponent(part: ComponentPartKey, component: () => React.ComponentType): IDisposable {+ registerComponent(part: ComponentPartKey, componentFactory: () => React.ComponentType): IDisposable {+ const componentType = componentFactory();const components = (this._componentsByPart.get(part)|| this._componentsByPart.set(part, new Set()).get(part)!- ).add(component);+ ).add(componentType);this._componentRegistered$.next(part);return toDisposable(() => {- components.delete(component);+ components.delete(componentType);if (components.size === 0) {this._componentsByPart.delete(part);}@@ -71,7 +72,7 @@ export class UIPartsService extends Disposable implements IUIPartsService {});}- getComponents(part: ComponentPartKey): Set<ComponentRenderer> {+ getComponents(part: ComponentPartKey): Set<ComponentType> {return new Set([...(this._componentsByPart.get(part) || new Set())]);}}diff --git a/packages/ui/src/views/App.tsx b/packages/ui/src/views/App.tsxindex 4b80e8bbd8..c87e0f6dd7 100644--- a/packages/ui/src/views/App.tsx+++ b/packages/ui/src/views/App.tsx@@ -21,9 +21,9 @@ 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 { BuiltInUIPart, IUIPartsService } from '../services/parts/parts.service';+import { BuiltInUIPart } from '../services/parts/parts.service';import styles from './app.module.less';-import { ComponentContainer } from './components/ComponentContainer';+import { ComponentContainer, useComponentsOfPart } from './components/ComponentContainer';import { Toolbar } from './components/doc-bars/Toolbar';import { Sidebar } from './components/sidebar/Sidebar';import { ZenZone } from './components/zen-zone/ZenZone';@@ -50,30 +50,12 @@ export function App(props: IUniverAppProps) {const messageService = useDependency(IMessageService);const contentRef = useRef<HTMLDivElement>(null);- const uiPartsService = useDependency(IUIPartsService);-- const [updateTrigger, setUpdateTrigger] = useState({});-- useEffect(() => {- const updateSubscription = uiPartsService.componentRegistered$.subscribe(() => {- setUpdateTrigger({});- });-- return () => {- updateSubscription.unsubscribe();- };- // eslint-disable-next-line react-hooks/exhaustive-deps- }, []);-- const { headerComponents, contentComponents, footerComponents, headerMenuComponents, leftSidebarComponents, globalComponents } = useMemo(() => ({- 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]);+ const footerComponents = useComponentsOfPart(BuiltInUIPart.FOOTER);+ const headerComponents = useComponentsOfPart(BuiltInUIPart.HEADER);+ const headerMenuComponents = useComponentsOfPart(BuiltInUIPart.HEADER_MENU);+ const contentComponents = useComponentsOfPart(BuiltInUIPart.CONTENT);+ const leftSidebarComponents = useComponentsOfPart(BuiltInUIPart.LEFT_SIDEBAR);+ const globalComponents = useComponentsOfPart(BuiltInUIPart.GLOBAL);useEffect(() => {if (!themeService.getCurrentTheme()) {@@ -135,12 +117,12 @@ export function App(props: IUniverAppProps) {<section className={styles.appContainer}><div className={styles.appContainerWrapper}><aside className={styles.appContainerLeftSidebar}>- <ComponentContainer components={leftSidebarComponents} />+ <ComponentContainer key="left-sidebar" components={leftSidebarComponents} /></aside><section className={styles.appContainerContent}><header>- {header && <ComponentContainer components={headerComponents} />}+ {header && <ComponentContainer key="header" components={headerComponents} />}</header><section@@ -149,7 +131,7 @@ export function App(props: IUniverAppProps) {data-range-selectoronContextMenu={(e) => e.preventDefault()}>- <ComponentContainer components={contentComponents} />+ <ComponentContainer key="content" components={contentComponents} /></section></section>@@ -161,15 +143,15 @@ export function App(props: IUniverAppProps) {{/* footer */}{footer && (<footer className={styles.appFooter}>- <ComponentContainer components={footerComponents} />+ <ComponentContainer key="footer" components={footerComponents} /></footer>)}<ZenZone /></section></div>- <ComponentContainer components={globalComponents} />- <ComponentContainer components={builtInGlobalComponents} />+ <ComponentContainer key="global" components={globalComponents} />+ <ComponentContainer key="built-in-global" components={builtInGlobalComponents} />{contextMenu && <ContextMenu />}</ConfigProvider>);diff --git a/packages/ui/src/views/components/ComponentContainer.tsx b/packages/ui/src/views/components/ComponentContainer.tsxindex b4018aeca2..0ba1716543 100644--- a/packages/ui/src/views/components/ComponentContainer.tsx+++ b/packages/ui/src/views/components/ComponentContainer.tsx@@ -23,7 +23,7 @@ import { useObservable } from '../../components/hooks/observable';import { IUIPartsService } from '../../services/parts/parts.service';export interface IComponentContainerProps {- components?: Set<() => ComponentType>;+ components?: Set<ComponentType>;fallback?: React.ReactNode;sharedProps?: Record<string, unknown>;}@@ -32,9 +32,9 @@ export function ComponentContainer(props: IComponentContainerProps) {const { components, fallback, sharedProps } = props;if (!components || components.size === 0) return fallback ?? null;- return Array.from(components.values()).map((component, index) =>- React.createElement(component(), { key: `${index}`, ...sharedProps })- );+ return Array.from(components.values()).map((component, index) => {+ return React.createElement(component, { key: `${component.displayName ?? index}`, ...sharedProps });+ });}/**@@ -55,9 +55,9 @@ export function useComponentsOfPart(part: string, injector?: Injector) {),undefined,undefined,- [uiPartsService]+ [uiPartsService, part]);- // eslint-disable-next-line react-hooks/exhaustive-deps+ // eslint-disable-next-line react-hooks/exhaustive-depsreturn useMemo(() => uiPartsService.getComponents(part), [componentPartUpdateCount]);}diff --git a/packages/ui/src/views/components/doc-bars/Toolbar.tsx b/packages/ui/src/views/components/doc-bars/Toolbar.tsxindex 3e85eefbf6..725521fc6a 100644--- a/packages/ui/src/views/components/doc-bars/Toolbar.tsx+++ b/packages/ui/src/views/components/doc-bars/Toolbar.tsx@@ -39,7 +39,7 @@ const MENU_POSITIONS = [];export interface IToolbarProps {- headerMenuComponents?: Set<() => ComponentType>;+ headerMenuComponents?: Set<ComponentType>;}/**diff --git a/packages/ui/src/views/parts.ts b/packages/ui/src/views/parts.tsindex d00f32fa85..5c49a45acd 100644--- a/packages/ui/src/views/parts.ts+++ b/packages/ui/src/views/parts.ts@@ -18,6 +18,4 @@ import type { ComponentType } from 'react';import { GlobalZone } from './components/global-zone/GlobalZone';-export const builtInGlobalComponents: Set<() => ComponentType> = new Set([- () => GlobalZone,-]);+export const builtInGlobalComponents: Set<ComponentType> = new Set([GlobalZone]);