diff --git a/packages/sheets-formula/src/controllers/update-formula.controller.ts b/packages/sheets-formula/src/controllers/update-formula.controller.ts index fdc6aa9fe2..8d8e5ce3fa 100644 --- a/packages/sheets-formula/src/controllers/update-formula.controller.ts +++ b/packages/sheets-formula/src/controllers/update-formula.controller.ts @@ -48,6 +48,7 @@ import { InsertSheetMutation, RemoveSheetMutation, SetBorderCommand, + SetRangeCustomMetadataCommand, SetRangeValuesMutation, SetStyleCommand, SheetInterceptorService, @@ -115,7 +116,8 @@ export class UpdateFormulaController extends Disposable { (options && options.syncOnly === true) || params.trigger === SetStyleCommand.id || params.trigger === SetBorderCommand.id || - params.trigger === ClearSelectionFormatCommand.id + params.trigger === ClearSelectionFormatCommand.id || + params.trigger === SetRangeCustomMetadataCommand.id ) { return; } diff --git a/packages/sheets/src/commands/commands/set-range-custom-metadata.command.ts b/packages/sheets/src/commands/commands/set-range-custom-metadata.command.ts new file mode 100644 index 0000000000..c3fcd77039 --- /dev/null +++ b/packages/sheets/src/commands/commands/set-range-custom-metadata.command.ts @@ -0,0 +1,70 @@ +/** + * Copyright 2023-present DreamNum Co., Ltd. + * + * 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 { CustomData, IAccessor, ICellData, ICommand, IRange } from '@univerjs/core'; +import type { ISheetCommandSharedParams } from '../utils/interface'; +import { + CommandType, + ICommandService, + IUniverInstanceService, + ObjectMatrix, + Tools, +} from '@univerjs/core'; +import { SetRangeValuesMutation } from '../mutations/set-range-values.mutation'; +import { getSheetCommandTarget } from './utils/target-util'; + +interface ICustomMetadata { + custom: CustomData; +}; + +export interface ISetRangeCustomMetadataCommandParams extends Partial { + range: IRange; + customMetadata: ICustomMetadata | ICustomMetadata[][]; +} + +/** + * The command to set custom metadata for a range of cells, and not support undo/redo. + */ +export const SetRangeCustomMetadataCommand: ICommand = { + id: 'sheet.command.set-range-custom-metadata', + type: CommandType.COMMAND, + + handler: (accessor: IAccessor, params: ISetRangeCustomMetadataCommandParams) => { + const target = getSheetCommandTarget(accessor.get(IUniverInstanceService), params); + if (!target) return false; + + const commandService = accessor.get(ICommandService); + const { unitId, subUnitId } = target; + const { range, customMetadata } = params; + + const { startRow, startColumn, endRow, endColumn } = range; + const isArray = Tools.isArray(customMetadata); + const cellValue = new ObjectMatrix(); + + for (let r = startRow; r <= endRow; r++) { + for (let c = startColumn; c <= endColumn; c++) { + const value = isArray ? (customMetadata as ICustomMetadata[][])[r - startRow][c - startColumn] : (customMetadata as ICustomMetadata); + cellValue.setValue(r, c, value); + } + } + + return commandService.syncExecuteCommand(SetRangeValuesMutation.id, { + unitId, + subUnitId, + cellValue: cellValue.getMatrix(), + }); + }, +}; diff --git a/packages/sheets/src/controllers/basic-worksheet.controller.ts b/packages/sheets/src/controllers/basic-worksheet.controller.ts index e5338555ee..f84591d31c 100644 --- a/packages/sheets/src/controllers/basic-worksheet.controller.ts +++ b/packages/sheets/src/controllers/basic-worksheet.controller.ts @@ -74,6 +74,7 @@ import { SetDefinedNameCommand } from '../commands/commands/set-defined-name.com import { CancelFrozenCommand, SetFrozenCommand } from '../commands/commands/set-frozen.command'; import { SetGridlinesColorCommand } from '../commands/commands/set-gridlines-color.command'; import { SetProtectionCommand } from '../commands/commands/set-protection.command'; +import { SetRangeCustomMetadataCommand } from '../commands/commands/set-range-custom-metadata.command'; import { SetRangeValuesCommand } from '../commands/commands/set-range-values.command'; import { SetRowDataCommand } from '../commands/commands/set-row-data.command'; import { @@ -275,6 +276,7 @@ export class BasicWorksheetController extends Disposable implements IDisposable SetFrozenMutation, CancelFrozenCommand, SetHorizontalTextAlignCommand, + SetRangeCustomMetadataCommand, SetRangeValuesCommand, SetRowHeightCommand, SetRowHiddenCommand, diff --git a/packages/sheets/src/facade/f-range.ts b/packages/sheets/src/facade/f-range.ts index a0dc8e0bd5..763537a605 100644 --- a/packages/sheets/src/facade/f-range.ts +++ b/packages/sheets/src/facade/f-range.ts @@ -15,13 +15,50 @@ */ import type { AbsoluteRefType, BorderStyleTypes, BorderType, CellValue, CustomData, ICellData, IColorStyle, IDocumentData, IObjectMatrixPrimitiveType, IRange, IStyleData, ITextDecoration, Nullable, Workbook, Worksheet } from '@univerjs/core'; -import type { ISetBorderBasicCommandParams, ISetHorizontalTextAlignCommandParams, ISetRangeValuesCommandParams, ISetSelectionsOperationParams, ISetStyleCommandParams, ISetTextRotationCommandParams, ISetTextWrapCommandParams, ISetVerticalTextAlignCommandParams, IStyleTypeValue, SplitDelimiterEnum } from '@univerjs/sheets'; +import type { + ISetBorderBasicCommandParams, + ISetHorizontalTextAlignCommandParams, + ISetRangeCustomMetadataCommandParams, + ISetRangeValuesCommandParams, + ISetSelectionsOperationParams, + ISetStyleCommandParams, + ISetTextRotationCommandParams, + ISetTextWrapCommandParams, + ISetVerticalTextAlignCommandParams, + IStyleTypeValue, + SplitDelimiterEnum, +} from '@univerjs/sheets'; import type { IFacadeClearOptions } from './f-worksheet'; import type { FHorizontalAlignment, FVerticalAlignment } from './utils'; import { BooleanNumber, covertCellValue, covertCellValues, DEFAULT_STYLES, Dimension, ICommandService, Inject, Injector, isNullCell, Rectangle, RichTextValue, TextStyleValue, WrapStrategy } from '@univerjs/core'; import { FBaseInitialable } from '@univerjs/core/facade'; import { FormulaDataModel, serializeRange, serializeRangeWithSheet } from '@univerjs/engine-formula'; -import { addMergeCellsUtil, ClearSelectionAllCommand, ClearSelectionContentCommand, ClearSelectionFormatCommand, DeleteRangeMoveLeftCommand, DeleteRangeMoveUpCommand, DeleteWorksheetRangeThemeStyleCommand, getAddMergeMutationRangeByType, getPrimaryForRange, InsertRangeMoveDownCommand, InsertRangeMoveRightCommand, RemoveWorksheetMergeCommand, SetBorderBasicCommand, SetHorizontalTextAlignCommand, SetRangeValuesCommand, SetSelectionsOperation, SetStyleCommand, SetTextRotationCommand, SetTextWrapCommand, SetVerticalTextAlignCommand, SetWorksheetRangeThemeStyleCommand, SheetRangeThemeService, SplitTextToColumnsCommand } from '@univerjs/sheets'; +import { + addMergeCellsUtil, + ClearSelectionAllCommand, + ClearSelectionContentCommand, + ClearSelectionFormatCommand, + DeleteRangeMoveLeftCommand, + DeleteRangeMoveUpCommand, + DeleteWorksheetRangeThemeStyleCommand, + getAddMergeMutationRangeByType, + getPrimaryForRange, + InsertRangeMoveDownCommand, + InsertRangeMoveRightCommand, + RemoveWorksheetMergeCommand, + SetBorderBasicCommand, + SetHorizontalTextAlignCommand, + SetRangeCustomMetadataCommand, + SetRangeValuesCommand, + SetSelectionsOperation, + SetStyleCommand, + SetTextRotationCommand, + SetTextWrapCommand, + SetVerticalTextAlignCommand, + SetWorksheetRangeThemeStyleCommand, + SheetRangeThemeService, + SplitTextToColumnsCommand, +} from '@univerjs/sheets'; import { FWorkbook } from './f-workbook'; import { FWorksheet } from './f-worksheet'; import { FRangePermission } from './permission/f-range-permission'; @@ -946,9 +983,18 @@ export class FRange extends FBaseInitialable { * ``` */ setCustomMetaData(data: CustomData): FRange { - return this.setValue({ - custom: data, - }); + const params: ISetRangeCustomMetadataCommandParams = { + unitId: this._workbook.getUnitId(), + subUnitId: this._worksheet.getSheetId(), + range: this._range, + customMetadata: { + custom: data, + }, + }; + + this._commandService.syncExecuteCommand(SetRangeCustomMetadataCommand.id, params); + + return this; } /** @@ -967,7 +1013,16 @@ export class FRange extends FBaseInitialable { * ``` */ setCustomMetaDatas(datas: CustomData[][]): FRange { - return this.setValues(datas.map((row) => row.map((data) => ({ custom: data })))); + const params: ISetRangeCustomMetadataCommandParams = { + unitId: this._workbook.getUnitId(), + subUnitId: this._worksheet.getSheetId(), + range: this._range, + customMetadata: datas.map((row) => row.map((data) => ({ custom: data }))), + }; + + this._commandService.syncExecuteCommand(SetRangeCustomMetadataCommand.id, params); + + return this; } /** diff --git a/packages/sheets/src/index.ts b/packages/sheets/src/index.ts index b53d4593a6..5fa6bd2319 100644 --- a/packages/sheets/src/index.ts +++ b/packages/sheets/src/index.ts @@ -137,6 +137,7 @@ export { type ICancelFrozenCommandParams, type ISetFrozenCommandParams } from '. export { CancelFrozenCommand, SetFrozenCommand } from './commands/commands/set-frozen.command'; export { type ISetGridlinesColorCommandParams, SetGridlinesColorCommand } from './commands/commands/set-gridlines-color.command'; export { SetProtectionCommand } from './commands/commands/set-protection.command'; +export { type ISetRangeCustomMetadataCommandParams, SetRangeCustomMetadataCommand } from './commands/commands/set-range-custom-metadata.command'; export { type ISetRangeValuesCommandParams, SetRangeValuesCommand } from './commands/commands/set-range-values.command'; export { type ISetRowDataCommandParams, SetRowDataCommand } from './commands/commands/set-row-data.command'; export {