Skip to content

Commit

Permalink
fix: auto fill should clear dv rules
Browse files Browse the repository at this point in the history
  • Loading branch information
lumixraku committed Nov 15, 2024
1 parent bd11940 commit cd5e42f
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@
* limitations under the License.
*/

import type { IAutoFillLocation, ISheetAutoFillHook } from '@univerjs/sheets-ui';
import type { COPY_TYPE, IAutoFillLocation, IDiscreteRange, ISheetAutoFillHook } from '@univerjs/sheets-ui';
import { DataValidationType, Disposable, Inject, Injector, ObjectMatrix, queryObjectMatrix, Range, Rectangle } from '@univerjs/core';
import { DATA_VALIDATION_PLUGIN_NAME, getDataValidationDiffMutations, SheetDataValidationModel } from '@univerjs/sheets-data-validation';
import { APPLY_TYPE, getAutoFillRepeatRange, IAutoFillService, virtualizeDiscreteRanges } from '@univerjs/sheets-ui';
import { APPLY_TYPE, getAutoFillRepeatRange, getRepeatRange, IAutoFillService, virtualizeDiscreteRanges } from '@univerjs/sheets-ui';

// TODO: adjust imports

export class DataValidationAutoFillController extends Disposable {
constructor(
@IAutoFillService private readonly _autoFillService: IAutoFillService,
@Inject(SheetDataValidationModel) private readonly _dataValidationModel: SheetDataValidationModel,
@Inject(SheetDataValidationModel) private readonly _sheetDataValidationModel: SheetDataValidationModel,
@Inject(Injector) private readonly _injector: Injector
) {
super();
Expand All @@ -37,7 +37,7 @@ export class DataValidationAutoFillController extends Disposable {

const generalApplyFunc = (location: IAutoFillLocation, applyType: APPLY_TYPE) => {
const { source: sourceRange, target: targetRange, unitId, subUnitId } = location;
const ruleMatrixCopy = this._dataValidationModel.getRuleObjectMatrix(unitId, subUnitId).clone();
const ruleMatrixCopy = this._sheetDataValidationModel.getRuleObjectMatrix(unitId, subUnitId).clone();

const virtualRange = virtualizeDiscreteRanges([sourceRange, targetRange]);
const [vSourceRange, vTargetRange] = virtualRange.ranges;
Expand Down Expand Up @@ -75,27 +75,26 @@ export class DataValidationAutoFillController extends Disposable {
sourceRange
);
const { row: sourceRow, col: sourceCol } = mapFunc(sourcePositionRange.startRow, sourcePositionRange.startColumn);
const ruleId = this._dataValidationModel.getRuleIdByLocation(unitId, subUnitId, sourceRow, sourceCol);
if (ruleId) {
const targetPositionRange = Rectangle.getPositionRange(
{
startRow: row,
startColumn: col,
endColumn: col,
endRow: row,
},
targetRange
);
const { row: targetRow, col: targetCol } = mapFunc(targetPositionRange.startRow, targetPositionRange.startColumn);
// if ruleId exists, set more dv rules, if not, clear dv rules.
const ruleId = this._sheetDataValidationModel.getRuleIdByLocation(unitId, subUnitId, sourceRow, sourceCol) || '';
const targetPositionRange = Rectangle.getPositionRange(
{
startRow: row,
startColumn: col,
endColumn: col,
endRow: row,
},
targetRange
);
const { row: targetRow, col: targetCol } = mapFunc(targetPositionRange.startRow, targetPositionRange.startColumn);

additionMatrix.setValue(targetRow, targetCol, ruleId);
additionRules.add(ruleId);
}
additionMatrix.setValue(targetRow, targetCol, ruleId);
additionRules.add(ruleId);
});
});
const additions = Array.from(additionRules).map((id) => ({ id, ranges: queryObjectMatrix(additionMatrix, (value) => value === id) }));
ruleMatrixCopy.addRangeRules(additions);
const diffs = ruleMatrixCopy.diff(this._dataValidationModel.getRules(unitId, subUnitId));
const diffs = ruleMatrixCopy.diff(this._sheetDataValidationModel.getRules(unitId, subUnitId));
const { redoMutations, undoMutations } = getDataValidationDiffMutations(unitId, subUnitId, diffs, this._injector, 'patched', applyType === APPLY_TYPE.ONLY_FORMAT);
return {
undos: undoMutations,
Expand All @@ -111,7 +110,7 @@ export class DataValidationAutoFillController extends Disposable {
const { source: sourceRange, unitId, subUnitId } = location;
for (const row of sourceRange.rows) {
for (const col of sourceRange.cols) {
const dv = this._dataValidationModel.getRuleByLocation(unitId, subUnitId, row, col);
const dv = this._sheetDataValidationModel.getRuleByLocation(unitId, subUnitId, row, col);
if (dv && disabledDataVallation.indexOf(dv.type) > -1) {
this._autoFillService.setDisableApplyType(APPLY_TYPE.SERIES, true);
return;
Expand All @@ -135,4 +134,57 @@ export class DataValidationAutoFillController extends Disposable {
};
this.disposeWithMe(this._autoFillService.addHook(hook));
}

_generateMutations(
pastedRange: IDiscreteRange,
copyInfo: {
copyType: COPY_TYPE;
copyRange?: IDiscreteRange;
pasteType: string;
unitId: string;
subUnitId: string;
}
) {
const { unitId, subUnitId } = copyInfo;
const ruleMatrix = this._sheetDataValidationModel.getRuleObjectMatrix(unitId, subUnitId).clone();
const additionMatrix = new ObjectMatrix();
const additionRules = new Set<string>();

const { ranges: [vCopyRange, vPastedRange], mapFunc } = virtualizeDiscreteRanges([copyInfo.copyRange!, pastedRange]);

const repeatRange = getRepeatRange(vCopyRange, vPastedRange, true);

// repeatRange.forEach(({ startRange }) => {
// this._copyInfo?.matrix.forValue((row, col, ruleId) => {
// const range = Rectangle.getPositionRange(
// {
// startRow: row,
// endRow: row,
// startColumn: col,
// endColumn: col,
// },
// startRange
// );
// const { row: startRow, col: startColumn } = mapFunc(range.startRow, range.startColumn);
// additionMatrix.setValue(startRow, startColumn, ruleId);
// additionRules.add(ruleId);
// });
// });

const additions = Array.from(additionRules).map((id) => ({ id, ranges: queryObjectMatrix(additionMatrix, (value) => value === id) }));
ruleMatrix.addRangeRules(additions);
const { redoMutations, undoMutations } = getDataValidationDiffMutations(
unitId,
subUnitId,
ruleMatrix.diff(this._sheetDataValidationModel.getRules(unitId, subUnitId)),
this._injector,
'patched',
false
);

return {
redos: redoMutations,
undos: undoMutations,
};
}
}
36 changes: 18 additions & 18 deletions packages/sheets-ui/src/services/clipboard/clipboard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@
* limitations under the License.
*/

import type {
ICellData, IDisposable,
IMutationInfo,
IRange,
Nullable, Workbook, Worksheet,
} from '@univerjs/core';
import type { ISetSelectionsOperationParams } from '@univerjs/sheets';
import type { IDiscreteRange } from '../../controllers/utils/range-tools';
import type {
ICellDataWithSpanInfo,
IClipboardPropertyItem,
IPasteTarget,
ISheetClipboardHook,
ISheetDiscreteRangeLocation,
IUniverSheetCopyDataModel,
} from './type';
import {
createIdentifier, Disposable, ErrorService, extractPureTextFromCell, ICommandService,
ILogService,
Expand All @@ -29,43 +45,27 @@ import {
UniverInstanceType,
} from '@univerjs/core';
import { IRenderManagerService } from '@univerjs/engine-render';

import {
getPrimaryForRange,
SetSelectionsOperation,
SheetsSelectionsService,
} from '@univerjs/sheets';
import { HTML_CLIPBOARD_MIME_TYPE, IClipboardInterfaceService, INotificationService, IPlatformService, PLAIN_TEXT_CLIPBOARD_MIME_TYPE } from '@univerjs/ui';
import { BehaviorSubject } from 'rxjs';
import type {
ICellData, IDisposable,
IMutationInfo,
IRange,
Nullable, Workbook, Worksheet,
} from '@univerjs/core';

import type { ISetSelectionsOperationParams } from '@univerjs/sheets';
import { rangeToDiscreteRange, virtualizeDiscreteRanges } from '../../controllers/utils/range-tools';
import { IMarkSelectionService } from '../mark-selection/mark-selection.service';
import { SheetSkeletonManagerService } from '../sheet-skeleton-manager.service';
import { createCopyPasteSelectionStyle } from '../utils/selection-util';
import { CopyContentCache, extractId, genId } from './copy-content-cache';

import { HtmlToUSMService } from './html-to-usm/converter';
import { LarkPastePlugin } from './html-to-usm/paste-plugins/plugin-lark';

import { UniverPastePlugin } from './html-to-usm/paste-plugins/plugin-univer';
import { WordPastePlugin } from './html-to-usm/paste-plugins/plugin-word';
import { COPY_TYPE } from './type';
import { USMToHtmlService } from './usm-to-html/convertor';
import { clipboardItemIsFromExcel, convertTextToTable, discreteRangeContainsRange, mergeSetRangeValues, rangeIntersectWithDiscreteRange } from './utils';
import type { IDiscreteRange } from '../../controllers/utils/range-tools';
import type {
ICellDataWithSpanInfo,
IClipboardPropertyItem,
IPasteTarget,
ISheetClipboardHook,
ISheetDiscreteRangeLocation,
IUniverSheetCopyDataModel,
} from './type';

export const PREDEFINED_HOOK_NAME = {
DEFAULT_COPY: 'default-copy',
Expand Down

0 comments on commit cd5e42f

Please sign in to comment.