diff --git a/plugins/gallery/main.js b/plugins/gallery/main.js index daeeba1..3a6ddf4 100644 --- a/plugins/gallery/main.js +++ b/plugins/gallery/main.js @@ -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/勾选角标 —— 原来整个网格重建,滚动条被顶回顶部, + // 而且正在双击的那张 被换掉,双击因此经常打不开。 + 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('刷新中…') diff --git a/plugins/gallery/manifest.json b/plugins/gallery/manifest.json index b2d31f9..90a2367 100644 --- a/plugins/gallery/manifest.json +++ b/plugins/gallery/manifest.json @@ -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" } } diff --git a/plugins/index.json b/plugins/index.json index bb97ed8..edb3fd8 100644 --- a/plugins/index.json +++ b/plugins/index.json @@ -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" } },