Files
image-workbench-desktop/plugins/runtime/plugin-blank-canvas.80bcccc04527.js
T
qiqi 036865a201
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 38s
chore(plugins+preflight): white-bg-800 1.2.3 / blank-canvas 1.0.1 产物 + preflight B1/B2/B3
插件批次(插件仓 e2982e3,产物同步到 plugins/):
- white-bg-800 1.2.3:覆盖模式走 saveNodeImage(落盘校验/失败回滚/可读原因),老宿主回退同步 setNodeImage;
  harness 改为双形态(new/old)并把两处过期断言改对
- blank-canvas 1.0.1:空白判定补 localRef/ref,避免只有本地引用的节点被白图覆盖

preflight / 守卫:
- B1 preflight 第 6 项:扫现行文档里已下线的路径与命令(functions/api、verify:release、plugins-publish、_inbox、wrangler),
  删除线/已弃用豁免;首次运行抓到 4 处仍在教 NAS _inbox 取件的过期说明,改为 tailnet HTTP 流程
- B2 source-text-ratchet 增加 src global.* 写入口棘轮(实测 122,只减不增)
- B3 棘轮上限行必须带「日期 + 相对上一版的差(或基线说明)+ 原因」,否则红(防"谁写谁抬")
2026-12-02 08:53:02 +08:00

