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: refactor menu component with submenu options

wzhudev24338a7535c0
diff --git a/packages/base-sheets/src/Commands/Commands/set-style.command.ts b/packages/base-sheets/src/Commands/Commands/set-style.command.ts
index 8386d1a6f5..b6dad539f2 100644
--- a/packages/base-sheets/src/Commands/Commands/set-style.command.ts
+++ b/packages/base-sheets/src/Commands/Commands/set-style.command.ts
@@ -274,6 +274,24 @@ export const SetTextColorCommand: ICommand<ISetColorCommandParams> = {
},
};
+export const ResetTextColorCommand: ICommand = {
+ type: CommandType.COMMAND,
+ id: 'sheet.command.reset-text-color',
+ handler: async (accessor) => {
+ const commandService = accessor.get(ICommandService);
+ const setStyleParams: ISetStyleParams<IColorStyle> = {
+ style: {
+ type: 'cl',
+ value: {
+ rgb: '#000',
+ },
+ },
+ };
+
+ return commandService.executeCommand(SetStyleCommand.id, setStyleParams);
+ },
+};
+
export const SetBackgroundColorCommand: ICommand<ISetColorCommandParams> = {
type: CommandType.COMMAND,
id: 'sheet.command.set-background-color',
diff --git a/packages/base-sheets/src/Controller/BasicWorksheet.controller.ts b/packages/base-sheets/src/Controller/BasicWorksheet.controller.ts
index ca562e400d..84a5f43b2e 100644
--- a/packages/base-sheets/src/Controller/BasicWorksheet.controller.ts
+++ b/packages/base-sheets/src/Controller/BasicWorksheet.controller.ts
@@ -9,6 +9,7 @@ import { SetWorksheetActivateCommand } from '../Commands/Commands/set-worksheet-
import { SetWorksheetActivateMutation } from '../Commands/Mutations/set-worksheet-activate.mutation';
import {
ISetStyleParams,
+ ResetTextColorCommand,
SetBackgroundColorCommand,
SetCellBorderCommand,
SetStyleCommand,
@@ -104,6 +105,7 @@ export class BasicWorksheetController extends Disposable implements IDisposable
SetCellBorderCommand,
SetTextWrapCommand,
SetTextRotationCommand,
+ ResetTextColorCommand,
].forEach((command) => this.disposeWithMe(this._commandService.registerCommand(command)));
}
diff --git a/packages/base-sheets/src/index.ts b/packages/base-sheets/src/index.ts
index 9317510a65..219358cb02 100644
--- a/packages/base-sheets/src/index.ts
+++ b/packages/base-sheets/src/index.ts
@@ -32,6 +32,7 @@ export {
SetSpanCommand,
SetTextWrapCommand,
SetVerticalTextAlignCommand,
+ ResetTextColorCommand,
} from './Commands/Commands/set-style.command';
export { InsertSheetCommand } from './Commands/Commands/insert-sheet.command';
export { ClearSelectionContentCommand } from './Commands/Commands/clear-selection-content.command';
diff --git a/packages/base-ui/src/Components/CustomLabel/CustomLabel.module.less b/packages/base-ui/src/Components/CustomLabel/CustomLabel.module.less
index 41556b46dd..9b3283e96a 100644
--- a/packages/base-ui/src/Components/CustomLabel/CustomLabel.module.less
+++ b/packages/base-ui/src/Components/CustomLabel/CustomLabel.module.less
@@ -9,4 +9,11 @@
left: 50%;
transform: translate(-50%, 0);
}
+}
+
+.font-select {
+ width: 120px;
+ text-overflow: ellipsis;
+ overflow: hidden;
+ white-space: nowrap;
}
\ No newline at end of file
diff --git a/packages/base-ui/src/Components/CustomLabel/CustomLabel.tsx b/packages/base-ui/src/Components/CustomLabel/CustomLabel.tsx
index 02f39e8af6..887d73663c 100644
--- a/packages/base-ui/src/Components/CustomLabel/CustomLabel.tsx
+++ b/packages/base-ui/src/Components/CustomLabel/CustomLabel.tsx
@@ -1,12 +1,47 @@
import { Component, isValidElement, JSX } from 'preact';
import { useContext } from 'preact/hooks';
-import { AppContext, ICustomComponent } from '../../Common';
+import { AppContext, AppContextValues, ICustomComponent } from '../../Common';
import { IBaseCustomLabelProps } from '../../Interfaces';
import { DisplayTypes } from '../Select';
import styles from './CustomLabel.module.less';
+import { IMenuSelectorItem } from '../../services/menu/menu.service';
+function getLocale(context: Partial<AppContextValues>, name: string) {
+ return context.localeService?.t(name);
+}
+
+export interface INeoCustomLabelProps<T> {
+ value: T;
+ onChange?(v: T): void;
+}
+
+export function NeoCustomLabel<T>(props: Pick<IMenuSelectorItem<unknown>, 'icon' | 'display' | 'title'> & INeoCustomLabelProps<T>): JSX.Element | null {
+ const context = useContext(AppContext);
+ const { display, value, title, icon } = props;
+
+ if (display === DisplayTypes.COLOR) {
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ return <ColorSelect value={value as any} title={title} icon={icon} />;
+ }
+
+ if (display === DisplayTypes.FONT) {
+ return (
+ <div className={styles.fontSelect} style={{ fontFamily: value as string }}>
+ {getLocale(context, value as string)}
+ </div>
+ );
+ }
+
+ if (display === DisplayTypes.INPUT) {
+ // TODO: implement display input
+ }
+
+ return <>{value}</>;
+}
+
+/** @deprecated */
export function CustomLabel(props: IBaseCustomLabelProps): JSX.Element | null {
const context = useContext(AppContext);
const { label, display } = props;
@@ -59,4 +94,4 @@ export class ColorSelect extends Component<IColorSelectProps> {
</div>
);
}
-}
\ No newline at end of file
+}
diff --git a/packages/base-ui/src/Components/Dropdown/Dropdown.tsx b/packages/base-ui/src/Components/Dropdown/Dropdown.tsx
index 2206601a15..cf32ec823a 100644
--- a/packages/base-ui/src/Components/Dropdown/Dropdown.tsx
+++ b/packages/base-ui/src/Components/Dropdown/Dropdown.tsx
@@ -1,4 +1,4 @@
-import { createRef } from 'preact';
+import { JSX, createRef } from 'preact';
import { PureComponent } from 'preact/compat';
import { Icon } from '..';
import { JSXComponent } from '../../BaseComponent';
@@ -46,7 +46,7 @@ export class Dropdown extends PureComponent<BaseDropdownProps, IState> {
}
};
- componentDidMount() {
+ override componentDidMount() {
const { placement } = this.props;
const style: Record<string, string | number> = { position: 'absolute' };
if (!placement || placement === 'Bottom') {
@@ -68,12 +68,12 @@ export class Dropdown extends PureComponent<BaseDropdownProps, IState> {
window.addEventListener('click', this.hideMenuClick, true);
}
- componentWillUnmount() {
+ override componentWillUnmount() {
window.removeEventListener('click', this.hideMenuClick, true);
}
render() {
- const { children, menu, showArrow, icon, tooltip } = this.props;
+ const { children, menu, showArrow, icon, tooltip, content } = this.props;
const { menuStyle } = this.state;
return (
@@ -84,14 +84,20 @@ export class Dropdown extends PureComponent<BaseDropdownProps, IState> {
{showArrow ? <Icon.Format.NextIcon /> : ''}
</div>
</Tooltip>
- {icon ? (
+ {icon && (
<div ref={this.IconRef} className={styles.dropIcon} onClick={this.handleSubClick}>
{icon}
</div>
- ) : (
- ''
)}
- <Menu onClick={menu.onClick} ref={this.MenuRef} menu={menu.menu} className={menu.className} style={{ ...menu.style, ...menuStyle }}></Menu>
+ {content}
+ <Menu
+ onClick={menu.onClick}
+ ref={this.MenuRef}
+ menu={menu.menu}
+ menuItems={menu.menuItems}
+ className={menu.className}
+ style={{ ...menu.style, ...menuStyle }}
+ ></Menu>
</div>
);
}
diff --git a/packages/base-ui/src/Components/Menu/Menu.tsx b/packages/base-ui/src/Components/Menu/Menu.tsx
index 1a1fa83bf4..69ec99d9b0 100644
--- a/packages/base-ui/src/Components/Menu/Menu.tsx
+++ b/packages/base-ui/src/Components/Menu/Menu.tsx
@@ -6,7 +6,7 @@ import { joinClassNames } from '../../Utils';
import { CustomLabel } from '../CustomLabel';
import styles from './index.module.less';
import { IDisplayMenuItem } from '../../services/menu/menu.service';
-import AppContext from '../../Common/AppContext';
+import { AppContext } from '../../Common/AppContext';
export class Menu extends Component<BaseMenuProps, BaseMenuState> {
private _MenuRef = createRef<HTMLUListElement>();
@@ -90,20 +90,6 @@ export class Menu extends Component<BaseMenuProps, BaseMenuState> {
posStyle: style,
});
}
- // else {
- // if (curPosition.width > docPosition!.right) {
- // // 边界碰到univer-main-content右边界或者小于视口
- // style.right = '0';
- // style.left = 'auto';
- // } else {
- // style.left = '0';
- // style.right = 'auto';
- // }
- // if (curPosition.top >= docPosition.bottom) {
- // style.top = 'auto';
- // style.bottom = '100%';
- // }
- // }
};
render() {
@@ -113,6 +99,7 @@ export class Menu extends Component<BaseMenuProps, BaseMenuState> {
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 */}
{menu?.map((item: BaseMenuItem, index: number) => {
if (item.show === false) return;
return (
@@ -147,6 +134,7 @@ export class Menu extends Component<BaseMenuProps, BaseMenuState> {
</>
);
})}
+ {/* render submenus */}
{menuItems?.map((item, index) => (
<MenuItem menuItem={item} index={index} onClick={() => this.showMenu(false)} />
))}
@@ -162,7 +150,7 @@ export class Menu extends Component<BaseMenuProps, BaseMenuState> {
}
}
-export class MenuItem extends Component<{ menuItem: IDisplayMenuItem; index: number; onClick: () => void }, { disabled: boolean }> {
+export class MenuItem extends Component<{ menuItem: IDisplayMenuItem<any>; index: number; onClick: () => void }, { disabled: boolean }> {
static override contextType = AppContext;
private disabledSubscription: Subscription | undefined;
@@ -178,7 +166,7 @@ export class MenuItem extends Component<{ menuItem: IDisplayMenuItem; index: num
}
override componentDidMount(): void {
- this.disabledSubscription = this.props.menuItem.disabled$?.subscribe((disabled) => {
+ this.disabledSubscription = this.props.menuItem.disabled$?.subscribe((disabled: boolean) => {
this.setState({ disabled });
});
}
diff --git a/packages/base-ui/src/Components/Select/Select.tsx b/packages/base-ui/src/Components/Select/Select.tsx
index a569f11ec8..0f6f40128c 100644
--- a/packages/base-ui/src/Components/Select/Select.tsx
+++ b/packages/base-ui/src/Components/Select/Select.tsx
@@ -1,13 +1,13 @@
import { ComponentChildren, createRef, RefObject } from 'preact';
import { PureComponent } from 'preact/compat';
-import { ICustomComponent } from '../../Common';
+import { AppContext, ICustomComponent } from '../../Common';
import { BaseMenuItem } from '../../Interfaces';
import { Dropdown } from '../Dropdown';
import { Input } from '../Input';
import { BaseItemProps, Item } from '../Item/Item';
-import { CustomLabel } from '../CustomLabel';
+import { CustomLabel, NeoCustomLabel } from '../CustomLabel';
import styles from './index.module.less';
-import { Icon } from '../..'; // FIXME: strange import
+import { Icon, IDisplayMenuItem, IMenuItem, IMenuService } from '../..'; // FIXME: strange import
// TODO: these type definitions should be moved out of components to menu service
@@ -19,12 +19,25 @@ export enum SelectTypes {
DOUBLE,
FIX,
DOUBLEFIX,
+
+ /** This should be the only type. The enum would be removed after we finish refactor. */
+ NEO,
}
export enum DisplayTypes {
LABEL,
+
+ /** @deprecated */
SUFFIX,
+
+ ICON,
+
+ /** Label as color display. */
COLOR,
+
+ INPUT,
+
+ FONT,
}
export interface BaseSelectChildrenProps extends BaseItemProps {
@@ -52,20 +65,29 @@ export interface BaseSelectProps {
tooltip?: string;
value?: string | number;
icon?: string;
+ id?: string;
+ title?: string;
}
interface IState {
menu: BaseMenuItem[];
- color: string;
+ color: string; // TODO@wzhudev: this state is strange
content: ComponentChildren;
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
value: any;
+ menuItems: Array<IDisplayMenuItem<IMenuItem>>;
}
export class Select extends PureComponent<BaseSelectProps, IState> {
+ static override contextType = AppContext;
+
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
ColorRef: any;
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
onClick: (...arg: any) => void;
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
onPressEnter: (...arg: any[]) => void;
constructor(props: BaseSelectProps) {
@@ -82,6 +104,7 @@ export class Select extends PureComponent<BaseSelectProps, IState> {
color: this.props.defaultColor ?? '#000',
content: '',
value: '',
+ menuItems: [],
};
}
@@ -97,6 +120,8 @@ export class Select extends PureComponent<BaseSelectProps, IState> {
return this.getFix();
case SelectTypes.DOUBLEFIX:
return this.getDoubleFix();
+ case SelectTypes.NEO:
+ return this.renderNeo();
}
}
@@ -104,6 +129,8 @@ export class Select extends PureComponent<BaseSelectProps, IState> {
initData() {
const { type, display, children = [], label } = this.props;
+ // TODO@wzhudev: the way of binding onClick is ridiculous. Fix it.
+
// 显示值可变
if (type === 0 || type === 3) {
if (!children.length) return;
@@ -236,16 +263,20 @@ export class Select extends PureComponent<BaseSelectProps, IState> {
override componentDidMount() {
this.initData();
+ this.getSubMenus();
}
override componentWillReceiveProps(nextProps: BaseSelectProps) {
this.updateData();
- // this.props = Object.assign(this.props, nextProps);
- // this.initData();
+
+ if (this.props.id !== nextProps.id && nextProps.id) {
+ this.getSubMenus();
+ }
}
resetMenu(children: BaseSelectChildrenProps[], hideSelectedIcon?: boolean) {
const list: BaseMenuItem[] = [];
+ // TODO@wzhudev: ID prop is not handled
for (let i = 0; i < children.length; i++) {
let selected = false;
if (!hideSelectedIcon) {
@@ -358,11 +389,34 @@ export class Select extends PureComponent<BaseSelectProps, IState> {
getDoubleFix = () => {
const { label, className = '', tooltip, onClick, display, value, icon } = this.props;
const { menu } = this.state;
+
return (
<div className={`${styles.selectDouble} ${className}`}>
<Dropdown tooltip={tooltip} onClick={onClick} menu={{ menu, onClick: this.onClick }} icon={<Icon.NextIcon />}>
<div className={styles.selectLabel}>
- <CustomLabel display={display} label={label} value={value} icon={icon} />
+ <CustomLabel display={display} label={label} value={`${value}`} icon={icon} />
+ </div>
+ </Dropdown>
+ </div>
+ );
+ };
+
+ renderNeo = () => {
+ const { tooltip, onClick, display, value, icon, title, id } = this.props;
+ const { menuItems } = this.state;
+
+ const onClickInner = (...args: unknown[]) => {
+ onClick?.(args[1] as number | string);
+ };
+
+ return (
+ <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 />}>
+ {/* 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)} />
</div>
</Dropdown>
</div>
@@ -374,4 +428,14 @@ export class Select extends PureComponent<BaseSelectProps, IState> {
return <div className={styles.selectComponent}>{this.getType(type)}</div>;
}
-}
\ No newline at end of file
+
+ private getSubMenus() {
+ const { id } = this.props;
+ if (id) {
+ const menuService: IMenuService = this.context.injector.get(IMenuService);
+ this.setState({
+ menuItems: menuService.getSubMenuItems(id),
+ });
+ }
+ }
+}
diff --git a/packages/base-ui/src/Interfaces/Dropdown.ts b/packages/base-ui/src/Interfaces/Dropdown.ts
index 5e37eb6de8..4fd0d1f657 100644
--- a/packages/base-ui/src/Interfaces/Dropdown.ts
+++ b/packages/base-ui/src/Interfaces/Dropdown.ts
@@ -4,6 +4,7 @@ import { BaseMenuProps } from './Menu';
export interface BaseDropdownProps {
children: ComponentChildren;
+ /** @deprecated dropdown shouldn't know what is inside */
menu: BaseMenuProps;
placement?: 'Left' | 'Right' | 'Top' | 'Bottom';
showArrow?: boolean;
@@ -11,6 +12,7 @@ export interface BaseDropdownProps {
onClick?: () => void;
onMainClick?: () => void; //非功能按钮事件
tooltip?: string;
+ content?: ComponentChildren;
}
export interface DropdownComponent extends BaseComponent<BaseDropdownProps> {
diff --git a/packages/base-ui/src/Interfaces/Menu.ts b/packages/base-ui/src/Interfaces/Menu.ts
index a1764e9621..00aa25abf6 100644
--- a/packages/base-ui/src/Interfaces/Menu.ts
+++ b/packages/base-ui/src/Interfaces/Menu.ts
@@ -16,8 +16,12 @@ export interface BaseMenuItem {
export interface BaseMenuProps {
/** @deprecated this is legacy menu mechanism. Do not use it. Use `menuItems` instead. */
+ parentId?: string;
menu?: BaseMenuItem[];
- menuItems?: IDisplayMenuItem[];
+
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ menuItems?: Array<IDisplayMenuItem<any>>;
+
onClick?: (...arg: any) => void;
className?: string;
style?: JSX.CSSProperties;
diff --git a/packages/base-ui/src/services/menu/menu.service.ts b/packages/base-ui/src/services/menu/menu.service.ts
index 85b95e273b..0c234cd8b0 100644
--- a/packages/base-ui/src/services/menu/menu.service.ts
+++ b/packages/base-ui/src/services/menu/menu.service.ts
@@ -6,11 +6,12 @@ import { Disposable, toDisposable } from '@univerjs/core';
import { IShortcutService } from '../shortcut/shortcut.service';
import { ICustomLabelType } from '../../Interfaces/CustomLabel';
-import { BaseSelectProps, DisplayTypes, SelectTypes } from '../../Components/Select/Select';
+import { DisplayTypes, SelectTypes } from '../../Components/Select/Select';
export type OneOrMany<T> = T | T[];
export const enum MenuPosition {
+ VOID,
TOOLBAR,
CONTEXT_MENU,
TAB_CONTEXT_MENU,
@@ -19,43 +20,74 @@ export const enum MenuPosition {
}
export const enum MenuItemType {
- /** default */
+ /** 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;
- label?: string | ICustomLabelType | ComponentChildren;
- menu: OneOrMany<MenuPosition>;
- title: string;
tooltip?: string;
- type?: MenuItemType;
- /** @deprecated should avoid using this */
+ 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;
- /** @deprecated */
- subMenus?: string[]; // submenu id list
parentId?: string; // if it is submenu
hidden$?: Observable<boolean>;
- activated$?: Observable<boolean>;
disabled$?: Observable<boolean>;
}
-export interface IMenuButtonItem extends IMenuItemBase {}
+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 extends IMenuItemBase {
+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?: Array<number | BaseSelectProps>;
+
+ // 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<unknown>; // TODO@wzhudev: it could be a generic type. optional for now. But it should be required.
+ value$?: Observable<V>;
/** @deprecated */
label?: {
@@ -64,13 +96,15 @@ export interface IMenuSelectorItem extends IMenuItemBase {
};
}
-export type IMenuItem = IMenuButtonItem | IMenuSelectorItem;
-
-// TODO@wzhudev: maybe we should separate `IMenuItemState` to different types of menu items.
+export type IMenuItem = IMenuButtonItem | IMenuSelectorItem<unknown>;
+/**
+ * @internal
+ */
export type IDisplayMenuItem<T extends IMenuItem> = T & {
- /** MenuService should get responsible shortcut and display on the UI. */
+ /** MenuService get responsible shortcut and display on the UI. */
shortcut?: string;
+
/** Composed menu structure by the menu service. */
subMenuItems?: Array<IDisplayMenuItem<IMenuItem>>; // TODO@wzhudev: related mechanism is not implemented yet
};
@@ -82,6 +116,7 @@ export interface IMenuService {
/** Get menu items for display at a given position. */
getMenuItems(position: MenuPosition): Array<IDisplayMenuItem<IMenuItem>>;
+ getSubMenuItems(parentId: string): Array<IDisplayMenuItem<IMenuItem>>;
getMenuItem(id: string): IMenuItem | null;
}
@@ -90,6 +125,9 @@ 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[]>();
+
constructor(@IShortcutService private readonly _shortcutService: IShortcutService) {
super();
}
@@ -104,30 +142,55 @@ export class DesktopMenuService extends Disposable implements IMenuService {
}
this._menuItemMap.set(item.id, item);
- if (Array.isArray(item.menu)) {
- item.menu.forEach((menu) => this.appendMenuToPosition(item, menu));
+
+ if (Array.isArray(item.positions)) {
+ item.positions.forEach((menu) => this.appendMenuToPosition(item, menu));
} else {
- this.appendMenuToPosition(item, item.menu);
+ 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.menu)) {
- item.menu.forEach((menu) => this._menuByPositions.get(menu)?.delete(item.id));
+
+ if (Array.isArray(item.positions)) {
+ item.positions.forEach((menu) => this._menuByPositions.get(menu)?.delete(item.id));
} else {
- this._menuByPositions.get(item.menu)?.delete(item.id);
+ this._menuByPositions.get(item.positions)?.delete(item.id);
+ }
+
+ if (item.parentId) {
+ const children = this._menuByParentId.get(item.parentId);
+ if (children) {
+ const index = children.findIndex((id) => id === item.id);
+ if (index > -1) {
+ children.splice(index, 1);
+ }
+ }
}
});
}
getMenuItems(positions: MenuPosition): Array<IDisplayMenuItem<IMenuItem>> {
if (this._menuByPositions.has(positions)) {
- return [...this._menuByPositions.get(positions)!.values()].map((menu) => this.getDisplayMenuItems(menu));
+ return [...this._menuByPositions.get(positions)!.values()].filter((menu) => !menu.parentId).map((menu) => this.getDisplayMenuItems(menu));
}
return [] as Array<IDisplayMenuItem<any>>;
}
+ 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)!;
diff --git a/packages/ui-plugin-sheets/src/Controller/ToolbarUIController.ts b/packages/ui-plugin-sheets/src/Controller/ToolbarUIController.ts
index 72da2fc516..a3c9eb785e 100644
--- a/packages/ui-plugin-sheets/src/Controller/ToolbarUIController.ts
+++ b/packages/ui-plugin-sheets/src/Controller/ToolbarUIController.ts
@@ -6,23 +6,7 @@ import { ComponentChildren } from 'preact';
import { SheetToolbarConfig, SHEET_UI_PLUGIN_NAME } from '../Basics';
import { ColorSelect, LineBold, LineColor, Toolbar } from '../View';
-import {
- BackgroundColorSelectorMenuItemFactory,
- BoldMenuItemFactory,
- CellBorderSelectorMenuItemFactory,
- TextColorSelectorMenuItemFactory,
- FontFamilySelectorMenuItemFactory,
- FontSizeSelectorMenuItemFactory,
- ItalicMenuItemFactory,
- RedoMenuItemFactory,
- StrikeThroughMenuItemFactory,
- TextRotateMenuItemFactory,
- UnderlineMenuItemFactory,
- UndoMenuItemFactory,
- WrapTextMenuItemFactory,
- HorizontalAlignMenuItemFactory,
- VerticalAlignMenuItemFactory,
-} from './menu';
+import { ResetTextColorMenuItemFactory, TextColorSelectorMenuItemFactory } from './menu';
export interface BaseToolbarSelectProps extends BaseSelectProps {
children?: BaseSelectChildrenProps[];
@@ -118,21 +102,22 @@ 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,
- BackgroundColorSelectorMenuItemFactory,
- CellBorderSelectorMenuItemFactory,
- HorizontalAlignMenuItemFactory,
- VerticalAlignMenuItemFactory,
- WrapTextMenuItemFactory,
- TextRotateMenuItemFactory,
+ ResetTextColorMenuItemFactory,
+ // BackgroundColorSelectorMenuItemFactory,
+ // CellBorderSelectorMenuItemFactory,
+ // HorizontalAlignMenuItemFactory,
+ // VerticalAlignMenuItemFactory,
+ // WrapTextMenuItemFactory,
+ // TextRotateMenuItemFactory,
].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 e63617f125..0150e1528d 100644
--- a/packages/ui-plugin-sheets/src/Controller/menu.ts
+++ b/packages/ui-plugin-sheets/src/Controller/menu.ts
@@ -23,8 +23,9 @@ import {
SetTextWrapCommand,
SetHorizontalTextAlignCommand,
SetVerticalTextAlignCommand,
+ ResetTextColorCommand,
} from '@univerjs/base-sheets';
-import { ColorPicker, DisplayTypes, IMenuItem, IMenuSelectorItem, MenuItemType, MenuPosition, SelectTypes } from '@univerjs/base-ui';
+import { ColorPicker, DisplayTypes, IMenuButtonItem, IMenuItem, IMenuSelectorItem, MenuItemType, MenuPosition, SelectTypes } from '@univerjs/base-ui';
import { FontItalic, FontWeight, IBorderData, ICommandService, IPermissionService, IUndoRedoService, RedoCommand, UndoCommand } from '@univerjs/core';
import { IAccessor } from '@wendellhu/redi';
import { Observable } from 'rxjs';
@@ -45,42 +46,45 @@ import { SHEET_UI_PLUGIN_NAME } from '../Basics/Const/PLUGIN_NAME';
import styles from '../View/Toolbar/index.module.less';
-export function UndoMenuItemFactory(accessor: IAccessor): IMenuItem {
+export function UndoMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
const undoRedoService = accessor.get(IUndoRedoService);
return {
id: UndoCommand.id,
+ type: MenuItemType.BUTTON,
icon: 'ForwardIcon',
title: 'Undo',
- menu: [MenuPosition.TOOLBAR],
+ positions: [MenuPosition.TOOLBAR],
disabled$: undoRedoService.undoRedoStatus$.pipe(map((v) => v.undos <= 0)),
};
}
-export function RedoMenuItemFactory(accessor: IAccessor): IMenuItem {
+export function RedoMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
const undoRedoService = accessor.get(IUndoRedoService);
return {
id: RedoCommand.id,
+ type: MenuItemType.BUTTON,
icon: 'BackIcon',
title: 'Redo',
- menu: [MenuPosition.TOOLBAR],
+ positions: [MenuPosition.TOOLBAR],
disabled$: undoRedoService.undoRedoStatus$.pipe(map((v) => v.redos <= 0)),
};
}
// TODO@wzhudev: in the future we will support add rich format value to in-cell texts. Then we would make some changes to how these menu items works.
-export function BoldMenuItemFactory(accessor: IAccessor): IMenuItem {
+export function BoldMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
const commandService = accessor.get(ICommandService);
const permissionService = accessor.get(IPermissionService);
const selectionManager = accessor.get(ISelectionManager);
return {
id: SetBoldCommand.id,
+ type: MenuItemType.BUTTON,
icon: 'BoldIcon',
title: 'Set bold',
- menu: [MenuPosition.TOOLBAR],
+ positions: [MenuPosition.TOOLBAR],
disabled$: new Observable<boolean>((subscriber) => {
let editable = false;
function update() {
@@ -106,7 +110,6 @@ export function BoldMenuItemFactory(accessor: IAccessor): IMenuItem {
return;
}
- // FIXME: range is undefined when moving selection with cursor
const range = selectionManager.getCurrentCell();
const isBold = range?.getFontWeight();
@@ -119,16 +122,17 @@ export function BoldMenuItemFactory(accessor: IAccessor): IMenuItem {
};
}
-export function ItalicMenuItemFactory(accessor: IAccessor): IMenuItem {
+export function ItalicMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
const permissionService = accessor.get(IPermissionService);
const commandService = accessor.get(ICommandService);
const selectionManager = accessor.get(ISelectionManager);
return {
id: SetItalicCommand.id,
+ type: MenuItemType.BUTTON,
icon: 'ItalicIcon',
title: 'Set italic',
- menu: [MenuPosition.TOOLBAR],
+ positions: [MenuPosition.TOOLBAR],
disabled$: new Observable<boolean>((subscriber) => {
let editable = false;
function update() {
@@ -165,16 +169,17 @@ export function ItalicMenuItemFactory(accessor: IAccessor): IMenuItem {
};
}
-export function UnderlineMenuItemFactory(accessor: IAccessor): IMenuItem {
+export function UnderlineMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
const permissionService = accessor.get(IPermissionService);
const commandService = accessor.get(ICommandService);
const selectionManager = accessor.get(ISelectionManager);
return {
id: SetUnderlineCommand.id,
+ type: MenuItemType.BUTTON,
icon: 'UnderLineIcon',
title: 'Set underline',
- menu: [MenuPosition.TOOLBAR],
+ positions: [MenuPosition.TOOLBAR],
disabled$: new Observable<boolean>((subscriber) => {
let editable = false;
function update() {
@@ -211,16 +216,17 @@ export function UnderlineMenuItemFactory(accessor: IAccessor): IMenuItem {
};
}
-export function StrikeThroughMenuItemFactory(accessor: IAccessor): IMenuItem {
+export function StrikeThroughMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
const permissionService = accessor.get(IPermissionService);
const commandService = accessor.get(ICommandService);
const selectionManager = accessor.get(ISelectionManager);
return {
id: SetStrikeThroughCommand.id,
+ type: MenuItemType.BUTTON,
icon: 'DeleteLineIcon',
title: 'Set strike through',
- menu: [MenuPosition.TOOLBAR],
+ positions: [MenuPosition.TOOLBAR],
disabled$: new Observable<boolean>((subscriber) => {
let editable = false;
function update() {
@@ -257,7 +263,7 @@ export function StrikeThroughMenuItemFactory(accessor: IAccessor): IMenuItem {
};
}
-export function FontFamilySelectorMenuItemFactory(accessor: IAccessor): IMenuSelectorItem {
+export function FontFamilySelectorMenuItemFactory(accessor: IAccessor): IMenuSelectorItem<string> {
// NOTE: we could get more font options from (a) font service, so user can provide their own fonts
const permissionService = accessor.get(IPermissionService);
const commandService = accessor.get(ICommandService);
@@ -265,14 +271,15 @@ export function FontFamilySelectorMenuItemFactory(accessor: IAccessor): IMenuSel
return {
id: SetFontFamilyCommand.id,
- title: 'font',
+ title: 'toolbar.font',
tooltip: 'toolbar.font',
- selectType: SelectTypes.FIX,
+ selectType: SelectTypes.NEO,
type: MenuItemType.SELECTOR,
- menu: [MenuPosition.TOOLBAR],
+ display: DisplayTypes.FONT,
+ positions: [MenuPosition.TOOLBAR],
selections: FONT_FAMILY_CHILDREN,
className: styles.selectLabelString,
- disabled$: new Observable<boolean>((subscriber) => {
+ disabled$: new Observable((subscriber) => {
let editable = false;
function update() {
subscriber.next(!editable);
@@ -289,7 +296,7 @@ export function FontFamilySelectorMenuItemFactory(accessor: IAccessor): IMenuSel
permission$.unsubscribe();
};
}),
- value$: new Observable<string>((subscriber) => {
+ value$: new Observable((subscriber) => {
const defaultValue = FONT_FAMILY_CHILDREN[0].value;
const disposable = commandService.onCommandExecuted((c) => {
@@ -310,7 +317,7 @@ export function FontFamilySelectorMenuItemFactory(accessor: IAccessor): IMenuSel
};
}
-export function FontSizeSelectorMenuItemFactory(accessor: IAccessor): IMenuSelectorItem {
+export function FontSizeSelectorMenuItemFactory(accessor: IAccessor): IMenuSelectorItem<number> {
const permissionService = accessor.get(IPermissionService);
const commandService = accessor.get(ICommandService);
const selectionManager = accessor.get(ISelectionManager);
@@ -320,8 +327,9 @@ export function FontSizeSelectorMenuItemFactory(accessor: IAccessor): IMenuSelec
title: 'fontSize',
tooltip: 'toolbar.fontSize',
type: MenuItemType.SELECTOR,
- selectType: SelectTypes.INPUT,
- menu: [MenuPosition.TOOLBAR],
+ selectType: SelectTypes.NEO,
+ display: DisplayTypes.INPUT,
+ positions: [MenuPosition.TOOLBAR],
selections: FONT_SIZE_CHILDREN,
disabled$: new Observable<boolean>((subscriber) => {
let editable = false;
@@ -340,7 +348,7 @@ export function FontSizeSelectorMenuItemFactory(accessor: IAccessor): IMenuSelec
permission$.unsubscribe();
};
}),
- value$: new Observable<number>((subscriber) => {
+ value$: new Observable((subscriber) => {
const DEFAULT_SIZE = 14;
const disposable = commandService.onCommandExecuted((c) => {
const id = c.id;
@@ -354,38 +362,47 @@ export function FontSizeSelectorMenuItemFactory(accessor: IAccessor): IMenuSelec
subscriber.next(fs);
});
+ subscriber.next(DEFAULT_SIZE);
+
return disposable.dispose;
}),
};
}
-export function TextColorSelectorMenuItemFactory(accessor: IAccessor): IMenuSelectorItem {
+export function ResetTextColorMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
+ return {
+ id: ResetTextColorCommand.id,
+ type: MenuItemType.BUTTON,
+ title: 'toolbar.resetColor',
+ positions: [MenuPosition.TOOLBAR],
+ parentId: SetTextColorCommand.id,
+ };
+}
+
+export function TextColorSelectorMenuItemFactory(accessor: IAccessor): IMenuSelectorItem<string> {
const commandService = accessor.get(ICommandService);
const selectionManager = accessor.get(ISelectionManager);
return {
id: SetTextColorCommand.id,
- title: 'TextColorIcon',
+ title: 'toolbar.textColor.main',
icon: 'TextColorIcon',
tooltip: 'toolbar.textColor.main',
type: MenuItemType.SELECTOR,
- selectType: SelectTypes.DOUBLEFIX,
- menu: [MenuPosition.TOOLBAR],
+ selectType: SelectTypes.NEO,
+ positions: [MenuPosition.TOOLBAR],
display: DisplayTypes.COLOR,
className: styles.selectColorPickerParent,
- // FIXME: click this toolbar icon would not trigger color change
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,
+ // label: {
+ // name: SHEET_UI_PLUGIN_NAME + ColorPicker.name,
+ // // },
+ // className: styles.selectColorPicker,
},
],
value$: new Observable<string>((subscriber) => {
@@ -415,8 +432,8 @@ export function BackgroundColorSelectorMenuItemFactory(accessor: IAccessor): IMe
tooltip: 'toolbar.fillColor.main',
title: 'TextColorIcon',
type: MenuItemType.SELECTOR,
- selectType: SelectTypes.DOUBLEFIX,
- menu: [MenuPosition.TOOLBAR],
+ selectType: SelectTypes.NEO,
+ positions: [MenuPosition.TOOLBAR],
display: DisplayTypes.COLOR,
icon: 'FillColorIcon',
className: styles.selectColorPickerParent,
@@ -461,7 +478,7 @@ export function CellBorderSelectorMenuItemFactory(accessor: IAccessor): IMenuSel
id: SetCellBorderCommand.id,
title: 'border',
display: DisplayTypes.SUFFIX,
- menu: [MenuPosition.TOOLBAR],
+ positions: [MenuPosition.TOOLBAR],
type: MenuItemType.SELECTOR,
selectType: SelectTypes.DOUBLE,
selections: [
@@ -530,7 +547,7 @@ export function HorizontalAlignMenuItemFactory(accessor: IAccessor): IMenuSelect
return {
id: SetHorizontalTextAlignCommand.id,
title: 'horizontalAlignMode',
- menu: [MenuPosition.TOOLBAR],
+ positions: [MenuPosition.TOOLBAR],
tooltip: 'toolbar.horizontalAlignMode.main',
className: styles.selectDoubleString,
display: DisplayTypes.SUFFIX,
@@ -548,7 +565,7 @@ export function VerticalAlignMenuItemFactory(accessor: IAccessor): IMenuSelector
className: styles.selectDoubleString,
display: DisplayTypes.SUFFIX,
type: MenuItemType.SELECTOR,
- menu: [MenuPosition.TOOLBAR],
+ positions: [MenuPosition.TOOLBAR],
selectType: SelectTypes.DOUBLE,
selections: VERTICAL_ALIGN_CHILDREN,
};
@@ -564,7 +581,7 @@ export function WrapTextMenuItemFactory(accessor: IAccessor): IMenuSelectorItem
tooltip: 'toolbar.textWrapMode.main',
type: MenuItemType.SELECTOR,
selectType: SelectTypes.DOUBLE,
- menu: [MenuPosition.TOOLBAR],
+ positions: [MenuPosition.TOOLBAR],
selections: TEXT_WRAP_CHILDREN,
display: DisplayTypes.SUFFIX,
value$: new Observable((subscriber) => {
@@ -598,7 +615,7 @@ export function TextRotateMenuItemFactory(accessor: IAccessor): IMenuSelectorIte
display: DisplayTypes.SUFFIX,
type: MenuItemType.SELECTOR,
selectType: SelectTypes.DOUBLE,
- menu: [MenuPosition.TOOLBAR],
+ positions: [MenuPosition.TOOLBAR],
value$: new Observable<number>((subscriber) => {
commandService.onCommandExecuted((c) => {
const id = c.id;
@@ -627,14 +644,14 @@ export function ClearSelectionMenuItemFactory(accessor: IAccessor): IMenuItem {
return {
id: ClearSelectionContentCommand.id,
title: 'rightClick.clearContent',
- menu: [MenuPosition.CONTEXT_MENU],
+ positions: [MenuPosition.CONTEXT_MENU],
};
}
export function InsertRowMenuItemFactory(accessor: IAccessor): IMenuItem {
return {
id: InsertRowCommand.id,
- menu: [MenuPosition.CONTEXT_MENU],
+ positions: [MenuPosition.CONTEXT_MENU],
title: 'rightClick.insertRow',
};
}
@@ -642,7 +659,7 @@ export function InsertRowMenuItemFactory(accessor: IAccessor): IMenuItem {
export function InsertColMenuItemFactory(accessor: IAccessor): IMenuItem {
return {
id: InsertColCommand.id,
- menu: [MenuPosition.CONTEXT_MENU],
+ positions: [MenuPosition.CONTEXT_MENU],
title: 'rightClick.insertColumn',
};
}
@@ -650,7 +667,7 @@ export function InsertColMenuItemFactory(accessor: IAccessor): IMenuItem {
export function RemoveRowMenuItemFactory(accessor: IAccessor): IMenuItem {
return {
id: RemoveRowCommand.id,
- menu: [MenuPosition.CONTEXT_MENU],
+ positions: [MenuPosition.CONTEXT_MENU],
title: 'rightClick.deleteSelectedRow',
};
}
@@ -658,7 +675,7 @@ export function RemoveRowMenuItemFactory(accessor: IAccessor): IMenuItem {
export function RemoveColMenuItemFactory(accessor: IAccessor): IMenuItem {
return {
id: RemoveColCommand.id,
- menu: [MenuPosition.CONTEXT_MENU],
+ positions: [MenuPosition.CONTEXT_MENU],
title: 'rightClick.deleteSelectedColumn',
};
}
@@ -666,7 +683,7 @@ export function RemoveColMenuItemFactory(accessor: IAccessor): IMenuItem {
export function SetRowHeightMenuItemFactory(accessor: IAccessor): IMenuItem {
return {
id: SetWorksheetRowHeightCommand.id,
- menu: [MenuPosition.CONTEXT_MENU],
+ positions: [MenuPosition.CONTEXT_MENU],
title: 'rightClick.rowHeight',
label: {
name: RightMenuInput.name,
@@ -681,7 +698,7 @@ export function SetRowHeightMenuItemFactory(accessor: IAccessor): IMenuItem {
export function SetColWidthMenuItemFactory(accessor: IAccessor): IMenuItem {
return {
id: SetWorksheetColWidthCommand.id,
- menu: [MenuPosition.CONTEXT_MENU],
+ positions: [MenuPosition.CONTEXT_MENU],
title: 'rightClick.columnWidth',
label: {
name: RightMenuInput.name,
@@ -696,15 +713,15 @@ export function SetColWidthMenuItemFactory(accessor: IAccessor): IMenuItem {
export function DeleteRangeMenuItemFactory(accessor: IAccessor): IMenuItem {
return {
id: `${DeleteRangeCommand.id}.parent`,
- menu: [MenuPosition.CONTEXT_MENU],
+ positions: [MenuPosition.CONTEXT_MENU],
title: 'rightClick.deleteCell',
- subMenus: [DeleteRangeCommand.id, `${DeleteRangeCommand.id}up`],
+ submenus: [DeleteRangeCommand.id, `${DeleteRangeCommand.id}up`],
};
}
export function DeleteRangeMoveLeftMenuItemFactory(accessor: IAccessor): IMenuItem {
return {
id: DeleteRangeCommand.id,
- menu: [MenuPosition.CONTEXT_MENU],
+ positions: [MenuPosition.CONTEXT_MENU],
title: 'rightClick.moveLeft',
parentId: `${DeleteRangeCommand.id}.parent`,
};
@@ -713,8 +730,8 @@ export function DeleteRangeMoveLeftMenuItemFactory(accessor: IAccessor): IMenuIt
export function DeleteRangeMoveUpMenuItemFactory(accessor: IAccessor): IMenuItem {
return {
id: `${DeleteRangeCommand.id}up`,
- menu: [MenuPosition.CONTEXT_MENU],
+ positions: [MenuPosition.CONTEXT_MENU],
title: 'rightClick.moveUp',
parentId: `${DeleteRangeCommand.id}.parent`,
};
-}
\ No newline at end of file
+}
diff --git a/packages/ui-plugin-sheets/src/View/Toolbar/ToolbarItem.tsx b/packages/ui-plugin-sheets/src/View/Toolbar/ToolbarItem.tsx
index fd9a66807c..fc8d9d8d10 100644
--- a/packages/ui-plugin-sheets/src/View/Toolbar/ToolbarItem.tsx
+++ b/packages/ui-plugin-sheets/src/View/Toolbar/ToolbarItem.tsx
@@ -1,7 +1,7 @@
import { ComponentChild, Component } from 'preact';
import { Subscription } from 'rxjs';
-import { AppContext, Button, Tooltip, CustomLabel, IDisplayMenuItem, MenuItemType, Select, IMenuSelectorItem, IMenuItem } from '@univerjs/base-ui';
+import { AppContext, Button, Tooltip, CustomLabel, IDisplayMenuItem, MenuItemType, Select, IMenuSelectorItem, IMenuItem, IMenuButtonItem } from '@univerjs/base-ui';
import { ICommandService } from '@univerjs/core';
import styles from './index.module.less';
@@ -13,6 +13,8 @@ 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;
@@ -37,12 +39,15 @@ export class ToolbarItem extends Component<IDisplayMenuItem<IMenuItem>, IToolbar
this.setState({ disabled });
});
- this.activatedSubscription = this.props.activated$?.subscribe((activated) => {
- this.setState({ activated });
- });
+ if (this.props.type === MenuItemType.BUTTON) {
+ const props = this.props as IDisplayMenuItem<IMenuButtonItem>;
+ this.activatedSubscription = props.activated$?.subscribe((activated) => {
+ this.setState({ activated });
+ });
+ }
if (this.props.type === MenuItemType.SELECTOR) {
- const props = this.props as IDisplayMenuItem<IMenuSelectorItem>;
+ const props = this.props as IDisplayMenuItem<IMenuSelectorItem<unknown>>;
this.currentValueSubscription = props.value$?.subscribe((value) => {
this.setState({ value });
});
@@ -52,10 +57,10 @@ export class ToolbarItem extends Component<IDisplayMenuItem<IMenuItem>, IToolbar
override componentWillMount() {
this.disabledSubscription?.unsubscribe();
this.activatedSubscription?.unsubscribe();
+ this.currentValueSubscription?.unsubscribe();
}
render(): ComponentChild {
- // here we render different types of toolbar item according to their type
switch (this.props.type) {
case MenuItemType.SELECTOR:
return this.renderSelectorType();
@@ -67,21 +72,25 @@ export class ToolbarItem extends Component<IDisplayMenuItem<IMenuItem>, IToolbar
private renderSelectorType(): ComponentChild {
const { context, state } = this;
const commandService: ICommandService = context.injector.get(ICommandService);
- const { disabled, activated, value } = state;
+ const { disabled, value } = state;
- const props = this.props as IDisplayMenuItem<IMenuSelectorItem>;
+ const props = this.props as IDisplayMenuItem<IMenuSelectorItem<unknown>>;
+ const { icon, title, className, label, display, selectType, selections, id } = props;
return (
- <Tooltip title={props.tooltip + (props.shortcut ? ` (${props.shortcut})` : '')} placement="bottom">
+ <Tooltip title={this.getTooltip()} placement="bottom">
<Select
- icon={props.icon}
- className={props.className}
- label={value ?? props.label}
- display={props.display}
- type={props.selectType!}
- children={props.selections!}
- onClick={(value) => commandService.executeCommand(props.id, { value })}
- onPressEnter={(value) => commandService.executeCommand(props.id, { value })}
+ id={id}
+ title={title}
+ children={selections!}
+ className={className}
+ display={display}
+ 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
+ onPressEnter={(value) => commandService.executeCommand(id, { value })}
+ type={selectType!}
></Select>
</Tooltip>
);
@@ -89,15 +98,19 @@ export class ToolbarItem extends Component<IDisplayMenuItem<IMenuItem>, IToolbar
private renderButtonType(): ComponentChild {
const { props, context, state } = this;
- const commandService: ICommandService = context.injector.get(ICommandService);
const { disabled, activated } = state;
+ const commandService: ICommandService = context.injector.get(ICommandService);
return (
- <Tooltip title={props.title + (props.shortcut ? ` (${props.shortcut})` : '')} placement="bottom">
+ <Tooltip title={this.getTooltip()} placement="bottom">
<Button active={activated} className={styles.textButton} type="text" disabled={disabled} onClick={() => commandService.executeCommand(props.id)}>
<CustomLabel label={{ name: props.icon }} />
</Button>
</Tooltip>
);
}
-}
\ No newline at end of file
+
+ private getTooltip(): string {
+ return this.context.localeService?.t(this.props.tooltip) + (this.props.shortcut ? ` (${this.props.shortcut})` : '');
+ }
+}