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(sheets): hide rows cols should skip over already hidden ranges #2517

Merged
merged 1 commit into from
Jun 18, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,34 @@ describe('Test row col hide/unhine commands', () => {
expect(getRowRawVisible(0)).toBeFalsy();
expect(getRowRawVisible(2)).toBeFalsy();
});

it('should skip over already hidden rows', async () => {
expect(getRowRawVisible(0)).toBeTruthy();

selectRow(2, 2);
await commandService.executeCommand(SetRowHiddenCommand.id);
expect(getRowRawVisible(2)).toBeFalsy();

selectRow(1, 4);
await commandService.executeCommand(SetRowHiddenCommand.id);
expect(getRowRawVisible(1)).toBeFalsy();
expect(getRowRawVisible(2)).toBeFalsy();
expect(getRowRawVisible(3)).toBeFalsy();

await commandService.executeCommand(UndoCommand.id);
expect(getRowRawVisible(1)).toBeTruthy();
expect(getRowRawVisible(2)).toBeFalsy();
expect(getRowRawVisible(3)).toBeTruthy();

selectRow(1, 2);
await commandService.executeCommand(SetRowHiddenCommand.id);
expect(getRowRawVisible(1)).toBeFalsy();
expect(getRowRawVisible(2)).toBeFalsy();

await commandService.executeCommand(UndoCommand.id);
expect(getRowRawVisible(1)).toBeTruthy();
expect(getRowRawVisible(2)).toBeFalsy();
});
});

describe('hide / unhide columns', () => {
Expand Down Expand Up @@ -207,5 +235,33 @@ describe('Test row col hide/unhine commands', () => {
expect(getColVisible(0)).toBeFalsy();
expect(getColVisible(2)).toBeFalsy();
});

it('should skip over already hidden cols', async () => {
expect(getColVisible(0)).toBeTruthy();

selectColumn(2, 2);
await commandService.executeCommand(SetColHiddenCommand.id);
expect(getColVisible(2)).toBeFalsy();

selectColumn(1, 4);
await commandService.executeCommand(SetColHiddenCommand.id);
expect(getColVisible(1)).toBeFalsy();
expect(getColVisible(2)).toBeFalsy();
expect(getColVisible(3)).toBeFalsy();

await commandService.executeCommand(UndoCommand.id);
expect(getColVisible(1)).toBeTruthy();
expect(getColVisible(2)).toBeFalsy();
expect(getColVisible(3)).toBeTruthy();

selectColumn(1, 2);
await commandService.executeCommand(SetColHiddenCommand.id);
expect(getColVisible(1)).toBeFalsy();
expect(getColVisible(2)).toBeFalsy();

await commandService.executeCommand(UndoCommand.id);
expect(getColVisible(1)).toBeTruthy();
expect(getColVisible(2)).toBeFalsy();
});
});
});
106 changes: 61 additions & 45 deletions packages/sheets/src/commands/commands/set-col-visible.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
* limitations under the License.
*/

import type { ICommand, IRange, Nullable, Workbook } from '@univerjs/core';
import type { ICommand, IRange, Nullable,
Worksheet } from '@univerjs/core';
import {
CommandType,
ICommandService,
IUndoRedoService,
IUniverInstanceService,
RANGE_TYPE,
sequenceExecute,
UniverInstanceType,
} from '@univerjs/core';
import type { IAccessor } from '@wendellhu/redi';

Expand All @@ -38,6 +38,7 @@ import type { ISetSelectionsOperationParams } from '../operations/selection.oper
import { SetSelectionsOperation } from '../operations/selection.operation';
import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';
import { getPrimaryForRange } from './utils/selection-utils';
import { getSheetCommandTarget } from './utils/target-util';

export interface ISetSpecificColsVisibleCommandParams {
unitId: string;
Expand All @@ -49,26 +50,29 @@ export const SetSpecificColsVisibleCommand: ICommand<ISetSpecificColsVisibleComm
type: CommandType.COMMAND,
id: 'sheet.command.set-col-visible-on-cols',
handler: async (accessor, params: ISetSpecificColsVisibleCommandParams) => {
const sheetInterceptorService = accessor.get(SheetInterceptorService);
const { unitId, subUnitId, ranges } = params;

const worksheet = accessor
.get(IUniverInstanceService)
.getUniverSheetInstance(unitId)!
.getSheetBySheetId(subUnitId)!;
const sheetInterceptorService = accessor.get(SheetInterceptorService);
const commandService = accessor.get(ICommandService);
const instanceService = accessor.get(IUniverInstanceService);

const target = getSheetCommandTarget(instanceService, { unitId, subUnitId });
if (!target) return false;

const { worksheet } = target;
const redoMutationParams: ISetColVisibleMutationParams = {
unitId,
subUnitId,
ranges,
};
const undoMutationParams = SetColVisibleUndoMutationFactory(accessor, redoMutationParams);
const setSelectionOperationParams: ISetSelectionsOperationParams = {
unitId,
subUnitId,
pluginName: NORMAL_SELECTION_PLUGIN_NAME,
selections: ranges.map((r) => ({ range: r, primary: getPrimaryForRange(r, worksheet), style: null })),
};

const undoMutationParams = SetColVisibleUndoMutationFactory(accessor, redoMutationParams);
const undoSetSelectionsOperationParams: ISetSelectionsOperationParams = {
unitId,
subUnitId,
Expand All @@ -80,8 +84,6 @@ export const SetSpecificColsVisibleCommand: ICommand<ISetSpecificColsVisibleComm
})),
};

const commandService = accessor.get(ICommandService);

