Skip to content

Commit

Permalink
feat(cf): support clear (#433)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gggpound committed Mar 23, 2024
1 parent c392497 commit 61d8535
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* 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.
*/

import type { IMutationInfo } from '@univerjs/core';
import { Disposable, IUniverInstanceService, LifecycleStages, OnLifecycle, Rectangle } from '@univerjs/core';
import { ClearSelectionAllCommand, ClearSelectionFormatCommand, RangeMergeUtil, SelectionManagerService, SheetInterceptorService } from '@univerjs/sheets';
import { Inject, Injector } from '@wendellhu/redi';
import { ConditionalFormatRuleModel } from '../models/conditional-format-rule-model';
import type { ISetConditionalRuleMutationParams } from '../commands/mutations/setConditionalRule.mutation';
import { setConditionalRuleMutation, setConditionalRuleMutationUndoFactory } from '../commands/mutations/setConditionalRule.mutation';
import type { IDeleteConditionalRuleMutationParams } from '../commands/mutations/deleteConditionalRule.mutation';
import { deleteConditionalRuleMutation, deleteConditionalRuleMutationUndoFactory } from '../commands/mutations/deleteConditionalRule.mutation';

@OnLifecycle(LifecycleStages.Rendered, ConditionalFormatClearController)
export class ConditionalFormatClearController extends Disposable {
constructor(
@Inject(Injector) private _injector: Injector,
@Inject(IUniverInstanceService) private _univerInstanceService: IUniverInstanceService,
@Inject(SheetInterceptorService) private _sheetInterceptorService: SheetInterceptorService,
@Inject(SelectionManagerService) private _selectionManagerService: SelectionManagerService,
@Inject(ConditionalFormatRuleModel) private _conditionalFormatRuleModel: ConditionalFormatRuleModel

) {
super();

this._init();
}

private _init() {
this.disposeWithMe(this._sheetInterceptorService.interceptCommand({ getMutations: (commandInfo) => {
const redos: IMutationInfo[] = [];
const undos: IMutationInfo[] = [];
const defaultV = { redos, undos };
if ([ClearSelectionFormatCommand.id, ClearSelectionAllCommand.id].includes(commandInfo.id)) {
const ranges = this._selectionManagerService.getSelectionRanges();
if (!ranges) {
return defaultV;
}
const workbook = this._univerInstanceService.getCurrentUniverSheetInstance();
const worksheet = workbook.getActiveSheet();
const allRules = this._conditionalFormatRuleModel.getSubunitRules(workbook.getUnitId(), worksheet.getSheetId());
if (!allRules || !allRules.length) {
return defaultV;
}
allRules.filter((rule) => {
return ranges.some((range) => rule.ranges.some((ruleRange) => Rectangle.getIntersects(ruleRange, range)));
}).forEach((rule) => {
const mergeUtil = new RangeMergeUtil();
const mergeRanges = mergeUtil.add(...rule.ranges).subtract(...ranges).merge();
if (mergeRanges.length) {
const redo: IMutationInfo<ISetConditionalRuleMutationParams> = {
id: setConditionalRuleMutation.id,
params: {
unitId: workbook.getUnitId(),
subUnitId: worksheet.getSheetId(),
rule: { ...rule, ranges: mergeRanges },
},
};
const undo = setConditionalRuleMutationUndoFactory(this._injector, redo.params);
redos.push(redo);
undos.push(...undo);
} else {
const redo: IMutationInfo<IDeleteConditionalRuleMutationParams> = {
id: deleteConditionalRuleMutation.id,
params: {
unitId: workbook.getUnitId(),
subUnitId: worksheet.getSheetId(),
cfId: rule.cfId,
},
};
const undo = deleteConditionalRuleMutationUndoFactory(this._injector, redo.params);
redos.push(redo);
undos.push(...undo);
}
});
}
return defaultV;
} }));
}
}
2 changes: 2 additions & 0 deletions packages/sheets-conditional-format/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import { moveConditionalRuleMutation } from './commands/mutations/move-condition
import { ConditionalFormatFormulaService } from './services/conditional-format-formula.service';
import { conditionalFormatFormulaMarkDirty } from './commands/mutations/formula-mark-dirty.mutation';
import { ConditionalFormatEditorController } from './controllers/cf.editor.controller';
import { ConditionalFormatClearController } from './controllers/cf.clear.controller';

export class SheetsConditionalFormatPlugin extends Plugin {
static override type = PluginType.Sheet;
Expand Down Expand Up @@ -95,6 +96,7 @@ export class SheetsConditionalFormatPlugin extends Plugin {
this._injector.add([ConditionalFormatMenuController]);
this._injector.add([ConditionalFormatI18nController]);
this._injector.add([ConditionalFormatEditorController]);
this._injector.add([ConditionalFormatClearController]);
}

_initCommand() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const highlightCellCalculateUnit: ICalculateUnit = {
case SubRuleType.text:{
const subRuleConfig = ruleConfig as ITextHighlightCell;
const value = getCellValue(cellValue!);
const v = String(value);
const v = value === null ? '' : String(value);
const condition = subRuleConfig.value || '';
switch (subRuleConfig.operator) {
case TextOperator.beginsWith:{
Expand Down

0 comments on commit 61d8535

Please sign in to comment.