From 317c8557f4325a4eb2737cf611ce077aee51e8e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=BE=B7=E5=AE=87?= Date: Mon, 21 Sep 2026 11:59:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=BE=93=E5=85=A5=E5=8C=BA?= =?UTF-8?q?=E5=8F=97=E6=8E=A7=E5=9B=9E=E5=86=99=E7=94=A8=E6=BB=9E=E5=90=8E?= =?UTF-8?q?=E6=B8=B2=E6=9F=93=E7=9A=84=20props=20=E6=B8=85=E7=A9=BA?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E8=8D=89=E7=A8=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - apps/ai-game-creator-shell/src/features/project-workspace/ResourceReferenceInput.tsx 的受控同步由 useLayoutEffect 承担:宿主对草稿的回显晚于编辑器提交,被动 effect 可能在上一次渲染的 props 上延迟执行,读到旧 value 加新编辑器状态后重建整份草稿,把刚输入的内容清掉并形成来回清空 - 补注释说明该 effect 必须与本次提交同帧执行,以及直接改成 content 单事实源前的行为约束 --- .../project-workspace/ResourceReferenceInput.tsx | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/apps/ai-game-creator-shell/src/features/project-workspace/ResourceReferenceInput.tsx b/apps/ai-game-creator-shell/src/features/project-workspace/ResourceReferenceInput.tsx index a52d65afe..4c385b91c 100644 --- a/apps/ai-game-creator-shell/src/features/project-workspace/ResourceReferenceInput.tsx +++ b/apps/ai-game-creator-shell/src/features/project-workspace/ResourceReferenceInput.tsx @@ -41,6 +41,7 @@ import { useCallback, useEffect, useImperativeHandle, + useLayoutEffect, useMemo, useRef, useState, @@ -874,7 +875,13 @@ function ResourceReferenceEditor({ // 受控聊天入口在清空/恢复草稿时同步编辑器;普通输入变更若已与当前内容一致则不重建, // 避免每个按键都把光标跳到末尾。 - useEffect(() => { + // + // 这里必须是 layout effect:`value` / `references` 只是宿主对编辑器草稿的回显,宿主状态 + // 更新会晚于编辑器的 Lexical 提交。用被动 effect 时,一次滞后渲染的回调可能在用户已经 + // 继续输入之后才执行,它读到的是旧 `value` 加新编辑器状态,就会把整份草稿重建掉 + // (编辑器再通过 onChange 把空草稿写回宿主,来回清空)。layout effect 与本次提交同帧执行, + // 读到的 props 与编辑器状态属于同一次提交。 + useLayoutEffect(() => { if (value === undefined && references === undefined) return; const desiredValue = value ?? ''; const desiredRefs = references ?? [];