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): account for sheet bar scroll while dragging tabs (#7220)
白熱431dffbc895e
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 0364ae3ccd..3d50fa0e75 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@@ -190,8 +190,10 @@ export function SheetBarTabs() {return true;}, [localeService, openSheetNameErrorDialog]);- const nameRepeatCheck = useCallback((name: string) => {- const currentSheetName = workbook.getActiveSheet()?.getName();+ const nameRepeatCheck = useCallback((name: string, subUnitId?: string) => {+ const currentSheetName = subUnitId+ ? workbook.getSheetBySheetId(subUnitId)?.getName()+ : workbook.getActiveSheet()?.getName();if (currentSheetName === name) {return false;}@@ -518,8 +520,8 @@ export function SheetBarTabs() {onScroll: (state: IScrollState) => {sheetBarService.setScroll(state);},- onNameCheckAlert: (name: string) => {- return nameEmptyCheck(name) || sheetNameSpecCharCheck(name) || nameRepeatCheck(name);+ onNameCheckAlert: (name: string, subUnitId?: string) => {+ return nameEmptyCheck(name) || sheetNameSpecCharCheck(name) || nameRepeatCheck(name, subUnitId);},onNameChangeCheck: canRenameActiveSheet,});diff --git a/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/utils/__tests__/slide-tab-bar.spec.ts b/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/utils/__tests__/slide-tab-bar.spec.tsindex b09cd50d6b..6529fd59da 100644--- a/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/utils/__tests__/slide-tab-bar.spec.ts+++ b/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/utils/__tests__/slide-tab-bar.spec.ts@@ -14,7 +14,7 @@* limitations under the License.*/-import { afterEach, describe, expect, it } from 'vitest';+import { afterEach, describe, expect, it, vi } from 'vitest';import { SlideTabBar, SlideTabItem } from '../slide-tab-bar';const originalRequestAnimationFrame = window.requestAnimationFrame;@@ -133,6 +133,7 @@ function createBar(root: HTMLDivElement, events: TestSlideEvents, currentIndex =describe('slide-tab-bar', () => {afterEach(() => {+ vi.useRealTimers();window.requestAnimationFrame = originalRequestAnimationFrame;window.cancelAnimationFrame = originalCancelAnimationFrame;console.error = originalConsoleError;@@ -216,6 +217,63 @@ describe('slide-tab-bar', () => {expect(frameState.cancellations).toEqual([1]);});+ it('keeps tab interactions available after finishing sheet name editing', () => {+ const { root, events, bar: barElement } = setupDOM();+ const bar = createBar(root, events);+ const firstItem = bar.getSlideTabItems()[0];+ const firstEditor = firstItem.getEditor() as HTMLSpanElement;+ const secondTab = barElement.querySelectorAll<HTMLElement>('.tab-item')[1];++ firstItem.setEditor();+ firstEditor.textContent = 'Renamed';+ firstEditor.dispatchEvent(new FocusEvent('focusout'));++ secondTab.dispatchEvent(new PointerEvent('pointerdown', { bubbles: true }));++ expect(events.changedTabs).toEqual([{ event: expect.any(PointerEvent), id: 'sheet-1' }]);+ });++ it('does not start delayed dragging after a double click enters name editing', () => {+ vi.useFakeTimers();+ const { root, bar: barElement } = setupDOM();+ createBar(root, new TestSlideEvents());++ const tabElement = barElement.querySelector<HTMLElement>('.tab-item')!;+ const editor = tabElement.querySelector<HTMLSpanElement>('span')!;+ const originalSetPointerCapture = tabElement.setPointerCapture;+ const originalReleasePointerCapture = tabElement.releasePointerCapture;+ const originalHasPointerCapture = tabElement.hasPointerCapture;++ tabElement.setPointerCapture = () => {};+ tabElement.releasePointerCapture = () => {};+ tabElement.hasPointerCapture = () => true;++ const dispatchPointer = (type: 'pointerdown' | 'pointerup') => {+ tabElement.dispatchEvent(new PointerEvent(type, {+ bubbles: true,+ clientX: 8,+ clientY: 8,+ pointerId: 1,+ }));+ };++ dispatchPointer('pointerdown');+ dispatchPointer('pointerup');+ dispatchPointer('pointerdown');+ expect(editor.getAttribute('contentEditable')).toBe('true');+ dispatchPointer('pointerup');++ editor.dispatchEvent(new FocusEvent('focusout'));+ vi.advanceTimersByTime(SlideTabBar.LongPressDelay + 1);++ expect(tabElement.parentElement).not.toBe(document.body);+ expect(tabElement.style.position).toBe('');++ tabElement.setPointerCapture = originalSetPointerCapture;+ tabElement.releasePointerCapture = originalReleasePointerCapture;+ tabElement.hasPointerCapture = originalHasPointerCapture;+ });+it('starts or closes edge auto-scroll during sheet tab dragging', () => {const { root, events } = setupDOM();const frameState = new TestAnimationFrameState();@@ -245,6 +303,22 @@ describe('slide-tab-bar', () => {expect(frameState.cancellations).toContain(2);});+ it('includes sheet bar scroll delta when calculating the drag target index', () => {+ const { root } = setupDOM();+ const bar = createBar(root, new TestSlideEvents(), 1);+ const active = bar.getSlideTabItems()[1];++ (bar as unknown as { _activeTabItem: SlideTabItem })._activeTabItem = active;+ (bar as unknown as { _activeTabItemIndex: number })._activeTabItemIndex = 1;+ (bar as unknown as { _dragStartScrollX: number })._dragStartScrollX = 0;+ (bar as unknown as { _moveActionX: number })._moveActionX = 0;++ bar.getScrollbar().scrollX(50);+ (bar as unknown as { _updateDragSortState: () => void })._updateDragSortState();++ expect((bar as unknown as { _compareIndex: number })._compareIndex).toBe(2);+ });+it('handles static text-selection and slide-skip helpers', () => {const el = document.createElement('span');el.textContent = 'abc';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.tsindex b007a6a6e4..4b76c717bd 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@@ -29,7 +29,7 @@ export interface ISlideTabBarConfig {onChangeName: (id: string, name: string) => void;onChangeTab: (event: MouseEvent, id: string) => void;onScroll: (state: IScrollState) => void;- onNameCheckAlert: (text: string) => boolean;+ onNameCheckAlert: (text: string, id?: string) => boolean;onNameChangeCheck: () => boolean;}@@ -123,7 +123,7 @@ export class SlideTabItem {startSheetTabNameEditor({slideTabItem: this._slideTabItem,canStart: () => this._slideTabBar.getConfig().onNameChangeCheck(),- checkName: (name) => this._slideTabBar.getConfig().onNameCheckAlert(name),+ checkName: (name) => this._slideTabBar.getConfig().onNameCheckAlert(name, this.getId()),setEditMode: (editing) => {this._editMode = editing;},@@ -132,9 +132,13 @@ export class SlideTabItem {this._slideTabBar.updateItems();},onCommit: (name, focusEvent) => {- this._slideTabBar.getConfig().onChangeName(this.getId(), name);- if (callback) {- callback(focusEvent);+ try {+ this._slideTabBar.getConfig().onChangeName(this.getId(), name);+ if (callback) {+ callback(focusEvent);+ }+ } finally {+ this._slideTabBar.addListener();}},});@@ -145,7 +149,7 @@ export class SlideTabItem {if (!input) return false;const text = input.textContent ?? '';- const checkAlert = this._slideTabBar.getConfig().onNameCheckAlert(text);+ const checkAlert = this._slideTabBar.getConfig().onNameCheckAlert(text, this.getId());return checkAlert;}@@ -353,6 +357,8 @@ export class SlideTabBar {protected _scrollIncremental: number = 0;+ protected _dragStartScrollX: number = 0;+protected _compareDirection: number = 0;protected _autoScrollTime: number | null = null;@@ -433,6 +439,7 @@ export class SlideTabBar {this._downActionX = downEvent.pageX;this._moveActionX = 0;this._scrollIncremental = 0;+ this._dragStartScrollX = this._slideScrollbar.getScrollX();this._activeTabItem = this._slideTabItems[slideItemIndex];if (!this._activeTabItem) {console.error('Not found active slide-tab-item in sheet bar');@@ -485,10 +492,6 @@ export class SlideTabBar {};this._upAction = (upEvent: MouseEvent) => {- if (this._activeTabItem?.isEditMode()) {- return;- }-// Clear timerif (this._longPressTimer) {clearTimeout(this._longPressTimer);@@ -497,6 +500,16 @@ export class SlideTabBar {if (!this._activeTabItem) return;+ if (this._activeTabItem.isEditMode()) {+ const activeSlideItemElement = this._activeTabItem.getSlideTabItem();+ const pointerId = (upEvent as PointerEvent).pointerId;+ if (pointerId != null && activeSlideItemElement.hasPointerCapture(pointerId)) {+ activeSlideItemElement.releasePointerCapture(pointerId);+ }+ this._activeTabItem.removeEventListener('pointerup', this._upAction);+ return;+ }+// When blurring after editing the table name, _activeTabItemIndex and _compareIndex may not be equal, causing slideEnd to be triggeredconst isFromScroll = this._autoScrollTime !== null;@@ -514,7 +527,11 @@ export class SlideTabBar {this._activeTabItem?.removeEventListener('pointermove', this._moveAction);this._activeTabItem?.removeEventListener('pointerup', this._upAction);- if (this._config.onSlideEnd && this._activeTabItemIndex !== this._compareIndex && isFromScroll) {+ if (+ this._config.onSlideEnd &&+ this._activeTabItemIndex !== this._compareIndex &&+ isFromScroll+ ) {this.removeListener();this._config.onSlideEnd(upEvent, this._compareIndex || 0);}@@ -522,6 +539,7 @@ export class SlideTabBar {this._scrollIncremental = 0;this._downActionX = 0;this._moveActionX = 0;+ this._dragStartScrollX = this._slideScrollbar.getScrollX();this._compareIndex = 0;};@@ -783,6 +801,7 @@ export class SlideTabBar {this._downActionX = 0;this._moveActionX = 0;+ this._dragStartScrollX = this._slideScrollbar.getScrollX();this._compareDirection = 0;this._compareIndex = 0;this._slideTabItems = [];@@ -877,6 +896,7 @@ export class SlideTabBar {return;}+ const dragSortOffsetX = this._moveActionX + this._slideScrollbar.getScrollX() - this._dragStartScrollX;const dragSortResult = calculateSheetTabDragSort(this._slideTabItems.map((item) => ({id: item.getId(),@@ -884,7 +904,7 @@ export class SlideTabBar {width: item.getWidth(),})),this._activeTabItemIndex,- this._moveActionX+ dragSortOffsetX);this._compareIndex = dragSortResult.targetIndex;@@ -895,7 +915,7 @@ export class SlideTabBar {}if (item.equals(this._activeTabItem)) {- this._compareDirection = item.translateX(offset);+ this._compareDirection = item.translateX(this._moveActionX);return;}