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: merge positions & parentId
wzhudeve727ef086add
diff --git a/packages/base-ui/src/Components/Menu/Menu.tsx b/packages/base-ui/src/Components/Menu/Menu.tsxindex 0f5436818f..5b3e31a641 100644--- a/packages/base-ui/src/Components/Menu/Menu.tsx+++ b/packages/base-ui/src/Components/Menu/Menu.tsx@@ -178,7 +178,6 @@ export class Menu extends Component<BaseMenuProps, IBaseMenuState> {// render value optionconst isValueOption = isValueOptions(option);if (isValueOption) {- console.log('selected', value, option.value);return (<liclassName={joinClassNames(styles.colsMenuitem, option.disabled ? styles.colsMenuitemDisabled : '')}@@ -235,7 +234,7 @@ export class Menu extends Component<BaseMenuProps, IBaseMenuState> {if (menuId) {const menuService: IMenuService = this.context.injector.get(IMenuService);this.setState({- menuItems: menuService.getSubMenuItems(menuId),+ menuItems: menuService.getMenuItems(menuId),});}}@@ -422,7 +421,7 @@ export class MenuItem extends Component<IMenuItemProps, IMenuItemState> {if (id) {const menuService: IMenuService = this.context.injector.get(IMenuService);this.setState({- menuItems: menuService.getSubMenuItems(id),+ menuItems: menuService.getMenuItems(id),});}}diff --git a/packages/base-ui/src/services/menu/menu.service.ts b/packages/base-ui/src/services/menu/menu.service.tsindex efa7f8d79a..2712b8dbb9 100644--- a/packages/base-ui/src/services/menu/menu.service.ts+++ b/packages/base-ui/src/services/menu/menu.service.ts@@ -35,18 +35,15 @@ export const IMenuService = createIdentifier<IMenuService>('univer.menu-service'export interface IMenuService {addMenuItem(item: IMenuItem): IDisposable;- /** Get menu items for display at a given position. */- getMenuItems(position: MenuPosition): Array<IDisplayMenuItem<IMenuItem>>;- getSubMenuItems(parentId: string): Array<IDisplayMenuItem<IMenuItem>>;+ /** Get menu items for display at a given position or a submenu. */+ getMenuItems(position: MenuPosition | string): Array<IDisplayMenuItem<IMenuItem>>;getMenuItem(id: string): IMenuItem | null;}export class DesktopMenuService extends Disposable implements IMenuService {private readonly _menuItemMap = new Map<string, IMenuItem>();- private readonly _menuByPositions = new Map<MenuPosition, Map<string, IMenuItem>>();-- private readonly _menuByParentId = new Map<string, string[]>(); // NOTE: this could actually be merged with _menuByPositions?+ private readonly _menuByPositions = new Map<MenuPosition | string, Array<[string, IMenuItem]>>();constructor(@IShortcutService private readonly _shortcutService: IShortcutService) {super();@@ -69,49 +66,44 @@ export class DesktopMenuService extends Disposable implements IMenuService {this.appendMenuToPosition(item, item.positions);}- if (item.parentId) {- this._menuByParentId.set(item.parentId, [...(this._menuByParentId.get(item.parentId) ?? []), item.id]);- }-return toDisposable(() => {this._menuItemMap.delete(item.id);if (Array.isArray(item.positions)) {- item.positions.forEach((menu) => this._menuByPositions.get(menu)?.delete(item.id));- } else {- this._menuByPositions.get(item.positions)?.delete(item.id);- }+ item.positions.forEach((menu) => {+ const menus = this._menuByPositions.get(menu);+ if (!menus) {+ return;+ }- if (item.parentId) {- const children = this._menuByParentId.get(item.parentId);- if (children) {- const index = children.findIndex((id) => id === item.id);+ const index = menus.findIndex((m) => m[0] === item.id);if (index > -1) {- children.splice(index, 1);+ menus.splice(index, 1);}+ });+ } else {+ const menus = this._menuByPositions.get(item.positions);+ if (!menus) {+ return;+ }++ const index = menus.findIndex((m) => m[0] === item.id);+ if (index > -1) {+ menus.splice(index, 1);}}});}- getMenuItems(positions: MenuPosition): Array<IDisplayMenuItem<IMenuItem>> {+ getMenuItems(positions: MenuPosition | string): Array<IDisplayMenuItem<IMenuItem>> {// TODO: @wzhudev: compose shortcut to returned menu items.if (this._menuByPositions.has(positions)) {- return [...this._menuByPositions.get(positions)!.values()].filter((menu) => !menu.parentId).map((menu) => this.getDisplayMenuItems(menu));+ return [...this._menuByPositions.get(positions)!.values()].map((menu) => this.getDisplayMenuItems(menu[1]));}return [] as Array<IDisplayMenuItem<IMenuItem>>;}- getSubMenuItems(parentId: string): Array<IDisplayMenuItem<IMenuItem>> {- const subMenuIds = this._menuByParentId.get(parentId);- if (!subMenuIds) {- return [];- }-- return subMenuIds.map((id) => this.getDisplayMenuItems(this._menuItemMap.get(id)!));- }-getMenuItem(id: string): IMenuItem | null {if (this._menuItemMap.has(id)) {return this._menuItemMap.get(id)!;@@ -132,16 +124,16 @@ export class DesktopMenuService extends Disposable implements IMenuService {};}- private appendMenuToPosition(menu: IMenuItem, position: MenuPosition) {+ private appendMenuToPosition(menu: IMenuItem, position: MenuPosition | string) {if (!this._menuByPositions.has(position)) {- this._menuByPositions.set(position, new Map<string, IMenuItem>());+ this._menuByPositions.set(position, []);}- const menuSet = this._menuByPositions.get(position)!;- if (menuSet.has(menu.id)) {+ const menuList = this._menuByPositions.get(position)!;+ if (menuList.findIndex((m) => m[0] === menu.id) > -1) {throw new Error(`Menu item with the same id ${menu.id} has already been added!`);}- menuSet.set(menu.id, menu);+ menuList.push([menu.id, menu]);}}diff --git a/packages/base-ui/src/services/menu/menu.ts b/packages/base-ui/src/services/menu/menu.tsindex 6e3f8dd544..d457002190 100644--- a/packages/base-ui/src/services/menu/menu.ts+++ b/packages/base-ui/src/services/menu/menu.ts@@ -6,13 +6,13 @@ 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,- SHEET_BAR,+ VOID = 'void',+ TOOLBAR = 'toolbar',+ CONTEXT_MENU = 'contextMenu',+ TAB_CONTEXT_MENU = 'tabContextMenu',+ ROW_HEADER_CONTEXT_MENU = 'rowHeaderContextMenu',+ COL_HEADER_CONTEXT_MENU = 'colHeaderContextMenu',+ SHEET_BAR = 'sheetBar',}export const enum MenuItemType {@@ -36,7 +36,7 @@ interface IMenuItemBase {type: MenuItemType;/** In what menu should the item display. */- positions: OneOrMany<MenuPosition>;+ positions: OneOrMany<MenuPosition | string>;/*** Custom label component id.@@ -48,7 +48,10 @@ interface IMenuItemBase {/** @deprecated this parameter would be removed after refactoring */className?: string;- /** If the menu is in a submenu, this property would be its parent menu item's id. */+ /**+ * If the menu is in a submenu, this property would be its parent menu item's id.+ * @deprecated use positions instead+ */parentId?: string;hidden$?: Observable<boolean>;diff --git a/packages/ui-plugin-sheets/src/Controller/RightMenuUIController.ts b/packages/ui-plugin-sheets/src/Controller/RightMenuUIController.tsindex 3f608922c3..c8a90e6aec 100644--- a/packages/ui-plugin-sheets/src/Controller/RightMenuUIController.ts+++ b/packages/ui-plugin-sheets/src/Controller/RightMenuUIController.ts@@ -4,6 +4,7 @@ import { ComponentChildren } from 'preact';import { Disposable, ICurrentUniverService, ObserverManager, Tools, UIObserver } from '@univerjs/core';import { Inject, Injector, SkipSelf } from '@wendellhu/redi';import { CanvasView } from '@univerjs/base-sheets';+import { IMenuItemFactory } from '@univerjs/base-ui/src/services/menu/menu';import { DefaultRightMenuConfig, SheetRightMenuConfig } from '../Basics';import { RightMenu, RightMenuInput, RightMenuItem } from '../View';import styles from '../View/RightMenu/index.module.less';@@ -304,18 +305,20 @@ export class RightMenuUIController extends Disposable {}private _initializeContextMenu() {- [- ClearSelectionMenuItemFactory,- InsertRowMenuItemFactory,- InsertColMenuItemFactory,- RemoveRowMenuItemFactory,- RemoveColMenuItemFactory,- SetRowHeightMenuItemFactory,- SetColWidthMenuItemFactory,- DeleteRangeMenuItemFactory,- DeleteRangeMoveLeftMenuItemFactory,- DeleteRangeMoveUpMenuItemFactory,- ].forEach((factory) => {+ (+ [+ ClearSelectionMenuItemFactory,+ InsertRowMenuItemFactory,+ InsertColMenuItemFactory,+ RemoveRowMenuItemFactory,+ RemoveColMenuItemFactory,+ SetRowHeightMenuItemFactory,+ SetColWidthMenuItemFactory,+ DeleteRangeMenuItemFactory,+ DeleteRangeMoveLeftMenuItemFactory,+ DeleteRangeMoveUpMenuItemFactory,+ ] as IMenuItemFactory[]+ ).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.tsindex ec7ac977ea..e8b3964a9b 100644--- a/packages/ui-plugin-sheets/src/Controller/menu.ts+++ b/packages/ui-plugin-sheets/src/Controller/menu.ts@@ -384,8 +384,7 @@ export function ResetTextColorMenuItemFactory(accessor: IAccessor): IMenuButtonIid: ResetTextColorCommand.id,type: MenuItemType.BUTTON,title: 'toolbar.resetColor',- positions: [MenuPosition.TOOLBAR],- parentId: SetTextColorCommand.id,+ positions: SetTextColorCommand.id,};}@@ -428,8 +427,7 @@ export function ResetBackgroundColorMenuItemFactory(accessor: IAccessor): IMenuBid: ResetBackgroundColorCommand.id,type: MenuItemType.BUTTON,title: 'toolbar.resetColor',- positions: [MenuPosition.TOOLBAR],- parentId: SetBackgroundColorCommand.id,+ positions: SetBackgroundColorCommand.id,};}@@ -553,7 +551,6 @@ export function WrapTextMenuItemFactory(accessor: IAccessor): IMenuSelectorItem<};}-// FIXME: set rotation would cause a bugexport function TextRotateMenuItemFactory(accessor: IAccessor): IMenuSelectorItem<number | string> {return {id: SetTextRotationCommand.id,@@ -580,14 +577,12 @@ export function TextRotateMenuItemFactory(accessor: IAccessor): IMenuSelectorIte};}-// right menu in main containerexport function ClearSelectionMenuItemFactory(accessor: IAccessor): IMenuButtonItem {return {id: ClearSelectionContentCommand.id,type: MenuItemType.BUTTON,title: 'rightClick.clearContent',positions: [MenuPosition.CONTEXT_MENU],- parentId: 'right-menu',};}@@ -597,7 +592,6 @@ export function InsertRowMenuItemFactory(accessor: IAccessor): IMenuButtonItem {type: MenuItemType.BUTTON,positions: [MenuPosition.CONTEXT_MENU],title: 'rightClick.insertRow',- parentId: 'right-menu',};}@@ -607,7 +601,6 @@ export function InsertColMenuItemFactory(accessor: IAccessor): IMenuButtonItem {type: MenuItemType.BUTTON,positions: [MenuPosition.CONTEXT_MENU],title: 'rightClick.insertColumn',- parentId: 'right-menu',};}@@ -617,7 +610,6 @@ export function RemoveRowMenuItemFactory(accessor: IAccessor): IMenuButtonItem {type: MenuItemType.BUTTON,positions: [MenuPosition.CONTEXT_MENU],title: 'rightClick.deleteSelectedRow',- parentId: 'right-menu',};}@@ -627,7 +619,6 @@ export function HideRowMenuItemFactory(accessor: IAccessor): IMenuButtonItem {type: MenuItemType.BUTTON,positions: [MenuPosition.CONTEXT_MENU],title: 'rightClick.hideSelectedRow',- parentId: 'right-menu',};}@@ -637,7 +628,6 @@ export function ShowRowMenuItemFactory(accessor: IAccessor): IMenuButtonItem {type: MenuItemType.BUTTON,positions: [MenuPosition.CONTEXT_MENU],title: 'rightClick.showHideRow',- parentId: 'right-menu',};}@@ -647,7 +637,6 @@ export function RemoveColMenuItemFactory(accessor: IAccessor): IMenuButtonItem {type: MenuItemType.BUTTON,positions: [MenuPosition.CONTEXT_MENU],title: 'rightClick.deleteSelectedColumn',- parentId: 'right-menu',};}@@ -664,7 +653,6 @@ export function SetRowHeightMenuItemFactory(accessor: IAccessor): IMenuButtonItesuffix: 'px',},},- parentId: 'right-menu',};}@@ -681,7 +669,6 @@ export function SetColWidthMenuItemFactory(accessor: IAccessor): IMenuButtonItemsuffix: 'px',},},- parentId: 'right-menu',};}@@ -691,7 +678,6 @@ export function DeleteRangeMenuItemFactory(accessor: IAccessor): IMenuSelectorIttype: MenuItemType.SUBITEMS, // 子菜单也需要单独command id,无法使用selector的selectionspositions: [MenuPosition.CONTEXT_MENU],title: 'rightClick.deleteCell',- parentId: 'right-menu',};}@@ -699,9 +685,8 @@ export function DeleteRangeMoveLeftMenuItemFactory(accessor: IAccessor): IMenuBureturn {id: DeleteRangeMoveLeftCommand.id,type: MenuItemType.BUTTON,- positions: [MenuPosition.CONTEXT_MENU],title: 'rightClick.moveLeft',- parentId: DeleteRangeCommand.id,+ positions: DeleteRangeCommand.id,};}@@ -709,9 +694,8 @@ export function DeleteRangeMoveUpMenuItemFactory(accessor: IAccessor): IMenuButtreturn {id: DeleteRangeMoveUpCommand.id,type: MenuItemType.BUTTON,- positions: [MenuPosition.CONTEXT_MENU],title: 'rightClick.moveUp',- parentId: DeleteRangeCommand.id,+ positions: DeleteRangeCommand.id,};}@@ -722,7 +706,6 @@ export function DeleteSheetMenuItemFactory(accessor: IAccessor): IMenuButtonItemtype: MenuItemType.BUTTON,positions: [MenuPosition.SHEET_BAR],title: 'sheetConfig.delete',- parentId: 'sheet-bar-right-menu',};}@@ -733,7 +716,6 @@ export function CopySheetMenuItemFactory(accessor: IAccessor): IMenuButtonItem {type: MenuItemType.BUTTON,positions: [MenuPosition.SHEET_BAR],title: 'sheetConfig.copy',- parentId: 'sheet-bar-right-menu',};}@@ -745,7 +727,6 @@ export function RenameSheetMenuItemFactory(accessor: IAccessor): IMenuButtonItemtype: MenuItemType.BUTTON,positions: [MenuPosition.SHEET_BAR],title: 'sheetConfig.rename',- parentId: 'sheet-bar-right-menu',onClick: () => {// TODO@Dushusir 这里能监听到点击事件,但是无法触发到 this._sheetBar.reNameSheet(this._dataId);console.info('rename=========');@@ -767,7 +748,6 @@ export function ChangeColorSheetMenuItemFactory(accessor: IAccessor): IMenuSelecid: SHEET_UI_PLUGIN_NAME + ColorPicker.name,},],- parentId: 'sheet-bar-right-menu',};}@@ -777,7 +757,6 @@ export function HideSheetMenuItemFactory(accessor: IAccessor): IMenuButtonItem {type: MenuItemType.BUTTON,positions: [MenuPosition.SHEET_BAR],title: 'sheetConfig.hide',- parentId: 'sheet-bar-right-menu',};}@@ -788,10 +767,10 @@ export function UnHideSheetMenuItemFactory(accessor: IAccessor): IMenuButtonItemtype: MenuItemType.BUTTON,positions: [MenuPosition.SHEET_BAR],title: 'sheetConfig.unhide',- parentId: 'sheet-bar-right-menu',};}+/** @deprecated not the correct ui mechanism */export function buildMenuTree(items: IMenuItem[], parentId?: string): IDisplayMenuItem[] {const tree: IDisplayMenuItem[] = [];diff --git a/packages/ui-plugin-sheets/src/Controller/menu/border.menu.ts b/packages/ui-plugin-sheets/src/Controller/menu/border.menu.tsindex 5f06a4d998..42e237ab3f 100644--- a/packages/ui-plugin-sheets/src/Controller/menu/border.menu.ts+++ b/packages/ui-plugin-sheets/src/Controller/menu/border.menu.ts@@ -137,8 +137,7 @@ export function SetBorderColorMenuItemFactory(accessor: IAccessor): IMenuSelectoreturn {id: SetBorderColorCommand.id,title: 'border',- positions: [MenuPosition.TOOLBAR],- parentId: SetBorderPositionCommand.id,+ positions: SetBorderPositionCommand.id,display: DisplayTypes.COLOR,selectType: SelectTypes.NEO,type: MenuItemType.SELECTOR,@@ -151,7 +150,6 @@ export function SetBorderColorMenuItemFactory(accessor: IAccessor): IMenuSelecto};}-// TODO@wzhudev: fix UI problemsexport function SetBorderStyleMenuItemFactory(accessor: IAccessor): IMenuSelectorItem<BorderStyleTypes> {const permissionService = accessor.get(IPermissionService);const commandService = accessor.get(ICommandService);@@ -161,8 +159,7 @@ export function SetBorderStyleMenuItemFactory(accessor: IAccessor): IMenuSelectoid: SetBorderStyleCommand.id,title: 'borderLine.borderSize',label: SHEET_UI_PLUGIN_NAME + LineBold.name,- positions: [MenuPosition.TOOLBAR],- parentId: SetBorderPositionCommand.id,+ positions: SetBorderPositionCommand.id,display: DisplayTypes.CUSTOM,selectType: SelectTypes.NEO,type: MenuItemType.SELECTOR,diff --git a/packages/ui-plugin-sheets/src/View/RightMenu/RightMenu.tsx b/packages/ui-plugin-sheets/src/View/RightMenu/RightMenu.tsxindex 8684472152..3d6d3573b0 100644--- a/packages/ui-plugin-sheets/src/View/RightMenu/RightMenu.tsx+++ b/packages/ui-plugin-sheets/src/View/RightMenu/RightMenu.tsx@@ -1,9 +1,8 @@import { IMouseEvent } from '@univerjs/base-render';-import { BaseRightMenuProps, IDisplayMenuItem, IMenuItem, Menu } from '@univerjs/base-ui';+import { BaseRightMenuProps, IDisplayMenuItem, IMenuItem, Menu, MenuPosition } from '@univerjs/base-ui';import { Component, createRef } from 'preact';import { RightMenuProps } from '../../Controller';import Style from './index.module.less';-import { RightMenuItem } from './RightMenuItem';interface IState {visible: boolean;@@ -142,10 +141,6 @@ export class RightMenu extends Component<BaseRightMenuProps, IState> {}render() {- // if (!this.state.children.length) {- // return;- // }-const wrapStyles = { ...this.props.style };const { visible } = this.state;@@ -159,7 +154,7 @@ export class RightMenu extends Component<BaseRightMenuProps, IState> {e.preventDefault();}}>- <Menu ref={this.ulRef} menuId={'right-menu'} onClick={this.handleClick}></Menu>+ <Menu ref={this.ulRef} menuId={MenuPosition.CONTEXT_MENU} onClick={this.handleClick}></Menu></div>));diff --git a/packages/ui-plugin-sheets/src/View/SheetBar/SheetBar.tsx b/packages/ui-plugin-sheets/src/View/SheetBar/SheetBar.tsxindex 7811c64157..a50f555ceb 100644--- a/packages/ui-plugin-sheets/src/View/SheetBar/SheetBar.tsx+++ b/packages/ui-plugin-sheets/src/View/SheetBar/SheetBar.tsx@@ -9,7 +9,7 @@ type SheetState = {sheetList: BaseSheetBarProps[];menuList: BaseSheetBarProps[];sheetUl: BaseMenuItem[];- menuItems: IDisplayMenuItem[];+ menuItems: Array<IDisplayMenuItem<IMenuItem>>;};export class SheetBar extends Component<BaseSheetBarProps, SheetState> {diff --git a/packages/ui-plugin-sheets/src/View/Toolbar/Toolbar.tsx b/packages/ui-plugin-sheets/src/View/Toolbar/Toolbar.tsxindex 9a66b57d20..4c2e9a9e15 100644--- a/packages/ui-plugin-sheets/src/View/Toolbar/Toolbar.tsx+++ b/packages/ui-plugin-sheets/src/View/Toolbar/Toolbar.tsx@@ -1,4 +1,4 @@-import { AppContext, BaseComponentProps, Button, Container, CustomLabel, debounce, IDisplayMenuItem, Select, Tooltip } from '@univerjs/base-ui';+import { AppContext, BaseComponentProps, Button, Container, CustomLabel, debounce, IDisplayMenuItem, IMenuItem, Select, Tooltip } from '@univerjs/base-ui';import { Component, createRef } from 'preact';import { IToolbarItemProps, SheetContainerUIController } from '../../Controller';import styles from './index.module.less';@@ -150,7 +150,7 @@ export class Toolbar extends Component<IProps, IState> {}};- setToolbarNeo = (menuItems: IDisplayMenuItem[]) => {+ setToolbarNeo = (menuItems: Array<IDisplayMenuItem<IMenuItem>>) => {this.setState({menuItems,