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.

feat: update menu component

wzhudev8fe23fd81330
diff --git a/packages/base-ui/src/Components/CustomLabel/CustomLabel.tsx b/packages/base-ui/src/Components/CustomLabel/CustomLabel.tsx
index 132a0e4da9..a9f5847828 100644
--- a/packages/base-ui/src/Components/CustomLabel/CustomLabel.tsx
+++ b/packages/base-ui/src/Components/CustomLabel/CustomLabel.tsx
@@ -88,13 +88,13 @@ export interface IColorSelectProps {
export class ColorSelect extends Component<IColorSelectProps> {
render() {
- const { value, icon } = this.props;
+ const { value, icon, title } = this.props;
+
+ console.log('debug color select value', value);
return (
<div className={styles.colorSelect}>
- <div>
- <CustomLabel label={{ name: icon }} />
- </div>
+ <div>{icon ? <CustomLabel label={{ name: icon }} /> : title}</div>
<div className={styles.colorSelectLine} style={{ background: value }}></div>
</div>
);
diff --git a/packages/base-ui/src/Components/Menu/Menu.tsx b/packages/base-ui/src/Components/Menu/Menu.tsx
index cfde773b63..8567cb15e1 100644
--- a/packages/base-ui/src/Components/Menu/Menu.tsx
+++ b/packages/base-ui/src/Components/Menu/Menu.tsx
@@ -1,18 +1,15 @@
import { Component, ComponentChild, createRef } from 'preact';
import { Subscription } from 'rxjs';
import { ICommandService, isRealNum } from '@univerjs/core';
-import { BaseMenuProps, BaseMenuState, BaseMenuItem, BaseMenuStyle } from '../../Interfaces';
+import { BaseMenuProps, BaseMenuItem, BaseMenuStyle } from '../../Interfaces';
import { joinClassNames } from '../../Utils';
-import { CustomLabel } from '../CustomLabel';
-
-import styles from './index.module.less';
import { AppContext } from '../../Common/AppContext';
-import { ICustomComponentOption, IDisplayMenuItem, IMenuItem, IValueOption, isValueOptions } from '../../services/menu/menu';
-import { BaseMenuItem, BaseMenuProps, BaseMenuStyle } from '../../Interfaces/Menu';
-import { joinClassNames } from '../../Utils/util';
+import { ICustomComponentOption, IDisplayMenuItem, IMenuButtonItem, IMenuItem, IMenuSelectorItem, IValueOption, MenuItemType, isValueOptions } from '../../services/menu/menu';
import { CustomLabel, NeoCustomLabel } from '../CustomLabel/CustomLabel';
import { IMenuService } from '../../services/menu/menu.service';
+import styles from './index.module.less';
+
export interface IBaseMenuState {
show: boolean;
posStyle: BaseMenuStyle;
@@ -225,11 +222,21 @@ export interface IMenuItemProps {
onClick: () => void;
}
-export class MenuItem extends Component<IMenuItemProps, { disabled: boolean }> {
+export interface IMenuItemState {
+ disabled: boolean;
+ menuItems: Array<IDisplayMenuItem<IMenuItem>>;
+ value: any;
+}
+
+// TODO: this component should have something in common with ToolbarItem
+
+export class MenuItem extends Component<IMenuItemProps, IMenuItemState> {
static override contextType = AppContext;
private disabledSubscription: Subscription | undefined;
+ private valueSubscription: Subscription | undefined;
+
private _refs: Menu[] = [];
constructor() {
@@ -237,42 +244,48 @@ export class MenuItem extends Component<IMenuItemProps, { disabled: boolean }> {
this.state = {
disabled: false,
- value: {},
+ value: undefined,
+ menuItems: [],
};
}
mouseEnter = (e: MouseEvent, index: number) => {
- const { menuItem } = this.props;
- if (menuItem.subMenus) {
- this._refs[index].showMenu(true);
- }
+ this._refs[index]?.showMenu(true);
};
mouseLeave = (e: MouseEvent, index: number) => {
- const { menuItem } = this.props;
- if (menuItem.subMenus) {
- this._refs[index].showMenu(false);
- }
+ this._refs[index]?.showMenu(false);
};
override componentDidMount(): void {
this.disabledSubscription = this.props.menuItem.disabled$?.subscribe((disabled) => {
this.setState({ disabled });
});
+
+ this.disabledSubscription = this.props.menuItem.value$?.subscribe((value) => {
+ this.setState({ value });
+ });
+
+ this.getSubMenus();
}
override componentWillUnmount(): void {
this.disabledSubscription?.unsubscribe();
}
- handleClick = (e: MouseEvent, item: IDisplayMenuItem, index: number) => {
- // item.onClick?.call(null, e, item.value, index, deep);
+ override componentWillReceiveProps(nextProps: Readonly<IMenuItemProps>): void {
+ if (this.props.menuItem.id !== nextProps.menuItem.id) {
+ this.getSubMenus();
+ }
+ }
+
+ handleClick = (e: MouseEvent, item: IDisplayMenuItem<IMenuItem>, index: number) => {
const commandService: ICommandService = this.context.injector.get(ICommandService);
this.props.onClick();
+ // TODO: type here is not correct
const value = this.state.value;
- !(item.subMenuItems && item.subMenuItems.length > 0) && commandService.executeCommand(item.id, { value });
- // this.showMenu(false);
+ // !(item.subMenuItems && item.subMenuItems.length > 0) && commandService.executeCommand(item.id, { value });
};
/**
@@ -289,33 +302,53 @@ export class MenuItem extends Component<IMenuItemProps, { disabled: boolean }> {
};
override render(): ComponentChild {
- const { menuItem: item, index } = this.props;
- // const commandService: ICommandService = this.context.injector.get(ICommandService);
+ switch (this.props.menuItem.type) {
+ case MenuItemType.DROPDOWN:
+ case MenuItemType.SELECTOR:
+ return this.renderSelectorType();
+ default:
+ return this.renderButtonType();
+ }
+ }
+
+ private renderButtonType(): ComponentChild {
+ const { menuItem, index } = this.props;
const { disabled } = this.state;
+ const item = menuItem as IDisplayMenuItem<IMenuButtonItem>;
+
+ return <li>{'button element to do'}</li>;
+ }
+
+ private renderSelectorType(): ComponentChild {
+ const { menuItem, index } = this.props;
+ const { disabled, menuItems, value } = this.state;
+ const item = menuItem as IDisplayMenuItem<IMenuSelectorItem<unknown>>;
return (
<li
className={joinClassNames(styles.colsMenuitem, disabled ? styles.colsMenuitemDisabled : '')}
- // onClick={() => {
- // this.props.onClick();
- // commandService.executeCommand(item.id);
- // }}
+ onMouseEnter={(e) => this.mouseEnter(e, index)}
+ onMouseLeave={(e) => this.mouseLeave(e, index)}
onClick={(e) => this.handleClick(e, item, index)}
- onMouseEnter={(e) => {
- this.mouseEnter(e, index);
- }}
- onMouseLeave={(e) => {
- this.mouseLeave(e, index);
- }}
>
- <CustomLabel label={item.label || item.title} onChange={this.onChange}></CustomLabel>
+ <NeoCustomLabel title={item.title} value={value} onChange={this.onChange} icon={item.icon} display={item.display}></NeoCustomLabel>
{item.shortcut && ` (${item.shortcut})`}
- {item.subMenuItems && item.subMenuItems.length > 0 ? (
- <Menu ref={(ele: Menu) => (this._refs[index] = ele)} menuItems={item.subMenuItems} parent={this}></Menu>
- ) : (
- <></>
+ {(menuItems.length > 0 || (item as IMenuSelectorItem<unknown>).selections?.length) && (
+ <Menu ref={(ele: Menu) => (this._refs[index] = ele)} menuId={item.id} options={item.selections} display={item.display} parent={this}></Menu>
)}
</li>
);
}
+
+ private getSubMenus() {
+ const { menuItem } = this.props;
+ const { id } = menuItem;
+
+ if (id) {
+ const menuService: IMenuService = this.context.injector.get(IMenuService);
+ this.setState({
+ menuItems: menuService.getSubMenuItems(id),
+ });
+ }
+ }
}
diff --git a/packages/ui-plugin-sheets/src/Controller/ToolbarUIController.ts b/packages/ui-plugin-sheets/src/Controller/ToolbarUIController.ts
index a1909abc8c..d6c717a77f 100644
--- a/packages/ui-plugin-sheets/src/Controller/ToolbarUIController.ts
+++ b/packages/ui-plugin-sheets/src/Controller/ToolbarUIController.ts
@@ -6,7 +6,7 @@ import { ComponentChildren } from 'preact';
import { SheetToolbarConfig, SHEET_UI_PLUGIN_NAME } from '../Basics';
import { ColorSelect, LineBold, LineColor, Toolbar } from '../View';
-import { SetBorderColorMenuItemFactory, SetBorderStyleMenuItemFactory } from './menu';
+import { SetBorderColorMenuItemFactory } from './menu';
import { CellBorderSelectorMenuItemFactory } from './menu/border.menu';
export interface BaseToolbarSelectProps extends BaseSelectProps {
@@ -117,7 +117,7 @@ export class ToolbarUIController extends Disposable {
// ResetBackgroundColorMenuItemFactory,
CellBorderSelectorMenuItemFactory,
SetBorderColorMenuItemFactory,
- SetBorderStyleMenuItemFactory,
+ // SetBorderStyleMenuItemFactory,
// HorizontalAlignMenuItemFactory,
// VerticalAlignMenuItemFactory,
// WrapTextMenuItemFactory,
diff --git a/packages/ui-plugin-sheets/src/Controller/menu/border.menu.ts b/packages/ui-plugin-sheets/src/Controller/menu/border.menu.ts
index d8ec3d1edf..c5b7bf872e 100644
--- a/packages/ui-plugin-sheets/src/Controller/menu/border.menu.ts
+++ b/packages/ui-plugin-sheets/src/Controller/menu/border.menu.ts
@@ -41,7 +41,7 @@ export function CellBorderSelectorMenuItemFactory(accessor: IAccessor): IMenuSel
};
}
-export function SetBorderColorMenuItemFactory(accessor: IAccessor): IMenuSelectorItem<IBorderData | undefined> {
+export function SetBorderColorMenuItemFactory(accessor: IAccessor): IMenuSelectorItem<string> {
return {
id: SetBorderColorCommand.id,
title: 'border',
@@ -55,6 +55,9 @@ export function SetBorderColorMenuItemFactory(accessor: IAccessor): IMenuSelecto
id: SHEET_UI_PLUGIN_NAME + ColorPicker.name,
},
],
+ value$: new Observable<string>((subscriber) => {
+ subscriber.next('#ff0000');
+ }),
};
}
diff --git a/packages/ui-plugin-sheets/src/View/Toolbar/ToolbarItem.tsx b/packages/ui-plugin-sheets/src/View/Toolbar/ToolbarItem.tsx
index b7e4a8f888..037b6fba88 100644
--- a/packages/ui-plugin-sheets/src/View/Toolbar/ToolbarItem.tsx
+++ b/packages/ui-plugin-sheets/src/View/Toolbar/ToolbarItem.tsx
@@ -13,7 +13,6 @@ export interface IToolbarItemStatus {
value: any;
}
-// TODO@wzhudev: the following component should be moved to base-ui after refactoring
/** The component to render toolbar item. */
export class ToolbarItem extends Component<IDisplayMenuItem<IMenuItem>, IToolbarItemStatus> {
static override contextType = AppContext;