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: background color selector shows selected color (#7028)

白熱4ded25fb6759
diff --git a/packages/docs-ui/src/menu/menu.ts b/packages/docs-ui/src/menu/menu.ts
index 3f81e73c9b..b193a14c84 100644
--- a/packages/docs-ui/src/menu/menu.ts
+++ b/packages/docs-ui/src/menu/menu.ts
@@ -1272,7 +1272,7 @@ export function BackgroundColorSelectorMenuItemFactory(accessor: IAccessor): IMe
selectable: false,
},
value$: new Observable<string>((subscriber) => {
- const defaultValue = DEFAULT_STYLES.bg.rgb;
+ const defaultValue = themeService.getColorFromTheme('primary.600');
const calc = () => {
const textRun = getFontStyleAtCursor(accessor);
diff --git a/packages/sheets-ui/src/menu/__tests__/menu-advanced.spec.ts b/packages/sheets-ui/src/menu/__tests__/menu-advanced.spec.ts
index f5ce7a8172..b870f7e974 100644
--- a/packages/sheets-ui/src/menu/__tests__/menu-advanced.spec.ts
+++ b/packages/sheets-ui/src/menu/__tests__/menu-advanced.spec.ts
@@ -15,7 +15,7 @@
*/
import type { Univer } from '@univerjs/core';
-import { ICommandService, Injector, RANGE_TYPE } from '@univerjs/core';
+import { ICommandService, Injector, RANGE_TYPE, ThemeService } from '@univerjs/core';
import { SetRangeValuesMutation, SetSelectionsOperation, SheetsSelectionsService } from '@univerjs/sheets';
import { firstValueFrom, take } from 'rxjs';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
@@ -94,11 +94,12 @@ describe('advanced menu state streams', () => {
const textColorSelections = textColor.selections as any[];
const bgColorSelections = bgColor.selections as any[];
+ const themeBackgroundColor = get(ThemeService).getColorFromTheme('primary.600');
expect(await firstValueFrom(textColor.value$!.pipe(take(1)))).toBeTypeOf('string');
expect(await firstValueFrom(textColorSelections[0].value$!.pipe(take(1)))).toBeTypeOf('string');
- expect(await firstValueFrom(bgColor.value$!.pipe(take(1)))).toBeTypeOf('string');
- expect(await firstValueFrom(bgColorSelections[0].value$!.pipe(take(1)))).toBeTypeOf('string');
+ expect(await firstValueFrom(bgColor.value$!.pipe(take(1)))).toBe(themeBackgroundColor);
+ expect(await firstValueFrom(bgColorSelections[0].value$!.pipe(take(1)))).toBe(themeBackgroundColor);
expect(await firstValueFrom(horizontal.value$!.pipe(take(1)))).not.toBeUndefined();
expect(await firstValueFrom(vertical.value$!.pipe(take(1)))).not.toBeUndefined();
expect(await firstValueFrom(wrap.value$!.pipe(take(1)))).not.toBeUndefined();
diff --git a/packages/sheets-ui/src/menu/menu.ts b/packages/sheets-ui/src/menu/menu.ts
index 7360c5b4da..e1b4a18e88 100644
--- a/packages/sheets-ui/src/menu/menu.ts
+++ b/packages/sheets-ui/src/menu/menu.ts
@@ -564,7 +564,7 @@ export function BackgroundColorSelectorMenuItemFactory(accessor: IAccessor): IMe
selectable: false,
},
value$: new Observable<string>((subscriber) => {
- const defaultValue = DEFAULT_STYLES.bg.rgb;
+ const defaultValue = themeService.getColorFromTheme('primary.600');
const { isAllValuesSame, value: currentValue } = selectionManagerService.getCellStylesProperty('bg');
if (isAllValuesSame) {
diff --git a/packages/ui/src/views/components/ribbon/ToolbarItem.tsx b/packages/ui/src/views/components/ribbon/ToolbarItem.tsx
index d84ebe641e..630266a6e7 100644
--- a/packages/ui/src/views/components/ribbon/ToolbarItem.tsx
+++ b/packages/ui/src/views/components/ribbon/ToolbarItem.tsx
@@ -36,7 +36,7 @@ export const ToolbarItem = forwardRef<ITooltipWrapperRef, IDisplayMenuItem<IMenu
const layoutService = useDependency(ILayoutService);
const componentManager = useDependency(ComponentManager);
- const { value, hidden, disabled, activated, selectionsValue } = useToolbarItemStatus(props);
+ const { value, hidden, disabled, activated } = useToolbarItemStatus(props);
const executeCommand = (commandId: string, params?: Record<string, unknown>) => {
layoutService.focus();
@@ -133,7 +133,7 @@ export const ToolbarItem = forwardRef<ITooltipWrapperRef, IDisplayMenuItem<IMenu
<CustomLabel
icon={iconToDisplay}
title={title!}
- value={selectionsValue ?? value}
+ value={value}
label={label}
onChange={handleSelectionsValueChange}
/>
diff --git a/packages/ui/src/views/components/ribbon/__tests__/hook.spec.ts b/packages/ui/src/views/components/ribbon/__tests__/hook.spec.ts
new file mode 100644
index 0000000000..a0e93eb8a7
--- /dev/null
+++ b/packages/ui/src/views/components/ribbon/__tests__/hook.spec.ts
@@ -0,0 +1,42 @@
+/**
+ * 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 type { IDisplayMenuItem, IMenuItem } from '../../../../services/menu/menu';
+import { act, renderHook } from '@testing-library/react';
+import { BehaviorSubject } from 'rxjs';
+import { describe, expect, it } from 'vitest';
+import { MenuItemType } from '../../../../services/menu/menu';
+import { useToolbarItemStatus } from '../hook';
+
+describe('useToolbarItemStatus', () => {
+ it('uses the latest emitted selector color as the toolbar value', () => {
+ const value$ = new BehaviorSubject('#111111');
+ const selectionValue$ = new BehaviorSubject('#222222');
+ const menuItem = {
+ type: MenuItemType.BUTTON_SELECTOR,
+ value$,
+ selections: [{ value: 'color-picker', value$: selectionValue$ }],
+ } as unknown as IDisplayMenuItem<IMenuItem>;
+
+ const { result } = renderHook(() => useToolbarItemStatus(menuItem));
+
+ expect(result.current.value).toBe('#222222');
+
+ act(() => value$.next('#ff0000'));
+
+ expect(result.current.value).toBe('#ff0000');
+ });
+});
diff --git a/packages/ui/src/views/components/ribbon/hook.ts b/packages/ui/src/views/components/ribbon/hook.ts
index 0097600784..042d094401 100644
--- a/packages/ui/src/views/components/ribbon/hook.ts
+++ b/packages/ui/src/views/components/ribbon/hook.ts
@@ -63,8 +63,13 @@ export function useToolbarItemStatus(menuItem: IDisplayMenuItem<IMenuItem>): ITo
disabled$ && subscriptions.push(disabled$.subscribe((disabled) => setDisabled(disabled)));
hidden$ && subscriptions.push(hidden$.subscribe((hidden) => setHidden(hidden)));
activated$ && subscriptions.push(activated$.subscribe((activated) => setActivated(activated)));
- value$ && subscriptions.push(value$.subscribe((value) => setValue(value)));
- selectionsValue$ && subscriptions.push(selectionsValue$.subscribe((value) => setSelectionsValue(value)));
+ value$ && subscriptions.push(value$.subscribe((value) => {
+ setValue(value);
+ }));
+ selectionsValue$ && subscriptions.push(selectionsValue$.subscribe((value) => {
+ setSelectionsValue(value);
+ setValue(value);
+ }));
return () => subscriptions.forEach((subscription) => subscription.unsubscribe());
}, [activated$, disabled$, hidden$, value$, selectionsValue$]);