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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix(sheets-ui): preserve caret when renaming empty sheet tabs (#7958)
wpxp123456da1a41ffa30e
diff --git a/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/SheetBarTabs.tsx b/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/SheetBarTabs.tsxindex 9d44d70683..23cdc85c29 100644--- a/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/SheetBarTabs.tsx+++ b/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/SheetBarTabs.tsx@@ -17,9 +17,6 @@import type { ICommandInfo } from '@univerjs/core';import type { IUniverUIConfig } from '@univerjs/ui';import type { KeyboardEvent as ReactKeyboardEvent, MouseEvent as ReactMouseEvent } from 'react';-import type { LocaleKey } from '../../../locale/types';-import type { IScrollState } from '../../../services/sheet-bar/type';-import type { IBaseSheetBarProps } from './SheetBarItem';import {ICommandService,IConfirmService,@@ -57,6 +54,9 @@ import {import { ILayoutService, UI_PLUGIN_CONFIG_KEY, useConfigValue, useDependency, useObservable } from '@univerjs/ui';import { useCallback, useEffect, useRef, useState } from 'react';import { merge } from 'rxjs';+import type { LocaleKey } from '../../../locale/types';+import type { IScrollState } from '../../../services/sheet-bar/type';+import type { IBaseSheetBarProps } from './SheetBarItem';import { getEmbedSheetsTabCustomData } from '../../../embed-tab-anchor';import { IEditorBridgeService } from '../../../services/editor-bridge.service';import { ISheetBarService } from '../../../services/sheet-bar/sheet-bar.service';@@ -307,7 +307,7 @@ export function SheetBarTabs() {<>{embedChildType != null && <EmbedSheetTabProductIcon childType={embedChildType} />}{hasProtection && <LockIcon className="univer-shrink-0" />}- <span className="univer-truncate univer-outline-none">{sheet.getName()}</span>+ <span className="univer-min-w-px univer-truncate univer-outline-none">{sheet.getName()}</span></>),index,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.tsnew file mode 100644index 0000000000..2486392890--- /dev/null+++ b/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/utils/__tests__/sheet-tab-name-editor.spec.ts@@ -0,0 +1,62 @@+/**+ * Copyright 2023-present DreamNum Co., Ltd.+ *+ * 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 { afterEach, describe, expect, it, vi } from 'vitest';+import { keepTextSelected } from '../sheet-tab-name-editor';++describe('keepTextSelected', () => {+ afterEach(() => {+ vi.useRealTimers();+ window.getSelection()?.removeAllRanges();+ document.body.replaceChildren();+ });++ it.each(['', '<br>'])('restores a collapsed caret after refocusing an empty editor containing %j', (html) => {+ vi.useFakeTimers();+ const editor = document.createElement('span');+ editor.contentEditable = 'true';+ editor.innerHTML = html;+ document.body.appendChild(editor);+ editor.focus();+ editor.blur();+ editor.focus();++ keepTextSelected(editor);+ vi.runAllTimers();++ const selection = window.getSelection()!;+ expect(selection.rangeCount).toBe(1);+ expect(selection.isCollapsed).toBe(true);+ expect(selection.anchorNode).toBe(editor);+ expect(selection.anchorOffset).toBe(0);+ });++ it('selects the entire nonempty name for replacement after refocusing', () => {+ vi.useFakeTimers();+ const editor = document.createElement('span');+ editor.contentEditable = 'true';+ editor.textContent = '工作表 1';+ document.body.appendChild(editor);+ editor.focus();++ keepTextSelected(editor);+ vi.runAllTimers();++ const selection = window.getSelection()!;+ expect(selection.isCollapsed).toBe(false);+ expect(selection.toString()).toBe('工作表 1');+ });+});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.tsindex 27b2ec0231..d0177e6cf9 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@@ -52,10 +52,16 @@ export function keepLastTextIndex(inputHtml: HTMLElement) {export function keepTextSelected(inputHtml: HTMLElement) {setTimeout(() => {const selection = window.getSelection();- if (!selection) return;+ if (!selection) {+ return;+ }const range = document.createRange();range.selectNodeContents(inputHtml);+ // An empty contenteditable can retain a <br>, which must not become a non-collapsed selection.+ if (!getEditorText(inputHtml)) {+ range.collapse(true);+ }selection.removeAllRanges();selection.addRange(range);