Skip to content

Commit e4d096a

Browse files
committed
PR comments
1 parent d3b1c9f commit e4d096a

File tree

2 files changed

+33
-35
lines changed

2 files changed

+33
-35
lines changed

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

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -88,21 +88,31 @@ export function ScreenshotEditorFactory({
8888

8989
hooks.useEffect(() => {
9090
WINDOW.addEventListener('resize', resize, false);
91+
92+
// return () => {
93+
// WINDOW.removeEventListener('resize', resize, false);
94+
// };
9195
}, []);
9296

93-
function resize(): void {
94-
const cropper = croppingRef.current;
95-
const imageDimensions = constructRect(getContainedSize(imageBuffer));
96-
if (cropper) {
97-
cropper.width = imageDimensions.width * DPI;
98-
cropper.height = imageDimensions.height * DPI;
99-
cropper.style.width = `${imageDimensions.width}px`;
100-
cropper.style.height = `${imageDimensions.height}px`;
101-
const ctx = cropper.getContext('2d');
97+
function resizeCanvas(canvasRef: Hooks.Ref<HTMLCanvasElement>, imageDimensions: Rect): void {
98+
const canvas = canvasRef.current;
99+
if (canvas) {
100+
canvas.width = imageDimensions.width * DPI;
101+
canvas.height = imageDimensions.height * DPI;
102+
canvas.style.width = `${imageDimensions.width}px`;
103+
canvas.style.height = `${imageDimensions.height}px`;
104+
const ctx = canvas.getContext('2d');
102105
if (ctx) {
103106
ctx.scale(DPI, DPI);
104107
}
105108
}
109+
}
110+
111+
function resize(): void {
112+
const imageDimensions = constructRect(getContainedSize(imageBuffer));
113+
114+
resizeCanvas(croppingRef, imageDimensions);
115+
resizeCanvas(annotatingRef, imageDimensions);
106116

107117
const cropContainer = cropContainerRef.current;
108118
if (cropContainer) {
@@ -111,18 +121,6 @@ export function ScreenshotEditorFactory({
111121
}
112122

113123
setCroppingRect({ startX: 0, startY: 0, endX: imageDimensions.width, endY: imageDimensions.height });
114-
115-
const annotater = annotatingRef.current;
116-
if (annotater) {
117-
annotater.width = imageDimensions.width * DPI;
118-
annotater.height = imageDimensions.height * DPI;
119-
annotater.style.width = `${imageDimensions.width}px`;
120-
annotater.style.height = `${imageDimensions.height}px`;
121-
const ctx = annotater.getContext('2d');
122-
if (ctx) {
123-
ctx.scale(DPI, DPI);
124-
}
125-
}
126124
}
127125

128126
hooks.useEffect(() => {
@@ -262,11 +260,10 @@ export function ScreenshotEditorFactory({
262260
DOCUMENT.addEventListener('mouseup', handleMouseUp);
263261
}
264262

265-
function onAnnotateStart(e: MouseEvent): void {
263+
function onAnnotateStart(): void {
266264
if (!isAnnotating) return;
267265

268266
const handleMouseMove = (moveEvent: MouseEvent): void => {
269-
console.log(moveEvent.clientX, moveEvent.clientY);
270267
const annotateCanvas = annotatingRef.current;
271268
if (annotateCanvas) {
272269
const rect = annotateCanvas.getBoundingClientRect();
@@ -293,7 +290,7 @@ export function ScreenshotEditorFactory({
293290

294291
// draws the annotation onto the image buffer
295292
// TODO: move this to a better place
296-
submitAnnotate();
293+
applyAnnotation();
297294

298295
DOCUMENT.removeEventListener('mousemove', handleMouseMove);
299296
DOCUMENT.removeEventListener('mouseup', handleMouseUp);
@@ -303,7 +300,7 @@ export function ScreenshotEditorFactory({
303300
DOCUMENT.addEventListener('mouseup', handleMouseUp);
304301
}
305302

306-
function submitCrop(): void {
303+
function applyCrop(): void {
307304
const cutoutCanvas = DOCUMENT.createElement('canvas');
308305
const imageBox = constructRect(getContainedSize(imageBuffer));
309306
const croppingBox = constructRect(croppingRect);
@@ -337,7 +334,7 @@ export function ScreenshotEditorFactory({
337334
}
338335
}
339336

340-
function submitAnnotate(): void {
337+
function applyAnnotation(): void {
341338
// draw the annotations onto the image (ie "squash" the canvases)
342339
const imageCtx = imageBuffer.getContext('2d');
343340
const annotateCanvas = annotatingRef.current;
@@ -406,14 +403,10 @@ export function ScreenshotEditorFactory({
406403
></button>
407404
)}
408405
<div class="editor__canvas-container" ref={canvasContainerRef}>
409-
<div
410-
class="editor__crop-container"
411-
style={{ position: 'absolute', zIndex: isAnnotating ? 1 : 2 }}
412-
ref={cropContainerRef}
413-
>
406+
<div class="editor__crop-container" style={{ zIndex: isAnnotating ? 1 : 2 }} ref={cropContainerRef}>
414407
<canvas
415408
onMouseDown={onDragStart}
416-
style={{ position: 'absolute', cursor: confirmCrop ? 'move' : 'auto' }}
409+
style={{ cursor: confirmCrop ? 'move' : 'auto' }}
417410
ref={croppingRef}
418411
></canvas>
419412
<CropCorner
@@ -468,7 +461,7 @@ export function ScreenshotEditorFactory({
468461
<button
469462
onClick={e => {
470463
e.preventDefault();
471-
submitCrop();
464+
applyCrop();
472465
setConfirmCrop(false);
473466
}}
474467
class="btn btn--primary"
@@ -478,8 +471,9 @@ export function ScreenshotEditorFactory({
478471
</div>
479472
</div>
480473
<canvas
474+
class="editor__annotation"
481475
onMouseDown={onAnnotateStart}
482-
style={{ position: 'absolute', zIndex: isAnnotating ? '2' : '1' }}
476+
style={{ zIndex: isAnnotating ? '2' : '1' }}
483477
ref={annotatingRef}
484478
></canvas>
485479
</div>

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ export function createScreenshotInputStyles(styleNonce?: string): HTMLStyleEleme
4444
4545
.editor__canvas-container canvas {
4646
object-fit: contain;
47-
position: relative;
47+
position: absolute;
48+
}
49+
50+
.editor__crop-container {
51+
position: absolute;
4852
}
4953
5054
.editor__crop-btn-group {

0 commit comments

Comments
 (0)