Files
iwb-plugins-desktop/promptlib/tools/plp-vars-editor-check.mjs
T
qiqi 89841c4d4d
validate / validate (push) Canceled after 0s
feat(promptlib): v1.13.0 正文 {} 里放内容原文(改内容正文跟着改)+ 节点名宽 4 字、内容框 10 行
- 用户裁定:变量「内容」就是正文 {…} 里的那段字 —— 改内容立刻改正文;名称只决定节点左栏显示,不动正文
- 占位符上限 24 → 60(KEY_MAX),5 处正则统一,避免「识别到但替换不到」把整段删掉
- 内容塞不进 {}(换行/超长)时保留原键 + 摘要提示,绝不硬塞(硬塞会让变量识别不到、sync 把内容清掉)
- 直接在正文里改 {…} 时,名称/说明继承过来(单删单增才判定为「改了这段」)
- 节点变量名列 76px → 56px(4 字),名称框提示同步 4 字
- 内容框最高 6 行(132px)→ 10 行(212px)
- 插入变量:选中原文直接作占位符;插入变量按钮不再被挤成两行

测试:harness / vars-editor-test / plp-vars-editor-check 全绿(新增 12 项断言)
2026-11-24 18:52:37 +08:00

207 lines
11 KiB
JavaScript
Raw 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.3)
// 真实 Chromium 挂载 promptlib 面板,走「新建」弹窗与模板节点 render,
// 断言用户能看到的三件事:
// ① 变量区只有 名称 / 内容 / 说明 三个输入项,没有占位符、没有功能性说明文案
// ② 内容框是多行(3 行起步,不再被压成一行裁切)
// ③ 节点输入框默认为空,不填说明时灰字显示「内容」文案
// 依赖宿主仓 playwright:node promptlib/tools/plp-vars-editor-check.mjs
import path from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url'
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 CSS = path.resolve(HERE, '..', 'styles.css')
const browser = await chromium.launch()
const page = await browser.newPage({ viewport: { width: 1080, height: 900 }, deviceScaleFactor: 2 })
const errors = []
page.on('pageerror', (e) => errors.push(String(e)))
await page.setContent('<!doctype html><html><head><meta charset="utf-8"></head><body style="margin:0"><div id="host"></div><div id="nodehost"></div></body></html>')
await page.addStyleTag({ path: CSS })
await page.evaluate(() => {
window.IWBPluginHost = { register: (meta, factory) => { window.__reg = { meta, factory } } }
})
await page.addScriptTag({ path: SRC })
const info = await page.evaluate(() => {
const prompts = [{ id: 'a', name: '示例', text: '风格 {主色调}', group: '默认' }]
const groups = [{ id: '默认', name: '默认' }]
const data = { schemaVersion: 3, prompts, groups }
let nodeDef = null
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: () => {} },
nodes: { register: (def) => { nodeDef = def } },
canvas: { screenToWorld: (x, y) => ({ x, y }) },
canvasWrite: { createNode: () => 'n1', createImageNode: () => 'n2' },
}
window.__reg.factory(ctx)
window.__panel.mount(document.getElementById('host'))
// 模板节点:同样用真实 render
const node = {
id: 'n1',
data: {
name: '测试模板',
tpl: '风格 {主色调},背景 {留白}',
vdefs: [
{ key: '主色调', label: '主色调', desc: '', def: '橙红 金色描边' },
{ key: '留白', label: '留白', desc: '画面留白比例', def: '四周留 10%' },
],
vars: {}, // 默认为空(新节点就是这个状态)
outKind: 'prompt',
},
}
// 收集节点写回的 patch —— 用户最关心的「留空时提示词还有没有内容」只能从这里看
window.__patches = []
const props = { onUpdate: (id, patch) => { window.__patches.push(patch) }, onSelect: () => {}, selected: false }
nodeDef.render(node, props, document.getElementById('nodehost'))
return { version: window.__reg.meta.version, nodeTitle: nodeDef.title }
})
console.log('插件版本', info.version, '| 节点', info.nodeTitle)
await page.waitForTimeout(500)
// ---- 打开「新建」弹窗,正文里写一段带变量的提示词 ----
await page.evaluate(() => {
const btn = Array.from(document.querySelectorAll('#host button')).find((b) => b.textContent.trim() === '新建')
btn.click()
})
await page.waitForTimeout(400)
await page.evaluate(() => {
const ta = document.querySelector('.iwb-plp-etext')
ta.value = '一个顶部金色、顶部和底部有金色边缘、底部悬挂红色,整体风格 {主色调}'
ta.dispatchEvent(new Event('input', { bubbles: true }))
})
await page.waitForTimeout(300)
// 展开第一个变量
await page.evaluate(() => {
const row = document.querySelector('.iwb-plp-vrow2')
if (row) row.click()
})
await page.waitForTimeout(400)
const dom = await page.evaluate(() => {
const q = (s) => document.querySelectorAll(s).length
const area = document.querySelector('.iwb-plp-vdef')
const nodeIns = Array.from(document.querySelectorAll('#nodehost .iwb-plp-nin'))
const text = document.querySelector('.iwb-plp-vars').textContent
return {
counts: { name: q('.iwb-plp-vname'), def: q('.iwb-plp-vdef'), desc: q('.iwb-plp-vdesc'), key: q('.iwb-plp-vkey') },
areaH: area ? Math.round(area.getBoundingClientRect().height) : 0,
areaRows: area ? area.getAttribute('rows') : '',
varText: text.replace(/\s+/g, ' ').trim().slice(0, 80),
nodeInputs: nodeIns.map((n) => ({ tag: n.tagName, value: n.value, ph: n.placeholder })),
nodeLabels: Array.from(document.querySelectorAll('#nodehost .iwb-plp-nk')).map((n) => n.textContent),
}
})
console.log('编辑器输入项', JSON.stringify(dom.counts))
console.log('内容框高 / rows =', dom.areaH, '/', dom.areaRows)
console.log('变量区文案 =', JSON.stringify(dom.varText))
console.log('节点输入框 =', JSON.stringify(dom.nodeInputs))
console.log('节点标签 =', JSON.stringify(dom.nodeLabels))
// ---- v1.13.0:改内容 ⇒ 正文 {…} 同步(用户报的「没关联」)+ 输入框不失焦 ----
const link = await page.evaluate(async () => {
const wait = (ms) => new Promise((r) => setTimeout(r, ms))
const area = document.querySelector('.iwb-plp-vdef')
const nameIn = document.querySelector('.iwb-plp-vname')
const ta = document.querySelector('.iwb-plp-etext')
const before = ta.value
area.focus()
area.value = '主色'
area.dispatchEvent(new Event('input', { bubbles: true }))
await wait(80)
const afterContent = ta.value
const focusOnArea = document.activeElement === area
nameIn.value = '主面积色'
nameIn.dispatchEvent(new Event('input', { bubbles: true }))
await wait(80)
return {
before,
afterContent,
afterName: ta.value,
focused: focusOnArea,
hl: document.querySelectorAll('.iwb-plp-hlvar').length,
}
})
console.log('改内容前正文 =', JSON.stringify(link.before), '→', JSON.stringify(link.afterContent))
// ---- v1.13.0:内容框最高 10 行(212px)后框内滚动 ----
const areaMax = await page.evaluate(() => {
const area = document.querySelector('.iwb-plp-vdef')
area.value = Array.from({ length: 20 }, (_, i) => '第' + (i + 1) + '行内容').join('\n')
area.dispatchEvent(new Event('input', { bubbles: true }))
return { h: Math.round(area.getBoundingClientRect().height), scroll: area.scrollHeight > area.clientHeight + 1 }
})
// ---- v1.13.0:节点变量名列定宽 4 字(56px)----
const nkWidth = await page.evaluate(() => {
const nk = document.querySelector('#nodehost .iwb-plp-nk')
return nk ? Math.round(nk.getBoundingClientRect().width) : 0
})
console.log('内容框封顶高 =', areaMax.h, '| 滚动 =', areaMax.scroll, '| 节点名宽 =', nkWidth)
// ---- 留空 / 填写 / 再清空:看节点写回的 text 与 vars ----
const flow = await page.evaluate(async () => {
const ta = document.querySelectorAll('#nodehost .iwb-plp-nin')[0]
const wait = (ms) => new Promise((r) => setTimeout(r, ms))
// 已填:用用户输入
ta.value = '深蓝'
ta.dispatchEvent(new Event('input', { bubbles: true }))
await wait(420)
const filled = window.__patches[window.__patches.length - 1]
// 清空:回到内容兜底
ta.value = ''
ta.dispatchEvent(new Event('input', { bubbles: true }))
await wait(420)
const cleared = window.__patches[window.__patches.length - 1]
return {
filledText: filled && filled.text,
filledVars: filled && filled.vars,
clearedText: cleared && cleared.text,
clearedVars: cleared && cleared.vars,
}
})
console.log('填写后 text =', JSON.stringify(flow.filledText))
console.log('清空后 text =', JSON.stringify(flow.clearedText), '| vars =', JSON.stringify(flow.clearedVars))
await page.screenshot({ path: '/tmp/plp-vars-1123.png', fullPage: true })
const checks = [
['编辑器只有 名称/内容/说明 三项(无占位符框)', dom.counts.key === 0 && dom.counts.name === 1 && dom.counts.def === 1 && dom.counts.desc === 1],
['内容框 rows=3', dom.areaRows === '3'],
['内容框实际高度 ≥ 60px(3 行,不再被压成一行)', dom.areaH >= 60],
['变量区没有功能性说明文案', !/名称只影响显示|与正文双向同步/.test(dom.varText)],
['变量区没有「占位符」字眼', !/占位符/.test(dom.varText)],
['节点输入框默认为空(2 个都是空 value)', dom.nodeInputs.length === 2 && dom.nodeInputs.every((n) => n.value === '')],
['无说明时 placeholder = 内容文案', dom.nodeInputs[0] && dom.nodeInputs[0].ph === '橙红 金色描边'],
['有说明时 placeholder = 说明', dom.nodeInputs[1] && dom.nodeInputs[1].ph === '画面留白比例'],
['节点仍是 textarea(可多行)', dom.nodeInputs.every((n) => n.tag === 'TEXTAREA')],
['填写后提示词用填写值', /深蓝/.test(flow.filledText || '') && !/橙红 金色描边/.test(flow.filledText || '')],
['填写值写进 vars', flow.filledVars && flow.filledVars['主色调'] === '深蓝'],
['清空后提示词回到「内容」兜底(不丢内容)', /橙红 金色描边/.test(flow.clearedText || '') && !/\{主色调\}/.test(flow.clearedText || '')],
['清空后 vars 里是空串(输入框下次仍为空)', flow.clearedVars && flow.clearedVars['主色调'] === ''],
['v1.13.0 改内容 ⇒ 正文 {…} 同步改名(联动)', /\{主色调\}/.test(link.before) && /\{主色\}/.test(link.afterContent) && !/\{主色调\}/.test(link.afterContent)],
['v1.13.0 改内容时高亮块仍在(正文重绘后不丢色)', link.hl >= 1],
['v1.13.0 改内容时焦点留在内容框(不重绘整行)', link.focused === true],
['v1.13.0 改名称不动正文(名称只影响节点显示)', link.afterName === link.afterContent],
['v1.13.0 内容框最多 10 行(约 222px 盒高)后框内滚动', areaMax.h > 200 && areaMax.h <= 230 && areaMax.scroll === true],
['v1.13.0 节点变量名列定宽 4 字(56px)', nkWidth === 56],
]
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 browser.close()
process.exit(failed ? 1 : 0)