Files
qiqi a923834595
validate / validate (push) Canceled after 0s
feat(album): 1.1.2 看大图支持左右切换(整本相册循环浏览)
宿主 props.onPreview 只收「已解析好的 URL 列表」:要切换就得开窗前把整本相册
的原图全部转成 dataURL(30 张 4K 图 = 几十上百 MB,还会卡主线程)。因此改成
插件自带全屏查看器,挂在 document.body:

- 点右下角放大镜 → 打开查看器,显示「N / 总数」+ 文件名;
- ← → / 左右按钮 / 滚轮切换,首尾循环,可跨分页浏览整本;
- 点图片外空白或 Esc 退出(沿用 1.1.1 定的交互);
- 只解码当前张 + 预取左右各一张(最多 3 张常驻),切页先用缩略图占位不闪黑;
- 键盘/滚轮在捕获阶段吃掉并关闭时摘掉监听,不影响宿主画布快捷键;
- 补回宿主预览原有的「复制(PNG)/ 下载」按钮,能力不回退。

测试:静态 harness 增 14 项断言;新增 album/tools/viewer-e2e.mjs(Chromium
真浏览器 22 项:开/关、键盘与按钮、循环、滚轮、单图、监听清理、复制下载)。
npm test 全过。
2026-11-25 10:32:10 +08:00

171 lines
8.7 KiB
JavaScript