const result = sequenceExecute([
{ id: SetColVisibleMutation.id, params: redoMutationParams },
{ id: SetSelectionsOperation.id, params: setSelectionOperationParams },
Expand Down Expand Up @@ -122,28 +124,19 @@ export const SetSelectedColsVisibleCommand: ICommand = {
id: 'sheet.command.set-selected-cols-visible',
handler: async (accessor: IAccessor) => {
const selectionManagerService = accessor.get(SelectionManagerService);
const ranges = selectionManagerService
.getSelections()
?.map((s) => s.range)
.filter((r) => r.rangeType === RANGE_TYPE.COLUMN);
if (!ranges?.length) {
return false;
}
const commandService = accessor.get(ICommandService);

const univerInstanceService = accessor.get(IUniverInstanceService);
const workbook = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET);
if (!workbook) return false;
const worksheet = workbook.getActiveSheet();
if (!worksheet) return false;
const ranges = selectionManagerService.getSelections()?.map((s) => s.range).filter((r) => r.rangeType === RANGE_TYPE.COLUMN);
if (!ranges?.length) return false;

const target = getSheetCommandTarget(accessor.get(IUniverInstanceService));
if (!target) return false;

const unitId = workbook.getUnitId();
const subUnitId = worksheet.getSheetId();
const { worksheet, unitId, subUnitId } = target;
// `ranges` would not overlap each other, so `hiddenRanges` would not overlap each other either
const hiddenRanges = ranges.map((r) => worksheet.getHiddenCols(r.startColumn, r.endColumn)).flat();

return accessor
.get(ICommandService)
.executeCommand<ISetSpecificColsVisibleCommandParams>(SetSpecificColsVisibleCommand.id, {
return commandService.executeCommand<ISetSpecificColsVisibleCommandParams>(SetSpecificColsVisibleCommand.id, {
unitId,
subUnitId,
ranges: hiddenRanges,
Expand All @@ -157,23 +150,20 @@ export const SetColHiddenCommand: ICommand = {
handler: async (accessor: IAccessor) => {
const selectionManagerService = accessor.get(SelectionManagerService);
const sheetInterceptorService = accessor.get(SheetInterceptorService);
const univerInstanceService = accessor.get(IUniverInstanceService);
const commandService = accessor.get(ICommandService);

const ranges = selectionManagerService.getSelections()?.map((s) => s.range).filter((r) => r.rangeType === RANGE_TYPE.COLUMN);
if (!ranges?.length) {
return false;
}
let ranges = selectionManagerService.getSelections()?.map((s) => s.range).filter((r) => r.rangeType === RANGE_TYPE.COLUMN);
if (!ranges?.length) return false;

const univerInstanceService = accessor.get(IUniverInstanceService);
const workbook = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET);
if (!workbook) return false;
const worksheet = workbook.getActiveSheet();
if (!worksheet) return false;

const unitId = workbook.getUnitId();
const subUnitId = worksheet.getSheetId();
const redoMutationParams: ISetColHiddenMutationParams = {
unitId, subUnitId, ranges,
};
const target = getSheetCommandTarget(univerInstanceService);
if (!target) return false;

const { worksheet, unitId, subUnitId } = target;

ranges = divideRangesByHiddenCols(target.worksheet, ranges);

const redoMutationParams: ISetColHiddenMutationParams = { unitId, subUnitId, ranges };
const setSelectionOperationParams: ISetSelectionsOperationParams = {
unitId, subUnitId, pluginName: NORMAL_SELECTION_PLUGIN_NAME,
selections: getSelectionsAfterHiding(ranges).map((range) => ({
Expand All @@ -182,6 +172,8 @@ export const SetColHiddenCommand: ICommand = {
style: null,
})),
};

const undoMutationParams = SetColHiddenUndoMutationFactory(accessor, redoMutationParams);
const undoSetSelectionsOperationParams: ISetSelectionsOperationParams = {
unitId, subUnitId, pluginName: NORMAL_SELECTION_PLUGIN_NAME,
selections: ranges.map((range) => ({
Expand All @@ -191,8 +183,6 @@ export const SetColHiddenCommand: ICommand = {
})),
};

const commandService = accessor.get(ICommandService);

const result = sequenceExecute([
{ id: SetColHiddenMutation.id, params: redoMutationParams },
{ id: SetSelectionsOperation.id, params: setSelectionOperationParams },
Expand All @@ -207,7 +197,6 @@ export const SetColHiddenCommand: ICommand = {

if (result.result && interceptedResult.result) {
const undoRedoService = accessor.get(IUndoRedoService);
const undoMutationParams = SetColHiddenUndoMutationFactory(accessor, redoMutationParams);
undoRedoService.pushUndoRedo({
unitID: unitId,
undoMutations: [
Expand All @@ -229,6 +218,33 @@ export const SetColHiddenCommand: ICommand = {
},
};

export function divideRangesByHiddenCols(worksheet: Worksheet, ranges: IRange[]): IRange[] {
const endRow = worksheet.getRowCount() - 1;
const hiddenCols = worksheet.getHiddenCols();
const divided: IRange[] = [];

ranges.forEach((range) => {
const hiddenColsInSelection = hiddenCols.filter((c) => c.startColumn >= range.startColumn && c.endColumn <= range.endColumn);
if (hiddenColsInSelection.length) {
let startColumn = range.startColumn;
hiddenColsInSelection.forEach((hiddenRange) => {
if (hiddenRange.startColumn > startColumn) {
divided.push({ startColumn, endColumn: hiddenRange.startColumn - 1, startRow: 0, endRow });
startColumn = hiddenRange.endColumn + 1;
}
});

if (startColumn <= range.endColumn) {
divided.push({ startColumn, endColumn: range.endColumn, startRow: 0, endRow });
}
} else {
divided.push(range);
}
});

return divided;
}

/**
* Get the selections after hiding cols.
* @param worksheet the worksheet the command invoked on
Expand Down
Loading
Loading