feat(gallery): 图库插件 1.8.0 —— 懒加载缩略图、选中不跳顶、双击稳定
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 53s

用户反馈三个问题,都能在代码里对上:
1) 加载不及时:renderGrid 一次性为所有条目发 IPC 取缩略图(每张一次 invoke)
2) 点选后自动回到顶部:pick() → render() → renderGrid() 整个网格 clear + 重建,滚动容器内容被换掉
3) 双击打不开:第一次点击触发整格重建,正在双击的 <img> 被销毁,dblclick 再也落不到同一个元素上
   (鼠标 hover 动效只是让它更明显,根因是重绘把节点换了)

改动(plugins/gallery/main.js):
- 缩略图按可见性懒加载:IntersectionObserver(上下预取 300px),前 30 张仍同步取(首屏/e2e)
- pick() 只就地切换选中 class 与勾选角标(syncSelection),不再整格重绘 ⇒ 滚动位置不动、双击元素不换
- render() 重排前后保存/恢复滚动容器的 scrollTop(刷新、显示更多、改列宽时不跳顶)
- 卡片加 data-name,选中态查找用 querySelectorAll 一次完成
- 版本 1.8.0,manifest 与 plugins/index.json 的哈希同步
This commit is contained in:
qiqi committed 2026-11-28 01:30:04 +08:00
1 parent 72ccb8f517
commit 31ffde68ce
3 files changed
+65 -9

No files matched your search

