Skip to content

Commit

Permalink
feat(editor): support shortcut (#1513)
Browse files Browse the repository at this point in the history
  • Loading branch information
DR-Univer committed Mar 7, 2024
1 parent d398384 commit b362e73
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
Expand Up @@ -25,6 +25,7 @@ export interface ISelectEditorFormulaOperationParam {
eventType: DeviceInputEventType;
keycode?: KeyCode;
metaKey?: MetaKeys | typeof META_KEY_CTRL_AND_SHIFT;
isSingleEditor?: boolean;
}

export const SelectEditorFormulaOperation: IOperation<ISelectEditorFormulaOperationParam> = {
Expand Down
Expand Up @@ -38,6 +38,7 @@ import {
promptSelectionShortcutItemCtrl,
promptSelectionShortcutItemCtrlAndShift,
promptSelectionShortcutItemShift,
singleEditorPromptSelectionShortcutItem,
} from './shortcuts/prompt.shortcut';

@OnLifecycle(LifecycleStages.Ready, FormulaUIController)
Expand Down Expand Up @@ -89,6 +90,7 @@ export class FormulaUIController extends Disposable {
...promptSelectionShortcutItemShift(),
...promptSelectionShortcutItemCtrl(),
...promptSelectionShortcutItemCtrlAndShift(),
...singleEditorPromptSelectionShortcutItem(),
ChangeRefToAbsoluteShortcut,
].forEach((item) => {
this.disposeWithMe(this._shortcutService.registerShortcut(item));
Expand Down
16 changes: 15 additions & 1 deletion packages/sheets-formula/src/controllers/prompt.controller.ts
Expand Up @@ -1649,13 +1649,18 @@ export class PromptController extends Disposable {
this._changeRefString();
} else if (updateCommandList.includes(command.id)) {
const params = command.params as ISelectEditorFormulaOperationParam;
const { keycode, metaKey } = params;
const { keycode, metaKey, isSingleEditor = false } = params;

if (keycode === KeyCode.ENTER) {
if (this._formulaPromptService.isSearching()) {
this._formulaPromptService.accept(true);
return;
}

if (isSingleEditor === true) {
return;
}

this._editorBridgeService.changeVisible({
visible: false,
eventType: DeviceInputEventType.Keyboard,
Expand All @@ -1671,6 +1676,11 @@ export class PromptController extends Disposable {
this._formulaPromptService.accept(true);
return;
}

if (isSingleEditor === true) {
return;
}

this._editorBridgeService.changeVisible({
visible: false,
eventType: DeviceInputEventType.Keyboard,
Expand All @@ -1693,6 +1703,10 @@ export class PromptController extends Disposable {
}
}

if (isSingleEditor === true) {
return;
}

if (this._arrowMoveActionState === ArrowMoveAction.moveCursor) {
this._moveInEditor(keycode);
return;
Expand Down
Expand Up @@ -22,6 +22,7 @@ import { KeyCode, MetaKeys } from '@univerjs/ui';
import { SelectEditorFormulaOperation } from '../../commands/operations/editor-formula.operation';
import { ReferenceAbsoluteOperation } from '../../commands/operations/reference-absolute.operation';
import { META_KEY_CTRL_AND_SHIFT } from '../../common/prompt';
import { whenEditorStandalone } from '../utils/utils';

export const PROMPT_SELECTION_KEYCODE_ARROW_LIST = [
KeyCode.ARROW_DOWN,
Expand Down Expand Up @@ -104,3 +105,20 @@ export const ChangeRefToAbsoluteShortcut: IShortcutItem = {
binding: KeyCode.F4,
preconditions: (contextService) => whenFormulaEditorActivated(contextService),
};

export function singleEditorPromptSelectionShortcutItem() {
const shortcutList: IShortcutItem[] = [];
for (const keycode of [KeyCode.ENTER, KeyCode.TAB, KeyCode.ARROW_DOWN, KeyCode.ARROW_UP]) {
shortcutList.push({
id: SelectEditorFormulaOperation.id,
binding: keycode,
preconditions: (contextService) => whenEditorStandalone(contextService),
staticParameters: {
eventType: DeviceInputEventType.Keyboard,
keycode,
isSingleEditor: true,
},
});
}
return shortcutList;
}
26 changes: 26 additions & 0 deletions packages/sheets-formula/src/controllers/utils/utils.ts
@@ -0,0 +1,26 @@
/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import type { IContextService } from '@univerjs/core';
import { FOCUSING_DOC, FOCUSING_UNIVER_EDITOR, FOCUSING_UNIVER_EDITOR_SINGLE_MODE } from '@univerjs/core';

export function whenEditorStandalone(contextService: IContextService) {
return (
contextService.getContextValue(FOCUSING_DOC) &&
contextService.getContextValue(FOCUSING_UNIVER_EDITOR) &&
contextService.getContextValue(FOCUSING_UNIVER_EDITOR_SINGLE_MODE)
);
}

0 comments on commit b362e73

Please sign in to comment.