feat(gallery): 自动恢复(面板可见/窗口聚焦时重拉)+ 底部刷新按钮 + 坏图不留白块 v1.3.0
This commit is contained in:
1 parent
70b50dc654
commit
b904e9739e
2 files changed
+29
-4
No files matched your search
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@iwb-plugin/gallery",
|
||||
"private": true,
|
||||
"version": "1.2.1",
|
||||
"version": "1.3.0",
|
||||
"description": "图库:右栏图片墙(筛选/多选/预览/插入画布/下载/删除),数据由宿主 gallery 能力提供",
|
||||
"ibwPlugin": {
|
||||
"id": "gallery",
|
||||
|
||||
+28
-3
@@ -70,9 +70,11 @@
|
||||
var countEl = el('span', { class: 'iwb-gal-count', text: '0 / 0 张' })
|
||||
var pickedEl = el('span', { class: 'iwb-gal-sel', text: '' })
|
||||
var statusEl = el('span', { class: 'iwb-gal-status', text: '' })
|
||||
var refreshBtn = el('button', { class: 'iwb-btn iwb-btn-sm', text: '刷新', title: '重新向宿主拉取图库(图床恢复/外部改动后点这里)' })
|
||||
refreshBtn.addEventListener('click', function () { load(true) })
|
||||
var opts = el('div', { class: 'iwb-gal-opts' })
|
||||
var foot = el('div', { class: 'iwb-gal-foot' },
|
||||
el('div', { class: 'iwb-gal-foot-left' }, countEl, pickedEl, statusEl), opts)
|
||||
el('div', { class: 'iwb-gal-foot-left' }, countEl, pickedEl, statusEl, refreshBtn), opts)
|
||||
var wrap = el('div', { class: 'iwb-history-embedded' }, [head, grid, more, empty, foot])
|
||||
host.appendChild(wrap)
|
||||
|
||||
@@ -174,6 +176,8 @@
|
||||
var img = el('img', { src: url, alt: '', loading: 'lazy', draggable: 'true' })
|
||||
if (it.w && it.h) img.setAttribute('style', 'aspect-ratio:' + it.w + ' / ' + it.h)
|
||||
img.addEventListener('load', function () {
|
||||
// naturalWidth=0 说明拿到的是坏图(空 data URL / 截断文件):按失败处理,别留白块
|
||||
if (!img.naturalWidth) { tries++; if (tries <= 2) { setTimeout(boot, 400 * tries); return } if (img.parentNode === box) box.replaceChild(holder, img); fail('缩略图加载失败(点击重试)'); return }
|
||||
if (holder.parentNode === box) box.replaceChild(img, holder)
|
||||
})
|
||||
img.addEventListener('error', function () {
|
||||
@@ -195,6 +199,7 @@
|
||||
box.appendChild(img)
|
||||
// 加载成功后再替换占位(避免破图标 + 长 title 占满格子)
|
||||
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('缩略图加载失败(点击重试)') } }
|
||||
}).catch(function () { fail('缩略图加载失败(点击重试)') })
|
||||
}
|
||||
boot()
|
||||
@@ -233,10 +238,12 @@
|
||||
|
||||
function render () { renderHead(); renderGrid() }
|
||||
|
||||
function load () {
|
||||
function load (force) {
|
||||
if (force) setStatus('刷新中…')
|
||||
return Promise.resolve(ctx.gallery.list({ range: cfg.range, limit: limit })).then(function (r) {
|
||||
items = (r && r.items) || []
|
||||
total = (r && r.total) || items.length
|
||||
lastLoadAt = Date.now()
|
||||
render()
|
||||
}).catch(function () { items = []; total = 0; render() })
|
||||
}
|
||||
@@ -258,6 +265,24 @@
|
||||
if (ro) ro.observe(grid)
|
||||
measure()
|
||||
|
||||
// v1.3.0:自动恢复 —— 面板重新可见(切回 tab / 窗口聚焦 / 后台回来)且距上次拉取 >5s 时重拉一次,
|
||||
// 这样图床恢复、后台补图后不必手动刷新;同时保留底部「刷新」按钮做即时重试。
|
||||
var lastLoadAt = 0
|
||||
var autoRefresh = function () {
|
||||
if (Date.now() - lastLoadAt < 5000) return
|
||||
load()
|
||||
}
|
||||
try {
|
||||
if (typeof IntersectionObserver === 'function') {
|
||||
var io = new IntersectionObserver(function (entries) {
|
||||
for (var i = 0; i < entries.length; i++) if (entries[i].isIntersecting) autoRefresh()
|
||||
}, { threshold: 0.15 })
|
||||
io.observe(wrap)
|
||||
}
|
||||
document.addEventListener('visibilitychange', function () { if (!document.hidden) autoRefresh() })
|
||||
window.addEventListener('focus', autoRefresh)
|
||||
} catch (e) { /* 环境不支持则只保留手动刷新 */ }
|
||||
|
||||
// 先读配置,再拉列表
|
||||
Promise.resolve(ctx.data && ctx.data.read ? ctx.data.read() : {}).then(function (d) {
|
||||
if (d && d.cfg) cfg = Object.assign({}, DEF, d.cfg)
|
||||
@@ -271,7 +296,7 @@
|
||||
global.IWBPluginHost.register({
|
||||
id: 'gallery',
|
||||
name: '图库',
|
||||
version: '1.2.1',
|
||||
version: '1.3.0',
|
||||
apiVersion: 1,
|
||||
capabilities: ['panels', 'gallery'],
|
||||
data: { schemaVersion: 1, maxBytes: 65536 },
|
||||
|
||||
Reference in new issue
Block a user