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(sheets-ui): optimize popup service (#1912)

zhangwa555160a97f7
diff --git a/examples/src/sheets/main.ts b/examples/src/sheets/main.ts
index 6d6d19399b..12b76b6fc9 100644
--- a/examples/src/sheets/main.ts
+++ b/examples/src/sheets/main.ts
@@ -38,6 +38,9 @@ import { DebuggerPlugin } from '../plugins/debugger';
import { DEFAULT_WORKBOOK_DATA_DEMO } from '../data/sheets/demo/default-workbook-data-demo';
import { locales } from './locales';
+// const app = document.getElementById('app')!;
+// app.style.marginLeft = '100px';
+
const LOAD_LAZY_PLUGINS_TIMEOUT = 1_000;
// univer
const univer = new Univer({
diff --git a/packages/design/src/components/popup/RectPopup.tsx b/packages/design/src/components/popup/RectPopup.tsx
index 10c926b38b..58afc5d8d9 100644
--- a/packages/design/src/components/popup/RectPopup.tsx
+++ b/packages/design/src/components/popup/RectPopup.tsx
@@ -78,7 +78,7 @@ const calcPopupPosition = (layout: IPopupLayoutInfo) => {
function RectPopup(props: IRectPopupProps) {
const { children, anchorRect, direction = 'vertical', onClickOutside, excludeOutside } = props;
- const nodeRef = useRef(null);
+ const nodeRef = useRef<HTMLElement>(null);
const clickOtherFn = useEvent(onClickOutside ?? (() => {}));
const [position, setPosition] = useState<Partial<IAbsolutePosition>>({
@@ -112,27 +112,33 @@ function RectPopup(props: IRectPopupProps) {
]);
useEffect(() => {
- const handleClick = (e: MouseEvent) => {
- if (excludeOutside && (excludeOutside.indexOf(e.target as any) > -1)) {
- return;
- }
- if (e.clientX <= anchorRect.right && e.clientX >= anchorRect.left && e.clientY <= anchorRect.bottom && e.clientY >= anchorRect.top) {
- return;
- }
- clickOtherFn(e);
- };
+ const parent = nodeRef.current?.parentElement;
+ if (parent) {
+ const handleClick = (e: MouseEvent) => {
+ if (excludeOutside && (excludeOutside.indexOf(e.target as any) > -1)) {
+ return;
+ }
+ const bounding = parent.getBoundingClientRect();
- window.addEventListener('click', handleClick);
+ const x = e.clientX - bounding.left;
+ const y = e.clientY - bounding.top;
+ if (x <= anchorRect.right && x >= anchorRect.left && y <= anchorRect.bottom && y >= anchorRect.top) {
+ return;
+ }
+ clickOtherFn(e);
+ };
+ parent.addEventListener('click', handleClick);
- return () => {
- window.removeEventListener('click', handleClick);
- };
- }, [anchorRect.bottom, anchorRect.left, anchorRect.right, anchorRect.top, clickOtherFn, excludeOutside]);
+ return () => {
+ parent.removeEventListener('click', handleClick);
+ };
+ }
+ }, [anchorRect, anchorRect.bottom, anchorRect.left, anchorRect.right, anchorRect.top, clickOtherFn, excludeOutside]);
return (
<section
ref={nodeRef}
- className={styles.popup}
+ className={styles.popupAbsolute}
style={position}
onClick={(e) => {
e.stopPropagation();
diff --git a/packages/design/src/components/popup/index.module.less b/packages/design/src/components/popup/index.module.less
index 2a3442a92c..1c3de9d16c 100644
--- a/packages/design/src/components/popup/index.module.less
+++ b/packages/design/src/components/popup/index.module.less
@@ -1,3 +1,10 @@
+.popup-absolute {
+ position: absolute;
+ z-index: 1070;
+ top: -9999px;
+ left: -9999px;
+}
+
.popup {
position: fixed;
z-index: 1070;
diff --git a/packages/sheets-data-validation/src/controllers/dv-alert.controller.ts b/packages/sheets-data-validation/src/controllers/dv-alert.controller.ts
index a9e8e098a3..ca8ef4b10b 100644
--- a/packages/sheets-data-validation/src/controllers/dv-alert.controller.ts
+++ b/packages/sheets-data-validation/src/controllers/dv-alert.controller.ts
@@ -17,7 +17,9 @@
import { DataValidationStatus, Disposable, IUniverInstanceService, LifecycleStages, LocaleService, OnLifecycle } from '@univerjs/core';
import { CellAlertManagerService, CellAlertType, HoverManagerService } from '@univerjs/sheets-ui';
import { Inject } from '@wendellhu/redi';
-import { SheetDataValidationService } from '../services/dv.service';
+import type { BaseDataValidator } from '@univerjs/data-validation';
+
+const ALERT_KEY = 'SHEET_DATA_VALIDATION_ALERT';
@OnLifecycle(LifecycleStages.Rendered, DataValidationAlertController)
export class DataValidationAlertController extends Disposable {
@@ -25,7 +27,6 @@ export class DataValidationAlertController extends Disposable {
@Inject(HoverManagerService) private readonly _hoverManagerService: HoverManagerService,
@Inject(CellAlertManagerService) private readonly _cellAlertManagerService: CellAlertManagerService,
@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
- @Inject(SheetDataValidationService) private readonly _sheetDataValidationService: SheetDataValidationService,
@Inject(LocaleService) private readonly _localeService: LocaleService
) {
super();
@@ -41,11 +42,11 @@ export class DataValidationAlertController extends Disposable {
if (cellPos) {
const workbook = this._univerInstanceService.getCurrentUniverSheetInstance()!;
const worksheet = workbook.getActiveSheet();
- const manager = this._sheetDataValidationService.currentManager?.manager;
const cellData = worksheet.getCell(cellPos.location.row, cellPos.location.col);
if (cellData?.dataValidation?.validStatus === DataValidationStatus.INVALID) {
- const currentLoc = this._cellAlertManagerService.currentAlert?.location;
+ const currentAlert = this._cellAlertManagerService.currentAlert.get(ALERT_KEY);
+ const currentLoc = currentAlert?.alert?.location;
if (
currentLoc &&
currentLoc.row === cellPos.location.row &&
@@ -55,31 +56,25 @@ export class DataValidationAlertController extends Disposable {
) {
return;
}
+ const validator = cellData.dataValidation.validator as BaseDataValidator;
+ const rule = cellData.dataValidation.rule;
+ if (!validator) {
+ return;
+ }
this._cellAlertManagerService.showAlert({
type: CellAlertType.ERROR,
title: this._localeService.t('dataValidation.error.title'),
- message: manager?.getRuleErrorMsg(cellData.dataValidation.ruleId),
+ message: validator?.generateRuleErrorMessage(rule),
location: cellPos.location,
width: 200,
height: 74,
+ key: ALERT_KEY,
});
return;
}
-
- // if (cellData?.dataValidation?.rule.showInputMessage) {
- // this._cellAlertManagerService.showAlert({
- // type: CellAlertType.INFO,
- // title: this._localeService.t('dataValidation.error.title'),
- // message: manager?.getRuleErrorMsg(cellData.dataValidation.ruleId),
- // location: cellPos.location,
- // width: 200,
- // height: 74,
- // });
- // return;
- // }
}
- this._cellAlertManagerService.clearAlert();
+ this._cellAlertManagerService.removeAlert(ALERT_KEY);
}));
}
}
diff --git a/packages/sheets-data-validation/src/controllers/dv-render.controller.ts b/packages/sheets-data-validation/src/controllers/dv-render.controller.ts
index 9d0f332af7..8d4e2d3232 100644
--- a/packages/sheets-data-validation/src/controllers/dv-render.controller.ts
+++ b/packages/sheets-data-validation/src/controllers/dv-render.controller.ts
@@ -14,6 +14,7 @@
* limitations under the License.
*/
+
import type { ICellDataForSheetInterceptor, ICellRenderContext } from '@univerjs/core';
import { DataValidationRenderMode, DataValidationStatus, DataValidationType, IUniverInstanceService, LifecycleStages, OnLifecycle, RxDisposable, WrapStrategy } from '@univerjs/core';
import { DataValidationModel, DataValidatorRegistryService } from '@univerjs/data-validation';
@@ -197,11 +198,13 @@ export class DataValidationRenderController extends RxDisposable {
}));
}
+ // eslint-disable-next-line max-lines-per-function
private _initViewModelIntercept() {
this.disposeWithMe(
this._sheetInterceptorService.intercept(
INTERCEPTOR_POINT.CELL_CONTENT,
{
+ // eslint-disable-next-line max-lines-per-function
handler: (cell, pos, next) => {
const { row, col, unitId, subUnitId } = pos;
const manager = this._dataValidationModel.ensureManager(unitId, subUnitId) as SheetDataValidationManager;
diff --git a/packages/sheets-data-validation/src/controllers/dv.controller.ts b/packages/sheets-data-validation/src/controllers/dv.controller.ts
index f676ca54d0..393abf8f31 100644
--- a/packages/sheets-data-validation/src/controllers/dv.controller.ts
+++ b/packages/sheets-data-validation/src/controllers/dv.controller.ts
@@ -14,7 +14,6 @@
* limitations under the License.
*/
-import type { Workbook } from '@univerjs/core';
import { DisposableCollection, IUniverInstanceService, LifecycleStages, OnLifecycle, RxDisposable, toDisposable } from '@univerjs/core';
import { DataValidationModel, DataValidatorRegistryService } from '@univerjs/data-validation';
import { Inject, Injector } from '@wendellhu/redi';
@@ -77,24 +76,28 @@ export class DataValidationController extends RxDisposable {
}
private _initInstanceChange() {
- // TODO@wzhudev: sheet added and sheet dispose event
- }
-
- private _initOnWorkbook(workbook: Workbook): void {
- const disposeCollection = new DisposableCollection();
+ const disposableCollection = new DisposableCollection();
+ this._univerInstanceService.currentSheet$.subscribe((workbook) => {
+ disposableCollection.dispose();
+ if (!workbook) {
+ return;
+ }
+ this._sheetDataValidationService.switchCurrent(workbook.getUnitId(), workbook.getActiveSheet().getSheetId());
+ disposableCollection.add(toDisposable(
+ workbook.activeSheet$.subscribe((worksheet) => {
+ if (worksheet) {
+ const unitId = workbook.getUnitId();
+ const subUnitId = worksheet.getSheetId();
+ this._sheetDataValidationService.switchCurrent(unitId, subUnitId);
+ }
+ })
+ ));
+ });
- this._sheetDataValidationService.switchCurrent(workbook.getUnitId(), workbook.getActiveSheet().getSheetId());
- disposeCollection.add(toDisposable(
- workbook.activeSheet$.subscribe((worksheet) => {
- if (worksheet) {
- const unitId = workbook.getUnitId();
- const subUnitId = worksheet.getSheetId();
- this._sheetDataValidationService.switchCurrent(unitId, subUnitId);
- }
- })
- ));
+ this.disposeWithMe(disposableCollection);
}
+
private _initCommandInterceptor() {
this._sheetInterceptorService.interceptCommand({
getMutations: (commandInfo) => {
diff --git a/packages/sheets-ui/src/controllers/cell-custom-render.controller.ts b/packages/sheets-ui/src/controllers/cell-custom-render.controller.ts
index 2402e86ad6..48241411eb 100644
--- a/packages/sheets-ui/src/controllers/cell-custom-render.controller.ts
+++ b/packages/sheets-ui/src/controllers/cell-custom-render.controller.ts
@@ -41,9 +41,12 @@ export class CellCustomRenderController extends Disposable {
this._initEventBinding();
}
+ // eslint-disable-next-line max-lines-per-function
private _initEventBinding() {
const disposableCollection = new DisposableCollection();
+ // eslint-disable-next-line max-lines-per-function
this._univerInstanceService.currentSheet$.subscribe((workbook) => {
+ disposableCollection.dispose();
if (workbook) {
const unitId = workbook.getUnitId();
@@ -160,8 +163,6 @@ export class CellCustomRenderController extends Disposable {
moveDisposable && disposableCollection.add(moveDisposable);
}
}
-
- disposableCollection.dispose();
});
this.disposeWithMe(disposableCollection);
diff --git a/packages/sheets-ui/src/services/canvas-pop-manager.service.ts b/packages/sheets-ui/src/services/canvas-pop-manager.service.ts
index 345abfe786..57b7e5ae08 100644
--- a/packages/sheets-ui/src/services/canvas-pop-manager.service.ts
+++ b/packages/sheets-ui/src/services/canvas-pop-manager.service.ts
@@ -29,13 +29,14 @@ import { SetScrollOperation } from '../commands/operations/scroll.operation';
import { SetZoomRatioOperation } from '../commands/operations/set-zoom-ratio.operation';
import { SheetSkeletonManagerService } from './sheet-skeleton-manager.service';
-interface ICanvasPopup {
+export interface ICanvasPopup {
componentKey: string;
mask?: boolean;
onClickOutside?: (e: MouseEvent) => void;
direction?: 'vertical' | 'horizontal';
offset?: [number, number];
excludeOutside?: HTMLElement[];
+ extraProps?: Record<string, any>;
}
export class SheetCanvasPopManagerService extends Disposable {
diff --git a/packages/sheets-ui/src/services/cell-alert-manager.service.ts b/packages/sheets-ui/src/services/cell-alert-manager.service.ts
index 752b4db722..24ba327a16 100644
--- a/packages/sheets-ui/src/services/cell-alert-manager.service.ts
+++ b/packages/sheets-ui/src/services/cell-alert-manager.service.ts
@@ -17,7 +17,6 @@
import { Subject } from 'rxjs';
import type { IDisposable } from '@wendellhu/redi';
import { Inject } from '@wendellhu/redi';
-import type { Nullable } from '@univerjs/core';
import type { ISheetLocation } from '@univerjs/sheets';
import { IRenderManagerService } from '@univerjs/engine-render';
import { CELL_ALERT_KEY } from '../views/cell-alert';
@@ -36,13 +35,12 @@ export interface ICellAlert {
location: ISheetLocation;
width: number;
height: number;
+ key: string;
}
export class CellAlertManagerService {
- private _currentAlert$ = new Subject<Nullable<ICellAlert>>();
- private _currentPopup: Nullable<IDisposable> = undefined;
-
- private _currentAlert: Nullable<ICellAlert> = null;
+ private _currentAlert$ = new Subject<[string, { alert: ICellAlert; dispose: IDisposable }][]>();
+ private _currentAlert: Map<string, { alert: ICellAlert; dispose: IDisposable }> = new Map();
get currentAlert() {
return this._currentAlert;
@@ -56,8 +54,19 @@ export class CellAlertManagerService {
) {}
showAlert(alert: ICellAlert) {
- this._currentPopup && this._currentPopup.dispose();
- this._currentPopup = null;
+ let lastPopup = this._currentAlert.get(alert.key);
+ lastPopup && lastPopup.dispose.dispose();
+ if (lastPopup) {
+ lastPopup.dispose.dispose();
+ } else {
+ lastPopup = {
+ alert,
+ dispose: {
+ dispose() {},
+ },
+ };
+ this._currentAlert.set(alert.key, lastPopup);
+ }
const { location } = alert;
const { row, col, unitId } = location;
@@ -67,19 +76,36 @@ export class CellAlertManagerService {
return;
}
- this._currentAlert = alert;
- this._currentAlert$.next(alert);
- this._currentPopup = this._canvasPopManagerService.attachPopupToCell(row, col, {
+ const disposable = this._canvasPopManagerService.attachPopupToCell(row, col, {
componentKey: CELL_ALERT_KEY,
direction: 'horizontal',
offset: [2, 0],
+ extraProps: {
+ alert,
+ },
});
+ if (disposable) {
+ lastPopup.dispose = disposable;
+ }
+
+ this._currentAlert$.next(Array.from(this._currentAlert.entries()));
+ }
+
+ removeAlert(key: string) {
+ const lastPopup = this._currentAlert.get(key);
+ if (lastPopup) {
+ this._currentAlert.delete(key);
+ lastPopup?.dispose.dispose();
+ this._currentAlert$.next(Array.from(this._currentAlert.entries()));
+ }
}
clearAlert() {
- this._currentPopup && this._currentPopup.dispose();
- this._currentPopup = null;
- this._currentAlert = null;
- this._currentAlert$.next(null);
+ this._currentAlert.forEach((alert) => {
+ alert.dispose.dispose();
+ });
+
+ this._currentAlert.clear();
+ this._currentAlert$.next(Array.from(this._currentAlert.entries()));
}
}
diff --git a/packages/sheets-ui/src/views/cell-alert/CellAlertPopup.tsx b/packages/sheets-ui/src/views/cell-alert/CellAlertPopup.tsx
index de64b7c6e6..b8f1bd3497 100644
--- a/packages/sheets-ui/src/views/cell-alert/CellAlertPopup.tsx
+++ b/packages/sheets-ui/src/views/cell-alert/CellAlertPopup.tsx
@@ -14,19 +14,21 @@
* limitations under the License.
*/
-import { useDependency } from '@wendellhu/redi/react-bindings';
-import { useObservable } from '@univerjs/ui';
import React from 'react';
import { ErrorSingle, WarningSingle } from '@univerjs/icons';
import cs from 'clsx';
-import { CellAlertManagerService, CellAlertType } from '../../services/cell-alert-manager.service';
+import type { ICellAlert } from '../../services/cell-alert-manager.service';
+import { CellAlertType } from '../../services/cell-alert-manager.service';
+import type { ICanvasPopup } from '../../services/canvas-pop-manager.service';
import styles from './index.module.less';
-export function CellAlert() {
- const cellAlertService = useDependency(CellAlertManagerService);
- const currentCell = useObservable(cellAlertService.currentAlert$, cellAlertService.currentAlert);
+export function CellAlert({ popup }: { popup: ICanvasPopup }) {
+ const alert = popup.extraProps?.alert;
- const { type, title, message } = currentCell || {};
+ if (!alert) {
+ return null;
+ }
+ const { type, title, message } = alert as ICellAlert;
const iconMap = {
[CellAlertType.ERROR]: <ErrorSingle className={cs(styles.cellAlertIcon, styles.cellAlertIconError)} />,
@@ -34,6 +36,7 @@ export function CellAlert() {
[CellAlertType.WARNING]: <WarningSingle className={cs(styles.cellAlertIcon, styles.cellAlertIconWarning)} />,
};
+
return (
<div className={styles.cellAlert}>
<div className={styles.cellAlertTitle}>
diff --git a/packages/ui/src/views/App.tsx b/packages/ui/src/views/App.tsx
index e6464564f8..98df9efaca 100644
--- a/packages/ui/src/views/App.tsx
+++ b/packages/ui/src/views/App.tsx
@@ -28,6 +28,7 @@ import { Toolbar } from './components/doc-bars/Toolbar';
import { Sidebar } from './components/sidebar/Sidebar';
import { ZenZone } from './components/zen-zone/ZenZone';
import { globalComponents } from './parts';
+import { CanvasPopup } from './components/popup';
export interface IUniverAppProps extends IWorkbenchOptions {
mountContainer: HTMLElement;
@@ -139,6 +140,7 @@ export function App(props: IUniverAppProps) {
<ZenZone />
</section>
+ <CanvasPopup />
</div>
<ComponentContainer components={globalComponents} />
diff --git a/packages/ui/src/views/app.module.less b/packages/ui/src/views/app.module.less
index 29564e6bf1..9cc4e51faf 100644
--- a/packages/ui/src/views/app.module.less
+++ b/packages/ui/src/views/app.module.less
@@ -5,6 +5,7 @@
height: 100%;
background-color: rgb(var(--bg-color));
min-height: 0;
+ position: relative;
}
&-header {
diff --git a/packages/ui/src/views/components/popup/CanvasPopup.tsx b/packages/ui/src/views/components/popup/CanvasPopup.tsx
index e6c970c14b..1aba5e8590 100644
--- a/packages/ui/src/views/components/popup/CanvasPopup.tsx
+++ b/packages/ui/src/views/components/popup/CanvasPopup.tsx
@@ -61,8 +61,7 @@ export function CanvasPopup() {
key={key}
popup={popup}
>
-
- {Component ? <Component /> : null}
+ {Component ? <Component popup={popup} /> : null}
</SingleCanvasPopup>
);
});
diff --git a/packages/ui/src/views/parts.ts b/packages/ui/src/views/parts.ts
index 0520c849fc..87aff8a047 100644
--- a/packages/ui/src/views/parts.ts
+++ b/packages/ui/src/views/parts.ts
@@ -21,7 +21,6 @@ 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 { CanvasPopup } from './components/popup';
export const globalComponents: Set<() => ComponentType> = new Set([
() => DialogPart,
@@ -29,5 +28,4 @@ export const globalComponents: Set<() => ComponentType> = new Set([
() => Notification,
() => ContextMenu,
() => GlobalZone,
- () => CanvasPopup,
]);