-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
wip: feedback screenshot annotations #11175
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import { DOCUMENT } from '../../constants'; | ||
|
||
/** | ||
* Creates <style> element for widget dialog | ||
*/ | ||
export function createScreenshotAnnotateStyles(): HTMLStyleElement { | ||
const style = DOCUMENT.createElement('style'); | ||
|
||
const surface200 = '#FAF9FB'; | ||
const gray100 = '#F0ECF3'; | ||
|
||
style.textContent = ` | ||
.canvas { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd be careful with these class names as they can conflict with other css we are injecting into the shadow dom. I tried to use BEM naming convention for the other components There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for bringing that up, we would need to modify this for screenshots & cropping too! |
||
cursor: crosshair; | ||
max-width: 100vw; | ||
max-height: 100vh; | ||
} | ||
|
||
.container { | ||
position: fixed; | ||
z-index: 10000; | ||
height: 100vh; | ||
width: 100vw; | ||
top: 0; | ||
left: 0; | ||
background-color: rgba(240, 236, 243, 1); | ||
background-image: repeating-linear-gradient( | ||
45deg, | ||
transparent, | ||
transparent 5px, | ||
rgba(0, 0, 0, 0.03) 5px, | ||
rgba(0, 0, 0, 0.03) 10px | ||
); | ||
} | ||
|
||
.canvasWrapper { | ||
position: relative; | ||
width: 100%; | ||
margin-top: 32px; | ||
height: calc(100% - 96px); | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
} | ||
|
||
.toolbarGroup { | ||
display: flex; | ||
flex-direction: row; | ||
height: 42px; | ||
background-color: white; | ||
border: rgba(58, 17, 95, 0.14) 1px solid; | ||
border-radius: 10px; | ||
padding: 4px; | ||
overflow: hidden; | ||
gap: 4px; | ||
box-shadow: 0px 1px 2px 1px rgba(43, 34, 51, 0.04); | ||
} | ||
|
||
.toolbar { | ||
position: absolute; | ||
width: 100%; | ||
bottom: 0px; | ||
padding: 12px 16px; | ||
display: flex; | ||
gap: 12px; | ||
flex-direction: row; | ||
justify-content: center; | ||
} | ||
|
||
.flexSpacer { | ||
flex: 1; | ||
} | ||
|
||
.toolButton { | ||
width: 32px; | ||
height: 32px; | ||
border-radius: 6px; | ||
border: none; | ||
background-color: white; | ||
color: rgba(43, 34, 51, 1); | ||
font-size: 16px; | ||
font-weight: 600; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
cursor: pointer; | ||
&:hover { | ||
background-color: rgba(43, 34, 51, 0.06); | ||
} | ||
&:active { | ||
background-color: rgba(108, 95, 199, 1) !important;\ | ||
color: white; | ||
} | ||
} | ||
|
||
.cancelButton { | ||
height: 40px; | ||
width: 84px; | ||
border: rgba(58, 17, 95, 0.14) 1px solid; | ||
background-color: #fff; | ||
color: rgba(43, 34, 51, 1); | ||
font-size: 14px; | ||
font-weight: 500; | ||
cursor: pointer; | ||
border-radius: 10px; | ||
&:hover { | ||
background-color: #eee; | ||
} | ||
} | ||
|
||
.submitButton { | ||
height: 40px; | ||
width: 84px; | ||
border: none; | ||
background-color: rgba(108, 95, 199, 1); | ||
color: #fff; | ||
font-size: 14px; | ||
font-weight: 500; | ||
cursor: pointer; | ||
border-radius: 10px; | ||
&:hover { | ||
background-color: rgba(88, 74, 192, 1); | ||
} | ||
} | ||
|
||
.colorInput { | ||
position: relative; | ||
display: flex; | ||
width: 32px; | ||
height: 32px; | ||
align-items: center; | ||
justify-content: center; | ||
margin: 0; | ||
cursor: pointer; | ||
& input[type='color'] { | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
opacity: 0; | ||
width: 0; | ||
height: 0; | ||
} | ||
} | ||
|
||
.colorDisplay { | ||
width: 16px; | ||
height: 16px; | ||
border-radius: 4px; | ||
background-color: var(--annotateColor); | ||
} | ||
`; | ||
|
||
return style; | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's be consistent with file naming, |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import { useCallback, useEffect, useMemo, useRef, useState } from 'preact/hooks'; | ||
import { h } from 'preact'; | ||
import type { ToolKey } from './useImageEditor'; | ||
import { Tools, useImageEditor } from './useImageEditor'; | ||
import { ArrowIcon, HandIcon, PenIcon, RectangleIcon } from './useImageEditor/icons'; | ||
import { WINDOW } from './../../constants'; | ||
import type { ComponentType } from 'preact'; | ||
import { createScreenshotAnnotateStyles } from './imageEditorWrapper.css'; | ||
|
||
export interface Rect { | ||
height: number; | ||
width: number; | ||
x: number; | ||
y: number; | ||
} | ||
Comment on lines
+10
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be in a |
||
interface ImageEditorWrapperProps { | ||
onCancel: () => void; | ||
onSubmit: (screenshot: HTMLCanvasElement | null) => void; | ||
src: HTMLCanvasElement; | ||
} | ||
|
||
const iconMap: Record<ToolKey, ComponentType> = { | ||
arrow: ArrowIcon, | ||
pen: PenIcon, | ||
rectangle: RectangleIcon, | ||
select: HandIcon, | ||
}; | ||
|
||
const getCanvasRenderSize = (canvas: HTMLCanvasElement, containerElement: HTMLDivElement) => { | ||
const canvasWidth = canvas.width; | ||
const canvasHeight = canvas.height; | ||
const maxWidth = containerElement.getBoundingClientRect().width; | ||
const maxHeight = containerElement.getBoundingClientRect().height; | ||
Comment on lines
+32
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's have an intermediate var to store |
||
// fit canvas to window | ||
let width = canvasWidth; | ||
let height = canvasHeight; | ||
const canvasRatio = canvasWidth / canvasHeight; | ||
const windowRatio = maxWidth / maxHeight; | ||
|
||
if (canvasRatio > windowRatio && canvasWidth > maxWidth) { | ||
height = (maxWidth / canvasWidth) * canvasHeight; | ||
width = maxWidth; | ||
} | ||
|
||
if (canvasRatio < windowRatio && canvasHeight > maxHeight) { | ||
width = (maxHeight / canvasHeight) * canvasWidth; | ||
height = maxHeight; | ||
} | ||
|
||
return { width, height }; | ||
}; | ||
|
||
const srcToImage = (src: string): HTMLImageElement => { | ||
const image = new Image(); | ||
image.src = src; | ||
return image; | ||
}; | ||
|
||
function ToolIcon({ tool }: { tool: ToolKey }) { | ||
const Icon = tool ? iconMap[tool] : HandIcon; | ||
return <Icon />; | ||
} | ||
|
||
export function ImageEditorWrapper({ src, onCancel, onSubmit }: ImageEditorWrapperProps) { | ||
const wrapperRef = useRef<HTMLDivElement>(null); | ||
const [canvas, setCanvas] = useState<HTMLCanvasElement | null>(null); | ||
const styles = useMemo(() => ({ __html: createScreenshotAnnotateStyles().innerText }), []); | ||
|
||
const resizeCanvas = useCallback(() => { | ||
if (!canvas || !wrapperRef.current) { | ||
return; | ||
} | ||
// fit canvas to window | ||
const { width, height } = getCanvasRenderSize(canvas, wrapperRef.current); | ||
canvas.style.width = `${width}px`; | ||
canvas.style.height = `${height}px`; | ||
}, [canvas]); | ||
|
||
// const image = useMemo(() => srcToImage(src), [src]); | ||
const { selectedTool, setSelectedTool, selectedColor, setSelectedColor, getBlob } = useImageEditor({ | ||
canvas, | ||
image: src, | ||
onLoad: resizeCanvas, | ||
}); | ||
|
||
useEffect(() => { | ||
resizeCanvas(); | ||
WINDOW.addEventListener('resize', resizeCanvas); | ||
return () => { | ||
WINDOW.removeEventListener('resize', resizeCanvas); | ||
}; | ||
}, [resizeCanvas]); | ||
Comment on lines
+86
to
+92
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You'll want to throttle or debounce this otherwise many |
||
|
||
return ( | ||
<div> | ||
<style dangerouslySetInnerHTML={styles} /> | ||
<div class="container"> | ||
<div class="canvasWrapper" ref={wrapperRef}> | ||
<canvas class="canvas" ref={setCanvas} /> | ||
</div> | ||
<div class="toolbar"> | ||
<button class="cancelButton" onClick={() => onCancel()}> | ||
Cancel | ||
</button> | ||
<div class="flexSpacer" /> | ||
<div class="toolbarGroup"> | ||
{Tools.map(tool => ( | ||
<button | ||
class="toolButton" | ||
key={tool} | ||
// active={selectedTool === tool} | ||
onClick={() => setSelectedTool(tool)} | ||
> | ||
<ToolIcon tool={tool} /> | ||
</button> | ||
))} | ||
</div> | ||
<div class="toolbarGroup"> | ||
<label class="colorInput"> | ||
<div | ||
class="colorDisplay" | ||
// color={selectedColor} | ||
/> | ||
<input | ||
type="color" | ||
value={selectedColor} | ||
// onChange={e => setSelectedColor(e?.target?.value)} | ||
/> | ||
</label> | ||
</div> | ||
<div /> | ||
<button class="submitButton" onClick={async () => onSubmit(canvas)}> | ||
Save | ||
</button> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks to be unused