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: copy sheet should copy filter #3592

Merged
merged 5 commits into from
Sep 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 @@ -14,14 +14,14 @@
* limitations under the License.
*/

import type { IAccessor, ICommand, IMutationInfo, IRange, Nullable, Workbook } from '@univerjs/core';
import { CommandType, ICommandService, IUndoRedoService, IUniverInstanceService, LocaleService, Quantity, sequenceExecute, UniverInstanceType } from '@univerjs/core';
import { MessageType } from '@univerjs/design';
import type { ISheetCommandSharedParams } from '@univerjs/sheets';
import { expandToContinuousRange, getSheetCommandTarget, isSingleCellSelection, SheetsSelectionsService } from '@univerjs/sheets';
import type { FilterColumn, IAutoFilter, IFilterColumn, ISetSheetsFilterCriteriaMutationParams, ISetSheetsFilterRangeMutationParams } from '@univerjs/sheets-filter';
import { ReCalcSheetsFilterMutation, RemoveSheetsFilterMutation, SetSheetsFilterCriteriaMutation, SetSheetsFilterRangeMutation, SheetsFilterService } from '@univerjs/sheets-filter';
import { IMessageService } from '@univerjs/ui';
import type { IAccessor, ICommand, IMutationInfo, IRange, Nullable, Workbook } from '@univerjs/core';
import type { ISheetCommandSharedParams } from '@univerjs/sheets';
import type { FilterColumn, IAutoFilter, IFilterColumn, ISetSheetsFilterCriteriaMutationParams, ISetSheetsFilterRangeMutationParams } from '@univerjs/sheets-filter';

/**
* Parameters of command {@link SetSheetFilterRangeCommand}.
Expand Down Expand Up @@ -85,6 +85,7 @@ export const RemoveSheetFilterCommand: ICommand<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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
* limitations under the License.
*/

import React, { useCallback } from 'react';
import { LocaleService, useDependency } from '@univerjs/core';
import { Button, Checkbox, Input, Tooltip } from '@univerjs/design';
import { useObservable } from '@univerjs/ui';
import List from 'rc-virtual-list';
import { Button, Checkbox, Input, Tooltip } from '@univerjs/design';
import type { ByValuesModel, IFilterByValueItem } from '../../services/sheets-filter-panel.service';
import React, { useCallback } from 'react';
import { statisticFilterByValueItems } from '../../models/utils';
import styles from './index.module.less';
import type { ByValuesModel, IFilterByValueItem } from '../../services/sheets-filter-panel.service';

/**
* Filter by values.
Expand Down Expand Up @@ -54,7 +54,7 @@ export function FilterByValue(props: { model: ByValuesModel }) {

return (
<div className={styles.sheetsFilterPanelValuesContainer}>
<Input value={searchText} placeholder={localeService.t('sheets-filter.panel.search-placeholder')} onChange={onSearchValueChange} />
<Input autoFocus value={searchText} placeholder={localeService.t('sheets-filter.panel.search-placeholder')} onChange={onSearchValueChange} />
<div className={styles.sheetsFilterPanelValuesList}>
{/* The on-top select all button */}
<div className={styles.sheetsFilterPanelValuesItem}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@
* limitations under the License.
*/

import React, { useCallback, useMemo } from 'react';
import { Button, type ISegmentedProps, Segmented } from '@univerjs/design';
import { ICommandService, LocaleService, useDependency } from '@univerjs/core';
import { ComponentContainer, useComponentsOfPart, useObservable } from '@univerjs/ui';

import { of } from 'rxjs';
import { Button, type ISegmentedProps, Segmented } from '@univerjs/design';
import { SheetsFilterService } from '@univerjs/sheets-filter';
import { SheetsUIPart } from '@univerjs/sheets-ui';
import type { ByConditionsModel, ByValuesModel } from '../../services/sheets-filter-panel.service';
import { FilterBy, SheetsFilterPanelService } from '../../services/sheets-filter-panel.service';

import { ComponentContainer, useComponentsOfPart, useObservable } from '@univerjs/ui';
import React, { useCallback, useMemo } from 'react';
import { of } from 'rxjs';
import { ChangeFilterByOperation, CloseFilterPanelOperation } from '../../commands/operations/sheets-filter.operation';
import { FilterBy, SheetsFilterPanelService } from '../../services/sheets-filter-panel.service';
import styles from './index.module.less';
import { FilterByCondition } from './SheetsFilterByConditionsPanel';
import { FilterByValue } from './SheetsFilterByValuesPanel';
import type { ByConditionsModel, ByValuesModel } from '../../services/sheets-filter-panel.service';

