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.

feat(core): cell editor highlight color calc (#5312)

ZhangWei5c0734c870f1
diff --git a/packages/core/src/docs/data-model/text-x/build-utils/text-x-utils.ts b/packages/core/src/docs/data-model/text-x/build-utils/text-x-utils.ts
index 918aa80512..39c5f96fc1 100644
--- a/packages/core/src/docs/data-model/text-x/build-utils/text-x-utils.ts
+++ b/packages/core/src/docs/data-model/text-x/build-utils/text-x-utils.ts
@@ -14,6 +14,7 @@
* limitations under the License.
*/
+import type { ThemeService } from '../../../../services/theme/theme.service';
import type { Nullable } from '../../../../shared';
import type { ITextRange, ITextRangeParam } from '../../../../sheets/typedef';
import type { CustomRangeType, IDocumentBody, ITextRun } from '../../../../types/interfaces';
@@ -338,8 +339,12 @@ function isTextRunsEqual(textRuns: ITextRun[] | undefined, oldTextRuns: ITextRun
return false;
}
-export const replaceSelectionTextRuns = (params: IReplaceSelectionTextXParams) => {
- const { selection, body: insertBody, doc } = params;
+export interface IReplaceSelectionTextRunsParams extends IReplaceSelectionTextXParams {
+ themeService: ThemeService;
+}
+
+export const replaceSelectionTextRuns = (params: IReplaceSelectionTextRunsParams) => {
+ const { selection, body: insertBody, doc, themeService } = params;
const segmentId = selection.segmentId;
const body = doc.getSelfOrHeaderFooterModel(segmentId)?.getBody();
if (!body) return false;
@@ -358,7 +363,15 @@ export const replaceSelectionTextRuns = (params: IReplaceSelectionTextXParams) =
body: isTextRunsEqual(textRunsSlice, oldTextRunsSlice)
? undefined
: {
- textRuns: textRunsSlice,
+ textRuns: textRunsSlice?.map((textRun) => ({
+ ...textRun,
+ ts: {
+ ...textRun.ts,
+ cl: textRun.ts?.cl?.rgb?.includes('.')
+ ? { rgb: themeService.getColorFromTheme(textRun.ts?.cl?.rgb ?? '') }
+ : textRun.ts?.cl,
+ },
+ })),
dataStream: '',
},
len: text.length,
diff --git a/packages/core/src/services/theme/theme.service.ts b/packages/core/src/services/theme/theme.service.ts
index 67172f580f..565439f8df 100644
--- a/packages/core/src/services/theme/theme.service.ts
+++ b/packages/core/src/services/theme/theme.service.ts
@@ -28,6 +28,9 @@ export class ThemeService extends Disposable {
get darkMode(): boolean { return this._darkMode$.getValue(); }
+ // Cache for valid theme colors
+ private _validColorCache = new Map<string, boolean>();
+
private _currentTheme: Theme = defaultTheme;
private readonly _currentTheme$ = new BehaviorSubject<Theme>(this._currentTheme);
readonly currentTheme$: Observable<Theme> = this._currentTheme$.asObservable();
@@ -42,19 +45,70 @@ export class ThemeService extends Disposable {
}));
}
+ /**
+ * Whether the given color is a valid theme color.
+ * A valid theme color can be a direct key in the theme object or a nested key with a dot notation.
+ * For example:
+ * @param {string} color - The color string to validate.
+ * @returns {boolean} True if the color is valid, false otherwise.
+ * @example
+ * isValidThemeColor('primary.600'); // true
+ * isValidThemeColor('blue'); // false
+ */
+ isValidThemeColor(color: string): boolean {
+ // Check if the color is already cached
+ if (this._validColorCache.has(color)) {
+ return this._validColorCache.get(color)!;
+ }
+
+ let isValid = false;
+ const parts = color.split('.');
+
+ if (parts.length === 1) {
+ // If there is no dot, check if the color key exists directly
+ isValid = color in defaultTheme;
+ } else if (parts.length === 2) {
+ // If there is a dot, check if the nested color exists
+ const [baseColor, shade] = parts;
+ isValid = baseColor in defaultTheme && shade in (this._currentTheme[baseColor as keyof typeof this._currentTheme] as Record<string, string>);
+ }
+
+ // Cache the result
+ this._validColorCache.set(color, isValid);
+
+ return isValid; // Return false if the format is incorrect
+ }
+
+ /**
+ * Get the current theme.
+ * @returns The current theme.
+ */
getCurrentTheme(): Theme {
return this._currentTheme;
}
+ /**
+ * Set the current theme.
+ * @param theme - The new theme to set.
+ */
setTheme(theme: Theme): void {
this._currentTheme = theme;
this._currentTheme$.next(theme);
}
+ /**
+ * Get the current theme as an observable.
+ * @param {boolean} darkMode - Whether to set the theme in dark mode.
+ */
setDarkMode(darkMode: boolean): void {
this._darkMode$.next(darkMode);
}
+ /**
+ * Get a color from the current theme.
+ * @param {string} color - The color key to retrieve.
+ * @returns {string} The color value from the current theme.
+ */
getColorFromTheme(color: string): string {
return get(this._currentTheme, color);
}
diff --git a/packages/docs-ui/src/commands/commands/replace-content.command.ts b/packages/docs-ui/src/commands/commands/replace-content.command.ts
index d6cb490c91..c43a810e84 100644
--- a/packages/docs-ui/src/commands/commands/replace-content.command.ts
+++ b/packages/docs-ui/src/commands/commands/replace-content.command.ts
@@ -17,7 +17,7 @@
import type { DocumentDataModel, ICommand, IDocumentBody, IDocumentData, IMutationInfo, ITextRange, JSONXActions } from '@univerjs/core';
import type { IRichTextEditingMutationParams } from '@univerjs/docs';
import type { ITextRangeWithStyle } from '@univerjs/engine-render';
-import { BuildTextUtils, CommandType, ICommandService, IUndoRedoService, IUniverInstanceService, JSONX, TextX, TextXActionType, Tools, UniverInstanceType } from '@univerjs/core';
+import { BuildTextUtils, CommandType, ICommandService, IUndoRedoService, IUniverInstanceService, JSONX, TextX, TextXActionType, ThemeService, Tools, UniverInstanceType } from '@univerjs/core';
import { DocSelectionManagerService, RichTextEditingMutation } from '@univerjs/docs';
import { getRichTextEditPath } from '../util';
@@ -357,6 +357,7 @@ export const ReplaceTextRunsCommand: ICommand<IReplaceContentCommandParams> = {
const { unitId, body, textRanges, segmentId = '', options } = params;
const univerInstanceService = accessor.get(IUniverInstanceService);
const commandService = accessor.get(ICommandService);
+ const themeService = accessor.get(ThemeService);
// const docSelectionManagerService = accessor.get(DocSelectionManagerService);
const docDataModel = univerInstanceService.getUnit<DocumentDataModel>(unitId, UniverInstanceType.UNIVER_DOC);
@@ -374,6 +375,7 @@ export const ReplaceTextRunsCommand: ICommand<IReplaceContentCommandParams> = {
endOffset: prevBody.dataStream.length - 2,
collapsed: false,
},
+ themeService,
});
if (!textX) {
diff --git a/packages/engine-render/src/services/canvas-color.service.ts b/packages/engine-render/src/services/canvas-color.service.ts
index cb3b26c9c9..7371344830 100644
--- a/packages/engine-render/src/services/canvas-color.service.ts
+++ b/packages/engine-render/src/services/canvas-color.service.ts
@@ -81,7 +81,7 @@ export class CanvasColorService extends Disposable implements ICanvasColorServic
const stripped = color.slice(4, -1).split(',');
const invertedColor = this._invertAlgo(stripped.map(Number) as RGBColorType);
cachedColor = `rgb(${invertedColor[0]},${invertedColor[1]},${invertedColor[2]})`;
- } else if (color.includes('.')) {
+ } else if (this._themeService.isValidThemeColor(color)) {
// If the color is a theme token, we can get the color from the theme service
return this._themeService.getColorFromTheme(color);
} else if (new ColorKit(color).isValid) {
diff --git a/packages/sheets-formula-ui/src/common/selection.ts b/packages/sheets-formula-ui/src/common/selection.ts
index ce7185dfd4..02e556d23f 100644
--- a/packages/sheets-formula-ui/src/common/selection.ts
+++ b/packages/sheets-formula-ui/src/common/selection.ts
@@ -22,10 +22,11 @@ export const FORMULA_REF_SELECTION_PLUGIN_NAME = 'formula_reference_selection_pl
export function genFormulaRefSelectionStyle(themeService: ThemeService, refColor: string, id: string): ISelectionStyle {
const fill = new ColorKit(refColor).setAlpha(0.05).toRgbString();
+
return {
id,
strokeWidth: 1,
- stroke: themeService.getColorFromTheme(refColor),
+ stroke: refColor,
fill,
widgets: { tl: true, tc: true, tr: true, ml: true, mr: true, bl: true, bc: true, br: true },
widgetSize: 6,
diff --git a/packages/sheets-formula-ui/src/views/formula-editor/hooks/use-highlight.ts b/packages/sheets-formula-ui/src/views/formula-editor/hooks/use-highlight.ts
index 7d6b1d3461..6090dd4dd7 100644
--- a/packages/sheets-formula-ui/src/views/formula-editor/hooks/use-highlight.ts
+++ b/packages/sheets-formula-ui/src/views/formula-editor/hooks/use-highlight.ts
@@ -211,6 +211,7 @@ export function useDocHight(_leadingCharacter: string = '') {
e.st = e.st + leadingCharacterLength;
});
}
+
cloneBody.textRuns = [{ st: 0, ed: 1, ts: { fs: 11 } }, ...textRuns];
const text = sequenceNodes.reduce((pre, cur) => {
if (typeof cur === 'string') {
@@ -265,7 +266,7 @@ export function useColor(): IColorMap {
themeService.getColorFromTheme('loop-color.10'),
themeService.getColorFromTheme('loop-color.11'),
themeService.getColorFromTheme('loop-color.12'),
- ].map((e) => e.includes('.') ? themeService.getColorFromTheme(e) : e);
+ ].map((color) => themeService.isValidThemeColor(color) ? themeService.getColorFromTheme(color) : color);
const numberColor = themeService.getColorFromTheme('blue.700');
const stringColor = themeService.getColorFromTheme('jiqing.800');
const plainTextColor = themeService.getColorFromTheme('black');