From bb55b71583542d63aeb049c02a63e3217b688762 Mon Sep 17 00:00:00 2001 From: yuanbin Date: Tue, 5 Mar 2024 11:35:43 +0800 Subject: [PATCH 1/2] fix(sheet): fix unreasonable merged selections --- .../commands/add-worksheet-merge.command.ts | 19 +++++++---- .../commands/utils/selection-utils.ts | 34 +++++++++++++++++++ 2 files changed, 47 insertions(+), 6 deletions(-) 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.ts index 9f59cfaefd1..7ccd525c91b 100644 --- a/packages/sheets-ui/src/commands/commands/add-worksheet-merge.command.ts +++ b/packages/sheets-ui/src/commands/commands/add-worksheet-merge.command.ts @@ -37,6 +37,7 @@ import { IConfirmService } from '@univerjs/ui'; import type { IAccessor } from '@wendellhu/redi'; import { checkCellContentInRanges, getClearContentMutationParamsForRanges } from '../../common/utils'; +import { getMergeableSelectionsByType, MergeType } from './utils/selection-utils'; export interface IAddMergeCommandParams { value?: Dimension.ROWS | Dimension.COLUMNS; @@ -129,9 +130,11 @@ export const AddWorksheetMergeAllCommand: ICommand = { const commandService = accessor.get(ICommandService); const selectionManagerService = accessor.get(SelectionManagerService); const selections = selectionManagerService.getSelectionRanges(); - if (!selections?.length) { + const mergeableSelections = getMergeableSelectionsByType(MergeType.MergeAll, selections); + if (!mergeableSelections?.length) { return false; } + const univerInstanceService = accessor.get(IUniverInstanceService); const workbook = univerInstanceService.getCurrentUniverSheetInstance(); @@ -144,7 +147,7 @@ export const AddWorksheetMergeAllCommand: ICommand = { const subUnitId = workSheet.getSheetId(); return commandService.executeCommand(AddWorksheetMergeCommand.id, { - selections, + selections: mergeableSelections, unitId, subUnitId, } as IAddMergeCommandParams); @@ -158,9 +161,11 @@ export const AddWorksheetMergeVerticalCommand: ICommand = { const commandService = accessor.get(ICommandService); const selectionManagerService = accessor.get(SelectionManagerService); const selections = selectionManagerService.getSelectionRanges(); - if (!selections?.length) { + const mergeableSelections = getMergeableSelectionsByType(MergeType.MergeVertical, selections); + if (!mergeableSelections?.length) { return false; } + const univerInstanceService = accessor.get(IUniverInstanceService); const workbook = univerInstanceService.getCurrentUniverSheetInstance(); @@ -174,7 +179,7 @@ export const AddWorksheetMergeVerticalCommand: ICommand = { return commandService.executeCommand(AddWorksheetMergeCommand.id, { value: Dimension.COLUMNS, - selections, + selections: mergeableSelections, unitId, subUnitId, } as IAddMergeCommandParams); @@ -188,9 +193,11 @@ export const AddWorksheetMergeHorizontalCommand: ICommand = { const commandService = accessor.get(ICommandService); const selectionManagerService = accessor.get(SelectionManagerService); const selections = selectionManagerService.getSelectionRanges(); - if (!selections?.length) { + const mergeableSelections = getMergeableSelectionsByType(MergeType.MergeHorizontal, selections); + if (!mergeableSelections?.length) { return false; } + const univerInstanceService = accessor.get(IUniverInstanceService); const workbook = univerInstanceService.getCurrentUniverSheetInstance(); @@ -203,7 +210,7 @@ export const AddWorksheetMergeHorizontalCommand: ICommand = { const subUnitId = workSheet.getSheetId(); return commandService.executeCommand(AddWorksheetMergeCommand.id, { value: Dimension.ROWS, - selections, + selections: mergeableSelections, unitId, subUnitId, } as IAddMergeCommandParams); diff --git a/packages/sheets-ui/src/commands/commands/utils/selection-utils.ts b/packages/sheets-ui/src/commands/commands/utils/selection-utils.ts index 1eab7e6776c..424aa4588d2 100644 --- a/packages/sheets-ui/src/commands/commands/utils/selection-utils.ts +++ b/packages/sheets-ui/src/commands/commands/utils/selection-utils.ts @@ -18,6 +18,12 @@ import type { ICellData, IRange, ISelection, ISelectionCell, Nullable, ObjectMat import { Direction, getReverseDirection, RANGE_TYPE, Rectangle } from '@univerjs/core'; import { alignToMergedCellsBorders } from '@univerjs/sheets'; +export enum MergeType { + MergeAll = 'mergeAll', + MergeVertical = 'mergeVertical', + MergeHorizontal = 'mergeHorizontal', +} + export interface IExpandParams { left?: boolean; right?: boolean; @@ -708,3 +714,31 @@ export function isAllColumnsCovered(allColumnRanges: IRange[], ranges: IRange[]) return true; }); } + +export function getMergeableSelectionsByType(type: MergeType, selections: Nullable): Nullable { + if (!selections) return null; + if (type === MergeType.MergeAll) { + return selections.filter((selection) => { + if (selection.startRow === selection.endRow && selection.startColumn === selection.endColumn) { + return false; + } + return true; + }); + } else if (type === MergeType.MergeVertical) { + return selections.filter((selection) => { + if (selection.startRow === selection.endRow) { + return false; + } + return true; + }); + } else if (type === MergeType.MergeHorizontal) { + return selections.filter((selection) => { + if (selection.startColumn === selection.endColumn) { + return false; + } + return true; + }); + } + + return selections; +} From fae073dc5f8846b040dbd24d862077faefb882dd Mon Sep 17 00:00:00 2001 From: yuanbin Date: Tue, 5 Mar 2024 15:15:25 +0800 Subject: [PATCH 2/2] test: update test --- .../__tests__/add-worksheet-merge.command.spec.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/sheets-ui/src/commands/commands/__tests__/add-worksheet-merge.command.spec.ts b/packages/sheets-ui/src/commands/commands/__tests__/add-worksheet-merge.command.spec.ts index f388041746e..1170f8099b5 100644 --- a/packages/sheets-ui/src/commands/commands/__tests__/add-worksheet-merge.command.spec.ts +++ b/packages/sheets-ui/src/commands/commands/__tests__/add-worksheet-merge.command.spec.ts @@ -108,6 +108,11 @@ describe('Test add worksheet merge commands', () => { primary: null, style: null, }, + { + range: { startRow: 10, startColumn: 10, endColumn: 10, endRow: 10, rangeType: RANGE_TYPE.NORMAL }, + primary: null, + style: null, + }, ]); function getMerge(): IRange[] | undefined { @@ -154,6 +159,11 @@ describe('Test add worksheet merge commands', () => { primary: null, style: null, }, + { + range: { startRow: 10, startColumn: 10, endColumn: 15, endRow: 10, rangeType: RANGE_TYPE.NORMAL }, + primary: null, + style: null, + }, ]); function getMerge(): IRange[] | undefined { @@ -198,6 +208,11 @@ describe('Test add worksheet merge commands', () => { primary: null, style: null, }, + { + range: { startRow: 10, startColumn: 10, endColumn: 10, endRow: 15, rangeType: RANGE_TYPE.NORMAL }, + primary: null, + style: null, + }, ]); function getMerge(): IRange[] | undefined {