Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix(filter): filter range change error when remove rows/cols (#3324)
YuHonga70ef37bbd46
diff --git a/packages/sheets-filter/src/controllers/__tests__/sheets-filter.controller.spec.ts b/packages/sheets-filter/src/controllers/__tests__/sheets-filter.controller.spec.tsindex 554cc0c3e7..c1f974483e 100644--- a/packages/sheets-filter/src/controllers/__tests__/sheets-filter.controller.spec.ts+++ b/packages/sheets-filter/src/controllers/__tests__/sheets-filter.controller.spec.ts@@ -18,9 +18,10 @@ import type { IWorkbookData, Workbook } from '@univerjs/core';import { ICommandService, Inject, Injector, IUniverInstanceService, LocaleType, Plugin, RANGE_TYPE, UndoCommand, Univer, UniverInstanceType } from '@univerjs/core';import { afterEach, beforeEach, describe, expect, it } from 'vitest';import type { ISetRangeValuesMutationParams } from '@univerjs/sheets';-import { MoveColsCommand, MoveColsMutation, MoveRowsCommand, MoveRowsMutation, RefRangeService, SetRangeValuesMutation, SetSelectionsOperation, SheetInterceptorService, SheetsSelectionsService } from '@univerjs/sheets';+import { InsertColMutation, MoveColsCommand, MoveColsMutation, MoveRowsCommand, MoveRowsMutation, RefRangeService, RemoveColCommand, RemoveColMutation, RemoveRowCommand, RemoveRowMutation, SetRangeValuesMutation, SetSelectionsOperation, SheetInterceptorService, SheetsSelectionsService } from '@univerjs/sheets';import { SHEET_FILTER_SNAPSHOT_ID, SheetsFilterService } from '../../services/sheet-filter.service';import { SheetsFilterController } from '../sheets-filter.controller';+import { SetSheetsFilterCriteriaMutation } from '../../commands/mutations/sheets-filter.mutation';function testWorkbookDataWithFilterFactory(): IWorkbookData {return {@@ -145,6 +146,11 @@ function createFilterControllerTestBed(workbookData?: IWorkbookData) {MoveRowsMutation,MoveColsCommand,MoveColsMutation,+ InsertColMutation,+ RemoveRowCommand,+ RemoveRowMutation,+ RemoveColCommand,+ RemoveColMutation,SetSelectionsOperation,SetRangeValuesMutation,]@@ -280,4 +286,76 @@ describe('test controller of sheets filter', () => {.toEqual({ startRow: 3, startColumn: 0, endRow: 6, endColumn: 5 });});});++ describe('test interceptor of remove-rows-command', () => {+ it('filter range should move up if remove rows before filter header', async () => {+ expect(await commandService.executeCommand(SetSelectionsOperation.id, {+ unitId: 'test',+ subUnitId: 'sheet1',+ selections: [+ {+ range: {+ rangeType: RANGE_TYPE.ROW,+ startRow: 0,+ endRow: 0,+ },+ },+ ],+ })).toBeTruthy();+ expect(await commandService.executeCommand(RemoveRowCommand.id, {+ unitId: 'test',+ subUnitId: 'sheet1',+ range: { startRow: 0, startColumn: 0, endRow: 0, endColumn: 5 },+ })).toBeTruthy();+ expect((sheetsFilterService as SheetsFilterService).getFilterModel('test', 'sheet1')!.getRange())+ .toEqual({ startRow: 2, startColumn: 0, endRow: 4, endColumn: 5 });+ });+ });++ describe('test interceptor of remove-cols-command', () => {+ it('filter range should move left if remove cols before filter header', async () => {+ expect(await commandService.executeCommand(SetSheetsFilterCriteriaMutation.id, {+ unitId: 'test',+ subUnitId: 'sheet1',+ col: 1,+ criteria: {+ colId: 1,+ filters: {+ filters: ['test'],+ },+ },+ })).toBeTruthy();+ expect(await commandService.executeCommand(SetSelectionsOperation.id, {+ unitId: 'test',+ subUnitId: 'sheet1',+ selections: [+ {+ range: {+ rangeType: RANGE_TYPE.COLUMN,+ startRow: 0,+ endRow: 10,+ startColumn: 0,+ endColumn: 0,+ },+ },+ ],+ })).toBeTruthy();+ expect(await commandService.executeCommand(RemoveColCommand.id, {+ unitId: 'test',+ subUnitId: 'sheet1',+ range: { startRow: 0, startColumn: 0, endRow: 10, endColumn: 0 },+ })).toBeTruthy();+ expect((sheetsFilterService as SheetsFilterService).getFilterModel('test', 'sheet1')!.getRange())+ .toEqual({ startRow: 3, startColumn: 0, endRow: 5, endColumn: 4 });+ expect((sheetsFilterService as SheetsFilterService).getFilterModel('test', 'sheet1')!.getAllFilterColumns()?.[0][0]).toBe(0);++ expect(await commandService.executeCommand(UndoCommand.id, {+ unitId: 'test',+ subUnitId: 'sheet1',+ })).toBeTruthy();+ expect((sheetsFilterService as SheetsFilterService).getFilterModel('test', 'sheet1')!.getRange())+ .toEqual({ startRow: 3, startColumn: 0, endRow: 5, endColumn: 5 });+ expect((sheetsFilterService as SheetsFilterService).getFilterModel('test', 'sheet1')!.getAllFilterColumns()?.[0][0]).toBe(1);+ });+ });});diff --git a/packages/sheets-filter/src/controllers/sheets-filter.controller.ts b/packages/sheets-filter/src/controllers/sheets-filter.controller.tsindex ff66f157be..d36d647cf9 100644--- a/packages/sheets-filter/src/controllers/sheets-filter.controller.ts+++ b/packages/sheets-filter/src/controllers/sheets-filter.controller.ts@@ -191,9 +191,9 @@ export class SheetsFilterController extends Disposable {const filterColumn = filterModel.getAllFilterColumns();const effected = filterColumn.filter((column) => column[0] >= anchor);if (effected.length !== 0) {- const { undos: moveUndos, redos: moveRedos } = this.moveCriteria(unitId, subUnitId, effected, count);- redos.push(...moveRedos);- undos.push(...moveUndos);+ const { newRange, oldRange } = this.moveCriteria(unitId, subUnitId, effected, count);+ redos.push(...newRange.redos, ...oldRange.redos);+ undos.push(...newRange.undos, ...oldRange.undos);}return { redos: mergeSetFilterCriteria(redos), undos: mergeSetFilterCriteria(undos) };@@ -247,7 +247,6 @@ export class SheetsFilterController extends Disposable {if (removeStartColumn > endColumn) {return this._handleNull();}-const redos: IMutationInfo[] = [];const undos: IMutationInfo[] = [];@@ -271,10 +270,13 @@ export class SheetsFilterController extends Disposable {const [col, _] = column;return col > removeEndColumn;});++ let newRangeCriteria: { undos: IMutationInfo[]; redos: IMutationInfo[] } = { undos: [], redos: [] };if (shifted.length > 0) {- const { undos: moveUndos, redos: moveRedos } = this.moveCriteria(unitId, subUnitId, shifted, -removeCount);- redos.push(...moveRedos);- undos.push(...moveUndos);+ const { oldRange, newRange } = this.moveCriteria(unitId, subUnitId, shifted, -removeCount);+ newRangeCriteria = newRange;+ redos.push(...oldRange.redos);+ undos.unshift(...oldRange.undos);}if (rangeRemoveCount === endColumn - startColumn + 1) {@@ -283,36 +285,27 @@ export class SheetsFilterController extends Disposable {subUnitId,};redos.push({ id: RemoveSheetsFilterMutation.id, params: removeFilterRangeMutationParams });+ undos.push({ id: SetSheetsFilterRangeMutation.id, params: { range: filterRange, unitId, subUnitId } });} else {- if (startColumn <= removeStartColumn) {- const finalEndColumn = endColumn - rangeRemoveCount;- const setFilterRangeMutationParams: ISetSheetsFilterRangeMutationParams = {- unitId,- subUnitId,- range: {- ...filterRange,- endColumn: finalEndColumn,- },- };- redos.push({ id: SetSheetsFilterRangeMutation.id, params: setFilterRangeMutationParams });- } else {- const setFilterRangeMutationParams: ISetSheetsFilterRangeMutationParams = {- unitId,- subUnitId,- range: {- ...filterRange,- startColumn: removeStartColumn,- endColumn: endColumn - (removeEndColumn - removeStartColumn + 1),- },- };- redos.push({ id: SetSheetsFilterRangeMutation.id, params: setFilterRangeMutationParams });- }+ const newStartColumn = startColumn <= removeStartColumn+ ? startColumn :+ (rangeRemoveCount === 0 ? startColumn - removeCount : removeStartColumn);+ const newEndColumn = startColumn <= removeStartColumn ? endColumn - rangeRemoveCount : endColumn - removeCount;+ const setFilterRangeMutationParams: ISetSheetsFilterRangeMutationParams = {+ unitId,+ subUnitId,+ range: { ...filterRange, startColumn: newStartColumn, endColumn: newEndColumn },+ };+ redos.push({ id: SetSheetsFilterRangeMutation.id, params: setFilterRangeMutationParams });+ undos.unshift({ id: SetSheetsFilterRangeMutation.id, params: { range: filterRange, unitId, subUnitId } });++ redos.push(...newRangeCriteria.redos);+ undos.unshift(...newRangeCriteria.undos);}- undos.push({ id: SetSheetsFilterRangeMutation.id, params: { range: filterRange, unitId, subUnitId } });return {- undos: mergeSetFilterCriteria(undos),- redos: mergeSetFilterCriteria(redos),+ undos,+ redos,};}@@ -328,6 +321,18 @@ export class SheetsFilterController extends Disposable {if (removeStartRow > endRow) {return this._handleNull();}+ if (removeEndRow < startRow) {+ return {+ undos: [{ id: SetSheetsFilterRangeMutation.id, params: { range: filterRange, unitId, subUnitId } }],+ redos: [{ id: SetSheetsFilterRangeMutation.id, params: {+ range: {+ ...filterRange,+ startRow: startRow - (removeEndRow - removeStartRow + 1),+ endRow: endRow - (removeEndRow - removeStartRow + 1),+ },+ unitId, subUnitId } }],+ };+ }const redos: IMutationInfo[] = [];const undos: IMutationInfo[] = [];const filterColumn = filterModel.getAllFilterColumns();@@ -648,19 +653,21 @@ export class SheetsFilterController extends Disposable {criteria: null,col: -1,};- const undos: IMutationInfo[] = [];- const redos: IMutationInfo[] = [];+ const oldUndos: IMutationInfo[] = [];+ const oldRedos: IMutationInfo[] = [];+ const newUndos: IMutationInfo[] = [];+ const newRedos: IMutationInfo[] = [];target.forEach((column) => {const [offset, filter] = column;- redos.push({+ oldRedos.push({id: SetSheetsFilterCriteriaMutation.id,params: {...defaultSetCriteriaMutationParams,col: offset,},});- undos.push({+ oldUndos.push({id: SetSheetsFilterCriteriaMutation.id,params: {...defaultSetCriteriaMutationParams,@@ -672,7 +679,7 @@ export class SheetsFilterController extends Disposable {target.forEach((column) => {const [offset, filter] = column;- redos.push({+ newRedos.push({id: SetSheetsFilterCriteriaMutation.id,params: {...defaultSetCriteriaMutationParams,@@ -680,7 +687,7 @@ export class SheetsFilterController extends Disposable {criteria: { ...filter.serialize(), colId: offset + step },},});- undos.push({+ newUndos.push({id: SetSheetsFilterCriteriaMutation.id,params: {...defaultSetCriteriaMutationParams,@@ -691,8 +698,14 @@ export class SheetsFilterController extends Disposable {});return {- redos,- undos,+ newRange: {+ redos: newRedos,+ undos: newUndos,+ },+ oldRange: {+ redos: oldRedos,+ undos: oldUndos,+ },};}