Skip to content

Commit

Permalink
fix(formula): calculate after renderer (#4024)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dushusir authored Nov 9, 2024
1 parent d41b305 commit 0afcc5d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 25 deletions.
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,4 @@ export type { IDataValidationRule, IDataValidationRuleBase, IDataValidationRuleI
export { type IRTreeItem, RTree } from './shared/r-tree';

export { type IUniverConfig, Univer } from './univer';
export { isNodeEnv } from './shared/tools';
5 changes: 5 additions & 0 deletions packages/core/src/shared/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -732,3 +732,8 @@ export function composeStyles(...styles: Nullable<IStyleData>[]): IStyleData {
}
return result;
}

export const isNodeEnv = () => {
// eslint-disable-next-line node/prefer-global/process
return typeof process !== 'undefined' && process.versions != null && process.versions.node != null;
};
26 changes: 16 additions & 10 deletions packages/engine-formula/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import type { Dependency } from '@univerjs/core';
import type { IUniverEngineFormulaConfig } from './controller/config.schema';
import { IConfigService, Inject, Injector, Plugin } from '@univerjs/core';
import { IConfigService, Inject, Injector, Plugin, touchDependencies } from '@univerjs/core';
import { CalculateController } from './controller/calculate.controller';
import { defaultPluginConfig, PLUGIN_CONFIG_KEY } from './controller/config.schema';
import { FormulaController } from './controller/formula.controller';
Expand Down Expand Up @@ -78,22 +78,28 @@ export class UniverFormulaEnginePlugin extends Plugin {
}

override onReady(): void {
this._injector.get(FormulaController);
this._injector.get(SetDefinedNameController);
this._injector.get(SetSuperTableController);
touchDependencies(this._injector, [
[FormulaController],
[SetDefinedNameController],
[SetSuperTableController],
]);

if (!this._config?.notExecuteFormula) {
this._injector.get(SetOtherFormulaController);
this._injector.get(SetFeatureCalculationController);
this._injector.get(SetDependencyController);
this._injector.get(CalculateController);
touchDependencies(this._injector, [
[SetOtherFormulaController],
[SetFeatureCalculationController],
[SetDependencyController],
[CalculateController],
]);
}
}

override onRendered(): void {
if (!this._config?.notExecuteFormula) {
this._injector.get(ICalculateFormulaService);
this._injector.get(IFormulaDependencyGenerator);
touchDependencies(this._injector, [
[ICalculateFormulaService],
[IFormulaDependencyGenerator],
]);
}
}

Expand Down
13 changes: 6 additions & 7 deletions packages/sheets-formula-ui/src/sheets-formula-ui.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import type { Dependency } from '@univerjs/core';
import type { IUniverSheetsFormulaBaseConfig } from './controllers/config.schema';
import { DependentOn, IConfigService, Inject, Injector, Plugin, UniverInstanceType } from '@univerjs/core';
import { DependentOn, IConfigService, Inject, Injector, Plugin, touchDependencies, UniverInstanceType } from '@univerjs/core';
import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula';
import { IRenderManagerService } from '@univerjs/engine-render';
import { UniverSheetsFormulaPlugin } from '@univerjs/sheets-formula';
Expand Down Expand Up @@ -78,10 +78,6 @@ export class UniverSheetsFormulaUIPlugin extends Plugin {
dependencies.forEach((dependency) => j.add(dependency));
}

override onReady(): void {
this._injector.get(FormulaUIController);
}

override onRendered(): void {
([
[RefSelectionsRenderService],
Expand All @@ -90,8 +86,11 @@ export class UniverSheetsFormulaUIPlugin extends Plugin {
this.disposeWithMe(this._renderManagerService.registerRenderModule(UniverInstanceType.UNIVER_SHEET, dep));
});

this._injector.get(FormulaClipboardController);
this._injector.get(FormulaRenderManagerController);
touchDependencies(this._injector, [
[FormulaUIController], // FormulaProgressBar relies on TriggerCalculationController, but it is necessary to ensure that the formula calculation is done after rendered.
[FormulaClipboardController],
[FormulaRenderManagerController],
]);

const componentManager = this._injector.get(ComponentManager);

Expand Down
26 changes: 18 additions & 8 deletions packages/sheets-formula/src/sheets-formula.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type {
IUniverSheetsFormulaBaseConfig,
IUniverSheetsFormulaRemoteConfig,
} from './controllers/config.schema';
import { DependentOn, IConfigService, Inject, Injector, Plugin, UniverInstanceType } from '@univerjs/core';
import { DependentOn, IConfigService, Inject, Injector, isNodeEnv, Plugin, touchDependencies, UniverInstanceType } from '@univerjs/core';
import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula';

import { fromModule, IRPCChannelService, toModule } from '@univerjs/rpc';
Expand Down Expand Up @@ -117,15 +117,25 @@ export class UniverSheetsFormulaPlugin extends Plugin {
}

override onReady(): void {
this._injector.get(FormulaController);
this._injector.get(TriggerCalculationController);
this._injector.get(ActiveDirtyController);
this._injector.get(ArrayFormulaCellInterceptorController);
this._injector.get(UpdateFormulaController);
this._injector.get(UpdateDefinedNameController);
touchDependencies(this._injector, [
[FormulaController],
[ActiveDirtyController],
[ArrayFormulaCellInterceptorController],
[UpdateFormulaController],
[UpdateDefinedNameController],
]);

// The nodejs environment needs to be initialized, otherwise it has been initialized in FormulaUIController
if (isNodeEnv()) {
touchDependencies(this._injector, [
[TriggerCalculationController],
]);
}
}

override onRendered(): void {
this._injector.get(DefinedNameController);
touchDependencies(this._injector, [
[DefinedNameController],
]);
}
}

0 comments on commit 0afcc5d

Please sign in to comment.