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(ui): reopen shortcut panel after sidebar dismissal (#7889)

白熱7ec244371c29
diff --git a/packages/ui/src/commands/operations/__tests__/toggle-shortcut-panel.operation.spec.ts b/packages/ui/src/commands/operations/__tests__/toggle-shortcut-panel.operation.spec.ts
index 138080f290..01c8ffad52 100644
--- a/packages/ui/src/commands/operations/__tests__/toggle-shortcut-panel.operation.spec.ts
+++ b/packages/ui/src/commands/operations/__tests__/toggle-shortcut-panel.operation.spec.ts
@@ -14,53 +14,61 @@
* limitations under the License.
*/
-import type { IAccessor } from '@univerjs/core';
-import { describe, expect, it, vi } from 'vitest';
+import { Injector } from '@univerjs/core';
+import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { ShortcutPanelService } from '../../../services/shortcut/shortcut-panel.service';
+import { DesktopSidebarService } from '../../../services/sidebar/desktop-sidebar.service';
import { ISidebarService } from '../../../services/sidebar/sidebar.service';
import { ShortcutPanelComponentName, ToggleShortcutPanelOperation } from '../toggle-shortcut-panel.operation';
-function createAccessor(isOpen: boolean) {
- const shortcutPanelService = {
- isOpen,
- open: vi.fn(),
- close: vi.fn(),
- } as unknown as ShortcutPanelService;
+describe('ToggleShortcutPanelOperation', () => {
+ let injector: Injector;
- const sidebarService = {
- open: vi.fn(),
- close: vi.fn(),
- };
+ beforeEach(() => {
+ injector = new Injector([
+ [ShortcutPanelService],
+ [ISidebarService, { useClass: DesktopSidebarService }],
+ ]);
+ });
- const accessor = {
- get: (token: unknown) => {
- if (token === ShortcutPanelService) return shortcutPanelService;
- if (token === ISidebarService) return sidebarService;
- throw new Error('Unknown token');
- },
- } as unknown as IAccessor;
+ afterEach(() => injector.dispose());
- return { accessor, shortcutPanelService, sidebarService };
-}
+ it('should close shortcut panel and sidebar when toggled twice', () => {
+ const shortcutPanelService = injector.get(ShortcutPanelService);
+ const sidebarService = injector.get(ISidebarService);
-describe('ToggleShortcutPanelOperation', () => {
- it('should close shortcut panel and sidebar when panel is open', () => {
- const { accessor, shortcutPanelService, sidebarService } = createAccessor(true);
+ ToggleShortcutPanelOperation.handler!(injector, {});
- expect(ToggleShortcutPanelOperation.handler!(accessor, {})).toBe(true);
- expect(shortcutPanelService.close).toHaveBeenCalledOnce();
- expect(sidebarService.close).toHaveBeenCalledOnce();
- expect(sidebarService.open).not.toHaveBeenCalled();
+ expect(ToggleShortcutPanelOperation.handler!(injector, {})).toBe(true);
+ expect(shortcutPanelService.isOpen).toBe(false);
+ expect(sidebarService.visible).toBe(false);
});
it('should open shortcut panel and sidebar when panel is closed', () => {
- const { accessor, shortcutPanelService, sidebarService } = createAccessor(false);
+ const shortcutPanelService = injector.get(ShortcutPanelService);
+ const sidebarService = injector.get(ISidebarService);
- expect(ToggleShortcutPanelOperation.handler!(accessor, {})).toBe(true);
- expect(shortcutPanelService.open).toHaveBeenCalledOnce();
- expect(sidebarService.open).toHaveBeenCalledWith({
+ expect(ToggleShortcutPanelOperation.handler!(injector, {})).toBe(true);
+ expect(shortcutPanelService.isOpen).toBe(true);
+ expect(sidebarService.options).toMatchObject({
+ visible: true,
header: { title: 'ui.shortcut-panel.title' },
children: { label: ShortcutPanelComponentName },
});
});
+
+ it('should reopen on the first toggle after the sidebar is closed directly', () => {
+ const shortcutPanelService = injector.get(ShortcutPanelService);
+ const sidebarService = injector.get(ISidebarService);
+
+ ToggleShortcutPanelOperation.handler!(injector, {});
+ sidebarService.close();
+
+ expect(shortcutPanelService.isOpen).toBe(false);
+
+ ToggleShortcutPanelOperation.handler!(injector, {});
+
+ expect(sidebarService.visible).toBe(true);
+ expect(sidebarService.options.children?.label).toBe(ShortcutPanelComponentName);
+ });
});
diff --git a/packages/ui/src/commands/operations/toggle-shortcut-panel.operation.ts b/packages/ui/src/commands/operations/toggle-shortcut-panel.operation.ts
index e710a671f6..c1830a09fe 100644
--- a/packages/ui/src/commands/operations/toggle-shortcut-panel.operation.ts
+++ b/packages/ui/src/commands/operations/toggle-shortcut-panel.operation.ts
@@ -38,6 +38,7 @@ export const ToggleShortcutPanelOperation: IOperation = {
sidebarService.open({
header: { title: 'ui.shortcut-panel.title' },
children: { label: ShortcutPanelComponentName },
+ onClose: () => shortcutPanelService.close(),
});
}
diff --git a/packages/ui/src/views/components/sidebar/__tests__/MobileSidebar.spec.tsx b/packages/ui/src/views/components/sidebar/__tests__/MobileSidebar.spec.tsx
new file mode 100644
index 0000000000..1e3061d42b
--- /dev/null
+++ b/packages/ui/src/views/components/sidebar/__tests__/MobileSidebar.spec.tsx
@@ -0,0 +1,69 @@
+/**
+ * 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 { act, cleanup, fireEvent, render, screen, within } from '@testing-library/react';
+import { DesktopLogService, ILogService, Injector, LocaleService, LocaleType } from '@univerjs/core';
+import { afterEach, describe, expect, it } from 'vitest';
+import { ComponentManager } from '../../../../common/component-manager';
+import { IconManager } from '../../../../common/icon-manager';
+import enUS from '../../../../locale/en-US';
+import { DesktopSidebarService } from '../../../../services/sidebar/desktop-sidebar.service';
+import { ISidebarService } from '../../../../services/sidebar/sidebar.service';
+import { RediProvider } from '../../../../utils/di';
+import { MobileSidebar } from '../MobileSidebar';
+
+(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
+
+describe('MobileSidebar', () => {
+ let injector: Injector;
+
+ afterEach(() => {
+ cleanup();
+ injector.dispose();
+ });
+
+ it('renders sidebar content in a modal drawer that can be closed', () => {
+ injector = new Injector([
+ [ISidebarService, { useClass: DesktopSidebarService }],
+ [ILogService, { useClass: DesktopLogService }],
+ [LocaleService],
+ [ComponentManager],
+ [IconManager],
+ ]);
+ const localeService = injector.get(LocaleService);
+ localeService.load({ [LocaleType.EN_US]: enUS });
+ localeService.setLocale(LocaleType.EN_US);
+ const sidebarService = injector.get(ISidebarService);
+ render(<RediProvider value={{ injector }}><MobileSidebar /></RediProvider>);
+
+ act(() => {
+ sidebarService.open({
+ id: 'comments',
+ header: { title: <span>Comments</span> },
+ children: { title: <span>Comment list</span> },
+ });
+ });
+
+ const drawer = screen.getByRole('dialog', { name: 'Sidebar panel' });
+ expect(drawer.getAttribute('aria-modal')).toBe('true');
+ expect(within(drawer).getByText('Comment list')).toBeTruthy();
+
+ fireEvent.click(within(drawer).getByRole('button', { name: 'Close sidebar' }));
+
+ expect(sidebarService.visible).toBe(false);
+ expect(screen.queryByRole('dialog', { name: 'Sidebar panel' })).toBeNull();
+ });
+});
diff --git a/packages/ui/src/views/components/sidebar/__tests__/Sidebar.spec.tsx b/packages/ui/src/views/components/sidebar/__tests__/Sidebar.spec.tsx
index 32828c2282..21a08b8476 100644
--- a/packages/ui/src/views/components/sidebar/__tests__/Sidebar.spec.tsx
+++ b/packages/ui/src/views/components/sidebar/__tests__/Sidebar.spec.tsx
@@ -18,24 +18,16 @@ import type { ReactElement } from 'react';
import { act, cleanup, fireEvent, render, screen } from '@testing-library/react';
import { DesktopLogService, ILogService, Injector, LocaleService, LocaleType } from '@univerjs/core';
import { afterEach, describe, expect, it } from 'vitest';
-import { ComponentManager, IconManager } from '../../../../common';
+import { ComponentManager } from '../../../../common/component-manager';
+import { IconManager } from '../../../../common/icon-manager';
import enUS from '../../../../locale/en-US';
import { DesktopSidebarService } from '../../../../services/sidebar/desktop-sidebar.service';
import { ISidebarService } from '../../../../services/sidebar/sidebar.service';
import { RediProvider } from '../../../../utils/di';
-import { MobileSidebar } from '../MobileSidebar';
import { Sidebar } from '../Sidebar';
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
-class TestState {
- static closes = 0;
-
- static reset(): void {
- this.closes = 0;
- }
-}
-
function renderWithDependencies(element: ReactElement) {
const injector = new Injector();
injector.add([ISidebarService, { useClass: DesktopSidebarService }]);
@@ -67,12 +59,12 @@ describe('Sidebar', () => {
cleanup();
document.body.style.cursor = '';
document.body.style.userSelect = '';
- TestState.reset();
});
it('closes the active panel through the close button and reports the service state', () => {
const rendered = renderWithDependencies(<Sidebar />);
const sidebarService = rendered.injector.get(ISidebarService);
+ let closes = 0;
act(() => {
sidebarService.open({
@@ -80,7 +72,7 @@ describe('Sidebar', () => {
header: { title: <span>Inspector</span> },
children: { title: <span>Cell details</span> },
onClose: () => {
- TestState.closes += 1;
+ closes += 1;
},
});
});
@@ -90,7 +82,7 @@ describe('Sidebar', () => {
fireEvent.click(screen.getByRole('button', { name: 'Close sidebar' }));
expect(sidebarService.visible).toBe(false);
- expect(TestState.closes).toBe(1);
+ expect(closes).toBe(1);
expect(screen.queryByRole('complementary', { name: 'Sidebar panel' })).toBeNull();
expect(rendered.container.querySelector('[data-u-comp="sidebar"]')?.getAttribute('aria-expanded')).toBe('false');
rendered.dispose();
@@ -122,7 +114,7 @@ describe('Sidebar', () => {
expect(document.activeElement).toBe(opener);
expect(screen.queryByRole('complementary', { name: 'Sidebar panel' })).toBeNull();
- expect(rendered.container.querySelector('[data-u-comp="sidebar"]')?.className).toContain('univer-invisible');
+ expect(screen.queryByRole('button', { name: 'Focusable content' })).toBeNull();
rendered.dispose();
});
@@ -170,25 +162,4 @@ describe('Sidebar', () => {
expect(sidebarService.width).toBe(800);
rendered.dispose();
});
-
- it('renders sidebar content as a modal bottom drawer on mobile', () => {
- const rendered = renderWithDependencies(<MobileSidebar />);
- const sidebarService = rendered.injector.get(ISidebarService);
-
- act(() => {
- sidebarService.open({
- id: 'comments',
- header: { title: <span>Comments</span> },
- children: { title: <span>Comment list</span> },
- });
- });
-
- const drawer = screen.getByRole('dialog', { name: 'Sidebar panel' });
- expect(drawer.className).toContain('univer-bottom-0');
- expect(screen.getByText('Comment list')).toBeTruthy();
-
- fireEvent.click(screen.getAllByRole('button', { name: 'Close sidebar' })[0]);
- expect(screen.queryByRole('dialog', { name: 'Sidebar panel' })).toBeNull();
- rendered.dispose();
- });
});