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): support popup service (#1640)

zhangw5e597fdd4bd9
diff --git a/packages/design/src/components/popup/RectPopup.tsx b/packages/design/src/components/popup/RectPopup.tsx
new file mode 100644
index 0000000000..e38b2a463b
--- /dev/null
+++ b/packages/design/src/components/popup/RectPopup.tsx
@@ -0,0 +1,139 @@
+/**
+ * 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 React, { useEffect, useRef, useState } from 'react';
+import styles from './index.module.less';
+
+interface IAbsolutePosition {
+ left: number;
+ right: number;
+ top: number;
+ bottom: number;
+}
+
+export interface IRectPopupProps {
+ children?: React.ReactNode;
+
+ /**
+ * the anchor element bounding rect
+ */
+ anchorRect: IAbsolutePosition;
+
+ direction?: 'horizontal' | 'vertical';
+
+ onClickOutside?: (e: MouseEvent) => void;
+}
+
+export interface IPopupLayoutInfo {
+ position: IAbsolutePosition;
+ width: number;
+ height: number;
+ containerWidth: number;
+ containerHeight: number;
+ direction?: 'horizontal' | 'vertical';
+}
+
+const calcPopupPosition = (layout: IPopupLayoutInfo) => {
+ const { position, width, height, containerHeight, containerWidth, direction = 'vertical' } = layout;
+ if (direction === 'vertical') {
+ const { left: startX, top: startY, right: endX, bottom: endY } = position;
+
+ const verticalStyle = (endY + height) > containerHeight ? { top: startY - height } : { top: endY };
+ const horizontalStyle = (startX + width) > containerWidth ? { left: endX - width } : { left: startX };
+
+ return {
+ ...verticalStyle,
+ ...horizontalStyle,
+ };
+ } else {
+ const { left: startX, top: startY, right: endX, bottom: endY } = position;
+
+ const verticalStyle = ((startY + height) > containerHeight) ? { top: endY - height } : { top: startY };
+ const horizontalStyle = ((endX + width) > containerWidth) ? { left: startX - width } : { left: endX };
+
+ return {
+ ...verticalStyle,
+ ...horizontalStyle,
+ };
+ }
+};
+
+function RectPopup(props: IRectPopupProps) {
+ const { children, anchorRect, direction = 'vertical', onClickOutside } = props;
+
+ const nodeRef = useRef(null);
+ const clickOtherFn = useRef(onClickOutside);
+
+ clickOtherFn.current = onClickOutside;
+ const [position, setPosition] = useState<Partial<IAbsolutePosition>>({
+ top: -9999,
+ left: -9999,
+ });
+
+ useEffect(() => {
+ const { clientWidth, clientHeight } = nodeRef.current!;
+ const { innerWidth, innerHeight } = window;
+ setPosition(
+ calcPopupPosition(
+ {
+ position: anchorRect,
+ width: clientWidth,
+ height: clientHeight,
+ containerWidth: innerWidth,
+ containerHeight: innerHeight,
+ direction,
+ }
+ )
+ );
+ },
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ [
+ anchorRect.left,
+ anchorRect.top,
+ anchorRect.bottom,
+ anchorRect.right,
+ direction,
+ ]);
+
+ useEffect(() => {
+ const handleClick = (e: MouseEvent) => {
+ clickOtherFn.current?.(e);
+ };
+ setTimeout(() => {
+ window.addEventListener('click', handleClick);
+ }, 100);
+ return () => {
+ window.removeEventListener('click', handleClick);
+ };
+ }, [clickOtherFn]);
+
+ return (
+ <section
+ ref={nodeRef}
+ className={styles.popup}
+ style={position}
+ onClick={(e) => {
+ e.stopPropagation();
+ }}
+ >
+ {children}
+ </section>
+ );
+}
+
+RectPopup.calcPopupPosition = calcPopupPosition;
+
+export { RectPopup };
diff --git a/packages/design/src/components/popup/index.module.less b/packages/design/src/components/popup/index.module.less
index 3a0c31bd4e..2a3442a92c 100644
--- a/packages/design/src/components/popup/index.module.less
+++ b/packages/design/src/components/popup/index.module.less
@@ -64,3 +64,12 @@
}
}
}
+
+.popup-mask {
+ position: fixed;
+ z-index: 1060;
+ top: 0;
+ left: 0;
+ right: 0;
+ bottom: 0;
+}
diff --git a/packages/design/src/components/popup/index.ts b/packages/design/src/components/popup/index.ts
index 9ad882bc4d..32976a2ba1 100644
--- a/packages/design/src/components/popup/index.ts
+++ b/packages/design/src/components/popup/index.ts
@@ -15,3 +15,4 @@
*/
export { type IPopupProps, Popup } from './Popup';
+export { RectPopup, type IRectPopupProps } from './RectPopup';
diff --git a/packages/design/src/index.ts b/packages/design/src/index.ts
index 516242b17b..b45583c7bd 100644
--- a/packages/design/src/index.ts
+++ b/packages/design/src/index.ts
@@ -32,7 +32,7 @@ export { type IInputNumberProps, InputNumber } from './components/input-number';
export { Menu, MenuItem, MenuItemGroup, SubMenu } from './components/menu';
export { type IMessageMethodOptions, type IMessageProps, Message, MessageType } from './components/message';
export { type IPagerProps, Pager } from './components/pager';
-export { type IPopupProps, Popup } from './components/popup';
+export { type IPopupProps, Popup, RectPopup, type IRectPopupProps } from './components/popup';
export { type IRadioProps, Radio } from './components/radio';
export { type IRadioGroupProps, RadioGroup } from './components/radio-group';
export { type IScrollbarProps, Scrollbar } from './components/scrollbar';
diff --git a/packages/sheets-ui/src/common/utils.ts b/packages/sheets-ui/src/common/utils.ts
index f1dbcec704..8b8924a6a4 100644
--- a/packages/sheets-ui/src/common/utils.ts
+++ b/packages/sheets-ui/src/common/utils.ts
@@ -19,6 +19,8 @@ import { ObjectMatrix } from '@univerjs/core';
import type { ISetRangeValuesMutationParams } from '@univerjs/sheets';
import { SetRangeValuesMutation, SetRangeValuesUndoMutationFactory } from '@univerjs/sheets';
import type { IAccessor } from '@wendellhu/redi';
+import type { IBoundRectNoAngle, Scene, SpreadsheetSkeleton } from '@univerjs/engine-render';
+import { VIEWPORT_KEY } from './keys';
export function checkCellContentInRanges(worksheet: Worksheet, ranges: IRange[]): boolean {
return ranges.some((range) => checkCellContentInRange(worksheet, range));
@@ -89,3 +91,79 @@ export function getClearContentMutationParamForRange(
return redoMatrix;
}
+
+export function getViewportByCell(row: number, column: number, scene: Scene, worksheet: Worksheet) {
+ const freeze = worksheet.getFreeze();
+ if (!freeze || (freeze.startRow <= 0 && freeze.startColumn <= 0)) {
+ return scene.getViewport(VIEWPORT_KEY.VIEW_MAIN);
+ }
+
+ if (row > freeze.startRow && column > freeze.startColumn) {
+ return scene.getViewport(VIEWPORT_KEY.VIEW_MAIN);
+ }
+
+ if (row <= freeze.startRow && column <= freeze.startColumn) {
+ return scene.getViewport(VIEWPORT_KEY.VIEW_MAIN_LEFT_TOP);
+ }
+
+ if (row <= freeze.startRow && column > freeze.startColumn) {
+ return scene.getViewport(VIEWPORT_KEY.VIEW_MAIN_TOP);
+ }
+
+ if (row > freeze.startRow && column <= freeze.startColumn) {
+ return scene.getViewport(VIEWPORT_KEY.VIEW_MAIN_LEFT);
+ }
+}
+
+export function transformBound2OffsetBound(originBound: IBoundRectNoAngle, scene: Scene, skeleton: SpreadsheetSkeleton, worksheet: Worksheet): IBoundRectNoAngle {
+ const topLeft = transformPosition2Offset(originBound.left, originBound.top, scene, skeleton, worksheet);
+ const bottomRight = transformPosition2Offset(originBound.right, originBound.bottom, scene, skeleton, worksheet);
+
+ return {
+ left: topLeft.x,
+ top: topLeft.y,
+ right: bottomRight.x,
+ bottom: bottomRight.y,
+ };
+}
+
+export function transformPosition2Offset(x: number, y: number, scene: Scene, skeleton: SpreadsheetSkeleton, worksheet: Worksheet) {
+ const { scaleX, scaleY } = scene.getAncestorScale();
+ const viewMain = scene.getViewport(VIEWPORT_KEY.VIEW_MAIN);
+ if (!viewMain) {
+ return {
+ x,
+ y,
+ };
+ }
+ const freeze = worksheet.getFreeze();
+ const { startColumn, startRow, xSplit, ySplit } = freeze;
+ // freeze start
+ const startSheetView = skeleton.getNoMergeCellPositionByIndexWithNoHeader(startRow - ySplit, startColumn - xSplit);
+ // freeze end
+ const endSheetView = skeleton.getNoMergeCellPositionByIndexWithNoHeader(startRow, startColumn);
+ const { rowHeaderWidth, columnHeaderHeight } = skeleton;
+ const freezeWidth = endSheetView.startX - startSheetView.startX;
+ const freezeHeight = endSheetView.startY - startSheetView.startY;
+
+ const { top, left, actualScrollX, actualScrollY } = viewMain;
+ let offsetX: number;
+ // viewMain or viewTop
+ if (x > left) {
+ offsetX = (x - actualScrollX) * scaleX;
+ } else {
+ offsetX = ((freezeWidth + rowHeaderWidth) - (left - x)) * scaleX;
+ }
+
+ let offsetY: number;
+ if (y > top) {
+ offsetY = (y - actualScrollY) * scaleY;
+ } else {
+ offsetY = ((freezeHeight + columnHeaderHeight) - (top - y)) * scaleX;
+ }
+
+ return {
+ x: offsetX,
+ y: offsetY,
+ };
+}
diff --git a/packages/sheets-ui/src/index.ts b/packages/sheets-ui/src/index.ts
index b6d26a2f72..15257503d8 100644
--- a/packages/sheets-ui/src/index.ts
+++ b/packages/sheets-ui/src/index.ts
@@ -76,3 +76,4 @@ export { SheetSkeletonManagerService } from './services/sheet-skeleton-manager.s
export { UniverSheetsUIPlugin } from './sheets-ui-plugin';
export { SheetCanvasView } from './views/sheet-canvas-view';
export { SHEET_VIEW_KEY } from './common/keys';
+export { CanvasPopManagerService } from './services/canvas-pop-manager.service';
diff --git a/packages/sheets-ui/src/services/canvas-pop-manager.service.ts b/packages/sheets-ui/src/services/canvas-pop-manager.service.ts
new file mode 100644
index 0000000000..43de540aac
--- /dev/null
+++ b/packages/sheets-ui/src/services/canvas-pop-manager.service.ts
@@ -0,0 +1,255 @@
+/**
+ * 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 { Worksheet } from '@univerjs/core';
+import { Disposable, DisposableCollection, ICommandService, IUniverInstanceService } from '@univerjs/core';
+import { IRenderManagerService } from '@univerjs/engine-render';
+import type { BaseObject, IBoundRectNoAngle, IRender, SpreadsheetSkeleton, Viewport } from '@univerjs/engine-render';
+import { IGlobalPopupManagerService } from '@univerjs/ui';
+import type { IDisposable } from '@wendellhu/redi';
+import { Inject } from '@wendellhu/redi';
+import { BehaviorSubject } from 'rxjs';
+import type { ISetWorksheetRowAutoHeightMutationParams } from '@univerjs/sheets';
+import { COMMAND_LISTENER_SKELETON_CHANGE, SetWorksheetRowAutoHeightMutation } from '@univerjs/sheets';
+import { getViewportByCell, transformBound2OffsetBound } from '../common/utils';
+import { SetScrollOperation } from '../commands/operations/scroll.operation';
+import { SetZoomRatioOperation } from '..';
+import { SheetSkeletonManagerService } from './sheet-skeleton-manager.service';
+
+interface ICanvasPopup {
+ componentKey: string;
+ mask?: boolean;
+ onClickOutside?: (e: MouseEvent) => void;
+ direction?: 'vertical' | 'horizontal';
+ offset?: [number, number];
+}
+
+export class CanvasPopManagerService extends Disposable {
+ constructor(
+ @Inject(IGlobalPopupManagerService) private readonly _globalPopupManagerService: IGlobalPopupManagerService,
+ @IRenderManagerService private readonly _renderManagerService: IRenderManagerService,
+ @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
+ @Inject(SheetSkeletonManagerService) private readonly _sheetSkeletonManagerService: SheetSkeletonManagerService,
+ @ICommandService private readonly _commandService: ICommandService
+ ) {
+ super();
+ }
+
+ private _calcCellPosition(
+ row: number,
+ col: number,
+ currentRender: IRender,
+ skeleton: SpreadsheetSkeleton,
+ activeViewport: Viewport
+ ) {
+ const { scene, engine } = currentRender;
+
+ const cellInfo = skeleton.getCellByIndex(row, col);
+
+ const { scaleX, scaleY } = scene.getAncestorScale();
+
+ const scrollXY = {
+ x: activeViewport.actualScrollX,
+ y: activeViewport.actualScrollY,
+ };
+
+ const bounding = engine.getCanvasElement().getBoundingClientRect();
+
+ const position: IBoundRectNoAngle = {
+ left: ((cellInfo.startX - scrollXY.x) * scaleX),
+ right: (cellInfo.endX - scrollXY.x) * scaleX,
+ top: ((cellInfo.startY - scrollXY.y) * scaleY) + bounding.top,
+ bottom: ((cellInfo.endY - scrollXY.y) * scaleY) + bounding.top,
+ };
+
+ return position;
+ }
+
+ private _createCellPositionObserver(
+ row: number,
+ col: number,
+ currentRender: IRender,
+ skeleton: SpreadsheetSkeleton,
+ activeViewport: Viewport
+ ) {
+ const position = this._calcCellPosition(row, col, currentRender, skeleton, activeViewport);
+ const position$ = new BehaviorSubject(position);
+
+ const disposableCollection = new DisposableCollection();
+ disposableCollection.add(this._commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id === SetWorksheetRowAutoHeightMutation.id) {
+ const params = commandInfo.params as ISetWorksheetRowAutoHeightMutationParams;
+ if (params.rowsAutoHeightInfo.findIndex((item) => item.row === row) > -1) {
+ position$.next(this._calcCellPosition(row, col, currentRender, skeleton, activeViewport));
+ return;
+ }
+ }
+
+ if (
+ COMMAND_LISTENER_SKELETON_CHANGE.indexOf(commandInfo.id) > -1 ||
+ commandInfo.id === SetScrollOperation.id ||
+ commandInfo.id === SetZoomRatioOperation.id
+ ) {
+ position$.next(this._calcCellPosition(row, col, currentRender, skeleton, activeViewport));
+ }
+ }));
+
+ return {
+ position$,
+ disposableCollection,
+ position,
+ };
+ }
+
+ private _createObjectPositionObserver(
+ targetObject: BaseObject,
+ currentRender: IRender,
+ skeleton: SpreadsheetSkeleton,
+ worksheet: Worksheet
+ ) {
+ const calc = () => {
+ const { scene } = currentRender;
+ const { left, top, width, height } = targetObject;
+
+ const bound: IBoundRectNoAngle = {
+ left,
+ right: left + width,
+ top,
+ bottom: top + height,
+ };
+
+ const offsetBound = transformBound2OffsetBound(bound, scene, skeleton, worksheet);
+ const bounding = currentRender.engine.getCanvasElement().getBoundingClientRect();
+
+ const position = {
+ left: offsetBound.left,
+ right: offsetBound.right,
+ top: offsetBound.top + bounding.top,
+ bottom: offsetBound.bottom + bounding.top,
+ };
+ return position;
+ };
+
+ const position = calc();
+ const position$ = new BehaviorSubject(position);
+ const disposableCollection = new DisposableCollection();
+ disposableCollection.add(this._commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id === SetScrollOperation.id || commandInfo.id === SetZoomRatioOperation.id) {
+ position$.next(calc());
+ }
+ }));
+
+ return {
+ position,
+ position$,
+ disposableCollection,
+ };
+ }
+
+ /**
+ * attach a popup to canvas object
+ * @param targetObject target canvas object
+ * @param popup popup item
+ * @returns disposable
+ */
+ attachPopupToObject(targetObject: BaseObject, popup: ICanvasPopup): IDisposable {
+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();
+ const worksheet = workbook.getActiveSheet();
+ const unitId = workbook.getUnitId();
+ const subUnitId = worksheet.getSheetId();
+ const skeleton = this._sheetSkeletonManagerService.getOrCreateSkeleton({
+ unitId,
+ sheetId: subUnitId,
+ });
+ const currentRender = this._renderManagerService.getRenderById(unitId);
+
+ if (!currentRender || !skeleton) {
+ return {
+ dispose: () => { },
+ };
+ }
+
+ const { position, position$, disposableCollection } = this._createObjectPositionObserver(targetObject, currentRender, skeleton, worksheet);
+
+ const id = this._globalPopupManagerService.addPopup({
+ ...popup,
+ unitId,
+ subUnitId,
+ anchorRect: position,
+ anchorRect$: position$,
+ });
+
+ return {
+ dispose: () => {
+ this._globalPopupManagerService.removePopup(id);
+ position$.complete();
+ disposableCollection.dispose();
+ },
+ };
+ }
+
+ /**
+ * attach a popup to given cell
+ * @param row cell row index
+ * @param col cell column index
+ * @param popup popup item
+ * @param viewport target viewport
+ * @returns disposable
+ */
+ attachPopupToCell(row: number, col: number, popup: ICanvasPopup, viewport?: Viewport) {
+ const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();
+ const worksheet = workbook.getActiveSheet();
+ const unitId = workbook.getUnitId();
+ const subUnitId = worksheet.getSheetId();
+ const skeleton = this._sheetSkeletonManagerService.getOrCreateSkeleton({
+ unitId,
+ sheetId: subUnitId,
+ });
+ const currentRender = this._renderManagerService.getRenderById(unitId);
+
+ if (!currentRender || !skeleton) {
+ return {
+ dispose: () => { },
+ };
+ }
+
+ const activeViewport = viewport ?? getViewportByCell(row, col, currentRender.scene, worksheet);
+
+ if (!activeViewport) {
+ return {
+ dispose: () => { },
+ };
+ }
+
+ const { position, position$, disposableCollection } = this._createCellPositionObserver(row, col, currentRender, skeleton, activeViewport);
+
+ const id = this._globalPopupManagerService.addPopup({
+ ...popup,
+ unitId,
+ subUnitId,
+ anchorRect: position,
+ anchorRect$: position$,
+ });
+
+ return {
+ dispose: () => {
+ this._globalPopupManagerService.removePopup(id);
+ disposableCollection.dispose();
+ position$.complete();
+ },
+ };
+ }
+}
diff --git a/packages/sheets-ui/src/sheets-ui-plugin.ts b/packages/sheets-ui/src/sheets-ui-plugin.ts
index 53d08fc570..b4c592fd45 100644
--- a/packages/sheets-ui/src/sheets-ui-plugin.ts
+++ b/packages/sheets-ui/src/sheets-ui-plugin.ts
@@ -62,6 +62,7 @@ import { SheetSkeletonManagerService } from './services/sheet-skeleton-manager.s
import { ShortcutExperienceService } from './services/shortcut-experience.service';
import { IStatusBarService, StatusBarService } from './services/status-bar.service';
import { SheetCanvasView } from './views/sheet-canvas-view';
+import { CanvasPopManagerService } from './services/canvas-pop-manager.service';
export class UniverSheetsUIPlugin extends Plugin {
static override type = PluginType.Sheet;
@@ -104,6 +105,7 @@ export class UniverSheetsUIPlugin extends Plugin {
],
[IStatusBarService, { useClass: StatusBarService }],
[IMarkSelectionService, { useClass: MarkSelectionService }],
+ [CanvasPopManagerService],
// controllers
[ActiveWorksheetController],
diff --git a/packages/ui/src/index.ts b/packages/ui/src/index.ts
index eaf1aec1e4..734a515fca 100644
--- a/packages/ui/src/index.ts
+++ b/packages/ui/src/index.ts
@@ -82,3 +82,4 @@ export { RangeSelector } from './components/range-selector/RangeSelector';
export { IRangeSelectorService } from './services/range-selector/range-selector.service';
export { DesktopLocalStorageService } from './services/local-storage/local-storage.service';
export { useActiveWorkbook, useActiveWorksheet } from './views/hooks/active';
+export { GlobalPopupManagerService, IGlobalPopupManagerService } from './services/popup/global-popup-manager.service';
diff --git a/packages/ui/src/services/popup/global-popup-manager.service.ts b/packages/ui/src/services/popup/global-popup-manager.service.ts
new file mode 100644
index 0000000000..c075aed31a
--- /dev/null
+++ b/packages/ui/src/services/popup/global-popup-manager.service.ts
@@ -0,0 +1,77 @@
+/**
+ * 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 { Tools } from '@univerjs/core';
+import type { IBoundRectNoAngle } from '@univerjs/engine-render';
+import { createIdentifier } from '@wendellhu/redi';
+import type { Observable } from 'rxjs';
+import { BehaviorSubject } from 'rxjs';
+
+export interface IPopup {
+ anchorRect: IBoundRectNoAngle;
+ anchorRect$: Observable<IBoundRectNoAngle>;
+ componentKey: string;
+ onClickOutside?: (e: MouseEvent) => void;
+ unitId: string;
+ subUnitId: string;
+ direction?: 'vertical' | 'horizontal';
+ offset?: [number, number];
+}
+
+export interface IGlobalPopupManagerService {
+ addPopup(item: IPopup): string;
+ removePopup(id: string): void;
+ removeAll(): void;
+
+ popups$: Observable<[string, IPopup][]>;
+
+ get popups(): [string, IPopup][];
+}
+
+export const IGlobalPopupManagerService = createIdentifier<IGlobalPopupManagerService>('univer.popup.service');
+
+export class GlobalPopupManagerService implements IGlobalPopupManagerService {
+ private _popupMap = new Map<string, IPopup>();
+ private _popups$ = new BehaviorSubject<[string, IPopup][]>([]);
+
+ popups$ = this._popups$.asObservable();
+
+ get popups() {
+ return Array.from(this._popupMap.entries());
+ }
+
+ private _notice() {
+ this._popups$.next(Array.from(this._popupMap.entries()));
+ }
+
+ addPopup(item: IPopup): string {
+ const id = Tools.generateRandomId();
+ this._popupMap.set(id, item);
+ this._notice();
+ return id;
+ }
+
+ removePopup(id: string): void {
+ if (this._popupMap.delete(id)) {
+ this._notice();
+ }
+ }
+
+ removeAll(): void {
+ this._popupMap.clear();
+ this._notice();
+ }
+}
diff --git a/packages/ui/src/ui-plugin.ts b/packages/ui/src/ui-plugin.ts
index 48fe932b4a..ac0d7a547e 100644
--- a/packages/ui/src/ui-plugin.ts
+++ b/packages/ui/src/ui-plugin.ts
@@ -18,6 +18,7 @@ import { ILocalStorageService, LocaleService, Plugin, PluginType } from '@univer
import type { Dependency } from '@wendellhu/redi';
import { Inject, Injector } from '@wendellhu/redi';
+import { GlobalPopupManagerService, IGlobalPopupManagerService } from './services/popup/global-popup-manager.service';
import { DesktopGlobalZoneService } from './services/global-zone/desktop-global-zone.service';
import { IGlobalZoneService } from './services/global-zone/global-zone.service';
import { ComponentManager } from './common/component-manager';
@@ -114,7 +115,7 @@ export class UniverUIPlugin extends Plugin {
[IBeforeCloseService, { useClass: DesktopBeforeCloseService }],
[IEditorService, { useClass: EditorService }],
[IRangeSelectorService, { useClass: RangeSelectorService }],
-
+ [IGlobalPopupManagerService, { useClass: GlobalPopupManagerService }],
// controllers
[IUIController, { useClass: DesktopUIController }],
[SharedController],
diff --git a/packages/ui/src/views/components/popup/GlobalPopup.tsx b/packages/ui/src/views/components/popup/GlobalPopup.tsx
new file mode 100644
index 0000000000..40fcd78ba8
--- /dev/null
+++ b/packages/ui/src/views/components/popup/GlobalPopup.tsx
@@ -0,0 +1,68 @@
+/**
+ * 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 { useDependency } from '@wendellhu/redi/react-bindings';
+import React, { useMemo } from 'react';
+import { RectPopup } from '@univerjs/design';
+import type { IBoundRectNoAngle } from '@univerjs/engine-render';
+import { IGlobalPopupManagerService } from '../../../services/popup/global-popup-manager.service';
+import { useObservable } from '../../../components/hooks/observable';
+import { ComponentManager } from '../../../common';
+import type { IPopup } from '../../../services/popup/global-popup-manager.service';
+
+const SingleGlobalPopup = ({ popup, children }: { popup: IPopup; children?: React.ReactNode }) => {
+ const anchorRect = useObservable(popup.anchorRect$, popup.anchorRect);
+ const rect: IBoundRectNoAngle = useMemo(() => {
+ const [x = 0, y = 0] = popup.offset ?? [];
+ return {
+ left: anchorRect.left - x,
+ right: anchorRect.right + x,
+ top: anchorRect.top - y,
+ bottom: anchorRect.bottom + y,
+ };
+ }, [anchorRect.bottom, anchorRect.left, anchorRect.right, anchorRect.top, popup.offset]);
+
+ return (
+ <RectPopup
+ anchorRect={rect}
+ direction={popup.direction}
+ onClickOutside={popup.onClickOutside}
+ >
+
+ {children}
+ </RectPopup>
+ );
+};
+
+export function GlobalPopup() {
+ const popupService = useDependency(IGlobalPopupManagerService);
+ const popups = useObservable(popupService.popups$, popupService.popups);
+ const componentManager = useDependency(ComponentManager);
+
+ return popups.map((item) => {
+ const [key, popup] = item;
+ const Component = componentManager.get(popup.componentKey);
+ return (
+ <SingleGlobalPopup
+ key={key}
+ popup={popup}
+ >
+
+ {Component ? <Component /> : null}
+ </SingleGlobalPopup>
+ );
+ });
+}
diff --git a/packages/ui/src/views/components/popup/index.ts b/packages/ui/src/views/components/popup/index.ts
new file mode 100644
index 0000000000..40e4b6e539
--- /dev/null
+++ b/packages/ui/src/views/components/popup/index.ts
@@ -0,0 +1,17 @@
+/**
+ * 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.
+ */
+
+export { GlobalPopup } from './GlobalPopup';
diff --git a/packages/ui/src/views/parts.ts b/packages/ui/src/views/parts.ts
index 87aff8a047..f6b25df07a 100644
--- a/packages/ui/src/views/parts.ts
+++ b/packages/ui/src/views/parts.ts
@@ -21,6 +21,7 @@ import { ConfirmPart } from './components/confirm-part/ConfirmPart';
import { ContextMenu } from './components/context-menu/ContextMenu';
import { DialogPart } from './components/dialog-part/DialogPart';
import { GlobalZone } from './components/global-zone/GlobalZone';
+import { GlobalPopup } from './components/popup';
export const globalComponents: Set<() => ComponentType> = new Set([
() => DialogPart,
@@ -28,4 +29,5 @@ export const globalComponents: Set<() => ComponentType> = new Set([
() => Notification,
() => ContextMenu,
() => GlobalZone,
+ () => GlobalPopup,
]);