feat(desktop): render selection payload into composer text

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
程序员阿江(Relakkes) 2026-05-29 13:29:59 +08:00
parent 14e7228e33
commit 96a288425f
2 changed files with 39 additions and 0 deletions

View File

@ -0,0 +1,15 @@
import { expect, it } from 'vitest'
import { buildSelectionComposerText } from './selectionComposer'
it('renders a readable block with locator + change + source', () => {
const text = buildSelectionComposerText({
pageUrl: 'http://127.0.0.1:4321/preview-fs/s1/index.html',
sourceHint: 'index.html',
element: { selector: '#title', tag: 'h1', text: '模型还没用熟', classes: [] } as never,
change: { text: { from: '模型还没用熟', to: '模型用熟了' }, description: '标题改积极一点' } as never,
})
expect(text).toContain('#title')
expect(text).toContain('index.html')
expect(text).toContain('模型用熟了')
expect(text).toContain('标题改积极一点')
})

View File

@ -0,0 +1,24 @@
import type { ElementMetadata } from '../preview-agent/metadata'
import type { EditDiff } from '../preview-agent/popover'
export type SelectionPayload = {
pageUrl: string; sourceHint?: string
element: ElementMetadata
change?: EditDiff & { description?: string }
}
export function buildSelectionComposerText(p: SelectionPayload): string {
const lines: string[] = []
lines.push(`针对页面元素 \`${p.element.selector}\`${p.element.tag}${p.element.text ? `,文本「${p.element.text}` : ''}`)
if (p.sourceHint) lines.push(`- 来源:${p.sourceHint}`)
lines.push(`- 页面:${p.pageUrl}`)
const c = p.change
if (c?.text) lines.push(`- 文本:「${c.text.from}」→「${c.text.to}`)
if (c?.color) lines.push(`- 文字颜色:${c.color.from}${c.color.to}`)
if (c?.background) lines.push(`- 背景:${c.background.from}${c.background.to}`)
if (c?.opacity) lines.push(`- 不透明度:${c.opacity.from}${c.opacity.to}`)
if (c?.fontFamily) lines.push(`- 字体:${c.fontFamily.from}${c.fontFamily.to}`)
if (c?.description) lines.push(`- 说明:${c.description}`)
lines.push('请在源码中落地以上修改。')
return lines.join('\n')
}