Skip to content

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.

feat: add MarkDirtyFilterChangeMutation (#6786)

wpxp12345677d648ce5fe4
diff --git a/packages/core/src/shared/object-matrix.ts b/packages/core/src/shared/object-matrix.ts
index 31caec487a..873d540293 100644
--- a/packages/core/src/shared/object-matrix.ts
+++ b/packages/core/src/shared/object-matrix.ts
@@ -712,10 +712,10 @@ export class ObjectMatrix<T> {
* @returns {IRange} the start and end scope of the matrix
*/
getStartEndScope(): IRange {
- let startRow = Infinity;
- let endRow = -Infinity;
- let startColumn = Infinity;
- let endColumn = -Infinity;
+ let startRow = -1;
+ let endRow = -1;
+ let startColumn = -1;
+ let endColumn = -1;
const rows = Object.keys(this._matrix);
if (rows.length > 0) {
@@ -726,7 +726,7 @@ export class ObjectMatrix<T> {
for (const row of rows) {
const columns = Object.keys(this._matrix[row as unknown as number]);
if (columns.length > 0) {
- startColumn = Math.min(startColumn, +columns[0]);
+ startColumn = startColumn === -1 ? +columns[0] : Math.min(startColumn, +columns[0]);
endColumn = Math.max(endColumn, +columns[columns.length - 1]);
}
}
diff --git a/packages/sheets-filter-ui/src/commands/commands/__tests__/sheets-filter.command.spec.ts b/packages/sheets-filter-ui/src/commands/commands/__tests__/sheets-filter.command.spec.ts
index fedc3b2656..a780d31814 100644
--- a/packages/sheets-filter-ui/src/commands/commands/__tests__/sheets-filter.command.spec.ts
+++ b/packages/sheets-filter-ui/src/commands/commands/__tests__/sheets-filter.command.spec.ts
@@ -20,7 +20,7 @@ import type { FilterModel, ISetSheetsFilterRangeMutationParams } from '@univerjs
import type { ISetSheetsFilterCriteriaCommandParams } from '@univerjs/sheets-filter/commands/commands/sheets-filter.command.js';
import { AuthzIoLocalService, IAuthzIoService, ICommandService, Inject, Injector, IUniverInstanceService, LocaleType, Plugin, RANGE_TYPE, RedoCommand, UndoCommand, Univer, UniverInstanceType } from '@univerjs/core';
import { ActiveDirtyManagerService, IActiveDirtyManagerService, ISheetRowFilteredService, SheetRowFilteredService } from '@univerjs/engine-formula';
-import { RangeProtectionRuleModel, RefRangeService, SetRangeValuesCommand, SetRangeValuesMutation, SheetInterceptorService, SheetRangeThemeModel, SheetsSelectionsService, WorkbookPermissionService, WorksheetPermissionService, WorksheetProtectionPointModel, WorksheetProtectionRuleModel, ZebraCrossingCacheController } from '@univerjs/sheets';
+import { MarkDirtyFilterChangeMutation, RangeProtectionRuleModel, RefRangeService, SetRangeValuesCommand, SetRangeValuesMutation, SheetInterceptorService, SheetRangeThemeModel, SheetsSelectionsService, WorkbookPermissionService, WorksheetPermissionService, WorksheetProtectionPointModel, WorksheetProtectionRuleModel, ZebraCrossingCacheController } from '@univerjs/sheets';
import { SetSheetsFilterRangeMutation, SheetsFilterService, UniverSheetsFilterPlugin } from '@univerjs/sheets-filter';
import { ClearSheetsFilterCriteriaCommand, ReCalcSheetsFilterCommand, RemoveSheetFilterCommand, SetSheetFilterRangeCommand, SetSheetsFilterCriteriaCommand, SmartToggleSheetsFilterCommand } from '@univerjs/sheets-filter/commands/commands/sheets-filter.command.js';
import { IMessageService } from '@univerjs/ui';
@@ -127,6 +127,7 @@ function createFilterCommandTestBed() {
SetSheetsFilterCriteriaCommand,
ClearSheetsFilterCriteriaCommand,
ReCalcSheetsFilterCommand,
+ MarkDirtyFilterChangeMutation,
].forEach((command) => commandService.registerCommand(command));
const univerInstanceService = get(IUniverInstanceService);
diff --git a/packages/sheets-filter-ui/src/services/__tests__/sheets-filter-panel.service.spec.ts b/packages/sheets-filter-ui/src/services/__tests__/sheets-filter-panel.service.spec.ts
index 524950bae9..28492bf992 100644
--- a/packages/sheets-filter-ui/src/services/__tests__/sheets-filter-panel.service.spec.ts
+++ b/packages/sheets-filter-ui/src/services/__tests__/sheets-filter-panel.service.spec.ts
@@ -21,7 +21,7 @@ import type { IFilterConditionFormParams } from '../../models/conditions';
import type { IFilterByValueWithTreeItem } from '../sheets-filter-panel.service';
import { CommandType, ICommandService, Inject, Injector, LocaleService, Plugin, Univer, UniverInstanceType } from '@univerjs/core';
import { ActiveDirtyManagerService, IActiveDirtyManagerService, ISheetRowFilteredService, SheetRowFilteredService } from '@univerjs/engine-formula';
-import { RefRangeService, SheetInterceptorService, SheetRangeThemeModel, SheetsSelectionsService, ZebraCrossingCacheController } from '@univerjs/sheets';
+import { MarkDirtyFilterChangeMutation, RefRangeService, SheetInterceptorService, SheetRangeThemeModel, SheetsSelectionsService, ZebraCrossingCacheController } from '@univerjs/sheets';
import { CustomFilterOperator, FilterBy, SheetsFilterService, UniverSheetsFilterPlugin } from '@univerjs/sheets-filter';
import { SetSheetsFilterCriteriaCommand } from '@univerjs/sheets-filter/commands/commands/sheets-filter.command.js';
import { afterEach, beforeEach, describe, expect, it, vitest } from 'vitest';
@@ -117,6 +117,7 @@ function createSheetsFilterPanelServiceTestBed(workbookData: IWorkbookData) {
CloseFilterPanelOperation,
SetSheetsFilterCriteriaCommand,
SetCellEditVisibleOperation,
+ MarkDirtyFilterChangeMutation,
].forEach((command) => commandService.registerCommand(command));
return { univer, get };
diff --git a/packages/sheets-filter/src/commands/commands/sheets-filter.command.ts b/packages/sheets-filter/src/commands/commands/sheets-filter.command.ts
index 6cf0eae599..8c40d23139 100644
--- a/packages/sheets-filter/src/commands/commands/sheets-filter.command.ts
+++ b/packages/sheets-filter/src/commands/commands/sheets-filter.command.ts
@@ -15,12 +15,12 @@
*/
import type { IAccessor, ICommand, IMutationInfo, IRange, Nullable, Workbook } from '@univerjs/core';
-import type { ISheetCommandSharedParams } from '@univerjs/sheets';
+import type { IMarkDirtyFilterChangeMutationParams, ISheetCommandSharedParams } from '@univerjs/sheets';
import type { FilterColumn } from '../../models/filter-model';
import type { IAutoFilter, IFilterColumn } from '../../models/types';
import type { ISetSheetsFilterCriteriaMutationParams, ISetSheetsFilterRangeMutationParams } from '../mutations/sheets-filter.mutation';
import { CommandType, ErrorService, ICommandService, IUndoRedoService, IUniverInstanceService, LocaleService, sequenceExecute, UniverInstanceType } from '@univerjs/core';
-import { expandToContinuousRange, getSheetCommandTarget, isSingleCellSelection, SheetsSelectionsService } from '@univerjs/sheets';
+import { expandToContinuousRange, getSheetCommandTarget, isSingleCellSelection, MarkDirtyFilterChangeMutation, SheetsSelectionsService } from '@univerjs/sheets';
import { SheetsFilterService } from '../../services/sheet-filter.service';
import { ReCalcSheetsFilterMutation, RemoveSheetsFilterMutation, SetSheetsFilterCriteriaMutation, SetSheetsFilterRangeMutation } from '../mutations/sheets-filter.mutation';
@@ -83,22 +83,23 @@ export const RemoveSheetFilterCommand: ICommand<ISheetCommandSharedParams> = {
id: 'sheet.command.remove-sheet-filter',
type: CommandType.COMMAND,
handler: (accessor, params: ISheetCommandSharedParams) => {
- const univerInstanceService = accessor.get(IUniverInstanceService);
- const sheetsFilterService = accessor.get(SheetsFilterService);
- const commandService = accessor.get(ICommandService);
-
- const undoRedoService = accessor.get(IUndoRedoService);
-
- const commandTarget = getSheetCommandTarget(univerInstanceService, params);
- if (!commandTarget) return false;
+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService), params);
+ if (!target) return false;
- // If there is a filter model, we should remove it and prepare undo redo.
- const { unitId, subUnitId } = commandTarget;
+ const sheetsFilterService = accessor.get(SheetsFilterService);
+ const { unitId, subUnitId } = target;
const filterModel = sheetsFilterService.getFilterModel(unitId, subUnitId);
if (!filterModel) return false;
- const autoFilter = filterModel?.serialize();
+ const filterRange = filterModel.getRange();
+ if (!filterRange) return false;
+
+ const commandService = accessor.get(ICommandService);
+ const undoRedoService = accessor.get(IUndoRedoService);
+
+ const autoFilter = filterModel.serialize();
const undoMutations = destructFilterModel(unitId, subUnitId, autoFilter);
+
const result = commandService.syncExecuteCommand(RemoveSheetsFilterMutation.id, { unitId, subUnitId });
if (result) {
undoRedoService.pushUndoRedo({
@@ -106,6 +107,11 @@ export const RemoveSheetFilterCommand: ICommand<ISheetCommandSharedParams> = {
undoMutations,
redoMutations: [{ id: RemoveSheetsFilterMutation.id, params: { unitId, subUnitId } }],
});
+ commandService.executeCommand<IMarkDirtyFilterChangeMutationParams>(MarkDirtyFilterChangeMutation.id, {
+ unitId,
+ subUnitId,
+ filterRange,
+ });
}
return result;
@@ -178,16 +184,20 @@ export const SetSheetsFilterCriteriaCommand: ICommand<ISetSheetsFilterCriteriaCo
id: 'sheet.command.set-filter-criteria',
type: CommandType.COMMAND,
handler: (accessor: IAccessor, params: ISetSheetsFilterCriteriaCommandParams) => {
- const sheetsFilterService = accessor.get(SheetsFilterService);
- const commandService = accessor.get(ICommandService);
- const undoRedoService = accessor.get(IUndoRedoService);
+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService), params);
+ if (!target) return false;
- const { unitId, subUnitId, col, criteria } = params;
+ const sheetsFilterService = accessor.get(SheetsFilterService);
+ const { unitId, subUnitId } = target;
const filterModel = sheetsFilterService.getFilterModel(unitId, subUnitId);
if (!filterModel) return false;
- const range = filterModel.getRange();
- if (!range || col < range.startColumn || col > range.endColumn) return false;
+ const { col, criteria } = params;
+ const filterRange = filterModel.getRange();
+ if (!filterRange || col < filterRange.startColumn || col > filterRange.endColumn) return false;
+
+ const commandService = accessor.get(ICommandService);
+ const undoRedoService = accessor.get(IUndoRedoService);
const filterColumn = filterModel.getFilterColumn(col);
const undoMutation = destructFilterColumn(unitId, subUnitId, col, filterColumn);
@@ -208,6 +218,11 @@ export const SetSheetsFilterCriteriaCommand: ICommand<ISetSheetsFilterCriteriaCo
undoMutations: [undoMutation],
redoMutations: [redoMutation],
});
+ commandService.executeCommand<IMarkDirtyFilterChangeMutationParams>(MarkDirtyFilterChangeMutation.id, {
+ unitId,
+ subUnitId,
+ filterRange,
+ });
}
return result;
@@ -222,17 +237,19 @@ export const ClearSheetsFilterCriteriaCommand: ICommand<ISheetCommandSharedParam
id: 'sheet.command.clear-filter-criteria',
type: CommandType.COMMAND,
handler: (accessor: IAccessor, params) => {
+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService), params);
+ if (!target) return false;
+
const sheetsFilterService = accessor.get(SheetsFilterService);
- const undoRedoService = accessor.get(IUndoRedoService);
- const commandService = accessor.get(ICommandService);
- const instanceSrv = accessor.get(IUniverInstanceService);
+ const { unitId, subUnitId } = target;
+ const filterModel = sheetsFilterService.getFilterModel(unitId, subUnitId);
+ if (!filterModel) return false;
- const commandTarget = getSheetCommandTarget(instanceSrv, params);
- if (!commandTarget) return false;
+ const filterRange = filterModel.getRange();
+ if (!filterRange) return false;
- const { unitId, subUnitId } = commandTarget;
- const filterModel = sheetsFilterService.getFilterModel(commandTarget.unitId, commandTarget.subUnitId);
- if (!filterModel) return false;
+ const undoRedoService = accessor.get(IUndoRedoService);
+ const commandService = accessor.get(ICommandService);
const autoFilter = filterModel.serialize();
const undoMutations = destructFilterCriteria(unitId, subUnitId, autoFilter);
@@ -245,7 +262,11 @@ export const ClearSheetsFilterCriteriaCommand: ICommand<ISheetCommandSharedParam
undoMutations,
redoMutations,
});
-
+ commandService.executeCommand<IMarkDirtyFilterChangeMutationParams>(MarkDirtyFilterChangeMutation.id, {
+ unitId,
+ subUnitId,
+ filterRange,
+ });
return true;
}
diff --git a/packages/sheets-filter/src/facade/__tests__/create-test-bed.ts b/packages/sheets-filter/src/facade/__tests__/create-test-bed.ts
index 5abe7ecb73..d5b3027fda 100644
--- a/packages/sheets-filter/src/facade/__tests__/create-test-bed.ts
+++ b/packages/sheets-filter/src/facade/__tests__/create-test-bed.ts
@@ -16,6 +16,7 @@
import type { Dependency, IWorkbookData, UnitModel } from '@univerjs/core';
import {
+ ICommandService,
ILogService,
Inject,
Injector,
@@ -33,6 +34,7 @@ import { FUniver } from '@univerjs/core/facade';
import { ActiveDirtyManagerService, DefinedNamesService, FormulaDataModel, FunctionService, IActiveDirtyManagerService, IDefinedNamesService, IFunctionService, ISheetRowFilteredService, LexerTreeBuilder } from '@univerjs/engine-formula';
import { SheetRowFilteredService } from '@univerjs/engine-formula/services/sheet-row-filtered.service.js';
import {
+ MarkDirtyFilterChangeMutation,
RefRangeService,
SheetInterceptorService,
SheetRangeThemeModel,
@@ -162,6 +164,9 @@ export function createFacadeTestBed(workbookData?: IWorkbookData, dependencies?:
univer.registerPlugin(TestPlugin);
univer.registerPlugin(UniverSheetsFilterPlugin);
+ const commandService = injector.get(ICommandService);
+ commandService.registerCommand(MarkDirtyFilterChangeMutation);
+
const sheet = univer.createUnit<IWorkbookData, UnitModel<IWorkbookData>>(UniverInstanceType.UNIVER_SHEET, workbookData || getTestWorkbookDataDemo());
const univerInstanceService = injector.get(IUniverInstanceService);
univerInstanceService.focusUnit('test');
diff --git a/packages/sheets/src/commands/mutations/mark-dirty-auto-height.mutation.ts b/packages/sheets/src/commands/mutations/mark-dirty-auto-height.mutation.ts
index dfb4c8b43e..7d987cf93e 100644
--- a/packages/sheets/src/commands/mutations/mark-dirty-auto-height.mutation.ts
+++ b/packages/sheets/src/commands/mutations/mark-dirty-auto-height.mutation.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { IOperation, IRange } from '@univerjs/core';
+import type { IMutation, IRange } from '@univerjs/core';
import { CommandType } from '@univerjs/core';
export interface IMarkDirtyRowAutoHeightMutationParams {
@@ -24,9 +24,9 @@ export interface IMarkDirtyRowAutoHeightMutationParams {
id: string;
}
-export const MarkDirtyRowAutoHeightMutation: IOperation<IMarkDirtyRowAutoHeightMutationParams> = {
- id: 'sheet.operation.mark-dirty-row-auto-height',
- type: CommandType.OPERATION,
+export const MarkDirtyRowAutoHeightMutation: IMutation<IMarkDirtyRowAutoHeightMutationParams> = {
+ id: 'sheet.mutation.mark-dirty-row-auto-height',
+ type: CommandType.MUTATION,
handler: () => {
return true;
},
@@ -38,9 +38,9 @@ export interface ICancelMarkDirtyRowAutoHeightMutationParams {
id: string;
}
-export const CancelMarkDirtyRowAutoHeightMutation: IOperation<ICancelMarkDirtyRowAutoHeightMutationParams> = {
- id: 'sheet.operation.cancel-mark-dirty-row-auto-height',
- type: CommandType.OPERATION,
+export const CancelMarkDirtyRowAutoHeightMutation: IMutation<ICancelMarkDirtyRowAutoHeightMutationParams> = {
+ id: 'sheet.mutation.cancel-mark-dirty-row-auto-height',
+ type: CommandType.MUTATION,
handler: () => {
return true;
},
diff --git a/packages/sheets/src/commands/mutations/mark-dirty-filter-change.mutation.ts b/packages/sheets/src/commands/mutations/mark-dirty-filter-change.mutation.ts
new file mode 100644
index 0000000000..cb187b7f97
--- /dev/null
+++ b/packages/sheets/src/commands/mutations/mark-dirty-filter-change.mutation.ts
@@ -0,0 +1,33 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * 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.
+ */
+
+import type { IMutation } from '@univerjs/core';
+import type { IRange } from '@univerjs/protocol';
+import { CommandType } from '@univerjs/core';
+
+export interface IMarkDirtyFilterChangeMutationParams {
+ unitId: string;
+ subUnitId: string;
+ filterRange: IRange;
+}
+
+export const MarkDirtyFilterChangeMutation: IMutation<IMarkDirtyFilterChangeMutationParams> = {
+ id: 'sheet.mutation.mark-dirty-filter-change',
+ type: CommandType.MUTATION,
+ handler: () => {
+ return true;
+ },
+};
diff --git a/packages/sheets/src/controllers/basic-worksheet.controller.ts b/packages/sheets/src/controllers/basic-worksheet.controller.ts
index e4b6ae69db..df7de180f2 100644
--- a/packages/sheets/src/controllers/basic-worksheet.controller.ts
+++ b/packages/sheets/src/controllers/basic-worksheet.controller.ts
@@ -131,6 +131,7 @@ import { EmptyMutation } from '../commands/mutations/empty.mutation';
import { InsertColMutation, InsertRowMutation } from '../commands/mutations/insert-row-col.mutation';
import { InsertSheetMutation } from '../commands/mutations/insert-sheet.mutation';
import { CancelMarkDirtyRowAutoHeightMutation, MarkDirtyRowAutoHeightMutation } from '../commands/mutations/mark-dirty-auto-height.mutation';
+import { MarkDirtyFilterChangeMutation } from '../commands/mutations/mark-dirty-filter-change.mutation';
import { MoveRangeMutation } from '../commands/mutations/move-range.mutation';
import { MoveColsMutation, MoveRowsMutation } from '../commands/mutations/move-rows-cols.mutation';
import { RemoveNumfmtMutation, SetNumfmtMutation } from '../commands/mutations/numfmt-mutation';
@@ -223,6 +224,8 @@ export class BasicWorksheetController extends Disposable implements IDisposable
SetWorksheetRowCountMutation,
SetWorksheetColumnCountMutation,
+
+ MarkDirtyFilterChangeMutation,
] as IMutation<object>[]).forEach((mutation) => {
this._commandService.registerCommand(mutation);
this._dataSyncPrimaryController?.registerSyncingMutations(mutation);
diff --git a/packages/sheets/src/index.ts b/packages/sheets/src/index.ts
index 54fe45f9d4..24469e85b0 100644
--- a/packages/sheets/src/index.ts
+++ b/packages/sheets/src/index.ts
@@ -236,6 +236,7 @@ export {
type IMarkDirtyRowAutoHeightMutationParams,
MarkDirtyRowAutoHeightMutation,
} from './commands/mutations/mark-dirty-auto-height.mutation';
+export { type IMarkDirtyFilterChangeMutationParams, MarkDirtyFilterChangeMutation } from './commands/mutations/mark-dirty-filter-change.mutation';
export { type IMoveRangeMutationParams, MoveRangeMutation } from './commands/mutations/move-range.mutation';
export {
type IMoveColumnsMutationParams,