fix(style-source): 插入位置交给宿主,不再自己估算 (1.1.2)

用户反馈:插入的提示词会叠在已有节点上,也不在画布中间。

根因:插件自己算坐标。它只有节点的 x/y,拿不到 viewport(pan/zoom)、canvasSize 和各节点
实测高度,旧实现是「选中节点右侧固定 +380 / 最右节点右侧 / 空画布 (80,80)」——既不居中也不避让。

- 删掉 nextSpot / GAP_X,insertPrompt 改为 createNode('prompt', null, null, ...)
- 宿主收到空坐标时走 findSpot():从视口中心向外环形扫,跳过与已有节点相交的格子,
  并把候选钳在视口内;宿主那边有 viewport、canvasSize、measuredH,只有它算得准
- 参考图本来就经 createImageNode → findSpot('ref'),同一套逻辑,无需改动
- harness:mock 宿主复刻 findSpot 的最小行为;断言从「插在选中节点右侧 (480,120)」
  改为「插件传空坐标(auto=true)」+「宿主定位结果不与已有节点相交」
- README 补「插入位置为什么必须交给宿主」

⚠️ 依赖宿主 2026-11-02 的 createNode 改动(空坐标 → findSpot),发布顺序:先宿主后插件。
This commit is contained in:
qiqi committed 2026-11-02 18:48:07 +08:00
1 parent 3969acb9b9
commit 75ea785fcc
4 files changed
+44 -30

No files matched your search

+1 -1
View File
@@ -20,7 +20,7 @@
- **详情**:窄面板铺满覆盖层,宽面板变右侧栏;大图 + 标签 + 摘要 + 提示词 + 变量表单。
- **变量替换**:提示词里的 `{角色描述}` 这类占位符给输入框,填了就替换,**留空的保留 `{原占位符}`**(换成空串会把提示词结构弄丢)。未填数量实时提示。**变量全部平铺,不做折叠**——折叠的「收起」状态容易让人以为变量就这几个(Vigo 那批曾最多 24 个,详情页本身可滚动)。输入框的灰色提示**优先用数据里的 `desc`**(「主体花卉,如:栀子花 / 白牡丹」),没有才退回占位符名。
- **插入参考图**:`fetch` 大图 → blob → dataURL → `createImageNode`(宿主只认 `data:` URL)。
- **插入提示词**:插到当前选中节点右侧;没选中时插到最右节点的右侧;空画布落 `(80, 80)`。
- **插入位置**:一律**不传坐标**(`createNode('prompt', null, null, …)`),交给宿主 `findSpot()` —— 从**视口中心**向外螺旋扫空位、跳过已有节点、钳在视口内。插件只有节点的 `x/y`,拿不到 pan/zoom 和实测高度,自己排必然插到看不见的地方或压在别的节点上(旧版就是「选中节点右侧固定 380,无避让」)。
## 变量:只有提示词里的 `{占位符}` 才算数
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "@iwb-plugin/style-source",
"private": true,
"version": "1.1.1",
"version": "1.1.2",
"description": "风格库:资源库面板里浏览 310 个 AI 绘画风格(12 分类 / 搜索 / 变量替换),一键把预览图插入画布 ref 节点、把提示词插入 prompt 节点",
"ibwPlugin": {
"id": "style-source",
+6 -25
View File
@@ -508,27 +508,9 @@
// ---------------- 插画布 ----------------
// getNodes 里没有节点宽度,用经验值估:prompt 节点约 320 宽,留 60 间距
const GAP_X = 380
function nextSpot(ctx) {
let nodes = []
let sel = null
try { nodes = ctx.canvas.getNodes() || [] } catch (error) { nodes = [] }
try { sel = ctx.canvas.getSelectedNode() } catch (error) { sel = null }
if (sel && sel.id) {
for (const n of nodes) {
if (n.id === sel.id) return { x: Math.round((Number(n.x) || 0) + GAP_X), y: Math.round(Number(n.y) || 0) }
}
}
if (!nodes.length) return { x: 80, y: 80 }
let best = nodes[0]
let maxX = -Infinity
for (const n of nodes) {
const x = Number(n.x) || 0
if (x > maxX) { maxX = x; best = n }
}
return { x: Math.round(maxX + GAP_X), y: Math.round(Number(best.y) || 0) }
}
// 位置一律交给宿主:createNode / createImageNode 收到空坐标时走宿主 findSpot()
// (视口中心起螺旋扫空位、跳过已有节点、钳在视口内)。
// 插件这边只拿得到节点 x/y,没有 pan/zoom 也没有各节点实测高度 —— 自己排必然插到看不见的地方或压住节点。
function useThumb() {
return prefAll().thumbOnly === true
@@ -660,9 +642,8 @@
const values = CACHE.vars[style.id] || {}
const text = applyVars(style, values).trim()
if (!text) { toast('这条风格没有提示词'); return }
const spot = nextSpot(ctx)
// createNode 不会自己避让,坐标必须插件算好(createImageNode 内部才走 findSpot 自动排布)
const id = api.createNode('prompt', spot.x, spot.y, { text: text })
// 不传坐标:null/null ⇒ 宿主走 findSpot(视口中心 + 避开已有节点 + 钳在视口内)
const id = api.createNode('prompt', null, null, { text: text })
if (!id) { toast('插入失败:prompt 节点类型未注册'); return }
const left = unfilledCount(style, values)
// 超过最大高度就会在节点内滚动:说一声,免得用户以为内容被截掉了
@@ -977,7 +958,7 @@
global.IWBPluginHost.register({
id: 'style-source',
name: '风格库',
version: '1.1.1',
version: '1.1.2',
apiVersion: 1,
capabilities: ['panels', 'canvas', 'canvasWrite'],
settings: {
+36 -3
View File
@@ -140,6 +140,23 @@ async function main() {
{ id: 'n_ref', type: 'ref', x: 520, y: 120, data: { name: '参考图' } },
]
window.__sel = { id: 'n_prompt', type: 'prompt', name: '原有提示词' }
// 宿主 findSpot 的最小复刻:视口 1280×900,从中心向外按环形扫,返回第一个不与已有节点相交的格子
window.__mockFindSpot = () => {
const CW = 1280, CH = 900, W = 320, H = 216, PAD = 20
const boxes = (window.__nodes || []).map((n) => ({ x: n.x, y: n.y, w: 320, h: 216 }))
const hit = (x, y) => boxes.some((b) => x < b.x + b.w && x + W > b.x && y < b.y + b.h && y + H > b.y)
const cx = CW / 2 - W / 2, cy = CH / 2 - H / 2
for (let ring = 0; ring <= 6; ring++) {
for (let dy = -ring; dy <= ring; dy++) {
for (let dx = -ring; dx <= ring; dx++) {
if (Math.max(Math.abs(dx), Math.abs(dy)) !== ring) continue
const x = Math.round(cx + dx * (W + PAD)), y = Math.round(cy + dy * (H + PAD))
if (!hit(x, y)) return { x, y, noOverlap: true }
}
}
}
return { x: Math.round(cx), y: Math.round(cy), noOverlap: false }
}
window.IWBPluginHost = {
register(manifest, setup) {
window.__calls.manifests.push(manifest)
@@ -179,7 +196,19 @@ async function main() {
canvas: {
getNodes: () => window.__nodes,
getSelectedNode: () => window.__sel,
createNode: (type, x, y, data) => { window.__calls.nodes.push({ type, x, y, data }); return 'n_new_' + window.__calls.nodes.length },
// 模拟宿主新语义:x/y 为 null/undefined ⇒ 走 findSpot(视口中心起螺旋找空位、避开已有节点)
createNode: (type, x, y, data) => {
const auto = x === null || x === undefined || y === null || y === undefined
const rec = { type, x, y, data, auto }
if (auto) {
const s = window.__mockFindSpot()
rec.x = s.x
rec.y = s.y
rec.noOverlap = s.noOverlap
}
window.__calls.nodes.push(rec)
return 'n_new_' + window.__calls.nodes.length
},
createImageNode: (payload) => { window.__calls.images.push(payload); return 'n_img_' + window.__calls.images.length },
},
}
@@ -342,8 +371,12 @@ async function main() {
const promptCall = await page.evaluate(() => window.__calls.nodes[0] || null)
check('插入提示词调用 createNode', Boolean(promptCall) && promptCall.type === 'prompt', promptCall ? 'type=' + promptCall.type : '未调用')
check('提示词带变量替换结果', Boolean(promptCall) && String(promptCall.data.text).includes('测试角色ABC'))
check('提示词插在选中节点右侧', Boolean(promptCall) && promptCall.x === 480 && promptCall.y === 120,
promptCall ? '(' + promptCall.x + ',' + promptCall.y + ') 期望 (480,120)' : '')
check('提示词坐标交给宿主(插件不自己算,拿不到 pan/zoom)',
Boolean(promptCall) && promptCall.auto === true,
promptCall ? 'auto=' + promptCall.auto + ' raw=(' + promptCall.x + ',' + promptCall.y + ')' : '未调用')
check('宿主定位结果不压在已有节点上',
Boolean(promptCall) && promptCall.noOverlap === true,
promptCall ? '(' + promptCall.x + ',' + promptCall.y + ')' : '')
// ---- 插参考图(blob → dataURL) ----
await page.locator('#hostA .iwb-ss-dfoot .iwb-ss-btn').first().click()