diff --git a/packages/sheets-ui/src/commands/operations/cell-edit.operation.ts b/packages/sheets-ui/src/commands/operations/cell-edit.operation.ts index 85c5afe610a..28fe99ee578 100644 --- a/packages/sheets-ui/src/commands/operations/cell-edit.operation.ts +++ b/packages/sheets-ui/src/commands/operations/cell-edit.operation.ts @@ -15,9 +15,9 @@ */ import type { IOperation } from '@univerjs/core'; -import { CommandType, ICommandService } from '@univerjs/core'; - import type { IEditorBridgeServiceVisibleParam } from '../../services/editor-bridge.service'; + +import { CommandType, ICommandService } from '@univerjs/core'; import { IEditorBridgeService } from '../../services/editor-bridge.service'; export const SetCellEditVisibleOperation: IOperation = { diff --git a/packages/sheets-ui/src/facade/f-workbook.ts b/packages/sheets-ui/src/facade/f-workbook.ts index d757763f3d7..29963d8d413 100644 --- a/packages/sheets-ui/src/facade/f-workbook.ts +++ b/packages/sheets-ui/src/facade/f-workbook.ts @@ -14,11 +14,13 @@ * limitations under the License. */ +import type { IEditorBridgeServiceVisibleParam } from '@univerjs/sheets-ui'; import type { IHoverRichTextInfo, IHoverRichTextPosition } from '../services/hover-manager.service'; -import { type IDisposable, ILogService, toDisposable } from '@univerjs/core'; +import { awaitTime, ICommandService, type IDisposable, ILogService, toDisposable } from '@univerjs/core'; +import { DeviceInputEventType } from '@univerjs/engine-render'; import { FWorkbook } from '@univerjs/sheets/facade'; -import { HoverManagerService } from '@univerjs/sheets-ui'; -import { type IDialogPartMethodOptions, IDialogService, type ISidebarMethodOptions, ISidebarService } from '@univerjs/ui'; +import { HoverManagerService, SetCellEditVisibleOperation } from '@univerjs/sheets-ui'; +import { type IDialogPartMethodOptions, IDialogService, type ISidebarMethodOptions, ISidebarService, KeyCode } from '@univerjs/ui'; import { filter } from 'rxjs'; interface IFWorkbookSheetsUIMixin { @@ -57,6 +59,20 @@ interface IFWorkbookSheetsUIMixin { * @returns A disposable object that can be used to unsubscribe from the event */ onCellHover(callback: (cell: IHoverRichTextPosition) => void): IDisposable; + + /** + * Start the editing process + * @returns A boolean value + */ + startEditing(): boolean; + + /** + * End the editing process + * @async + * @param save - Whether to save the changes + * @returns A promise that resolves to a boolean value + */ + endEditing(save?: boolean): Promise; } class FWorokbookSheetsUIMixin extends FWorkbook implements IFWorkbookSheetsUIMixin { @@ -104,6 +120,29 @@ class FWorokbookSheetsUIMixin extends FWorkbook implements IFWorkbookSheetsUIMix .subscribe(callback) ); } + + override startEditing(): boolean { + const commandService = this._injector.get(ICommandService); + return commandService.syncExecuteCommand(SetCellEditVisibleOperation.id, { + eventType: DeviceInputEventType.Dblclick, + unitId: this._workbook.getUnitId(), + visible: true, + } as IEditorBridgeServiceVisibleParam); + } + + override async endEditing(save?: boolean): Promise { + const commandService = this._injector.get(ICommandService); + commandService.syncExecuteCommand(SetCellEditVisibleOperation.id, { + eventType: DeviceInputEventType.Keyboard, + keycode: save ? KeyCode.ENTER : KeyCode.ESC, + visible: false, + unitId: this._workbook.getUnitId(), + } as IEditorBridgeServiceVisibleParam); + + // wait for the async cell edit operation to complete + await awaitTime(0); + return true; + } } FWorkbook.extend(FWorokbookSheetsUIMixin);