diff --git a/style-source/README.md b/style-source/README.md index 4d893da..0686113 100644 --- a/style-source/README.md +++ b/style-source/README.md @@ -20,7 +20,9 @@ - **详情**:窄面板铺满覆盖层,宽面板变右侧栏;大图 + 标签 + 摘要 + 提示词 + 变量表单。 - **变量替换**:提示词里的 `{角色描述}` 这类占位符给输入框,填了就替换,**留空的保留 `{原占位符}`**(换成空串会把提示词结构弄丢)。未填数量实时提示。**变量全部平铺,不做折叠**——折叠的「收起」状态容易让人以为变量就这几个(Vigo 那批曾最多 24 个,详情页本身可滚动)。输入框的灰色提示**优先用数据里的 `desc`**(「主体花卉,如:栀子花 / 白牡丹」),没有才退回占位符名。 - **插入参考图**:`fetch` 大图 → blob → dataURL → `createImageNode`(宿主只认 `data:` URL)。 -- **插入位置**:一律**不传坐标**(`createNode('prompt', null, null, …)`),交给宿主 `findSpot()` —— 从**视口中心**向外螺旋扫空位、跳过已有节点、钳在视口内。插件只有节点的 `x/y`,拿不到 pan/zoom 和实测高度,自己排必然插到看不见的地方或压在别的节点上(旧版就是「选中节点右侧固定 380,无避让」)。 +- **插入位置(两种方式)** + - **点击**:不传坐标(`createNode('prompt', null, null, …)`),交给宿主 `findSpot()` —— 从**视口中心**向外螺旋扫空位、跳过已有节点、并**避开压在画布上的浮层**(资源中心 / 插件面板 / 左右侧栏)。插件只有节点的 `x/y`,拿不到 pan/zoom 和实测高度,自己排必然插到看不见的地方或压在别的节点上(旧版就是「选中节点右侧固定 380,无避让」)。 + - **按住拖到画布上松手**:节点落在**松手点**(`screenToWorld` 换算,节点左上角对齐光标)。落点由宿主算:pan/zoom 只有它知道。拖动中光标下跟一张幽灵卡片,进入画布会提示「松手放这里」;松手点不在画布上(还在面板里)⇒ 取消。位移不足 6px 视作点击,两条路径并存、互不干扰。 ## 变量:只有提示词里的 `{占位符}` 才算数 diff --git a/style-source/package.json b/style-source/package.json index b4c412d..608d5c0 100644 --- a/style-source/package.json +++ b/style-source/package.json @@ -1,7 +1,7 @@ { "name": "@iwb-plugin/style-source", "private": true, - "version": "1.1.2", + "version": "1.2.0", "description": "风格库:资源库面板里浏览 310 个 AI 绘画风格(12 分类 / 搜索 / 变量替换),一键把预览图插入画布 ref 节点、把提示词插入 prompt 节点", "ibwPlugin": { "id": "style-source", diff --git a/style-source/src/index.js b/style-source/src/index.js index 7cf0cd9..62d6bb7 100644 --- a/style-source/src/index.js +++ b/style-source/src/index.js @@ -131,6 +131,18 @@ .iwb-ss-btn:disabled { opacity: .45; cursor: not-allowed; } .iwb-ss-btn.is-key { border-color: var(--iwb-brand, #6b7280); } .iwb-ss-btn.is-key:hover:not(:disabled) { background: var(--iwb-brand, #6b7280); color: var(--iwb-panel, #fff); } +/* 按住按钮往外拖 = 拖拽插入:按钮本体压暗,光标下跟一个幽灵卡片 */ +.iwb-ss-btn { user-select: none; -webkit-user-select: none; touch-action: none; } +.iwb-ss-btn.is-dragging { opacity: .5; } +.iwb-ss-ghost { + position: fixed; z-index: 2147483000; left: 0; top: 0; pointer-events: none; + padding: 5px 10px; border-radius: 6px; font-size: 12px; line-height: 1.2; + font-family: inherit; white-space: nowrap; + background: rgba(22, 26, 34, .88); color: #fff; + border: 1px dashed rgba(255, 255, 255, .55); + transform: translate(-2px, -2px); +} +.iwb-ss-ghost.is-over { background: var(--iwb-brand, #6b7280); border-style: solid; } /* 分类 chips:按面板宽度自动换行。 以前是单行横向滚动(scrollbar 又藏起来了):面板拖窄/拖宽时右边那几类永远看不见,用户以为只有 7 个分类。 @@ -549,7 +561,7 @@ const ui = { root: null, search: null, chips: null, grid: null, detail: null, status: null, toastEl: null, - btnRef: null, btnPrompt: null, busy: false, timer: null, seq: 0, + btnRef: null, btnPrompt: null, busy: false, timer: null, seq: 0, justDragged: false, } const view = { list: [], count: 0 } @@ -613,7 +625,7 @@ // ---- 插入动作 ---- - async function insertImage(style) { + async function insertImage(style, at) { const api = ctx.canvas if (!api || typeof api.createImageNode !== 'function') { toast('当前宿主不支持插入图片(缺 canvasWrite 能力)'); return } const url = useThumb() ? thumbOf(style) : bigOf(style) @@ -623,10 +635,13 @@ const blob = await loadBlob(url) const dataUrl = await blobToDataUrl(blob) // createImageNode 只认 data: URL(宿主 app.js 里 /^data:/i 校验),必须 blob → base64 - const id = api.createImageNode({ + // at = 拖拽松手点(世界坐标);不传 ⇒ 宿主 findSpot 找位 + const payload = { dataUrl: dataUrl, name: String(style.code || 'style') + '_' + String(style.name || ''), - }) + } + if (at && Number.isFinite(at.x) && Number.isFinite(at.y)) { payload.x = at.x; payload.y = at.y } + const id = api.createImageNode(payload) if (!id) throw new Error('画布拒绝了这张图(data URL 无效)') toast('已插入参考图') } catch (error) { @@ -636,14 +651,16 @@ } } - function insertPrompt(style) { + function insertPrompt(style, at) { const api = ctx.canvas if (!api || typeof api.createNode !== 'function') { toast('当前宿主不支持插入节点(缺 canvasWrite 能力)'); return } const values = CACHE.vars[style.id] || {} const text = applyVars(style, values).trim() if (!text) { toast('这条风格没有提示词'); return } - // 不传坐标:null/null ⇒ 宿主走 findSpot(视口中心 + 避开已有节点 + 钳在视口内) - const id = api.createNode('prompt', null, null, { text: text }) + // 不传坐标(点击):null/null ⇒ 宿主走 findSpot(视口中心 + 避开已有节点 + 避开浮层) + // 传了坐标(拖拽松手):直接落在松手点 + const dropAt = (at && Number.isFinite(at.x) && Number.isFinite(at.y)) ? { x: at.x, y: at.y } : null + const id = dropAt ? api.createNode('prompt', dropAt.x, dropAt.y, { text: text }) : api.createNode('prompt', null, null, { text: text }) if (!id) { toast('插入失败:prompt 节点类型未注册'); return } const left = unfilledCount(style, values) // 超过最大高度就会在节点内滚动:说一声,免得用户以为内容被截掉了 @@ -652,6 +669,86 @@ toast((left ? '已插入提示词(还有 ' + left + ' 个变量没填)' : '已插入提示词') + tail) } + // ---- 拖拽插入:按住按钮 → 拖到画布上松手 → 节点落在松手位置 ---- + // + // 为什么要有第二套交互:点击插入只能由宿主 findSpot 猜位置(视口中心、避开已有节点与浮层), + // 但"我想放在这里"是宿主猜不出来的。拖拽把落点交回给用户:松手点即节点左上角。 + // + // 关键约束: + // · 插件面板在 .iwb-fullmask 里,宿主在 mask 上挂了 onPointerDown/Move 并 stopPropagation + // ⇒ 监听必须挂在 window 且用**捕获阶段**,否则事件到不了插件; + // · 屏幕 → 世界坐标只能问宿主(pan/zoom 在它手里):ctx.canvas.screenToWorld; + // · 松手点不在画布上(比如还在面板里)⇒ 取消,不建节点; + // · 位移不足阈值 ⇒ 视为点击,走原来的 findSpot 路径(click 与拖拽并存)。 + const DRAG_MIN = 6 + const dragHooks = [] + function clearDragHooks() { + for (const fn of dragHooks.splice(0)) { try { fn() } catch (error) { /* 忽略 */ } } + } + + function overCanvas(clientX, clientY) { + try { + const hit = document.elementFromPoint(clientX, clientY) + return !!(hit && hit.closest && hit.closest('.iwb-canvas')) + } catch (error) { return false } + } + + function attachDragInsert(btn, label, drop) { + const api = ctx.canvas + // 旧宿主没有 screenToWorld ⇒ 只有点击插入(功能不倒退) + if (!api || typeof api.screenToWorld !== 'function') return + + let drag = null + const cleanup = () => { + if (drag && drag.ghost && drag.ghost.parentNode) drag.ghost.parentNode.removeChild(drag.ghost) + btn.classList.remove('is-dragging') + drag = null + } + btn.addEventListener('pointerdown', (event) => { + if (event.button !== 0 || ui.busy || btn.disabled) return + drag = { id: event.pointerId, sx: event.clientX, sy: event.clientY, moved: false, ghost: null } + }) + const onMove = (event) => { + if (!drag || event.pointerId !== drag.id) return + if (!drag.moved) { + if (Math.abs(event.clientX - drag.sx) + Math.abs(event.clientY - drag.sy) < DRAG_MIN) return + drag.moved = true + drag.ghost = el('div', { class: 'iwb-ss-ghost', text: '拖到画布上松手' }) + document.body.appendChild(drag.ghost) + btn.classList.add('is-dragging') + // 指针捕获:拖出窗口外也能收到 pointerup(失败不影响 —— window 捕获监听仍在) + try { btn.setPointerCapture(event.pointerId) } catch (error) { /* 忽略 */ } + } + const over = overCanvas(event.clientX, event.clientY) + drag.ghost.style.left = event.clientX + 'px' + drag.ghost.style.top = event.clientY + 'px' + drag.ghost.classList.toggle('is-over', over) + drag.ghost.textContent = over ? label + '(松手放这里)' : '拖到画布上松手' + } + const onUp = (event) => { + if (!drag || event.pointerId !== drag.id) return + if (!drag.moved) { drag = null; return } + const over = overCanvas(event.clientX, event.clientY) + const world = over ? api.screenToWorld(event.clientX, event.clientY) : null + cleanup() + if (!over || !world) { toast('松手位置不在画布上,已取消'); return } + // 抑制紧随其后的 click(松手又回到按钮上时浏览器仍会派发 click ⇒ 否则会插入两次) + ui.justDragged = true + global.setTimeout(() => { ui.justDragged = false }, 400) + drop(world) + } + const onCancel = () => { if (drag && drag.moved) toast('已取消'); cleanup() } + // 捕获阶段 + window:面板的 stopPropagation 拦不住,且拖到画布上仍收得到 + global.addEventListener('pointermove', onMove, true) + global.addEventListener('pointerup', onUp, true) + global.addEventListener('pointercancel', onCancel, true) + dragHooks.push(() => { + global.removeEventListener('pointermove', onMove, true) + global.removeEventListener('pointerup', onUp, true) + global.removeEventListener('pointercancel', onCancel, true) + }) + } + // ---- 渲染 ---- // 名称与数量拆成两个 span:窄档只藏数量(.iwb-ss-chipn 在 CSS 里 display:none), @@ -747,6 +844,7 @@ } function renderDetail() { + clearDragHooks() // 详情重绘会换掉两个按钮 ⇒ 先摘掉旧按钮的拖拽监听(否则每次选中都叠 3 个 window 监听) const style = CACHE.selId ? styleById(CACHE.selId) : null if (!style) { @@ -850,13 +948,17 @@ ui.btnRef = el('button', { class: 'iwb-ss-btn', text: '插入参考图', - onClick: () => insertImage(style), + title: '点击:自动找位置插入参考图;按住拖到画布上松手:放在你指定的位置', + onClick: () => { if (ui.justDragged) return; insertImage(style) }, }) + const style0 = style ui.btnPrompt = el('button', { class: 'iwb-ss-btn is-key', text: '插入提示词', - title: '插入为提示词节点;内容按「最大高度」截断显示,节点内可滚动,全文用气泡「放大编辑」⌘E 查看', - onClick: () => insertPrompt(style), + title: '点击:自动找位置插入提示词;按住拖到画布上松手:放在你指定的位置。内容按「最大高度」截断显示,节点内可滚动,全文用气泡「放大编辑」⌘E 查看', + onClick: () => { if (ui.justDragged) return; insertPrompt(style0) }, }) + attachDragInsert(ui.btnRef, '参考图', (at) => insertImage(style0, at)) + attachDragInsert(ui.btnPrompt, '提示词', (at) => insertPrompt(style0, at)) ui.detail.appendChild(head) ui.detail.appendChild(bodyEl) @@ -945,6 +1047,7 @@ destroy() { if (ro) { try { ro.disconnect() } catch (error) { /* 忽略 */ } ro = null } if (ui.timer) { global.clearTimeout(ui.timer); ui.timer = null } + clearDragHooks() ui.seq++ }, } @@ -958,7 +1061,7 @@ global.IWBPluginHost.register({ id: 'style-source', name: '风格库', - version: '1.1.2', + version: '1.2.0', apiVersion: 1, capabilities: ['panels', 'canvas', 'canvasWrite'], settings: { diff --git a/style-source/tools/harness.mjs b/style-source/tools/harness.mjs index 352054f..6d7f562 100644 --- a/style-source/tools/harness.mjs +++ b/style-source/tools/harness.mjs @@ -210,6 +210,9 @@ async function main() { return 'n_new_' + window.__calls.nodes.length }, createImageNode: (payload) => { window.__calls.images.push(payload); return 'n_img_' + window.__calls.images.length }, + // 模拟宿主新能力(2026-11-02):屏幕坐标 → 世界坐标。 + // 视口口径:画布容器左上角 (320,0),平移 (100,-50),缩放 0.5 + screenToWorld: (clientX, clientY) => ({ x: (clientX - 320 - 100) / 0.5, y: (clientY - 0 + 50) / 0.5, scale: 0.5 }), }, } window.__setup(ctx) @@ -378,6 +381,53 @@ async function main() { Boolean(promptCall) && promptCall.noOverlap === true, promptCall ? '(' + promptCall.x + ',' + promptCall.y + ')' : '') + // ---- 拖拽插入:按住「插入提示词」拖到画布上松手 ⇒ 节点落在松手点 ---- + await page.evaluate(() => { + // 造一块 .iwb-canvas(插件靠 elementFromPoint + closest('.iwb-canvas') 判断「是否在画布上」) + const cv = document.createElement('div') + cv.className = 'iwb-canvas' + cv.id = 'fakeCanvas' + cv.style.cssText = 'position:fixed;left:320px;top:0;width:900px;height:640px;z-index:1;background:#eee' + document.body.appendChild(cv) + window.__calls.nodes.length = 0 + }) + const btnBox = await page.locator('#hostA .iwb-ss-dfoot .iwb-ss-btn').nth(1).boundingBox() + const dropX = 700 + const dropY = 300 + await page.mouse.move(btnBox.x + btnBox.width / 2, btnBox.y + btnBox.height / 2) + await page.mouse.down() + await page.mouse.move(dropX - 60, dropY - 40, { steps: 6 }) + const ghost = await page.evaluate(() => { + const g = document.querySelector('.iwb-ss-ghost') + return g ? { text: g.textContent, over: g.classList.contains('is-over'), pe: getComputedStyle(g).pointerEvents } : null + }) + check('拖拽中出现跟随光标的幽灵卡片', Boolean(ghost), ghost ? ghost.text : '未出现') + check('幽灵卡片 pointer-events:none(不挡 elementFromPoint)', Boolean(ghost) && ghost.pe === 'none', ghost ? ghost.pe : '') + check('拖到画布上时幽灵提示「松手放这里」', Boolean(ghost) && ghost.over === true, ghost ? ghost.text : '') + await page.mouse.move(dropX, dropY, { steps: 4 }) + await page.mouse.up() + await page.waitForTimeout(150) + const dragCall = await page.evaluate(() => window.__calls.nodes[0] || null) + const expectX = (dropX - 320 - 100) / 0.5 + const expectY = (dropY + 50) / 0.5 + check('拖拽松手 ⇒ createNode 带显式坐标(不再交给宿主猜)', + Boolean(dragCall) && dragCall.auto === false, dragCall ? 'auto=' + dragCall.auto : '未调用') + check('落点 = 松手点换算出的世界坐标', + Boolean(dragCall) && Math.abs(dragCall.x - expectX) < 1 && Math.abs(dragCall.y - expectY) < 1, + dragCall ? '(' + dragCall.x + ',' + dragCall.y + ') 期望 (' + expectX + ',' + expectY + ')' : '未调用') + check('拖拽只插入一次(松手后的 click 被抑制)', + await page.evaluate(() => window.__calls.nodes.length === 1), + '调用次数=' + await page.evaluate(() => window.__calls.nodes.length)) + // 松手在面板里(不在画布上)⇒ 取消,不建节点 + await page.mouse.move(btnBox.x + btnBox.width / 2, btnBox.y + btnBox.height / 2) + await page.mouse.down() + await page.mouse.move(btnBox.x + 40, btnBox.y - 60, { steps: 4 }) + await page.mouse.up() + await page.waitForTimeout(150) + check('松手点不在画布上 ⇒ 不建节点', await page.evaluate(() => window.__calls.nodes.length === 1), + '调用次数=' + await page.evaluate(() => window.__calls.nodes.length)) + await page.evaluate(() => { const cv = document.getElementById('fakeCanvas'); if (cv) cv.remove(); window.__calls.nodes.length = 0 }) + // ---- 插参考图(blob → dataURL) ---- await page.locator('#hostA .iwb-ss-dfoot .iwb-ss-btn').first().click() const imgCall = await poll(() => page.evaluate(() => window.__calls.images[0] || null), 8000)