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(sheet): only one sheet, disable remove button (#1110)

Dushusir358d6a0fd4f1
diff --git a/packages/sheets-ui/src/controllers/menu/sheet.menu.ts b/packages/sheets-ui/src/controllers/menu/sheet.menu.ts
index f18bbb5d38..7bccb73465 100644
--- a/packages/sheets-ui/src/controllers/menu/sheet.menu.ts
+++ b/packages/sheets-ui/src/controllers/menu/sheet.menu.ts
@@ -14,11 +14,14 @@
* limitations under the License.
*/
-import { ICommandService, IUniverInstanceService } from '@univerjs/core';
+import { BooleanNumber, ICommandService, IUniverInstanceService } from '@univerjs/core';
import {
CopySheetCommand,
+ InsertSheetMutation,
+ RemoveSheetMutation,
SetTabColorCommand,
SetWorksheetHideCommand,
+ SetWorksheetHideMutation,
SetWorksheetShowCommand,
} from '@univerjs/sheets';
import type { IMenuButtonItem, IMenuSelectorItem } from '@univerjs/ui';
@@ -32,12 +35,34 @@ import { RenameSheetOperation } from '../../commands/operations/rename-sheet.ope
import { COLOR_PICKER_COMPONENT } from '../../components/color-picker';
import { SheetMenuPosition } from './menu';
-export function DeleteSheetMenuItemFactory(): IMenuButtonItem {
+export function DeleteSheetMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
+ const univerInstanceService = accessor.get(IUniverInstanceService);
+ const commandService = accessor.get(ICommandService);
return {
id: RemoveSheetConfirmCommand.id,
type: MenuItemType.BUTTON,
positions: [SheetMenuPosition.SHEET_BAR],
title: 'sheetConfig.delete',
+ disabled$: new Observable<boolean>((subscriber) => {
+ const disposable = commandService.onCommandExecuted((c) => {
+ const id = c.id;
+ if (
+ id === RemoveSheetMutation.id ||
+ id === InsertSheetMutation.id ||
+ id === SetWorksheetHideMutation.id
+ ) {
+ const worksheets = univerInstanceService.getCurrentUniverSheetInstance().getWorksheets();
+ // loop through all worksheets Map to see if there is more than one visible sheet
+ const visibleSheets = Array.from(worksheets.values()).filter(
+ (sheet) => sheet.getConfig().hidden === BooleanNumber.FALSE
+ );
+
+ subscriber.next(visibleSheets.length === 1);
+ }
+ });
+ subscriber.next(false);
+ return disposable.dispose;
+ }),
};
}
@@ -76,12 +101,34 @@ export function ChangeColorSheetMenuItemFactory(): IMenuSelectorItem<string> {
};
}
-export function HideSheetMenuItemFactory(): IMenuButtonItem {
+export function HideSheetMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
+ const univerInstanceService = accessor.get(IUniverInstanceService);
+ const commandService = accessor.get(ICommandService);
return {
id: SetWorksheetHideCommand.id,
type: MenuItemType.BUTTON,
positions: [SheetMenuPosition.SHEET_BAR],
title: 'sheetConfig.hide',
+ disabled$: new Observable<boolean>((subscriber) => {
+ const disposable = commandService.onCommandExecuted((c) => {
+ const id = c.id;
+ if (
+ id === RemoveSheetMutation.id ||
+ id === InsertSheetMutation.id ||
+ id === SetWorksheetHideMutation.id
+ ) {
+ const worksheets = univerInstanceService.getCurrentUniverSheetInstance().getWorksheets();
+ // loop through all worksheets Map to see if there is more than one visible sheet
+ const visibleSheets = Array.from(worksheets.values()).filter(
+ (sheet) => sheet.getConfig().hidden === BooleanNumber.FALSE
+ );
+
+ subscriber.next(visibleSheets.length === 1);
+ }
+ });
+ subscriber.next(false);
+ return disposable.dispose;
+ }),
};
}
@@ -127,11 +174,31 @@ export function UnHideSheetMenuItemFactory(accessor: IAccessor): IMenuSelectorIt
};
}
-export function ShowMenuItemFactory(): IMenuButtonItem {
+export function ShowMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
+ const univerInstanceService = accessor.get(IUniverInstanceService);
+ const commandService = accessor.get(ICommandService);
return {
id: ShowMenuListCommand.id,
type: MenuItemType.BUTTON,
positions: [SheetMenuPosition.SHEET_BAR],
title: 'sheetConfig.unhide',
+ disabled$: new Observable<boolean>((subscriber) => {
+ const disposable = commandService.onCommandExecuted((c) => {
+ const id = c.id;
+ if (
+ id === RemoveSheetMutation.id ||
+ id === InsertSheetMutation.id ||
+ id === SetWorksheetHideMutation.id
+ ) {
+ const worksheets = univerInstanceService.getCurrentUniverSheetInstance().getWorksheets();
+ // loop through all worksheets Map to see if there is more than one sheet
+ const visibleSheets = Array.from(worksheets.values());
+
+ subscriber.next(visibleSheets.length === 1);
+ }
+ });
+ subscriber.next(false);
+ return disposable.dispose;
+ }),
};
}
diff --git a/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/SheetBarItem.tsx b/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/SheetBarItem.tsx
index 47f17e30cc..96dd7c7ef6 100644
--- a/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/SheetBarItem.tsx
+++ b/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/SheetBarItem.tsx
@@ -15,13 +15,10 @@
*/
import type { BooleanNumber } from '@univerjs/core';
-import { ColorKit, ICommandService, ThemeService } from '@univerjs/core';
-import { Dropdown } from '@univerjs/design';
-import { Menu } from '@univerjs/ui';
+import { ColorKit, ThemeService } from '@univerjs/core';
import { useDependency } from '@wendellhu/redi/react-bindings';
import React, { useEffect, useState } from 'react';
-import { SheetMenuPosition } from '../../../controllers/menu/menu';
import styles from './index.module.less';
export interface IBaseSheetBarProps {
@@ -38,10 +35,8 @@ export interface IBaseSheetBarProps {
export function SheetBarItem(props: IBaseSheetBarProps) {
const { sheetId, label, color, selected } = props;
- const [visible, setVisible] = useState(false);
const [currentSelected, setCurrentSelected] = useState(selected);
- const commandService = useDependency(ICommandService);
const themeService = useDependency(ThemeService);
useEffect(() => {
@@ -49,10 +44,6 @@ export function SheetBarItem(props: IBaseSheetBarProps) {
setCurrentSelected(selected);
}, [selected]);
- const onVisibleChange = (visible: boolean) => {
- setVisible(visible);
- };
-
const getTextColor = (color: string) => {
const theme = themeService.getCurrentTheme();
const darkTextColor = theme.textColor;
@@ -61,36 +52,18 @@ export function SheetBarItem(props: IBaseSheetBarProps) {
};
return (
- <Dropdown
- visible={visible}
- trigger={['contextMenu']}
- overlay={
- <Menu
- menuType={SheetMenuPosition.SHEET_BAR}
- onOptionSelect={(params) => {
- const { label: commandId, value } = params;
- commandService.executeCommand(commandId as string, { value, subUnitId: sheetId });
- setVisible(false);
- }}
- />
- }
- onVisibleChange={onVisibleChange}
+ <div
+ key={sheetId}
+ data-id={sheetId}
+ className={currentSelected ? `${styles.slideTabActive} ${styles.slideTabItem}` : styles.slideTabItem}
+ style={{
+ backgroundColor: !currentSelected && color ? color : '',
+ color: !currentSelected && color ? getTextColor(color) : '',
+ boxShadow:
+ currentSelected && color ? `0px 0px 8px rgba(0, 0, 0, 0.08), inset 0px -2px 0px 0px ${color}` : '',
+ }}
>
- <div
- key={sheetId}
- data-id={sheetId}
- className={currentSelected ? `${styles.slideTabActive} ${styles.slideTabItem}` : styles.slideTabItem}
- style={{
- backgroundColor: !currentSelected && color ? color : '',
- color: !currentSelected && color ? getTextColor(color) : '',
- boxShadow:
- currentSelected && color
- ? `0px 0px 8px rgba(0, 0, 0, 0.08), inset 0px -2px 0px 0px ${color}`
- : '',
- }}
- >
- <span className={styles.slideTabSpan}>{label}</span>
- </div>
- </Dropdown>
+ <span className={styles.slideTabSpan}>{label}</span>
+ </div>
);
}
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.tsx
index a50e931919..422bded3f3 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
@@ -16,6 +16,7 @@
import type { ICommandInfo } from '@univerjs/core';
import { BooleanNumber, ICommandService, IUniverInstanceService, LocaleService } from '@univerjs/core';
+import { Dropdown } from '@univerjs/design';
import { ITextSelectionRenderManager } from '@univerjs/engine-render';
import {
InsertSheetMutation,
@@ -29,10 +30,11 @@ import {
SetWorksheetOrderCommand,
SetWorksheetOrderMutation,
} from '@univerjs/sheets';
-import { IConfirmService } from '@univerjs/ui';
+import { IConfirmService, Menu } from '@univerjs/ui';
import { useDependency, useInjector } from '@wendellhu/redi/react-bindings';
import React, { useEffect, useRef, useState } from 'react';
+import { SheetMenuPosition } from '../../../controllers/menu/menu';
import { ISheetBarService } from '../../../services/sheet-bar/sheet-bar.service';
import styles from './index.module.less';
import type { IBaseSheetBarProps } from './SheetBarItem';
@@ -46,6 +48,8 @@ export function SheetBarTabs() {
const [sheetList, setSheetList] = useState<IBaseSheetBarProps[]>([]);
const [activeKey, setActiveKey] = useState('');
const [boxShadow, setBoxShadow] = useState('');
+ const [visible, setVisible] = useState(false);
+ const [offset, setOffset] = useState([0, 0]);
const slideTabBarRef = useRef<{ slideTabBar: SlideTabBar | null }>({ slideTabBar: null });
const slideTabBarContainerRef = useRef<HTMLDivElement>(null);
@@ -100,11 +104,19 @@ export function SheetBarTabs() {
onSlideEnd: (event: Event, order: number) => {
commandService.executeCommand(SetWorksheetOrderCommand.id, { order });
},
- onChangeTab: (event: Event, subUnitId: string) => {
- commandService.executeCommand(SetWorksheetActivateCommand.id, {
- subUnitId,
- unitId: workbook.getUnitId(),
- });
+ onChangeTab: (event: MouseEvent, subUnitId: string) => {
+ commandService
+ .executeCommand(SetWorksheetActivateCommand.id, {
+ subUnitId,
+ unitId: workbook.getUnitId(),
+ })
+ .then(() => {
+ // right click to show menu
+ if (event.button === 2) {
+ onVisibleChange(true);
+ }
+ });
+
// The 'onChangeTab' event occurs during the 'pointerDown' event.
// The triggering time is too early.
// Settimeout is required to delay resetting the focus.
@@ -283,7 +295,7 @@ export function SheetBarTabs() {
const slideTabBarContainer = slideTabBarContainerRef.current?.querySelector(`.${styles.slideTabBar}`);
if (!slideTabBarContainer) return;
- // Create a Resizeobserver
+ // Create a ResizeObserver
const observer = new ResizeObserver(() => {
buttonScroll(slideTabBar);
});
@@ -292,13 +304,45 @@ export function SheetBarTabs() {
observer.observe(slideTabBarContainer);
};
+ const onVisibleChange = (visible: boolean) => {
+ if (visible) {
+ const { left: containerLeft } = slideTabBarContainerRef.current?.getBoundingClientRect() ?? {};
+ // current active tab position
+ const { left: activeTabLeft } =
+ slideTabBarRef.current.slideTabBar?.getActiveItem()?.getSlideTabItem().getBoundingClientRect() ?? {};
+
+ if (containerLeft !== undefined && activeTabLeft !== undefined) {
+ setOffset([activeTabLeft - containerLeft, 0]);
+ }
+ }
+ setVisible(visible);
+ };
+
return (
- <div className={styles.slideTabBarContainer} ref={slideTabBarContainerRef}>
- <div className={styles.slideTabBar} style={{ boxShadow }}>
- {sheetList.map((item) => (
- <SheetBarItem {...item} key={item.sheetId} selected={activeKey === item.sheetId} />
- ))}
+ <Dropdown
+ className={styles.slideTabItemDropdown}
+ visible={visible}
+ align={{ offset }}
+ trigger={['contextMenu']}
+ overlay={
+ <Menu
+ menuType={SheetMenuPosition.SHEET_BAR}
+ onOptionSelect={(params) => {
+ const { label: commandId, value } = params;
+ commandService.executeCommand(commandId as string, { value, subUnitId: activeKey });
+ setVisible(false);
+ }}
+ />
+ }
+ onVisibleChange={onVisibleChange}
+ >
+ <div className={styles.slideTabBarContainer} ref={slideTabBarContainerRef}>
+ <div className={styles.slideTabBar} style={{ boxShadow }}>
+ {sheetList.map((item) => (
+ <SheetBarItem {...item} key={item.sheetId} selected={activeKey === item.sheetId} />
+ ))}
+ </div>
</div>
- </div>
+ </Dropdown>
);
}
diff --git a/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/index.module.less b/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/index.module.less
index 6fb6ebae3f..366229f517 100644
--- a/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/index.module.less
+++ b/packages/sheets-ui/src/views/sheet-bar/sheet-bar-tabs/index.module.less
@@ -75,3 +75,8 @@ Tab Item, Put outside to be compatible with drag styles
transition: box-shadow .2s;
}
+
+.slide-tab-item-dropdown{
+ user-select: none;
+ min-width: auto !important;
+}
\ No newline at end of file
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 dd18729075..8f4e6e9d3c 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
@@ -30,7 +30,7 @@ export interface SlideTabBarConfig {
currentIndex: number;
onSlideEnd: (event: MouseEvent, compareIndex: number) => void;
onChangeName: (id: string, name: string) => void;
- onChangeTab: (event: FocusEvent, id: string) => void;
+ onChangeTab: (event: MouseEvent, id: string) => void;
onScroll: (state: IScrollState) => void;
onNameCheckAlert: (text: string) => boolean;
}