import { useState, useEffect, useMemo } from 'react'; import { DocContent } from '../api'; import { cleanDisplayText } from '../presentation'; import { renderKnowledgeMarkdown } from '../markdown'; interface Props { doc: DocContent; onSave: (title: string, body: string) => void; onWikiSelect?: (target: string) => void; } export function Editor({ doc, onSave, onWikiSelect }: Props) { const [editing, setEditing] = useState(false); const [title, setTitle] = useState(doc.meta.title); const [body, setBody] = useState(doc.body); // 切换文档时重置 useEffect(() => { setTitle(doc.meta.title); setBody(doc.body); setEditing(false); }, [doc.meta.id]); const rendered = useMemo(() => { try { return renderKnowledgeMarkdown(body); } catch { return '

渲染失败

'; } }, [body]); const handleSave = () => { onSave(title, body); setEditing(false); }; const handleCancel = () => { setTitle(doc.meta.title); setBody(doc.body); setEditing(false); }; const displayPath = doc.meta.id.split('/').map(cleanDisplayText).join(' / '); const handleRenderedClick = (event: React.MouseEvent) => { const anchor = (event.target as HTMLElement).closest('a[data-hololake-wiki="true"]'); if (!anchor) return; event.preventDefault(); const prefix = 'hololake://knowledge/'; const rawTarget = anchor.href.startsWith(prefix) ? anchor.href.slice(prefix.length) : anchor.title; if (rawTarget) onWikiSelect?.(decodeURIComponent(rawTarget)); }; return (
{/* 元信息栏 */}
HoloLake / {displayPath} 更新于 {new Date(doc.meta.updatedAt).toLocaleString('zh-CN')} by {doc.meta.author}
{/* 标题 */} {editing ? ( setTitle(e.target.value)} placeholder="文档标题" /> ) : (

setEditing(true)}> {cleanDisplayText(doc.meta.title)}

)} {/* 操作栏 */}
{editing ? ( <> ) : ( )}
{/* 内容区 */} {editing ? (