Skip to content

Commit f47c083

Browse files
committed
add comments + refactor strokeColor
1 parent 89608d9 commit f47c083

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

packages/feedback/src/screenshot/components/ScreenshotEditor.tsx

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,18 @@ interface Props {
2222
onError: (error: Error) => void;
2323
}
2424

25+
type Action = 'highlight' | 'hide' | '';
26+
2527
interface Box {
26-
action: 'highlight' | 'hide' | '';
28+
action: Action;
2729
startX: number;
2830
startY: number;
2931
endX: number;
3032
endY: number;
3133
}
3234

3335
interface Rect {
34-
action: 'highlight' | 'hide' | '';
36+
action: Action;
3537
x: number;
3638
y: number;
3739
height: number;
@@ -63,7 +65,7 @@ const getContainedSize = (measurementDiv: HTMLDivElement, imageSource: HTMLCanva
6365
return { action: '', x: x, y: y, width: width, height: height };
6466
};
6567

66-
function drawRect(rect: Rect, ctx: CanvasRenderingContext2D, feedbackId: string, scale: number = 1): void {
68+
function drawRect(rect: Rect, ctx: CanvasRenderingContext2D, color: string, scale: number = 1): void {
6769
const scaledX = rect.x * scale;
6870
const scaledY = rect.y * scale;
6971
const scaledWidth = rect.width * scale;
@@ -84,16 +86,7 @@ function drawRect(rect: Rect, ctx: CanvasRenderingContext2D, feedbackId: string,
8486
ctx.shadowColor = 'transparent';
8587
ctx.shadowBlur = 0;
8688

87-
// draws outline around rectangle in the same colour as the submit button
88-
let strokeColor;
89-
const sentryFeedback = DOCUMENT.getElementById(feedbackId);
90-
if (sentryFeedback) {
91-
const computedStyle = getComputedStyle(sentryFeedback);
92-
strokeColor =
93-
computedStyle.getPropertyValue('--button-primary-background') ||
94-
computedStyle.getPropertyValue('--accent-background');
95-
}
96-
ctx.strokeStyle = strokeColor || 'white';
89+
ctx.strokeStyle = color;
9790
ctx.strokeRect(scaledX + 1, scaledY + 1, scaledWidth - 2, scaledHeight - 2);
9891

9992
break;
@@ -129,20 +122,41 @@ export function ScreenshotEditorFactory({
129122
const useTakeScreenshot = useTakeScreenshotFactory({ hooks });
130123
const Toolbar = ToolbarFactory({ h });
131124
const IconClose = IconCloseFactory({ h });
132-
return function ScreenshotEditor({ onError }: Props): VNode {
133-
const styles = hooks.useMemo(() => ({ __html: createScreenshotInputStyles(options.styleNonce).innerText }), []);
125+
const styles = { __html: createScreenshotInputStyles(options.styleNonce).innerText };
134126

127+
return function ScreenshotEditor({ onError }: Props): VNode {
128+
// Data for rendering:
135129
const [action, setAction] = hooks.useState<'highlight' | 'hide' | ''>('');
136130
const [drawRects, setDrawRects] = hooks.useState<Rect[]>([]);
137131
const [currentRect, setCurrentRect] = hooks.useState<Rect | undefined>(undefined);
132+
133+
// Refs to our html components:
138134
const measurementRef = hooks.useRef<HTMLDivElement>(null);
139135
const screenshotRef = hooks.useRef<HTMLCanvasElement>(null);
140136
const annotatingRef = hooks.useRef<HTMLCanvasElement>(null);
141137
const rectContainerRef = hooks.useRef<HTMLDivElement>(null);
138+
139+
// The canvas that contains the original screenshot
142140
const [imageSource, setImageSource] = hooks.useState<HTMLCanvasElement | null>(null);
141+
142+
// Hide the whole feedback widget when we take the screenshot
143143
const [displayEditor, setDisplayEditor] = hooks.useState<boolean>(true);
144+
145+
// The size of our window, relative to the imageSource
144146
const [scaleFactor, setScaleFactor] = hooks.useState<number>(1);
145147

148+
const strokeColor = hooks.useMemo((): string => {
149+
const sentryFeedback = DOCUMENT.getElementById(options.id);
150+
if (!sentryFeedback) {
151+
return 'white';
152+
}
153+
const computedStyle = getComputedStyle(sentryFeedback);
154+
return (
155+
computedStyle.getPropertyValue('--button-primary-background') ||
156+
computedStyle.getPropertyValue('--accent-background')
157+
);
158+
}, [options.id]);
159+
146160
const resize = hooks.useCallback((): void => {
147161
if (!displayEditor) {
148162
return;
@@ -225,10 +239,10 @@ export function ScreenshotEditorFactory({
225239

226240
grayCtx.lineWidth = 4;
227241
drawRects.forEach(rect => {
228-
drawRect(rect, grayCtx, options.id);
242+
drawRect(rect, grayCtx, strokeColor);
229243
});
230244
ctx.drawImage(annotatingBufferBig, 0, 0);
231-
}, [drawRects]);
245+
}, [drawRects, strokeColor]);
232246

233247
const drawScene = hooks.useCallback((): void => {
234248
const annotatingCanvas = annotatingRef.current;
@@ -252,13 +266,13 @@ export function ScreenshotEditorFactory({
252266
ctx.lineWidth = 2;
253267
const scale = annotatingCanvas.clientWidth / imageBuffer.width;
254268
drawRects.forEach(rect => {
255-
drawRect(rect, ctx, options.id, scale);
269+
drawRect(rect, ctx, strokeColor, scale);
256270
});
257271

258272
if (currentRect) {
259-
drawRect(currentRect, ctx, options.id);
273+
drawRect(currentRect, ctx, strokeColor);
260274
}
261-
}, [drawRects, currentRect]);
275+
}, [drawRects, currentRect, strokeColor]);
262276

263277
useTakeScreenshot({
264278
onBeforeScreenshot: hooks.useCallback(() => {

0 commit comments

Comments
 (0)