feat: image resizer

This commit is contained in:
Ivan Rezchikov
2026-07-15 22:19:26 +03:00
parent e56157f562
commit 687fbaf3a6
7 changed files with 80 additions and 23 deletions
+1 -1
View File
@@ -31,7 +31,7 @@ function DraftItem({ draft, onDeleted }: { draft: Draft; onDeleted: (id: string)
{draft.title}
</h3>
<div className="flex flex-row items-center gap-2">
<Link href={`/news/${draft.slug}/edit`}>
<Link href={`/${draft.type.toLocaleLowerCase()}/${draft.slug}/edit`}>
<Button variant="secondary" size="icon-sm"><PencilIcon /></Button>
</Link>
<Button disabled={isPending} variant="secondary" size="icon-sm" onClick={() => run(draft.id, draft.type)}>
+54 -11
View File
@@ -7,7 +7,7 @@ import { Button } from "@/components/ui/button";
import { Field, FieldError, FieldGroup, FieldLabel, FieldSet } from "@/components/ui/field";
import { InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput } from "@/components/ui/input-group";
import { Block, BlockType, Page, PageMeta, PageType } from "@/lib/pages/types";
import { Globe2Icon, PlusIcon, Trash2Icon, XIcon, ChevronDownIcon, ChevronUpIcon, ImageIcon, QuoteIcon, AlertCircleIcon, MinusIcon, ListIcon, TableIcon, TypeIcon, AlignLeftIcon, InfoIcon, DotIcon, ListOrderedIcon, LinkIcon } from "lucide-react";
import { Globe2Icon, PlusIcon, Trash2Icon, XIcon, ChevronDownIcon, ChevronUpIcon, ImageIcon, QuoteIcon, AlertCircleIcon, MinusIcon, ListIcon, TableIcon, TypeIcon, AlignLeftIcon, InfoIcon, DotIcon, ListOrderedIcon, LinkIcon, MoveDiagonalIcon, MoveDiagonal2Icon } from "lucide-react";
import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue } from "./ui/select";
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu";
import { cn } from "@/lib/utils";
@@ -346,7 +346,7 @@ function BlockInput({ block, onChange, previewUrls, onPreviewReady }: { block: B
value={String((block.content as { type: "HEADING"; text: string; level: 1 | 2 | 3 }).level ?? 2)}
onValueChange={(v) => onChange({ type: "HEADING", text: (block.content as { type: "HEADING"; text: string; level: 1 | 2 | 3 }).text ?? "", level: Number(v) as 1 | 2 | 3 })}
>
<SelectTrigger className="w-16 h-7 opacity-0 group-hover:opacity-100 transition-opacity">
<SelectTrigger className="w-16 h-7">
<SelectValue />
</SelectTrigger>
<SelectContent>
@@ -626,7 +626,43 @@ function TableEditor({ content, onChange }: { content: { type: "TABLE"; headers:
function ImageBlock({ block, previewUrl, onChange, onPreviewReady }: { block: Block; previewUrl?: string; onChange: (c: Block["content"]) => void; onPreviewReady: (id: string, url: string) => void }) {
const content = block.content as { type: "IMAGE"; url: string; caption?: string; width?: number };
const [isUploading, setIsUploading] = useState(false);
const currentWidth = content.width ?? 100;
const [isDragging, setIsDragging] = useState(false);
const [supportsDrag, setSupportsDrag] = useState(false);
const dragStartRef = useRef<{ x: number; width: number } | null>(null);
const imgRef = useRef<HTMLImageElement>(null);
useEffect(() => {
setSupportsDrag(window.matchMedia("(hover: hover) and (pointer: fine)").matches);
}, []);
const currentWidth = content.width;
function handleDragStart(e: React.PointerEvent) {
e.preventDefault();
e.stopPropagation();
const startWidth = imgRef.current?.offsetWidth ?? 500;
dragStartRef.current = { x: e.clientX, width: startWidth };
setIsDragging(true);
function handleMove(ev: PointerEvent) {
if (!dragStartRef.current) return;
const delta = ev.clientX - dragStartRef.current.x;
const newWidth = Math.round(
Math.min(800, Math.max(150, dragStartRef.current.width + delta))
);
onChange({ type: "IMAGE", url: content.url, caption: content.caption ?? "", width: newWidth });
}
function handleUp() {
setIsDragging(false);
dragStartRef.current = null;
window.removeEventListener("pointermove", handleMove);
window.removeEventListener("pointerup", handleUp);
}
window.addEventListener("pointermove", handleMove);
window.addEventListener("pointerup", handleUp);
}
async function handleFileSelect(file: File) {
setIsUploading(true);
@@ -653,24 +689,31 @@ function ImageBlock({ block, previewUrl, onChange, onPreviewReady }: { block: Bl
label=""
variant="editor"
value={previewUrl}
previewStyle={currentWidth < 100 ? { width: `${currentWidth}%` } : undefined}
imgRef={imgRef}
previewStyle={currentWidth ? { width: `min(${currentWidth}px, 100%)` } : undefined}
onFileSelect={handleFileSelect}
onRemove={handleRemove}
isUploading={isUploading}
caption={content.caption}
onCaptionChange={(caption) => onChange({ type: "IMAGE", url: content.url, caption, width: content.width })}
resizeHandle={
supportsDrag && content.url ? (
<Button variant="secondary" size="icon-xs" className="absolute bottom-1 right-1 cursor-nwse-resize touch-none" onPointerDown={handleDragStart}><MoveDiagonal2Icon /></Button>
) : null
}
/>
{content.url && (
{content.url && !supportsDrag && (
<div className="flex flex-col gap-2">
<div className="flex items-center justify-between gap-2">
<Label className="text-xs md:text-sm">Image size</Label>
<span className="text-xs md:text-sm text-muted-foreground">{currentWidth}%</span>
<span className="text-xs md:text-sm text-muted-foreground">{currentWidth ?? 800}px</span>
</div>
<Slider
value={[currentWidth]}
min={10}
max={100}
step={1}
value={[currentWidth ?? 800]}
min={150}
max={800}
step={10}
onValueChange={([v]) => onChange({ type: "IMAGE", url: content.url, caption: content.caption ?? "", width: v })}
/>
</div>
@@ -701,7 +744,7 @@ function getDefaultContent(type: BlockType): Block["content"] {
case "DIVIDER": return { type: "DIVIDER" };
case "LIST": return { type: "LIST", style: "BULLET", items: [""] };
case "TABLE": return { type: "TABLE", headers: ["", ""], rows: [["", ""]] };
case "IMAGE": return { type: "IMAGE", url: "", caption: "", width: 100 };
case "IMAGE": return { type: "IMAGE", url: "", caption: "", width: undefined };
case "LINK": return { type: "LINK", label: "", href: "" };
default: return { type: "PARAGRAPH", text: "" };
}
+16 -4
View File
@@ -26,6 +26,8 @@ type Props = {
children?: React.ReactNode;
caption?: string;
onCaptionChange?: (caption: string) => void;
imgRef?: React.Ref<HTMLImageElement>;
resizeHandle?: React.ReactNode;
};
const MAX_SIZE = 5 * 1024 * 1024;
@@ -56,7 +58,7 @@ function fileToFileList(file: File): FileList {
return dt.files;
}
export function ImageUploader({ id, name, label, description, error, variant = "default", previewClassName, previewStyle, accept = DEFAULT_ACCEPT, required, value, onFileSelect, onRemove, isUploading, children, caption, onCaptionChange }: Props) {
export function ImageUploader({ id, name, label, description, error, variant = "default", previewClassName, previewStyle, accept = DEFAULT_ACCEPT, required, value, onFileSelect, onRemove, isUploading, children, caption, onCaptionChange, imgRef, resizeHandle }: Props) {
const inputRef = useRef<HTMLInputElement>(null);
const [previewUrl, setPreviewUrl] = useState<string | null>(null);
const [fileInfo, setFileInfo] = useState<{ name: string; size: number } | null>(null);
@@ -124,16 +126,26 @@ export function ImageUploader({ id, name, label, description, error, variant = "
{displayUrl ? (
<div className={EDITOR_WRAPPER_CLASS}>
<div className="group relative rounded-md">
<img src={displayUrl} alt="" className={previewClassName ?? EDITOR_IMG_CLASS} style={previewStyle} />
<div
className="relative rounded-md max-w-full"
style={previewStyle}
>
<img
ref={imgRef}
src={displayUrl}
alt=""
className={previewClassName ?? "block w-full h-auto rounded-md border object-contain"}
/>
<div className="absolute top-2 right-2 opacity-0 group-hover:opacity-100 transition-opacity">
<div className="absolute top-2 right-2">
<Button type="button" variant="secondary" size="sm" onClick={() => inputRef.current?.click()} disabled={isUploading}>
<RefreshCwIcon />
Replace
</Button>
</div>
{resizeHandle}
{isUploading && (
<div className="absolute inset-0 flex items-center justify-center bg-background/90 rounded-md">
<Spinner />
+1 -1
View File
@@ -8,7 +8,7 @@ export function NewsCard({ news }: { news: PagePublic }) {
return (
<div className="flex flex-col gap-1">
<Link href={`/news/${news.slug}`} className="w-max">
<h2 className="hover:underline underline-offset-8 decoration-2 hover:duration-200 cursor-pointer">{news.title}</h2>
<h2 className="hover:underline underline-offset-8 decoration-2 cursor-pointer">{news.title}</h2>
</Link>
<div className="flex flex-col md:flex-row gap-2 md:gap-8 md:items-center">
<div className="flex flex-row gap-4 md:gap-8">