Files
hyperframes/packages/studio/src/components/editor/inlineTextStyleRead.ts
T
James RussoandMiguel Ángel 5c52f72399 fix(studio): parse modern CSS colors without a new dependency (#3931)
* fix(studio): parse modern CSS colors without a new dependency

* refactor(studio): one colour parser for the text toolbar, stricter rgb numbers

The inline text toolbar kept its own canvas fallback for named colours.
parseCssColor now resolves them, so the toolbar uses it alone.
rgb() channels must be well-formed numbers; that replaces the NaN check
after parsing and rejects rgb(1. 2 3) as CSS does.

* refactor(studio): drop comment lines the comment ratchet refuses in colorValue

* fix(studio): linear-time rgb() patterns and no new colour literal in the parser

The rgb() number pattern now has one way to match each digit run, which clears
CodeQL's slow-regex alerts. The canvas reset uses transparent so the Studio
colour-literal ratchet stays at its baseline.

* fix(studio): legacy colours resolve in browsers without relative colour syntax

parseBrowserColor reads the colour itself from the canvas first and accepts a
#rrggbb or rgba() result, then tries color(from ...). Named, hsl() and percent
rgb() colours resolve again in the text toolbar on Chrome before 119 and Safari 17.

* test(studio): a colour the canvas rejects parses to null, not transparent black

* fix(studio): an inserted gradient stop takes the colour the gradient paints there

The stop colour is color-mix() in oklab, or srgb when both stops are legacy,
resolved by the browser, so it matches the painted midpoint including
premultiplied alpha; the channel blend stays as the fallback. The parser tries
color(from ...) before the legacy read, keeping alpha precise, and calls
CSS.supports inside its try. A Chrome-147-shaped fake canvas covers the modern
path in unit tests.

* test(studio): keep the fake browser canvas inside its test file

* fix(studio): mix an inserted gradient stop in the space the whole gradient uses

* fix(studio): let an unresolved var() stop keep a legacy gradient in sRGB

* fix(studio): only a bare var() stop leaves the gradient space to its neighbours

* fix(studio): pick light-dark() text colour from what the element paints

* refactor(studio): drop the neighbour clause the whole-gradient check already covers

* fix(studio): resolve the picker colour from the element that paints the selected text

* fix(studio): read the picker colour from the character the swatch colour came from

---------

Co-authored-by: Miguel Ángel <miguel.sierra@heygen.com>
2026-10-02 22:26:00 +00:00

57 lines
2.3 KiB
TypeScript

import { readCoveredInlineStyleChars } from "./inlineTextStyleRange";
/**
* What the range is styled with, for a toolbar that has to open showing the
* truth rather than a default. Reports a property only when the whole range
* agrees about it, which is what a control can honestly display.
*/
export function readInlineStyle(range: Range, properties: string[]): Record<string, string> {
const chars = readCoveredInlineStyleChars(range);
if (!chars) return {};
const styles: Record<string, string> = {};
for (const property of properties) {
const first: string | undefined = chars[0]?.style[property];
if (first === undefined) continue;
if (chars.every(({ style }) => style[property] === first)) styles[property] = first;
}
return styles;
}
/**
* The distinct values one property takes across the range, in document order:
* `["red", "lime"]`.
*
* `readInlineStyle` above answers "what is this range" and reports nothing when
* the range disagrees with itself — right for a toggle, which can only be on or
* off. A swatch can show more than one value at once, and showing the default
* instead reads as "this text is white" when none of it is.
*/
export function readInlineStyleSpread(range: Range, property: string): string[] {
const covered = readCoveredInlineStyleChars(range);
if (!covered) return [];
const seen = new Set<string>();
const spread: string[] = [];
for (const { char, style } of covered) {
// A space paints nothing, so the colour it inherits is not a colour anyone
// can see. Counting it puts the element's own colour in the swatch for text
// that shows none of it — the stray band on a selection that happens to
// start or end next to a space.
if (!char.trim()) continue;
const value = style[property];
if (value === undefined || seen.has(value)) continue;
seen.add(value);
spread.push(value);
}
return spread;
}
export function readFirstPaintedElement(range: Range, property: string): Element | null {
const covered = readCoveredInlineStyleChars(range);
if (!covered) return null;
const painted = covered.filter(({ char }) => char.trim());
const owner =
painted.find(({ style }) => style[property] !== undefined) ?? painted[0] ?? covered[0];
return owner?.painter ?? null;
}