mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-23 11:29:54 +00:00
fix(studio): resolve the picker colour from the element that paints the selected text
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { createPortal } from "react-dom";
|
||||
import { applyInlineStyle } from "./inlineTextStyleRange";
|
||||
import { readInlineStyle, readInlineStyleSpread } from "./inlineTextStyleRead";
|
||||
import {
|
||||
readFirstPaintedElement,
|
||||
readInlineStyle,
|
||||
readInlineStyleSpread,
|
||||
} from "./inlineTextStyleRead";
|
||||
import { resolvePickerColor } from "./colorValue";
|
||||
import type { InlineTextEditSession } from "../../hooks/useInlineTextEdit";
|
||||
|
||||
@@ -250,7 +254,7 @@ function placeOverSelection(
|
||||
colours,
|
||||
pickerColour: resolvePickerColor(
|
||||
styles.color ?? colours[0],
|
||||
rangeStartElement(range),
|
||||
readFirstPaintedElement(range),
|
||||
DEFAULT_COLOR,
|
||||
),
|
||||
};
|
||||
@@ -261,8 +265,3 @@ function isBold(weight: string | undefined): boolean {
|
||||
if (weight === "bold" || weight === "bolder") return true;
|
||||
return Number.parseInt(weight, 10) >= 600;
|
||||
}
|
||||
|
||||
function rangeStartElement(range: Range): Element | null {
|
||||
const node = range.startContainer;
|
||||
return node.nodeType === Node.ELEMENT_NODE ? (node as Element) : node.parentElement;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,11 @@
|
||||
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { applyInlineStyle } from "./inlineTextStyleRange";
|
||||
import { readInlineStyle, readInlineStyleSpread } from "./inlineTextStyleRead";
|
||||
import {
|
||||
readFirstPaintedElement,
|
||||
readInlineStyle,
|
||||
readInlineStyleSpread,
|
||||
} from "./inlineTextStyleRead";
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
@@ -741,3 +745,19 @@ describe("readInlineStyleSpread", () => {
|
||||
expect(readInlineStyleSpread(rangeOver(host, 0, 5), "color")).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("readFirstPaintedElement", () => {
|
||||
it("names the span that paints the text when the selection starts on its parent", () => {
|
||||
const host = mount('<span style="color: red">Hello</span>');
|
||||
const range = document.createRange();
|
||||
range.selectNodeContents(host);
|
||||
|
||||
expect(readFirstPaintedElement(range)).toBe(host.querySelector("span"));
|
||||
});
|
||||
|
||||
it("skips a leading space, which paints nothing", () => {
|
||||
const host = mount(' <span style="color: red">Hello</span>');
|
||||
|
||||
expect(readFirstPaintedElement(rangeOver(host, 0, 6))).toBe(host.querySelector("span"));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -49,6 +49,7 @@ interface StyledRun {
|
||||
* identity to lose, and merges with its neighbour exactly as before.
|
||||
*/
|
||||
identity: string;
|
||||
painter?: Element | null;
|
||||
}
|
||||
|
||||
export type InlineStyleDelta = Record<string, string | null>;
|
||||
@@ -185,6 +186,7 @@ function holdsBothEnds(host: Element, range: Range): boolean {
|
||||
export interface InlineStyleChar {
|
||||
char: string;
|
||||
style: Readonly<Record<string, string>>;
|
||||
painter: Element | null;
|
||||
}
|
||||
|
||||
/** Every character the range covers with its style, or null when it covers none. */
|
||||
@@ -197,7 +199,7 @@ export function readCoveredInlineStyleChars(range: Range): readonly InlineStyleC
|
||||
const collapsed = start === end;
|
||||
const covered = charRuns(readRuns(host))
|
||||
.slice(collapsed ? Math.max(0, start - 1) : start, collapsed ? Math.max(1, start) : end)
|
||||
.map((entry) => ({ char: entry.char, style: entry.style }));
|
||||
.map((entry) => ({ char: entry.char, style: entry.style, painter: entry.painter ?? null }));
|
||||
return covered.length > 0 ? covered : null;
|
||||
}
|
||||
|
||||
@@ -244,7 +246,7 @@ function visitRunFrame(frame: RunWalkFrame, runs: StyledRun[]): RunWalkFrame[] {
|
||||
const { node, inherited, origin } = frame;
|
||||
if (node.nodeType === 3) {
|
||||
const text = node.textContent ?? "";
|
||||
if (text) runs.push(styledRun(text, inherited, origin));
|
||||
if (text) runs.push({ ...styledRun(text, inherited, origin), painter: node.parentElement });
|
||||
return [];
|
||||
}
|
||||
if (!isStyleElement(node)) return [];
|
||||
@@ -312,6 +314,7 @@ function charRuns(runs: StyledRun[]): Array<Omit<StyledRun, "text"> & { char: st
|
||||
style: run.style,
|
||||
origin: run.origin,
|
||||
identity: run.identity,
|
||||
painter: run.painter,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,3 +45,9 @@ export function readInlineStyleSpread(range: Range, property: string): string[]
|
||||
}
|
||||
return spread;
|
||||
}
|
||||
|
||||
export function readFirstPaintedElement(range: Range): Element | null {
|
||||
const covered = readCoveredInlineStyleChars(range);
|
||||
if (!covered) return null;
|
||||
return (covered.find(({ char }) => char.trim()) ?? covered[0])?.painter ?? null;
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ try {
|
||||
"build",
|
||||
"packages/studio/src/components/editor/colorValue.ts",
|
||||
"packages/studio/src/components/editor/gradientValue.ts",
|
||||
"packages/studio/src/components/editor/inlineTextStyleRead.ts",
|
||||
"--target",
|
||||
"browser",
|
||||
"--outdir",
|
||||
@@ -35,10 +36,11 @@ try {
|
||||
});
|
||||
const page = await browser.newPage();
|
||||
const results = await page.evaluate(
|
||||
async (colorUrl, gradientUrl) => {
|
||||
async (colorUrl, gradientUrl, readUrl) => {
|
||||
const { parseCssColor, toColorPickerValue, mergeColorWithExistingAlpha, resolvePickerColor } =
|
||||
await import(colorUrl);
|
||||
const { parseGradient, insertGradientStop } = await import(gradientUrl);
|
||||
const { readFirstPaintedElement, readInlineStyleSpread } = await import(readUrl);
|
||||
const inputs = [
|
||||
"white",
|
||||
"rebeccapurple",
|
||||
@@ -67,16 +69,24 @@ try {
|
||||
document.body.append(element);
|
||||
const frame = document.createElement("iframe");
|
||||
frame.srcdoc =
|
||||
'<html style="color-scheme: dark"><body><span style="color: light-dark(white, red)">x</span></body></html>';
|
||||
'<html style="color-scheme: dark"><body><div contenteditable="true" style="color: green">' +
|
||||
'<span style="color: light-dark(white, red)">x</span></div></body></html>';
|
||||
await new Promise((loaded) => {
|
||||
frame.onload = loaded;
|
||||
document.body.append(frame);
|
||||
});
|
||||
const darkSpan = frame.contentDocument.querySelector("span");
|
||||
const darkDoc = frame.contentDocument;
|
||||
const darkSelection = darkDoc.createRange();
|
||||
darkSelection.selectNodeContents(darkDoc.querySelector("div"));
|
||||
const darkAuthored = readInlineStyleSpread(darkSelection, "color")[0];
|
||||
return {
|
||||
colors,
|
||||
darkParse: parseCssColor(darkSpan.style.color),
|
||||
darkPicker: resolvePickerColor(darkSpan.style.color, darkSpan, "#ffffff"),
|
||||
darkParse: parseCssColor(darkAuthored),
|
||||
darkPicker: resolvePickerColor(
|
||||
darkAuthored,
|
||||
readFirstPaintedElement(darkSelection),
|
||||
"#ffffff",
|
||||
),
|
||||
picker: toColorPickerValue(getComputedStyle(element).color),
|
||||
alpha: mergeColorWithExistingAlpha("#123456", "color(srgb 0.4 0 0.6 / 0.25)"),
|
||||
gradient: insertGradientStop(gradient, 50).stops[1].color,
|
||||
@@ -85,6 +95,7 @@ try {
|
||||
},
|
||||
moduleUrl("colorValue"),
|
||||
moduleUrl("gradientValue"),
|
||||
moduleUrl("inlineTextStyleRead"),
|
||||
);
|
||||
const expected = [
|
||||
[255, 255, 255, 1],
|
||||
|
||||
Reference in New Issue
Block a user