feat: improve ai writing imitation flow
This commit is contained in:
1 parent
0547fb1715
commit
025e43f4c5
14 files changed
+927
-117
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import { useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react';
|
||||
import { useNavigate, useSearchParams } from 'react-router-dom';
|
||||
import { ChevronsDown, CloudUpload, PanelRightClose, Pause, Play, Plus, RotateCcw, Settings2, Trash2 } from 'lucide-react';
|
||||
import { toast } from 'sonner';
|
||||
@@ -44,6 +44,8 @@ import { DraftGeneratingView } from './DraftGeneratingView';
|
||||
import { DraftSync } from './DraftSync';
|
||||
import { IntentResultCard } from './IntentResultCard';
|
||||
import { IntentStreamingView } from './IntentStreamingView';
|
||||
import { SampleAnalysisStreamingView } from './SampleAnalysisStreamingView';
|
||||
import { SampleStyleProfileCard } from './SampleStyleProfileCard';
|
||||
import { OutlineStreamingView } from './OutlineStreamingView';
|
||||
import { ReviewStreamingView } from './ReviewStreamingView';
|
||||
import { RevisionSummaryCard } from './RevisionSummaryCard';
|
||||
@@ -98,93 +100,204 @@ export function AIWritingPanel({
|
||||
const isDone = state.status === 'done';
|
||||
const isError = state.status === 'error';
|
||||
const isRunning = !isIdle && !isDone && !isError;
|
||||
const shouldAutoFollow = !isIdle && !isHistoryMode;
|
||||
const showSkeleton = isRunning && !state.currentPause && !state.isPaused;
|
||||
const intentSlot = (state.intentStreamingText || state.intentThinkingText) ? (
|
||||
<IntentStreamingView text={state.intentStreamingText} thinkingText={state.intentThinkingText} />
|
||||
) : state.parsedIntent ? (
|
||||
<IntentResultCard
|
||||
intent={state.parsedIntent}
|
||||
articleTypes={articleTypes}
|
||||
hideTargetAudience={hideAdvancedFields}
|
||||
embedded
|
||||
/>
|
||||
) : null;
|
||||
const sampleAnalysisSlot = (state.sampleAnalysisStreamingText || state.sampleAnalysisThinkingText) ? (
|
||||
<SampleAnalysisStreamingView
|
||||
text={state.sampleAnalysisStreamingText}
|
||||
thinkingText={state.sampleAnalysisThinkingText}
|
||||
/>
|
||||
) : null;
|
||||
const sampleProfileSlot = state.sampleStyleProfile ? (
|
||||
<SampleStyleProfileCard profile={state.sampleStyleProfile} />
|
||||
) : null;
|
||||
|
||||
// ── 自动滚底逻辑 ────────────────────────────────────────────────────────────
|
||||
const scrollRef = useRef<HTMLDivElement>(null);
|
||||
const bottomRef = useRef<HTMLDivElement>(null);
|
||||
// 程序触发滚动时置 true,阻止 scroll 事件误判为用户上划
|
||||
const isAutoScrollingRef = useRef(false);
|
||||
// 同步 ref:让 scroll/wheel 回调可以立即读取最新值,不依赖 state 异步更新
|
||||
const autoScrollRef = useRef(true);
|
||||
const shouldAutoFollowRef = useRef(shouldAutoFollow);
|
||||
shouldAutoFollowRef.current = shouldAutoFollow;
|
||||
const scrollbarDragRef = useRef(false);
|
||||
const touchYRef = useRef<number | null>(null);
|
||||
const [autoScroll, setAutoScroll] = useState(true);
|
||||
|
||||
const enableAutoScroll = useCallback(() => {
|
||||
autoScrollRef.current = true;
|
||||
setAutoScroll(true);
|
||||
}, []);
|
||||
|
||||
const disableAutoScroll = useCallback(() => {
|
||||
autoScrollRef.current = false;
|
||||
setAutoScroll(false);
|
||||
}, []);
|
||||
|
||||
const scrollToBottomNow = useCallback(() => {
|
||||
const el = scrollRef.current;
|
||||
if (!el) return;
|
||||
isAutoScrollingRef.current = true;
|
||||
|
||||
const jump = () => {
|
||||
if (bottomRef.current) {
|
||||
bottomRef.current.scrollIntoView({ block: 'end' });
|
||||
} else {
|
||||
el.scrollTop = el.scrollHeight;
|
||||
}
|
||||
};
|
||||
|
||||
jump();
|
||||
requestAnimationFrame(() => {
|
||||
jump();
|
||||
requestAnimationFrame(() => {
|
||||
isAutoScrollingRef.current = false;
|
||||
});
|
||||
});
|
||||
}, []);
|
||||
|
||||
const maybeAutoScrollToBottom = useCallback(() => {
|
||||
if (!autoScrollRef.current || !shouldAutoFollowRef.current) return;
|
||||
scrollToBottomNow();
|
||||
}, [scrollToBottomNow]);
|
||||
|
||||
// 新会话 / 重置后恢复自动滚底
|
||||
useEffect(() => {
|
||||
if (isIdle) {
|
||||
autoScrollRef.current = true;
|
||||
setAutoScroll(true);
|
||||
enableAutoScroll();
|
||||
}
|
||||
}, [isIdle]);
|
||||
}, [enableAutoScroll, isIdle]);
|
||||
|
||||
// 开始写作 / 点击继续后恢复自动滚底
|
||||
useEffect(() => {
|
||||
if (isRunning && !state.isPaused) {
|
||||
autoScrollRef.current = true;
|
||||
setAutoScroll(true);
|
||||
if (shouldAutoFollow && !state.isPaused) {
|
||||
enableAutoScroll();
|
||||
requestAnimationFrame(scrollToBottomNow);
|
||||
}
|
||||
}, [isRunning, state.isPaused]);
|
||||
}, [enableAutoScroll, scrollToBottomNow, shouldAutoFollow, state.isPaused]);
|
||||
|
||||
// 监听用户手动滚动(挂载一次)
|
||||
useEffect(() => {
|
||||
const el = scrollRef.current;
|
||||
if (!el) return;
|
||||
const nearBottom = () => el.scrollHeight - el.scrollTop - el.clientHeight < 24;
|
||||
|
||||
// wheel 事件:鼠标滚轮 / 触控板,只由用户触发
|
||||
const onWheel = (e: WheelEvent) => {
|
||||
if (e.deltaY < 0) disableAutoScroll(); // 向上滚
|
||||
};
|
||||
|
||||
// scroll 事件:兼容拖动滚动条的场景
|
||||
// 通过对比前后 scrollTop 判断是否向上滚(排除程序触发的向下滚)
|
||||
const onTouchStart = (e: TouchEvent) => {
|
||||
touchYRef.current = e.touches[0]?.clientY ?? null;
|
||||
};
|
||||
|
||||
const onTouchMove = (e: TouchEvent) => {
|
||||
const prevY = touchYRef.current;
|
||||
const nextY = e.touches[0]?.clientY ?? null;
|
||||
if (prevY !== null && nextY !== null && nextY > prevY + 4) {
|
||||
disableAutoScroll();
|
||||
}
|
||||
touchYRef.current = nextY;
|
||||
};
|
||||
|
||||
const onPointerDown = (e: PointerEvent) => {
|
||||
if (e.target === el) {
|
||||
scrollbarDragRef.current = true;
|
||||
}
|
||||
};
|
||||
|
||||
const onPointerDone = () => {
|
||||
scrollbarDragRef.current = false;
|
||||
};
|
||||
|
||||
// scroll 事件只处理「用户拖动滚动条」和「手动回到底部」。
|
||||
// 普通布局变化也会触发 scroll,不能再把所有 scrollTop 下降都当成用户上划。
|
||||
let lastTop = el.scrollTop;
|
||||
const onScroll = () => {
|
||||
const cur = el.scrollTop;
|
||||
if (!isAutoScrollingRef.current && cur < lastTop - 2) {
|
||||
if (isAutoScrollingRef.current) {
|
||||
lastTop = cur;
|
||||
return;
|
||||
}
|
||||
if (nearBottom()) {
|
||||
enableAutoScroll();
|
||||
} else if (scrollbarDragRef.current && cur < lastTop - 2) {
|
||||
disableAutoScroll();
|
||||
}
|
||||
lastTop = cur;
|
||||
};
|
||||
|
||||
el.addEventListener('wheel', onWheel, { passive: true });
|
||||
el.addEventListener('touchstart', onTouchStart, { passive: true });
|
||||
el.addEventListener('touchmove', onTouchMove, { passive: true });
|
||||
el.addEventListener('pointerdown', onPointerDown);
|
||||
window.addEventListener('pointerup', onPointerDone);
|
||||
window.addEventListener('pointercancel', onPointerDone);
|
||||
el.addEventListener('scroll', onScroll, { passive: true });
|
||||
return () => {
|
||||
el.removeEventListener('wheel', onWheel);
|
||||
el.removeEventListener('touchstart', onTouchStart);
|
||||
el.removeEventListener('touchmove', onTouchMove);
|
||||
el.removeEventListener('pointerdown', onPointerDown);
|
||||
window.removeEventListener('pointerup', onPointerDone);
|
||||
window.removeEventListener('pointercancel', onPointerDone);
|
||||
el.removeEventListener('scroll', onScroll);
|
||||
};
|
||||
}, [disableAutoScroll]);
|
||||
}, [disableAutoScroll, enableAutoScroll]);
|
||||
|
||||
// 内容高度变化兜底:同一 timeline 节点内卡片展开、文风画像替换、流式文本变长,
|
||||
// 都可能不改变 progressEvents.length。监听 DOM 变化可以把这些情况统一托底。
|
||||
useEffect(() => {
|
||||
const el = scrollRef.current;
|
||||
if (!el || typeof MutationObserver === 'undefined') return;
|
||||
const observer = new MutationObserver(() => {
|
||||
maybeAutoScrollToBottom();
|
||||
});
|
||||
observer.observe(el, { childList: true, subtree: true, characterData: true });
|
||||
return () => observer.disconnect();
|
||||
}, [maybeAutoScrollToBottom]);
|
||||
|
||||
// 内容变化时若自动滚底开启则滚到底部
|
||||
useEffect(() => {
|
||||
// 检查同步 ref,避免 state 异步导致的时序竞争
|
||||
if (!autoScrollRef.current || !isRunning || !scrollRef.current) return;
|
||||
const el = scrollRef.current;
|
||||
isAutoScrollingRef.current = true;
|
||||
el.scrollTop = el.scrollHeight;
|
||||
requestAnimationFrame(() => { isAutoScrollingRef.current = false; });
|
||||
useLayoutEffect(() => {
|
||||
maybeAutoScrollToBottom();
|
||||
}, [
|
||||
maybeAutoScrollToBottom,
|
||||
autoScroll, // 点击按钮后重新触发一次
|
||||
isRunning,
|
||||
state.status,
|
||||
state.progressEvents.length,
|
||||
state.sampleAnalysisStreamingText,
|
||||
state.sampleAnalysisThinkingText,
|
||||
state.sampleStyleProfile,
|
||||
state.intentStreamingText,
|
||||
state.intentThinkingText,
|
||||
state.parsedIntent,
|
||||
state.skillSearchSteps.length,
|
||||
state.skillSelectStreamingText,
|
||||
state.skillSelectThinkingText,
|
||||
state.streamingDraftSections.length,
|
||||
state.streamingReviewItems.length,
|
||||
state.outlineStreamingText,
|
||||
state.outlineThinkingText,
|
||||
state.revisionSummary,
|
||||
state.currentPause,
|
||||
]);
|
||||
|
||||
const scrollToBottom = useCallback(() => {
|
||||
const el = scrollRef.current;
|
||||
if (!el) return;
|
||||
isAutoScrollingRef.current = true;
|
||||
el.scrollTop = el.scrollHeight;
|
||||
requestAnimationFrame(() => { isAutoScrollingRef.current = false; });
|
||||
autoScrollRef.current = true;
|
||||
setAutoScroll(true);
|
||||
}, []);
|
||||
enableAutoScroll();
|
||||
scrollToBottomNow();
|
||||
}, [enableAutoScroll, scrollToBottomNow]);
|
||||
|
||||
const handleSelectHistory = useCallback((session: AIWritingSessionSummary) => {
|
||||
void openHistory(session);
|
||||
@@ -447,7 +560,13 @@ export function AIWritingPanel({
|
||||
|
||||
{/* 主体:relative 父容器 + absolute 滚动容器,确保滚动容器尺寸始终正确 */}
|
||||
<div className="flex-1 min-h-0 relative">
|
||||
<div ref={scrollRef} className="absolute inset-0 overflow-y-auto">
|
||||
<div
|
||||
ref={scrollRef}
|
||||
className={cn(
|
||||
'absolute inset-0 overflow-y-auto',
|
||||
autoScroll && '[overflow-anchor:none]',
|
||||
)}
|
||||
>
|
||||
{/* 历史回看:左侧时间线(与实时写作同一套 AIWritingTimeline) */}
|
||||
{isHistoryMode && historySession && (
|
||||
<AIWritingHistoryView session={historySession} />
|
||||
@@ -464,27 +583,16 @@ export function AIWritingPanel({
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* 意图解析:流式 → 结果卡片承接。
|
||||
放在 timeline 之上 —— 解析后 timeline 会从 step_done 起一直长,
|
||||
如果把意图块塞在 timeline 下面就会随 timeline 增长被推走;
|
||||
放上面则恒定锚定在内容最顶部,刷到底也能滚回顶部看清楚意图理解结果。 */}
|
||||
{(state.intentStreamingText || state.intentThinkingText) && (
|
||||
<IntentStreamingView text={state.intentStreamingText} thinkingText={state.intentThinkingText} />
|
||||
)}
|
||||
{state.parsedIntent && !state.intentStreamingText && (
|
||||
<IntentResultCard
|
||||
intent={state.parsedIntent}
|
||||
articleTypes={articleTypes}
|
||||
hideTargetAudience={hideAdvancedFields}
|
||||
/>
|
||||
)}
|
||||
|
||||
{/* 运行中或完成后显示时间线 */}
|
||||
{!isIdle && !isHistoryMode && (
|
||||
<AIWritingTimeline
|
||||
events={state.progressEvents}
|
||||
completedInterventions={state.completedInterventions}
|
||||
currentPause={state.currentPause}
|
||||
sampleAnalysisSlot={sampleAnalysisSlot}
|
||||
sampleProfileSlot={sampleProfileSlot}
|
||||
intentSlot={intentSlot}
|
||||
intentSlotDone={!!state.parsedIntent && !state.intentStreamingText && !state.intentThinkingText}
|
||||
currentInterventionSlot={
|
||||
state.currentPause ? (
|
||||
<InterventionCard
|
||||
@@ -571,6 +679,8 @@ export function AIWritingPanel({
|
||||
!state.skillSearchPending &&
|
||||
!(state.status === 'researching' && state.skillSearchSteps.length > 0) &&
|
||||
!(state.status === 'researching' && (state.skillSelectStreamingText || state.skillSelectThinkingText)) &&
|
||||
/* 样文分析已有流式过程卡时,不再叠加 skeleton */
|
||||
!state.sampleAnalysisStreamingText && !state.sampleAnalysisThinkingText &&
|
||||
/* 意图解析流式过程中(LLM 还在吐 JSON)不要显示 skeleton */
|
||||
!state.intentStreamingText && !state.intentThinkingText && (
|
||||
<div className="px-4 pb-4">
|
||||
@@ -587,6 +697,7 @@ export function AIWritingPanel({
|
||||
{state.error}
|
||||
</div>
|
||||
)}
|
||||
<div ref={bottomRef} className="h-px" aria-hidden="true" />
|
||||
</div>{/* 滚动内容区结束 */}
|
||||
|
||||
{/* 回到底部悬浮按钮:生成中且用户已上划时出现 */}
|
||||
|
||||
Reference in new issue
Block a user