prod
This commit is contained in:
+110
-58
@@ -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, GripVerticalIcon, ChevronDownIcon, 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 } 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";
|
||||
@@ -15,6 +15,9 @@ import { Input } from "./ui/input";
|
||||
import { Separator } from "./ui/separator";
|
||||
import { Alert } from "./ui/alert";
|
||||
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
|
||||
import { uploadPageImage, getImagePreviewUrl } from "@/lib/pages/actions";
|
||||
import { ImageUploader } from "@/components/image-uploader";
|
||||
import { Spinner } from "@/components/ui/spinner";
|
||||
|
||||
export type EditorData = {
|
||||
title: string;
|
||||
@@ -67,8 +70,8 @@ export default function Editor({ page, initialBlocks = [], onChange, errors }: E
|
||||
const [tags, setTags] = useState<string[]>(meta.tags ?? []);
|
||||
const [tagInput, setTagInput] = useState("");
|
||||
const [blocks, setBlocks] = useState<Block[]>(initialBlocks);
|
||||
const [dragIndex, setDragIndex] = useState<number | null>(null);
|
||||
const [dropIndex, setDropIndex] = useState<number | null>(null);
|
||||
const [previewUrls, setPreviewUrls] = useState<Map<string, string>>(new Map());
|
||||
const previewCacheRef = useRef<Map<string, string>>(new Map());
|
||||
|
||||
useEffect(() => {
|
||||
onChange({
|
||||
@@ -81,6 +84,37 @@ export default function Editor({ page, initialBlocks = [], onChange, errors }: E
|
||||
});
|
||||
}, [title, slug, location, tags, blocks]);
|
||||
|
||||
useEffect(() => {
|
||||
const imageBlocks = blocks.filter(
|
||||
(b) => b.type === "IMAGE" && !!(b.content as { type: string; url?: string }).url
|
||||
);
|
||||
|
||||
const unresolved = imageBlocks.filter((b) => !previewCacheRef.current.has(b.id));
|
||||
if (unresolved.length === 0) return;
|
||||
|
||||
let cancelled = false;
|
||||
Promise.all(
|
||||
unresolved.map(async (b) => {
|
||||
try {
|
||||
const url = await getImagePreviewUrl((b.content as { type: string; url: string }).url);
|
||||
return { id: b.id, url };
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
})
|
||||
).then((results) => {
|
||||
if (cancelled) return;
|
||||
const newEntries = results.filter((r): r is { id: string; url: string } => r !== null);
|
||||
if (newEntries.length === 0) return;
|
||||
const next = new Map(previewCacheRef.current);
|
||||
for (const { id, url } of newEntries) next.set(id, url);
|
||||
previewCacheRef.current = next;
|
||||
setPreviewUrls(new Map(next));
|
||||
});
|
||||
|
||||
return () => { cancelled = true; };
|
||||
}, [blocks]);
|
||||
|
||||
function addTag() {
|
||||
const trimmed = tagInput.trim();
|
||||
if (!trimmed || tags.includes(trimmed)) return;
|
||||
@@ -108,6 +142,12 @@ export default function Editor({ page, initialBlocks = [], onChange, errors }: E
|
||||
|
||||
function removeBlock(id: string) {
|
||||
setBlocks(blocks.filter((b) => b.id !== id));
|
||||
previewCacheRef.current.delete(id);
|
||||
setPreviewUrls((prev) => {
|
||||
const next = new Map(prev);
|
||||
next.delete(id);
|
||||
return next;
|
||||
});
|
||||
}
|
||||
|
||||
function updateBlock(id: string, content: Block["content"]) {
|
||||
@@ -121,19 +161,14 @@ export default function Editor({ page, initialBlocks = [], onChange, errors }: E
|
||||
setBlocks(updated.map((b, i) => ({ ...b, position: i })));
|
||||
}
|
||||
|
||||
function handleDragStart(index: number) {
|
||||
setDragIndex(index);
|
||||
function moveBlockUp(index: number) {
|
||||
if (index === 0) return;
|
||||
moveBlock(index, index - 1);
|
||||
}
|
||||
|
||||
function handleDragOver(e: React.DragEvent, index: number) {
|
||||
e.preventDefault();
|
||||
setDropIndex(index);
|
||||
}
|
||||
|
||||
function handleDrop(index: number) {
|
||||
if (dragIndex !== null && dragIndex !== index) moveBlock(dragIndex, index);
|
||||
setDragIndex(null);
|
||||
setDropIndex(null);
|
||||
function moveBlockDown(index: number) {
|
||||
if (index === blocks.length - 1) return;
|
||||
moveBlock(index, index + 1);
|
||||
}
|
||||
|
||||
return (
|
||||
@@ -226,27 +261,26 @@ export default function Editor({ page, initialBlocks = [], onChange, errors }: E
|
||||
)}
|
||||
<div className="flex flex-col gap-4 md:gap-6">
|
||||
{blocks.map((block, index) => (
|
||||
<div
|
||||
key={block.id}
|
||||
draggable
|
||||
onDragStart={() => handleDragStart(index)}
|
||||
onDragOver={(e) => handleDragOver(e, index)}
|
||||
onDrop={() => handleDrop(index)}
|
||||
onDragEnd={() => { setDragIndex(null); setDropIndex(null); }}
|
||||
className={cn(
|
||||
"group relative flex flex-row gap-2 md:gap-4 items-start py-1 transition-opacity",
|
||||
dragIndex === index && "opacity-30",
|
||||
dropIndex === index && dragIndex !== index && "border-t-2 border-primary"
|
||||
)}
|
||||
>
|
||||
<div key={block.id} className="group relative flex flex-row gap-2 md:gap-4 items-start py-1">
|
||||
<div className="flex-1 min-w-0">
|
||||
<BlockInput block={block} onChange={(content) => updateBlock(block.id, content)} />
|
||||
<BlockInput
|
||||
block={block}
|
||||
onChange={(content) => updateBlock(block.id, content)}
|
||||
previewUrls={previewUrls}
|
||||
onPreviewReady={(id, url) => {
|
||||
previewCacheRef.current.set(id, url);
|
||||
setPreviewUrls((prev) => new Map(prev).set(id, url));
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
<div className="flex flex-col gap-2 items-center opacity-0 group-hover:opacity-100 transition-opacity shrink-0">
|
||||
<Button variant="ghost" className="cursor-grab active:cursor-grabbing" size="icon-sm" >
|
||||
<GripVerticalIcon />
|
||||
<div className="flex flex-col gap-1 items-center opacity-0 group-hover:opacity-100 transition-opacity shrink-0">
|
||||
<Button variant="ghost" onClick={() => moveBlockUp(index)} disabled={index === 0} size="icon-sm">
|
||||
<ChevronUpIcon />
|
||||
</Button>
|
||||
<Button variant="ghost" onClick={() => removeBlock(block.id)} size="icon-sm" >
|
||||
<Button variant="ghost" onClick={() => moveBlockDown(index)} disabled={index === blocks.length - 1} size="icon-sm">
|
||||
<ChevronDownIcon />
|
||||
</Button>
|
||||
<Button variant="ghost" onClick={() => removeBlock(block.id)} size="icon-sm">
|
||||
<Trash2Icon />
|
||||
</Button>
|
||||
</div>
|
||||
@@ -278,7 +312,7 @@ export default function Editor({ page, initialBlocks = [], onChange, errors }: E
|
||||
);
|
||||
}
|
||||
|
||||
function BlockInput({ block, onChange }: { block: Block; onChange: (c: Block["content"]) => void }) {
|
||||
function BlockInput({ block, onChange, previewUrls, onPreviewReady }: { block: Block; onChange: (c: Block["content"]) => void; previewUrls?: Map<string, string>; onPreviewReady?: (id: string, url: string) => void }) {
|
||||
switch (block.type) {
|
||||
case "PARAGRAPH":
|
||||
return (
|
||||
@@ -363,31 +397,12 @@ function BlockInput({ block, onChange }: { block: Block; onChange: (c: Block["co
|
||||
|
||||
case "IMAGE":
|
||||
return (
|
||||
<div className="flex flex-col gap-4">
|
||||
<input
|
||||
className="w-full bg-transparent outline-none text-muted-foreground placeholder:text-muted-foreground py-1.5 border-b border-dashed border-muted-foreground/30 focus:border-muted-foreground/60 transition-colors"
|
||||
placeholder="https://example.com/image.jpg"
|
||||
value={(block.content as any).url ?? ""}
|
||||
onChange={(e) => onChange({ type: "IMAGE", url: e.target.value })}
|
||||
/>
|
||||
{(block.content as any).url && (
|
||||
<div className="relative max-w-150 rounded-md overflow-hidden border bg-muted/30">
|
||||
{/* eslint-disable-next-line @next/next/no-img-element */}
|
||||
<img
|
||||
src={(block.content as any).url}
|
||||
alt="preview"
|
||||
className="block max-w-full h-auto object-contain"
|
||||
onError={(e) => { (e.target as HTMLImageElement).style.display = "none"; }}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
<Input
|
||||
className="max-w-64"
|
||||
placeholder="Caption (optional)"
|
||||
value={(block.content as any).caption ?? ""}
|
||||
onChange={(e) => onChange({ type: "IMAGE", url: (block.content as any).url ?? "", caption: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
<ImageBlock
|
||||
block={block}
|
||||
previewUrl={previewUrls?.get(block.id)}
|
||||
onChange={onChange}
|
||||
onPreviewReady={onPreviewReady!}
|
||||
/>
|
||||
);
|
||||
|
||||
case "DIVIDER":
|
||||
@@ -608,6 +623,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 };
|
||||
const [isUploading, setIsUploading] = useState(false);
|
||||
|
||||
async function handleFileSelect(file: File) {
|
||||
setIsUploading(true);
|
||||
try {
|
||||
const key = await uploadPageImage(file);
|
||||
const url = await getImagePreviewUrl(key);
|
||||
onPreviewReady(block.id, url);
|
||||
onChange({ type: "IMAGE", url: key, caption: content.caption ?? "" });
|
||||
} catch (e) {
|
||||
console.error("Failed to upload image:", e);
|
||||
} finally {
|
||||
setIsUploading(false);
|
||||
}
|
||||
}
|
||||
|
||||
function handleRemove() {
|
||||
onChange({ type: "IMAGE", url: "", caption: content.caption ?? "" });
|
||||
}
|
||||
|
||||
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 })}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
function AutoResizeTextarea({ value, onChange, className, placeholder }: { value: string; onChange: (e: React.ChangeEvent<HTMLTextAreaElement>) => void; className?: string; placeholder?: string }) {
|
||||
const ref = useRef<HTMLTextAreaElement>(null);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user