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: fix text border issue in editor and optimized the active color in panels related to cell color properties (#5917)

wpxp12345679ca94cb3e34
diff --git a/packages/core/src/sheets/util.ts b/packages/core/src/sheets/util.ts
index 4bbb3511cf..c7e84a2cee 100644
--- a/packages/core/src/sheets/util.ts
+++ b/packages/core/src/sheets/util.ts
@@ -168,7 +168,7 @@ export function getFontFormat(format?: Nullable<IStyleData>): IStyleBase {
if (!format) {
return {};
}
- const { ff, fs, it, bl, ul, st, ol, cl, bg, bd } = format;
+ const { ff, fs, it, bl, ul, st, ol, cl, bg } = format;
const style: IStyleBase = {};
ff && (style.ff = ff);
fs && (style.fs = fs);
@@ -179,7 +179,6 @@ export function getFontFormat(format?: Nullable<IStyleData>): IStyleBase {
ol && (style.ol = ol);
cl && (style.cl = cl);
bg && (style.bg = bg);
- bd && (style.bd = bd);
return style;
}
diff --git a/packages/sheets-ui/src/components/border-panel/BorderPanel.tsx b/packages/sheets-ui/src/components/border-panel/BorderPanel.tsx
index f1207cbd07..1adc3f9681 100644
--- a/packages/sheets-ui/src/components/border-panel/BorderPanel.tsx
+++ b/packages/sheets-ui/src/components/border-panel/BorderPanel.tsx
@@ -14,18 +14,13 @@
* limitations under the License.
*/
-import type { DocumentDataModel, IBorderData, Nullable,
-} from '@univerjs/core';
+import type { IBorderData, Nullable } from '@univerjs/core';
import type { IBorderInfo } from '@univerjs/sheets';
import type { IBorderPanelProps } from './interface';
-import {
- BorderStyleTypes,
- DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
- IUniverInstanceService,
-} from '@univerjs/core';
+import { BorderStyleTypes } from '@univerjs/core';
import { clsx, ColorPicker, Dropdown, Separator } from '@univerjs/design';
import { CheckMarkIcon, MoreDownIcon, PaintBucketDoubleIcon } from '@univerjs/icons';
-import { BorderStyleManagerService } from '@univerjs/sheets';
+import { BorderStyleManagerService, SheetsSelectionsService } from '@univerjs/sheets';
import { ComponentManager, useDependency } from '@univerjs/ui';
import { BorderLine } from './border-line/BorderLine';
import { BORDER_LINE_CHILDREN } from './interface';
@@ -100,11 +95,11 @@ function getBorderStyle(borderData: Nullable<IBorderData>): BorderStyleTypes | u
export function BorderPanel(props: IBorderPanelProps) {
const componentManager = useDependency(ComponentManager);
const borderStyleManagerService = useDependency(BorderStyleManagerService);
- const univerInstanceService = useDependency(IUniverInstanceService);
- const editorDataModel = univerInstanceService.getUnit<DocumentDataModel>(DOCS_NORMAL_EDITOR_UNIT_ID_KEY);
- const textRuns = editorDataModel?.getBody()?.textRuns;
- const color = getBorderColor(textRuns?.[0]?.ts?.bd);
- const type = getBorderStyle(textRuns?.[0]?.ts?.bd);
+ const selectionManagerService = useDependency(SheetsSelectionsService);
+
+ const { isAllValuesSame, value: currentValue } = selectionManagerService.getCellStylesProperty('bd');
+ const color = isAllValuesSame ? getBorderColor(currentValue as Nullable<IBorderData>) : undefined;
+ const type = isAllValuesSame ? getBorderStyle(currentValue as Nullable<IBorderData>) : undefined;
const { onChange, value } = props;
diff --git a/packages/sheets-ui/src/controllers/menu/menu.ts b/packages/sheets-ui/src/controllers/menu/menu.ts
index 3511bd819d..69541e9012 100644
--- a/packages/sheets-ui/src/controllers/menu/menu.ts
+++ b/packages/sheets-ui/src/controllers/menu/menu.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { DocumentDataModel, IAccessor, Workbook } from '@univerjs/core';
+import type { DocumentDataModel, IAccessor, IColorStyle, Nullable, Workbook } from '@univerjs/core';
import type { IMenuButtonItem, IMenuSelectorItem } from '@univerjs/ui';
import {
BooleanNumber,
@@ -536,6 +536,7 @@ export function ResetTextColorMenuItemFactory(accessor: IAccessor): IMenuButtonI
export function TextColorSelectorMenuItemFactory(accessor: IAccessor): IMenuSelectorItem<string, string | undefined> {
const commandService = accessor.get(ICommandService);
const themeService = accessor.get(ThemeService);
+ const selectionManagerService = accessor.get(SheetsSelectionsService);
return {
id: SetRangeTextColorCommand.id,
@@ -551,26 +552,13 @@ export function TextColorSelectorMenuItemFactory(accessor: IAccessor): IMenuSele
},
value$: new Observable<string>((subscriber) => {
const defaultValue = DEFAULT_STYLES.cl.rgb;
- const calc = () => {
- const textRun = getFontStyleAtCursor(accessor);
-
- if (!textRun) {
- subscriber.next(defaultValue);
- return;
- }
-
- const color = textRun.ts?.cl?.rgb;
- subscriber.next(color ?? defaultValue);
- };
-
- const disposable = commandService.onCommandExecuted((c) => {
- if (c.id === SetRangeTextColorCommand.id) {
- calc();
- }
- });
-
- calc();
- return disposable.dispose;
+ const { isAllValuesSame, value: currentValue } = selectionManagerService.getCellStylesProperty('cl');
+
+ if (isAllValuesSame) {
+ subscriber.next((currentValue as Nullable<IColorStyle>)?.rgb ?? defaultValue);
+ } else {
+ subscriber.next(defaultValue);
+ }
}),
},
],
@@ -612,6 +600,7 @@ export function ResetBackgroundColorMenuItemFactory(accessor: IAccessor): IMenuB
export function BackgroundColorSelectorMenuItemFactory(accessor: IAccessor): IMenuSelectorItem<string, string | undefined> {
const commandService = accessor.get(ICommandService);
const themeService = accessor.get(ThemeService);
+ const selectionManagerService = accessor.get(SheetsSelectionsService);
return {
id: SetBackgroundColorCommand.id,
@@ -627,27 +616,13 @@ export function BackgroundColorSelectorMenuItemFactory(accessor: IAccessor): IMe
},
value$: new Observable<string>((subscriber) => {
const defaultValue = DEFAULT_STYLES.bg.rgb;
- const calc = () => {
- const textRun = getFontStyleAtCursor(accessor);
-
- if (!textRun) {
- subscriber.next(defaultValue);
- return;
- }
+ const { isAllValuesSame, value: currentValue } = selectionManagerService.getCellStylesProperty('bg');
- const color = textRun.ts?.bg?.rgb;
-
- subscriber.next(color ?? defaultValue);
- };
-
- const disposable = commandService.onCommandExecuted((c) => {
- if (c.id === SetRangeTextColorCommand.id) {
- calc();
- }
- });
-
- calc();
- return disposable.dispose;
+ if (isAllValuesSame) {
+ subscriber.next((currentValue as Nullable<IColorStyle>)?.rgb ?? defaultValue);
+ } else {
+ subscriber.next(defaultValue);
+ }
}),
},
],
diff --git a/packages/sheets/src/services/selections/selection.service.ts b/packages/sheets/src/services/selections/selection.service.ts
index 818438de21..72ffea1f27 100644
--- a/packages/sheets/src/services/selections/selection.service.ts
+++ b/packages/sheets/src/services/selections/selection.service.ts
@@ -14,12 +14,11 @@
* limitations under the License.
*/
-import type { DeepReadonly, ISelectionCell, Nullable, Workbook } from '@univerjs/core';
+import type { DeepReadonly, ISelectionCell, IStyleData, Nullable, Workbook } from '@univerjs/core';
import type { Observable } from 'rxjs';
import type { ISelectionWithStyle } from '../../basics/selection';
import type { ISelectionManagerSearchParam } from './type';
-
-import { IUniverInstanceService, RxDisposable, UniverInstanceType } from '@univerjs/core';
+import { IUniverInstanceService, RxDisposable, Tools, UniverInstanceType } from '@univerjs/core';
import { distinctUntilChanged, of, shareReplay, skip, switchMap, takeUntil } from 'rxjs';
import { WorkbookSelectionModel } from './selection-data-model';
import { SelectionMoveType } from './type';
@@ -41,6 +40,11 @@ export class SheetsSelectionsService extends RxDisposable {
return this._currentSelectionPos;
}
+ /**
+ * Cache cell styles for current selections, key is `${row}_${column}`.
+ */
+ private _cellStylesCache = new Map<string, IStyleData>();
+
/**
* Selection Events, usually triggered when pointerdown in spreadsheet by selection render service after selectionModel has updated.
*/
@@ -94,6 +98,11 @@ export class SheetsSelectionsService extends RxDisposable {
this._instanceSrv.getTypeOfUnitDisposed$(UniverInstanceType.UNIVER_SHEET).pipe(takeUntil(this.dispose$)).subscribe((workbook) => {
this._removeWorkbookSelection(workbook.getUnitId());
});
+
+ // Clear cell styles cache when current selections changed.
+ this.selectionChanged$.pipe(takeUntil(this.dispose$)).subscribe(() => {
+ this._cellStylesCache.clear();
+ });
}
/**
@@ -232,6 +241,62 @@ export class SheetsSelectionsService extends RxDisposable {
protected _removeWorkbookSelection(unitId: string): void {
this._workbookSelections.delete(unitId);
}
+
+ /**
+ * This method is used to get the common value of a specific cell style property in the current selections.
+ * Used to determine the state related to color panels in the toolbar.
+ * Because in Excel, only the color panels need to show the common color of the current selections, other properties based on the current selection primary cell.
+ * Now only handles text color, fill color, border style, border color.
+ */
+ getCellStylesProperty(property: keyof IStyleData): {
+ isAllValuesSame: boolean;
+ value: Nullable<IStyleData[keyof IStyleData]>;
+ } {
+ const worksheet = this._instanceSrv.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)?.getActiveSheet();
+ const selections = this.getCurrentSelections();
+ if (!worksheet || selections.length === 0) {
+ return {
+ isAllValuesSame: false,
+ value: null,
+ };
+ }
+
+ let value: Nullable<IStyleData[keyof IStyleData]> = null;
+
+ for (let i = 0; i < selections.length; i++) {
+ const selection = selections[i];
+ const { startRow, endRow, startColumn, endColumn } = selection.range;
+
+ for (let row = startRow; row <= endRow; row++) {
+ for (let column = startColumn; column <= endColumn; column++) {
+ const key = `${row}_${column}`;
+ let style: IStyleData;
+ if (this._cellStylesCache.has(key)) {
+ style = this._cellStylesCache.get(key)!;
+ } else {
+ style = worksheet.getComposedCellStyle(row, column);
+ this._cellStylesCache.set(key, style);
+ }
+
+ const _value = style[property];
+
+ if (value !== undefined && value !== null && !Tools.diffValue(value, _value)) {
+ return {
+ isAllValuesSame: false,
+ value: null,
+ };
+ }
+
+ value = _value;
+ }
+ }
+ }
+
+ return {
+ isAllValuesSame: true,
+ value,
+ };
+ }
}
/** An context key to disable normal selections if its value is set to `true`. */