Skip to content

Commit e25cb9e

Browse files
committed
fix(ui): 优化历史导航和粘贴处理逻辑
- 在历史导航中同步捕获当前草稿,避免异步状态不一致 - 修正使用最新捕获草稿替代旧状态以设置缓冲区文本 - 在粘贴处理钩子中引入 useEffect 钩子以响应缓冲区文本变化 - 通过副作用保持衍生标志状态与缓冲区文本同步 - 移除潜在的异步更新问题,提升用户交互体验
1 parent d65260e commit e25cb9e

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

src/ui/hooks/useHistoryNavigation.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ export function useHistoryNavigation(
3838

3939
const previousCursor = historyCursor === -1 ? promptHistory.length : historyCursor;
4040
const nextCursor = Math.max(0, Math.min(promptHistory.length, previousCursor + direction));
41+
// Capture the current draft synchronously before `setDraftBeforeHistory`.
42+
const draft = historyCursor === -1 ? buffer.text : draftBeforeHistory;
4143

4244
if (historyCursor === -1) {
4345
setDraftBeforeHistory(buffer.text);
4446
}
4547

4648
if (nextCursor === promptHistory.length) {
47-
const text = draftBeforeHistory ?? "";
49+
const text = draft ?? "";
4850
setBuffer({ text, cursor: text.length });
4951
setHistoryCursor(-1);
5052
setDraftBeforeHistory(null);

src/ui/hooks/usePasteHandling.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type React from "react";
2-
import { useRef, useState } from "react";
2+
import { useEffect, useRef, useState } from "react";
33
import type { PromptBufferState } from "../core/prompt-buffer";
44
import { cleanPasteContent, findPasteMarkerContaining, hasActivePasteMarkers, insertText } from "../core/prompt-buffer";
55

@@ -51,6 +51,13 @@ export function usePasteHandling(
5151
setHasExpandedRegions(expandedRegionsRef.current.size > 0);
5252
}
5353

54+
// Recompute derived flags whenever the buffer text changes, so they stay
55+
// in sync after any state update (e.g. large paste, expand/collapse, undo).
56+
useEffect(() => {
57+
refreshDerivedFlags();
58+
// eslint-disable-next-line react-hooks/exhaustive-deps
59+
}, [buffer.text]);
60+
5461
function handlePaste(pastedText: string): void {
5562
const totalChars = pastedText.length;
5663

0 commit comments

Comments
 (0)