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): cancel sheet tab rename with Escape (#8135)

wpxp1234566077d8ef53d8
diff --git a/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/utils/__tests__/sheet-tab-name-editor.spec.ts b/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/utils/__tests__/sheet-tab-name-editor.spec.ts
index 2486392890..e2f9efd6ef 100644
--- a/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/utils/__tests__/sheet-tab-name-editor.spec.ts
+++ b/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/utils/__tests__/sheet-tab-name-editor.spec.ts
@@ -15,7 +15,7 @@
*/
import { afterEach, describe, expect, it, vi } from 'vitest';
-import { keepTextSelected } from '../sheet-tab-name-editor';
+import { keepTextSelected, startSheetTabNameEditor } from '../sheet-tab-name-editor';
describe('keepTextSelected', () => {
afterEach(() => {
@@ -59,4 +59,38 @@ describe('keepTextSelected', () => {
expect(selection.isCollapsed).toBe(false);
expect(selection.toString()).toBe('工作表 1');
});
+
+ it('cancels editing on Escape and restores the original name', () => {
+ const slideTabItem = document.createElement('div');
+ const editor = document.createElement('span');
+ editor.textContent = '工作表 1';
+ slideTabItem.appendChild(editor);
+ document.body.appendChild(slideTabItem);
+
+ const setEditMode = vi.fn();
+ const beforeCommit = vi.fn();
+ const onCommit = vi.fn();
+ const onCancel = vi.fn();
+ startSheetTabNameEditor({
+ slideTabItem,
+ canStart: () => true,
+ checkName: () => false,
+ setEditMode,
+ beforeCommit,
+ onCommit,
+ onCancel,
+ });
+
+ editor.textContent = '改名';
+ const event = new KeyboardEvent('keydown', { key: 'Escape', bubbles: true, cancelable: true });
+ editor.dispatchEvent(event);
+
+ expect(event.defaultPrevented).toBe(true);
+ expect(editor.textContent).toBe('工作表 1');
+ expect(editor.getAttribute('contentEditable')).toBeNull();
+ expect(setEditMode).toHaveBeenLastCalledWith(false);
+ expect(beforeCommit).toHaveBeenCalledOnce();
+ expect(onCancel).toHaveBeenCalledOnce();
+ expect(onCommit).not.toHaveBeenCalled();
+ });
});
diff --git a/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/utils/sheet-tab-name-editor.ts b/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/utils/sheet-tab-name-editor.ts
index d0177e6cf9..bd11bf5311 100644
--- a/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/utils/sheet-tab-name-editor.ts
+++ b/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/utils/sheet-tab-name-editor.ts
@@ -21,6 +21,7 @@ export interface IStartSheetTabNameEditorParams {
setEditMode: (editing: boolean) => void;
beforeCommit: () => void;
onCommit: (name: string, event: FocusEvent) => void;
+ onCancel: () => void;
}
interface ISheetTabNameEditorActions {
@@ -74,6 +75,7 @@ function createSheetTabNameEditorActions(
): ISheetTabNameEditorActions {
let compositionFlag = true;
let cleanup = () => { };
+ const originalName = getEditorText(input);
const pasteAction = (event: ClipboardEvent) => {
event.preventDefault();
@@ -95,10 +97,21 @@ function createSheetTabNameEditorActions(
params.onCommit(getEditorText(input), focusEvent);
};
+ const cancelAction = () => {
+ input.textContent = originalName;
+ params.setEditMode(false);
+ cleanup();
+ params.beforeCommit();
+ params.onCancel();
+ };
+
const keydownAction = (event: KeyboardEvent) => {
event.stopPropagation();
- if (event.key === 'Enter') {
+ if (event.key === 'Escape') {
+ event.preventDefault();
+ cancelAction();
+ } else if (event.key === 'Enter') {
input.blur();
event.preventDefault();
}
diff --git a/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/utils/slide-tab-bar.ts b/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/utils/slide-tab-bar.ts
index 6d1270a833..804e8af971 100644
--- a/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/utils/slide-tab-bar.ts
+++ b/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/utils/slide-tab-bar.ts
@@ -141,6 +141,9 @@ export class SlideTabItem {
this._slideTabBar.addListener();
}
},
+ onCancel: () => {
+ this._slideTabBar.addListener();
+ },
});
}