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: fix uni mode errors (#2959)

Wenzhao Hude57b8e2f58d
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.ts
index 582afc4d34..6c118314b7 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
@@ -27,12 +27,11 @@ import {
CustomRangeType,
ICommandService,
Inject,
+ Injector,
IResourceManagerService,
IUniverInstanceService,
- LifecycleStages,
makeCustomRangeStream,
- OnLifecycle,
- Optional,
+ Quantity,
RCDisposable,
toDisposable,
UniverInstanceType,
@@ -42,6 +41,7 @@ import { DataSyncPrimaryController } from '@univerjs/rpc';
import { RegisterOtherFormulaService } from '@univerjs/sheets-formula';
import type { IDocFormulaCache } from '@univerjs/uni-formula';
import { DumbUniFormulaService, IUniFormulaService } from '@univerjs/uni-formula';
+import { take } from 'rxjs';
const DOC_PSEUDO_SUBUNIT = 'DOC_PSEUDO_SUBUNIT';
/**
@@ -107,7 +107,7 @@ export const UpdateDocUniFormulaCacheCommand: ICommand<IUpdateDocUniFormulaCache
});
if (redoMutation) {
- return commandService.syncExecuteCommand(redoMutation.id, redoMutation.params, { onlyLocal: true });
+ return commandService.syncExecuteCommand(redoMutation.id, redoMutation.params);
}
return false;
@@ -120,18 +120,35 @@ export const UpdateDocUniFormulaCacheCommand: ICommand<IUpdateDocUniFormulaCache
* 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 DumbUniFormulaService implements IUniFormulaService {
private readonly _formulaIdToKey = new Map<string, string>();
+ private _canPerformFormulaCalculation = false;
+
+ private get _registerOtherFormulaSrv() { return this._injector.get(RegisterOtherFormulaService); }
+ private get _dataSyncPrimaryController() { return this._injector.get(DataSyncPrimaryController, Quantity.OPTIONAL); }
+
constructor(
- @IResourceManagerService resourceManagerService: IResourceManagerService,
- @ICommandService _commandSrv: ICommandService,
- @IUniverInstanceService _instanceSrv: IUniverInstanceService,
- @Inject(RegisterOtherFormulaService) private readonly _registerOtherFormulaSrv: RegisterOtherFormulaService,
- @Optional(DataSyncPrimaryController) private readonly _dataSyncPrimaryController?: DataSyncPrimaryController
+ @Inject(Injector) private readonly _injector: Injector,
+ @IResourceManagerService resourceManagerService: IResourceManagerService,
+ @ICommandService commandSrv: ICommandService,
+ @IUniverInstanceService instanceSrv: IUniverInstanceService
) {
- super(resourceManagerService, _commandSrv, _instanceSrv);
+ super(resourceManagerService, commandSrv, instanceSrv);
+
+ commandSrv.registerCommand(UpdateDocUniFormulaCacheCommand);
+
+ // Only able to perform formula calculation after a sheet is loaded.
+ // FIXME: the formula engine is not unit-agnostic.
+ if (instanceSrv.getAllUnitsForType(UniverInstanceType.UNIVER_SHEET).length) {
+ this._canPerformFormulaCalculation = true;
+ } else {
+ this.disposeWithMe(this._instanceSrv.getTypeOfUnitAdded$(UniverInstanceType.UNIVER_SHEET).pipe(take(1))
+ .subscribe(() => {
+ this._canPerformFormulaCalculation = true;
+ this._initFormulaRegistration();
+ }));
+ }
}
/**
@@ -143,13 +160,18 @@ export class UniFormulaService extends DumbUniFormulaService implements IUniForm
throw new Error(`[UniFormulaService]: cannot register formula ${key} when it is already registered!`);
}
- const pseudoId = getPseudoUnitKey(unitId);
- this._checkSyncingUnit(pseudoId);
- this._checkResultSubscription();
+ if (this._canPerformFormulaCalculation) {
+ const pseudoId = getPseudoUnitKey(unitId);
+ this._checkSyncingUnit(pseudoId);
- 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);
+
+ this._checkResultSubscription();
+ } else {
+ this._docFormulas.set(key, { unitId, rangeId, f, formulaId: '', v, t });
+ }
return toDisposable(() => this.unregisterDocFormula(unitId, rangeId));
}
@@ -157,17 +179,34 @@ export class UniFormulaService extends DumbUniFormulaService implements IUniForm
override unregisterDocFormula(unitId: string, rangeId: string): void {
const key = getDocFormulaKey(unitId, rangeId);
const item = this._docFormulas.get(key);
- if (!item) {
- return;
- }
+ if (!item) return;
const pseudoId = getPseudoUnitKey(unitId);
this._checkDisposingResultSubscription();
this._dataSyncDisposables.get(pseudoId)?.dec();
- this._registerOtherFormulaSrv.deleteFormula(pseudoId, DOC_PSEUDO_SUBUNIT, [item.formulaId]);
+ if (this._canPerformFormulaCalculation) {
+ this._registerOtherFormulaSrv.deleteFormula(pseudoId, DOC_PSEUDO_SUBUNIT, [item.formulaId]);
+ this._formulaIdToKey.delete(item.formulaId);
+ }
+
this._docFormulas.delete(key);
- this._formulaIdToKey.delete(item.formulaId);
+ }
+
+ private _initFormulaRegistration(): void {
+ // When the doc bootstraps, there could be no sheets modules loaded. So we need to check if there
+ // are registered formulas but not added to the formula system.
+ this._docFormulas.forEach((value, key) => {
+ if (!value.formulaId) {
+ const { unitId, f } = value;
+ const pseudoId = getPseudoUnitKey(unitId);
+ this._checkSyncingUnit(pseudoId);
+
+ const id = this._registerOtherFormulaSrv.registerFormula(pseudoId, DOC_PSEUDO_SUBUNIT, f);
+ value.formulaId = id;
+ this._formulaIdToKey.set(id, key);
+ }
+ });
}
private _dataSyncDisposables = new Map<string, RCDisposable>();
@@ -234,6 +273,12 @@ export class UniFormulaService extends DumbUniFormulaService implements IUniForm
this._resultSubscription = null;
}
}
+
+ private _checkFormulaUsable(): void {
+ if (!this._canPerformFormulaCalculation && this._instanceSrv.getAllUnitsForType(UniverInstanceType.UNIVER_SHEET).length) {
+ this._canPerformFormulaCalculation = true;
+ }
+ }
}
function getPseudoUnitKey(unitId: string): string {
diff --git a/packages-experimental/uni-formula-ui/src/uni-formula-ui.plugin.ts b/packages-experimental/uni-formula-ui/src/uni-formula-ui.plugin.ts
index de26e0cf72..9ed4172d83 100644
--- a/packages-experimental/uni-formula-ui/src/uni-formula-ui.plugin.ts
+++ b/packages-experimental/uni-formula-ui/src/uni-formula-ui.plugin.ts
@@ -26,7 +26,7 @@ import { UniFormulaService } from './services/uni-formula.service';
@DependentOn(UniverDocUniFormulaPlugin)
export class UniverDocUniFormulaUIPlugin extends Plugin {
static override pluginName: string = DOC_FORMULA_UI_PLUGIN_NAME;
- static override type: UniverInstanceType = UniverInstanceType.UNIVER_SHEET;
+ static override type: UniverInstanceType = UniverInstanceType.UNIVER_UNKNOWN;
constructor(
_config: unknown,
@@ -42,4 +42,8 @@ export class UniverDocUniFormulaUIPlugin extends Plugin {
[IUniFormulaService, { useClass: UniFormulaService }],
] as Dependency[]).forEach((d) => injector.add(d));
}
+
+ override onReady(): void {
+ this._injector.get(IUniFormulaService);
+ }
}
diff --git a/packages-experimental/uni-formula/package.json b/packages-experimental/uni-formula/package.json
index 5013503345..5c7c02edd3 100644
--- a/packages-experimental/uni-formula/package.json
+++ b/packages-experimental/uni-formula/package.json
@@ -60,8 +60,7 @@
"build": "tsc && vite build"
},
"peerDependencies": {
- "@univerjs/core": "workspace:*",
- "@univerjs/docs": "workspace:*"
+ "@univerjs/core": "workspace:*"
},
"dependencies": {
"@univerjs/icons": "^0.1.72"
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 ba67573152..2b4cd88f50 100644
--- a/packages-experimental/uni-formula/src/services/uni-formula.service.ts
+++ b/packages-experimental/uni-formula/src/services/uni-formula.service.ts
@@ -17,6 +17,7 @@
import type { ICellData, IDisposable, Nullable } from '@univerjs/core';
import {
createIdentifier,
+ Disposable,
ICommandService,
IResourceManagerService,
IUniverInstanceService,
@@ -38,7 +39,7 @@ export interface IUniFormulaService {
export const IUniFormulaService = createIdentifier<IUniFormulaService>('uni-formula.uni-formula.service');
-export class DumbUniFormulaService implements IUniFormulaService {
+export class DumbUniFormulaService extends Disposable implements IUniFormulaService {
/** This data maps doc formula key to the formula id in the formula system. */
protected readonly _docFormulas = new Map<string, IDocFormulaReference>();
@@ -47,6 +48,8 @@ export class DumbUniFormulaService implements IUniFormulaService {
@ICommandService protected readonly _commandSrv: ICommandService,
@IUniverInstanceService protected readonly _instanceSrv: IUniverInstanceService
) {
+ super();
+
this._initDocFormulaResources(resourceManagerService);
this._instanceSrv.getTypeOfUnitDisposed$(UniverInstanceType.UNIVER_DOC).subscribe((doc) => {
diff --git a/packages/docs-ui/src/controllers/menu/context-menu.ts b/packages/docs-ui/src/controllers/menu/context-menu.ts
index 1d9f4884c4..af25dad125 100644
--- a/packages/docs-ui/src/controllers/menu/context-menu.ts
+++ b/packages/docs-ui/src/controllers/menu/context-menu.ts
@@ -87,7 +87,8 @@ const getDisableWhenSelectionNotInTableObservable = (accessor: IAccessor) => {
export const CopyMenuFactory = (accessor: IAccessor): IMenuButtonItem => {
return {
- id: DocCopyCommand.id,
+ id: DocCopyCommand.name,
+ commandId: DocCopyCommand.id,
group: MenuGroup.CONTEXT_MENU_FORMAT,
type: MenuItemType.BUTTON,
icon: 'Copy',
diff --git a/packages/sheets-ui/src/controllers/menu/menu.ts b/packages/sheets-ui/src/controllers/menu/menu.ts
index 300cc00719..f900b40e1f 100644
--- a/packages/sheets-ui/src/controllers/menu/menu.ts
+++ b/packages/sheets-ui/src/controllers/menu/menu.ts
@@ -70,7 +70,6 @@ import {
import type { IMenuButtonItem, IMenuSelectorItem } from '@univerjs/ui';
import {
- CopyCommand,
CutCommand,
FONT_FAMILY_LIST,
FONT_SIZE_LIST,
@@ -79,14 +78,16 @@ import {
MenuGroup,
MenuItemType,
MenuPosition,
- PasteCommand,
} from '@univerjs/ui';
import type { IAccessor } from '@univerjs/core';
import { combineLatestWith, map, Observable } from 'rxjs';
import {
+ SheetCopyCommand,
+ SheetCutCommand,
SheetPasteBesidesBorderCommand,
SheetPasteColWidthCommand,
+ SheetPasteCommand,
SheetPasteFormatCommand,
SheetPasteValueCommand,
} from '../../commands/commands/clipboard.command';
@@ -901,7 +902,8 @@ function menuClipboardDisabledObservable(injector: IAccessor): Observable<boolea
export function CopyMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
return {
- id: CopyCommand.id,
+ id: SheetCopyCommand.name,
+ commandId: SheetCopyCommand.id,
group: MenuGroup.CONTEXT_MENU_FORMAT,
type: MenuItemType.BUTTON,
title: 'rightClick.copy',
@@ -918,7 +920,8 @@ export function CopyMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
export function CutMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
return {
- id: CutCommand.id,
+ id: SheetCutCommand.name,
+ commandId: CutCommand.id,
group: MenuGroup.CONTEXT_MENU_FORMAT,
type: MenuItemType.BUTTON,
title: 'contextMenu.cut',
@@ -934,7 +937,8 @@ export function CutMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
export function PasteMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
return {
- id: PasteCommand.id,
+ id: SheetPasteCommand.name,
+ commandId: SheetPasteCommand.id,
group: MenuGroup.CONTEXT_MENU_FORMAT,
type: MenuItemType.BUTTON,
title: 'rightClick.paste',
diff --git a/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/SheetBarTabs.tsx b/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/SheetBarTabs.tsx
index 10498854a3..8f944f64da 100644
--- a/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/SheetBarTabs.tsx
+++ b/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/SheetBarTabs.tsx
@@ -411,8 +411,8 @@ export function SheetBarTabs() {
<Menu
menuType={SheetMenuPosition.SHEET_BAR}
onOptionSelect={(params) => {
- const { label: commandId, value } = params;
- commandService.executeCommand(commandId as string, { value, subUnitId: activeKey });
+ const { label: id, value, commandId } = params;
+ commandService.executeCommand(commandId ?? id as string, { value, subUnitId: activeKey });
setVisible(false);
}}
/>
diff --git a/packages/sheets-zen-editor/src/plugin.ts b/packages/sheets-zen-editor/src/plugin.ts
index 041f6a02d3..ef68823faf 100644
--- a/packages/sheets-zen-editor/src/plugin.ts
+++ b/packages/sheets-zen-editor/src/plugin.ts
@@ -24,7 +24,7 @@ import { IZenEditorManagerService, ZenEditorManagerService } from './services/ze
export class UniverSheetsZenEditorPlugin extends Plugin {
static override pluginName = 'zen-editor';
- static override type = UniverInstanceType.UNIVER_DOC;
+ static override type = UniverInstanceType.UNIVER_SHEET;
constructor(
private readonly _config: Partial<IUniverSheetsZenEditorUIConfig> = {},
diff --git a/packages/ui/src/components/menu/desktop/Menu.tsx b/packages/ui/src/components/menu/desktop/Menu.tsx
index e23c55620f..423ef82500 100644
--- a/packages/ui/src/components/menu/desktop/Menu.tsx
+++ b/packages/ui/src/components/menu/desktop/Menu.tsx
@@ -128,7 +128,7 @@ function MenuOptionsWrapper(props: IBaseMenuProps) {
const key = `${parentKey}-${option.label ?? option.id}-${index}`;
const onChange = (v: string | number) => {
- onOptionSelect?.({ value: v, label: option?.label });
+ onOptionSelect?.({ value: v, label: option?.label, commandId: option?.commandId });
};
const handleClick = () => {
@@ -232,7 +232,7 @@ function MenuItem({ menuItem, onClick }: IMenuItemProps) {
[styles.menuItemActivated]: activated,
})}
onClick={() => {
- onClick({ value: inputValue, id: item.id }); // merge cell
+ onClick({ commandId: item.commandId, value: inputValue, id: item.id });
}}
>
<span className={styles.menuItemContent}>
@@ -271,7 +271,7 @@ function MenuItem({ menuItem, onClick }: IMenuItemProps) {
menuType={item.id}
options={selections}
onOptionSelect={(v) => {
- onClick({ value: v.value, id: item.id });
+ onClick({ value: v.value, id: item.id, commandId: v.commandId });
}}
/>
)}
diff --git a/packages/ui/src/services/menu/menu.ts b/packages/ui/src/services/menu/menu.ts
index 88d9bd422c..2757f5cbed 100644
--- a/packages/ui/src/services/menu/menu.ts
+++ b/packages/ui/src/services/menu/menu.ts
@@ -59,6 +59,13 @@ export enum MenuItemType {
interface IMenuItemBase<V> {
/** ID of the menu item. Normally it should be the same as the ID of the command that it would invoke. */
id: string;
+
+ /**
+ * If two menus reuse the same command (e.g. copy & paste command). They should have the same command
+ * id and different ids.
+ */
+ commandId?: string;
+
subId?: string;
title?: string;
description?: string;
@@ -97,6 +104,7 @@ export interface IMenuButtonItem<V = undefined> extends IMenuItemBase<V> {
}
export interface IValueOption<T = undefined> {
+ id?: string;
value?: string | number;
value$?: Observable<T>;
label?:
@@ -110,7 +118,7 @@ export interface IValueOption<T = undefined> {
tooltip?: string;
style?: object;
disabled?: boolean;
- id?: string; // command id
+ commandId?: string;
}
export interface ICustomComponentProps<T> {
diff --git a/packages/ui/src/views/components/context-menu/ContextMenu.tsx b/packages/ui/src/views/components/context-menu/ContextMenu.tsx
index 3ad44cb8fe..9da0fc0806 100644
--- a/packages/ui/src/views/components/context-menu/ContextMenu.tsx
+++ b/packages/ui/src/views/components/context-menu/ContextMenu.tsx
@@ -78,8 +78,8 @@ export function DesktopContextMenu() {
<Menu
menuType={[menuType]}
onOptionSelect={(params) => {
- const { label: commandId, value } = params;
- commandService && commandService.executeCommand(commandId as string, { value });
+ const { label: id, commandId, value } = params;
+ commandService && commandService.executeCommand(commandId ?? id as string, { value });
const textSelectionRenderManager = injector.get(ITextSelectionRenderManager);
textSelectionRenderManager.focus();
setVisible(false);
diff --git a/packages/ui/src/views/components/context-menu/MobileContextMenu.tsx b/packages/ui/src/views/components/context-menu/MobileContextMenu.tsx
index b578f0ba19..537bed0a29 100644
--- a/packages/ui/src/views/components/context-menu/MobileContextMenu.tsx
+++ b/packages/ui/src/views/components/context-menu/MobileContextMenu.tsx
@@ -70,8 +70,8 @@ export function MobileContextMenu() {
<MobileMenu
menuType={[menuType]}
onOptionSelect={(params) => {
- const { label: commandId, value } = params;
- commandService && commandService.executeCommand(commandId as string, { value });
+ const { label: id, value, commandId } = params;
+ commandService && commandService.executeCommand(commandId ?? id as string, { value });
setVisible(false);
}}
/>
diff --git a/packages/ui/src/views/components/doc-bars/ToolbarItem.tsx b/packages/ui/src/views/components/doc-bars/ToolbarItem.tsx
index 2d0a3e4807..d095e9cc0b 100644
--- a/packages/ui/src/views/components/doc-bars/ToolbarItem.tsx
+++ b/packages/ui/src/views/components/doc-bars/ToolbarItem.tsx
@@ -206,7 +206,7 @@ export const ToolbarItem = forwardRef((props: IDisplayMenuItem<IMenuItem>, ref:
className={styles.toolbarItemTextButton}
active={activated}
disabled={disabled}
- onClick={() => handleCommandExecuted(props.id)}
+ onClick={() => handleCommandExecuted(props.commandId ?? props.id)}
onDoubleClick={() => props.subId && handleCommandExecuted(props.subId)}
>
{isCustomComponent