141 lines
6.3 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.
// iwb-plugin: 空白画布 —— 给「空白的参考图节点」填入指定尺寸(1K)的纯白图片,作为插图/标注的起点。
// 移植自 iwb-plaus 仓库 iwb-cutout-extension:src/content.js 的 BLANK_RATIOS / performBlankCanvas / showBlankMenu。
// 有意差异:只保留 1K 档(上游菜单实际也只用 1K);只填当前节点(不新建);不联动编辑器。
;(function (global) {
'use strict'
// 与宿主 app.js 的 RATIOS 表 1K 档逐字一致。插件不能 import 宿主内部,故照抄数值;
// 上游扩展也是这么做的(BLANK_RATIOS 抄自宿主)。
var SIZES = [
{ label: '1:1 方形', w: 1024, h: 1024 },
{ label: '2:3 竖版', w: 832, h: 1248 },
{ label: '3:2 横版', w: 1248, h: 832 },
{ label: '3:4 竖版', w: 880, h: 1168 },
{ label: '4:3 横版', w: 1184, h: 880 },
{ label: '4:5 竖版', w: 912, h: 1136 },
{ label: '5:4 横版', w: 1136, h: 896 },
{ label: '9:16 竖版', w: 768, h: 1360 },
{ label: '16:9 横版', w: 1360, h: 752 },
{ label: '21:9 超宽', w: 1552, h: 656 },
{ label: '9:21 超宽竖版', w: 656, h: 1520 },
]
var CSS = [
'.iwb-blankcanvas-menu{position:fixed;left:50%;top:64px;transform:translateX(-50%);z-index:2147483000;',
'width:312px;padding:10px;border-radius:10px;box-sizing:border-box;',
'background:var(--iwb-panel,#fff);color:var(--iwb-text,#1c2333);',
'border:1px solid var(--iwb-border,#d5d9e0);box-shadow:0 12px 32px rgba(0,0,0,.28);',
'font:12px/1.4 system-ui,-apple-system,"PingFang SC",sans-serif}',
'.iwb-blankcanvas-title{font-weight:600;margin:2px 2px 8px}',
'.iwb-blankcanvas-grid{display:grid;grid-template-columns:repeat(3,1fr);gap:6px}',
'.iwb-blankcanvas-item{display:flex;flex-direction:column;align-items:center;gap:2px;padding:8px 4px;',
'border-radius:8px;border:1px solid var(--iwb-border,#d5d9e0);background:var(--iwb-layer,#f0f1f4);',
'color:inherit;font:inherit;cursor:pointer}',
'.iwb-blankcanvas-item:hover{background:var(--iwb-layer2,#fff)}',
'.iwb-blankcanvas-item b{font-weight:600}',
'.iwb-blankcanvas-item i{font-style:normal;font-size:11px;color:var(--iwb-text2,#4d5668)}',
].join('')
global.IWBPluginHost.register({
id: 'blank-canvas',
name: '空白画布',
version: '1.0.1',
apiVersion: 1,
capabilities: ['nodeActions', 'canvas', 'canvasWrite'],
}, function (ctx) {
var STYLE_ID = 'iwb-blankcanvas-style'
var menuEl = null
var outsideHandler = null
function injectStyle() {
if (document.getElementById(STYLE_ID)) return
var style = document.createElement('style')
style.id = STYLE_ID
style.textContent = CSS
;(document.head || document.documentElement).appendChild(style)
}
// 与上游 isEmptyRefNode 等价:ref 节点且既无本地数据也无 URL({__blob}/{__cache} 引用是对象,天然为真)
// 空白判定:data / url / localRef / ref 任一存在都不算空白。
// v1.0.1:宿主 3.26.183 起大图会被引用化(data 变成 {__blob}/{__file} 对象、localRef/ref 同时指向它),
// 只看 data/url 时「只有本地引用」的节点会被误判为空、直接被白图覆盖掉。
function isEmptyImageNode(node) {
if (!node || node.type !== 'ref') return false
var d = node.data || {}
return !d.data && !d.url && !d.localRef && !d.ref
}
function whitePng(w, h) {
var canvas = document.createElement('canvas')
canvas.width = w
canvas.height = h
var ctx2d = canvas.getContext('2d')
ctx2d.fillStyle = '#ffffff'
ctx2d.fillRect(0, 0, w, h)
return canvas.toDataURL('image/png')
}
function closeMenu() {
if (menuEl && menuEl.parentNode) menuEl.parentNode.removeChild(menuEl)
menuEl = null
if (outsideHandler) { document.removeEventListener('pointerdown', outsideHandler, true); outsideHandler = null }
document.removeEventListener('keydown', onKeyDown, true)
}
function onKeyDown(e) { if (e.key === 'Escape') closeMenu() }
function apply(nodeId, w, h) {
var nodes = (ctx.canvas && typeof ctx.canvas.getNodes === 'function') ? ctx.canvas.getNodes() : []
var target = null
for (var i = 0; i < nodes.length; i++) if (nodes[i] && nodes[i].id === nodeId) target = nodes[i]
if (!target || !isEmptyImageNode(target)) throw new Error('该节点不是空白的图片节点')
// 宿主把 canvasWrite 的 API 合并进 ctx.canvas(plugin-host.js 的 CAPABILITY_API_KEYS)⇒ 写接口在 ctx.canvas 上
if (!ctx.canvas.setNodeImage(nodeId, whitePng(w, h))) throw new Error('写入节点失败')
}
function openMenu(nodeId) {
injectStyle()
closeMenu()
var menu = document.createElement('div')
menu.className = 'iwb-blankcanvas-menu'
var title = document.createElement('div')
title.className = 'iwb-blankcanvas-title'
title.textContent = '创建空白画布(1K)'
menu.appendChild(title)
var grid = document.createElement('div')
grid.className = 'iwb-blankcanvas-grid'
SIZES.forEach(function (size) {
var btn = document.createElement('button')
btn.type = 'button'
btn.className = 'iwb-blankcanvas-item'
btn.setAttribute('data-size', size.w + 'x' + size.h)
btn.innerHTML = '<b>' + size.label + '</b><i>' + size.w + '×' + size.h + '</i>'
btn.addEventListener('pointerdown', function (e) { e.stopPropagation() })
btn.addEventListener('click', function (e) {
e.stopPropagation()
e.preventDefault()
closeMenu()
apply(nodeId, size.w, size.h)
})
grid.appendChild(btn)
})
menu.appendChild(grid)
document.body.appendChild(menu)
menuEl = menu
outsideHandler = function (e) { if (!menuEl || !menuEl.contains(e.target)) closeMenu() }
document.addEventListener('pointerdown', outsideHandler, true)
document.addEventListener('keydown', onKeyDown, true)
}
ctx.nodeActions.register({
id: 'blank-canvas-open',
title: '空白画布',
icon: 'plus',
appliesTo: function (node) { return isEmptyImageNode(node) },
run: function (node) { openMenu(node.id); return true },
})
return { dispose: closeMenu }
})
})(typeof window !== 'undefined' ? window : globalThis)