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

Merged
merged 3 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions packages/core/src/services/context/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
*/

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';
import { EndEditController } from '../../controllers/editor/end-edit.controller';

export const SetCellEditVisibleOperation: IOperation<IEditorBridgeServiceVisibleParam> = {
id: 'sheet.operation.set-cell-edit-visible',
Expand All @@ -36,6 +37,20 @@ 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);
const endEditorController = accessor.get(EndEditController);

commandService.syncExecuteCommand(SetCellEditVisibleOperation.id, params);
endEditorController.updateCursorChangeState();

return true;
},
};

/**
* When the editor is not clicked to change the cursor,
* the arrow keys will exit editing and move the cell.
Expand Down
34 changes: 20 additions & 14 deletions packages/sheets-ui/src/controllers/editor/end-edit.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ enum CursorChange {
StartEditor,
CursorChange,
}

@OnLifecycle(LifecycleStages.Rendered, EndEditController)
export class EndEditController extends Disposable {
private _cursorChangeObservers: Nullable<Observer<IPointerEvent | IMouseEvent>>;
Expand All @@ -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,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -310,14 +311,17 @@ export class EndEditController extends Disposable {
}
}

public updateCursorChangeState() {
this._cursorChange = CursorChange.CursorChange;
}

private _cursorStateListener() {
/**
* The user's operations follow the sequence of opening the editor and then moving the cursor.
* The logic here predicts the user's first cursor movement behavior based on this rule
*/

const editorObject = this._getEditorObject();

if (editorObject == null) {
return;
}
Expand All @@ -327,8 +331,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;
}
})
)
Expand All @@ -348,7 +352,9 @@ 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;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/sheets-ui/src/controllers/sheet-ui.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -183,6 +184,7 @@ import {
EditorDeleteLeftShortcut,
EditorDeleteLeftShortcutInActive,
generateArrowSelectionShortCutItem,
StartEditWithF2Shortcut,
} from './shortcuts/editor.shortcut';
import { SetColHiddenShortcutItem, SetRowHiddenShortcutItem } from './shortcuts/operation.shortcut';
import {
Expand Down Expand Up @@ -284,6 +286,7 @@ export class SheetUIController extends Disposable {
SetBoldCommand,
SetCellEditVisibleArrowOperation,
SetCellEditVisibleOperation,
SetCellEditVisibleWithF2Operation,
SetRangeBoldCommand,
SetRangeItalicCommand,
SetRangeUnderlineCommand,
Expand Down Expand Up @@ -445,6 +448,7 @@ export class SheetUIController extends Disposable {
ClearSelectionValueShortcutItem,
...generateArrowSelectionShortCutItem(),
EditorCursorEnterShortcut,
StartEditWithF2Shortcut,
EditorCursorTabShortcut,
EditorBreakLineShortcut,
EditorDeleteLeftShortcut,
Expand Down
14 changes: 14 additions & 0 deletions packages/sheets-ui/src/controllers/shortcuts/editor.shortcut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import {
SetCellEditVisibleArrowOperation,
SetCellEditVisibleOperation,
SetCellEditVisibleWithF2Operation,
} from '../../commands/operations/cell-edit.operation';
import {
whenEditorActivated,
Expand Down Expand Up @@ -72,6 +73,19 @@
return shortcutList;
}

export const StartEditWithF2Shortcut: IShortcutItem = {
id: SetCellEditVisibleWithF2Operation.id,

Check failure on line 77 in packages/sheets-ui/src/controllers/shortcuts/editor.shortcut.ts

View workflow job for this annotation

GitHub Actions / test

src/apis/__tests__/facade.spec.ts

TypeError: Cannot read properties of undefined (reading 'id') ❯ ../sheets-ui/src/controllers/shortcuts/editor.shortcut.ts:77:43 ❯ ../sheets-ui/src/controllers/editor/end-edit.controller.ts:13:32

Check failure on line 77 in packages/sheets-ui/src/controllers/shortcuts/editor.shortcut.ts

View workflow job for this annotation

GitHub Actions / test

src/apis/docs/__tests__/f-document.spec.ts

TypeError: Cannot read properties of undefined (reading 'id') ❯ ../sheets-ui/src/controllers/shortcuts/editor.shortcut.ts:77:43 ❯ ../sheets-ui/src/controllers/editor/end-edit.controller.ts:13:32

Check failure on line 77 in packages/sheets-ui/src/controllers/shortcuts/editor.shortcut.ts

View workflow job for this annotation

GitHub Actions / test

src/apis/sheets/__tests__/f-range.spec.ts

TypeError: Cannot read properties of undefined (reading 'id') ❯ ../sheets-ui/src/controllers/shortcuts/editor.shortcut.ts:77:43 ❯ ../sheets-ui/src/controllers/editor/end-edit.controller.ts:13:32
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
},
Expand All @@ -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,
},
Expand Down
9 changes: 9 additions & 0 deletions packages/sheets-ui/src/controllers/shortcuts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading