feat: image resizer
This commit is contained in:
@@ -135,6 +135,7 @@ function BlockRenderer({ block }: { block: Block }) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
case "IMAGE":
|
case "IMAGE":
|
||||||
|
const imageWidth = (b as { width?: number }).width;
|
||||||
return (
|
return (
|
||||||
<figure className="flex flex-col gap-2">
|
<figure className="flex flex-col gap-2">
|
||||||
<img
|
<img
|
||||||
@@ -142,7 +143,11 @@ function BlockRenderer({ block }: { block: Block }) {
|
|||||||
alt={b.caption || "Image"}
|
alt={b.caption || "Image"}
|
||||||
width={1200}
|
width={1200}
|
||||||
height={800}
|
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 && (
|
{b.caption && (
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ export default function CreatePageClient() {
|
|||||||
|
|
||||||
const dataRef = useRef<EditorData>({
|
const dataRef = useRef<EditorData>({
|
||||||
title: "",
|
title: "",
|
||||||
type: "NEWS",
|
type: "PAGE",
|
||||||
slug: "",
|
slug: "",
|
||||||
location: "",
|
location: "",
|
||||||
tags: [],
|
tags: [],
|
||||||
|
|||||||
+42
-5
@@ -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 }) {
|
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 [isUploading, setIsUploading] = useState(false);
|
||||||
|
const currentWidth = content.width ?? 100;
|
||||||
|
|
||||||
async function handleFileSelect(file: File) {
|
async function handleFileSelect(file: File) {
|
||||||
setIsUploading(true);
|
setIsUploading(true);
|
||||||
@@ -631,7 +634,7 @@ function ImageBlock({ block, previewUrl, onChange, onPreviewReady }: { block: Bl
|
|||||||
const key = await uploadPageImage(file);
|
const key = await uploadPageImage(file);
|
||||||
const url = await getImagePreviewUrl(key);
|
const url = await getImagePreviewUrl(key);
|
||||||
onPreviewReady(block.id, url);
|
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) {
|
} catch (e) {
|
||||||
console.error("Failed to upload image:", e);
|
console.error("Failed to upload image:", e);
|
||||||
} finally {
|
} finally {
|
||||||
@@ -640,10 +643,15 @@ function ImageBlock({ block, previewUrl, onChange, onPreviewReady }: { block: Bl
|
|||||||
}
|
}
|
||||||
|
|
||||||
function handleRemove() {
|
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 (
|
return (
|
||||||
|
<div className="flex flex-col gap-2">
|
||||||
<ImageUploader
|
<ImageUploader
|
||||||
id={`image-${block.id}`}
|
id={`image-${block.id}`}
|
||||||
label=""
|
label=""
|
||||||
@@ -653,8 +661,37 @@ function ImageBlock({ block, previewUrl, onChange, onPreviewReady }: { block: Bl
|
|||||||
onRemove={handleRemove}
|
onRemove={handleRemove}
|
||||||
isUploading={isUploading}
|
isUploading={isUploading}
|
||||||
caption={content.caption}
|
caption={content.caption}
|
||||||
onCaptionChange={(caption) => onChange({ type: "IMAGE", url: content.url, 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 "DIVIDER": return { type: "DIVIDER" };
|
||||||
case "LIST": return { type: "LIST", style: "BULLET", items: [""] };
|
case "LIST": return { type: "LIST", style: "BULLET", items: [""] };
|
||||||
case "TABLE": return { type: "TABLE", headers: ["", ""], rows: [["", ""]] };
|
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: "" };
|
case "LINK": return { type: "LINK", label: "", href: "" };
|
||||||
default: return { type: "PARAGRAPH", text: "" };
|
default: return { type: "PARAGRAPH", text: "" };
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -44,7 +44,7 @@ export type BlockContent =
|
|||||||
| { type: "QUOTE"; text: string; author?: string }
|
| { type: "QUOTE"; text: string; author?: string }
|
||||||
| { type: "DIVIDER" }
|
| { type: "DIVIDER" }
|
||||||
| { type: "CALLOUT"; title: string; text: string }
|
| { 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: "LIST"; style: "BULLET" | "ORDERED"; items: string[] }
|
||||||
| { type: "TABLE"; headers: string[]; rows: string[][] }
|
| { type: "TABLE"; headers: string[]; rows: string[][] }
|
||||||
| { type: "LINK"; label: string; href: string }
|
| { type: "LINK"; label: string; href: string }
|
||||||
|
|||||||
@@ -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("QUOTE"), text: z.string(), author: z.string().optional() }),
|
||||||
z.object({ type: z.literal("DIVIDER") }),
|
z.object({ type: z.literal("DIVIDER") }),
|
||||||
z.object({ type: z.literal("CALLOUT"), title: z.string(), text: z.string() }),
|
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("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("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() }),
|
z.object({ type: z.literal("LINK"), label: z.string(), href: z.string() }),
|
||||||
|
|||||||
Reference in New Issue
Block a user