test(promptlib): 指针拖拽插入断言 + 1.6.15 同步

This commit is contained in:
qiqi committed 2026-11-22 21:09:25 +08:00
1 parent 10d97e74d8
commit de85aff6df
5 files changed
+95 -43

No files matched your search

+3 -3
View File
@@ -44,10 +44,10 @@
"dir": "promptlib",
"main": "main.js",
"style": "styles.css",
"version": "1.6.14",
"version": "1.6.15",
"files": {
"main.js": "e16c61af81472fd644cbc8ce039cb6202bc3527011a114765a748b5d5bb46221",
"styles.css": "052891bfe2e9cdd636103ab424aec484574a92f0e87bc3edf1ffc03385a25cc5"
"main.js": "6a6eeb564ccd3a7d8024a2d4f49271ff442416e3cf53d7c810d10ecbeb75736e",
"styles.css": "1d3d84220118ed97490314d2aa122126d505c9eabcf1f8bd53a627983e7d5942"
}
},
{
+69 -29
View File
@@ -732,10 +732,76 @@
box.appendChild(el('div', { class: 'iwb-plp-ghosthint', text: vd.length ? ('模板节点 · ' + vd.length + ' 个变量') : '文本节点' }))
g.appendChild(box)
g.style.position = 'fixed'; g.style.left = '-9999px'; g.style.top = '0'
g.classList.add('is-live')
document.body.appendChild(g)
return g
}
// v1.6.15:改成和风格库一样的「指针拖拽插入」,不再用原生 HTML5 drag&drop。
// 原生拖放在 WKWebView 里会吞掉 pointerup,落点节点会带着“焦点/编辑态”,第一次点击拖不动。
var cardDrag = null
function moveGhostTo (x, y) {
if (!cardDrag || !cardDrag.ghost) return
cardDrag.ghost.style.left = (x + 14) + 'px'
cardDrag.ghost.style.top = (y + 14) + 'px'
}
function endCardDrag (commit, x, y) {
var st = cardDrag
if (!st) return
cardDrag = null
try {
global.removeEventListener('pointermove', st.onMove, true)
global.removeEventListener('pointerup', st.onUp, true)
global.removeEventListener('pointercancel', st.onCancel, true)
} catch (e) { /* ignore */ }
if (st.ghost && st.ghost.parentNode) st.ghost.parentNode.removeChild(st.ghost)
try { st.card.classList.remove('is-dragging'); st.card.classList.remove('is-pressed') } catch (e) { /* ignore */ }
if (!commit || !st.active) return
try {
var api2 = ctx.canvas
var cnv = document.querySelector('.iwb-canvas')
if (cnv) {
var rc = cnv.getBoundingClientRect()
if (x < rc.left || x > rc.right || y < rc.top || y > rc.bottom) return
}
if (!api2 || typeof api2.screenToWorld !== 'function' || typeof api2.createNode !== 'function') { notify('当前宿主不支持拖拽插入', 'err'); return }
var w = api2.screenToWorld(x, y)
if (!w) return
var vd = varsOf(st.p)
if (vd.length) {
api2.createNode('ptpl', w.x, w.y, { promptId: '', name: st.p.name || '提示词模板', tpl: st.p.text || '', vdefs: vd, vars: {}, cover: st.p.cover || '', refOn: false, text: st.p.text || '', outKind: 'prompt' })
notify('已插入模板节点:' + (st.p.name || ''))
} else {
api2.createNode('prompt', w.x, w.y, { text: st.p.text || '', title: st.p.name || '' })
notify('已插入提示词:' + (st.p.name || ''))
}
} catch (err) { notify('拖拽插入失败:' + ((err && err.message) || err), 'err') }
}
function startCardDrag (e, p, card) {
if (e.button !== 0) return
// 不用 preventDefault:否则 WebKit 可能吞掉 click/dblclick(双击详情失效)
card.classList.add('is-pressed')
var st = { card: card, p: p, sx: e.clientX, sy: e.clientY, active: false, ghost: null }
st.onMove = function (ev) {
var dx = ev.clientX - st.sx, dy = ev.clientY - st.sy
if (!st.active && Math.hypot(dx, dy) < 5) return
if (!st.active) {
st.active = true
card.classList.add('is-dragging')
try { st.ghost = makeDragGhost(p) } catch (err) { st.ghost = null }
}
moveGhostTo(ev.clientX, ev.clientY)
}
st.onUp = function (ev) { endCardDrag(true, ev.clientX, ev.clientY) }
st.onCancel = function () { endCardDrag(false, 0, 0) }
try {
global.addEventListener('pointermove', st.onMove, true)
global.addEventListener('pointerup', st.onUp, true)
global.addEventListener('pointercancel', st.onCancel, true)
} catch (err) { /* ignore */ }
cardDrag = st
}
function openDetail (id) { view.mode = 'detail'; view.id = String(id || ''); render(); try { scrollBox.scrollTop = 0 } catch (e) { /* ignore */ } }
function backToList () { view.mode = 'list'; view.id = ''; render() }
@@ -1470,40 +1536,14 @@
items.forEach(function (p) {
var starred = (p.group || '') === '收藏'
var coverUrl = coverOf(p)
var card = el('div', { class: 'iwb-plp-card' + (p.cover ? ' has-cover' : ' no-cover'), draggable: 'true', title: '双击查看详情 · 拖到画布插入 · 右键移动到分组' })
var card = el('div', { class: 'iwb-plp-card' + (p.cover ? ' has-cover' : ' no-cover'), title: '双击查看详情 · 拖到画布插入 · 右键移动到分组' })
var bg = el('div', { class: 'iwb-plp-bg' })
bg.style.background = coverUrl ? 'var(--iwb-plugin-inset)' : gradOf(p)
if (coverUrl) bg.appendChild(el('img', { class: 'iwb-plp-cover', src: coverUrl, alt: '', draggable: 'false' }))
card.appendChild(bg)
card.appendChild(el('div', { class: 'iwb-plp-t', text: p.name || '未命名' }))
card.addEventListener('pointerdown', function (e) {
if (e.button !== 0) return
card.classList.add('is-pressed')
})
card.addEventListener('dragstart', function (e) {
card.classList.add('is-dragging')
var ghost = null
try { ghost = makeDragGhost(p); if (e.dataTransfer && e.dataTransfer.setDragImage) e.dataTransfer.setDragImage(ghost, 26, 26) } catch (err) { /* ignore */ }
setTimeout(function () { if (ghost && ghost.parentNode) ghost.parentNode.removeChild(ghost) }, 0)
try {
e.stopPropagation()
var vd = varsOf(p)
e.dataTransfer.setData('application/x-iwb-prompt', JSON.stringify({
text: p.text || '', name: p.name || '',
vars: vd.map(function (v) { return v.key }),
vdefs: vd,
cover: p.cover || '', pluginId: ctx.id, nodeType: 'ptpl',
}))
e.dataTransfer.setData('application/x-iwb-promptid', p.id)
e.dataTransfer.setData('text/plain', (p.text || '').slice(0, 200))
e.dataTransfer.effectAllowed = 'copy'
} catch (err) { /* ignore */ }
})
card.addEventListener('pointerdown', function (e) { startCardDrag(e, p, card) })
card.addEventListener('dblclick', function (e) { e.stopPropagation(); try { openDetailModal(p) } catch (err) { notify('详情打开失败:' + ((err && err.message) || err), 'err') } })
card.addEventListener('dragend', function () {
card.classList.remove('is-dragging'); card.classList.remove('is-pressed')
})
card.addEventListener('pointerup', function () { card.classList.remove('is-pressed') })
card.addEventListener('contextmenu', function (e) { openMenu(e, p) })
cards.appendChild(card)
})
@@ -1538,7 +1578,7 @@
global.IWBPluginHost.register({
id: 'promptlib',
name: '提示词库',
version: '1.6.14',
version: '1.6.15',
apiVersion: 1,
capabilities: ['panels', 'promptlib', 'nodeTypes', 'canvasWrite'],
data: { schemaVersion: 3, maxBytes: 4 * 1024 * 1024 },
+3 -3
View File
@@ -1,7 +1,7 @@
{
"id": "promptlib",
"name": "提示词库",
"version": "1.6.14",
"version": "1.6.15",
"apiVersion": 1,
"main": "main.js",
"style": "styles.css",
@@ -13,7 +13,7 @@
"提示词"
],
"files": {
"main.js": "e16c61af81472fd644cbc8ce039cb6202bc3527011a114765a748b5d5bb46221",
"styles.css": "052891bfe2e9cdd636103ab424aec484574a92f0e87bc3edf1ffc03385a25cc5"
"main.js": "6a6eeb564ccd3a7d8024a2d4f49271ff442416e3cf53d7c810d10ecbeb75736e",
"styles.css": "1d3d84220118ed97490314d2aa122126d505c9eabcf1f8bd53a627983e7d5942"
}
}
+1
View File
@@ -387,3 +387,4 @@ box-shadow: 0 24px 60px rgba(0,0,0,.32);
/* v1.6.13:未选中时变量输入框不拦截指针 —— 单击节点=选中/可拖动;选中后再点输入框编辑 */
.iwb-plp-node:not(.is-selected) input{pointer-events:none}
.iwb-plp-card{user-select:none}
+19 -8
View File
@@ -33,7 +33,7 @@ const wait = (ms) => new Promise((r) => setTimeout(r, ms))
{ id: 'q3', name: '未分组的一条', text: '没有分组的', tags: [], group: '' },
]
let groups = [{ id: '小红书', name: '小红书' }, { id: '收藏', name: '收藏' }]
const calls = { save: 0, insert: [], copy: [], notify: [] }
const calls = { save: 0, insert: [], copy: [], notify: [], create: [] }
let panel = null
const ctx = {
@@ -46,6 +46,10 @@ const wait = (ms) => new Promise((r) => setTimeout(r, ms))
notify: (t, ty) => { calls.notify.push([t, ty]) },
onChanged: () => () => {},
},
canvas: {
screenToWorld: (x, y) => ({ x, y }),
createNode: (type, x, y, data) => { calls.create.push({ type, x, y, data }); return 'n1' },
},
}
registered.f(ctx)
assert.ok(panel && typeof panel.mount === 'function', '应注册面板')
@@ -84,14 +88,21 @@ const wait = (ms) => new Promise((r) => setTimeout(r, ms))
const back = host.querySelector('.iwb-chip-active')
assert.ok(back && back.textContent.indexOf('小红书') === 0, '「×」清空后应回到「小红书」,实际 ' + (back && back.textContent))
// 拖拽 payload
// v1.6.15:改为指针拖拽插入(与风格库一致),不再走 HTML5 dataTransfer
const canvasEl = window.document.createElement('div')
canvasEl.className = 'iwb-canvas'
window.document.body.appendChild(canvasEl)
canvasEl.getBoundingClientRect = () => ({ left: 0, top: 0, right: 800, bottom: 600, width: 800, height: 600 })
const card = cards()[0]
const store = {}
const dragEvt = new window.Event('dragstart', { bubbles: true })
dragEvt.dataTransfer = { setData: (k, v) => { store[k] = v }, effectAllowed: '' }
card.dispatchEvent(dragEvt)
assert.ok(store['application/x-iwb-promptid'], '拖拽应带提示词 id')
assert.strictEqual(JSON.parse(store['application/x-iwb-prompt']).text, '生成小红书封面图', '拖拽应带正文 payload')
card.dispatchEvent(new window.MouseEvent('pointerdown', { bubbles: true, clientX: 40, clientY: 40, button: 0 }))
window.dispatchEvent(new window.MouseEvent('pointermove', { bubbles: true, clientX: 70, clientY: 70 }))
window.dispatchEvent(new window.MouseEvent('pointermove', { bubbles: true, clientX: 300, clientY: 200 }))
assert.ok(window.document.querySelector('.iwb-plp-dragghost'), '拖动时应出现幽灵卡片')
window.dispatchEvent(new window.MouseEvent('pointerup', { bubbles: true, clientX: 300, clientY: 200 }))
await wait(20)
assert.strictEqual(calls.create.length, 1, '拖入画布应调用 createNode')
assert.strictEqual(calls.create[0].type, 'prompt', '无变量应插入普通提示词节点')
assert.strictEqual(calls.create[0].data.text, '生成小红书封面图', '插入应带正文')
// v1.6.12:双击卡片 → 详情(只读),再点「编辑」→ 浮层
card.dispatchEvent(new window.MouseEvent('dblclick', { bubbles: true }))