Stage 1: Course/Module/Lesson CRUD admin UI with TipTap editor
This commit is contained in:
@@ -0,0 +1,183 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useCallback, useTransition } from "react";
|
||||
import { useEditor, EditorContent } from "@tiptap/react";
|
||||
import StarterKit from "@tiptap/starter-kit";
|
||||
import Image from "@tiptap/extension-image";
|
||||
import Link from "@tiptap/extension-link";
|
||||
import Placeholder from "@tiptap/extension-placeholder";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { saveLesson } from "@/app/admin/courses/[courseId]/modules/[moduleId]/lessons/[lessonId]/actions";
|
||||
|
||||
interface LessonData {
|
||||
id: string;
|
||||
title: string;
|
||||
kinescopeId: string;
|
||||
content: object;
|
||||
published: boolean;
|
||||
}
|
||||
|
||||
export function LessonEditor({
|
||||
lesson,
|
||||
courseId,
|
||||
moduleId,
|
||||
}: {
|
||||
lesson: LessonData;
|
||||
courseId: string;
|
||||
moduleId: string;
|
||||
}) {
|
||||
const [title, setTitle] = useState(lesson.title);
|
||||
const [kinescopeId, setKinescopeId] = useState(lesson.kinescopeId);
|
||||
const [published, setPublished] = useState(lesson.published);
|
||||
const [uploading, setUploading] = useState(false);
|
||||
const [saved, setSaved] = useState(false);
|
||||
const [pending, startTransition] = useTransition();
|
||||
|
||||
const editor = useEditor({
|
||||
extensions: [
|
||||
StarterKit,
|
||||
Image.configure({ inline: false }),
|
||||
Link.configure({ openOnClick: false }),
|
||||
Placeholder.configure({ placeholder: "Начните писать текст урока..." }),
|
||||
],
|
||||
content: Object.keys(lesson.content).length ? lesson.content : undefined,
|
||||
editorProps: {
|
||||
attributes: {
|
||||
class: "prose prose-slate max-w-none min-h-[300px] focus:outline-none p-4",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const uploadImage = useCallback(async () => {
|
||||
const input = document.createElement("input");
|
||||
input.type = "file";
|
||||
input.accept = "image/*";
|
||||
input.onchange = async () => {
|
||||
const file = input.files?.[0];
|
||||
if (!file || !editor) return;
|
||||
setUploading(true);
|
||||
const fd = new FormData();
|
||||
fd.append("file", file);
|
||||
const res = await fetch("/api/admin/upload", { method: "POST", body: fd });
|
||||
const data = await res.json();
|
||||
if (data.url) editor.chain().focus().setImage({ src: data.url }).run();
|
||||
setUploading(false);
|
||||
};
|
||||
input.click();
|
||||
}, [editor]);
|
||||
|
||||
function handleSave() {
|
||||
if (!editor) return;
|
||||
startTransition(async () => {
|
||||
await saveLesson(lesson.id, courseId, moduleId, {
|
||||
title,
|
||||
kinescopeId,
|
||||
content: editor.getJSON(),
|
||||
published,
|
||||
});
|
||||
setSaved(true);
|
||||
setTimeout(() => setSaved(false), 2000);
|
||||
});
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-5">
|
||||
{/* Header controls */}
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex items-center gap-3">
|
||||
<button
|
||||
type="button"
|
||||
role="switch"
|
||||
aria-checked={published}
|
||||
onClick={() => setPublished(!published)}
|
||||
className={`relative w-10 h-6 rounded-full transition-colors ${published ? "bg-green-500" : "bg-slate-300"}`}
|
||||
>
|
||||
<span className={`absolute top-1 left-1 w-4 h-4 bg-white rounded-full shadow transition-transform ${published ? "translate-x-4" : ""}`} />
|
||||
</button>
|
||||
<span className="text-sm text-slate-600">{published ? "Опубликован" : "Черновик"}</span>
|
||||
</div>
|
||||
<Button onClick={handleSave} disabled={pending || uploading}>
|
||||
{pending ? "Сохранение..." : saved ? "✓ Сохранено" : "Сохранить урок"}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{/* Title */}
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="lesson-title">Заголовок урока</Label>
|
||||
<Input
|
||||
id="lesson-title"
|
||||
value={title}
|
||||
onChange={(e) => setTitle(e.target.value)}
|
||||
className="text-lg font-medium"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Kinescope ID */}
|
||||
<div className="space-y-1.5">
|
||||
<Label htmlFor="kinescope-id">Kinescope ID</Label>
|
||||
<Input
|
||||
id="kinescope-id"
|
||||
value={kinescopeId}
|
||||
onChange={(e) => setKinescopeId(e.target.value)}
|
||||
placeholder="Оставьте пустым если видео нет"
|
||||
className="font-mono text-sm"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* TipTap Editor */}
|
||||
<div className="space-y-1.5">
|
||||
<Label>Содержимое урока</Label>
|
||||
|
||||
{/* Toolbar */}
|
||||
<div className="flex flex-wrap gap-1 p-2 bg-slate-50 border border-slate-200 rounded-t-lg border-b-0">
|
||||
<ToolBtn onClick={() => editor?.chain().focus().toggleBold().run()} active={editor?.isActive("bold")}>Ж</ToolBtn>
|
||||
<ToolBtn onClick={() => editor?.chain().focus().toggleItalic().run()} active={editor?.isActive("italic")}><em>К</em></ToolBtn>
|
||||
<ToolBtn onClick={() => editor?.chain().focus().toggleHeading({ level: 2 }).run()} active={editor?.isActive("heading", { level: 2 })}>H2</ToolBtn>
|
||||
<ToolBtn onClick={() => editor?.chain().focus().toggleHeading({ level: 3 }).run()} active={editor?.isActive("heading", { level: 3 })}>H3</ToolBtn>
|
||||
<div className="w-px bg-slate-200 mx-1" />
|
||||
<ToolBtn onClick={() => editor?.chain().focus().toggleBulletList().run()} active={editor?.isActive("bulletList")}>• Список</ToolBtn>
|
||||
<ToolBtn onClick={() => editor?.chain().focus().toggleOrderedList().run()} active={editor?.isActive("orderedList")}>1. Список</ToolBtn>
|
||||
<ToolBtn onClick={() => editor?.chain().focus().toggleBlockquote().run()} active={editor?.isActive("blockquote")}>“”</ToolBtn>
|
||||
<ToolBtn onClick={() => editor?.chain().focus().toggleCodeBlock().run()} active={editor?.isActive("codeBlock")}>{'</>'}</ToolBtn>
|
||||
<div className="w-px bg-slate-200 mx-1" />
|
||||
<ToolBtn onClick={uploadImage} disabled={uploading}>{uploading ? "Загрузка..." : "🖼 Фото"}</ToolBtn>
|
||||
<div className="w-px bg-slate-200 mx-1" />
|
||||
<ToolBtn onClick={() => editor?.chain().focus().undo().run()}>↩</ToolBtn>
|
||||
<ToolBtn onClick={() => editor?.chain().focus().redo().run()}>↪</ToolBtn>
|
||||
</div>
|
||||
|
||||
{/* Editor content */}
|
||||
<div className="border border-slate-200 rounded-b-lg bg-white">
|
||||
<EditorContent editor={editor} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function ToolBtn({
|
||||
onClick,
|
||||
active,
|
||||
disabled,
|
||||
children,
|
||||
}: {
|
||||
onClick: () => void;
|
||||
active?: boolean;
|
||||
disabled?: boolean;
|
||||
children: React.ReactNode;
|
||||
}) {
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
onClick={onClick}
|
||||
disabled={disabled}
|
||||
className={`px-2 py-1 text-sm rounded transition-colors ${
|
||||
active ? "bg-slate-700 text-white" : "hover:bg-slate-200 text-slate-700"
|
||||
} disabled:opacity-50`}
|
||||
>
|
||||
{children}
|
||||
</button>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user