Skip to content

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.

fix(uni): uni formula service should run on server without formula (#2946)

Wenzhao Hu2aa0d840a5dd
diff --git a/packages-experimental/uni-formula-ui/package.json b/packages-experimental/uni-formula-ui/package.json
index c88212c209..a17b2552f8 100644
--- a/packages-experimental/uni-formula-ui/package.json
+++ b/packages-experimental/uni-formula-ui/package.json
@@ -62,6 +62,7 @@
"@univerjs/core": "workspace:*",
"@univerjs/docs": "workspace:*",
"@univerjs/docs-ui": "workspace:*",
+ "@univerjs/icons": "^0.1.72",
"@univerjs/sheets-formula": "workspace:*",
"@univerjs/ui": "workspace:*",
"@univerjs/uni-formula": "workspace:*",
@@ -74,6 +75,7 @@
"@univerjs/design": "workspace:*",
"@univerjs/docs": "workspace:*",
"@univerjs/docs-ui": "workspace:*",
+ "@univerjs/icons": "^0.1.72",
"@univerjs/shared": "workspace:*",
"@univerjs/sheets-formula": "workspace:*",
"@univerjs/ui": "workspace:*",
diff --git a/packages-experimental/uni-formula-ui/src/commands/command.ts b/packages-experimental/uni-formula-ui/src/commands/command.ts
index f7fc0a908a..4f9746e6e1 100644
--- a/packages-experimental/uni-formula-ui/src/commands/command.ts
+++ b/packages-experimental/uni-formula-ui/src/commands/command.ts
@@ -49,19 +49,19 @@ export const AddDocUniFormulaCommand: ICommand<IAddDocUniFormulaCommandParams> =
}],
};
- const redoMutation = replaceSelectionFactory(accessor, {
+ const insertCustomRangeMutation = replaceSelectionFactory(accessor, {
unitId,
body,
selection: makeSelection(startIndex, startIndex + 1),
});
- if (redoMutation) {
+ if (insertCustomRangeMutation) {
const addFormulaResourceMutation: IMutationInfo<IAddDocUniFormulaMutationParams> = {
id: AddDocUniFormulaMutation.id,
params: { unitId, rangeId, f },
};
- return sequenceExecute([addFormulaResourceMutation, redoMutation], commandService).result;
+ return sequenceExecute([insertCustomRangeMutation, addFormulaResourceMutation], commandService).result;
}
return false;
diff --git a/packages-experimental/uni-formula/package.json b/packages-experimental/uni-formula/package.json
index 6002d0812d..ca7e9a9c9c 100644
--- a/packages-experimental/uni-formula/package.json
+++ b/packages-experimental/uni-formula/package.json
@@ -62,7 +62,6 @@
"peerDependencies": {
"@univerjs/core": "workspace:*",
"@univerjs/docs": "workspace:*",
- "@univerjs/engine-formula": "workspace:*",
"@univerjs/rpc": "workspace:*",
"@univerjs/sheets-formula": "workspace:*"
},
@@ -71,9 +70,7 @@
},
"devDependencies": {
"@univerjs/core": "workspace:*",
- "@univerjs/design": "workspace:*",
"@univerjs/docs": "workspace:*",
- "@univerjs/engine-formula": "workspace:*",
"@univerjs/rpc": "workspace:*",
"@univerjs/shared": "workspace:*",
"@univerjs/sheets-formula": "workspace:*",
diff --git a/packages-experimental/uni-formula/src/commands/mutation.ts b/packages-experimental/uni-formula/src/commands/mutation.ts
index 0034edfd06..2678fc9546 100644
--- a/packages-experimental/uni-formula/src/commands/mutation.ts
+++ b/packages-experimental/uni-formula/src/commands/mutation.ts
@@ -50,9 +50,7 @@ export const UpdateDocUniFormulaMutation: IMutation<IUpdateDocUniFormulaMutation
const { unitId, f, rangeId: id } = params;
const uniFormulaService = accessor.get(UniFormulaService);
- if (!uniFormulaService.hasFocFormula(unitId, id)) {
- return false;
- }
+ if (!uniFormulaService.hasFocFormula(unitId, id)) return false;
uniFormulaService.unregisterDocFormula(unitId, id);
uniFormulaService.registerDocFormula(unitId, id, f);
diff --git a/packages-experimental/uni-formula/src/services/uni-formula.service.ts b/packages-experimental/uni-formula/src/services/uni-formula.service.ts
index 35c4a58318..b782ee5747 100644
--- a/packages-experimental/uni-formula/src/services/uni-formula.service.ts
+++ b/packages-experimental/uni-formula/src/services/uni-formula.service.ts
@@ -22,7 +22,6 @@ import {
CustomRangeType,
Disposable,
ICommandService,
- Inject,
IResourceManagerService,
IUniverInstanceService,
LifecycleStages,
@@ -53,8 +52,13 @@ export interface IUpdateDocUniFormulaCacheMutationParams {
cache: IDocFormulaCache[];
}
-/** This mutation is internal. It should not be exposed to third-party developers. */
-const UpdateDocUniFormulaCacheMutation: IMutation<IUpdateDocUniFormulaCacheMutationParams> = {
+/**
+ * This mutation is internal. It should not be exposed to third-party developers.
+ * This mutation should be registered on the server side as well.
+ *
+ * @ignore
+ */
+export const UpdateDocUniFormulaCacheMutation: IMutation<IUpdateDocUniFormulaCacheMutationParams> = {
type: CommandType.MUTATION,
id: 'doc.mutation.update-doc-uni-formula-cache',
handler(accessor, params: IUpdateDocUniFormulaCacheMutationParams) {
@@ -109,8 +113,9 @@ const UpdateDocUniFormulaCacheMutation: IMutation<IUpdateDocUniFormulaCacheMutat
};
/**
- * This service provides methods for docs and slides to register a formula into
- * Univer's formula system.
+ * This service provides methods for docs and slides to register a formula into Univer's formula system.
+ * And it also manages formula resources fields of docs and slides. `SHEETS_FORMULA_REMOTE_PLUGIN`
+ * is not required but optional here.
*/
@OnLifecycle(LifecycleStages.Steady, UniFormulaService)
export class UniFormulaService extends Disposable {
@@ -120,23 +125,19 @@ export class UniFormulaService extends Disposable {
constructor(
@IResourceManagerService resourceManagerService: IResourceManagerService,
- // TODO: wzhudev We may need to register the doc formulas to formula service in later lifecycle stages.
@ICommandService private readonly _commandSrv: ICommandService,
@IUniverInstanceService private readonly _instanceSrv: IUniverInstanceService,
- @Inject(RegisterOtherFormulaService) private readonly _registerOtherFormulaSrv: RegisterOtherFormulaService,
+ @Optional(RegisterOtherFormulaService) private readonly _registerOtherFormulaSrv?: RegisterOtherFormulaService,
@Optional(DataSyncPrimaryController) private readonly _dataSyncPrimaryController?: DataSyncPrimaryController
) {
super();
this._initCommands();
this._initDocFormulaResources(resourceManagerService);
- this._initDocDisposingListener();
- }
-
- override dispose(): void {
- super.dispose();
- // TODO: wzhudev: concrete disposing methods
+ this._instanceSrv.getTypeOfUnitDisposed$(UniverInstanceType.UNIVER_DOC).subscribe((doc) => {
+ this._unregisterDoc(doc.getUnitId());
+ });
}
private _initCommands(): void {
@@ -171,13 +172,17 @@ export class UniFormulaService extends Disposable {
throw new Error(`[UniFormulaService]: cannot register formula ${key} when it is already registered!`);
}
- const pseudoId = getPseudoUnitKey(unitId);
- this._checkResultSubscription();
- this._checkSyncingUnit(pseudoId);
+ if (this._registerOtherFormulaSrv) {
+ const pseudoId = getPseudoUnitKey(unitId);
+ this._checkSyncingUnit(pseudoId);
+ this._checkResultSubscription();
- const id = this._registerOtherFormulaSrv.registerFormula(pseudoId, DOC_PSEUDO_SUBUNIT, f);
- this._docFormulas.set(key, { unitId, rangeId, f, formulaId: id, v, t });
- this._formulaIdToKey.set(id, key);
+ const id = this._registerOtherFormulaSrv.registerFormula(pseudoId, DOC_PSEUDO_SUBUNIT, f);
+ this._docFormulas.set(key, { unitId, rangeId, f, formulaId: id, v, t });
+ this._formulaIdToKey.set(id, key);
+ } else {
+ this._docFormulas.set(key, { unitId, rangeId, f, formulaId: '', v, t });
+ }
return toDisposable(() => this.unregisterDocFormula(unitId, rangeId));
}
@@ -193,9 +198,11 @@ export class UniFormulaService extends Disposable {
this._checkDisposingResultSubscription();
this._dataSyncDisposables.get(pseudoId)?.dec();
- this._registerOtherFormulaSrv.deleteFormula(pseudoId, DOC_PSEUDO_SUBUNIT, [item.formulaId]);
- this._docFormulas.delete(key);
- this._formulaIdToKey.delete(item.formulaId);
+ if (this._registerOtherFormulaSrv) {
+ this._registerOtherFormulaSrv.deleteFormula(pseudoId, DOC_PSEUDO_SUBUNIT, [item.formulaId]);
+ this._docFormulas.delete(key);
+ this._formulaIdToKey.delete(item.formulaId);
+ }
}
hasFocFormula(unitId: string, formulaId: string): boolean {
@@ -229,7 +236,7 @@ export class UniFormulaService extends Disposable {
private _resultSubscription: Nullable<IDisposable>;
private _checkResultSubscription(): void {
- if (this._resultSubscription) return;
+ if (this._resultSubscription || !this._registerOtherFormulaSrv) return;
this._resultSubscription = toDisposable(this._registerOtherFormulaSrv.formulaResult$.subscribe((resultMap) => {
for (const resultOfUnit in resultMap) {
@@ -270,7 +277,11 @@ export class UniFormulaService extends Disposable {
}
private _checkDisposingResultSubscription(): void {
- if (this._docFormulas.size === 0 && this._resultSubscription) {
+ if (this._docFormulas.size === 0) this._disposeResultSubscription();
+ }
+
+ private _disposeResultSubscription(): void {
+ if (this._resultSubscription) {
this._resultSubscription.dispose();
this._resultSubscription = null;
}
@@ -289,7 +300,6 @@ export class UniFormulaService extends Disposable {
return formulas;
},
onLoad: (unitId, formulas) => {
- // NOTE: Should wait for lifecycle?
formulas.forEach((f) => this.registerDocFormula(unitId, f.rangeId, f.f, f.v, f.t));
},
onUnLoad: (unitId) => {
@@ -298,12 +308,6 @@ export class UniFormulaService extends Disposable {
});
}
- private _initDocDisposingListener(): void {
- this._instanceSrv.getTypeOfUnitDisposed$(UniverInstanceType.UNIVER_DOC).subscribe((doc) => {
- this._unregisterDoc(doc.getUnitId());
- });
- }
-
private _getAllFormulasOfUnit(unitId: string) {
const formulas = Array.from(this._docFormulas.entries()).filter((v) => v[1].unitId === unitId);
return formulas;
@@ -317,4 +321,3 @@ function getPseudoUnitKey(unitId: string): string {
function getDocFormulaKey(unitId: string, formulaId: string): string {
return `pseudo-${unitId}-${formulaId}`;
}
-
diff --git a/packages-experimental/uni-formula/src/uni-formula.plugin.ts b/packages-experimental/uni-formula/src/uni-formula.plugin.ts
index 221fa8661e..07b2ec1da0 100644
--- a/packages-experimental/uni-formula/src/uni-formula.plugin.ts
+++ b/packages-experimental/uni-formula/src/uni-formula.plugin.ts
@@ -14,13 +14,10 @@
* limitations under the License.
*/
-import { DependentOn, Inject, Injector, Plugin, UniverInstanceType } from '@univerjs/core';
-import { UniverFormulaEnginePlugin } from '@univerjs/engine-formula';
-import { UniverSheetsFormulaPlugin } from '@univerjs/sheets-formula';
+import { Inject, Injector, Plugin, UniverInstanceType } from '@univerjs/core';
import { UniFormulaService } from './services/uni-formula.service';
import { DOC_FORMULA_PLUGIN_NAME } from './const';
-@DependentOn(UniverFormulaEnginePlugin, UniverSheetsFormulaPlugin)
export class UniverDocUniFormulaPlugin extends Plugin {
static override pluginName: string = DOC_FORMULA_PLUGIN_NAME;
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 211f28f3b3..0abcf02a76 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -456,15 +456,9 @@ importers:
'@univerjs/core':
specifier: workspace:*
version: link:../../packages/core
- '@univerjs/design':
- specifier: workspace:*
- version: link:../../packages/design
'@univerjs/docs':
specifier: workspace:*
version: link:../../packages/docs
- '@univerjs/engine-formula':
- specifier: workspace:*
- version: link:../../packages/engine-formula
'@univerjs/rpc':
specifier: workspace:*
version: link:../../packages/rpc
@@ -657,9 +651,6 @@ importers:
'@floating-ui/react-dom':
specifier: ^2.1.1
- '@univerjs/docs':
- specifier: workspace:*
- version: link:../../packages/docs
'@univerjs/icons':
specifier: ^0.1.72