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.

fix(sheets-ui): backspace shortcut not work (#4740)

zhangwe4a5639c52c4
diff --git a/packages/sheets-ui/src/controllers/sheet-ui.controller.ts b/packages/sheets-ui/src/controllers/sheet-ui.controller.ts
index 1288885cb4..3f917791ee 100644
--- a/packages/sheets-ui/src/controllers/sheet-ui.controller.ts
+++ b/packages/sheets-ui/src/controllers/sheet-ui.controller.ts
@@ -151,7 +151,7 @@ import {
SetStrikeThroughShortcutItem,
SetUnderlineShortcutItem,
} from './shortcuts/style.shortcut';
-import { ClearSelectionValueShortcutItem } from './shortcuts/value.shortcut';
+import { ClearSelectionValueShortcutItem, ClearSelectionValueShortcutItemMac } from './shortcuts/value.shortcut';
import {
PreventDefaultResetZoomShortcutItem,
PreventDefaultZoomInShortcutItem,
@@ -329,6 +329,7 @@ export class SheetUIController extends Disposable {
// cell content editing shortcuts
ClearSelectionValueShortcutItem,
+ ClearSelectionValueShortcutItemMac,
...generateArrowSelectionShortCutItem(),
EditorCursorEnterShortcut,
StartEditWithF2Shortcut,
diff --git a/packages/sheets-ui/src/controllers/shortcuts/editor.shortcut.ts b/packages/sheets-ui/src/controllers/shortcuts/editor.shortcut.ts
index e2afb233f9..234e514189 100644
--- a/packages/sheets-ui/src/controllers/shortcuts/editor.shortcut.ts
+++ b/packages/sheets-ui/src/controllers/shortcuts/editor.shortcut.ts
@@ -14,11 +14,12 @@
* limitations under the License.
*/
+import type { IShortcutItem } from '@univerjs/ui';
+import { DOCS_NORMAL_EDITOR_UNIT_ID_KEY } from '@univerjs/core';
import { BreakLineCommand, DeleteLeftCommand } from '@univerjs/docs-ui';
+
import { DeviceInputEventType } from '@univerjs/engine-render';
import { KeyCode, MetaKeys } from '@univerjs/ui';
-
-import type { IShortcutItem } from '@univerjs/ui';
import {
SetCellEditVisibleArrowOperation,
SetCellEditVisibleOperation,
@@ -136,18 +137,19 @@ export const EditorDeleteLeftShortcut: IShortcutItem = {
preconditions: (contextService) =>
whenEditorActivated(contextService) || whenFormulaEditorFocused(contextService),
binding: KeyCode.BACKSPACE,
+ mac: KeyCode.DELETE,
};
export const EditorDeleteLeftShortcutInActive: IShortcutItem = {
id: SetCellEditVisibleOperation.id,
description: 'shortcut.sheet.delete-and-start-editing',
group: '4_sheet-edit',
- preconditions: (contextService) =>
- whenSheetEditorFocused(contextService) && !whenFormulaEditorFocused(contextService),
+ preconditions: (contextService) => whenSheetEditorFocused(contextService) && !whenFormulaEditorFocused(contextService),
binding: KeyCode.BACKSPACE,
staticParameters: {
visible: true,
eventType: DeviceInputEventType.Keyboard,
keycode: KeyCode.BACKSPACE,
+ unitId: DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
},
};
diff --git a/packages/sheets-ui/src/controllers/shortcuts/value.shortcut.ts b/packages/sheets-ui/src/controllers/shortcuts/value.shortcut.ts
index 5e087ef3d0..dcba6ef7c2 100644
--- a/packages/sheets-ui/src/controllers/shortcuts/value.shortcut.ts
+++ b/packages/sheets-ui/src/controllers/shortcuts/value.shortcut.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import { ClearSelectionContentCommand } from '@univerjs/sheets';
import type { IShortcutItem } from '@univerjs/ui';
+import { ClearSelectionContentCommand } from '@univerjs/sheets';
import { KeyCode } from '@univerjs/ui';
import { whenSheetEditorFocused } from './utils';
@@ -27,3 +27,10 @@ export const ClearSelectionValueShortcutItem: IShortcutItem = {
binding: KeyCode.DELETE,
mac: KeyCode.BACKSPACE,
};
+
+export const ClearSelectionValueShortcutItemMac: IShortcutItem = {
+ id: ClearSelectionContentCommand.id,
+ // when focusing on any other input tag do not trigger this shortcut
+ preconditions: (contextService) => whenSheetEditorFocused(contextService),
+ mac: KeyCode.DELETE,
+};
diff --git a/packages/ui/src/services/shortcut/shortcut.service.ts b/packages/ui/src/services/shortcut/shortcut.service.ts
index a1b119479a..191bb9781b 100644
--- a/packages/ui/src/services/shortcut/shortcut.service.ts
+++ b/packages/ui/src/services/shortcut/shortcut.service.ts
@@ -50,7 +50,7 @@ export interface IShortcutItem<P extends object = object> {
*
* @example { binding: KeyCode.ENTER | MetaKeys.ALT }
*/
- binding: KeyCode | number;
+ binding?: KeyCode | number;
/**
* The binding of the shortcut for macOS. If the property is not specified, the default binding would be used.
*/
@@ -120,9 +120,9 @@ export interface IShortcutService {
/**
* Get the display string of the shortcut item.
* @param shortcut - the shortcut item to get the display string.
- * @returns {string} the display string of the shortcut. For example `Ctrl+Enter`.
+ * @returns {string | null} The display string of the shortcut. For example `Ctrl+Enter`.
*/
- getShortcutDisplay(shortcut: IShortcutItem): string;
+ getShortcutDisplay(shortcut: IShortcutItem): string | null;
/**
* Get the display string of the shortcut of the command.
* @param id the id of the command to get the shortcut display.
@@ -177,6 +177,8 @@ export class ShortcutService extends Disposable implements IShortcutService {
registerShortcut(shortcut: IShortcutItem): IDisposable {
// first map shortcut to a number, so it could be converted and fetched quickly
const binding = this._getBindingFromItem(shortcut);
+ if (!binding) return toDisposable(() => {});
+
const bindingSet = this._shortCutMapping.get(binding);
if (bindingSet) {
bindingSet.add(shortcut);
@@ -210,7 +212,6 @@ export class ShortcutService extends Disposable implements IShortcutService {
getShortcutDisplayOfCommand(id: string): string | null {
const set = this._commandIDMapping.get(id);
- // if (!set || set.size > 1) {
if (!set) {
return null;
}
@@ -223,8 +224,10 @@ export class ShortcutService extends Disposable implements IShortcutService {
return null;
}
- getShortcutDisplay(shortcut: IShortcutItem): string {
+ getShortcutDisplay(shortcut: IShortcutItem): string | null {
const binding = this._getBindingFromItem(shortcut);
+ if (!binding) return null;
+
const ctrlKey = binding & MetaKeys.CTRL_COMMAND;
const shiftKey = binding & MetaKeys.SHIFT;
const altKey = binding & MetaKeys.ALT;
@@ -293,7 +296,7 @@ export class ShortcutService extends Disposable implements IShortcutService {
return candidateShortcut;
}
- private _getBindingFromItem(item: IShortcutItem): number {
+ private _getBindingFromItem(item: IShortcutItem): number | undefined {
if (this._platformService.isMac && item.mac) {
return item.mac;
}
diff --git a/packages/ui/src/views/components/shortcut-panel/ShortcutPanel.tsx b/packages/ui/src/views/components/shortcut-panel/ShortcutPanel.tsx
index cec4000185..7fa4363ee6 100644
--- a/packages/ui/src/views/components/shortcut-panel/ShortcutPanel.tsx
+++ b/packages/ui/src/views/components/shortcut-panel/ShortcutPanel.tsx
@@ -23,7 +23,7 @@ import styles from './index.module.less';
interface IRenderShortcutItem {
title: string;
- shortcut: string;
+ shortcut: string | null;
}
interface IShortcutGroup {
@@ -76,7 +76,6 @@ export function ShortcutPanel() {
.sort((a, b) => a.sequence - b.sequence);
setShortcutItems(toRender);
- // eslint-disable-next-line react-hooks/exhaustive-deps
}, [shortcutService, localeService, currentLocale]);
useEffect(() => {