Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots 📖

feat(editor): make crop adjustable and give replace image a proper dialog

Luis Ortizb0404c6b2083
diff --git a/src/locales/en.yml b/src/locales/en.yml
index db361ed..4f8d8f6 100644
--- a/src/locales/en.yml
+++ b/src/locales/en.yml
@@ -167,6 +167,9 @@ screenshotView:
redact: Redact
crop: Crop
replaceImage: Replace image
+ replaceHint: "Choose a new image for this step"
+ replaceDrop: "Click to browse or drop an image here"
+ replaceFormats: "PNG, JPG or WebP"
download: Download
deleteImage: Delete image
deleteConfirm: "Delete this screenshot? This cannot be undone."
diff --git a/src/locales/es.yml b/src/locales/es.yml
index 43addc5..fe939f0 100644
--- a/src/locales/es.yml
+++ b/src/locales/es.yml
@@ -167,6 +167,9 @@ screenshotView:
redact: Ocultar
crop: Recortar
replaceImage: Reemplazar imagen
+ replaceHint: "Elige una nueva imagen para este paso"
+ replaceDrop: "Haz clic para buscar o suelta una imagen aquí"
+ replaceFormats: "PNG, JPG o WebP"
download: Descargar
deleteImage: Eliminar imagen
deleteConfirm: "¿Eliminar esta captura? No hay vuelta atrás."
diff --git a/src/locales/fr.yml b/src/locales/fr.yml
index de1380f..49dfb23 100644
--- a/src/locales/fr.yml
+++ b/src/locales/fr.yml
@@ -167,6 +167,9 @@ screenshotView:
redact: "Masquer"
crop: "Recadrer"
replaceImage: "Remplacer l'image"
+ replaceHint: "Choisissez une nouvelle image pour cette étape"
+ replaceDrop: "Cliquez pour parcourir ou déposez une image ici"
+ replaceFormats: "PNG, JPG ou WebP"
download: "Télécharger"
deleteImage: "Supprimer l'image"
deleteConfirm: "Supprimer cette capture ? Pas de retour en arrière."
diff --git a/src/locales/pt-BR.yml b/src/locales/pt-BR.yml
index 95799ff..2b4edd3 100644
--- a/src/locales/pt-BR.yml
+++ b/src/locales/pt-BR.yml
@@ -167,6 +167,9 @@ screenshotView:
redact: Ocultar
crop: Recortar
replaceImage: Substituir imagem
+ replaceHint: "Escolha uma nova imagem para esta etapa"
+ replaceDrop: "Clique para procurar ou solte uma imagem aqui"
+ replaceFormats: "PNG, JPG ou WebP"
download: Baixar
deleteImage: Excluir imagem
deleteConfirm: "Excluir essa captura? Não tem como desfazer."
diff --git a/src/ui/shared/AnnotationEditor.tsx b/src/ui/shared/AnnotationEditor.tsx
index 47768a4..5ac3382 100644
--- a/src/ui/shared/AnnotationEditor.tsx
+++ b/src/ui/shared/AnnotationEditor.tsx
@@ -54,6 +54,8 @@ type DragState =
| { mode: 'move'; id: string; lastX: number; lastY: number }
| { mode: 'resize'; id: string; handle: Handle; lastX: number; lastY: number }
| { mode: 'crop'; start: { x: number; y: number }; rect: ScreenshotBounds }
+ | { mode: 'cropResize'; handle: Handle; rect: ScreenshotBounds; lastX: number; lastY: number }
+ | { mode: 'cropMove'; rect: ScreenshotBounds; lastX: number; lastY: number }
| { mode: 'draw'; start: { x: number; y: number }; shape: Annotation };
const COLORS = ['#4F46E5', '#DC2626', '#059669', '#F59E0B', '#1E1B4B'];
@@ -319,12 +321,22 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:
const scale = getScale();
- if (cropDraft) {
+ if (mode === 'crop') {
+ const frame = cropDraft ?? viewport ?? { x: 0, y: 0, width: canvas.width, height: canvas.height };
ctx.save();
- ctx.strokeStyle = '#4F46E5';
+ ctx.fillStyle = 'rgba(30, 27, 75, 0.55)';
+ ctx.beginPath();
+ ctx.rect(0, 0, canvas.width, canvas.height);
+ ctx.rect(frame.x, frame.y, frame.width, frame.height);
+ ctx.fill('evenodd');
+ ctx.strokeStyle = '#FFFFFF';
ctx.lineWidth = 2 * scale;
- ctx.setLineDash([6 * scale, 4 * scale]);
- ctx.strokeRect(cropDraft.x, cropDraft.y, cropDraft.width, cropDraft.height);
+ ctx.strokeRect(frame.x, frame.y, frame.width, frame.height);
+ const hs = HANDLE_DISPLAY_SIZE * scale;
+ ctx.fillStyle = '#FFFFFF';
+ for (const [, hx, hy] of handleCorners(frame)) {
+ ctx.fillRect(hx - hs / 2, hy - hs / 2, hs, hs);
+ }
ctx.restore();
}
@@ -344,7 +356,7 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:
}
ctx.restore();
}
- }, [annotations, draft, cropDraft, selectedId, bitmap, getScale]);
+ }, [annotations, draft, cropDraft, selectedId, bitmap, getScale, mode, viewport]);
const handlePointerDown = (e: React.PointerEvent<HTMLCanvasElement>) => {
e.currentTarget.setPointerCapture(e.pointerId);
@@ -373,6 +385,19 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:
}
if (mode === 'crop') {
+ const scale = getScale();
+ const frame = viewport ?? { x: 0, y: 0, width: screenshot.width, height: screenshot.height };
+ 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);
+ 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);
+ return;
+ }
const rect: ScreenshotBounds = { x: p.x, y: p.y, width: 0, height: 0 };
dragRef.current = { mode: 'crop', start: p, rect };
setCropDraft(rect);
@@ -444,6 +469,38 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:
return;
}
+ if (drag.mode === 'cropResize') {
+ const dx = p.x - drag.lastX;
+ const dy = p.y - drag.lastY;
+ drag.lastX = p.x;
+ drag.lastY = p.y;
+ const left = drag.handle === 'nw' || drag.handle === 'sw';
+ const top = drag.handle === 'nw' || drag.handle === 'ne';
+ const r = drag.rect;
+ const next: ScreenshotBounds = {
+ x: left ? r.x + dx : r.x,
+ y: top ? r.y + dy : r.y,
+ width: Math.max(MIN_CROP_SIZE, left ? r.width - dx : r.width + dx),
+ 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);
+ return;
+ }
+
+ if (drag.mode === 'cropMove') {
+ const dx = p.x - drag.lastX;
+ const dy = p.y - drag.lastY;
+ drag.lastX = p.x;
+ drag.lastY = p.y;
+ drag.rect = cropTo(
+ { ...drag.rect, x: drag.rect.x + dx, y: drag.rect.y + dy },
+ { width: screenshot.width, height: screenshot.height },
+ );
+ setCropDraft(drag.rect);
+ return;
+ }
+
if (drag.mode === 'crop') {
const rect: ScreenshotBounds = {
x: Math.min(drag.start.x, p.x),
@@ -480,12 +537,11 @@ export default function AnnotationEditor({ screenshot, tool, onDone, onCancel }:
dragRef.current = null;
if (!drag) return;
- if (drag.mode === 'crop') {
+ if (drag.mode === 'crop' || drag.mode === 'cropResize' || drag.mode === 'cropMove') {
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 }));
- setActiveTool('select');
return;
}
diff --git a/src/ui/shared/ReplaceImageDialog.tsx b/src/ui/shared/ReplaceImageDialog.tsx
new file mode 100644
index 0000000..199de06
--- /dev/null
+++ b/src/ui/shared/ReplaceImageDialog.tsx
@@ -0,0 +1,68 @@
+import { ImageUp } from 'lucide-react';
+import { useRef, useState } from 'react';
+import { i18n } from '#imports';
+import { Button } from '@/ui/components/ui/button';
+import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/ui/components/ui/dialog';
+
+interface ReplaceImageDialogProps {
+ open: boolean;
+ onSelect: (file: File) => void;
+ onCancel: () => void;
+}
+
+export default function ReplaceImageDialog({ open, onSelect, onCancel }: ReplaceImageDialogProps) {
+ const inputRef = useRef<HTMLInputElement>(null);
+ const [dragging, setDragging] = useState(false);
+
+ const take = (file: File | undefined) => {
+ if (file?.type.startsWith('image/')) onSelect(file);
+ };
+
+ return (
+ <Dialog open={open} onOpenChange={(next) => !next && onCancel()}>
+ <DialogContent className="sm:max-w-[420px]">
+ <DialogHeader>
+ <DialogTitle>{i18n.t('screenshotView.replaceImage')}</DialogTitle>
+ <DialogDescription>{i18n.t('screenshotView.replaceHint')}</DialogDescription>
+ </DialogHeader>
+ <button
+ type="button"
+ onClick={() => inputRef.current?.click()}
+ onDragOver={(e) => {
+ e.preventDefault();
+ setDragging(true);
+ }}
+ onDragLeave={() => setDragging(false)}
+ onDrop={(e) => {
+ e.preventDefault();
+ setDragging(false);
+ take(e.dataTransfer.files?.[0]);
+ }}
+ className={`w-full flex flex-col items-center justify-center gap-2 rounded-xl border-2 border-dashed py-10 transition-colors ${
+ dragging ? 'border-accent bg-secondary' : 'border-border hover:border-accent hover:bg-secondary/50'
+ }`}
+ >
+ <ImageUp size={26} className="text-accent" />
+ <span className="text-[13px] font-semibold text-foreground">{i18n.t('screenshotView.replaceDrop')}</span>
+ <span className="text-[11px] text-muted-foreground">{i18n.t('screenshotView.replaceFormats')}</span>
+ </button>
+ <div className="flex justify-end">
+ <Button variant="outline" size="sm" onClick={onCancel}>
+ {i18n.t('common.cancel')}
+ </Button>
+ </div>
+ <input
+ ref={inputRef}
+ type="file"
+ accept="image/*"
+ className="hidden"
+ onChange={(e) => {
+ const file = e.target.files?.[0];
+ e.target.value = '';
+ take(file);
+ }}
+ />
+ </DialogContent>
+ </Dialog>
+ );
+}
diff --git a/src/ui/shared/ScreenshotView.tsx b/src/ui/shared/ScreenshotView.tsx
index ecc6384..7d777ab 100644
--- a/src/ui/shared/ScreenshotView.tsx
+++ b/src/ui/shared/ScreenshotView.tsx
@@ -16,6 +16,7 @@ import {
} from '@/ui/components/ui/dropdown-menu';
import { Popover, PopoverContent, PopoverTrigger } from '@/ui/components/ui/popover';
import ConfirmDialog from '@/ui/shared/ConfirmDialog';
+import ReplaceImageDialog from '@/ui/shared/ReplaceImageDialog';
interface ScreenshotViewProps {
screenshot: Screenshot;
@@ -85,6 +86,7 @@ export default function ScreenshotView({
const [saved, setSaved] = useState(false);
const [altDraft, setAltDraft] = useState('');
const [confirmDelete, setConfirmDelete] = useState(false);
+ const [replaceOpen, setReplaceOpen] = useState(false);
const processedKeyRef = useRef<string | null>(null);
const urlRef = useRef<string | null>(null);
const idRef = useRef(screenshot.id);
@@ -92,7 +94,6 @@ export default function ScreenshotView({
const saveTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const savedTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const dragRef = useRef<DragState | null>(null);
- const fileInputRef = useRef<HTMLInputElement>(null);
const baseScreenshot = screenshotOverride ?? screenshot;
const effectiveEdits = editsOverride ?? screenshot.edits;
@@ -253,14 +254,8 @@ export default function ScreenshotView({
scheduleSave(nextEdits);
};
- const handleReplaceClick = () => {
- setTimeout(() => fileInputRef.current?.click(), 0);
- };
-
- const handleFileChange = async (e: React.ChangeEvent<HTMLInputElement>) => {
- const file = e.target.files?.[0];
- e.target.value = '';
- if (!file) return;
+ const handleReplaceFile = async (file: File) => {
+ setReplaceOpen(false);
const bitmap = await createImageBitmap(file);
const { width, height } = bitmap;
@@ -418,7 +413,7 @@ export default function ScreenshotView({
{i18n.t('screenshotView.crop')}
</DropdownMenuItem>
<DropdownMenuSeparator />
- <DropdownMenuItem onSelect={handleReplaceClick}>
+ <DropdownMenuItem onSelect={() => setReplaceOpen(true)}>
<ImageUp size={14} />
{i18n.t('screenshotView.replaceImage')}
</DropdownMenuItem>
@@ -433,7 +428,7 @@ export default function ScreenshotView({
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
- <input ref={fileInputRef} type="file" accept="image/*" className="hidden" onChange={handleFileChange} />
+ <ReplaceImageDialog open={replaceOpen} onSelect={handleReplaceFile} onCancel={() => setReplaceOpen(false)} />
<ConfirmDialog
open={confirmDelete}
title={i18n.t('screenshotView.deleteImage')}