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(paste): optimize the paste menu to support copy and paste within Univer in environments that do not support the Clipboard API (#6406)

wpxp123456b6222ce051e9
diff --git a/packages/sheets-formula-ui/src/controllers/menu.ts b/packages/sheets-formula-ui/src/controllers/menu.ts
index e9eca3d247..69c17a5323 100644
--- a/packages/sheets-formula-ui/src/controllers/menu.ts
+++ b/packages/sheets-formula-ui/src/controllers/menu.ts
@@ -14,9 +14,9 @@
* limitations under the License.
*/
-import type { IAccessor, Workbook } from '@univerjs/core';
+import type { IAccessor } from '@univerjs/core';
import type { IMenuItem } from '@univerjs/ui';
-import { IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
+import { UniverInstanceType } from '@univerjs/core';
import {
RangeProtectionPermissionEditPoint,
RangeProtectionPermissionViewPoint,
@@ -26,9 +26,9 @@ import {
WorksheetEditPermission,
WorksheetSetCellValuePermission,
} from '@univerjs/sheets';
-import { getCurrentRangeDisable$ } from '@univerjs/sheets-ui';
-import { getMenuHiddenObservable, IClipboardInterfaceService, MenuItemType } from '@univerjs/ui';
-import { combineLatestWith, map, Observable, of, switchMap } from 'rxjs';
+import { getCurrentRangeDisable$, menuClipboardDisabledObservable } from '@univerjs/sheets-ui';
+import { getMenuHiddenObservable, MenuItemType } from '@univerjs/ui';
+import { combineLatestWith, map } from 'rxjs';
import { SheetCopyFormulaOnlyCommand, SheetOnlyPasteFormulaCommand } from '../commands/commands/formula-clipboard.command';
import { InsertFunctionOperation } from '../commands/operations/insert-function.operation';
import { MoreFunctionsOperation } from '../commands/operations/more-functions.operation';
@@ -198,23 +198,6 @@ export function MoreFunctionsMenuItemFactory(accessor: IAccessor): IMenuItem {
};
}
-function menuClipboardDisabledObservable(injector: IAccessor): Observable<boolean> {
- const univerInstanceService = injector.get(IUniverInstanceService);
- const workbook$ = univerInstanceService.getCurrentTypeOfUnit$<Workbook>(UniverInstanceType.UNIVER_SHEET);
- return workbook$.pipe(
- switchMap((workbook) => {
- if (!workbook) {
- return of(true);
- }
- const clipboardInterfaceService = injector.get(IClipboardInterfaceService);
- if (clipboardInterfaceService) {
- return new Observable((subscriber) => subscriber.next(!injector.get(IClipboardInterfaceService).supportClipboard)) as Observable<boolean>;
- }
- return of(true);
- })
- );
-}
-
// Right click menu - Copy Formula Only
export function CopyFormulaOnlyMenuItemFactory(accessor: IAccessor): IMenuItem {
return {
diff --git a/packages/sheets-ui/src/commands/commands/clipboard.command.ts b/packages/sheets-ui/src/commands/commands/clipboard.command.ts
index 54e9586225..8e824f571c 100644
--- a/packages/sheets-ui/src/commands/commands/clipboard.command.ts
+++ b/packages/sheets-ui/src/commands/commands/clipboard.command.ts
@@ -72,12 +72,20 @@ export const SheetPasteCommand: IMultiCommand = {
// TODO: @yuhongz: check if there is excel content in the clipboard, if so
// ask users to use shortcuts instead.
- const clipboardInterfaceService = accessor.get(IClipboardInterfaceService);
- const clipboardItems = await clipboardInterfaceService.read();
const sheetClipboardService = accessor.get(ISheetClipboardService);
- if (clipboardItems.length !== 0) {
- return sheetClipboardService.paste(clipboardItems[0], params?.value);
+ const clipboardInterfaceService = accessor.get(IClipboardInterfaceService);
+ if (clipboardInterfaceService.supportClipboard) {
+ const clipboardItems = await clipboardInterfaceService.read();
+ if (clipboardItems.length !== 0) {
+ return sheetClipboardService.paste(clipboardItems[0], params?.value);
+ }
+ }
+
+ const lastCopyId = sheetClipboardService.copyContentCache().getLastCopyId();
+ if (lastCopyId) {
+ console.warn('Since the current environment does not support the Clipboard API, we will use the internal copyId to paste the content.');
+ return sheetClipboardService.pasteByCopyId(lastCopyId, params?.value);
}
return false;
diff --git a/packages/sheets-ui/src/controllers/menu/menu.ts b/packages/sheets-ui/src/controllers/menu/menu.ts
index 4b45fbbed3..fb187c01db 100644
--- a/packages/sheets-ui/src/controllers/menu/menu.ts
+++ b/packages/sheets-ui/src/controllers/menu/menu.ts
@@ -101,6 +101,7 @@ import {
import { SetInfiniteFormatPainterCommand, SetOnceFormatPainterCommand } from '../../commands/commands/set-format-painter.command';
import { SetWorksheetColAutoWidthCommand } from '../../commands/commands/set-worksheet-auto-col-width.command';
import { MENU_ITEM_INPUT_COMPONENT } from '../../components/menu-item-input';
+import { ISheetClipboardService } from '../../services/clipboard/clipboard.service';
import { FormatPainterStatus, IFormatPainterService } from '../../services/format-painter/format-painter.service';
import { deriveStateFromActiveSheet$, getCurrentRangeDisable$, getObservableWithExclusiveRange$ } from './menu-util';
import { getFontStyleAtCursor } from './utils';
@@ -890,9 +891,20 @@ export function TextRotateMenuItemFactory(accessor: IAccessor): IMenuSelectorIte
// #region - copy cut paste
// TODO@wzhudev: maybe we should move these menu factory to @univerjs/ui
-function menuClipboardDisabledObservable(injector: IAccessor): Observable<boolean> {
- const clipboardDisabled$: Observable<boolean> = new Observable((subscriber) => subscriber.next(!injector.get(IClipboardInterfaceService).supportClipboard));
- return clipboardDisabled$;
+export function menuClipboardDisabledObservable(injector: IAccessor): Observable<boolean> {
+ return new Observable((subscriber) => {
+ const clipboardInterfaceService = injector.get(IClipboardInterfaceService);
+ const supportClipboard = clipboardInterfaceService.supportClipboard;
+
+ const sheetClipboardService = injector.get(ISheetClipboardService);
+ const subscription = sheetClipboardService.copyContentCache().lastCopyId$.subscribe((lastCopyId) => {
+ subscriber.next(!supportClipboard && !lastCopyId);
+ });
+
+ subscriber.next(!supportClipboard);
+
+ return () => subscription.unsubscribe();
+ });
}
export function CopyMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
@@ -1043,6 +1055,8 @@ export function PasteBesidesBorderMenuItemFactory(accessor: IAccessor): IMenuBut
};
}
+//#endregion
+
export function FitContentMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
return {
id: SetWorksheetRowIsAutoHeightCommand.id,
diff --git a/packages/sheets-ui/src/index.ts b/packages/sheets-ui/src/index.ts
index 0d7a6cd7c7..b3a8231e4f 100644
--- a/packages/sheets-ui/src/index.ts
+++ b/packages/sheets-ui/src/index.ts
@@ -106,7 +106,7 @@ export { EMBEDDING_FORMULA_EDITOR, isEmbeddingFormulaEditor } from './controller
export { isRangeSelector, RANGE_SELECTOR_SYMBOLS } from './controllers/editor/utils/is-range-selector';
export { HoverRenderController } from './controllers/hover-render.controller';
export { menuSchema as SheetsUIMenuSchema } from './controllers/menu.schema';
-export { COPY_SPECIAL_MENU_ID, PASTE_SPECIAL_MENU_ID, SheetMenuPosition } from './controllers/menu/menu';
+export { COPY_SPECIAL_MENU_ID, menuClipboardDisabledObservable, PASTE_SPECIAL_MENU_ID, SheetMenuPosition } from './controllers/menu/menu';
export { deriveStateFromActiveSheet$, getCurrentExclusiveRangeInterest$, getCurrentRangeDisable$, getObservableWithExclusiveRange$ } from './controllers/menu/menu-util';
export { SheetPermissionCheckUIController } from './controllers/permission/sheet-permission-check-ui.controller';
export { HeaderFreezeRenderController } from './controllers/render-controllers/freeze.render-controller';
diff --git a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
index c2429d5170..a78020975e 100644
--- a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
+++ b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
@@ -138,6 +138,7 @@ export interface ISheetClipboardService {
copy(options?: ICopyOptions): Promise<boolean>;
cut(): Promise<boolean>;
paste(item: ClipboardItem, pasteType?: string): Promise<boolean>; // get content from a ClipboardItem and paste it.
+ pasteByCopyId(copyId: string, pasteType?: string): Promise<boolean>; // paste content by internal copyId.
legacyPaste(html?: string, text?: string, files?: File[]): Promise<boolean>; // paste a HTML string or plain text directly.
rePasteWithPasteType(type: IPasteHookKeyType): boolean;
@@ -344,6 +345,10 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
return false;
}
+ async pasteByCopyId(copyId: string, pasteType = PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE): Promise<boolean> {
+ return this._pasteInternal(copyId, pasteType);
+ }
+
async legacyPaste(html?: string, text?: string, files?: File[]): Promise<boolean> {
const isFromExcel = htmlIsFromExcel(html ?? '');
diff --git a/packages/sheets-ui/src/services/clipboard/copy-content-cache.ts b/packages/sheets-ui/src/services/clipboard/copy-content-cache.ts
index edf9ea147c..39c7ec4953 100644
--- a/packages/sheets-ui/src/services/clipboard/copy-content-cache.ts
+++ b/packages/sheets-ui/src/services/clipboard/copy-content-cache.ts
@@ -16,9 +16,9 @@
import type { Nullable, ObjectMatrix } from '@univerjs/core';
import type { IDiscreteRange } from '../../controllers/utils/range-tools';
-
import type { COPY_TYPE, ICellDataWithSpanInfo } from './type';
import { generateRandomId, LRUMap } from '@univerjs/core';
+import { BehaviorSubject } from 'rxjs';
const COPY_CONTENT_CACHE_LIMIT = 10;
const ID_LENGTH = 6;
@@ -47,9 +47,12 @@ export function extractId(html: string) {
export class CopyContentCache {
private _cache = new LRUMap<string, ICopyContentCacheData>(COPY_CONTENT_CACHE_LIMIT);
+ private readonly _lastCopyId$: BehaviorSubject<string | null> = new BehaviorSubject<string | null>(null);
+ readonly lastCopyId$ = this._lastCopyId$.asObservable();
set(id: string, clipboardData: ICopyContentCacheData) {
this._cache.set(id, clipboardData);
+ this._lastCopyId$.next(id);
}
get(id: string) {
@@ -58,10 +61,14 @@ export class CopyContentCache {
del(id: string) {
this._cache.delete(id);
+ if (this._lastCopyId$.getValue() === id) {
+ this._lastCopyId$.next(null);
+ }
}
clear() {
this._cache.clear();
+ this._lastCopyId$.next(null);
}
clearWithUnitId(unitId: string) {
@@ -71,6 +78,10 @@ export class CopyContentCache {
}
});
}
+
+ getLastCopyId(): string | null {
+ return this._lastCopyId$.getValue();
+ }
}
export const copyContentCache = new CopyContentCache();