Skip to content

Commit

Permalink
feat(sheets): add set workbook name command (#2249)
Browse files Browse the repository at this point in the history
* feat(sheets): add set workbook name command

* feat: add exports
  • Loading branch information
wzhudev authored May 16, 2024
1 parent 87e0f84 commit 3c24cdd
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 16 deletions.
4 changes: 4 additions & 0 deletions packages/core/src/sheets/workbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
59 changes: 59 additions & 0 deletions packages/sheets/src/commands/commands/set-workbook-name.command.ts
Original file line number Diff line number Diff line change
@@ -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
*
* https://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;
},
};
26 changes: 10 additions & 16 deletions packages/sheets/src/commands/commands/set-worksheet-name.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
},
};
Original file line number Diff line number Diff line change
@@ -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
*
* https://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;
},
};
5 changes: 5 additions & 0 deletions packages/sheets/src/controllers/basic-worksheet.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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
Expand Down Expand Up @@ -200,6 +203,8 @@ export class BasicWorksheetController extends Disposable implements IDisposable
SetTextRotationCommand,
SetTextWrapCommand,
SetVerticalTextAlignCommand,
SetWorkbookNameCommand,
SetWorkbookNameMutation,
SetWorksheetActivateCommand,
SetWorksheetActiveOperation,
SetWorksheetColWidthMutation,
Expand Down
2 changes: 2 additions & 0 deletions packages/sheets/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 3c24cdd

Please sign in to comment.