Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sheet): fix unreasonable merged selections #1477

Merged
merged 2 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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;
}