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.

refactor(ui): sheet bar right menu

Dushusira5aac53666a8
diff --git a/packages/base-sheets/src/index.ts b/packages/base-sheets/src/index.ts
index ab3ef374ca..90831a0bef 100644
--- a/packages/base-sheets/src/index.ts
+++ b/packages/base-sheets/src/index.ts
@@ -44,3 +44,4 @@ export { DeleteRangeMoveUpCommand } from './Commands/Commands/delete-range-move-
export { RemoveRowCommand, RemoveColCommand } from './Commands/Commands/remove-row-col.command';
export { SetWorksheetRowHideCommand } from './Commands/Commands/set-worksheet-row-hide.command';
export { SetWorksheetRowShowCommand } from './Commands/Commands/set-worksheet-row-show.command';
+export { RemoveSheetCommand } from './Commands/Commands/remove-sheet.command';
diff --git a/packages/base-ui/src/services/menu/menu.service.ts b/packages/base-ui/src/services/menu/menu.service.ts
index cc25c47797..084a84d1b8 100644
--- a/packages/base-ui/src/services/menu/menu.service.ts
+++ b/packages/base-ui/src/services/menu/menu.service.ts
@@ -11,6 +11,7 @@ export type OneOrMany<T> = T | T[];
export const enum MenuPosition {
TOOLBAR,
CONTEXT_MENU,
+ SHEET_BAR,
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
diff --git a/packages/ui-plugin-sheets/src/Controller/SheetBarUIContruller.ts b/packages/ui-plugin-sheets/src/Controller/SheetBarUIContruller.ts
index 49397fcbe6..c401528c5f 100644
--- a/packages/ui-plugin-sheets/src/Controller/SheetBarUIContruller.ts
+++ b/packages/ui-plugin-sheets/src/Controller/SheetBarUIContruller.ts
@@ -1,4 +1,4 @@
-import { BaseMenuItem, BaseUlProps, ColorPicker, ComponentManager, ICustomComponent } from '@univerjs/base-ui';
+import { BaseMenuItem, BaseUlProps, ColorPicker, ComponentManager, ICustomComponent, IMenuService, MenuPosition } from '@univerjs/base-ui';
import {
Nullable,
CommandManager,
@@ -18,11 +18,12 @@ import {
ICommandService,
} from '@univerjs/core';
import { InsertSheetMutation, RemoveSheetMutation, SetWorksheetActivateCommand, SetWorksheetOrderCommand } from '@univerjs/base-sheets';
-import { Inject, SkipSelf } from '@wendellhu/redi';
+import { Inject, Injector, SkipSelf } from '@wendellhu/redi';
import { SheetBar } from '../View/SheetBar';
import styles from '../View/SheetBar/index.module.less';
import { SheetBarMenuItem } from '../View/SheetBar/SheetBarMenu';
import { SHEET_UI_PLUGIN_NAME } from '../Basics/Const';
+import { DeleteSheetMenuItemFactory } from './menu';
interface SheetUl extends BaseMenuItem {
label?: ICustomComponent | string;
@@ -50,10 +51,12 @@ export class SheetBarUIController extends Disposable {
// eslint-disable-next-line max-lines-per-function
constructor(
+ @Inject(Injector) private readonly _injector: Injector,
@ICurrentUniverService private readonly _currentUniverService: ICurrentUniverService,
@Inject(ComponentManager) private readonly _componentManager: ComponentManager,
@SkipSelf() @Inject(ObserverManager) private readonly _observerManager: ObserverManager,
- @ICommandService private readonly _commandService: ICommandService
+ @ICommandService private readonly _commandService: ICommandService,
+ @IMenuService private readonly _menuService: IMenuService
) {
super();
const that = this;
@@ -115,6 +118,8 @@ export class SheetBarUIController extends Disposable {
},
];
+ this._initializeContextMenu();
+
this._componentManager.register(SHEET_UI_PLUGIN_NAME + ColorPicker.name, ColorPicker);
this.disposeWithMe(
@@ -291,6 +296,8 @@ export class SheetBarUIController extends Disposable {
//this._barControl.dragEnd(elements);
},
});
+
+ this._sheetBar.setSheetUlNeo(this._menuService.getMenuItems(MenuPosition.SHEET_BAR));
}
protected _refreshSheetData(): void {
@@ -349,4 +356,10 @@ export class SheetBarUIController extends Disposable {
this._refreshSheetData();
this._refreshSheetBarUI();
}
+
+ private _initializeContextMenu() {
+ [DeleteSheetMenuItemFactory].forEach((factory) => {
+ this.disposeWithMe(this._menuService.addMenuItem(this._injector.invoke(factory)));
+ });
+ }
}
diff --git a/packages/ui-plugin-sheets/src/Controller/menu.ts b/packages/ui-plugin-sheets/src/Controller/menu.ts
index 398f09f3be..aafa45fad1 100644
--- a/packages/ui-plugin-sheets/src/Controller/menu.ts
+++ b/packages/ui-plugin-sheets/src/Controller/menu.ts
@@ -18,8 +18,9 @@ import {
DeleteRangeMoveUpCommand,
SetWorksheetRowHideCommand,
SetWorksheetRowShowCommand,
+ RemoveSheetCommand,
} from '@univerjs/base-sheets';
-import { IMenuItem, MenuPosition } from '@univerjs/base-ui';
+import { IDisplayMenuItem, IMenuItem, MenuPosition } from '@univerjs/base-ui';
import { FontItalic, FontWeight, ICommandService, IPermissionService, IUndoRedoService, RedoCommand, UndoCommand } from '@univerjs/core';
import { IAccessor } from '@wendellhu/redi';
import { Observable } from 'rxjs';
@@ -255,6 +256,7 @@ export function NumberFormatMenuItemFactory(accessor: IAccessor): IMenuItem {}
// export function ImportMenuItemFactory(accessor: IAccessor): IMenuItem {}
// export function ImageMenuItemFactory(accessor: IAccessor): IMenuItem {}
+// right menu in main container
export function ClearSelectionMenuItemFactory(accessor: IAccessor): IMenuItem {
return {
id: ClearSelectionContentCommand.id,
@@ -367,3 +369,28 @@ export function DeleteRangeMoveUpMenuItemFactory(accessor: IAccessor): IMenuItem
parentId: DeleteRangeCommand.id,
};
}
+
+// right menu in main sheet bar
+export function DeleteSheetMenuItemFactory(accessor: IAccessor): IMenuItem {
+ return {
+ id: RemoveSheetCommand.id,
+ menu: [MenuPosition.SHEET_BAR],
+ title: 'sheetConfig.delete',
+ };
+}
+
+export function buildMenuTree(items: IMenuItem[], parentId?: string): IDisplayMenuItem[] {
+ const tree: IDisplayMenuItem[] = [];
+
+ for (const item of items) {
+ if (item.parentId === parentId) {
+ const treeItem: IDisplayMenuItem = {
+ ...item,
+ subMenuItems: buildMenuTree(items, item.id),
+ };
+ tree.push(treeItem);
+ }
+ }
+
+ return tree;
+}
diff --git a/packages/ui-plugin-sheets/src/View/RightMenu/RightMenu.tsx b/packages/ui-plugin-sheets/src/View/RightMenu/RightMenu.tsx
index 6f9e9a90ae..0734c2f611 100644
--- a/packages/ui-plugin-sheets/src/View/RightMenu/RightMenu.tsx
+++ b/packages/ui-plugin-sheets/src/View/RightMenu/RightMenu.tsx
@@ -3,6 +3,7 @@ import { BaseRightMenuProps, IDisplayMenuItem, IMenuItem, Menu } from '@univerjs
import { Component, createRef } from 'preact';
import { RightMenuProps } from '../../Controller';
import Style from './index.module.less';
+import { buildMenuTree } from '../../Controller/menu';
interface IState {
visible: boolean;
@@ -40,26 +41,10 @@ export class RightMenu extends Component<BaseRightMenuProps, IState> {
setMenuListNeo = (menuItems: IMenuItem[]) => {
this.setState({
- menuItems: this.buildMenuTree(menuItems),
+ menuItems: buildMenuTree(menuItems),
});
};
- buildMenuTree(items: IMenuItem[], parentId?: string): IDisplayMenuItem[] {
- const tree: IDisplayMenuItem[] = [];
-
- for (const item of items) {
- if (item.parentId === parentId) {
- const treeItem: IDisplayMenuItem = {
- ...item,
- subMenuItems: this.buildMenuTree(items, item.id),
- };
- tree.push(treeItem);
- }
- }
-
- return tree;
- }
-
// TODO:添加到具体的元素
override componentDidMount() {
this.props.getComponent?.(this);
diff --git a/packages/ui-plugin-sheets/src/View/SheetBar/SheetBar.tsx b/packages/ui-plugin-sheets/src/View/SheetBar/SheetBar.tsx
index e306e97f3f..3ca5fc3838 100644
--- a/packages/ui-plugin-sheets/src/View/SheetBar/SheetBar.tsx
+++ b/packages/ui-plugin-sheets/src/View/SheetBar/SheetBar.tsx
@@ -1,13 +1,15 @@
-import { BaseMenuItem, BaseSheetBarProps, Button, CustomLabel, Icon, Menu } from '@univerjs/base-ui';
+import { BaseMenuItem, BaseSheetBarProps, Button, CustomLabel, IDisplayMenuItem, IMenuItem, Icon, Menu } from '@univerjs/base-ui';
import { RefObject, Component, createRef } from 'preact';
import { SheetBarMenu, ISheetBarMenuItem } from './SheetBarMenu';
import styles from './index.module.less';
import { SlideTabBar } from '../../Basics/SlideTabBar/SlideTabBar';
+import { buildMenuTree } from '../../Controller/menu';
type SheetState = {
sheetList: BaseSheetBarProps[];
menuList: BaseSheetBarProps[];
sheetUl: BaseMenuItem[];
+ menuItems: IDisplayMenuItem[];
};
export class SheetBar extends Component<BaseSheetBarProps, SheetState> {
@@ -56,6 +58,12 @@ export class SheetBar extends Component<BaseSheetBarProps, SheetState> {
this.setState((prevState) => ({ ...value }), fn);
};
+ setSheetUlNeo(menuItems: IMenuItem[]) {
+ this.setState({
+ menuItems: buildMenuTree(menuItems),
+ });
+ }
+
// 点击按钮左右滑动
scrollLeft = (e: MouseEvent) => {
this.slideTabBar.getScrollbar().scrollX(this.slideTabBar.getScrollbar().getScrollX() - 50);
@@ -206,7 +214,7 @@ export class SheetBar extends Component<BaseSheetBarProps, SheetState> {
</div>
{/* mouse right button context menu */}
- <Menu className={styles.sheetUl} menu={sheetUl} ref={this.ulRef} />
+ <Menu className={styles.sheetUl} menu={sheetUl} menuItems={this.state.menuItems} ref={this.ulRef} />
{/* prev next scroll button */}
<div className={`${styles.sheetBarOptions} ${styles.sheetBarScrollButton}`}>