|
| 1 | +import type { VNode, h as hType } from 'preact'; |
| 2 | +import type * as Hooks from 'preact/hooks'; |
| 3 | +import { DOCUMENT } from '../../constants'; |
| 4 | + |
| 5 | +interface FactoryParams { |
| 6 | + h: typeof hType; |
| 7 | +} |
| 8 | + |
| 9 | +export default function AnnotationsFactory({ |
| 10 | + h, // eslint-disable-line @typescript-eslint/no-unused-vars |
| 11 | +}: FactoryParams) { |
| 12 | + return function Annotations({ |
| 13 | + action, |
| 14 | + imageBuffer, |
| 15 | + annotatingRef, |
| 16 | + }: { |
| 17 | + action: 'crop' | 'annotate' | ''; |
| 18 | + imageBuffer: HTMLCanvasElement; |
| 19 | + annotatingRef: Hooks.Ref<HTMLCanvasElement>; |
| 20 | + }): VNode { |
| 21 | + const onAnnotateStart = (): void => { |
| 22 | + if (action !== 'annotate') { |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + const handleMouseMove = (moveEvent: MouseEvent): void => { |
| 27 | + const annotateCanvas = annotatingRef.current; |
| 28 | + if (annotateCanvas) { |
| 29 | + const rect = annotateCanvas.getBoundingClientRect(); |
| 30 | + const x = moveEvent.clientX - rect.x; |
| 31 | + const y = moveEvent.clientY - rect.y; |
| 32 | + |
| 33 | + const ctx = annotateCanvas.getContext('2d'); |
| 34 | + if (ctx) { |
| 35 | + ctx.lineTo(x, y); |
| 36 | + ctx.stroke(); |
| 37 | + ctx.beginPath(); |
| 38 | + ctx.moveTo(x, y); |
| 39 | + } |
| 40 | + } |
| 41 | + }; |
| 42 | + |
| 43 | + const handleMouseUp = (): void => { |
| 44 | + const ctx = annotatingRef.current?.getContext('2d'); |
| 45 | + if (ctx) { |
| 46 | + ctx.beginPath(); |
| 47 | + } |
| 48 | + |
| 49 | + // Add your apply annotation logic here |
| 50 | + applyAnnotation(); |
| 51 | + |
| 52 | + DOCUMENT.removeEventListener('mousemove', handleMouseMove); |
| 53 | + DOCUMENT.removeEventListener('mouseup', handleMouseUp); |
| 54 | + }; |
| 55 | + |
| 56 | + DOCUMENT.addEventListener('mousemove', handleMouseMove); |
| 57 | + DOCUMENT.addEventListener('mouseup', handleMouseUp); |
| 58 | + }; |
| 59 | + |
| 60 | + const applyAnnotation = (): void => { |
| 61 | + // Logic to apply the annotation |
| 62 | + const imageCtx = imageBuffer.getContext('2d'); |
| 63 | + const annotateCanvas = annotatingRef.current; |
| 64 | + if (imageCtx && annotateCanvas) { |
| 65 | + imageCtx.drawImage( |
| 66 | + annotateCanvas, |
| 67 | + 0, |
| 68 | + 0, |
| 69 | + annotateCanvas.width, |
| 70 | + annotateCanvas.height, |
| 71 | + 0, |
| 72 | + 0, |
| 73 | + imageBuffer.width, |
| 74 | + imageBuffer.height, |
| 75 | + ); |
| 76 | + |
| 77 | + const annotateCtx = annotateCanvas.getContext('2d'); |
| 78 | + if (annotateCtx) { |
| 79 | + annotateCtx.clearRect(0, 0, annotateCanvas.width, annotateCanvas.height); |
| 80 | + } |
| 81 | + } |
| 82 | + }; |
| 83 | + return ( |
| 84 | + <canvas |
| 85 | + class={`editor__annotation ${action === 'annotate' ? 'editor__annotation--active' : ''}`} |
| 86 | + onMouseDown={onAnnotateStart} |
| 87 | + ref={annotatingRef} |
| 88 | + ></canvas> |
| 89 | + ); |
| 90 | + }; |
| 91 | +} |
0 commit comments