+60 -5
View File
@@ -340,6 +340,7 @@
}
function renderGrid () {
if (io) { try { io.disconnect() } catch (e) { /* ignore */ } }
clear(grid)
var gap = 6
// 每次重排都重新量一次宽度:面板显隐/停靠栏展开时 gridW 可能是旧值,
@@ -363,9 +364,10 @@
cols[min].push(it)
heights[min] += colW * ratio + gap
}
var seq = 0
for (var ci = 0; ci < cols.length; ci++) {
var col = el('div', { class: 'iwb-history-col', style: 'width:' + Math.round(colW) + 'px;flex:0 0 auto' })
for (var k = 0; k < cols[ci].length; k++) col.appendChild(itemNode(cols[ci][k]))
for (var k = 0; k < cols[ci].length; k++) { col.appendChild(itemNode(cols[ci][k], seq)); seq++ }
grid.appendChild(col)
}
empty.style.display = items.length ? 'none' : ''
@@ -377,9 +379,39 @@
}
}
function itemNode (it) {
// ── v1.8.0:懒加载观察器 / 滚动位置保持 / 选中态就地更新 ─────────────────
var io = null
function ensureIO () {
if (io || typeof global.IntersectionObserver !== 'function') return io
io = new global.IntersectionObserver(function (list) {
for (var i = 0; i < list.length; i++) {
var e = list[i]
if (!e || !e.isIntersecting) continue
var node = e.target
try { io.unobserve(node) } catch (err) { /* ignore */ }
var fn = node && node.__iwbBoot
node.__iwbBoot = null
if (typeof fn === 'function') fn()
}
}, { root: null, rootMargin: '300px 0px' })
return io
}
// 找到真正滚动的祖先(grid 自己在 .iwb-history 里滚),用于重排后恢复位置
function scrollerOf () {
var p = grid && grid.parentNode
while (p && p.nodeType === 1) {
var oy = ''
try { oy = global.getComputedStyle ? global.getComputedStyle(p).overflowY : '' } catch (e) { oy = '' }
if (p.scrollHeight > p.clientHeight + 4 && /(auto|scroll)/.test(String(oy))) return p
p = p.parentNode
}
return (grid && grid.parentNode) || null
}
function itemNode (it, seq) {
var on = !!picked[it.name]
var box = el('div', { class: 'iwb-history-item' + (count() ? ' iwb-history-managed' : '') + (on ? ' iwb-history-selected' : '') })
box.setAttribute('data-name', it.name)
// 缩略图是**异步**的(ctx.gallery.thumb 返回 Promise):先占位,拿到地址再挂 img。
// v1.2.0:加载失败自动重试 2 次(图片还没落盘/临时不可读),仍失败才显示可点击重试的占位。
box.title = it.name + '(单击单选 · Shift+单击多选 · 双击放大 · 拖到画布=参考图节点)'
@@ -464,7 +496,13 @@
if (img.complete && img.naturalWidth > 0) { img.style.display = ''; if (holder.parentNode === box) box.replaceChild(img, holder) }
else if (img.complete && !img.naturalWidth) { /* 立即失败(坏图)*/ tries++; if (tries <= 2) setTimeout(boot, 400 * tries); else { if (img.parentNode === box) box.replaceChild(holder, img); fail('缩略图加载失败(点击重试)') } }
}
boot()
// v1.8.0:缩略图按可见性懒加载(IntersectionObserver,上下预取 300px)——
// 原来一次性对全部条目发 IPC 取缩略图,列表一长首屏就慢。
box.__iwbBoot = boot
var io0 = ensureIO()
// 前 30 张仍然同步取(首屏 / e2e 需要立刻可见),其余按可见性懒加载
if (!io0 || Number(seq) < 30) boot()
else io0.observe(box)
box.addEventListener('contextmenu', function (e) { openMenu(e, it) })
var acts = el('div', { class: 'iwb-history-actions' })
function act (label, title, fn) {
@@ -510,10 +548,27 @@
function pick (name, shift) {
if (shift) { if (picked[name]) delete picked[name]; else picked[name] = 1 } else { picked = {}; picked[name] = 1 }
render()
// v1.8.0:选中只就地改 class/勾选角标 —— 原来整个网格重建,滚动条被顶回顶部,
// 而且正在双击的那张 <img> 被换掉,双击因此经常打不开。
syncSelection()
renderHead()
}
function syncSelection () {
var managed = !!count()
var boxes = grid.querySelectorAll('[data-name]')
for (var i = 0; i < boxes.length; i++) {
var b = boxes[i]
var nm = b.getAttribute('data-name')
var on = !!picked[nm]
if (b.classList.contains('iwb-history-selected') !== on) b.classList.toggle('iwb-history-selected', on)
if (b.classList.contains('iwb-history-managed') !== managed) b.classList.toggle('iwb-history-managed', managed)
var check = b.querySelector('.iwb-history-check')
if (on && !check) b.appendChild(el('span', { class: 'iwb-history-check', title: '已选中' }))
else if (!on && check && check.parentNode === b) b.removeChild(check)
}
}
function render () { renderHead(); renderGrid(); global.requestAnimationFrame(function () { try { measure() } catch (e) {} }); setTimeout(function () { try { measure() } catch (e) {} }, 150) }
function render () { var sc = scrollerOf(); var top = sc ? sc.scrollTop : 0; renderHead(); renderGrid(); if (sc) sc.scrollTop = top; global.requestAnimationFrame(function () { try { measure() } catch (e) {} }); setTimeout(function () { try { measure() } catch (e) {} }, 150) }
function load (force) {
if (force) setStatus('刷新中…')
+2 -2
View File
@@ -1,7 +1,7 @@
{
"id": "gallery",
"name": "图库",
"version": "1.7.5",
"version": "1.8.0",
"apiVersion": 1,
"main": "main.js",
"style": "styles.css",
@@ -13,7 +13,7 @@
"图库"
],
"files": {
"main.js": "fd06fd4a68ff35a584678fbcea18fd09479c27ad94f31c66bbcd092d53a67596",
"main.js": "39a93d36a9294c183136e20f90c8c8885a67ada6adc1602b334836cfcba0e15d",
"styles.css": "14d374b5d0e1461cb1ca2b125865e9c27df7c3bd79c11caa0afc52152c7d0aef"
}
}
+3 -2
View File
@@ -33,9 +33,10 @@
"dir": "gallery",
"main": "main.js",
"style": "styles.css",
"version": "1.7.5",
"version": "1.8.0",
"files": {
"main.js": "fd06fd4a68ff35a584678fbcea18fd09479c27ad94f31c66bbcd092d53a67596",
"manifest.json": "3eabd6d707e2154a955049bda895d9c4f32afecaa00df1d9b3e92c92fd4930cd",
"main.js": "39a93d36a9294c183136e20f90c8c8885a67ada6adc1602b334836cfcba0e15d",
"styles.css": "14d374b5d0e1461cb1ca2b125865e9c27df7c3bd79c11caa0afc52152c7d0aef"
}
},