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.

fix(ui): canvas popup event bind error (#1683)

zhangw8a0bfd6be563
diff --git a/packages/design/src/components/popup/RectPopup.tsx b/packages/design/src/components/popup/RectPopup.tsx
index e38b2a463b..e2a8d83a75 100644
--- a/packages/design/src/components/popup/RectPopup.tsx
+++ b/packages/design/src/components/popup/RectPopup.tsx
@@ -73,7 +73,7 @@ const calcPopupPosition = (layout: IPopupLayoutInfo) => {
function RectPopup(props: IRectPopupProps) {
const { children, anchorRect, direction = 'vertical', onClickOutside } = props;
-
+ const mounted = useRef(false);
const nodeRef = useRef(null);
const clickOtherFn = useRef(onClickOutside);
@@ -109,13 +109,17 @@ function RectPopup(props: IRectPopupProps) {
]);
useEffect(() => {
+ mounted.current = true;
const handleClick = (e: MouseEvent) => {
clickOtherFn.current?.(e);
};
setTimeout(() => {
- window.addEventListener('click', handleClick);
+ if (mounted.current) {
+ window.addEventListener('click', handleClick);
+ }
}, 100);
return () => {
+ mounted.current = false;
window.removeEventListener('click', handleClick);
};
}, [clickOtherFn]);
diff --git a/packages/sheets-ui/src/index.ts b/packages/sheets-ui/src/index.ts
index 761a04c7a9..b1a59e775b 100644
--- a/packages/sheets-ui/src/index.ts
+++ b/packages/sheets-ui/src/index.ts
@@ -76,5 +76,5 @@ 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';
+export { SheetCanvasPopManagerService } from './services/canvas-pop-manager.service';
export { mergeSetRangeValues } from './services/clipboard/utils';
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 43de540aac..e4f872e620 100644
--- a/packages/sheets-ui/src/services/canvas-pop-manager.service.ts
+++ b/packages/sheets-ui/src/services/canvas-pop-manager.service.ts
@@ -15,15 +15,15 @@
*/
import type { Worksheet } from '@univerjs/core';
-import { Disposable, DisposableCollection, ICommandService, IUniverInstanceService } from '@univerjs/core';
+import { Disposable, DisposableCollection, ICommandService, IUniverInstanceService, Rectangle } 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 { ICanvasPopupService } 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 type { IRemoveColMutationParams, IRemoveRowsMutationParams, ISetWorksheetRowAutoHeightMutationParams } from '@univerjs/sheets';
+import { COMMAND_LISTENER_SKELETON_CHANGE, RemoveColMutation, RemoveRowMutation, SetWorksheetRowAutoHeightMutation } from '@univerjs/sheets';
import { getViewportByCell, transformBound2OffsetBound } from '../common/utils';
import { SetScrollOperation } from '../commands/operations/scroll.operation';
import { SetZoomRatioOperation } from '..';
@@ -37,9 +37,9 @@ interface ICanvasPopup {
offset?: [number, number];
}
-export class CanvasPopManagerService extends Disposable {
+export class SheetCanvasPopManagerService extends Disposable {
constructor(
- @Inject(IGlobalPopupManagerService) private readonly _globalPopupManagerService: IGlobalPopupManagerService,
+ @Inject(ICanvasPopupService) private readonly _globalPopupManagerService: ICanvasPopupService,
@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,
@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
@Inject(SheetSkeletonManagerService) private readonly _sheetSkeletonManagerService: SheetSkeletonManagerService,
@@ -88,8 +88,8 @@ export class CanvasPopManagerService extends Disposable {
const position = this._calcCellPosition(row, col, currentRender, skeleton, activeViewport);
const position$ = new BehaviorSubject(position);
- const disposableCollection = new DisposableCollection();
- disposableCollection.add(this._commandService.onCommandExecuted((commandInfo) => {
+ const disposable = new DisposableCollection();
+ disposable.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) {
@@ -109,7 +109,7 @@ export class CanvasPopManagerService extends Disposable {
return {
position$,
- disposableCollection,
+ disposable,
position,
};
}
@@ -145,8 +145,9 @@ export class CanvasPopManagerService extends Disposable {
const position = calc();
const position$ = new BehaviorSubject(position);
- const disposableCollection = new DisposableCollection();
- disposableCollection.add(this._commandService.onCommandExecuted((commandInfo) => {
+ const disposable = new DisposableCollection();
+
+ disposable.add(this._commandService.onCommandExecuted((commandInfo) => {
if (commandInfo.id === SetScrollOperation.id || commandInfo.id === SetZoomRatioOperation.id) {
position$.next(calc());
}
@@ -155,7 +156,7 @@ export class CanvasPopManagerService extends Disposable {
return {
position,
position$,
- disposableCollection,
+ disposable,
};
}
@@ -182,7 +183,7 @@ export class CanvasPopManagerService extends Disposable {
};
}
- const { position, position$, disposableCollection } = this._createObjectPositionObserver(targetObject, currentRender, skeleton, worksheet);
+ const { position, position$, disposable } = this._createObjectPositionObserver(targetObject, currentRender, skeleton, worksheet);
const id = this._globalPopupManagerService.addPopup({
...popup,
@@ -196,7 +197,7 @@ export class CanvasPopManagerService extends Disposable {
dispose: () => {
this._globalPopupManagerService.removePopup(id);
position$.complete();
- disposableCollection.dispose();
+ disposable.dispose();
},
};
}
@@ -234,7 +235,7 @@ export class CanvasPopManagerService extends Disposable {
};
}
- const { position, position$, disposableCollection } = this._createCellPositionObserver(row, col, currentRender, skeleton, activeViewport);
+ const { position, position$, disposable } = this._createCellPositionObserver(row, col, currentRender, skeleton, activeViewport);
const id = this._globalPopupManagerService.addPopup({
...popup,
@@ -244,11 +245,49 @@ export class CanvasPopManagerService extends Disposable {
anchorRect$: position$,
});
+ const onDispose = () => {
+ this._globalPopupManagerService.removePopup(id);
+ disposable.dispose();
+ position$.complete();
+ };
+
+ const commandDisposable = this._commandService.onCommandExecuted((commandInfo) => {
+ if (commandInfo.id === RemoveColMutation.id) {
+ const params = commandInfo.params as IRemoveColMutationParams;
+
+ if (Rectangle.contains(
+ params.range,
+ {
+ startColumn: col,
+ endColumn: col,
+ startRow: row,
+ endRow: row,
+ }
+ )) {
+ onDispose();
+ }
+ }
+
+ if (commandInfo.id === RemoveRowMutation.id) {
+ const params = commandInfo.params as IRemoveRowsMutationParams;
+ if (Rectangle.contains(
+ params.range,
+ {
+ startColumn: col,
+ endColumn: col,
+ startRow: row,
+ endRow: row,
+ }
+ )) {
+ onDispose();
+ }
+ }
+ });
+
return {
dispose: () => {
- this._globalPopupManagerService.removePopup(id);
- disposableCollection.dispose();
- position$.complete();
+ commandDisposable.dispose();
+ onDispose();
},
};
}
diff --git a/packages/sheets-ui/src/sheets-ui-plugin.ts b/packages/sheets-ui/src/sheets-ui-plugin.ts
index b4c592fd45..238033f2f4 100644
--- a/packages/sheets-ui/src/sheets-ui-plugin.ts
+++ b/packages/sheets-ui/src/sheets-ui-plugin.ts
@@ -62,7 +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';
+import { SheetCanvasPopManagerService } from './services/canvas-pop-manager.service';
export class UniverSheetsUIPlugin extends Plugin {
static override type = PluginType.Sheet;
@@ -105,7 +105,7 @@ export class UniverSheetsUIPlugin extends Plugin {
],
[IStatusBarService, { useClass: StatusBarService }],
[IMarkSelectionService, { useClass: MarkSelectionService }],
- [CanvasPopManagerService],
+ [SheetCanvasPopManagerService],
// controllers
[ActiveWorksheetController],
diff --git a/packages/ui/src/index.ts b/packages/ui/src/index.ts
index 734a515fca..a71f5eb8ff 100644
--- a/packages/ui/src/index.ts
+++ b/packages/ui/src/index.ts
@@ -82,4 +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';
+export { CanvasPopupService, ICanvasPopupService } from './services/popup/canvas-popup.service';
diff --git a/packages/ui/src/services/popup/global-popup-manager.service.ts b/packages/ui/src/services/popup/canvas-popup.service.ts
similarity index 89%
rename from packages/ui/src/services/popup/global-popup-manager.service.ts
rename to packages/ui/src/services/popup/canvas-popup.service.ts
index c075aed31a..fe411746df 100644
--- a/packages/ui/src/services/popup/global-popup-manager.service.ts
+++ b/packages/ui/src/services/popup/canvas-popup.service.ts
@@ -31,7 +31,7 @@ export interface IPopup {
offset?: [number, number];
}
-export interface IGlobalPopupManagerService {
+export interface ICanvasPopupService {
addPopup(item: IPopup): string;
removePopup(id: string): void;
removeAll(): void;
@@ -41,9 +41,9 @@ export interface IGlobalPopupManagerService {
get popups(): [string, IPopup][];
}
-export const IGlobalPopupManagerService = createIdentifier<IGlobalPopupManagerService>('univer.popup.service');
+export const ICanvasPopupService = createIdentifier<ICanvasPopupService>('univer.popup.service');
-export class GlobalPopupManagerService implements IGlobalPopupManagerService {
+export class CanvasPopupService implements ICanvasPopupService {
private _popupMap = new Map<string, IPopup>();
private _popups$ = new BehaviorSubject<[string, IPopup][]>([]);
diff --git a/packages/ui/src/ui-plugin.ts b/packages/ui/src/ui-plugin.ts
index ac0d7a547e..fb5f65aec8 100644
--- a/packages/ui/src/ui-plugin.ts
+++ b/packages/ui/src/ui-plugin.ts
@@ -18,7 +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 { CanvasPopupService, ICanvasPopupService } from './services/popup/canvas-popup.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';
@@ -115,7 +115,7 @@ export class UniverUIPlugin extends Plugin {
[IBeforeCloseService, { useClass: DesktopBeforeCloseService }],
[IEditorService, { useClass: EditorService }],
[IRangeSelectorService, { useClass: RangeSelectorService }],
- [IGlobalPopupManagerService, { useClass: GlobalPopupManagerService }],
+ [ICanvasPopupService, { useClass: CanvasPopupService }],
// controllers
[IUIController, { useClass: DesktopUIController }],
[SharedController],
diff --git a/packages/ui/src/views/components/popup/GlobalPopup.tsx b/packages/ui/src/views/components/popup/CanvasPopup.tsx
similarity index 83%
rename from packages/ui/src/views/components/popup/GlobalPopup.tsx
rename to packages/ui/src/views/components/popup/CanvasPopup.tsx
index 40fcd78ba8..301eb40060 100644
--- a/packages/ui/src/views/components/popup/GlobalPopup.tsx
+++ b/packages/ui/src/views/components/popup/CanvasPopup.tsx
@@ -18,12 +18,12 @@ 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 { ICanvasPopupService } from '../../../services/popup/canvas-popup.service';
import { useObservable } from '../../../components/hooks/observable';
import { ComponentManager } from '../../../common';
-import type { IPopup } from '../../../services/popup/global-popup-manager.service';
+import type { IPopup } from '../../../services/popup/canvas-popup.service';
-const SingleGlobalPopup = ({ popup, children }: { popup: IPopup; children?: React.ReactNode }) => {
+const SingleCanvasPopup = ({ 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 ?? [];
@@ -47,8 +47,8 @@ const SingleGlobalPopup = ({ popup, children }: { popup: IPopup; children?: Reac
);
};
-export function GlobalPopup() {
- const popupService = useDependency(IGlobalPopupManagerService);
+export function CanvasPopup() {
+ const popupService = useDependency(ICanvasPopupService);
const popups = useObservable(popupService.popups$, popupService.popups);
const componentManager = useDependency(ComponentManager);
@@ -56,13 +56,13 @@ export function GlobalPopup() {
const [key, popup] = item;
const Component = componentManager.get(popup.componentKey);
return (
- <SingleGlobalPopup
+ <SingleCanvasPopup
key={key}
popup={popup}
>
{Component ? <Component /> : null}
- </SingleGlobalPopup>
+ </SingleCanvasPopup>
);
});
}
diff --git a/packages/ui/src/views/components/popup/index.ts b/packages/ui/src/views/components/popup/index.ts
index 40e4b6e539..186cabf35d 100644
--- a/packages/ui/src/views/components/popup/index.ts
+++ b/packages/ui/src/views/components/popup/index.ts
@@ -14,4 +14,4 @@
* limitations under the License.
*/
-export { GlobalPopup } from './GlobalPopup';
+export { CanvasPopup } from './CanvasPopup';
diff --git a/packages/ui/src/views/parts.ts b/packages/ui/src/views/parts.ts
index f6b25df07a..0520c849fc 100644
--- a/packages/ui/src/views/parts.ts
+++ b/packages/ui/src/views/parts.ts
@@ -21,7 +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';
+import { CanvasPopup } from './components/popup';
export const globalComponents: Set<() => ComponentType> = new Set([
() => DialogPart,
@@ -29,5 +29,5 @@ export const globalComponents: Set<() => ComponentType> = new Set([
() => Notification,
() => ContextMenu,
() => GlobalZone,
- () => GlobalPopup,
+ () => CanvasPopup,
]);