138 lines
6.0 KiB
JavaScript
138 lines
6.0 KiB
JavaScript
// 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)
|