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): remove command service in menu component (#140)

Dushusir7efa1f518cbf
diff --git a/packages/base-ui/src/Common/DragManager.ts b/packages/base-ui/src/Common/DragManager.ts
index 510857102b..a48a8de8dc 100644
--- a/packages/base-ui/src/Common/DragManager.ts
+++ b/packages/base-ui/src/Common/DragManager.ts
@@ -5,6 +5,9 @@ import { Inject } from '@wendellhu/redi';
// onKeyDownObservable: Observable<DragEvent>;
// };
+/**
+ * @deprecated
+ */
export class DragManager {
constructor(@Inject(ObserverManager) private readonly _observerManager: ObserverManager) {
this._installObserver();
diff --git a/packages/base-ui/src/Components/Dropdown/Dropdown.tsx b/packages/base-ui/src/Components/Dropdown/Dropdown.tsx
index b893259658..ec5d90344e 100644
--- a/packages/base-ui/src/Components/Dropdown/Dropdown.tsx
+++ b/packages/base-ui/src/Components/Dropdown/Dropdown.tsx
@@ -189,7 +189,8 @@ export const Dropdown = (props: BaseDropdownProps) => {
style={{ ...props.menu.style, ...menuStyle }}
onOptionSelect={(v) => {
props.menu.onOptionSelect?.(v);
- hideMenu();
+ // There is no need to hide the menu after selecting the border color or style.
+ !v.show && hideMenu();
}}
show={menuShow}
></Menu>
diff --git a/packages/base-ui/src/Components/Menu/Menu.tsx b/packages/base-ui/src/Components/Menu/Menu.tsx
index 0ca2ebfa35..ade17eceff 100644
--- a/packages/base-ui/src/Components/Menu/Menu.tsx
+++ b/packages/base-ui/src/Components/Menu/Menu.tsx
@@ -342,7 +342,7 @@
// }
// }
// }
-import { ICommandService, isRealNum } from '@univerjs/core';
+import { isRealNum } from '@univerjs/core';
import React, { useContext, useEffect, useRef, useState } from 'react';
import { Subscription } from 'rxjs';
@@ -597,8 +597,8 @@ export const Menu = (props: BaseMenuProps) => {
menuItem={item}
key={index}
index={index}
- onClick={() => {
- onOptionSelect?.({ value: item.id, label: item.title });
+ onClick={(object: Partial<IValueOption>) => {
+ onOptionSelect?.({ value: '', label: item.id, ...object });
}}
/>
))}
@@ -609,7 +609,7 @@ export const Menu = (props: BaseMenuProps) => {
export interface IMenuItemProps {
menuItem: IDisplayMenuItem<IMenuItem>;
index: number;
- onClick: () => void;
+ onClick: (params: Partial<IValueOption>) => void;
}
export interface IMenuItemState {
@@ -684,11 +684,6 @@ export function MenuItem({ menuItem, index, onClick }: IMenuItemProps) {
}, [menuItem]);
const renderButtonType = () => {
- const { injector } = context;
- const commandService = injector?.get(ICommandService);
- if (!commandService) {
- throw new Error('commandService is not defined');
- }
const item = menuItem as IDisplayMenuItem<IMenuButtonItem>;
const { title, display, label } = item;
@@ -697,8 +692,8 @@ export function MenuItem({ menuItem, index, onClick }: IMenuItemProps) {
className={joinClassNames(styles.colsMenuitem, disabled ? styles.colsMenuitemDisabled : '')}
// disabled={disabled} // FIXME disabled is not working
onClick={() => {
- commandService.executeCommand(item.id, { value });
- onClick();
+ // commandService.executeCommand(item.id, { value });// move to business components
+ onClick({ value, id: item.id }); // merge cell
}}
>
<NeoCustomLabel
@@ -708,8 +703,6 @@ export function MenuItem({ menuItem, index, onClick }: IMenuItemProps) {
label={label}
onChange={(v) => {
onChange(v);
- // commandService.executeCommand(item.id, { value });
- // onClick();
}}
></NeoCustomLabel>
</li>
@@ -718,11 +711,6 @@ export function MenuItem({ menuItem, index, onClick }: IMenuItemProps) {
const renderSelectorType = () => {
const item = menuItem as IDisplayMenuItem<IMenuSelectorItem<unknown>>;
- const commandService = context.injector?.get(ICommandService);
-
- if (!commandService) {
- throw new Error('commandService is not defined');
- }
return (
<li
@@ -737,8 +725,9 @@ export function MenuItem({ menuItem, index, onClick }: IMenuItemProps) {
<Menu
show={itemShow}
onOptionSelect={(v) => {
- commandService.executeCommand(item.id, { value: v.value });
- // onClick(); // border style don't trigger hide menu
+ // commandService.executeCommand(item.id, { value: v.value });
+ onClick({ value: v.value, id: item.id, show: true }); // border style don't trigger hide menu, set show true
+ setItemShow(false); // hide current menu
}}
menuId={item.id}
options={item.selections}
diff --git a/packages/base-ui/src/Components/Select/Select.tsx b/packages/base-ui/src/Components/Select/Select.tsx
index 6df2751c0a..4178701e41 100644
--- a/packages/base-ui/src/Components/Select/Select.tsx
+++ b/packages/base-ui/src/Components/Select/Select.tsx
@@ -667,7 +667,7 @@ export function Select(props: BaseSelectProps) {
};
const onOptionSelect = (option: IValueOption) => {
- onClick?.(option.value);
+ onClick?.(option);
};
const iconToDisplay = options?.find((o) => o.value === value)?.icon ?? icon;
@@ -677,7 +677,7 @@ export function Select(props: BaseSelectProps) {
<div className={`${styles.selectDouble}`}>
<Dropdown
tooltip={tooltip}
- onClick={type === SelectTypes.NEO ? () => onClick?.(value) : onClick}
+ onClick={type === SelectTypes.NEO ? () => onClick?.({ value }) : onClick}
menu={{
menuId: id,
options,
diff --git a/packages/base-ui/src/services/menu/menu.ts b/packages/base-ui/src/services/menu/menu.ts
index b1c614b430..715a4734c1 100644
--- a/packages/base-ui/src/services/menu/menu.ts
+++ b/packages/base-ui/src/services/menu/menu.ts
@@ -77,6 +77,8 @@ export interface IValueOption {
tooltip?: string;
style?: object;
disabled?: boolean;
+ id?: string; // command id
+ show?: boolean; // show parent menu or not
}
export function isValueOptions(v: IValueOption | ICustomComponentOption): v is IValueOption {
diff --git a/packages/ui-plugin-sheets/src/View/RightMenu/RightMenu.tsx b/packages/ui-plugin-sheets/src/View/RightMenu/RightMenu.tsx
index 3e3241fe09..712c15a1e5 100644
--- a/packages/ui-plugin-sheets/src/View/RightMenu/RightMenu.tsx
+++ b/packages/ui-plugin-sheets/src/View/RightMenu/RightMenu.tsx
@@ -1,5 +1,6 @@
import { IMouseEvent } from '@univerjs/base-render';
-import { BaseComponentProps, IDisplayMenuItem, IMenuItem, Menu, MenuPosition } from '@univerjs/base-ui';
+import { AppContext, BaseComponentProps, IDisplayMenuItem, IMenuItem, Menu, MenuPosition } from '@univerjs/base-ui';
+import { ICommandService, IKeyValue } from '@univerjs/core';
import { Component, createRef } from 'react';
import { RightMenuProps } from '../../Controller';
@@ -24,6 +25,8 @@ interface IState {
}
export class RightMenu extends Component<BaseRightMenuProps, IState> {
+ static override contextType = AppContext;
+
root = createRef<HTMLDivElement>();
constructor(props: BaseRightMenuProps) {
@@ -132,7 +135,10 @@ export class RightMenu extends Component<BaseRightMenuProps, IState> {
menuId={MenuPosition.CONTEXT_MENU}
onClick={this.handleClick}
clientPosition={clientPosition}
- onOptionSelect={() => {
+ onOptionSelect={(params) => {
+ const { label: commandId, value } = params;
+ const commandService: ICommandService = (this.context as IKeyValue).injector.get(ICommandService);
+ commandService.executeCommand(commandId as string, { value });
this.showRightMenu(false);
}}
></Menu>
diff --git a/packages/ui-plugin-sheets/src/View/SheetBar/SheetBar.tsx b/packages/ui-plugin-sheets/src/View/SheetBar/SheetBar.tsx
index 1a79fec31f..841bb230a4 100644
--- a/packages/ui-plugin-sheets/src/View/SheetBar/SheetBar.tsx
+++ b/packages/ui-plugin-sheets/src/View/SheetBar/SheetBar.tsx
@@ -311,7 +311,18 @@ export class SheetBar extends Component<BaseSheetBarProps, SheetState> {
</div>
{/* mouse right button context menu */}
- <Menu className={styles.sheetUl} menu={sheetUl} menuId={MenuPosition.SHEET_BAR} show={showMenu} style={menuStyle} />
+ <Menu
+ className={styles.sheetUl}
+ menu={sheetUl}
+ menuId={MenuPosition.SHEET_BAR}
+ show={showMenu}
+ style={menuStyle}
+ onOptionSelect={(params) => {
+ const { label: commandId } = params;
+ const commandService: ICommandService = (this.context as IKeyValue).injector.get(ICommandService);
+ commandService.executeCommand(commandId as string);
+ }}
+ />
{/* prev next scroll button */}
<div className={`${styles.sheetBarOptions} ${styles.sheetBarScrollButton}`}>
diff --git a/packages/ui-plugin-sheets/src/View/Toolbar/ToolbarItem.tsx b/packages/ui-plugin-sheets/src/View/Toolbar/ToolbarItem.tsx
index 7a5b54fe98..654cdd47d1 100644
--- a/packages/ui-plugin-sheets/src/View/Toolbar/ToolbarItem.tsx
+++ b/packages/ui-plugin-sheets/src/View/Toolbar/ToolbarItem.tsx
@@ -1,4 +1,17 @@
-import { AppContext, Button, CustomLabel, IDisplayMenuItem, IMenuButtonItem, IMenuItem, IMenuSelectorItem, IValueOption, MenuItemType, Select, Tooltip } from '@univerjs/base-ui';
+import {
+ AppContext,
+ Button,
+ ButtonType,
+ CustomLabel,
+ IDisplayMenuItem,
+ IMenuButtonItem,
+ IMenuItem,
+ IMenuSelectorItem,
+ IValueOption,
+ MenuItemType,
+ Select,
+ Tooltip,
+} from '@univerjs/base-ui';
import { ICommandService, IKeyValue } from '@univerjs/core';
import { Component } from 'react';
import { Subscription } from 'rxjs';
@@ -87,7 +100,9 @@ export class ToolbarItem extends Component<IDisplayMenuItem<IMenuItem>, IToolbar
icon={icon}
value={value}
label={value ?? label} // TODO: this line is strange
- onClick={(value) => commandService.executeCommand(id, { value })} // TODO@wzhudev: should be merged to a single API on value change
+ onClick={({ value, id: selectorId }) => {
+ commandService.executeCommand(selectorId ?? id, { value });
+ }} // TODO@wzhudev: should be merged to a single API on value change
onPressEnter={(value) => commandService.executeCommand(id, { value })}
type={selectType!}
></Select>
@@ -103,7 +118,7 @@ export class ToolbarItem extends Component<IDisplayMenuItem<IMenuItem>, IToolbar
return (
<Tooltip title={this.getTooltip()} placement="bottom">
- <Button active={activated} className={styles.textButton} type="text" disabled={disabled} onClick={() => commandService.executeCommand(props.id)}>
+ <Button active={activated} className={styles.textButton} type={'text' as ButtonType} disabled={disabled} onClick={() => commandService.executeCommand(props.id)}>
<CustomLabel label={icon ? { name: icon } : title} />
</Button>
</Tooltip>