Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
refactor(sheets-conditional-formatting): ref-range (#6335)
WEI ZHANG82cf0056155a
diff --git a/packages/sheets-conditional-formatting-ui/package.json b/packages/sheets-conditional-formatting-ui/package.jsonindex 0feb5d06ed..028fdf302b 100644--- a/packages/sheets-conditional-formatting-ui/package.json+++ b/packages/sheets-conditional-formatting-ui/package.json@@ -75,6 +75,7 @@"@univerjs/icons": "^1.0.2","@univerjs/sheets": "workspace:*","@univerjs/sheets-conditional-formatting": "workspace:*",+ "@univerjs/sheets-formula": "workspace:*","@univerjs/sheets-formula-ui": "workspace:*","@univerjs/sheets-ui": "workspace:*","@univerjs/ui": "workspace:*"diff --git a/packages/sheets-conditional-formatting-ui/src/controllers/cf-formula-ref-range.controller.ts b/packages/sheets-conditional-formatting-ui/src/controllers/cf-formula-ref-range.controller.tsnew file mode 100644index 0000000000..c551dd01e3--- /dev/null+++ b/packages/sheets-conditional-formatting-ui/src/controllers/cf-formula-ref-range.controller.ts@@ -0,0 +1,262 @@+/**+ * Copyright 2023-present DreamNum Co., Ltd.+ *+ * 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 { IDisposable, IMutationInfo, IRange } from '@univerjs/core';+import type { IColorScale, IConditionFormattingRule, IDataBar, IFormulaHighlightCell, IIconSet } from '@univerjs/sheets-conditional-formatting';+import { Disposable, Inject, Injector, toDisposable, Tools } from '@univerjs/core';+import {+ AddConditionalRuleMutation,+ AddConditionalRuleMutationUndoFactory,+ CFRuleType,+ CFSubRuleType,+ CFValueType,+ ConditionalFormattingRuleModel,+ createCfId,+ DeleteConditionalRuleMutation,+ SetConditionalRuleMutation,+ setConditionalRuleMutationUndoFactory,+} from '@univerjs/sheets-conditional-formatting';+import { FormulaRefRangeService } from '@univerjs/sheets-formula';++export class ConditionalFormattingFormulaRefRangeController extends Disposable {+ private _disposableMap: Map<string, IDisposable> = new Map();++ constructor(+ @Inject(ConditionalFormattingRuleModel) private _conditionalFormattingRuleModel: ConditionalFormattingRuleModel,+ @Inject(FormulaRefRangeService) private _formulaRefRangeService: FormulaRefRangeService,+ @Inject(Injector) private _injector: Injector+ ) {+ super();+ this._initRefRange();+ }++ private _getIdWithUnitId(unitID: string, subUnitId: string, cfId: string) {+ return `${unitID}_${subUnitId}_${cfId}`;+ }++ private _getRuleFormulas(rule: IConditionFormattingRule): string[] {+ const formulas: string[] = [];+ const ruleConfig = rule.rule;+ switch (ruleConfig.type) {+ case CFRuleType.highlightCell:+ if (ruleConfig.subType === CFSubRuleType.formula) {+ formulas.push((ruleConfig as IFormulaHighlightCell).value);+ }+ break;+ case CFRuleType.dataBar: {+ const dataBar = ruleConfig as IDataBar;+ if (dataBar.config.min.type === CFValueType.formula) {+ formulas.push(dataBar.config.min.value as string);+ }+ if (dataBar.config.max.type === CFValueType.formula) {+ formulas.push(dataBar.config.max.value as string);+ }+ break;+ }+ case CFRuleType.colorScale: {+ const colorScale = ruleConfig as IColorScale;+ colorScale.config.forEach((item) => {+ if (item.value.type === CFValueType.formula) {+ formulas.push(item.value.value as string);+ }+ });+ break;+ }+ case CFRuleType.iconSet: {+ const iconSet = ruleConfig as IIconSet;+ iconSet.config.forEach((item) => {+ if (item.value.type === CFValueType.formula) {+ formulas.push(item.value.value as string);+ }+ });+ break;+ }+ }+ return formulas;+ }++ private _updateRuleFormulas(rule: IConditionFormattingRule, formulas: string[]): IConditionFormattingRule {+ const newRule = Tools.deepClone(rule);+ const ruleConfig = newRule.rule;+ let formulaIndex = 0;+ switch (ruleConfig.type) {+ case CFRuleType.highlightCell:+ if (ruleConfig.subType === CFSubRuleType.formula) {+ (ruleConfig as IFormulaHighlightCell).value = formulas[formulaIndex++];+ }+ break;+ case CFRuleType.dataBar: {+ const dataBar = ruleConfig as IDataBar;+ if (dataBar.config.min.type === CFValueType.formula) {+ dataBar.config.min.value = formulas[formulaIndex++];+ }+ if (dataBar.config.max.type === CFValueType.formula) {+ dataBar.config.max.value = formulas[formulaIndex++];+ }+ break;+ }+ case CFRuleType.colorScale: {+ const colorScale = ruleConfig as IColorScale;+ colorScale.config.forEach((item) => {+ if (item.value.type === CFValueType.formula) {+ item.value.value = formulas[formulaIndex++];+ }+ });+ break;+ }+ case CFRuleType.iconSet: {+ const iconSet = ruleConfig as IIconSet;+ iconSet.config.forEach((item) => {+ if (item.value.type === CFValueType.formula) {+ item.value.value = formulas[formulaIndex++];+ }+ });+ break;+ }+ }+ return newRule;+ }++ register(unitId: string, subUnitId: string, rule: IConditionFormattingRule) {+ const oldRanges = rule.ranges;+ const oldFormulas = this._getRuleFormulas(rule);++ const disposable = this._formulaRefRangeService.registerRangeFormula(unitId, subUnitId, oldRanges, oldFormulas, (res: { formulas: string[]; ranges: IRange[] }[]) => {+ if (res.length === 0) {+ return {+ undos: [{+ id: AddConditionalRuleMutation.id,+ params: {+ unitId,+ subUnitId,+ rule,+ },+ }],+ redos: [{+ id: DeleteConditionalRuleMutation.id,+ params: {+ unitId,+ subUnitId,+ cfId: rule.cfId,+ },+ }],+ };+ }++ const redos: IMutationInfo[] = [];+ const undos: IMutationInfo[] = [];++ const first = res[0];+ const firstRule = this._updateRuleFormulas(rule, first.formulas);+ firstRule.ranges = first.ranges;++ redos.push({+ id: SetConditionalRuleMutation.id,+ params: {+ unitId,+ subUnitId,+ cfId: rule.cfId,+ rule: firstRule,+ },+ });+ undos.push(...setConditionalRuleMutationUndoFactory(this._injector, {+ unitId,+ subUnitId,+ cfId: rule.cfId,+ rule: firstRule,+ }));++ for (let i = 1; i < res.length; i++) {+ const item = res[i];+ const newCfId = createCfId();+ const newRule = this._updateRuleFormulas(rule, item.formulas);+ newRule.cfId = newCfId;+ newRule.ranges = item.ranges;++ redos.push({+ id: AddConditionalRuleMutation.id,+ params: {+ unitId,+ subUnitId,+ rule: newRule,+ },+ });+ undos.push(AddConditionalRuleMutationUndoFactory(this._injector, {+ unitId,+ subUnitId,+ rule: newRule,+ }));+ }++ return {+ undos,+ redos,+ };+ });++ const id = this._getIdWithUnitId(unitId, subUnitId, rule.cfId);+ this._disposableMap.set(id, disposable);+ }++ private _initRefRange() {+ const allRules = this._conditionalFormattingRuleModel.getAll();+ for (const [unitId, subUnitMap] of allRules) {+ for (const [subUnitId, rules] of subUnitMap) {+ for (const rule of rules) {+ this.register(unitId, subUnitId, rule);+ }+ }+ }++ this.disposeWithMe(+ this._conditionalFormattingRuleModel.$ruleChange.subscribe((option) => {+ const { unitId, subUnitId, rule } = option;+ switch (option.type) {+ case 'add': {+ this.register(unitId, subUnitId, rule);+ break;+ }+ case 'delete': {+ const id = this._getIdWithUnitId(unitId, subUnitId, rule.cfId);+ const disposable = this._disposableMap.get(id);+ if (disposable) {+ disposable.dispose();+ this._disposableMap.delete(id);+ }+ break;+ }+ case 'set': {+ const id = this._getIdWithUnitId(unitId, subUnitId, rule.cfId);+ const disposable = this._disposableMap.get(id);+ if (disposable) {+ disposable.dispose();+ this._disposableMap.delete(id);+ }+ this.register(unitId, subUnitId, rule);+ break;+ }+ }+ })+ );++ this.disposeWithMe(toDisposable(() => {+ this._disposableMap.forEach((item) => {+ item.dispose();+ });+ this._disposableMap.clear();+ }));+ }+}diff --git a/packages/sheets-conditional-formatting-ui/src/controllers/cf.ref-range.controller.ts b/packages/sheets-conditional-formatting-ui/src/controllers/cf.ref-range.controller.tsdeleted file mode 100644index 4126c12e0f..0000000000--- a/packages/sheets-conditional-formatting-ui/src/controllers/cf.ref-range.controller.ts+++ /dev/null@@ -1,102 +0,0 @@-/**- * Copyright 2023-present DreamNum Co., Ltd.- *- * 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 { IRange, Workbook } from '@univerjs/core';-import type { EffectRefRangeParams } from '@univerjs/sheets';-import type { IConditionFormattingRule, IDeleteConditionalRuleMutationParams, 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';--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- ) {- super();-- this._initRefRange();- }-- private _initRefRange() {- const disposableMap: Map<string, () => void> = new Map();- const getCfIdWithUnitId = (unitID: string, subUnitId: string, cfId: string) => `${unitID}_${subUnitId}_${cfId}`;- const register = (unitId: string, subUnitId: string, rule: IConditionFormattingRule) => {- const handleRangeChange = (commandInfo: EffectRefRangeParams) => {- const oldRanges = [...rule.ranges];- const resultRanges = oldRanges.map((range) => {- return handleDefaultRangeChangeWithEffectRefCommands(range, commandInfo) as IRange;- }).filter((range) => !!range);- const isEqual = isRangesEqual(resultRanges, oldRanges);- if (isEqual) {- return { redos: [], undos: [] };- }- if (resultRanges.length) {- const redoParams: ISetConditionalRuleMutationParams = { unitId, subUnitId, rule: { ...rule, ranges: resultRanges } };- const redos = [{ id: SetConditionalRuleMutation.id, params: redoParams }];- const undos = setConditionalRuleMutationUndoFactory(this._injector, redoParams);- return { redos, undos };- } else {- const redoParams: IDeleteConditionalRuleMutationParams = { unitId, subUnitId, cfId: rule.cfId };- const redos = [{ id: DeleteConditionalRuleMutation.id, params: redoParams }];- const undos = DeleteConditionalRuleMutationUndoFactory(this._injector, redoParams);- return { redos, undos };- }- };- const disposeList: (() => void)[] = [];- rule.ranges.forEach((range) => {- const disposable = this._refRangeService.registerRefRange(range, handleRangeChange);- disposeList.push(() => disposable.dispose());- });- disposableMap.set(getCfIdWithUnitId(unitId, subUnitId, rule.cfId), () => disposeList.forEach((dispose) => dispose()));- };-- this.disposeWithMe(this._conditionalFormattingRuleModel.$ruleChange.subscribe((option) => {- const { unitId, subUnitId, rule } = option;- const workbook = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;- const worksheet = workbook.getActiveSheet();- if (option.unitId !== workbook.getUnitId() || option.subUnitId !== worksheet?.getSheetId()) {- return;- }- switch (option.type) {- case 'add':{- register(option.unitId, option.subUnitId, option.rule);- break;- }- case 'delete':{- const dispose = disposableMap.get(getCfIdWithUnitId(unitId, subUnitId, rule.cfId));- dispose && dispose();- break;- }- case 'set':{- const dispose = disposableMap.get(getCfIdWithUnitId(unitId, subUnitId, rule.cfId));- dispose && dispose();- // and to add- register(option.unitId, option.subUnitId, option.rule);- }- }- }));-- this.disposeWithMe(toDisposable(() => {- disposableMap.forEach((item) => {- item();- });- disposableMap.clear();- }));- }-}diff --git a/packages/sheets-conditional-formatting-ui/src/mobile-plugin.ts b/packages/sheets-conditional-formatting-ui/src/mobile-plugin.tsindex 19f3854e51..344adeca7a 100644--- a/packages/sheets-conditional-formatting-ui/src/mobile-plugin.ts+++ b/packages/sheets-conditional-formatting-ui/src/mobile-plugin.ts@@ -37,10 +37,10 @@ import { AddTimePeriodCfCommand } from './commands/commands/add-time-period-cf.cimport { AddUniqueValuesCfCommand } from './commands/commands/add-unique-values-cf.command';import { OpenConditionalFormattingOperator } from './commands/operations/open-conditional-formatting-panel';+import { ConditionalFormattingFormulaRefRangeController } from './controllers/cf-formula-ref-range.controller';import { ConditionalFormattingCopyPasteController } from './controllers/cf.copy-paste.controller';import { ConditionalFormattingI18nController } from './controllers/cf.i18n.controller';import { ConditionalFormattingPermissionController } from './controllers/cf.permission.controller';-import { SheetsCfRefRangeController } from './controllers/cf.ref-range.controller';import { SheetsCfRenderController } from './controllers/cf.render.controller';import { defaultPluginConfig, SHEETS_CONDITIONAL_FORMATTING_UI_PLUGIN_CONFIG_KEY } from './controllers/config.schema';@@ -71,10 +71,10 @@ export class UniverSheetsConditionalFormattingMobileUIPlugin extends Plugin {this._initCommand();this._injector.add([SheetsCfRenderController]);- this._injector.add([SheetsCfRefRangeController]);this._injector.add([ConditionalFormattingCopyPasteController]);this._injector.add([ConditionalFormattingPermissionController]);this._injector.add([ConditionalFormattingI18nController]);+ this._injector.add([ConditionalFormattingFormulaRefRangeController]);}private _initCommand() {diff --git a/packages/sheets-conditional-formatting-ui/src/plugin.ts b/packages/sheets-conditional-formatting-ui/src/plugin.tsindex da18fe8ef6..87666014aa 100644--- a/packages/sheets-conditional-formatting-ui/src/plugin.ts+++ b/packages/sheets-conditional-formatting-ui/src/plugin.ts@@ -28,6 +28,7 @@ import {UniverInstanceType,} from '@univerjs/core';import { SHEET_CONDITIONAL_FORMATTING_PLUGIN, UniverSheetsConditionalFormattingPlugin } from '@univerjs/sheets-conditional-formatting';+import { UniverSheetsFormulaPlugin } from '@univerjs/sheets-formula';import { AddAverageCfCommand } from './commands/commands/add-average-cf.command';import { AddColorScaleConditionalRuleCommand } from './commands/commands/add-color-scale-cf.command';import { AddDataBarConditionalRuleCommand } from './commands/commands/add-data-bar-cf.command';@@ -36,9 +37,10 @@ import { AddNumberCfCommand } from './commands/commands/add-number-cf.command';import { AddRankCfCommand } from './commands/commands/add-rank-cf.command';import { AddTextCfCommand } from './commands/commands/add-text-cf.command';import { AddTimePeriodCfCommand } from './commands/commands/add-time-period-cf.command';-import { AddUniqueValuesCfCommand } from './commands/commands/add-unique-values-cf.command';+import { AddUniqueValuesCfCommand } from './commands/commands/add-unique-values-cf.command';import { OpenConditionalFormattingOperator } from './commands/operations/open-conditional-formatting-panel';+import { ConditionalFormattingFormulaRefRangeController } from './controllers/cf-formula-ref-range.controller';import { ConditionalFormattingAutoFillController } from './controllers/cf.auto-fill.controller';import { ConditionalFormattingClearController } from './controllers/cf.clear.controller';import { ConditionalFormattingCopyPasteController } from './controllers/cf.copy-paste.controller';@@ -48,12 +50,11 @@ import { ConditionalFormattingMenuController } from './controllers/cf.menu.contrimport { ConditionalFormattingPainterController } from './controllers/cf.painter.controller';import { ConditionalFormattingPanelController } from './controllers/cf.panel.controller';import { ConditionalFormattingPermissionController } from './controllers/cf.permission.controller';-import { SheetsCfRefRangeController } from './controllers/cf.ref-range.controller';import { SheetsCfRenderController } from './controllers/cf.render.controller';import { ConditionalFormattingViewportController } from './controllers/cf.viewport.controller';import { defaultPluginConfig, SHEETS_CONDITIONAL_FORMATTING_UI_PLUGIN_CONFIG_KEY } from './controllers/config.schema';-@DependentOn(UniverSheetsConditionalFormattingPlugin)+@DependentOn(UniverSheetsConditionalFormattingPlugin, UniverSheetsFormulaPlugin)export class UniverSheetsConditionalFormattingUIPlugin extends Plugin {static override pluginName = `${SHEET_CONDITIONAL_FORMATTING_PLUGIN}_UI_PLUGIN`;static override type = UniverInstanceType.UNIVER_SHEET;@@ -83,7 +84,6 @@ export class UniverSheetsConditionalFormattingUIPlugin extends Plugin {override onStarting(): void {registerDependencies(this._injector, [[SheetsCfRenderController],- [SheetsCfRefRangeController],[ConditionalFormattingCopyPasteController],[ConditionalFormattingAutoFillController],[ConditionalFormattingPermissionController],@@ -94,10 +94,12 @@ export class UniverSheetsConditionalFormattingUIPlugin extends Plugin {[ConditionalFormattingClearController],[ConditionalFormattingPainterController],[ConditionalFormattingViewportController],+ [ConditionalFormattingFormulaRefRangeController],]);touchDependencies(this._injector, [[SheetsCfRenderController],+ [ConditionalFormattingFormulaRefRangeController],]);}@@ -117,7 +119,6 @@ export class UniverSheetsConditionalFormattingUIPlugin extends Plugin {[ConditionalFormattingI18nController],[ConditionalFormattingPainterController],[ConditionalFormattingPermissionController],- [SheetsCfRefRangeController],[ConditionalFormattingViewportController],]);}diff --git a/packages/sheets-conditional-formatting/package.json b/packages/sheets-conditional-formatting/package.jsonindex 2fcc01ee82..19ff50e542 100644--- a/packages/sheets-conditional-formatting/package.json+++ b/packages/sheets-conditional-formatting/package.json@@ -76,8 +76,7 @@"@univerjs/core": "workspace:*","@univerjs/engine-formula": "workspace:*","@univerjs/engine-render": "workspace:*",- "@univerjs/sheets": "workspace:*",- "@univerjs/sheets-formula": "workspace:*"+ "@univerjs/sheets": "workspace:*"},"devDependencies": {"@univerjs-infra/shared": "workspace:*",diff --git a/packages/sheets-conditional-formatting/src/models/conditional-formatting-rule-model.ts b/packages/sheets-conditional-formatting/src/models/conditional-formatting-rule-model.tsindex 9057f8b02f..fbacf6a4f0 100644--- a/packages/sheets-conditional-formatting/src/models/conditional-formatting-rule-model.ts+++ b/packages/sheets-conditional-formatting/src/models/conditional-formatting-rule-model.ts@@ -62,6 +62,10 @@ export class ConditionalFormattingRuleModel {return list || null;}+ getAll() {+ return this._model;+ }+deleteRule(unitId: string, subUnitId: string, cfId: string) {const list = this.getSubunitRules(unitId, subUnitId);if (list) {diff --git a/packages/sheets-conditional-formatting/src/plugin.ts b/packages/sheets-conditional-formatting/src/plugin.tsindex e51ed89f61..7ab92e39e5 100644--- a/packages/sheets-conditional-formatting/src/plugin.ts+++ b/packages/sheets-conditional-formatting/src/plugin.ts@@ -17,7 +17,7 @@import type { Dependency } from '@univerjs/core';import type { IUniverSheetsConditionalFormattingConfig } from './controllers/config.schema';import { DependentOn, ICommandService, IConfigService, Inject, Injector, merge, Plugin, touchDependencies, UniverInstanceType } from '@univerjs/core';-import { UniverSheetsFormulaPlugin } from '@univerjs/sheets-formula';+import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula';import { SHEET_CONDITIONAL_FORMATTING_PLUGIN } from './base/const';import { AddCfCommand } from './commands/commands/add-cf.command';import { ClearRangeCfCommand } from './commands/commands/clear-range-cf.command';@@ -38,7 +38,7 @@ import { ConditionalFormattingViewModel } from './models/conditional-formatting-import { ConditionalFormattingFormulaService } from './services/conditional-formatting-formula.service';import { ConditionalFormattingService } from './services/conditional-formatting.service';-@DependentOn(UniverSheetsFormulaPlugin)+@DependentOn(UniverFormulaEnginePlugin)export class UniverSheetsConditionalFormattingPlugin extends Plugin {static override pluginName = SHEET_CONDITIONAL_FORMATTING_PLUGIN;static override type = UniverInstanceType.UNIVER_SHEET;diff --git a/packages/sheets-formula/src/services/formula-ref-range.service.ts b/packages/sheets-formula/src/services/formula-ref-range.service.tsindex 086ea656a6..8cbf7df626 100644--- a/packages/sheets-formula/src/services/formula-ref-range.service.ts+++ b/packages/sheets-formula/src/services/formula-ref-range.service.ts@@ -136,7 +136,6 @@ export class FormulaRefRangeService extends Disposable {const rangeMap = new Map<string, { unitId: string; subUnitId: string; range: IRange; sheetName: string }>();const sequenceNodes = this._lexerTreeBuilder.sequenceNodesBuilder(formula);const disposableCollection = new DisposableCollection();-const handleChange = (params: EffectRefRangeParams) => {const currentUnit = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;const currentSheet = currentUnit.getActiveSheet();@@ -251,7 +250,6 @@ export class FormulaRefRangeService extends Disposable {registerRangeFormula(unitId: string, subUnitId: string, oldRanges: IRange[], formulas: string[], callback: RangeFormulaChangeCallback): IDisposable {const disposableCollection = new DisposableCollection();const formulaDeps = formulas.map((formula) => this._getFormulaDependcy(unitId, subUnitId, formula, oldRanges));-// eslint-disable-next-line max-lines-per-functionconst handleRangeChange = (commandInfo: EffectRefRangeParams) => {const orginStartRow = oldRanges[0].startRow;diff --git a/packages/sheets/src/services/ref-range/__tests__/utils.spec.ts b/packages/sheets/src/services/ref-range/__tests__/utils.spec.tsindex f59d0e0f41..8ed7f8c8da 100644--- a/packages/sheets/src/services/ref-range/__tests__/utils.spec.ts+++ b/packages/sheets/src/services/ref-range/__tests__/utils.spec.ts@@ -1181,7 +1181,7 @@ describe('test ref-range move', () => {const result = handleInsertRowCommon(info, targetRange);expect(result).toEqual([{- startRow: 8,+ startRow: 5,endRow: 13, // 10 + (7 - 5 + 1)startColumn: 2,endColumn: 8,@@ -1311,7 +1311,7 @@ describe('test ref-range move', () => {expect(result).toEqual([{startRow: 2,endRow: 8,- startColumn: 8, // 5 + (7 - 5 + 1)+ startColumn: 5,endColumn: 13, // 10 + (7 - 5 + 1)}]);});diff --git a/packages/sheets/src/services/ref-range/ref-range.service.ts b/packages/sheets/src/services/ref-range/ref-range.service.tsindex 13981780d5..89f3813246 100644--- a/packages/sheets/src/services/ref-range/ref-range.service.ts+++ b/packages/sheets/src/services/ref-range/ref-range.service.ts@@ -182,7 +182,7 @@ export class RefRangeService extends Disposable {const params = command.params!;const startColumn = Math.min(params.fromRange.startColumn, params.toRange.startColumn);return this._checkRange(- [{ ...params.fromRange, startColumn, endColumn: worksheet.getColumnCount() - 1 }],+ [{ ...params.fromRange, startColumn, endColumn: MAX_ROW_COL }],unitId,subUnitId);@@ -192,7 +192,7 @@ export class RefRangeService extends Disposable {const startRow = Math.min(params.fromRange.startRow, params.toRange.startRow);return this._checkRange(- [{ ...params.fromRange, startRow, endRow: worksheet.getRowCount() - 1 }],+ [{ ...params.fromRange, startRow, endRow: MAX_ROW_COL }],unitId,subUnitId);@@ -209,10 +209,10 @@ export class RefRangeService extends Disposable {const params = command;const rowStart = params.params!.range.startRow;const range: IRange = {- startRow: rowStart,- endRow: worksheet.getRowCount() - 1,+ startRow: Math.max(0, rowStart - 1),+ endRow: MAX_ROW_COL,startColumn: 0,- endColumn: worksheet.getColumnCount() - 1,+ endColumn: MAX_ROW_COL,rangeType: RANGE_TYPE.ROW,};return this._checkRange([range], unitId, subUnitId);@@ -222,9 +222,9 @@ export class RefRangeService extends Disposable {const colStart = params.params!.range.startColumn;const range: IRange = {startRow: 0,- endRow: worksheet.getRowCount() - 1,- startColumn: colStart,- endColumn: worksheet.getColumnCount() - 1,+ endRow: MAX_ROW_COL,+ startColumn: Math.max(0, colStart - 1),+ endColumn: MAX_ROW_COL,rangeType: RANGE_TYPE.COLUMN,};return this._checkRange([range], unitId, subUnitId);@@ -235,9 +235,9 @@ export class RefRangeService extends Disposable {const rowStart = params.params!.range.startRow;const range: IRange = {startRow: rowStart,- endRow: worksheet.getRowCount() - 1,+ endRow: MAX_ROW_COL,startColumn: 0,- endColumn: worksheet.getColumnCount() - 1,+ endColumn: MAX_ROW_COL,rangeType: RANGE_TYPE.ROW,};return this._checkRange([range], unitId, subUnitId);@@ -247,9 +247,9 @@ export class RefRangeService extends Disposable {const colStart = params.params!.range.startColumn;const range: IRange = {startRow: 0,- endRow: worksheet.getRowCount() - 1,+ endRow: MAX_ROW_COL,startColumn: colStart,- endColumn: worksheet.getColumnCount() - 1,+ endColumn: MAX_ROW_COL,rangeType: RANGE_TYPE.COLUMN,};return this._checkRange([range], unitId, subUnitId);@@ -364,6 +364,7 @@ export class RefRangeService extends Disposable {endColumn: +range.endColumn,rangeType: range.rangeType && +range.rangeType,};+// Todo@Gggpound : How to reduce this calculationif (effectRanges.some((item) => Rectangle.intersects(item, realRange))diff --git a/packages/sheets/src/services/ref-range/util.ts b/packages/sheets/src/services/ref-range/util.tsindex b60b17b671..190415524b 100644--- a/packages/sheets/src/services/ref-range/util.ts+++ b/packages/sheets/src/services/ref-range/util.ts@@ -36,7 +36,7 @@ import type {IRemoveRowColCommand,IReorderRangeCommand,} from './type';-import { IUniverInstanceService, ObjectMatrix, queryObjectMatrix, Range, RANGE_TYPE, Rectangle } from '@univerjs/core';+import { Direction, IUniverInstanceService, ObjectMatrix, queryObjectMatrix, Range, RANGE_TYPE, Rectangle } from '@univerjs/core';import { DeleteRangeMoveLeftCommand } from '../../commands/commands/delete-range-move-left.command';import { DeleteRangeMoveUpCommand } from '../../commands/commands/delete-range-move-up.command';import { InsertRangeMoveDownCommand } from '../../commands/commands/insert-range-move-down.command';@@ -117,7 +117,7 @@ interface ILine {/*** see docs/tldr/ref-range/move-rows-cols.tldr*/-// eslint-disable-next-line max-lines-per-function, complexity+// eslint-disable-next-line max-lines-per-functionexport const handleBaseMoveRowsCols = (fromRange: ILine,toRange: ILine,@@ -982,48 +982,74 @@ export const handleInsertRowCommon = (info: ICommandInfo<IInsertRowCommandParamsconst param = info.params!;const insertRow = param.range.startRow;const insertCount = param.range.endRow - param.range.startRow + 1;-- if (targetRange.startRow >= insertRow) {- return [{- startRow: targetRange.startRow + insertCount,- endRow: targetRange.endRow + insertCount,- startColumn: targetRange.startColumn,- endColumn: targetRange.endColumn,- }];- } else if (targetRange.endRow < insertRow) {- return [targetRange];+ const direction = param.direction;++ if (direction === Direction.UP) {+ if (insertRow < targetRange.startRow) {+ return [{+ ...targetRange,+ startRow: targetRange.startRow + insertCount,+ endRow: targetRange.endRow + insertCount,+ }];+ } else if (insertRow <= targetRange.endRow) {+ return [{+ ...targetRange,+ endRow: targetRange.endRow + insertCount,+ }];+ }} else {- return [{- startRow: targetRange.startRow,- endRow: targetRange.endRow + insertCount,- startColumn: targetRange.startColumn,- endColumn: targetRange.endColumn,- }];+ if (insertRow <= targetRange.startRow) {+ return [{+ ...targetRange,+ startRow: targetRange.startRow + insertCount,+ endRow: targetRange.endRow + insertCount,+ }];+ } else if (insertRow <= targetRange.endRow + 1) {+ return [{+ ...targetRange,+ endRow: targetRange.endRow + insertCount,+ }];+ }}++ return [targetRange];};export const handleInsertColCommon = (info: ICommandInfo<IInsertColCommandParams>, targetRange: IRange) => {const param = info.params!;const insertColumn = param.range.startColumn;const insertCount = param.range.endColumn - param.range.startColumn + 1;-- if (targetRange.startColumn >= insertColumn) {- return [{- startRow: targetRange.startRow,- endRow: targetRange.endRow,- startColumn: targetRange.startColumn + insertCount,- endColumn: targetRange.endColumn + insertCount,- }];- } else if (targetRange.endColumn < insertColumn) {- return [targetRange];+ const direction = param.direction;++ if (direction === Direction.LEFT) {+ if (insertColumn < targetRange.startColumn) {+ return [{+ ...targetRange,+ startColumn: targetRange.startColumn + insertCount,+ endColumn: targetRange.endColumn + insertCount,+ }];+ } else if (insertColumn <= targetRange.endColumn) {+ return [{+ ...targetRange,+ endColumn: targetRange.endColumn + insertCount,+ }];+ }} else {- return [{- startRow: targetRange.startRow,- endRow: targetRange.endRow,- startColumn: targetRange.startColumn,- endColumn: targetRange.endColumn + insertCount,- }];+ if (insertColumn <= targetRange.startColumn) {+ return [{+ ...targetRange,+ startColumn: targetRange.startColumn + insertCount,+ endColumn: targetRange.endColumn + insertCount,+ }];+ } else if (insertColumn <= targetRange.endColumn + 1) {+ return [{+ ...targetRange,+ endColumn: targetRange.endColumn + insertCount,+ }];+ }}++ return [targetRange];};export const runRefRangeMutations = (operators: IOperator[], range: IRange) => {diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 53a4e3176b..49f56cc2ba 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -1970,9 +1970,6 @@ importers:'@univerjs/sheets':specifier: workspace:*version: link:../sheets- '@univerjs/sheets-formula':- specifier: workspace:*- version: link:../sheets-formuladevDependencies:'@univerjs-infra/shared':specifier: workspace:*@@ -2016,6 +2013,9 @@ importers:'@univerjs/sheets-conditional-formatting':specifier: workspace:*version: link:../sheets-conditional-formatting+ '@univerjs/sheets-formula':+ specifier: workspace:*+ version: link:../sheets-formula'@univerjs/sheets-formula-ui':specifier: workspace:*version: link:../sheets-formula-ui