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(sheets-ui): preserve formula clipboard payload on shortcut paste (#7161)

wpxp123456d8d1f624a149
diff --git a/packages/sheets-ui/src/commands/commands/__tests__/confirm-and-clipboard.command.spec.ts b/packages/sheets-ui/src/commands/commands/__tests__/confirm-and-clipboard.command.spec.ts
index e28af68f80..9d0a7e5cee 100644
--- a/packages/sheets-ui/src/commands/commands/__tests__/confirm-and-clipboard.command.spec.ts
+++ b/packages/sheets-ui/src/commands/commands/__tests__/confirm-and-clipboard.command.spec.ts
@@ -280,8 +280,13 @@ describe('clipboard command branches', () => {
]);
expect(await SheetPasteCommand.handler(accessorNoData, { value: 'none' })).toBe(false);
- expect(await SheetPasteShortKeyCommand.handler(accessor, { htmlContent: '<b>x</b>', textContent: 'x', files: [] })).toBe(true);
- expect(legacyPaste).toHaveBeenCalledWith('<b>x</b>', 'x', []);
+ expect(await SheetPasteShortKeyCommand.handler(accessor, {
+ htmlContent: '<b>x</b>',
+ textContent: 'x',
+ files: [],
+ formulaClipboardPayload: '{"formulas":[]}',
+ })).toBe(true);
+ expect(legacyPaste).toHaveBeenCalledWith('<b>x</b>', 'x', [], '{"formulas":[]}');
expect(await SheetOptionalPasteCommand.handler(accessor, { type: PREDEFINED_HOOK_NAME_PASTE.SPECIAL_PASTE_VALUE })).toBe(true);
expect(rePasteWithPasteType).toHaveBeenCalledWith(PREDEFINED_HOOK_NAME_PASTE.SPECIAL_PASTE_VALUE);
diff --git a/packages/sheets-ui/src/commands/commands/clipboard.command.ts b/packages/sheets-ui/src/commands/commands/clipboard.command.ts
index 8e824f571c..186b08205b 100644
--- a/packages/sheets-ui/src/commands/commands/clipboard.command.ts
+++ b/packages/sheets-ui/src/commands/commands/clipboard.command.ts
@@ -57,6 +57,7 @@ export interface ISheetPasteByShortKeyParams {
htmlContent?: string;
textContent?: string;
files?: File[];
+ formulaClipboardPayload?: string;
}
export const SheetPasteCommand: IMultiCommand = {
@@ -97,8 +98,8 @@ export const SheetPasteShortKeyCommand: ICommand = {
type: CommandType.COMMAND,
handler: async (accessor: IAccessor, params: ISheetPasteByShortKeyParams) => {
const clipboardService = accessor.get(ISheetClipboardService);
- const { htmlContent, textContent, files } = params;
- clipboardService.legacyPaste(htmlContent, textContent, files);
+ const { htmlContent, textContent, files, formulaClipboardPayload } = params;
+ clipboardService.legacyPaste(htmlContent, textContent, files, formulaClipboardPayload);
return true;
},
diff --git a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
index 3fd9528ccf..577caf3586 100644
--- a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
+++ b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
@@ -79,7 +79,7 @@ import {
SetWorksheetColWidthMutation,
SetWorksheetColWidthMutationFactory,
} from '@univerjs/sheets';
-import { BuiltInUIPart, connectInjector, IMessageService, IUIPartsService } from '@univerjs/ui';
+import { BuiltInUIPart, connectInjector, IClipboardInterfaceService, IMessageService, IUIPartsService } from '@univerjs/ui';
import { Subject, takeUntil } from 'rxjs';
import {
SheetCopyCommand,
@@ -96,6 +96,7 @@ import { SetScrollOperation } from '../../commands/operations/scroll.operation';
import { SHEETS_UI_PLUGIN_CONFIG_KEY } from '../../config/config';
import {
escapeSpecialCode,
+ FORMULA_CLIPBOARD_MIME_TYPE,
ISheetClipboardService,
PREDEFINED_HOOK_NAME_COPY,
PREDEFINED_HOOK_NAME_PASTE,
@@ -139,6 +140,7 @@ export class SheetClipboardController extends RxDisposable {
@IContextService private readonly _contextService: IContextService,
@IConfigService private readonly _configService: IConfigService,
@ISheetClipboardService private readonly _sheetClipboardService: ISheetClipboardService,
+ @IClipboardInterfaceService private readonly _clipboardInterfaceService: IClipboardInterfaceService,
@IMessageService private readonly _messageService: IMessageService,
@Inject(LocaleService) private readonly _localService: LocaleService,
@IUIPartsService protected readonly _uiPartsService: IUIPartsService
@@ -156,7 +158,7 @@ export class SheetClipboardController extends RxDisposable {
private _pasteWithDoc() {
const sheetPasteShortKeyFn = (docSelectionRenderService: DocSelectionRenderService) => {
- docSelectionRenderService.onPaste$.pipe(takeUntil(this.dispose$)).subscribe((config) => {
+ docSelectionRenderService.onPaste$.pipe(takeUntil(this.dispose$)).subscribe(async (config) => {
if (!whenSheetEditorFocused(this._contextService)) {
return;
}
@@ -168,8 +170,14 @@ export class SheetClipboardController extends RxDisposable {
const htmlContent = clipboardEvent.clipboardData?.getData('text/html');
const textContent = clipboardEvent.clipboardData?.getData('text/plain');
const files = this._resolveClipboardFiles(clipboardEvent.clipboardData);
+ const formulaClipboardPayload = await this._readFormulaClipboardPayload();
- this._commandService.executeCommand(SheetPasteShortKeyCommand.id, { htmlContent, textContent, files });
+ this._commandService.executeCommand(SheetPasteShortKeyCommand.id, {
+ htmlContent,
+ textContent,
+ files,
+ formulaClipboardPayload,
+ });
});
docSelectionRenderService?.onKeydown$.subscribe((event) => {
@@ -210,6 +218,25 @@ export class SheetClipboardController extends RxDisposable {
return files.length > 0 ? files : undefined;
}
+ private async _readFormulaClipboardPayload(): Promise<string | undefined> {
+ if (!this._clipboardInterfaceService.supportClipboard) {
+ return undefined;
+ }
+
+ try {
+ const items = await this._clipboardInterfaceService.read();
+ const item = items.find((clipboardItem) => clipboardItem.types.includes(FORMULA_CLIPBOARD_MIME_TYPE));
+ if (!item) {
+ return undefined;
+ }
+
+ const blob = await item.getType(FORMULA_CLIPBOARD_MIME_TYPE);
+ return blob.text();
+ } catch {
+ return undefined;
+ }
+ }
+
private _init(): void {
// register sheet clipboard commands
[SheetCopyCommand, SheetCutCommand, SheetPasteCommand].forEach((command) =>
diff --git a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
index 2a9194b316..07c32abcaf 100644
--- a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
+++ b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
@@ -186,7 +186,7 @@ export interface ISheetClipboardService {
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.
+ legacyPaste(html?: string, text?: string, files?: File[], formulaClipboardPayload?: string): Promise<boolean>; // paste a HTML string or plain text directly.
rePasteWithPasteType(type: IPasteHookKeyType): boolean;
disposePasteOptionsCache(): void;
@@ -413,13 +413,13 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
return this._pasteInternal(copyId, pasteType);
}
- async legacyPaste(html?: string, text?: string, files?: File[]): Promise<boolean> {
+ async legacyPaste(html?: string, text?: string, files?: File[], formulaClipboardPayload?: string): Promise<boolean> {
const isFromExcel = htmlIsFromExcel(html ?? '');
if (files && !isFromExcel) {
return this._pasteFiles(files, PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE);
} else if (html) {
- return this._pasteHTML(html, PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE);
+ return this._pasteHTML(html, PREDEFINED_HOOK_NAME_PASTE.DEFAULT_PASTE, formulaClipboardPayload);
} else if (text) {
// Converts text with tabs and newlines into an HTML table
if (/[\n\t]/.test(text)) {