#!/usr/bin/env node
/* album 全屏查看器「真浏览器」冒烟(1.1.2)
*
* 为什么要单独跑:查看器是挂在 document.body 上的全局浮层 + 全局键盘/滚轮监听,
* 静态断言只能证明「代码里有这些字符串」,证明不了点开、切页、Esc/空白关闭真的能跑。
* 这里用 Chromium 装一个合成宿主,加载插件源码本体,走完整交互。
*
* 运行:node album/tools/viewer-e2e.mjs
* 依赖 playwright(宿主仓 node_modules;或 IWB_PLAYWRIGHT_ROOT 指定目录)。
* 不进 npm test:跨仓依赖,CI 上不一定装得齐。
*/
import { readFileSync } from 'node:fs'
import path from 'node:path'
import { createRequire } from 'node:module'
import { fileURLToPath } from 'node:url'
const HERE = path.dirname(fileURLToPath(import.meta.url))
const ROOT = path.resolve(HERE, '..')
const src = readFileSync(path.join(ROOT, 'src/index.js'), 'utf8')
const css = readFileSync(path.join(ROOT, 'styles.css'), 'utf8')
let chromium = null
for (const base of [process.env.IWB_PLAYWRIGHT_ROOT,
'/Users/qiqi/open/github/image-workbench-desktop',
'/Users/qiqi/open/github/image-workbench-web', ROOT]) {
if (!base) continue
try { chromium = createRequire(path.join(base, 'package.json'))('playwright').chromium; break } catch (e) { /* 试下一个 */ }
}
if (!chromium) {
console.error('✗ 找不到 playwright:请安装,或设 IWB_PLAYWRIGHT_ROOT=/path/to/repo')
process.exit(2)
}
let failed = 0
const ok = (name, cond, extra) => {
if (!cond) failed++
console.log((cond ? '✓ ' : '✗ ') + name + (cond ? '' : (' » ' + (extra == null ? '' : extra))))
}
const browser = await chromium.launch({ headless: true })
const page = await browser.newPage({ viewport: { width: 1100, height: 800 } })
page.on('pageerror', (e) => { failed++; console.log('✗ 页面脚本异常 » ' + e.message) })
await page.setContent('<!doctype html><html><head><meta charset="utf-8"></head><body style="margin:0"><div id="host" style="width:300px;height:470px;position:relative"></div></body></html>')
await page.addStyleTag({ content: css })
await page.evaluate(() => {
const colors = ['#e57373', '#81c784', '#64b5f6', '#ffd54f', '#ba68c8']
window.__urls = colors.map((c) => 'data:image/svg+xml;base64,' + btoa(
'<svg xmlns="http://www.w3.org/2000/svg" width="60" height="60"><rect width="60" height="60" fill="' + c + '"/></svg>'))
window.__items = window.__urls.map((u, i) => ({ src: u, name: '图' + (i + 1) + '.svg', on: true }))
window.IWBPluginHost = {
register (meta, factory) {
window.__meta = meta
const ctx = {
nodes: { register (spec) { window.__spec = spec } },
images: {
canReference: () => false,
dataUrl: async (v) => String(v || ''),
thumb: () => '',
pick: async () => [],
stat: async () => ({ ok: true, exists: true }),
listImages: async () => [],
},
files: { get: async () => ({ ok: false }), put: async () => ({ ok: true }) },
settings: { get: () => true },
}
window.__ctx = ctx
factory(ctx)
},
}
})
await page.addScriptTag({ content: src })
await page.evaluate(() => {
window.__render = (items) => {
// 不清 host:宿主每次重渲染复用同一个容器,插件靠 STATE 增量更新;
// 这里手动清空会把插件的 root 摘掉,STATE 还拿着游离节点 ⇒ 界面空白(测试自身不能造这种场景)
const host = document.getElementById('host')
const node = { id: 'n1', w: 300, h: 470, data: { outKind: 'images', items: items, albumMode: 'per-image', density: 'S', page: 0, onlyPage: false } }
const props = { onUpdate () {}, onSnapshot () {}, onPreview (u) { window.__hostPreview = u } }
window.__spec.render(node, props, host)
}
window.__render(window.__items)
})
try {
await page.waitForFunction(() => document.querySelectorAll('.iwb-album-cell').length >= 5, null, { timeout: 10000 })
} catch (e) {
console.log('DEBUG host =', (await page.evaluate(() => document.getElementById('host').innerHTML)).slice(0, 1200))
console.log('DEBUG meta =', await page.evaluate(() => JSON.stringify(window.__meta)))
console.log('DEBUG spec =', await page.evaluate(() => String(window.__spec && window.__spec.id)))
throw e
}
// 1) 点放大镜 → 全屏查看器
await page.locator('.iwb-album-zoom').first().click({ force: true })
await page.waitForFunction(() => document.querySelectorAll('.iwb-album-fs').length === 1)
ok('点放大镜开出全屏查看器', true)
ok('计数显示 1 / 5', await page.locator('.iwb-album-fs-count').textContent() === '1 / 5')
ok('原图异步换上(不是只显示缩略图)', await page.waitForFunction(
() => { const im = document.querySelector('.iwb-album-fs img'); return !!im && (im.getAttribute('src') || '').indexOf('data:image/svg+xml') === 0 },
null, { timeout: 5000 }).then(() => true).catch(() => false))
ok('左右箭头按钮都渲染出来了', await page.locator('.iwb-album-fs-nav').count() === 2)
// 2) 键盘切换(含首尾循环)
await page.keyboard.press('ArrowRight')
ok('→ 切到第 2 张', await page.locator('.iwb-album-fs-count').textContent() === '2 / 5')
await page.keyboard.press('ArrowLeft')
await page.keyboard.press('ArrowLeft')
ok('← 从第 1 张循环到最后一张', await page.locator('.iwb-album-fs-count').textContent() === '5 / 5')
await page.keyboard.press('ArrowRight')
ok('再从最后一张循环回第 1 张', await page.locator('.iwb-album-fs-count').textContent() === '1 / 5')
// 3) 按钮 / 滚轮切换
await page.locator('.iwb-album-fs-nav.next').click()
ok('下一张按钮生效', await page.locator('.iwb-album-fs-count').textContent() === '2 / 5')
await page.mouse.move(550, 400)
await page.mouse.wheel(0, 120)
await page.waitForTimeout(80)
ok('滚轮向下切一张', await page.locator('.iwb-album-fs-count').textContent() === '3 / 5')
// 3b) 宿主预览原有的「复制 / 下载」能力不能丢
ok('查看器带复制 / 下载按钮', await page.locator('.iwb-album-fs-btn').count() === 2)
await page.locator('.iwb-album-fs-btn').nth(1).click()
ok('点下载不会把查看器关掉', await page.locator('.iwb-album-fs').count() === 1)
await page.locator('.iwb-album-fs-btn').first().click()
await page.waitForTimeout(120)
ok('点复制不会报错 / 不关闭(无剪贴板权限时也只给提示)', await page.locator('.iwb-album-fs').count() === 1)
// 4) 点空白关闭;键盘监听要摘干净(关掉后再按 → 不能影响画布)
await page.mouse.click(8, 8)
ok('点图片外的空白关闭', await page.locator('.iwb-album-fs').count() === 0)
ok('关闭后画布没有被误删格子', await page.locator('.iwb-album-cell').count() === 6)
await page.locator('.iwb-album-zoom').first().click({ force: true })
await page.keyboard.press('Escape')
ok('Esc 关闭', await page.locator('.iwb-album-fs').count() === 0)
await page.keyboard.press('ArrowRight')
ok('关闭后不再残留键盘监听(→ 不报错、不重建浮层)', await page.locator('.iwb-album-fs').count() === 0)
// 5) 只有一张图时不显示左右按钮
await page.evaluate(() => window.__render([window.__items[0]]))
try {
await page.waitForFunction(() => document.querySelectorAll('.iwb-album-cell').length === 2, null, { timeout: 5000 })
} catch (e) {
console.log('DEBUG single host =', (await page.evaluate(() => document.getElementById('host').innerHTML)).slice(0, 600))
console.log('DEBUG cells =', await page.evaluate(() => document.querySelectorAll('.iwb-album-cell').length))
throw e
}
await page.locator('.iwb-album-zoom').first().click({ force: true })
const single = await page.evaluate(() => {
const root = document.querySelector('.iwb-album-fs')
const nav = document.querySelector('.iwb-album-fs-nav')
return { cls: root && root.className, display: nav ? getComputedStyle(nav).display : 'missing' }
})
ok('单图查看器隐藏左右按钮', (single.cls || '').indexOf('single') >= 0 && single.display === 'none', JSON.stringify(single))
ok('单图计数 1 / 1', await page.locator('.iwb-album-fs-count').textContent() === '1 / 1')
await page.mouse.click(8, 8)
// 6) 查看器挂到 body(不是节点里),事件不会穿透到画布
const inBody = await page.evaluate(() => {
window.__render(window.__items)
document.querySelector('.iwb-album-zoom').click()
const root = document.querySelector('.iwb-album-fs')
return !!root && root.parentNode === document.body
})
ok('浮层挂在 document.body(不受节点 transform 影响)', inBody)
ok('开启查看器没有走宿主 props.onPreview', await page.evaluate(() => typeof window.__hostPreview === 'undefined'))
await page.keyboard.press('Escape')
await browser.close()
console.log('\n' + (failed ? ('✗ ' + failed + ' 项失败') : '✓ 全部通过'))
process.exit(failed ? 1 : 0)