feat(plugin): 新增 blank-canvas 空白画布插件(移植自 iwb-cutout-extension)
- 仅空白 ref 节点出现入口;11 种 1K 比例;纯白 PNG 原地填当前节点 - 与上游差异:只 1K、只填当前节点、不联动编辑器
This commit is contained in:
1 parent
d41e85e288
commit
9e09a48285
3 files changed
+186
No files matched your search
@@ -0,0 +1,42 @@
|
||||
# 空白画布(插件)
|
||||
|
||||
给**空白的参考图节点**填入一张指定尺寸的**纯白图片**,作为插图 / 标注的起点。
|
||||
典型用途:新建参考图节点(还没有图)→ 直接铺一张 1024×1024 白底 → 在上面标注或继续合成。
|
||||
|
||||
**移植自** `iwb-plaus` 仓库的 `iwb-cutout-extension`:
|
||||
- 源:`src/content.js` 的 `BLANK_RATIOS`(尺寸表)、`performBlankCanvas`(合成与写回)、`showBlankMenu`(选择菜单)。
|
||||
- 只有宿主胶水(`IWBPluginHost` 注册 / `ctx` 调用)是新写的;尺寸表与合成方式逐字对齐上游。
|
||||
|
||||
### 与上游的差异(有意为之)
|
||||
- **只保留 1K 档**:上游表里有 1K/2K/4K,但菜单实际只用 1K(`sizes['1K']`)。
|
||||
- **只填当前节点**:上游也是覆盖当前空节点;本插件不提供"新建节点"模式。
|
||||
- **不联动编辑器**:上游建完会自动打开它自己的编辑器;这里的编辑器是另一个插件(`image-annotate`),本插件不跨插件调用。
|
||||
- **入口是节点浮动栏按钮**:仅当选中**空白 ref 节点**时出现(`nodeActions.appliesTo`),不常驻。
|
||||
|
||||
## 入口
|
||||
- 选中一个**空白参考图节点** → 节点浮动栏出现「空白画布」按钮 → 点开选尺寸 → 立即填入纯白 PNG。
|
||||
|
||||
## 尺寸(与宿主 `RATIOS` 的 1K 档一致)
|
||||
|
||||
| 比例 | 尺寸 | 比例 | 尺寸 |
|
||||
|---|---|---|---|
|
||||
| 1:1 方形 | 1024×1024 | 5:4 横版 | 1136×896 |
|
||||
| 2:3 竖版 | 832×1248 | 9:16 竖版 | 768×1360 |
|
||||
| 3:2 横版 | 1248×832 | 16:9 横版 | 1360×752 |
|
||||
| 3:4 竖版 | 880×1168 | 21:9 超宽 | 1552×656 |
|
||||
| 4:3 横版 | 1184×880 | 9:21 超宽竖版 | 656×1520 |
|
||||
| 4:5 竖版 | 912×1136 | | |
|
||||
|
||||
## 实现要点
|
||||
- 合成:`canvas(w,h)` → `fillStyle='#ffffff'` → `fillRect` → `toDataURL('image/png')`。
|
||||
- 写回:`ctx.canvas.setNodeImage(nodeId, dataUrl)`(原地覆盖,`url` 清空)。注意宿主把 `canvasWrite` 的 API **合并进 `ctx.canvas`**,`ctx.canvasWrite` 并不存在 —— 但 manifest 的 `capabilities` 仍须同时声明 `canvas` 与 `canvasWrite`,否则拿不到写接口。
|
||||
- 空白判定:`node.type === 'ref' && !data.data && !data.url`(与上游 `isEmptyRefNode` 等价)。
|
||||
- 样式用宿主 CSS 变量(`--iwb-panel/--iwb-text/--iwb-border/--iwb-layer`),明暗主题自适应;CSP 允许 `style-src 'unsafe-inline'`。
|
||||
|
||||
## 已知限制
|
||||
- `setNodeImage` 不改节点名,所以不会像上游那样命名为 `<名字>_blank_<W>x<h>.png`(节点名保持不变)。若需要文件名,得宿主开放"改节点名"的写接口。
|
||||
- 1K 纯白 PNG 的 dataURL 约 30–34 KB,落库时会被宿主引用化为 `{__blob}`(超过 3000 字符阈值),属正常路径。
|
||||
|
||||
## 发布
|
||||
与其它插件一致:先 `npm run validate`,再由宿主仓的 `scripts/plugins-publish.mjs` 以哈希名入库。
|
||||
⚠️ 发布时**必须把所有插件文件一起传入**(发布器用传入的文件重建清单,不做合并)。
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"name": "@iwb-plugin/blank-canvas",
|
||||
"private": true,
|
||||
"version": "1.0.0",
|
||||
"description": "空白画布:给空白的参考图节点填入指定尺寸(1K)的纯白图片,作为插图/标注的起点",
|
||||
"ibwPlugin": { "id": "blank-canvas", "entry": "src/index.js", "apiVersion": 1 }
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
// 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.0',
|
||||
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} 引用是对象,天然为真)
|
||||
function isEmptyImageNode(node) {
|
||||
if (!node || node.type !== 'ref') return false
|
||||
var d = node.data || {}
|
||||
return !d.data && !d.url
|
||||
}
|
||||
|
||||
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)
|
||||
Reference in new issue
Block a user