feat: image resizer

This commit is contained in:
Ivan Rezchikov
2026-07-13 19:18:15 +03:00
parent d59b4a79f8
commit 22122aedeb
5 changed files with 61 additions and 19 deletions
+6 -1
View File
@@ -135,6 +135,7 @@ function BlockRenderer({ block }: { block: Block }) {
);
case "IMAGE":
const imageWidth = (b as { width?: number }).width;
return (
<figure className="flex flex-col gap-2">
<img
@@ -142,7 +143,11 @@ function BlockRenderer({ block }: { block: Block }) {
alt={b.caption || "Image"}
width={1200}
height={800}
className="w-full md:w-auto md:max-w-200 h-auto rounded-md border object-contain"
style={imageWidth && imageWidth < 100 ? { width: `${imageWidth}%` } : undefined}
className={cn(
"h-auto rounded-md border object-contain",
imageWidth && imageWidth < 100 ? "" : "w-full md:w-auto md:max-w-200"
)}
/>
{b.caption && (
+1 -1
View File
@@ -17,7 +17,7 @@ export default function CreatePageClient() {
const dataRef = useRef<EditorData>({
title: "",
type: "NEWS",
type: "PAGE",
slug: "",
location: "",
tags: [],
+52 -15
View File
@@ -621,9 +621,12 @@ function TableEditor({ content, onChange }: { content: { type: "TABLE"; headers:
);
}
const WIDTH_PRESETS = [25, 50, 75, 100] as const;
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 };
const content = block.content as { type: "IMAGE"; url: string; caption?: string; width?: number };
const [isUploading, setIsUploading] = useState(false);
const currentWidth = content.width ?? 100;
async function handleFileSelect(file: File) {
setIsUploading(true);
@@ -631,7 +634,7 @@ function ImageBlock({ block, previewUrl, onChange, onPreviewReady }: { block: Bl
const key = await uploadPageImage(file);
const url = await getImagePreviewUrl(key);
onPreviewReady(block.id, url);
onChange({ type: "IMAGE", url: key, caption: content.caption ?? "" });
onChange({ type: "IMAGE", url: key, caption: content.caption ?? "", width: content.width });
} catch (e) {
console.error("Failed to upload image:", e);
} finally {
@@ -640,21 +643,55 @@ function ImageBlock({ block, previewUrl, onChange, onPreviewReady }: { block: Bl
}
function handleRemove() {
onChange({ type: "IMAGE", url: "", caption: content.caption ?? "" });
onChange({ type: "IMAGE", url: "", caption: content.caption ?? "", width: content.width });
}
function handleWidthChange(width: number) {
onChange({ type: "IMAGE", url: content.url, caption: content.caption ?? "", width });
}
return (
<ImageUploader
id={`image-${block.id}`}
label=""
variant="editor"
value={previewUrl}
onFileSelect={handleFileSelect}
onRemove={handleRemove}
isUploading={isUploading}
caption={content.caption}
onCaptionChange={(caption) => onChange({ type: "IMAGE", url: content.url, caption })}
/>
<div className="flex flex-col gap-2">
<ImageUploader
id={`image-${block.id}`}
label=""
variant="editor"
value={previewUrl}
onFileSelect={handleFileSelect}
onRemove={handleRemove}
isUploading={isUploading}
caption={content.caption}
onCaptionChange={(caption) => onChange({ type: "IMAGE", url: content.url, caption, width: content.width })}
/>
{content.url && (
<div className="flex flex-col gap-2">
<div className="flex items-center gap-3">
<input
type="range"
min={10}
max={100}
value={currentWidth}
onChange={(e) => handleWidthChange(Number(e.target.value))}
className="flex-1 h-1.5 bg-muted rounded-full appearance-none cursor-pointer accent-primary"
/>
<span className="text-xs text-muted-foreground w-8 text-right tabular-nums">{currentWidth}%</span>
</div>
<div className="flex gap-1.5">
{WIDTH_PRESETS.map((preset) => (
<Button
key={preset}
variant={currentWidth === preset ? "default" : "outline"}
size="sm"
className="h-7 px-2 text-xs"
onClick={() => handleWidthChange(preset)}
>
{preset}%
</Button>
))}
</div>
</div>
)}
</div>
);
}
@@ -680,7 +717,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: "" };
case "IMAGE": return { type: "IMAGE", url: "", caption: "", width: 100 };
case "LINK": return { type: "LINK", label: "", href: "" };
default: return { type: "PARAGRAPH", text: "" };
}
+1 -1
View File
@@ -44,7 +44,7 @@ export type BlockContent =
| { type: "QUOTE"; text: string; author?: string }
| { type: "DIVIDER" }
| { type: "CALLOUT"; title: string; text: string }
| { type: "IMAGE"; url: string; caption?: string }
| { type: "IMAGE"; url: string; caption?: string; width?: number }
| { type: "LIST"; style: "BULLET" | "ORDERED"; items: string[] }
| { type: "TABLE"; headers: string[]; rows: string[][] }
| { type: "LINK"; label: string; href: string }
+1 -1
View File
@@ -6,7 +6,7 @@ const blockContentSchema = z.discriminatedUnion("type", [
z.object({ type: z.literal("QUOTE"), text: z.string(), author: z.string().optional() }),
z.object({ type: z.literal("DIVIDER") }),
z.object({ type: z.literal("CALLOUT"), title: z.string(), text: z.string() }),
z.object({ type: z.literal("IMAGE"), url: z.string(), caption: z.string().optional() }),
z.object({ type: z.literal("IMAGE"), url: z.string(), caption: z.string().optional(), width: z.number().min(1).max(100).optional() }),
z.object({ type: z.literal("LIST"), style: z.enum(["BULLET", "ORDERED"]), items: z.array(z.string()) }),
z.object({ type: z.literal("TABLE"), headers: z.array(z.string()), rows: z.array(z.array(z.string())) }),
z.object({ type: z.literal("LINK"), label: z.string(), href: z.string() }),