diff --git a/app/[type]/[slug]/page.tsx b/app/[type]/[slug]/page.tsx index 179cf0e..5c289e6 100644 --- a/app/[type]/[slug]/page.tsx +++ b/app/[type]/[slug]/page.tsx @@ -135,6 +135,7 @@ function BlockRenderer({ block }: { block: Block }) { ); case "IMAGE": + const imageWidth = (b as { width?: number }).width; return (
{b.caption {b.caption && ( diff --git a/app/create/client.tsx b/app/create/client.tsx index 142dbe9..6da12b7 100644 --- a/app/create/client.tsx +++ b/app/create/client.tsx @@ -17,7 +17,7 @@ export default function CreatePageClient() { const dataRef = useRef({ title: "", - type: "NEWS", + type: "PAGE", slug: "", location: "", tags: [], diff --git a/components/editor.tsx b/components/editor.tsx index f9a77fc..b1371eb 100644 --- a/components/editor.tsx +++ b/components/editor.tsx @@ -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 ( - onChange({ type: "IMAGE", url: content.url, caption })} - /> +
+ onChange({ type: "IMAGE", url: content.url, caption, width: content.width })} + /> + {content.url && ( +
+
+ handleWidthChange(Number(e.target.value))} + className="flex-1 h-1.5 bg-muted rounded-full appearance-none cursor-pointer accent-primary" + /> + {currentWidth}% +
+
+ {WIDTH_PRESETS.map((preset) => ( + + ))} +
+
+ )} +
); } @@ -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: "" }; } diff --git a/lib/pages/types.ts b/lib/pages/types.ts index 9e29d58..7a258ee 100644 --- a/lib/pages/types.ts +++ b/lib/pages/types.ts @@ -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 } diff --git a/lib/pages/validation.ts b/lib/pages/validation.ts index 07fe084..3dc38ad 100644 --- a/lib/pages/validation.ts +++ b/lib/pages/validation.ts @@ -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() }),