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): sync toggle menus with panel and note visibility (#8101)

白熱48aa5726f032
diff --git a/packages/docs-thread-comment-ui/src/menu/menu.ts b/packages/docs-thread-comment-ui/src/menu/menu.ts
index 209301ce99..4413f9e9c0 100644
--- a/packages/docs-thread-comment-ui/src/menu/menu.ts
+++ b/packages/docs-thread-comment-ui/src/menu/menu.ts
@@ -18,13 +18,14 @@ import { IPermissionService, IUniverInstanceService, SHEET_EDITOR_UNITS, UniverI
import { DocSelectionManagerService, DocSkeletonManagerService, getDocumentPermissionValue } from '@univerjs/docs';
import { DocumentEditArea, IRenderManagerService, withCurrentTypeOfRenderer } from '@univerjs/engine-render';
import { UnitAction } from '@univerjs/protocol';
-import { getMenuHiddenObservable, MenuItemType } from '@univerjs/ui';
-import { combineLatest, debounceTime, map, startWith } from 'rxjs';
+import { getMenuHiddenObservable, ISidebarService, MenuItemType } from '@univerjs/ui';
+import { combineLatest, debounceTime, defer, distinctUntilChanged, map, startWith } from 'rxjs';
import {
AddDocDrawingCommentOperation,
StartAddCommentOperation,
ToggleCommentPanelOperation,
} from '../commands/operations/show-comment-panel.operation';
+import { DOCS_THREAD_COMMENT_PANEL } from '../common/const';
type MenuAccessor = Parameters<typeof getMenuHiddenObservable>[0];
@@ -110,7 +111,14 @@ export function AddDocCommentMenuItemFactory(accessor: MenuAccessor) {
}
export function ToolbarDocCommentMenuItemFactory(accessor: MenuAccessor) {
+ const sidebarService = accessor.get(ISidebarService);
+
return {
+ activated$: defer(() => sidebarService.sidebarOptions$.pipe(
+ startWith(sidebarService.options),
+ map((options) => options.visible === true && options.children?.label === DOCS_THREAD_COMMENT_PANEL),
+ distinctUntilChanged()
+ )),
id: ToggleCommentPanelOperation.id,
type: MenuItemType.BUTTON,
icon: 'CommentIcon',
diff --git a/packages/sheets-data-validation-ui/src/menu/__tests__/dv.menu.spec.ts b/packages/sheets-data-validation-ui/src/menu/__tests__/dv.menu.spec.ts
deleted file mode 100644
index 7a87a85a86..0000000000
--- a/packages/sheets-data-validation-ui/src/menu/__tests__/dv.menu.spec.ts
+++ /dev/null
@@ -1,28 +0,0 @@
-/**
- * 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 { describe, expect, it } from 'vitest';
-import { OpenValidationPanelOperation } from '../../commands/operations/data-validation.operation';
-import { openDataValidationMenuFactory } from '../dv.menu';
-
-describe('data validation menu', () => {
- it('passes the empty parameter object required to open the manager', () => {
- expect(openDataValidationMenuFactory()).toMatchObject({
- id: OpenValidationPanelOperation.id,
- params: {},
- });
- });
-});
diff --git a/packages/sheets-data-validation-ui/src/menu/dv.menu.ts b/packages/sheets-data-validation-ui/src/menu/dv.menu.ts
index 3f0324c2ff..ffa4285bdd 100644
--- a/packages/sheets-data-validation-ui/src/menu/dv.menu.ts
+++ b/packages/sheets-data-validation-ui/src/menu/dv.menu.ts
@@ -27,13 +27,14 @@ import {
WorksheetSetCellStylePermission,
} from '@univerjs/sheets';
import { getCurrentRangeDisable$ } from '@univerjs/sheets-ui';
-import { getMenuHiddenObservable, MenuItemType } from '@univerjs/ui';
+import { getMenuHiddenObservable, ISidebarService, MenuItemType } from '@univerjs/ui';
+import { defer, distinctUntilChanged, map, startWith } from 'rxjs';
import {
AddSheetDataValidationAndOpenCommand,
ClearQuickSheetDataValidationCommand,
InsertQuickSheetDataValidationCommand,
} from '../commands/commands/data-validation-ui.command';
-import { OpenValidationPanelOperation } from '../commands/operations/data-validation.operation';
+import { DATA_VALIDATION_PANEL, OpenValidationPanelOperation } from '../commands/operations/data-validation.operation';
import { DROPDOWN_PRESETS_COMPONENT } from '../views/components/DropdownPresets';
export const DATA_VALIDATION_MENU_ID = 'sheet.menu.data-validation';
@@ -200,8 +201,15 @@ export function quickDateMenuFactory(accessor: IAccessor): IMenuSelectorItem<Loc
};
}
-export function openDataValidationMenuFactory(): IMenuItem<LocaleKey> {
+export function openDataValidationMenuFactory(accessor: IAccessor): IMenuItem<LocaleKey> {
+ const sidebarService = accessor.get(ISidebarService);
+
return {
+ activated$: defer(() => sidebarService.sidebarOptions$.pipe(
+ startWith(sidebarService.options),
+ map((options) => options.visible === true && options.id === DATA_VALIDATION_PANEL),
+ distinctUntilChanged()
+ )),
id: OpenValidationPanelOperation.id,
title: 'sheets-data-validation-ui.panel.title',
type: MenuItemType.BUTTON,
diff --git a/packages/sheets-note-ui/src/menu/note.menu.ts b/packages/sheets-note-ui/src/menu/note.menu.ts
index 447cc1a968..c297d9a589 100644
--- a/packages/sheets-note-ui/src/menu/note.menu.ts
+++ b/packages/sheets-note-ui/src/menu/note.menu.ts
@@ -14,37 +14,45 @@
* limitations under the License.
*/
-import type { IAccessor } from '@univerjs/core';
+import type { IAccessor, Nullable } from '@univerjs/core';
+import type { ISheetNote } from '@univerjs/sheets-note';
import type { IMenuButtonItem } from '@univerjs/ui';
import type { Observable } from 'rxjs';
import type { LocaleKey } from '../locale/types';
import { IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
import {
- getSheetCommandTarget,
SheetsSelectionsService,
WorkbookEditablePermission,
WorksheetEditPermission,
} from '@univerjs/sheets';
import { SheetDeleteNoteCommand, SheetsNoteModel, SheetToggleNotePopupCommand } from '@univerjs/sheets-note';
-import { getCurrentRangeDisable$ } from '@univerjs/sheets-ui';
+import { deriveStateFromActiveSheet$, getCurrentRangeDisable$ } from '@univerjs/sheets-ui';
import { getMenuHiddenObservable, MenuItemType } from '@univerjs/ui';
-import { combineLatest, map } from 'rxjs';
+import { combineLatest, distinctUntilChanged, map, merge, startWith } from 'rxjs';
import { AddNotePopupOperation } from '../commands/operations/add-note-popup.operation';
export const SHEET_NOTE_CONTEXT_MENU_ID = 'sheet.menu.note';
-function getHasNote$(accessor: IAccessor): Observable<boolean> {
+function getSelectedNote$(accessor: IAccessor): Observable<Nullable<ISheetNote>> {
const sheetsSelectionsService = accessor.get(SheetsSelectionsService);
const univerInstanceService = accessor.get(IUniverInstanceService);
- return sheetsSelectionsService.selectionMoveEnd$.pipe(map(() => {
- const selection = sheetsSelectionsService.getCurrentLastSelection();
- if (!selection?.primary) return false;
- const target = getSheetCommandTarget(univerInstanceService);
- if (!target) return false;
- const { actualColumn, actualRow } = selection.primary;
- const noteModel = accessor.get(SheetsNoteModel);
- return Boolean(noteModel.getNote(target.unitId, target.subUnitId, { row: actualRow, col: actualColumn }));
- }));
+ const noteModel = accessor.get(SheetsNoteModel);
+
+ return deriveStateFromActiveSheet$<Nullable<ISheetNote>>(univerInstanceService, null, ({ workbook, worksheet }) =>
+ merge(sheetsSelectionsService.selectionChanged$, noteModel.change$).pipe(
+ startWith(null),
+ map(() => {
+ const selection = sheetsSelectionsService.getCurrentLastSelection();
+ if (!selection?.primary) {
+ return null;
+ }
+ const { actualColumn, actualRow } = selection.primary;
+ return noteModel.getNote(workbook.getUnitId(), worksheet.getSheetId(), {
+ row: actualRow,
+ col: actualColumn,
+ });
+ })
+ ));
}
export function sheetNoteContextMenuFactory(accessor: IAccessor): IMenuButtonItem<LocaleKey> {
@@ -55,8 +63,8 @@ export function sheetNoteContextMenuFactory(accessor: IAccessor): IMenuButtonIte
icon: 'AddNoteIcon',
hidden$: combineLatest([
getMenuHiddenObservable(accessor, UniverInstanceType.UNIVER_SHEET),
- getHasNote$(accessor),
- ]).pipe(map(([hidden, hasNote]) => hidden || hasNote)),
+ getSelectedNote$(accessor),
+ ]).pipe(map(([hidden, note]) => hidden || !!note)),
disabled$: getCurrentRangeDisable$(accessor, {
workbookTypes: [WorkbookEditablePermission],
worksheetTypes: [WorksheetEditPermission],
@@ -71,7 +79,7 @@ export function sheetDeleteNoteMenuFactory(accessor: IAccessor): IMenuButtonItem
type: MenuItemType.BUTTON,
title: 'sheets-note-ui.rightClick.deleteNote',
icon: 'DeleteNoteIcon',
- hidden$: getHasNote$(accessor).pipe(map((hasNote) => !hasNote)),
+ hidden$: getSelectedNote$(accessor).pipe(map((note) => !note), distinctUntilChanged()),
disabled$: getCurrentRangeDisable$(accessor, {
workbookTypes: [WorkbookEditablePermission],
worksheetTypes: [WorksheetEditPermission],
@@ -79,11 +87,14 @@ export function sheetDeleteNoteMenuFactory(accessor: IAccessor): IMenuButtonItem
};
}
export function sheetNoteToggleMenuFactory(accessor: IAccessor): IMenuButtonItem<LocaleKey> {
+ const selectedNote$ = getSelectedNote$(accessor);
+
return {
id: SheetToggleNotePopupCommand.id,
type: MenuItemType.BUTTON,
title: 'sheets-note-ui.rightClick.toggleNote',
icon: 'HideNoteIcon',
- hidden$: getHasNote$(accessor).pipe(map((hasNote) => !hasNote)),
+ activated$: selectedNote$.pipe(map((note) => note?.show === true), distinctUntilChanged()),
+ hidden$: selectedNote$.pipe(map((note) => !note), distinctUntilChanged()),
};
}
diff --git a/packages/sheets-thread-comment-ui/src/menu/__tests__/menu.spec.ts b/packages/sheets-thread-comment-ui/src/menu/__tests__/menu.spec.ts
deleted file mode 100644
index c61df6f10d..0000000000
--- a/packages/sheets-thread-comment-ui/src/menu/__tests__/menu.spec.ts
+++ /dev/null
@@ -1,101 +0,0 @@
-/**
- * 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 {
- FOCUSING_COMMON_DRAWINGS,
- IContextService,
- Injector,
- IPermissionService,
- IUniverInstanceService,
- UserManagerService,
-} from '@univerjs/core';
-import { RangeProtectionRuleModel, SheetsSelectionsService, WorksheetProtectionRuleModel } from '@univerjs/sheets';
-import { BehaviorSubject, of, Subject } from 'rxjs';
-import { describe, expect, it } from 'vitest';
-import { drawingCommentMenuFactory, threadCommentMenuFactory } from '../menu';
-
-describe('drawingCommentMenuFactory', () => {
- it('stays enabled for a focused drawing until comment permission is denied', () => {
- const permissionValues$ = new BehaviorSubject([{ value: true }, { value: true }]);
- const workbook = {
- getUnitId: () => 'unit-1',
- activeSheet$: of({ getSheetId: () => 'sheet-1' }),
- };
- const injector = new Injector([
- [IContextService, {
- useValue: {
- subscribeContextValue$: (key: string) => of(key === FOCUSING_COMMON_DRAWINGS),
- },
- }],
- [IPermissionService, {
- useValue: {
- permissionPointUpdate$: new Subject(),
- composePermission$: () => permissionValues$,
- },
- }],
- [IUniverInstanceService, {
- useValue: {
- focused$: new Subject(),
- getCurrentTypeOfUnit$: () => of(workbook),
- },
- }],
- [RangeProtectionRuleModel, { useValue: { getSubunitRuleList: () => [] } }],
- [SheetsSelectionsService, {
- useValue: {
- selectionChanged$: new Subject(),
- getWorkbookSelections: () => ({ getSelectionsOfWorksheet: () => [] }),
- },
- }],
- [UserManagerService, { useValue: { currentUser$: of(null) } }],
- [WorksheetProtectionRuleModel, { useValue: { getRule: () => null } }],
- ]);
- const disabledValues: boolean[] = [];
- const subscription = drawingCommentMenuFactory(injector).disabled$.subscribe((disabled) => {
- disabledValues.push(disabled);
- });
-
- expect(disabledValues).toEqual([false]);
-
- permissionValues$.next([{ value: false }, { value: true }]);
-
- expect(disabledValues).toEqual([false, true]);
- subscription.unsubscribe();
- injector.dispose();
- });
-});
-
-describe('thread comment menu', () => {
- it('shows the add comment label in its tooltip', () => {
- const service = {
- currentUser$: of(null),
- focused$: of(null),
- getCurrentTypeOfUnit$: () => of(null),
- subscribeContextValue$: () => of(false),
- };
- const accessor = new Injector([
- [IContextService, { useValue: service }],
- [IUniverInstanceService, { useValue: service }],
- [UserManagerService, { useValue: service }],
- ]);
-
- expect(threadCommentMenuFactory(accessor)).toMatchObject({
- title: 'sheets-thread-comment-ui.menu.addComment',
- tooltip: 'sheets-thread-comment-ui.menu.addComment',
- });
-
- accessor.dispose();
- });
-});
diff --git a/packages/sheets-thread-comment-ui/src/menu/menu.ts b/packages/sheets-thread-comment-ui/src/menu/menu.ts
index df60bd4036..d72913d820 100644
--- a/packages/sheets-thread-comment-ui/src/menu/menu.ts
+++ b/packages/sheets-thread-comment-ui/src/menu/menu.ts
@@ -17,11 +17,20 @@
import type { IAccessor } from '@univerjs/core';
import type { IShortcutItem } from '@univerjs/ui';
import { IPermissionService, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
-import { RangeProtectionPermissionViewPoint, WorkbookCommentPermission, WorksheetViewPermission } from '@univerjs/sheets';
+import {
+ RangeProtectionPermissionViewPoint,
+ WorkbookCommentPermission,
+ WorksheetViewPermission,
+} from '@univerjs/sheets';
import { deriveStateFromActiveSheet$, getCurrentRangeDisable$, whenSheetEditorFocused } from '@univerjs/sheets-ui';
-import { getMenuHiddenObservable, KeyCode, MenuItemType, MetaKeys } from '@univerjs/ui';
-import { map } from 'rxjs';
-import { AddSheetDrawingCommentOperation, ShowAddSheetCommentModalOperation, ToggleSheetCommentPanelOperation } from '../commands/operations/comment.operation';
+import { getMenuHiddenObservable, ISidebarService, KeyCode, MenuItemType, MetaKeys } from '@univerjs/ui';
+import { defer, distinctUntilChanged, map, startWith } from 'rxjs';
+import {
+ AddSheetDrawingCommentOperation,
+ ShowAddSheetCommentModalOperation,
+ ToggleSheetCommentPanelOperation,
+} from '../commands/operations/comment.operation';
+import { SHEETS_THREAD_COMMENT_PANEL } from '../types/const';
function getDrawingCommentDisable$(accessor: IAccessor) {
const univerInstanceService = accessor.get(IUniverInstanceService);
@@ -65,7 +74,14 @@ export const threadCommentMenuFactory = (accessor: IAccessor) => {
};
export const threadPanelMenuFactory = (accessor: IAccessor) => {
+ const sidebarService = accessor.get(ISidebarService);
+
return {
+ activated$: defer(() => sidebarService.sidebarOptions$.pipe(
+ startWith(sidebarService.options),
+ map((options) => options.visible === true && options.children?.label === SHEETS_THREAD_COMMENT_PANEL),
+ distinctUntilChanged()
+ )),
id: ToggleSheetCommentPanelOperation.id,
type: MenuItemType.BUTTON,
icon: 'CommentIcon',
diff --git a/packages/ui/src/menu/shortcut-panel.menu.ts b/packages/ui/src/menu/shortcut-panel.menu.ts
index da22b29361..34e1790267 100644
--- a/packages/ui/src/menu/shortcut-panel.menu.ts
+++ b/packages/ui/src/menu/shortcut-panel.menu.ts
@@ -14,13 +14,26 @@
* limitations under the License.
*/
+import type { IAccessor } from '@univerjs/core';
import type { LocaleKey } from '../locale/types';
import type { IMenuButtonItem } from '../services/menu/menu';
-import { ToggleShortcutPanelOperation } from '../commands/operations/toggle-shortcut-panel.operation';
+import { defer, distinctUntilChanged, map, startWith } from 'rxjs';
+import {
+ ShortcutPanelComponentName,
+ ToggleShortcutPanelOperation,
+} from '../commands/operations/toggle-shortcut-panel.operation';
import { MenuItemType } from '../services/menu/menu';
+import { ISidebarService } from '../services/sidebar/sidebar.service';
+
+export function ShortcutPanelMenuItemFactory(accessor: IAccessor): IMenuButtonItem<LocaleKey> {
+ const sidebarService = accessor.get(ISidebarService);
-export function ShortcutPanelMenuItemFactory(): IMenuButtonItem<LocaleKey> {
return {
+ activated$: defer(() => sidebarService.sidebarOptions$.pipe(
+ startWith(sidebarService.options),
+ map((options) => options.visible === true && options.children?.label === ShortcutPanelComponentName),
+ distinctUntilChanged()
+ )),
id: ToggleShortcutPanelOperation.id,
title: 'ui.toggle-shortcut-panel',
tooltip: 'ui.toggle-shortcut-panel',