From 99717eb5e8ff11414a331e09c44a7dbc1e0209b7 Mon Sep 17 00:00:00 2001 From: qiqi Date: Mon, 2 Nov 2026 09:50:25 +0800 Subject: [PATCH] =?UTF-8?q?feat(style-source):=20=E5=88=86=E7=B1=BB=20chip?= =?UTF-8?q?s=20=E9=9A=8F=E9=9D=A2=E6=9D=BF=E5=AE=BD=E5=BA=A6=E6=8D=A2?= =?UTF-8?q?=E8=A1=8C=20+=20=E9=95=BF=E6=8F=90=E7=A4=BA=E8=AF=8D=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E5=86=85=E6=BB=9A=E5=8A=A8=20(1.0.1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 分类 chips: - 由「单行 + 横向滚动 + scrollbar 隐藏」改为 flex-wrap:宽档一行铺完,窄档折两三行, 右边几类不再永远看不见(用户以为只有 7 个分类) - 名称/数量拆 span,窄档(<480)只藏数量后缀,少折一行 - harness 新增 4 条守门:chips 全部落在容器内 / 窄档多行 / 宽档一行 / 数量后缀随档显隐 长提示词: - 宿主 autoGrow 把 textarea 高度设成 scrollHeight 且无上限,一条长提示词会把节点拉成 两米高柱子;插件挂载时注入 `.iwb-node .iwb-textarea-plain{max-height:240px}`(幂等、 只碰节点内纯文本域,不动宿主弹窗),节点尺寸固定、超出部分节点内滚动,全文走「放大编辑」⌘E - 新增设置项 promptMaxH(120~800,默认 240);插入时长文本额外提示一次 - harness 新增 3 条:样式注入 / 内联 height=1200 被压到 240 / overflow-y 为 auto --- style-source/README.md | 23 +++++++- style-source/package.json | 2 +- style-source/src/index.js | 96 +++++++++++++++++++++++++++------- style-source/tools/harness.mjs | 53 ++++++++++++++++++- 4 files changed, 151 insertions(+), 23 deletions(-) diff --git a/style-source/README.md b/style-source/README.md index 914b331..71a497d 100644 --- a/style-source/README.md +++ b/style-source/README.md @@ -15,7 +15,7 @@ ## 交互 - **搜索**:匹配名称 / 编号 / id / 分类 / 摘要 / 标签 / 触发词,150ms 防抖。 -- **分类**:单行横向滚动的 chips,按数量降序,记住上次选的分类。 +- **分类**:chips 按面板宽度**自动换行**(宽档一行铺完,窄档折两三行,永远不会把后面的分类藏起来);按数量降序,记住上次选的分类。窄档不显示数量后缀,省一行高度。 - **网格**:分批渲染(每批 90 个),滚到底自动续渲染;缩略图原生懒加载。 - **详情**:窄面板铺满覆盖层,宽面板变右侧栏;大图 + 标签 + 摘要 + 提示词 + 变量表单。 - **变量替换**:提示词里的 `{角色描述}` 这类占位符给输入框,填了就替换,**留空的保留 `{原占位符}`**(换成空串会把提示词结构弄丢)。未填数量实时提示。 @@ -38,6 +38,25 @@ | 键 | 说明 | |---|---| | `dataUrl` | styles.json 地址,默认 `https://malongan.github.io/style-source/data/styles.json`。国内访问慢可自行镜像后改这里。 | +| `promptMaxH` | 文本节点最大高度(px,默认 240,可填 120~800)。见下节。 | + +## 长提示词:节点内滚动,不把画布撑爆 + +风格提示词动辄上千字。宿主 `renderPromptBody` 用 `autoGrow` 把 textarea 高度直接设成 `scrollHeight`(**没有上限**), +所以一条长提示词插进画布会变成一根两米高的柱子,一张卡吃掉整个视口。 + +插件在挂载时注入一条(幂等的)样式: + +```css +.iwb-node .iwb-textarea-plain { max-height: 240px } /* 高度来自设置项 promptMaxH */ +``` + +- 节点尺寸从此**固定**(≈ max-height + 内边距),插了几张卡一眼看清; +- 宿主 `.iwb-textarea` 本来就是 `overflow-y: auto`,超出的部分在节点内滚动,**内容一个字都不丢**; +- 想看全文照旧用节点气泡的「**放大编辑**」(⌘/Ctrl + E),那是不限高的浮层; +- 选择器只碰**节点内**的纯文本域,宿主的弹窗 / 设置面板(`.iwb-modal-lg .iwb-textarea`)不受影响。 + +> 这条规则在宿主 `style.css` 里补一次更「正」,但插件自带可以保证换宿主版本也生效;宿主自己后续若要收编,删掉插件里的 `ensureNodeCapStyle` 即可。 插图默认用 `preview_webp`(大图,清晰)。详情里勾「插图用缩略图」改用 `*.thumb.webp`,省画布体积(dataURL 会让存档膨胀约 33%)。 @@ -52,7 +71,7 @@ ```bash cp src/index.js /tmp/plugin-style-source.js cd ../image-workbench-web -node scripts/plugins-publish.mjs /tmp/plugin-<其余插件>.js /tmp/plugin-style-source.js --upload --version style-source=1.0.0 +node scripts/plugins-publish.mjs /tmp/plugin-<其余插件>.js /tmp/plugin-style-source.js --upload --version style-source=1.0.1 ``` 图标:宿主 `app.js` 的 `PLUGIN_ICONS` 可加 `'style-source': 'palette'`(不加则用面板 manifest 自带的 `palette`,效果一致)。 diff --git a/style-source/package.json b/style-source/package.json index 7c8aad5..d1326bf 100644 --- a/style-source/package.json +++ b/style-source/package.json @@ -1,7 +1,7 @@ { "name": "@iwb-plugin/style-source", "private": true, - "version": "1.0.0", + "version": "1.0.1", "description": "风格库:资源库面板里浏览 310 个 AI 绘画风格(12 分类 / 搜索 / 变量替换),一键把预览图插入画布 ref 节点、把提示词插入 prompt 节点", "ibwPlugin": { "id": "style-source", diff --git a/style-source/src/index.js b/style-source/src/index.js index e2a008e..0975555 100644 --- a/style-source/src/index.js +++ b/style-source/src/index.js @@ -132,20 +132,27 @@ .iwb-ss-btn.is-key { border-color: var(--iwb-brand, #6b7280); } .iwb-ss-btn.is-key:hover:not(:disabled) { background: var(--iwb-brand, #6b7280); color: var(--iwb-panel, #fff); } -/* 分类 chips:单行横向滚动。窄面板下如果换行会直接吃掉半屏高度 */ +/* 分类 chips:按面板宽度自动换行。 + 以前是单行横向滚动(scrollbar 又藏起来了):面板拖窄/拖宽时右边那几类永远看不见,用户以为只有 7 个分类。 + 现在交给 flex-wrap 排布 —— 宽档一行放完,窄档自然折成两三行,全部可见。 + min-width:0 仍要留着:chips 的 max-content 近 950px,不允许收缩就会把祖先容器顶宽。 */ .iwb-ss-chips { - flex: 0 0 auto; display: flex; gap: 4px; padding: 0 10px 7px; - overflow-x: auto; overflow-y: hidden; scrollbar-width: none; - /* 13 个 chip 不换行 ⇒ max-content 近 950px。不夹住会让祖先容器按固有宽度撑开 */ + flex: 0 0 auto; display: flex; flex-wrap: wrap; align-content: flex-start; + gap: 4px; padding: 0 10px 7px; min-width: 0; max-width: 100%; } -.iwb-ss-chips::-webkit-scrollbar { display: none; } .iwb-ss-chip { - flex: 0 0 auto; height: 22px; padding: 0 9px; + flex: 0 1 auto; display: inline-flex; align-items: center; min-width: 0; max-width: 100%; + height: 22px; padding: 0 9px; border: 1px solid var(--iwb-border, #d5d9e0); border-radius: 11px; background: transparent; color: var(--iwb-text2, #4d5668); font-size: var(--fs-sm, 11px); cursor: pointer; white-space: nowrap; font-family: inherit; } +.iwb-ss-chipname { overflow: hidden; text-overflow: ellipsis; } +.iwb-ss-chipn { flex: 0 0 auto; margin-left: 4px; opacity: .6; } +/* 窄档(侧栏 240~480)不显示数量:13 个分类带上数字要折成 4 行,白吃掉 100px 高度;只留分类名能少一行 */ +.iwb-ss-root[data-layout="narrow"] .iwb-ss-chipn { display: none; } +.iwb-ss-root[data-layout="narrow"] .iwb-ss-chip { padding: 0 7px; } .iwb-ss-chip:hover { border-color: var(--iwb-border2, #b9bfca); } .iwb-ss-chip.is-on { background: var(--iwb-brand, #6b7280); border-color: var(--iwb-brand, #6b7280); color: var(--iwb-panel, #fff); } @@ -273,6 +280,38 @@ } catch (error) { /* 样式注入失败不影响功能,只是丑一点 */ } } + // ---------------- 长提示词:节点内滚动,不让节点越拉越长 ---------------- + // + // 宿主 renderPromptBody 用 autoGrow 把 textarea 高度直接设成 scrollHeight(无上限), + // 所以一条 1500 字的风格提示词插进画布会变成一根两米高的柱子,一张卡就吃掉整个视口。 + // 宿主里 prompt/textout/plugin 用的都是 .iwb-textarea-plain,这里给这类文本域加一个 max-height: + // · 不再增长 ⇒ 节点尺寸固定(≈ max-height + 内边距),一眼能看出插了几张卡; + // · 宿主 .iwb-textarea 本身是 overflow-y:auto,超出部分在节点内滚动,内容一个字都不丢; + // · 想看全文照旧用节点气泡的「放大编辑」(⌘/Ctrl+E),那里是不限高的浮层。 + // 选择器只碰节点内的纯文本域,宿主的弹窗/设置面板(.iwb-modal-lg .iwb-textarea)不受影响。 + const PROMPT_MAX_H = 240 + + function capHeightOf(ctx) { + let raw = '' + try { raw = ctx.settings.get('promptMaxH') } catch (error) { raw = '' } + const num = Math.round(Number(raw)) + if (!Number.isFinite(num) || num <= 0) return PROMPT_MAX_H + return Math.max(120, Math.min(800, num)) + } + + function ensureNodeCapStyle(ctx) { + const id = 'iwb-ss-nodecap' + const maxH = capHeightOf(ctx) + let node = document.getElementById(id) + try { + if (!node) { + node = el('style', { id: id }) + ;(document.head || document.documentElement).appendChild(node) + } + node.textContent = '.iwb-node .iwb-textarea-plain{max-height:' + maxH + 'px}' + } catch (error) { /* 注入失败只是节点还会被拉长,不影响插入 */ } + } + // ---------------- 数据 ---------------- function dataUrlOf(ctx) { @@ -431,6 +470,7 @@ } ensureStyle() + ensureNodeCapStyle(ctx) host.innerHTML = '' // ⚠️ 宿主给的面板容器 .iwb-full-panel-plugin 是裸 div,没任何样式,flex 子项的 min-width 默认 auto。 // 分类 chips 是单行不换行(13 个),它的 max-content 约 950px,会把容器顶到 950, @@ -543,29 +583,40 @@ const id = api.createNode('prompt', spot.x, spot.y, { text: text }) if (!id) { toast('插入失败:prompt 节点类型未注册'); return } const left = unfilledCount(style, values) - toast(left ? '已插入提示词(还有 ' + left + ' 个变量没填)' : '已插入提示词') + // 超过最大高度就会在节点内滚动:说一声,免得用户以为内容被截掉了 + const capped = text.split('\n').reduce((n, line) => n + Math.max(1, Math.ceil(line.length / 24)), 0) * 19 > capHeightOf(ctx) + const tail = capped ? ',长内容在节点内滚动(⌘E 看全文)' : '' + toast((left ? '已插入提示词(还有 ' + left + ' 个变量没填)' : '已插入提示词') + tail) } // ---- 渲染 ---- + // 名称与数量拆成两个 span:窄档只藏数量(.iwb-ss-chipn 在 CSS 里 display:none), + // 名称单独 overflow:hidden 才能在极端窄的时候省略号收尾而不是把 chip 撑出面板 + function chipNode(text, count, on, title, active) { + return el('button', { + class: 'iwb-ss-chip' + (active ? ' is-on' : ''), + title: title || (text + ' ' + count), + onClick: on, + }, [ + el('span', { class: 'iwb-ss-chipname', text: text }), + el('span', { class: 'iwb-ss-chipn', text: String(count) }), + ]) + } + function renderChips() { const all = (CACHE.data && CACHE.data.styles) || [] const counts = {} for (const s of all) counts[s.category] = (counts[s.category] || 0) + 1 const keys = Object.keys(counts).sort((a, b) => counts[b] - counts[a]) ui.chips.innerHTML = '' - ui.chips.appendChild(el('button', { - class: 'iwb-ss-chip' + (CACHE.cat === 'all' ? ' is-on' : ''), - text: '全部 ' + all.length, - onClick: () => { CACHE.cat = 'all'; prefSet('cat', 'all'); renderAll() }, - })) + ui.chips.appendChild(chipNode('全部', all.length, () => { + CACHE.cat = 'all'; prefSet('cat', 'all'); renderAll() + }, '全部', CACHE.cat === 'all')) for (const key of keys) { - ui.chips.appendChild(el('button', { - class: 'iwb-ss-chip' + (CACHE.cat === key ? ' is-on' : ''), - text: catName(key) + ' ' + counts[key], - title: key, - onClick: () => { CACHE.cat = key; prefSet('cat', key); renderAll() }, - })) + ui.chips.appendChild(chipNode(catName(key), counts[key], () => { + CACHE.cat = key; prefSet('cat', key); renderAll() + }, key, CACHE.cat === key)) } } @@ -730,6 +781,7 @@ }) ui.btnPrompt = el('button', { class: 'iwb-ss-btn is-key', text: '插入提示词', + title: '插入为提示词节点;内容按「最大高度」截断显示,节点内可滚动,全文用气泡「放大编辑」⌘E 查看', onClick: () => insertPrompt(style), }) @@ -833,7 +885,7 @@ global.IWBPluginHost.register({ id: 'style-source', name: '风格库', - version: '1.0.0', + version: '1.0.1', apiVersion: 1, capabilities: ['panels', 'canvas', 'canvasWrite'], settings: { @@ -843,6 +895,12 @@ desc: 'styles.json 的地址。默认直连 GitHub Pages;国内慢可自行镜像后改这里。', default: DEFAULT_DATA, }, + promptMaxH: { + type: 'text', + label: '文本节点最大高度(px)', + desc: '提示词超过这个高度就不再撑高节点,改为节点内滚动(120~800)。想看全文用节点气泡的「放大编辑」⌘E。留空 = 240。', + default: String(PROMPT_MAX_H), + }, }, }, function (ctx) { const prefs = prefAll() diff --git a/style-source/tools/harness.mjs b/style-source/tools/harness.mjs index fb755cc..a26086c 100644 --- a/style-source/tools/harness.mjs +++ b/style-source/tools/harness.mjs @@ -126,7 +126,7 @@ async function main() { const m = window.__manifest return { id: m.id, name: m.name, version: m.version, apiVersion: m.apiVersion, caps: m.capabilities, hasDataUrl: Boolean(m.settings && m.settings.dataUrl) } }) - check('manifest id/version 正确', manifest.id === 'style-source' && manifest.version === '1.0.0', JSON.stringify({ id: manifest.id, version: manifest.version })) + check('manifest id/version 正确', manifest.id === 'style-source' && manifest.version === '1.0.1', JSON.stringify({ id: manifest.id, version: manifest.version })) check('capabilities 含 panels+canvas+canvasWrite', ['panels', 'canvas', 'canvasWrite'].every((c) => manifest.caps.includes(c)), manifest.caps.join(',')) check('settings 暴露 dataUrl', manifest.hasDataUrl) @@ -159,6 +159,29 @@ async function main() { check('注册面板「风格库」+ palette 图标', mounted.title === '风格库' && mounted.icon === 'palette', JSON.stringify(mounted)) + // ---- 长提示词:节点内滚动(宿主 autoGrow 把 textarea 撑成 scrollHeight,一条长提示词能拉成一米高) ---- + const cap = await page.evaluate(() => { + const st = document.getElementById('iwb-ss-nodecap') + if (!st) return { injected: false } + // 造一个和宿主一致的节点结构,验证 max-height 真能压住内联 height + const wrap = document.createElement('div') + wrap.className = 'iwb-node' + wrap.style.cssText = 'width:280px;display:flex;flex-direction:column' + const ta = document.createElement('textarea') + ta.className = 'iwb-textarea iwb-textarea-plain' + ta.style.height = '1200px' // autoGrow 的产物 + ta.value = 'x'.repeat(4000) + wrap.appendChild(ta) + document.getElementById('stage').appendChild(wrap) + const h = Math.round(ta.getBoundingClientRect().height) + const oy = getComputedStyle(ta).overflowY + wrap.remove() + return { injected: true, css: st.textContent, cappedH: h, overflowY: oy } + }) + check('注入文本节点高度上限样式', cap.injected && /max-height:\s*240px/.test(String(cap.css)), String(cap.css)) + check('内联 height=1200 的文本域被压到 240(节点不再被拉长)', cap.cappedH > 0 && cap.cappedH <= 260, 'h=' + cap.cappedH + '(240 内容 + 边框)') + check('超出部分在文本域内滚动', cap.overflowY === 'auto', cap.overflowY) + // ---- 数据加载 ---- const loaded = await poll(async () => { const txt = await page.locator('.iwb-ss-status').innerText().catch(() => '') @@ -192,6 +215,28 @@ async function main() { }) check('窄档无水平溢出(chip max-content 不顶宽容器)', ov1.sw <= ov1.cw + 1, 'scrollW=' + ov1.sw + ' clientW=' + ov1.cw) + // ---- 分类 chips 自适应换行 ---- + // 以前 chips 是「单行 + 横向滚动 + scrollbar 隐藏」:面板拖窄时右边几类永远看不见,用户以为只有 7 个分类。 + // 现在改 flex-wrap,这里守住两件事:所有 chip 都落在 chips 容器内(不被裁),且容器没有横向滚动条。 + const measureChips = () => page.evaluate(() => { + const box = document.querySelector('#hostA .iwb-ss-chips') + const all = Array.from(box.querySelectorAll('.iwb-ss-chip')) + const bb = box.getBoundingClientRect() + const rows = new Set(all.map((c) => Math.round(c.getBoundingClientRect().top))).size + const outside = all.filter((c) => { + const r = c.getBoundingClientRect() + return r.right > bb.right + 1 || r.left < bb.left - 1 || r.bottom > bb.bottom + 1 + }).length + const countShown = all[0] ? getComputedStyle(all[0].querySelector('.iwb-ss-chipn')).display !== 'none' : false + return { count: all.length, rows, outside, sw: box.scrollWidth, cw: box.clientWidth, h: Math.round(bb.height), countShown } + }) + const chipsNarrow = await measureChips() + check('窄档 chips 全部落在容器内(换行而非横向滚动)', + chipsNarrow.outside === 0 && chipsNarrow.sw <= chipsNarrow.cw + 1, + JSON.stringify(chipsNarrow)) + check('窄档 chips 折成多行', chipsNarrow.rows >= 2, 'rows=' + chipsNarrow.rows + ' h=' + chipsNarrow.h) + check('窄档隐藏数量后缀(省一行高度)', chipsNarrow.countShown === false) + // ---- 默认窄档 ---- const layoutNarrow = await page.evaluate(() => window.__hostA.querySelector('.iwb-ss-root').getAttribute('data-layout')) check('320px 初始为 narrow', layoutNarrow === 'narrow', String(layoutNarrow)) @@ -268,6 +313,12 @@ async function main() { }) check('宽档无水平溢出', ov2.sw <= ov2.cw + 1, 'scrollW=' + ov2.sw + ' clientW=' + ov2.cw) + const chipsWide = await measureChips() + check('宽档 chips 一行铺完(不再溢出到右侧看不见)', + chipsWide.rows === 1 && chipsWide.outside === 0, + JSON.stringify(chipsWide)) + check('宽档恢复显示数量后缀', chipsWide.countShown === true) + // ---- 恢复窄档,测边界:479/480 ---- await page.evaluate(() => { window.__hostA.style.width = '479px' }) const at479 = await poll(async () => {