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
fix: fix uni formula collab (#2951)
Wenzhao Hu5b754694b642
diff --git a/packages-experimental/uni-formula-ui/src/services/uni-formula.service.ts b/packages-experimental/uni-formula-ui/src/services/uni-formula.service.tsindex 7de8ce41df..582afc4d34 100644--- a/packages-experimental/uni-formula-ui/src/services/uni-formula.service.ts+++ b/packages-experimental/uni-formula-ui/src/services/uni-formula.service.ts@@ -15,33 +15,105 @@*/import type {+ DocumentDataModel,ICellData,+ ICommand,IDisposable,- Nullable } from '@univerjs/core';+ IDocumentBody,+ Nullable,+} from '@univerjs/core';import {+ CommandType,+ CustomRangeType,ICommandService,Inject,IResourceManagerService,IUniverInstanceService,LifecycleStages,+ makeCustomRangeStream,OnLifecycle,Optional,RCDisposable,toDisposable,+ UniverInstanceType,} from '@univerjs/core';+import { makeSelection, replaceSelectionFactory } from '@univerjs/docs';import { DataSyncPrimaryController } from '@univerjs/rpc';import { RegisterOtherFormulaService } from '@univerjs/sheets-formula';-import type {- IDocFormulaCache,- IDocFormulaReference,- IUniFormulaService,- IUpdateDocUniFormulaCacheMutationParams } from '@univerjs/uni-formula';-import {- DumbUniFormulaService,- UpdateDocUniFormulaCacheMutation,-} from '@univerjs/uni-formula';+import type { IDocFormulaCache } from '@univerjs/uni-formula';+import { DumbUniFormulaService, IUniFormulaService } from '@univerjs/uni-formula';const DOC_PSEUDO_SUBUNIT = 'DOC_PSEUDO_SUBUNIT';+/**+ * Update calculating result in a batch.+ */+export interface IUpdateDocUniFormulaCacheCommandParams {+ /** The doc in which formula results changed. */+ unitId: string;+ /** Range ids. */+ ids: string[];+ /** Calculation results. */+ cache: IDocFormulaCache[];+}++/**+ * This command is internal. It should not be exposed to third-party developers.+ *+ * @ignore+ */+export const UpdateDocUniFormulaCacheCommand: ICommand<IUpdateDocUniFormulaCacheCommandParams> = {+ type: CommandType.COMMAND,+ id: 'doc.mutation.update-doc-uni-formula-cache',+ handler(accessor, params: IUpdateDocUniFormulaCacheCommandParams) {+ const { unitId, ids, cache } = params;++ const uniFormulaService = accessor.get(IUniFormulaService);+ const instanceService = accessor.get(IUniverInstanceService);+ const commandService = accessor.get(ICommandService);++ const data = instanceService.getUnit<DocumentDataModel>(unitId, UniverInstanceType.UNIVER_DOC);++ /** The document may have not loaded on this client. We are safe to ignore cache updating. */+ if (!data) return true;+ const body = data.getBody();++ function getRange(rangeId: string) {+ return body?.customRanges?.find((r) => r.rangeId === rangeId);+ }++ const saveCacheResult = uniFormulaService.updateFormulaResults(unitId, ids, cache);+ if (!saveCacheResult) return false;++ return ids.every((id, index) => {+ const range = getRange(id);+ if (!range) return true; // If we cannot find that rangeId, we are save to ignore cache.++ const dataStream = makeCustomRangeStream(`${cache[index].v ?? ''}`);+ const body: IDocumentBody = {+ dataStream,+ customRanges: [{+ startIndex: 0,+ endIndex: dataStream.length - 1,+ rangeId: id,+ rangeType: CustomRangeType.UNI_FORMULA,+ wholeEntity: true,+ }],+ };++ const redoMutation = replaceSelectionFactory(accessor, {+ unitId,+ body,+ selection: makeSelection(range.startIndex, range.endIndex),+ });++ if (redoMutation) {+ return commandService.syncExecuteCommand(redoMutation.id, redoMutation.params, { onlyLocal: true });+ }++ return false;+ });+ },+};/*** This service provides methods for docs and slides to register a formula into Univer's formula system.@@ -62,10 +134,6 @@ export class UniFormulaService extends DumbUniFormulaService implements IUniFormsuper(resourceManagerService, _commandSrv, _instanceSrv);}- getFormulaWithRangeId(unitId: string, rangeId: string): Nullable<IDocFormulaReference> {- return this._docFormulas.get(getDocFormulaKey(unitId, rangeId)) ?? null;- }-/*** Register a doc formula into the formula system.*/@@ -102,23 +170,6 @@ export class UniFormulaService extends DumbUniFormulaService implements IUniFormthis._formulaIdToKey.delete(item.formulaId);}- hasFocFormula(unitId: string, formulaId: string): boolean {- return this._docFormulas.has(getDocFormulaKey(unitId, formulaId));- }-- updateFormulaResults(unitId: string, formulaIds: string[], v: IDocFormulaCache[]): boolean {- formulaIds.forEach((id, index) => {- const formulaData = this._docFormulas.get(getDocFormulaKey(unitId, id));- if (!formulaData) return true;-- formulaData.v = v[index].v;- formulaData.t = v[index].t;- return true;- });-- return true;- }-private _dataSyncDisposables = new Map<string, RCDisposable>();private _checkSyncingUnit(unitId: string): void {if (!this._dataSyncPrimaryController) return;@@ -167,7 +218,7 @@ export class UniFormulaService extends DumbUniFormulaService implements IUniFormif (mutationParam.ids.length === 0) return;- this._commandSrv.executeCommand(UpdateDocUniFormulaCacheMutation.id, mutationParam as IUpdateDocUniFormulaCacheMutationParams);+ this._commandSrv.executeCommand(UpdateDocUniFormulaCacheCommand.id, mutationParam as IUpdateDocUniFormulaCacheCommandParams);}}}));diff --git a/packages-experimental/uni-formula/src/index.ts b/packages-experimental/uni-formula/src/index.tsindex 57975245ed..544e9043e3 100644--- a/packages-experimental/uni-formula/src/index.ts+++ b/packages-experimental/uni-formula/src/index.ts@@ -15,7 +15,7 @@*/export { UniverDocUniFormulaPlugin } from './uni-formula.plugin';-export { IUniFormulaService, DumbUniFormulaService, UpdateDocUniFormulaCacheMutation, type IUpdateDocUniFormulaCacheMutationParams } from './services/uni-formula.service';+export { IUniFormulaService, DumbUniFormulaService } from './services/uni-formula.service';export { DOC_FORMULA_PLUGIN_NAME } from './const';export { type IDocFormulaCache, type IDocFormulaData, type IDocFormulaReference } from './models/doc-formula';diff --git a/packages-experimental/uni-formula/src/services/uni-formula.service.ts b/packages-experimental/uni-formula/src/services/uni-formula.service.tsindex 4a21ccda06..05b85011ef 100644--- a/packages-experimental/uni-formula/src/services/uni-formula.service.ts+++ b/packages-experimental/uni-formula/src/services/uni-formula.service.ts@@ -14,96 +14,20 @@* limitations under the License.*/-import type { DocumentDataModel, ICellData, IDisposable, IDocumentBody, IMutation, Nullable } from '@univerjs/core';+import type { ICellData, IDisposable, Nullable } from '@univerjs/core';import {- CommandType,createIdentifier,- CustomRangeType,ICommandService,IResourceManagerService,IUniverInstanceService,- makeCustomRangeStream,toDisposable,UniverInstanceType,} from '@univerjs/core';-import { makeSelection, replaceSelectionFactory } from '@univerjs/docs';import { type IDocFormulaCache, type IDocFormulaData, type IDocFormulaReference, toJson } from '../models/doc-formula';import { DOC_FORMULA_PLUGIN_NAME } from '../const';import { AddDocUniFormulaMutation, RemoveDocUniFormulaMutation, UpdateDocUniFormulaMutation } from '../commands/mutation';-/**- * Update calculating result in a batch.- */-export interface IUpdateDocUniFormulaCacheMutationParams {- /** The doc in which formula results changed. */- unitId: string;- /** Range ids. */- ids: string[];- /** Calculation results. */- cache: IDocFormulaCache[];-}--/**- * 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) {- const { unitId, ids, cache } = params;-- const uniFormulaService = accessor.get(IUniFormulaService);- const instanceService = accessor.get(IUniverInstanceService);- const commandService = accessor.get(ICommandService);-- const data = instanceService.getUnit<DocumentDataModel>(unitId, UniverInstanceType.UNIVER_DOC);-- /** The document may have not loaded on this client. We are safe to ignore cache updating. */- if (!data) return true;- const body = data.getBody();-- function getRange(rangeId: string) {- return body?.customRanges?.find((r) => r.rangeId === rangeId);- }-- const saveCacheResult = uniFormulaService.updateFormulaResults(unitId, ids, cache);- if (!saveCacheResult) return false;-- return ids.every((id, index) => {- const range = getRange(id);- if (!range) return true; // If we cannot find that rangeId, we are save to ignore cache.-- const dataStream = makeCustomRangeStream(`${cache[index].v ?? ''}`);- const body: IDocumentBody = {- dataStream,- customRanges: [{- startIndex: 0,- endIndex: dataStream.length - 1,- rangeId: id,- rangeType: CustomRangeType.UNI_FORMULA,- wholeEntity: true,- }],- };-- const redoMutation = replaceSelectionFactory(accessor, {- unitId,- body,- selection: makeSelection(range.startIndex, range.endIndex),- });-- if (redoMutation) {- return commandService.syncExecuteCommand(redoMutation.id, redoMutation.params, { onlyLocal: true });- }-- return false;- });- },-};-export interface IUniFormulaService {getFormulaWithRangeId(unitId: string, rangeId: string): Nullable<IDocFormulaReference>;registerDocFormula(unitId: string, rangeId: string, f: string, v: ICellData['v'], t: ICellData['t']): IDisposable;@@ -115,7 +39,7 @@ export interface IUniFormulaService {export const IUniFormulaService = createIdentifier<IUniFormulaService>('uni-formula.uni-formula.service');-export class DumbUniFormulaService {+export class DumbUniFormulaService implements IUniFormulaService {/** This data maps doc formula key to the formula id in the formula system. */protected readonly _docFormulas = new Map<string, IDocFormulaReference>();@@ -132,11 +56,31 @@ export class DumbUniFormulaService {});}+ hasFocFormula(unitId: string, formulaId: string): boolean {+ return this._docFormulas.has(getDocFormulaKey(unitId, formulaId));+ }++ getFormulaWithRangeId(unitId: string, rangeId: string): Nullable<IDocFormulaReference> {+ return this._docFormulas.get(getDocFormulaKey(unitId, rangeId)) ?? null;+ }++ updateFormulaResults(unitId: string, formulaIds: string[], v: IDocFormulaCache[]): boolean {+ formulaIds.forEach((id, index) => {+ const formulaData = this._docFormulas.get(getDocFormulaKey(unitId, id));+ if (!formulaData) return true;++ formulaData.v = v[index].v;+ formulaData.t = v[index].t;+ return true;+ });++ return true;+ }+private _initCommands(): void {[AddDocUniFormulaMutation,UpdateDocUniFormulaMutation,- UpdateDocUniFormulaCacheMutation,RemoveDocUniFormulaMutation,].forEach((command) => this._commandSrv.registerCommand(command));}diff --git a/packages-experimental/uni-formula/src/uni-formula.plugin.ts b/packages-experimental/uni-formula/src/uni-formula.plugin.tsindex 360c038351..01cfb1eab0 100644--- a/packages-experimental/uni-formula/src/uni-formula.plugin.ts+++ b/packages-experimental/uni-formula/src/uni-formula.plugin.ts@@ -23,7 +23,7 @@ export class UniverDocUniFormulaPlugin extends Plugin {static override pluginName: string = DOC_FORMULA_PLUGIN_NAME;// This plugin should load only when sheet related modules are loaded.- static override type: UniverInstanceType = UniverInstanceType.UNIVER_SHEET;+ static override type: UniverInstanceType = UniverInstanceType.UNIVER_UNKNOWN;constructor(private readonly _config: { playDumb: boolean } | undefined,@@ -35,11 +35,6 @@ export class UniverDocUniFormulaPlugin extends Plugin {override onStarting(): void {if (this._config?.playDumb) {this._injector.add([IUniFormulaService, { useClass: DumbUniFormulaService }] as Dependency);- }- }-- override onReady(): void {- if (this._config?.playDumb) {this._injector.get(IUniFormulaService);}}