feat(sender): support pasteFilter prop for custom paste text cleaning - #2001
feat(sender): support pasteFilter prop for custom paste text cleaning#2001Y-MuYi wants to merge 1 commit into
Conversation
In slot mode, the default `getCleanedText` strips line breaks from pasted
text, making it impossible to preserve multi-line formatting (e.g. code
snippets). Add a `pasteFilter` prop so users can fully replace the default
cleaning:
const cleanedText = pasteFilter ? pasteFilter(text) : getCleanedText(text);
`pasteFilter` receives the raw pasted text and should return the final text
to insert. e.g. `pasteFilter={(t) => t}` keeps line breaks intact.
Slot mode only; the non-slot (native textarea) path is unchanged.
Closes ant-design#1946
Co-Authored-By: Claude <noreply@anthropic.com>
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
📝 WalkthroughWalkthroughSender 新增可选的 ChangesSender 粘贴过滤支持
Estimated code review effort: 2 (Simple) | ~10 minutes Sequence Diagram(s)sequenceDiagram
participant Sender
participant SenderContext
participant SlotTextArea
participant Browser
Sender->>SenderContext: 传递 pasteFilter
SenderContext->>SlotTextArea: 提供粘贴过滤配置
SlotTextArea->>SlotTextArea: 处理原始粘贴文本
SlotTextArea->>Browser: execCommand 插入过滤后文本
Possibly related PRs
Suggested labels: Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/x/components/sender/__tests__/slot.test.tsx (1)
671-690: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win补充粘贴后的最终值断言。
当前测试只检查
document.execCommand的第三个参数。它没有确认SlotTextArea的最终值仍包含换行。jest.fn()默认返回undefined,因此测试还会进入insertfallback。请增加getValue().value或onChange断言,并明确模拟execCommand成功或 fallback。这样可以覆盖粘贴后的实际结果。🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/x/components/sender/__tests__/slot.test.tsx` around lines 671 - 690, 增强“should apply pasteFilter that preserves line breaks”测试:明确让 document.execCommand 模拟成功,或显式覆盖并验证 insert fallback 路径;同时通过 SlotTextArea 的 getValue().value 或 onChange 断言粘贴后的最终值仍为包含换行的 “a\nb\nc”,不要只断言 execCommand 的参数。
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@packages/x/components/sender/__tests__/slot.test.tsx`:
- Around line 671-690: 增强“should apply pasteFilter that preserves line
breaks”测试:明确让 document.execCommand 模拟成功,或显式覆盖并验证 insert fallback 路径;同时通过
SlotTextArea 的 getValue().value 或 onChange 断言粘贴后的最终值仍为包含换行的 “a\nb\nc”,不要只断言
execCommand 的参数。
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 4b2b5bce-eccc-45c0-9b17-f1caca373500
📒 Files selected for processing (6)
packages/x/components/sender/Sender.tsxpackages/x/components/sender/__tests__/slot.test.tsxpackages/x/components/sender/components/SlotTextArea.tsxpackages/x/components/sender/demo/paste-filter.mdpackages/x/components/sender/demo/paste-filter.tsxpackages/x/components/sender/interface.ts
What
Add a
pasteFilterprop toSenderso users can customize how pasted text is cleaned in slot mode.Why
In slot mode, the default
getCleanedTextstrips all line breaks (use-cursor.ts→.replace(/\n/g, '')), so pasting multi-line text (code snippets, structured text) loses its formatting. Closes #1946.How
The new prop is wired through
Sender→SenderContext→SlotTextArea, and the paste handler uses it to replace the default cleaning:pasteFilterreceives the raw pasted text and should return the final text to insert. For example,pasteFilter={(t) => t}keeps the original line breaks that the default cleaning would strip.Slot mode only — the non-slot (native textarea) path is unchanged.
Changes
interface.ts: addpasteFilter?: (text: string) => string(+ JSDoc)Sender.tsx: passpasteFilterthrough context (destructure, value object, dependency array)SlotTextArea.tsx: apply the filter inonInternalPastedemo/paste-filter.tsx+.md: new demoslot.test.tsx: test thatpasteFilterreceives the raw text and that its return value is what gets insertedDemo
Summary by CodeRabbit
新功能
pasteFilter配置,支持自定义粘贴文本处理方式。文档