/**
* This Filter Panel component is used to filter the data in the sheet.
Expand Down Expand Up @@ -79,15 +79,17 @@ export function FilterPanel() {
<div className={styles.sheetsFilterPanelHeader}>
<Segmented value={filterBy} options={options} onChange={(value) => onFilterByTypeChange(value as FilterBy)}></Segmented>
</div>
{ filterByModel
{filterByModel
? (
<div className={styles.sheetsFilterPanelContent}>
{filterBy === FilterBy.VALUES
? <FilterByValue model={filterByModel as ByValuesModel} />
: <FilterByCondition model={filterByModel as ByConditionsModel} />}
</div>
)
: null }
: (
<div style={{ flex: 1 }} />
)}
<div className={styles.sheetsFilterPanelFooter}>
<Button type="link" onClick={onClearCriteria} disabled={clearFilterDisabled}>{localeService.t('sheets-filter.panel.clear-filter')}</Button>
<span className={styles.sheetsFilterPanelFooterPrimaryButtons}>
Expand All @@ -105,7 +107,7 @@ function useFilterByOptions(localeService: LocaleService): ISegmentedProps['opti
{ label: localeService.t('sheets-filter.panel.by-values'), value: FilterBy.VALUES },
{ label: localeService.t('sheets-filter.panel.by-conditions'), value: FilterBy.CONDITIONS },
]
// eslint-disable-next-line react-hooks/exhaustive-deps
// eslint-disable-next-line react-hooks/exhaustive-deps
, [locale, localeService]);
}

Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
* limitations under the License.
*/

/* eslint-disable perfectionist/sort-imports */

import type { IWorkbookData, Workbook } from '@univerjs/core';
import { ICommandService, Inject, Injector, IUniverInstanceService, LocaleType, Plugin, RANGE_TYPE, UndoCommand, Univer, UniverInstanceType } from '@univerjs/core';
import { ICommandService, Inject, Injector, IUniverInstanceService, LocaleService, 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 { InsertColMutation, MoveColsCommand, MoveColsMutation, MoveRowsCommand, MoveRowsMutation, RefRangeService, RemoveColCommand, RemoveColMutation, RemoveRowCommand, RemoveRowMutation, SetRangeValuesMutation, SetSelectionsOperation, SheetInterceptorService, SheetsSelectionsService } from '@univerjs/sheets';
import { CopySheetCommand, InsertColMutation, InsertSheetMutation, 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';
Expand Down Expand Up @@ -87,6 +89,11 @@ function testWorkbookDataWithFilterFactory(): IWorkbookData {
v: 'D',
},
},
4: {
0: {
v: 'H',
},
},
},
name: 'Sheet-001',
},
Expand All @@ -97,6 +104,8 @@ function testWorkbookDataWithFilterFactory(): IWorkbookData {
data: JSON.stringify({
sheet1: {
ref: { startRow: 3, startColumn: 0, endRow: 5, endColumn: 5 },
filterColumns: [{ colId: 0, filters: { blank: true } }],
cachedFilteredOut: [4],
},
}),
},
Expand Down Expand Up @@ -153,6 +162,8 @@ function createFilterControllerTestBed(workbookData?: IWorkbookData) {
RemoveColMutation,
SetSelectionsOperation,
SetRangeValuesMutation,
CopySheetCommand,
InsertSheetMutation,
]
).forEach((command) => commandService.registerCommand(command));

Expand All @@ -177,6 +188,8 @@ describe('test controller of sheets filter', () => {
univer = testBed.univer;
get = testBed.get;

get(LocaleService).load({ zhCN: {} });

commandService = get(ICommandService);
sheetsFilterService = get(SheetsFilterService);
instanceService = get(IUniverInstanceService);
Expand Down Expand Up @@ -358,4 +371,19 @@ describe('test controller of sheets filter', () => {
expect((sheetsFilterService as SheetsFilterService).getFilterModel('test', 'sheet1')!.getAllFilterColumns()?.[0][0]).toBe(1);
});
});

