Skip to content

Commit

Permalink
fix(sheet): fix unreasonable merged selections (#1477)
Browse files Browse the repository at this point in the history
* fix(sheet): fix unreasonable merged selections

* test: update test

---------

Co-authored-by: yuanbin <ybzkyfafa@gamil.com>
  • Loading branch information
ybzky and yuanbin committed Mar 5, 2024
1 parent 371b4f1 commit bd8c5df
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -144,7 +147,7 @@ export const AddWorksheetMergeAllCommand: ICommand = {
const subUnitId = workSheet.getSheetId();

return commandService.executeCommand(AddWorksheetMergeCommand.id, {
selections,
selections: mergeableSelections,
unitId,
subUnitId,
} as IAddMergeCommandParams);
Expand All @@ -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();
Expand All @@ -174,7 +179,7 @@ export const AddWorksheetMergeVerticalCommand: ICommand = {

return commandService.executeCommand(AddWorksheetMergeCommand.id, {
value: Dimension.COLUMNS,
selections,
selections: mergeableSelections,
unitId,
subUnitId,
} as IAddMergeCommandParams);
Expand All @@ -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();
Expand All @@ -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);
Expand Down
34 changes: 34 additions & 0 deletions packages/sheets-ui/src/commands/commands/utils/selection-utils.ts
Expand Up @@ -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;
Expand Down Expand Up @@ -708,3 +714,31 @@ export function isAllColumnsCovered(allColumnRanges: IRange[], ranges: IRange[])
return true;
});
}

export function getMergeableSelectionsByType(type: MergeType, selections: Nullable<IRange[]>): Nullable<IRange[]> {
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;
}

0 comments on commit bd8c5df

Please sign in to comment.