Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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> = {
Expand Down
45 changes: 42 additions & 3 deletions packages/sheets-ui/src/facade/f-workbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
Loading