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(ui): right menu use input, command value params
Dushusir3e3a6e14731b
diff --git a/packages/base-sheets/src/Commands/Commands/set-worksheet-col-width.command.ts b/packages/base-sheets/src/Commands/Commands/set-worksheet-col-width.command.tsindex ba18c4f04c..c73d0c5756 100644--- a/packages/base-sheets/src/Commands/Commands/set-worksheet-col-width.command.ts+++ b/packages/base-sheets/src/Commands/Commands/set-worksheet-col-width.command.ts@@ -1,26 +1,52 @@-import { CommandType, ICommand, ICommandService, IUndoRedoService } from '@univerjs/core';+import { CommandType, ICommand, ICommandService, ICurrentUniverService, IUndoRedoService } from '@univerjs/core';import { IAccessor } from '@wendellhu/redi';import { ISetWorksheetColWidthMutationParams, SetWorksheetColWidthMutation, SetWorksheetColWidthMutationFactory } from '../Mutations/set-worksheet-col-width.mutation';+import { ISelectionManager } from '../../Services/tokens';export interface SetWorksheetColWidthCommandParams {- workbookId: string;- worksheetId: string;- colIndex: number;- colWidth: number[];+ workbookId?: string;+ worksheetId?: string;+ colIndex?: number;+ value: number | number[];}export const SetWorksheetColWidthCommand: ICommand = {type: CommandType.COMMAND,id: 'sheet.command.set-worksheet-col-width',handler: async (accessor: IAccessor, params: SetWorksheetColWidthCommandParams) => {+ const selectionManager = accessor.get(ISelectionManager);const commandService = accessor.get(ICommandService);const undoRedoService = accessor.get(IUndoRedoService);+ const currentUniverService = accessor.get(ICurrentUniverService);+ const selections = selectionManager.getCurrentSelections();+ const workbook = currentUniverService.getCurrentUniverSheetInstance().getWorkBook();+ const range = selections[0];++ let { worksheetId, workbookId, colIndex, value } = params;+ if (!workbookId) {+ workbookId = workbook.getUnitId();+ }++ if (!worksheetId) {+ worksheetId = workbook.getActiveSheet().getSheetId();+ }++ if (!colIndex) {+ colIndex = range.startColumn;+ }++ if (!Array.isArray(value)) {+ if (typeof value === 'object') {+ value = 1;+ }+ value = new Array(range.endColumn - range.startColumn + 1).fill(value);+ }const redoMutationParams: ISetWorksheetColWidthMutationParams = {- worksheetId: params.worksheetId,- workbookId: params.workbookId,- colIndex: params.colIndex,- colWidth: params.colWidth,+ worksheetId,+ workbookId,+ colIndex,+ colWidth: value,};const undoMutationParams: ISetWorksheetColWidthMutationParams = SetWorksheetColWidthMutationFactory(accessor, redoMutationParams);const result = commandService.executeCommand(SetWorksheetColWidthMutation.id, redoMutationParams);diff --git a/packages/base-sheets/src/Commands/Commands/set-worksheet-row-height.command.ts b/packages/base-sheets/src/Commands/Commands/set-worksheet-row-height.command.tsindex ea8b666688..450d815aa4 100644--- a/packages/base-sheets/src/Commands/Commands/set-worksheet-row-height.command.ts+++ b/packages/base-sheets/src/Commands/Commands/set-worksheet-row-height.command.ts@@ -1,26 +1,52 @@-import { CommandType, ICommand, ICommandService, IUndoRedoService } from '@univerjs/core';+import { CommandType, ICommand, ICommandService, ICurrentUniverService, IUndoRedoService } from '@univerjs/core';import { IAccessor } from '@wendellhu/redi';import { ISetWorksheetRowHeightMutationParams, SetWorksheetRowHeightMutation, SetWorksheetRowHeightMutationFactory } from '../Mutations/set-worksheet-row-height.mutation';+import { ISelectionManager } from '../../Services/tokens';export interface SetWorksheetRowHeightCommandParams {- workbookId: string;- worksheetId: string;- rowIndex: number;- rowHeight: number[];+ workbookId?: string;+ worksheetId?: string;+ rowIndex?: number;+ value: number | number[];}export const SetWorksheetRowHeightCommand: ICommand = {type: CommandType.COMMAND,id: 'sheet.command.set-worksheet-row-height',handler: async (accessor: IAccessor, params: SetWorksheetRowHeightCommandParams) => {+ const selectionManager = accessor.get(ISelectionManager);const commandService = accessor.get(ICommandService);const undoRedoService = accessor.get(IUndoRedoService);+ const currentUniverService = accessor.get(ICurrentUniverService);+ const selections = selectionManager.getCurrentSelections();+ const workbook = currentUniverService.getCurrentUniverSheetInstance().getWorkBook();+ const range = selections[0];++ let { worksheetId, workbookId, rowIndex, value } = params;+ if (!workbookId) {+ workbookId = workbook.getUnitId();+ }++ if (!worksheetId) {+ worksheetId = workbook.getActiveSheet().getSheetId();+ }++ if (!rowIndex) {+ rowIndex = range.startRow;+ }++ if (!Array.isArray(value)) {+ if (typeof value === 'object') {+ value = 1;+ }+ value = new Array(range.endRow - range.startRow + 1).fill(value);+ }const redoMutationParams: ISetWorksheetRowHeightMutationParams = {- worksheetId: params.worksheetId,- workbookId: params.workbookId,- rowIndex: params.rowIndex,- rowHeight: params.rowHeight,+ worksheetId,+ workbookId,+ rowIndex,+ rowHeight: value,};const undoMutationParams: ISetWorksheetRowHeightMutationParams = SetWorksheetRowHeightMutationFactory(accessor, redoMutationParams);const result = commandService.executeCommand(SetWorksheetRowHeightMutation.id, redoMutationParams);diff --git a/packages/base-ui/src/Components/CustomLabel/CustomLabel.tsx b/packages/base-ui/src/Components/CustomLabel/CustomLabel.tsxindex 5e6dd25287..7b8c12ad75 100644--- a/packages/base-ui/src/Components/CustomLabel/CustomLabel.tsx+++ b/packages/base-ui/src/Components/CustomLabel/CustomLabel.tsx@@ -5,7 +5,7 @@ import { IBaseCustomLabelProps } from '../../Interfaces';export function CustomLabel(props: IBaseCustomLabelProps): JSX.Element | null {const context = useContext(AppContext);- const { label } = props;+ const { label, onChange } = props;function getLocale(name: string) {return context.localeService?.t(name);@@ -21,7 +21,7 @@ export function CustomLabel(props: IBaseCustomLabelProps): JSX.Element | null {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;diff --git a/packages/base-ui/src/Components/Menu/Menu.tsx b/packages/base-ui/src/Components/Menu/Menu.tsxindex b2c65f63cf..1dff6b0f53 100644--- a/packages/base-ui/src/Components/Menu/Menu.tsx+++ b/packages/base-ui/src/Components/Menu/Menu.tsx@@ -1,11 +1,10 @@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 { BaseMenuProps, BaseMenuState, BaseMenuItem, BaseMenuStyle } from '../../Interfaces';-import styles from './index.module.less';import { IDisplayMenuItem } from '../../services/menu/menu.service';import AppContext from '../../Common/AppContext';@@ -164,7 +163,11 @@ export class Menu extends Component<BaseMenuProps, BaseMenuState> {}}-export class MenuItem extends Component<{ menuItem: IDisplayMenuItem; index: number; onClick: () => void }, { disabled: boolean }> {+/**+ * state+ * value: use for command params+ */+export class MenuItem extends Component<{ menuItem: IDisplayMenuItem; index: number; onClick: () => void }, { disabled: boolean; value: any }> {static override contextType = AppContext;private disabledSubscription: Subscription | undefined;@@ -176,6 +179,7 @@ export class MenuItem extends Component<{ menuItem: IDisplayMenuItem; index: numthis.state = {disabled: false,+ value: {},};}@@ -207,10 +211,25 @@ export class MenuItem extends Component<{ menuItem: IDisplayMenuItem; index: num// item.onClick?.call(null, e, item.value, index, deep);const commandService: ICommandService = this.context.injector.get(ICommandService);this.props.onClick();- !(item.subMenuItems && item.subMenuItems.length > 0) && commandService.executeCommand(item.id);++ const value = this.state.value;+ !(item.subMenuItems && item.subMenuItems.length > 0) && commandService.executeCommand(item.id, { value });// this.showMenu(false);};+ /**+ * user input change value from CustomLabel+ * @param e+ */+ onChange = (e: Event) => {+ const targetValue = (e.target as HTMLInputElement).value;+ const value = isRealNum(targetValue) ? parseInt(targetValue) : targetValue;++ this.setState({+ value,+ });+ };+override render(): ComponentChild {const { menuItem: item, index } = this.props;// const commandService: ICommandService = this.context.injector.get(ICommandService);@@ -231,7 +250,7 @@ export class MenuItem extends Component<{ menuItem: IDisplayMenuItem; index: numthis.mouseLeave(e, index);}}>- <CustomLabel label={item.label || item.title}></CustomLabel>+ <CustomLabel label={item.label || item.title} onChange={this.onChange}></CustomLabel>{item.shortcut && ` (${item.shortcut})`}{item.subMenuItems && item.subMenuItems.length > 0 ? (<Menu ref={(ele: Menu) => (this._refs[index] = ele)} menuItems={item.subMenuItems} parent={this}></Menu>diff --git a/packages/base-ui/src/Interfaces/CustomLabel.ts b/packages/base-ui/src/Interfaces/CustomLabel.tsindex 91443f1eaf..77bf7580a9 100644--- a/packages/base-ui/src/Interfaces/CustomLabel.ts+++ b/packages/base-ui/src/Interfaces/CustomLabel.ts@@ -19,6 +19,7 @@ export interface ICustomLabelType {export interface IBaseCustomLabelProps {label: string | ICustomComponent | ComponentChildren;+ onChange?: (e: Event) => void;}export interface CustomLabelComponent extends BaseComponent<IBaseCustomLabelProps> {diff --git a/packages/core/src/Shared/Generate.ts b/packages/core/src/Shared/Generate.tsindex aff3041ee0..a0bdb0c2b9 100644--- a/packages/core/src/Shared/Generate.ts+++ b/packages/core/src/Shared/Generate.ts@@ -26,7 +26,7 @@ function valueIsError(value: string) {}// 是否是纯数字-function isRealNum(val: string | number) {+export function isRealNum(val: string | number) {if (val === null || val.toString().replace(/\s/g, '') === '') {return false;}@@ -35,7 +35,7 @@ function isRealNum(val: string | number) {return false;}- if (!Number.isNaN(val as number)) {+ if (!isNaN(val as number)) {return true;}return false;diff --git a/packages/ui-plugin-sheets/src/Controller/menu.ts b/packages/ui-plugin-sheets/src/Controller/menu.tsindex 16aebbd51e..f740c043a5 100644--- a/packages/ui-plugin-sheets/src/Controller/menu.ts+++ b/packages/ui-plugin-sheets/src/Controller/menu.ts@@ -317,7 +317,7 @@ export function SetColWidthMenuItemFactory(accessor: IAccessor): IMenuItem {name: RightMenuInput.name,props: {prefix: 'rightClick.columnWidth',- suffix: 'px'+ suffix: 'px',},},};diff --git a/packages/ui-plugin-sheets/src/View/RightMenu/RightMenuInput.tsx b/packages/ui-plugin-sheets/src/View/RightMenu/RightMenuInput.tsxindex 993f34d7a4..f7a0a79ce4 100644--- a/packages/ui-plugin-sheets/src/View/RightMenu/RightMenuInput.tsx+++ b/packages/ui-plugin-sheets/src/View/RightMenu/RightMenuInput.tsx@@ -5,6 +5,7 @@ interface IProps {prefix: string;suffix: string;onKeyUp?: (e: Event) => void;+ onChange?: (e: Event) => void;}export class RightMenuInput extends Component<IProps> {@@ -17,12 +18,17 @@ export class RightMenuInput extends Component<IProps> {onKeyUp?.(e);}+ onChange(e: Event) {+ const { onChange } = this.props;+ onChange?.(e);+ }+render() {const { prefix, suffix } = this.props;return (<div><CustomLabel label={prefix} />- <Input onPressEnter={this.handleKeyUp.bind(this)} type="number" placeholder="1" onClick={this.handleClick}></Input>+ <Input onPressEnter={this.handleKeyUp.bind(this)} type="number" placeholder="1" onClick={this.handleClick.bind(this)} onChange={this.onChange.bind(this)}></Input><CustomLabel label={suffix} /></div>);