From e914d6a508114cc12c80065e73cfe083c6bcaf67 Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Mon, 30 Nov 2026 18:12:27 +0800 Subject: [PATCH 1/2] fix(agents): edit user messages in place without layout jump --- src/features/agents/Session.module.css | 139 +++++++++- src/features/agents/messageEditing.test.tsx | 28 ++ src/features/agents/session/messages.tsx | 278 ++++++++++++-------- 3 files changed, 317 insertions(+), 128 deletions(-) diff --git a/src/features/agents/Session.module.css b/src/features/agents/Session.module.css index 7a852ecfa7..60e8b2e7a5 100644 --- a/src/features/agents/Session.module.css +++ b/src/features/agents/Session.module.css @@ -28,9 +28,15 @@ .fitWidth .stream > * { width: 100%; } -.fitWidth .userMessage > .user { +.fitWidth .userMessage > .user, +.fitWidth .userMessage > .editCard, +.fitWidth .userMessage > .editNote, +.fitWidth .userMessage > .editError { max-width: var(--session-card-width); } +.fitWidth .userMessage > .editCard { + width: var(--edit-width, var(--session-card-width)); +} .fitWidth .composerArea { max-width: none; } @@ -338,6 +344,15 @@ width: fit-content; max-width: min(var(--session-card-width), 720px); } +.userMessage > .editCard { + width: var(--edit-width, min(var(--session-card-width), 720px)); + min-width: min(100%, 220px); + max-width: min(var(--session-card-width), 720px); +} +.userMessage > .editNote, +.userMessage > .editError { + max-width: min(var(--session-card-width), 720px); +} .userMessage > .messageHead { justify-content: flex-end; } @@ -518,33 +533,131 @@ overflow-wrap: anywhere; } -.editMessage { - width: 100%; +/* In-place bubble editor: matches the Islands composer frame and keeps the text origin fixed. */ +.editCard { box-sizing: border-box; - padding: 6px 8px; - resize: vertical; + display: flex; + flex-direction: column; + border-radius: var(--radius-tab); + background: var(--bg-elev); + box-shadow: inset 0 0 0 1px var(--border-popup); + cursor: text; + transition: + background var(--dur-fast) var(--ease), + box-shadow var(--dur-fast) var(--ease); +} +.editCard:focus-within { + background: var(--bg-field, var(--bg-button)); + box-shadow: + inset 0 0 0 1px var(--focus-ring), + 0 0 0 1px var(--focus-ring); +} +@media (prefers-reduced-motion: reduce) { + .editCard { + transition: none; + } +} + +.editField { + display: grid; + grid-template-columns: minmax(0, 1fr); + min-width: 0; + max-height: 280px; +} + +.editMirror, +.editMessage { + grid-area: 1 / 1; + min-width: 0; + margin: 0; + padding: 6px 10px 4px; + border: 0; font: inherit; + line-height: 1.4; + white-space: pre-wrap; + overflow-wrap: anywhere; + box-sizing: border-box; +} + +.editMirror { + visibility: hidden; + pointer-events: none; + max-height: 280px; + overflow: hidden; + -webkit-user-select: none; + user-select: none; +} + +.editMessage { + width: 100%; + height: 100%; + min-height: 0; + overflow-y: auto; + resize: none; + background: transparent; color: var(--fg-strong); - background: var(--bg-input); - border: 1px solid var(--accent); - border-radius: var(--radius-control); + caret-color: var(--fg-strong); outline: none; } -.editBar { +.editImages { + padding: 0 10px 4px; +} +.editImages .images { + margin-top: 0; +} + +.editFooter { display: flex; align-items: center; + justify-content: space-between; gap: var(--space-2); - margin-top: 6px; - font-size: var(--fs-small); + padding: 2px 6px 6px 10px; + cursor: default; +} + +.editHint { + display: inline-flex; + align-items: center; + gap: 2px; + color: var(--fg-faint); + font-size: var(--fs-tiny, 11px); + -webkit-user-select: none; + user-select: none; +} +.editHint kbd { + display: inline-grid; + place-items: center; + min-width: 16px; + height: 16px; + padding: 0 3px; + border-radius: var(--radius-small); + background: var(--bg-hover); + color: var(--fg-muted); + font-family: var(--font-keys, inherit); + font-size: var(--fs-tiny, 11px); } .editButtons { - display: flex; - gap: var(--space-1-5); + display: inline-flex; + align-items: center; + gap: var(--space-1); margin-left: auto; } +.editError { + margin-top: var(--space-1-5); + font-size: var(--fs-small); +} + +.editNote { + margin-top: var(--space-1); + color: var(--fg-subtle); + font-size: var(--fs-small); + line-height: 1.35; + text-align: right; +} + .markdown { line-height: 1.4; overflow-wrap: anywhere; diff --git a/src/features/agents/messageEditing.test.tsx b/src/features/agents/messageEditing.test.tsx index 3bf86210c9..1be1da90c5 100644 --- a/src/features/agents/messageEditing.test.tsx +++ b/src/features/agents/messageEditing.test.tsx @@ -44,6 +44,7 @@ vi.mock("@tauri-apps/api/webview", () => ({ })); import { SessionEditor } from "./SessionEditor"; +import styles from "./Session.module.css"; import { textOf, typeIn } from "./composerTesting"; import { registerActions } from "@/platform/actions"; import { composerDraft, rollBackTo, rerunFrom, sendMessage, updateComposer, useAgents } from "./store"; @@ -463,6 +464,33 @@ describe("editing a sent agent message", () => { expect(textOf(screen.getAllByRole("textbox", { name: "Message" })[0]!)).toBe("Original message"); }, ); + + it("edits in place inside the user message header and preserves the bubble width", async () => { + const { container } = render( + , + ); + const user = userEvent.setup(); + const editButton = await screen.findByRole("button", { name: "Edit" }); + const bubble = container.querySelector(`.${styles.user}`)!; + vi.spyOn(bubble, "getBoundingClientRect").mockReturnValue(new DOMRect(100, 50, 272, 32)); + const focusSpy = vi.spyOn(HTMLTextAreaElement.prototype, "focus"); + try { + await user.click(editButton); + const message = container.querySelector(`.${styles.userMessage}`)!; + expect(message).toBeInTheDocument(); + expect(message.querySelector(`.${styles.messageHead}`)).toBeInTheDocument(); + expect(message.querySelector(`.${styles.role}`)).toHaveTextContent("You"); + const card = message.querySelector(`.${styles.editCard}`)!; + expect(card).toBeInTheDocument(); + expect(card.style.getPropertyValue("--edit-width")).toBe("272px"); + const box = screen.getAllByRole("textbox", { name: "Message" })[0] as HTMLTextAreaElement; + expect(focusSpy).toHaveBeenCalledWith({ preventScroll: true }); + expect(box.selectionStart).toBe("Original message".length); + expect(box.selectionEnd).toBe("Original message".length); + } finally { + focusSpy.mockRestore(); + } + }); }); describe("session composer submission", () => { diff --git a/src/features/agents/session/messages.tsx b/src/features/agents/session/messages.tsx index 53b7d6b09f..a99cf30f37 100644 --- a/src/features/agents/session/messages.tsx +++ b/src/features/agents/session/messages.tsx @@ -2,11 +2,12 @@ import type { AgentEntry, AgentImage, AgentTask } from "@/bindings"; import { useTx } from "@/platform/i18n"; import { isImeKey } from "@/platform/ime"; import { call, commands, isTauri } from "@/platform/ipc"; +import { isMacHost } from "@/platform/keymap"; import { basename } from "@/platform/project"; import { Button, copyText, fileIconName, Icon, IconButton, T } from "@/ui"; import { notifications } from "@/workbench/contributions"; import clsx from "clsx"; -import { memo, useLayoutEffect, useRef, useState, type RefObject } from "react"; +import { memo, useLayoutEffect, useRef, useState, type CSSProperties, type RefObject } from "react"; import { AgentReply, MessageActions, ReplyActions } from "../AgentReply"; import { CliSignInDialog } from "@/workbench/CliSignInDialog"; import { ImageStrip } from "../ImageStrip"; @@ -287,6 +288,8 @@ function UserMessage({ }) { const tx = useTx(); const [editing, setEditing] = useState(false); + const [editWidth, setEditWidth] = useState(null); + const bubbleRef = useRef(null); if (entry.recovery) { return (
@@ -310,20 +313,12 @@ function UserMessage({ // A copy up to an earlier turn drops the later ones from the copied session. const canFork = idle && fork && (rollback || turn === turnCount - 1); const text = userMessageText(entry.text); - if (editing && turn !== null) { - return ( - setEditing(false)} - onSend={async (text, images) => { - if (await rerunFrom(task.id, turn, text, images)) setEditing(false); - }} - /> - ); - } const images = entry.images ?? []; + const startEditing = () => { + const width = bubbleRef.current?.getBoundingClientRect().width ?? 0; + setEditWidth(width > 0 ? width : null); + setEditing(true); + }; // Images are listed after the files in `attachments` and shown as thumbnails instead. // Files mentioned in the text are chips there already. const mentioned = new Set( @@ -337,67 +332,80 @@ function UserMessage({
{/* Actions and the time sit beside the author on hover: the reply under it has the turn's duration. */}
- - void copyText(text)} /> - {inPlace && entry.text && ( - setEditing(true)} /> - )} - {inPlace && ( - - )} - {!archived && entry.text && entry.attachments.length === 0 && ( - - void ( - inPlace - ? rerunFrom(task.id, turn, entry.text, images, { - title: "Run Again From Here", - later: turn < turnCount - 1, - }) - : resendMessage(task.id, entry.text) - ).catch(fail) - } - /> - )} - {canFork && ( - void forkTask(task.id, turn + 1).catch(failed("Couldn't fork the task"))} - /> - )} - + {!(editing && turn !== null) && ( + + void copyText(text)} /> + {inPlace && entry.text && } + {inPlace && ( + + )} + {!archived && entry.text && entry.attachments.length === 0 && ( + + void ( + inPlace + ? rerunFrom(task.id, turn, entry.text, images, { + title: "Run Again From Here", + later: turn < turnCount - 1, + }) + : resendMessage(task.id, entry.text) + ).catch(fail) + } + /> + )} + {canFork && ( + void forkTask(task.id, turn + 1).catch(failed("Couldn't fork the task"))} + /> + )} + + )} You
-
- - - {files.length > 0 && ( -
- {files.map((a, i) => ( - // oxlint-disable-next-line react/no-array-index-key -- attachments from different directories can share a basename - - - {a} - - ))} -
- )} -
+ {editing && turn !== null ? ( + setEditing(false)} + onSend={async (nextText, nextImages) => { + if (await rerunFrom(task.id, turn, nextText, nextImages)) setEditing(false); + }} + /> + ) : ( +
+ + + {files.length > 0 && ( +
+ {files.map((a, i) => ( + // oxlint-disable-next-line react/no-array-index-key -- attachments from different directories can share a basename + + + {a} + + ))} +
+ )} +
+ )}
); } @@ -406,12 +414,15 @@ function UserMessage({ function MessageEditor({ initial, initialImages, + initialWidth, later, onCancel, onSend, }: { initial: string; initialImages: AgentImage[]; + /** Measured width of the bubble being replaced, so editing starts in place without jumping. */ + initialWidth: number | null; /** Turns follow it (they are dropped on send). */ later: boolean; onCancel: () => void; @@ -423,6 +434,25 @@ function MessageEditor({ const [sending, setSending] = useState(false); const [error, setError] = useState(null); const pending = useRef(false); + const cardRef = useRef(null); + const textareaRef = useRef(null); + useLayoutEffect(() => { + const box = textareaRef.current; + if (!box) return; + // Avoid native autoFocus scroll jumps; only nudge if the expanded footer sits past the bottom edge. + box.focus({ preventScroll: true }); + box.setSelectionRange(box.value.length, box.value.length); + const card = cardRef.current; + const scroller = card?.closest(`.${styles.stream}`); + if (card && scroller instanceof HTMLElement) { + const view = scroller.getBoundingClientRect(); + const rect = card.getBoundingClientRect(); + if (rect.bottom > view.bottom) { + const nudge = Math.min(rect.bottom - view.bottom + 8, Math.max(0, rect.top - view.top)); + if (nudge > 0) scroller.scrollTop += nudge; + } + } + }, []); const send = async () => { if ((!text.trim() && images.length === 0) || pending.current) return; pending.current = true; @@ -442,59 +472,77 @@ function MessageEditor({ setSending(false); } }; + const cardStyle = initialWidth ? ({ "--edit-width": `${initialWidth}px` } as CSSProperties) : undefined; return ( -
-