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(cf): fix cf custom formula range change #4381

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions packages/sheets-conditional-formatting-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"@univerjs/icons": "^0.2.11",
"@univerjs/sheets": "workspace:*",
"@univerjs/sheets-conditional-formatting": "workspace:*",
"@univerjs/sheets-formula": "workspace:*",
"@univerjs/sheets-formula-ui": "workspace:*",
"@univerjs/sheets-ui": "workspace:*",
"@univerjs/ui": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,34 @@

import type { IRange, Workbook } from '@univerjs/core';
import type { EffectRefRangeParams } from '@univerjs/sheets';
import type { IConditionFormattingRule, IDeleteConditionalRuleMutationParams, ISetConditionalRuleMutationParams } from '@univerjs/sheets-conditional-formatting';
import type { IColorScale, IConditionFormattingRule, IDataBar, IDeleteConditionalRuleMutationParams, IFormulaHighlightCell, IIconSet, ISetConditionalRuleMutationParams } from '@univerjs/sheets-conditional-formatting';
import { Disposable, Inject, Injector, IUniverInstanceService, toDisposable, UniverInstanceType } from '@univerjs/core';
import { handleDefaultRangeChangeWithEffectRefCommands, RefRangeService } from '@univerjs/sheets';
import { ConditionalFormattingRuleModel, DeleteConditionalRuleMutation, DeleteConditionalRuleMutationUndoFactory, isRangesEqual, SetConditionalRuleMutation, setConditionalRuleMutationUndoFactory } from '@univerjs/sheets-conditional-formatting';
import { CFRuleType, CFSubRuleType, CFValueType, ConditionalFormattingRuleModel, DeleteConditionalRuleMutation, DeleteConditionalRuleMutationUndoFactory, isRangesEqual, SetConditionalRuleMutation, setConditionalRuleMutationUndoFactory } from '@univerjs/sheets-conditional-formatting';
import { FormulaRefRangeService } from '@univerjs/sheets-formula';

interface ISetConditionalRuleMutationParamsRedo extends ISetConditionalRuleMutationParams {
payload: {
newRanges?: IRange[];
newFormulaString?: string;
type?: CFRuleType;
valueTypeOrIndex?: string | number;
};
}

export class SheetsCfRefRangeController extends Disposable {
constructor(
@Inject(ConditionalFormattingRuleModel) private _conditionalFormattingRuleModel: ConditionalFormattingRuleModel,
@Inject(IUniverInstanceService) private _univerInstanceService: IUniverInstanceService,
@Inject(Injector) private _injector: Injector,
@Inject(RefRangeService) private _refRangeService: RefRangeService
@Inject(RefRangeService) private _refRangeService: RefRangeService,
@Inject(FormulaRefRangeService) private _formulaRefRangeService: FormulaRefRangeService
) {
super();

this._initRefRange();
this._initMergeRedo();
this._initMergeUndo();
}

private _initRefRange() {
Expand All @@ -47,7 +60,7 @@ export class SheetsCfRefRangeController extends Disposable {
return { redos: [], undos: [] };
}
if (resultRanges.length) {
const redoParams: ISetConditionalRuleMutationParams = { unitId, subUnitId, rule: { ...rule, ranges: resultRanges } };
const redoParams: ISetConditionalRuleMutationParamsRedo = { unitId, subUnitId, rule, payload: { newRanges: resultRanges } };
const redos = [{ id: SetConditionalRuleMutation.id, params: redoParams }];
const undos = setConditionalRuleMutationUndoFactory(this._injector, redoParams);
return { redos, undos };
Expand All @@ -58,11 +71,14 @@ export class SheetsCfRefRangeController extends Disposable {
return { redos, undos };
}
};

const disposeList: (() => void)[] = [];
rule.ranges.forEach((range) => {
const disposable = this._refRangeService.registerRefRange(range, handleRangeChange);
disposeList.push(() => disposable.dispose());
});
// Conditional formatting formulas also need to monitor changes in the reference range
this._registerFormula(unitId, subUnitId, rule, disposeList);
disposableMap.set(getCfIdWithUnitId(unitId, subUnitId, rule.cfId), () => disposeList.forEach((dispose) => dispose()));
};

Expand Down Expand Up @@ -99,4 +115,236 @@ export class SheetsCfRefRangeController extends Disposable {
disposableMap.clear();
}));
}

// eslint-disable-next-line max-lines-per-function
private _registerFormula(unitId: string, subUnitId: string, rule: IConditionFormattingRule, disposeList: (() => void)[]) {
// highlightCell custom formula
if ('subType' in rule.rule && rule.rule.subType === CFSubRuleType.formula) {
const disposable = this._formulaRefRangeService.registerFormula(
unitId,
subUnitId,
rule.rule.value,
(newFormulaString) => this._handleFormulaChange(
newFormulaString,
(rule.rule as IFormulaHighlightCell).value,
unitId,
subUnitId,
rule,
CFRuleType.highlightCell
)
);
disposeList.push(() => disposable.dispose());
}

// dataBar value formula
if (rule.rule.type === CFRuleType.dataBar) {
if (rule.rule.config.min.type === CFValueType.formula) {
const disposable = this._formulaRefRangeService.registerFormula(
unitId,
subUnitId,
rule.rule.config.min.value as string,
(newFormulaString) => this._handleFormulaChange(
newFormulaString,
(rule.rule as IDataBar).config.min.value as string,
unitId,
subUnitId,
rule,
CFRuleType.dataBar,
'min'
)
);
disposeList.push(() => disposable.dispose());
}

if (rule.rule.config.max.type === CFValueType.formula) {
const disposable = this._formulaRefRangeService.registerFormula(
unitId,
subUnitId,
rule.rule.config.max.value as string,
(newFormulaString) => this._handleFormulaChange(
newFormulaString,
(rule.rule as IDataBar).config.max.value as string,
unitId,
subUnitId,
rule,
CFRuleType.dataBar,
'max'
)
);
disposeList.push(() => disposable.dispose());
}
}

// colorScale value formula
if (rule.rule.type === CFRuleType.colorScale) {
rule.rule.config.forEach((item) => {
if (item.value.type === CFValueType.formula) {
const disposable = this._formulaRefRangeService.registerFormula(
unitId,
subUnitId,
item.value.value as string,
(newFormulaString) => this._handleFormulaChange(
newFormulaString,
item.value.value as string,
unitId,
subUnitId,
rule,
CFRuleType.colorScale,
item.index
)
);
disposeList.push(() => disposable.dispose());
}
});
}

// iconSet value formula
if (rule.rule.type === CFRuleType.iconSet) {
rule.rule.config.forEach((item) => {
if (item.value.type === CFValueType.formula) {
const disposable = this._formulaRefRangeService.registerFormula(
unitId,
subUnitId,
item.value.value as string,
(newFormulaString) => this._handleFormulaChange(
newFormulaString,
item.value.value as string,
unitId,
subUnitId,
rule,
CFRuleType.iconSet,
item.iconId
)
);
disposeList.push(() => disposable.dispose());
}
});
}
}

private _handleFormulaChange(
newFormulaString: string,
oldFormulaString: string,
unitId: string,
subUnitId: string,
rule: IConditionFormattingRule,
type: CFRuleType,
valueTypeOrIndex?: string | number
) {
if (oldFormulaString === newFormulaString) {
return { redos: [], undos: [] };
}

const redoParams: ISetConditionalRuleMutationParamsRedo = {
unitId,
subUnitId,
rule,
payload: {
newFormulaString,
type,
valueTypeOrIndex,
},
};
const redos = [{ id: SetConditionalRuleMutation.id, params: redoParams }];
const undos = setConditionalRuleMutationUndoFactory(this._injector, redoParams);
return { redos, undos };
}

private _initMergeRedo() {
this._refRangeService.interceptor.intercept(this._refRangeService.interceptor.getInterceptPoints().MERGE_REDO, {
priority: 100,
handler: (list, _c, next) => {
const redo = list?.filter((item) => item.id === SetConditionalRuleMutation.id) || [];
const result = list?.filter((item) => item.id !== SetConditionalRuleMutation.id) || [];

if (redo.length) {
// merge redo with the same cfId
const redoMap: Record<string, ISetConditionalRuleMutationParamsRedo[]> = {};
redo.forEach((item) => {
const { unitId, subUnitId, rule: { cfId } } = item.params as ISetConditionalRuleMutationParamsRedo;
if (!redoMap[`${unitId}_${subUnitId}_${cfId}`]) {
redoMap[`${unitId}_${subUnitId}_${cfId}`] = [];
}
redoMap[`${unitId}_${subUnitId}_${cfId}`].push(item.params as ISetConditionalRuleMutationParamsRedo);
});
Object.values(redoMap).forEach((paramsList) => {
const { payload, ...initValue } = paramsList[0];

result.push({
id: SetConditionalRuleMutation.id,
params: paramsList.reduce((pre, cur) => {
const newRule = { ...pre };
if (cur.payload.newRanges) {
newRule.rule.ranges = cur.payload.newRanges;
}
if (cur.payload.newFormulaString) {
if (cur.payload.type === CFRuleType.highlightCell) {
(newRule.rule.rule as IFormulaHighlightCell).value = cur.payload.newFormulaString;
}
if (cur.payload.type === CFRuleType.dataBar) {
if (cur.payload.valueTypeOrIndex === 'min') {
(newRule.rule.rule as IDataBar).config.min.value = cur.payload.newFormulaString;
}
if (cur.payload.valueTypeOrIndex === 'max') {
(newRule.rule.rule as IDataBar).config.max.value = cur.payload.newFormulaString;
}
}
if (cur.payload.type === CFRuleType.colorScale) {
(newRule.rule.rule as IColorScale).config.map((item) => {
if (item.index === cur.payload.valueTypeOrIndex) {
item.value.value = cur.payload.newFormulaString;
}
return item;
});
}
if (cur.payload.type === CFRuleType.iconSet) {
(newRule.rule.rule as IIconSet).config.map((item) => {
if (item.iconId === cur.payload.valueTypeOrIndex) {
item.value.value = cur.payload.newFormulaString;
}
return item;
});
}
}
return newRule;
}, initValue),
});
});
}

return next(result);
},
});
}

private _initMergeUndo() {
this._refRangeService.interceptor.intercept(this._refRangeService.interceptor.getInterceptPoints().MERGE_UNDO, {
priority: 100,
handler: (list, _c, next) => {
const undo = list?.filter((item) => item.id === SetConditionalRuleMutation.id) || [];
const result = list?.filter((item) => item.id !== SetConditionalRuleMutation.id) || [];

if (undo.length) {
// merge undo with the same cfId
const undoMap: Record<string, ISetConditionalRuleMutationParams[]> = {};
undo.forEach((item) => {
const { unitId, subUnitId, rule: { cfId } } = item.params as ISetConditionalRuleMutationParams;
if (!undoMap[`${unitId}_${subUnitId}_${cfId}`]) {
undoMap[`${unitId}_${subUnitId}_${cfId}`] = [];
}
undoMap[`${unitId}_${subUnitId}_${cfId}`].push(item.params as ISetConditionalRuleMutationParams);
});
Object.values(undoMap).forEach((paramsList) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undo 不需要处理么?

result.push({
id: SetConditionalRuleMutation.id,
params: paramsList[0],
});
});
}

return next(result);
},

});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/

import type { IAccessor, IMutation } from '@univerjs/core';
import type { IConditionFormattingRule } from '../../models/type';
import { CommandType, Tools } from '@univerjs/core';
import { ConditionalFormattingRuleModel } from '../../models/conditional-formatting-rule-model';
import type { IConditionFormattingRule } from '../../models/type';

export interface ISetConditionalRuleMutationParams {
unitId: string;
Expand Down Expand Up @@ -56,8 +56,7 @@ export const setConditionalRuleMutationUndoFactory = (accessor: IAccessor, param
cfId,
rule: Tools.deepClone(rule),
} as ISetConditionalRuleMutationParams,
},
];
}];
}
return [];
};
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading