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: move row&col effects on filter-range #2284

Merged
merged 4 commits into from
May 27, 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 @@ -15,9 +15,9 @@
*/

import type { Nullable } from '@univerjs/core';
import { ICommandService, IContextService, LifecycleStages, OnLifecycle, RxDisposable, UniverInstanceType } from '@univerjs/core';
import { ICommandService, IContextService, LifecycleStages, LocaleService, OnLifecycle, RxDisposable, UniverInstanceType } from '@univerjs/core';
import { ComponentManager, IMenuService, IMessageService, IShortcutService } from '@univerjs/ui';
import type { IMenuItemFactory, MenuConfig } from '@univerjs/ui';
import { ComponentManager, IMenuService, IShortcutService } from '@univerjs/ui';
import type { IDisposable } from '@wendellhu/redi';
import { Inject, Injector } from '@wendellhu/redi';

Expand All @@ -26,6 +26,8 @@ import { SheetCanvasPopManagerService } from '@univerjs/sheets-ui';
import { FilterSingle } from '@univerjs/icons';

import { IRenderManagerService } from '@univerjs/engine-render';
import { SheetsFilterService } from '@univerjs/sheets-filter';
import { MessageType } from '@univerjs/design';
import { ClearSheetsFilterCriteriaCommand, ReCalcSheetsFilterCommand, SetSheetsFilterCriteriaCommand, SmartToggleSheetsFilterCommand } from '../commands/sheets-filter.command';
import { FilterPanel } from '../views/components/SheetsFilterPanel';
import { ChangeFilterByOperation, CloseFilterPanelOperation, FILTER_PANEL_OPENED_KEY, OpenFilterPanelOperation } from '../commands/sheets-filter.operation';
Expand Down Expand Up @@ -53,11 +55,14 @@ export class SheetsFilterUIController extends RxDisposable {
@Inject(ComponentManager) private readonly _componentManager: ComponentManager,
@Inject(SheetsFilterPanelService) private readonly _sheetsFilterPanelService: SheetsFilterPanelService,
@Inject(SheetCanvasPopManagerService) private _sheetCanvasPopupService: SheetCanvasPopManagerService,
@Inject(SheetsFilterService) private _sheetsFilterService: SheetsFilterService,
@Inject(LocaleService) private _localeService: LocaleService,
@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,
@IShortcutService private readonly _shortcutService: IShortcutService,
@ICommandService private readonly _commandService: ICommandService,
@IMenuService private readonly _menuService: IMenuService,
@IContextService private readonly _contextService: IContextService
@IContextService private readonly _contextService: IContextService,
@IMessageService private readonly _messageService: IMessageService
) {
super();

Expand Down Expand Up @@ -120,6 +125,14 @@ export class SheetsFilterUIController extends RxDisposable {
this._closeFilterPopup();
}
}));
this.disposeWithMe(this._sheetsFilterService.errorMsg$.subscribe((content) => {
if (content) {
this._messageService.show({
type: MessageType.Error,
content: this._localeService.t(content),
});
}
}));
}

private _initRenderControllers(): void {
Expand Down
3 changes: 3 additions & 0 deletions packages/sheets-filter-ui/src/locale/en-US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ const locale: typeof zhCN = {
'not-between': 'Not Between',
custom: 'Custom',
},
msg: {
'filter-header-forbidden': 'You can\'t move the header row of a filter.',
},
},
};

Expand Down
3 changes: 3 additions & 0 deletions packages/sheets-filter-ui/src/locale/ru-RU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ const locale: typeof zhCN = {
'not-between': 'Не между',
custom: 'Пользовательский',
},
msg: {
'filter-header-forbidden': 'Вы не можете переместить строку заголовка фильтра.',
},
},
};

