302 lines
17 KiB
JavaScript
302 lines
17 KiB
JavaScript
/* 「模型设置」面板(2026-10-30):生图(配置)节点的模型选择浮层
|
||
*
|
||
* 背景:原来节点参数行一行 4 个控件(服务商 / 分组 / 模型 / 尺寸),且 `.iwb-gen-row` 不换行,
|
||
* 节点只能靠 480px 宽度硬撑。服务商与分组是**低频操作**,移进这个面板后节点行只剩「模型 + 尺寸」。
|
||
*
|
||
* 模型列表的布局要求(用户明确):
|
||
* · 模型名**单行显示,永不省略**(省略就无法识别具体是哪个模型)
|
||
* · 短名一行 2 个;长名(放不下的)**自动独占一行**
|
||
* ⇒ 用 flex + `flex: 1 1 calc(50% - gap/2)` + `min-width: max-content` + `nowrap` 实现,
|
||
* 由浏览器按内容宽度决定换行,**不需要按字数猜、也不需要 canvas 测量**。
|
||
* 极长(比面板还宽)时由 `.iwb-gc-grid` 横向滚动兜底,仍然不省略、不换行。
|
||
*
|
||
* 约定:模块不捕获 app.js 符号,数据与回调全部由 props 传入(模型清单规则仍留在宿主,避免两处漂移)。
|
||
*/
|
||
;(function (global) {
|
||
'use strict'
|
||
const React = global.React
|
||
const ReactDOM = global.ReactDOM
|
||
if (!React || !React.createElement) return
|
||
const el = React.createElement
|
||
const useState = React.useState
|
||
const useRef = React.useRef
|
||
const useEffect = React.useEffect
|
||
|
||
function GenConfigPanel(props) {
|
||
const p = props || {}
|
||
const stop = (e) => e.stopPropagation()
|
||
const panelRef = useRef(null)
|
||
const [pos, setPos] = useState(null) // null = 未定位:先隐藏,避免首帧在左上角闪一下
|
||
const useLayoutEffect = React.useLayoutEffect || useEffect
|
||
|
||
useLayoutEffect(() => { // 绘制前定位
|
||
if (!p.anchor) return undefined
|
||
const place = () => {
|
||
if (typeof p.placePanel !== 'function') return
|
||
const next = p.placePanel(p.anchor, panelRef.current, global.innerWidth || 1200, global.innerHeight || 800)
|
||
if (next) setPos(next)
|
||
}
|
||
place()
|
||
// 首帧面板高度可能还没量到(为 0),下一帧再算一次,避免定位到错误位置
|
||
const raf = global.requestAnimationFrame ? global.requestAnimationFrame(place) : null
|
||
global.addEventListener('resize', place)
|
||
return () => {
|
||
global.removeEventListener('resize', place)
|
||
if (raf && global.cancelAnimationFrame) global.cancelAnimationFrame(raf)
|
||
}
|
||
}, [p.anchor])
|
||
|
||
// 点面板外关闭(捕获阶段 pointerdown);触发器自身排除,避免"先关后开"
|
||
useEffect(() => {
|
||
const onDown = (e) => {
|
||
const box = panelRef.current
|
||
const t = e.target
|
||
if (box && t && box.contains(t)) return
|
||
if (t && t.closest && (t.closest('.iwb-pick-genmodel') || t.closest('.iwb-gen-summary'))) return
|
||
if (typeof p.onClose === 'function') p.onClose()
|
||
}
|
||
global.document.addEventListener('pointerdown', onDown, true)
|
||
return () => global.document.removeEventListener('pointerdown', onDown, true)
|
||
}, [p.onClose])
|
||
|
||
useEffect(() => {
|
||
const onKey = (e) => { if (e.key === 'Escape') { e.preventDefault(); if (typeof p.onClose === 'function') p.onClose() } }
|
||
global.addEventListener('keydown', onKey)
|
||
return () => global.removeEventListener('keydown', onKey)
|
||
}, [p.onClose])
|
||
|
||
const providers = Array.isArray(p.providers) ? p.providers : []
|
||
const keyGroups = Array.isArray(p.keyGroups) ? p.keyGroups : []
|
||
const modelOpts = Array.isArray(p.modelOpts) ? p.modelOpts : []
|
||
const curProviderId = String(p.curProviderId || '')
|
||
const curKeyGroup = String(p.curKeyGroup || '')
|
||
const curModel = String(p.curModel || '')
|
||
|
||
const selectRow = el('div', { className: 'iwb-gc-row' },
|
||
el('label', { className: 'iwb-gc-field', title: '服务商(生图服务)' },
|
||
el('span', { className: 'iwb-gc-label' }, '服务商'),
|
||
el('select', {
|
||
className: 'iwb-gc-select',
|
||
value: curProviderId,
|
||
disabled: !providers.length,
|
||
onPointerDown: stop,
|
||
onChange: (e) => { if (typeof p.onPickProvider === 'function') p.onPickProvider(e.target.value) },
|
||
}, providers.length
|
||
? providers.map((pr, i) => el('option', { key: String(pr.id || i), value: String(pr.id || '') }, String(pr.name || pr.id || '')))
|
||
: el('option', { value: '' }, '无服务商'))),
|
||
el('label', { className: 'iwb-gc-field', title: keyGroups.length ? 'Key 分组' : '该服务商没有 Key 分组' },
|
||
el('span', { className: 'iwb-gc-label' }, '分组'),
|
||
el('select', {
|
||
className: 'iwb-gc-select',
|
||
value: curKeyGroup,
|
||
disabled: !keyGroups.length,
|
||
onPointerDown: stop,
|
||
onChange: (e) => { if (typeof p.onPickGroup === 'function') p.onPickGroup(e.target.value) },
|
||
}, keyGroups.length
|
||
? keyGroups.map((k, i) => el('option', { key: String(k.id || i), value: String(k.id || '') }, String(k.name || k.id || '')))
|
||
: el('option', { value: '' }, '无分组'))),
|
||
)
|
||
|
||
const grid = el('div', { className: 'iwb-gc-grid' },
|
||
modelOpts.length
|
||
? modelOpts.map((o) => {
|
||
const on = String(o.value) === curModel
|
||
const err = on && p.modelAvailable === false
|
||
return el('button', {
|
||
key: 'm_' + String(o.value),
|
||
className: 'iwb-gc-item' + (on ? ' iwb-gc-item-on' : '') + (err ? ' iwb-gc-item-err' : ''),
|
||
title: String(o.label), // 悬停也能看全名(虽然这里不省略)
|
||
onPointerDown: stop,
|
||
onClick: () => { if (typeof p.onPickModel === 'function') p.onPickModel(String(o.value)) },
|
||
},
|
||
el('span', { className: 'iwb-gc-name' }, String(o.label)),
|
||
on ? el('span', { className: 'iwb-gc-tick' }, '✓') : null)
|
||
})
|
||
: el('div', { className: 'iwb-gc-empty' }, '该服务商还没有模型,请先在「设置 → 生图服务」添加'))
|
||
|
||
// 与「图像设置」面板同一套外壳:.iwb-pop-anchor 提供 position: fixed + z-index: 3200,
|
||
// 缺了它面板会落在文档流里(位置 0,0 且被应用层盖住、点不动)。
|
||
const body = el('div', {
|
||
className: 'iwb-pop-anchor',
|
||
style: { left: (pos ? pos.x : 0) + 'px', top: (pos ? pos.y : 0) + 'px', visibility: pos ? 'visible' : 'hidden' },
|
||
onPointerDown: stop,
|
||
onWheel: (e) => e.stopPropagation(),
|
||
},
|
||
el('div', { ref: panelRef, className: 'iwb-pop iwb-gen-config', onPointerDown: stop },
|
||
el('div', { className: 'iwb-pop-head' },
|
||
el('span', { className: 'iwb-pop-title' }, '模型设置'),
|
||
),
|
||
selectRow,
|
||
el('div', { className: 'iwb-gc-sub' }, '模型'),
|
||
grid,
|
||
),
|
||
)
|
||
// **必须 portal 到 body**:节点在画布世界坐标的 transform 里,
|
||
// fixed 定位会被那个 transform 变成相对定位(位置算错 + 被裁切),
|
||
// 而且面板会参与节点布局把节点撑开(用户实测反馈)。
|
||
if (ReactDOM && typeof ReactDOM.createPortal === 'function') return ReactDOM.createPortal(body, global.document.body)
|
||
return body
|
||
}
|
||
|
||
// ── 节点背景色色板(2026-11-01)────────────────────────────────────────────
|
||
// 设计要点:
|
||
// · 预设 6 色(无 / 白 / 青 / 紫 / 橙 / 黄)——**不用自由取色器**:预设能保证深色文字
|
||
// 压在任何背景上都可读,也免掉跨端不一致的原生取色控件。
|
||
// · 纯静态 CSS 背景,无动画/滤镜 ⇒ 改色只让**该节点**重渲染一次(节点卡 memo + 引用比较),
|
||
// 不改色时开销恒为 0。
|
||
// · 一处实现三处调用:单选气泡 / 多选气泡 / 右键菜单(避免两套实现漂移)。
|
||
// 8 色令牌:无 / 白 / 青 / 紫 / 粉红 / 橙 / 黄 / 浅绿
|
||
// 2026-11-01 第二轮(用户要求):降低饱和度、提亮 ⇒ 莫兰迪式浅色;
|
||
// **暗色主题另配一套深色**(不是把浅色压暗)⇒ 色值由 CSS 按主题映射,
|
||
// 因此 data.bg 存的是**令牌**(如 'yellow'),不是 hex。
|
||
// 旧数据里若已是 hex(第一版写的),节点卡会走「原样自定义色」回退分支,不会丢色。
|
||
const BG_COLORS = [
|
||
{ k: '', t: '无背景' },
|
||
{ k: 'white', t: '白' },
|
||
{ k: 'cyan', t: '青' },
|
||
{ k: 'purple', t: '紫' },
|
||
{ k: 'pink', t: '粉红' },
|
||
{ k: 'orange', t: '橙' },
|
||
{ k: 'yellow', t: '黄' },
|
||
{ k: 'green', t: '浅绿' },
|
||
]
|
||
const ICON_PALETTE = 'M12 3a9 9 0 1 0 0 18h1.5a2.5 2.5 0 0 0 0-5H13a2 2 0 0 1 0-4h2.5A5.5 5.5 0 0 0 21 6.5C21 4.6 16.97 3 12 3ZM7.5 9.5h.01M11 7h.01M7 13h.01'
|
||
const iconEl = (d) => el('svg', { className: 'iwb-ic', viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor', strokeWidth: 2, strokeLinecap: 'round', strokeLinejoin: 'round', 'aria-hidden': true }, el('path', { d }, null))
|
||
const stopEv = (e) => e.stopPropagation()
|
||
// hover = 预览(只改 DOM,不写数据、不进撤销栈);click = 最终确认
|
||
const swatches = (cur, onPick, onPreview) => BG_COLORS.map((c) => el('button', {
|
||
key: 'bg-' + (c.k || 'none'), type: 'button', title: c.t, 'aria-label': c.t, 'data-bg': c.k || 'none',
|
||
className: 'iwb-bg-swatch' + (c.k ? '' : ' iwb-bg-swatch-none') + (String(cur || '') === c.k ? ' iwb-bg-swatch-on' : ''),
|
||
onMouseEnter: () => { if (typeof onPreview === 'function') onPreview(c.k) },
|
||
onClick: (e) => { e.stopPropagation(); onPick(c.k) },
|
||
}, null))
|
||
|
||
// 触发器模式(气泡上用):按钮 + portal 面板。props: { current, onPick(bg) }
|
||
function NodeBgPalette(props) {
|
||
const p = props || {}
|
||
const [open, setOpen] = useState(false)
|
||
const [pos, setPos] = useState({ x: 40, y: 40 })
|
||
const btnRef = useRef(null)
|
||
const panelRef = useRef(null)
|
||
useEffect(() => {
|
||
if (!open) return undefined
|
||
const place = () => {
|
||
const r = btnRef.current && btnRef.current.getBoundingClientRect ? btnRef.current.getBoundingClientRect() : null
|
||
const pw = (panelRef.current && panelRef.current.offsetWidth) || 240
|
||
const ph = (panelRef.current && panelRef.current.offsetHeight) || 44
|
||
const vw = global.innerWidth || 1200
|
||
const vh = global.innerHeight || 800
|
||
let x = r ? r.left : 40
|
||
let y = r ? r.bottom + 6 : 40
|
||
if (x + pw > vw - 8) x = Math.max(8, vw - pw - 8)
|
||
if (y + ph > vh - 8) y = r ? Math.max(8, r.top - ph - 6) : Math.max(8, vh - ph - 8)
|
||
setPos({ x: Math.round(x), y: Math.round(y) })
|
||
}
|
||
place()
|
||
const raf = global.requestAnimationFrame ? global.requestAnimationFrame(place) : null
|
||
const onDoc = (e) => {
|
||
const t = e.target
|
||
if (panelRef.current && panelRef.current.contains && panelRef.current.contains(t)) return
|
||
if (btnRef.current && btnRef.current.contains && btnRef.current.contains(t)) return
|
||
setOpen(false)
|
||
}
|
||
const onKey = (e) => { if (e.key === 'Escape') setOpen(false) }
|
||
global.document.addEventListener('pointerdown', onDoc, true)
|
||
global.document.addEventListener('keydown', onKey, true)
|
||
global.addEventListener('resize', place)
|
||
return () => {
|
||
if (raf) global.cancelAnimationFrame(raf)
|
||
global.document.removeEventListener('pointerdown', onDoc, true)
|
||
global.document.removeEventListener('keydown', onKey, true)
|
||
global.removeEventListener('resize', place)
|
||
}
|
||
}, [open])
|
||
const pick = (v) => {
|
||
setOpen(false)
|
||
try { if (typeof p.onPick === 'function') p.onPick(v) } catch (e) { /* 选色失败不得冒泡 */ }
|
||
if (typeof p.onPreviewEnd === 'function') p.onPreviewEnd() // 预览交还给真实数据
|
||
}
|
||
// 悬停即展开(省一次点击);鼠标离开按钮/色板后再延迟收起,留出移动时间
|
||
const closeTimer = useRef(null)
|
||
const openNow = () => { if (closeTimer.current) { clearTimeout(closeTimer.current); closeTimer.current = null } setOpen(true) }
|
||
const scheduleClose = () => {
|
||
if (closeTimer.current) clearTimeout(closeTimer.current)
|
||
closeTimer.current = setTimeout(() => {
|
||
closeTimer.current = null
|
||
setOpen(false)
|
||
if (typeof p.onPreviewEnd === 'function') p.onPreviewEnd() // 移开未确认 ⇒ 撤销预览
|
||
}, 600)
|
||
}
|
||
const curKey = String(p.current || '')
|
||
// 指针在「按钮 + 色板」范围内就一直保持展开,离开后才收起。
|
||
// 用文档级 pointermove 判断(而不是按钮/面板各自的 mouseenter/leave),
|
||
// 避免鼠标在两者之间移动时反复开关造成抖动(2026-11-01 实测踩到)。
|
||
useEffect(() => {
|
||
if (!open) return
|
||
const onMove = (e) => {
|
||
const inBtn = btnRef.current && btnRef.current.contains(e.target)
|
||
const inPanel = panelRef.current && panelRef.current.contains(e.target)
|
||
if (inBtn || inPanel) openNow()
|
||
else scheduleClose()
|
||
}
|
||
document.addEventListener('pointermove', onMove, true)
|
||
return () => document.removeEventListener('pointermove', onMove, true)
|
||
}, [open])
|
||
const btn = el('button', {
|
||
ref: btnRef, 'aria-label': '背景色', 'data-tip': '背景色', onPointerDown: stopEv,
|
||
onMouseEnter: openNow,
|
||
// 点击**只打开不切换**:hover 已经展开时再点一下不应把色板关掉(触屏也能打开;关闭靠选色/移开/Esc)
|
||
onClick: (e) => { e.stopPropagation(); openNow() },
|
||
}, el('span', { className: 'iwb-bg-dot iwb-bg-swatch' + (curKey ? '' : ' iwb-bg-swatch-none'), 'data-bg': curKey || 'none', 'aria-hidden': 'true' }, null))
|
||
if (!open) return btn
|
||
const anchor = el('div', { className: 'iwb-pop-anchor', style: { left: pos.x + 'px', top: pos.y + 'px' }, onWheel: stopEv },
|
||
el('div', { ref: panelRef, className: 'iwb-pop iwb-bg-palette', onPointerDown: stopEv, onMouseLeave: () => { if (typeof p.onPreviewEnd === 'function') p.onPreviewEnd() } }, swatches(p.current, pick, p.onPreview)),
|
||
)
|
||
return el(React.Fragment, null, btn, ReactDOM && typeof ReactDOM.createPortal === 'function' ? ReactDOM.createPortal(anchor, global.document.body) : anchor)
|
||
}
|
||
|
||
// 非触发器场景(右键菜单):临时挂一个色板到 body,选完 / 点外部 / Esc 即卸载。
|
||
// opts: { current, anchorRect, onPick(bg) }
|
||
function openNodeBgPalette(opts) {
|
||
const o = opts || {}
|
||
if (!ReactDOM || typeof ReactDOM.createRoot !== 'function' || !global.document) return
|
||
const host = global.document.createElement('div')
|
||
host.className = 'iwb-bg-palette-host'
|
||
global.document.body.appendChild(host)
|
||
const root = ReactDOM.createRoot(host)
|
||
const teardown = () => { try { root.unmount() } catch (e) { /* ignore */ } if (host.parentNode) host.parentNode.removeChild(host) }
|
||
const r = o.anchorRect || { left: 40, top: 40, bottom: 40 }
|
||
const Wrap = function () {
|
||
const [pos, setPos] = useState(null)
|
||
const panelRef = useRef(null)
|
||
useEffect(() => {
|
||
if (!panelRef.current || pos) return
|
||
const vw = global.innerWidth || 1200
|
||
const vh = global.innerHeight || 800
|
||
const pw = panelRef.current.offsetWidth || 240
|
||
const ph = panelRef.current.offsetHeight || 44
|
||
const x = Math.max(8, Math.min(Math.round(r.left || 40), vw - pw - 8))
|
||
const y = (r.bottom || 40) + 6 + ph > vh - 8 ? Math.max(8, (r.top || 40) - ph - 6) : (r.bottom || 40) + 6
|
||
setPos({ x, y })
|
||
})
|
||
useEffect(() => {
|
||
const onDoc = (e) => { if (panelRef.current && panelRef.current.contains && panelRef.current.contains(e.target)) return; teardown() }
|
||
const onKey = (e) => { if (e.key === 'Escape') teardown() }
|
||
const t = global.setTimeout(() => {
|
||
global.document.addEventListener('pointerdown', onDoc, true)
|
||
global.document.addEventListener('keydown', onKey, true)
|
||
}, 0)
|
||
return () => { global.clearTimeout(t); global.document.removeEventListener('pointerdown', onDoc, true); global.document.removeEventListener('keydown', onKey, true) }
|
||
}, [])
|
||
const box = el('div', { ref: panelRef, className: 'iwb-pop iwb-bg-palette', onPointerDown: stopEv },
|
||
swatches(o.current, (v) => { teardown(); try { if (typeof o.onPick === 'function') o.onPick(v) } catch (err) { /* ignore */ } if (typeof o.onPreviewEnd === 'function') o.onPreviewEnd() }, o.onPreview),
|
||
)
|
||
if (!pos) return el('div', { className: 'iwb-pop-anchor', style: { left: (r.left || 40) + 'px', top: (r.bottom || 40) + 'px', visibility: 'hidden', position: 'fixed' } }, box)
|
||
return el('div', { className: 'iwb-pop-anchor', style: { left: pos.x + 'px', top: pos.y + 'px' } }, box)
|
||
}
|
||
root.render(el(Wrap, null))
|
||
}
|
||
|
||
global.IWBUIGenConfigPanel = { GenConfigPanel, NodeBgPalette, openNodeBgPalette, BG_COLORS }
|
||
})(typeof window !== 'undefined' ? window : globalThis)
|