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
refactor: refactor menu component with submenu options
wzhudev1903eae33ea5
diff --git a/packages/base-ui/src/Components/Dropdown/Dropdown.tsx b/packages/base-ui/src/Components/Dropdown/Dropdown.tsxindex cf32ec823a..23c7f26e3f 100644--- a/packages/base-ui/src/Components/Dropdown/Dropdown.tsx+++ b/packages/base-ui/src/Components/Dropdown/Dropdown.tsx@@ -91,6 +91,7 @@ export class Dropdown extends PureComponent<BaseDropdownProps, IState> {)}{content}<Menu+ options={menu.options}onClick={menu.onClick}ref={this.MenuRef}menu={menu.menu}diff --git a/packages/base-ui/src/Components/Menu/Menu.tsx b/packages/base-ui/src/Components/Menu/Menu.tsxindex 69ec99d9b0..4f688e07cf 100644--- a/packages/base-ui/src/Components/Menu/Menu.tsx+++ b/packages/base-ui/src/Components/Menu/Menu.tsx@@ -7,6 +7,7 @@ import { CustomLabel } from '../CustomLabel';import styles from './index.module.less';import { IDisplayMenuItem } from '../../services/menu/menu.service';import { AppContext } from '../../Common/AppContext';+import { IMenuItem } from '../../services/menu/menu';export class Menu extends Component<BaseMenuProps, BaseMenuState> {private _MenuRef = createRef<HTMLUListElement>();@@ -93,15 +94,15 @@ export class Menu extends Component<BaseMenuProps, BaseMenuState> {};render() {- const { className = '', style = '', menu, menuItems, deep = 0 } = this.props;+ const { className = '', style = '', menu, menuItems, deep = 0, options } = this.props;const { show, posStyle } = this.state;return (<ul className={joinClassNames(styles.colsMenu, className)} style={{ ...style, ...posStyle, display: show ? 'block' : 'none' }} ref={this._MenuRef}>- {/* NOTE: this will be dropped after we complete migrating to new UI system. */}- {/* render selections */}+ {/* legacy: render selections */}{menu?.map((item: BaseMenuItem, index: number) => {if (item.show === false) return;+ // TODO@wzhudev: missing a mechanism to highlight currently selected option via value.return (<><li@@ -116,6 +117,7 @@ export class Menu extends Component<BaseMenuProps, BaseMenuState> {}}><CustomLabel label={item.label} />+ {/* TODO: if the menu itself contains a submenu. It should be served as an `IMenuItem` not wrapped options. */}{item.children ? (<Menuref={(ele: Menu) => (this._refs[index] = ele)}@@ -134,6 +136,7 @@ export class Menu extends Component<BaseMenuProps, BaseMenuState> {</>);})}+ {/* render custom components */}{/* render submenus */}{menuItems?.map((item, index) => (<MenuItem menuItem={item} index={index} onClick={() => this.showMenu(false)} />@@ -150,7 +153,13 @@ export class Menu extends Component<BaseMenuProps, BaseMenuState> {}}-export class MenuItem extends Component<{ menuItem: IDisplayMenuItem<any>; index: number; onClick: () => void }, { disabled: boolean }> {+export interface IMenuItemProps {+ menuItem: IDisplayMenuItem<IMenuItem>;+ index: number;+ onClick: () => void;+}++export class MenuItem extends Component<IMenuItemProps, { disabled: boolean }> {static override contextType = AppContext;private disabledSubscription: Subscription | undefined;diff --git a/packages/base-ui/src/Components/Select/Select.tsx b/packages/base-ui/src/Components/Select/Select.tsxindex 0f6f40128c..09e32e3aad 100644--- a/packages/base-ui/src/Components/Select/Select.tsx+++ b/packages/base-ui/src/Components/Select/Select.tsx@@ -8,6 +8,7 @@ import { BaseItemProps, Item } from '../Item/Item';import { CustomLabel, NeoCustomLabel } from '../CustomLabel';import styles from './index.module.less';import { Icon, IDisplayMenuItem, IMenuItem, IMenuService } from '../..'; // FIXME: strange import+import { IValueOption } from '../../services/menu/menu.service';// TODO: these type definitions should be moved out of components to menu service@@ -66,6 +67,7 @@ export interface BaseSelectProps {value?: string | number;icon?: string;id?: string;+ options?: IValueOption;title?: string;}@@ -402,7 +404,7 @@ export class Select extends PureComponent<BaseSelectProps, IState> {};renderNeo = () => {- const { tooltip, onClick, display, value, icon, title, id } = this.props;+ const { tooltip, onClick, display, value, icon, title, id, options } = this.props;const { menuItems } = this.state;const onClickInner = (...args: unknown[]) => {@@ -413,7 +415,7 @@ export class Select extends PureComponent<BaseSelectProps, IState> {<div className={`${styles.selectDouble}`}>{/* TODO@wzhudev: we should compose options and builtin component here. They may have different onClick callback. */}{/* TODO@wzhudev: should pass in a value to set the menu's selected status. */}- <Dropdown tooltip={tooltip} onClick={onClick} menu={{ parentId: id, onClick: onClickInner, menuItems }} icon={<Icon.NextIcon />}>+ <Dropdown tooltip={tooltip} onClick={onClick} menu={{ parentId: id, options, onClick: onClickInner, menuItems }} icon={<Icon.NextIcon />}>{/* TODO@wzhudev: change menu props of Dropdown. Dropdown shouldn't know how to create a menu. */}<div className={styles.selectLabel}><NeoCustomLabel icon={icon} display={display} title={title!} value={value} onChange={(v) => onClick?.(v)} />diff --git a/packages/base-ui/src/Interfaces/Menu.ts b/packages/base-ui/src/Interfaces/Menu.tsindex 00aa25abf6..9639fe9ae7 100644--- a/packages/base-ui/src/Interfaces/Menu.ts+++ b/packages/base-ui/src/Interfaces/Menu.ts@@ -1,6 +1,6 @@import { ComponentChildren, JSX } from 'preact';import { BaseComponent, JSXComponent } from '../BaseComponent';-import { IDisplayMenuItem } from '../services/menu/menu.service';+import { IDisplayMenuItem, IValueOption } from '../services/menu/menu.service';export interface BaseMenuItem {className?: string;@@ -15,13 +15,6 @@ export interface BaseMenuItem {}export interface BaseMenuProps {- /** @deprecated this is legacy menu mechanism. Do not use it. Use `menuItems` instead. */- parentId?: string;- menu?: BaseMenuItem[];-- // eslint-disable-next-line @typescript-eslint/no-explicit-any- menuItems?: Array<IDisplayMenuItem<any>>;-onClick?: (...arg: any) => void;className?: string;style?: JSX.CSSProperties;@@ -29,6 +22,15 @@ export interface BaseMenuProps {dom?: HTMLElement;ref?: any;deep?: number;+ /** @deprecated this is legacy menu mechanism. Do not use it. Use `menuItems` instead. */+ menu?: BaseMenuItem[]; // TODO: should be renamed to `items`++ // NOTE: options above are legacy. They are going to be removed after we complete refactoring.++ menuId?: string;+ // eslint-disable-next-line @typescript-eslint/no-explicit-any+ menuItems?: Array<IDisplayMenuItem<any>>;+ options?: IValueOption[];}export type BaseMenuState = {diff --git a/packages/base-ui/src/index.ts b/packages/base-ui/src/index.tsindex e2e94243c7..9826d8c8cc 100644--- a/packages/base-ui/src/index.ts+++ b/packages/base-ui/src/index.ts@@ -8,7 +8,8 @@ export * from './Components';export * from './BaseComponent';export * from './UIPlugin';-export { IMenuService, IMenuItem, IMenuButtonItem, IMenuSelectorItem, MenuPosition, DesktopMenuService, IDisplayMenuItem, MenuItemType } from './services/menu/menu.service';+export { MenuItemType, MenuPosition, ICustomComponentOption, IMenuButtonItem, IMenuItem, IMenuSelectorItem, IValueOption } from './services/menu/menu';+export { IMenuService, DesktopMenuService, IDisplayMenuItem } from './services/menu/menu.service';export { KeyCode, MetaKeys } from './services/shortcut/keycode';export { IShortcutService, IShortcutItem, DesktopShortcutService } from './services/shortcut/shortcut.service';export { IPlatformService, DesktopPlatformService } from './services/platform/platform.service';diff --git a/packages/base-ui/src/services/menu/menu.service.ts b/packages/base-ui/src/services/menu/menu.service.tsindex 0c234cd8b0..55aeb95ee9 100644--- a/packages/base-ui/src/services/menu/menu.service.ts+++ b/packages/base-ui/src/services/menu/menu.service.ts@@ -1,102 +1,9 @@import { createIdentifier, IDisposable } from '@wendellhu/redi';-import { Observable } from 'rxjs';-import { ComponentChildren } from 'preact';import { Disposable, toDisposable } from '@univerjs/core';import { IShortcutService } from '../shortcut/shortcut.service';-import { ICustomLabelType } from '../../Interfaces/CustomLabel';-import { DisplayTypes, SelectTypes } from '../../Components/Select/Select';--export type OneOrMany<T> = T | T[];--export const enum MenuPosition {- VOID,- TOOLBAR,- CONTEXT_MENU,- TAB_CONTEXT_MENU,- ROW_HEADER_CONTEXT_MENU,- COL_HEADER_CONTEXT_MENU,-}--export const enum MenuItemType {- /** Button style menu item. */- BUTTON,- /** Menu item with submenus. Submenus could be other IMenuItem or an ID of a registered component. */- SELECTOR,-}--interface IMenuItemBase {- /** ID of the menu item. Normally it should be the same as the ID of the command that it would invoke. */- id: string;- title: string;- description?: string;- icon?: string;- tooltip?: string;-- type: MenuItemType;-- /** In what menu should the item display. */- positions: OneOrMany<MenuPosition>;-- /** @deprecated this parameter would be removed after refactoring */- label?: string | ICustomLabelType | ComponentChildren;-- /** @deprecated this parameter would be removed after refactoring */- className?: string;-- parentId?: string; // if it is submenu-- hidden$?: Observable<boolean>;- disabled$?: Observable<boolean>;-}--export interface IMenuButtonItem extends IMenuItemBase {- type: MenuItemType.BUTTON;-- activated$?: Observable<boolean>;-}--export interface IValueOption {- value: string | number;- label: string;- icon?: string;- tooltip?: string;- style?: object;-}--export interface ICustomComponentOption {- id: string;-}--export interface IMenuSelectorItem<V> extends IMenuItemBase {- type: MenuItemType.SELECTOR;-- /** Determines how the label of the selector should display. */- display?: DisplayTypes;- /** @deprecated this parameter would be removed after we complete refactoring */- selectType: SelectTypes;-- // selections 子菜单可以为三种类型- // 一个是当前 menu 的 options,选中后直接使用其 value 触发 command- // 一个是一个特殊组件,比如 color picker,选中后直接使用其 value 触发 command- // 一个是其他 menu 的 id,直接渲染成其他的 menu- /** Options or IDs of registered components. */- selections?: Array<IValueOption | ICustomComponentOption>; // TODO: we should probably change this to an observable value- /** @deprecated use parentId instead. */- submenus?: string[];-- /** On observable value that should emit the value of the corresponding selection component. */- value$?: Observable<V>;-- /** @deprecated */- label?: {- name: string;- props: any; // TODO@wzhudev: fix later- };-}--export type IMenuItem = IMenuButtonItem | IMenuSelectorItem<unknown>;+import { IMenuItem, MenuPosition } from './menu';/*** @internal@@ -125,8 +32,7 @@ export class DesktopMenuService extends Disposable implements IMenuService {private readonly _menuByPositions = new Map<MenuPosition, Map<string, IMenuItem>>();- // TODO@wzhudev: add map to parent menu item & child menu item- private readonly _menuByParentId = new Map<string, string[]>();+ private readonly _menuByParentId = new Map<string, string[]>(); // NOTE: this could actually be merged with _menuByPositions?constructor(@IShortcutService private readonly _shortcutService: IShortcutService) {super();diff --git a/packages/base-ui/src/services/menu/menu.ts b/packages/base-ui/src/services/menu/menu.tsnew file mode 100644index 0000000000..94d019972d--- /dev/null+++ b/packages/base-ui/src/services/menu/menu.ts@@ -0,0 +1,90 @@+import { Observable } from 'rxjs';+import { ComponentChildren } from 'preact';++import { ICustomLabelType } from '../../Interfaces/CustomLabel';+import { DisplayTypes, SelectTypes } from '../../Components/Select/Select';++export type OneOrMany<T> = T | T[];++export const enum MenuPosition {+ VOID,+ TOOLBAR,+ CONTEXT_MENU,+ TAB_CONTEXT_MENU,+ ROW_HEADER_CONTEXT_MENU,+ COL_HEADER_CONTEXT_MENU,+}++export const enum MenuItemType {+ /** Button style menu item. */+ BUTTON,+ /** Menu item with submenus. Submenus could be other IMenuItem or an ID of a registered component. */+ SELECTOR,+}++interface IMenuItemBase {+ /** ID of the menu item. Normally it should be the same as the ID of the command that it would invoke. */+ id: string;+ title: string;+ description?: string;+ icon?: string;+ tooltip?: string;++ /** @deprecated this type seems unnecessary */+ type: MenuItemType;++ /** In what menu should the item display. */+ positions: OneOrMany<MenuPosition>;++ /** @deprecated this parameter would be removed after refactoring */+ label?: string | ICustomLabelType | ComponentChildren;++ /** @deprecated this parameter would be removed after refactoring */+ className?: string;++ parentId?: string; // if it is submenu++ hidden$?: Observable<boolean>;+ disabled$?: Observable<boolean>;+}++export interface IMenuButtonItem extends IMenuItemBase {+ type: MenuItemType.BUTTON;++ activated$?: Observable<boolean>;+}++export interface IValueOption {+ value: string | number;+ label: string;+ icon?: string;+ tooltip?: string;+ style?: object;+ disabled?: boolean;+}++export interface ICustomComponentOption {+ id: string;+}++export interface IMenuSelectorItem<V> extends IMenuItemBase {+ type: MenuItemType.SELECTOR;++ /** Determines how the label of the selector should display. */+ display?: DisplayTypes;++ /** @deprecated this parameter would be removed after we complete refactoring */+ selectType: SelectTypes;++ // selections 子菜单可以为三种类型+ // 一个是当前 menu 的 options,选中后直接使用其 value 触发 command+ // 一个是一个特殊组件,比如 color picker,选中后直接使用其 value 触发 command+ // 一个是其他 menu 的 id,直接渲染成其他的 menu+ /** Options or IDs of registered components. */+ selections?: Array<IValueOption | ICustomComponentOption>;++ /** On observable value that should emit the value of the corresponding selection component. */+ value$?: Observable<V>;+}++export type IMenuItem = IMenuButtonItem | IMenuSelectorItem<unknown>;diff --git a/packages/ui-plugin-sheets/src/Controller/ToolbarUIController.ts b/packages/ui-plugin-sheets/src/Controller/ToolbarUIController.tsindex a3c9eb785e..f3b064779d 100644--- a/packages/ui-plugin-sheets/src/Controller/ToolbarUIController.ts+++ b/packages/ui-plugin-sheets/src/Controller/ToolbarUIController.ts@@ -6,7 +6,18 @@ import { ComponentChildren } from 'preact';import { SheetToolbarConfig, SHEET_UI_PLUGIN_NAME } from '../Basics';import { ColorSelect, LineBold, LineColor, Toolbar } from '../View';-import { ResetTextColorMenuItemFactory, TextColorSelectorMenuItemFactory } from './menu';+import {+ BoldMenuItemFactory,+ FontFamilySelectorMenuItemFactory,+ FontSizeSelectorMenuItemFactory,+ ItalicMenuItemFactory,+ RedoMenuItemFactory,+ ResetTextColorMenuItemFactory,+ StrikeThroughMenuItemFactory,+ TextColorSelectorMenuItemFactory,+ UnderlineMenuItemFactory,+ UndoMenuItemFactory,+} from './menu';export interface BaseToolbarSelectProps extends BaseSelectProps {children?: BaseSelectChildrenProps[];@@ -102,14 +113,14 @@ export class ToolbarUIController extends Disposable {// TODO: @wzhudev: now we register menu items that only display in the toolbar here. In fact we should register all commands and menu items and shortcuts// in a single controller. I will do that layer.[- // UndoMenuItemFactory,- // RedoMenuItemFactory,- // FontFamilySelectorMenuItemFactory,- // FontSizeSelectorMenuItemFactory,- // BoldMenuItemFactory,- // ItalicMenuItemFactory,- // UnderlineMenuItemFactory,- // StrikeThroughMenuItemFactory,+ UndoMenuItemFactory,+ RedoMenuItemFactory,+ FontFamilySelectorMenuItemFactory,+ FontSizeSelectorMenuItemFactory,+ BoldMenuItemFactory,+ ItalicMenuItemFactory,+ UnderlineMenuItemFactory,+ StrikeThroughMenuItemFactory,TextColorSelectorMenuItemFactory,ResetTextColorMenuItemFactory,// BackgroundColorSelectorMenuItemFactory,diff --git a/packages/ui-plugin-sheets/src/Controller/menu.ts b/packages/ui-plugin-sheets/src/Controller/menu.tsindex 0150e1528d..fdc515bba7 100644--- a/packages/ui-plugin-sheets/src/Controller/menu.ts+++ b/packages/ui-plugin-sheets/src/Controller/menu.ts@@ -33,7 +33,6 @@ import { map } from 'rxjs/operators';import { RightMenuInput } from '../View';import {BORDER_LINE_CHILDREN,- BORDER_SIZE_CHILDREN,FONT_FAMILY_CHILDREN,FONT_SIZE_CHILDREN,HORIZONTAL_ALIGN_CHILDREN,@@ -41,7 +40,6 @@ import {TEXT_WRAP_CHILDREN,VERTICAL_ALIGN_CHILDREN,} from '../View/Toolbar/Const';-import { LineBold, LineColor } from '../View/Common';import { SHEET_UI_PLUGIN_NAME } from '../Basics/Const/PLUGIN_NAME';import styles from '../View/Toolbar/index.module.less';@@ -399,10 +397,6 @@ export function TextColorSelectorMenuItemFactory(accessor: IAccessor): IMenuSele},{id: SHEET_UI_PLUGIN_NAME + ColorPicker.name,- // label: {- // name: SHEET_UI_PLUGIN_NAME + ColorPicker.name,- // // },- // className: styles.selectColorPicker,},],value$: new Observable<string>((subscriber) => {@@ -423,7 +417,7 @@ export function TextColorSelectorMenuItemFactory(accessor: IAccessor): IMenuSele};}-export function BackgroundColorSelectorMenuItemFactory(accessor: IAccessor): IMenuSelectorItem {+export function BackgroundColorSelectorMenuItemFactory(accessor: IAccessor): IMenuSelectorItem<string> {const commandService = accessor.get(ICommandService);const selectionManager = accessor.get(ISelectionManager);@@ -439,16 +433,10 @@ export function BackgroundColorSelectorMenuItemFactory(accessor: IAccessor): IMeclassName: styles.selectColorPickerParent,selections: [{- label: 'toolbar.resetColor',+ id: 'toolbar.resetColor',},{- label: {- name: SHEET_UI_PLUGIN_NAME + ColorPicker.name,- props: {- onClick: (color: string, e: MouseEvent) => {},- },- },- className: styles.selectColorPicker,+ id: SHEET_UI_PLUGIN_NAME + ColorPicker.name,},],value$: new Observable<string>((subscriber) => {@@ -469,7 +457,7 @@ export function BackgroundColorSelectorMenuItemFactory(accessor: IAccessor): IMe};}-export function CellBorderSelectorMenuItemFactory(accessor: IAccessor): IMenuSelectorItem {+export function CellBorderSelectorMenuItemFactory(accessor: IAccessor): IMenuSelectorItem<IBorderData | undefined> {const permissionService = accessor.get(IPermissionService);const commandService = accessor.get(ICommandService);const selectionManager = accessor.get(ISelectionManager);@@ -483,41 +471,43 @@ export function CellBorderSelectorMenuItemFactory(accessor: IAccessor): IMenuSelselectType: SelectTypes.DOUBLE,selections: [...BORDER_LINE_CHILDREN,- {- name: 'borderColor',- label: {- name: SHEET_UI_PLUGIN_NAME + LineColor.name,- props: {- color: '#000',- label: 'borderLine.borderColor',- },- },- className: styles.selectColorPickerParent,- children: [- {- label: {- name: SHEET_UI_PLUGIN_NAME + ColorPicker.name,- props: {},- },- className: styles.selectColorPicker,- onClick: (...arg) => {- arg[0].stopPropagation();- },- },- ],- },- {- label: {- name: SHEET_UI_PLUGIN_NAME + LineBold.name,- props: {- img: 0,- label: 'borderLine.borderSize',- },- },- onClick: (...arg) => {},- className: styles.selectLineBoldParent,- children: BORDER_SIZE_CHILDREN,- },+ // TODO: change to border color menu item here+ // {+ // name: 'borderColor',+ // label: {+ // name: SHEET_UI_PLUGIN_NAME + LineColor.name,+ // props: {+ // color: '#000',+ // label: 'borderLine.borderColor',+ // },+ // },+ // className: styles.selectColorPickerParent,+ // children: [+ // {+ // label: {+ // name: SHEET_UI_PLUGIN_NAME + ColorPicker.name,+ // props: {},+ // },+ // className: styles.selectColorPicker,+ // onClick: (...arg) => {+ // arg[0].stopPropagation();+ // },+ // },+ // ],+ // },+ // TODO: add a set line bold menu item here+ // {+ // label: {+ // name: SHEET_UI_PLUGIN_NAME + LineBold.name,+ // props: {+ // img: 0,+ // label: 'borderLine.borderSize',+ // },+ // },+ // onClick: (...arg) => {},+ // className: styles.selectLineBoldParent,+ // children: BORDER_SIZE_CHILDREN,+ // },],value$: new Observable<IBorderData | undefined>((subscriber) => {const disposable = commandService.onCommandExecuted((c) => {@@ -543,7 +533,7 @@ export function CellBorderSelectorMenuItemFactory(accessor: IAccessor): IMenuSel// }// }-export function HorizontalAlignMenuItemFactory(accessor: IAccessor): IMenuSelectorItem {+export function HorizontalAlignMenuItemFactory(accessor: IAccessor): IMenuSelectorItem<number> {return {id: SetHorizontalTextAlignCommand.id,title: 'horizontalAlignMode',@@ -557,7 +547,7 @@ export function HorizontalAlignMenuItemFactory(accessor: IAccessor): IMenuSelect};}-export function VerticalAlignMenuItemFactory(accessor: IAccessor): IMenuSelectorItem {+export function VerticalAlignMenuItemFactory(accessor: IAccessor): IMenuSelectorItem<number> {return {id: SetVerticalTextAlignCommand.id,title: 'verticalAlignMode',@@ -571,7 +561,7 @@ export function VerticalAlignMenuItemFactory(accessor: IAccessor): IMenuSelector};}-export function WrapTextMenuItemFactory(accessor: IAccessor): IMenuSelectorItem {+export function WrapTextMenuItemFactory(accessor: IAccessor): IMenuSelectorItem<number> {const commandService = accessor.get(ICommandService);const selectionManager = accessor.get(ISelectionManager);