301 lines
11 KiB
TypeScript
301 lines
11 KiB
TypeScript
import { useState, useMemo } from 'react';
|
|
import { ChevronRight, Plus, X, GripVertical } from 'lucide-react';
|
|
import { cn } from '@/lib/utils';
|
|
import type { KeyPoint, OutlineSection as OutlineSectionType } from '../../types/ai-writing';
|
|
import { normalizeKeyPoint } from '../../types/ai-writing';
|
|
import { CollapseTransition } from './CollapseTransition';
|
|
|
|
interface OutlineSectionProps {
|
|
section: OutlineSectionType;
|
|
index: number;
|
|
onChange: (updated: OutlineSectionType) => void;
|
|
onRemove?: () => void;
|
|
readOnly?: boolean;
|
|
// Drag-and-drop (章节级别)
|
|
isDragging?: boolean;
|
|
isOver?: boolean;
|
|
onDragStart?: () => void;
|
|
onDragEnter?: () => void;
|
|
onDrop?: () => void;
|
|
onDragEnd?: () => void;
|
|
}
|
|
|
|
/**
|
|
* 章节卡片(Level 1)。内部渲染 keyPoints[] 为「小节子卡片」(Level 2),小节里再渲染
|
|
* subPoints[] 为 bullet 列表(Level 3)。
|
|
*
|
|
* 兼容旧 2 级大纲:section.keyPoints 可能是 (string | KeyPoint)[];进入组件前通过
|
|
* normalizeKeyPoint 统一成 KeyPoint[],对外输出也一律是 KeyPoint[](自然升级,用户
|
|
* 编辑过的旧 session 大纲会被持久化为新格式)。
|
|
*/
|
|
export function OutlineSection({
|
|
section, index, onChange, onRemove, readOnly,
|
|
isDragging, isOver, onDragStart, onDragEnter, onDrop, onDragEnd,
|
|
}: OutlineSectionProps) {
|
|
const [expanded, setExpanded] = useState(false);
|
|
const [editingTitle, setEditingTitle] = useState(false);
|
|
const [titleDraft, setTitleDraft] = useState(section.sectionTitle);
|
|
|
|
// 把 keyPoints 统一成 KeyPoint[];旧 string 元素会被 lift。useMemo 避免每次 render
|
|
// 都生成新引用导致子组件无谓重渲染。
|
|
const keyPoints: KeyPoint[] = useMemo(
|
|
() => (section.keyPoints ?? []).map(normalizeKeyPoint),
|
|
[section.keyPoints],
|
|
);
|
|
|
|
function commitTitle() {
|
|
setEditingTitle(false);
|
|
const trimmed = titleDraft.trim();
|
|
if (trimmed && trimmed !== section.sectionTitle) {
|
|
onChange({ ...section, sectionTitle: trimmed });
|
|
} else {
|
|
setTitleDraft(section.sectionTitle);
|
|
}
|
|
}
|
|
|
|
function updateKeyPoint(i: number, updated: KeyPoint) {
|
|
const next = [...keyPoints];
|
|
next[i] = updated;
|
|
onChange({ ...section, keyPoints: next });
|
|
}
|
|
|
|
function removeKeyPoint(i: number) {
|
|
onChange({ ...section, keyPoints: keyPoints.filter((_, idx) => idx !== i) });
|
|
}
|
|
|
|
function addKeyPoint() {
|
|
onChange({ ...section, keyPoints: [...keyPoints, { pointTitle: '', subPoints: [] }] });
|
|
}
|
|
|
|
// 子要点总数,显示在章节头给个粗略概览
|
|
const totalSubPoints = keyPoints.reduce((sum, kp) => sum + (kp.subPoints?.length || 0), 0);
|
|
|
|
return (
|
|
<div
|
|
draggable={!readOnly}
|
|
onDragStart={(e) => { e.stopPropagation(); onDragStart?.(); }}
|
|
onDragOver={(e) => { e.preventDefault(); e.stopPropagation(); onDragEnter?.(); }}
|
|
onDrop={(e) => { e.preventDefault(); e.stopPropagation(); onDrop?.(); }}
|
|
onDragEnd={(e) => { e.stopPropagation(); onDragEnd?.(); }}
|
|
className={cn(
|
|
'rounded border bg-background text-xs transition-all duration-150 select-none',
|
|
isDragging ? 'opacity-40 border-border/60' : isOver ? 'border-blue-400 ring-1 ring-blue-400/40' : 'border-border/60',
|
|
)}
|
|
>
|
|
{/* 章节头 */}
|
|
<div className="flex items-center gap-1.5 px-2.5 py-2">
|
|
{!readOnly && (
|
|
<GripVertical className="size-3 text-muted-foreground/40 shrink-0 cursor-grab active:cursor-grabbing" />
|
|
)}
|
|
<button
|
|
type="button"
|
|
onClick={() => setExpanded((v) => !v)}
|
|
className="shrink-0"
|
|
>
|
|
<ChevronRight
|
|
className={cn(
|
|
'size-3.5 text-muted-foreground transition-transform duration-200',
|
|
expanded && 'rotate-90',
|
|
)}
|
|
/>
|
|
</button>
|
|
|
|
<span className="text-muted-foreground/60 shrink-0 w-4 text-center">{index + 1}.</span>
|
|
|
|
{editingTitle && !readOnly ? (
|
|
<input
|
|
autoFocus
|
|
value={titleDraft}
|
|
onChange={(e) => setTitleDraft(e.target.value)}
|
|
onBlur={commitTitle}
|
|
onKeyDown={(e) => {
|
|
if (e.key === 'Enter') commitTitle();
|
|
if (e.key === 'Escape') { setTitleDraft(section.sectionTitle); setEditingTitle(false); }
|
|
}}
|
|
className="flex-1 bg-transparent border-b border-primary outline-none font-medium"
|
|
/>
|
|
) : (
|
|
<span
|
|
className={cn('flex-1 font-medium', !readOnly && 'cursor-text hover:text-primary')}
|
|
onClick={() => !readOnly && setEditingTitle(true)}
|
|
>
|
|
{section.sectionTitle || <span className="text-muted-foreground italic">(未命名)</span>}
|
|
</span>
|
|
)}
|
|
|
|
<span className="text-muted-foreground/50 shrink-0">
|
|
{keyPoints.length} 小节{totalSubPoints > 0 ? ` · ${totalSubPoints} 子要点` : ''}
|
|
</span>
|
|
{!readOnly && onRemove && (
|
|
<button type="button" onClick={onRemove} className="shrink-0 text-muted-foreground/40 hover:text-destructive">
|
|
<X className="size-3" />
|
|
</button>
|
|
)}
|
|
</div>
|
|
|
|
{/* 小节列表 (Level 2) */}
|
|
<CollapseTransition open={expanded}>
|
|
<div className="px-6 pb-2.5 pt-1 space-y-1.5 border-t">
|
|
{keyPoints.map((kp, i) => (
|
|
<KeyPointRow
|
|
key={i}
|
|
keyPoint={kp}
|
|
onChange={(updated) => updateKeyPoint(i, updated)}
|
|
onRemove={readOnly ? undefined : () => removeKeyPoint(i)}
|
|
readOnly={readOnly}
|
|
/>
|
|
))}
|
|
{!readOnly && (
|
|
<button
|
|
type="button"
|
|
onClick={addKeyPoint}
|
|
className="flex items-center gap-1 text-muted-foreground/60 hover:text-primary pt-0.5"
|
|
>
|
|
<Plus className="size-3" />
|
|
添加小节
|
|
</button>
|
|
)}
|
|
</div>
|
|
</CollapseTransition>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
// ─── 小节行(Level 2,内嵌 Level 3 子要点) ───────────────────────────────────────
|
|
interface KeyPointRowProps {
|
|
keyPoint: KeyPoint;
|
|
onChange: (updated: KeyPoint) => void;
|
|
onRemove?: () => void;
|
|
readOnly?: boolean;
|
|
}
|
|
|
|
function KeyPointRow({ keyPoint, onChange, onRemove, readOnly }: KeyPointRowProps) {
|
|
const [expanded, setExpanded] = useState(false);
|
|
const [editingTitle, setEditingTitle] = useState(false);
|
|
const [titleDraft, setTitleDraft] = useState(keyPoint.pointTitle);
|
|
|
|
function commitTitle() {
|
|
setEditingTitle(false);
|
|
const trimmed = titleDraft.trim();
|
|
if (trimmed !== keyPoint.pointTitle) {
|
|
onChange({ ...keyPoint, pointTitle: trimmed });
|
|
}
|
|
}
|
|
|
|
function updateSubPoint(i: number, value: string) {
|
|
const next = [...keyPoint.subPoints];
|
|
next[i] = value;
|
|
onChange({ ...keyPoint, subPoints: next });
|
|
}
|
|
|
|
function removeSubPoint(i: number) {
|
|
onChange({ ...keyPoint, subPoints: keyPoint.subPoints.filter((_, idx) => idx !== i) });
|
|
}
|
|
|
|
function addSubPoint() {
|
|
onChange({ ...keyPoint, subPoints: [...keyPoint.subPoints, ''] });
|
|
setExpanded(true); // 添加时自动展开,免得用户看不到刚加的输入框
|
|
}
|
|
|
|
return (
|
|
<div className="rounded border border-border/40 bg-muted/10">
|
|
{/* 小节头 */}
|
|
<div className="flex items-center gap-1.5 px-2 py-1.5">
|
|
<button
|
|
type="button"
|
|
onClick={() => setExpanded((v) => !v)}
|
|
className="shrink-0"
|
|
aria-label="展开/收起子要点"
|
|
>
|
|
<ChevronRight
|
|
className={cn(
|
|
'size-3 text-muted-foreground/60 transition-transform duration-200',
|
|
expanded && 'rotate-90',
|
|
)}
|
|
/>
|
|
</button>
|
|
|
|
<span className="text-purple-500/70 shrink-0">###</span>
|
|
|
|
{editingTitle && !readOnly ? (
|
|
<input
|
|
autoFocus
|
|
value={titleDraft}
|
|
onChange={(e) => setTitleDraft(e.target.value)}
|
|
onBlur={commitTitle}
|
|
onKeyDown={(e) => {
|
|
if (e.key === 'Enter') commitTitle();
|
|
if (e.key === 'Escape') { setTitleDraft(keyPoint.pointTitle); setEditingTitle(false); }
|
|
}}
|
|
className="flex-1 bg-transparent border-b border-primary outline-none"
|
|
placeholder="小节标题"
|
|
/>
|
|
) : (
|
|
<span
|
|
className={cn('flex-1', !readOnly && 'cursor-text hover:text-primary')}
|
|
onClick={() => !readOnly && setEditingTitle(true)}
|
|
>
|
|
{keyPoint.pointTitle || <span className="text-muted-foreground italic">(未命名小节)</span>}
|
|
</span>
|
|
)}
|
|
|
|
<span className="text-muted-foreground/40 shrink-0 text-[10px]">
|
|
{keyPoint.subPoints.length > 0 ? `${keyPoint.subPoints.length} 子要点` : '无子要点'}
|
|
</span>
|
|
{!readOnly && onRemove && (
|
|
<button
|
|
type="button"
|
|
onClick={onRemove}
|
|
className="shrink-0 text-muted-foreground/40 hover:text-destructive"
|
|
aria-label="删除小节"
|
|
>
|
|
<X className="size-3" />
|
|
</button>
|
|
)}
|
|
</div>
|
|
|
|
{/* 子要点 (Level 3) — 每个子要点会变成正文里的 #### 标题 + 一段独立论述 */}
|
|
<CollapseTransition open={expanded}>
|
|
<div className="px-6 pb-1.5 space-y-0.5 border-t border-border/30 bg-background/40">
|
|
{keyPoint.subPoints.map((sp, i) => (
|
|
<div key={i} className="flex items-center gap-1.5 pt-1">
|
|
{/* 用 #### 标记替代 bullet,提示这是「四级标题」而非简单列表项 */}
|
|
<span className="text-pink-500/70 shrink-0 text-[10px] font-mono">####</span>
|
|
{readOnly ? (
|
|
<span className="flex-1 text-muted-foreground">{sp}</span>
|
|
) : (
|
|
<input
|
|
value={sp}
|
|
onChange={(e) => updateSubPoint(i, e.target.value)}
|
|
placeholder="子要点标题(会展开成一段正文)"
|
|
className="flex-1 bg-transparent border-b border-transparent hover:border-border focus:border-primary outline-none text-muted-foreground"
|
|
/>
|
|
)}
|
|
{!readOnly && (
|
|
<button
|
|
type="button"
|
|
onClick={() => removeSubPoint(i)}
|
|
className="shrink-0 text-muted-foreground/30 hover:text-destructive"
|
|
aria-label="删除子要点"
|
|
>
|
|
<X className="size-3" />
|
|
</button>
|
|
)}
|
|
</div>
|
|
))}
|
|
{!readOnly && (
|
|
<button
|
|
type="button"
|
|
onClick={addSubPoint}
|
|
className="flex items-center gap-1 text-muted-foreground/50 hover:text-primary pt-0.5 text-[11px]"
|
|
>
|
|
<Plus className="size-3" />
|
|
添加子要点
|
|
</button>
|
|
)}
|
|
</div>
|
|
</CollapseTransition>
|
|
</div>
|
|
);
|
|
}
|