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): add set workbook name command (#2249)

Wenzhao Hu3c24cdd1df07
diff --git a/packages/core/src/sheets/workbook.ts b/packages/core/src/sheets/workbook.ts
index 3709c9e571..d16717b0d3 100644
--- a/packages/core/src/sheets/workbook.ts
+++ b/packages/core/src/sheets/workbook.ts
@@ -134,6 +134,10 @@ export class Workbook extends UnitModel<IWorkbookData, UniverInstanceType.UNIVER
return this._snapshot.name;
}
+ setName(name: string): void {
+ this._snapshot.name = name;
+ }
+
getUnitId() {
return this._unitId;
}
diff --git a/packages/sheets/src/commands/commands/set-workbook-name.command.ts b/packages/sheets/src/commands/commands/set-workbook-name.command.ts
new file mode 100644
index 0000000000..f336b22fab
--- /dev/null
+++ b/packages/sheets/src/commands/commands/set-workbook-name.command.ts
@@ -0,0 +1,59 @@
+/**
+ * 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 { CommandType, type ICommand, ICommandService, IUniverInstanceService, sequenceExecute, UniverInstanceType } from '@univerjs/core';
+import type { IAccessor } from '@wendellhu/redi';
+import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';
+import type { ISetWorkbookNameMutationParams } from '../mutations/set-workbook-name.mutation';
+import { SetWorkbookNameMutation } from '../mutations/set-workbook-name.mutation';
+
+export interface ISetWorkbookNameCommandParams {
+ name: string;
+ unitId: string;
+}
+
+/**
+ * The command to set the workbook name. It does not support undo redo.
+ */
+export const SetWorkbookNameCommand: ICommand = {
+ type: CommandType.COMMAND,
+ id: 'sheet.command.set-workbook-name',
+ handler: async (accessor: IAccessor, params: ISetWorkbookNameCommandParams) => {
+ const instanceService = accessor.get(IUniverInstanceService);
+ const workbook = instanceService.getUnit(params.unitId, UniverInstanceType.UNIVER_SHEET);
+ if (!workbook) return false;
+
+ const sheetInterceptorService = accessor.get(SheetInterceptorService);
+ const interceptedCommands = sheetInterceptorService.onCommandExecute({
+ id: SetWorkbookNameCommand.id,
+ params,
+ });
+
+ const redoMutationParams: ISetWorkbookNameMutationParams = {
+ name: params.name,
+ unitId: params.unitId,
+ };
+
+ const redos = [
+ ...(interceptedCommands.preRedos ?? []),
+ { id: SetWorkbookNameMutation.id, params: redoMutationParams },
+ ...interceptedCommands.redos,
+ ];
+
+ const commandService = accessor.get(ICommandService);
+ return sequenceExecute(redos, commandService).result;
+ },
+};
diff --git a/packages/sheets/src/commands/commands/set-worksheet-name.command.ts b/packages/sheets/src/commands/commands/set-worksheet-name.command.ts
index 6d7cc131aa..aeb217a3e3 100644
--- a/packages/sheets/src/commands/commands/set-worksheet-name.command.ts
+++ b/packages/sheets/src/commands/commands/set-worksheet-name.command.ts
@@ -66,8 +66,16 @@ export const SetWorksheetNameCommand: ICommand = {
params,
});
- const redos = [...(interceptorCommands.preRedos ?? []), { id: SetWorksheetNameMutation.id, params: redoMutationParams }, ...interceptorCommands.redos];
- const undos = [...(interceptorCommands.preUndos ?? []), { id: SetWorksheetNameMutation.id, params: undoMutationParams }, ...interceptorCommands.undos];
+ const redos = [
+ ...(interceptorCommands.preRedos ?? []),
+ { id: SetWorksheetNameMutation.id, params: redoMutationParams },
+ ...interceptorCommands.redos,
+ ];
+ const undos = [
+ ...(interceptorCommands.preUndos ?? []),
+ { id: SetWorksheetNameMutation.id, params: undoMutationParams },
+ ...interceptorCommands.undos,
+ ];
const result = await sequenceExecute(redos, commandService).result;
if (result) {
@@ -79,19 +87,5 @@ export const SetWorksheetNameCommand: ICommand = {
return true;
}
return false;
-
- // const result = commandService.syncExecuteCommand(SetWorksheetNameMutation.id, redoMutationParams);
-
- // if (result) {
- // undoRedoService.pushUndoRedo({
- // unitID: unitId,
- // undoMutations: [{ id: SetWorksheetNameMutation.id, params: undoMutationParams }],
- // redoMutations: [{ id: SetWorksheetNameMutation.id, params: redoMutationParams }],
- // });
-
- // return true;
- // }
-
- // return false;
},
};
diff --git a/packages/sheets/src/commands/mutations/set-workbook-name.mutation.ts b/packages/sheets/src/commands/mutations/set-workbook-name.mutation.ts
new file mode 100644
index 0000000000..c294dab4c2
--- /dev/null
+++ b/packages/sheets/src/commands/mutations/set-workbook-name.mutation.ts
@@ -0,0 +1,35 @@
+/**
+ * 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 { IMutation, Workbook } from '@univerjs/core';
+import { CommandType, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
+
+export interface ISetWorkbookNameMutationParams {
+ name: string;
+ unitId: string;
+}
+
+export const SetWorkbookNameMutation: IMutation<ISetWorkbookNameMutationParams> = {
+ id: 'sheet.mutation.set-workbook-name',
+ type: CommandType.MUTATION,
+ handler: (accessor, params) => {
+ const workbook = accessor.get(IUniverInstanceService).getUnit<Workbook>(params.unitId, UniverInstanceType.UNIVER_SHEET);
+ if (!workbook) return false;
+
+ workbook.setName(params.name);
+ return true;
+ },
+};
diff --git a/packages/sheets/src/controllers/basic-worksheet.controller.ts b/packages/sheets/src/controllers/basic-worksheet.controller.ts
index 5fe3dc9d23..b18fe608a5 100644
--- a/packages/sheets/src/controllers/basic-worksheet.controller.ts
+++ b/packages/sheets/src/controllers/basic-worksheet.controller.ts
@@ -113,6 +113,8 @@ import { InsertDefinedNameCommand } from '../commands/commands/insert-defined-na
import { RemoveDefinedNameCommand } from '../commands/commands/remove-defined-name.command';
import { SetDefinedNameCommand } from '../commands/commands/set-defined-name.command';
import { ScrollToCellOperation } from '../commands/operations/scroll-to-cell.operation';
+import { SetWorkbookNameCommand } from '../commands/commands/set-workbook-name.command';
+import { SetWorkbookNameMutation } from '../commands/mutations/set-workbook-name.mutation';
import { MAX_CELL_PER_SHEET_DEFAULT, MAX_CELL_PER_SHEET_KEY } from './config/config';
export interface IStyleTypeValue<T> {
@@ -125,6 +127,7 @@ export interface IStyleTypeValue<T> {
*/
@OnLifecycle(LifecycleStages.Starting, BasicWorksheetController)
export class BasicWorksheetController extends Disposable implements IDisposable {
+ // eslint-disable-next-line max-lines-per-function
constructor(
@ICommandService private readonly _commandService: ICommandService,
@IConfigService private readonly _configService: IConfigService
@@ -200,6 +203,8 @@ export class BasicWorksheetController extends Disposable implements IDisposable
SetTextRotationCommand,
SetTextWrapCommand,
SetVerticalTextAlignCommand,
+ SetWorkbookNameCommand,
+ SetWorkbookNameMutation,
SetWorksheetActivateCommand,
SetWorksheetActiveOperation,
SetWorksheetColWidthMutation,
diff --git a/packages/sheets/src/index.ts b/packages/sheets/src/index.ts
index a4cc6c2a9d..edbb1c8d08 100644
--- a/packages/sheets/src/index.ts
+++ b/packages/sheets/src/index.ts
@@ -263,6 +263,8 @@ export { INumfmtService } from './services/numfmt/type';
export { RefRangeService } from './services/ref-range/ref-range.service';
export type { EffectRefRangeParams, IOperator } from './services/ref-range/type';
export { EffectRefRangId, OperatorType } from './services/ref-range/type';
+export { type ISetWorkbookNameCommandParams, SetWorkbookNameCommand } from './commands/commands/set-workbook-name.command';
+export { type ISetWorkbookNameMutationParams, SetWorkbookNameMutation } from './commands/mutations/set-workbook-name.mutation';
export {
handleBaseInsertRange,
handleBaseMoveRowsCols,