Expand Down
3 changes: 3 additions & 0 deletions packages/sheets-filter-ui/src/locale/zh-CN.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ const locale = {
'not-between': '不介于',
custom: '自定义',
},
msg: {
'filter-header-forbidden': '无法移动筛选行头',
},
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import type { ICommandInfo, IMutationInfo, IObjectArrayPrimitiveType, Nullable } from '@univerjs/core';
import { Disposable, DisposableCollection, ICommandService, IUniverInstanceService, LifecycleStages, moveMatrixArray, OnLifecycle, Rectangle } from '@univerjs/core';
import type { EffectRefRangeParams, IAddWorksheetMergeMutationParams, IInsertColCommandParams, IInsertRowCommandParams, IInsertRowMutationParams, IMoveColsCommandParams, IMoveRangeCommandParams, IMoveRowsCommandParams, IRemoveColMutationParams, IRemoveRowsMutationParams, IRemoveSheetCommandParams, ISetWorksheetActivateCommandParams, ISheetCommandSharedParams } from '@univerjs/sheets';
import { EffectRefRangId, InsertColCommand, InsertRowCommand, InsertRowMutation, INTERCEPTOR_POINT, MoveRangeCommand, RefRangeService, RemoveColCommand, RemoveRowCommand, RemoveRowMutation, RemoveSheetCommand, SetWorksheetActivateCommand, SheetInterceptorService } from '@univerjs/sheets';
import { EffectRefRangId, getSheetCommandTarget, InsertColCommand, InsertRowCommand, InsertRowMutation, INTERCEPTOR_POINT, MoveRangeCommand, MoveRowsCommand, RefRangeService, RemoveColCommand, RemoveRowCommand, RemoveRowMutation, RemoveSheetCommand, SetWorksheetActivateCommand, SheetInterceptorService } from '@univerjs/sheets';
import { Inject } from '@wendellhu/redi';

import { SheetsFilterService } from '../services/sheet-filter.service';
Expand All @@ -41,6 +41,7 @@ export class SheetsFilterController extends Disposable {
this._initRowFilteredInterceptor();
this._initInterceptors();
this._commandExecutedListener();
this._initErrorHandling();
}

private _initCommands(): void {
Expand Down Expand Up @@ -389,6 +390,7 @@ export class SheetsFilterController extends Disposable {
};
}

