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
+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: "" };
}