Files
qiqi 8f1f7ea7a7
validate / validate (push) Canceled after 0s
fix(promptlib): v1.12.2 搜索框常驻(中文输入法能正常输入)+ 变量标签定宽 6 字
用户报障:① 变量名长短不一,输入框左边缘不齐;② 搜索框打不进中文。

② 的根因(有对照实验)
- 搜索框写在 render() 里,而 render() 第一件事是 clear(fixed) ⇒ 每敲一个键都把搜索框
  自身销毁重建。中文输入法在组合态(compositionstart → compositionend)被打断。
- 用 Playwright 跑旧版实测:组合态中搜框 connected=false、卡片被拼音过滤成 0 条;
  走 CDP Input.imeSetComposition 真实输入法通道时 value 变成 "shui水"(拼音与汉字拼接),
  正是用户看到的「打不进中文」。
修法
- 搜索框只创建一次(ensureSearchRow),render() 只清其余节点、把搜索行留在首位;
- compositionstart/end 之外不重排:组合态只记 query(拼音不是搜索词),上屏后过滤一次;
- 结果重排走 120ms 防抖(顺带解决逐键全量重建卡顿);清空搜索后焦点回到搜索框;
- dispose 清掉防抖定时器。

① 变量标签列定宽 76px(12px 字号 × 6 字 + 4px 余量)
- 此前 flex:0 0 auto,宽度跟着名称长短变 ⇒ 每行 textarea 左边缘错位;定宽后四行实测同为 508.66px
- 6 字名称完整显示,超 6 字省略号截断(完整名称在 title)
- 名称框 placeholder 改为「变量名称(节点上最多显示 6 字)」

验证
- 新增 tools/plp-search-ime-check.mjs:真实 Chromium 挂载面板,合成组合事件 + CDP 输入法双路,
  10 项断言(含「组合态不按拼音过滤」「光标不跳末尾」);同一脚本跑 HEAD 旧版失败 6 项 ⇒ 对照成立
- harness 61 → 66 项;宿主 plugin-promptlib-render-test 的搜索等待按 120ms 防抖改 200ms
- 宿主 test:unit 全绿;scale-contract / token-lint 通过
2026-11-24 18:16:10 +08:00

