From 96a288425f4544b8b703dc0bb8e4380bfa0291ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E9=98=BF=E6=B1=9F=28Relakkes?= =?UTF-8?q?=29?= Date: Fri, 29 May 2026 13:29:59 +0800 Subject: [PATCH] feat(desktop): render selection payload into composer text Co-Authored-By: Claude Sonnet 4.6 --- desktop/src/lib/selectionComposer.test.ts | 15 ++++++++++++++ desktop/src/lib/selectionComposer.ts | 24 +++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 desktop/src/lib/selectionComposer.test.ts create mode 100644 desktop/src/lib/selectionComposer.ts diff --git a/desktop/src/lib/selectionComposer.test.ts b/desktop/src/lib/selectionComposer.test.ts new file mode 100644 index 00000000..6b6d8262 --- /dev/null +++ b/desktop/src/lib/selectionComposer.test.ts @@ -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('标题改积极一点') +}) diff --git a/desktop/src/lib/selectionComposer.ts b/desktop/src/lib/selectionComposer.ts new file mode 100644 index 00000000..68d87722 --- /dev/null +++ b/desktop/src/lib/selectionComposer.ts @@ -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') +}