用户报障:① 变量名长短不一,输入框左边缘不齐;② 搜索框打不进中文。 ② 的根因(有对照实验) - 搜索框写在 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 通过
This commit is contained in:
1 parent
48d86f3e0f
commit
8f1f7ea7a7
4 files changed
+234
-19
No files matched your search
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@iwb-plugin/promptlib",
|
||||
"private": true,
|
||||
"version": "1.12.1",
|
||||
"version": "1.12.2",
|
||||
"description": "提示词库:右栏提示词列表(搜索/分组/收藏/新建编辑/插入画布/复制/删除),数据由宿主 promptlib 能力提供",
|
||||
"ibwPlugin": {
|
||||
"id": "promptlib",
|
||||
|
||||
+49
-13
@@ -1972,19 +1972,39 @@
|
||||
},
|
||||
})
|
||||
}
|
||||
function render () {
|
||||
clear(fixed); clear(scrollBox)
|
||||
var gs = groupsOf()
|
||||
|
||||
// 搜索行
|
||||
var top = el('div', { class: 'iwb-row' })
|
||||
var search = el('input', { class: 'iwb-input iwb-grow', placeholder: '搜索名称 / 内容 / 标签…', value: query })
|
||||
search.addEventListener('input', function () { query = search.value; render(); var n = wrap.querySelector('input.iwb-input'); if (n) { n.focus(); try { n.setSelectionRange(n.value.length, n.value.length) } catch (e) { /* ignore */ } } })
|
||||
// ---- v1.12.2:搜索框常驻 ----
|
||||
// 旧实现把搜索框写在 render() 里,而 render() 第一件事是 clear(fixed) —— 每敲一个键都把
|
||||
// 搜索框自身销毁重建。英文/数字勉强能用(重建后重新 focus、光标扔到末尾),但中文输入法
|
||||
// 在组合态(compositionstart → compositionend)被打断 ⇒ 拼音打不出字、候选词消失。
|
||||
// 现在:搜索框只创建一次;render() 只重建其余节点;组合态期间不重排,只记 query。
|
||||
var searchRow = null
|
||||
var searchEl = null
|
||||
var composing = false
|
||||
var searchTimer = 0
|
||||
function scheduleResults () {
|
||||
if (searchTimer) clearTimeout(searchTimer)
|
||||
searchTimer = setTimeout(function () { searchTimer = 0; render() }, 120)
|
||||
}
|
||||
function ensureSearchRow () {
|
||||
if (searchRow) return searchRow
|
||||
searchRow = el('div', { class: 'iwb-row' })
|
||||
searchEl = el('input', { class: 'iwb-input iwb-grow', placeholder: '搜索名称 / 内容 / 标签…' })
|
||||
searchEl.addEventListener('compositionstart', function () { composing = true })
|
||||
searchEl.addEventListener('compositionend', function () {
|
||||
composing = false
|
||||
query = searchEl.value
|
||||
scheduleResults()
|
||||
})
|
||||
searchEl.addEventListener('input', function (e) {
|
||||
query = searchEl.value
|
||||
// 组合中的拼音不是搜索词:不重排,等上屏后由 compositionend 触发一次
|
||||
if (composing || (e && e.isComposing)) return
|
||||
scheduleResults()
|
||||
})
|
||||
// v1.10.0:顶部三入口 = 文件(从分享文件导入)/ 文本(从剪贴板导入)/ 新建。
|
||||
var fileBtn = el('button', { class: 'iwb-btn iwb-btn-sm', title: '从分享文件导入词例', text: '文件' })
|
||||
var textBtn = el('button', { class: 'iwb-btn iwb-btn-sm', title: '从剪贴板导入词例', text: '文本' })
|
||||
var newBtn = el('button', { class: 'iwb-btn iwb-btn-sm iwb-btn-primary', title: '新建词例', text: '新建' })
|
||||
|
||||
fileBtn.addEventListener('click', function () {
|
||||
fileBtn.disabled = true
|
||||
importShareFromFile().then(function (r) {
|
||||
@@ -2002,14 +2022,29 @@
|
||||
}).catch(function (e) { textBtn.disabled = false; notify('导入失败:' + ((e && e.message) || e), 'err') })
|
||||
})
|
||||
newBtn.addEventListener('click', function () { openEditModal(null, true) })
|
||||
top.appendChild(search); top.appendChild(fileBtn); top.appendChild(textBtn); top.appendChild(newBtn)
|
||||
fixed.appendChild(top)
|
||||
searchRow.appendChild(searchEl); searchRow.appendChild(fileBtn)
|
||||
searchRow.appendChild(textBtn); searchRow.appendChild(newBtn)
|
||||
return searchRow
|
||||
}
|
||||
|
||||
function render () {
|
||||
// 搜索行必须留下:清掉它会让输入法组合中断、光标丢失(中文打不进字的直接原因)
|
||||
var row = ensureSearchRow()
|
||||
Array.prototype.slice.call(fixed.children).forEach(function (c) { if (c !== row) fixed.removeChild(c) })
|
||||
if (fixed.firstChild !== row) fixed.insertBefore(row, fixed.firstChild)
|
||||
if (searchEl && !composing && searchEl.value !== query) searchEl.value = query
|
||||
clear(scrollBox)
|
||||
var gs = groupsOf()
|
||||
|
||||
|
||||
if (words().length) {
|
||||
var scope = el('div', { class: 'iwb-lib-searchscope-line' },
|
||||
el('span', { class: 'iwb-lib-searchscope', title: '搜索会跨全部分组进行' }, '在全部中搜索'),
|
||||
el('button', { class: 'iwb-lib-searchscope-back', title: '清空搜索,回到分组筛选', text: '×' }))
|
||||
scope.querySelector('.iwb-lib-searchscope-back').addEventListener('click', function () { query = ''; render() })
|
||||
scope.querySelector('.iwb-lib-searchscope-back').addEventListener('click', function () {
|
||||
query = ''; render()
|
||||
if (searchEl) { try { searchEl.focus() } catch (e) { /* ignore */ } }
|
||||
})
|
||||
fixed.appendChild(scope)
|
||||
}
|
||||
|
||||
@@ -2108,6 +2143,7 @@
|
||||
load()
|
||||
|
||||
return function dispose () {
|
||||
if (searchTimer) { clearTimeout(searchTimer); searchTimer = 0 }
|
||||
document.removeEventListener('keydown', onKey, true)
|
||||
try { if (typeof off === 'function') off() } catch (e) { /* ignore */ }
|
||||
closeMenu(); closeEditor(); closeManager()
|
||||
@@ -2117,7 +2153,7 @@
|
||||
global.IWBPluginHost.register({
|
||||
id: 'promptlib',
|
||||
name: '提示词库',
|
||||
version: '1.12.1',
|
||||
version: '1.12.2',
|
||||
apiVersion: 1,
|
||||
capabilities: ['panels', 'promptlib', 'nodeTypes', 'canvas', 'canvasWrite'],
|
||||
data: { schemaVersion: 3, maxBytes: 4 * 1024 * 1024 },
|
||||
|
||||
@@ -20,9 +20,9 @@ const ok = (name, cond) => {
|
||||
}
|
||||
|
||||
// ---- 1) 版本 / manifest ----
|
||||
ok("manifest.version = 1.12.1", /version:\s*'1\.12\.1'/.test(src))
|
||||
ok('package.json version = 1.12.1',
|
||||
JSON.parse(fs.readFileSync(path.join(ROOT, 'package.json'), 'utf8')).version === '1.12.1')
|
||||
ok("manifest.version = 1.12.2", /version:\s*'1\.12\.2'/.test(src))
|
||||
ok('package.json version = 1.12.2',
|
||||
JSON.parse(fs.readFileSync(path.join(ROOT, 'package.json'), 'utf8')).version === '1.12.2')
|
||||
ok('manifest 已移除「分享时附带封面图片」开关声明',
|
||||
!/shareWithCover:\s*\{\s*type:\s*'boolean'/.test(src))
|
||||
ok('源码不再定义/读取 shareWithCover 设置',
|
||||
@@ -185,11 +185,26 @@ ok('样式:收起态摘要与三角已定义',
|
||||
ok('样式:内容多行框自适应(overflow 由 JS 控制)',
|
||||
css.includes('.iwb-plp-vdef{height:auto;min-height:30px'))
|
||||
|
||||
ok('v1.12.1 节点变量标签列定宽 6 字(76px),输入框左边缘才对齐',
|
||||
ok('v1.12.2 节点变量标签列定宽 6 字(76px),输入框左边缘才对齐',
|
||||
css.includes('.iwb-plp-nk{flex:0 0 76px;width:76px;max-width:76px') &&
|
||||
!css.includes('.iwb-plp-nk{flex:0 0 auto'))
|
||||
ok('v1.12.1 名称框提示说明「节点上最多显示 6 字」',
|
||||
ok('v1.12.2 名称框提示说明「节点上最多显示 6 字」',
|
||||
src.includes("'变量名称(节点上最多显示 6 字)'"))
|
||||
|
||||
// ---- 9) v1.12.2 搜索框常驻(中文输入法组合态不被重建打断)----
|
||||
ok('v1.12.2 搜索框不再在 render 里 clear(fixed) 重建(render 只清其余节点)',
|
||||
!/function render \(\) \{\s*\n\s*clear\(fixed\); clear\(scrollBox\)/.test(src) &&
|
||||
src.includes('Array.prototype.slice.call(fixed.children).forEach'))
|
||||
ok('v1.12.2 搜索框只创建一次(ensureSearchRow 复用同一个节点)',
|
||||
src.includes('function ensureSearchRow ()') && src.includes('if (searchRow) return searchRow'))
|
||||
ok('v1.12.2 输入法组合态:compositionstart/end + 组合中不重排',
|
||||
src.includes("searchEl.addEventListener('compositionstart'") &&
|
||||
src.includes("searchEl.addEventListener('compositionend'") &&
|
||||
/composing \|\| \(e && e\.isComposing\)\) return/.test(src))
|
||||
ok('v1.12.2 结果重排走 120ms 防抖(打字不再逐键全量重建)',
|
||||
src.includes('function scheduleResults ()') && src.includes('setTimeout(function () { searchTimer = 0; render() }, 120)'))
|
||||
ok('v1.12.2 清空搜索后焦点回到搜索框',
|
||||
/query = ''; render\(\)\s*\n\s*if \(searchEl\)/.test(src))
|
||||
|
||||
console.log(failed ? ('✗ ' + failed + ' 项失败') : '✓ 全部通过')
|
||||
if (failed) process.exitCode = 1
|
||||
@@ -0,0 +1,164 @@
|
||||
// 搜索框中文输入法回归验证(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)
|
||||
Reference in new issue
Block a user