Skip to content

Commit 36be0c9

Browse files
committed
Minor change to SPAN highlight bgcolor computation, allowing for any CSS string
1 parent e9b40f9 commit 36be0c9

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { colord } from 'colord';
2+
import { DEFAULT_STYLE, type HighlightStyle } from '../HighlightStyle';
3+
4+
export const getBackgroundColor = (style?: HighlightStyle) => {
5+
if (style?.fillOpacity !== undefined) {
6+
// User-defined opacity: compute with user-defined fill or default
7+
return colord(style?.fill || DEFAULT_STYLE.fill)
8+
.alpha(style.fillOpacity)
9+
.toHex();
10+
} else if (!style?.fill) {
11+
// Neither fill nor opacity - default
12+
return colord(DEFAULT_STYLE.fill).alpha(DEFAULT_STYLE.fillOpacity).toHex();
13+
} else {
14+
// User defined fill, no opacity. Just pass through whatever the user
15+
// provided (incl. oklch colors, etc.)
16+
return style.fill;
17+
}
18+
}

packages/text-annotator/src/highlight/span/spansRenderer.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { DEFAULT_STYLE, type HighlightStyleExpression } from '../HighlightStyle'
1010
import type { TextAnnotation } from 'src/model';
1111

1212
import './spansRenderer.css';
13+
import { getBackgroundColor } from './color';
1314

1415
const computeZIndex = (rect: Rect, all: Highlight[]): number => {
1516
const intersects = (a: Rect, b: Rect): boolean => (
@@ -85,9 +86,7 @@ const createRenderer = (container: HTMLElement): RendererImplementation => {
8586
span.style.width = `${rect.width}px`;
8687
span.style.height = `${rect.height}px`;
8788

88-
span.style.backgroundColor = colord(style?.fill || DEFAULT_STYLE.fill)
89-
.alpha(style?.fillOpacity === undefined ? DEFAULT_STYLE.fillOpacity : style.fillOpacity)
90-
.toHex();
89+
span.style.backgroundColor = getBackgroundColor(style);
9190

9291
if (style.underlineStyle)
9392
span.style.borderStyle = style.underlineStyle;

packages/text-annotator/test/index.html

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,11 @@ <h3>Not annotatable block!</h3>
334334

335335
const style = ((annotation, state, z) =>
336336
({
337-
fill: state.hovered ? '#ff0000' : undefined,
338-
fillOpacity: state.selected ? 0.9 : 0.2,
339-
underlineColor: '#9b3c06',
337+
// fill: state.hovered ? '#ff0000' : undefined,
338+
fill: 'oklch(0.72 0.18 237.15 / 0.25)',
339+
// fillOpacity: state.selected ? 0.9 : 0.2,
340+
// underlineColor: '#9b3c06',
341+
underlineColor: 'oklch(0.72 0.19 232.06)',
340342
underlineOffset: (z * 3.5) || 0,
341343
underlineThickness: 2
342344
}));

0 commit comments

Comments
 (0)