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.

chore: fix clipboard menu memory leak (#4595)

ybzky022499229f8e
diff --git a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
index 94a24e5ac7..bf44a98e2a 100644
--- a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
+++ b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
@@ -41,6 +41,8 @@ import type {
ISheetClipboardHook,
ISheetDiscreteRangeLocation,
} from '../../services/clipboard/type';
+import type { IScrollStateWithSearchParam } from '../../services/scroll-manager.service';
+
import type { IUniverSheetsUIConfig } from '../config.schema';
import {
@@ -70,10 +72,9 @@ import {
} from '@univerjs/core';
import { MessageType } from '@univerjs/design';
-
import { DocSelectionRenderService } from '@univerjs/docs-ui';
-import { IRenderManagerService } from '@univerjs/engine-render';
+import { IRenderManagerService } from '@univerjs/engine-render';
import {
InsertColMutation,
InsertRowMutation,
@@ -89,7 +90,7 @@ import {
SetWorksheetRowHeightMutation,
} from '@univerjs/sheets';
import { BuiltInUIPart, IMessageService, IUIPartsService } from '@univerjs/ui';
-import { takeUntil } from 'rxjs';
+import { Subject, takeUntil } from 'rxjs';
import { AddWorksheetMergeCommand } from '../../commands/commands/add-worksheet-merge.command';
import {
SheetCopyCommand,
@@ -102,6 +103,7 @@ import {
SheetPasteShortKeyCommand,
SheetPasteValueCommand,
} from '../../commands/commands/clipboard.command';
+import { SetScrollOperation } from '../../commands/operations/scroll.operation';
import { ISheetClipboardService, PREDEFINED_HOOK_NAME } from '../../services/clipboard/clipboard.service';
import { SheetSkeletonManagerService } from '../../services/sheet-skeleton-manager.service';
import { ClipboardPopupMenu } from '../../views/clipboard/ClipboardPopupMenu';
@@ -133,6 +135,9 @@ const shouldRemoveShapeIds = [
];
export class SheetClipboardController extends RxDisposable {
+ private _refreshOptionalPaste$ = new Subject();
+ refreshOptionalPaste$ = this._refreshOptionalPaste$.asObservable();
+
constructor(
@Inject(Injector) private readonly _injector: Injector,
@IUniverInstanceService private readonly _currentUniverSheet: IUniverInstanceService,
@@ -152,6 +157,10 @@ export class SheetClipboardController extends RxDisposable {
this._pasteWithDoc();
}
+ refreshOptionalPaste() {
+ this._refreshOptionalPaste$.next(Math.random());
+ }
+
private _pasteWithDoc() {
const sheetPasteShortKeyFn = (docSelectionRenderService: DocSelectionRenderService) => {
docSelectionRenderService.onPaste$.pipe(takeUntil(this.dispose$)).subscribe((config) => {
@@ -906,6 +915,23 @@ export class SheetClipboardController extends RxDisposable {
}
})
);
+
+ this.disposeWithMe(
+ this._commandService.onCommandExecuted((command: ICommandInfo) => {
+ if (command.id === SetScrollOperation.id) {
+ if (!this._sheetClipboardService.getPasteMenuVisible()) {
+ return;
+ }
+ const params = command.params as IScrollStateWithSearchParam;
+ const scrollUnitId = params.unitId;
+ const pasteOptionsCache = this._sheetClipboardService.getPasteOptionsCache();
+ const menuUnitId = pasteOptionsCache?.target.unitId;
+ if (scrollUnitId === menuUnitId) {
+ this._refreshOptionalPaste$.next(Math.random());
+ }
+ }
+ })
+ );
}
private _initUIComponents() {
diff --git a/packages/sheets-ui/src/controllers/render-controllers/clipboard.render-controller.ts b/packages/sheets-ui/src/controllers/render-controllers/clipboard.render-controller.ts
new file mode 100644
index 0000000000..8b233235d5
--- /dev/null
+++ b/packages/sheets-ui/src/controllers/render-controllers/clipboard.render-controller.ts
@@ -0,0 +1,56 @@
+/**
+ * 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 { Workbook } from '@univerjs/core';
+import type { IRenderContext, IRenderModule } from '@univerjs/engine-render';
+import {
+ Disposable,
+ Inject,
+ toDisposable,
+} from '@univerjs/core';
+import { ISheetClipboardService } from '../../services/clipboard/clipboard.service';
+import { SheetSkeletonManagerService } from '../../services/sheet-skeleton-manager.service';
+import { SheetClipboardController } from '../clipboard/clipboard.controller';
+
+export class ClipboardRenderController extends Disposable implements IRenderModule {
+ constructor(
+ private readonly _context: IRenderContext<Workbook>,
+ @Inject(SheetSkeletonManagerService) private readonly _sheetSkeletonManagerService: SheetSkeletonManagerService,
+ @ISheetClipboardService private readonly _sheetClipboardService: ISheetClipboardService,
+ @Inject(SheetClipboardController) private readonly _sheetClipboardController: SheetClipboardController
+ ) {
+ super();
+ this._initialize();
+ }
+
+ private _initialize() {
+ this.disposeWithMe(
+ toDisposable(this._sheetSkeletonManagerService.currentSkeleton$.subscribe((skeleton) => {
+ if (!skeleton?.unitId) {
+ return;
+ }
+ if (!this._sheetClipboardService.getPasteMenuVisible()) {
+ return;
+ }
+ const pasteOptionsCache = this._sheetClipboardService.getPasteOptionsCache();
+ const menuUnitId = pasteOptionsCache?.target.unitId;
+ if (skeleton.unitId === menuUnitId) {
+ this._sheetClipboardController.refreshOptionalPaste();
+ }
+ }))
+ );
+ }
+}
diff --git a/packages/sheets-ui/src/plugin.ts b/packages/sheets-ui/src/plugin.ts
index 0a0b425e0f..4afde201b5 100644
--- a/packages/sheets-ui/src/plugin.ts
+++ b/packages/sheets-ui/src/plugin.ts
@@ -47,6 +47,7 @@ import { SheetPermissionInterceptorCanvasRenderController } from './controllers/
import { SheetPermissionInterceptorClipboardController } from './controllers/permission/sheet-permission-interceptor-clipboard.controller';
import { SheetPermissionInterceptorFormulaRenderController } from './controllers/permission/sheet-permission-interceptor-formula-render.controller';
import { SheetPermissionRenderController, SheetPermissionRenderManagerController, WorksheetProtectionRenderController } from './controllers/permission/sheet-permission-render.controller';
+import { ClipboardRenderController } from './controllers/render-controllers/clipboard.render-controller';
import { SheetContextMenuRenderController } from './controllers/render-controllers/contextmenu.render-controller';
import { EditorBridgeRenderController } from './controllers/render-controllers/editor-bridge.render-controller';
import { FormatPainterRenderController } from './controllers/render-controllers/format-painter.render-controller';
@@ -233,6 +234,7 @@ export class UniverSheetsUIPlugin extends Plugin {
[SheetCellEditorResizeService],
[FormatPainterRenderController],
+ [ClipboardRenderController],
[CellAlertRenderController],
[ForceStringAlertRenderController],
[MarkSelectionRenderController],
diff --git a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
index bcb6ccc368..614fd3f8e6 100644
--- a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
+++ b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
@@ -59,10 +59,11 @@ import {
SetSelectionsOperation,
SetWorksheetActiveOperation,
- SheetsSelectionsService } from '@univerjs/sheets';
+ SheetsSelectionsService,
+} from '@univerjs/sheets';
import { FILE__BMP_CLIPBOARD_MIME_TYPE, FILE__JPEG_CLIPBOARD_MIME_TYPE, FILE__WEBP_CLIPBOARD_MIME_TYPE, FILE_PNG_CLIPBOARD_MIME_TYPE, HTML_CLIPBOARD_MIME_TYPE, IClipboardInterfaceService, imageMimeTypeSet, INotificationService, IPlatformService, PLAIN_TEXT_CLIPBOARD_MIME_TYPE } from '@univerjs/ui';
-import { BehaviorSubject, Subject } from 'rxjs';
+import { BehaviorSubject } from 'rxjs';
import { rangeToDiscreteRange, virtualizeDiscreteRanges } from '../../controllers/utils/range-tools';
import { IMarkSelectionService } from '../mark-selection/mark-selection.service';
import { SheetSkeletonManagerService } from '../sheet-skeleton-manager.service';
@@ -113,8 +114,10 @@ HtmlToUSMService.use(UniverPastePlugin);
export interface ISheetClipboardService {
showMenu$: Observable<boolean>;
setShowMenu: (show: boolean) => void;
+ getPasteMenuVisible: () => boolean;
pasteOptionsCache$: Observable<IPasteOptionCache | null>;
+ getPasteOptionsCache: () => IPasteOptionCache | null;
updatePasteOptionsCache(cache: IPasteOptionCache | null): void;
copy(): Promise<boolean>;
@@ -152,7 +155,7 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
readonly pasteOptionsCache$ = this._pasteOptionsCache$.asObservable();
//Control the visibility of the Paste Options menu
- private readonly _showMenu$: Subject<boolean> = new Subject<boolean>();
+ private readonly _showMenu$ = new BehaviorSubject(false);
readonly showMenu$ = this._showMenu$.asObservable();
constructor(
@@ -187,6 +190,14 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
this._showMenu$.next(show);
}
+ getPasteMenuVisible() {
+ return this._showMenu$.getValue();
+ }
+
+ getPasteOptionsCache() {
+ return this._pasteOptionsCache$.getValue();
+ }
+
copyContentCache(): CopyContentCache {
return this._copyContentCache;
}
diff --git a/packages/sheets-ui/src/views/clipboard/ClipboardPopupMenu.tsx b/packages/sheets-ui/src/views/clipboard/ClipboardPopupMenu.tsx
index 654a73a75f..425af93d68 100644
--- a/packages/sheets-ui/src/views/clipboard/ClipboardPopupMenu.tsx
+++ b/packages/sheets-ui/src/views/clipboard/ClipboardPopupMenu.tsx
@@ -14,18 +14,17 @@
* limitations under the License.
*/
-import type { ICommandInfo } from '@univerjs/core';
import type { IDiscreteRange } from '../../controllers/utils/range-tools';
import type { IPasteHookKeyType } from '../../services/clipboard/type';
-import { ICommandService, IUniverInstanceService, LocaleService, toDisposable, useDependency, useObservable } from '@univerjs/core';
+import { ICommandService, IUniverInstanceService, LocaleService, useDependency, useObservable } from '@univerjs/core';
import { DropdownOverlay, DropdownProvider, DropdownTrigger } from '@univerjs/design';
import { convertTransformToOffsetX, convertTransformToOffsetY, IRenderManagerService } from '@univerjs/engine-render';
import { CheckMarkSingle, MoreDownSingle, PasteSpecial } from '@univerjs/icons';
import clsx from 'clsx';
-import { useCallback, useEffect, useState } from 'react';
+import { useState } from 'react';
import { SheetOptionalPasteCommand } from '../../commands/commands/clipboard.command';
-import { SetScrollOperation } from '../../commands/operations/scroll.operation';
-import { useActiveWorkbook, useSheetSkeleton } from '../../components/hook';
+import { useActiveWorkbook } from '../../components/hook';
+import { SheetClipboardController } from '../../controllers/clipboard/clipboard.controller';
import { getSheetObject } from '../../controllers/utils/component-tools';
import { ISheetClipboardService, PREDEFINED_HOOK_NAME } from '../../services/clipboard/clipboard.service';
import { ISheetSelectionRenderService } from '../../services/selection/base-selection-render.service';
@@ -111,7 +110,8 @@ const useMenuPosition = (range?: IDiscreteRange) => {
export const ClipboardPopupMenu = () => {
const clipboardService = useDependency(ISheetClipboardService);
- const showMenu = useObservable(clipboardService.showMenu$, true);
+ const showMenu = useObservable(clipboardService.showMenu$, false);
+ const clipboardController = useDependency(SheetClipboardController);
const pasteOptionsCache = useObservable(clipboardService.pasteOptionsCache$, null);
const localeService = useDependency(LocaleService);
const commandService = useDependency(ICommandService);
@@ -119,42 +119,12 @@ export const ClipboardPopupMenu = () => {
const [menuHovered, setMenuHovered] = useState(false);
const [menuVisible, setMenuVisible] = useState(false);
- const [_, setVersion] = useState(Math.random());
+ const version = useObservable(clipboardController.refreshOptionalPaste$, Math.random());
const range = pasteOptionsCache?.target.pastedRange;
const relativePosition = useMenuPosition(range);
- const sheetSkeletonManagerService = useSheetSkeleton();
-
- useEffect(() => {
- if (!showMenu) {
- setMenuHovered(false);
- setMenuVisible(false);
- }
- }, [showMenu]);
-
- const forceUpdate = useCallback(() => setVersion(Math.random()), []);
-
- useEffect(() => {
- const disposable = sheetSkeletonManagerService && toDisposable(
- sheetSkeletonManagerService.currentSkeleton$.subscribe((skeleton) => {
- if (skeleton) {
- forceUpdate();
- }
- }));
- return disposable?.dispose;
- }, [sheetSkeletonManagerService, forceUpdate]);
-
- useEffect(() => {
- const disposable = commandService.onCommandExecuted((command: ICommandInfo) => {
- if (command.id === SetScrollOperation.id) {
- forceUpdate();
- }
- });
- return disposable.dispose;
- }, [forceUpdate, commandService]);
-
if (!relativePosition || !showMenu) return null;
if (relativePosition.positionX < 50 || relativePosition.positionY < 30) return null;