ai写作修改
This commit is contained in:
1 parent
fdfb888be9
commit
3b5525a0a5
8 files changed
+583
-356
No files matched your search
@@ -1,7 +1,9 @@
|
||||
import { FileText, PenLine } from 'lucide-react';
|
||||
|
||||
import { useAIWritingOptional } from '../../contexts/AIWritingContext';
|
||||
import { useCanvasGraphContext } from '../../contexts/CanvasGraphContext';
|
||||
import { useCanvasUIContext } from '../../contexts/CanvasUIContext';
|
||||
import { assembleStreamingDraftMarkdown } from '../../hooks/useAIWritingStream';
|
||||
import { getCurrentArtifactContent, isCodeArtifact } from '../../utils/artifact';
|
||||
import { TextArtifactRenderer } from '../artifact/TextArtifactRenderer';
|
||||
import { DraftStreamingPreview } from './DraftStreamingPreview';
|
||||
@@ -29,12 +31,25 @@ function DraftWaitingPlaceholder() {
|
||||
}
|
||||
|
||||
export function AIWritingDraftPanel() {
|
||||
const writing = useAIWritingOptional();
|
||||
const { artifact } = useCanvasGraphContext();
|
||||
const { isDraftStreaming, draftStreamingMarkdown, isDraftEditable } = useCanvasUIContext();
|
||||
const { isDraftEditable } = useCanvasUIContext();
|
||||
|
||||
// 流式生成中:只读流式预览,数据走独立通道(不依赖全局 artifact),全程不挂载编辑器,
|
||||
// 彻底隔绝高频内容刷新触发的编辑器更新风暴。
|
||||
if (isDraftStreaming) {
|
||||
const writingStatus = writing?.state.status;
|
||||
// 仅当章节仍在累积时走流式 Markdown;draft_ready 清空 sections 后即使 status 尚未切走也进画布
|
||||
const isActivelyStreamingDraft =
|
||||
(writingStatus === 'writing_draft' || writingStatus === 'revising') &&
|
||||
(writing?.state.streamingDraftSections.length ?? 0) > 0;
|
||||
|
||||
const streamingMarkdown = isActivelyStreamingDraft && writing
|
||||
? assembleStreamingDraftMarkdown(
|
||||
writing.state.streamingDraftSections,
|
||||
writing.state.streamingDraftTitle || writing.state.outline?.title || '',
|
||||
)
|
||||
: '';
|
||||
|
||||
// 流式生成中:只读 Markdown 预览
|
||||
if (isActivelyStreamingDraft) {
|
||||
return (
|
||||
<div className="h-full flex flex-col overflow-hidden border-l">
|
||||
<div className="h-10 border-b flex items-center gap-2 px-4 shrink-0 bg-muted/20">
|
||||
@@ -42,8 +57,8 @@ export function AIWritingDraftPanel() {
|
||||
<span className="text-sm font-medium truncate flex-1">草稿生成中…</span>
|
||||
</div>
|
||||
<div className="flex-1 overflow-hidden">
|
||||
{draftStreamingMarkdown ? (
|
||||
<DraftStreamingPreview markdown={draftStreamingMarkdown} />
|
||||
{streamingMarkdown ? (
|
||||
<DraftStreamingPreview markdown={streamingMarkdown} />
|
||||
) : (
|
||||
<DraftWaitingPlaceholder />
|
||||
)}
|
||||
@@ -52,38 +67,45 @@ export function AIWritingDraftPanel() {
|
||||
);
|
||||
}
|
||||
|
||||
if (!artifact) {
|
||||
const artifactContent = artifact ? getCurrentArtifactContent(artifact) : null;
|
||||
const textFromArtifact =
|
||||
artifactContent && !isCodeArtifact(artifactContent)
|
||||
? (artifactContent as CanvasArtifactText)
|
||||
: null;
|
||||
// draft_ready 后 DraftSync 写 artifact 可能晚一帧;先用 context 里的成稿兜底,避免闪占位符
|
||||
const textContent: CanvasArtifactText | null =
|
||||
textFromArtifact ??
|
||||
(writing?.state.currentDraftMarkdown
|
||||
? {
|
||||
index: 0,
|
||||
type: 'text',
|
||||
title: writing.state.currentDraftTitle || '草稿',
|
||||
fullMarkdown: writing.state.currentDraftMarkdown,
|
||||
}
|
||||
: null);
|
||||
|
||||
if (textContent) {
|
||||
return (
|
||||
<div className="h-full bg-muted/10 border-l">
|
||||
<DraftWaitingPlaceholder />
|
||||
<div className="h-full flex flex-col overflow-hidden border-l">
|
||||
<div className="h-10 border-b flex items-center gap-2 px-4 shrink-0 bg-muted/20">
|
||||
<FileText className="w-3.5 h-3.5 text-muted-foreground shrink-0" />
|
||||
<span className="text-sm font-medium truncate flex-1">
|
||||
{textContent.title || '草稿预览'}
|
||||
</span>
|
||||
</div>
|
||||
<div className="flex-1 overflow-hidden">
|
||||
<TextArtifactRenderer
|
||||
content={textContent}
|
||||
isStreaming={!isDraftEditable}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const content = getCurrentArtifactContent(artifact);
|
||||
const isText = content && !isCodeArtifact(content);
|
||||
|
||||
return (
|
||||
<div className="h-full flex flex-col overflow-hidden border-l">
|
||||
{/* 标题栏 */}
|
||||
<div className="h-10 border-b flex items-center gap-2 px-4 shrink-0 bg-muted/20">
|
||||
<FileText className="w-3.5 h-3.5 text-muted-foreground shrink-0" />
|
||||
<span className="text-sm font-medium truncate flex-1">
|
||||
{content?.title ?? '草稿预览'}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div className="flex-1 overflow-hidden">
|
||||
{isText ? (
|
||||
// 仅"生成完成的稳定态"挂载带编辑器的渲染器(润色 / 滑词);
|
||||
// 审核中等其它进行中状态用只读预览,避免编辑器在活跃流中挂载导致更新风暴。
|
||||
isDraftEditable ? (
|
||||
<TextArtifactRenderer content={content as CanvasArtifactText} />
|
||||
) : (
|
||||
<DraftStreamingPreview markdown={(content as CanvasArtifactText).fullMarkdown} showCursor={false} />
|
||||
)
|
||||
) : null}
|
||||
</div>
|
||||
<div className="h-full bg-muted/10 border-l">
|
||||
<DraftWaitingPlaceholder />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
import { useArticleTypes } from '../../hooks/useArticleTypes';
|
||||
|
||||
import { useCanvasGraphContext } from '../../contexts/CanvasGraphContext';
|
||||
import { useAIWriting } from '../../hooks/useAIWritingStream';
|
||||
import { useAIWriting } from '../../contexts/AIWritingContext';
|
||||
import { useAIWritingSessions } from '../../hooks/useAIWritingSessions';
|
||||
import { adminCleanupAIWritingSessions } from '../../api/ai-writing-sessions';
|
||||
|
||||
@@ -42,6 +42,7 @@ import { SkillStepsCard } from './SkillStepsCard';
|
||||
import { SkillSelectStreamingView } from './SkillSelectStreamingView';
|
||||
import { WritingSkeletonCard } from './WritingSkeletonCard';
|
||||
import { DraftGeneratingView } from './DraftGeneratingView';
|
||||
import { DraftSync } from './DraftSync';
|
||||
import { IntentResultCard } from './IntentResultCard';
|
||||
import { IntentStreamingView } from './IntentStreamingView';
|
||||
import { OutlineStreamingView } from './OutlineStreamingView';
|
||||
@@ -251,6 +252,7 @@ export function AIWritingPanel({
|
||||
if (historySession) {
|
||||
return (
|
||||
<div className="flex flex-col h-full overflow-hidden bg-background">
|
||||
<DraftSync status={state.status} markdown={state.currentDraftMarkdown} title={state.currentDraftTitle} />
|
||||
<div className="flex items-center justify-between px-4 py-2.5 border-b shrink-0">
|
||||
<span className="text-sm font-semibold truncate flex-1 min-w-0 mr-2">
|
||||
{historySession.draft_title ?? historySession.title}
|
||||
@@ -287,6 +289,7 @@ export function AIWritingPanel({
|
||||
|
||||
return (
|
||||
<div className="flex flex-col h-full overflow-hidden bg-background">
|
||||
<DraftSync status={state.status} markdown={state.currentDraftMarkdown} title={state.currentDraftTitle} />
|
||||
{/* 头部 */}
|
||||
<div className="flex items-center justify-between px-4 py-2.5 border-b shrink-0">
|
||||
<span
|
||||
|
||||
@@ -32,26 +32,66 @@ function ThinkingBox({ text, done }: { text: string; done: boolean }) {
|
||||
);
|
||||
}
|
||||
|
||||
/** 「内容生成中」骨架屏:闪烁的占位行,提示正文正在右侧实时生成 */
|
||||
/** 「内容生成中」骨架屏:流光扫过的占位行 + 写字笔抖动 + 跳动圆点,提示正文正在右侧实时生成 */
|
||||
const SKELETON_WIDTHS = ['92%', '100%', '85%', '96%', '70%'];
|
||||
|
||||
function GeneratingSkeleton() {
|
||||
return (
|
||||
<div className="rounded-xl border bg-card shadow-sm p-4">
|
||||
<div className="flex items-center gap-2 mb-3">
|
||||
<PenLine className="size-3.5 text-primary animate-pulse" />
|
||||
<span className="text-xs font-medium text-foreground">内容生成中…</span>
|
||||
<div className="group relative overflow-hidden rounded-xl border bg-card shadow-sm p-4">
|
||||
{/* 顶部一道缓慢流过的高光,强化"正在工作"的呼吸感 */}
|
||||
<div className="pointer-events-none absolute inset-0 -translate-x-full bg-gradient-to-r from-transparent via-primary/[0.07] to-transparent animate-[card-sheen_2.8s_ease-in-out_infinite]" />
|
||||
|
||||
<div className="relative flex items-center gap-2 mb-3">
|
||||
<PenLine className="size-3.5 text-primary animate-[pen-write_1.2s_ease-in-out_infinite] origin-bottom-left" />
|
||||
<span className="text-xs font-medium text-foreground">内容生成中</span>
|
||||
<span className="flex items-end gap-0.5">
|
||||
<span className="size-1 rounded-full bg-primary/70 animate-[dot-bounce_1.4s_ease-in-out_infinite]" />
|
||||
<span className="size-1 rounded-full bg-primary/70 animate-[dot-bounce_1.4s_ease-in-out_.2s_infinite]" />
|
||||
<span className="size-1 rounded-full bg-primary/70 animate-[dot-bounce_1.4s_ease-in-out_.4s_infinite]" />
|
||||
</span>
|
||||
<span className="text-[11px] text-muted-foreground ml-auto">正文已在右侧实时显示</span>
|
||||
</div>
|
||||
<div className="space-y-2.5">
|
||||
<div className="h-3 rounded bg-muted/70 animate-pulse w-[92%]" />
|
||||
<div className="h-3 rounded bg-muted/70 animate-pulse w-full" />
|
||||
<div className="h-3 rounded bg-muted/70 animate-pulse w-[85%]" />
|
||||
<div className="h-3 rounded bg-muted/70 animate-pulse w-[70%]" />
|
||||
|
||||
<div className="relative space-y-2.5">
|
||||
{SKELETON_WIDTHS.map((w, i) => (
|
||||
<div
|
||||
key={i}
|
||||
className="relative h-3 overflow-hidden rounded-full bg-muted/60"
|
||||
style={{ width: w }}
|
||||
>
|
||||
<div
|
||||
className="absolute inset-0 -translate-x-full bg-gradient-to-r from-transparent via-white/45 to-transparent dark:via-white/10 animate-[shimmer-sweep_1.5s_ease-in-out_infinite]"
|
||||
style={{ animationDelay: `${i * 0.15}s` }}
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<style>{`
|
||||
@keyframes blink {
|
||||
0%, 100% { opacity: 1; }
|
||||
50% { opacity: 0; }
|
||||
}
|
||||
/* 骨架行流光:高光从左扫到右 */
|
||||
@keyframes shimmer-sweep {
|
||||
0% { transform: translateX(-100%); }
|
||||
100% { transform: translateX(220%); }
|
||||
}
|
||||
/* 卡片整体微高光呼吸 */
|
||||
@keyframes card-sheen {
|
||||
0%, 60%, 100% { transform: translateX(-100%); }
|
||||
80% { transform: translateX(100%); }
|
||||
}
|
||||
/* 写字笔:轻微上下 + 摆动,像在书写 */
|
||||
@keyframes pen-write {
|
||||
0%, 100% { transform: translateY(0) rotate(0deg); }
|
||||
50% { transform: translateY(-1.5px) rotate(-12deg); }
|
||||
}
|
||||
/* 跳动圆点 */
|
||||
@keyframes dot-bounce {
|
||||
0%, 100% { opacity: .35; transform: translateY(0); }
|
||||
50% { opacity: 1; transform: translateY(-2px); }
|
||||
}
|
||||
`}</style>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import { useEffect } from 'react';
|
||||
|
||||
import { useCanvasGraphContext } from '../../contexts/CanvasGraphContext';
|
||||
import { useCanvasUIContext } from '../../contexts/CanvasUIContext';
|
||||
import type { WritingStatus } from '../../types/ai-writing';
|
||||
|
||||
interface DraftSyncProps {
|
||||
status: WritingStatus;
|
||||
markdown: string;
|
||||
title: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 把 AI 写作草稿在「稳定态」同步到画布 artifact / 编辑权限。
|
||||
*
|
||||
* 流式生成中(writing_draft / revising)**不再**逐 chunk 写入 CanvasUIContext,
|
||||
* 右侧预览改由 ``AIWritingDraftPanel`` 直接从 ``AIWritingContext`` 读章节并渲染 Markdown,
|
||||
* 避免高频 setState 引发更新风暴。
|
||||
*/
|
||||
export function DraftSync({ status, markdown, title }: DraftSyncProps) {
|
||||
const { setArtifact } = useCanvasGraphContext();
|
||||
const { setIsDraftEditable } = useCanvasUIContext();
|
||||
|
||||
const streaming = status === 'writing_draft' || status === 'revising';
|
||||
// 注意:reviewing(编辑审核)不算 generating —— 审核时草稿已定稿、内容稳定,
|
||||
// 右侧保持编辑器(富文本)视图,不再切回只读 markdown。仅真正在改写正文的
|
||||
// writing_draft / revising 才只读。
|
||||
const generating =
|
||||
status === 'researching' ||
|
||||
status === 'writing_outline' ||
|
||||
streaming;
|
||||
|
||||
useEffect(() => {
|
||||
setIsDraftEditable(!generating);
|
||||
}, [generating, setIsDraftEditable]);
|
||||
|
||||
useEffect(() => {
|
||||
if (streaming || !markdown) return;
|
||||
setArtifact({
|
||||
currentIndex: 0,
|
||||
contents: [{ index: 0, type: 'text', title: title || '草稿', fullMarkdown: markdown }],
|
||||
});
|
||||
}, [streaming, markdown, title, setArtifact]);
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import { CanvasStreamProvider } from "../../contexts/CanvasStreamContext";
|
||||
import { CanvasChatPanel } from "../chat/CanvasChatPanel";
|
||||
import { CanvasArtifactPanel } from "../artifact/CanvasArtifactPanel";
|
||||
import { AIWritingPanel } from "../ai-writing/AIWritingPanel";
|
||||
import { AIWritingProvider } from "../../contexts/AIWritingContext";
|
||||
|
||||
const LAYOUT_COLLAPSED = { chat: 0, artifact: 100 };
|
||||
const LAYOUT_DEFAULT = { chat: 35, artifact: 65 };
|
||||
@@ -65,7 +66,11 @@ export function CanvasResizableLayout() {
|
||||
</div>
|
||||
|
||||
<div className="flex-1 min-h-0 overflow-hidden">
|
||||
{leftMode === "chat" ? <CanvasChatPanel /> : <AIWritingPanel />}
|
||||
{leftMode === "chat" ? <CanvasChatPanel /> : (
|
||||
<AIWritingProvider>
|
||||
<AIWritingPanel />
|
||||
</AIWritingProvider>
|
||||
)}
|
||||
</div>
|
||||
</ResizablePanel>
|
||||
|
||||
|
||||
Reference in new issue
Block a user