Skip to content

Commit

Permalink
feat(conditional-formatting): support set cfId
Browse files Browse the repository at this point in the history
  • Loading branch information
Gggpound committed Apr 1, 2024
1 parent 2019a77 commit cfe2083
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type { IConditionFormattingRule } from '../../models/type';
export interface ISetConditionalRuleMutationParams {
unitId: string;
subUnitId: string;
cfId?: string;
rule: IConditionFormattingRule;
}

Expand All @@ -36,25 +37,29 @@ export const SetConditionalRuleMutation: IMutation<ISetConditionalRuleMutationPa
return false;
}
const { unitId, subUnitId, rule } = params;
const cfId = params.cfId || params.rule.cfId;

const conditionalFormattingRuleModel = accessor.get(ConditionalFormattingRuleModel);
conditionalFormattingRuleModel.setRule(unitId, subUnitId, rule);
conditionalFormattingRuleModel.setRule(unitId, subUnitId, rule, cfId);
return true;
},
};

export const setConditionalRuleMutationUndoFactory = (accessor: IAccessor, param: ISetConditionalRuleMutationParams) => {
const conditionalFormattingRuleModel = accessor.get(ConditionalFormattingRuleModel);
const { unitId, subUnitId } = param;
const cfId = param.rule.cfId;
const cfId = param.cfId || param.rule.cfId;
const rule = conditionalFormattingRuleModel.getRule(unitId, subUnitId, cfId);
if (rule) {
return [{
id: SetConditionalRuleMutation.id,
params: {
unitId,
subUnitId,
cfId,
rule: Tools.deepClone(rule),
} as ISetConditionalRuleMutationParams },
} as ISetConditionalRuleMutationParams,
},
];
}
return [];
Expand Down
1 change: 1 addition & 0 deletions packages/sheets-conditional-formatting/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export * from './models/type';
export * from './models/conditional-formatting-rule-model';
export * from './models/conditional-formatting-view-model';
export * from './utils/getStringFromDataStream';
export * from './utils/createCfId';
export * from './utils/isRangesEqual';
export * from './utils/removeUndefinedAttr';
export * from './utils/type';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@
import { Inject, Injector } from '@wendellhu/redi';
import { Range } from '@univerjs/core';
import { Subject } from 'rxjs';
import { createCfId } from '../utils/createCfId';
import { ConditionalFormattingService } from '../services/conditional-formatting.service';
import type { IConditionFormattingRule, IRuleModel } from './type';
import { ConditionalFormattingViewModel } from './conditional-formatting-view-model';

type RuleOperatorType = 'delete' | 'set' | 'add' | 'sort';
export class ConditionalFormattingRuleModel {
// Map<unitID ,<sheetId ,IConditionFormattingRule[]>>
// Map<unitID ,<sheetId ,IConditionFormattingRule[]>>
private _model: IRuleModel = new Map();
private _ruleChange$ = new Subject<{ rule: IConditionFormattingRule;unitId: string;subUnitId: string; type: RuleOperatorType }>();
private _ruleChange$ = new Subject<{ rule: IConditionFormattingRule; unitId: string; subUnitId: string; type: RuleOperatorType }>();
$ruleChange = this._ruleChange$.asObservable();

constructor(@Inject(ConditionalFormattingViewModel) private _conditionalFormattingViewModel: ConditionalFormattingViewModel,
Expand Down Expand Up @@ -84,9 +85,9 @@ export class ConditionalFormattingRuleModel {
}
}

setRule(unitId: string, subUnitId: string, rule: IConditionFormattingRule) {
setRule(unitId: string, subUnitId: string, rule: IConditionFormattingRule, oldCfId: string) {
const list = this._ensureList(unitId, subUnitId);
const oldRule = list.find((item) => item.cfId === rule.cfId);
const oldRule = list.find((item) => item.cfId === oldCfId);
if (oldRule) {
const cfPriorityMap = list.map((item) => item.cfId).reduce((map, cur, index) => {
map.set(cur, index);
Expand All @@ -97,32 +98,36 @@ export class ConditionalFormattingRuleModel {
// Otherwise the render will flash once
const cloneRange = [...oldRule.ranges];
const conditionalFormattingService = this._injector.get(ConditionalFormattingService);

Object.assign(oldRule, rule);

const dispose = conditionalFormattingService.interceptorManager.intercept(conditionalFormattingService.interceptorManager.getInterceptPoints().beforeUpdateRuleResult, {
handler: (config) => {
if (unitId === config?.unitId && subUnitId === config.subUnitId && rule.cfId === config.cfId) {
if (unitId === config?.unitId && subUnitId === config.subUnitId && oldRule.cfId === config.cfId) {
cloneRange.forEach((range) => {
Range.foreach(range, (row, col) => {
this._conditionalFormattingViewModel.deleteCellCf(unitId, subUnitId, row, col, oldRule.cfId);
});
});
rule.ranges.forEach((range) => {
oldRule.ranges.forEach((range) => {
Range.foreach(range, (row, col) => {
this._conditionalFormattingViewModel.pushCellCf(unitId, subUnitId, row, col, rule.cfId);
this._conditionalFormattingViewModel.pushCellCf(unitId, subUnitId, row, col, oldRule.cfId);
this._conditionalFormattingViewModel.sortCellCf(unitId, subUnitId, row, col, cfPriorityMap);
});
});
dispose();
}
},
});
rule.ranges.forEach((range) => {

oldRule.ranges.forEach((range) => {
Range.foreach(range, (row, col) => {
this._conditionalFormattingViewModel.pushCellCf(unitId, subUnitId, row, col, rule.cfId);
this._conditionalFormattingViewModel.pushCellCf(unitId, subUnitId, row, col, oldRule.cfId);
});
});
Object.assign(oldRule, rule);
this._conditionalFormattingViewModel.markRuleDirty(unitId, subUnitId, rule);
this._ruleChange$.next({ rule, subUnitId, unitId, type: 'set' });

this._conditionalFormattingViewModel.markRuleDirty(unitId, subUnitId, oldRule);
this._ruleChange$.next({ rule: oldRule, subUnitId, unitId, type: 'set' });
}
}

Expand Down Expand Up @@ -175,8 +180,7 @@ export class ConditionalFormattingRuleModel {
}
}

createCfId(unitId: string, subUnitId: string) {
const list = this._model.get(unitId)?.get(subUnitId);
return `${(list?.length || 0) + 1}`;
createCfId(_unitId: string, _subUnitId: string) {
return createCfId();
}
}
20 changes: 20 additions & 0 deletions packages/sheets-conditional-formatting/src/utils/createCfId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* 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 { Tools } from '@univerjs/core';

// Given that unit and sunUnit will change in the case of replica creation, the ID will not be spelled in here
export const createCfId = () => `${Tools.generateRandomId(8)}`;

0 comments on commit cfe2083

Please sign in to comment.