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
feat(ui): optimize popup portal to support multi-instance (#4924)
ZhangWei8b980be6e619
diff --git a/packages/sheets-numfmt-ui/src/plugin.ts b/packages/sheets-numfmt-ui/src/plugin.tsindex 55167b9f04..0199a64405 100644--- a/packages/sheets-numfmt-ui/src/plugin.ts+++ b/packages/sheets-numfmt-ui/src/plugin.ts@@ -20,7 +20,6 @@ import { DependentOn, IConfigService, Inject, Injector, merge, Plugin, registerDimport { IRenderManagerService } from '@univerjs/engine-render';import { UniverSheetsNumfmtPlugin } from '@univerjs/sheets-numfmt';import { UniverSheetsUIPlugin } from '@univerjs/sheets-ui';-import { UI_PLUGIN_CONFIG_KEY } from '@univerjs/ui';import { defaultPluginConfig } from './controllers/config.schema';import { NumfmtAlertRenderController } from './controllers/numfmt-alert-render.controller';import { SheetNumfmtUIController } from './controllers/numfmt.controller';@@ -53,7 +52,7 @@ export class UniverSheetsNumfmtUIPlugin extends Plugin {this._configService.setConfig('menu', menu, { merge: true });}- this._configService.setConfig(UI_PLUGIN_CONFIG_KEY, rest);+ this._configService.setConfig('sheets-numfmt-ui.config', rest);}override onStarting(): void {diff --git a/packages/ui/src/controllers/config.schema.ts b/packages/ui/src/controllers/config.schema.tsindex b0c216832d..7f2c2f5045 100644--- a/packages/ui/src/controllers/config.schema.ts+++ b/packages/ui/src/controllers/config.schema.ts@@ -29,6 +29,8 @@ export interface IUniverUIConfig extends IWorkbenchOptions {override?: DependencyOverride;menu?: MenuConfig;++ popupRootId?: string;}export const defaultPluginConfig: IUniverUIConfig = {};diff --git a/packages/ui/src/ui-plugin.ts b/packages/ui/src/ui-plugin.tsindex f61944d0ea..655edbde87 100644--- a/packages/ui/src/ui-plugin.ts+++ b/packages/ui/src/ui-plugin.ts@@ -16,7 +16,7 @@import type { Dependency } from '@univerjs/core';import type { IUniverUIConfig } from './controllers/config.schema';-import { DependentOn, IConfigService, IContextService, ILocalStorageService, Inject, Injector, merge, mergeOverrideWithDependencies, Plugin } from '@univerjs/core';+import { DependentOn, generateRandomId, IConfigService, IContextService, ILocalStorageService, Inject, Injector, merge, mergeOverrideWithDependencies, Plugin } from '@univerjs/core';import { UniverRenderEnginePlugin } from '@univerjs/engine-render';import { ComponentManager } from './common/component-manager';@@ -77,10 +77,13 @@ export class UniverUIPlugin extends Plugin {// Manage the plugin configuration.const { menu, ...rest } = merge(- {},+ {+ popupRootId: `univer-popup-portal-${generateRandomId(6)}`,+ },defaultPluginConfig,this._config);+if (rest.disableAutoFocus) {this._contextService.setContextValue(DISABLE_AUTO_FOCUS_KEY, true);}diff --git a/packages/ui/src/views/components/popup/RectPopup.tsx b/packages/ui/src/views/components/popup/RectPopup.tsxindex b75952efdc..b7678a851f 100644--- a/packages/ui/src/views/components/popup/RectPopup.tsx+++ b/packages/ui/src/views/components/popup/RectPopup.tsx@@ -17,9 +17,13 @@import type { Nullable } from '@univerjs/core';import type { RefObject } from 'react';import type { Observable } from 'rxjs';-import React, { createContext, useContext, useEffect, useRef } from 'react';+import type { IUniverUIConfig } from '../../../controllers/config.schema';+import { IConfigService } from '@univerjs/core';+import { useDependency } from '@wendellhu/redi/react-bindings';+import React, { createContext, use, useEffect, useRef } from 'react';import { createPortal } from 'react-dom';import { useEvent } from '../../../components/hooks/event';+import { UI_PLUGIN_CONFIG_KEY } from '../../../controllers/config.schema';interface IAbsolutePosition {left: number;@@ -123,7 +127,10 @@ function RectPopup(props: IRectPopupProps) {left: -9999,});const excludeRectsRef = excludeRects;+ const configService = useDependency(IConfigService);const anchorRectRef = useRef<IAbsolutePosition | undefined>(undefined);+ const uiConfig = configService.getConfig(UI_PLUGIN_CONFIG_KEY) as IUniverUIConfig;+ const popupRootId = uiConfig?.popupRootId ?? 'univer-popup-portal';function updatePosition(position: IAbsolutePosition) {requestAnimationFrame(() => {@@ -237,11 +244,11 @@ function RectPopup(props: IRectPopupProps) {</section>);- return !portal ? ele : createPortal(ele, document.getElementById('univer-popup-portal')!);+ return !portal ? ele : document.getElementById(popupRootId) ? createPortal(ele, document.getElementById(popupRootId)!) : null;}RectPopup.calcPopupPosition = calcPopupPosition;-RectPopup.useContext = () => useContext(RectPopupContext);+RectPopup.useContext = () => use(RectPopupContext);export { RectPopup };diff --git a/packages/ui/src/views/workbench/Workbench.tsx b/packages/ui/src/views/workbench/Workbench.tsxindex 9fcb771b02..5998f463be 100644--- a/packages/ui/src/views/workbench/Workbench.tsx+++ b/packages/ui/src/views/workbench/Workbench.tsx@@ -16,11 +16,13 @@import type { DocumentDataModel, IDocumentData, Nullable } from '@univerjs/core';import type { ILocale } from '@univerjs/design';+import type { IUniverUIConfig } from '../../controllers/config.schema';import type { IWorkbenchOptions } from '../../controllers/ui/ui.controller';-import { DocumentFlavor, IUniverInstanceService, LocaleService, ThemeService, UniverInstanceType } from '@univerjs/core';+import { DocumentFlavor, IConfigService, IUniverInstanceService, LocaleService, ThemeService, UniverInstanceType } from '@univerjs/core';import { clsx, ConfigContext, ConfigProvider, defaultTheme, themeInstance } from '@univerjs/design';-import { useContext, useEffect, useMemo, useRef, useState } from 'react';+import { use, useEffect, useMemo, useRef, useState } from 'react';import { createPortal } from 'react-dom';+import { UI_PLUGIN_CONFIG_KEY } from '../../controllers/config.schema';import { BuiltInUIPart } from '../../services/parts/parts.service';import { useDependency } from '../../utils/di';import { ComponentContainer, useComponentsOfPart } from '../components/ComponentContainer';@@ -50,7 +52,8 @@ export function DesktopWorkbench(props: IUniverWorkbenchProps) {const themeService = useDependency(ThemeService);const instanceService = useDependency(IUniverInstanceService);const contentRef = useRef<HTMLDivElement>(null);-+ const configService = useDependency(IConfigService);+ const uiConfig = configService.getConfig(UI_PLUGIN_CONFIG_KEY) as IUniverUIConfig;const customHeaderComponents = useComponentsOfPart(BuiltInUIPart.CUSTOM_HEADER);const footerComponents = useComponentsOfPart(BuiltInUIPart.FOOTER);const headerComponents = useComponentsOfPart(BuiltInUIPart.HEADER);@@ -59,8 +62,8 @@ export function DesktopWorkbench(props: IUniverWorkbenchProps) {const leftSidebarComponents = useComponentsOfPart(BuiltInUIPart.LEFT_SIDEBAR);const globalComponents = useComponentsOfPart(BuiltInUIPart.GLOBAL);const toolbarComponents = useComponentsOfPart(BuiltInUIPart.TOOLBAR);-const [docSnapShot, setDocSnapShot] = useState<Nullable<IDocumentData>>(null);+ const popupRootId = uiConfig?.popupRootId ?? 'univer-popup-portal';useEffect(() => {const sub = instanceService.focused$.subscribe((id) => {@@ -196,13 +199,13 @@ export function DesktopWorkbench(props: IUniverWorkbenchProps) {<GlobalZone />{contextMenu && <DesktopContextMenu />}<FloatingContainer />- <div id="univer-popup-portal" />+ <div id={popupRootId} /></ConfigProvider>);}function FloatingContainer() {- const { mountContainer } = useContext(ConfigContext);+ const { mountContainer } = use(ConfigContext);const floatingComponents = useComponentsOfPart(BuiltInUIPart.FLOATING);return createPortal(<ComponentContainer key="floating" components={floatingComponents} />, mountContainer!);