annotate-render.ts
3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import type { Page } from 'playwright';
import type { MisalignedComponentData } from '../types';
import { ANNOTATION_COLORS, createBoxStyle, createCircleLabelStyle, createContainerStyle, createTextLabelStyle } from './annotation-styles';
/**
* Annotates the rendered page with RED boxes showing current positions.
* Injects DOM elements via page.evaluate() to mark misaligned component positions.
*/
export async function annotateRenderWithPlaywright(page: Page, misalignedData: MisalignedComponentData[]): Promise<void> {
await page.evaluate(
(data: {
items: MisalignedComponentData[];
styles: {
container: string;
boxes: Record<number, string>;
circleLabels: Record<number, string>;
textLabels: Record<number, string>;
};
}) => {
const { items, styles } = data;
const container = document.createElement('div');
container.id = 'validation-annotations';
container.style.cssText = styles.container;
items.forEach((item: MisalignedComponentData) => {
// Create RED box at current position with browser dimensions
const box = document.createElement('div');
const boxStyle = styles.boxes[item.index];
if (boxStyle) {
box.style.cssText = boxStyle;
}
container.appendChild(box);
// Create numbered circle label
const label = document.createElement('div');
label.textContent = `${item.index}`;
const circleLabelStyle = styles.circleLabels[item.index];
if (circleLabelStyle) {
label.style.cssText = circleLabelStyle;
}
container.appendChild(label);
// Create text label with component name and distance
const textLabel = document.createElement('div');
textLabel.textContent = `${item.componentName} (${item.distance.toFixed(1)}px)`;
const textLabelStyle = styles.textLabels[item.index];
if (textLabelStyle) {
textLabel.style.cssText = textLabelStyle;
}
container.appendChild(textLabel);
});
document.body.appendChild(container);
},
{
items: misalignedData,
styles: {
container: createContainerStyle(),
boxes: Object.fromEntries(
misalignedData.map(item => [
item.index,
createBoxStyle(item.currentX, item.currentY, item.currentWidth, item.currentHeight, ANNOTATION_COLORS.RED),
])
),
circleLabels: Object.fromEntries(
misalignedData.map(item => [item.index, createCircleLabelStyle(item.currentX, item.currentY, ANNOTATION_COLORS.RED)])
),
textLabels: Object.fromEntries(
misalignedData.map(item => [
item.index,
createTextLabelStyle(item.currentX, item.currentY, item.currentHeight, ANNOTATION_COLORS.RED),
])
),
},
}
);
}