Skip to content

Commit 74f23bf

Browse files
committed
various changes: make highlight default enabled and never untoggleable, clean up xindex/mousedown bug, refactor action/box/rect overload
1 parent f47c083 commit 74f23bf

File tree

3 files changed

+21
-25
lines changed

3 files changed

+21
-25
lines changed

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

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

25-
type Action = 'highlight' | 'hide' | '';
25+
type Action = 'highlight' | 'hide';
2626

2727
interface Box {
28-
action: Action;
2928
startX: number;
3029
startY: number;
3130
endX: number;
3231
endY: number;
3332
}
3433

35-
interface Rect {
36-
action: Action;
34+
interface Dimensions {
3735
x: number;
3836
y: number;
3937
height: number;
4038
width: number;
4139
}
4240

41+
interface Rect extends Dimensions {
42+
action: Action;
43+
}
44+
4345
const DPI = WINDOW.devicePixelRatio;
4446

45-
const constructRect = (box: Box): Rect => ({
46-
action: box.action,
47+
const constructRect = (action: Action, box: Box): Rect => ({
48+
action,
4749
x: Math.min(box.startX, box.endX),
4850
y: Math.min(box.startY, box.endY),
4951
width: Math.abs(box.startX - box.endX),
5052
height: Math.abs(box.startY - box.endY),
5153
});
5254

53-
const getContainedSize = (measurementDiv: HTMLDivElement, imageSource: HTMLCanvasElement): Rect => {
55+
const getContainedSize = (measurementDiv: HTMLDivElement, imageSource: HTMLCanvasElement): Dimensions => {
5456
const imgClientHeight = measurementDiv.clientHeight;
5557
const imgClientWidth = measurementDiv.clientWidth;
5658
const ratio = imageSource.width / imageSource.height;
@@ -62,7 +64,7 @@ const getContainedSize = (measurementDiv: HTMLDivElement, imageSource: HTMLCanva
6264
}
6365
const x = (imgClientWidth - width) / 2;
6466
const y = (imgClientHeight - height) / 2;
65-
return { action: '', x: x, y: y, width: width, height: height };
67+
return { x: x, y: y, width: width, height: height };
6668
};
6769

6870
function drawRect(rect: Rect, ctx: CanvasRenderingContext2D, color: string, scale: number = 1): void {
@@ -101,7 +103,7 @@ function drawRect(rect: Rect, ctx: CanvasRenderingContext2D, color: string, scal
101103
}
102104
}
103105

104-
function resizeCanvas(canvas: HTMLCanvasElement, imageDimensions: Rect): void {
106+
function resizeCanvas(canvas: HTMLCanvasElement, imageDimensions: Dimensions): void {
105107
canvas.width = imageDimensions.width * DPI;
106108
canvas.height = imageDimensions.height * DPI;
107109
canvas.style.width = `${imageDimensions.width}px`;
@@ -126,7 +128,7 @@ export function ScreenshotEditorFactory({
126128

127129
return function ScreenshotEditor({ onError }: Props): VNode {
128130
// Data for rendering:
129-
const [action, setAction] = hooks.useState<'highlight' | 'hide' | ''>('');
131+
const [action, setAction] = hooks.useState<Action>('highlight');
130132
const [drawRects, setDrawRects] = hooks.useState<Rect[]>([]);
131133
const [currentRect, setCurrentRect] = hooks.useState<Rect | undefined>(undefined);
132134

@@ -314,7 +316,7 @@ export function ScreenshotEditorFactory({
314316
const handleMouseMove = (e: MouseEvent): void => {
315317
const endX = e.clientX - boundingRect.left;
316318
const endY = e.clientY - boundingRect.top;
317-
const rect = constructRect({ action, startX, startY, endX, endY });
319+
const rect = constructRect(action, { startX, startY, endX, endY });
318320
// prevent drawing when just clicking (not dragging) on the canvas
319321
if (startX != endX && startY != endY) {
320322
setCurrentRect(rect);
@@ -330,8 +332,7 @@ export function ScreenshotEditorFactory({
330332
if (startX != endX && startY != endY) {
331333
// scale to image buffer
332334
const scale = imageBuffer.width / annotatingCanvas.clientWidth;
333-
const rect = constructRect({
334-
action,
335+
const rect = constructRect(action, {
335336
startX: startX * scale,
336337
startY: startY * scale,
337338
endX: endX * scale,
@@ -360,8 +361,8 @@ export function ScreenshotEditorFactory({
360361
<div class="editor__image-container">
361362
<div class="editor__canvas-container" ref={measurementRef}>
362363
<canvas ref={screenshotRef}></canvas>
363-
<canvas class="editor__canvas-annotate" ref={annotatingRef} onMouseDown={handleMouseDown}></canvas>
364-
<div class="editor__rect-container" ref={rectContainerRef}>
364+
<canvas ref={annotatingRef}></canvas>
365+
<div class="editor__rect-container" ref={rectContainerRef} onMouseDown={handleMouseDown}>
365366
{drawRects.map((rect, index) => (
366367
<div
367368
key={index}
@@ -372,7 +373,6 @@ export function ScreenshotEditorFactory({
372373
width: `${rect.width * scaleFactor}px`,
373374
height: `${rect.height * scaleFactor}px`,
374375
}}
375-
onMouseDown={handleMouseDown}
376376
>
377377
<button type="button" onClick={() => handleDeleteRect(index)}>
378378
<IconClose />

packages/feedback/src/screenshot/components/ScreenshotInput.css.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ export function createScreenshotInputStyles(styleNonce?: string): HTMLStyleEleme
5454
position: absolute;
5555
}
5656
57-
.editor__canvas-annotate {
58-
z-index: 2;
59-
}
60-
6157
.editor__tool-container {
6258
padding-top: 8px;
6359
display: flex;
@@ -79,6 +75,7 @@ export function createScreenshotInputStyles(styleNonce?: string): HTMLStyleEleme
7975
}
8076
8177
.editor__tool--active {
78+
cursor: default;
8279
background: var(--button-primary-background, var(--accent-background));
8380
color: var(--button-primary-color, var(--accent-foreground));
8481
}
@@ -89,7 +86,6 @@ export function createScreenshotInputStyles(styleNonce?: string): HTMLStyleEleme
8986
9087
.editor__rect {
9188
position: absolute;
92-
z-index: 2;
9389
}
9490
9591
.editor__rect button {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export default function ToolbarFactory({
1212
action,
1313
setAction,
1414
}: {
15-
action: 'highlight' | 'hide' | '';
16-
setAction: Hooks.StateUpdater<'highlight' | 'hide' | ''>;
15+
action: 'highlight' | 'hide';
16+
setAction: Hooks.StateUpdater<'highlight' | 'hide'>;
1717
}): VNode {
1818
return (
1919
<div class="editor__tool-container">
@@ -22,7 +22,7 @@ export default function ToolbarFactory({
2222
type="button"
2323
class={`editor__tool ${action === 'highlight' ? 'editor__tool--active' : ''}`}
2424
onClick={() => {
25-
setAction(action === 'highlight' ? '' : 'highlight');
25+
setAction('highlight');
2626
}}
2727
>
2828
Highlight
@@ -31,7 +31,7 @@ export default function ToolbarFactory({
3131
type="button"
3232
class={`editor__tool ${action === 'hide' ? 'editor__tool--active' : ''}`}
3333
onClick={() => {
34-
setAction(action === 'hide' ? '' : 'hide');
34+
setAction('hide');
3535
}}
3636
>
3737
Hide

0 commit comments

Comments
 (0)