feat(album): 1.1.0 方形宫格 + 清除按钮 + 放大镜预览 + 修 5 个布局/翻页 bug
validate / validate (push) Canceled after 0s
validate / validate (push) Canceled after 0s
用户提出的交互改造:
- 顶栏最右「清空」:一键清空(走撤销快照,⌘Z 可整体恢复;不删磁盘原文件)
- 删除右上角的批次角标「1」:顶栏「N 张 → M 个任务」已经说清楚,重复信息
- 选中线 2px → 1px(用户:太粗);失联虚线同步 1px
- 格子固定方形:aspect-ratio 1/1。此前高度由图片比例决定(4 张 16:9 会变 22px 细条),
与 capacityOf() 按方形算的容量是两套账,最后一行常被裁
- 放大镜 A+B 方案:hover → 节点框内浮出大图(inset:0,pointer-events:none 不挡点击);
点击 → 宿主 props.onPreview 全屏预览。删除按钮让到右上角避开
顺手修的实测 bug(都在 1.0.4 复现过):
- S/M/L 档位反了:{S:64, M:48, L:100} 里 M 比 S 小 ⇒ 中格每页 30 张比小格 20 张还多。
现在 S/M/L = 64/80/104,300×470 下每页 20/12/4
- 容量常量与 CSS 对齐:GAP 5→4(CSS 用 --iwb-space-4)、TOOLS_H 27→25、HINT_H 24→28
- 跨页拖拽错位:pageStart 用 page × 格子数(含「+添加」格)⇒ 第 2 页拖到第 2 位实际插到全局第 13 位。
改用 st.cap.capacity
- 删除任意页都跳回第 1 页(硬编码 page:0)⇒ 改为只做页数回圈
- 缩略图串味:key 曾是 fileRefOf(src) || name,固化后 src 无路径 ⇒ 同名文件共用缩略图。
改 thumbKeyOf(f:/b:/c:/d: 前缀),且 localthumb 请求加 in-flight 去重
- img.src='' 触发对页面 URL 的空请求;空节点仍显示「全部 0 张一页展示」;
网页版「文件夹」按钮 canReference() || true 恒真 ⇒ 点了没反应;addPaths 里 keep 死变量
- 缩略图预生成补进度(500 张导入时界面不再像卡死)
测试:新增 album/tools/harness.mjs(44 项,此前插件侧零测试),含「35 种节点尺寸 × 3 档 ×
有无提示行,算出的容量都装得进可视区」的几何不变量;根 package.json 加 npm test
This commit is contained in:
1 parent
01253fceea
commit
3eb8900b51
5 files changed
+407
-46
No files matched your search
+1
-1
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@iwb-plugin/album",
|
||||
"private": true,
|
||||
"version": "1.0.4",
|
||||
"version": "1.1.0",
|
||||
"description": "相册:一个节点装多张本地图(只引用不复制),宫格自适应分页,一图一任务 / 批量两种模式批量生图",
|
||||
"ibwPlugin": {
|
||||
"id": "album",
|
||||
|
||||
+195
-36
@@ -12,14 +12,18 @@
|
||||
'use strict'
|
||||
if (!global.IWBPluginHost) return
|
||||
|
||||
const DENSITY = { S: 64, M: 48, L: 100 }
|
||||
const GAP = 5
|
||||
const PAD_X = 16 // 宫格左右 padding 合计(与 styles.css 对齐)
|
||||
const PAD_Y = 8 // 上下 padding 合计
|
||||
// 密度档位 = 目标单元格边长(px)。1.1.0 修正:旧值 {S:64, M:48, L:100} 里 M 比 S 还小,
|
||||
// 于是「中格」每页装 30 张(比小格的 20 张还多),档位语义是反的。现在 S < M < L 严格递增。
|
||||
const DENSITY = { S: 64, M: 80, L: 104 }
|
||||
// ⚠ 下面这一组必须与 styles.css 逐个对齐(换了 box-sizing / padding 就会算错容量 ⇒ 最后一行被裁)。
|
||||
// 宿主全局 * { box-sizing: border-box },所以 CSS 里写的 height 已含 padding。
|
||||
const GAP = 4 // .iwb-album-grid 的 gap = --iwb-space-4
|
||||
const PAD_X = 16 // 宫格左右 padding 合计(.iwb-album-grid 左右各 --iwb-space-8)
|
||||
const PAD_Y = 8 // 上下 padding 合计(.iwb-album-grid 2px + 6px)
|
||||
const BAR_H = 26 // 状态栏
|
||||
const TOOLS_H = 27 // 工具条
|
||||
const TOOLS_H = 25 // 工具条(padding 2+2 + 按钮 21)
|
||||
const PAGER_H = 26 // 分页条 / 一页提示条
|
||||
const HINT_H = 24 // 批量模式提示行
|
||||
const HINT_H = 28 // 批量模式提示行(含 margin-bottom 4)
|
||||
const DEFAULT_H = 470 // 注册时声明的默认高度(与 height 字段保持一致)
|
||||
const THUMB_EDGE = 256
|
||||
const THUMB_Q = 0.8
|
||||
@@ -67,6 +71,25 @@
|
||||
// ── 缩略图(插件私有缓存,几十 KB/张,不碰画布存储)────────────
|
||||
const thumbNameOf = (key) => 't-' + hash32(key) + '.jpg'
|
||||
|
||||
// 缩略图缓存键(1.1.0 修「串味」):旧实现是 `fileRefOf(src) || it.name`,一旦固化(src 变成
|
||||
// {__blob})或从别处导入,路径就没了 ⇒ 键退化成纯文件名 ⇒ 不同目录下的 1.png 共用同一张缩略图。
|
||||
// 现在按引用本身取键:路径 → 路径;blob/cache → 各自 id;dataURL → 内容哈希;只有都没有才退回文件名。
|
||||
function thumbKeyOf (it) {
|
||||
const src = (it && it.src) != null ? it.src : it
|
||||
const path = fileRefOf(src)
|
||||
if (path) return 'f:' + path
|
||||
if (src && typeof src === 'object') {
|
||||
if (typeof src.__blob === 'string' && src.__blob) return 'b:' + src.__blob
|
||||
if (typeof src.__cache === 'string' && src.__cache) return 'c:' + src.__cache
|
||||
}
|
||||
if (typeof src === 'string' && src) {
|
||||
if (src.length > 4096) return 'd:' + hash32(src.slice(0, 2048) + src.slice(-2048) + ':' + src.length)
|
||||
return 'd:' + hash32(src)
|
||||
}
|
||||
const n = (it && (it.name || it.size)) || ''
|
||||
return 'n:' + str(n)
|
||||
}
|
||||
|
||||
function makeThumb (dataUrl) {
|
||||
return new Promise((resolve) => {
|
||||
try {
|
||||
@@ -120,10 +143,14 @@
|
||||
const st = {
|
||||
nodeId: node.id,
|
||||
thumbs: new Map(), // key → dataURL(内存缓存)
|
||||
pending: new Map(), // key → Promise(并发去重:格子挂载和预生成会同时要同一张)
|
||||
missing: new Set(), // 失联文件的 key
|
||||
busy: '',
|
||||
checkedMissing: false,
|
||||
sig: '',
|
||||
cap: null, // 最近一次容量计算(拖拽排序换算页码要用)
|
||||
lastFull: { key: '', url: '' }, // 原图只保留最近一张(节点内预览用,不做 LRU:多存几张大图就是几十 MB)
|
||||
previewToken: 0,
|
||||
}
|
||||
const bar = el('div', { className: 'iwb-album-bar' })
|
||||
const track = el('span', { className: 'iwb-album-track' }, el('i', null))
|
||||
@@ -139,7 +166,10 @@
|
||||
update(st.props || props, cur, { albumMode: next })
|
||||
})
|
||||
const count = el('span', { className: 'iwb-album-count' })
|
||||
bar.appendChild(modeLabel); bar.appendChild(count)
|
||||
// 一键清空(2026-11-26 用户需求):只清本节点持有的 items,删源文件与画布存储一概不碰。
|
||||
// 走 snapshot:true ⇒ ⌘Z 能整批恢复,所以不加二次确认弹窗(多一步点击反而更烦)。
|
||||
const btnClear = el('button', { className: 'iwb-album-btn iwb-album-clear', title: '清空相册(⌘Z 可撤销;不会删除磁盘上的原文件)' }, '清空')
|
||||
bar.appendChild(modeLabel); bar.appendChild(count); bar.appendChild(btnClear)
|
||||
|
||||
const btnAdd = el('button', { className: 'iwb-album-btn', title: '选择本地图片(桌面端只引用,不复制字节)' }, '+ 添加图片')
|
||||
const btnDir = el('button', { className: 'iwb-album-btn', title: '按文件夹批量添加' }, '文件夹')
|
||||
@@ -161,11 +191,16 @@
|
||||
])
|
||||
const pager = el('div', { className: 'iwb-album-pager' })
|
||||
const onepage = el('div', { className: 'iwb-album-onepage' }, '全部图片一页展示')
|
||||
// 节点内预览层(放大镜 hover 时出现):铺满整个节点框,绝不越界到画布。
|
||||
// pointer-events:none ⇒ 底下的格子仍然能点(点穿 = 切换选中,不会「卡住」在预览里)。
|
||||
const previewImg = el('img', { alt: '', draggable: 'false' })
|
||||
const previewName = el('span', { className: 'iwb-album-preview-name' })
|
||||
const preview = el('div', { className: 'iwb-album-preview' }, [previewImg, previewName])
|
||||
|
||||
host.textContent = ''
|
||||
const root = el('div', { className: 'iwb-album' }, [bar, tools, hint, grid, empty, pager, onepage])
|
||||
const root = el('div', { className: 'iwb-album' }, [bar, tools, hint, grid, empty, pager, onepage, preview])
|
||||
host.appendChild(root)
|
||||
Object.assign(st, { root, bar, track, count, btnAdd, btnDir, btnSettle, segs, grid, empty, pager, onepage, hint })
|
||||
Object.assign(st, { root, bar, track, count, btnClear, btnAdd, btnDir, btnSettle, segs, grid, empty, pager, onepage, hint, preview, previewImg, previewName })
|
||||
|
||||
// 按钮绑定(用闭包里的当前 node/props:每次 sync 会更新 st.node/st.props)
|
||||
btnAdd.addEventListener('pointerdown', (e) => e.stopPropagation())
|
||||
@@ -174,6 +209,8 @@
|
||||
btnDir.addEventListener('click', (e) => { e.stopPropagation(); pickFiles(ctx, st, host, true) })
|
||||
btnSettle.addEventListener('pointerdown', (e) => e.stopPropagation())
|
||||
btnSettle.addEventListener('click', (e) => { e.stopPropagation(); settleAll(ctx, st) })
|
||||
btnClear.addEventListener('pointerdown', (e) => e.stopPropagation())
|
||||
btnClear.addEventListener('click', (e) => { e.stopPropagation(); clearAll(ctx, st) })
|
||||
|
||||
// 拖拽入图:桌面端拿不到绝对路径 ⇒ 按「方案 1」复制进画布存储
|
||||
host.addEventListener('dragover', (e) => { e.preventDefault(); e.stopPropagation() })
|
||||
@@ -202,6 +239,7 @@
|
||||
const wantH = Math.max(120, Math.round(cardH) - 4) + 'px'
|
||||
if (st.root.style.height !== wantH) st.root.style.height = wantH
|
||||
const cap = capacityOf(cardW, cardH, density, mode === 'batch')
|
||||
st.cap = cap // 拖拽排序换算「本页第 i 格 → 全局第几格」要用(旧代码用格子数,把「+添加」也算进去了)
|
||||
const pages = Math.max(1, Math.ceil(items.length / cap.capacity))
|
||||
const page = Math.max(0, Math.min(pages - 1, Number(d.page) || 0))
|
||||
const from = page * cap.capacity
|
||||
@@ -217,7 +255,11 @@
|
||||
for (const k in st.segs) st.segs[k].className = (density === k ? 'on' : '')
|
||||
const hasFileRef = items.some((it) => fileRefOf(it && it.src))
|
||||
st.btnSettle.style.display = hasFileRef ? '' : 'none'
|
||||
st.btnDir.style.display = (ctx.images && ctx.images.canReference()) || true ? '' : 'none'
|
||||
// 「文件夹」只在能做路径引用的宿主里有意义(桌面端)。旧写法 `canReference() || true` 恒真,
|
||||
// 于是在网页版点它永远没反应。网页版仍可用「+ 添加图片」走文件选择器。
|
||||
const canRef = !!(ctx.images && typeof ctx.images.canReference === 'function' && ctx.images.canReference())
|
||||
st.btnDir.style.display = canRef ? '' : 'none'
|
||||
st.btnClear.style.display = items.length ? '' : 'none'
|
||||
if (st.busy) { st.btnAdd.textContent = st.busy } else { st.btnAdd.textContent = '+ 添加图片' }
|
||||
|
||||
// 批量模式提示:N 张 → M 个任务(每组 ≤ 12)
|
||||
@@ -230,10 +272,12 @@
|
||||
} else st.hint.style.display = 'none'
|
||||
|
||||
// 分页条 / 一页展示
|
||||
// 空节点不再显示「全部 0 张一页展示」那条灰条(2026-11-26):它压着空态提示,说的还是废话。
|
||||
const showPager = pages > 1
|
||||
const showOne = !showPager && items.length > 0
|
||||
st.pager.style.display = showPager ? '' : 'none'
|
||||
st.onepage.style.display = showPager ? 'none' : ''
|
||||
if (!showPager) st.onepage.textContent = '全部 ' + items.length + ' 张一页展示 · 无需翻页'
|
||||
st.onepage.style.display = showOne ? '' : 'none'
|
||||
if (showOne) st.onepage.textContent = '全部 ' + items.length + ' 张一页展示 · 无需翻页'
|
||||
if (showPager) {
|
||||
st.pager.innerHTML = ''
|
||||
const pg = el('span', { className: 'iwb-album-pg' })
|
||||
@@ -263,7 +307,7 @@
|
||||
|
||||
// 宫格(签名不变就不重建:避免每帧重排导致闪烁/丢拖拽态)
|
||||
const sig = [node.id, density, page, cap.capacity, mode, items.length, d.onlyPage === true ? 1 : 0,
|
||||
items.map((it) => (it && (fileRefOf(it.src) || it.name) + (it && it.on === false ? ':0' : ':1')) || '').join(','), Array.from(st.missing).join(',')].join('|')
|
||||
items.map((it) => thumbKeyOf(it) + (it && it.on === false ? ':0' : ':1')).join(','), Array.from(st.missing).join(',')].join('|')
|
||||
if (sig !== st.sig) {
|
||||
st.sig = sig
|
||||
st.grid.innerHTML = ''
|
||||
@@ -291,10 +335,23 @@
|
||||
}
|
||||
|
||||
// ── 单元格 ──────────────────────────────────────────────
|
||||
// 放大镜图标(内联 SVG,CSP 禁外部资源):圆 + 手柄,10px 下仍看得清
|
||||
function zoomIcon () {
|
||||
const NS = 'http://www.w3.org/2000/svg'
|
||||
const mk = (tag, attrs) => {
|
||||
const n = document.createElementNS(NS, tag)
|
||||
for (const k in attrs) n.setAttribute(k, attrs[k])
|
||||
return n
|
||||
}
|
||||
const svg = mk('svg', { viewBox: '0 0 24 24', 'aria-hidden': 'true' })
|
||||
svg.appendChild(mk('circle', { cx: 10, cy: 10, r: 7, fill: 'none', stroke: 'currentColor', 'stroke-width': 2.4 }))
|
||||
svg.appendChild(mk('path', { d: 'M15.2 15.2 21 21', fill: 'none', stroke: 'currentColor', 'stroke-width': 2.4, 'stroke-linecap': 'round' }))
|
||||
return svg
|
||||
}
|
||||
function cellOf (ctx, st, it, index, mode) {
|
||||
const src = it && it.src
|
||||
const path = fileRefOf(src)
|
||||
const key = path || str(it && it.name) || String(index)
|
||||
const key = thumbKeyOf(it)
|
||||
const on = !(it && it.on === false)
|
||||
const missing = path && st.missing.has(path)
|
||||
const btn = el('button', {
|
||||
@@ -304,22 +361,35 @@
|
||||
const img = el('img', { alt: '', draggable: 'false' })
|
||||
const cached = st.thumbs.get(key)
|
||||
if (cached) img.src = cached
|
||||
else {
|
||||
img.src = ''
|
||||
loadThumb(ctx, st, it, key).then((url) => { if (url) img.src = url })
|
||||
}
|
||||
// 不设 img.src = '':空字符串会向页面 URL 发一次请求(控制台 ERR_UNKNOWN_URL_SCHEME)。
|
||||
// 不设 src 属性时 <img> 什么都不做,缩略图到了再赋上即可。
|
||||
else loadThumb(ctx, st, it, key).then((url) => { if (url && img.isConnected !== false) img.src = url })
|
||||
btn.appendChild(img)
|
||||
btn.appendChild(el('span', { className: 'iwb-album-idx' }, String(index + 1)))
|
||||
if (mode === 'batch' && on) btn.appendChild(el('span', { className: 'iwb-album-batch' }, String(Math.floor(index / 12) + 1)))
|
||||
const del = el('span', { className: 'iwb-album-del', title: '移除' }, '×')
|
||||
// 批次角标(右上角那个「1」)已删除:顶栏「N 张 → M 个任务」已经说清楚,角标是重复信息,
|
||||
// 还会和删除按钮抢同一个角。位置让给了 ×。
|
||||
const del = el('span', { className: 'iwb-album-del', title: '移除(不删除磁盘上的原文件)' }, '×')
|
||||
del.addEventListener('pointerdown', (e) => e.stopPropagation())
|
||||
del.addEventListener('click', (e) => {
|
||||
e.stopPropagation()
|
||||
const items = itemsOf(st.node).slice()
|
||||
items.splice(index, 1)
|
||||
update(st.props, st.node, { items, page: 0 }, true)
|
||||
// 删除后留在当前页(旧实现硬编码 page:0,第 2 页删一张直接跳回第 1 页)。
|
||||
// 只做「页数变少时往回收」,正常情况 page 不动。
|
||||
const capNow = st.cap || { capacity: 20 }
|
||||
const pagesNow = Math.max(1, Math.ceil(items.length / capNow.capacity))
|
||||
const pageNow = Math.max(0, Number((st.node.data || {}).page) || 0)
|
||||
hidePreview(st, true)
|
||||
update(st.props, st.node, { items: items, page: Math.min(pageNow, pagesNow - 1) }, true)
|
||||
})
|
||||
btn.appendChild(del)
|
||||
// 放大镜(右下角):hover 在节点框内预览,点一下进宿主全屏预览
|
||||
const zoom = el('span', { className: 'iwb-album-zoom', title: '预览大图(点一下全屏)' }, zoomIcon())
|
||||
zoom.addEventListener('pointerdown', (e) => e.stopPropagation())
|
||||
zoom.addEventListener('pointerenter', () => showPreview(ctx, st, it, key))
|
||||
zoom.addEventListener('pointerleave', () => hidePreview(st, false))
|
||||
zoom.addEventListener('click', (e) => { e.stopPropagation(); openFull(ctx, st, it) })
|
||||
btn.appendChild(zoom)
|
||||
btn.addEventListener('pointerdown', (e) => e.stopPropagation())
|
||||
btn.addEventListener('click', (e) => {
|
||||
e.stopPropagation()
|
||||
@@ -356,16 +426,19 @@
|
||||
Array.from(st.grid.querySelectorAll('.dropbefore')).forEach((c) => c.classList.remove('dropbefore'))
|
||||
if (!moved) return
|
||||
const items = itemsOf(st.node).slice()
|
||||
const cellCount = st.grid.querySelectorAll('.iwb-album-cell').length
|
||||
const pageStart = (Number((st.node.data || {}).page) || 0) * cellCount
|
||||
const from = pageStart
|
||||
// 1.1.0 修跨页错位:本页起点 = page × **每页容量**(旧代码乘的是「本页格子数」,而格子数里
|
||||
// 还含末尾那个「+添加」格 ⇒ 第 2 页永远算少一格,把 21 号拖到第 2 位会插到全局第 13 位)。
|
||||
const capNow = st.cap || { capacity: 20 }
|
||||
const pageNow = Math.max(0, Number((st.node.data || {}).page) || 0)
|
||||
const pageStart = pageNow * capNow.capacity
|
||||
const realCount = Math.min(capNow.capacity, Math.max(0, items.length - pageStart))
|
||||
const srcIndex = index
|
||||
let dest = from + target.index
|
||||
let dest = pageStart + Math.min(target.index, realCount)
|
||||
if (dest === srcIndex || dest < 0) return
|
||||
const [movedItem] = items.splice(srcIndex, 1)
|
||||
if (dest > srcIndex) dest -= 1
|
||||
items.splice(Math.max(0, Math.min(items.length, dest)), 0, movedItem)
|
||||
update(st.props, st.node, { items }, true)
|
||||
update(st.props, st.node, { items: items }, true)
|
||||
}
|
||||
document.addEventListener('pointermove', onMove)
|
||||
document.addEventListener('pointerup', onUp)
|
||||
@@ -374,7 +447,17 @@
|
||||
}
|
||||
|
||||
// ── 缩略图取用:内存 → 插件文件 → 现做 ───────────────────
|
||||
// 并发去重(1.1.0):格子一挂载就各自要缩略图,而 addPaths 又顺序预热一遍 ⇒ 同一张图会被读两次盘。
|
||||
// 用 pending 表把同 key 的请求合成一个 Promise(否则导入 30 张会解 60 次原图,内存直接翻倍)。
|
||||
async function loadThumb (ctx, st, it, key) {
|
||||
if (st.thumbs.has(key)) return st.thumbs.get(key)
|
||||
const flight = st.pending && st.pending.get(key)
|
||||
if (flight) return flight
|
||||
const p = loadThumbInner(ctx, st, it, key).finally(() => { if (st.pending) st.pending.delete(key) })
|
||||
if (st.pending) st.pending.set(key, p)
|
||||
return p
|
||||
}
|
||||
async function loadThumbInner (ctx, st, it, key) {
|
||||
if (st.thumbs.has(key)) return st.thumbs.get(key)
|
||||
const name = thumbNameOf(key)
|
||||
try {
|
||||
@@ -394,10 +477,72 @@
|
||||
const thumb = await makeThumb(dataUrl)
|
||||
if (!thumb) return ''
|
||||
st.thumbs.set(key, thumb)
|
||||
try { if (ctx.files) await ctx.files.put(name, thumb) } catch (e) { /* 配额满就只留内存 */ }
|
||||
// 内存里已经有一张就够本会话用了;写盘失败(配额满)不影响显示
|
||||
try { if (ctx.files) await ctx.files.put(name, thumb) } catch (e) { /* ignore */ }
|
||||
return thumb
|
||||
}
|
||||
|
||||
// ── 节点内预览(放大镜 hover)──────────────────────────────
|
||||
// 用户定的交互:鼠标移到格子右下角放大镜 → 节点框内浮出大图;再点一下 → 宿主全屏预览。
|
||||
// 预览层铺满节点(position:absolute; inset:0),所以「大图」永不出节点框,也不挡画布别处。
|
||||
function hidePreview (st, now) {
|
||||
if (!st || !st.preview) return
|
||||
if (st.hideTimer) { clearTimeout(st.hideTimer); st.hideTimer = null }
|
||||
if (now) { st.preview.classList.remove('on'); st.previewToken++; return }
|
||||
// 140ms 宽限:从放大镜边缘滑出去时不会闪一下就没(也不必让用户精准停在圆点上)
|
||||
st.hideTimer = setTimeout(() => {
|
||||
st.hideTimer = null
|
||||
st.preview.classList.remove('on')
|
||||
st.previewToken++
|
||||
}, 140)
|
||||
}
|
||||
function showPreview (ctx, st, it, key) {
|
||||
if (!st || !st.preview) return
|
||||
if (st.hideTimer) { clearTimeout(st.hideTimer); st.hideTimer = null }
|
||||
const cached = st.thumbs.get(key) || ''
|
||||
if (cached) st.previewImg.src = cached
|
||||
else st.previewImg.removeAttribute('src') // 绝不设 src='':那会向页面 URL 发一次请求
|
||||
st.previewName.textContent = (it && it.name) || ''
|
||||
st.preview.classList.add('on')
|
||||
const token = ++st.previewToken
|
||||
// 原图只留最近一张:节点内预览要看得清(缩略图 256px 拉宽到 600 就糊),但多存几张 4K dataURL
|
||||
// 就是几十 MB 常驻内存。留一张,来回 hover 同一张不重复读盘。
|
||||
if (st.lastFull.key === key && st.lastFull.url) { st.previewImg.src = st.lastFull.url; return }
|
||||
if (!ctx.images || typeof ctx.images.dataUrl !== 'function') return
|
||||
try {
|
||||
Promise.resolve(ctx.images.dataUrl(it && it.src)).then((url) => {
|
||||
if (!url) return
|
||||
st.lastFull = { key: key, url: url }
|
||||
if (token === st.previewToken && st.preview.classList.contains('on')) st.previewImg.src = url
|
||||
}).catch(() => { /* 读盘失败就用缩略图 */ })
|
||||
} catch (e) { /* ignore */ }
|
||||
}
|
||||
// 点放大镜 → 宿主全屏预览(props.onPreview 是宿主给的,节点体里拿得到)
|
||||
function openFull (ctx, st, it) {
|
||||
hidePreview(st, true)
|
||||
const props = st.props || {}
|
||||
if (typeof props.onPreview !== 'function') return
|
||||
const fallback = st.thumbs.get(thumbKeyOf(it)) || ''
|
||||
const run = (url) => { try { props.onPreview(url || fallback) } catch (e) { /* ignore */ } }
|
||||
if (!ctx.images || typeof ctx.images.dataUrl !== 'function') { run(fallback); return }
|
||||
try {
|
||||
Promise.resolve(ctx.images.dataUrl(it && it.src)).then((u) => run(u)).catch(() => run(''))
|
||||
} catch (e) { run(fallback) }
|
||||
}
|
||||
|
||||
// ── 一键清空 ────────────────────────────────────────────
|
||||
function clearAll (ctx, st) {
|
||||
const items = itemsOf(st.node)
|
||||
if (!items.length) return
|
||||
st.thumbs.clear(); st.pending.clear(); st.missing.clear()
|
||||
st.lastFull = { key: '', url: '' }
|
||||
st.checkedMissing = false // 清空后再导入要重新做一次失联检测
|
||||
hidePreview(st, true)
|
||||
update(st.props, st.node, { items: [], page: 0, pageRange: null }, true)
|
||||
st.sig = ''
|
||||
sync(ctx, st, st.node, st.props)
|
||||
}
|
||||
|
||||
// ── 导入:选择器 / 文件夹 / 拖入 / 固化 / 重新定位 ──────────
|
||||
async function pickFiles (ctx, st, host, directory) {
|
||||
if (st.busy) return
|
||||
@@ -437,7 +582,6 @@
|
||||
async function addPaths (ctx, st, paths) {
|
||||
const list = paths.filter(isImagePath)
|
||||
const items = itemsOf(st.node).slice()
|
||||
const keep = (st.node.data && st.node.data.keepOriginal !== false)
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
st.busy = '导入 ' + (i + 1) + '/' + list.length; st.sig = ''; sync(ctx, st, st.node, st.props)
|
||||
const ref = await ctx.images.reference(list[i])
|
||||
@@ -445,14 +589,23 @@
|
||||
items.push({ src: { __file: ref.item.path }, name: ref.item.name, size: ref.item.size, mtime: ref.item.mtime, on: true })
|
||||
}
|
||||
st.busy = ''
|
||||
st.checkedMissing = false // 新图要重新做一次失联检测(否则清空后再导入,检测永远不再跑)
|
||||
update(st.props, st.node, { items }, true)
|
||||
st.sig = ''
|
||||
sync(ctx, st, st.node, st.props)
|
||||
// 顺序生成缩略图(避免一次读几十张原图造成内存尖峰)
|
||||
for (const it of items) {
|
||||
const key = fileRefOf(it.src) || it.name
|
||||
if (key) await loadThumb(ctx, st, it, key)
|
||||
// 顺序预生成缩略图(避免一次读几十张原图造成内存尖峰)。
|
||||
// 1.1.0:进度写进「+ 添加图片」按钮(旧实现 busy 在这里已经清空 ⇒ 导入 500 张时界面像卡死);
|
||||
// 只改按钮文字、不重置 sig ⇒ 不触发整块宫格重建(重建 500 次比读盘本身还慢)。
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
const it = items[i]
|
||||
const key = thumbKeyOf(it)
|
||||
if (!key || st.thumbs.has(key)) continue
|
||||
st.busy = '缩略图 ' + (i + 1) + '/' + items.length
|
||||
st.btnAdd.textContent = st.busy
|
||||
await loadThumb(ctx, st, it, key)
|
||||
}
|
||||
st.busy = ''
|
||||
st.btnAdd.textContent = '+ 添加图片'
|
||||
st.sig = ''
|
||||
sync(ctx, st, st.node, st.props)
|
||||
}
|
||||
@@ -473,11 +626,17 @@
|
||||
const src = res.ref || res.data
|
||||
if (!src) continue
|
||||
const name = f.name || ('图 ' + (items.length + 1))
|
||||
items.push({ src, name, size: f.size || 0, on: true })
|
||||
items.push({ src, name: name, size: f.size || 0, on: true })
|
||||
const thumb = await makeThumb(dataUrl)
|
||||
if (thumb) { st.thumbs.set(name, thumb); try { if (ctx.files) await ctx.files.put(thumbNameOf(name), thumb) } catch (e) { /* ignore */ } }
|
||||
// 键必须和 cellOf / loadThumb 完全一致(thumbKeyOf),否则写进缓存也命中不到
|
||||
if (thumb) {
|
||||
const key = thumbKeyOf(items[items.length - 1])
|
||||
st.thumbs.set(key, thumb)
|
||||
try { if (ctx.files) await ctx.files.put(thumbNameOf(key), thumb) } catch (e) { /* ignore */ }
|
||||
}
|
||||
}
|
||||
st.busy = ''
|
||||
st.checkedMissing = false
|
||||
update(st.props, st.node, { items }, true)
|
||||
st.sig = ''
|
||||
sync(ctx, st, st.node, st.props)
|
||||
@@ -534,7 +693,7 @@
|
||||
update(st.props, st.node, { items }, true)
|
||||
st.sig = ''
|
||||
sync(ctx, st, st.node, st.props)
|
||||
loadThumb(ctx, st, items[index], fileRefOf(items[index].src)).then(() => { st.sig = ''; try { sync(ctx, st, st.node, st.props) } catch (e) { /* ignore */ } })
|
||||
loadThumb(ctx, st, items[index], thumbKeyOf(items[index])).then(() => { st.sig = ''; try { sync(ctx, st, st.node, st.props) } catch (e) { /* ignore */ } })
|
||||
}
|
||||
|
||||
|
||||
@@ -543,7 +702,7 @@
|
||||
global.IWBPluginHost.register({
|
||||
id: 'album',
|
||||
name: '相册',
|
||||
version: '1.0.4',
|
||||
version: '1.1.0',
|
||||
apiVersion: 1,
|
||||
capabilities: ['nodeTypes', 'images', 'files'],
|
||||
settings: {
|
||||
|
||||
+28
-8
@@ -2,7 +2,7 @@
|
||||
* 宿主只给一块空容器(.iwb-plugin-node-body),所有排版由这里决定。
|
||||
* 尺寸全部用像素而不是 rem:节点宽度是可拖拽的(264~600),rem 会跟着宿主字号漂移。
|
||||
*/
|
||||
.iwb-album { display: flex; flex-direction: column; height: 100%; overflow: hidden; font-size: 11px; color: var(--iwb-node-text); }
|
||||
.iwb-album { position: relative; display: flex; flex-direction: column; height: 100%; overflow: hidden; font-size: 11px; color: var(--iwb-node-text); }
|
||||
.iwb-album-bar, .iwb-album-tools, .iwb-album-pager, .iwb-album-onepage {
|
||||
display: flex; align-items: center; gap: var(--iwb-space-6); padding: var(--iwb-space-4) var(--iwb-space-8); flex: 0 0 auto;
|
||||
}
|
||||
@@ -13,27 +13,48 @@
|
||||
.iwb-album-track i { position: absolute; top: 1.5px; left: 1.5px; width: 9px; height: 9px; border-radius: 50%; background: var(--iwb-panel); transition: left .15s; }
|
||||
.iwb-album-track.on { background: var(--iwb-brand); }
|
||||
.iwb-album-track.on i { left: 11.5px; }
|
||||
.iwb-album-count { margin-left: auto; font-size: var(--iwb-font-2xs); color: var(--iwb-node-text2); white-space: nowrap; }
|
||||
.iwb-album-count { margin-left: auto; font-size: var(--iwb-font-2xs); color: var(--iwb-node-text2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
||||
.iwb-album-count b { color: var(--iwb-node-text); font-weight: 600; }
|
||||
.iwb-album-btn { height: 21px; padding: 0 var(--iwb-space-8); border: 1px solid var(--iwb-border); border-radius: var(--iwb-radius-chip); background: var(--iwb-panel); color: var(--iwb-node-text2); font-size: var(--iwb-font-2xs); display: inline-flex; align-items: center; gap: var(--iwb-space-4); cursor: pointer; white-space: nowrap; }
|
||||
.iwb-album-btn:hover { border-color: var(--iwb-brand); color: var(--iwb-node-text); }
|
||||
.iwb-album-btn.on { border-color: var(--iwb-brand); color: var(--iwb-node-text); }
|
||||
/* 清空:常驻在顶栏最右,只有相册里有图时才出现。hover 转红是为了和「添加」一类中性操作区分开。 */
|
||||
.iwb-album-clear { flex: 0 0 auto; padding: 0 var(--iwb-space-6); }
|
||||
.iwb-album-clear:hover { border-color: #c0392b; color: #c0392b; }
|
||||
.iwb-album-seg { margin-left: auto; display: inline-flex; border: 1px solid var(--iwb-border); border-radius: var(--iwb-radius-chip); overflow: hidden; }
|
||||
.iwb-album-seg span { width: 21px; height: 21px; display: inline-flex; align-items: center; justify-content: center; font-size: var(--iwb-font-2xs); color: var(--iwb-node-text2); background: var(--iwb-panel); cursor: pointer; }
|
||||
.iwb-album-seg span.on { background: var(--iwb-brand); color: var(--iwb-on-brand); }
|
||||
.iwb-album-grid { display: grid; gap: var(--iwb-space-4); padding: var(--iwb-space-2) var(--iwb-space-8) var(--iwb-space-6); overflow: hidden; align-content: start; flex: 1 1 auto; min-height: 0; }
|
||||
.iwb-album-cell { position: relative; border-radius: var(--iwb-radius-chip); overflow: hidden; background: color-mix(in srgb, var(--iwb-border) 55%, transparent); border: 1px solid var(--iwb-border); cursor: grab; }
|
||||
/* 格子固定正方形(2026-11-26):此前高度由图片比例决定 —— 4 张 16:9 宽图会变成 22px 细条、
|
||||
竖图撑到 167px,和 capacityOf() 按方形算的容量是两套账(最后一行常被裁一半)。
|
||||
aspect-ratio 让每一格高 = 宽,容量因此可精确预测,宫格也终于整齐。
|
||||
padding:0 必须显式写:<button> 的 UA 默认内边距会把图顶出边框。 */
|
||||
.iwb-album-cell { position: relative; aspect-ratio: 1 / 1; padding: 0; border-radius: var(--iwb-radius-chip); overflow: hidden; background: color-mix(in srgb, var(--iwb-border) 55%, transparent); border: 1px solid var(--iwb-border); cursor: grab; }
|
||||
.iwb-album-cell img { width: 100%; height: 100%; object-fit: cover; display: block; -webkit-user-drag: none; }
|
||||
.iwb-album-cell.off img { opacity: .38; }
|
||||
.iwb-album-cell.sel { outline: 2px solid var(--iwb-brand); outline-offset: -2px; }
|
||||
.iwb-album-cell.missing { outline: 2px dashed #c0392b; outline-offset: -2px; }
|
||||
/* 选中线 2px → 1px(用户:太粗)。1px 在浅底上仍分得清「选中」与「描边」。 */
|
||||
.iwb-album-cell.sel { outline: 1px solid var(--iwb-brand); outline-offset: -1px; }
|
||||
.iwb-album-cell.missing { outline: 1px dashed #c0392b; outline-offset: -1px; }
|
||||
.iwb-album-cell.missing::after { content: '失联'; position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; font-size: var(--iwb-font-2xs); color: #c0392b; background: rgba(255,255,255,.72); }
|
||||
.iwb-album-cell.dragging { opacity: .45; }
|
||||
.iwb-album-cell.dropbefore { box-shadow: -3px 0 0 0 var(--iwb-brand); }
|
||||
.iwb-album-idx { position: absolute; left: var(--iwb-space-2); top: var(--iwb-space-2); min-width: 14px; height: 14px; padding: 0 var(--iwb-space-4); border-radius: 999px; background: rgba(0,0,0,.55); color: #fff; font-size: var(--iwb-font-2xs); display: flex; align-items: center; justify-content: center; }
|
||||
.iwb-album-batch { position: absolute; right: var(--iwb-space-2); top: var(--iwb-space-2); padding: 0 var(--iwb-space-4); height: 14px; border-radius: 999px; background: rgba(107,114,128,.92); color: #fff; font-size: var(--iwb-font-2xs); display: flex; align-items: center; }
|
||||
.iwb-album-del { position: absolute; right: var(--iwb-space-2); bottom: 3px; width: 15px; height: 15px; border-radius: 50%; background: var(--iwb-panel); color: var(--iwb-node-text2); font-size: 11px; line-height: 1; display: flex; align-items: center; justify-content: center; opacity: 0; cursor: pointer; }
|
||||
/* 批次角标已移除(2026-11-26):顶栏「N 张 → M 个任务」已经说清楚,每格右上角再挂个「1」是重复信息。
|
||||
位置让给删除按钮:右下角要留给放大镜。 */
|
||||
.iwb-album-del { position: absolute; right: var(--iwb-space-2); top: var(--iwb-space-2); width: 15px; height: 15px; border-radius: 50%; background: var(--iwb-panel); color: var(--iwb-node-text2); font-size: 11px; line-height: 1; display: flex; align-items: center; justify-content: center; opacity: 0; cursor: pointer; }
|
||||
.iwb-album-cell:hover .iwb-album-del { opacity: 1; }
|
||||
/* 放大镜:右下角,hover 才出现(静息态不抢视觉,和 × 一致)。
|
||||
18px 圆 / 12px 图标:10px 时镜片只有 5px,缩到小截图里就成一团白点了。 */
|
||||
.iwb-album-zoom { position: absolute; right: var(--iwb-space-2); bottom: var(--iwb-space-2); width: 17px; height: 17px; border-radius: 50%; background: var(--iwb-panel); color: var(--iwb-node-text2); display: flex; align-items: center; justify-content: center; opacity: 0; cursor: zoom-in; transition: opacity .12s; }
|
||||
.iwb-album-cell:hover .iwb-album-zoom { opacity: 1; }
|
||||
.iwb-album-zoom:hover { color: var(--iwb-brand); }
|
||||
.iwb-album-zoom svg { width: 12px; height: 12px; display: block; }
|
||||
/* 节点内预览层:铺满节点框(用户要求「大图显示范围控制在节点框内」),绝不溢出到画布。
|
||||
点放大镜后出现的浮层 —— pointer-events:none 所以不会挡住下面格子的点击(点穿到底层格子 = 切换选中,符合预期)。*/
|
||||
.iwb-album-preview { position: absolute; inset: 0; z-index: 9; display: none; align-items: center; justify-content: center; background: rgba(12,16,22,.82); pointer-events: none; }
|
||||
.iwb-album-preview.on { display: flex; }
|
||||
.iwb-album-preview img { max-width: 100%; max-height: 100%; width: auto; height: auto; object-fit: contain; display: block; }
|
||||
.iwb-album-preview-name { position: absolute; left: var(--iwb-space-8); bottom: var(--iwb-space-6); right: var(--iwb-space-8); color: #fff; font-size: var(--iwb-font-2xs); opacity: .82; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||
.iwb-album-add { border: 1px dashed var(--iwb-border); background: transparent; color: var(--iwb-node-text2); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: var(--iwb-space-2); font-size: var(--iwb-font-2xs); border-radius: var(--iwb-radius-chip); cursor: pointer; }
|
||||
.iwb-album-add b { font-size: 15px; line-height: 1; font-weight: 600; }
|
||||
.iwb-album-pager { border-top: 1px solid color-mix(in srgb, var(--iwb-border) 55%, transparent); height: 26px; }
|
||||
@@ -46,4 +67,3 @@
|
||||
.iwb-album-hint b { color: var(--iwb-node-text); }
|
||||
.iwb-album-empty { flex: 1 1 auto; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: var(--iwb-space-6); color: var(--iwb-node-text2); font-size: 11px; text-align: center; padding: 0 var(--iwb-space-12); }
|
||||
.iwb-album-empty .box { width: 66px; height: 50px; border: 1.5px dashed var(--iwb-border); border-radius: var(--iwb-radius-chip); display: flex; align-items: center; justify-content: center; font-size: var(--iwb-font-xl); color: var(--iwb-node-text2); }
|
||||
|
||||
@@ -0,0 +1,180 @@
|
||||
#!/usr/bin/env node
|
||||
/* album 插件自测(v1.1.0,2026-11-26)
|
||||
*
|
||||
* 为什么要有它:这个插件此前**零自动化测试**(宿主侧 tests/album-task-graph-test.js 只覆盖任务展开)。
|
||||
* 而 1.1.0 动的是「宫格尺寸 = 分页容量」这条最脆的链:capacityOf() 里的常量必须和 styles.css 逐个对齐,
|
||||
* 错 1px 就会「最后一行被裁一半」,而那种错误肉眼要盯很久才发现。这里把它变成断言。
|
||||
*
|
||||
* 两部分:
|
||||
* 一、静态断言:源码 / 样式里的关键实现必须还在(防回归,不依赖浏览器)
|
||||
* 二、算术断言:把常量段 + capacityOf / thumbKeyOf 从 IIFE 里抽出来,在 vm 里真跑
|
||||
*
|
||||
* 浏览器渲染验证(方形格子、裁切、hover 预览)不在这里 —— 那需要 playwright,
|
||||
* 跨仓依赖不适合当门禁。跑法见 README「验证」一节。
|
||||
*/
|
||||
import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import vm from 'node:vm'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
|
||||
const HERE = path.dirname(fileURLToPath(import.meta.url))
|
||||
const ROOT = path.resolve(HERE, '..')
|
||||
const src = fs.readFileSync(path.join(ROOT, 'src/index.js'), 'utf8')
|
||||
const css = fs.readFileSync(path.join(ROOT, 'styles.css'), 'utf8')
|
||||
const pkg = JSON.parse(fs.readFileSync(path.join(ROOT, 'package.json'), 'utf8'))
|
||||
|
||||
let failed = 0
|
||||
const ok = (name, cond, extra) => {
|
||||
if (!cond) failed++
|
||||
console.log((cond ? '✓ ' : '✗ ') + name + (cond ? '' : (' » ' + (extra == null ? '' : extra))))
|
||||
}
|
||||
|
||||
// ── 一、版本与注册契约 ───────────────────────────────────────
|
||||
ok('package.json version = 1.1.0', pkg.version === '1.1.0', pkg.version)
|
||||
ok('manifest.version = 1.1.0', /version:\s*'1\.1\.0'/.test(src))
|
||||
ok('id = album', /id:\s*'album'/.test(src))
|
||||
ok('capabilities 含 nodeTypes / images / files',
|
||||
/capabilities:\s*\['nodeTypes',\s*'images',\s*'files'\]/.test(src))
|
||||
ok('默认 outKind = images(宿主靠它认「多图源」,决定连线白名单 + 任务展开)',
|
||||
/defaults:\s*\{[^}]*outKind:\s*'images'/.test(src))
|
||||
ok('端口 in/out 都在(相册要接提示词、要出图)', /ports:\s*\{\s*in:\s*true,\s*out:\s*true\s*\}/.test(src))
|
||||
|
||||
// ── 二、容量常量必须与 styles.css 对齐 ────────────────────────
|
||||
// 宿主全局 * { box-sizing: border-box },所以 CSS 的 height 已含 padding;JS 侧照抄即可。
|
||||
const cssGap = /\.iwb-album-grid\s*\{[^}]*gap:\s*var\(--iwb-space-4\)/.test(css) ? 4 : -1
|
||||
ok('宫格 gap = 4px(CSS 用 --iwb-space-4)', cssGap === 4)
|
||||
ok('JS GAP 与 CSS 一致(4)', /const GAP = 4\b/.test(src))
|
||||
ok('PAD_X = 16(左右各 --iwb-space-8)', /const PAD_X = 16\b/.test(src))
|
||||
ok('PAD_Y = 8(宫格 padding 2 + 6)', /const PAD_Y = 8\b/.test(src))
|
||||
ok('BAR_H = 26(顶栏 height: 26px)', /const BAR_H = 26\b/.test(src))
|
||||
ok('TOOLS_H = 25(工具条 padding 2+2 + 按钮 21)', /const TOOLS_H = 25\b/.test(src))
|
||||
ok('PAGER_H = 26(分页条 height: 26px)', /const PAGER_H = 26\b/.test(src))
|
||||
ok('HINT_H = 28(提示行 24 + margin-bottom 4)', /const HINT_H = 28\b/.test(src))
|
||||
ok('顶栏确实 height: 26px', /\.iwb-album-bar\s*\{[^}]*height:\s*26px/.test(css))
|
||||
ok('分页条确实 height: 26px', /\.iwb-album-pager\s*\{[^}]*height:\s*26px/.test(css))
|
||||
|
||||
// ── 三、把纯逻辑抽出来在 vm 里真跑 ────────────────────────────
|
||||
const from = src.indexOf(' const DENSITY')
|
||||
const to = src.indexOf(' // ── 数据读写')
|
||||
ok('能定位到常量段与 capacityOf(抽出来跑算术)', from > 0 && to > from, from + '/' + to)
|
||||
let sandbox = {}
|
||||
if (from > 0 && to > from) {
|
||||
// const / 箭头函数在 vm 顶层是词法的,不会自动挂到 sandbox 上 ⇒ 显式导出一份
|
||||
const code = src.slice(from, to) + '\n;globalThis.__album = { DENSITY: DENSITY, capacityOf: capacityOf, thumbKeyOf: thumbKeyOf, thumbNameOf: thumbNameOf }\n'
|
||||
sandbox = { document: { createElement: () => ({ style: {}, appendChild() {} }) }, setTimeout, clearTimeout, Math, String, Number, Array, Object, Map, Set, Promise, JSON, RegExp, console, window: {} }
|
||||
vm.createContext(sandbox)
|
||||
vm.runInContext(code, sandbox, { filename: 'album-slice.js' })
|
||||
Object.assign(sandbox, sandbox.__album || {})
|
||||
}
|
||||
|
||||
const cap = sandbox.capacityOf
|
||||
const DENSITY = sandbox.DENSITY
|
||||
ok('DENSITY 三档严格递增(旧值 M:48 < S:64 是 bug:中格比小格还小)',
|
||||
DENSITY && DENSITY.S < DENSITY.M && DENSITY.M < DENSITY.L, JSON.stringify(DENSITY))
|
||||
|
||||
if (typeof cap === 'function') {
|
||||
// 默认节点 300×470(DEFAULT_H),这是用户最常看到的那一档
|
||||
const s = cap(300, 470, 'S', false)
|
||||
const m = cap(300, 470, 'M', false)
|
||||
const l = cap(300, 470, 'L', false)
|
||||
ok('300×470 小格 = 4 列(实测渲染 4 列 68px)', s.cols === 4, JSON.stringify(s))
|
||||
ok('每页容量随档位递减 S > M > L(旧的 M 反而最多)',
|
||||
s.capacity > m.capacity && m.capacity > l.capacity,
|
||||
[s.capacity, m.capacity, l.capacity].join(' / '))
|
||||
ok('小格每页 20 张(批量导入的常见预期值)', s.capacity === 20, String(s.capacity))
|
||||
// 关键几何不变量:cols 个 cell + (cols-1) 个 gap 必须装得进内容宽度,
|
||||
// rows 个 cell + (rows-1) 个 gap 必须装得进内容高度。差 1px 就会让最后一行被裁。
|
||||
const fits = (w, h, d, hint) => {
|
||||
const c = cap(w, h, d, hint)
|
||||
const innerW = w - 4 - 16
|
||||
const innerH = h - 4 - 26 - 25 - 26 - (hint ? 28 : 0) - 8
|
||||
const needW = c.cols * c.cell + (c.cols - 1) * 4
|
||||
const needH = c.rows * c.cell + (c.rows - 1) * 4
|
||||
return { c, okW: needW <= innerW, okH: needH <= innerH, needW, needH, innerW, innerH }
|
||||
}
|
||||
let allFit = true
|
||||
const misses = []
|
||||
for (const w of [264, 280, 300, 360, 420, 520, 600]) {
|
||||
for (const h of [300, 380, 470, 560, 700]) {
|
||||
for (const d of ['S', 'M', 'L']) {
|
||||
for (const hint of [false, true]) {
|
||||
const r = fits(w, h, d, hint)
|
||||
if (!r.okW || !r.okH) { allFit = false; misses.push(w + 'x' + h + ' ' + d + (hint ? '+hint ' : ' ') + JSON.stringify([r.needW, r.innerW, r.needH, r.innerH])) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ok('35 种节点尺寸 × 3 档 × 有无提示行:算出的容量都能装进可视区(不会裁掉最后一行)',
|
||||
allFit, misses.slice(0, 3).join(' | '))
|
||||
const min = cap(180, 160, 'S', true)
|
||||
ok('极小节点也不返回 0 列 0 行(否则宫格空白)', min.cols >= 1 && min.rows >= 1 && min.capacity >= 1, JSON.stringify(min))
|
||||
}
|
||||
|
||||
// thumbKeyOf:固化后(src 变成 {__blob})不能退化成纯文件名(否则同名文件串味)
|
||||
const tk = sandbox.thumbKeyOf
|
||||
if (typeof tk === 'function') {
|
||||
const a1 = tk({ src: { __file: '/a/1.png' }, name: '1.png' })
|
||||
const a2 = tk({ src: { __file: '/b/1.png' }, name: '1.png' })
|
||||
ok('同名不同目录的本地图 → 不同键(旧实现两处都是 "1.png",缩略图互相覆盖)', a1 !== a2, a1 + ' vs ' + a2)
|
||||
const b1 = tk({ src: { __blob: 'x1' }, name: '1.png' })
|
||||
const b2 = tk({ src: { __blob: 'x2' }, name: '1.png' })
|
||||
ok('固化后按 __blob id 取键,不退化到文件名', b1 !== b2 && b1.indexOf('b:') === 0, b1 + ' vs ' + b2)
|
||||
ok('同一引用取键稳定(拖拽排序后不会重建缩略图)', tk({ src: { __file: '/a/1.png' }, name: '1.png' }) === a1)
|
||||
ok('键做成了文件安全的形式(t-<hash8>.jpg,符合插件文件白名单)',
|
||||
/^[a-z0-9][a-z0-9_-]{0,63}\.jpg$/.test(sandbox.thumbNameOf(a1)), sandbox.thumbNameOf(a1))
|
||||
} else ok('thumbKeyOf 可被抽取执行', false)
|
||||
|
||||
// ── 四、交互:清空 / 放大镜 / 固定方形 / 细边线 ────────────────
|
||||
ok('有「清空」按钮', /className:\s*'iwb-album-btn iwb-album-clear'/.test(src))
|
||||
ok('清空走快照(⌘Z 能整体撤销)',
|
||||
/function clearAll[\s\S]{0,700}update\(st\.props, st\.node, \{ items: \[\], page: 0, pageRange: null \}, true\)/.test(src))
|
||||
ok('清空只在有图时出现', /st\.btnClear\.style\.display = items\.length \? '' : 'none'/.test(src))
|
||||
ok('清空后重置失联检测(否则清空再导入永远不检测)', /function clearAll[\s\S]{0,600}st\.checkedMissing = false/.test(src))
|
||||
|
||||
ok('格子固定正方形(aspect-ratio: 1 / 1)', /\.iwb-album-cell\s*\{[^}]*aspect-ratio:\s*1 \/ 1/.test(css))
|
||||
ok('格子显式 padding: 0(button 默认内边距会把图顶出边框)', /\.iwb-album-cell\s*\{[^}]*padding:\s*0;/.test(css))
|
||||
ok('选中线 1px(用户:2px 太粗)', /\.iwb-album-cell\.sel\s*\{[^}]*outline:\s*1px solid/.test(css))
|
||||
ok('失联虚线同步 1px', /\.iwb-album-cell\.missing\s*\{[^}]*outline:\s*1px dashed/.test(css))
|
||||
|
||||
ok('放大镜元素存在(右下角)', /className:\s*'iwb-album-zoom'/.test(src))
|
||||
ok('放大镜 hover 触发节点内预览', /pointerenter', \(\) => showPreview\(/.test(src))
|
||||
ok('放大镜点击进宿主全屏预览(props.onPreview)',
|
||||
/zoom\.addEventListener\('click',[\s\S]{0,120}openFull\(ctx, st, it\)/.test(src) &&
|
||||
/function openFull[\s\S]{0,700}props\.onPreview\(/.test(src))
|
||||
ok('预览层铺满节点框(inset: 0,绝不溢出到画布)', /\.iwb-album-preview\s*\{[^}]*inset:\s*0/.test(css))
|
||||
ok('预览层 pointer-events: none(不挡住底下格子的点击)', /\.iwb-album-preview\s*\{[^}]*pointer-events:\s*none/.test(css))
|
||||
ok('离开有 140ms 宽限(从放大镜滑出不闪断)', /setTimeout\([\s\S]{0,160}\}, 140\)/.test(src))
|
||||
ok('预览先用缩略图占位、再异步换原图', /st\.previewImg\.src = cached/.test(src) && /st\.lastFull = \{ key: key, url: url \}/.test(src))
|
||||
ok('原图只缓存最近一张(多存几张 4K dataURL 就是几十 MB 常驻内存)',
|
||||
/lastFull: \{ key: '', url: '' \}/.test(src) && !/fulls\.set/.test(src))
|
||||
|
||||
// ── 五、已修复的老 bug 必须有防回归断言 ───────────────────────
|
||||
ok('批次角标彻底移除(右上角那个「1」是重复信息)',
|
||||
!src.includes('iwb-album-batch') && !css.includes('iwb-album-batch'))
|
||||
ok('不再有 img.src = \'\'(会向页面 URL 发空请求,控制台报 ERR_UNKNOWN_URL_SCHEME)',
|
||||
!/\.src = ''/.test(src.split('\n').filter((l) => l.trim().indexOf('//') !== 0).join('\n')))
|
||||
ok('跨页拖拽用「每页容量」换算,不用「本页格子数」',
|
||||
/pageStart = pageNow \* capNow\.capacity/.test(src) && !/page \* cellCount/.test(src))
|
||||
ok('删除不再硬编码跳回第 1 页', !/\{ items, page: 0 \}/.test(src) && /page: Math\.min\(pageNow, pagesNow - 1\)/.test(src))
|
||||
ok('空节点不再显示「全部 0 张一页展示」(压着空态提示的废话)',
|
||||
/const showOne = !showPager && items\.length > 0/.test(src))
|
||||
ok('「文件夹」按钮不再 canReference() || true(网页版点了没反应)',
|
||||
!/\|\| true \? '' : 'none'/.test(src))
|
||||
ok('addPaths 里没有 keep 死变量', !/const keep = \(st\.node\.data/.test(src))
|
||||
ok('缩略图请求并发去重(st.pending,否则导入 30 张要解 60 次原图)',
|
||||
/const flight = st\.pending && st\.pending\.get\(key\)/.test(src))
|
||||
ok('缩略图预生成有进度(500 张导入时不会看起来卡死)', /st\.busy = '缩略图 '/.test(src))
|
||||
ok('新导入后重置 checkedMissing', /function addPaths[\s\S]{0,900}st\.checkedMissing = false/.test(src))
|
||||
|
||||
// ── 六、宿主契约不能碰 ───────────────────────────────────────
|
||||
ok('仍然只写 items / page / pageRange 这几个字段给宿主(不新增宿主不认识的键)',
|
||||
/outKind: 'images'/.test(src) && /pageRange: wantRange/.test(src))
|
||||
ok('只引用不复制:本地图仍写 { __file: 路径 }', /\{ __file: ref\.item\.path \}/.test(src))
|
||||
ok('缩略图仍存宿主插件沙箱(ctx.files),不塞进节点数据',
|
||||
/ctx\.files\.put\(name, thumb\)/.test(src) && !/thumbs[\s\S]{0,40}onUpdate/.test(src))
|
||||
ok('没有引入 import/export(必须是经典脚本 IIFE,CSP script-src self)',
|
||||
!/^\s*(import|export)\s/m.test(src) && /^;\(function \(global\)/m.test(src))
|
||||
ok('样式只能 JS 内联注入之外的地方没有新增 <style> 标签', !/<style/.test(src))
|
||||
|
||||
console.log('\n' + (failed ? ('✗ ' + failed + ' 项失败') : '✓ 全部通过'))
|
||||
process.exit(failed ? 1 : 0)
|
||||
+3
-1
@@ -5,7 +5,9 @@
|
||||
"description": "IWB 插件仓:只写插件,不复制宿主契约",
|
||||
"scripts": {
|
||||
"validate": "node scripts/validate.mjs",
|
||||
"build": "node scripts/build-plugin.mjs"
|
||||
"build": "node scripts/build-plugin.mjs",
|
||||
"test": "npm run validate && npm run test:album",
|
||||
"test:album": "node album/tools/harness.mjs"
|
||||
},
|
||||
"devDependencies": {
|
||||
"esbuild": "^0.24.2"
|
||||
|
||||
Reference in new issue
Block a user