π¬ A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots π
TypeScript browser-extensionchrome-extensiondocumentationfirefox-extensionguide-generatorlocal-firstmanifest-v3privacy-firstproductivityreactscreenshotstep-by-step-guidetutorialtypescriptworkflowwxt
refactor(ui): group the annotation pointer gesture into a hook
Luis Ortizc7b443a54c31
diff --git a/AGENTS.md b/AGENTS.mdindex 944cff3..6fef603 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -777,11 +777,15 @@ because each is bound to its own control; what the hook owns is the machinery arsnapshot, the diff, the debounce, the flush on unmount and the saved badge β which is the part thatwas subtle and the part nobody should have to read twice.-`AnnotationEditor` resisted a bulk rewrite and is worth saying so: twenty-eight states, most of them-genuinely interdependent canvas state. Undo/redo and the text style came out cleanly because both-are self-contained; a regex pass across the rest broke the file's syntax and was reverted. The-drawing gesture β `draft`, `cropDraft`, `grabbing`, `hovering`, `anchor` β is still a cluster, and-still wants a careful hand rather than a script.+`AnnotationEditor` went from twenty-eight states to sixteen across three hooks β `useEditHistory`,+`useTextStyle` and `usePointerGesture`, the last holding everything the pointer is doing right now:+the shape being drawn, the crop being dragged, the hover and grab cursors and the floating toolbar's+anchor. What is left is genuinely interdependent canvas state.++How it was done matters more than the count. A regex pass over the style names rewrote object keys+and type members as well as reads and broke the file's syntax; it was reverted and redone as a list+of exact string replacements. Every later cluster was done the same way. A file this size does not+take a search and replace.## Fullview Storediff --git a/packages/ui/src/shared/AnnotationEditor.tsx b/packages/ui/src/shared/AnnotationEditor.tsxindex d279eb6..c2f4e98 100644--- a/packages/ui/src/shared/AnnotationEditor.tsx+++ b/packages/ui/src/shared/AnnotationEditor.tsx@@ -42,6 +42,7 @@ import {import { Popover, PopoverContent, PopoverTrigger } from '@mimik/ui/components/ui/popover';import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@mimik/ui/components/ui/tooltip';import { useEditHistory } from '@mimik/ui/shared/use-edit-history';+import { usePointerGesture } from '@mimik/ui/shared/use-pointer-gesture';import { useTextStyle } from '@mimik/ui/shared/use-text-style';import {ArrowUpRight,@@ -316,17 +317,13 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:const [viewport, setViewport] = useState<ScreenshotBounds | undefined>(screenshot.edits?.viewport);const [selectedId, setSelectedId] = useState<string | null>(tool === 'target' ? TARGET_ID : null);const [color, setColor] = useState<string>(COLORS[0]);- const [draft, setDraft] = useState<Annotation | null>(null);- const [cropDraft, setCropDraft] = useState<ScreenshotBounds | null>(null);+ const gesture = usePointerGesture();const [textEditor, setTextEditor] = useState<TextEditorState | null>(null);const [textValue, setTextValue] = useState('');const [bitmap, setBitmap] = useState<ImageBitmap | null>(null);const [saving, setSaving] = useState(false);- const [hovering, setHovering] = useState(false);- const [anchor, setAnchor] = useState<{ x: number; y: number } | null>(null);const [pulse, setPulse] = useState(0);const [defaultTargetColor, setDefaultTargetColor] = useState(DEFAULT_TARGET_COLOR);- const [grabbing, setGrabbing] = useState(false);const [fill] = useState('transparent');const [lineWidth] = useState<LineWidth>('ms');@@ -444,11 +441,11 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:useEffect(() => {const sel = annotationsRef.current.find((a) => a.id === selectedId);if (!sel) {- setAnchor(null);+ gesture.setAnchor(null);return;}const b = annotationBounds(sel);- setAnchor({ x: b.x + b.width / 2, y: b.y + b.height });+ gesture.setAnchor({ x: b.x + b.width / 2, y: b.y + b.height });}, [selectedId]);useEffect(() => {@@ -500,17 +497,17 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:}drawAnnotation(ctx, a, 0, 0);}- if (draft) {+ if (gesture.draft) {ctx.save();ctx.globalAlpha = 0.7;- drawAnnotation(ctx, draft, 0, 0);+ drawAnnotation(ctx, gesture.draft, 0, 0);ctx.restore();}const scale = getScale();if (mode === 'crop') {- const frame = cropDraft ?? viewport ?? { x: 0, y: 0, width: canvas.width, height: canvas.height };+ const frame = gesture.cropDraft ?? viewport ?? { x: 0, y: 0, width: canvas.width, height: canvas.height };ctx.save();ctx.fillStyle = 'rgba(30, 27, 75, 0.55)';ctx.beginPath();@@ -551,7 +548,7 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:}ctx.restore();}- }, [annotations, draft, cropDraft, selectedId, bitmap, getScale, mode, viewport, pulse]);+ }, [annotations, gesture.draft, gesture.cropDraft, selectedId, bitmap, getScale, mode, viewport, pulse]);const handlePointerDown = (e: React.PointerEvent<HTMLCanvasElement>) => {e.currentTarget.setPointerCapture(e.pointerId);@@ -563,24 +560,24 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:const handle = hitHandle(frame, p.x, p.y, HANDLE_HIT_PX * scale);if (handle) {dragRef.current = { mode: 'cropResize', handle, rect: frame, lastX: p.x, lastY: p.y };- setCropDraft(frame);+ gesture.setCropDraft(frame);return;}if (p.x > frame.x && p.x < frame.x + frame.width && p.y > frame.y && p.y < frame.y + frame.height) {dragRef.current = { mode: 'cropMove', rect: frame, lastX: p.x, lastY: p.y };- setCropDraft(frame);+ gesture.setCropDraft(frame);return;}const rect: ScreenshotBounds = { x: p.x, y: p.y, width: 0, height: 0 };dragRef.current = { mode: 'crop', start: p, rect };- setCropDraft(rect);+ gesture.setCropDraft(rect);return;}if (mode === 'redact') {const shape: Annotation = { id: DRAFT_ID, type: 'redact', x: p.x, y: p.y, w: 0, h: 0, style: 'blur' };dragRef.current = { mode: 'draw', start: p, shape };- setDraft(shape);+ gesture.setDraft(shape);return;}@@ -600,7 +597,7 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:pushHistory();setSelectedId(hit.id);dragRef.current = { mode: 'move', id: hit.id, lastX: p.x, lastY: p.y };- setGrabbing(true);+ gesture.setGrabbing(true);return;}setSelectedId(null);@@ -639,7 +636,7 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:shape = { id: DRAFT_ID, type: 'box', x: p.x, y: p.y, w: 0, h: 0, color, fill, lineWidth, radius };}dragRef.current = { mode: 'draw', start: p, shape };- setDraft(shape);+ gesture.setDraft(shape);};const handlePointerMove = (e: React.PointerEvent<HTMLCanvasElement>) => {@@ -647,7 +644,7 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:if (!drag) {if (mode === 'annotate') {const p = toImageSpace(e);- setHovering(Boolean(hitTest(annotations, p.x, p.y)));+ gesture.setHovering(Boolean(hitTest(annotations, p.x, p.y)));}return;}@@ -666,7 +663,7 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:const moved = next.find((a) => a.id === drag.id);if (moved) {const b = annotationBounds(moved);- setAnchor({ x: b.x + b.width / 2, y: b.y + b.height });+ gesture.setAnchor({ x: b.x + b.width / 2, y: b.y + b.height });}return;}@@ -686,7 +683,7 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:height: Math.max(MIN_CROP_SIZE, top ? r.height - dy : r.height + dy),};drag.rect = cropTo(next, { width: screenshot.width, height: screenshot.height });- setCropDraft(drag.rect);+ gesture.setCropDraft(drag.rect);return;}@@ -699,7 +696,7 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:{ ...drag.rect, x: drag.rect.x + dx, y: drag.rect.y + dy },{ width: screenshot.width, height: screenshot.height },);- setCropDraft(drag.rect);+ gesture.setCropDraft(drag.rect);return;}@@ -711,7 +708,7 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:height: Math.abs(p.y - drag.start.y),};drag.rect = rect;- setCropDraft(rect);+ gesture.setCropDraft(rect);return;}@@ -731,22 +728,22 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:next = { ...shape, points: [...shape.points, p.x, p.y] };}drag.shape = next;- setDraft(next);+ gesture.setDraft(next);};const handlePointerUp = () => {- setGrabbing(false);+ gesture.setGrabbing(false);const settled = annotations.find((a) => a.id === selectedId);if (settled) {const b = annotationBounds(settled);- setAnchor({ x: b.x + b.width / 2, y: b.y + b.height });+ gesture.setAnchor({ x: b.x + b.width / 2, y: b.y + b.height });}const drag = dragRef.current;dragRef.current = null;if (!drag) return;if (drag.mode === 'crop' || drag.mode === 'cropResize' || drag.mode === 'cropMove') {- setCropDraft(null);+ gesture.setCropDraft(null);const rect = drag.rect;if (rect.width < MIN_CROP_SIZE || rect.height < MIN_CROP_SIZE) return;setViewport(cropTo(rect, { width: screenshot.width, height: screenshot.height }));@@ -754,7 +751,7 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:}if (drag.mode === 'draw') {- setDraft(null);+ gesture.setDraft(null);const shape = drag.shape;if (shape.type === 'freehand') {if (shape.points.length < 4) return;@@ -773,8 +770,7 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:const handlePointerCancel = () => {dragRef.current = null;- setDraft(null);- setCropDraft(null);+ gesture.clear();};const setProp = (key: string, value: unknown) => {@@ -943,7 +939,7 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:type="button"onClick={() => {setViewport(undefined);- setCropDraft(null);+ gesture.setCropDraft(null);}}className="flex items-center gap-1.5 px-2.5 h-8 rounded-lg text-[12px] font-semibold text-foreground/75 hover:bg-secondary">@@ -976,7 +972,7 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:width={screenshot.width}height={screenshot.height}className="block max-w-full max-h-[calc(100vh-98px)] rounded-lg shadow-2xl touch-none"- style={{ cursor: cursorFor(mode, activeTool, hovering, grabbing) }}+ style={{ cursor: cursorFor(mode, activeTool, gesture.hovering, gesture.grabbing) }}onPointerDown={handlePointerDown}onPointerMove={handlePointerMove}onPointerUp={handlePointerUp}@@ -1013,12 +1009,12 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:}}/>)}- {selected && anchor && (+ {selected && gesture.anchor && (<divclassName="absolute z-20 -translate-x-1/2 flex flex-col items-center gap-1.5"style={{- left: `${((anchor?.x ?? 0) / screenshot.width) * 100}%`,- top: `${((anchor?.y ?? 0) / screenshot.height) * 100}%`,+ left: `${((gesture.anchor?.x ?? 0) / screenshot.width) * 100}%`,+ top: `${((gesture.anchor?.y ?? 0) / screenshot.height) * 100}%`,}}><div className="mt-2 flex items-center gap-1 rounded-xl bg-primary px-2 py-1.5 shadow-xl">diff --git a/packages/ui/src/shared/use-pointer-gesture.ts b/packages/ui/src/shared/use-pointer-gesture.tsnew file mode 100644index 0000000..2298324--- /dev/null+++ b/packages/ui/src/shared/use-pointer-gesture.ts@@ -0,0 +1,44 @@+import type { ScreenshotBounds } from '@mimik/core/guides/types';+import type { Annotation } from '@mimik/core/screenshot/types';+import { useState } from 'react';++export interface PointerGesture {+ draft: Annotation | null;+ setDraft: (next: Annotation | null) => void;+ cropDraft: ScreenshotBounds | null;+ setCropDraft: (next: ScreenshotBounds | null) => void;+ hovering: boolean;+ setHovering: (next: boolean) => void;+ grabbing: boolean;+ setGrabbing: (next: boolean) => void;+ anchor: { x: number; y: number } | null;+ setAnchor: (next: { x: number; y: number } | null) => void;+ clear: () => void;+}++export function usePointerGesture(): PointerGesture {+ const [draft, setDraft] = useState<Annotation | null>(null);+ const [cropDraft, setCropDraft] = useState<ScreenshotBounds | null>(null);+ const [hovering, setHovering] = useState(false);+ const [grabbing, setGrabbing] = useState(false);+ const [anchor, setAnchor] = useState<{ x: number; y: number } | null>(null);++ const clear = () => {+ setDraft(null);+ setCropDraft(null);+ };++ return {+ draft,+ setDraft,+ cropDraft,+ setCropDraft,+ hovering,+ setHovering,+ grabbing,+ setGrabbing,+ anchor,+ setAnchor,+ clear,+ };+}