feat: image resizer
This commit is contained in:
@@ -141,10 +141,10 @@ function BlockRenderer({ block }: { block: Block }) {
|
|||||||
<img
|
<img
|
||||||
src={b.url}
|
src={b.url}
|
||||||
alt={b.caption || "Image"}
|
alt={b.caption || "Image"}
|
||||||
style={imageWidth && imageWidth < 100 ? { width: `${imageWidth}%` } : undefined}
|
style={imageWidth ? { width: `min(${imageWidth}px, 100%)` } : undefined}
|
||||||
className={cn(
|
className={cn(
|
||||||
"h-auto rounded-md border object-contain",
|
"h-auto rounded-md border object-contain",
|
||||||
imageWidth && imageWidth < 100 ? "" : "w-full md:w-auto md:max-w-200"
|
imageWidth ? "" : "w-full md:w-auto md:max-w-200"
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ export default function CreatePageClient() {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-col p-4 md:p-16 gap-6 md:gap-12 mb-4 md:mb-0">
|
<div className="flex flex-col p-4 md:p-16 gap-6 md:gap-12 mb-4 md:mb-0">
|
||||||
<div className="flex flex-row items-center justify-between">
|
<div className="flex flex-col gap-4 md:flex-row md:items-center justify-between">
|
||||||
<h1>Create page</h1>
|
<h1>Create page</h1>
|
||||||
<div className="flex flex-row gap-3">
|
<div className="flex flex-row gap-3">
|
||||||
<Button
|
<Button
|
||||||
|
|||||||
+5
-3
@@ -1,7 +1,7 @@
|
|||||||
import { getPages } from "@/lib/pages/repository";
|
import { getPages } from "@/lib/pages/repository";
|
||||||
import { getCurrentUser } from "@/lib/user";
|
import { getCurrentUser } from "@/lib/user";
|
||||||
import { formatDate } from "@/lib/utils";
|
import { formatDate } from "@/lib/utils";
|
||||||
import { ArchiveIcon, CalendarIcon } from "lucide-react";
|
import { ArchiveIcon, CalendarIcon, PencilLineIcon } from "lucide-react";
|
||||||
import { Metadata } from "next";
|
import { Metadata } from "next";
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
@@ -14,8 +14,9 @@ export default async function List() {
|
|||||||
|
|
||||||
const pagesPublished = await getPages("PAGE", "PUBLISHED");
|
const pagesPublished = await getPages("PAGE", "PUBLISHED");
|
||||||
const pagesArchived = await getPages("PAGE", "ARCHIVED");
|
const pagesArchived = await getPages("PAGE", "ARCHIVED");
|
||||||
|
const pagesDraft = await getPages("PAGE", "DRAFT");
|
||||||
|
|
||||||
const pages = [...pagesPublished, ...pagesArchived].sort((a, b) => {
|
const pages = [...pagesPublished, ...pagesArchived, ...pagesDraft].sort((a, b) => {
|
||||||
const aDate = new Date(a.updatedAt ?? a.publishedAt).getTime();
|
const aDate = new Date(a.updatedAt ?? a.publishedAt).getTime();
|
||||||
const bDate = new Date(b.updatedAt ?? b.publishedAt).getTime();
|
const bDate = new Date(b.updatedAt ?? b.publishedAt).getTime();
|
||||||
|
|
||||||
@@ -33,8 +34,9 @@ export default async function List() {
|
|||||||
<h3 className="truncate">{p.title}</h3>
|
<h3 className="truncate">{p.title}</h3>
|
||||||
</Link>
|
</Link>
|
||||||
<div className="flex flex-row gap-4">
|
<div className="flex flex-row gap-4">
|
||||||
<h5 className="flex gap-1 items-center"><CalendarIcon size={14} />{formatDate(p.publishedAt)}</h5>
|
{p.publishedAt && <h5 className="flex gap-1 items-center"><CalendarIcon size={14} />{formatDate(p.publishedAt)}</h5>}
|
||||||
{p.status === "ARCHIVED" && <h5 className="flex gap-1 items-center"><ArchiveIcon size={14} />Archived</h5>}
|
{p.status === "ARCHIVED" && <h5 className="flex gap-1 items-center"><ArchiveIcon size={14} />Archived</h5>}
|
||||||
|
{p.status === "DRAFT" && <h5 className="flex gap-1 items-center"><PencilLineIcon size={14} />Draft</h5>}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)) : <h2 className="text-muted-foreground">There are no pages yet...</h2>
|
)) : <h2 className="text-muted-foreground">There are no pages yet...</h2>
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ function DraftItem({ draft, onDeleted }: { draft: Draft; onDeleted: (id: string)
|
|||||||
{draft.title}
|
{draft.title}
|
||||||
</h3>
|
</h3>
|
||||||
<div className="flex flex-row items-center gap-2">
|
<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>
|
<Button variant="secondary" size="icon-sm"><PencilIcon /></Button>
|
||||||
</Link>
|
</Link>
|
||||||
<Button disabled={isPending} variant="secondary" size="icon-sm" onClick={() => run(draft.id, draft.type)}>
|
<Button disabled={isPending} variant="secondary" size="icon-sm" onClick={() => run(draft.id, draft.type)}>
|
||||||
|
|||||||
+54
-11
@@ -7,7 +7,7 @@ import { Button } from "@/components/ui/button";
|
|||||||
import { Field, FieldError, FieldGroup, FieldLabel, FieldSet } from "@/components/ui/field";
|
import { Field, FieldError, FieldGroup, FieldLabel, FieldSet } from "@/components/ui/field";
|
||||||
import { InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput } from "@/components/ui/input-group";
|
import { InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput } from "@/components/ui/input-group";
|
||||||
import { Block, BlockType, Page, PageMeta, PageType } from "@/lib/pages/types";
|
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 { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue } from "./ui/select";
|
||||||
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu";
|
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu";
|
||||||
import { cn } from "@/lib/utils";
|
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)}
|
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 })}
|
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 />
|
<SelectValue />
|
||||||
</SelectTrigger>
|
</SelectTrigger>
|
||||||
<SelectContent>
|
<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 }) {
|
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 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;
|
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) {
|
async function handleFileSelect(file: File) {
|
||||||
setIsUploading(true);
|
setIsUploading(true);
|
||||||
@@ -653,24 +689,31 @@ function ImageBlock({ block, previewUrl, onChange, onPreviewReady }: { block: Bl
|
|||||||
label=""
|
label=""
|
||||||
variant="editor"
|
variant="editor"
|
||||||
value={previewUrl}
|
value={previewUrl}
|
||||||
previewStyle={currentWidth < 100 ? { width: `${currentWidth}%` } : undefined}
|
imgRef={imgRef}
|
||||||
|
previewStyle={currentWidth ? { width: `min(${currentWidth}px, 100%)` } : undefined}
|
||||||
onFileSelect={handleFileSelect}
|
onFileSelect={handleFileSelect}
|
||||||
onRemove={handleRemove}
|
onRemove={handleRemove}
|
||||||
isUploading={isUploading}
|
isUploading={isUploading}
|
||||||
caption={content.caption}
|
caption={content.caption}
|
||||||
onCaptionChange={(caption) => onChange({ type: "IMAGE", url: content.url, caption, width: content.width })}
|
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 flex-col gap-2">
|
||||||
<div className="flex items-center justify-between gap-2">
|
<div className="flex items-center justify-between gap-2">
|
||||||
<Label className="text-xs md:text-sm">Image size</Label>
|
<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>
|
</div>
|
||||||
<Slider
|
<Slider
|
||||||
value={[currentWidth]}
|
value={[currentWidth ?? 800]}
|
||||||
min={10}
|
min={150}
|
||||||
max={100}
|
max={800}
|
||||||
step={1}
|
step={10}
|
||||||
onValueChange={([v]) => onChange({ type: "IMAGE", url: content.url, caption: content.caption ?? "", width: v })}
|
onValueChange={([v]) => onChange({ type: "IMAGE", url: content.url, caption: content.caption ?? "", width: v })}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
@@ -701,7 +744,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: "", width: 100 };
|
case "IMAGE": return { type: "IMAGE", url: "", caption: "", width: undefined };
|
||||||
case "LINK": return { type: "LINK", label: "", href: "" };
|
case "LINK": return { type: "LINK", label: "", href: "" };
|
||||||
default: return { type: "PARAGRAPH", text: "" };
|
default: return { type: "PARAGRAPH", text: "" };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,8 @@ type Props = {
|
|||||||
children?: React.ReactNode;
|
children?: React.ReactNode;
|
||||||
caption?: string;
|
caption?: string;
|
||||||
onCaptionChange?: (caption: string) => void;
|
onCaptionChange?: (caption: string) => void;
|
||||||
|
imgRef?: React.Ref<HTMLImageElement>;
|
||||||
|
resizeHandle?: React.ReactNode;
|
||||||
};
|
};
|
||||||
|
|
||||||
const MAX_SIZE = 5 * 1024 * 1024;
|
const MAX_SIZE = 5 * 1024 * 1024;
|
||||||
@@ -56,7 +58,7 @@ function fileToFileList(file: File): FileList {
|
|||||||
return dt.files;
|
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 inputRef = useRef<HTMLInputElement>(null);
|
||||||
const [previewUrl, setPreviewUrl] = useState<string | null>(null);
|
const [previewUrl, setPreviewUrl] = useState<string | null>(null);
|
||||||
const [fileInfo, setFileInfo] = useState<{ name: string; size: number } | 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 ? (
|
{displayUrl ? (
|
||||||
<div className={EDITOR_WRAPPER_CLASS}>
|
<div className={EDITOR_WRAPPER_CLASS}>
|
||||||
<div className="group relative rounded-md">
|
<div
|
||||||
<img src={displayUrl} alt="" className={previewClassName ?? EDITOR_IMG_CLASS} style={previewStyle} />
|
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}>
|
<Button type="button" variant="secondary" size="sm" onClick={() => inputRef.current?.click()} disabled={isUploading}>
|
||||||
<RefreshCwIcon />
|
<RefreshCwIcon />
|
||||||
Replace
|
Replace
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{resizeHandle}
|
||||||
|
|
||||||
{isUploading && (
|
{isUploading && (
|
||||||
<div className="absolute inset-0 flex items-center justify-center bg-background/90 rounded-md">
|
<div className="absolute inset-0 flex items-center justify-center bg-background/90 rounded-md">
|
||||||
<Spinner />
|
<Spinner />
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ export function NewsCard({ news }: { news: PagePublic }) {
|
|||||||
return (
|
return (
|
||||||
<div className="flex flex-col gap-1">
|
<div className="flex flex-col gap-1">
|
||||||
<Link href={`/news/${news.slug}`} className="w-max">
|
<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>
|
</Link>
|
||||||
<div className="flex flex-col md:flex-row gap-2 md:gap-8 md:items-center">
|
<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">
|
<div className="flex flex-row gap-4 md:gap-8">
|
||||||
|
|||||||
Reference in New Issue
Block a user