feat: image resizer
This commit is contained in:
+54
-11
@@ -7,7 +7,7 @@ import { Button } from "@/components/ui/button";
|
||||
import { Field, FieldError, FieldGroup, FieldLabel, FieldSet } from "@/components/ui/field";
|
||||
import { InputGroup, InputGroupAddon, InputGroupButton, InputGroupInput } from "@/components/ui/input-group";
|
||||
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 { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu";
|
||||
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)}
|
||||
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 />
|
||||
</SelectTrigger>
|
||||
<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 }) {
|
||||
const content = block.content as { type: "IMAGE"; url: string; caption?: string; width?: number };
|
||||
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) {
|
||||
setIsUploading(true);
|
||||
@@ -653,24 +689,31 @@ function ImageBlock({ block, previewUrl, onChange, onPreviewReady }: { block: Bl
|
||||
label=""
|
||||
variant="editor"
|
||||
value={previewUrl}
|
||||
previewStyle={currentWidth < 100 ? { width: `${currentWidth}%` } : undefined}
|
||||
imgRef={imgRef}
|
||||
previewStyle={currentWidth ? { width: `min(${currentWidth}px, 100%)` } : undefined}
|
||||
onFileSelect={handleFileSelect}
|
||||
onRemove={handleRemove}
|
||||
isUploading={isUploading}
|
||||
caption={content.caption}
|
||||
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 items-center justify-between gap-2">
|
||||
<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>
|
||||
<Slider
|
||||
value={[currentWidth]}
|
||||
min={10}
|
||||
max={100}
|
||||
step={1}
|
||||
value={[currentWidth ?? 800]}
|
||||
min={150}
|
||||
max={800}
|
||||
step={10}
|
||||
onValueChange={([v]) => onChange({ type: "IMAGE", url: content.url, caption: content.caption ?? "", width: v })}
|
||||
/>
|
||||
</div>
|
||||
@@ -701,7 +744,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: "", width: 100 };
|
||||
case "IMAGE": return { type: "IMAGE", url: "", caption: "", width: undefined };
|
||||
case "LINK": return { type: "LINK", label: "", href: "" };
|
||||
default: return { type: "PARAGRAPH", text: "" };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user