// eslint-disable-next-line max-lines-per-function
private _handleMoveColsCommand(config: IMoveColsCommandParams, unitId: string, subUnitId: string) {
const filterModel = this._sheetsFilterService.getFilterModel(unitId, subUnitId);
const filterRange = filterModel?.getRange() ?? null;
Expand All @@ -413,24 +415,44 @@ export class SheetsFilterController extends Disposable {
}
moveMatrixArray(fromRange.startColumn, fromRange.endColumn - fromRange.startColumn + 1, toRange.startColumn, filterCol);

let startBorder = filterRange.startColumn;
let endBorder = filterRange.endColumn;

// border will change if first col or last col moves.
if (startColumn >= fromRange.startColumn && startColumn <= fromRange.endColumn
&& toRange.startColumn > fromRange.startColumn
&& fromRange.endColumn < endColumn
) {
startBorder = fromRange.endColumn + 1;
}
if (endColumn >= fromRange.startColumn && endColumn <= fromRange.endColumn
&& toRange.startColumn < fromRange.startColumn
&& fromRange.startColumn > startColumn
) {
endBorder = fromRange.startColumn - 1;
}

const numberCols = Object.keys(filterCol).map((col) => Number(col)) as number[];

const newEnd = Math.max(...numberCols);
const newStart = Math.min(...numberCols);
// find the start col & end col of new filter range by border.
const newEnd = numberCols.find((col) => filterCol[col].colIndex === endBorder) as number;
const newStart = numberCols.find((col) => filterCol[col].colIndex === startBorder) as number;

numberCols.forEach((col) => {
const { colIndex: oldColIndex, filter } = filterCol[col];
const newColIndex = col;
if (filter) {
const setCriteriaMutationParams: ISetSheetsFilterCriteriaMutationParams = {
unitId,
subUnitId,
col: newColIndex,
criteria: { ...filter.serialize(), colId: newColIndex },
};
redos.push({ id: SetSheetsFilterCriteriaMutation.id, params: setCriteriaMutationParams });
undos.push({ id: RemoveSheetsFilterMutation.id, params: { unitId, subUnitId, col: newColIndex, criteria: { ...filterModel.getFilterColumn(newColIndex)?.serialize(), colId: newColIndex } } });

if (filter) {
if (newColIndex >= newStart && newColIndex <= newEnd) {
const setCriteriaMutationParams: ISetSheetsFilterCriteriaMutationParams = {
unitId,
subUnitId,
col: newColIndex,
criteria: { ...filter.serialize(), colId: newColIndex },
};
redos.push({ id: SetSheetsFilterCriteriaMutation.id, params: setCriteriaMutationParams });
undos.push({ id: RemoveSheetsFilterMutation.id, params: { unitId, subUnitId, col: newColIndex, criteria: { ...filterModel.getFilterColumn(newColIndex)?.serialize(), colId: newColIndex } } });
}
if (!filterCol[oldColIndex]?.filter) {
const setCriteriaMutationParams: ISetSheetsFilterCriteriaMutationParams = {
unitId,
Expand Down Expand Up @@ -479,18 +501,29 @@ export class SheetsFilterController extends Disposable {
}
const redos: IMutationInfo[] = [];
const undos: IMutationInfo[] = [];
const filterRow: IObjectArrayPrimitiveType<{ offset: number }> = {};
const filterRow: IObjectArrayPrimitiveType<{ oldIndex: number }> = {};
for (let row = startRow; row <= endRow; row++) {
filterRow[row] = {
offset: row - startRow,
oldIndex: row,
};
}
const startBorder = startRow;
let endBorder = endRow;

// only need to deal with endBorder, startRow will not be moved.
if (endRow >= fromRange.startRow && endRow <= fromRange.endRow
&& toRange.startRow < fromRange.startRow
&& fromRange.startRow > startRow
) {
endBorder = fromRange.startRow - 1;
}

moveMatrixArray(fromRange.startRow, fromRange.endRow - fromRange.startRow + 1, toRange.startRow, filterRow);
const numberRows = Object.keys(filterRow).map((row) => Number(row));

const newEnd = Math.max(...numberRows);
const newStart = Math.min(...numberRows);
const newEnd = numberRows.find((row) => filterRow[row].oldIndex === endBorder) as number;
const newStart = numberRows.find((row) => filterRow[row].oldIndex === startBorder) as number;

if (startRow !== newStart || endRow !== newEnd) {
const setFilterRangeMutationParams: ISetSheetsFilterRangeMutationParams = {
unitId,
Expand Down Expand Up @@ -756,5 +789,22 @@ export class SheetsFilterController extends Disposable {
// }
}));
}

private _initErrorHandling() {
this.disposeWithMe(this._commandService.beforeCommandExecuted((command) => {
const params = command.params as IMoveRowsCommandParams;
const target = getSheetCommandTarget(this._univerInstanceService);
if (!target) return;

const { subUnitId, unitId } = target;
const filterModel = this._sheetsFilterService.getFilterModel(unitId, subUnitId);
if (!filterModel) return;
const filterRange = filterModel.getRange();
if (command.id === MoveRowsCommand.id && params.fromRange.startRow <= filterRange.startRow && params.fromRange.endRow >= filterRange.startRow) {
this._sheetsFilterService.setFilterErrorMsg('sheets-filter.msg.filter-header-forbidden');
throw new Error('[SheetsFilterController]: Cannot move header row of filter');
}
}));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ describe('Test "Filter Interceptor"', () => {
selectionManagerService.setCurrentSelection({ unitId: 'workbookId', sheetId: 'worksheetId', pluginName: NORMAL_SELECTION_PLUGIN_NAME });
await commandService.executeCommand(SetSelectionsOperation.id, { unitId: 'workbookId', subUnitId: 'worksheetId', pluginName: NORMAL_SELECTION_PLUGIN_NAME, selections: [{ style: null, range: { startColumn: 1, endColumn: 1, startRow: 0, endRow: 4, rangeType: RANGE_TYPE.COLUMN }, primary: {} }] } as ISetSelectionsOperationParams);
await commandService.executeCommand(MoveColsCommand.id, moveColCommandParams);
expect(sheetsFilterService.getFilterModel('workbookId', 'worksheetId')!.getRange()).toStrictEqual({ startColumn: 1, endColumn: 2, startRow: 1, endRow: 2 });
expect(sheetsFilterService.getFilterModel('workbookId', 'worksheetId')!.getRange()).toStrictEqual({ startColumn: 1, endColumn: 1, startRow: 1, endRow: 2 });
expect(sheetsFilterService.getFilterModel('workbookId', 'worksheetId')!.getAllFilterColumns().map((x) => x[0])).toStrictEqual([1]);
});
it('move col command, filter column move to right', async () => {
Expand All @@ -132,13 +132,13 @@ describe('Test "Filter Interceptor"', () => {
});

it('move row command', async () => {
const moveColCommandParams = { unitId: 'workbookId', subUnitId: 'worksheetId', fromRange: { startColumn: 0, endColumn: 3, startRow: 1, endRow: 1, type: RANGE_TYPE.ROW,
}, toRange: { startColumn: 0, endColumn: 3, startRow: 3, endRow: 3, type: RANGE_TYPE.ROW }, direction: Direction.RIGHT } as IMoveRowsCommandParams;
const moveRowCommandParams = { unitId: 'workbookId', subUnitId: 'worksheetId', fromRange: { startColumn: 0, endColumn: 3, startRow: 2, endRow: 2, type: RANGE_TYPE.ROW,
}, toRange: { startColumn: 0, endColumn: 3, startRow: 4, endRow: 4, type: RANGE_TYPE.ROW }, direction: Direction.RIGHT } as IMoveRowsCommandParams;
const selectionManagerService = get(SelectionManagerService);
selectionManagerService.setCurrentSelection({ unitId: 'workbookId', sheetId: 'worksheetId', pluginName: NORMAL_SELECTION_PLUGIN_NAME });
await commandService.executeCommand(SetSelectionsOperation.id, { unitId: 'workbookId', subUnitId: 'worksheetId', pluginName: NORMAL_SELECTION_PLUGIN_NAME, selections: [{ style: null, range: { startColumn: 0, endColumn: 3, startRow: 1, endRow: 1, rangeType: RANGE_TYPE.ROW }, primary: {} }] } as ISetSelectionsOperationParams);
await commandService.executeCommand(MoveRowsCommand.id, moveColCommandParams);
expect(sheetsFilterService.getFilterModel('workbookId', 'worksheetId')!.getRange()).toStrictEqual({ startColumn: 1, endColumn: 2, startRow: 1, endRow: 2 });
await commandService.executeCommand(SetSelectionsOperation.id, { unitId: 'workbookId', subUnitId: 'worksheetId', pluginName: NORMAL_SELECTION_PLUGIN_NAME, selections: [{ style: null, range: { startColumn: 0, endColumn: 3, startRow: 2, endRow: 2, rangeType: RANGE_TYPE.ROW }, primary: {} }] } as ISetSelectionsOperationParams);
await commandService.executeCommand(MoveRowsCommand.id, moveRowCommandParams);
expect(sheetsFilterService.getFilterModel('workbookId', 'worksheetId')!.getRange()).toStrictEqual({ startColumn: 1, endColumn: 2, startRow: 1, endRow: 3 });
expect(sheetsFilterService.getFilterModel('workbookId', 'worksheetId')!.getAllFilterColumns().map((x) => x[0])).toStrictEqual([2]);
});
});
Expand Down
7 changes: 7 additions & 0 deletions packages/sheets-filter/src/services/sheet-filter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ export class SheetsFilterService extends Disposable {
private readonly _loadedUnitId$ = new BehaviorSubject<Nullable<string>>(null);
readonly loadedUnitId$ = this._loadedUnitId$.asObservable();

private readonly _errorMsg$ = new BehaviorSubject<Nullable<string>>(null);
readonly errorMsg$ = this._errorMsg$.asObservable();

private readonly _activeFilterModel$ = new BehaviorSubject<Nullable<FilterModel>>(null);
/** An observable value emitting the current Workbook's active Worksheet's filter model (if there is one). */
readonly activeFilterModel$ = this._activeFilterModel$.asObservable();
Expand Down Expand Up @@ -116,6 +119,10 @@ export class SheetsFilterService extends Disposable {
return false;
}

setFilterErrorMsg(content: string) {
this._errorMsg$.next(content);
}

private _updateActiveFilterModel() {
let workbook: Nullable<Workbook>;
try {
Expand Down
16 changes: 16 additions & 0 deletions packages/sheets-sort/src/services/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

Loading