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.

feat(facade): add api for cell editing (#4124)

zhangw19808d01d27a
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 85c5afe610..28fe99ee57 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<IEditorBridgeServiceVisibleParam> = {
diff --git a/packages/sheets-ui/src/facade/f-workbook.ts b/packages/sheets-ui/src/facade/f-workbook.ts
index d757763f3d..29963d8d41 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<boolean>;
}
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<boolean> {
+ 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);