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(sheets-ui): add f2 to start editing (#1875)

Wenzhao Hub740dfa86416
diff --git a/packages/core/src/services/context/context.ts b/packages/core/src/services/context/context.ts
index cc087bed61..8a1014c2b0 100644
--- a/packages/core/src/services/context/context.ts
+++ b/packages/core/src/services/context/context.ts
@@ -23,6 +23,8 @@ export const FOCUSING_EDITOR_BUT_HIDDEN = 'FOCUSING_EDITOR_BUT_HIDDEN';
export const EDITOR_ACTIVATED = 'EDITOR_ACTIVATED';
export const FOCUSING_EDITOR_INPUT_FORMULA = 'FOCUSING_EDITOR_INPUT_FORMULA';
+
+/** The focusing state of the formula editor (Fx bar). */
export const FOCUSING_FORMULA_EDITOR = 'FOCUSING_FORMULA_EDITOR';
export const FOCUSING_UNIVER_EDITOR = 'FOCUSING_UNIVER_EDITOR';
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 85a59a7914..db8847c808 100644
--- a/packages/sheets-ui/src/commands/operations/cell-edit.operation.ts
+++ b/packages/sheets-ui/src/commands/operations/cell-edit.operation.ts
@@ -15,7 +15,7 @@
*/
import type { IOperation } from '@univerjs/core';
-import { CommandType } from '@univerjs/core';
+import { CommandType, ICommandService } from '@univerjs/core';
import type { IEditorBridgeServiceVisibleParam } from '../../services/editor-bridge.service';
import { IEditorBridgeService } from '../../services/editor-bridge.service';
@@ -36,6 +36,18 @@ export const SetCellEditVisibleOperation: IOperation<IEditorBridgeServiceVisible
},
};
+export const SetCellEditVisibleWithF2Operation: IOperation<IEditorBridgeServiceVisibleParam> = {
+ id: 'sheet.operation.set-cell-edit-visible-f2',
+ type: CommandType.OPERATION,
+ handler: (accessor, params) => {
+ const commandService = accessor.get(ICommandService);
+
+ commandService.syncExecuteCommand(SetCellEditVisibleOperation.id, params);
+
+ return true;
+ },
+};
+
/**
* When the editor is not clicked to change the cursor,
* the arrow keys will exit editing and move the cell.
diff --git a/packages/sheets-ui/src/controllers/editor/end-edit.controller.ts b/packages/sheets-ui/src/controllers/editor/end-edit.controller.ts
index f15c378cfb..4621ceda81 100644
--- a/packages/sheets-ui/src/controllers/editor/end-edit.controller.ts
+++ b/packages/sheets-ui/src/controllers/editor/end-edit.controller.ts
@@ -49,7 +49,7 @@ import { Inject } from '@wendellhu/redi';
import { getEditorObject } from '../../basics/editor/get-editor-object';
import { MoveSelectionCommand, MoveSelectionEnterAndTabCommand } from '../../commands/commands/set-selection.command';
-import { SetCellEditVisibleArrowOperation } from '../../commands/operations/cell-edit.operation';
+import { SetCellEditVisibleArrowOperation, SetCellEditVisibleWithF2Operation } from '../../commands/operations/cell-edit.operation';
import { ICellEditorManagerService } from '../../services/editor/cell-editor-manager.service';
import type { IEditorBridgeServiceVisibleParam } from '../../services/editor-bridge.service';
import { IEditorBridgeService } from '../../services/editor-bridge.service';
@@ -70,6 +70,7 @@ enum CursorChange {
StartEditor,
CursorChange,
}
+
@OnLifecycle(LifecycleStages.Rendered, EndEditController)
export class EndEditController extends Disposable {
private _cursorChangeObservers: Nullable<Observer<IPointerEvent | IMouseEvent>>;
@@ -79,7 +80,7 @@ export class EndEditController extends Disposable {
/**
* It is used to distinguish whether the user has actively moved the cursor in the editor, mainly through mouse clicks.
*/
- private _isCursorChange: CursorChange = CursorChange.InitialState;
+ private _cursorChange: CursorChange = CursorChange.InitialState;
constructor(
@IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,
@@ -132,14 +133,14 @@ export class EndEditController extends Disposable {
if (visible === true) {
// Change `CursorChange` to changed status, when formula bar clicked.
- this._isCursorChange =
+ this._cursorChange =
eventType === DeviceInputEventType.PointerDown
? CursorChange.CursorChange
: CursorChange.StartEditor;
return;
}
- this._isCursorChange = CursorChange.InitialState;
+ this._cursorChange = CursorChange.InitialState;
const selections = this._selectionManagerService.getSelections();
const currentSelection = this._selectionManagerService.getCurrent();
@@ -212,19 +213,19 @@ export class EndEditController extends Disposable {
col: column,
};
- /**
- * When closing the editor, switch to the current tab of the editor.
- */
+ /**
+ * When closing the editor, switch to the current tab of the editor.
+ */
if (workbookId === unitId && sheetId !== worksheetId && this._editorBridgeService.isForceKeepVisible()) {
this._commandService.executeCommand(SetWorksheetActivateCommand.id, {
subUnitId: sheetId,
unitId,
});
}
- /**
- * When switching tabs while the editor is open,
- * the operation to refresh the selection will be blocked and needs to be triggered manually.
- */
+ /**
+ * When switching tabs while the editor is open,
+ * the operation to refresh the selection will be blocked and needs to be triggered manually.
+ */
this._selectionManagerService.refreshSelection();
const cell = this._editorBridgeService.interceptor.fetchThroughInterceptors(
@@ -317,7 +318,6 @@ export class EndEditController extends Disposable {
*/
const editorObject = this._getEditorObject();
-
if (editorObject == null) {
return;
}
@@ -327,8 +327,8 @@ export class EndEditController extends Disposable {
this.disposeWithMe(
toDisposable(
documentComponent.onPointerDownObserver.add(() => {
- if (this._isCursorChange === CursorChange.StartEditor) {
- this._isCursorChange = CursorChange.CursorChange;
+ if (this._cursorChange === CursorChange.StartEditor) {
+ this._cursorChange = CursorChange.CursorChange;
}
})
)
@@ -337,6 +337,7 @@ export class EndEditController extends Disposable {
private _commandExecutedListener() {
const updateCommandList = [SetCellEditVisibleArrowOperation.id];
+
this.disposeWithMe(
this._commandService.onCommandExecuted((command: ICommandInfo) => {
if (updateCommandList.includes(command.id)) {
@@ -348,13 +349,19 @@ export class EndEditController extends Disposable {
* the up, down, left, and right keys can no longer switch editing cells,
* but move the cursor within the editor instead.
*/
- if (keycode != null && this._isCursorChange === CursorChange.CursorChange) {
+ if (keycode != null &&
+ (this._cursorChange === CursorChange.CursorChange || this._contextService.getContextValue(FOCUSING_FORMULA_EDITOR))
+ ) {
this._moveInEditor(keycode, isShift);
return;
}
this._editorBridgeService.changeVisible(params);
}
+
+ if (command.id === SetCellEditVisibleWithF2Operation.id) {
+ this._cursorChange = CursorChange.CursorChange;
+ }
})
);
}
diff --git a/packages/sheets-ui/src/controllers/sheet-ui.controller.ts b/packages/sheets-ui/src/controllers/sheet-ui.controller.ts
index d55ac51f4a..b606e63ef7 100644
--- a/packages/sheets-ui/src/controllers/sheet-ui.controller.ts
+++ b/packages/sheets-ui/src/controllers/sheet-ui.controller.ts
@@ -78,6 +78,7 @@ import { SetActivateCellEditOperation } from '../commands/operations/activate-ce
import {
SetCellEditVisibleArrowOperation,
SetCellEditVisibleOperation,
+ SetCellEditVisibleWithF2Operation,
} from '../commands/operations/cell-edit.operation';
import { RenameSheetOperation } from '../commands/operations/rename-sheet.operation';
import { SetScrollOperation } from '../commands/operations/scroll.operation';
@@ -183,6 +184,7 @@ import {
EditorDeleteLeftShortcut,
EditorDeleteLeftShortcutInActive,
generateArrowSelectionShortCutItem,
+ StartEditWithF2Shortcut,
} from './shortcuts/editor.shortcut';
import { SetColHiddenShortcutItem, SetRowHiddenShortcutItem } from './shortcuts/operation.shortcut';
import {
@@ -284,6 +286,7 @@ export class SheetUIController extends Disposable {
SetBoldCommand,
SetCellEditVisibleArrowOperation,
SetCellEditVisibleOperation,
+ SetCellEditVisibleWithF2Operation,
SetRangeBoldCommand,
SetRangeItalicCommand,
SetRangeUnderlineCommand,
@@ -445,6 +448,7 @@ export class SheetUIController extends Disposable {
ClearSelectionValueShortcutItem,
...generateArrowSelectionShortCutItem(),
EditorCursorEnterShortcut,
+ StartEditWithF2Shortcut,
EditorCursorTabShortcut,
EditorBreakLineShortcut,
EditorDeleteLeftShortcut,
diff --git a/packages/sheets-ui/src/controllers/shortcuts/editor.shortcut.ts b/packages/sheets-ui/src/controllers/shortcuts/editor.shortcut.ts
index d21b03b34b..effa526f7c 100644
--- a/packages/sheets-ui/src/controllers/shortcuts/editor.shortcut.ts
+++ b/packages/sheets-ui/src/controllers/shortcuts/editor.shortcut.ts
@@ -22,6 +22,7 @@ import { KeyCode, MetaKeys } from '@univerjs/ui';
import {
SetCellEditVisibleArrowOperation,
SetCellEditVisibleOperation,
+ SetCellEditVisibleWithF2Operation,
} from '../../commands/operations/cell-edit.operation';
import {
whenEditorActivated,
@@ -72,6 +73,19 @@ export function generateArrowSelectionShortCutItem() {
return shortcutList;
}
+export const StartEditWithF2Shortcut: IShortcutItem = {
+ id: SetCellEditVisibleWithF2Operation.id,
+ binding: KeyCode.F2,
+ description: 'shortcut.sheet.start-editing',
+ group: '4_sheet-edit',
+ preconditions: whenSheetEditorFocused,
+ staticParameters: {
+ visible: true,
+ eventType: DeviceInputEventType.Keyboard,
+ keycode: KeyCode.F2,
+ },
+};
+
export const EditorCursorEnterShortcut: IShortcutItem = {
id: SetCellEditVisibleOperation.id,
binding: KeyCode.ENTER,
diff --git a/packages/sheets-ui/src/controllers/shortcuts/selection.shortcut.ts b/packages/sheets-ui/src/controllers/shortcuts/selection.shortcut.ts
index 3c463d20e1..1253e7af6a 100644
--- a/packages/sheets-ui/src/controllers/shortcuts/selection.shortcut.ts
+++ b/packages/sheets-ui/src/controllers/shortcuts/selection.shortcut.ts
@@ -31,7 +31,7 @@ import {
MoveSelectionEnterAndTabCommand,
SelectAllCommand,
} from '../../commands/commands/set-selection.command';
-import { whenSheetEditorFocused } from './utils';
+import { whenSheetEditorFocused, whenSheetEditorFocusedAndFxNotFocused } from './utils';
export const MoveSelectionDownShortcutItem: IShortcutItem<IMoveSelectionCommandParams> = {
id: MoveSelectionCommand.id,
@@ -63,7 +63,7 @@ export const MoveSelectionLeftShortcutItem: IShortcutItem<IMoveSelectionCommandP
group: '3_sheet-view',
binding: KeyCode.ARROW_LEFT,
priority: 100,
- preconditions: whenSheetEditorFocused,
+ preconditions: whenSheetEditorFocusedAndFxNotFocused,
staticParameters: {
direction: Direction.LEFT,
},
@@ -75,7 +75,7 @@ export const MoveSelectionRightShortcutItem: IShortcutItem<IMoveSelectionCommand
group: '3_sheet-view',
binding: KeyCode.ARROW_RIGHT,
priority: 100,
- preconditions: whenSheetEditorFocused,
+ preconditions: whenSheetEditorFocusedAndFxNotFocused,
staticParameters: {
direction: Direction.RIGHT,
},
diff --git a/packages/sheets-ui/src/controllers/shortcuts/utils.ts b/packages/sheets-ui/src/controllers/shortcuts/utils.ts
index 4b86c60ef1..89aaa13450 100644
--- a/packages/sheets-ui/src/controllers/shortcuts/utils.ts
+++ b/packages/sheets-ui/src/controllers/shortcuts/utils.ts
@@ -41,6 +41,15 @@ export function whenSheetEditorFocused(contextService: IContextService): boolean
);
}
+export function whenSheetEditorFocusedAndFxNotFocused(contextService: IContextService): boolean {
+ return (
+ contextService.getContextValue(FOCUSING_SHEET) &&
+ contextService.getContextValue(FOCUSING_UNIVER_EDITOR) &&
+ !contextService.getContextValue(EDITOR_ACTIVATED) &&
+ !contextService.getContextValue(FOCUSING_FORMULA_EDITOR)
+ );
+}
+
/**
* Requires the currently focused unit to be Workbook and the sheet editor is activated.
* @param contextService