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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
feat: move add merge command to sheets-ui (#622)
Wenzhao Hucbe0988d493e
diff --git a/packages/core/src/sheets/worksheet.ts b/packages/core/src/sheets/worksheet.tsindex 1ab0fdaf69..f8fd1a8a3a 100644--- a/packages/core/src/sheets/worksheet.ts+++ b/packages/core/src/sheets/worksheet.ts@@ -175,6 +175,9 @@ export class Worksheet {/*** Get cell matrix from a given range and pick out non-first cells of merged cells.+ *+ * Notice that `ICellData` here is not after copying. In another word, the object matrix here should be+ * considered as a slice of the original worksheet data matrix.*/// PERF: we could not skip indexes with merged cells, because we have already known the merged cells' rangegetMatrixWithMergedCells(@@ -196,7 +199,6 @@ export class Worksheet {createRowColIter(row, endRow, col, endCol).forEach((row, col) => {const v = matrix.getValue(row, col);if (v) {- // ICellData should combine merge inforet.setValue(row, col, v);}});@@ -447,4 +449,8 @@ export class Worksheet {getLastColumnWithContent(): number {return this._cellData.getRange().endColumn;}++ cellHasValue(value: ICellData) {+ return value && (value.v !== undefined || value.f !== undefined || value.p !== undefined);+ }}diff --git a/packages/core/src/types/interfaces/i-cell-data.ts b/packages/core/src/types/interfaces/i-cell-data.tsindex 8910f36ade..c37d2d4acf 100644--- a/packages/core/src/types/interfaces/i-cell-data.ts+++ b/packages/core/src/types/interfaces/i-cell-data.ts@@ -12,10 +12,13 @@ export type ICellV = string | number | boolean;* Cell data*/export interface ICellData {- // The unique key, a random string, is used for the plug-in to associate the cell. When the cell information changes, the plug-in does not need to change the data, reducing the pressure on the back-end interface- // id?: string;+ /**+ * The unique key, a random string, is used for the plug-in to associate the cell. When the cell information changes,+ * the plug-in does not need to change the data, reducing the pressure on the back-end interface id?: string.+ */p?: Nullable<IDocumentData>; // univer docs, set null for cell clear all- s?: Nullable<IStyleData | string>; // style id+ /** style id */+ s?: Nullable<IStyleData | string>;/*** Origin value*/diff --git a/packages/sheets-numfmt/src/numfmt-plugin.ts b/packages/sheets-numfmt/src/numfmt-plugin.tsindex 3ccbb7628f..1a37c9999f 100644--- a/packages/sheets-numfmt/src/numfmt-plugin.ts+++ b/packages/sheets-numfmt/src/numfmt-plugin.ts@@ -12,7 +12,11 @@ import { UserHabitController } from './controllers/user-habit.controller';export class NumfmtPlugin extends Plugin {static override type = PluginType.Sheet;- constructor(@Inject(Injector) override readonly _injector: Injector) {++ constructor(+ _config: unknown,+ @Inject(Injector) override readonly _injector: Injector+ ) {super(SHEET_NUMFMT_PLUGIN);}diff --git a/packages/sheets/src/commands/commands/__tests__/add-worksheet-merge.command.spec.ts b/packages/sheets-ui/src/commands/commands/__tests__/add-worksheet-merge.command.spec.tssimilarity index 84%rename from packages/sheets/src/commands/commands/__tests__/add-worksheet-merge.command.spec.tsrename to packages/sheets-ui/src/commands/commands/__tests__/add-worksheet-merge.command.spec.tsindex 83080bdd85..8cb7d0901e 100644--- a/packages/sheets/src/commands/commands/__tests__/add-worksheet-merge.command.spec.ts+++ b/packages/sheets-ui/src/commands/commands/__tests__/add-worksheet-merge.command.spec.ts@@ -1,19 +1,32 @@import type { IRange, Univer } from '@univerjs/core';-import { ICommandService, IUniverInstanceService, RANGE_TYPE, RedoCommand, UndoCommand } from '@univerjs/core';-import type { Injector } from '@wendellhu/redi';+import {+ ICommandService,+ IUniverInstanceService,+ LocaleService,+ RANGE_TYPE,+ RedoCommand,+ UndoCommand,+} from '@univerjs/core';+import {+ AddWorksheetMergeMutation,+ NORMAL_SELECTION_PLUGIN_NAME,+ RemoveWorksheetMergeCommand,+ RemoveWorksheetMergeMutation,+ SelectionManagerService,+ SetRangeValuesMutation,+} from '@univerjs/sheets';+import { createCommandTestBed } from '@univerjs/sheets/commands/commands/__tests__/create-command-test-bed.js';+import { type IConfirmPartMethodOptions, IConfirmService } from '@univerjs/ui';+import type { IDisposable, Injector } from '@wendellhu/redi';+import { Subject } from 'rxjs';import { afterEach, beforeEach, describe, expect, it } from 'vitest';-import { NORMAL_SELECTION_PLUGIN_NAME, SelectionManagerService } from '../../../services/selection-manager.service';-import { AddWorksheetMergeMutation } from '../../mutations/add-worksheet-merge.mutation';-import { RemoveWorksheetMergeMutation } from '../../mutations/remove-worksheet-merge.mutation';import {AddWorksheetMergeAllCommand,AddWorksheetMergeCommand,AddWorksheetMergeHorizontalCommand,AddWorksheetMergeVerticalCommand,} from '../add-worksheet-merge.command';-import { RemoveWorksheetMergeCommand } from '../remove-worksheet-merge.command';-import { createCommandTestBed } from './create-command-test-bed';describe('Test style commands', () => {let univer: Univer;@@ -21,12 +34,34 @@ describe('Test style commands', () => {let commandService: ICommandService;beforeEach(() => {- const testBed = createCommandTestBed();+ const testBed = createCommandTestBed(undefined, [+ [+ IConfirmService,+ {+ useClass: class MockConfirmService implements IConfirmService {+ confirmOptions$: Subject<IConfirmPartMethodOptions[]> = new Subject();++ open(params: IConfirmPartMethodOptions): IDisposable {+ throw new Error('Method not implemented.');+ }++ confirm(params: IConfirmPartMethodOptions): Promise<boolean> {+ return Promise.resolve(true);+ }++ close(id: string): void {+ throw new Error('Method not implemented.');+ }+ },+ },+ ],+ ]);univer = testBed.univer;get = testBed.get;commandService = get(ICommandService);+ commandService.registerCommand(SetRangeValuesMutation);commandService.registerCommand(AddWorksheetMergeCommand);commandService.registerCommand(AddWorksheetMergeAllCommand);commandService.registerCommand(AddWorksheetMergeVerticalCommand);@@ -34,6 +69,8 @@ describe('Test style commands', () => {commandService.registerCommand(RemoveWorksheetMergeCommand);commandService.registerCommand(AddWorksheetMergeMutation);commandService.registerCommand(RemoveWorksheetMergeMutation);++ get(LocaleService).load({});});afterEach(() => {diff --git a/packages/sheets-ui/src/commands/commands/add-worksheet-merge.command.ts b/packages/sheets-ui/src/commands/commands/add-worksheet-merge.command.tsnew file mode 100644index 0000000000..9e144ed76e--- /dev/null+++ b/packages/sheets-ui/src/commands/commands/add-worksheet-merge.command.ts@@ -0,0 +1,271 @@+import type { ICommand, IMutationInfo, IRange, Worksheet } from '@univerjs/core';+import {+ CommandType,+ Dimension,+ ICellData,+ ICommandService,+ IUndoRedoService,+ IUniverInstanceService,+ LocaleService,+ ObjectMatrix,+ sequenceExecute,+} from '@univerjs/core';+import type {+ IAddWorksheetMergeMutationParams,+ IRemoveWorksheetMergeMutationParams,+ ISetRangeValuesMutationParams,+} from '@univerjs/sheets';+import {+ AddMergeUndoMutationFactory,+ AddWorksheetMergeMutation,+ getAddMergeMutationRangeByType,+ RemoveMergeUndoMutationFactory,+ RemoveWorksheetMergeMutation,+ SelectionManagerService,+ SetRangeValuesMutation,+ SetRangeValuesUndoMutationFactory,+} from '@univerjs/sheets';+import { IConfirmService } from '@univerjs/ui';+import type { IAccessor } from '@wendellhu/redi';++export interface IAddMergeCommandParams {+ value?: Dimension.ROWS | Dimension.COLUMNS;+ selections: IRange[];+ workbookId: string;+ worksheetId: string;+}++function checkCellContentInRanges(worksheet: Worksheet, ranges: IRange[]): boolean {+ return ranges.some((range) => checkCellContentInRange(worksheet, range));+}++function checkCellContentInRange(worksheet: Worksheet, range: IRange): boolean {+ const { startRow, startColumn, endColumn, endRow } = range;+ const cellMatrix = worksheet.getMatrixWithMergedCells(startRow, startColumn, endRow, endColumn);++ let someCellGoingToBeRemoved = false;+ cellMatrix.forValue((row, col, cellData) => {+ if (cellData && (row !== startRow || col !== startColumn) && worksheet.cellHasValue(cellData)) {+ someCellGoingToBeRemoved = true;+ return false;+ }+ });+ return someCellGoingToBeRemoved;+}++function getClearContentMutationParamsForRanges(+ accessor: IAccessor,+ workbookId: string,+ worksheet: Worksheet,+ ranges: IRange[]+): {+ undos: IMutationInfo[];+ redos: IMutationInfo[];+} {+ const undos: IMutationInfo[] = [];+ const redos: IMutationInfo[] = [];++ const worksheetId = worksheet.getSheetId();++ // Use the following file as a reference.+ // packages/sheets/src/commands/commands/clear-selection-all.command.ts+ // packages/sheets/src/commands/mutations/set-range-values.mutation.ts+ ranges.forEach((range) => {+ const redoMatrix = getClearContentMutationParamForRange(worksheet, range);+ const redoMutationParams: ISetRangeValuesMutationParams = {+ workbookId,+ worksheetId,+ cellValue: redoMatrix.getData(),+ };+ const undoMutationParams: ISetRangeValuesMutationParams = SetRangeValuesUndoMutationFactory(+ accessor,+ redoMutationParams+ );++ undos.push({ id: SetRangeValuesMutation.id, params: undoMutationParams });+ redos.push({ id: SetRangeValuesMutation.id, params: redoMutationParams });+ });++ return {+ undos,+ redos,+ };+}++function getClearContentMutationParamForRange(worksheet: Worksheet, range: IRange): ObjectMatrix<ICellData> {+ const { startRow, startColumn, endColumn, endRow } = range;+ const cellMatrix = worksheet.getMatrixWithMergedCells(startRow, startColumn, endRow, endColumn);+ const redoMatrix = new ObjectMatrix<ICellData>();+ cellMatrix.forValue((row, col, cellData) => {+ if (cellData && (row !== startRow || col !== startColumn)) {+ redoMatrix.setValue(row, col, null);+ }+ });++ return redoMatrix;+}++export const AddWorksheetMergeCommand: ICommand = {+ type: CommandType.COMMAND,+ id: 'sheet.command.add-worksheet-merge',+ // eslint-disable-next-line max-lines-per-function+ handler: async (accessor: IAccessor, params: IAddMergeCommandParams) => {+ const commandService = accessor.get(ICommandService);+ const undoRedoService = accessor.get(IUndoRedoService);+ const univerInstanceService = accessor.get(IUniverInstanceService);+ const confirmService = accessor.get(IConfirmService);+ const localeService = accessor.get(LocaleService);++ const workbookId = params.workbookId;+ const worksheetId = params.worksheetId;+ const selections = params.selections;+ const ranges = getAddMergeMutationRangeByType(selections, params.value);+ const worksheet = univerInstanceService.getUniverSheetInstance(workbookId)!.getSheetBySheetId(worksheetId)!;++ const redoMutations: IMutationInfo[] = [];+ const undoMutations: IMutationInfo[] = [];++ // First we should check if there are values in the going-to-be-merged cells.+ const willClearSomeCell = checkCellContentInRanges(worksheet, ranges);+ if (willClearSomeCell) {+ const result = await confirmService.confirm({+ id: 'merge.confirm.add-worksheet-merge',+ title: {+ title: 'merge.confirm.title',+ },+ cancelText: localeService.t('merge.confirm.cancel'),+ confirmText: localeService.t('merge.confirm.confirm'),+ });++ if (!result) {+ return false;+ }+ }++ // prepare redo mutations+ const removeMergeMutationParams: IRemoveWorksheetMergeMutationParams = {+ workbookId,+ worksheetId,+ ranges,+ };+ const addMergeMutationParams: IAddWorksheetMergeMutationParams = {+ workbookId,+ worksheetId,+ ranges,+ };+ redoMutations.push({ id: RemoveWorksheetMergeMutation.id, params: removeMergeMutationParams });+ redoMutations.push({ id: AddWorksheetMergeMutation.id, params: addMergeMutationParams });++ // prepare undo mutations+ const undoRemoveMergeMutationParams = RemoveMergeUndoMutationFactory(accessor, removeMergeMutationParams);+ const undoMutationParams = AddMergeUndoMutationFactory(accessor, addMergeMutationParams);+ undoMutations.push({ id: RemoveWorksheetMergeMutation.id, params: undoMutationParams });+ undoMutations.push({ id: AddWorksheetMergeMutation.id, params: undoRemoveMergeMutationParams });++ // add set range values mutations to undo redo mutations+ if (willClearSomeCell) {+ const data = getClearContentMutationParamsForRanges(accessor, workbookId, worksheet, ranges);+ redoMutations.unshift(...data.redos);+ undoMutations.push(...data.undos);+ }++ const result = sequenceExecute(redoMutations, commandService);+ if (result.result) {+ undoRedoService.pushUndoRedo({+ unitID: workbookId,+ undoMutations,+ redoMutations,+ });+ return true;+ }+ return false;+ },+};++export const AddWorksheetMergeAllCommand: ICommand = {+ type: CommandType.COMMAND,+ id: 'sheet.command.add-worksheet-merge-all',+ handler: async (accessor) => {+ const commandService = accessor.get(ICommandService);+ const selectionManagerService = accessor.get(SelectionManagerService);+ const selections = selectionManagerService.getSelectionRanges();+ if (!selections?.length) {+ return false;+ }+ const univerInstanceService = accessor.get(IUniverInstanceService);++ const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ if (!workbook) return false;++ const workSheet = workbook.getActiveSheet();+ if (!workSheet) return false;++ const workbookId = workbook.getUnitId();+ const worksheetId = workSheet.getSheetId();++ return commandService.executeCommand(AddWorksheetMergeCommand.id, {+ selections,+ workbookId,+ worksheetId,+ } as IAddMergeCommandParams);+ },+};++export const AddWorksheetMergeVerticalCommand: ICommand = {+ type: CommandType.COMMAND,+ id: 'sheet.command.add-worksheet-merge-vertical',+ handler: async (accessor) => {+ const commandService = accessor.get(ICommandService);+ const selectionManagerService = accessor.get(SelectionManagerService);+ const selections = selectionManagerService.getSelectionRanges();+ if (!selections?.length) {+ return false;+ }+ const univerInstanceService = accessor.get(IUniverInstanceService);++ const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ if (!workbook) return false;++ const workSheet = workbook.getActiveSheet();+ if (!workSheet) return false;++ const workbookId = workbook.getUnitId();+ const worksheetId = workSheet.getSheetId();++ return commandService.executeCommand(AddWorksheetMergeCommand.id, {+ value: Dimension.COLUMNS,+ selections,+ workbookId,+ worksheetId,+ } as IAddMergeCommandParams);+ },+};++export const AddWorksheetMergeHorizontalCommand: ICommand = {+ type: CommandType.COMMAND,+ id: 'sheet.command.add-worksheet-merge-horizontal',+ handler: async (accessor) => {+ const commandService = accessor.get(ICommandService);+ const selectionManagerService = accessor.get(SelectionManagerService);+ const selections = selectionManagerService.getSelectionRanges();+ if (!selections?.length) {+ return false;+ }+ const univerInstanceService = accessor.get(IUniverInstanceService);++ const workbook = univerInstanceService.getCurrentUniverSheetInstance();+ if (!workbook) return false;++ const workSheet = workbook.getActiveSheet();+ if (!workSheet) return false;++ const workbookId = workbook.getUnitId();+ const worksheetId = workSheet.getSheetId();+ return commandService.executeCommand(AddWorksheetMergeCommand.id, {+ value: Dimension.ROWS,+ selections,+ workbookId,+ worksheetId,+ } as IAddMergeCommandParams);+ },+};diff --git a/packages/sheets-ui/src/controllers/menu/merge.menu.ts b/packages/sheets-ui/src/controllers/menu/merge.menu.tsindex 83e8a139d0..62dc9f204c 100644--- a/packages/sheets-ui/src/controllers/menu/merge.menu.ts+++ b/packages/sheets-ui/src/controllers/menu/merge.menu.ts@@ -1,13 +1,14 @@+import { RemoveWorksheetMergeCommand } from '@univerjs/sheets';+import type { IMenuButtonItem, IMenuSelectorItem } from '@univerjs/ui';+import { MenuGroup, MenuItemType, MenuPosition } from '@univerjs/ui';+import type { IAccessor } from '@wendellhu/redi';+import {AddWorksheetMergeAllCommand,AddWorksheetMergeCommand,AddWorksheetMergeHorizontalCommand,AddWorksheetMergeVerticalCommand,- RemoveWorksheetMergeCommand,-} from '@univerjs/sheets';-import type { IMenuButtonItem, IMenuSelectorItem } from '@univerjs/ui';-import { MenuGroup, MenuItemType, MenuPosition } from '@univerjs/ui';-import type { IAccessor } from '@wendellhu/redi';+} from '../../commands/commands/add-worksheet-merge.command';export function CellMergeMenuItemFactory(accessor: IAccessor): IMenuSelectorItem<string> {return {diff --git a/packages/sheets-ui/src/controllers/sheet-ui.controller.ts b/packages/sheets-ui/src/controllers/sheet-ui.controller.tsindex ebdac00536..1f24b11e4e 100644--- a/packages/sheets-ui/src/controllers/sheet-ui.controller.ts+++ b/packages/sheets-ui/src/controllers/sheet-ui.controller.ts@@ -13,6 +13,12 @@ import { ComponentManager, IMenuService, IShortcutService, IUIController } fromimport { Inject, Injector } from '@wendellhu/redi';import { connectInjector } from '@wendellhu/redi/react-bindings';+import {+ AddWorksheetMergeAllCommand,+ AddWorksheetMergeCommand,+ AddWorksheetMergeHorizontalCommand,+ AddWorksheetMergeVerticalCommand,+} from '../commands/commands/add-worksheet-merge.command';import {SetRangeBoldCommand,SetRangeFontFamilyCommand,@@ -215,6 +221,10 @@ export class SheetUIController extends Disposable {// init commands[+ AddWorksheetMergeAllCommand,+ AddWorksheetMergeCommand,+ AddWorksheetMergeHorizontalCommand,+ AddWorksheetMergeVerticalCommand,ChangeZoomRatioCommand,ExpandSelectionCommand,MoveSelectionCommand,diff --git a/packages/sheets-ui/src/locale/en-US.ts b/packages/sheets-ui/src/locale/en-US.tsindex 9026ffb4cf..acc0225ca8 100644--- a/packages/sheets-ui/src/locale/en-US.ts+++ b/packages/sheets-ui/src/locale/en-US.ts@@ -379,6 +379,11 @@ export default {cancel: 'Cancel merge',overlappingError: 'Cannot merge overlapping areas',partiallyError: 'Cannot perform this operation on partially merged cells',+ confirm: {+ title: 'Continue merging would only keep the upper-left cell value, discard other values. Are you sure to continue?',+ cancel: 'Cancel merging',+ confirm: 'Continue merging',+ },},textWrap: {overflow: 'Overflow',diff --git a/packages/sheets-ui/src/locale/zh-CN.ts b/packages/sheets-ui/src/locale/zh-CN.tsindex 29005929e5..8eb0fa8be9 100644--- a/packages/sheets-ui/src/locale/zh-CN.ts+++ b/packages/sheets-ui/src/locale/zh-CN.ts@@ -243,6 +243,11 @@ export default {cancel: '取消合并',overlappingError: '不能合并重叠区域',partiallyError: '无法对部分合并单元格执行此操作',+ confirm: {+ title: '合并单元格仅保存左上角单元格的值,是否继续?',+ cancel: '取消合并',+ confirm: '继续合并',+ },},textWrap: {overflow: '溢出',diff --git a/packages/sheets/src/commands/commands/__tests__/set-border.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/set-border.command.spec.tsindex e884e3662f..59bfffcec3 100644--- a/packages/sheets/src/commands/commands/__tests__/set-border.command.spec.ts+++ b/packages/sheets/src/commands/commands/__tests__/set-border.command.spec.ts@@ -3,10 +3,10 @@ import { BorderStyleTypes, BorderType, ICommandService, IUniverInstanceService,import type { Injector } from '@wendellhu/redi';import { afterEach, beforeEach, describe, expect, it } from 'vitest';-import { SetRangeValuesMutation } from '../../..';import { NORMAL_SELECTION_PLUGIN_NAME, SelectionManagerService } from '../../../services/selection-manager.service';import { AddWorksheetMergeMutation } from '../../mutations/add-worksheet-merge.mutation';import { RemoveWorksheetMergeMutation } from '../../mutations/remove-worksheet-merge.mutation';+import { SetRangeValuesMutation } from '../../mutations/set-range-values.mutation';import { AddWorksheetMergeAllCommand, AddWorksheetMergeCommand } from '../add-worksheet-merge.command';import { RemoveWorksheetMergeCommand } from '../remove-worksheet-merge.command';import {diff --git a/packages/sheets/src/commands/commands/add-worksheet-merge.command.ts b/packages/sheets/src/commands/commands/add-worksheet-merge.command.tsindex da04239a4a..46447347ca 100644--- a/packages/sheets/src/commands/commands/add-worksheet-merge.command.ts+++ b/packages/sheets/src/commands/commands/add-worksheet-merge.command.ts@@ -1,17 +1,31 @@-import type { ICommand, IRange } from '@univerjs/core';-import { CommandType, Dimension, ICommandService, IUndoRedoService, IUniverInstanceService } from '@univerjs/core';+// The file is for tests only.+// Please refer to packages/sheets-ui/src/commands/commands/add-worksheet-merge.command.ts++import type { ICellData, ICommand, IMutationInfo, IRange, Worksheet } from '@univerjs/core';+import {+ CommandType,+ Dimension,+ ICommandService,+ IUndoRedoService,+ IUniverInstanceService,+ ObjectMatrix,+ sequenceExecute,+} from '@univerjs/core';import type { IAccessor } from '@wendellhu/redi';import type {IAddWorksheetMergeMutationParams,IRemoveWorksheetMergeMutationParams,} from '../../basics/interfaces/mutation-interface';+import { getAddMergeMutationRangeByType } from '../../controllers/merge-cell.controller';import { SelectionManagerService } from '../../services/selection-manager.service';import { AddMergeUndoMutationFactory, AddWorksheetMergeMutation } from '../mutations/add-worksheet-merge.mutation';import {RemoveMergeUndoMutationFactory,RemoveWorksheetMergeMutation,} from '../mutations/remove-worksheet-merge.mutation';+import type { ISetRangeValuesMutationParams } from '../mutations/set-range-values.mutation';+import { SetRangeValuesMutation, SetRangeValuesUndoMutationFactory } from '../mutations/set-range-values.mutation';export interface IAddMergeCommandParams {value?: Dimension.ROWS | Dimension.COLUMNS;@@ -20,6 +34,76 @@ export interface IAddMergeCommandParams {worksheetId: string;}+function checkCellContentInRanges(worksheet: Worksheet, ranges: IRange[]): boolean {+ return ranges.some((range) => checkCellContentInRange(worksheet, range));+}++function checkCellContentInRange(worksheet: Worksheet, range: IRange): boolean {+ const { startRow, startColumn, endColumn, endRow } = range;+ const cellMatrix = worksheet.getMatrixWithMergedCells(startRow, startColumn, endRow, endColumn);++ let someCellGoingToBeRemoved = false;+ cellMatrix.forValue((row, col, cellData) => {+ if (cellData && (row !== startRow || col !== startColumn) && worksheet.cellHasValue(cellData)) {+ someCellGoingToBeRemoved = true;+ return false;+ }+ });+ return someCellGoingToBeRemoved;+}++function getClearContentMutationParamsForRanges(+ accessor: IAccessor,+ workbookId: string,+ worksheet: Worksheet,+ ranges: IRange[]+): {+ undos: IMutationInfo[];+ redos: IMutationInfo[];+} {+ const undos: IMutationInfo[] = [];+ const redos: IMutationInfo[] = [];++ const worksheetId = worksheet.getSheetId();++ // Use the following file as a reference.+ // packages/sheets/src/commands/commands/clear-selection-all.command.ts+ // packages/sheets/src/commands/mutations/set-range-values.mutation.ts+ ranges.forEach((range) => {+ const redoMatrix = getClearContentMutationParamForRange(worksheet, range);+ const redoMutationParams: ISetRangeValuesMutationParams = {+ workbookId,+ worksheetId,+ cellValue: redoMatrix.getData(),+ };+ const undoMutationParams: ISetRangeValuesMutationParams = SetRangeValuesUndoMutationFactory(+ accessor,+ redoMutationParams+ );++ undos.push({ id: SetRangeValuesMutation.id, params: undoMutationParams });+ redos.push({ id: SetRangeValuesMutation.id, params: redoMutationParams });+ });++ return {+ undos,+ redos,+ };+}++function getClearContentMutationParamForRange(worksheet: Worksheet, range: IRange): ObjectMatrix<ICellData> {+ const { startRow, startColumn, endColumn, endRow } = range;+ const cellMatrix = worksheet.getMatrixWithMergedCells(startRow, startColumn, endRow, endColumn);+ const redoMatrix = new ObjectMatrix<ICellData>();+ cellMatrix.forValue((row, col, cellData) => {+ if (cellData && (row !== startRow || col !== startColumn)) {+ redoMatrix.setValue(row, col, null);+ }+ });++ return redoMatrix;+}+export const AddWorksheetMergeCommand: ICommand = {type: CommandType.COMMAND,id: 'sheet.command.add-worksheet-merge',@@ -27,48 +111,53 @@ export const AddWorksheetMergeCommand: ICommand = {handler: async (accessor: IAccessor, params: IAddMergeCommandParams) => {const commandService = accessor.get(ICommandService);const undoRedoService = accessor.get(IUndoRedoService);+ const univerInstanceService = accessor.get(IUniverInstanceService);+const workbookId = params.workbookId;const worksheetId = params.worksheetId;const selections = params.selections;-const ranges = getAddMergeMutationRangeByType(selections, params.value);+ const worksheet = univerInstanceService.getUniverSheetInstance(workbookId)!.getSheetBySheetId(worksheetId)!;++ const redoMutations: IMutationInfo[] = [];+ const undoMutations: IMutationInfo[] = [];++ // First we should check if there are values in the going-to-be-merged cells.+ const willRemoveSomeCell = checkCellContentInRanges(worksheet, ranges);+ // prepare redo mutationsconst removeMergeMutationParams: IRemoveWorksheetMergeMutationParams = {workbookId,worksheetId,ranges,};- const undoRemoveMergeMutationParams: IAddWorksheetMergeMutationParams = RemoveMergeUndoMutationFactory(- accessor,- removeMergeMutationParams- );- const removeResult = commandService.syncExecuteCommand(- RemoveWorksheetMergeMutation.id,- removeMergeMutationParams- );-const addMergeMutationParams: IAddWorksheetMergeMutationParams = {workbookId,worksheetId,ranges,};- const undoMutationParams: IRemoveWorksheetMergeMutationParams = AddMergeUndoMutationFactory(- accessor,- addMergeMutationParams- );- const result = commandService.syncExecuteCommand(AddWorksheetMergeMutation.id, addMergeMutationParams);+ redoMutations.push({ id: RemoveWorksheetMergeMutation.id, params: removeMergeMutationParams });+ redoMutations.push({ id: AddWorksheetMergeMutation.id, params: addMergeMutationParams });++ // prepare undo mutations+ const undoRemoveMergeMutationParams = RemoveMergeUndoMutationFactory(accessor, removeMergeMutationParams);+ const undoMutationParams = AddMergeUndoMutationFactory(accessor, addMergeMutationParams);+ undoMutations.push({ id: RemoveWorksheetMergeMutation.id, params: undoMutationParams });+ undoMutations.push({ id: AddWorksheetMergeMutation.id, params: undoRemoveMergeMutationParams });++ // add set range values mutations to undo redo mutations+ if (willRemoveSomeCell) {+ const data = getClearContentMutationParamsForRanges(accessor, workbookId, worksheet, ranges);+ redoMutations.unshift(...data.redos);+ undoMutations.push(...data.undos);+ }- if (result && removeResult) {+ const result = sequenceExecute(redoMutations, commandService);+ if (result.result) {undoRedoService.pushUndoRedo({unitID: workbookId,- undoMutations: [- { id: RemoveWorksheetMergeMutation.id, params: undoMutationParams },- { id: AddWorksheetMergeMutation.id, params: undoRemoveMergeMutationParams },- ],- redoMutations: [- { id: RemoveWorksheetMergeMutation.id, params: removeMergeMutationParams },- { id: AddWorksheetMergeMutation.id, params: addMergeMutationParams },- ],+ undoMutations,+ redoMutations,});return true;}@@ -104,6 +193,7 @@ export const AddWorksheetMergeAllCommand: ICommand = {} as IAddMergeCommandParams);},};+export const AddWorksheetMergeVerticalCommand: ICommand = {type: CommandType.COMMAND,id: 'sheet.command.add-worksheet-merge-vertical',@@ -162,42 +252,3 @@ export const AddWorksheetMergeHorizontalCommand: ICommand = {} as IAddMergeCommandParams);},};--/**- * calculates the selection based on the merged cell type- * @param {IRange[]} selection- * @param {Dimension} [type]- * @return {*}- */-export const getAddMergeMutationRangeByType = (selection: IRange[], type?: Dimension) => {- let ranges = selection;- if (type !== undefined) {- const rectangles: IRange[] = [];- for (let i = 0; i < ranges.length; i++) {- const { startRow, endRow, startColumn, endColumn } = ranges[i];- if (type === Dimension.ROWS) {- for (let r = startRow; r <= endRow; r++) {- const data = {- startRow: r,- endRow: r,- startColumn,- endColumn,- };- rectangles.push(data);- }- } else if (type === Dimension.COLUMNS) {- for (let c = startColumn; c <= endColumn; c++) {- const data = {- startRow,- endRow,- startColumn: c,- endColumn: c,- };- rectangles.push(data);- }- }- }- ranges = rectangles;- }- return ranges;-};diff --git a/packages/sheets/src/commands/mutations/add-worksheet-merge.mutation.ts b/packages/sheets/src/commands/mutations/add-worksheet-merge.mutation.tsindex a11e418209..cc37402087 100644--- a/packages/sheets/src/commands/mutations/add-worksheet-merge.mutation.ts+++ b/packages/sheets/src/commands/mutations/add-worksheet-merge.mutation.ts@@ -41,6 +41,7 @@ export const AddWorksheetMergeMutation: IMutation<IAddWorksheetMergeMutationParaconst config = worksheet.getConfig()!;const mergeConfigData = config.mergeData;const mergeAppendData = params.ranges;+ console.log('config merge data', mergeAppendData);for (let i = 0; i < mergeAppendData.length; i++) {mergeConfigData.push(mergeAppendData[i]);}diff --git a/packages/sheets/src/controllers/basic-worksheet.controller.ts b/packages/sheets/src/controllers/basic-worksheet.controller.tsindex 449c8cd8c5..f16e5e9534 100644--- a/packages/sheets/src/controllers/basic-worksheet.controller.ts+++ b/packages/sheets/src/controllers/basic-worksheet.controller.ts@@ -2,12 +2,6 @@ import type { IStyleData } from '@univerjs/core';import { Disposable, ICommandService, IConfigService, LifecycleStages, OnLifecycle } from '@univerjs/core';import type { IDisposable } from '@wendellhu/redi';-import {- AddWorksheetMergeAllCommand,- AddWorksheetMergeCommand,- AddWorksheetMergeHorizontalCommand,- AddWorksheetMergeVerticalCommand,-} from '../commands/commands/add-worksheet-merge.command';import { ClearSelectionAllCommand } from '../commands/commands/clear-selection-all.command';import { ClearSelectionContentCommand } from '../commands/commands/clear-selection-content.command';import { ClearSelectionFormatCommand } from '../commands/commands/clear-selection-format.command';@@ -118,11 +112,7 @@ export class BasicWorksheetController extends Disposable implements IDisposablesuper();[- AddWorksheetMergeAllCommand,- AddWorksheetMergeCommand,- AddWorksheetMergeHorizontalCommand,AddWorksheetMergeMutation,- AddWorksheetMergeVerticalCommand,ClearSelectionAllCommand,ClearSelectionContentCommand,ClearSelectionFormatCommand,diff --git a/packages/sheets/src/controllers/merge-cell.controller.ts b/packages/sheets/src/controllers/merge-cell.controller.tsindex 1c6e3b27ad..e903a77c98 100644--- a/packages/sheets/src/controllers/merge-cell.controller.ts+++ b/packages/sheets/src/controllers/merge-cell.controller.ts@@ -1,5 +1,6 @@import type { IMutationInfo, IRange, Workbook } from '@univerjs/core';import {+ Dimension,Disposable,DisposableCollection,ICommandService,@@ -17,7 +18,6 @@ import type {IRemoveRowsMutationParams,IRemoveWorksheetMergeMutationParams,} from '../basics/interfaces/mutation-interface';-import { getAddMergeMutationRangeByType } from '../commands/commands/add-worksheet-merge.command';import { ClearSelectionAllCommand } from '../commands/commands/clear-selection-all.command';import { ClearSelectionFormatCommand } from '../commands/commands/clear-selection-format.command';import type { IDeleteRangeMoveLeftCommandParams } from '../commands/commands/delete-range-move-left.command';@@ -48,6 +48,45 @@ import type { EffectRefRangeParams } from '../services/ref-range/type';import { SelectionManagerService } from '../services/selection-manager.service';import { SheetInterceptorService } from '../services/sheet-interceptor/sheet-interceptor.service';+/**+ * calculates the selection based on the merged cell type+ * @param {IRange[]} selection+ * @param {Dimension} [type]+ * @return {*}+ */+export function getAddMergeMutationRangeByType(selection: IRange[], type?: Dimension) {+ let ranges = selection;+ if (type !== undefined) {+ const rectangles: IRange[] = [];+ for (let i = 0; i < ranges.length; i++) {+ const { startRow, endRow, startColumn, endColumn } = ranges[i];+ if (type === Dimension.ROWS) {+ for (let r = startRow; r <= endRow; r++) {+ const data = {+ startRow: r,+ endRow: r,+ startColumn,+ endColumn,+ };+ rectangles.push(data);+ }+ } else if (type === Dimension.COLUMNS) {+ for (let c = startColumn; c <= endColumn; c++) {+ const data = {+ startRow,+ endRow,+ startColumn: c,+ endColumn: c,+ };+ rectangles.push(data);+ }+ }+ }+ ranges = rectangles;+ }+ return ranges;+}+@OnLifecycle(LifecycleStages.Steady, MergeCellController)export class MergeCellController extends Disposable {constructor(diff --git a/packages/sheets/src/index.ts b/packages/sheets/src/index.tsindex f1cb11e2d2..d7dd454cb8 100644--- a/packages/sheets/src/index.ts+++ b/packages/sheets/src/index.ts@@ -42,13 +42,6 @@ export { NORMAL_SELECTION_PLUGIN_NAME, SelectionManagerService } from './service// #region commands-export {- AddWorksheetMergeAllCommand,- AddWorksheetMergeCommand,- AddWorksheetMergeHorizontalCommand,- AddWorksheetMergeVerticalCommand,- getAddMergeMutationRangeByType,-} from './commands/commands/add-worksheet-merge.command';export { ClearSelectionAllCommand } from './commands/commands/clear-selection-all.command';export { ClearSelectionContentCommand } from './commands/commands/clear-selection-content.command';export { ClearSelectionFormatCommand } from './commands/commands/clear-selection-format.command';@@ -224,6 +217,7 @@ export {} from './commands/mutations/set-worksheet-row-height.mutation';export { type ISetSelectionsOperationParams, SetSelectionsOperation } from './commands/operations/selection.operation';export { type ISheetCommandSharedParams } from './commands/utils/interface';+export { getAddMergeMutationRangeByType } from './controllers/merge-cell.controller';export type { FormatType, INumfmtItem, INumfmtItemWithCache } from './services/numfmt/type';export { INumfmtService } from './services/numfmt/type';export { RefRangeService } from './services/ref-range/ref-range.service';diff --git a/packages/ui/src/index.ts b/packages/ui/src/index.tsindex 4f89ac21dd..8ef8415e0e 100644--- a/packages/ui/src/index.ts+++ b/packages/ui/src/index.ts@@ -54,3 +54,4 @@ export { DesktopSidebarService } from './services/sidebar/desktop-sidebar.servicexport { ISidebarService } from './services/sidebar/sidebar.service';export { UIPlugin } from './ui-plugin';export * from './utils';+export { type IConfirmPartMethodOptions } from './views/components/confirm-part/interface';diff --git a/packages/ui/src/views/components/confirm-part/ConfirmPart.tsx b/packages/ui/src/views/components/confirm-part/ConfirmPart.tsxindex d8eb41bc62..4bdad2c9e8 100644--- a/packages/ui/src/views/components/confirm-part/ConfirmPart.tsx+++ b/packages/ui/src/views/components/confirm-part/ConfirmPart.tsx@@ -13,12 +13,12 @@ export function ConfirmPart() {const [confirmOptions, setConfirmOptions] = useState<IConfirmPartMethodOptions[]>([]);useEffect(() => {- const subscribtion = confirmService.confirmOptions$.subscribe((options: IConfirmPartMethodOptions[]) => {+ const subscription = confirmService.confirmOptions$.subscribe((options: IConfirmPartMethodOptions[]) => {setConfirmOptions(options);});return () => {- subscribtion.unsubscribe();+ subscription.unsubscribe();};}, []);