Files
eu/components/editor.tsx
T
2026-06-21 13:35:32 +03:00

368 lines
11 KiB
TypeScript

"use client";
import { useState, useEffect } 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 { Block, BlockType, Page, PageMeta, PageType } from "@/lib/pages/types";
import { Globe2, Plus, Trash2, X } from "lucide-react";
import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue } from "./ui/select";
export type EditorData = {
title: string;
type: PageType;
slug: string;
location: string;
tags: string[];
blocks: Block[];
};
type EditorProps = {
page?: Page | null;
type?: PageType;
initialBlocks?: Block[];
onChange: (data: EditorData) => void;
};
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 [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);
useEffect(() => {
onChange({
title,
type,
slug: slug || title.toLowerCase().replace(/\s+/g, "-"),
location,
tags,
blocks,
});
}, [title, slug, location, tags, blocks]);
function addTag() {
const trimmed = tagInput.trim();
if (!trimmed || tags.includes(trimmed)) return;
setTags([...tags, trimmed]);
setTagInput("");
}
function removeTag(tag: string) {
setTags(tags.filter((t) => t !== tag));
}
function addBlock(type: BlockType) {
setBlocks([
...blocks,
{
id: crypto.randomUUID(),
pageId: page?.id ?? "",
parentId: null,
type,
position: blocks.length,
content: getDefaultContent(type),
},
]);
}
function removeBlock(id: string) {
setBlocks(blocks.filter((b) => b.id !== id));
}
function updateBlock(id: string, content: Block["content"]) {
setBlocks(blocks.map((b) => (b.id === id ? { ...b, content } : b)));
}
return (
<div className="flex flex-col gap-10">
<FieldSet>
<h2 className="underline underline-offset-8 decoration-2 mb-4">Meta</h2>
<FieldGroup>
<Field>
<FieldLabel htmlFor="title">Title</FieldLabel>
<InputGroup>
<InputGroupInput
id="title"
value={title}
onChange={(e) => {
setTitle(e.target.value);
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}>
<SelectTrigger>
<SelectValue placeholder="Type" />
</SelectTrigger>
<SelectContent>
<SelectGroup>
<SelectItem value="PAGE">Page</SelectItem>
<SelectItem value="NEWS">News</SelectItem>
</SelectGroup>
</SelectContent>
</Select>
</Field>
<Field>
<FieldLabel htmlFor="slug">Slug</FieldLabel>
<InputGroup>
<InputGroupInput
id="slug"
value={slug}
onChange={(e) => setSlug(e.target.value)}
placeholder="page-slug"
disabled={true}
/>
</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>
</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
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)}
>
<Plus size={12} />
{t.charAt(0) + t.slice(1).toLowerCase()}
</Button>
))}
</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>
<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>
);
}
function BlockInput({ block, onChange }: { block: Block; onChange: (c: Block["content"]) => void }) {
switch (block.type) {
case "PARAGRAPH":
return (
<textarea
className="w-full bg-transparent resize-none outline-none text-sm leading-relaxed"
rows={3}
placeholder="Start writing..."
value={(block.content as any).text ?? ""}
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 })
}
/>
);
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}
placeholder="Quote text..."
value={(block.content as any).text ?? ""}
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"
value={(block.content as any).author ?? ""}
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}
placeholder="Callout text..."
value={(block.content as any).text ?? ""}
onChange={(e) =>
onChange({ type: "CALLOUT", text: e.target.value })
}
/>
</div>
);
case "IMAGE":
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 })}
/>
);
default:
return (
<p className="text-muted-foreground text-sm">
{block.type} — editor coming soon
</p>
);
}
}
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: "" };
}
}