describe('test copy sheet', () => {
it('should copy filter', async () => {
const res = await commandService.executeCommand(CopySheetCommand.id, {
unitId: 'test',
subUnitId: 'sheet1',
});
expect(res).toBeTruthy();
const workbook = instanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
const sheet2 = workbook.getSheets()[1];
const filterModel = (sheetsFilterService as SheetsFilterService).getFilterModel('test', sheet2.getSheetId());
expect(filterModel).toBeTruthy();
expect(filterModel?.getFilterColumn(0)?.getColumnData()).toStrictEqual({ colId: 0, filters: { blank: true } });
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/

import { Disposable, DisposableCollection, ICommandService, Inject, IUniverInstanceService, moveMatrixArray, Rectangle } from '@univerjs/core';
import { EffectRefRangId, expandToContinuousRange, getSheetCommandTarget, InsertColCommand, InsertRowCommand, InsertRowMutation, INTERCEPTOR_POINT, MoveRangeCommand, MoveRowsCommand, RefRangeService, RemoveColCommand, RemoveRowCommand, RemoveRowMutation, RemoveSheetCommand, SetRangeValuesMutation, SetWorksheetActivateCommand, SheetInterceptorService } from '@univerjs/sheets';
import { CopySheetCommand, EffectRefRangId, expandToContinuousRange, getSheetCommandTarget, InsertColCommand, InsertRowCommand, InsertRowMutation, INTERCEPTOR_POINT, MoveRangeCommand, MoveRowsCommand, RefRangeService, RemoveColCommand, RemoveRowCommand, RemoveRowMutation, RemoveSheetCommand, SetRangeValuesMutation, SetWorksheetActiveOperation, SheetInterceptorService } from '@univerjs/sheets';
import type { ICellData, ICommandInfo, IMutationInfo, IObjectArrayPrimitiveType, IRange, Nullable, Workbook } from '@univerjs/core';
import type { EffectRefRangeParams, IAddWorksheetMergeMutationParams, IInsertColCommandParams, IInsertRowCommandParams, IInsertRowMutationParams, IMoveColsCommandParams, IMoveRangeCommandParams, IMoveRowsCommandParams, IRemoveColMutationParams, IRemoveRowsMutationParams, IRemoveSheetCommandParams, ISetRangeValuesMutationParams, ISetWorksheetActivateCommandParams, ISheetCommandSharedParams } from '@univerjs/sheets';
import type { EffectRefRangeParams, IAddWorksheetMergeMutationParams, ICopySheetCommandParams, IInsertColCommandParams, IInsertRowCommandParams, IInsertRowMutationParams, IMoveColsCommandParams, IMoveRangeCommandParams, IMoveRowsCommandParams, IRemoveColMutationParams, IRemoveRowsMutationParams, IRemoveSheetCommandParams, ISetRangeValuesMutationParams, ISetWorksheetActiveOperationParams, ISheetCommandSharedParams } from '@univerjs/sheets';

import { ReCalcSheetsFilterMutation, RemoveSheetsFilterMutation, SetSheetsFilterCriteriaMutation, SetSheetsFilterRangeMutation } from '../commands/mutations/sheets-filter.mutation';
import { SheetsFilterService } from '../services/sheet-filter.service';
Expand Down Expand Up @@ -57,8 +57,8 @@ export class SheetsFilterController extends Disposable {
getMutations: (command) => this._getUpdateFilter(command),
}));
this.disposeWithMe(this._commandService.onCommandExecuted((commandInfo) => {
if (commandInfo.id === SetWorksheetActivateCommand.id) {
const params = commandInfo.params as ISetWorksheetActivateCommandParams;
if (commandInfo.id === SetWorksheetActiveOperation.id) {
const params = commandInfo.params as ISetWorksheetActiveOperationParams;
const sheetId = params.subUnitId;
const unitId = params.unitId;
if (!sheetId || !unitId) {
Expand Down Expand Up @@ -144,6 +144,14 @@ export class SheetsFilterController extends Disposable {
const params = command.params as ISheetCommandSharedParams;
return this._handleRemoveSheetCommand(params, params.unitId, params.subUnitId);
}
case CopySheetCommand.id: {
const params = command.params as ICopySheetCommandParams & { targetSubUnitId: string };
const { targetSubUnitId, unitId, subUnitId } = params;
if (!unitId || !subUnitId || !targetSubUnitId) {
return this._handleNull();
}
return this._handleCopySheetCommand(unitId, subUnitId, targetSubUnitId);
}
}
return {
redos: [],
Expand Down Expand Up @@ -251,9 +259,9 @@ export class SheetsFilterController extends Disposable {
const undos: IMutationInfo[] = [];

const rangeRemoveCount =
removeEndColumn < startColumn
? 0 :
Math.min(removeEndColumn, endColumn) - Math.max(removeStartColumn, startColumn) + 1;
removeEndColumn < startColumn
? 0 :
Math.min(removeEndColumn, endColumn) - Math.max(removeStartColumn, startColumn) + 1;

const removeCount = removeEndColumn - removeStartColumn + 1;

Expand Down Expand Up @@ -285,7 +293,7 @@ export class SheetsFilterController extends Disposable {
subUnitId,
};
redos.push({ id: RemoveSheetsFilterMutation.id, params: removeFilterRangeMutationParams });
undos.push({ id: SetSheetsFilterRangeMutation.id, params: { range: filterRange, unitId, subUnitId } });
undos.unshift({ id: SetSheetsFilterRangeMutation.id, params: { range: filterRange, unitId, subUnitId } });
} else {
const newStartColumn = startColumn <= removeStartColumn
? startColumn :
Expand Down Expand Up @@ -324,13 +332,16 @@ export class SheetsFilterController extends Disposable {
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),
redos: [{
id: SetSheetsFilterRangeMutation.id, params: {
range: {
...filterRange,
startRow: startRow - (removeEndRow - removeStartRow + 1),
endRow: endRow - (removeEndRow - removeStartRow + 1),
},
unitId, subUnitId,
},
unitId, subUnitId } }],
}],
};
}
const redos: IMutationInfo[] = [];
Expand Down Expand Up @@ -620,8 +631,7 @@ export class SheetsFilterController extends Disposable {
const redos: IMutationInfo[] = [];
const undos: IMutationInfo[] = [];
const filterCols = filterModel.getAllFilterColumns();
filterCols.forEach((col) => {
const [_, filter] = col;
filterCols.forEach(([col, filter]) => {
undos.push({ id: SetSheetsFilterCriteriaMutation.id, params: { unitId, subUnitId, col, criteria: { ...filter.serialize(), colId: col } } });
});
redos.push({ id: RemoveSheetsFilterMutation.id, params: { unitId, subUnitId, range: filterRange } });
Expand All @@ -632,6 +642,34 @@ export class SheetsFilterController extends Disposable {
};
}

private _handleCopySheetCommand(unitId: string, subUnitId: string, targetSubUnitId: string) {
const filterModel = this._sheetsFilterService.getFilterModel(unitId, subUnitId);
if (!filterModel) {
return this._handleNull();
}
const filterRange = filterModel.getRange();
if (!filterRange) {
return this._handleNull();
}
const redos: IMutationInfo[] = [];
const undos: IMutationInfo[] = [];
const preUndos: IMutationInfo[] = [];
const preRedos: IMutationInfo[] = [];
const filterCols = filterModel.getAllFilterColumns();
filterCols.forEach(([col, filter]) => {
redos.push({ id: SetSheetsFilterCriteriaMutation.id, params: { unitId, subUnitId: targetSubUnitId, col, criteria: { ...filter.serialize(), colId: col } } });
preUndos.push({ id: SetSheetsFilterCriteriaMutation.id, params: { unitId, subUnitId: targetSubUnitId, col, criteria: null } });
});
preUndos.push({ id: RemoveSheetsFilterMutation.id, params: { unitId, subUnitId: targetSubUnitId, range: filterRange } });
redos.unshift({ id: SetSheetsFilterRangeMutation.id, params: { range: filterRange, unitId, subUnitId: targetSubUnitId } });
return {
undos,
redos,
preUndos,
preRedos,
};
}

private _handleNull() {
return { redos: [], undos: [] };
}
Expand Down Expand Up @@ -756,7 +794,7 @@ export class SheetsFilterController extends Disposable {
}

// extend filter range when set range values
if (command.id === SetRangeValuesMutation.id && !options?.fromCollab && !options?.onlyLocal) {
if (command.id === SetRangeValuesMutation.id && !options?.onlyLocal) {
const extendRegion = this._getExtendRegion(unitId, subUnitId);
if (extendRegion) {
const cellValue = (command.params as ISetRangeValuesMutationParams).cellValue;
Expand Down
Loading
Loading