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): right context menu

Dushusir8099fd9f6baa
diff --cc packages/base-sheets/src/index.ts
index 188afbdd33,90831a0bef..1943f5673e
--- a/packages/base-sheets/src/index.ts
+++ b/packages/base-sheets/src/index.ts
@@@ -41,18 -39,9 +41,23 @@@ export { SetWorksheetOrderCommand } fro
export { SetWorksheetRowHeightCommand } from './Commands/Commands/set-worksheet-row-height.command';
export { SetWorksheetColWidthCommand } from './Commands/Commands/set-worksheet-col-width.command';
export { DeleteRangeCommand } from './Commands/Commands/delete-range.command';
+ export { DeleteRangeMoveLeftCommand } from './Commands/Commands/delete-range-move-left.command';
+ export { DeleteRangeMoveUpCommand } from './Commands/Commands/delete-range-move-up.command';
export { RemoveRowCommand, RemoveColCommand } from './Commands/Commands/remove-row-col.command';
+export { InsertColCommand, InsertRowCommand, InsertColCommandParams, InsertRowCommandParams } from './Commands/Commands/insert-row-col.command';
+export { ClearSelectionContentCommand } from './Commands/Commands/clear-selection-content.command';
+export {
+ SetBorderColorCommand,
+ SetBorderCommand,
+ SetBorderPositionCommand,
+ SetBorderStyleCommand,
+ ISetBorderColorCommandParams,
+ ISetBorderCommandParams,
+ ISetBorderPositionCommandParams,
+ ISetBorderStyleCommandParams,
+} from './Commands/Commands/set-border-command';
+
+// #endregion
+ export { SetWorksheetRowHideCommand } from './Commands/Commands/set-worksheet-row-hide.command';
+ export { SetWorksheetRowShowCommand } from './Commands/Commands/set-worksheet-row-show.command';
+ export { RemoveSheetCommand } from './Commands/Commands/remove-sheet.command';
diff --cc packages/base-ui/src/Components/CustomLabel/CustomLabel.tsx
index c5cd2ab887,7b8c12ad75..132a0e4da9
--- a/packages/base-ui/src/Components/CustomLabel/CustomLabel.tsx
+++ b/packages/base-ui/src/Components/CustomLabel/CustomLabel.tsx
@@@ -1,55 -1,11 +1,55 @@@
-import { isValidElement } from 'preact';
+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';
+
+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
+ }
+
+ // if the render type is icon, then render value to icon
+ if (display === DisplayTypes.ICON) {
+ return <>{value}</>;
+ }
+
+ return <>{value}</>;
+}
+
+/** @deprecated */
export function CustomLabel(props: IBaseCustomLabelProps): JSX.Element | null {
const context = useContext(AppContext);
- const { label, display } = props;
- const { label, onChange } = props;
++ const { label, display, onChange } = props;
function getLocale(name: string) {
return context.localeService?.t(name);
@@@ -72,31 -21,8 +72,31 @@@
const Label = context.componentManager?.get((label as ICustomComponent).name) as any;
if (Label) {
const props = (label as ICustomComponent).props ?? {};
- return <Label {...props} />;
+ return <Label onChange={onChange} {...props} />;
}
}
+
return null;
}
+
+export interface IColorSelectProps {
+ icon?: string;
+ title: string;
+ /** color */
+ value: string;
+}
+
+export class ColorSelect extends Component<IColorSelectProps> {
+ render() {
+ const { value, icon } = this.props;
+
+ return (
+ <div className={styles.colorSelect}>
+ <div>
+ <CustomLabel label={{ name: icon }} />
+ </div>
+ <div className={styles.colorSelectLine} style={{ background: value }}></div>
+ </div>
+ );
+ }
+}
diff --cc packages/base-ui/src/Components/Menu/Menu.tsx
index 036337e086,1dff6b0f53..cfde773b63
--- a/packages/base-ui/src/Components/Menu/Menu.tsx
+++ b/packages/base-ui/src/Components/Menu/Menu.tsx
@@@ -1,23 -1,15 +1,27 @@@
import { Component, ComponentChild, createRef } from 'preact';
import { Subscription } from 'rxjs';
- import { ICommandService } from '@univerjs/core';
+ import { ICommandService, isRealNum } from '@univerjs/core';
+ import { BaseMenuProps, BaseMenuState, BaseMenuItem, BaseMenuStyle } from '../../Interfaces';
+ 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';
+import { ICustomComponentOption, IDisplayMenuItem, IMenuItem, IValueOption, isValueOptions } from '../../services/menu/menu';
+import { BaseMenuItem, BaseMenuProps, BaseMenuStyle } from '../../Interfaces/Menu';
+import { joinClassNames } from '../../Utils/util';
+import { CustomLabel, NeoCustomLabel } from '../CustomLabel/CustomLabel';
+import { IMenuService } from '../../services/menu/menu.service';
+
+export interface IBaseMenuState {
+ show: boolean;
+ posStyle: BaseMenuStyle;
+ menuItems: Array<IDisplayMenuItem<IMenuItem>>;
+}
+
+export class Menu extends Component<BaseMenuProps, IBaseMenuState> {
+ static override contextType = AppContext;
-export class Menu extends Component<BaseMenuProps, BaseMenuState> {
private _MenuRef = createRef<HTMLUListElement>();
private _refs: Menu[] = [];
diff --cc packages/base-ui/src/Interfaces/CustomLabel.ts
index e2952cb07c,77bf7580a9..34b293b9f3
--- a/packages/base-ui/src/Interfaces/CustomLabel.ts
+++ b/packages/base-ui/src/Interfaces/CustomLabel.ts
@@@ -18,10 -18,8 +18,11 @@@ export interface ICustomLabelType
}
export interface IBaseCustomLabelProps {
+ icon?: string;
+ value?: string;
label: string | ICustomComponent | ComponentChildren;
+ display?: DisplayTypes;
+ onChange?: (e: Event) => void;
}
export interface CustomLabelComponent extends BaseComponent<IBaseCustomLabelProps> {
diff --cc packages/base-ui/src/services/menu/menu.ts
index 4c0e8b709c,0000000000..12d04c4376
mode 100644,000000..100644
--- a/packages/base-ui/src/services/menu/menu.ts
+++ b/packages/base-ui/src/services/menu/menu.ts
@@@ -1,108 -1,0 +1,109 @@@
+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,
++ SHEET_BAR,
+}
+
+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,
+ DROPDOWN,
+}
+
+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 function isValueOptions(v: IValueOption | ICustomComponentOption): v is IValueOption {
+ return typeof (v as IValueOption).value !== 'undefined';
+}
+
+export interface ICustomComponentOption {
+ id: string;
+}
+
+export interface ICustomComponentProps<T> {
+ value: T;
+ onValueChange: (v: T) => void;
+}
+
+export function isCustomComponentOption(v: IValueOption | ICustomComponentOption): v is ICustomComponentOption {
+ return typeof (v as ICustomComponentOption).id !== 'undefined';
+}
+
+export interface IMenuSelectorItem<V> extends IMenuItemBase {
+ type: MenuItemType.SELECTOR | MenuItemType.DROPDOWN;
+
+ /** 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>;
+
+export type IDisplayMenuItem<T extends IMenuItem> = T & {
+ shortcut?: string;
+};
diff --cc packages/ui-plugin-sheets/src/Controller/menu.ts
index 44db9dd88a,aafa45fad1..8fe0c0a16f
--- a/packages/ui-plugin-sheets/src/Controller/menu.ts
+++ b/packages/ui-plugin-sheets/src/Controller/menu.ts
@@@ -14,22 -14,14 +14,28 @@@ import
SetWorksheetRowHeightCommand,
RemoveRowCommand,
RemoveColCommand,
+ SetFontFamilyCommand,
+ SetFontSizeCommand,
+ SetTextColorCommand,
+ SetBackgroundColorCommand,
+ SetBorderColorCommand,
+ SetBorderPositionCommand,
+ SetTextRotationCommand,
+ SetTextWrapCommand,
+ SetHorizontalTextAlignCommand,
+ SetVerticalTextAlignCommand,
+ ResetTextColorCommand,
+ ResetBackgroundColorCommand,
+ SetBorderStyleCommand,
+ DeleteRangeMoveLeftCommand,
+ DeleteRangeMoveUpCommand,
++ RemoveSheetCommand,
+ SetWorksheetRowHideCommand,
+ SetWorksheetRowShowCommand,
- RemoveSheetCommand,
} from '@univerjs/base-sheets';
- import { ColorPicker, DisplayTypes, IMenuButtonItem, IMenuItem, IMenuSelectorItem, MenuItemType, MenuPosition, SelectTypes } from '@univerjs/base-ui';
-import { IDisplayMenuItem, IMenuItem, MenuPosition } from '@univerjs/base-ui';
-import { FontItalic, FontWeight, ICommandService, IPermissionService, IUndoRedoService, RedoCommand, UndoCommand } from '@univerjs/core';
++import { ColorPicker, DisplayTypes, IMenuButtonItem, IMenuItem, IMenuSelectorItem, MenuItemType, MenuPosition, SelectTypes, IDisplayMenuItem } 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';
import { map } from 'rxjs/operators';