feat: pages
This commit is contained in:
+464
-210
@@ -1,23 +1,19 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect } from "react";
|
||||
import { useState, useEffect, useRef } from "react";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
Field,
|
||||
FieldGroup,
|
||||
FieldLabel,
|
||||
FieldSet,
|
||||
} from "@/components/ui/field";
|
||||
import {
|
||||
InputGroup,
|
||||
InputGroupAddon,
|
||||
InputGroupButton,
|
||||
InputGroupInput,
|
||||
} from "@/components/ui/input-group";
|
||||
import { Field, 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 { Globe2, Plus, Trash2, X } from "lucide-react";
|
||||
import { Globe2Icon, PlusIcon, Trash2Icon, XIcon, GripVerticalIcon, ChevronDownIcon, 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";
|
||||
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";
|
||||
|
||||
export type EditorData = {
|
||||
title: string;
|
||||
@@ -35,20 +31,42 @@ type EditorProps = {
|
||||
onChange: (data: EditorData) => void;
|
||||
};
|
||||
|
||||
export default function Editor({
|
||||
page,
|
||||
initialBlocks = [],
|
||||
onChange,
|
||||
}: EditorProps) {
|
||||
const BLOCK_ICONS: Record<BlockType, React.ReactNode> = {
|
||||
PARAGRAPH: <AlignLeftIcon size={14} />,
|
||||
HEADING: <TypeIcon size={14} />,
|
||||
IMAGE: <ImageIcon size={14} />,
|
||||
QUOTE: <QuoteIcon size={14} />,
|
||||
CALLOUT: <AlertCircleIcon size={14} />,
|
||||
DIVIDER: <MinusIcon size={14} />,
|
||||
LIST: <ListIcon size={14} />,
|
||||
TABLE: <TableIcon size={14} />,
|
||||
LINK: <LinkIcon size={14} />
|
||||
};
|
||||
|
||||
const BLOCK_LABELS: Record<BlockType, string> = {
|
||||
PARAGRAPH: "Text",
|
||||
HEADING: "Heading",
|
||||
IMAGE: "Image",
|
||||
QUOTE: "Quote",
|
||||
CALLOUT: "Callout",
|
||||
DIVIDER: "Divider",
|
||||
LIST: "List",
|
||||
TABLE: "Table",
|
||||
LINK: "Link"
|
||||
};
|
||||
|
||||
export default function Editor({ page, initialBlocks = [], onChange }: EditorProps) {
|
||||
const meta = (page?.meta as PageMeta) ?? {};
|
||||
|
||||
const [title, setTitle] = useState(page?.title ?? "");
|
||||
const [type, setType] = useState(page?.type ?? "NEWS");
|
||||
const [type, setType] = useState<PageType>(page?.type ?? "NEWS");
|
||||
const [slug, setSlug] = useState(page?.slug ?? "");
|
||||
const [location, setLocation] = useState(meta.location ?? "");
|
||||
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);
|
||||
|
||||
useEffect(() => {
|
||||
onChange({
|
||||
@@ -72,16 +90,16 @@ export default function Editor({
|
||||
setTags(tags.filter((t) => t !== tag));
|
||||
}
|
||||
|
||||
function addBlock(type: BlockType) {
|
||||
function addBlock(blockType: BlockType) {
|
||||
setBlocks([
|
||||
...blocks,
|
||||
{
|
||||
id: crypto.randomUUID(),
|
||||
pageId: page?.id ?? "",
|
||||
parentId: null,
|
||||
type,
|
||||
type: blockType,
|
||||
position: blocks.length,
|
||||
content: getDefaultContent(type),
|
||||
content: getDefaultContent(blockType),
|
||||
},
|
||||
]);
|
||||
}
|
||||
@@ -94,10 +112,32 @@ export default function Editor({
|
||||
setBlocks(blocks.map((b) => (b.id === id ? { ...b, content } : b)));
|
||||
}
|
||||
|
||||
function moveBlock(from: number, to: number) {
|
||||
const updated = [...blocks];
|
||||
const [moved] = updated.splice(from, 1);
|
||||
updated.splice(to, 0, moved);
|
||||
setBlocks(updated.map((b, i) => ({ ...b, position: i })));
|
||||
}
|
||||
|
||||
function handleDragStart(index: number) {
|
||||
setDragIndex(index);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-10">
|
||||
<FieldSet>
|
||||
<h2 className="underline underline-offset-8 decoration-2 mb-4">Meta</h2>
|
||||
<h2 className="underline underline-offset-8 decoration-2 mb-6">Meta</h2>
|
||||
<FieldGroup>
|
||||
<Field>
|
||||
<FieldLabel htmlFor="title">Title</FieldLabel>
|
||||
@@ -105,21 +145,17 @@ export default function Editor({
|
||||
<InputGroupInput
|
||||
id="title"
|
||||
value={title}
|
||||
placeholder="Enter page title"
|
||||
onChange={(e) => {
|
||||
setTitle(e.target.value);
|
||||
if (!page) {
|
||||
setSlug(
|
||||
e.target.value.toLowerCase().replace(/\s+/g, "-")
|
||||
);
|
||||
}
|
||||
if (!page) setSlug(e.target.value.toLowerCase().replace(/\s+/g, "-"));
|
||||
}}
|
||||
placeholder="Enter page title"
|
||||
/>
|
||||
</InputGroup>
|
||||
</Field>
|
||||
<Field>
|
||||
<FieldLabel htmlFor="type">Page type</FieldLabel>
|
||||
<Select defaultValue={type} onValueChange={(e) => setType(e)} disabled={true}>
|
||||
<Select defaultValue={type} onValueChange={(e) => setType(e as PageType)}>
|
||||
<SelectTrigger>
|
||||
<SelectValue placeholder="Type" />
|
||||
</SelectTrigger>
|
||||
@@ -134,122 +170,105 @@ export default function Editor({
|
||||
<Field>
|
||||
<FieldLabel htmlFor="slug">Slug</FieldLabel>
|
||||
<InputGroup>
|
||||
<InputGroupInput
|
||||
id="slug"
|
||||
value={slug}
|
||||
onChange={(e) => setSlug(e.target.value)}
|
||||
placeholder="page-slug"
|
||||
disabled={true}
|
||||
/>
|
||||
<InputGroupInput id="slug" value={slug} placeholder="page-slug" onChange={(e) => setSlug(e.target.value)} />
|
||||
</InputGroup>
|
||||
</Field>
|
||||
<Field>
|
||||
<FieldLabel htmlFor="location">Location</FieldLabel>
|
||||
<InputGroup>
|
||||
<InputGroupAddon align="inline-start">
|
||||
<Globe2 size={14} className="text-muted-foreground" />
|
||||
</InputGroupAddon>
|
||||
<InputGroupInput
|
||||
id="location"
|
||||
value={location}
|
||||
onChange={(e) => setLocation(e.target.value)}
|
||||
placeholder="Liberty City, European Federation"
|
||||
/>
|
||||
</InputGroup>
|
||||
</Field>
|
||||
<Field>
|
||||
<FieldLabel htmlFor="tags">Tags</FieldLabel>
|
||||
<InputGroup>
|
||||
<InputGroupInput
|
||||
id="tags"
|
||||
value={tagInput}
|
||||
onChange={(e) => setTagInput(e.target.value)}
|
||||
onKeyDown={(e) => e.key === "Enter" && addTag()}
|
||||
placeholder="Add a tag and press Enter"
|
||||
/>
|
||||
<InputGroupAddon align="inline-end">
|
||||
<InputGroupButton onClick={addTag}>Add</InputGroupButton>
|
||||
</InputGroupAddon>
|
||||
</InputGroup>
|
||||
{tags.length > 0 && (
|
||||
<div className="flex flex-row gap-2 flex-wrap mt-3">
|
||||
{tags.map((tag) => (
|
||||
<Badge
|
||||
key={tag}
|
||||
variant="secondary"
|
||||
className="flex gap-1 items-center cursor-pointer"
|
||||
onClick={() => removeTag(tag)}
|
||||
>
|
||||
{tag}
|
||||
<X size={11} />
|
||||
</Badge>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</Field>
|
||||
{type === "NEWS" && (
|
||||
<Field>
|
||||
<FieldLabel htmlFor="location">Location</FieldLabel>
|
||||
<InputGroup>
|
||||
<InputGroupAddon align="inline-start">
|
||||
<Globe2Icon size={14} className="text-muted-foreground" />
|
||||
</InputGroupAddon>
|
||||
<InputGroupInput id="location" value={location} placeholder="Liberty City, European Federation" onChange={(e) => setLocation(e.target.value)} />
|
||||
</InputGroup>
|
||||
</Field>
|
||||
)}
|
||||
{type === "NEWS" && (
|
||||
<Field>
|
||||
<FieldLabel htmlFor="tags">Tags</FieldLabel>
|
||||
<InputGroup>
|
||||
<InputGroupInput
|
||||
id="tags"
|
||||
value={tagInput}
|
||||
placeholder="Add a tag and press Enter"
|
||||
onChange={(e) => setTagInput(e.target.value)}
|
||||
onKeyDown={(e) => e.key === "Enter" && addTag()}
|
||||
/>
|
||||
<InputGroupAddon align="inline-end">
|
||||
<InputGroupButton onClick={addTag}>Add</InputGroupButton>
|
||||
</InputGroupAddon>
|
||||
</InputGroup>
|
||||
{tags.length > 0 && (
|
||||
<div className="flex flex-row gap-2 flex-wrap mt-3">
|
||||
{tags.map((tag) => (
|
||||
<Badge key={tag} variant="secondary" className="flex gap-1 items-center cursor-pointer" onClick={() => removeTag(tag)}>
|
||||
{tag}
|
||||
<XIcon size={11} />
|
||||
</Badge>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</Field>
|
||||
)}
|
||||
</FieldGroup>
|
||||
</FieldSet>
|
||||
|
||||
<FieldSet>
|
||||
<h2 className="underline underline-offset-8 decoration-2 mb-4">Content</h2>
|
||||
<div className="flex flex-col gap-3">
|
||||
{blocks.length === 0 && (
|
||||
<p className="text-sm text-muted-foreground">
|
||||
No blocks yet. Add one below.
|
||||
</p>
|
||||
)}
|
||||
{blocks.map((block) => (
|
||||
<BlockEditor
|
||||
<div className="flex flex-col gap-1">
|
||||
<h2 className="underline underline-offset-8 decoration-2 mb-6">Content</h2>
|
||||
{blocks.length === 0 && (
|
||||
<p className="text-sm text-muted-foreground">No blocks yet. Add one below.</p>
|
||||
)}
|
||||
<div className="flex flex-col gap-6">
|
||||
{blocks.map((block, index) => (
|
||||
<div
|
||||
key={block.id}
|
||||
block={block}
|
||||
onChange={(content) => updateBlock(block.id, content)}
|
||||
onRemove={() => removeBlock(block.id)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
<div className="flex flex-row gap-2 flex-wrap mt-6">
|
||||
{(
|
||||
[
|
||||
"PARAGRAPH",
|
||||
"HEADING",
|
||||
"IMAGE",
|
||||
"QUOTE",
|
||||
"CALLOUT",
|
||||
"DIVIDER",
|
||||
"LIST",
|
||||
"TABLE",
|
||||
] as BlockType[]
|
||||
).map((t) => (
|
||||
<Button
|
||||
key={t}
|
||||
variant="outline"
|
||||
size="sm"
|
||||
onClick={() => addBlock(t)}
|
||||
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-4 items-start py-1 transition-opacity",
|
||||
dragIndex === index && "opacity-30",
|
||||
dropIndex === index && dragIndex !== index && "border-t-2 border-primary"
|
||||
)}
|
||||
>
|
||||
<Plus size={12} />
|
||||
{t.charAt(0) + t.slice(1).toLowerCase()}
|
||||
</Button>
|
||||
<div className="flex-1 min-w-0">
|
||||
<BlockInput block={block} onChange={(content) => updateBlock(block.id, content)} />
|
||||
</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 />
|
||||
</Button>
|
||||
<Button variant="ghost" onClick={() => removeBlock(block.id)} size="icon-sm" >
|
||||
<Trash2Icon />
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</FieldSet>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function BlockEditor({ block, onChange, onRemove }: { block: Block; onChange: (content: Block["content"]) => void; onRemove: () => void }) {
|
||||
return (
|
||||
<div className="flex flex-row gap-3 items-start group">
|
||||
<div className="flex-1 rounded-md border bg-background p-4">
|
||||
<BlockInput block={block} onChange={onChange} />
|
||||
<div className="mt-4">
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button variant="outline" size="sm" className="gap-1.5 text-muted-foreground">
|
||||
<PlusIcon size={14} />
|
||||
<p>Add block</p>
|
||||
<ChevronDownIcon size={12} />
|
||||
</Button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="start" className="w-44">
|
||||
{(["PARAGRAPH", "HEADING", "IMAGE", "QUOTE", "CALLOUT", "DIVIDER", "LIST", "TABLE", "LINK"] as BlockType[]).map((t) => (
|
||||
<DropdownMenuItem key={t} onClick={() => addBlock(t)} className="gap-2">
|
||||
<p className="text-muted-foreground">{BLOCK_ICONS[t]}</p>
|
||||
<p>{BLOCK_LABELS[t]}</p>
|
||||
</DropdownMenuItem>
|
||||
))}
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
</div>
|
||||
</div>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="icon"
|
||||
onClick={onRemove}
|
||||
className="opacity-0 group-hover:opacity-100 transition-opacity mt-1 shrink-0"
|
||||
>
|
||||
<Trash2 size={14} />
|
||||
</Button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -258,111 +277,346 @@ function BlockInput({ block, onChange }: { block: Block; onChange: (c: Block["co
|
||||
switch (block.type) {
|
||||
case "PARAGRAPH":
|
||||
return (
|
||||
<textarea
|
||||
className="w-full bg-transparent resize-none outline-none text-sm leading-relaxed"
|
||||
rows={3}
|
||||
<AutoResizeTextarea
|
||||
className="w-full bg-transparent resize-none outline-none leading-relaxed placeholder:text-muted-foreground"
|
||||
placeholder="Start writing..."
|
||||
value={(block.content as any).text ?? ""}
|
||||
onChange={(e) =>
|
||||
onChange({ type: "PARAGRAPH", text: e.target.value })
|
||||
}
|
||||
onChange={(e) => onChange({ type: "PARAGRAPH", text: e.target.value })}
|
||||
/>
|
||||
);
|
||||
|
||||
case "HEADING":
|
||||
return (
|
||||
<input
|
||||
className="w-full bg-transparent outline-none text-lg tracking-tight"
|
||||
placeholder="Heading"
|
||||
value={(block.content as any).text ?? ""}
|
||||
onChange={(e) =>
|
||||
onChange({ type: "HEADING", text: e.target.value, level: 2 })
|
||||
}
|
||||
/>
|
||||
<div className="flex items-center gap-2">
|
||||
<input
|
||||
className="flex-1 bg-transparent outline-none tracking-tight placeholder:text-muted-foreground"
|
||||
placeholder="Heading"
|
||||
value={(block.content as any).text ?? ""}
|
||||
onChange={(e) => onChange({ type: "HEADING", text: e.target.value, level: (block.content as any).level ?? 2 })}
|
||||
/>
|
||||
<Select
|
||||
value={String((block.content as any).level ?? 2)}
|
||||
onValueChange={(v) => onChange({ type: "HEADING", text: (block.content as any).text ?? "", level: Number(v) as 1 | 2 | 3 })}
|
||||
>
|
||||
<SelectTrigger className="w-16 h-7 opacity-0 group-hover:opacity-100 transition-opacity">
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="1">H1</SelectItem>
|
||||
<SelectItem value="2">H2</SelectItem>
|
||||
<SelectItem value="3">H3</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
);
|
||||
|
||||
case "QUOTE":
|
||||
return (
|
||||
<div className="flex flex-col gap-2 border-l-2 pl-4">
|
||||
<textarea
|
||||
className="w-full bg-transparent resize-none outline-none text-sm italic leading-relaxed"
|
||||
rows={2}
|
||||
<div className="flex flex-col gap-2 border-l-2 border-muted-foreground pl-4">
|
||||
<AutoResizeTextarea
|
||||
className="w-full bg-transparent resize-none outline-none leading-relaxed placeholder:text-muted-foreground"
|
||||
placeholder="Quote text..."
|
||||
value={(block.content as any).text ?? ""}
|
||||
onChange={(e) =>
|
||||
onChange({
|
||||
type: "QUOTE",
|
||||
text: e.target.value,
|
||||
author: (block.content as any).author,
|
||||
})
|
||||
}
|
||||
onChange={(e) => onChange({ type: "QUOTE", text: e.target.value, author: (block.content as any).author })}
|
||||
/>
|
||||
<input
|
||||
className="w-full bg-transparent outline-none text-xs text-muted-foreground"
|
||||
placeholder="Author"
|
||||
className="w-full bg-transparent outline-none text-sm text-muted-foreground placeholder:text-muted-foreground/60"
|
||||
placeholder="— Author"
|
||||
value={(block.content as any).author ?? ""}
|
||||
onChange={(e) =>
|
||||
onChange({
|
||||
type: "QUOTE",
|
||||
text: (block.content as any).text,
|
||||
author: e.target.value,
|
||||
})
|
||||
}
|
||||
onChange={(e) => onChange({ type: "QUOTE", text: (block.content as any).text, author: e.target.value })}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
case "DIVIDER":
|
||||
return <hr className="border-border" />;
|
||||
|
||||
case "CALLOUT":
|
||||
return (
|
||||
<div className="flex flex-row gap-3 items-start rounded-md bg-muted p-3">
|
||||
<span className="text-base leading-none mt-0.5">⚠️</span>
|
||||
<textarea
|
||||
className="w-full bg-transparent resize-none outline-none text-sm leading-relaxed"
|
||||
rows={2}
|
||||
<Alert>
|
||||
<InfoIcon />
|
||||
<input
|
||||
className="w-full bg-transparent outline-none text-sm font-medium placeholder:text-muted-foreground"
|
||||
placeholder="Callout title"
|
||||
value={(block.content as any).title ?? ""}
|
||||
onChange={(e) => onChange({ type: "CALLOUT", title: e.target.value, text: (block.content as any).text ?? "" })}
|
||||
/>
|
||||
<AutoResizeTextarea
|
||||
className="w-full bg-transparent resize-none outline-none text-sm leading-relaxed text-muted-foreground placeholder:text-muted-foreground/60"
|
||||
placeholder="Callout text..."
|
||||
value={(block.content as any).text ?? ""}
|
||||
onChange={(e) =>
|
||||
onChange({ type: "CALLOUT", text: e.target.value })
|
||||
}
|
||||
onChange={(e) => onChange({ type: "CALLOUT", title: (block.content as any).title ?? "", text: e.target.value })}
|
||||
/>
|
||||
</Alert>
|
||||
);
|
||||
|
||||
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="w-max relative 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="max-h-64 w-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>
|
||||
);
|
||||
case "IMAGE":
|
||||
|
||||
case "DIVIDER":
|
||||
return <Separator />;
|
||||
|
||||
case "LIST":
|
||||
return <ListEditor content={block.content as any} onChange={(c) => onChange(c)} />;
|
||||
|
||||
case "TABLE":
|
||||
return <TableEditor content={block.content as any} onChange={(c) => onChange(c)} />;
|
||||
|
||||
case "LINK":
|
||||
return (
|
||||
<input
|
||||
className="w-full bg-transparent outline-none text-sm text-muted-foreground"
|
||||
placeholder="https://example.com/image.jpg"
|
||||
value={(block.content as any).url ?? ""}
|
||||
onChange={(e) => onChange({ type: "IMAGE", url: e.target.value })}
|
||||
/>
|
||||
<div className="flex flex-row gap-2.5">
|
||||
<LinkIcon size={16} />
|
||||
<div className="flex flex-col gap-2">
|
||||
<input
|
||||
className="w-full bg-transparent outline-none text-sm"
|
||||
placeholder="Link label"
|
||||
value={(block.content as any).label ?? ""}
|
||||
onChange={(e) =>
|
||||
onChange({
|
||||
type: "LINK",
|
||||
label: e.target.value,
|
||||
href: (block.content as any).href ?? "",
|
||||
})
|
||||
}
|
||||
/>
|
||||
|
||||
<input
|
||||
className="w-full bg-transparent outline-none text-sm text-muted-foreground"
|
||||
placeholder="URL"
|
||||
value={(block.content as any).href ?? ""}
|
||||
onChange={(e) =>
|
||||
onChange({
|
||||
type: "LINK",
|
||||
label: (block.content as any).label ?? "",
|
||||
href: e.target.value,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
default:
|
||||
return (
|
||||
<p className="text-muted-foreground text-sm">
|
||||
{block.type} — editor coming soon
|
||||
</p>
|
||||
);
|
||||
return <p className="text-muted-foreground text-sm py-1.5">{block.type} — editor coming soon</p>;
|
||||
}
|
||||
}
|
||||
|
||||
function ListEditor({ content, onChange }: { content: { type: "LIST"; style: "BULLET" | "ORDERED"; items: string[] }; onChange: (c: any) => void }) {
|
||||
const items = content.items ?? [""];
|
||||
|
||||
function updateItem(index: number, value: string) {
|
||||
const updated = [...items];
|
||||
updated[index] = value;
|
||||
onChange({ ...content, items: updated });
|
||||
}
|
||||
|
||||
function addItem() {
|
||||
onChange({ ...content, items: [...items, ""] });
|
||||
}
|
||||
|
||||
function removeItem(index: number) {
|
||||
const updated = items.filter((_, i) => i !== index);
|
||||
onChange({ ...content, items: updated.length ? updated : [""] });
|
||||
}
|
||||
|
||||
function handleKeyDown(e: React.KeyboardEvent, index: number) {
|
||||
if (e.key === "Enter") {
|
||||
e.preventDefault();
|
||||
const updated = [...items];
|
||||
updated.splice(index + 1, 0, "");
|
||||
onChange({ ...content, items: updated });
|
||||
setTimeout(() => {
|
||||
const inputs = document.querySelectorAll(`[data-list-item]`);
|
||||
(inputs[index + 1] as HTMLInputElement)?.focus();
|
||||
}, 0);
|
||||
}
|
||||
if (e.key === "Backspace" && items[index] === "" && items.length > 1) {
|
||||
e.preventDefault();
|
||||
removeItem(index);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-2">
|
||||
<div className="flex items-center gap-2 mb-2">
|
||||
<Select value={content.style} onValueChange={(v) => onChange({ ...content, style: v })}>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="BULLET"><ListIcon />Bullet</SelectItem>
|
||||
<SelectItem value="ORDERED"><ListOrderedIcon /> Ordered</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
<div className="flex flex-col gap-1">
|
||||
{items.map((item, index) => (
|
||||
<div key={index} className="flex items-center gap-2">
|
||||
<span className="text-muted-foreground w-4 shrink-0 text-right">
|
||||
{content.style === "ORDERED" ? `${index + 1}.` : <DotIcon />}
|
||||
</span>
|
||||
<input
|
||||
data-list-item
|
||||
className="w-64 bg-transparent outline-none py-0.5 placeholder:text-muted-foreground"
|
||||
placeholder="List item"
|
||||
value={item}
|
||||
onChange={(e) => updateItem(index, e.target.value)}
|
||||
onKeyDown={(e) => handleKeyDown(e, index)}
|
||||
/>
|
||||
{items.length > 1 && (
|
||||
<Button variant="ghost" onClick={() => removeItem(index)} size="icon-sm" >
|
||||
<XIcon />
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
<Button className="w-max mt-2 text-muted-foreground" variant="outline" onClick={addItem} size="sm">
|
||||
<PlusIcon />
|
||||
Add item
|
||||
</Button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function TableEditor({ content, onChange }: { content: { type: "TABLE"; headers: string[]; rows: string[][] }; onChange: (c: any) => void }) {
|
||||
const headers = content.headers ?? ["Column 1"];
|
||||
const rows = content.rows ?? [];
|
||||
|
||||
function updateHeader(index: number, value: string) {
|
||||
const updated = [...headers];
|
||||
updated[index] = value;
|
||||
onChange({ ...content, headers: updated });
|
||||
}
|
||||
|
||||
function addColumn() {
|
||||
onChange({ ...content, headers: headers.concat(""), rows: rows.map((row) => [...row, ""]) });
|
||||
}
|
||||
|
||||
function removeColumn(colIndex: number) {
|
||||
if (headers.length <= 1) return;
|
||||
onChange({
|
||||
...content,
|
||||
headers: headers.filter((_, i) => i !== colIndex),
|
||||
rows: rows.map((row) => row.filter((_, i) => i !== colIndex)),
|
||||
});
|
||||
}
|
||||
|
||||
function addRow() {
|
||||
onChange({ ...content, rows: [...rows, headers.map(() => "")] });
|
||||
}
|
||||
|
||||
function removeRow(rowIndex: number) {
|
||||
onChange({ ...content, rows: rows.filter((_, i) => i !== rowIndex) });
|
||||
}
|
||||
|
||||
function updateCell(rowIndex: number, colIndex: number, value: string) {
|
||||
onChange({
|
||||
...content,
|
||||
rows: rows.map((row, ri) => ri === rowIndex ? row.map((cell, ci) => (ci === colIndex ? value : cell)) : row),
|
||||
});
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-2 py-1 overflow-x-auto">
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
{headers.map((header, ci) => (
|
||||
<TableHead key={ci}>
|
||||
<div className="flex flex-row items-center">
|
||||
<input
|
||||
className="flex-1 bg-transparent outline-none placeholder:text-muted-foreground min-w-0"
|
||||
placeholder={`Column ${ci + 1}`}
|
||||
value={header}
|
||||
onChange={(e) => updateHeader(ci, e.target.value)}
|
||||
/>
|
||||
{headers.length > 1 && (
|
||||
<Button variant="ghost" onClick={() => removeColumn(ci)} size="icon-sm" >
|
||||
<XIcon />
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</TableHead>
|
||||
))}
|
||||
<TableHead>
|
||||
<Button variant="ghost" onClick={addColumn} size="icon-sm" >
|
||||
<PlusIcon />
|
||||
</Button>
|
||||
</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{rows.map((row, ri) => (
|
||||
<TableRow key={ri}>
|
||||
{row.map((cell, ci) => (
|
||||
<TableCell key={ci}>
|
||||
<input className="w-full bg-transparent outline-none placeholder:text-muted-foreground min-w-0" placeholder="Value" value={cell} onChange={(e) => updateCell(ri, ci, e.target.value)} />
|
||||
</TableCell>
|
||||
))}
|
||||
<TableCell>
|
||||
<Button variant="ghost" onClick={() => removeRow(ri)} size="icon-sm" >
|
||||
<XIcon />
|
||||
</Button>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
</Table>
|
||||
<Button className="w-max mt-2 text-muted-foreground" variant="outline" onClick={addRow} size="sm">
|
||||
<PlusIcon />
|
||||
Add row
|
||||
</Button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function AutoResizeTextarea({ value, onChange, className, placeholder }: { value: string; onChange: (e: React.ChangeEvent<HTMLTextAreaElement>) => void; className?: string; placeholder?: string }) {
|
||||
const ref = useRef<HTMLTextAreaElement>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (ref.current) {
|
||||
ref.current.style.height = "auto";
|
||||
ref.current.style.height = `${ref.current.scrollHeight}px`;
|
||||
}
|
||||
}, [value]);
|
||||
|
||||
return <textarea ref={ref} rows={1} className={cn("overflow-hidden", className)} placeholder={placeholder} value={value} onChange={onChange} />;
|
||||
}
|
||||
|
||||
function getDefaultContent(type: BlockType): Block["content"] {
|
||||
switch (type) {
|
||||
case "PARAGRAPH":
|
||||
return { type: "PARAGRAPH", text: "" };
|
||||
case "HEADING":
|
||||
return { type: "HEADING", text: "", level: 2 };
|
||||
case "QUOTE":
|
||||
return { type: "QUOTE", text: "" };
|
||||
case "CALLOUT":
|
||||
return { type: "CALLOUT", text: "" };
|
||||
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: "" };
|
||||
default:
|
||||
return { type: "PARAGRAPH", text: "" };
|
||||
case "PARAGRAPH": return { type: "PARAGRAPH", text: "" };
|
||||
case "HEADING": return { type: "HEADING", text: "", level: 2 };
|
||||
case "QUOTE": return { type: "QUOTE", text: "" };
|
||||
case "CALLOUT": return { type: "CALLOUT", title: "", text: "" };
|
||||
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: "" };
|
||||
default: return { type: "PARAGRAPH", text: "" };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user