165 lines
7.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 搜索框中文输入法回归验证(v1.12.2 起)
// 为什么单独一个脚本:宿主 CI 里没有 playwright,跑不进 harness 门禁;但改搜索框时值得手动跑一次。
// 依赖宿主仓的 playwright:image-workbench-desktop/node_modules/playwright。
// 用法:node promptlib/tools/plp-search-ime-check.mjs
// PLP_SRC=/path/to/other/index.js node promptlib/tools/plp-search-ime-check.mjs # 对照旧版
// 覆盖:组合态中搜索框不被重建 / 不按拼音过滤 / 光标不跳末尾 / 上屏后过滤生效
// CDP Input.imeSetComposition 走真实输入法通道
// 验证 promptlib 搜索框:中文输入法组合态不再被打断(v1.12.2)
// 做法:真实 Chromium 加载插件源码 + 最小宿主 stub,挂载面板后
// ① 模拟 compositionstart → input(拼音) → compositionend(中文)
// ② 用 CDP Input.imeSetComposition 走真实输入法通道
import path from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url'
// playwright 装在宿主仓(插件仓没有 node_modules);按本级目录推,别写死绝对路径
const HERE = path.dirname(fileURLToPath(import.meta.url))
const PW = pathToFileURL(path.resolve(HERE, '..', '..', '..', 'image-workbench-desktop', 'node_modules', 'playwright', 'index.mjs')).href
const { chromium } = await import(PW)
const SRC = process.env.PLP_SRC || path.resolve(HERE, '..', 'src', 'index.js')
const browser = await chromium.launch()
const page = await browser.newPage({ viewport: { width: 900, height: 700 } })
const errors = []
page.on('pageerror', (e) => errors.push(String(e)))
await page.setContent('<!doctype html><html><body style="margin:0"><div id="host"></div></body></html>')
// 宿主 stub:捕获 register → 拿到 factory
await page.evaluate(() => {
window.IWBPluginHost = { register: (meta, factory) => { window.__reg = { meta, factory } } }
})
await page.addScriptTag({ path: SRC })
const mountInfo = await page.evaluate(() => {
const prompts = [
{ id: 'a', name: '水光肌底妆', text: '水光 底妆 提亮 通透', group: '美妆' },
{ id: 'b', name: '复古胶片', text: '胶片 颗粒 暖调 怀旧', group: '风格' },
{ id: 'c', name: '水果静物', text: '水果 静物 清新', group: '美妆' },
{ id: 'd', name: '水波纹背景', text: '水波 纹理 背景', group: '风格' },
]
const groups = [{ id: '美妆', name: '美妆' }, { id: '风格', name: '风格' }]
const data = { schemaVersion: 3, prompts, groups }
const ctx = {
data: {
read: async () => data,
write: async () => ({ ok: true }),
set: async () => ({ ok: true }),
get: async () => null,
},
promptlib: {
list: async () => ({ prompts, groups }),
save: async () => ({ ok: true }),
insert: async () => ({ ok: true }),
notify: () => {},
onChanged: () => () => {},
filePut: async () => ({ ok: true }),
fileGet: async () => '',
fileList: async () => [],
fileRemove: async () => ({ ok: true }),
fileStat: async () => ({ count: 0, bytes: 0 }),
},
files: {
put: async () => ({ ok: true }),
get: async () => '',
list: async () => [],
remove: async () => ({ ok: true }),
stat: async () => ({ count: 0, bytes: 0 }),
},
storage: { set: () => {}, get: () => null },
ui: { registerPanel: (p) => { window.__panel = p }, toast: () => {} },
}
window.__reg.factory(ctx)
const host = document.getElementById('host')
window.__dispose = window.__panel.mount(host)
return { meta: window.__reg.meta.version, hasPanel: !!window.__panel }
})
console.log('插件版本', mountInfo.meta, '面板已注册', mountInfo.hasPanel)
await page.waitForTimeout(600)
const countCards = () => page.evaluate(() => document.querySelectorAll('.iwb-plp-card').length)
const before = await countCards()
console.log('初始卡片数', before)
// ---- ① 合成事件:组合态期间搜索框不能被重建、列表不能按拼音过滤 ----
const synth = await page.evaluate(async () => {
const host = document.getElementById('host')
const el = host.querySelector('input.iwb-grow')
el.__mark = 'same-node'
el.focus()
el.setSelectionRange(2, 2)
const fire = (type, extra) => el.dispatchEvent(new (type === 'input' ? InputEvent : CompositionEvent)(type, Object.assign({ bubbles: true }, extra || {})))
fire('compositionstart', { data: '' })
// 拼音逐个上屏:真实输入法每敲一个字母都会派发 isComposing=true 的 input
el.value = '水'
fire('input', { isComposing: true, data: 'shui' })
el.value = '水s'
fire('input', { isComposing: true, data: 'shuis' })
await new Promise((r) => setTimeout(r, 250)) // 超过 120ms 防抖窗口
const during = {
sameNode: host.querySelector('input.iwb-grow') === el,
connected: el.isConnected,
mark: el.__mark,
cards: document.querySelectorAll('.iwb-plp-card').length,
selStart: el.selectionStart,
}
// 上屏
el.value = '水'
el.dispatchEvent(new CompositionEvent('compositionend', { bubbles: true, data: '水' }))
await new Promise((r) => setTimeout(r, 300))
const after = {
sameNode: host.querySelector('input.iwb-grow') === el,
value: el.value,
cards: document.querySelectorAll('.iwb-plp-card').length,
names: Array.from(document.querySelectorAll('.iwb-plp-t')).map((n) => n.textContent),
}
return { during, after }
})
console.log('组合态中 :', JSON.stringify(synth.during))
console.log('上屏之后 :', JSON.stringify(synth.after))
// ---- ② CDP 真实输入法通道 ----
const cdp = await page.context().newCDPSession(page)
await page.evaluate(() => { const el = document.querySelector('#host input.iwb-grow'); el.value = ''; el.focus() })
await cdp.send('Input.imeSetComposition', { text: 'shui', selectionStart: 4, selectionEnd: 4 })
await page.waitForTimeout(120)
const composingState = await page.evaluate(() => document.querySelector('#host input.iwb-grow').value)
await cdp.send('Input.insertText', { text: '水' })
await page.waitForTimeout(300)
const imeState = await page.evaluate(() => ({
value: document.querySelector('#host input.iwb-grow').value,
cards: document.querySelectorAll('.iwb-plp-card').length,
names: Array.from(document.querySelectorAll('.iwb-plp-t')).map((n) => n.textContent),
}))
console.log('imeSetComposition 后 value =', JSON.stringify(composingState))
console.log('真实输入法提交后 :', JSON.stringify(imeState))
// ---- 判定 ----
const checks = [
['组合态中搜索框未被重建(同一节点)', synth.during.sameNode === true],
['组合态中搜索框仍在文档里(没被移除)', synth.during.connected === true],
['组合态中保留自定义属性(未被克隆替换)', synth.during.mark === 'same-node'],
['组合态中不按拼音过滤(卡片数不变)', synth.during.cards === before],
['组合态中光标位置不被打到末尾', synth.during.selStart === 2],
['上屏后搜索框仍是同一节点', synth.after.sameNode === true],
['上屏后搜索框值 = 水', synth.after.value === '水'],
['上屏后过滤到 3 条含水的结果', synth.after.cards === 3],
['真实输入法:组合态拼音留在框内', composingState === 'shui'],
['真实输入法:提交后可搜到 3 条', imeState.cards === 3],
]
let failed = 0
for (const [name, okv] of checks) { if (!okv) failed++; console.log((okv ? '✓ ' : '✗ ') + name) }
if (errors.length) { console.log('页面异常:', errors.slice(0, 3)) }
console.log(failed ? `✗ ${failed} 项失败` : '✓ 全部通过')
await page.screenshot({ path: '/tmp/plp-search.png' })
await browser.close()
process.exit(failed ? 1 : 0)