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(docs-ui): follow theme for overlay and caret (#7518)

Univera7656906c330
diff --git a/packages/docs-ui/src/controllers/__tests__/doc-header-footer.controller.spec.ts b/packages/docs-ui/src/controllers/__tests__/doc-header-footer.controller.spec.ts
index 54a4eace01..a64593fa44 100644
--- a/packages/docs-ui/src/controllers/__tests__/doc-header-footer.controller.spec.ts
+++ b/packages/docs-ui/src/controllers/__tests__/doc-header-footer.controller.spec.ts
@@ -175,12 +175,12 @@ describe('DocHeaderFooterController', () => {
expect(rectSpy.mock.calls[0][1]).toMatchObject({
width: 200,
height: 30,
- fill: 'alpha(white, 0.5)',
+ fill: 'alpha(gray.0, 0.5)',
});
expect(rectSpy.mock.calls[1][1]).toMatchObject({
width: 200,
height: 40,
- fill: 'alpha(white, 0.5)',
+ fill: 'alpha(gray.0, 0.5)',
});
expect(pathSpy).not.toHaveBeenCalled();
expect(textSpy).not.toHaveBeenCalled();
diff --git a/packages/docs-ui/src/controllers/doc-header-footer.controller.ts b/packages/docs-ui/src/controllers/doc-header-footer.controller.ts
index 7274fdadc8..98193ac214 100644
--- a/packages/docs-ui/src/controllers/doc-header-footer.controller.ts
+++ b/packages/docs-ui/src/controllers/doc-header-footer.controller.ts
@@ -49,7 +49,7 @@ import { DocSelectionRenderService } from '../services/selection/doc-selection-r
import { getDocPageSectionContext } from '../utils/section-header-footer';
import { TextBubbleShape } from '../views/header-footer/text-bubble';
-const HEADER_FOOTER_COVER_COLOR = 'alpha(white, 0.5)';
+const HEADER_FOOTER_COVER_COLOR = 'alpha(gray.0, 0.5)';
const HEADER_FOOTER_STROKE_COLOR = 'primary.600';
const HEADER_FOOTER_LABEL_COLOR = 'alpha(primary.600, 0.08)';
diff --git a/packages/docs-ui/src/services/selection/__tests__/range-state.spec.ts b/packages/docs-ui/src/services/selection/__tests__/range-state.spec.ts
index 7ab36f6d15..b32143dbe6 100644
--- a/packages/docs-ui/src/services/selection/__tests__/range-state.spec.ts
+++ b/packages/docs-ui/src/services/selection/__tests__/range-state.spec.ts
@@ -69,12 +69,17 @@ function createNodePosition(glyph = 0) {
};
}
-function createTextRangeHarness() {
+function createTextRangeHarness(backgroundColor?: string, themeColors: Record<string, string> = {}) {
const addedObjects: unknown[] = [];
const scene = {
addObject: (object: unknown) => {
addedObjects.push(object);
},
+ getEngine: () => ({
+ canvasColorService: {
+ getRenderColor: (color: string) => themeColors[color] ?? color,
+ },
+ }),
};
const document = {
getOffsetConfig: () => ({
@@ -85,7 +90,7 @@ function createTextRangeHarness() {
const skeleton = {
findNodePositionByCharIndex: (index: number) => createNodePosition(index),
findGlyphByPosition: () => ({
- ts: {},
+ ts: backgroundColor ? { bg: { rgb: backgroundColor } } : {},
}),
getViewModel: () => ({
getDataModel: () => ({
@@ -136,11 +141,47 @@ describe('selection range state', () => {
});
expect(addedObjects).toHaveLength(1);
expect(range.getAnchor()).not.toBeNull();
+ expect(range.getAnchor()?.stroke).toBe('gray.1000');
range.dispose();
rangePointSpy.mockRestore();
});
+ it('contrasts the caret endpoint with the effective text background across theme directions', () => {
+ const rangePointSpy = vi.spyOn(NodePositionConvertToCursor.prototype, 'getRangePointData').mockImplementation(() => ({
+ contentBoxPointGroup: [[
+ { x: 10, y: 20 },
+ { x: 12, y: 20 },
+ { x: 12, y: 36 },
+ { x: 10, y: 36 },
+ ]],
+ borderBoxPointGroup: [],
+ cursorList: [{ startOffset: 1, endOffset: 1, collapsed: true }],
+ }) as never);
+
+ const createRange = (backgroundColor: string | undefined, themeColors: Record<string, string>) => {
+ const { document, scene, skeleton } = createTextRangeHarness(backgroundColor, themeColors);
+ return cursorConvertToTextRange(
+ scene as never,
+ { startOffset: 1, endOffset: 1, segmentId: '', segmentPage: -1 },
+ skeleton as never,
+ document as never
+ )!;
+ };
+ const lightBackgroundRange = createRange('#ffffff', { 'gray.0': '#ffffff', 'gray.1000': '#000000' });
+ const darkBackgroundRange = createRange('#070b19', { 'gray.0': '#ffffff', 'gray.1000': '#000000' });
+ const deepOceanRange = createRange(undefined, { 'gray.0': '#070b19', 'gray.1000': '#f5f7ff' });
+
+ expect(lightBackgroundRange.getAnchor()?.stroke).toBe('gray.1000');
+ expect(darkBackgroundRange.getAnchor()?.stroke).toBe('gray.0');
+ expect(deepOceanRange.getAnchor()?.stroke).toBe('gray.1000');
+
+ lightBackgroundRange.dispose();
+ darkBackgroundRange.dispose();
+ deepOceanRange.dispose();
+ rangePointSpy.mockRestore();
+ });
+
it('renders expanded text ranges and reports document direction from anchor to focus', () => {
const { addedObjects, document, scene, skeleton } = createTextRangeHarness();
const rangePointSpy = vi.spyOn(NodePositionConvertToCursor.prototype, 'getRangePointData').mockImplementation(() => ({
diff --git a/packages/docs-ui/src/services/selection/text-range.ts b/packages/docs-ui/src/services/selection/text-range.ts
index c40fd793fb..2c2bc86be9 100644
--- a/packages/docs-ui/src/services/selection/text-range.ts
+++ b/packages/docs-ui/src/services/selection/text-range.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { ITextRange, Nullable } from '@univerjs/core';
+import type { IColorStyle, ITextRange, Nullable } from '@univerjs/core';
import type {
Documents,
DocumentSkeleton,
@@ -26,8 +26,8 @@ import type {
Scene,
} from '@univerjs/engine-render';
import type { IDocRange } from './range-interface';
-import { BooleanNumber, COLORS, DOC_RANGE_TYPE, generateRandomId, RANGE_DIRECTION } from '@univerjs/core';
-import { getColor, NORMAL_TEXT_SELECTION_PLUGIN_STYLE, Rect, RegularPolygon } from '@univerjs/engine-render';
+import { BooleanNumber, ColorKit, COLORS, DOC_RANGE_TYPE, generateRandomId, getColorStyle, RANGE_DIRECTION } from '@univerjs/core';
+import { DocumentSkeletonPageType, getColor, NORMAL_TEXT_SELECTION_PLUGIN_STYLE, Rect, RegularPolygon } from '@univerjs/engine-render';
import {
compareNodePosition,
compareNodePositionLogic,
@@ -42,6 +42,7 @@ const TEXT_ANCHOR_KEY_PREFIX = '__TestSelectionAnchor__';
const ID_LENGTH = 6;
const BLINK_ON = 500;
const BLINK_OFF = 500;
+const DEFAULT_CARET_COLOR = 'gray.1000';
export const TEXT_RANGE_LAYER_INDEX = 3;
@@ -120,6 +121,7 @@ export class TextRange implements IDocRange {
private _cursorList: ITextRange[] = [];
private _anchorBlinkTimer: Nullable<ReturnType<typeof setInterval>> = null;
+ private _anchorActiveStroke = DEFAULT_CARET_COLOR;
constructor(
private _scene: Scene,
@@ -301,7 +303,7 @@ export class TextRange implements IDocRange {
activeStatic() {
this._anchorShape?.setProps({
- stroke: this.style?.strokeActive || getColor(COLORS.black, 1),
+ stroke: this._anchorActiveStroke,
});
}
@@ -469,6 +471,7 @@ export class TextRange implements IDocRange {
let left = boundingLeft + docsLeft;
const top = boundingTop + docsTop;
const isItalic = glyph?.ts?.it === BooleanNumber.TRUE;
+ this._anchorActiveStroke = this._getAnchorActiveStroke(glyph);
if (isItalic) {
left += height * Math.tan((ITALIC_DEGREE * Math.PI) / 180) / 2;
@@ -483,6 +486,7 @@ export class TextRange implements IDocRange {
} else {
this._anchorShape.skew(0, 0);
}
+ this.activeStatic();
return;
}
@@ -492,7 +496,7 @@ export class TextRange implements IDocRange {
top,
height,
strokeWidth: this.style?.strokeWidth || 1,
- stroke: this.style?.strokeActive || getColor(COLORS.black, 1),
+ stroke: this._anchorActiveStroke,
evented: false,
});
@@ -505,6 +509,44 @@ export class TextRange implements IDocRange {
this.activeStatic();
}
+ private _getAnchorActiveStroke(glyph: Nullable<IDocumentSkeletonGlyph>): string {
+ if (this.style?.strokeActive && this.style.strokeActive !== NORMAL_TEXT_SELECTION_PLUGIN_STYLE.strokeActive) {
+ return this.style.strokeActive;
+ }
+
+ const colorService = this._scene.getEngine?.()?.canvasColorService;
+ if (!colorService) {
+ return DEFAULT_CARET_COLOR;
+ }
+
+ try {
+ const backgroundColorKit = new ColorKit(colorService.getRenderColor(this._getAnchorBackgroundColor(glyph)));
+ if (!backgroundColorKit.isValid) {
+ return DEFAULT_CARET_COLOR;
+ }
+
+ return ColorKit.getContrastRatio(colorService.getRenderColor('gray.0'), backgroundColorKit) > ColorKit.getContrastRatio(colorService.getRenderColor(DEFAULT_CARET_COLOR), backgroundColorKit)
+ ? 'gray.0'
+ : DEFAULT_CARET_COLOR;
+ } catch {
+ return DEFAULT_CARET_COLOR;
+ }
+ }
+
+ private _getAnchorBackgroundColor(glyph: Nullable<IDocumentSkeletonGlyph>): string {
+ const line = glyph?.parent?.parent;
+ const page = line?.parent?.parent?.parent;
+ let cellBackground: IColorStyle | undefined;
+ if (page?.type === DocumentSkeletonPageType.CELL && page.parent && 'rowSource' in page.parent) {
+ const row = page.parent;
+ const cellIndex = row.cells.indexOf(page);
+ cellBackground = row.rowSource.tableCells[cellIndex]?.backgroundColor;
+ }
+
+ const background = glyph?.ts?.bg ?? line?.backgroundColor ?? cellBackground;
+ return background?.rgb ?? getColorStyle(background) ?? 'gray.0';
+ }
+
private _setCursorList(cursorList: ITextRange[]) {
if (cursorList.length === 0) {
return;