-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(feedback): Revamp of user feedback screenshot editing #15424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
c6a13d8
wip: need to fix border & resizing
c298lee b52d5a7
Merge branch 'develop' into cl/annotations-v2
c298lee 2c0fd8c
buggy resizing & screenshotting
c298lee 50ca3ea
back to prev drawBuffer plus move scaling out
c298lee 0ba53a6
shrinking works
c298lee 8908648
working resize & delete
c298lee dc273c3
no bugs hopefully
c298lee 3282a1d
custom rectangle outline colour
c298lee 43be14b
PR comments
c298lee 6b88c44
one more callback
c298lee a301ce1
Merge branch 'develop' into cl/annotations-v2
c298lee a8c4840
rm old screenshot stuff
c298lee 6a18b5a
clean up css & hide is just black rectangle
c298lee 7c52b22
fix lint
c298lee 766e036
Merge branch 'develop' into cl/annotations-v2
c298lee aa80b58
fix linewidth
c298lee 1c198fb
rm annotations flag
c298lee 9f206eb
PR comments
c298lee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { VNode, h as hType } from 'preact'; | ||
|
||
interface FactoryParams { | ||
h: typeof hType; | ||
} | ||
|
||
export default function IconCloseFactory({ | ||
h, // eslint-disable-line @typescript-eslint/no-unused-vars | ||
}: FactoryParams) { | ||
return function IconClose(): VNode { | ||
return ( | ||
<svg data-test-id="icon-close" viewBox="0 0 16 16" fill="#2B2233" height="25px" width="25px"> | ||
<circle r="7" cx="8" cy="8" fill="white" /> | ||
<path | ||
strokeWidth="1.5" | ||
d="M8,16a8,8,0,1,1,8-8A8,8,0,0,1,8,16ZM8,1.53A6.47,6.47,0,1,0,14.47,8,6.47,6.47,0,0,0,8,1.53Z" | ||
></path> | ||
<path | ||
strokeWidth="1.5" | ||
d="M5.34,11.41a.71.71,0,0,1-.53-.22.74.74,0,0,1,0-1.06l5.32-5.32a.75.75,0,0,1,1.06,1.06L5.87,11.19A.74.74,0,0,1,5.34,11.41Z" | ||
></path> | ||
<path | ||
strokeWidth="1.5" | ||
d="M10.66,11.41a.74.74,0,0,1-.53-.22L4.81,5.87A.75.75,0,0,1,5.87,4.81l5.32,5.32a.74.74,0,0,1,0,1.06A.71.71,0,0,1,10.66,11.41Z" | ||
></path> | ||
</svg> | ||
); | ||
}; | ||
} |
356 changes: 356 additions & 0 deletions
356
packages/feedback/src/screenshot/components/ScreenshotEditorv2.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,356 @@ | ||
/* eslint-disable max-lines */ | ||
import type { ComponentType, VNode, h as hType } from 'preact'; | ||
import { h } from 'preact'; // eslint-disable-line @typescript-eslint/no-unused-vars | ||
import type * as Hooks from 'preact/hooks'; | ||
import { useTakeScreenshotFactory } from './useTakeScreenshot'; | ||
import type { FeedbackInternalOptions, FeedbackModalIntegration } from '@sentry/core'; | ||
import { DOCUMENT, WINDOW } from '../../constants'; | ||
import { createScreenshotInputStyles } from './ScreenshotInput.css'; | ||
import ToolbarFactoryv2 from './Toolbarv2'; | ||
import IconCloseFactory from './IconClose'; | ||
|
||
interface FactoryParams { | ||
h: typeof hType; | ||
hooks: typeof Hooks; | ||
imageBuffer: HTMLCanvasElement; | ||
dialog: ReturnType<FeedbackModalIntegration['createDialog']>; | ||
options: FeedbackInternalOptions; | ||
} | ||
|
||
interface Props { | ||
onError: (error: Error) => void; | ||
} | ||
|
||
interface Box { | ||
action: 'highlight' | 'hide' | ''; | ||
startX: number; | ||
startY: number; | ||
endX: number; | ||
endY: number; | ||
} | ||
|
||
interface Rect { | ||
action: 'highlight' | 'hide' | ''; | ||
x: number; | ||
y: number; | ||
height: number; | ||
width: number; | ||
} | ||
|
||
const DPI = WINDOW.devicePixelRatio; | ||
|
||
const constructRect = (box: Box): Rect => ({ | ||
action: box.action, | ||
x: Math.min(box.startX, box.endX), | ||
y: Math.min(box.startY, box.endY), | ||
width: Math.abs(box.startX - box.endX), | ||
height: Math.abs(box.startY - box.endY), | ||
}); | ||
|
||
const getContainedSize = (measurementDiv: HTMLDivElement, imageSource: HTMLCanvasElement): Rect => { | ||
const imgClientHeight = measurementDiv.clientHeight; | ||
const imgClientWidth = measurementDiv.clientWidth; | ||
const ratio = imageSource.width / imageSource.height; | ||
let width = imgClientHeight * ratio; | ||
let height = imgClientHeight; | ||
if (width > imgClientWidth) { | ||
width = imgClientWidth; | ||
height = imgClientWidth / ratio; | ||
} | ||
const x = (imgClientWidth - width) / 2; | ||
const y = (imgClientHeight - height) / 2; | ||
return { action: '', x: x, y: y, width: width, height: height }; | ||
}; | ||
|
||
function drawRect(rect: Rect, ctx: CanvasRenderingContext2D, scale: number = 1, lineWidth: number = 4): void { | ||
const scaledX = rect.x * scale; | ||
const scaledY = rect.y * scale; | ||
const scaledWidth = rect.width * scale; | ||
const scaledHeight = rect.height * scale; | ||
|
||
// creates a shadow around | ||
ctx.shadowColor = 'rgba(0, 0, 0, 0.7)'; | ||
ctx.shadowBlur = 50; // Amount of blur for the shadow | ||
|
||
switch (rect.action) { | ||
case 'highlight': | ||
// draws a rectangle first so that the shadow is visible before clearing | ||
ctx.fillStyle = 'rgb(0, 0, 0)'; | ||
ctx.fillRect(scaledX, scaledY, scaledWidth, scaledHeight); | ||
|
||
ctx.clearRect(scaledX, scaledY, scaledWidth, scaledHeight); | ||
|
||
break; | ||
case 'hide': | ||
ctx.fillStyle = 'rgb(0, 0, 0)'; | ||
ctx.fillRect(scaledX, scaledY, scaledWidth, scaledHeight); | ||
|
||
break; | ||
default: | ||
break; | ||
} | ||
|
||
// Disable shadow after the action is drawn | ||
ctx.shadowColor = 'transparent'; | ||
ctx.shadowBlur = 0; | ||
|
||
ctx.strokeStyle = '#ff0000'; | ||
ctx.lineWidth = lineWidth; | ||
c298lee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ctx.strokeRect(scaledX + 1, scaledY + 1, scaledWidth - 2, scaledHeight - 2); | ||
} | ||
|
||
function resizeCanvas(canvas: HTMLCanvasElement, imageDimensions: Rect): void { | ||
canvas.width = imageDimensions.width * DPI; | ||
canvas.height = imageDimensions.height * DPI; | ||
canvas.style.width = `${imageDimensions.width}px`; | ||
canvas.style.height = `${imageDimensions.height}px`; | ||
} | ||
|
||
export function ScreenshotEditorFactoryv2({ | ||
h, | ||
hooks, | ||
imageBuffer, | ||
dialog, | ||
options, | ||
}: FactoryParams): ComponentType<Props> { | ||
const useTakeScreenshot = useTakeScreenshotFactory({ hooks }); | ||
const Toolbarv2 = ToolbarFactoryv2({ h }); | ||
const IconClose = IconCloseFactory({ h }); | ||
return function ScreenshotEditor({ onError }: Props): VNode { | ||
const styles = hooks.useMemo(() => ({ __html: createScreenshotInputStyles(options.styleNonce).innerText }), []); | ||
|
||
const [action, setAction] = hooks.useState<'highlight' | 'hide' | ''>(''); | ||
const [drawCommands, setDrawCommands] = hooks.useState<Rect[]>([]); | ||
const [currentRect, setCurrentRect] = hooks.useState<Rect | undefined>(undefined); | ||
const measurementRef = hooks.useRef<HTMLDivElement>(null); | ||
const screenshotRef = hooks.useRef<HTMLCanvasElement>(null); | ||
const graywashRef = hooks.useRef<HTMLCanvasElement>(null); | ||
const rectDivRef = hooks.useRef<HTMLDivElement>(null); | ||
const [imageSource, setimageSource] = hooks.useState<HTMLCanvasElement | null>(null); | ||
const [displayEditor, setdisplayEditor] = hooks.useState<boolean>(true); | ||
const [scaleFactor, setScaleFactor] = hooks.useState<number>(1); | ||
|
||
const resize = hooks.useCallback((): void => { | ||
const screenshotCanvas = screenshotRef.current; | ||
c298lee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const graywashCanvas = graywashRef.current; | ||
const measurementDiv = measurementRef.current; | ||
const rectDiv = rectDivRef.current; | ||
if (!screenshotCanvas || !graywashCanvas || !imageSource || !measurementDiv || !rectDiv) { | ||
return; | ||
} | ||
|
||
const imageDimensions = getContainedSize(measurementDiv, imageSource); | ||
|
||
resizeCanvas(screenshotCanvas, imageDimensions); | ||
resizeCanvas(graywashCanvas, imageDimensions); | ||
|
||
rectDiv.style.width = `${imageDimensions.width}px`; | ||
rectDiv.style.height = `${imageDimensions.height}px`; | ||
|
||
const scale = graywashCanvas.clientWidth / imageBuffer.width; | ||
setScaleFactor(scale); | ||
|
||
const screenshotContext = screenshotCanvas.getContext('2d', { alpha: false }); | ||
if (!screenshotContext) { | ||
return; | ||
} | ||
screenshotContext.drawImage(imageSource, 0, 0, imageDimensions.width, imageDimensions.height); | ||
drawScene(); | ||
}, [imageSource, drawCommands]); | ||
|
||
hooks.useEffect(() => { | ||
WINDOW.addEventListener('resize', resize); | ||
|
||
return () => { | ||
WINDOW.removeEventListener('resize', resize); | ||
}; | ||
}, [resize]); | ||
|
||
hooks.useLayoutEffect(() => { | ||
resize(); | ||
}, [displayEditor]); | ||
c298lee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
hooks.useEffect(() => { | ||
drawScene(); | ||
drawBuffer(); | ||
}, [drawCommands]); | ||
|
||
hooks.useEffect(() => { | ||
if (currentRect) { | ||
drawScene(); | ||
} | ||
}, [currentRect]); | ||
|
||
function drawBuffer(): void { | ||
const ctx = imageBuffer.getContext('2d', { alpha: false }); | ||
const measurementDiv = measurementRef.current; | ||
if (!imageBuffer || !ctx || !imageSource || !measurementDiv) { | ||
return; | ||
} | ||
|
||
ctx.drawImage(imageSource, 0, 0); | ||
|
||
const grayWashBufferBig = DOCUMENT.createElement('canvas'); | ||
grayWashBufferBig.width = imageBuffer.width; | ||
grayWashBufferBig.height = imageBuffer.height; | ||
|
||
const grayCtx = grayWashBufferBig.getContext('2d'); | ||
if (!grayCtx) { | ||
return; | ||
} | ||
|
||
// applies the graywash if there's any boxes drawn | ||
if (drawCommands.length || currentRect) { | ||
grayCtx.fillStyle = 'rgba(0, 0, 0, 0.25)'; | ||
grayCtx.fillRect(0, 0, imageBuffer.width, imageBuffer.height); | ||
} | ||
|
||
drawCommands.forEach(rect => { | ||
drawRect(rect, grayCtx); | ||
}); | ||
c298lee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ctx.drawImage(grayWashBufferBig, 0, 0); | ||
} | ||
|
||
function drawScene(): void { | ||
c298lee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const graywashCanvas = graywashRef.current; | ||
if (!graywashCanvas) { | ||
return; | ||
} | ||
|
||
const ctx = graywashCanvas.getContext('2d'); | ||
if (!ctx) { | ||
return; | ||
} | ||
|
||
ctx.clearRect(0, 0, graywashCanvas.width, graywashCanvas.height); | ||
|
||
// applies the graywash if there's any boxes drawn | ||
if (drawCommands.length || currentRect) { | ||
ctx.fillStyle = 'rgba(0, 0, 0, 0.25)'; | ||
ctx.fillRect(0, 0, graywashCanvas.width, graywashCanvas.height); | ||
} | ||
|
||
const scale = graywashCanvas.clientWidth / imageBuffer.width; | ||
drawCommands.forEach(rect => { | ||
drawRect(rect, ctx, scale, 2); | ||
}); | ||
c298lee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (currentRect) { | ||
drawRect(currentRect, ctx, 1, 2); | ||
setCurrentRect(undefined); | ||
c298lee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
useTakeScreenshot({ | ||
onBeforeScreenshot: hooks.useCallback(() => { | ||
(dialog.el as HTMLElement).style.display = 'none'; | ||
setdisplayEditor(false); | ||
}, []), | ||
onScreenshot: hooks.useCallback((imageSource: HTMLVideoElement) => { | ||
const bufferCanvas = DOCUMENT.createElement('canvas'); | ||
bufferCanvas.width = imageSource.videoWidth; | ||
bufferCanvas.height = imageSource.videoHeight; | ||
bufferCanvas.getContext('2d', { alpha: false })?.drawImage(imageSource, 0, 0); | ||
setimageSource(bufferCanvas); | ||
|
||
imageBuffer.width = imageSource.videoWidth; | ||
imageBuffer.height = imageSource.videoHeight; | ||
}, []), | ||
onAfterScreenshot: hooks.useCallback(() => { | ||
(dialog.el as HTMLElement).style.display = 'block'; | ||
setdisplayEditor(true); | ||
}, []), | ||
onError: hooks.useCallback(error => { | ||
(dialog.el as HTMLElement).style.display = 'block'; | ||
setdisplayEditor(true); | ||
onError(error); | ||
}, []), | ||
}); | ||
|
||
const onDraw = (e: MouseEvent): void => { | ||
c298lee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const graywashCanvas = graywashRef.current; | ||
if (!action || !graywashCanvas) { | ||
return; | ||
} | ||
|
||
const boundingRect = graywashCanvas.getBoundingClientRect(); | ||
|
||
const startX = e.clientX - boundingRect.left; | ||
const startY = e.clientY - boundingRect.top; | ||
|
||
const handleMouseMove = (e: MouseEvent): void => { | ||
const endX = e.clientX - boundingRect.left; | ||
const endY = e.clientY - boundingRect.top; | ||
|
||
const rect = constructRect({ action, startX, startY, endX, endY }); | ||
|
||
// prevent drawing rect when clicking on the canvas (ie clicking delete) | ||
if (action && startX != endX && startY != endY) { | ||
setCurrentRect(rect); | ||
} | ||
}; | ||
|
||
const handleMouseUp = (e: MouseEvent): void => { | ||
const endX = Math.max(0, Math.min(e.clientX - boundingRect.left, graywashCanvas.width / DPI)); | ||
const endY = Math.max(0, Math.min(e.clientY - boundingRect.top, graywashCanvas.height / DPI)); | ||
// prevent drawing rect when clicking on the canvas (ie. clicking delete) | ||
if (startX != endX && startY != endY) { | ||
// scale to image buffer | ||
const scale = imageBuffer.width / graywashCanvas.clientWidth; | ||
const rect = constructRect({ | ||
action, | ||
startX: startX * scale, | ||
startY: startY * scale, | ||
endX: endX * scale, | ||
endY: endY * scale, | ||
}); | ||
setDrawCommands(prev => [...prev, rect]); | ||
} | ||
|
||
DOCUMENT.removeEventListener('mousemove', handleMouseMove); | ||
DOCUMENT.removeEventListener('mouseup', handleMouseUp); | ||
}; | ||
|
||
DOCUMENT.addEventListener('mousemove', handleMouseMove); | ||
DOCUMENT.addEventListener('mouseup', handleMouseUp); | ||
}; | ||
|
||
const handleDeleteRect = (index: number): void => { | ||
const updatedRects = [...drawCommands]; | ||
updatedRects.splice(index, 1); | ||
setDrawCommands(updatedRects); | ||
}; | ||
|
||
return ( | ||
<div class="editor"> | ||
<style nonce={options.styleNonce} dangerouslySetInnerHTML={styles} /> | ||
<div class="editor__image-container"> | ||
<div class="editor__canvas-container" ref={measurementRef}> | ||
<canvas ref={screenshotRef}></canvas> | ||
<canvas class="editor__canvas-annotate" ref={graywashRef} onMouseDown={onDraw}></canvas> | ||
<div class="editor__rect-container" ref={rectDivRef} onMouseDown={onDraw}> | ||
c298lee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{drawCommands.map((rect, index) => ( | ||
<div | ||
key={index} | ||
class="editor__rect" | ||
style={{ | ||
top: `${rect.y * scaleFactor}px`, | ||
left: `${rect.x * scaleFactor}px`, | ||
width: `${rect.width * scaleFactor}px`, | ||
height: `${rect.height * scaleFactor}px`, | ||
}} | ||
onMouseDown={onDraw} | ||
c298lee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
> | ||
<button type="button" onClick={() => handleDeleteRect(index)}> | ||
<IconClose /> | ||
</button> | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
</div> | ||
<Toolbarv2 action={action} setAction={setAction} /> | ||
</div> | ||
); | ||
}; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.