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): expose copied sheet name to interceptors (#7780)

wpxp12345614bb44a587d9
diff --git a/packages/sheets/src/commands/commands/__tests__/copy-worksheet.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/copy-worksheet.command.spec.ts
index c9a81f6c49..5fc0269062 100644
--- a/packages/sheets/src/commands/commands/__tests__/copy-worksheet.command.spec.ts
+++ b/packages/sheets/src/commands/commands/__tests__/copy-worksheet.command.spec.ts
@@ -15,12 +15,14 @@
*/
import type { Injector, Univer, Workbook, Worksheet } from '@univerjs/core';
+import type { ICopySheetCommandInterceptorParams } from '../copy-worksheet.command';
import { ICommandService, IConfigService, IUniverInstanceService, LocaleService, LocaleType, RedoCommand, UndoCommand, UniverInstanceType } from '@univerjs/core';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { SHEETS_PLUGIN_CONFIG_KEY } from '../../../config/config';
import enUS from '../../../locale/en-US';
import zhCN from '../../../locale/zh-CN';
import { SheetLazyExecuteScheduleService } from '../../../services/lazy-execute-schedule.service';
+import { SheetInterceptorService } from '../../../services/sheet-interceptor/sheet-interceptor.service';
import { CopyWorksheetEndMutation } from '../../mutations/copy-worksheet-end.mutation';
import { InsertSheetMutation } from '../../mutations/insert-sheet.mutation';
import { RemoveSheetMutation } from '../../mutations/remove-sheet.mutation';
@@ -123,6 +125,33 @@ describe('Test copy worksheet commands', () => {
expect(getCopyUniqueSheetName(workbook, localeService, name)).toBe('Sheet1 (Copy2)');
});
+ it('provides the generated sheet id and name to copy interceptors', async () => {
+ const workbook = get(IUniverInstanceService).getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
+ const interceptedParams: ICopySheetCommandInterceptorParams[] = [];
+ get(SheetInterceptorService).interceptCommand({
+ getMutations: (commandInfo) => {
+ if (commandInfo.id === CopySheetCommand.id) {
+ interceptedParams.push(commandInfo.params as ICopySheetCommandInterceptorParams);
+ }
+ return { redos: [], undos: [] };
+ },
+ });
+
+ expect(commandService.syncExecuteCommand(CopySheetCommand.id, {
+ unitId: 'test',
+ subUnitId: 'sheet1',
+ })).toBeTruthy();
+
+ const copiedSheet = workbook.getSheets()[1];
+ expect(interceptedParams).toHaveLength(1);
+ expect(interceptedParams[0]).toMatchObject({
+ unitId: 'test',
+ subUnitId: 'sheet1',
+ targetSubUnitId: copiedSheet.getSheetId(),
+ targetSubUnitName: copiedSheet.getName(),
+ });
+ });
+
it('split large sheet copy should schedule remaining mutations and disable redo', async () => {
const sourceWorkbook = get(IUniverInstanceService).getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
const sourceSheet = sourceWorkbook.getActiveSheet();
diff --git a/packages/sheets/src/commands/commands/copy-worksheet.command.ts b/packages/sheets/src/commands/commands/copy-worksheet.command.ts
index 92835e6cc8..3d137480c9 100644
--- a/packages/sheets/src/commands/commands/copy-worksheet.command.ts
+++ b/packages/sheets/src/commands/commands/copy-worksheet.command.ts
@@ -142,6 +142,7 @@ export interface ICopySheetCommandParams {
export interface ICopySheetCommandInterceptorParams extends ICopySheetCommandParams {
targetSubUnitId: string;
+ targetSubUnitName: string;
copyContext: Map<string, unknown>;
}
@@ -226,7 +227,13 @@ function buildCopySheetMutations(
const copyContext = new Map<string, unknown>();
const intercepted = sheetInterceptorService.onCommandExecute({
id: COPY_SHEET_COMMAND_ID,
- params: { unitId, subUnitId, targetSubUnitId: config.id, copyContext } satisfies ICopySheetCommandInterceptorParams,
+ params: {
+ unitId,
+ subUnitId,
+ targetSubUnitId: config.id,
+ targetSubUnitName: config.name,
+ copyContext,
+ } satisfies ICopySheetCommandInterceptorParams,
});
// Redos only include InsertSheetMutation (with first chunk), remaining mutations are scheduled