Skip to content

Commit

Permalink
fix(conditional-formatting): delete and undo error
Browse files Browse the repository at this point in the history
  • Loading branch information
Gggpound committed Apr 2, 2024
1 parent 6a299ad commit cfea213
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
} from '@univerjs/core';
import type { IAccessor } from '@wendellhu/redi';
import { ConditionalFormattingRuleModel } from '../../models/conditional-formatting-rule-model';
import { transformSupportSymmetryAnchor } from '../../utils/anchor';
import type { IAddConditionalRuleMutationParams } from './add-conditional-rule.mutation';
import { AddConditionalRuleMutation } from './add-conditional-rule.mutation';
import type { IMoveConditionalRuleMutationParams } from './move-conditional-rule.mutation';
Expand All @@ -34,18 +35,30 @@ export interface IDeleteConditionalRuleMutationParams {
export const DeleteConditionalRuleMutationUndoFactory = (accessor: IAccessor, param: IDeleteConditionalRuleMutationParams) => {
const conditionalFormattingRuleModel = accessor.get(ConditionalFormattingRuleModel);
const { unitId, subUnitId, cfId } = param;
const rule = conditionalFormattingRuleModel.getRule(unitId, subUnitId, cfId);
const ruleList = conditionalFormattingRuleModel.getSubunitRules(unitId, subUnitId);
if (rule) {
const index = ruleList!.findIndex((rule) => rule.cfId === cfId);
const nextRule = ruleList![index - 1];
const result: IMutationInfo[] = [{ id: AddConditionalRuleMutation.id,
params: { unitId, subUnitId, rule: Tools.deepClone(rule) } as IAddConditionalRuleMutationParams },
];
if (nextRule && index !== 0) {
result.push({ id: MoveConditionalRuleMutation.id, params: {
unitId, subUnitId, cfId, targetCfId: nextRule.cfId,
} as IMoveConditionalRuleMutationParams });
const ruleList = ([...(conditionalFormattingRuleModel.getSubunitRules(unitId, subUnitId) || [])]);
const index = ruleList.findIndex((item) => item.cfId === cfId);
const beforeRule = ruleList[index - 1];
if (index > -1) {
const rule = ruleList[index];
const result: IMutationInfo[] = [{
id: AddConditionalRuleMutation.id,
params: { unitId, subUnitId, rule: Tools.deepClone(rule) } as IAddConditionalRuleMutationParams,
}];
ruleList.splice(index, 1);
if (index !== 0) {
const firstRule = ruleList[0];
if (firstRule) {
const transformResult = transformSupportSymmetryAnchor({ id: firstRule.cfId, type: 'before' }, { id: beforeRule.cfId, type: 'after' }, ruleList, (rule) => rule.cfId);
if (!transformResult) {
return result;
}
const [start, end] = transformResult;
const params: IMoveConditionalRuleMutationParams = {
unitId, subUnitId, start,
end,
};
result.push({ id: MoveConditionalRuleMutation.id, params });
}
}
return result;
}
Expand Down
8 changes: 3 additions & 5 deletions packages/sheets-conditional-formatting/src/utils/anchor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,6 @@ export const moveByAnchor = <T = unknown[]>(start: IAnchor, end: IAnchor, ruleLi

/**
* 只有 [after,after] and [after,before] 能够支持对称操作
*
* @template T
* @param {IAnchor} anchor
* @param {T[]} ruleList
* @param {(v: T) => string} get
*/
export const transformSupportSymmetryAnchor = <T = unknown[]>(start: IAnchor, end: IAnchor, ruleList: T[], get: (v: T) => string): [IAnchor, IAnchor] | null => {
if (start.type === 'after' && ['after', 'before'].includes(end.type)) {
Expand Down Expand Up @@ -127,6 +122,9 @@ export const transformSupportSymmetryAnchor = <T = unknown[]>(start: IAnchor, en
return null;
}
}
if (_start.id === _end.id && _start.type === _end.type) {
return null;
}
return [_start, _end];
};
export const anchorUndoFactory = (start: IAnchor, end: IAnchor): [IAnchor, IAnchor] | null => {
Expand Down

0 comments on commit cfea213

Please sign in to comment.