Skip to content

fix(playground): resolve cursor jump, implement image-only upload, fix textarea stretch - #1

Open
MIt-gancm wants to merge 2 commits into
mainfrom
fix/playground-editor-imageupload-textarea
Open

fix(playground): resolve cursor jump, implement image-only upload, fix textarea stretch#1
MIt-gancm wants to merge 2 commits into
mainfrom
fix/playground-editor-imageupload-textarea

Conversation

@MIt-gancm

Copy link
Copy Markdown
Owner

变更描述

三个修复覆盖了 Playground 聊天输入区域的问题:

1. 修复编辑历史对话时光标跳回开头

问题: 当用户编辑历史消息时,每次输入一个字符光标就会跳转到文本开头。

根因: PlaygroundChatuseEffect 的依赖项 [editingKey, messages] 中,messages 数组引用在每次父组件重渲染(包括用户输入触发 setState)时都会变成新引用,导致 effect 重复执行并重置 editText 为原始消息内容。

修复: 使用 useRef 追踪上一次的 editingKey,仅当 editingKey 实际变化(用户选择编辑另一条消息)时才重新初始化编辑内容,忽略 messages 引用变化带来的误触发。

2. 实现图片上传(仅允许图片格式)

问题: 附件按钮(Paperclip 图标)的四个菜单项全部只显示 "Feature in development" 的 toast,完全不可用。

修复:

  • 去掉了占位下拉菜单,改为直接点击图片图标即可触发文件选择器
  • PromptInput 添加 accept="image/*" 限制只能选择图片文件
  • 支持多选上传(multiple
  • 复用已有的 PromptInput 附件系统(缩略图显示、悬停预览、可删除、支持拖拽粘贴)

3. 修复输入框内容过长时向右拉伸

问题: 输入长无空格文本(如 URL)时 textarea 会横向无限拉伸。

根因: Textarea 使用 field-sizing-content 自动调整尺寸,但缺少文本换行控制。

修复: 添加 overflow-x-hidden break-words CSS 类,强制长文本在达到 textarea 宽度时换行。


此 PR 变更由 AI 辅助生成。

变更类型

  • 🐛 Bug 修复
  • ✨ 新功能

Checklist

  • 变更聚焦于当前任务范围
  • 基于对代码逻辑的理解撰写此描述
  • 代码中无敏感凭据

…x textarea horizontal stretch

Three fixes for the playground chat input area:

1. Fix cursor jumping to beginning when editing historical messages
   - Root cause: useEffect depended on [editingKey, messages], and messages array reference changed on every parent re-render, causing editText to be reset to original content on each keystroke
   - Fix: track previous editing key with useRef to avoid re-initializing editText when only messages reference changes

2. Implement image-only attachment upload
   - Removed placeholder dropdown with unimplemented actions (upload-file, upload-photo, take-screenshot, take-photo)
   - Added direct image upload button using PromptInput's built-in attachment system
   - Restricted to image/* via accept prop on PromptInput
   - Supports multi-select and drag-drop

3. Fix input textarea stretching right with long content
   - Added overflow-x-hidden break-words to textarea className
   - Prevents horizontal expansion when long unbroken text (URLs, etc.) is entered
…cursor jump

- Pass image URLs from PromptInput through PlaygroundInput.handleSubmit
  into handleSendMessage, createUserMessage, and formatMessageForAPI
- Add imageUrls field to Message type for storing uploaded image data URLs
- formatMessageForAPI now returns ContentPart[] when message has images
- Allow submit when only images are present without text
- Show attachment preview chips (PromptInputHeader + PromptInputAttachments)
  in the playground input area

- Fix CodeMirror cursor jumping: use lastDispatchedValueRef to track the
  last value emitted by CodeMirror's own update listener, skip re-dispatch
  when React sends the same value back via the prop, preserving cursor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant