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

+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: {