diff --git a/desktop/src/lib/previewEvents.test.ts b/desktop/src/lib/previewEvents.test.ts index 40594697..53b4088f 100644 --- a/desktop/src/lib/previewEvents.test.ts +++ b/desktop/src/lib/previewEvents.test.ts @@ -32,7 +32,7 @@ describe('subscribePreviewEvents', () => { const payload = { pageUrl: 'http://x/', element: { selector: '#t', tag: 'h1', classes: [] }, change: { description: '改一下' }, screenshot: { dataUrl: 'data:image/png;base64,AAAA', kind: 'element' } } listeners['preview://event']!({ payload: JSON.stringify({ v: 1, type: 'selection', payload }) }) expect(prefill).toHaveBeenCalledWith('s1', expect.objectContaining({ - text: expect.stringContaining('#t'), + text: expect.stringContaining('改一下'), attachments: [expect.objectContaining({ type: 'image', data: 'data:image/png;base64,AAAA' })], })) }) diff --git a/desktop/src/lib/selectionComposer.test.ts b/desktop/src/lib/selectionComposer.test.ts index 6b6d8262..dc9b06b6 100644 --- a/desktop/src/lib/selectionComposer.test.ts +++ b/desktop/src/lib/selectionComposer.test.ts @@ -1,15 +1,26 @@ import { expect, it } from 'vitest' import { buildSelectionComposerText } from './selectionComposer' -it('renders a readable block with locator + change + source', () => { +it('renders only the user instruction + concrete changes (no selector/DOM/page noise)', () => { 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('标题改积极一点') + expect(text).toContain('模型用熟了') + // selector / 页面 URL / 「请在源码中落地」 等 DOM 噪音不在(截图才是引用) + expect(text).not.toContain('#title') + expect(text).not.toContain('index.html') + expect(text).not.toContain('请在源码中落地') +}) + +it('returns empty text when there is no description or change (image alone is the reference)', () => { + const text = buildSelectionComposerText({ + pageUrl: 'http://x/', + element: { selector: '#t', tag: 'div', classes: [] } as never, + }) + expect(text).toBe('') }) diff --git a/desktop/src/lib/selectionComposer.ts b/desktop/src/lib/selectionComposer.ts index 68d87722..62356271 100644 --- a/desktop/src/lib/selectionComposer.ts +++ b/desktop/src/lib/selectionComposer.ts @@ -7,18 +7,20 @@ export type SelectionPayload = { change?: EditDiff & { description?: string } } +/** + * 选中元素的「引用」由随附的圈选标注截图承载(截图里已把元素圈出来)。 + * 这里只产出用户的修改意见 + 具体改动(人话),**不再**把 selector / DOM 定位 / + * computedStyles / 页面 URL 等写进输入框 —— 那些 DOM 噪音交互体验差,且图片已是引用。 + * 无描述、无改动时返回空串(让图片单独作为引用进输入框)。 + */ 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 + const lines: string[] = [] + if (c?.description) lines.push(c.description) 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') }