145 lines
5.1 KiB
TypeScript
145 lines
5.1 KiB
TypeScript
import { useState } from 'react';
|
|
import { ListTree, Plus } from 'lucide-react';
|
|
import { Button } from '@/components/ui/button';
|
|
import type { AIOutline, OutlineSection as OutlineSectionType } from '../../types/ai-writing';
|
|
import { OutlineSection } from './OutlineSection';
|
|
import { CollapseTransition } from './CollapseTransition';
|
|
|
|
interface OutlineCardProps {
|
|
outline: AIOutline;
|
|
onChange?: (updated: AIOutline) => void;
|
|
readOnly?: boolean;
|
|
}
|
|
|
|
export function OutlineCard({ outline, onChange, readOnly = false }: OutlineCardProps) {
|
|
const [collapsed, setCollapsed] = useState(false);
|
|
const [editingTitle, setEditingTitle] = useState(false);
|
|
const [titleDraft, setTitleDraft] = useState(outline.title);
|
|
const [dragIndex, setDragIndex] = useState<number | null>(null);
|
|
const [overIndex, setOverIndex] = useState<number | null>(null);
|
|
|
|
const editable = !readOnly && !!onChange;
|
|
|
|
function commitTitle() {
|
|
setEditingTitle(false);
|
|
const trimmed = titleDraft.trim();
|
|
if (trimmed && trimmed !== outline.title) {
|
|
onChange?.({ ...outline, title: trimmed });
|
|
} else {
|
|
setTitleDraft(outline.title);
|
|
}
|
|
}
|
|
|
|
function updateSection(i: number, updated: OutlineSectionType) {
|
|
const sections = [...outline.sections];
|
|
sections[i] = updated;
|
|
onChange?.({ ...outline, sections });
|
|
}
|
|
|
|
function removeSection(i: number) {
|
|
onChange?.({ ...outline, sections: outline.sections.filter((_, idx) => idx !== i) });
|
|
}
|
|
|
|
function reorderSections(from: number, to: number) {
|
|
if (from === to) return;
|
|
const sections = [...outline.sections];
|
|
const [moved] = sections.splice(from, 1);
|
|
sections.splice(to, 0, moved);
|
|
onChange?.({ ...outline, sections });
|
|
}
|
|
|
|
function addSection() {
|
|
onChange?.({
|
|
...outline,
|
|
sections: [...outline.sections, { sectionTitle: '', keyPoints: [], materialIds: [] }],
|
|
});
|
|
}
|
|
|
|
return (
|
|
<div className="rounded-md border bg-muted/20 overflow-hidden">
|
|
{/* 头部 */}
|
|
<div className="flex items-center gap-2 px-3 py-2 border-b">
|
|
<ListTree className="size-3.5 text-purple-500 shrink-0" />
|
|
<span className="text-xs font-medium flex-1">文章大纲</span>
|
|
<button
|
|
type="button"
|
|
onClick={() => setCollapsed((v) => !v)}
|
|
className="text-xs text-muted-foreground hover:text-foreground"
|
|
>
|
|
{collapsed ? '展开' : '收起'}
|
|
</button>
|
|
</div>
|
|
|
|
<CollapseTransition open={!collapsed}>
|
|
<div className="px-3 py-2.5 space-y-2">
|
|
{/* 文章标题 */}
|
|
<div className="flex items-center gap-1.5 text-xs">
|
|
<span className="text-muted-foreground shrink-0">标题:</span>
|
|
{editingTitle && editable ? (
|
|
<input
|
|
autoFocus
|
|
value={titleDraft}
|
|
onChange={(e) => setTitleDraft(e.target.value)}
|
|
onBlur={commitTitle}
|
|
onKeyDown={(e) => {
|
|
if (e.key === 'Enter') commitTitle();
|
|
if (e.key === 'Escape') { setTitleDraft(outline.title); setEditingTitle(false); }
|
|
}}
|
|
className="flex-1 bg-transparent border-b border-primary outline-none font-semibold"
|
|
/>
|
|
) : (
|
|
<span
|
|
className={editable ? 'flex-1 font-semibold cursor-text hover:text-primary' : 'flex-1 font-semibold'}
|
|
onClick={() => editable && setEditingTitle(true)}
|
|
>
|
|
{outline.title || <span className="text-muted-foreground italic font-normal">(未设置标题)</span>}
|
|
</span>
|
|
)}
|
|
</div>
|
|
|
|
{/* 章节列表 */}
|
|
<div className="space-y-1.5">
|
|
{outline.sections.map((section, i) => (
|
|
<OutlineSection
|
|
key={i}
|
|
section={section}
|
|
index={i}
|
|
onChange={(updated) => updateSection(i, updated)}
|
|
onRemove={editable ? () => removeSection(i) : undefined}
|
|
readOnly={!editable}
|
|
isDragging={dragIndex === i}
|
|
isOver={overIndex === i && dragIndex !== null && dragIndex !== i}
|
|
onDragStart={() => setDragIndex(i)}
|
|
onDragEnter={() => setOverIndex(i)}
|
|
onDrop={() => {
|
|
if (dragIndex !== null && dragIndex !== i) reorderSections(dragIndex, i);
|
|
setDragIndex(null);
|
|
setOverIndex(null);
|
|
}}
|
|
onDragEnd={() => {
|
|
setDragIndex(null);
|
|
setOverIndex(null);
|
|
}}
|
|
/>
|
|
))}
|
|
</div>
|
|
|
|
{/* 添加章节 */}
|
|
{editable && (
|
|
<Button
|
|
type="button"
|
|
variant="ghost"
|
|
size="sm"
|
|
className="w-full h-7 text-xs gap-1.5 text-muted-foreground"
|
|
onClick={addSection}
|
|
>
|
|
<Plus className="size-3" />
|
|
添加章节
|
|
</Button>
|
|
)}
|
|
</div>
|
|
</CollapseTransition>
|
|
</div>
|
|
);
|
|
}
|